Skip to content

Commit

Permalink
Update example code in README : Fix public module namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-mo committed Jan 4, 2024
1 parent a9f6921 commit d905147
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ This allows the user to mix+match features from either package in code.

For example:
``` python
from ncdata.iris_xarray import dataset_to_cubes, cubes_to_dataset
from ncdata.iris_xarray import cubes_to_xarray, cubes_from_xarray

# Apply Iris regridder to xarray data
dataset = xarray.open_dataset('file1.nc')
cube, = dataset_to_cubes(dataset)
cube, = cubes_from_xarray(dataset)
cube2 = cube.regrid(grid_cube, iris.analysis.PointInCell)
dataset2 = cubes_to_dataset(cube2)
dataset2 = cubes_to_xarray(cube2)

# Apply Xarray statistic to Iris data
cubes = iris.load('file1.nc')
dataset = cubes_to_dataset(cubes)
dataset = cubes_to_xarray(cubes)
dataset2 = dataset.group_by('time.dayofyear').argmin()
cubes2 = dataset_to_cubes(dataset2)
cubes2 = cubes_from_xarray(dataset2)
```
* data conversion is equivalent to writing to a file with one library, and reading it
back with the other ..
Expand All @@ -68,13 +68,14 @@ dimension control.
For example:
``` python
from ncdata.xarray import from_xarray
from ncdata.iris import to_cubes
from ncdata.iris import to_iris
from ncdata.netcdf4 import to_nc4, from_nc4

# Rename a dimension in xarray output
dataset = xr.open_dataset('file1.nc')
xr_ncdata = from_xarray(dataset)
dim = xr_ncdata.dimensions.pop('dim0')
dim.name = 'newdim'
xr_ncdata.dimensions['newdim'] = dim
for var in xr_ncdata.variables.values():
var.dimensions = [
Expand All @@ -90,7 +91,7 @@ for var in ncdata.variables:
100.e6 if dim == 'dim0' else -1
for dim in var.dimensions
)
cubes = to_cubes(ncdata)
cubes = to_iris(ncdata)
```

### Manipulation of data
Expand Down
2 changes: 2 additions & 0 deletions lib/ncdata/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from . import NcData
from .dataset_like import Nc4DatasetLike

__all__ = ["from_iris", "to_iris"]

#
# The primary conversion interfaces
#
Expand Down
2 changes: 2 additions & 0 deletions lib/ncdata/iris_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from .iris import from_iris, to_iris
from .xarray import from_xarray, to_xarray

__all__ = ["cubes_from_xarray", "cubes_to_xarray"]


def cubes_from_xarray(
xrds: xarray.Dataset, xr_save_kwargs=None, iris_load_kwargs=None
Expand Down
3 changes: 3 additions & 0 deletions lib/ncdata/netcdf4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

from . import NcAttribute, NcData, NcDimension, NcVariable

__all__ = ["from_nc4", "to_nc4"]


# The variable arguments which are 'claimed' by the existing to_nc4 code, and
# therefore not valid to appear in the 'var_kwargs' parameter.
_Forbidden_Variable_Kwargs = ["data", "dimensions", "datatype", "_FillValue"]
Expand Down

0 comments on commit d905147

Please sign in to comment.