Skip to content

Commit fc6a2ff

Browse files
committed
* changes permitting the creation of a location object from epw metadata.
- 2 options included: 'from_epw' or 'from_tmy_or_epw', to discuss
1 parent ee6d951 commit fc6a2ff

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

pvlib/location.py

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,46 +85,79 @@ def __repr__(self):
8585
('{}: {}'.format(attr, getattr(self, attr)) for attr in attrs)))
8686

8787
@classmethod
88-
def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs):
88+
def from_tmy_or_epw(cls, weather_metadata, weather_data=None, **kwargs):
8989
"""
9090
Create an object based on a metadata
91-
dictionary from tmy2 or tmy3 data readers.
91+
dictionary from tmy2, tmy3 or epw data readers.
9292
9393
Parameters
9494
----------
95-
tmy_metadata : dict
96-
Returned from tmy.readtmy2 or tmy.readtmy3
97-
tmy_data : None or DataFrame, default None
98-
Optionally attach the TMY data to this object.
95+
weather_metadata : dict
96+
Returned from tmy.readtmy2, tmy.readtmy3 or epw.read_epw
97+
weather_data : None or DataFrame, default None
98+
Optionally attach the weather data to this object.
9999
100100
Returns
101101
-------
102102
Location object (or the child class of Location that you
103103
called this method from).
104104
"""
105-
# not complete, but hopefully you get the idea.
106-
# might need code to handle the difference between tmy2 and tmy3
107-
108-
# determine if we're dealing with TMY2 or TMY3 data
109-
tmy2 = tmy_metadata.get('City', False)
110-
111-
latitude = tmy_metadata['latitude']
112-
longitude = tmy_metadata['longitude']
105+
# determine if we're dealing with TMY2, TMY3 or data formatted
106+
# in .epw (Note that TMY2 and TMY3 can be formatted in epw sometimes)
107+
tmy2 = weather_metadata.get('City', False)
108+
tmy3 = weather_metadata.get('Name', False)
113109

114110
if tmy2:
115-
name = tmy_metadata['City']
111+
name = weather_metadata['City']
112+
elif tmy3:
113+
name = weather_metadata['Name']
116114
else:
117-
name = tmy_metadata['Name']
115+
name = weather_metadata['city']
116+
117+
latitude = weather_metadata['latitude']
118+
longitude = weather_metadata['longitude']
118119

119-
tz = tmy_metadata['TZ']
120-
altitude = tmy_metadata['altitude']
120+
tz = weather_metadata['TZ']
121+
altitude = weather_metadata['altitude']
121122

122123
new_object = cls(latitude, longitude, tz=tz, altitude=altitude,
123124
name=name, **kwargs)
124125

125126
# not sure if this should be assigned regardless of input.
126-
if tmy_data is not None:
127-
new_object.tmy_data = tmy_data
127+
if weather_data is not None:
128+
new_object.weather_data = weather_data
129+
130+
return new_object
131+
132+
@classmethod
133+
def from_epw(cls, epw_metadata, epw_data=None, **kwargs):
134+
"""
135+
Create a Location object based on a metadata
136+
dictionary from epw data readers.
137+
138+
Parameters
139+
----------
140+
epw_metadata : dict
141+
Returned from epw.read_epw
142+
epw_data : None or DataFrame, default None
143+
Optionally attach the epw data to this object.
144+
145+
Returns
146+
-------
147+
Location object (or the child class of Location that you
148+
called this method from).
149+
"""
150+
151+
latitude = epw_metadata['latitude']
152+
longitude = epw_metadata['longitude']
153+
154+
name = epw_metadata['city']
155+
156+
tz = epw_metadata['TZ']
157+
altitude = epw_metadata['altitude']
158+
159+
new_object = cls(latitude, longitude, tz=tz, altitude=altitude,
160+
name=name, **kwargs)
128161

129162
return new_object
130163

pvlib/test/test_location.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,33 @@ def test_from_tmy_3():
214214
from test_tmy import tmy3_testfile
215215
from pvlib.iotools import read_tmy3
216216
data, meta = read_tmy3(tmy3_testfile)
217-
loc = Location.from_tmy(meta, data)
217+
loc = Location.from_tmy_or_epw(meta, data)
218218
assert loc.name is not None
219219
assert loc.altitude != 0
220220
assert loc.tz != 'UTC'
221-
assert_frame_equal(loc.tmy_data, data)
221+
assert_frame_equal(loc.weather_data, data)
222222

223223

224224
def test_from_tmy_2():
225225
from test_tmy import tmy2_testfile
226226
from pvlib.iotools import read_tmy2
227227
data, meta = read_tmy2(tmy2_testfile)
228-
loc = Location.from_tmy(meta, data)
228+
loc = Location.from_tmy_or_epw(meta, data)
229229
assert loc.name is not None
230230
assert loc.altitude != 0
231231
assert loc.tz != 'UTC'
232-
assert_frame_equal(loc.tmy_data, data)
232+
assert_frame_equal(loc.weather_data, data)
233+
234+
235+
def test_from_epw():
236+
from test_epw import epw_testfile
237+
from pvlib.iotools import read_epw
238+
data, meta = read_epw(epw_testfile)
239+
loc = Location.from_tmy_or_epw(meta, data)
240+
assert loc.name is not None
241+
assert loc.altitude != 0
242+
assert loc.tz != 'UTC'
243+
assert_frame_equal(loc.weather_data, data)
233244

234245

235246
def test_get_solarposition(expected_solpos, golden_mst):

0 commit comments

Comments
 (0)