Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UgridDataset.from_structured method #8

Closed
Huite opened this issue Dec 20, 2021 · 2 comments · Fixed by #303
Closed

Add UgridDataset.from_structured method #8

Huite opened this issue Dec 20, 2021 · 2 comments · Fixed by #303

Comments

@Huite
Copy link
Collaborator

Huite commented Dec 20, 2021

UgridDataArray already has a from_structured constructor, but doing so for every variable of a dataset means rederiving the UGRID topology every time -- so just do it once, then figure out how to add every variable.

@Huite
Copy link
Collaborator Author

Huite commented Jan 24, 2024

Some tedious details:

  • There may be multiple topologies present in a single dataset.
  • These could all be converted into single UgridDataset (since it supports multiple topologies), but this requires inventing names.
  • Perhaps the easiest is to add an argument mapping {topology_name: (x_coordinate, y_coordinate)}
  • If None, this defaults to {"mesh2d": ("x", "y")} (which somewhat bakes in a 2D structured topology, but I guess 3D structured topology is really far away enough...). This ensures uds = xu.UgridDataset.from_structured(ds) works for a lot of common cases.
  • All non-associated variables are skipped.
  • Unlike a DataArray, there's no such thing as a consistent dimension order across variables in a Dataset. Probably best to use an .isel to deal with potentially transposed (x before y) variables?

@veenstrajelmer
Copy link
Collaborator

veenstrajelmer commented Apr 3, 2024

This would be amazing. The cumbersome workaround code is below. Also note the rename_dims and rename_vars that are necessary until #9 is fixed.

import xarray as xr
import xugrid as xu
import matplotlib.pyplot as plt
plt.close("all")

def convert_struct_to_ugrid(ds):
    ds = ds.rename_dims({"longitude":"x","latitude":"y"})
    ds = ds.rename_vars({"longitude":"x","latitude":"y"})
    list_vars = []
    for var in ds.data_vars:
        uda = xu.UgridDataArray.from_structured(ds[var])
        list_vars.append(uda)
    uds = xu.UgridDataset(grids=[uda.grid])
    for var in list_vars:
        uds[var.name] = var
    
    return uds

file_nc = r"c:\DATA\dfm_tools\docs\notebooks\Bonaire_model\data\cmems\cmems_so_2022-11-01.nc"

ds = xr.open_dataset(file_nc)
uds = convert_struct_to_ugrid(ds)

fig,ax = plt.subplots()
ds.so.isel(time=-1, depth=0).plot()

fig,ax = plt.subplots()
uds.so.isel(time=-1, depth=0).ugrid.plot()
uds.ugrid.to_netcdf(file_nc.replace(".nc","_ugrid.nc"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants