Esempio n. 1
0
 def test_error_zdim(self):
     # Test error:
     # catches error from _regular_interp linked to z_dim
     with pytest.raises(RuntimeError):
         linear_interpolation_remap(
             self.dsfake["PRES"],
             self.dsfake["TEMP"],
             self.dsfake["Z_LEVELS"],
             z_regridded_dim="Z_LEVELS",
         )
Esempio n. 2
0
 def test_error_ds(self):
     # Test error:
     # catches error from linear_interpolation_remap linked to datatype
     with pytest.raises(ValueError):
         linear_interpolation_remap(
             self.dsfake["PRES"],
             self.dsfake,
             self.dsfake["Z_LEVELS"],
             z_dim="N_LEVELS",
             z_regridded_dim="Z_LEVELS",
         )
Esempio n. 3
0
 def test_interpolation(self):
     # Run it with success:
     dsi = linear_interpolation_remap(self.dsfake.PRES,
                                      self.dsfake['TEMP'],
                                      self.dsfake['Z_LEVELS'],
                                      z_dim='N_LEVELS',
                                      z_regridded_dim='Z_LEVELS')
     assert 'remapped' in dsi.dims
Esempio n. 4
0
 def test_error_zdim(self):
     # Test error:
     # catches error from _regular_interp linked to z_dim
     with pytest.raises(RuntimeError):
         dsi = linear_interpolation_remap(self.dsfake.PRES,
                                          self.dsfake['TEMP'],
                                          self.dsfake['Z_LEVELS'],
                                          z_regridded_dim='Z_LEVELS')
Esempio n. 5
0
 def test_interpolation_1d(self):
     # Run it with success:
     dsi = linear_interpolation_remap(
         self.dsfake["PRES"].isel(N_PROF=0),
         self.dsfake["TEMP"].isel(N_PROF=0),
         self.dsfake["Z_LEVELS"],
         z_regridded_dim="Z_LEVELS",
     )
     assert "remapped" in dsi.dims
Esempio n. 6
0
 def test_error_ds(self):
     # Test error:
     # catches error from linear_interpolation_remap linked to datatype
     with pytest.raises(ValueError):
         dsi = linear_interpolation_remap(self.dsfake.PRES,
                                          self.dsfake,
                                          self.dsfake['Z_LEVELS'],
                                          z_dim='N_LEVELS',
                                          z_regridded_dim='Z_LEVELS')
Esempio n. 7
0
    def interp_std_levels(self, std_lev):
        """ Returns a new dataset interpolated to new inputs levels                 
        
        Parameters
        ----------
        list or np.array 
            Standard levels used for interpolation

        Returns
        -------
        :class:`xarray.Dataset`           
        """

        if self._type != 'profile':
            raise InvalidDatasetStructure(
                "Method only available for a collection of profiles")

        if (self._mode != 'standard'):
            raise InvalidDatasetStructure(
                "Method only available for the standard mode yet")

        if (type(std_lev) is np.ndarray) | (type(std_lev) is list):
            std_lev = np.array(std_lev)
            if (np.any(sorted(std_lev) != std_lev)) | (np.any(std_lev < 0)):
                raise ValueError(
                    'Standard levels must be a list or a numpy array of positive and sorted values'
                )
        else:
            raise ValueError(
                'Standard levels must be a list or a numpy array of positive and sorted values'
            )

        ds = self._obj

        # Selecting profiles that have a max(pressure) > max(std_lev) to avoid extrapolation in that direction
        # For levels < min(pressure), first level values of the profile are extended to surface.
        i1 = (ds['PRES'].max('N_LEVELS') >= std_lev[-1])
        dsp = ds.where(i1, drop=True)

        # check if any profile is left, ie if any profile match the requested depth
        if (len(dsp['N_PROF']) == 0):
            raise Warning(
                'None of the profiles can be interpolated (not reaching the requested depth range).'
            )
            return None

        # add new vertical dimensions, this has to be in the datasets to apply ufunc later
        dsp['Z_LEVELS'] = xr.DataArray(std_lev, dims={'Z_LEVELS': std_lev})

        # init
        ds_out = xr.Dataset()

        # vars to interpolate
        datavars = [
            dv for dv in list(dsp.variables) if
            set(['N_LEVELS', 'N_PROF']) == set(dsp[dv].dims) and 'QC' not in dv
        ]
        # coords
        coords = [dv for dv in list(dsp.coords)]
        # vars depending on N_PROF only
        solovars = [
            dv for dv in list(dsp.variables)
            if dv not in datavars and dv not in coords and 'QC' not in dv
        ]

        for dv in datavars:
            ds_out[dv] = linear_interpolation_remap(dsp.PRES,
                                                    dsp[dv],
                                                    dsp['Z_LEVELS'],
                                                    z_dim='N_LEVELS',
                                                    z_regridded_dim='Z_LEVELS')
        ds_out = ds_out.rename({'remapped': 'PRES_INTERPOLATED'})

        for sv in solovars:
            ds_out[sv] = dsp[sv]

        for co in coords:
            ds_out.coords[co] = dsp[co]

        ds_out = ds_out.drop_vars(['N_LEVELS', 'Z_LEVELS'])
        ds_out = ds_out[np.sort(ds_out.data_vars)]
        ds_out.attrs = self.attrs  # Preserve original attributes
        ds_out.argo._add_history('Interpolated on standard levels')

        return ds_out