@@ -284,7 +284,7 @@ def Update_lon(particle, fieldset, time):
284284def test_write_xiyi (fieldset , mode , tmpdir ):
285285 outfilepath = tmpdir .join ("pfile_xiyi.zarr" )
286286 fieldset .U .data [:] = 1 # set a non-zero zonal velocity
287- fieldset .add_field (Field (name = 'P' , data = np .zeros ((2 , 20 )), lon = np .linspace (0 , 1 , 20 ), lat = [0 , 2 ]))
287+ fieldset .add_field (Field (name = 'P' , data = np .zeros ((3 , 20 )), lon = np .linspace (0 , 1 , 20 ), lat = [- 2 , 0 , 2 ]))
288288 dt = 3600
289289
290290 XiYiParticle = ptype [mode ].add_variables ([
@@ -304,26 +304,29 @@ def Get_XiYi(particle, fieldset, time):
304304
305305 def SampleP (particle , fieldset , time ):
306306 if time > 5 * 3600 :
307- tmp = fieldset .P [particle ] # noqa
307+ _ = fieldset .P [particle ] # To trigger sampling of the P field
308308
309- pset = ParticleSet (fieldset , pclass = XiYiParticle , lon = [0 ], lat = [0.2 ], lonlatdepth_dtype = np .float64 )
309+ pset = ParticleSet (fieldset , pclass = XiYiParticle , lon = [0 , 0.2 ], lat = [0.2 , 1 ], lonlatdepth_dtype = np .float64 )
310310 pfile = pset .ParticleFile (name = outfilepath , outputdt = dt )
311- pset .execute ([Get_XiYi , SampleP , AdvectionRK4 ], endtime = 10 * dt , dt = dt , output_file = pfile )
311+ pset .execute ([SampleP , Get_XiYi , AdvectionRK4 ], endtime = 10 * dt , dt = dt , output_file = pfile )
312312
313313 ds = xr .open_zarr (outfilepath )
314- pxi0 = ds ['pxi0' ][:].values [0 ].astype (np .int32 )
315- pxi1 = ds ['pxi1' ][:].values [0 ].astype (np .int32 )
316- lons = ds ['lon' ][:].values [0 ]
317- pyi = ds ['pyi' ][:].values [0 ].astype (np .int32 )
318- lats = ds ['lat' ][:].values [0 ]
319-
320- assert (pxi0 [0 ] == 0 ) and (pxi0 [- 1 ] == 11 ) # check that particle has moved
321- assert np .all (pxi1 [:7 ] == 0 ) # check that particle has not been sampled on grid 1 until time 6
322- assert np .all (pxi1 [7 :] > 0 ) # check that particle has not been sampled on grid 1 after time 6
323- for xi , lon in zip (pxi0 [1 :], lons [1 :]):
324- assert fieldset .U .grid .lon [xi ] <= lon < fieldset .U .grid .lon [xi + 1 ]
325- for yi , lat in zip (pyi [1 :], lats [1 :]):
326- assert fieldset .U .grid .lat [yi ] <= lat < fieldset .U .grid .lat [yi + 1 ]
314+ pxi0 = ds ['pxi0' ][:].values .astype (np .int32 )
315+ pxi1 = ds ['pxi1' ][:].values .astype (np .int32 )
316+ lons = ds ['lon' ][:].values
317+ pyi = ds ['pyi' ][:].values .astype (np .int32 )
318+ lats = ds ['lat' ][:].values
319+
320+ for p in range (pyi .shape [0 ]):
321+ assert (pxi0 [p , 0 ] == 0 ) and (pxi0 [p , - 1 ] == pset [p ].pxi0 ) # check that particle has moved
322+ assert np .all (pxi1 [p , :6 ] == 0 ) # check that particle has not been sampled on grid 1 until time 6
323+ assert np .all (pxi1 [p , 6 :] > 0 ) # check that particle has not been sampled on grid 1 after time 6
324+ for xi , lon in zip (pxi0 [p , 1 :], lons [p , 1 :]):
325+ assert fieldset .U .grid .lon [xi ] <= lon < fieldset .U .grid .lon [xi + 1 ]
326+ for xi , lon in zip (pxi1 [p , 6 :], lons [p , 6 :]):
327+ assert fieldset .P .grid .lon [xi ] <= lon < fieldset .P .grid .lon [xi + 1 ]
328+ for yi , lat in zip (pyi [p , 1 :], lats [p , 1 :]):
329+ assert fieldset .U .grid .lat [yi ] <= lat < fieldset .U .grid .lat [yi + 1 ]
327330 ds .close ()
328331
329332
0 commit comments