ncdata


Namencdata JSON
Version 0.3.1 PyPI version JSON
download
home_pageNone
SummaryAbstract NetCDF data objects, providing fast data transfer between analysis packages.
upload_time2025-11-07 10:23:52
maintainerNone
docs_urlNone
authorNone
requires_python<3.14,>=3.7
licenseBSD-3-Clause
keywords cf-metadata data-analysis netcdf iris xarray
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ncdata

<img src="docs/_static/ncdata_logo_full.png" alt="ncdata logo" width="400" class="center">

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: 
  * [stable](https://ncdata.readthedocs.io/en/stable/index.html)
  * [latest](https://ncdata.readthedocs.io/en/latest/index.html)

# Demonstration code examples:
  * [Apply Iris regrid to xarray data](#apply-iris-regrid-to-xarray-data)
  * [Use Zarr data in Iris](#use-zarr-data-in-iris)
  * [Correct a mis-coded attribute in Iris input](#correct-a-miscoded-attribute-in-iris-input)
  * [Rename a dimension in xarray output](#rename-a-dimension-in-xarray-output)
  * [Copy selected data to a new file](#copy-selected-data-to-a-new-file)

## Apply Iris regrid to xarray data
``` python
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
``` python
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
``` python
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
``` python
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
``` python
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")
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ncdata",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.14,>=3.7",
    "maintainer_email": null,
    "keywords": "cf-metadata, data-analysis, netcdf, iris, xarray",
    "author": null,
    "author_email": "Patrick Peglar <patrick.peglar@metoffice.gov.uk>",
    "download_url": "https://files.pythonhosted.org/packages/79/69/6975596486447fc80b8ee453a106654d4b3b5a2b6620226464663b03e45e/ncdata-0.3.1.tar.gz",
    "platform": null,
    "description": "# ncdata\n\n<img src=\"docs/_static/ncdata_logo_full.png\" alt=\"ncdata logo\" width=\"400\" class=\"center\">\n\nGeneric NetCDF data in Python.\n\nRepresents raw netCDF data in Python, with no structural assumptions or interpretations,\nand provides facilities to inspect and manipulate it with complete freedom.\n\n### Also\nProvides fast data exchange between analysis packages, and full control of storage\nformatting.\n\nEspecially : Ncdata **exchanges data between Xarray and Iris** as efficiently as possible  \n> \"lossless, copy-free and lazy-preserving\".\n\nThis enables the user to freely mix+match operations from both projects, getting the\n\"best of both worlds\".\n  > import xarray  \n  > import ncdata.iris_xarray as nci  \n  > import iris.quickplot as qplt  \n  >   \n  > ds = xarray.open_dataset(filepath)  \n  > ds_resample = ds.rolling(time=3).mean()  \n  > cubes = nci.cubes_from_xarray(ds_resample)  \n  > temp_cube = cubes.extract_cube(\"air_temperature\")  \n  > qplt.contourf(temp_cube[0])\n\n# Purposes\n  * represent netcdf data as structures of Python objects\n  * easy manipulation of netcdf data with pythonic syntax\n  * Fast and efficient translation of data between Xarray and Iris objects.\n     * This allows the user to mix+match features from either package in code. \n\nSee : https://ncdata.readthedocs.io/en/latest/userdocs/user_guide/design_principles.html\n\n# Documentation\nOn ReadTheDocs.  Please see: \n  * [stable](https://ncdata.readthedocs.io/en/stable/index.html)\n  * [latest](https://ncdata.readthedocs.io/en/latest/index.html)\n\n# Demonstration code examples:\n  * [Apply Iris regrid to xarray data](#apply-iris-regrid-to-xarray-data)\n  * [Use Zarr data in Iris](#use-zarr-data-in-iris)\n  * [Correct a mis-coded attribute in Iris input](#correct-a-miscoded-attribute-in-iris-input)\n  * [Rename a dimension in xarray output](#rename-a-dimension-in-xarray-output)\n  * [Copy selected data to a new file](#copy-selected-data-to-a-new-file)\n\n## Apply Iris regrid to xarray data\n``` python\nfrom ncdata.iris_xarray import cubes_to_xarray, cubes_from_xarray\ndataset = xarray.open_dataset(\"file1.nc\", chunks=\"auto\")\n(cube,) = cubes_from_xarray(dataset)\ncube2 = cube.regrid(grid_cube, iris.analysis.PointInCell)\ndataset2 = cubes_to_xarray(cube2)\n```\n\n## Use Zarr data in Iris\n``` python\nfrom ncdata.threadlock_sharing import enable_lockshare\nenable_lockshare(iris=True, xarray=True)\nimport xarray as xr\ndataset = xr.open_dataset(input_zarr_path, engine=\"zarr\", chunks=\"auto\")\ninput_cubes = cubes_from_xarray(dataset)\noutput_cubes = my_process(input_cubes)\ndataset2 = cubes_to_xarray(output_cubes)\ndataset2.to_zarr(output_zarr_path)\n``` \n\n## Correct a miscoded attribute in Iris input\n``` python\nfrom ncdata.iris import to_iris\nenable_lockshare(iris=True)\nncdata = from_nc4(input_path)\nfor var in ncdata.variables.values():\n    if \"coords\" in var.attributes:\n        var.attributes.rename(\"coords\", \"coordinates\")\ncubes = to_iris(ncdata)\n```\n\n## Rename a dimension in xarray output\n``` python\nenable_lockshare(xarray=True)\ndataset = xr.open_dataset(\"file1.nc\")\nxr_ncdata = from_xarray(dataset)\nfrom ncdata.utils import rename_dimension\nrename_dimension(xr_ncdata, \"dim0\", \"newdim\")\nto_nc4(ncdata, \"file_2a.nc\")\n```\n\n## Copy selected data to a new file\n``` python\nfrom ncdata.netcdf4 import from_nc4, to_nc4\nncdata = from_nc4(\"file1.nc\")\n\n# Make a list of partial names to select the wanted variables\nkeys = [\"air_\", \"surface\"]\n\n# Explicitly add dimension names, to include all the dimension variables\nkeys += list(ncdata.dimensions)\n\n# Identify the wanted variables\nselect_vars = [\n    var\n    for var in ncdata.variables.values()\n    if any(var.name.startswith(key) for key in keys)\n]\n\n# Add any referenced coordinate variables\nfor var in select_vars:\n    coordnames = var.avals.get(\"coordinates\")\n    if coordnames:\n        for coordname in coordnames.split(\" \"):\n            select_vars.append(ncdata.variables[coordname])\n\n# Replace variables with only the wanted ones\nncdata.variables.clear()\nncdata.variables.addall(select_vars)\n\n# Save\nto_nc4(ncdata, \"pruned.nc\")\n```\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Abstract NetCDF data objects, providing fast data transfer between analysis packages.",
    "version": "0.3.1",
    "project_urls": {
        "Code": "https://github.com/pp-mo/ncdata",
        "Discussions": "https://github.com/pp-mo/ncdata/discussions",
        "Documentation": "https://ncdata.readthedocs.io",
        "Issues": "https://github.com/pp-mo/ncdata/issues"
    },
    "split_keywords": [
        "cf-metadata",
        " data-analysis",
        " netcdf",
        " iris",
        " xarray"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0ad7c08eba5ba43c44907ef367073590b6d19366ad0ba4fd92e9323ea4ac7a5b",
                "md5": "10f1ca8ac69df2bd5d62fe05db4d8fe0",
                "sha256": "ba4505614e6b8a157a812c07d01168788c87eb566d9d510ea556483fd3887651"
            },
            "downloads": -1,
            "filename": "ncdata-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "10f1ca8ac69df2bd5d62fe05db4d8fe0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.14,>=3.7",
            "size": 43124,
            "upload_time": "2025-11-07T10:23:50",
            "upload_time_iso_8601": "2025-11-07T10:23:50.790973Z",
            "url": "https://files.pythonhosted.org/packages/0a/d7/c08eba5ba43c44907ef367073590b6d19366ad0ba4fd92e9323ea4ac7a5b/ncdata-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "79696975596486447fc80b8ee453a106654d4b3b5a2b6620226464663b03e45e",
                "md5": "3ab05a1fc3ec78b21050247eb4a04237",
                "sha256": "f2101220ccf11eb070c20197d30546062044184a843a1a278241b290f9077ed6"
            },
            "downloads": -1,
            "filename": "ncdata-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3ab05a1fc3ec78b21050247eb4a04237",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.14,>=3.7",
            "size": 604247,
            "upload_time": "2025-11-07T10:23:52",
            "upload_time_iso_8601": "2025-11-07T10:23:52.747486Z",
            "url": "https://files.pythonhosted.org/packages/79/69/6975596486447fc80b8ee453a106654d4b3b5a2b6620226464663b03e45e/ncdata-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-11-07 10:23:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pp-mo",
    "github_project": "ncdata",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ncdata"
}
        
Elapsed time: 4.40784s