#!/usr/bin/env python # coding: utf-8 # # Time interpolation # In[1]: import numpy as np import mikeio # In[2]: ds = mikeio.read("../tests/testdata/waves.dfs2") ds # ## Interpolate to specific timestep # # A common use case is to interpolate to a shorter timestep, in this case 1h. # In[3]: ds_h = ds.interp_time(3600) ds_h # And to store the interpolated data in a new file. # In[4]: ds_h.to_dfs("waves_3h.dfs2") # ## Interpolate to time axis of another dataset # Read some non-equidistant data typically found in observed data. # In[5]: ts = mikeio.read("../tests/testdata/waves.dfs0") ts # The observed timeseries is longer than the modelled data. Default is to fill values with NaN. # In[6]: dsi = ds.interp_time(ts) # In[15]: dsi.time # In[8]: dsi["Sign. Wave Height"].shape # In[9]: ax = dsi["Sign. Wave Height"].sel(x=250, y=1200).plot(marker='+') ts["Sign. Wave Height"].plot(ax=ax,marker='+') # ## Model validation # # A common metric for model validation is mean absolute error (MAE). # # In the example below we calculate this metric using the model data interpolated to the observed times. # # For a more elaborate model validation library which takes care of these things for you as well as calculating a number of relevant metrics, take a look at [fmskill](https://github.com/DHI/fmskill). # # Use `np.nanmean` to skip NaN. # In[10]: ts["Sign. Wave Height"] # In[11]: dsi["Sign. Wave Height"].sel(x=250, y=1200) # In[12]: diff = (ts["Sign. Wave Height"] - dsi["Sign. Wave Height"].sel(x=250, y=1200)) diff.plot() # In[13]: mae = np.abs(diff).nanmean().to_numpy() mae # # Clean up # In[14]: import os os.remove("waves_3h.dfs2")