Skip to content

Commit a8b85c3

Browse files
tylunelcwhanse
authored andcommitted
Location object creation from epw metadata (#821)
* * function location.Location.from_epw added. It permits to create a Location object from metadata of a epw file, typically retrieved with epw.read_epw(). * - Apply renaming suggestions with metadata and data
1 parent 9635a40 commit a8b85c3

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

docs/sphinx/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ in some files.
376376
:toctree: generated/
377377

378378
location.Location.from_tmy
379+
location.Location.from_epw
379380

380381

381382
TMY

docs/sphinx/source/whatsnew/v0.7.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Enhancements
140140
computing reductions in variability due to a spatially distributed plant.
141141
* Created one new incidence angle modifier (IAM) function for diffuse irradiance:
142142
:py:func:`pvlib.iam.martin_ruiz_diffuse`. (:issue:`751`)
143+
* Add :py:meth:`~pvlib.location.Location.from_epw`, a method to create a Location
144+
object from epw metadata, typically coming from `pvlib.iotools.epw.read_epw`.
143145

144146
Bug fixes
145147
~~~~~~~~~

pvlib/location.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,42 @@ def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs):
125125
# not sure if this should be assigned regardless of input.
126126
if tmy_data is not None:
127127
new_object.tmy_data = tmy_data
128+
new_object.weather = tmy_data
129+
130+
return new_object
131+
132+
@classmethod
133+
def from_epw(cls, metadata, data=None, **kwargs):
134+
"""
135+
Create a Location object based on a metadata
136+
dictionary from epw data readers.
137+
138+
Parameters
139+
----------
140+
metadata : dict
141+
Returned from epw.read_epw
142+
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 = metadata['latitude']
152+
longitude = metadata['longitude']
153+
154+
name = metadata['city']
155+
156+
tz = metadata['TZ']
157+
altitude = metadata['altitude']
158+
159+
new_object = cls(latitude, longitude, tz=tz, altitude=altitude,
160+
name=name, **kwargs)
161+
162+
if data is not None:
163+
new_object.weather = data
128164

129165
return new_object
130166

pvlib/test/test_location.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_from_tmy_3():
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)
222222

223223

224224
def test_from_tmy_2():
@@ -229,7 +229,18 @@ def test_from_tmy_2():
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)
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_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)
233244

234245

235246
def test_get_solarposition(expected_solpos, golden_mst):

0 commit comments

Comments
 (0)