1717from parcels ._core .xgrid import _DEFAULT_XGCM_KWARGS , XGrid
1818from parcels ._logger import logger
1919from parcels ._typing import Mesh
20+ from parcels .interpolators import XConstantField
2021
2122if TYPE_CHECKING :
2223 from parcels ._core .basegrid import BaseGrid
@@ -116,7 +117,7 @@ def add_field(self, field: Field, name: str | None = None):
116117
117118 self .fields [name ] = field
118119
119- def add_constant_field (self , name : str , value , mesh : Mesh = "flat " ):
120+ def add_constant_field (self , name : str , value , mesh : Mesh = "spherical " ):
120121 """Wrapper function to add a Field that is constant in space,
121122 useful e.g. when using constant horizontal diffusivity
122123
@@ -134,14 +135,24 @@ def add_constant_field(self, name: str, value, mesh: Mesh = "flat"):
134135 correction for zonal velocity U near the poles.
135136 2. flat: No conversion, lat/lon are assumed to be in m.
136137 """
137- ds = xr .Dataset ({name : (["time" , "lat" , "lon" , "depth" ], np .full ((1 , 1 , 1 , 1 ), value ))})
138- grid = XGrid (xgcm .Grid (ds , ** _DEFAULT_XGCM_KWARGS ))
138+ ds = xr .Dataset (
139+ {name : (["time" , "depth" , "lat" , "lon" ], np .full ((1 , 1 , 1 , 1 ), value ))},
140+ coords = {
141+ "time" : (["time" ], [np .timedelta64 (0 , "s" )], {"axis" : "T" }),
142+ "depth" : (["depth" ], [0 ], {"axis" : "Z" }),
143+ "lat" : (["lat" ], [0 ], {"axis" : "Y" , "c_grid_axis_shift" : - 0.5 }),
144+ "lon" : (["lon" ], [0 ], {"axis" : "X" , "c_grid_axis_shift" : - 0.5 }),
145+ "lat_C" : (["lat_C" ], [0.5 ], {"axis" : "Y" }), # TODO why is this needed?
146+ "lon_C" : (["lon_C" ], [0.5 ], {"axis" : "X" }), # TODO why is this needed?
147+ },
148+ )
149+ grid = XGrid (xgcm .Grid (ds , ** _DEFAULT_XGCM_KWARGS ), mesh = mesh )
139150 self .add_field (
140151 Field (
141152 name ,
142153 ds [name ],
143154 grid ,
144- interp_method = None , # TODO : Need to define an interpolation method for constants
155+ interp_method = XConstantField ,
145156 )
146157 )
147158
0 commit comments