Skip to content

Commit

Permalink
fixed WMCN_tidescourse.py example script
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Mar 22, 2024
1 parent b14b7ef commit 004c630
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 44 deletions.
1 change: 1 addition & 0 deletions hatyan/analysis_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def analysis(ts, const_list, hatyan_settings=None, **kwargs): # nodalfactors=Tru
metadata['fu_alltimes'] = hatyan_settings.fu_alltimes
metadata['source'] = hatyan_settings.source
COMP_mean_pd = metadata_add_to_obj(COMP_mean_pd, metadata)
COMP_all_pd = metadata_add_to_obj(COMP_all_pd, metadata)

if hatyan_settings.return_allperiods:
return COMP_mean_pd, COMP_all_pd
Expand Down
39 changes: 12 additions & 27 deletions hatyan/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,16 @@ def write_tsnetcdf(ts, station, vertref, filename, ts_ext=None, tzone_hr=1, nosi

def write_tsdia(ts, filename, headerformat='dia'):
"""
Writes the timeseries to an equidistant dia file. This is only supported
Writes the timeseries to an equidistant dia file or the extremes to
a non-equidistant dia file. This is only supported
for timeseries with a UTC+1 timestamp, since DONAR (and therefore dia)
data is always in UTC+1 (MET/CET), also during summertime periods.
Parameters
----------
ts : pandas.DataFrame
The DataFrame should contain a 'values' column and a pd.DatetimeIndex as index, it contains the timeseries.
The DataFrame should contain a 'values' column and a pd.DatetimeIndex as index, it contains the timeseries.
In case of extremes, the DataFrame should also contain a 'HWLW_code' column.
station : TYPE
DESCRIPTION.
vertref : TYPE
Expand All @@ -807,7 +809,14 @@ def write_tsdia(ts, filename, headerformat='dia'):
None.
"""
if "HWLWcode"in ts.columns:
if "HWLWcode" in ts.columns:
write_tsdia_HWLW(ts_ext=ts, filename=filename, headerformat=headerformat)
else:
write_tsdia_ts(ts=ts, filename=filename, headerformat=headerformat)


def write_tsdia_ts(ts, filename, headerformat='dia'):
if "HWLWcode" in ts.columns:
raise TypeError("a timeseries with extremes (HWLW) was passed to write_tsdia, use `write_tsdia_HWLW()` instead")
metadata = metadata_from_obj(ts)
waarnemingssoort = wns_from_metadata(metadata)
Expand Down Expand Up @@ -894,30 +903,6 @@ def write_tsdia(ts, filename, headerformat='dia'):


def write_tsdia_HWLW(ts_ext, filename, headerformat='dia'):
"""
writes the extremes timeseries to a non-equidistant dia file
Parameters
----------
ts_ext : pandas.DataFrame
The DataFrame should contain a 'values' and 'HWLW_code' column and a pd.DatetimeIndex as index, it contains the times, values and codes of the timeseries that are extremes.
station : TYPE
DESCRIPTION.
vertref : TYPE
DESCRIPTION.
filename : TYPE
DESCRIPTION.
Raises
------
Exception
DESCRIPTION.
Returns
-------
None.
"""

metadata = metadata_from_obj(ts_ext)
waarnemingssoort = wns_from_metadata(metadata)
Expand Down
11 changes: 4 additions & 7 deletions tests/examples/WMCN_tidescourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#component groups
file_data_comp0 = os.path.join(dir_testdata,'predictie2019','%s_ana.txt'%(current_station))
COMP_merged = hatyan.read_components(filename=file_data_comp0)
# overwrite metadata to avoid AssertionError from prediction
COMP_merged.attrs["xfac"] = False
COMP_merged.attrs["fu_alltimes"] = True

#prediction and validation
bool_end1 = COMP_merged.index.astype(str).str.endswith('1')
Expand Down Expand Up @@ -298,7 +301,7 @@
colors = plt.cm.jet(np.linspace(0,1,n_colors))
for i_stat, current_station in enumerate(selected_stations):
comp_frommeasurements_avg_group = hatyan.read_components(filename=os.path.join(dir_testdata,'predictie2019','%s_ana.txt'%(current_station)))
ts_prediction = hatyan.prediction(comp=comp_frommeasurements_avg_group, times=times_pred)
ts_prediction = hatyan.prediction(comp=comp_frommeasurements_avg_group, times=times_pred, xfac=True, fu_alltimes=False)
vals_real = ts_prediction['values']
times_real = ts_prediction.index
ax1.plot(times_real, vals_real, label=current_station, color=colors[i_stat])
Expand Down Expand Up @@ -363,9 +366,3 @@









2 changes: 1 addition & 1 deletion tests/examples/numbering_extremes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
pdrow['RDy'] = RDy/1000 #from m to km
stats = pd.concat([stats,pdrow])

#hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_step_pred, current_station))
#hatyan.write_tsdia(ts=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_step_pred, current_station))
#fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction, ts_ext_validation=ts_ext_validation)
#fig.savefig('prediction_%im_%s_HWLW'%(times_step_pred, current_station))
#fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction)
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/predictie_2019_19Ycomp4Ydia.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
ts_ext_validation = None
hatyan.write_tsdia(ts=ts_prediction, filename='prediction_%im_%s.dia'%(times_pred.step,current_station))
ts_ext_prediction = hatyan.calc_HWLW(ts=ts_prediction)
hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
hatyan.write_tsdia(ts=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction, ts_ext_validation=ts_ext_validation)
fig.savefig('prediction_%im_%s_HWLW'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_validation=ts_validation)
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/predictie_2019_19Ycomp4Ydia_VLISSGN.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
ts_ext_validation = hatyan.readts_dia(filename=file_data_predvaliHWLW, station=current_station)
hatyan.write_tsdia(ts=ts_prediction, filename='prediction_%im_%s.dia'%(times_pred.step,current_station))
ts_ext_prediction1min = hatyan.calc_HWLW(ts=ts_prediction1min)
hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction1min, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
hatyan.write_tsdia(ts=ts_ext_prediction1min, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction1min, ts_ext_validation=ts_ext_validation)
fig.savefig('prediction_%im_%s_HWLW'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_validation=ts_validation)
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/predictie_2019_allfromdia.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
#ts_ext_validation = hatyan.readts_dia(filename=file_data_predvaliHWLW, station=current_station)
hatyan.write_tsdia(ts=ts_prediction, filename='prediction_%im_%s.dia'%(times_pred.step,current_station))
#ts_ext_prediction = hatyan.calc_HWLW(ts=ts_prediction)
#hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
#hatyan.write_tsdia(ts=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
#fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction, ts_ext_validation=ts_ext_validation)
#fig.savefig('prediction_%im_%s_HWLW'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_validation=ts_validation)
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/predictie_2019_frommergedcomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#ts_ext_validation = hatyan.readts_dia(filename=file_data_predvaliHWLW, station=current_station)
hatyan.write_tsdia(ts=ts_prediction, filename='prediction_%im_%s.dia'%(times_pred.step,current_station))
#ts_ext_prediction = hatyan.calc_HWLW(ts=ts_prediction)
#hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
#hatyan.write_tsdia(ts=ts_ext_prediction, filename='prediction_HWLW_%im_%s.dia'%(times_pred.step, current_station))
#fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_ext=ts_ext_prediction, ts_ext_validation=ts_ext_validation)
#fig.savefig('prediction_%im_%s_HWLW'%(times_pred.step, current_station))
fig, (ax1,ax2) = hatyan.plot_timeseries(ts=ts_prediction, ts_validation=ts_validation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
ax.grid()
ax.set_xlabel('Tijd')
ax.set_ylabel('astro verval %d [m] (TERNZN-HANSWT)'%yr)
#hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction_main, station=current_station, vertref='NAP', filename='prediction_HWLW_%im_%s_main.dia'%(times_step_pred, current_station))
#hatyan.write_tsdia_HWLW(ts_ext=ts_ext_prediction_clean, station=current_station, vertref='NAP', filename='prediction_HWLW_%im_%s_agger345.dia'%(times_step_pred, current_station))
#hatyan.write_tsdia(ts=ts_ext_prediction_main, station=current_station, vertref='NAP', filename='prediction_HWLW_%im_%s_main.dia'%(times_step_pred, current_station))
#hatyan.write_tsdia(ts=ts_ext_prediction_clean, station=current_station, vertref='NAP', filename='prediction_HWLW_%im_%s_agger345.dia'%(times_step_pred, current_station))
fig.tight_layout()
fig.savefig('prediction_WSdwarsstroming')

Expand Down
6 changes: 3 additions & 3 deletions tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_readwrite_tsdia_ext_rounding():
for diff in [0, 0.004, -0.004]:
ts_pred_rounddiff = hatyan.readts_dia(file_pred)
ts_pred_rounddiff['values'] = ts_pred['values'] + diff
hatyan.write_tsdia_HWLW(ts_ext=ts_pred_rounddiff, filename=file_new)
hatyan.write_tsdia(ts=ts_pred_rounddiff, filename=file_new)
ts_new = hatyan.readts_dia(file_new)

assert np.allclose(ts_pred, ts_new)
Expand Down Expand Up @@ -211,8 +211,8 @@ def test_readwrite_diawia():

#write to files
if 'HWLWcode' in ts_dia.columns:
hatyan.write_tsdia_HWLW(ts_ext=ts_dia, filename=file_dia_out)
hatyan.write_tsdia_HWLW(ts_ext=ts_wia, filename=file_wia_out, headerformat='wia')
hatyan.write_tsdia(ts=ts_dia, filename=file_dia_out)
hatyan.write_tsdia(ts=ts_wia, filename=file_wia_out, headerformat='wia')
else:
hatyan.write_tsdia(ts=ts_dia, filename=file_dia_out)
hatyan.write_tsdia(ts=ts_wia, filename=file_wia_out, headerformat='wia')
Expand Down

0 comments on commit 004c630

Please sign in to comment.