Skip to content

Commit

Permalink
Xarray accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
loco-philippe committed May 20, 2024
1 parent 48b0a4a commit ce63555
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
35 changes: 27 additions & 8 deletions ntv_numpy/xarray_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
except AttributeError:
pass

@xr.register_dataset_accessor("nnp")
class NnpDatasetAccessor:
"""Accessor class for methods invoked as `xr.Dataset.nnp.*`"""
@xr.register_dataset_accessor("nxr")
class NxrDatasetAccessor:
"""Accessor class for methods invoked as `xr.Dataset.nxr.*`"""

def __init__(self, xarray_obj):
'''initialisation of the class'''
self._obj = xarray_obj

def to_dataframe(self, **kwargs):
"""Accessor for method `Xdataset.from_xarray.to_dataframe` invoked as
xr.Dataset.nnp.to_dataframe`.
xr.Dataset.nxr.to_dataframe`.
*Parameters*
Expand All @@ -41,12 +41,31 @@ def to_dataframe(self, **kwargs):

def to_scipp(self, **kwargs):
"""Accessor for method `Xdataset.from_xarray.to_scipp` invoked as
xr.Dataset.nnp.to_scipp`.
xr.Dataset.nxr.to_scipp`.
*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
- **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
"""
return Xdataset.from_xarray(self._obj, **kwargs).to_scipp(**kwargs)

def to_json(self, **kwargs):
"""Accessor for method `Xdataset.from_xarray.to_json` invoked as
xr.Dataset.nxr.to_scipp`.
*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,
"""
return Xdataset.from_xarray(self._obj, **kwargs).to_json(**kwargs)
4 changes: 2 additions & 2 deletions ntv_numpy/xdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ def to_scipp(self, **kwargs):
- **dataset** : Boolean (default True) - if False and a single data_var,
return a DataArray
- **info** : Boolean (default True) - if True return a DataGroup with
- **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
- **ntv_type** : Boolean (default True) - if True add ntv_type to the name
'''
return ScippConnec.xexport(self, **kwargs)

Expand Down

0 comments on commit ce63555

Please sign in to comment.