-
Notifications
You must be signed in to change notification settings - Fork 15
Collocation of emep-mscw output with gridded data in a projection #1274
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1928613
add projection support
heikoklein 54336b1
adding and fixing first tests
heikoklein 5ffd724
new test dataset and documentation
heikoklein be52243
using new test dataset
heikoklein 4fcf1ed
isort
heikoklein 87d96e1
further tests
heikoklein 0f6f744
adding pyproj explicitly
heikoklein c458fce
snake-case
heikoklein 49be898
fix path in README
heikoklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import xarray | ||
from pyproj import CRS, transformer | ||
|
||
|
||
class ProjectionInformationException(Exception): | ||
pass | ||
|
||
|
||
class ProjectionInformation: | ||
def __init__(self): | ||
self._crs = None | ||
self._x_axis = None | ||
self._y_axis = None | ||
self._units = None | ||
|
||
@property | ||
def x_axis(self): | ||
return self._x_axis | ||
|
||
@property | ||
def y_axis(self): | ||
return self._y_axis | ||
|
||
def to_proj(self, latitude, longitude): | ||
"""convert latitude and longitude to x and y | ||
|
||
:param latitude: latitude values | ||
:param longitude: longitude values | ||
:return: tuple of x and y coordinates | ||
""" | ||
trans = transformer.Transformer.from_crs(4326, self._crs) | ||
return trans.transform(latitude, longitude) | ||
|
||
def to_latlon(self, x, y): | ||
"""convert x and y to latitude and longitude | ||
|
||
:param x: x values | ||
:param y: y values | ||
:return: tuple of latitude and longitude coordinates | ||
""" | ||
trans = transformer.Transformer.from_crs(self._crs, 4326) | ||
return trans.transform(x, y) | ||
|
||
@staticmethod | ||
def from_xarray(ds: xarray.Dataset, var: str): | ||
"""initialize the ProjectionInformation from an xarray variable | ||
|
||
returns None if no projection exists for the variable or ProjectionInformation | ||
""" | ||
da = ds[var] | ||
if not "grid_mapping" in da.attrs: | ||
return None | ||
pi = ProjectionInformation() | ||
pi._crs = CRS.from_cf(ds[da.grid_mapping].attrs) | ||
|
||
for c in da.coords: | ||
if len(da.coords[c].dims) != 1: | ||
continue | ||
if "standard_name" in da.coords[c].attrs: | ||
if da.coords[c].standard_name in ( | ||
"longitude", | ||
"grid_longitude", | ||
"projection_x_coordinate", | ||
): | ||
pi._x_axis = c | ||
break | ||
if "axis" in da.coords[c].attrs: | ||
if da.coords[c].axis in ("x", "X"): | ||
pi._x_axis = c | ||
break | ||
for c in da.coords: | ||
if len(da.coords[c].dims) != 1: | ||
continue | ||
if "standard_name" in da.coords[c].attrs: | ||
if da.coords[c].standard_name in ( | ||
"latitude", | ||
"grid_latitude", | ||
"projection_y_coordinate", | ||
): | ||
pi._y_axis = c | ||
break | ||
if "axis" in da.coords[c].attrs: | ||
if da.coords[c].axis in ("y", "Y"): | ||
pi._y_axis = c | ||
break | ||
if pi._x_axis is None or pi._y_axis is None: | ||
raise ProjectionInformationException(f"no x or y axis found for variable '{var}'") | ||
pi._units = da.coords[pi._y_axis].units | ||
return pi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.