Beispiel #1
0
 def interpolate(self, lon: np.ndarray, lat: np.ndarray,
                 time: np.ndarray) -> np.ndarray:
     """Interpolate the SSH to the required coordinates"""
     interpolator = self.load_dataset(time.min(), time.max())
     ssh = pyinterp.trivariate(interpolator,
                               lon.flatten(),
                               lat.flatten(),
                               time,
                               bounds_error=True,
                               interpolator='bilinear').reshape(lon.shape)
     return ssh
Beispiel #2
0
def interp_on_alongtrack(gridded_dataset,
                         ds_alongtrack,
                         lon_min=0.,
                         lon_max=360.,
                         lat_min=-90,
                         lat_max=90.,
                         time_min='1900-10-01',
                         time_max='2100-01-01',
                         is_circle=True):

    # Interpolate maps onto alongtrack dataset
    x_axis, y_axis, z_axis, grid = read_l4_dataset(gridded_dataset,
                                                   lon_min=lon_min,
                                                   lon_max=lon_max,
                                                   lat_min=lat_min,
                                                   lat_max=lat_max,
                                                   time_min=time_min,
                                                   time_max=time_max,
                                                   is_circle=is_circle)

    ssh_map_interp = pyinterp.trivariate(
        grid,
        ds_alongtrack["longitude"].values,
        ds_alongtrack["latitude"].values,
        z_axis.safe_cast(ds_alongtrack.time.values),
        bounds_error=False).reshape(ds_alongtrack["longitude"].values.shape)

    ssh_alongtrack = (ds_alongtrack["sla_unfiltered"] + ds_alongtrack["mdt"] -
                      ds_alongtrack["lwe"]).values
    lon_alongtrack = ds_alongtrack["longitude"].values
    lat_alongtrack = ds_alongtrack["latitude"].values
    time_alongtrack = ds_alongtrack["time"].values

    # get and apply mask from map_interp & alongtrack on each dataset
    msk1 = np.ma.masked_invalid(ssh_alongtrack).mask
    msk2 = np.ma.masked_invalid(ssh_map_interp).mask
    msk = msk1 + msk2

    ssh_alongtrack = np.ma.masked_where(msk, ssh_alongtrack).compressed()
    lon_alongtrack = np.ma.masked_where(msk, lon_alongtrack).compressed()
    lat_alongtrack = np.ma.masked_where(msk, lat_alongtrack).compressed()
    time_alongtrack = np.ma.masked_where(msk, time_alongtrack).compressed()
    ssh_map_interp = np.ma.masked_where(msk, ssh_map_interp).compressed()

    # select inside value (this is done to insure similar number of point in statistical comparison between methods)
    indices = np.where((lon_alongtrack >= lon_min + 0.25)
                       & (lon_alongtrack <= lon_max - 0.25)
                       & (lat_alongtrack >= lat_min + 0.25)
                       & (lat_alongtrack <= lat_max - 0.25))[0]

    return time_alongtrack[indices], lat_alongtrack[indices], lon_alongtrack[
        indices], ssh_alongtrack[indices], ssh_map_interp[indices]
Beispiel #3
0
def oi_regrid(ds_source, ds_target):

    logging.info('     Regridding...')

    # Define source grid
    x_source_axis = pyinterp.Axis(ds_source["lon"][:], is_circle=False)
    y_source_axis = pyinterp.Axis(ds_source["lat"][:])
    z_source_axis = pyinterp.TemporalAxis(ds_source["time"][:])
    ssh_source = ds_source["gssh"][:].T
    grid_source = pyinterp.Grid3D(x_source_axis, y_source_axis, z_source_axis,
                                  ssh_source.data)

    # Define target grid
    mx_target, my_target, mz_target = numpy.meshgrid(
        ds_target['lon'].values,
        ds_target['lat'].values,
        z_source_axis.safe_cast(ds_target['time'].values),
        indexing="ij")
    # Spatio-temporal Interpolation
    ssh_interp = pyinterp.trivariate(grid_source,
                                     mx_target.flatten(),
                                     my_target.flatten(),
                                     mz_target.flatten(),
                                     bounds_error=True).reshape(
                                         mx_target.shape).T

    # Save to dataset
    ds_ssh_interp = xr.Dataset(
        {'sossheig': (('time', 'lat', 'lon'), ssh_interp)},
        coords={
            'time': ds_target['time'].values,
            'lon': ds_target['lon'].values,
            'lat': ds_target['lat'].values,
        })

    return ds_ssh_interp
Beispiel #4
0
    def interpolate(self, lon: np.ndarray, lat: np.ndarray,
                    dates: np.ndarray) -> np.ndarray:
        """Interpolate the SSH to the required coordinates."""
        ds = self._select_ds(
            dates.min(),  # type: ignore
            dates.max())  # type: ignore

        assert np.all(np.diff(ds.ocean_time.values) == self._dt)
        assert np.all(np.diff(ds.lon_rho.values, axis=0) < 1e-10)
        assert np.all(np.diff(ds.lat_rho.values, axis=1) < 1e-10)

        t_axis = pyinterp.TemporalAxis(ds.ocean_time.values)

        grid3d = pyinterp.Grid3D(
            pyinterp.Axis(ds.lon_rho.values[0, :], is_circle=True),
            pyinterp.Axis(ds.lat_rho.values[:, 0]), t_axis,
            ds[self.ssh].values.T)

        ssh = pyinterp.trivariate(grid3d,
                                  lon.ravel(),
                                  lat.ravel(),
                                  t_axis.safe_cast(dates.ravel()),
                                  num_threads=1).reshape(lon.shape)
        return ssh