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,16 +135,15 @@ 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 ))
139- self .add_field (
140- Field (
141- name ,
142- ds [name ],
143- grid ,
144- interp_method = None , # TODO : Need to define an interpolation method for constants
145- )
138+ ds = xr .Dataset (
139+ {name : (["lat" , "lon" ], np .full ((1 , 1 ), value ))},
140+ coords = {"lat" : (["lat" ], [0 ], {"axis" : "Y" }), "lon" : (["lon" ], [0 ], {"axis" : "X" })},
141+ )
142+ xgrid = xgcm .Grid (
143+ ds , coords = {"X" : {"left" : "lon" }, "Y" : {"left" : "lat" }}, autoparse_metadata = False , ** _DEFAULT_XGCM_KWARGS
146144 )
145+ grid = XGrid (xgrid , mesh = mesh )
146+ self .add_field (Field (name , ds [name ], grid , interp_method = XConstantField ))
147147
148148 def add_constant (self , name , value ):
149149 """Add a constant to the FieldSet. Note that all constants are
0 commit comments