In [1]:
import xarray as xr
import oocgcm.filtering.linearfilters as tf
import pylab as plt
import numpy as np
%matplotlib inline
In [2]:
sigdir = "../examples/signals/"
test_file = 'ORCA025_SSH_SWPAC.nc'
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.
In [24]:
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')
Out[24]:
A window object that is linked to the previous data is created by the following line:
In [4]:
win1D = signal_xyt.win
A boxcar window object that will be applied along the time dimension is simply be defined by setting its different properties:
In [5]:
win1D.set(window_name='boxcar', n=[5], dims=['time_counter'])
print win1D._depth.values()
print win1D
The weight distribution and the frequency response of the window may be plotted for one dimensional windows using the plot method:
In [6]:
win1D.plot()
This window can now be applied on the data by the associated procedure:
In [7]:
signal_LF_box = win1D.apply(compute=True)
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.
Setting now different properties to use a Lanczos window:
In [8]:
win1D.set(window_name='lanczos', n=[5], dims=['time_counter'], fc=0.1)
print win1D
win1D.plot()
In [9]:
signal_LF_lcz = win1D.apply(compute=True)
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.
In [10]:
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'])
Out[10]:
In [11]:
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
In [12]:
win_box2D.plot()
In [13]:
bw = win_box2D.boundary_weights()
In [14]:
bw.isel(time_counter=1).plot(vmin=0, vmax=1, cmap='spectral')
Out[14]:
In [15]:
signal_LS = win_box2D.apply(weights=bw)
In [16]:
# Original dataset
signal_xyt.std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)
Out[16]:
In [17]:
# Large-scale (>6°) dataset
signal_LS.std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)
Out[17]:
In [18]:
# Small-scale (<6°) dataset
(signal_xyt-signal_LS).std(dim='time_counter').plot(cmap='jet', vmin=0, vmax=0.06)
Out[18]:
In [ ]: