signal_xyt = xr.open_dataset(sigdir + test_file, decode_times=False)['sossheig'].chunk(chunks={'time_counter': 50})
print signal_xyt
signal_xyt.isel(time_counter=0).plot(vmin=-0.07, vmax=0.07, cmap='seismic')

"""
Explanation: Here is an example of simulated sea surface height from the NEMO model run at 1/4°, that is represented by an xarray.DataArray object. Note that dask array can be used by precising chunks.
End of explanation
"""


win1D = signal_xyt.win

"""
Explanation: A window object that is linked to the previous data is created by the following line:
End of explanation
"""


win1D.set(window_name='boxcar', n=[5], dims=['time_counter'])
print win1D._depth.values()
print win1D

"""
Explanation: Temporal filtering: Boxcar window
A boxcar window object that will be applied along the time dimension is simply be defined by setting its different properties:
End of explanation
"""


win1D.plot()

"""
Explanation: The weight distribution and the frequency response of the window may be plotted for one dimensional windows using the plot method:
End of explanation
"""


signal_LF_box = win1D.apply(compute=True)

"""
Explanation: This window can now be applied on the data by the associated procedure:
End of explanation
"""


win1D.set(window_name='lanczos', n=[5], dims=['time_counter'], fc=0.1)
print win1D
win1D.plot()

signal_LF_lcz = win1D.apply(compute=True)

"""
Explanation: By default the filtering is computed when using the apply function. If compute is set to False, the filtering will be computed when the output data is required. This allows the definition of several dask object before the global computation is performed.
Temporal filtering: Lanczos window
Setting now different properties to use a Lanczos window:
End of explanation
"""


signal_xyt.isel(x=50, y=50).plot()
signal_LF_box.isel(x=50, y=50).plot(color='g')
signal_LF_lcz.isel(x=50, y=50).plot(color='r')
plt.legend(['raw', 'boxcar 10yr', 'lanczos 10yr'])

"""
Explanation: The following figure gives the comparison between the boxcar and the lanczos window applied for the low-pass filtering at one grid point of the dataset.
End of explanation
"""


signal_xyt = xr.open_dataset(sigdir + test_file, decode_times=False)['sossheig'].chunk(chunks={'x': 40, 'y':40})
win_box2D = signal_xyt.win
win_box2D.set(window_name='lanczos', n=[24, 24], dims=['x', 'y'], fc=0.0416)
print signal_xyt
print win_box2D

win_box2D.plot()

"""
Explanation: Spatial filtering
The window object extend to multidimensional filtering such bidimensional spatial filtering. The filtering method used is able to deal with missing data or coastline by reweighting the filters weights. For example, a 2D Lanczos window may thus be associated to a dataset.
End of explanation
"""


bw = win_box2D.boundary_weights()

bw.isel(time_counter=1).plot(vmin=0, vmax=1, cmap='spectral')

signal_LS = win_box2D.apply(weights=bw)

# Original dataset
signal_xyt.std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)

"""
Explanation: Original dataset
Temporal standard deviation of the raw timeseries:
End of explanation
"""


# Large-scale (>6°) dataset
signal_LS.std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)

"""
Explanation: Large Scales (>6°)
Temporal standard deviation of the large field:
End of explanation
"""


# Small-scale (<6°) dataset
(signal_xyt-signal_LS).std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)

"""
Explanation: Small Scales (<6°)
Temporal standard deviation of the large field:
End of explanation
"""


