odc-geo icon indicating copy to clipboard operation
odc-geo copied to clipboard

Consider adding functionality like `odc.geo.xr.replace_coords()`

Open alexgleith opened this issue 11 months ago • 10 comments

Current options to write data out with a different definition of geobox coordinates include either:

a) using the private method

_write_cog(
      data,
      new_geobox,
      output_file,
)

b) assigning coordinates, which has a fixed coordinate name

# Set up a new Affine and GeoBox
new_affine = Affine(0.01, 0.0, -180.0, 0.0, 0.01, -89.995, 0.0, 0.0, 1.0)
new_geobox = GeoBox(data.odc.geobox.shape, new_affine, data.odc.geobox.crs)

new_coords = xr_coords(new_geobox)

# Rename lat to latitude and lon to longitude and update the coordinates of the xarray
new_data = data.rename({"lat": "latitude", "lon": "longitude"}).assign_coords(
    new_coords
)

So having a functionality that can safely do with without compromise or complexity would be nice.

alexgleith avatar Mar 04 '24 00:03 alexgleith

We should try to keep same style as xarray api, they tend to use set_ prefix, used to be assign_, but that seems to be deprecated. So something like set_geo_coords :: GeoBox -> xr.Dataset | xr.DataArray making sure to return a new xarray object wrapping same pixel data.

Kirill888 avatar Mar 04 '24 02:03 Kirill888

Possibly need to pass in dimension names too. Or to infer them, perhaps it's an implementation detail.

alexgleith avatar Mar 04 '24 02:03 alexgleith

currently, these are fixed to y,x or latitude,longitude based on CRS, but xr_coords should make it easy to

  1. force to always be y,x
  2. use custom names

and the proposed method should just delegate that part to xr_coords (pass-through kwargs). It should also ensure that whatever other, non-spatial dimensions and coords are present on input remain on output also.

Essentially what we want is xx | drop-geo-coords | set-geo-coords [opts]

Kirill888 avatar Mar 04 '24 06:03 Kirill888

Is this issue mostly resolved by the addition of .odc.reload() @Kirill888/@alexgleith? https://odc-geo.readthedocs.io/en/latest/_api/odc.geo.xr.ODCExtensionDa.reload.html

If so, we might want to add a little extra to those docstrings to explain how it could be used in a scenario like this.

robbibt avatar May 15 '24 00:05 robbibt

I don't think so, @robbibt. Sometimes automatic coordinate loading leads to bad numbers due to floating point precision. (i.e., 1.9999999 rather than 2.0).

So explicitly setting the geobox, although it's dangerous, is useful sometimes.

alexgleith avatar May 28 '24 02:05 alexgleith

So does something like:

xx = xx.assign_coords(odc.geo.xr.xr_coords(actual_geobox_you_want))

do what you need in this case @alexgleith

Kirill888 avatar May 30 '24 00:05 Kirill888

I guess I don't understand why you need to rename and keep old coords on the same dataset. Or is the problem in figuring out what actual_geobox_you_want from geobox_in_the_data_currently.

The problem of "I put one geobox in, but what I'm getting back is slightly different" should have been fixed by recent changes that take more care to re-use original affine matrix where possible instead of recomputing from coordinate data.

Kirill888 avatar May 30 '24 00:05 Kirill888

I guess I don't understand why you need to rename

It was throwing an error before. Looks like it's not required now, which is nice.

alexgleith avatar May 30 '24 00:05 alexgleith

There seems to be a geobox per variable now, @Kirill888

data.odc.geobox.affine

[0.01, 0.0, -180.0, 0.0, -0.01, 89.995, 0.0, 0.0, 1.0]

list(data.analysed_sst.odc.geobox.affine)

[0.010009765625, 0.0, -179.99501037597656, 0.0, -0.0099945068359375, 89.9949951171875, 0.0, 0.0, 1.0]

alexgleith avatar May 30 '24 05:05 alexgleith

Reported over here https://github.com/opendatacube/odc-geo/issues/157

alexgleith avatar May 30 '24 05:05 alexgleith