ntv-numpy.ntv_numpy.xarray_accessors

The xarray_accessors module is part of the ntv-numpy.ntv_numpy package.

It contains the class NnpDatasetAccessors.

For more information, see the user guide or the github repository.

 1# -*- coding: utf-8 -*-
 2"""
 3The `xarray_accessors` module is part of the `ntv-numpy.ntv_numpy` package.
 4
 5It contains the class `NnpDatasetAccessors`.
 6
 7For more information, see the
 8[user guide](https://loco-philippe.github.io/ntv-numpy/docs/user_guide.html)
 9 or the [github repository](https://github.com/loco-philippe/ntv-numpy).
10"""
11
12import xarray as xr
13
14from ntv_numpy.xdataset import Xdataset
15
16try:
17    # delete the accessor to avoid warning
18    del xr.Dataset.nnp
19except AttributeError:
20    pass
21
22
23@xr.register_dataset_accessor("nxr")
24class NxrDatasetAccessor:
25    """Accessor class for methods invoked as `xr.Dataset.nxr.*`"""
26
27    def __init__(self, xarray_obj):
28        """initialisation of the class"""
29        self._obj = xarray_obj
30
31    def to_dataframe(self, **kwargs):
32        """Accessor for method `Xdataset.from_xarray.to_dataframe` invoked as
33        xr.Dataset.nxr.to_dataframe`.
34
35        *Parameters*
36
37        - **ntv_type**: Boolean (default True) - if False use full_name else json_name
38        - **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
39        - **dims**: list of string (default None) - order of dimensions full_name to apply
40        - **index**: Boolean (default True) - if True, dimensions are translated into indexes
41        """
42        return Xdataset.from_xarray(self._obj, **kwargs).to_dataframe(**kwargs)
43
44    def to_scipp(self, **kwargs):
45        """Accessor for method `Xdataset.from_xarray.to_scipp` invoked as
46        xr.Dataset.nxr.to_scipp`.
47
48        *Parameters*
49
50        - **dataset** : Boolean (default True) - if False and a single data_var,
51        return a DataArray
52        - **info** : Boolean (default True) - if True return an additional DataGroup with
53        metadata and data_arrays
54        - **ntv_type** : Boolean (default True) - if True add ntv_type to the name
55        """
56        return Xdataset.from_xarray(self._obj, **kwargs).to_scipp(**kwargs)
57
58    def to_json(self, **kwargs):
59        """Accessor for method `Xdataset.from_xarray.to_json` invoked as
60        xr.Dataset.nxr.to_json`.
61
62        *Parameters*
63
64        - **encoded** : Boolean (default False) - json value if False else json text
65        - **header** : Boolean (default True) - including 'xdataset' type
66        - **notype** : list of Boolean (default list of None) - including data type if False
67        - **novalue** : Boolean (default False) - including value if False
68        - **noshape** : Boolean (default True) - if True, without shape if dim < 1
69        - **format** : list of string (default list of 'full') - representation
70        format of the ndarray,
71
72        """
73        return Xdataset.from_xarray(self._obj, **kwargs).to_json(**kwargs)
@xr.register_dataset_accessor('nxr')
class NxrDatasetAccessor:
24@xr.register_dataset_accessor("nxr")
25class NxrDatasetAccessor:
26    """Accessor class for methods invoked as `xr.Dataset.nxr.*`"""
27
28    def __init__(self, xarray_obj):
29        """initialisation of the class"""
30        self._obj = xarray_obj
31
32    def to_dataframe(self, **kwargs):
33        """Accessor for method `Xdataset.from_xarray.to_dataframe` invoked as
34        xr.Dataset.nxr.to_dataframe`.
35
36        *Parameters*
37
38        - **ntv_type**: Boolean (default True) - if False use full_name else json_name
39        - **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
40        - **dims**: list of string (default None) - order of dimensions full_name to apply
41        - **index**: Boolean (default True) - if True, dimensions are translated into indexes
42        """
43        return Xdataset.from_xarray(self._obj, **kwargs).to_dataframe(**kwargs)
44
45    def to_scipp(self, **kwargs):
46        """Accessor for method `Xdataset.from_xarray.to_scipp` invoked as
47        xr.Dataset.nxr.to_scipp`.
48
49        *Parameters*
50
51        - **dataset** : Boolean (default True) - if False and a single data_var,
52        return a DataArray
53        - **info** : Boolean (default True) - if True return an additional DataGroup with
54        metadata and data_arrays
55        - **ntv_type** : Boolean (default True) - if True add ntv_type to the name
56        """
57        return Xdataset.from_xarray(self._obj, **kwargs).to_scipp(**kwargs)
58
59    def to_json(self, **kwargs):
60        """Accessor for method `Xdataset.from_xarray.to_json` invoked as
61        xr.Dataset.nxr.to_json`.
62
63        *Parameters*
64
65        - **encoded** : Boolean (default False) - json value if False else json text
66        - **header** : Boolean (default True) - including 'xdataset' type
67        - **notype** : list of Boolean (default list of None) - including data type if False
68        - **novalue** : Boolean (default False) - including value if False
69        - **noshape** : Boolean (default True) - if True, without shape if dim < 1
70        - **format** : list of string (default list of 'full') - representation
71        format of the ndarray,
72
73        """
74        return Xdataset.from_xarray(self._obj, **kwargs).to_json(**kwargs)

Accessor class for methods invoked as xr.Dataset.nxr.*

NxrDatasetAccessor(xarray_obj)
28    def __init__(self, xarray_obj):
29        """initialisation of the class"""
30        self._obj = xarray_obj

initialisation of the class

def to_dataframe(self, **kwargs):
32    def to_dataframe(self, **kwargs):
33        """Accessor for method `Xdataset.from_xarray.to_dataframe` invoked as
34        xr.Dataset.nxr.to_dataframe`.
35
36        *Parameters*
37
38        - **ntv_type**: Boolean (default True) - if False use full_name else json_name
39        - **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
40        - **dims**: list of string (default None) - order of dimensions full_name to apply
41        - **index**: Boolean (default True) - if True, dimensions are translated into indexes
42        """
43        return Xdataset.from_xarray(self._obj, **kwargs).to_dataframe(**kwargs)

Accessor for method Xdataset.from_xarray.to_dataframe invoked as xr.Dataset.nxr.to_dataframe`.

Parameters

  • ntv_type: Boolean (default True) - if False use full_name else json_name
  • info: Boolean (default True) - if True add xdt.info in DataFrame.attrs
  • dims: list of string (default None) - order of dimensions full_name to apply
  • index: Boolean (default True) - if True, dimensions are translated into indexes
def to_scipp(self, **kwargs):
45    def to_scipp(self, **kwargs):
46        """Accessor for method `Xdataset.from_xarray.to_scipp` invoked as
47        xr.Dataset.nxr.to_scipp`.
48
49        *Parameters*
50
51        - **dataset** : Boolean (default True) - if False and a single data_var,
52        return a DataArray
53        - **info** : Boolean (default True) - if True return an additional DataGroup with
54        metadata and data_arrays
55        - **ntv_type** : Boolean (default True) - if True add ntv_type to the name
56        """
57        return Xdataset.from_xarray(self._obj, **kwargs).to_scipp(**kwargs)

Accessor for method Xdataset.from_xarray.to_scipp invoked as xr.Dataset.nxr.to_scipp`.

Parameters

  • dataset : Boolean (default True) - if False and a single data_var, return a DataArray
  • info : Boolean (default True) - if True return an additional DataGroup with metadata and data_arrays
  • ntv_type : Boolean (default True) - if True add ntv_type to the name
def to_json(self, **kwargs):
59    def to_json(self, **kwargs):
60        """Accessor for method `Xdataset.from_xarray.to_json` invoked as
61        xr.Dataset.nxr.to_json`.
62
63        *Parameters*
64
65        - **encoded** : Boolean (default False) - json value if False else json text
66        - **header** : Boolean (default True) - including 'xdataset' type
67        - **notype** : list of Boolean (default list of None) - including data type if False
68        - **novalue** : Boolean (default False) - including value if False
69        - **noshape** : Boolean (default True) - if True, without shape if dim < 1
70        - **format** : list of string (default list of 'full') - representation
71        format of the ndarray,
72
73        """
74        return Xdataset.from_xarray(self._obj, **kwargs).to_json(**kwargs)

Accessor for method Xdataset.from_xarray.to_json invoked as xr.Dataset.nxr.to_json`.

Parameters

  • encoded : Boolean (default False) - json value if False else json text
  • header : Boolean (default True) - including 'xdataset' type
  • notype : list of Boolean (default list of None) - including data type if False
  • novalue : Boolean (default False) - including value if False
  • noshape : Boolean (default True) - if True, without shape if dim < 1
  • format : list of string (default list of 'full') - representation format of the ndarray,