Skip to content

Commit 8d4ae8a

Browse files
committed
Make Field attributes read-only
- data - grid - lon - lat - depth - interp_method - gridindexingtype - cast_data_dtype - cell_edge_sizes
1 parent 6c0f524 commit 8d4ae8a

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

parcels/field.py

+43-18
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,15 @@ def __init__(
181181
raise ValueError(
182182
"Cannot combine Grid from defer_loaded Field with np.ndarray data. please specify lon, lat, depth and time dimensions separately"
183183
)
184-
self.grid = grid
184+
self._grid = grid
185185
else:
186186
if (time is not None) and isinstance(time[0], np.datetime64):
187187
time_origin = TimeConverter(time[0])
188188
time = np.array([time_origin.reltime(t) for t in time])
189189
else:
190190
time_origin = TimeConverter(0)
191-
self.grid = Grid.create_grid(lon, lat, depth, time, time_origin=time_origin, mesh=mesh)
191+
self._grid = Grid.create_grid(lon, lat, depth, time, time_origin=time_origin, mesh=mesh)
192192
self.igrid = -1
193-
# self.lon, self.lat, self.depth and self.time are not used any more in parcels.
194-
# self.grid should be used instead.
195-
# Those variables are still defined for backwards compatibility with users codes.
196-
self.lon = self.grid.lon
197-
self.lat = self.grid.lat
198-
self.depth = self.grid.depth
199193
self.fieldtype = self.name if fieldtype is None else fieldtype
200194
self.to_write = to_write
201195
if self.grid.mesh == "flat" or (self.fieldtype not in unitconverters_map.keys()):
@@ -207,12 +201,12 @@ def __init__(
207201
self.timestamps = timestamps
208202
if isinstance(interp_method, dict):
209203
if self.name in interp_method:
210-
self.interp_method = interp_method[self.name]
204+
self._interp_method = interp_method[self.name]
211205
else:
212206
raise RuntimeError(f"interp_method is a dictionary but {name} is not in it")
213207
else:
214-
self.interp_method = interp_method
215-
self.gridindexingtype = gridindexingtype
208+
self._interp_method = interp_method
209+
self._gridindexingtype = gridindexingtype
216210
if self.interp_method in ["bgrid_velocity", "bgrid_w_velocity", "bgrid_tracer"] and self.grid.gtype in [
217211
GridType.RectilinearSGrid,
218212
GridType.CurvilinearSGrid,
@@ -253,11 +247,11 @@ def __init__(
253247

254248
self.vmin = vmin
255249
self.vmax = vmax
256-
self.cast_data_dtype = cast_data_dtype
250+
self._cast_data_dtype = cast_data_dtype
257251
if self.cast_data_dtype == "float32":
258-
self.cast_data_dtype = np.float32
252+
self._cast_data_dtype = np.float32
259253
elif self.cast_data_dtype == "float64":
260-
self.cast_data_dtype = np.float64
254+
self._cast_data_dtype = np.float64
261255

262256
if not self.grid.defer_load:
263257
self.data = self.reshape(self.data, transpose)
@@ -307,6 +301,41 @@ def __init__(
307301
if len(kwargs) > 0:
308302
raise SyntaxError(f'Field received an unexpected keyword argument "{list(kwargs.keys())[0]}"')
309303

304+
@property
305+
def grid(self):
306+
return self._grid
307+
308+
@property
309+
def lon(self):
310+
"""Lon defined on the Grid object"""
311+
return self.grid.lon
312+
313+
@property
314+
def lat(self):
315+
"""Lat defined on the Grid object"""
316+
return self.grid.lat
317+
318+
@property
319+
def depth(self):
320+
"""Depth defined on the Grid object"""
321+
return self.grid.depth
322+
323+
@property
324+
def cell_edge_sizes(self):
325+
return self.grid.cell_edge_sizes
326+
327+
@property
328+
def interp_method(self):
329+
return self._interp_method
330+
331+
@property
332+
def gridindexingtype(self):
333+
return self._gridindexingtype
334+
335+
@property
336+
def cast_data_dtype(self):
337+
return self._cast_data_dtype
338+
310339
@classmethod
311340
def get_dim_filenames(cls, filenames, dim):
312341
if isinstance(filenames, str) or not isinstance(filenames, collections.abc.Iterable):
@@ -823,7 +852,6 @@ def calc_cell_edge_sizes(self):
823852
for x, (lon, dx) in enumerate(zip(self.grid.lon, np.gradient(self.grid.lon), strict=False)):
824853
self.grid.cell_edge_sizes["x"][y, x] = x_conv.to_source(dx, lon, lat, self.grid.depth[0])
825854
self.grid.cell_edge_sizes["y"][y, x] = y_conv.to_source(dy, lon, lat, self.grid.depth[0])
826-
self.cell_edge_sizes = self.grid.cell_edge_sizes
827855
else:
828856
raise ValueError(
829857
f"Field.cell_edge_sizes() not implemented for {self.grid.gtype} grids. "
@@ -1542,8 +1570,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5, data=None):
15421570
(data[:, :, :, -halosize:], data, data[:, :, :, 0:halosize]), axis=len(data.shape) - 1
15431571
)
15441572
assert data.shape[3] == self.grid.xdim, "Fourth dim must be x."
1545-
self.lon = self.grid.lon
1546-
self.lat = self.grid.lat
15471573
if meridional:
15481574
if len(data.shape) == 3:
15491575
data = lib.concatenate((data[:, -halosize:, :], data, data[:, 0:halosize, :]), axis=len(data.shape) - 2)
@@ -1553,7 +1579,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5, data=None):
15531579
(data[:, :, -halosize:, :], data, data[:, :, 0:halosize, :]), axis=len(data.shape) - 2
15541580
)
15551581
assert data.shape[2] == self.grid.ydim, "Third dim must be y."
1556-
self.lat = self.grid.lat
15571582
if dataNone:
15581583
self.data = data
15591584
else:

0 commit comments

Comments
 (0)