Skip to content

Add map_variables parameter to read_tmy3 function #1623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0e60277
Added map_variables param in read_tmy3 func
Dec 21, 2022
a8977fa
solved failing tests
Dec 21, 2022
054dd35
solved failing tests
Dec 22, 2022
3c7f7f6
solving stickerly test fail
Dec 22, 2022
dc6399d
solving stickerly test fail
Dec 23, 2022
773b94e
solving stickerly test fail
Dec 23, 2022
df97faf
added variable_map
Jan 21, 2023
2eee8ce
resolving formatting issues
Jan 21, 2023
7a86c55
Update pvlib/iotools/tmy.py
ooprathamm Jan 22, 2023
7013739
Update pvlib/iotools/tmy.py
ooprathamm Jan 22, 2023
16b3986
Update pvlib/iotools/tmy.py
ooprathamm Jan 22, 2023
5210407
Update pvlib/iotools/tmy.py
ooprathamm Jan 22, 2023
68b54fe
Added tests to map_variables and check depreciation warning
Jan 22, 2023
2ccae9e
resolving formatting issues
Jan 22, 2023
1ee7ff0
resolving formatting issues
Jan 22, 2023
98e53b3
Adding more test cases
Jan 24, 2023
8e5c4d7
Merge branch 'pvlib:main' into main
ooprathamm Jan 24, 2023
96a30a6
updated VARIABLE_MAP
Jan 26, 2023
9a69f12
updates DOIs in tmy.py
Jan 26, 2023
850a798
Merge branch 'pvlib:main' into main
ooprathamm Feb 2, 2023
1e86046
Final changes as requested
Feb 2, 2023
0244732
Fix stickler
AdamRJensen Feb 2, 2023
bccaf02
Update variables_style_rules.csv
AdamRJensen Feb 2, 2023
c4e4417
Add missing map_variables=False
AdamRJensen Feb 2, 2023
b91408a
Add whatsnew deprecation entry
AdamRJensen Feb 2, 2023
94383e9
Apply suggestions from code review
AdamRJensen Feb 7, 2023
2d02f4d
Merge branch 'main' into main
AdamRJensen Feb 7, 2023
c6ebd9e
Add separate if statement for warning
AdamRJensen Feb 7, 2023
616cc76
removed wrong whatsnew entry
Feb 9, 2023
cd9cc86
Update pvlib/tests/iotools/test_tmy.py
ooprathamm Feb 12, 2023
da591f5
Update warning messages according to table
AdamRJensen May 17, 2023
13b8782
Merge remote-tracking branch 'upstream/main' into pr/1623
AdamRJensen May 17, 2023
8f9c788
Update whatsnew
AdamRJensen May 17, 2023
5661f71
Set recolumn=None
AdamRJensen May 17, 2023
a3e2016
Update tests
AdamRJensen May 17, 2023
d0d1644
Update pvlib/iotools/tmy.py
AdamRJensen May 22, 2023
b080c76
Update pvlib/iotools/tmy.py
AdamRJensen May 22, 2023
da131ad
Update docs/sphinx/source/whatsnew/v0.9.6.rst
AdamRJensen May 22, 2023
828b994
Implement updated error message logic from kansersolar
AdamRJensen May 22, 2023
737f5d6
Merge branch 'main' of https://github.com/ooprathamm/pvlib-python int…
AdamRJensen May 22, 2023
1f31390
Clean up deprecation warnings
AdamRJensen May 22, 2023
b720399
Merge branch 'main' into main
AdamRJensen May 22, 2023
98e7891
Fix clearsky.rst
AdamRJensen May 22, 2023
d0c5309
Merge branch 'main' of https://github.com/ooprathamm/pvlib-python int…
AdamRJensen May 22, 2023
24e90d7
Update aod variable mapping
AdamRJensen May 22, 2023
b4bf004
Fix stickler
AdamRJensen May 23, 2023
143f9ac
Update tmy3 variables names in field,description table
AdamRJensen May 23, 2023
5d2ef3a
Update table
AdamRJensen May 24, 2023
fe2f343
More table stuff
AdamRJensen May 24, 2023
35d2d54
Table stuff galore
AdamRJensen May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/sphinx/source/whatsnew/v0.9.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Deprecations

Enhancements
~~~~~~~~~~~~

* Added variable mapping of columns when reading TMY3 dataframe using the
``read_tmy3`` function.
(:issue:`1517`, :pull:`1623`)

Bug fixes
~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions pvlib/data/variables_style_rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dni_extra;direct normal irradiance at top of atmosphere (extraterrestrial)
dhi;diffuse horizontal irradiance
bhi;beam/direct horizontal irradiance
ghi;global horizontal irradiance
ghi_extra;Extraterrestrial horizontal radiation recv’d during 60 minutes prior to timestamp
gri;ground-reflected irradiance
aoi;angle of incidence between :math:`90\deg` and :math:`90\deg`
aoi_projection;cos(aoi)
Expand Down Expand Up @@ -49,3 +50,5 @@ pac, ac; ac powe.
eta_inv; inverter efficiency
eta_inv_ref; reference inverter efficiency
eta_inv_nom; nominal inverter efficiency
albedo;The ratio of reflected solar irradiance to global horizontal irradiance, unitless
precipitable_water;Total precipitable water contained in a column of unit cross section from earth to top of atmosphere
44 changes: 38 additions & 6 deletions pvlib/iotools/tmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@
import datetime
import re
import pandas as pd


def read_tmy3(filename, coerce_year=None, recolumn=True):
import warnings
from pvlib._deprecation import pvlibDeprecationWarning

# Dictionary mapping TMY3 names to pvlib names
VARIABLE_MAP = {
'GHI (W/m^2)': 'ghi',
'ETR (W/m^2)': 'ghi_extra',
'DNI (W/m^2)': 'dni',
'ETRN (W/m^2)': 'dni_extra',
'DHI (W/m^2)': 'dhi',
'Pressure (mbar)': 'pressure',
'Wdir (degrees)': 'wind_direction',
'Wspd (m/s)': 'wind_speed',
'Dry-bulb (C)': 'temp_air',
'Dew-point (C)': 'temp_dew',
'RHum (%)': 'relative_humidity',
'Alb (unitless)': 'albedo',
'Pwat (cm)': 'precipitable_water'
}


def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=True):
"""Read a TMY3 file into a pandas dataframe.

Note that values contained in the metadata dictionary are unchanged
Expand All @@ -24,6 +43,9 @@ def read_tmy3(filename, coerce_year=None, recolumn=True):
If supplied, the year of the index will be set to `coerce_year`, except
for the last index value which will be set to the *next* year so that
the index increases monotonically.
map_variables : bool, default None
When True, renames columns of the DataFrame to pvlib variable names
where applicable. See variable :const:`VARIABLE_MAP`.
recolumn : bool, default True
If ``True``, apply standard names to TMY3 columns. Typically this
results in stripping the units from the column name.
Expand Down Expand Up @@ -152,8 +174,10 @@ def read_tmy3(filename, coerce_year=None, recolumn=True):
----------
.. [1] Wilcox, S and Marion, W. "Users Manual for TMY3 Data Sets".
NREL/TP-581-43156, Revised May 2008.
:doi:`10.2172/928611`
.. [2] Wilcox, S. (2007). National Solar Radiation Database 1991 2005
Update: Users Manual. 472 pp.; NREL Report No. TP-581-41364.
:doi:`10.2172/901864`
.. [3] `SolarAnywhere file formats
<https://www.solaranywhere.com/support/historical-data/file-formats/>`_
""" # noqa: E501
Expand Down Expand Up @@ -198,9 +222,17 @@ def read_tmy3(filename, coerce_year=None, recolumn=True):
# NOTE: as of pvlib-0.6.3, min req is pandas-0.18.1, so pd.to_timedelta
# unit must be in (D,h,m,s,ms,us,ns), but pandas>=0.24 allows unit='hour'
data.index = data_ymd + pd.to_timedelta(shifted_hour, unit='h')

if recolumn:
data = _recolumn(data) # rename to standard column names
if map_variables is None:
if recolumn:
data = _recolumn(data) # rename to standard column names
warnings.warn(
'TMY3 variable names will be renamed to pvlib conventions by '
'default starting in pvlib 0.11.0. Specify map_variables=True '
'to enable that behavior now, or specify map_variables=False '
'to hide this warning.', pvlibDeprecationWarning)
map_variables = False
if map_variables:
data = data.rename(columns=VARIABLE_MAP)

data = data.tz_localize(int(meta['TZ'] * 3600))

Expand Down
35 changes: 29 additions & 6 deletions pvlib/tests/iotools/test_tmy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pandas as pd
from pvlib.iotools import tmy
from pvlib._deprecation import pvlibDeprecationWarning
from ..conftest import DATA_DIR
import pytest

Expand All @@ -16,7 +17,7 @@


def test_read_tmy3():
tmy.read_tmy3(TMY3_TESTFILE)
tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)


def test_read_tmy3_recolumn():
Expand All @@ -25,20 +26,42 @@ def test_read_tmy3_recolumn():


def test_read_tmy3_norecolumn():
data, _ = tmy.read_tmy3(TMY3_TESTFILE, recolumn=False)
data, _ = tmy.read_tmy3(TMY3_TESTFILE, recolumn=False, map_variables=False)
assert 'GHI source' in data.columns


def test_read_tmy3_map_variables():
data, meta = tmy.read_tmy3(TMY3_TESTFILE, map_variables=True)
assert 'ghi' in data.columns
assert 'dni' in data.columns
assert 'dhi' in data.columns
assert 'pressure' in data.columns
assert 'wind_direction' in data.columns
assert 'wind_speed' in data.columns
assert 'temp_air' in data.columns
assert 'temp_dew' in data.columns
assert 'relative_humidity' in data.columns
assert 'albedo' in data.columns
assert 'ghi_extra' in data.columns
assert 'dni_extra' in data.columns
assert 'precipitable_water' in data.columns


def test_read_tmy3_map_variables_deprecating_warning():
with pytest.warns(pvlibDeprecationWarning, match='names will be renamed'):
data, meta = tmy.read_tmy3(TMY3_TESTFILE)


def test_read_tmy3_coerce_year():
coerce_year = 1987
data, _ = tmy.read_tmy3(TMY3_TESTFILE, coerce_year=coerce_year)
data, _ = tmy.read_tmy3(TMY3_TESTFILE, coerce_year=coerce_year, map_variables=False)
assert (data.index[:-1].year == 1987).all()
assert data.index[-1].year == 1988


def test_read_tmy3_no_coerce_year():
coerce_year = None
data, _ = tmy.read_tmy3(TMY3_TESTFILE, coerce_year=coerce_year)
data, _ = tmy.read_tmy3(TMY3_TESTFILE, coerce_year=coerce_year, map_variables=False)
assert 1997 and 1999 in data.index.year
assert data.index[-2] == pd.Timestamp('1998-12-31 23:00:00-09:00')
assert data.index[-1] == pd.Timestamp('1999-01-01 00:00:00-09:00')
Expand All @@ -50,7 +73,7 @@ def test_read_tmy2():

def test_gh865_read_tmy3_feb_leapyear_hr24():
"""correctly parse the 24th hour if the tmy3 file has a leap year in feb"""
data, meta = read_tmy3(TMY3_FEB_LEAPYEAR)
data, meta = read_tmy3(TMY3_FEB_LEAPYEAR, map_variables=False)
# just to be safe, make sure this _IS_ the Greensboro file
greensboro = {
'USAF': 723170,
Expand Down Expand Up @@ -87,7 +110,7 @@ def test_solaranywhere_tmy3(solaranywhere_index):
# The SolarAnywhere TMY3 format specifies midnight as 00:00 whereas the
# NREL TMY3 format utilizes 24:00. The SolarAnywhere file is therefore
# included to test files with 00:00 timestamps are parsed correctly
data, meta = tmy.read_tmy3(TMY3_SOLARANYWHERE)
data, meta = tmy.read_tmy3(TMY3_SOLARANYWHERE,map_variables=False)
pd.testing.assert_index_equal(data.index, solaranywhere_index)
assert meta['USAF'] == 0
assert meta['Name'] == 'Burlington United States'
Expand Down