Skip to main content

ncdata

ncdata logo

Generic NetCDF data in Python.

Represents raw netCDF data in Python, with no structural assumptions or interpretations, and provides facilities to inspect and manipulate it with complete freedom.

Also

Provides fast data exchange between analysis packages, and full control of storage formatting.

Especially : Ncdata exchanges data between Xarray and Iris as efficiently as possible

"lossless, copy-free and lazy-preserving".

This enables the user to freely mix+match operations from both projects, getting the "best of both worlds".

import xarray
import ncdata.iris_xarray as nci
import iris.quickplot as qplt

ds = xarray.open_dataset(filepath)
ds_resample = ds.rolling(time=3).mean()
cubes = nci.cubes_from_xarray(ds_resample)
temp_cube = cubes.extract_cube("air_temperature")
qplt.contourf(temp_cube[0])

Purposes

  • represent netcdf data as structures of Python objects
  • easy manipulation of netcdf data with pythonic syntax
  • Fast and efficient translation of data between Xarray and Iris objects.
    • This allows the user to mix+match features from either package in code.

See : https://ncdata.readthedocs.io/en/latest/userdocs/user_guide/design_principles.html

Documentation

On ReadTheDocs. Please see:

Demonstration code examples:

Apply Iris regrid to xarray data

from ncdata.iris_xarray import cubes_to_xarray, cubes_from_xarray
dataset = xarray.open_dataset("file1.nc", chunks="auto")
(cube,) = cubes_from_xarray(dataset)
cube2 = cube.regrid(grid_cube, iris.analysis.PointInCell)
dataset2 = cubes_to_xarray(cube2)

Use Zarr data in Iris

from ncdata.threadlock_sharing import enable_lockshare
enable_lockshare(iris=True, xarray=True)
import xarray as xr
dataset = xr.open_dataset(input_zarr_path, engine="zarr", chunks="auto")
input_cubes = cubes_from_xarray(dataset)
output_cubes = my_process(input_cubes)
dataset2 = cubes_to_xarray(output_cubes)
dataset2.to_zarr(output_zarr_path)

Correct a miscoded attribute in Iris input

from ncdata.iris import to_iris
enable_lockshare(iris=True)
ncdata = from_nc4(input_path)
for var in ncdata.variables.values():
    if "coords" in var.attributes:
        var.attributes.rename("coords", "coordinates")
cubes = to_iris(ncdata)

Rename a dimension in xarray output

enable_lockshare(xarray=True)
dataset = xr.open_dataset("file1.nc")
xr_ncdata = from_xarray(dataset)
from ncdata.utils import rename_dimension
rename_dimension(xr_ncdata, "dim0", "newdim")
to_nc4(ncdata, "file_2a.nc")

Copy selected data to a new file

from ncdata.netcdf4 import from_nc4, to_nc4
ncdata = from_nc4("file1.nc")

# Make a list of partial names to select the wanted variables
keys = ["air_", "surface"]

# Explicitly add dimension names, to include all the dimension variables
keys += list(ncdata.dimensions)

# Identify the wanted variables
select_vars = [
    var
    for var in ncdata.variables.values()
    if any(var.name.startswith(key) for key in keys)
]

# Add any referenced coordinate variables
for var in select_vars:
    coordnames = var.avals.get("coordinates")
    if coordnames:
        for coordname in coordnames.split(" "):
            select_vars.append(ncdata.variables[coordname])

# Replace variables with only the wanted ones
ncdata.variables.clear()
ncdata.variables.addall(select_vars)

# Save
to_nc4(ncdata, "pruned.nc")

Release files for ncdata 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ncdata 0.4.0
File Size Uploaded
ncdata-0.4.0.tar.gz 608.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ncdata 0.4.0
File Interpreter ABI Platform
ncdata-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 651.3 kB

Release files / ncdata-0.4.0.tar.gz

Download URL ncdata-0.4.0.tar.gz
Size 608.2 kB
Tags Source
SHA-256 checksum
How to use checksums
de83c69dd78dce75fb1825bbbcdc9b9b1e274af10d5c089fcbc1e9adfc27e09b
BLAKE2b-256 checksum
How to use checksums
9b660ac8802f7b9bd2832b0e6b24cd9a13dfed2e0508dc6097a9936948c60212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / ncdata-0.4.0-py3-none-any.whl

Download URL ncdata-0.4.0-py3-none-any.whl
Size 43.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42716c1d1ad0903b1765179111b9e8f6a2db8d43e2dbbc5ff77efa32a7a0a38b
BLAKE2b-256 checksum
How to use checksums
911760e2a9d5061140d69dca331bb4716a175355b32bf69c5e20cc0b9ed6d5fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page