Python xarray 模块,DataArray() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xarray.DataArray()

项目:xcesm    作者:Yefee    | 项目源码 | 文件源码
def mass_streamfun(self):

        from scipy import integrate

        data = self._obj
#        lonlen = len(data.lon)
        if 'lon' in data.dims:
            data = data.fillna(0).mean('lon')
        levax = data.get_axis_num('lev')
        stream = integrate.cumtrapz(data * np.cos(np.deg2rad(data.lat)), x=data.lev * 1e2, initial=0., axis=levax)
        stream = stream * 2 * np.pi  / cc.g * cc.rearth * 1e-9
        stream = xr.DataArray(stream, coords=data.coords, dims=data.dims)
        stream = stream.rename('ovt')
        stream.attrs['long name'] = 'atmosphere overturning circulation'
        stream.attrs['unit'] = 'Sv (1e9 kg/s)'
        return stream
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def _apply_detrend(da, axis_num):
    """Wrapper function for applying detrending"""
    if da.chunks:
        func = detrend_wrap(detrendn)
        da = xr.DataArray(func(da.data, axes=axis_num),
                        dims=da.dims, coords=da.coords)
    else:
        if da.ndim == 1:
            da = xr.DataArray(sps.detrend(da),
                            dims=da.dims, coords=da.coords)
        else:
            da = detrendn(da, axes=axis_num)
        # else:
        #     raise ValueError("Data should be dask array.")

    return da
项目:decode    作者:deshima-dev    | 项目源码 | 文件源码
def zeros_like(array, dtype=None, keepmeta=True):
    """Create an array of zeros with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        dtype (data-type, optional): If specified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array filled with zeros.
    """
    if keepmeta:
        return xr.zeros_like(array, dtype)
    else:
        return dc.zeros(array.shape, dtype)
项目:decode    作者:deshima-dev    | 项目源码 | 文件源码
def ones_like(array, dtype=None, keepmeta=True):
    """Create an array of ones with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        dtype (data-type, optional): If spacified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array filled with ones.
    """
    if keepmeta:
        return xr.ones_like(array, dtype)
    else:
        return dc.ones(array.shape, dtype)
项目:decode    作者:deshima-dev    | 项目源码 | 文件源码
def full_like(array, fill_value, reverse=False, dtype=None, keepmeta=True):
    """Create an array of `fill_value` with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        fill_value (scalar or numpy.ndarray): Fill value or array.
        dtype (data-type, optional): If spacified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array filled with `fill_value`.
    """
    if keepmeta:
        return (dc.zeros_like(array) + fill_value).astype(dtype)
    else:
        return dc.full(array.shape, fill_value, dtype)
项目:decode    作者:deshima-dev    | 项目源码 | 文件源码
def empty_like(array, dtype=None, keepmeta=True):
    """Create an array of empty with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        dtype (data-type, optional): If spacified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array without initializing entries.
    """
    if keepmeta:
        return dc.empty(array.shape, dtype,
            tcoords=array.dca.tcoords, chcoords=array.dca.chcoords,
            scalarcoords=array.dca.scalarcoords, attrs=array.attrs, name=array.name
        )
    else:
        return dc.empty(array.shape, dtype)
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def is_data_dependent(fmto, data):
    """Check whether a formatoption is data dependent

    Parameters
    ----------
    fmto: Formatoption
        The :class:`Formatoption` instance to check
    data: xarray.DataArray
        The data array to use if the :attr:`~Formatoption.data_dependent`
        attribute is a callable

    Returns
    -------
    bool
        True, if the formatoption depends on the data"""
    if callable(fmto.data_dependent):
        return fmto.data_dependent(data)
    return fmto.data_dependent
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def can_decode(cls, ds, var):
        """
        Class method to determine whether the object can be decoded by this
        decoder class.

        Parameters
        ----------
        ds: xarray.Dataset
            The dataset that contains the given `var`
        var: xarray.Variable or xarray.DataArray
            The array to decode

        Returns
        -------
        bool
            True if the decoder can decode the given array `var`. Otherwise
            False

        Notes
        -----
        The default implementation returns True for any argument. Subclass this
        method to be specific on what type of data your decoder can decode
        """
        return True
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def is_triangular(self, var):
        """
        Test if a variable is on a triangular grid

        This method first checks the `grid_type` attribute of the variable (if
        existent) whether it is equal to ``"unstructered"``, then it checks
        whether the bounds are not two-dimensional.

        Parameters
        ----------
        var: xarray.Variable or xarray.DataArray
            The variable to check

        Returns
        -------
        bool
            True, if the grid is triangular, else False"""
        return str(var.attrs.get('grid_type')) == 'unstructured' or \
            self._check_triangular_bounds(var)[0]
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def test_auto_update(self):
        """Test the :attr:`psyplot.plotter.Plotter.no_auto_update` attribute"""
        data = xr.DataArray([])
        plotter = TestPlotter(data, auto_update=False)
        self.assertFalse(plotter.no_auto_update)
        data.psy.init_accessor(auto_update=False)
        plotter = TestPlotter(data, auto_update=False)
        self.assertTrue(plotter.no_auto_update)

        plotter.update(fmt1=1)
        self.assertEqual(plotter['fmt1'], '')
        self.assertEqual(plotter._registered_updates['fmt1'], 1)

        plotter.start_update()
        self.assertEqual(plotter['fmt1'], '1')
        self.assertFalse(plotter._registered_updates)

        data.psy.no_auto_update = False
        self.assertFalse(plotter.data.psy.no_auto_update)
        self.assertFalse(plotter.no_auto_update)
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def test_data_props_list(self):
        """Test the data properties of Formatoptions with an InteractiveList"""
        data = psyd.InteractiveList([xr.DataArray([]), xr.DataArray([])])
        plot_data = data.copy(True)
        plot_data.extend([xr.DataArray([]), xr.DataArray([])],
                         new_name=True)
        plotter = TestPlotter(data)
        plotter.plot_data = plot_data
        plot_data = plotter.plot_data  # the data might have been copied

        self.assertIs(plotter.fmt1.raw_data, data)
        self.assertIs(plotter.fmt1.data, plot_data)

        # test with index in list
        plotter.fmt1.index_in_list = 1
        self.assertIs(plotter.fmt1.raw_data, data[1])
        self.assertIs(plotter.fmt1.data, plot_data[1])

        # test with index in list of plot_data outside raw_data
        plotter.fmt1.index_in_list = 3
        self.assertIs(plotter.fmt1.data, plot_data[3])
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def test_any_decoder(self):
        """Test the decoder property with an InteractiveList"""
        data = psyd.InteractiveList([xr.DataArray([]), xr.DataArray([])])
        plot_data = data.copy(True)
        plot_data.extend([xr.DataArray([]), xr.DataArray([])],
                         new_name=True)
        for arr in data:
            arr.psy.init_accessor(decoder=psyd.CFDecoder(arr.psy.base))
        plotter = TestPlotter(data)
        plotter.plot_data = plot_data
        plot_data = plotter.plot_data  # the data might have been copied

        # test without index in list
        decoder = psyd.CFDecoder(data[0].psy.base)
        plotter.fmt2.decoder = decoder
        for i, d2 in enumerate(plotter.plot_data_decoder):
            self.assertIs(d2, decoder,
                          msg='Decoder %i has been set wrong!' % i)
        self.assertEqual(plotter.fmt2.decoder, plotter.plot_data_decoder)
        self.assertIs(plotter.fmt2.any_decoder, decoder)
项目:psyplot    作者:Chilipp    | 项目源码 | 文件源码
def test_has_changed(self):
        """Test the :meth:`psyplot.plotter.Plotter.show_summaries` method"""
        plotter = TestPlotter(xr.DataArray([]), fmt1='something')
        self.assertEqual(plotter['fmt1'], 'something')
        for i in range(1, 4):
            key = 'fmt%i' % i
            fmto = getattr(plotter, key)
            self.assertEqual(plotter.has_changed(key),
                             [fmto.default, plotter[key]],
                             msg="Wrong value for " + key)
        plotter.update()
        self.assertIsNone(plotter.has_changed('fmt1'))
        plotter.update(fmt1='test', fmt3=plotter.fmt3.default, force=True)
        self.assertEqual(plotter.has_changed('fmt1'),
                         ['something', 'test'])
        self.assertIsNone(plotter.has_changed('fmt2'))
        self.assertIsNone(plotter.has_changed('fmt3', include_last=False))
        self.assertEqual(plotter.has_changed('fmt3'),
                         [plotter.fmt3.default, plotter.fmt3.default])
项目:sympl    作者:mcgibbon    | 项目源码 | 文件源码
def create_variable(dataset, name, data):
    if isinstance(data, xr.DataArray):
        for i in range(len(data.dims)):
            try:
                if i == 0:  # time
                    ensure_dimension_exists(
                        dataset, data.dims[i], None)
                else:
                    ensure_dimension_exists(
                        dataset, data.dims[i], data.values.shape[i])
            except IOError as err:
                raise IOError(
                    'Error while creating {}: {}'.format(name, err))
        dataset.createVariable(
            name, data.values.dtype, data.dims)
        for key, value in data.attrs.items():
            dataset.variables[name].setncattr(key, value)
    else:
        raise TypeError('data must be of type DataArray')
项目:physt    作者:janpipek    | 项目源码 | 文件源码
def to_xarray(self):
        """Convert to xarray.Dataset

        Returns
        -------
        xarray.Dataset
        """
        import xarray as xr
        data_vars = {
            "frequencies": xr.DataArray(self.frequencies, dims="bin"),
            "errors2": xr.DataArray(self.errors2, dims="bin"),
            "bins": xr.DataArray(self.bins, dims=("bin", "x01"))
        }
        coords = {}
        attrs = {
            "underflow": self.underflow,
            "overflow": self.overflow,
            "inner_missed": self.inner_missed,
            "keep_missed": self.keep_missed
        }
        attrs.update(self._meta_data)
        # TODO: Add stats
        return xr.Dataset(data_vars, coords, attrs)
项目:elm    作者:ContinuumIO    | 项目源码 | 文件源码
def second_layer_input_matrix(X, models):
    '''Build a second layer model input matrix by taking the
    metadata from X given to the first layer models and forming
    a new matrix from the 1-D predictions of the first layer models
    '''
    preds = predict_many(dict(X=X), to_raster=False,
                         ensemble=models)
    example = preds[0].flat
    input_matrix = np.empty((example.shape[0], len(preds)))
    for j, pred in enumerate(preds):
        input_matrix[:, j] = pred.flat.values[:, 0]
    attrs = X.attrs.copy()
    attrs['old_dims'] = [X[SOIL_MOISTURE].dims] * len(preds)
    attrs['canvas'] = X[SOIL_MOISTURE].canvas
    tags = [tag for tag, _ in models]
    arr = xr.DataArray(input_matrix,
                       coords=[('space', example.space),
                               ('band', tags)],
                       dims=('space', 'band'),
                       attrs=attrs)
    return xr.Dataset(dict(flat=arr), attrs=attrs)
项目:elm    作者:ContinuumIO    | 项目源码 | 文件源码
def kmeans_aic(model, X, **kwargs):
    '''AIC (Akaike Information Criterion) for k-means for model selection

    Parameters:
        :model:  An elm.pipeline.Pipeline with KMeans or MiniBatchKMeans as final step in Pipeline
        :X:      The X data that were just given to "fit", or "partial_fit"
        :kwargs: placeholder - ignored

    Returns:
        :AIC: float

    '''

    k, m = model._estimator.cluster_centers_.shape
    if isinstance(X, xr.DataArray):
        n = X.flat.values.shape[0]
    else:
        n = X.shape[0]
    d = model._estimator.inertia_
    aic =  d + 2 * m * k
    delattr(model._estimator, 'labels_')
    return aic
项目:xlearn    作者:wy2136    | 项目源码 | 文件源码
def predict(self, da):
        '''xarray.DataArray version of sklearn.cluster.KMeans.fit.'''

        # compatible with the sklean.cluster.KMeans predict method when the input data is not DataArray
        if not isinstance(da, xr.DataArray):
            return super().predict(da)

        # retrieve parameters
        n_samples = da.shape[0]
        features_shape = da.shape[1:]
        n_features = np.prod(features_shape)

        X = da.data.reshape(n_samples, n_features)# 'data' might be replaced with 'values'.
        # remove NaN values if exists in X
        try:
            X_valid = X[:, self.valid_features_index_]
        except:
            X_valid = X
        samples_dim = da.dims[0]
        samples_coord = {samples_dim: da.coords[samples_dim]}
        labels = xr.DataArray(super().predict(X_valid),
            dims=samples_dim, coords=samples_coord)

        return labels
项目:piradar    作者:scivision    | 项目源码 | 文件源码
def plasmaprop(iono:DataArray, f:float,B0:float):
    assert isinstance(iono,DataArray)
    Ne = iono.loc[:,'ne'].astype(float)
    w = 2*np.pi*f

    wp = np.sqrt(Ne*e**2/(eps0*me)) # electron plasma frequency [rad/sec]
    wH = B0*e/me               # electron gyrofrequency [rad/sec]


    if (w <= wp).any(): # else reflection doesn't occur, passes right through (radar freq > MUF)
        reflectionheight = iono.alt_km[abs(w-wp).argmin()]
    else:
        print(f'radar freq {f/1e6:.1f} MHz  >  max. plasma freq {wp.max()/(2*np.pi)/1e6:.1f} MHz: no reflection')
        reflectionheight = None

    return wp,wH,reflectionheight
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
def test_to_xarray(self):

        tm._skip_if_no_xarray()
        from xarray import DataArray

        p = tm.makePanel()

        result = p.to_xarray()
        self.assertIsInstance(result, DataArray)
        self.assertEqual(len(result.coords), 3)
        assert_almost_equal(list(result.coords.keys()),
                            ['items', 'major_axis', 'minor_axis'])
        self.assertEqual(len(result.dims), 3)

        # idempotency
        assert_panel_equal(result.to_pandas(), p)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
def test_to_xarray(self):

        tm._skip_if_no_xarray()
        from xarray import DataArray

        p = tm.makePanel4D()

        result = p.to_xarray()
        self.assertIsInstance(result, DataArray)
        self.assertEqual(len(result.coords), 4)
        assert_almost_equal(list(result.coords.keys()),
                            ['labels', 'items', 'major_axis', 'minor_axis'])
        self.assertEqual(len(result.dims), 4)

        # non-convertible
        self.assertRaises(ValueError, lambda: result.to_pandas())
项目:pyndl    作者:quantling    | 项目源码 | 文件源码
def test_activation_matrix():
    weights = xr.DataArray(np.array([[0, 1, 0], [1, 0, 0]]),
                           coords={
                               'outcomes': ['o1', 'o2'],
                               'cues': ['c1', 'c2', 'c3']
                           },
                           dims=('outcomes', 'cues'))

    events = [(['c1', 'c2', 'c3'], []),
              (['c1', 'c3'], []),
              (['c2'], []),
              (['c1', 'c1'], [])]
    reference_activations = np.array([[1, 0, 1, 0], [1, 1, 0, 1]])

    with pytest.raises(ValueError):
        activations = activation(events, weights, number_of_threads=1)

    activations = activation(events, weights, number_of_threads=1, remove_duplicates=True)
    activations_mp = activation(events, weights, number_of_threads=3, remove_duplicates=True)

    assert np.allclose(reference_activations, activations)
    assert np.allclose(reference_activations, activations_mp)
项目:earthio    作者:ContinuumIO    | 项目源码 | 文件源码
def load_subdataset(subdataset, attrs, layer_spec, **reader_kwargs):
    '''Load a single subdataset'''
    import gdal
    data_file = gdal.Open(subdataset)
    np_arr = data_file.ReadAsArray(**reader_kwargs)
    out = _np_arr_to_coords_dims(np_arr,
                 layer_spec,
                 reader_kwargs,
                 geo_transform=None,
                 layer_meta=attrs,
                 handle=data_file)
    np_arr, coords, dims, attrs2 = out
    attrs.update(attrs2)
    return xr.DataArray(data=np_arr,
                        coords=coords,
                        dims=dims,
                        attrs=attrs)
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _append_dataarray_extra_attrs(xarr,**extra_kwargs):
    """Update the dictionnary of attributes a xarray dataarray (xarr.attrs).

    Parameters
    ----------
    xarr : xarray.DataArray
        The function will add extra arguments to xarr.attrs
    **extra_kwargs
        not used

    Returns
    -------
    da : xarray.DataArray
    """
    if not(is_xarray(xarr)):
        raise TypeError('except a xarray.DataArray')
    for kwargs in extra_kwargs:
        xarr.attrs[kwargs] = extra_kwargs[kwargs]
    return xarr
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _grid_location_equals(xarr,grid_location=None):
    """Return True when the xarr grid_location attribute is grid_location

    Parameters
    ----------
    xarr : xarray.DataArray
       xarray dataarray which attributes should be tested.
    grid_location : str
        string describing the grid location : eg 'u','v','t','f'...

    Returns
    -------
    test : bool
        boolean value of the test.

    """
    test = True
    if xarr.attrs.has_key('grid_location'):
        test *= (xarr.attrs['grid_location']==grid_location)
    return test
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _di(scalararray):
    """Return the difference scalararray(i+1) - scalararray(i).

    A priori, for internal use only.

    Parameters
    ----------
    scalararray : xarray.DataArray
        xarray that should be differentiated.

    Returns
    -------
    di : xarray.DataArray
       xarray of difference, defined at point i+1/2
    """
    di = scalararray.shift(x=-1) - scalararray
    return di
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _dj(scalararray):
    """Return the difference scalararray(j+1) - scalararray(j)

    A priori, for internal use only.

    Parameters
    ----------
    scalararray : xarray.DataArray
        xarray that should be differentiated.

    Returns
    -------
    dj : xarray.DataArray
       xarray of difference, defined at point j+1/2
    """
    dj = scalararray.shift(y=-1) - scalararray
    return dj
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _mi(scalararray):
    """Return the average of scalararray(i+1) and scalararray(i)

    A priori, for internal use only.

    Parameters
    ----------
    scalararray : xarray.DataArray
        xarray that should be averaged at i+1/2.

    Returns
    -------
    mi : xarray.DataArray
       averaged xarray, defined at point i+1/2
    """
    mi = ( scalararray.shift(x=-1) + scalararray ) / 2.
    return mi
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def _finalize_dataarray_attributes(xarr,**kwargs):
    """Update the dictionary of attibutes of a xarray dataarray.

    Parameters
    ----------
    xarr : xarray.DataArray
       xarray dataarray which attributes will be updated.
    kwargs : dict-like object
       dictionnary of attributes

    Returns
    -------
    xarr : xarray.DataArray
    """
    if isinstance(xarr, xr.DataArray):
        xarr.attrs.update(kwargs)
    if xarr.attrs.has_key('short_name'):
        xarr.name = xarr.attrs['short_name']
    return xarr
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def chunk(self,chunks=None):
        """Rechunk all the variables defining the grid.

        Parameters
        ----------
        chunks : dict-like
            dictionnary of sizes of chunk along xarray dimensions.

        Example
        -------
        >>> xr_chunk = {'x':200,'y':200}
        >>> grd = generic_2d_grid(...)
        >>> grd.chunk(xr_chunk)

        """
        for dataname in self._arrays:
            data = self._arrays[dataname]
            if isinstance(data, xr.DataArray):
                self._arrays[dataname] = data.chunk(chunks)
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_u_to_t(self,scalararray,conserving='x_flux'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
              - 'area' : conserves the area
              - 'x_flux' : conserves the flux in x-direction (eastward)
              - 'y_flux' : conserves the flux in y-direction (northward)
        """
        check_input_array(scalararray,\
                          chunks=self.chunks,grid_location='u',ndims=self.ndims)
        wi, wo = self._weights_for_change_grid_location(input='u',output='t',
                                                        conserving=conserving)
        out = self._to_western_grid_location(scalararray,weights_in=wi,
                                                         weights_out=wo)
        return _append_dataarray_extra_attrs(out,grid_location='t')
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_t_to_v(self,scalararray,conserving='area'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
              - 'area' : conserves the area
              - 'x_flux' : conserves the flux in x-direction (eastward)
              - 'y_flux' : conserves the flux in y-direction (northward)
        """
        check_input_array(scalararray,\
                          chunks=self.chunks,grid_location='t',ndims=self.ndims)
        wi, wo = self._weights_for_change_grid_location(input='t',output='v',
                                                        conserving=conserving)
        out = self._to_northern_grid_location(scalararray,weights_in=wi,
                                                         weights_out=wo)
        return _append_dataarray_extra_attrs(out,grid_location='v')
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_v_to_t(self,scalararray,conserving='y_flux'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
              - 'area' : conserves the area
              - 'x_flux' : conserves the flux in x-direction (eastward)
              - 'y_flux' : conserves the flux in y-direction (northward)
        """
        check_input_array(scalararray,\
                          chunks=self.chunks,grid_location='v',ndims=self.ndims)
        wi, wo = self._weights_for_change_grid_location(input='v',output='t',
                                                        conserving=conserving)
        out = self._to_southern_grid_location(scalararray,weights_in=wi,
                                                         weights_out=wo)
        return _append_dataarray_extra_attrs(out,grid_location='t')
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_f_to_u(self,scalararray,conserving='x_flux'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
              - 'area' : conserves the area
              - 'x_flux' : conserves the flux in x-direction (eastward)
              - 'y_flux' : conserves the flux in y-direction (northward)
        """
        check_input_array(scalararray,\
                          chunks=self.chunks,grid_location='f',ndims=self.ndims)
        wi, wo = self._weights_for_change_grid_location(input='f',output='u',
                                                        conserving=conserving)
        out = self._to_southern_grid_location(scalararray,weights_in=wi,
                                                         weights_out=wo)
        return _append_dataarray_extra_attrs(out,grid_location='u')
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_f_to_v(self,scalararray,conserving='y_flux'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
              - 'area' : conserves the area
              - 'x_flux' : conserves the flux in x-direction (eastward)
              - 'y_flux' : conserves the flux in y-direction (northward)
        """
        check_input_array(scalararray,\
                          chunks=self.chunks,grid_location='f',ndims=self.ndims)
        wi, wo = self._weights_for_change_grid_location(input='f',output='v',
                                                        conserving=conserving)
        out = self._to_southern_grid_location(scalararray,weights_in=wi,
                                                         weights_out=wo)
        return _append_dataarray_extra_attrs(out,grid_location='v')
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def change_grid_location_u_to_v(self,scalararray,conserving='area'):
        """Return a xarray corresponding to scalararray averaged at a new
        grid location.

        Parameters
        ----------
        scalararray : xarray.DataArray
            original array to be relocated
        conserving : str
            any of 'area', 'x_flux' or 'y_flux'.
             - 'area' : conserves the area
             - 'x_flux' : conserves the flux in x-direction (eastward)
             - 'y_flux' : conserves the flux in y-direction (northward)
        """
        # first move to t-point
        newarr = self.change_grid_location_u_to_t(scalararray,
                                                  conserving=conserving)
        # then move t v-point
        out = self.change_grid_location_t_to_v(newarr,
                                                  conserving=conserving)
        return out

#---------------------------- Vector Operators ---------------------------------
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def norm_of_vectorfield(self,vectorfield):
        """Return the norm of a vector field, at t-point.

        So far, only available for vector fields at u,v grid_location

        Parameters
        ----------
        vectorfield : VectorField2d namedtuple
            so far only valid for vectorfield at u,v-points

        Return
        ------
        scalararray : xarray.DataArray
            xarray with a specified grid_location, so far t-point only
        """
        return xu.sqrt(self.scalar_product(vectorfield,vectorfield))
项目:oocgcm    作者:lesommer    | 项目源码 | 文件源码
def boundary_weights(self, mode='reflect', drop_dims=None):
        """
        Compute the boundary weights

        Parameters
        ----------
            mode:

            drop_dims:
                Specify dimensions along which the mask is constant

        Returns
        -------
        """
        mask = self.obj.notnull()
        new_dims = copy.copy(self.obj.dims)
        new_coords = copy.copy(self.coords)
        for dim in drop_dims:
            #TODO: Make the function work
            mask = mask.isel({dim:0})
            del(new_dims[dim])
            del(new_coords[dim])
        weights = im.convolve(mask.astype(float), self.coefficients, mode=mode)
        res = xr.DataArray(weights, dims=new_dims, coords=new_coords, name='boundary weights')
        return res.where(mask == 1)
项目:regionmask    作者:mathause    | 项目源码 | 文件源码
def _create_xarray_2D(mask, lon_or_obj, lat, lon_name, lat_name):
    """create an xarray DataArray for 2D fields"""

    lon2D, lat2D = _extract_lon_lat(lon_or_obj, lat, lon_name, lat_name)

    if isinstance(lon2D, xr.DataArray):
        dim1D_names = lon2D.dims
        dim1D_0 = lon2D[dim1D_names[0]]
        dim1D_1 = lon2D[dim1D_names[1]]
    else:
        dim1D_names = (lon_name + '_idx', lat_name + '_idx')
        dim1D_0 = np.arange(np.array(lon2D).shape[0])
        dim1D_1 = np.arange(np.array(lon2D).shape[1])

    # dict with the coordinates
    coords = {dim1D_names[0]: dim1D_0,
              dim1D_names[1]: dim1D_1,
              lat_name: (dim1D_names, lat2D),
              lon_name: (dim1D_names, lon2D)}

    mask = xr.DataArray(mask, coords = coords, dims=dim1D_names)

    return mask
项目:regionmask    作者:mathause    | 项目源码 | 文件源码
def test__mask_xarray_in_out_2D():
    # create xarray DataArray with 2D dims

    coords = {'lat_1D': [1, 2],
              'lon_1D': [1, 2],
              'lat_2D': (('lat_1D', 'lon_1D'), lat_2D),
              'lon_2D': (('lat_1D', 'lon_1D'), lon_2D)}

    d = np.random.rand(2, 2)

    data = xr.DataArray(d, coords = coords, dims=('lat_1D', 'lon_1D'))


    expected = expected_mask()
    result = r1.mask(data, lon_name='lon_2D', lat_name='lat_2D')

    assert isinstance(result, xr.DataArray)
    assert np.allclose(result, expected, equal_nan=True)
    assert np.allclose(result.lat_2D, lat_2D)
    assert np.allclose(result.lon_2D, lon_2D)

    assert np.allclose(result.lat_1D, [1, 2])
    assert np.allclose(result.lon_1D, [1, 2])
项目:xarray_filters    作者:ContinuumIO    | 项目源码 | 文件源码
def ts_probs(dset, bins=None, axis=0, dim=None, layer=None,
             log_counts=False, log_probs=False, names=None,
             keep_attrs=True, chunks=None):
    '''Fixed or unevenly spaced histogram binning for
    the time dimension of a 3-D cube DataArray in X
    Parameters:
        dset: MLDataset
        axis: Integer like 0, 1, 2 to indicate which is the time axis of cube
        layer: The name of the DataArray in MLDataset to run scipy.describe on
        bins: Passed to np.histogram
        log_probs: Return probabilities associated with log counts? True / False
    '''
    layer = _validate_layer(dset, layer)
    def each_arr(arr, layer):
        return resize_each_1d_slice(arr, _hist_1d, bins=bins,
                        axis=axis, dim=dim, layer=layer,
                        log_counts=log_counts,
                        log_probs=log_probs,
                        names=names,
                        keep_attrs=keep_attrs,
                        chunks=chunks)
    return concat_ml_features(*(each_arr(dset[_], layer) for _ in layer))
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def _apply_window(da, dims, window_type='hanning'):
    """Creating windows in dimensions dims."""

    if window_type not in ['hanning']:
        raise NotImplementedError("Only hanning window is supported for now.")

    numpy_win_func = getattr(np, window_type)

    if da.chunks:
        def dask_win_func(n):
            return dsar.from_delayed(
                delayed(numpy_win_func, pure=True)(n),
                (n,), float)
        win_func = dask_win_func
    else:
        win_func = numpy_win_func

    windows = [xr.DataArray(win_func(len(da[d])),
               dims=da[d].dims, coords=da[d].coords) for d in dims]

    return da * reduce(operator.mul, windows[::-1])
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def test_dft_4d():
    """Test the discrete Fourier transform on 2D data"""
    N = 16
    da = xr.DataArray(np.random.rand(N,N,N,N),
                    dims=['time','z','y','x'],
                    coords={'time':range(N),'z':range(N),
                            'y':range(N),'x':range(N)}
                     )
    ft = xrft.dft(da, shift=False)
    npt.assert_almost_equal(ft.values, np.fft.fftn(da.values))

    with pytest.raises(NotImplementedError):
        xrft.dft(da, detrend='linear')
    with pytest.raises(NotImplementedError):
        xrft.dft(da, dim=['time','y','x'], detrend='linear')

    da_prime = xrft.detrendn(da[:,0].values, [0,1,2]) # cubic detrend over time, y, and x
    npt.assert_almost_equal(xrft.dft(da[:,0].drop('z'),
                                    dim=['time','y','x'],
                                    shift=False, detrend='linear'
                                    ).values,
                            np.fft.fftn(da_prime))
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def test_dft_3d_dask():
    """Test the discrete Fourier transform on 3D dask array data"""
    N=16
    da = xr.DataArray(np.random.rand(N,N,N), dims=['time','x','y'],
                      coords={'time':range(N),'x':range(N),
                              'y':range(N)}
                     )
    daft = xrft.dft(da.chunk({'time': 1}), dim=['x','y'], shift=False)
    # assert hasattr(daft.data, 'dask')
    npt.assert_almost_equal(daft.values,
                        np.fft.fftn(da.chunk({'time': 1}).values, axes=[1,2])
                           )

    with pytest.raises(ValueError):
        xrft.dft(da.chunk({'time': 1, 'x': 1}), dim=['x'])

    daft = xrft.dft(da.chunk({'x': 1}), dim=['time'],
                    shift=False, detrend='linear')
    # assert hasattr(daft.data, 'dask')
    da_prime = sps.detrend(da.chunk({'x': 1}), axis=0)
    npt.assert_almost_equal(daft.values,
                        np.fft.fftn(da_prime, axes=[0])
                           )
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def test_power_spectrum_dask():
    """Test the power spectrum function on dask data"""
    N = 16
    dim = ['x','y']
    da = xr.DataArray(np.random.rand(2,N,N), dims=['time','x','y'],
                      coords={'time':range(2),'x':range(N),
                              'y':range(N)}).chunk({'time': 1}
                     )
    ps = xrft.power_spectrum(da, dim=dim, density=False)
    daft = xrft.dft(da, dim=['x','y'])
    npt.assert_almost_equal(ps.values, (daft * np.conj(daft)).real.values)

    ps = xrft.power_spectrum(da, dim=dim, window=True, detrend='constant')
    daft = xrft.dft(da, dim=dim, window=True, detrend='constant')
    coord = list(daft.coords)
    test = (daft * np.conj(daft)).real/N**4
    for i in dim:
        test /= daft['freq_' + i + '_spacing']
    npt.assert_almost_equal(ps.values, test)
    npt.assert_almost_equal(np.ma.masked_invalid(ps).mask.sum(), 0.)
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def test_cross_spectrum():
    """Test the cross spectrum function"""
    N = 16
    da = xr.DataArray(np.random.rand(N,N), dims=['x','y'],
                    coords={'x':range(N),'y':range(N)}
                     )
    da2 = xr.DataArray(np.random.rand(N,N), dims=['x','y'],
                    coords={'x':range(N),'y':range(N)}
                     )
    cs = xrft.cross_spectrum(da, da2, window=True, density=False,
                            detrend='constant')
    daft = xrft.dft(da,
                    dim=None, shift=True, detrend='constant',
                    window=True)
    daft2 = xrft.dft(da2,
                    dim=None, shift=True, detrend='constant',
                    window=True)
    npt.assert_almost_equal(cs.values, np.real(daft*np.conj(daft2)))
    npt.assert_almost_equal(np.ma.masked_invalid(cs).mask.sum(), 0.)
项目:xrft    作者:rabernat    | 项目源码 | 文件源码
def test_spacing_tol(test_data_1d):
    da = test_data_1d
    da2 = da.copy().load()

    # Create improperly spaced data
    Nx = 16
    Lx = 1.0
    x  = np.linspace(0, Lx, Nx)
    x[-1] = x[-1] + .001
    da3 = xr.DataArray(np.random.rand(Nx), coords=[x], dims=['x']) 

    # This shouldn't raise an error
    xrft.dft(da3, spacing_tol=1e-1)
    # But this should 
    with pytest.raises(ValueError):
        xrft.dft(da3, spacing_tol=1e-4)
项目:experiment    作者:darothen    | 项目源码 | 文件源码
def _master_dataarray(exp, data_dict):
    case_list = [exp._case_data[case] for case in exp.cases]
    stacked_data = _stack_dims(data_dict, case_list, {}, exp)

    test_case = next(exp.all_cases())
    test_da = data_dict[test_case]
    name = test_da.name

    new_coords = test_da.to_dataset().coords
    logger.debug("Creating master dataarray")
    for case in exp.cases:
        logger.debug("   " + case)
        new_coords[case] = exp._case_data[case].vals

    new_dims = list(exp.cases) + list(test_da.dims)

    new_da = DataArray(stacked_data, coords=new_coords,
                       dims=new_dims)
    new_da = copy_attrs(test_da, new_da)
    new_da.name = name

    return new_da
项目:linearmodels    作者:bashtage    | 项目源码 | 文件源码
def test_xarray_1d(self):
        x_np = np.random.randn(10)
        x = xr.DataArray(x_np)
        dh = IVData(x, 'some_variable')
        assert_equal(dh.ndarray, x_np[:, None])
        assert dh.rows == list(np.arange(10))
        assert dh.cols == ['some_variable.0']
        expected = pd.DataFrame(x_np, columns=dh.cols, index=dh.rows)
        assert_frame_equal(expected, dh.pandas)

        index = pd.date_range('2017-01-01', periods=10)
        x = xr.DataArray(x_np,
                         [('time', index)])
        dh = IVData(x, 'some_variable')
        assert_equal(dh.ndarray, x_np[:, None])
        assert_series_equal(pd.Series(dh.rows), pd.Series(list(index)))
        assert dh.cols == ['some_variable.0']
        expected = pd.DataFrame(x_np[:, None], columns=dh.cols, index=dh.rows)
        assert_frame_equal(expected, dh.pandas)
项目:linearmodels    作者:bashtage    | 项目源码 | 文件源码
def test_xarray_2d(self):
        x_np = np.random.randn(10, 2)
        x = xr.DataArray(x_np)
        dh = IVData(x)
        assert_equal(dh.ndarray, x_np)
        assert dh.rows == list(np.arange(10))
        assert dh.cols == ['x.0', 'x.1']
        expected = pd.DataFrame(x_np, columns=dh.cols, index=dh.rows)
        assert_frame_equal(expected, dh.pandas)

        index = pd.date_range('2017-01-01', periods=10)
        x = xr.DataArray(x_np,
                         [('time', index),
                          ('variables', ['apple', 'banana'])])
        dh = IVData(x)
        assert_equal(dh.ndarray, x_np)
        assert_series_equal(pd.Series(dh.rows), pd.Series(list(index)))
        assert dh.cols == ['apple', 'banana']
        expected = pd.DataFrame(x_np, columns=dh.cols, index=dh.rows)
        assert_frame_equal(expected, dh.pandas)