From 7af79b1814a2459d5705b994cb5410ed637ae60e Mon Sep 17 00:00:00 2001 From: cdeline Date: Fri, 23 Aug 2024 15:00:49 -0600 Subject: [PATCH 1/4] Enable coerce_year=None. Fixes #526 --- bifacial_radiance/main.py | 35 +++++++++++-------------- docs/sphinx/source/whatsnew/pending.rst | 1 + 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/bifacial_radiance/main.py b/bifacial_radiance/main.py index 702fa6ae..b4c167e0 100644 --- a/bifacial_radiance/main.py +++ b/bifacial_radiance/main.py @@ -981,8 +981,7 @@ def _saveTempTMY(self, tmydata, filename=None, starttime=None, endtime=None, if filename is None: filename = 'temp.csv' - - + gencumskydata = None gencumdict = None if len(tmydata) == 8760: @@ -991,24 +990,22 @@ def _saveTempTMY(self, tmydata, filename=None, starttime=None, endtime=None, " temporary weather files in EPW folder.") if coerce_year is None and starttime is not None: coerce_year = starttime.year - # SILVANA: If user doesn't pass starttime, and doesn't select - # coerce_year, then do we really need to coerce it? - elif coerce_year is None: + + elif coerce_year is None and len(tmydata.index[:-1].year.unique())>1: coerce_year = 2021 - print(f"Coercing year to {coerce_year}") - #with warnings.catch_warnings(): - # warnings.simplefilter("ignore") # can't get rid of vectorized - #tmydata.index.values[:] = tmydata.index[:] + pd.DateOffset(year=(coerce_year)) - #tmydata.index.values[-1] = tmydata.index[-1] + pd.DateOffset(year=(coerce_year+1)) - tz = tmydata.index.tz - year_vector = np.full(shape=tmydata.__len__(), fill_value=coerce_year) - year_vector[-1] = coerce_year+1 - tmydata.index = pd.to_datetime({ - 'year': year_vector, - 'month': tmydata.index.month, - 'day': tmydata.index.day, - 'hour': tmydata.index.hour}) - tmydata = tmydata.tz_localize(tz) + + if coerce_year: + print(f"Coercing year to {coerce_year}") + tz = tmydata.index.tz + year_vector = np.full(shape=len(tmydata), fill_value=coerce_year) + year_vector[-1] = coerce_year+1 + tmydata.index = pd.to_datetime({ + 'year': year_vector, + 'month': tmydata.index.month, + 'day': tmydata.index.day, + 'hour': tmydata.index.hour}) + + tmydata = tmydata.tz_localize(tz) diff --git a/docs/sphinx/source/whatsnew/pending.rst b/docs/sphinx/source/whatsnew/pending.rst index df27647f..c3bf800e 100644 --- a/docs/sphinx/source/whatsnew/pending.rst +++ b/docs/sphinx/source/whatsnew/pending.rst @@ -25,6 +25,7 @@ Bug fixes * Fixed bug in :func:`bifacial_radiance.mismatch.mismatch_fit3` where the function was not returning the correct values. It has also been deprecated in favour of :func:`bifacial_radiance.mismatch.mismatch_fit2` which has a greater agreement with anual energy yield data (:issue:`520`) * Updated Github Actions to use Node20: checkout@v4, setup-python@v5, coactions/setup-xvfb, setup-buildx-action@v3 (:pull:`517`) * Fix PerformanceWarning and SettingWithCopyWarning (:issue:`515`) +* Enable `coerce_year`=None if the TMYfile is all the same year (:issue:`526`) Documentation ~~~~~~~~~~~~~~ From 673f678605d783ae8c2d4fa13dd831d8f7420eb7 Mon Sep 17 00:00:00 2001 From: cdeline Date: Fri, 23 Aug 2024 15:34:02 -0600 Subject: [PATCH 2/4] Suppress new warnings from pvlib v0.11.0 tmy3 import. --- bifacial_radiance/main.py | 12 ++++++++---- requirements.txt | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bifacial_radiance/main.py b/bifacial_radiance/main.py index b4c167e0..f48e12fe 100644 --- a/bifacial_radiance/main.py +++ b/bifacial_radiance/main.py @@ -1174,10 +1174,14 @@ def _convertTMYdate(data, meta): import pvlib - #(tmydata, metadata) = pvlib.tmy.readtmy3(filename=tmyfile) #pvlib<=0.6 - (tmydata, metadata) = pvlib.iotools.tmy.read_tmy3(filename=tmyfile, - coerce_year=coerce_year) + try: + (tmydata, metadata) = pvlib.iotools.tmy.read_tmy3(filename=tmyfile, + coerce_year=coerce_year, + map_variables=False) + except TypeError: # pvlib < 0.10 + (tmydata, metadata) = pvlib.iotools.tmy.read_tmy3(filename=tmyfile, + coerce_year=coerce_year) try: tmydata = _convertTMYdate(tmydata, metadata) @@ -1221,7 +1225,7 @@ def _readEPW(self, epwfile=None, label = 'right', coerce_year=None): #(tmydata, metadata) = readepw(epwfile) # (tmydata, metadata) = pvlib.iotools.epw.read_epw(epwfile, coerce_year=coerce_year) #pvlib>0.6.1 - #pvlib uses -1hr offset that needs to be un-done. Why did they do this? + #pvlib uses -1hr offset that needs to be un-done. tmydata.index = tmydata.index+pd.Timedelta(hours=1) # rename different field parameters to match output from diff --git a/requirements.txt b/requirements.txt index 2556d7b5..fa3d21bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ coverage==7.2.1 cycler==0.11.0 idna==3.4 importlib-metadata==6.0.0 -ipython==8.10.0 +ipython==8.13.0 kiwisolver==1.4.4 matplotlib==3.5.1 more-itertools==9.1.0 From a75b26f781fd215de96f85d50c3c4e39256722b7 Mon Sep 17 00:00:00 2001 From: cdeline Date: Fri, 23 Aug 2024 15:48:38 -0600 Subject: [PATCH 3/4] fix some deprecation warnings --- bifacial_radiance/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bifacial_radiance/main.py b/bifacial_radiance/main.py index f48e12fe..2bc86a1a 100644 --- a/bifacial_radiance/main.py +++ b/bifacial_radiance/main.py @@ -265,9 +265,9 @@ def _subhourlydatatoGencumskyformat(gencumskydata, label='right'): #Resample to hourly. Gencumsky wants right-labeled data. try: - gencumskydata = gencumskydata.resample('60T', closed='right', label='right').mean() + gencumskydata = gencumskydata.resample('60min', closed='right', label='right').mean() except TypeError: # Pandas 2.0 error - gencumskydata = gencumskydata.resample('60T', closed='right', label='right').mean(numeric_only=True) + gencumskydata = gencumskydata.resample('60min', closed='right', label='right').mean(numeric_only=True) if label == 'left': #switch from left to right labeled by adding an hour gencumskydata.index = gencumskydata.index + pd.to_timedelta('1H') @@ -294,7 +294,7 @@ def _subhourlydatatoGencumskyformat(gencumskydata, label='right'): gencumskydata.loc[padend]=0 gencumskydata=gencumskydata.sort_index() # Fill empty timestamps with zeros - gencumskydata = gencumskydata.resample('60T').asfreq().fillna(0) + gencumskydata = gencumskydata.resample('60min').asfreq().fillna(0) # Mask leap year leapmask = ~(_is_leap_and_29Feb(gencumskydata)) gencumskydata = gencumskydata[leapmask] From 83e95447cb1a6afdc3d4e5f56d350e287b095a73 Mon Sep 17 00:00:00 2001 From: cdeline Date: Fri, 23 Aug 2024 16:01:27 -0600 Subject: [PATCH 4/4] change pvlib v0.11 column names for read_tmy3 --- bifacial_radiance/main.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bifacial_radiance/main.py b/bifacial_radiance/main.py index 2bc86a1a..9e47c10b 100644 --- a/bifacial_radiance/main.py +++ b/bifacial_radiance/main.py @@ -1178,7 +1178,7 @@ def _convertTMYdate(data, meta): try: (tmydata, metadata) = pvlib.iotools.tmy.read_tmy3(filename=tmyfile, coerce_year=coerce_year, - map_variables=False) + map_variables=True) except TypeError: # pvlib < 0.10 (tmydata, metadata) = pvlib.iotools.tmy.read_tmy3(filename=tmyfile, coerce_year=coerce_year) @@ -1187,7 +1187,14 @@ def _convertTMYdate(data, meta): tmydata = _convertTMYdate(tmydata, metadata) except KeyError: print('PVLib >= 0.8.0 is required for sub-hourly data input') - + + tmydata.rename(columns={'dni':'DNI', + 'dhi':'DHI', + 'temp_air':'DryBulb', + 'wind_speed':'Wspd', + 'ghi':'GHI', + 'albedo':'Alb' + }, inplace=True) #as of v0.11, PVLib changed tmy3 column names.. return tmydata, metadata