Skip to content

Commit a4c1d6f

Browse files
authored
Get file month/year from file instead of filename
Previously the month and year of the file were determined from the filename. This has now been changed such that the month/year is found from within the file's metadata section (second line).
1 parent db1ac24 commit a4c1d6f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pvlib/iotools/bsrn.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pandas as pd
66
import gzip
7-
import os
87

98
COL_SPECS = [(0, 3), (4, 9), (10, 16), (16, 22), (22, 27), (27, 32), (32, 39),
109
(39, 45), (45, 50), (50, 55), (55, 64), (64, 70), (70, 75)]
@@ -93,11 +92,17 @@ def read_bsrn(filename):
9392
if str(filename).endswith('.gz'): # check if file is a gzipped (.gz) file
9493
with gzip.open(filename, 'rt') as f:
9594
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)
9698
if line.startswith('*'): # Find start of all logical records
9799
line_no_dict[line[2:6]] = num # key is 4 digit LR number
98100
else:
99101
with open(filename, 'r') as f:
100102
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)
101106
if line.startswith('*'): # Find start of all logical records
102107
line_no_dict[line[2:6]] = num
103108

@@ -129,8 +134,7 @@ def read_bsrn(filename):
129134
data['minute'] = data['minute'].astype('Int64')
130135

131136
# Set datetime index and localize to UTC
132-
basename = os.path.basename(filename) # get month and year from filename
133-
data.index = (pd.to_datetime(basename[3:7], format='%m%y')
137+
data.index = (start_date
134138
+ pd.to_timedelta(data['day']-1, unit='d')
135139
+ pd.to_timedelta(data['minute'], unit='min'))
136140

0 commit comments

Comments
 (0)