11import functools
22import warnings
3- from ctypes import POINTER , Structure , c_double , c_float , c_int , c_void_p , cast , pointer
43from enum import IntEnum
54
65import numpy as np
1110from parcels .tools .warnings import FieldSetWarning
1211
1312__all__ = [
14- "CGrid" ,
1513 "CurvilinearSGrid" ,
1614 "CurvilinearZGrid" ,
1715 "Grid" ,
@@ -34,10 +32,6 @@ class GridType(IntEnum):
3432GridCode = GridType
3533
3634
37- class CGrid (Structure ):
38- _fields_ = [("gtype" , c_int ), ("grid" , c_void_p )]
39-
40-
4135class Grid :
4236 """Grid class that defines a (spatial and temporal) grid on which Fields are defined."""
4337
@@ -51,13 +45,10 @@ def __init__(
5145 ):
5246 self ._ti = - 1
5347 self ._update_status : UpdateStatus | None = None
54- if not lon .flags ["C_CONTIGUOUS" ]:
55- lon = np .array (lon , order = "C" )
56- if not lat .flags ["C_CONTIGUOUS" ]:
57- lat = np .array (lat , order = "C" )
48+ lon = np .array (lon )
49+ lat = np .array (lat )
5850 time = np .zeros (1 , dtype = np .float64 ) if time is None else time
59- if not time .flags ["C_CONTIGUOUS" ]:
60- time = np .array (time , order = "C" )
51+ time = np .array (time )
6152 if not lon .dtype == np .float32 :
6253 lon = lon .astype (np .float32 )
6354 if not lat .dtype == np .float32 :
@@ -76,7 +67,6 @@ def __init__(
7667 assert isinstance (self .time_origin , TimeConverter ), "time_origin needs to be a TimeConverter object"
7768 assert_valid_mesh (mesh )
7869 self ._mesh = mesh
79- self ._cstruct = None
8070 self ._cell_edge_sizes : dict [str , npt .NDArray ] = {}
8171 self ._zonal_periodic = False
8272 self ._zonal_halo = 0
@@ -179,67 +169,6 @@ def create_grid(
179169 else :
180170 return CurvilinearSGrid (lon , lat , depth , time , time_origin = time_origin , mesh = mesh , ** kwargs )
181171
182- @property
183- def ctypes_struct (self ):
184- # This is unnecessary for the moment, but it could be useful when going will fully unstructured grids
185- self ._cgrid = cast (pointer (self ._child_ctypes_struct ), c_void_p )
186- cstruct = CGrid (self ._gtype , self ._cgrid .value )
187- return cstruct
188-
189- @property
190- def _child_ctypes_struct (self ):
191- """Returns a ctypes struct object containing all relevant
192- pointers and sizes for this grid.
193- """
194-
195- class CStructuredGrid (Structure ):
196- # z4d is only to have same cstruct as RectilinearSGrid
197- _fields_ = [
198- ("xdim" , c_int ),
199- ("ydim" , c_int ),
200- ("zdim" , c_int ),
201- ("tdim" , c_int ),
202- ("z4d" , c_int ),
203- ("mesh_spherical" , c_int ),
204- ("zonal_periodic" , c_int ),
205- ("chunk_info" , POINTER (c_int )),
206- ("load_chunk" , POINTER (c_int )),
207- ("tfull_min" , c_double ),
208- ("tfull_max" , c_double ),
209- ("periods" , POINTER (c_int )),
210- ("lonlat_minmax" , POINTER (c_float )),
211- ("lon" , POINTER (c_float )),
212- ("lat" , POINTER (c_float )),
213- ("depth" , POINTER (c_float )),
214- ("time" , POINTER (c_double )),
215- ]
216-
217- # Create and populate the c-struct object
218- if not self ._cstruct : # Not to point to the same grid various times if grid in various fields
219- if not isinstance (self .periods , c_int ):
220- self .periods = c_int ()
221- self .periods .value = 0
222- self ._cstruct = CStructuredGrid (
223- self .xdim ,
224- self .ydim ,
225- self .zdim ,
226- self .tdim ,
227- self ._z4d ,
228- int (self .mesh == "spherical" ),
229- int (self .zonal_periodic ),
230- (c_int * len (self .chunk_info ))(* self .chunk_info ),
231- self ._load_chunk .ctypes .data_as (POINTER (c_int )),
232- self .time_full [0 ],
233- self .time_full [- 1 ],
234- pointer (self .periods ),
235- self .lonlat_minmax .ctypes .data_as (POINTER (c_float )),
236- self .lon .ctypes .data_as (POINTER (c_float )),
237- self .lat .ctypes .data_as (POINTER (c_float )),
238- self .depth .ctypes .data_as (POINTER (c_float )),
239- self .time .ctypes .data_as (POINTER (c_double )),
240- )
241- return self ._cstruct
242-
243172 def _check_zonal_periodic (self ):
244173 if self .zonal_periodic or self .mesh == "flat" or self .lon .size == 1 :
245174 return
@@ -278,7 +207,7 @@ def _add_Sdepth_periodic_halo(self, zonal, meridional, halosize):
278207
279208 def _computeTimeChunk (self , f , time , signdt ):
280209 nextTime_loc = np .inf if signdt >= 0 else - np .inf
281- periods = self .periods . value if isinstance ( self . periods , c_int ) else self . periods
210+ periods = self .periods
282211 prev_time_indices = self .time
283212 if self ._update_status == "not_updated" :
284213 if self ._ti >= 0 :
@@ -316,7 +245,7 @@ def _computeTimeChunk(self, f, time, signdt):
316245 if self ._ti == - 1 :
317246 self .time = self .time_full
318247 self ._ti , _ = f ._time_index (time )
319- periods = self .periods . value if isinstance ( self . periods , c_int ) else self . periods
248+ periods = self .periods
320249 if (
321250 signdt == - 1
322251 and self ._ti == 0
@@ -483,8 +412,7 @@ def __init__(self, lon, lat, depth=None, time=None, time_origin=None, mesh: Mesh
483412
484413 self ._gtype = GridType .RectilinearZGrid
485414 self ._depth = np .zeros (1 , dtype = np .float32 ) if depth is None else depth
486- if not self .depth .flags ["C_CONTIGUOUS" ]:
487- self ._depth = np .array (self .depth , order = "C" )
415+ self ._depth = np .array (self .depth )
488416 self ._z4d = - 1 # only used in RectilinearSGrid
489417 if not self .depth .dtype == np .float32 :
490418 self ._depth = self .depth .astype (np .float32 )
@@ -539,8 +467,7 @@ def __init__(
539467
540468 self ._gtype = GridType .RectilinearSGrid
541469 self ._depth = depth
542- if not self .depth .flags ["C_CONTIGUOUS" ]:
543- self ._depth = np .array (self .depth , order = "C" )
470+ self ._depth = np .array (self .depth )
544471 self ._z4d = 1 if len (self .depth .shape ) == 4 else 0
545472 if self ._z4d :
546473 # self.depth.shape[0] is 0 for S grids loaded from netcdf file
@@ -656,8 +583,6 @@ def __init__(
656583
657584 self ._gtype = GridType .CurvilinearZGrid
658585 self ._depth = np .zeros (1 , dtype = np .float32 ) if depth is None else depth
659- if not self .depth .flags ["C_CONTIGUOUS" ]:
660- self ._depth = np .array (self .depth , order = "C" )
661586 self ._z4d = - 1 # only for SGrid
662587 if not self .depth .dtype == np .float32 :
663588 self ._depth = self .depth .astype (np .float32 )
@@ -710,9 +635,7 @@ def __init__(
710635 assert isinstance (depth , np .ndarray ) and len (depth .shape ) in [3 , 4 ], "depth is not a 4D numpy array"
711636
712637 self ._gtype = GridType .CurvilinearSGrid
713- self ._depth = depth # should be a C-contiguous array of floats
714- if not self .depth .flags ["C_CONTIGUOUS" ]:
715- self ._depth = np .array (self .depth , order = "C" )
638+ self ._depth = depth
716639 self ._z4d = 1 if len (self .depth .shape ) == 4 else 0
717640 if self ._z4d :
718641 # self.depth.shape[0] is 0 for S grids loaded from netcdf file
0 commit comments