import pyinterp.backends.xarray import xarray try: # When generating the documentation the variable DATASET points to the data # path. DATASET = pathlib.Path(os.environ['DATASET']) except KeyError: # Otherwise, the relative folder path is used. DATASET = pathlib.Path("..", "..", "tests", "dataset") TCW = DATASET.joinpath("tcw.nc") #%% # The first step is to load the data into memory and create the interpolator # object: ds = xarray.open_dataset(TCW) interpolator = pyinterp.backends.xarray.Grid3D(ds.tcw) #%% # We will build a new grid that will be used to build a new interpolated grid. # # .. note :: # # The coordinates used for interpolation are shifted to avoid using the # points of the trivariate function. # # .. warning :: # # When using a time axis, care must be taken to use the same unit of dates, # between the axis defined and the dates supplied during interpolation. The # function :py:meth:`pyinterp.TemporalAxis.safe_cast` automates this task and
# tensor data so that it is properly stored in memory for pyinterp. #%% # * The shape of the tensor must be (len(x_axis), len(y_axis), len(t_axis)) tcw = tcw.T #%% # * The undefined values must be set to nan. tcw[tcw.mask] = float("nan") #%% # Now we can build the object handling the regular 3-dimensional grid. # # .. note:: # Grid data are not copied, the Grid3D class just keeps a reference on the # handled array. Axis data are copied for non-uniform axes, and only examined # for regular axes. grid_3d = pyinterp.Grid3D(x_axis, y_axis, t_axis, tcw) grid_3d #%% # xarray backend # ############## # # The construction of these objects manipulating the :py:class:`regular grids # <pyinterp.backends.xarray.RegularGridInterpolator>` can be done more easily # using the `xarray <http://xarray.pydata.org/>`_ library and `CF # <https://cfconventions.org/>`_ convention usually found in NetCDF files. interpolator = pyinterp.backends.xarray.RegularGridInterpolator( xarray.open_dataset(TCW).tcw) interpolator.grid
import pyinterp.backends.xarray import xarray try: # When generating the documentation the variable DATASET points to the data # path. DATASET = pathlib.Path(os.environ['DATASET']) except KeyError: # Otherwise, the relative folder path is used. DATASET = pathlib.Path("..", "..", "tests", "dataset") AOML = DATASET.joinpath("aoml_v2019.nc") #%% # The first step is to load the data into memory and create the interpolator # object: ds = xarray.open_dataset(AOML) #%% # Let's start by calculating the standard for vectors u and v. norm = (ds.ud**2 + ds.vd**2)**0.5 #%% # Now, we will describe the grid used to calculate our :py:class:`binned # <pyinterp.Binning2D>` statics. binning = pyinterp.Binning2D( pyinterp.Axis(numpy.arange(27, 42, 0.3), is_circle=True), pyinterp.Axis(numpy.arange(40, 47, 0.3))) binning #%% # We push the loaded data into the different defined bins using :ref:`simple
# .. note:: # # In the case of an interpolation of the nearest neighbor the undefined values # have no impact because no arithmetic operation is done on the grid values: # we just return the value of the nearest point. # # LOESS # ===== # # The :py:func:`first <pyinterp.fill.loess>` method applies a weighted local # regression to extrapolate the boundary between defined and undefined values. The # user must indicate the number of pixels on the X and Y axes to be considered in # the calculation. # # Let's start by building the object handling our grid. ds = xarray.open_dataset(MSS) grid = pyinterp.backends.xarray.Grid2D(ds.mss) #%% # The function filling the holes near the mask is called filled = pyinterp.fill.loess(grid, nx=3, ny=3) #%% # The image below illustrates the result: fig = matplotlib.pyplot.figure() ax1 = fig.add_subplot( 211, projection=cartopy.crs.PlateCarree(central_longitude=180)) lons, lats = numpy.meshgrid(grid.x, grid.y, indexing='ij') pcm = ax1.pcolormesh(lons, lats, ds.mss.T,
import pyinterp.backends.xarray import xarray try: # When generating the documentation the variable DATASET points to the data # path. DATASET = pathlib.Path(os.environ['DATASET']) except KeyError: # Otherwise, the relative folder path is used. DATASET = pathlib.Path("..", "..", "tests", "dataset") PRES_TEMP = DATASET.joinpath("pres_temp_4D.nc") #%% # The first step is to load the data into memory and create the interpolator # object: ds = xarray.open_dataset(PRES_TEMP) interpolator = pyinterp.backends.xarray.Grid4D(ds.pressure) #%% # We will build a new grid that will be used to build a new interpolated grid. # # .. warning :: # # When using a time axis, care must be taken to use the same unit of dates, # between the axis defined and the dates supplied during interpolation. The # function :py:meth:`pyinterp.TemporalAxis.safe_cast` automates this task and # will warn you if there is an inconsistency during the date conversion. mx, my, mz, mu = numpy.meshgrid(numpy.arange(-125, -70, 0.5), numpy.arange(25, 50, 0.5), numpy.datetime64("2000-01-01T12:00"), 0.5,