-
Notifications
You must be signed in to change notification settings - Fork 6
Add ying yang grid #20
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,112 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Conventions: | ||
|
||
- lon,lat is preferred to lat,lon for projection equations (i.e. x=lon in Plate | ||
Carree) | ||
- projected grids have shape [ny, nx] | ||
""" | ||
import abc | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from earth2grid import base | ||
from earth2grid._regrid import BilinearInterpolator | ||
|
||
try: | ||
import pyvista as pv | ||
except ImportError: | ||
pv = None | ||
|
||
|
||
class Projection(abc.ABC): | ||
@abc.abstractmethod | ||
def project(self, lon: np.ndarray, lat: np.ndarray) -> tuple[np.ndarray, np.ndarray]: | ||
""" | ||
Compute the projected x,y from lon,lat. | ||
|
||
""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def inverse_project(self, x: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, np.ndarray]: | ||
""" | ||
Compute the lon,lat from the projected x,y. | ||
""" | ||
pass | ||
|
||
|
||
class Grid(base.Grid): | ||
# nothing here is specific to the projection, so could be shared by any projected rectilinear grid | ||
def __init__(self, projection: Projection, x, y): | ||
""" | ||
Args: | ||
x: range of x values | ||
y: range of y values | ||
|
||
""" | ||
self.projection = projection | ||
|
||
self.x = np.array(x) | ||
self.y = np.array(y) | ||
|
||
@property | ||
def lat_lon(self): | ||
mesh_y, mesh_x = np.meshgrid(self.y, self.x, indexing='ij') | ||
return self.projection.inverse_project(mesh_x, mesh_y) | ||
|
||
@property | ||
def lat(self): | ||
return self.lat_lon[1] | ||
|
||
@property | ||
def lon(self): | ||
return self.lat_lon[0] | ||
|
||
@property | ||
def shape(self): | ||
return (len(self.y), len(self.x)) | ||
|
||
def get_bilinear_regridder_to(self, lat: np.ndarray, lon: np.ndarray): | ||
"""Get regridder to the specified lat and lon points""" | ||
|
||
x, y = self.projection.project(lon, lat) | ||
|
||
return BilinearInterpolator( | ||
x_coords=torch.from_numpy(self.x), | ||
y_coords=torch.from_numpy(self.y), | ||
x_query=torch.from_numpy(x), | ||
y_query=torch.from_numpy(y), | ||
) | ||
|
||
def __getitem__(self, idxs) -> "Grid": | ||
yidxs, xidxs = idxs | ||
return Grid(self.projection, x=self.x[xidxs], y=self.y[yidxs]) | ||
|
||
def visualize(self, data): | ||
raise NotImplementedError() | ||
|
||
def to_pyvista(self): | ||
if pv is None: | ||
raise ImportError("Need to install pyvista") | ||
|
||
lat, lon = self.lat_lon | ||
y = np.cos(np.deg2rad(lat)) * np.sin(np.deg2rad(lon)) | ||
x = np.cos(np.deg2rad(lat)) * np.cos(np.deg2rad(lon)) | ||
z = np.sin(np.deg2rad(lat)) | ||
grid = pv.StructuredGrid(x, y, z) | ||
return grid |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -44,3 +44,10 @@ def ang2vec(lon, lat): | |||||
y = torch.cos(lat) * torch.sin(lon) | ||||||
z = torch.sin(lat) | ||||||
return (x, y, z) | ||||||
|
||||||
|
||||||
def vec2ang(x, y, z): | ||||||
"""convert lon,lat in radians to cartesian coordinates""" | ||||||
lat = torch.asin(z) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes your inputs are already normalized. If you want it to work with non-normalized inputs, you would need
Suggested change
|
||||||
lon = torch.atan2(y, x) | ||||||
return lon, lat |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just renamed your LCC grid class @simonbyrne . Intended to be used like
earth2grid.projections.Grid
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you want to give it a more specific name (i.e. it assumes the underlying grid is rectilinear, not an unstructured mesh).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "projection" implies rectangle. This is just a style preference for
projection.Grid
overprojection.ProjectionGrid
. Same style ashealpix.Grid
.