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 tools #232

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cordex/ccrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from cartopy import crs as ccrs

crs_map = {
"rotated_latitude_longitude": {
"crs": ccrs.RotatedPole,
"kwargs": {
"pole_longitude": "grid_north_pole_longitude",
"pole_latitude": "grid_north_pole_latitude",
"central_rotated_longitude": 0.0,
},
}
}


def _ccrs_from_cf(mapping):
crs = crs_map.get(mapping.grid_mapping_name)
kwargs = {kw: (mapping.attrs.get(v) or v) for kw, v in crs["kwargs"].items()}
return crs["crs"](**kwargs)


def get_ccrs(ds):
mapping = ds.cf["grid_mapping"]
return _ccrs_from_cf(mapping)
13 changes: 13 additions & 0 deletions cordex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,16 @@ def cell_area(ds, R=6371000, attrs=True):
da.attrs = {}

return da


def create_polygon(obj):
"""create polygon in rotated pole coords"""
from shapely.geometry import Polygon

coords = [
[obj.cf["X"].min(), obj.cf["Y"].min()],
[obj.cf["X"].max(), obj.cf["Y"].min()],
[obj.cf["X"].max(), obj.cf["Y"].max()],
[obj.cf["X"].min(), obj.cf["Y"].max()],
]
return Polygon(coords)
Loading