Skip to content

Commit 89e352b

Browse files
committed
move metadata parsing out of context manager
1 parent e9c09d4 commit 89e352b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pvlib/iotools/psm4.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,16 +741,7 @@ def read_nsrdb_psm4(filename, map_variables=True):
741741
with context as fbuf:
742742
# The first 2 lines of the response are headers with metadata
743743
metadata_fields = fbuf.readline().split(',')
744-
metadata_fields[-1] = metadata_fields[-1].strip() # trailing newline
745744
metadata_values = fbuf.readline().split(',')
746-
metadata_values[-1] = metadata_values[-1].strip() # trailing newline
747-
metadata = dict(zip(metadata_fields, metadata_values))
748-
# the response is all strings, so set some metadata types to numbers
749-
metadata['Local Time Zone'] = int(metadata['Local Time Zone'])
750-
metadata['Time Zone'] = int(metadata['Time Zone'])
751-
metadata['Latitude'] = float(metadata['Latitude'])
752-
metadata['Longitude'] = float(metadata['Longitude'])
753-
metadata['Elevation'] = int(metadata['Elevation'])
754745
# get the column names so we can set the dtypes
755746
columns = fbuf.readline().split(',')
756747
columns[-1] = columns[-1].strip() # strip trailing newline
@@ -765,6 +756,16 @@ def read_nsrdb_psm4(filename, map_variables=True):
765756
fbuf, header=None, names=columns, usecols=columns, dtype=dtypes,
766757
delimiter=',', lineterminator='\n') # skip carriage returns \r
767758

759+
metadata_fields[-1] = metadata_fields[-1].strip() # trailing newline
760+
metadata_values[-1] = metadata_values[-1].strip() # trailing newline
761+
metadata = dict(zip(metadata_fields, metadata_values))
762+
# the response is all strings, so set some metadata types to numbers
763+
metadata['Local Time Zone'] = int(metadata['Local Time Zone'])
764+
metadata['Time Zone'] = int(metadata['Time Zone'])
765+
metadata['Latitude'] = float(metadata['Latitude'])
766+
metadata['Longitude'] = float(metadata['Longitude'])
767+
metadata['Elevation'] = int(metadata['Elevation'])
768+
768769
# the response 1st 5 columns are a date vector, convert to datetime
769770
dtidx = pd.to_datetime(data[['Year', 'Month', 'Day', 'Hour', 'Minute']])
770771
# in USA all timezones are integers

0 commit comments

Comments
 (0)