Skip to content

Saving Dataset with time varying data  #90

@krober10nd

Description

@krober10nd

I have a dataset like number of timesteps by number of nodes representing free surface on an unstructured triangular mesh. I'd like to save it to a UGRID compliant file using this package. gridded is not recognizing that my free_surface variable instance occurs in time and it only creates an error upon writing. Any idea on what I'm doing incorrectly in the syntax?

Thanks,

"""
Format a NetCDF file to be UGRID compliant using the Python package `gridded`.
"""

import gridded
from gridded.grids import UGrid
import matplotlib.pyplot as plt

import xarray as xr
import numpy as np
from datetime import timedelta, datetime

fname = "r2d_HD_ETC_HISTORICAL_HISTORICAL_OBS_00139.nc"

ds = xr.open_dataset(fname)

nodes = np.c_[ds["x"].values, ds["y"].values]
tris = np.c_[ds["v1"], ds["v2"], ds["v3"]]
times = ds["time"].values
times = [datetime.utcfromtimestamp(t.astype("O") / 1e9) for t in times]

grid = UGrid(nodes=nodes, faces=tris)

grid.build_boundaries()

time_obj = gridded.time.Time(
    data=times,
)

free_surface = ds["free_surface"].values

fs_var = gridded.variable.Variable(
    name='free_surface',
    units="meters",
    time=time_obj,
    data=free_surface.T,
    grid=grid,
)

ds_new = gridded.Dataset(
    grid=grid,
    variables={"free_surface": fs_var},
)

ds_new.save("test.nc")

produces

this

Traceback (most recent call last):
  File "/mnt/c/Users/kroberts/Projects/BD/BineraBaird/Animations/format_file_v1.py", line 32, in <module>
    fs_var = gridded.variable.Variable(
  File "/mnt/c/Users/kroberts/Resources/ugrid/gridded/gridded/variable.py", line 113, in __init__
    self.data = data
  File "/mnt/c/Users/kroberts/Resources/ugrid/gridded/gridded/variable.py", line 381, in data
    raise ValueError("Data/grid shape mismatch: Data shape is {0}, "
ValueError: Data/grid shape mismatch: Data shape is (120950, 425), Grid shape is (120950,)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions