diff --git a/docs/sphinx/source/api.rst b/docs/sphinx/source/api.rst index 7241f78f4b..fff7e1b14c 100644 --- a/docs/sphinx/source/api.rst +++ b/docs/sphinx/source/api.rst @@ -376,6 +376,7 @@ in some files. :toctree: generated/ location.Location.from_tmy + location.Location.from_epw TMY diff --git a/docs/sphinx/source/whatsnew/v0.7.0.rst b/docs/sphinx/source/whatsnew/v0.7.0.rst index 613b4588ed..ea5f226aea 100644 --- a/docs/sphinx/source/whatsnew/v0.7.0.rst +++ b/docs/sphinx/source/whatsnew/v0.7.0.rst @@ -123,6 +123,8 @@ Enhancements * Add `timeout` to :py:func:`pvlib.iotools.get_psm3`. * Created one new incidence angle modifier (IAM) function for diffuse irradiance: :py:func:`pvlib.iam.martin_ruiz_diffuse`. (:issue:`751`) +* Add :py:meth:`~pvlib.location.Location.from_epw`, a method to create a Location + object from epw metadata, typically coming from `pvlib.iotools.epw.read_epw`. Bug fixes ~~~~~~~~~ diff --git a/pvlib/location.py b/pvlib/location.py index 68b2062907..95029ca166 100644 --- a/pvlib/location.py +++ b/pvlib/location.py @@ -125,6 +125,42 @@ def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs): # not sure if this should be assigned regardless of input. if tmy_data is not None: new_object.tmy_data = tmy_data + new_object.weather = tmy_data + + return new_object + + @classmethod + def from_epw(cls, metadata, data=None, **kwargs): + """ + Create a Location object based on a metadata + dictionary from epw data readers. + + Parameters + ---------- + metadata : dict + Returned from epw.read_epw + data : None or DataFrame, default None + Optionally attach the epw data to this object. + + Returns + ------- + Location object (or the child class of Location that you + called this method from). + """ + + latitude = metadata['latitude'] + longitude = metadata['longitude'] + + name = metadata['city'] + + tz = metadata['TZ'] + altitude = metadata['altitude'] + + new_object = cls(latitude, longitude, tz=tz, altitude=altitude, + name=name, **kwargs) + + if data is not None: + new_object.weather = data return new_object diff --git a/pvlib/test/test_location.py b/pvlib/test/test_location.py index db32ac5970..d9503aefd8 100644 --- a/pvlib/test/test_location.py +++ b/pvlib/test/test_location.py @@ -218,7 +218,7 @@ def test_from_tmy_3(): assert loc.name is not None assert loc.altitude != 0 assert loc.tz != 'UTC' - assert_frame_equal(loc.tmy_data, data) + assert_frame_equal(loc.weather, data) def test_from_tmy_2(): @@ -229,7 +229,18 @@ def test_from_tmy_2(): assert loc.name is not None assert loc.altitude != 0 assert loc.tz != 'UTC' - assert_frame_equal(loc.tmy_data, data) + assert_frame_equal(loc.weather, data) + + +def test_from_epw(): + from test_epw import epw_testfile + from pvlib.iotools import read_epw + data, meta = read_epw(epw_testfile) + loc = Location.from_epw(meta, data) + assert loc.name is not None + assert loc.altitude != 0 + assert loc.tz != 'UTC' + assert_frame_equal(loc.weather, data) def test_get_solarposition(expected_solpos, golden_mst):