Skip to content

Commit 1122896

Browse files
author
dacoex
committed
minor changes to improve structure
1 parent fe33a2f commit 1122896

File tree

3 files changed

+122
-22
lines changed

3 files changed

+122
-22
lines changed

pvlib/io/iotools.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytz
2+
# How to get a time zone from a location using
3+
# latitude and longitude coordinates?
4+
# http://stackoverflow.com/a/16086964
5+
# upstream: https://github.com/MrMinimal64/timezonefinder
6+
from timezonefinder import TimezoneFinder
7+
8+
def get_loc_latlon(lat, lon):
9+
"""Returns the timezone for a coordinate pair.
10+
"""
11+
12+
13+
tf = TimezoneFinder()
14+
tz = tf.timezone_at(lng=lon, lat=lat)
15+
16+
return tz
17+
18+
def localise_df(df_notz, tz_source_str='UTC', tz_target_str):
19+
"""
20+
localises a pandas.DataFrame (df) to the target time zone of the pvlib-Location
21+
22+
Assumes that the input df does not have a timezone
23+
24+
"""
25+
26+
tz_source_str = pytz.timezone(tz_source_str)
27+
tz_target = pytz.timezone(tz_target_str)
28+
29+
df_tz_target = df_notz.tz_convert(tz_source)
30+
31+
return df_tz_target

pvlib/io/maccrad.py

Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
# standard library imports
2+
import logging
3+
4+
# related third party imports
15
import pandas as pd
6+
import pytz
7+
8+
# local application/library specific imports
9+
from pvlib.location import Location
10+
from pvlib.io.iotools import get_loc_latlon, localise_df
211

312
# required dateconverters
413
def dtp_soda_pro_macc_rad(date):
5-
"datetime converter for MACC-RAD data and others
14+
"""
15+
datetime converter for MACC-RAD data and others
16+
"""
617
datetime_stamp = date.split('/')[0]
718

819
return datetime_stamp
9-
20+
1021
#XXX read metadata / header
1122

1223
def read_maccrad_metadata(file_csv, name='maccrad'):
@@ -39,36 +50,90 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
3950
alt_line = line
4051
alt = float(alt_line.split(':')[1])
4152
# alt = float(line.split(':')[1])
53+
if "Time reference" in line:
54+
if "Universal time (UT)" in line:
55+
tz_raw = 'UTC'
56+
else:
57+
logging.debug('No metadata on timezone found in input file')
58+
logging.debug('Assuming UTC as default timezone')
59+
tz_raw = 'UTC'
60+
61+
tz_loc = get_loc_latlon(lat, lon)
4262

43-
# How to get a time zone from a location using
44-
# latitude and longitude coordinates?
45-
# http://stackoverflow.com/a/16086964
46-
# upstream: https://github.com/MrMinimal64/timezonefinder
47-
from timezonefinder import TimezoneFinder
48-
tf = TimezoneFinder()
49-
tz = tf.timezone_at(lng=lon, lat=lat)
5063

51-
from pvlib.location import Location
5264

5365
location = Location(lat, lon, name=name, altitude=alt,
54-
tz=tz)
66+
tz=tz_loc)
67+
68+
return tz_raw, location
69+
70+
def maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True):
71+
"""Change some properties of the dataframe to be more compliant with pvlib
5572
56-
return location
73+
* localisation
74+
* column renaming
75+
76+
"""
77+
78+
if localise:
79+
# timezone localisations
80+
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw, loc.tz)
81+
# column renaming
82+
df_pvlib.index.name = 'datetime'
83+
84+
return df_pvlib
85+
86+
#def maccrad_df_to_pvlib(df):
87+
#
88+
#
89+
# pass
90+
5791

5892
#XXX read data
59-
def read_maccrad(file_csv, output='df'):
93+
def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'):
6094
"""
6195
Read MACC-RAD current format for files into a pvlib-ready dataframe
96+
97+
Parameters
98+
----------
99+
file_csv : a csv file corresponding to the reader format
100+
skiprows : skiprows as in pandas.io.read_csv
101+
The example files require skipping 40 rows.
102+
output : all / loc / df_pvlib
103+
df_raw returns only the a pandas.DataFrame using the raw data from the
104+
file (this can be helpful for debugging and comparison
105+
with restuls obtained with other programs, e.g.
106+
spreadsheet)
107+
all returns df_raw, returns a pandas.DataFrame reformatted to match the
108+
`variable naming convention <variables_style_rules>`
109+
for pvliball outputs, and a location created from
110+
the metadata in raw input file header
111+
as tuple
112+
113+
62114
"""
63-
df = pd.read_csv(file_csv, sep=';', skiprows=40, header=0,
115+
df_raw = pd.read_csv(file_csv, sep=';', skiprows=skiprows, header=0,
64116
index_col=0, parse_dates=True,
65117
date_parser=dtp_soda_pro_macc_rad)
66-
67-
if output == 'loc':
68-
loc = read_maccrad_metadata(file_csv)
69-
res = (loc, df)
118+
#TODO: add loc_name
119+
#TODO: add reformat needs loc!
120+
#TODO: add simplify output options raw or all
121+
if output == 'df_raw':
122+
res = df_raw
123+
if output == 'test':
124+
res = df_pvlib
70125
else:
71-
res = df
126+
tz_raw, loc = read_maccrad_metadata(file_csv)
127+
df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True)
128+
res = (df_raw, df_pvlib, loc)
129+
# if output == 'loc':
130+
#
131+
# res = loc, df
132+
# if output == 'all':
133+
# # not calculated outside conditional to reduce overhead of metadata
134+
# # reading if not desired
135+
# loc = read_maccrad_metadata(file_csv)
136+
# res = (df_raw, df_pvlib, loc)
72137

73138

74139
return res

pvlib/test/test_io_maccrad.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# standard library imports
12
import os
23

4+
# local application/library specific imports
35
from pvlib.io.maccrad import read_maccrad
46

57

@@ -10,14 +12,16 @@
1012
maccrad_csv_dir = os.path.join("..", "..", "..", "pvlib_data", "MACC-RAD", "carpentras")
1113
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)
1214

13-
data_maccrad = read_maccrad(maccrad_csv_path, output='loc')
15+
data_maccrad = read_maccrad(maccrad_csv_path, output='all')
16+
data_maccrad = read_maccrad(maccrad_csv_path, output='test')
1417

1518
maccrad_loc = data_maccrad[0]
1619
maccrad_df = data_maccrad[1]
1720

1821
def test_location_coord():
19-
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude, maccrad_loc.longitude,
20-
maccrad_loc.altitude)
22+
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude,
23+
maccrad_loc.longitude,
24+
maccrad_loc.altitude)
2125

2226

2327
def test_location_tz():

0 commit comments

Comments
 (0)