cdflib
raw JSON → 1.3.10 verified Fri May 01 auth: no python
A Python library for reading and writing NASA Common Data Format (CDF) files. Current version 1.3.10, released 2025-05. Maintained by LASP, supports Python >=3.9. Includes integration with xarray and handles ISTP compliance checks.
pip install cdflib Common errors
error ModuleNotFoundError: No module named 'cdflib.cdf' ↓
cause Incorrect import path; CDF is a class in the top-level cdflib package, not a submodule.
fix
Use
from cdflib import CDF instead. error AttributeError: module 'cdflib' has no attribute 'cdf_to_xarray' ↓
cause Attempting to import cdf_to_xarray from a wrong location or missing install (e.g., without xarray dependency).
fix
Install xarray (
pip install xarray) and import using from cdflib import cdf_to_xarray. error TypeError: 'numpy.float64' object cannot be interpreted as an integer ↓
cause Passing a numpy float as start/count index to varget(). varget() expects Python ints.
fix
Cast to int:
start=int(start_val) or use varget('var', start=[int(s) for s in start]). Warnings
breaking In version 1.3.3, the deprecated functions `from_unixtime`, `from_datetime`, `unixtime_to_cdftt2000`, `datetime_to_cdftt2000`, and `datetime64_to_cdftt2000` were fully removed. Use `CDFtime` class or `astropy.time.Time` conversion instead. ↓
fix Replace `from cdflib.util import from_unixtime` with `CDFtime(utc).tt2000` or use astropy.
gotcha When using `cdf_to_xarray`, `DELTA_VAR` variables are no longer promoted to coordinates (since 1.3.5). If you rely on them as coordinates, you must manually add them via xarray. ↓
fix After calling `cdf_to_xarray`, do `ds = ds.assign_coords(delta_var=ds['delta_var'])` if needed.
gotcha The CDF class is not thread-safe. Do not share a CDF object across threads; open separate instances per thread. ↓
fix Use a new CDF instance per thread (or use read locks).
deprecated As of version 1.3.10, `CDF.varget()` with `start` and `count` parameters uses 0-based indexing. Some users mistakenly use 1-based indexing from IDL/old CDF standards. ↓
fix Always use 0-based indices: `cdf.varget('var', start=[0], count=[10])`.
Imports
- CDF wrong
from cdflib.cdf import CDFcorrectfrom cdflib import CDF - cdf_to_xarray wrong
from cdflib.xarray import cdf_to_xarraycorrectfrom cdflib import cdf_to_xarray - xarray_to_cdf wrong
from cdflib.xarray import xarray_to_cdfcorrectfrom cdflib import xarray_to_cdf
Quickstart
from cdflib import CDF
cdf_file = CDF('example.cdf')
info = cdf_file.globalattsget()
print(info)
vars = cdf_file.cdf_info()
print(vars)
data = cdf_file.varget('variable_name')
print(data.shape)
cdf_file.close()