Skip to content

Commit

Permalink
Grid attributes read only
Browse files Browse the repository at this point in the history
xdim, ydim
  • Loading branch information
VeckoTheGecko committed Sep 30, 2024
1 parent cf92019 commit 218b9bb
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions parcels/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,8 @@ def __init__(self, lon, lat, time, time_origin, mesh: Mesh):
assert len(time.shape) == 1, "time is not a vector"

super().__init__(lon, lat, time, time_origin, mesh)
self.xdim = self.lon.size
self.ydim = self.lat.size
self.tdim = self.time.size

if self.ydim > 1 and self.lat[-1] < self.lat[0]:
self._lat = np.flip(self.lat, axis=0)
self._lat_flipped = True
Expand All @@ -488,6 +487,14 @@ def __init__(self, lon, lat, time, time_origin, mesh: Mesh):
stacklevel=2,
)

@property
def xdim(self):
return self.lon.size

@property
def ydim(self):
return self.lat.size

def add_periodic_halo(self, zonal: bool, meridional: bool, halosize: int = 5):
"""Add a 'halo' to the Grid, through extending the Grid (and lon/lat)
similarly to the halo created for the Fields
Expand All @@ -512,7 +519,6 @@ def add_periodic_halo(self, zonal: bool, meridional: bool, halosize: int = 5):
stacklevel=2,
)
self._lon = np.concatenate((self.lon[-halosize:] - lonshift, self.lon, self.lon[0:halosize] + lonshift))
self.xdim = self.lon.size
self._zonal_periodic = True
self._zonal_halo = halosize
if meridional:
Expand All @@ -526,7 +532,6 @@ def add_periodic_halo(self, zonal: bool, meridional: bool, halosize: int = 5):
)
latshift = self.lat[-1] - 2 * self.lat[0] + self.lat[1]
self._lat = np.concatenate((self.lat[-halosize:] - latshift, self.lat, self.lat[0:halosize] + latshift))
self.ydim = self.lat.size
self._meridional_halo = halosize
self._lonlat_minmax = np.array(
[np.nanmin(self.lon), np.nanmax(self.lon), np.nanmin(self.lat), np.nanmax(self.lat)], dtype=np.float32
Expand Down Expand Up @@ -569,11 +574,14 @@ def __init__(self, lon, lat, depth=None, time=None, time_origin=None, mesh: Mesh
self._depth = np.zeros(1, dtype=np.float32) if depth is None else depth
if not self.depth.flags["C_CONTIGUOUS"]:
self._depth = np.array(self.depth, order="C")
self.zdim = self.depth.size
self._z4d = -1 # only used in RectilinearSGrid
if not self.depth.dtype == np.float32:
self._depth = self.depth.astype(np.float32)

@property
def zdim(self):
return self.depth.size


class RectilinearSGrid(RectilinearGrid):
"""Rectilinear S Grid. Same horizontal discretisation as a rectilinear z grid,
Expand Down Expand Up @@ -622,7 +630,6 @@ def __init__(
self._depth = depth
if not self.depth.flags["C_CONTIGUOUS"]:
self._depth = np.array(self.depth, order="C")
self.zdim = self.depth.shape[-3]
self._z4d = 1 if len(self.depth.shape) == 4 else 0
if self._z4d:
# self.depth.shape[0] is 0 for S grids loaded from netcdf file
Expand All @@ -647,6 +654,10 @@ def __init__(
if self._lat_flipped:
self._depth = np.flip(self.depth, axis=-2)

@property
def zdim(self):
return self.depth.shape[-3]


class CurvilinearGrid(Grid):
def __init__(
Expand All @@ -666,9 +677,14 @@ def __init__(
lon = lon.squeeze()
lat = lat.squeeze()
super().__init__(lon, lat, time, time_origin, mesh)
self.xdim = self.lon.shape[1]
self.ydim = self.lon.shape[0]
self.tdim = self.time.size

@property
def xdim(self):
return self.lon.shape[1]

@property
def ydim(self):
return self.lon.shape[0]

def add_periodic_halo(self, zonal, meridional, halosize=5):
"""Add a 'halo' to the Grid, through extending the Grid (and lon/lat)
Expand Down Expand Up @@ -704,8 +720,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5):
self._lat = np.concatenate(
(self.lat[:, -halosize:], self.lat, self.lat[:, 0:halosize]), axis=len(self.lat.shape) - 1
)
self.xdim = self.lon.shape[1]
self.ydim = self.lat.shape[0]
self._zonal_periodic = True
self._zonal_halo = halosize
if meridional:
Expand All @@ -729,8 +743,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5):
self._lon = np.concatenate(
(self.lon[-halosize:, :], self.lon, self.lon[0:halosize, :]), axis=len(self.lon.shape) - 2
)
self.xdim = self.lon.shape[1]
self.ydim = self.lat.shape[0]
self._meridional_halo = halosize
if isinstance(self, CurvilinearSGrid):
self._add_Sdepth_periodic_halo(zonal, meridional, halosize)
Expand Down Expand Up @@ -778,11 +790,14 @@ def __init__(
self._depth = np.zeros(1, dtype=np.float32) if depth is None else depth
if not self.depth.flags["C_CONTIGUOUS"]:
self._depth = np.array(self.depth, order="C")
self.zdim = self.depth.size
self._z4d = -1 # only for SGrid
if not self.depth.dtype == np.float32:
self._depth = self.depth.astype(np.float32)

@property
def zdim(self):
return self.depth.size


class CurvilinearSGrid(CurvilinearGrid):
"""Curvilinear S Grid.
Expand Down Expand Up @@ -830,7 +845,6 @@ def __init__(
self._depth = depth # should be a C-contiguous array of floats
if not self.depth.flags["C_CONTIGUOUS"]:
self._depth = np.array(self.depth, order="C")
self.zdim = self.depth.shape[-3]
self._z4d = 1 if len(self.depth.shape) == 4 else 0
if self._z4d:
# self.depth.shape[0] is 0 for S grids loaded from netcdf file
Expand All @@ -852,3 +866,7 @@ def __init__(
), "depth dimension has the wrong format. It should be [zdim, ydim, xdim]"
if not self.depth.dtype == np.float32:
self._depth = self.depth.astype(np.float32)

@property
def zdim(self):
return self.depth.shape[-3]

0 comments on commit 218b9bb

Please sign in to comment.