-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add ying yang grid #20
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# 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. | ||
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, lat: np.ndarray, lon: np.ndarray) -> tuple[np.ndarray, np.ndarray]: | ||
""" | ||
Compute the projected x,y from lat,lon. | ||
""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def inverse_project(self, x: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, np.ndarray]: | ||
""" | ||
Compute the lat,lon 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_x, mesh_y = np.meshgrid(self.x, self.y, indexing='ij') | ||
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. i needed to change this to use indexing=ij here versus the original which was 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. I don't really have a preference, I think I just copied LatLongGrid which uses Lat (Y) first. 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. Do you need to update the LCC tests? |
||
return self.projection.inverse_project(mesh_x, mesh_y) | ||
|
||
@property | ||
def lat(self): | ||
return self.lat_lon[0] | ||
|
||
@property | ||
def lon(self): | ||
return self.lat_lon[1] | ||
|
||
@property | ||
def shape(self): | ||
return (len(self.x), len(self.y)) | ||
|
||
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(lat, lon) | ||
|
||
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 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 |
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 |
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).
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
.