Skip to content

Commit 5f59024

Browse files
authored
Refactored file opening and utc localization
1 parent 6ab294f commit 5f59024

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

pvlib/iotools/bsrn.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,17 @@ def read_bsrn(filename):
9090
# Read file and store the starting line number for each logical record (LR)
9191
line_no_dict = {}
9292
if str(filename).endswith('.gz'): # check if file is a gzipped (.gz) file
93-
with gzip.open(filename, 'rt') as f:
94-
for num, line in enumerate(f):
95-
if num == 1: # Get month and year from the 2nd line
96-
start_date = pd.Timestamp(year=int(line[7:11]),
97-
month=int(line[3:6]), day=1)
98-
if line.startswith('*'): # Find start of all logical records
99-
line_no_dict[line[2:6]] = num # key is 4 digit LR number
93+
open_func, mode = gzip.open, 'rt'
10094
else:
101-
with open(filename, 'r') as f:
102-
for num, line in enumerate(f):
103-
if num == 1: # Get month and year from the 2nd line
104-
start_date = pd.Timestamp(year=int(line[7:11]),
105-
month=int(line[3:6]), day=1)
106-
if line.startswith('*'): # Find start of all logical records
107-
line_no_dict[line[2:6]] = num
95+
open_func, mode = open, 'r'
96+
with open_func(filename, mode) as f:
97+
for num, line in enumerate(f):
98+
if num == 1: # Get month and year from the 2nd line
99+
start_date = pd.Timestamp(year=int(line[7:11]),
100+
month=int(line[3:6]), day=1,
101+
tz='UTC') # BSRN timestamps are UTC
102+
if line.startswith('*'): # Find start of all logical records
103+
line_no_dict[line[2:6]] = num # key is 4 digit LR number
108104

109105
# Determine start and end line of logical record LR0100 to be parsed
110106
start_row = line_no_dict['0100'] + 1 # Start line number
@@ -133,14 +129,9 @@ def read_bsrn(filename):
133129
data['day'] = data['day'].astype('Int64')
134130
data['minute'] = data['minute'].astype('Int64')
135131

136-
# Set datetime index and localize to UTC
132+
# Set datetime index
137133
data.index = (start_date
138134
+ pd.to_timedelta(data['day']-1, unit='d')
139135
+ pd.to_timedelta(data['minute'], unit='min'))
140136

141-
try:
142-
data.index = data.index.tz_localize('UTC') # BSRN timestamps are UTC
143-
except TypeError:
144-
pass
145-
146137
return data

0 commit comments

Comments
 (0)