Grid Data Formats

1.1.0 · active · verified Thu Apr 16

griddataformats is a Python library for reading and writing data on regular grids. It provides a standardized way to handle gridded data, often used in scientific computing, meteorology, and other fields. The current version is 1.1.0, and it offers robust support for common formats like NetCDF and GRIB2 through optional dependencies.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `Grid` object with a `Field` representing temperature data, save it to a NetCDF file, and then read it back. Note that using `NetCDFFile` requires the optional `netcdf4` dependency (install with `pip install griddataformats[netcdf]`).

import tempfile
import os
import numpy as np

from griddataformats.grid import Grid
from griddataformats.common import Dimension, Field, Unit, Origin, GridType
from griddataformats.file import NetCDFFile # Requires 'netcdf4' optional dependency

# Create a simple 2D grid with dummy data
latitude = np.linspace(40, 50, 10)
longitude = np.linspace(-100, -90, 15)
temperature_data = np.random.rand(len(latitude), len(longitude)) * 30 + 273.15 # Kelvin

lat_dim = Dimension("latitude", latitude.shape[0], Unit.degree_north, data=latitude)
lon_dim = Dimension("longitude", longitude.shape[0], Unit.degree_east, data=longitude)

temp_field = Field(
    name="temperature",
    dimensions=[lat_dim, lon_dim],
    units=Unit.kelvin,
    data=temperature_data,
    description="Surface Temperature"
)

grid = Grid(fields=[temp_field], grid_type=GridType.regular_latitude_longitude)

# Save to a temporary NetCDF file
with tempfile.TemporaryDirectory() as tmpdir:
    filepath = os.path.join(tmpdir, "my_grid.nc")
    NetCDFFile.from_grid(grid, filepath)
    print(f"Grid saved to {filepath}")

    # Now read it back
    read_grid = NetCDFFile(filepath).to_grid()
    print(f"Read back grid with {len(read_grid.fields)} field(s).")
    read_field = read_grid.fields[0]
    print(f"Field name: {read_field.name}, Units: {read_field.units}")
    print(f"Data shape: {read_field.data.shape}")
    print(f"First data point: {read_field.data[0,0]:.2f} K")

view raw JSON →