Deltares / hatyan

Harmonic tidal analysis and prediction
https://deltares.github.io/hatyan/
GNU General Public License v3.0
13 stars 2 forks source link

Unexpected timezones in case of different timezones in components and times #334

Closed veenstrajelmer closed 3 months ago

veenstrajelmer commented 3 months ago

When passing components and times with different timezones to hatyan.prediction(), the prediction will have the timezone of the components instead of the times. This is unexpected:

import pandas as pd
import hatyan
comp = pd.DataFrame({"A": [1, 0.5, 0.2],
                     "phi_deg": [10,15,20]}, 
                    index=["M2","M4","S2"])
comp.attrs["nodalfactors"] = True
comp.attrs["fu_alltimes"] = True
comp.attrs["xfac"] = False
comp.attrs["source"] = "schureman"
comp.attrs["tzone"] = "UTC"
dtindex = pd.date_range("2020-01-01 00:00 +01:00","2020-01-02 00:00 +01:00", freq="10min")
pred = hatyan.prediction(comp, times=dtindex)
print(pred)

Gives:

                             values
2019-12-31 23:00:00+00:00 -0.397264
2019-12-31 23:10:00+00:00 -0.406804
2019-12-31 23:20:00+00:00 -0.422670
2019-12-31 23:30:00+00:00 -0.443680
2019-12-31 23:40:00+00:00 -0.468382
                            ...
2020-01-01 22:20:00+00:00 -0.542722
2020-01-01 22:30:00+00:00 -0.498343
2020-01-01 22:40:00+00:00 -0.456521
2020-01-01 22:50:00+00:00 -0.418966
2020-01-01 23:00:00+00:00 -0.387146
[145 rows x 1 columns]

When using:

import pandas as pd
import hatyan
comp = pd.DataFrame({"A": [1, 0.5, 0.2],
                     "phi_deg": [10,15,20]}, 
                    index=["M2","M4","S2"])
comp.attrs["nodalfactors"] = True
comp.attrs["fu_alltimes"] = True
comp.attrs["xfac"] = False
comp.attrs["source"] = "schureman"
comp.attrs["tzone"] = "UTC+01:00"
dtindex = pd.date_range("2020-01-01 00:00","2020-01-02 00:00", freq="10min")
pred = hatyan.prediction(comp, times=dtindex)
print(pred)

Gives:

                             values
2020-01-01 00:00:00+01:00 -0.522025
2020-01-01 00:10:00+01:00 -0.547194
2020-01-01 00:20:00+01:00 -0.568637
2020-01-01 00:30:00+01:00 -0.584397
2020-01-01 00:40:00+01:00 -0.592610
                            ...
2020-01-01 23:20:00+01:00 -0.345106
2020-01-01 23:30:00+01:00 -0.336258
2020-01-01 23:40:00+01:00 -0.335843
2020-01-01 23:50:00+01:00 -0.343647
2020-01-02 00:00:00+01:00 -0.359096
[145 rows x 1 columns]

The usecase in test_prediction_comp_and_times_different_timezones() even interprets the naive tstart/tstop in the tzone of the components:

import hatyan
file_dia = 'c:\\DATA\\hatyan\\tests\\data_unitsystemtests\\VLISSGN_obs1.txt'
df_meas = hatyan.read_dia(filename=file_dia)
comp = hatyan.analysis(ts=df_meas, const_list='year')

times_pred = slice("2019-01-01","2019-02-01", "10min")
ts_pred = hatyan.prediction(comp=comp, times=times_pred)
print(ts_pred)

Gives:

                             values
2019-01-01 00:00:00+01:00  0.989719
2019-01-01 00:10:00+01:00  0.878398
2019-01-01 00:20:00+01:00  0.758829
2019-01-01 00:30:00+01:00  0.631713
2019-01-01 00:40:00+01:00  0.498225
                            ...
2019-01-31 23:20:00+01:00  1.706555
2019-01-31 23:30:00+01:00  1.724520
2019-01-31 23:40:00+01:00  1.722927
2019-01-31 23:50:00+01:00  1.704116
2019-02-01 00:00:00+01:00  1.670706
[4465 rows x 1 columns]

Todo: