Skip to content

Commit 9d1112f

Browse files
committed
use bounds directly without converting to degrees
1 parent ecd3aa5 commit 9d1112f

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

uxarray/core/zonal.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ def _compute_non_conservative_zonal_mean(uxda, latitudes):
2424
for i, lat in enumerate(latitudes):
2525
face_indices = uxda.uxgrid.get_faces_at_constant_latitude(lat)
2626

27-
print(len(face_indices))
28-
29-
# z-coordinate in cartesian form
3027
z = np.sin(np.deg2rad(lat))
3128

3229
faces_edge_nodes_xyz_candidate = faces_edge_nodes_xyz[face_indices]

uxarray/grid/grid.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,9 +2357,13 @@ def get_faces_at_constant_latitude(
23572357
)
23582358

23592359
faces = constant_lat_intersections_face_bounds(
2360-
lat=lat,
2361-
face_bounds_lat=self.face_bounds_lat.values,
2360+
lat=lat, bounds=self.bounds.values
23622361
)
2362+
2363+
# faces = constant_lat_intersections_face_bounds(
2364+
# lat=lat,
2365+
# face_bounds_lat=self.face_bounds_lat.values,
2366+
# )
23632367
return faces
23642368

23652369
def get_edges_at_constant_longitude(

uxarray/grid/intersections.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,38 @@ def constant_lon_intersections_no_extreme(lon, edge_node_x, edge_node_y, n_edge)
128128
return np.unique(intersecting_edges)
129129

130130

131+
# @njit(cache=True)
132+
# def constant_lat_intersections_face_bounds(lat, face_bounds_lat):
133+
# """Identifies the candidate faces on a grid that intersect with a given
134+
# constant latitude.
135+
#
136+
# This function checks whether the specified latitude, `lat`, in degrees lies within
137+
# the latitude bounds of grid faces, defined by `face_min_lat_rad` and `face_max_lat_rad`,
138+
# which are given in radians. The function returns the indices of the faces where the
139+
# latitude is within these bounds.
140+
#
141+
# Parameters
142+
# ----------
143+
# lat : float
144+
# The latitude in degrees for which to find intersecting faces.
145+
# TODO:
146+
#
147+
# Returns
148+
# -------
149+
# candidate_faces : numpy.ndarray
150+
# A 1D array containing the indices of the faces that intersect with the given latitude.
151+
# """
152+
#
153+
# face_bounds_lat_min = face_bounds_lat[:, 0]
154+
# face_bounds_lat_max = face_bounds_lat[:, 1]
155+
#
156+
# within_bounds = (face_bounds_lat_min <= lat) & (face_bounds_lat_max >= lat)
157+
# candidate_faces = np.where(within_bounds)[0]
158+
# return
159+
160+
131161
@njit(cache=True)
132-
def constant_lat_intersections_face_bounds(lat, face_bounds_lat):
162+
def constant_lat_intersections_face_bounds(lat, bounds):
133163
"""Identifies the candidate faces on a grid that intersect with a given
134164
constant latitude.
135165
@@ -142,19 +172,33 @@ def constant_lat_intersections_face_bounds(lat, face_bounds_lat):
142172
----------
143173
lat : float
144174
The latitude in degrees for which to find intersecting faces.
145-
TODO:
175+
bounds: todo
176+
todo
146177
147178
Returns
148179
-------
149180
candidate_faces : numpy.ndarray
150181
A 1D array containing the indices of the faces that intersect with the given latitude.
151182
"""
152183

153-
face_bounds_lat_min = face_bounds_lat[:, 0]
154-
face_bounds_lat_max = face_bounds_lat[:, 1]
184+
lat_rad = np.deg2rad(lat)
185+
186+
# Check if the constant latitude is within the range of [-90, 90]
187+
if lat_rad < -np.pi or lat_rad > np.pi:
188+
raise ValueError(
189+
"The constant latitude must be within the range of [-90, 90] degree."
190+
)
155191

156-
within_bounds = (face_bounds_lat_min <= lat) & (face_bounds_lat_max >= lat)
192+
# Extract the latitude bounds
193+
lat_bounds_min = bounds[:, 0, 0] # Minimum latitude bound
194+
lat_bounds_max = bounds[:, 0, 1] # Maximum latitude bound
195+
196+
# Check if the constant latitude is within the bounds of each face
197+
within_bounds = (lat_bounds_min <= lat_rad) & (lat_bounds_max >= lat_rad)
198+
199+
# Get the indices of faces where the condition is True
157200
candidate_faces = np.where(within_bounds)[0]
201+
158202
return candidate_faces
159203

160204

0 commit comments

Comments
 (0)