netCDF4 Python Interface

1.7.4 · active · verified Thu Apr 09

The netCDF4-python library provides an object-oriented Python interface to the netCDF C library, enabling interaction with NetCDF version 4 and older NetCDF 3 format files. It leverages HDF5 for advanced features like hierarchical groups, zlib compression, and new data types. Currently at version 1.7.4, the library is actively maintained with frequent releases, including several minor updates and patches each year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a NetCDF4 file, define dimensions (including an unlimited dimension for time), create variables with attributes, write NumPy array data into these variables, and then read the data back. It uses `Dataset` for file operations and `numpy` for data generation.

import os
from netCDF4 import Dataset
import numpy as np

# Define a dummy NetCDF file path
filename = 'example.nc'

# Create a new NetCDF file in write mode ('w')
with Dataset(filename, 'w', format='NETCDF4') as nc_file:
    # Create dimensions
    nc_file.createDimension('x', 10)
    nc_file.createDimension('y', 5)
    nc_file.createDimension('time', None) # 'None' for unlimited dimension

    # Create variables
    x_var = nc_file.createVariable('x', 'i4', ('x',))
    y_var = nc_file.createVariable('y', 'i4', ('y',))
    time_var = nc_file.createVariable('time', 'f8', ('time',))
    data_var = nc_file.createVariable('temperature', 'f4', ('time', 'y', 'x'))

    # Add attributes to variables
    data_var.units = 'Celsius'
    data_var.long_name = 'Air Temperature'

    # Write data to variables
    x_var[:] = np.arange(10)
    y_var[:] = np.arange(5)
    
    # Write data for the first time step
    time_var[0] = 0.0
    data_var[0, :, :] = np.random.rand(5, 10) * 30 + 273.15 # Kelvin example
    
    # Write data for a second time step (demonstrates unlimited dimension)
    time_var[1] = 1.0
    data_var[1, :, :] = np.random.rand(5, 10) * 30 + 273.15

    print(f"Successfully created and wrote to {filename}")

# Read data from the NetCDF file in read mode ('r')
with Dataset(filename, 'r') as nc_file:
    print(f"\nOpened {filename} for reading:")
    print(f"File format: {nc_file.data_model}")
    print(f"Dimensions: {list(nc_file.dimensions.keys())}")
    print(f"Variables: {list(nc_file.variables.keys())}")

    temp_data = nc_file.variables['temperature'][:, :, :]
    print(f"Shape of 'temperature' data: {temp_data.shape}")
    print(f"Units of 'temperature': {nc_file.variables['temperature'].units}")
    print(f"Sample temperature data:\n{temp_data[0, 0, 0]:.2f} {nc_file.variables['temperature'].units}")

# Clean up the created file
os.remove(filename)
print(f"\nCleaned up {filename}")

view raw JSON →