Skip to content

Commit

Permalink
Make Field attributes read-only
Browse files Browse the repository at this point in the history
- data
- grid
- lon
- lat
- depth
- interp_method
- gridindexingtype
- cast_data_dtype
- cell_edge_sizes
  • Loading branch information
VeckoTheGecko committed Sep 18, 2024
1 parent 6c0f524 commit 8d4ae8a
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions parcels/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,15 @@ def __init__(
raise ValueError(
"Cannot combine Grid from defer_loaded Field with np.ndarray data. please specify lon, lat, depth and time dimensions separately"
)
self.grid = grid
self._grid = grid

Check warning on line 184 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L184

Added line #L184 was not covered by tests
else:
if (time is not None) and isinstance(time[0], np.datetime64):
time_origin = TimeConverter(time[0])
time = np.array([time_origin.reltime(t) for t in time])
else:
time_origin = TimeConverter(0)
self.grid = Grid.create_grid(lon, lat, depth, time, time_origin=time_origin, mesh=mesh)
self._grid = Grid.create_grid(lon, lat, depth, time, time_origin=time_origin, mesh=mesh)

Check warning on line 191 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L191

Added line #L191 was not covered by tests
self.igrid = -1
# self.lon, self.lat, self.depth and self.time are not used any more in parcels.
# self.grid should be used instead.
# Those variables are still defined for backwards compatibility with users codes.
self.lon = self.grid.lon
self.lat = self.grid.lat
self.depth = self.grid.depth
self.fieldtype = self.name if fieldtype is None else fieldtype
self.to_write = to_write
if self.grid.mesh == "flat" or (self.fieldtype not in unitconverters_map.keys()):
Expand All @@ -207,12 +201,12 @@ def __init__(
self.timestamps = timestamps
if isinstance(interp_method, dict):
if self.name in interp_method:
self.interp_method = interp_method[self.name]
self._interp_method = interp_method[self.name]
else:
raise RuntimeError(f"interp_method is a dictionary but {name} is not in it")
else:
self.interp_method = interp_method
self.gridindexingtype = gridindexingtype
self._interp_method = interp_method
self._gridindexingtype = gridindexingtype
if self.interp_method in ["bgrid_velocity", "bgrid_w_velocity", "bgrid_tracer"] and self.grid.gtype in [
GridType.RectilinearSGrid,
GridType.CurvilinearSGrid,
Expand Down Expand Up @@ -253,11 +247,11 @@ def __init__(

self.vmin = vmin
self.vmax = vmax
self.cast_data_dtype = cast_data_dtype
self._cast_data_dtype = cast_data_dtype

Check warning on line 250 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L250

Added line #L250 was not covered by tests
if self.cast_data_dtype == "float32":
self.cast_data_dtype = np.float32
self._cast_data_dtype = np.float32

Check warning on line 252 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L252

Added line #L252 was not covered by tests
elif self.cast_data_dtype == "float64":
self.cast_data_dtype = np.float64
self._cast_data_dtype = np.float64

Check warning on line 254 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L254

Added line #L254 was not covered by tests

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

@property
def grid(self):
return self._grid

Check warning on line 306 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L305-L306

Added lines #L305 - L306 were not covered by tests

@property
def lon(self):

Check warning on line 309 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L309

Added line #L309 was not covered by tests
"""Lon defined on the Grid object"""
return self.grid.lon

Check warning on line 311 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L311

Added line #L311 was not covered by tests

@property
def lat(self):

Check warning on line 314 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L314

Added line #L314 was not covered by tests
"""Lat defined on the Grid object"""
return self.grid.lat

Check warning on line 316 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L316

Added line #L316 was not covered by tests

@property
def depth(self):

Check warning on line 319 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L319

Added line #L319 was not covered by tests
"""Depth defined on the Grid object"""
return self.grid.depth

Check warning on line 321 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L321

Added line #L321 was not covered by tests

@property
def cell_edge_sizes(self):
return self.grid.cell_edge_sizes

Check warning on line 325 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L324-L325

Added lines #L324 - L325 were not covered by tests

@property
def interp_method(self):
return self._interp_method

Check warning on line 329 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L328-L329

Added lines #L328 - L329 were not covered by tests

@property
def gridindexingtype(self):
return self._gridindexingtype

Check warning on line 333 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L332-L333

Added lines #L332 - L333 were not covered by tests

@property
def cast_data_dtype(self):
return self._cast_data_dtype

Check warning on line 337 in parcels/field.py

View check run for this annotation

Codecov / codecov/patch

parcels/field.py#L336-L337

Added lines #L336 - L337 were not covered by tests

@classmethod
def get_dim_filenames(cls, filenames, dim):
if isinstance(filenames, str) or not isinstance(filenames, collections.abc.Iterable):
Expand Down Expand Up @@ -823,7 +852,6 @@ def calc_cell_edge_sizes(self):
for x, (lon, dx) in enumerate(zip(self.grid.lon, np.gradient(self.grid.lon), strict=False)):
self.grid.cell_edge_sizes["x"][y, x] = x_conv.to_source(dx, lon, lat, self.grid.depth[0])
self.grid.cell_edge_sizes["y"][y, x] = y_conv.to_source(dy, lon, lat, self.grid.depth[0])
self.cell_edge_sizes = self.grid.cell_edge_sizes
else:
raise ValueError(
f"Field.cell_edge_sizes() not implemented for {self.grid.gtype} grids. "
Expand Down Expand Up @@ -1542,8 +1570,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5, data=None):
(data[:, :, :, -halosize:], data, data[:, :, :, 0:halosize]), axis=len(data.shape) - 1
)
assert data.shape[3] == self.grid.xdim, "Fourth dim must be x."
self.lon = self.grid.lon
self.lat = self.grid.lat
if meridional:
if len(data.shape) == 3:
data = lib.concatenate((data[:, -halosize:, :], data, data[:, 0:halosize, :]), axis=len(data.shape) - 2)
Expand All @@ -1553,7 +1579,6 @@ def add_periodic_halo(self, zonal, meridional, halosize=5, data=None):
(data[:, :, -halosize:, :], data, data[:, :, 0:halosize, :]), axis=len(data.shape) - 2
)
assert data.shape[2] == self.grid.ydim, "Third dim must be y."
self.lat = self.grid.lat
if dataNone:
self.data = data
else:
Expand Down

0 comments on commit 8d4ae8a

Please sign in to comment.