13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
import numpy as np
16
- import torch
17
16
18
- from earth2grid import base
19
- from earth2grid ._regrid import BilinearInterpolator
17
+ from earth2grid import projections
20
18
21
19
try :
22
20
import pyvista as pv
31
29
]
32
30
33
31
34
- class LambertConformalConicProjection :
32
+ LambertConformalConicGrid = projections .Grid
33
+
34
+
35
+ class LambertConformalConicProjection (projections .Projection ):
35
36
def __init__ (self , lat0 : float , lon0 : float , lat1 : float , lat2 : float , radius : float ):
36
37
"""
37
38
@@ -80,7 +81,7 @@ def _theta(self, lon):
80
81
delta_lon = delta_lon - np .round (delta_lon / 360 ) * 360 # convert to [-180, 180]
81
82
return self .n * np .deg2rad (delta_lon )
82
83
83
- def project (self , lat , lon ):
84
+ def project (self , lon , lat ):
84
85
"""
85
86
Compute the projected x,y from lat,lon.
86
87
"""
@@ -100,90 +101,27 @@ def inverse_project(self, x, y):
100
101
101
102
lat = np .rad2deg (2 * np .arctan (np .power (self .RF / rho , 1 / self .n ))) - 90
102
103
lon = self .lon0 + np .rad2deg (theta / self .n )
103
- return lat , lon
104
+ return lon , lat
104
105
105
106
106
107
# Projection used by HRRR CONUS (Continental US) data
107
108
# https://rapidrefresh.noaa.gov/hrrr/HRRR_conus.domain.txt
108
109
HRRR_CONUS_PROJECTION = LambertConformalConicProjection (lon0 = - 97.5 , lat0 = 38.5 , lat1 = 38.5 , lat2 = 38.5 , radius = 6371229.0 )
109
110
110
111
111
- class LambertConformalConicGrid (base .Grid ):
112
- # nothing here is specific to the projection, so could be shared by any projected rectilinear grid
113
- def __init__ (self , projection : LambertConformalConicProjection , x , y ):
114
- """
115
- Args:
116
- projection: LambertConformalConicProjection object
117
- x: range of x values
118
- y: range of y values
119
-
120
- """
121
- self .projection = projection
122
-
123
- self .x = np .array (x )
124
- self .y = np .array (y )
125
-
126
- @property
127
- def lat_lon (self ):
128
- mesh_x , mesh_y = np .meshgrid (self .x , self .y )
129
- return self .projection .inverse_project (mesh_x , mesh_y )
130
-
131
- @property
132
- def lat (self ):
133
- return self .lat_lon [0 ]
134
-
135
- @property
136
- def lon (self ):
137
- return self .lat_lon [1 ]
138
-
139
- @property
140
- def shape (self ):
141
- return (len (self .y ), len (self .x ))
142
-
143
- def __getitem__ (self , idxs ):
144
- yidxs , xidxs = idxs
145
- return LambertConformalConicGrid (self .projection , x = self .x [xidxs ], y = self .y [yidxs ])
146
-
147
- def get_bilinear_regridder_to (self , lat : np .ndarray , lon : np .ndarray ):
148
- """Get regridder to the specified lat and lon points"""
149
-
150
- x , y = self .projection .project (lat , lon )
151
-
152
- return BilinearInterpolator (
153
- x_coords = torch .from_numpy (self .x ),
154
- y_coords = torch .from_numpy (self .y ),
155
- x_query = torch .from_numpy (x ),
156
- y_query = torch .from_numpy (y ),
157
- )
158
-
159
- def visualize (self , data ):
160
- raise NotImplementedError ()
161
-
162
- def to_pyvista (self ):
163
- if pv is None :
164
- raise ImportError ("Need to install pyvista" )
165
-
166
- lat , lon = self .lat_lon
167
- y = np .cos (np .deg2rad (lat )) * np .sin (np .deg2rad (lon ))
168
- x = np .cos (np .deg2rad (lat )) * np .cos (np .deg2rad (lon ))
169
- z = np .sin (np .deg2rad (lat ))
170
- grid = pv .StructuredGrid (x , y , z )
171
- return grid
172
-
173
-
174
112
def hrrr_conus_grid (ix0 = 0 , iy0 = 0 , nx = 1799 , ny = 1059 ):
175
113
# coordinates of point in top-left corner
176
114
lat0 = 21.138123
177
115
lon0 = 237.280472
178
116
# grid length (m)
179
117
scale = 3000.0
180
118
# coordinates on projected space
181
- x0 , y0 = HRRR_CONUS_PROJECTION .project (lat0 , lon0 )
119
+ x0 , y0 = HRRR_CONUS_PROJECTION .project (lon0 , lat0 )
182
120
183
121
x = [x0 + i * scale for i in range (ix0 , ix0 + nx )]
184
122
y = [y0 + i * scale for i in range (iy0 , iy0 + ny )]
185
123
186
- return LambertConformalConicGrid (HRRR_CONUS_PROJECTION , x , y )
124
+ return projections . Grid (HRRR_CONUS_PROJECTION , x , y )
187
125
188
126
189
127
# Grid used by HRRR CONUS (Continental US) data
0 commit comments