Skip to content

Commit

Permalink
add NSRDBWeatherData pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
cdeline committed Nov 29, 2023
1 parent 67f1891 commit 0c0365b
Show file tree
Hide file tree
Showing 4 changed files with 8,801 additions and 9 deletions.
33 changes: 24 additions & 9 deletions bifacial_radiance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
def _findme(lst, a): #find string match in a list. script from stackexchange
return [i for i, x in enumerate(lst) if x == a]

def _firstlist(l): #find first not-none value in a list. useful for checking multiple keys in dict
try:
return next(item for item in l if item is not None)
except StopIteration:
return None

def _missingKeyWarning(dictype, missingkey, newvalue): # prints warnings
if type(newvalue) is bool:
valueunit = ''
Expand Down Expand Up @@ -1053,13 +1059,22 @@ def _parseTimes(t, hour, coerce_year):
return t_out, coerce_year
# end _parseTimes

def _parseMetadataNSRDB(m):
# put correct keys on m = metadata dict

m['altitude'] = _firstlist([m.get('altitude'), m.get('elevation')])
m['TZ'] = _firstlist([m.get('TZ'), m.get('Time Zone'), m.get('timezone')])
m['Name'] = _firstlist([m.get('county'), f"nsrdb_{m.get('Location ID')}"])

try:
m['city'] = (m['county'] + ',' + m['state'] +
',' + m['country'])
except KeyError:
m['city'] = '-'

return m

metadata['TZ'] = metadata['timezone']
metadata['Name'] = metadata['county']
metadata['altitude'] = metadata['elevation']
metadata['city'] = (metadata['county'] + ',' + metadata['state'] +
',' + metadata['country'])
metadata = _parseMetadataNSRDB(metadata)

metdata.rename(columns={'dni': 'DNI',
'dhi': 'DHI',
Expand Down Expand Up @@ -3646,6 +3661,7 @@ def __init__(self, tmydata, metadata, label = 'right'):
self.longitude = metadata['longitude']; lon=self.longitude
self.elevation = metadata['altitude']; elev=self.elevation
self.timezone = metadata['TZ']

try:
self.city = metadata['Name'] # readepw version
except KeyError:
Expand All @@ -3655,10 +3671,9 @@ def __init__(self, tmydata, metadata, label = 'right'):
self.ghi = np.array(tmydata.GHI)
self.dhi = np.array(tmydata.DHI)
self.dni = np.array(tmydata.DNI)
try:
self.albedo = np.array(tmydata.Alb)
except AttributeError: # no TMY albedo data
self.albedo = None
self.albedo = np.array(_firstlist([tmydata.get('Alb'), tmydata.get('albedo'),
tmydata.get('Albedo')]) )
if pd.isnull(self.albedo).all(): self.albedo = None

# Try and retrieve dewpoint and pressure
try:
Expand Down
1 change: 1 addition & 0 deletions tests/nsrdb_boulder_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Source": "NSRDB", "Location ID": "149190", "City": "-", "State": "-", "Country": "-", "Time Zone": -7, "Local Time Zone": -7, "Dew Point Units": "c", "DHI Units": "w/m2", "DNI Units": "w/m2", "GHI Units": "w/m2", "Temperature Units": "c", "Pressure Units": "mbar", "Wind Direction Units": "Degrees", "Wind Speed Units": "m/s", "Surface Albedo Units": "N/A", "Version": "3.2.0", "latitude": 40.01, "longitude": -105.26, "altitude": 1636}
Loading

0 comments on commit 0c0365b

Please sign in to comment.