Repository navigation
Description
Hey @djhoese !
I write down here some thoughts I had for the design of an interface to facilitate the reading and writing of CF-compliant netCDF with CRS information of structured grids. Do you think other people would be interested to finalize this accessor?
Such accessor could be placed in a lightweight separate package and:
- Within pytroll, it could replace lot of code in
satpyCFWriter and/or be used bypyresampleto retrieve theAreaDefinitionfrom dataset. - In rioxarray, it could simplify and improve the
rioxarray.rioxarray.XRasterBase class - Facilitate user life when dealing with the creation of CF-compliant netCDF regarding the CRS and area information
- Improve compatibility across the CRS python ecosystem
Regarding package dependencies, pyproj, rasterio, pyresample and cartopy would be only optional.
Currently, I just have coded stuff for the CF-Writer utility (available here).
Currently, these codes enable to write CF-compliant netCDF (with respect to CRS) which are displayed correctly also on QGIS.
The remaining code could be "borrowed" from rioxarray, pyresample and xgeo
Here below the interface design I thought:
CF-Writer utility
ds.crs.remove() # remove crs coordinates, grid_mapping and coordinates variable attributes
ds.crs.add(crs, grid_mapping_name="spatial_ref", inplace=False) # Add CF-compliant info
CF-Reader utility
da.crs.is_swath() # Or is_non_uniform_grid/area
da.crs.is_area() # Or is_uniform_grid/area
da.crs.to_pyproj_crs(area_crs=True) # If 2 crs(area and WGS84 for lon/lat) return only area
da.crs.to_rasterio_crs(area_crs=True) # If 2 crs(area and WGS84 for lon/lat) return only area
da.crs.to_pyresample_area() # If exists
da.crs.to_pyresample_swath()
da.crs.to_cartopy_projection()
CRS information
da.crs.is_geographic()
da.crs.is_projected()
da.crs.is_geostationary() # Not in rasterio and pyproj crs
da.crs.is_polar() # Not in rasterio and pyproj crs
Area Information
Area Bounds (lower left, upper right)
- pyproj area_of_use.bounds: [west, south, east, north]
- rasterio.transform.from_bounds: [west, south, east, north]
- rasterio.BoundingBox: [left, bottom, right, top]
- rioxarray: [left, bottom, right, top]
- pyresample area_extent: [x_lower_left, y_lower_left, x_upper_right, y_upper_right]
- cartopy projection bounds: [west, east, south, north] (code here) follow another convention
- matplotlib imshow extent: [left, right, bottom, top] follow another convention
--> Available only for area, not swath !
da.crs.area_bounds # (or area_extent)
Area shape, resolution, and transform
da.crs.shape # (width, height)(x, y) or (heigth, width) (y, x)
da.crs.resolution # (x, y) (only for uniform area)
da.crs.transform # rioxarray, rasterio, gdal (only for uniform area)
Bouding Box [x_min, x_max, y_min, y_max]
- cartopy set_extent: [x_min, x_max, y_min, y_max)
da.crs.bbox # For uniform_area same as area_bounds (but rearranged). For non-uniform area it computes min/max of the boundary
da.crs.bbox_lonlat # For non-uniform area (i.e. swath) same as bbox. For uniform area need to project boundary coords. If polar wrapping area or crossing antimeridian, enforce -180, 180 on x ?
da.crs.to_shapely_bounds # [x_min, y_min, x_max, y_min]
Activity
ghiggi commentedon Mar 8, 2023
Additional notes:
grid_mappingdefined as:"CRS: x y"grid_mappingwith 2 CRS:"CRS: x y CRSWGS84: lat lon"spatial_ref(crs_wkt) attribute in the CRS coordinatecrs_wktto start withGEOGCSbutpyprojprovides thecrs_wktstarting withGEOGCRS!!!djhoese commentedon Mar 30, 2023
Sorry it took so long for me to get to this @ghiggi. I know we talked a little bit on slack about this, but when I started reading the issue and realized how long it was I wasn't going to have time to give it a proper review. I'm hoping I can start that discussion now...
First, thanks for putting all these ideas together. It is clear you've run into problems with existing tools and tried to come up with interfaces that support as much as possible. These are, in general, not easy things to figure out and nail down.
My main issue with what you've suggested here is that it conflates/confuses/combines two or more separate topics. The CRS is specifically the coordinate system which has a datum and possibly some bounds associated with it, but it does not have an idea of pixel resolution or shape/size or even coordinates (at least not ones specific to pixels or an array of data). This is made even more confusing by pyresample's hack of generating cartopy CRS objects where the bounds of the CRS are set based on the bounds of the data (a bad decision in hindsight by me). You might be trying to handle that with your
area_crskeyword argument in some of the conversion methods, but I'm not sure I understand what that kwarg does.So I think I'd be hesitant to want to put this type of functionality in an accessor called
.crs. You also refer to all of this stuff as being related to CF, but it isn't clear to me why CF matters for most of this functionality. For the stuff that is CF specific or has to be done differently to make CF happy, would this functionality be more appropriate forcf_xarray(https://cf-xarray.readthedocs.io/en/latest/)?I think the parts of this functionality that are xarray-specific could go in geoxarray in the sense that they need to know about coordinates and the CRS and other potentially other DataArrays in a Dataset. Otherwise I would hope that the functionality would go in GDAL, or rasterio, or pyproj, and then come "out of the box" by accessing a pyproj CRS object. For the parts of CF that aren't supported by GDAL or other libraries, could lowering the CF standard being supported allow for more flexibility currently (CF 1.6 or 1.7 instead of 1.8)?
Side note: I think cartopy can now accept pyproj CRS objects since it now subclasses from them. Or at least they can be easily made. Similarly, I would hope that libraries like pyresample or rioxarray would not require a user to explicitly convert to a certain CRS/area-like object before being called, but would rather depend on accessors like those in geoxarray/cf_xarray/rioxarray to get the information they need.
CC @snowman2 who is the maintainer or is a contributor to at least 25% of the packages mentioned in this issue.
rabernat commentedon Jul 17, 2023
I would definitely pay attention to what odc-geo is doing: https://github.com/opendatacube/odc-geo
mdsumner commentedon Dec 7, 2023
ah yes very nice, I literally just came here to search for "odc" and say something similar
there's a whole family of grid logic and crs tricks that exist as functions of very simple stuff: extent, dimension, crs string - and in R you see that the big packages all do this stuff and of course it is peppered everywhere through GDAL - things like a cell index abstraction, a singleton array index that treats a matrix as if it were flat, a complement to a row,col pair and function of the dimension (and extent) - so you want functions like rowcolFromXY, cellFromXY, XYFromCell etc etc. and these exist in the raster package in R, they are implemented again in terra and they exist in part in stars (but I got tired of requiring an object for functions that are literally parameterized by at most six numbers and a string, dimension, extent, crs). I know this is OT, but I'm here purposefully to prod, I want to find people across R and Python and others who want these basic foundations to exist in a core software lib. :) I don't really care if there's an R package and a Python package for it rather than a C++ lib - but a lot of stuff is coming out of odc as they modularize and generalize (and I wrote a lot of the R side already).
I'll look at that GDAL stuff mentioned above, FWIW there's a whole lot of stuff we wrote in R that worked in a local bespoke package ... but we figured out if we contribute upstream to GDAL we can throw all the code away, and it works in any GDAL package in R and any GDAL package in Python. There's also a family of to-be-fixed issues with GDAL raster reprojection (just to do normal simple-case longitude wrapping) that would remove the need for downstream churn and I'd like to find people to help me work on that.
mdsumner commentedon Dec 7, 2023
@ghiggi I just want to add here, should GDAL support these? Please add issues in the repo, or help me follow up. I'll chase down examples if you have them.