DHI/mikeio

NetCDF to Dfsu conversion

tofivan opened this issue · 1 comments

I have a problem with the NetCDF to Dfsu conversion. I have a POM dataset in NetCDF format that needs to be converted to Dfsu. My conversion process is as follows:
The necessary libraries, such as numpy, xarray, mikeio, pandas, os, and datetime, are imported.

The input and output folders are defined.

If the output folder does not exist, it is created.

The template Dfsu file is read using mikeio.Dfsu.

A loop is set up to iterate over all the files in the input folder. Only files with the extension ".nc" will be processed.

Inside the loop, each NetCDF file is opened using xr.open_dataset. The keys (variable names) present in the NetCDF file are printed.

The required variables ("u", "v", "w", "t", "s", "time") are extracted from the NetCDF dataset.

Coordinates for the mesh grid are created using numpy.linspace.

An empty list named data is created to store the DataArray objects.

A Grid3D object is created using the defined coordinates and projection.

A loop is set up to iterate over the layers and create a DataArray for each variable. The item parameter specifies the variable name, type, and unit.

A Dataset object is created using the list of DataArray objects and the time array.

The data is saved to a new Dfsu3D file using the write method from the template Dfsu file. The output file name is created by appending "_1pom.dfsu" to the original file name.

The start and end time of the code execution are printed using datetime.datetime.now().strftime.

This program reads NetCDF files, extracts specific variables, creates a mesh grid, and converts the data into Dfsu3D format using the mikeio library. The converted files are saved in the specified output folder.

However, an error message occurred:

Traceback (most recent call last):

File ~\anaconda3\envs\mikeio\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)

File d:\mikeio\untitled5.py:68
dfs.write(output_filename, my_ds)

File ~\anaconda3\envs\mikeio\lib\site-packages\mikeio\dfsu_dfsu.py:995 in write

return self._write(

File ~\anaconda3\envs\mikeio\lib\site-packages\mikeio\dfsu_dfsu.py:1057 in _write
if data.geometry.is_layered:

AttributeError: 'Grid3D' object has no attribute 'is_layered'

Attached is my program. Could you assist me in improving this program? Thank you!
pom_to_dfsu.zip

Dfsu is a file format for unstructured grids (flexible mesh) while Grid3D is a geometry used by the Dfs3 file format (cartesian 3d grid).

I am not familiar with the grid used by POM, but I think there is some additional processing necessary for this to work.