Skip to main content

Prepare xarray Datasets for CF-1.12 compliant NetCDF output

Project description

nc-check

Prepare xarray.Dataset objects to be written as CF-1.12-compliant NetCDF files.

Install

uv sync

Install optional CF checker dependencies (recommended for full CF checks):

uv sync --extra cf

Usage

import xarray as xr
import nc_check  # Registers the .check dataset accessor

ds = xr.Dataset(
    data_vars={"temp": (("time", "lat", "lon"), [[[280.0]]])},
    coords={"time": [0], "lat": [10.0], "lon": [20.0]},
)

issues = ds.check.compliance(report_format="python")
fixed = ds.check.make_cf_compliant()
ocean_report = ds.check.ocean_cover(report_format="python")
time_report = ds.check.time_cover(report_format="python")

By default (report_format="auto"), reports adapt to environment:

  • Jupyter notebooks: HTML
  • CLI terminals: rich tables
  • Other environments (scripts/tests): Python dicts

You can request rich table reports printed to stdout:

ds.check.compliance(report_format="tables")

CLI usage:

# Prints readable rich tables to stdout (default behavior)
nc-check input.nc

# Explicit check modes
nc-check compliance input.nc
nc-check compliance input.nc --engine heuristic
nc-check ocean-cover input.nc
nc-check time-cover input.nc
nc-check all input.nc
nc-check all input.nc --engine cfchecker

# Explicit coordinate names when they differ from lon/lat/time
nc-check ocean-cover input.nc --lon-name x --lat-name y --time-name t
nc-check time-cover input.nc --time-name t
nc-check all input.nc --lon-name x --lat-name y --time-name t

# Saves an HTML report beside input.nc as input_report.html
nc-check input.nc --save-report

# With "all", saves one combined report:
# input_all_report.html
nc-check all input.nc --save-report

For notebooks, use HTML output and optionally save it:

html = ds.check.compliance(
    report_format="html",
    report_html_file="cf-report.html",
)

You can choose which conventions to check:

ds.check.compliance(conventions="cf,ferret")
ds.check.compliance(conventions="ferret")  # custom-only checks
ds.check.compliance(engine="heuristic")  # force built-in heuristic engine
ds.check.compliance(engine="cfchecker")  # require cfchecker path

# Explicit coordinate names
ds.check.ocean_cover(lon_name="x", lat_name="y", time_name="t")
ds.check.time_cover(time_name="t")
ds.check.all(lon_name="x", lat_name="y", time_name="t")

compliance() runs cf-checker against an in-memory NetCDF payload created from dataset metadata (no .nc file written to disk). With report_format="python" it returns a dictionary of detected issues.

make_cf_compliant() returns a new dataset with safe automatic fixes, including:

  • Conventions = "CF-1.12"
  • standard coordinate attributes for inferred time, lat, and lon axes
  • creation of missing dimension coordinates for inferred axes
  • global extent attributes derived from inferred axes: time_coverage_start, time_coverage_end, geospatial_lat_min, geospatial_lat_max, geospatial_lon_min, and geospatial_lon_max

ocean_cover() runs fast ocean-grid checks and returns a report with:

  • east/west edge-of-map detection (persistent missing longitude columns, reported by longitude),
  • land/ocean sanity checks at fixed reference points (offset detection).
  • report_format="auto" by default.
  • When var_name is omitted, all data variables with inferred lat/lon dimensions are checked.

time_cover() runs time-dimension missing-data checks and reports missing time-slice ranges.

  • report_format="auto" by default.
  • When var_name is omitted, all data variables are checked.
  • Time coordinate units/type validation is reported by compliance().

You can disable individual checks:

ds.check.ocean_cover(
    check_edge_of_map=True,
    check_land_ocean_offset=True,
    report_format="python",
)
ds.check.time_cover(
    report_format="python",
)

Notes:

  • cfchecker requires the system udunits2 library via cfunits.
  • For large files, prefer opening with lazy chunks: xr.open_dataset(path, chunks={}).
  • The built-in ferret convention flags coordinate _FillValue usage as an error.
  • If cfchecker cannot run, compliance() falls back to heuristic checks and includes a checker_error field in the response.
  • You can choose the compliance engine explicitly with engine="heuristic" or engine="cfchecker" (CLI: --engine).
  • The report engine field reflects what ran: cfchecker, heuristic, or none (when CF checks are skipped, e.g. conventions="ferret").
  • You can bias standard-name suggestions by domain, e.g. ds.check.compliance(domain="ocean") (also supports atmosphere, land, cryosphere, and biogeochemistry).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nc_check-0.2.0.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nc_check-0.2.0-py3-none-any.whl (44.5 kB view details)

Uploaded Python 3

File details

Details for the file nc_check-0.2.0.tar.gz.

File metadata

  • Download URL: nc_check-0.2.0.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nc_check-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c50d9c0b869aaf78f48594cf95ab7adbda44216aaf1a4b1515ba21f195a0d849
MD5 79f0c0938b8ecb3b8a4afb898bd68df8
BLAKE2b-256 d00bba70ed481e453549d2f4d02dbfc67c4857e58676b9e64e29b2352cdab998

See more details on using hashes here.

File details

Details for the file nc_check-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nc_check-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 44.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nc_check-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1330219e74ce371b3412ad0f0dbac1b064c6ed7e2a86ecd4565da86c8d8a55f
MD5 69e29cd64e63175b785d877060aa84a0
BLAKE2b-256 3ef254651e51c4dbd761b4e127b1c23312d90df36181ce32716ee7a1b3e6efc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page