Skip to content

Commit c1ddf94

Browse files
author
dacoex
committed
improvements responding to comments
responding to comments in pvlib#271 - improved metadata retrieval and location setting - added better options to output responding to comments in pvlib#270 - moved some functions to general iotools
1 parent 1122896 commit c1ddf94

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

pvlib/io/iotools.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ def get_loc_latlon(lat, lon):
1515

1616
return tz
1717

18-
def localise_df(df_notz, tz_source_str='UTC', tz_target_str):
18+
def localise_df(df_notz, tz_source_str='UTC', tz_target_str=None):
1919
"""
2020
localises a pandas.DataFrame (df) to the target time zone of the pvlib-Location
2121
2222
Assumes that the input df does not have a timezone
2323
2424
"""
2525

26-
tz_source_str = pytz.timezone(tz_source_str)
26+
tz_source = pytz.timezone(tz_source_str)
2727
tz_target = pytz.timezone(tz_target_str)
2828

29-
df_tz_target = df_notz.tz_convert(tz_source)
29+
df_tz_source = df_notz.tz_localize(tz_source)
30+
df_tz_target = df_tz_source.tz_convert(tz_target)
31+
3032

3133
return df_tz_target

pvlib/io/maccrad.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
3434
## if file is on local drive
3535
f = open(file_csv)
3636
for line in f:
37+
if "Title" in line:
38+
title = line.split(':')[1].split('(')[0].strip()
39+
name = title
3740
if "Latitude" in line:
3841
# print (line)
3942
# if line.startswith( "# Latitude"):
@@ -66,20 +69,26 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
6669
tz=tz_loc)
6770

6871
return tz_raw, location
69-
72+
73+
7074
def maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True):
7175
"""Change some properties of the dataframe to be more compliant with pvlib
7276
7377
* localisation
7478
* column renaming
79+
* setting dataframe name description according to datasource
7580
7681
"""
7782

7883
if localise:
7984
# timezone localisations
80-
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw, loc.tz)
85+
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw,
86+
tz_target_str=loc.tz)
8187
# column renaming
8288
df_pvlib.index.name = 'datetime'
89+
90+
# name the dataframe according to data source
91+
df_pvlib.df_name = loc.name
8392

8493
return df_pvlib
8594

@@ -118,13 +127,15 @@ def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'):
118127
#TODO: add loc_name
119128
#TODO: add reformat needs loc!
120129
#TODO: add simplify output options raw or all
130+
print (output)
121131
if output == 'df_raw':
122132
res = df_raw
123-
if output == 'test':
124-
res = df_pvlib
125-
else:
133+
if output == 'all':
126134
tz_raw, loc = read_maccrad_metadata(file_csv)
135+
loc.name = (loc.name + ' @ ' + 'lat (deg. N), lon (deg. E): ' +
136+
str(loc.latitude) + ', ' + str(loc.longitude))
127137
df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True)
138+
# res = df_pvlib
128139
res = (df_raw, df_pvlib, loc)
129140
# if output == 'loc':
130141
#

pvlib/test/test_io_maccrad.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)
1414

1515
data_maccrad = read_maccrad(maccrad_csv_path, output='all')
16-
data_maccrad = read_maccrad(maccrad_csv_path, output='test')
16+
#data_maccrad = read_maccrad(maccrad_csv_path, output='test')
17+
18+
maccrad_raw = data_maccrad[0]
19+
maccrad_pvlib = data_maccrad[1]
20+
maccrad_loc = data_maccrad[2]
1721

18-
maccrad_loc = data_maccrad[0]
19-
maccrad_df = data_maccrad[1]
2022

2123
def test_location_coord():
2224
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude,
@@ -27,12 +29,14 @@ def test_location_coord():
2729
def test_location_tz():
2830
assert 'Europe/Paris' == maccrad_loc.tz
2931

32+
def test_tz_convert():
33+
assert maccrad_pvlib.index.tzinfo.zone == maccrad_loc.tz
3034

3135
def test_maccrad_recolumn():
32-
assert 'Clear sky GHI' in maccrad_df.columns
36+
assert 'Clear sky GHI' in maccrad_pvlib.columns
3337

3438
def test_maccrad_norecolumn():
35-
assert 'Clear sky GHI' in maccrad_df.columns
39+
assert 'Clear sky GHI' in maccrad_pvlib.columns
3640

3741
def test_maccrad_coerce_year():
3842
coerce_year = 2010

0 commit comments

Comments
 (0)