Write CRS informationΒΆ

Geoxarray can rewrite CRS information into a CF compatible manner. It can use either the CRS information discovered from .geo.crs (see the Access CRS information how to for more information on supported formats) or provided by a user.

If we start with an xarray DataArray with no CRS information in it (at least that geoxarray is unfamiliar with) that looks like this:

print(data_arr)
<xarray.DataArray (y: 20, x: 10)> Size: 2kB
...
Dimensions without coordinates: y, x

We can explicitly add the CRS information use the write_crs() method.

new_data_arr = data_arr.geo.write_crs("EPSG:4326", inplace=False)
print(new_data_arr)
<xarray.DataArray (y: 20, x: 10)> Size: 2kB
...
Coordinates:
    spatial_ref  int64 8B 0
Dimensions without coordinates: y, x

The new DataArray now has a spatial_ref coordinate variable that contains NetCDF/CF compatible grid mapping information. There is also a grid_mapping attribute (stored in .encoding) that will be written to a NetCDF file if we were to wrap the DataArray in a xarray.Dataset object and called xarray.Dataset.to_netcdf().

print(new_data_arr.encoding["grid_mapping"])
print(new_data_arr.coords["spatial_ref"])
spatial_ref
<xarray.DataArray 'spatial_ref' ()> Size: 8B
...
Coordinates:
    spatial_ref  int64 8B 0
Attributes:
    crs_wkt:                      GEOGCRS["WGS 84",ENSEMBLE["World Geodetic S...
    semi_major_axis:              6378137.0
    semi_minor_axis:              6356752.314245179
    inverse_flattening:           298.257223563
    reference_ellipsoid_name:     WGS 84
    longitude_of_prime_meridian:  0.0
    prime_meridian_name:          Greenwich
    geographic_crs_name:          WGS 84
    horizontal_datum_name:        World Geodetic System 1984 ensemble
    grid_mapping_name:            latitude_longitude
    spatial_ref:                  GEOGCRS["WGS 84",ENSEMBLE["World Geodetic S...

The spatial_ref attributes (.attrs) also include a crs_wkt attribute with a Well-Known Text (WKT) version of the CRS for better CF-compatibility. The spatial_ref attribute is the same WKT and exists for GDAL compatibility.

This same behavior and workflow can be achieved if starting from a xarray.Dataset class.