|
309 | 309 | " \"node_lon\": ((\"n_node\",), points[:, 0]),\n", |
310 | 310 | " \"node_lat\": ((\"n_node\",), points[:, 1]),\n", |
311 | 311 | " \"face_node_connectivity\": ((\"n_face\", \"n_max_face_nodes\"), face_tris),\n", |
312 | | - " \"face_polygon\": (\n", |
| 312 | + " \"GridID\": (\n", |
313 | 313 | " (\n", |
314 | 314 | " \"time\",\n", |
315 | | - " \"zf\",\n", |
| 315 | + " \"zc\",\n", |
316 | 316 | " \"n_face\",\n", |
317 | 317 | " ),\n", |
318 | 318 | " face_poly[np.newaxis, np.newaxis, :],\n", |
|
325 | 325 | " },\n", |
326 | 326 | " coords={\n", |
327 | 327 | " \"time\": np.array([np.timedelta64(0, \"ns\")]),\n", |
328 | | - " \"zf\": np.array([0]),\n", |
| 328 | + " \"zf\": np.array([0, 1]),\n", |
| 329 | + " \"zc\": np.array([0.5]),\n", |
329 | 330 | " \"n_node\": np.arange(n_node),\n", |
330 | 331 | " \"n_face\": np.arange(n_face),\n", |
331 | 332 | " },\n", |
332 | 333 | " attrs={\"Conventions\": \"UGRID-1.0\"},\n", |
333 | 334 | ")\n", |
334 | | - "\n", |
335 | | - "uxda = ux.UxDataArray(ds_tri[\"face_polygon\"], uxgrid=ux.Grid(ds_tri))\n", |
336 | | - "\n", |
337 | | - "GridID = parcels.Field(\n", |
338 | | - " \"GridID\",\n", |
339 | | - " uxda,\n", |
340 | | - " # TODO note that here we need to use mesh=\"flat\" otherwise the hashing doesn't work. See https://github.com/Parcels-code/Parcels/pull/2439#discussion_r2627664010\n", |
341 | | - " parcels.UxGrid(uxda.uxgrid, z=uxda[\"zf\"], mesh=\"flat\"),\n", |
342 | | - " interp_method=parcels.interpolators.UxConstantFaceConstantZC,\n", |
343 | | - ")\n", |
344 | | - "fieldset = parcels.FieldSet([GridID])" |
| 335 | + "uxda = ux.UxDataArray(ds_tri[\"GridID\"], uxgrid=ux.Grid(ds_tri)).to_dataset()\n", |
| 336 | + "uxda = uxda.assign_coords({\"zf\": ds_tri.coords[\"zf\"]})\n", |
| 337 | + "fieldset = parcels.FieldSet.from_ugrid_conventions(uxda, mesh=\"flat\")" |
345 | 338 | ] |
346 | 339 | }, |
347 | 340 | { |
|
407 | 400 | " fieldset.GridID.grid.uxgrid.node_lat.values,\n", |
408 | 401 | " triangles=fieldset.GridID.grid.uxgrid.face_node_connectivity.values,\n", |
409 | 402 | ")\n", |
410 | | - "facecolors = np.squeeze(uxda[0, :].values)\n", |
| 403 | + "facecolors = np.squeeze(uxda.GridID[0, :].values)\n", |
411 | 404 | "\n", |
412 | 405 | "ax.tripcolor(triang, facecolors=facecolors, shading=\"flat\", **plot_args)\n", |
413 | 406 | "ax.scatter(pset.lon, pset.lat, c=pset.gridID, **plot_args)\n", |
|
438 | 431 | "metadata": {}, |
439 | 432 | "outputs": [], |
440 | 433 | "source": [ |
441 | | - "fields = [GridID]\n", |
442 | 434 | "for i, ds in enumerate(ds_in):\n", |
443 | 435 | " fset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"spherical\")\n", |
444 | 436 | "\n", |
445 | | - " for fld in fset.fields.values():\n", |
446 | | - " fld.name = f\"{fld.name}{i}\"\n", |
447 | | - " fields.append(fld)\n", |
448 | | - "fieldset = parcels.FieldSet(fields)" |
| 437 | + " # Rename and add fields to the main fieldset\n", |
| 438 | + " # TODO This needs a beeter API!\n", |
| 439 | + " for old_name, fld in list(fset.fields.items()):\n", |
| 440 | + " new_name = f\"{old_name}{i}\"\n", |
| 441 | + " fld.name = new_name\n", |
| 442 | + " fieldset._fields[new_name] = fld" |
449 | 443 | ] |
450 | 444 | }, |
451 | 445 | { |
|
471 | 465 | " unique_ids = np.unique(particles.gridID)\n", |
472 | 466 | " for gid in unique_ids:\n", |
473 | 467 | " mask = particles.gridID == gid\n", |
474 | | - " UVField = getattr(fieldset, f\"UV{gid}\")\n", |
| 468 | + " UVField = fieldset._fields[f\"UV{gid}\"]\n", |
475 | 469 | " (u[mask], v[mask]) = UVField[particles[mask]]\n", |
476 | 470 | "\n", |
477 | 471 | " particles.dlon += u * particles.dt\n", |
|
550 | 544 | ], |
551 | 545 | "metadata": { |
552 | 546 | "kernelspec": { |
553 | | - "display_name": "docs", |
| 547 | + "display_name": "Python 3", |
554 | 548 | "language": "python", |
555 | 549 | "name": "python3" |
556 | 550 | }, |
|
564 | 558 | "name": "python", |
565 | 559 | "nbconvert_exporter": "python", |
566 | 560 | "pygments_lexer": "ipython3", |
567 | | - "version": "3.14.4" |
| 561 | + "version": "3.14.6" |
568 | 562 | } |
569 | 563 | }, |
570 | 564 | "nbformat": 4, |
|
0 commit comments