Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active September 26, 2025 11:24
Show Gist options
  • Save mdsumner/911c181467abb2c91d08544a94d8510a to your computer and use it in GitHub Desktop.
Save mdsumner/911c181467abb2c91d08544a94d8510a to your computer and use it in GitHub Desktop.

I was delighted to learn that flexible coordinates in xarray are fully operational.

Following on from this (unfinished) blog post on unnecessary netcdf longlat coordinates, I now have a neat xarray/netcdf native fix for the problem I was discussing, it is a direct match to the "parse_coordinates": False example in the xarray indexes gallery.

This only a few weeks ago was challenging for me to present to non-R audiences, so I'm very excited that we can now fix these broken grids in a way more accessible to Python communities. Degenerate coordinates represent a huge entropy problem in array metadata, and (I think) we need a coordinated effort to be able to easily assign a fix

(like in GDAL with vrt://{dsn}?a_gt=c,a,b,f,d,e) and have that knowledge also feed back up to the providers to stem the flow of problematic coordinates.

from affine import Affine
from rasterix import RasterIndex
import xarray

## this dataset has poorly formed coordinates, unnecessary and messily generate lonlat arrays
ds = xarray.open_dataset("https://zenodo.org/records/7327711/files/CS2WFA_25km_201007.nc?download=1", engine = "h5netcdf")

## so let's fix them
transform = Affine.from_gdal(-3950000, 25000, 0, 4350000, 0, -25000)
shape = ds.sea_ice_concentration.shape
ds = ds.assign_coords(xarray.Coordinates.from_xindex(RasterIndex.from_transform(transform, width=shape[2], height=shape[1])))
ds["lon"] = None
ds["lat"] = None

ds
<xarray.Dataset> Size: 3MB
Dimensions:                (time: 1, y: 332, x: 316)
Coordinates:
  * time                   (time) object 8B 2010-07-01 00:00:00
  * x                      (x) float64 3kB -3.938e+06 -3.912e+06 ... 3.938e+06
  * y                      (y) float64 3kB 4.338e+06 4.312e+06 ... -3.938e+06
Data variables:
    lat                    object 8B None
    lon                    object 8B None
    snow_freeboard         (time, y, x) float64 839kB ...
    ice_freeboard          (time, y, x) float64 839kB ...
    snow_depth             (time, y, x) float64 839kB ...
    sea_ice_concentration  (time, y, x) float64 839kB ...
Indexes:
  ┌ x        RasterIndex (crs=None)
  └ y
Attributes:
    title:        Antarctic sea ice physical properties obtained from CryoSat...
    institution:  NASA GSFC Cryospheric Sciences Laboratory and University of...
    history:      File created on November 15, 2022, 12:09:06

I'm not yet able to set the crs above yet, but I'm sure that's as easy to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment