You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clearsky functions handling tz-aware datetimes as tz-naive, contrary to user guide #2054
Describe the bug
Both of the functions clearsky.lookup_linke_turbidity and irradiance.get_extra_radiation are supposed to be tz-aware in accordance with the user guide, but take the tz-aware inputs as tz-naive in their implementations and return incorrect results.
Another functions using these two functions may also have been affected by the errors; i.e. Location.get_clearsky, clearsky.ineichen, or clearsky.simplified_solis.
To Reproduce
Refer to my comprehensive analysis on the .ipynb file simulated in the same way as the user guide.
It seems too big for the browser to render because it shows me only this message: An error occurred.
In a similar situation, please download the file or clone my repository to see the content.
Expected behavior
There should be no items searched on the file by the keyword rows , each of which means errors subject to timezones.
Screenshots
For instance, on the user guide, it prints: climatological linke_turbidity = 3.329496820286459.
However, after converting the local timezone to utc, it prints on my analysis above another value: climatological linke_turbidity = 3.3247187176482633.
changed the title [-]Misuses of tz-naive clearsky functions on the user guide[/-][+]Misused tz-naive clearsky functions on the user guide[/+]on May 19, 2024
I'm not quite following. You write "Two functions clearsky.lookup_linke_turbidity and irradiance.get_extra_radiation is tz-naive but has been used as if they were tz-aware" but in the clear-sky user guide, the times input is localized
tus = Location(32.2, -111, 'US/Arizona', 700, 'Tucson')
times = pd.date_range(start='2016-07-01', end='2016-07-04', freq='1min', tz=tus.tz)
Yes, that localized times is a tz-aware input which is not a problem, but when it is given to the two functions they return unexpected results and this is the problem. That's why I said the functions are not tz-aware but has been misused as if they were tz-aware, and we are supposed to fix them to be tz-aware.
For instance, try converting the timezone of the times input using a common tz-aware function tz_convert and you can see the difference. This example was copied from the .ipynb file I mentioned above:
tus=Location(32.2, -111, 'US/Arizona', 700, 'Tucson')
times=pd.date_range(start='2016-07-01', end='2016-07-04', freq='1min', tz=tus.tz)
cs=tus.get_clearsky(times) # ineichen with climatology table by defaultcs_utc=tus.get_clearsky(times.tz_convert('utc')).tz_convert(tus.tz) # ineichen with climatology table by defaultassertcs.index.equals(cs_utc.index)
assertnot ((cs<0).any().any() or (cs_utc<0).any().any())
print(len(cs))
df=cs.join(cs_utc, how='outer', rsuffix='_utc')[~cs.fillna(-1).eq(cs_utc.fillna(-1)).all(axis='columns')]
print(df.index.hour.unique(), df.index.minute.nunique())
df
Does that difference conform to an expected behavior? I think cs_utc is the more accurate result than cs on the user guide.
On the other hand, you can also see on the user guide another times input as below which is tz-naive, as well as the tz-aware or localized one you mentioned. Both tz-naive and tz-aware times inputs are used without distiction on the user guide, but it is highly likely that they have been misused because merely preprocessing the inputs by the well-known tz-aware function tz_convert makes the functions result another values.
Is this an expected behavior? Isn't turbidity_h right and turbidity_h_kst wrong? I think ideally the results should be the same as turbidity_h, and a different turbidity_h_kst is a bug, just like #237 mentioned above.
To summarize, the user guide has considered those functions as if tz-aware, where any tz_convert of the input may not change their results. However, you can easily find it does indeed as in my .ipynb file, which means the functions are tz-naive against our expectations and should be fixed into tz-aware like in #2055.
changed the title [-]Misused tz-naive clearsky functions on the user guide[/-][+]Fix clearsky functions handling tz-aware datetimes as tz-naive contrary to user guide[/+]on May 25, 2024
changed the title [-]Fix clearsky functions handling tz-aware datetimes as tz-naive contrary to user guide[/-][+]Clearsky functions handling tz-aware datetimes as tz-naive, contrary to user guide[/+]on May 25, 2024
@yhkee0404 aha, I think I partly understand what's going on here. The Linke turbidity data distributed with pvlib are gridded (location), monthly values. These values (from an outside source) do not have datetime stamps, only a month. pvlib (somewhat arbitrarily) assigns these values to the middle day of each month so that a daily value can be determined by interpolation. That machinery is not shown to the user; it happens here:
The examples you provide are comparing the interpolated turbidity at the same location but localized to different timezones. The interpolation is being done using time.dayofyear. It looks to me that the dayofyear function may operating in local time, because the day of year is changed at midnight local, not midnight UTC.
Arguably, pandas had to make a decision how to return a day of year for each location, so I don't think this is a fault in pandas - its behaving as intended (I assume).
So: if we want Linke turbidity at a location to be consistent with equal timestamps, we need to work under the hood in pvlib to fix the interpolation.
import numpy as np
import pandas as pd
from pvlib.location import Location
tus = Location(32.2, -111, 'US/Arizona', 700, 'Tucson')
times = pd.date_range(start='2016-07-01', end='2016-07-04', freq='30min', tz=tus.tz)
times_utc = times.tz_convert('utc')
doy = times.dayofyear
doy_utc = times_utc.dayofyear
td = np.append(0, np.diff(times.dayofyear)) # add a zero so it's the right length
print(times[td > 0])
td_utc = np.append(0, np.diff(times_utc.dayofyear)) # add a zero so it's the right length
print(times_utc[td_utc > 0])
Thank you for your clear explanation! Could I add some details to your example?
The inconsistent results of clearsky.lookup_linke_turbidity for each localized datetime depend not only on interp_turbidity=True and pandas.DatetimeIndex.dayofyear, but also on pandas.DatetimeIndex.month, regardless of interpolation. No matter why, the fundamental reason remains the same as you identified: the values are "changed at midnight local, not midnight UTC." Please note the influence of interp_turbidity and time.month:
The variability in irradiance.get_extra_radiation values results from pandas.DatetimeIndex.dayofyear, pandas.DatetimeIndex.month, and pandas.DatetimeIndex.year. The root cause, again, is the localized definition of midnight. Refer to Perform dayofyear-based calculations according to UTC, not local time #2055 for more details. Or, you can start by examining the following code and trace through irradiance._handle_extra_radiation_types and tools._pandas_to_doy to identify pandas.DatetimeIndex.dayofyear, then follow through to solarposition.pyephem_earthsun_distance which uses ephem.Date assuming UTC, and finally to solarposition.nrel_earthsun_distance which references pandas.DatetimeIndex.year and pandas.DatetimeIndex.month.
The assumed timezones for both functions seem to be UTC. As you mentioned, for clearsky.lookup_linke_turbidity "these values (from an outside source) do not have datetime stamps, only a month." Therefore, synchronizing input datetimes with the timezone distinguishing each day in the h5 file is key to resolving the timezone-related uncertainties in Linke turbidity.
Fortunately, we can experimentally confirm that both functions use UTC for dividing days without needing additional references. The following code provides evidence for irradiance.get_extra_radiation. Only assuming UTC makes the returned values unique for every day; localizing to any other timezone is awkward because two distinct values exist per each localized day then.
Activity
[-]Misuses of tz-naive clearsky functions on the user guide[/-][+]Misused tz-naive clearsky functions on the user guide[/+]cwhanse commentedon May 20, 2024
I'm not quite following. You write "Two functions clearsky.lookup_linke_turbidity and irradiance.get_extra_radiation is tz-naive but has been used as if they were tz-aware" but in the clear-sky user guide, the
timesinput is localizedyhkee0404 commentedon May 20, 2024
Hi, @cwhanse
Thank you for your attention.
Yes, that localized
timesis a tz-aware input which is not a problem, but when it is given to the two functions they return unexpected results and this is the problem. That's why I said the functions are not tz-aware but has been misused as if they were tz-aware, and we are supposed to fix them to be tz-aware.For instance, try converting the timezone of the
timesinput using a common tz-aware functiontz_convertand you can see the difference. This example was copied from the .ipynb file I mentioned above:Does that difference conform to an expected behavior? I think
cs_utcis the more accurate result thancson the user guide.On the other hand, you can also see on the user guide another
timesinput as below which is tz-naive, as well as the tz-aware or localized one you mentioned. Both tz-naive and tz-awaretimesinputs are used without distiction on the user guide, but it is highly likely that they have been misused because merely preprocessing the inputs by the well-known tz-aware functiontz_convertmakes the functions result another values.Another example from my analysis above:
Is this an expected behavior? Isn't
turbidity_hright andturbidity_h_kstwrong? I think ideally the results should be the same asturbidity_h, and a differentturbidity_h_kstis a bug, just like #237 mentioned above.To summarize, the user guide has considered those functions as if tz-aware, where any
tz_convertof the input may not change their results. However, you can easily find it does indeed as in my .ipynb file, which means the functions are tz-naive against our expectations and should be fixed into tz-aware like in #2055.[-]Misused tz-naive clearsky functions on the user guide[/-][+]Fix clearsky functions handling tz-aware datetimes as tz-naive contrary to user guide[/+][-]Fix clearsky functions handling tz-aware datetimes as tz-naive contrary to user guide[/-][+]Clearsky functions handling tz-aware datetimes as tz-naive, contrary to user guide[/+]yhkee0404 commentedon May 25, 2024
Sorry for my poor English. I've thoroughly revised my words. Could you please review them again? @cwhanse
cwhanse commentedon May 25, 2024
@yhkee0404 aha, I think I partly understand what's going on here. The Linke turbidity data distributed with pvlib are gridded (location), monthly values. These values (from an outside source) do not have datetime stamps, only a month. pvlib (somewhat arbitrarily) assigns these values to the middle day of each month so that a daily value can be determined by interpolation. That machinery is not shown to the user; it happens here:
pvlib-python/pvlib/clearsky.py
Line 228 in d53f97e
The examples you provide are comparing the interpolated turbidity at the same location but localized to different timezones. The interpolation is being done using
time.dayofyear. It looks to me that thedayofyearfunction may operating in local time, because the day of year is changed at midnight local, not midnight UTC.Arguably, pandas had to make a decision how to return a day of year for each location, so I don't think this is a fault in pandas - its behaving as intended (I assume).
So: if we want Linke turbidity at a location to be consistent with equal timestamps, we need to work under the hood in pvlib to fix the interpolation.
yhkee0404 commentedon May 26, 2024
Thank you for your clear explanation! Could I add some details to your example?
clearsky.lookup_linke_turbidityfor each localized datetime depend not only oninterp_turbidity=Trueandpandas.DatetimeIndex.dayofyear, but also onpandas.DatetimeIndex.month, regardless of interpolation. No matter why, the fundamental reason remains the same as you identified: the values are "changed at midnight local, not midnight UTC." Please note the influence ofinterp_turbidityandtime.month:pvlib-python/pvlib/clearsky.py
Lines 201 to 205 in d53f97e
irradiance.get_extra_radiationvalues results frompandas.DatetimeIndex.dayofyear,pandas.DatetimeIndex.month, andpandas.DatetimeIndex.year. The root cause, again, is the localized definition of midnight. Refer to Perform dayofyear-based calculations according to UTC, not local time #2055 for more details. Or, you can start by examining the following code and trace throughirradiance._handle_extra_radiation_typesandtools._pandas_to_doyto identifypandas.DatetimeIndex.dayofyear, then follow through tosolarposition.pyephem_earthsun_distancewhich uses ephem.Date assuming UTC, and finally tosolarposition.nrel_earthsun_distancewhich referencespandas.DatetimeIndex.yearandpandas.DatetimeIndex.month.pvlib-python/pvlib/irradiance.py
Lines 90 to 111 in d53f97e
clearsky.lookup_linke_turbidity"these values (from an outside source) do not have datetime stamps, only a month." Therefore, synchronizing input datetimes with the timezone distinguishing each day in the h5 file is key to resolving the timezone-related uncertainties in Linke turbidity.pvlib-python/pvlib/clearsky.py
Lines 174 to 193 in d53f97e
Fortunately, we can experimentally confirm that both functions use UTC for dividing days without needing additional references. The following code provides evidence for
irradiance.get_extra_radiation. Only assuming UTC makes the returned valuesuniquefor every day; localizing to any other timezone is awkward because two distinct values exist per each localized day then.