# unidata/netcdf4-python architecture

> Architecture diagram of the GitHub repository unidata/netcdf4-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/unidata/netcdf4-python
- Repository: https://github.com/unidata/netcdf4-python

## Overview

1. **What this repository is**
   - `netcdf4-python` is a Python package that exposes the netCDF data model through a Pythonic API backed by the native netCDF C library.
   - The repo is primarily a **language binding** project, not an application: most architecture revolves around bridging Python/numpy objects to C-level netCDF/HDF5 storage.

2. **Top-level runtime boundary**
   - The main user-facing package lives in `src/netCDF4/`.
   - Public imports are centered on `src/netCDF4/__init__.py`, which exposes the package API, while `_netCDF4.pyx` contains the compiled extension implementation.
   - The boundary is: **Python API in pure Python + Cython wrapper + native netCDF/HDF5 libraries**.

3. **Core data model**
   - The package maps netCDF concepts into Python objects such as datasets, groups, dimensions, variables, attributes, and special datatypes.
   - The design supports classic netCDF formats as well as netCDF-4/HDF5-backed features like groups, compound types, enums, variable-length types, strings, compression, and quantization.

4. **Compiled extension layer**
   - `src/netCDF4/_netCDF4.pyx` is the central Cython source that calls into the netCDF C API.
   - `include/netCDF4.pxi`, `include/netcdf-compat.h`, and `include/mpi-compat.h` provide compile-time declarations and compatibility glue.
   - `include/membuf.pyx` supports in-memory buffer handling, which is part of the package’s I/O surface.

5. **Native dependency stack**
   - The extension depends on the external netCDF C library, HDF5, and numpy.
   - Optional capabilities are wired through the native stack:
     - MPI / parallel I/O when netCDF/HDF5 are built with MPI support.
     - Compression filters and plugins for formats like zstd, bzip2, blosc, and szip.
     - OpenDAP/HTTPS access, which in newer releases depends on `certifi` for certificate discovery.

6. **Specialized native helper code**
   - `external/nc_complex` is an embedded helper library for complex-number support.
   - This is relevant because complex values are not a first-class netCDF classic primitive; the repository includes its own support layer for mapping complex types into netCDF-compatible representations.

7. **Package utilities and compatibility**
   - `src/netCDF4/utils.py` contains Python-side helper logic used by the package.
   - `_build/backend.py` and `_build/utils.py` indicate custom build support code used during extension compilation and packaging.
   - `checkversion.py` suggests release/version consistency checks as part of the project’s maintenance workflow.

8. **Build and packaging architecture**
   - `pyproject.toml`, `setup.py`, `setup.cfg`, and `MANIFEST.in` define packaging and extension build behavior.
   - The project is intended to be built into platform-specific wheels, including manylinux/macOS/Windows variants and special builds such as free-threaded Python and ARM64, as reflected in the CI workflow files.
   - The build is therefore a significant subsystem, not just a side concern.

9. **Documentation and examples as integration surfaces**
   - `docs/` and `docs/index.html` provide generated project documentation.
   - `examples/` demonstrates the major runtime usage patterns: reading/writing files, compression, string handling, complex numbers, MPI access, diskless/in-memory use, threading, and subsetting.
   - These examples are useful for understanding the supported execution paths and feature boundaries.

10. **Test architecture**
   - `test/` is extensive and mirrors the feature surface of the library rather than a single end-to-end app flow.
   - Test files are organized by capability: compression filters, DAP, masking/scaling, compound types, enums, slicing, diskless/open_mem, parallel/open/close behavior, unicode, and format variants like CDF5 and NETCDF3/4.
   - The presence of many data fixtures (`.nc` files) indicates the tests validate real binary interoperability with the netCDF format, not just mocked behavior.

11. **Operational modes and data flow**
   - The principal data flow is from Python/numpy objects into the Cython extension, then into the netCDF C library, and finally into on-disk HDF5/netCDF files or in-memory buffers.
   - Reads reverse that flow, with the extension translating native netCDF values into numpy arrays, masked arrays, strings, and structured dtypes.
   - Several alternate paths exist: diskless datasets, memory-backed datasets, multi-file aggregation, and remote OPeNDAP sources.

12. **CI and release automation**
   - `.github/workflows/` contains build matrices for different release branches and platform combinations.
   - `cibuildwheel.yml` and `miniconda.yml` show that wheel creation and conda-oriented validation are part of the project architecture.
   - Dependabot and stubtest allowlist files indicate ongoing dependency management and type-compatibility checking.

13. **Important architectural constraint**
   - Threading is constrained by the native stack: the repository notes that netcdf-c is not thread-safe, and the package uses internal locking, so concurrent access semantics are an important boundary.
   - Parallel I/O is a separate capability from threading and depends on MPI-enabled native libraries.

14. **How to draw the diagram**
   - A good architecture diagram would show:
     - Python application code using the `netCDF4` package API
     - the `src/netCDF4` Python layer
     - the compiled `_netCDF4` Cython extension
     - external dependencies: netCDF C, HDF5, numpy, optional MPI, optional compression plugins, optional `certifi`/OPeNDAP
     - supporting paths for docs/examples/tests/build/CI
   - The dominant boundary is between **pure Python** and **native file-format/IO implementation**.

Main entry points: [`src/netCDF4/_netCDF4.pyx`](https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/_netCDF4.pyx) (Cython core), [`src/netCDF4/__init__.py`](https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/__init__.py) (API entry), [`test/run_all.py`](https://github.com/unidata/netcdf4-python/blob/master/test/run_all.py) (Test suite), [`pyproject.toml`](https://github.com/unidata/netcdf4-python/blob/master/pyproject.toml) (Build config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_python_api["Python API"]
  node_node_init["API entry<br/>package init<br/>[__init__.py]"]
  node_node_utils["Python utils<br/>helpers<br/>[utils.py]"]
end

subgraph group_group_native_layer["Native Layer"]
  node_node_extension["Cython core<br/>binding extension<br/>[_netCDF4.pyx]"]
  node_node_pxi["C API glue<br/>compile-time includes<br/>[netCDF4.pxi]"]
  node_node_compat["Compat headers<br/>native compatibility<br/>[netcdf-compat.h]"]
  node_node_membuf["Mem buffer<br/>in-memory I/O helper<br/>[membuf.pyx]"]
  node_node_complex["Complex helper<br/>embedded native helper<br/>[nc_complex.c]"]
end

subgraph group_group_build_release["Build & Release"]
  node_node_build["Build config<br/>packaging<br/>[pyproject.toml]"]
  node_node_backend["Build backend<br/>build helpers<br/>[backend.py]"]
  node_node_release["Version checks<br/>maintenance<br/>[checkversion.py]"]
end

subgraph group_group_validation["Validation"]
  node_node_docs["Docs<br/>documentation<br/>[index.html]"]
  node_node_examples["Examples<br/>usage samples<br/>[README.md]"]
  node_node_tests["Test suite<br/>verification<br/>[run_all.py]"]
end

node_node_app(("Python apps<br/>consumer"))
node_node_native_stack[("netCDF/HDF5<br/>native dependencies")]
node_node_numpy[("NumPy<br/>array runtime")]
node_node_mpi(("MPI I/O<br/>optional parallel support"))
node_node_filters(("Compression filters<br/>optional plugins"))
node_node_certifi(("HTTPS certs<br/>optional dependency"))

node_node_app -->|"imports"| node_node_init
node_node_init -->|"exposes"| node_node_extension
node_node_init -->|"uses"| node_node_utils
node_node_extension -->|"includes"| node_node_pxi
node_node_extension -->|"includes"| node_node_compat
node_node_extension -->|"supports"| node_node_membuf
node_node_extension -->|"maps"| node_node_complex
node_node_extension -->|"adapts"| node_node_numpy
node_node_extension -->|"calls"| node_node_native_stack
node_node_extension -.->|"optional"| node_node_mpi
node_node_extension -.->|"optional"| node_node_filters
node_node_extension -.->|"optional"| node_node_certifi
node_node_build -->|"configures"| node_node_backend
node_node_backend -->|"builds"| node_node_extension
node_node_release -->|"checks"| node_node_build
node_node_docs -->|"documents"| node_node_init
node_node_examples -->|"demonstrates"| node_node_extension
node_node_tests -->|"verifies"| node_node_extension
node_node_tests -->|"interoperates"| node_node_native_stack

click node_node_init "https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/__init__.py"
click node_node_utils "https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/utils.py"
click node_node_extension "https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/_netCDF4.pyx"
click node_node_pxi "https://github.com/unidata/netcdf4-python/blob/master/include/netCDF4.pxi"
click node_node_compat "https://github.com/unidata/netcdf4-python/blob/master/include/netcdf-compat.h"
click node_node_membuf "https://github.com/unidata/netcdf4-python/blob/master/include/membuf.pyx"
click node_node_complex "https://github.com/unidata/netcdf4-python/blob/master/external/nc_complex/src/nc_complex.c"
click node_node_build "https://github.com/unidata/netcdf4-python/blob/master/pyproject.toml"
click node_node_backend "https://github.com/unidata/netcdf4-python/blob/master/_build/backend.py"
click node_node_release "https://github.com/unidata/netcdf4-python/blob/master/checkversion.py"
click node_node_docs "https://github.com/unidata/netcdf4-python/blob/master/docs/index.html"
click node_node_examples "https://github.com/unidata/netcdf4-python/blob/master/examples/README.md"
click node_node_tests "https://github.com/unidata/netcdf4-python/blob/master/test/run_all.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_init,node_node_utils toneBlue
class node_node_extension,node_node_pxi,node_node_compat,node_node_membuf,node_node_complex toneAmber
class node_node_build,node_node_backend,node_node_release toneMint
class node_node_docs,node_node_examples,node_node_tests toneRose
class node_node_app,node_node_native_stack,node_node_numpy,node_node_mpi,node_node_filters,node_node_certifi toneNeutral
```

## Components

### Python API

User-facing package surface and Python-side helpers

- [API entry](https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/__init__.py): package init · Public package exports and import surface · `src/netCDF4/__init__.py`
- [Python utils](https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/utils.py): helpers · Package-side helper logic used by the API · `src/netCDF4/utils.py`

### Native Layer

Cython bridge, compile-time glue, and embedded native helpers

- [Cython core](https://github.com/unidata/netcdf4-python/blob/master/src/netCDF4/_netCDF4.pyx): binding extension · Compiled wrapper implementing the main netCDF4 object model and I/O bridge · `src/netCDF4/_netCDF4.pyx`
- [C API glue](https://github.com/unidata/netcdf4-python/blob/master/include/netCDF4.pxi): compile-time includes · Cython declarations and compatibility headers for the native extension · `include/netCDF4.pxi`
- [Compat headers](https://github.com/unidata/netcdf4-python/blob/master/include/netcdf-compat.h): native compatibility · netCDF and MPI compatibility shims used during compilation · `include/netcdf-compat.h`
- [Mem buffer](https://github.com/unidata/netcdf4-python/blob/master/include/membuf.pyx): in-memory I/O helper · Support for buffer-backed reads and writes · `include/membuf.pyx`
- [Complex helper](https://github.com/unidata/netcdf4-python/blob/master/external/nc_complex/src/nc_complex.c): embedded native helper · Support library for complex-number mapping in netCDF storage · `external/nc_complex/src/nc_complex.c`

### Build & Release

Packaging, extension build, and release automation

- [Build config](https://github.com/unidata/netcdf4-python/blob/master/pyproject.toml): packaging · Project packaging and extension build configuration · `pyproject.toml`
- [Build backend](https://github.com/unidata/netcdf4-python/blob/master/_build/backend.py): build helpers · Custom build-time helpers for extension compilation · `_build/backend.py`
- [Version checks](https://github.com/unidata/netcdf4-python/blob/master/checkversion.py): maintenance · Release-time consistency and version validation · `checkversion.py`

### Validation

Documentation, examples, and tests that exercise supported behaviors

- [Docs](https://github.com/unidata/netcdf4-python/blob/master/docs/index.html): documentation · Generated project documentation and site assets · `docs/index.html`
- [Examples](https://github.com/unidata/netcdf4-python/blob/master/examples/README.md): usage samples · Executable and notebook examples covering major supported modes · `examples/README.md`
- [Test suite](https://github.com/unidata/netcdf4-python/blob/master/test/run_all.py): verification · Feature-oriented tests and binary interoperability fixtures · `test/run_all.py`

### Other components

- **Python apps**: consumer · External Python code using the netCDF4 package API
- **netCDF/HDF5**: native dependencies · External netCDF C library and HDF5 storage backend
- **NumPy**: array runtime · Array and dtype bridge used by the Python extension
- **MPI I/O**: optional parallel support · Parallel access path when native libraries are built with MPI
- **Compression filters**: optional plugins · Optional codec/filter ecosystem for compressed storage
- **HTTPS certs**: optional dependency · Certificate discovery used for remote OPeNDAP access

## Connections

- Python apps → API entry: imports · User code imports the package API
- API entry → Cython core: exposes · Public API is backed by the compiled extension
- API entry → Python utils: uses · Package API relies on Python helper routines
- Cython core → C API glue: includes · Extension compilation uses Cython declarations and glue
- Cython core → Compat headers: includes · Extension compilation uses compatibility headers
- Cython core → Mem buffer: supports · Extension provides in-memory buffer I/O paths
- Cython core → Complex helper: maps · Extension uses complex-number support code
- Cython core → NumPy: adapts · Python arrays and dtypes are translated through NumPy
- Cython core → netCDF/HDF5: calls · Core implementation delegates storage and metadata to native netCDF/HDF5
- Cython core → MPI I/O: optional · Parallel I/O is available when native MPI support exists
- Cython core → Compression filters: optional · Compression and filter behavior depend on native plugins
- Cython core → HTTPS certs: optional · Remote HTTPS/OPeNDAP access can use certificate discovery
- Build config → Build backend: configures · Packaging configuration drives extension build behavior
- Build backend → Cython core: builds · Build helpers compile the Cython extension
- Version checks → Build config: checks · Release workflow validates build/version consistency
- Docs → API entry: documents · Documentation describes the public package surface
- Examples → Cython core: demonstrates · Examples exercise the main runtime and I/O paths
- Test suite → Cython core: verifies · Tests cover the compiled extension and format behaviors
- Test suite → netCDF/HDF5: interoperates · Tests validate real netCDF/HDF5 file compatibility
