{"id":7280,"library":"griddataformats","title":"Grid Data Formats","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/griddataformats/python-griddataformats","tags":["data-formats","scientific-computing","geospatial","grids","netcdf","grib2","meteorology"],"install":[{"cmd":"pip install griddataformats","lang":"bash","label":"Base installation"},{"cmd":"pip install griddataformats[netcdf]","lang":"bash","label":"With NetCDF support"},{"cmd":"pip install griddataformats[grib2]","lang":"bash","label":"With GRIB2 support"},{"cmd":"pip install griddataformats[netcdf,grib2]","lang":"bash","label":"With both NetCDF and GRIB2 support"}],"dependencies":[{"reason":"Core dependency for array manipulation and data storage within Grid objects.","package":"numpy","optional":false},{"reason":"Required for reading and writing NetCDF files using NetCDFFile.","package":"netcdf4","optional":true},{"reason":"Required for reading and writing GRIB2 files using GRIB2File.","package":"eccodes","optional":true}],"imports":[{"symbol":"Grid","correct":"from griddataformats.grid import Grid"},{"symbol":"NetCDFFile","correct":"from griddataformats.file import NetCDFFile"},{"symbol":"GRIB2File","correct":"from griddataformats.file import GRIB2File"},{"symbol":"Dimension","correct":"from griddataformats.common import Dimension"},{"symbol":"Field","correct":"from griddataformats.common import Field"},{"symbol":"Unit","correct":"from griddataformats.common import Unit"}],"quickstart":{"code":"import tempfile\nimport os\nimport numpy as np\n\nfrom griddataformats.grid import Grid\nfrom griddataformats.common import Dimension, Field, Unit, Origin, GridType\nfrom griddataformats.file import NetCDFFile # Requires 'netcdf4' optional dependency\n\n# Create a simple 2D grid with dummy data\nlatitude = np.linspace(40, 50, 10)\nlongitude = np.linspace(-100, -90, 15)\ntemperature_data = np.random.rand(len(latitude), len(longitude)) * 30 + 273.15 # Kelvin\n\nlat_dim = Dimension(\"latitude\", latitude.shape[0], Unit.degree_north, data=latitude)\nlon_dim = Dimension(\"longitude\", longitude.shape[0], Unit.degree_east, data=longitude)\n\ntemp_field = Field(\n    name=\"temperature\",\n    dimensions=[lat_dim, lon_dim],\n    units=Unit.kelvin,\n    data=temperature_data,\n    description=\"Surface Temperature\"\n)\n\ngrid = Grid(fields=[temp_field], grid_type=GridType.regular_latitude_longitude)\n\n# Save to a temporary NetCDF file\nwith tempfile.TemporaryDirectory() as tmpdir:\n    filepath = os.path.join(tmpdir, \"my_grid.nc\")\n    NetCDFFile.from_grid(grid, filepath)\n    print(f\"Grid saved to {filepath}\")\n\n    # Now read it back\n    read_grid = NetCDFFile(filepath).to_grid()\n    print(f\"Read back grid with {len(read_grid.fields)} field(s).\")\n    read_field = read_grid.fields[0]\n    print(f\"Field name: {read_field.name}, Units: {read_field.units}\")\n    print(f\"Data shape: {read_field.data.shape}\")\n    print(f\"First data point: {read_field.data[0,0]:.2f} K\")","lang":"python","description":"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]`)."},"warnings":[{"fix":"Install `griddataformats` with optional dependencies: `pip install griddataformats[grib2]` or `pip install griddataformats[netcdf]` (or both for full functionality).","message":"Using `GRIB2File` or `NetCDFFile` requires additional optional dependencies (`eccodes` or `netcdf4` respectively). A `ModuleNotFoundError` will occur if these are not installed.","severity":"gotcha","affected_versions":"All versions >=1.0.0"},{"fix":"Ensure `Field.data` is a `numpy.ndarray`. Convert lists using `numpy.array(your_list)` before passing them to `Field`.","message":"The `Grid` and `Field` objects expect `numpy.ndarray` for field data. Providing raw Python lists directly to `Field.data` might lead to unexpected behavior or require explicit conversion, impacting performance or causing errors in data manipulation.","severity":"gotcha","affected_versions":"All versions >=1.0.0"},{"fix":"Carefully align the `dimensions` list and their `size` attributes with the actual `data.shape` of the `numpy.ndarray` provided to `Field`.","message":"The dimensions provided for a `Field` must exactly match the shape of the `data` array. A mismatch in `Dimension.size` attributes versus `data.shape` will raise a `ValueError`.","severity":"gotcha","affected_versions":"All versions >=1.0.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `griddataformats` with the NetCDF optional dependency: `pip install griddataformats[netcdf]`","cause":"Attempted to use `griddataformats.file.NetCDFFile` without installing the `netcdf4` package, which is an optional dependency.","error":"ModuleNotFoundError: No module named 'netCDF4'"},{"fix":"Install `griddataformats` with the GRIB2 optional dependency: `pip install griddataformats[grib2]`","cause":"Attempted to use `griddataformats.file.GRIB2File` without installing the `eccodes` package, which is an optional dependency.","error":"ModuleNotFoundError: No module named 'eccodes'"},{"fix":"Ensure `data.shape` aligns precisely with `(dim1.size, dim2.size, ...)`. For example, if `data.shape` is `(10, 20)`, you need two `Dimension` objects with `size=10` and `size=20` respectively.","cause":"The shape of the `numpy.ndarray` provided to `Field.data` does not match the total number of dimensions or the sizes specified in the `dimensions` list.","error":"ValueError: Field data shape mismatch with dimensions"}]}