cf-python icon indicating copy to clipboard operation
cf-python copied to clipboard

Projection lat-lon coordinates including new `GridMapping` objects

Open sadielbartholomew opened this issue 3 years ago • 6 comments

Part 1 of 2 towards #628. This first part sets up the Grid Mapping class and helper function infrastructure towards interfacing with PROJ using pyproj, so that in the second part (nearly ready, but with some issues left to resolve) we can use those to invoke PROJ cs2cs functionality to transform from one coordinate reference system to another, else be informed that the transformation isn't valid or possible, which allows us to get the lat-lon coordinates for a given projection with corresponding CRS parameters where that makes physical sense.

CF-supported Grid Mappings have been modelled explicitly as objects in this first step in order to, for example:

  • allow us to define all supported Grid Mappings to be a paradigm for those and the CF map parameter arguments listed there;
  • validate the respective map parameters;
  • allow us to interface with them using our own cf.Units instead of having to convert to and work with PROJ formats for inputs (which are strings with a unit as a suffix e.g. "30D", "0.5R", etc.), where functions to convert to and from PROJ data and units are provided;
  • to facilitate future cf-plot work and integration into the cf-python codebase.

Diagrams of new class hierarchy

Generated from this PR branch using pyreverse -o png -p GRIDMAPPINGS-1 gridmappings.py when in the cf directory (large image so e.g. open out by 'Open image in new tab' or similar, click to zoom):

classes_GRIDMAPPINGS-1

Sadly the diagram generator doesn't seem to show abstract methods, so we don't see those. And it only show classes, so it doesn't indicate functions in the module either. But should still be useful.

I haven't done a thorough self-review yet, but we can cross-reference this with Appendix F of the Conventions doc to check everything matches up.

sadielbartholomew avatar Sep 20 '23 14:09 sadielbartholomew

Something has gone weird with all the commit timestamps, they are all pointing to today after my rebase. I might need to force push a fix. One moment...

sadielbartholomew avatar Sep 20 '23 14:09 sadielbartholomew

And the order is messed up too :roll_eyes:

sadielbartholomew avatar Sep 20 '23 14:09 sadielbartholomew

I'm going to wait an hour or so for my break whilst I see if it's a sync'ing thing and GitHub fixes it by then, if notk I might open a new PR since I'm worried something has gone badly wrong with the rebase...

sadielbartholomew avatar Sep 20 '23 14:09 sadielbartholomew

OK timestamps are now all sensible and accurate (at least, under the 'Commits' pane now, not here on the discussion thread at least as far as I see now). Sorry about that - gits fault, not mine! The PR itself is all as intended, though I want to add a few updates here and there (some from part 2 that are better placed in part 1, and some docstring TODO filling out). Nothing that will change it in any way that makes it inaccurate to look at as-is, though!

I have went down late-night advanced git learning rabbit hole. I'm not sure why I haven't noticed and been annoyed by this before (probably I have, but have since forgotten), but when you do things such as a rebase, in my case to tidy some messy commits, some timestamps will be altered to the date of the rebase. Which can mess up the ordering and the dates shown for commits. Hence why git log was showing different stamps to what the listing here showed, which made me think something was dodgy with the code I'd pushed relative to the local copy before I'd tidied by rebase.

Turns out, if you use git log --pretty=fuller you can see the AuthorDate and CommitDate separately and when e.g. a rebase puts these out of sync you can use git filter-branch -f --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"' <hash of commit previous to one of concern e.g. first in PR>..HEAD to make those identical, and that fixed up the stamps here. I mention this all here partly as a reminder to myself, since I imagine I'll want to use this in future!

Anyway, all good now RE the commits here.

sadielbartholomew avatar Sep 21 '23 01:09 sadielbartholomew

@davidhassell thanks for the very useful review discussion on Friday. See below for a brief summary of my understanding of the main points of the conversation (the side points are scribbled in my notes). Please let me know if that all sounds (about) right to you. I'll be updating the PR in line with the agreed changes (starting now, since I can always tweak further later if you do query something below).

Summary

Note I'll tick these off with specific (sets of) commits, as I address them.

  • [x] Design Pattern Change: The current approach for creation of the Grid Mapping objects, for purposes mostly of validation of the map parameter inputs, is using an Abstract Factory design pattern, with use of the ABC metaclass and abstractmethod decorator, etc., for the base GridMapping class, but we want to convert to something more akin to using __new__ as a factory method to generate the specific concrete classes, so using the Factory design pattern instead (as indicated by our whiteboard notes). Benefits being:
    • this avoids the clunky _get_cf_grid_mapping_from_name to match CF Grid Mapping class to a 'grid_mapping_name', doing this upon instance creation;
    • by avoiding ABC we can avoid use of a metaclass which would forbid use of our docstring re-writer, which would be really useful here given the amount of map parameter documenting required (the bulk of the lines changes here at present, probably) - it was on the TODO list before you pointed out it wouldn't be possible with the present approach.
    • nicer interface to the validation process, custom to each concrete GM, with a validate method.
  • [x] Movement of code: the coordinatereference module does not have access to the coordinates themselves, so don't touch that and instead move the main function for this feature, create_2d_lats_and_lons, to the fielddomain object.
  • [x] House style: since we tend to have only one class at most per module, for consistency move the separate concrete GridMapping classes out into their own appropriately-named modules under a gridmappings directory.
  • [x] Special treatment of the LatLon class: since this corresponds to our destination CRS usually (though eventually we can/will make this configurable), it can take no __init__ and we can define a helper method is_latlon_gm() (or similar in name) to aid processing of any existing lat-lon coordinates, avoiding re-generation scenarios.

Notes

Including these for longevity and so I don't have to keep turning around to view them on my whiteboard :slightly_smiling_face:

20230922_175722

sadielbartholomew avatar Sep 25 '23 15:09 sadielbartholomew

Hi Sadie - thanks for the summary - that's how I remember it, too!

davidhassell avatar Sep 26 '23 10:09 davidhassell