# pip install mikeio xarray netcdf4
DFS2 - Export to NetCDF
NetCDF
import mikeio
import xarray as xr
import numpy as np
= mikeio.read("../tests/testdata/gebco_sound.dfs2") ds
; ds.Elevation.plot()
The simplest approach is to use the Dataset.to_xarray()
or DataArray.to_xarray()
method, if no custom information is neeeded.
= ds.to_xarray()
xr_ds xr_ds
<xarray.Dataset> Dimensions: (time: 1, y: 264, x: 216) Coordinates: * time (time) datetime64[ns] 2020-05-15T11:04:52 * y (y) float64 55.2 55.21 55.21 55.21 ... 56.29 56.29 56.29 56.3 * x (x) float64 12.2 12.21 12.21 12.21 ... 13.09 13.09 13.09 13.1 Data variables: Elevation (time, y, x) float32 -8.0 -8.0 -8.0 -8.0 -9.0 ... nan nan nan nan
= ds.Elevation.to_xarray()
xr_da xr_da
<xarray.DataArray 'Elevation' (time: 1, y: 264, x: 216)> array([[[ -8., -8., -8., ..., -37., -38., -38.], [ -7., -7., -7., ..., -38., -38., -38.], [ -5., -6., -6., ..., -37., -38., -38.], ..., [-30., -30., -30., ..., nan, nan, nan], [-30., -30., -30., ..., nan, nan, nan], [-31., -30., -31., ..., nan, nan, nan]]], dtype=float32) Coordinates: * time (time) datetime64[ns] 2020-05-15T11:04:52 * y (y) float64 55.2 55.21 55.21 55.21 55.22 ... 56.29 56.29 56.29 56.3 * x (x) float64 12.2 12.21 12.21 12.21 12.22 ... 13.09 13.09 13.09 13.1 Attributes: name: Elevation units: meter eumType: EUMType.Total_Water_Depth eumUnit: EUMUnit.meter
Save it as a NetCDF file:
"gebco_std.nc") xr_da.to_netcdf(
Customized NetCDF
- Time-invariant file -> remove time dimension
- Rename y, x to lat, lon
- Lowercase names
- Add other metadata
= ds.geometry.x
x = ds.geometry.y y
= {}
res
= ["lat", "lon"]
spdims
if len(ds.time) > 1:
= ["t"] + spdims
dims = {"t": ds.time}
coords else:
= spdims
dims = {}
coords
"lon"] = xr.DataArray(x, dims="lon", attrs={"standard_name" : "longitude", "units" : "degrees_east"})
coords["lat"] = xr.DataArray(y, dims="lat", attrs={"standard_name" : "latitude", "units" : "degrees_north"})
coords[
for da in ds:
= da.name.lower()
name = xr.DataArray(np.squeeze(da.to_numpy()), dims=dims,
res[name] ={'name': name,
attrs# TODO add standard name from https://cfconventions.org/standard-names.html
'units': da.unit.name,
'eumType' : da.type,
'eumUnit' : da.unit})
= xr.Dataset(res, coords=coords, attrs={'title': 'Converted from Dfs2 by MIKE IO'}) xr_ds
xr_ds
<xarray.Dataset> Dimensions: (lon: 216, lat: 264) Coordinates: * lon (lon) float64 12.2 12.21 12.21 12.21 ... 13.09 13.09 13.09 13.1 * lat (lat) float64 55.2 55.21 55.21 55.21 ... 56.29 56.29 56.29 56.3 Data variables: elevation (lat, lon) float32 -8.0 -8.0 -8.0 -8.0 -9.0 ... nan nan nan nan Attributes: title: Converted from Dfs2 by MIKE IO
; xr_ds.elevation.plot()
"gebco.nc") xr_ds.to_netcdf(
Clean up
import os
"gebco_std.nc")
os.remove("gebco.nc") os.remove(