@@ -8,6 +8,7 @@ import Thermodynamics as TD
88import ClimaOcean. EN4: download_dataset
99using KernelAbstractions: @kernel , @index , @inbounds
1010using XESMF # to load Oceananigans regridding extension
11+ using PythonCall
1112
1213OceananigansXESMFExt = Base. get_extension (OC, :OceananigansXESMFExt ). OceananigansXESMFExt;
1314
@@ -204,9 +205,20 @@ function construct_remappers(grid, boundary_space)
204205
205206 coords_climacore = Dict (" lat" => climacore_lat, " lon" => climacore_lon)
206207
208+ # Make arrays F-contiguous (column major) using `np.asfortranarray` to reduce memory footprint and
209+ # prevent segfaults since XESMF works more efficiently with F-contiguous arrays.
210+ np = pyimport (" numpy" )
211+ coords_oc_contiguous =
212+ Dict (k => pyconvert (Array, np. asfortranarray (v)) for (k, v) in coords_oc)
213+ coords_climacore_contiguous =
214+ Dict (k => pyconvert (Array, np. asfortranarray (v)) for (k, v) in coords_climacore)
215+
207216 # Construct the XESMF regridder object
208- regridder_oceananigans_to_climacore =
209- XESMF. Regridder (coords_oc, coords_climacore; method = " bilinear" )
217+ regridder_oceananigans_to_climacore = XESMF. Regridder (
218+ coords_oc_contiguous,
219+ coords_climacore_contiguous;
220+ method = " bilinear" ,
221+ )
210222
211223 # Allocate space for source an destination vectors to use as intermediate storage
212224 src_vec_oc = Array (vec (OC. Field {OC.Center, OC.Center, Nothing} (grid))) # 2D field on Center/Center
@@ -223,10 +235,17 @@ function construct_remappers(grid, boundary_space)
223235 )
224236
225237 # # Remapper: Cubed sphere nodes to Oceananigans grid `Center, Center`
238+ # For a TripolarGrid, latitude and longitude are already 2D arrays,
239+ # so we broadcast over them directly.
226240 long_oc = OC. λnodes (grid, OC. Center (), OC. Center (), OC. Center ())
227241 lat_oc = OC. φnodes (grid, OC. Center (), OC. Center (), OC. Center ())
228- long_oc = reshape (long_oc, length (long_oc), 1 )
229- lat_oc = reshape (lat_oc, 1 , length (lat_oc))
242+
243+ # For a LatitudeLongitudeGrid, latitude and longitude are 1D collections,
244+ # so we reshape them to 2D arrays.
245+ if grid isa OC. LatitudeLongitudeGrid
246+ long_oc = reshape (long_oc, length (long_oc), 1 )
247+ lat_oc = reshape (lat_oc, 1 , length (lat_oc))
248+ end
230249 target_points_oc = @. CC. Geometry. LatLongPoint (lat_oc, long_oc)
231250
232251 # Construct the ClimaCore remapper object
0 commit comments