-
Notifications
You must be signed in to change notification settings - Fork 8
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
Comments
Some tedious details:
|
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")) |
This was referenced Oct 18, 2024
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: