Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRS: CF grid_mapping attribute to encoding #1084

Closed
snowman2 opened this issue May 12, 2021 · 16 comments
Closed

CRS: CF grid_mapping attribute to encoding #1084

snowman2 opened this issue May 12, 2021 · 16 comments
Labels

Comments

@snowman2
Copy link
Contributor

snowman2 commented May 12, 2021

Related to:

Apparently it is not CF-compliant to store the CRS grid_mapping as a coordinate. However, it is desirable to keep it in the xarray coords as it persists better. The workaround is to store the grid_mapping in the encoding instead of attrs and to_netcdf will take care of the rest when writing to a netCDF file.

@snowman2
Copy link
Contributor Author

Note: This requires Python 3.7+ and xarray 0.17+

@Kirill888
Copy link
Member

Kirill888 commented May 13, 2021

@snowman2 these are all pretty long threads. Can I ask you to summarize what would you like to propose?

I'm open to a suggestion to

  • supporting xarray style of geospatial info in .geobox code
  • providing method for converting datacube xarrays to a form compatible with .to_netcdf

I'm not too keen to drop coords-based .geobox though, as this is the most robust way we found so far for preserving geo-registration information through data transformations.

@snowman2
Copy link
Contributor Author

The only change is to store grid_mapping in encoding instead of attrs.

rds.encoding["grid_mapping"] = "spatial_ref"

@snowman2
Copy link
Contributor Author

This PR is how rioxarray handled it: corteva/rioxarray#284

@snowman2
Copy link
Contributor Author

I'm not too keen to drop coords-based .geobox though, as this is the most robust way we found so far for preserving geo-registration information through data transformations.

I agree with you 💯

@Kirill888
Copy link
Member

Is this about data var attributes only or x,y coords too?

@Kirill888
Copy link
Member

def mk_data_var(m, data_func):
data = data_func(m)
attrs = dict(**m.dataarray_attrs(),
**crs_attrs)
return xarray.DataArray(data,
name=m.name,
coords=coords,
dims=dims,
attrs=attrs)

xx.attrs.update(grid_mapping=crs_coord_name)
if isinstance(xx, xr.Dataset):
for band in xx.data_vars.values():
band.attrs.update(grid_mapping=crs_coord_name)

@snowman2
Copy link
Contributor Author

Should just be for the data vars.

@snowman2
Copy link
Contributor Author

Remembered this today with something else I was working on. Is this a change you would like to see?

@Kirill888
Copy link
Member

@snowman2 I forgot about this one.

Ideally I would like to see this code taken out into a separate library under opendatacube org, as you were suggesting previously, it would make it easier to iterate on.

Looks like this work might be done under https://github.com/opendatacube/odc-tools/projects/2, as there is interest in decoupling data loading code from collection management. Moving geometry handling code out into a standalone library would be a first step toward that goal.

I'm yet to write an "Enhancement Proposal" for that task though. I'm thinking of doing some serious git history surgery on datacube repo to get geometry code and tests into a separate lib while keeping original authorship and history. Followed up by changing datacube to depend on that lib instead of internal implementation.

I believe this a more promising approach, that still delivers tangible benefits, than currently stalled datacube 2.0 effort.

cc: @gadomski

@snowman2
Copy link
Contributor Author

Sounds great 👍.

@gadomski
Copy link

I'm thinking of doing some serious git history surgery on datacube repo to get geometry code and tests into a separate lib while keeping original authorship and history.

For background @Kirill888 we used git-filter-repo when we broke out the stactools subpackages (stac-utils/stactools#111), and it worked well for us. Relevant line here: https://gist.github.com/gadomski/a0ab8d18f56ff34454d2ed890d234534#file-extract_stactools_subpackage-sh-L21.

@stale
Copy link

stale bot commented Dec 17, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Dec 17, 2021
@snowman2
Copy link
Contributor Author

Ping to leave open.

@stale stale bot removed the wontfix label Dec 17, 2021
@Kirill888
Copy link
Member

@snowman2 I pinned this so it won't expire. I'm starting the work of extracting odc.geo library from datacube-core. The plan is to

  1. Move geometry and related tests to a separate package and repository under opendatacube org (with history)
  2. Setup all the necessary infra: testing, packaging, docs in the new repository
  3. Do cleanup: formatting, docs, types, test coverage
  4. Replace datacube dependency in odc-stac with the odc-geo, this is likely to highlight various missing bits, so will require some iteration. It's likely that some parts from odc.algo will migrate to odc.geo as part of this work too (reprojection logic in particular).
  5. Finally refactor datacube to depend on odc.geo rather than having duplicate code

So it will take a while before datacube itself is updated, but work is starting now.

@snowman2
Copy link
Contributor Author

snowman2 commented May 6, 2023

I believe that this is addressed by #1424

@snowman2 snowman2 closed this as completed May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants