{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python3", "display_name": "Python 3.7.7 64-bit (conda)", "metadata": { "interpreter": { "hash": "925fb081efd8927dd3220b15e883a9858618b50119047b605c2ce4e0c78ada0f" } } } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# Dfsu - Export to netcdf\n", "* Read data from dfsu file\n", "* Convert to xarray dataset\n", "* Write to netcdf file" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "# pip install mikeio xarray netcdf4" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "import xarray as xr\n", "\n", "# remove this line if you like more advanced display of xarray datasets\n", "xr.set_options(display_style=\"text\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from mikeio import Dfsu" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Dfsu3DSigmaZ\n", "Number of elements: 17118\n", "Number of nodes: 12042\n", "Projection: UTM-33\n", "Number of sigma layers: 4\n", "Max number of z layers: 5\n", "Items:\n", " 0: Z coordinate (meter)\n", " 1: Temperature (degree Celsius)\n", " 2: Salinity (PSU)\n", "Time: 3 steps with dt=10800.0s\n", " 1997-09-15 21:00:00 -- 1997-09-16 03:00:00" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "dfsu = Dfsu(\"../tests/testdata/oresund_sigma_z.dfsu\")\n", "dfsu" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "nc = dfsu.node_coordinates\n", "xn = nc[:,0]\n", "yn = nc[:,1]\n", "zn = nc[:,2]\n", "\n", "ec = dfsu.element_coordinates\n", "xe = ec[:,0]\n", "ye = ec[:,1]\n", "ze = ec[:,2]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\n", "Dimensions: (3, 17118)\n", "Time: 1997-09-15 21:00:00 - 1997-09-16 03:00:00\n", "Items:\n", " 0: Z coordinate (meter)\n", " 1: Temperature (degree Celsius)\n", " 2: Salinity (PSU)" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "ds = dfsu.read()\n", "ds" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Time\n", "time = ds.time\n", "\n", "# Node based data\n", "node_ids = dfsu.node_ids\n", "z_dynamic = ds['Z coordinate']\n", "xn_da = xr.DataArray(xn, coords=[node_ids], dims=[\"nodes\"], attrs={'units': 'meter'})\n", "yn_da = xr.DataArray(xn, coords=[node_ids], dims=[\"nodes\"], attrs={'units': 'meter'})\n", "zn_da = xr.DataArray(zn, coords=[node_ids], dims=[\"nodes\"], attrs={'units': 'meter'})\n", "z_dyn_da = xr.DataArray(z_dynamic, coords =[time,node_ids],dims=[\"time\", \"nodes\"], attrs={'units': 'meter'})\n", "\n", "# Element based data\n", "el_ids = dfsu.element_ids\n", "xe_da = xr.DataArray(xe, coords=[el_ids], dims=[\"elements\"], attrs={'units': 'meter'})\n", "ye_da = xr.DataArray(ye, coords=[el_ids], dims=[\"elements\"], attrs={'units': 'meter'})\n", "ze_da = xr.DataArray(ze, coords=[el_ids], dims=[\"elements\"], attrs={'units': 'meter'})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Add coordinates for nodes and elements\n", "data_dict = {'x': xn_da,\n", " 'y' :yn_da,\n", " 'z' : zn_da,\n", " 'xe' : xe_da,\n", " 'ye' : ye_da,\n", " 'ze' : ze_da,\n", " 'z_dynamic' : z_dyn_da}\n", "\n", "# add rest of data\n", "for item in ds.items[1:]: # skip Z coordinate since this is defined on nodes\n", " name = item.name\n", " data = ds[item]\n", " da = xr.DataArray(data, coords =[time,el_ids],dims=[\"time\", \"elements\"], attrs={'units': item.unit.name})\n", "\n", " data_dict[name] = da\n", "\n", "# create an xarray dataset\n", "xr_ds = xr.Dataset(data_dict)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\n", "Dimensions: (elements: 17118, nodes: 12042, time: 3)\n", "Coordinates:\n", " * nodes (nodes) int32 0 1 2 3 4 5 ... 12037 12038 12039 12040 12041\n", " * elements (elements) int32 0 1 2 3 4 5 ... 17113 17114 17115 17116 17117\n", " * time (time) datetime64[ns] 1997-09-15T21:00:00 ... 1997-09-16T03:00:00\n", "Data variables:\n", " x (nodes) float64 3.602e+05 3.602e+05 ... 3.698e+05 3.698e+05\n", " y (nodes) float64 3.602e+05 3.602e+05 ... 3.698e+05 3.698e+05\n", " z (nodes) float64 -0.2711 -0.2033 -0.1355 ... -4.0 -2.0 0.0\n", " xe (elements) float64 3.54e+05 3.54e+05 ... 3.696e+05 3.696e+05\n", " ye (elements) float64 6.199e+06 6.199e+06 ... 6.167e+06 6.167e+06\n", " ze (elements) float64 -10.5 -7.0 -5.0 -3.0 ... -7.0 -5.0 -3.0 -1.0\n", " z_dynamic (time, nodes) float64 -0.2711 -0.1232 0.02472 ... -1.888 0.1495\n", " Temperature (time, elements) float64 16.31 16.43 16.54 ... 16.35 16.07\n", " Salinity (time, elements) float64 23.19 22.63 22.32 ... 21.57 21.53" ], "text/html": "
<xarray.Dataset>\nDimensions:      (elements: 17118, nodes: 12042, time: 3)\nCoordinates:\n  * nodes        (nodes) int32 0 1 2 3 4 5 ... 12037 12038 12039 12040 12041\n  * elements     (elements) int32 0 1 2 3 4 5 ... 17113 17114 17115 17116 17117\n  * time         (time) datetime64[ns] 1997-09-15T21:00:00 ... 1997-09-16T03:00:00\nData variables:\n    x            (nodes) float64 3.602e+05 3.602e+05 ... 3.698e+05 3.698e+05\n    y            (nodes) float64 3.602e+05 3.602e+05 ... 3.698e+05 3.698e+05\n    z            (nodes) float64 -0.2711 -0.2033 -0.1355 ... -4.0 -2.0 0.0\n    xe           (elements) float64 3.54e+05 3.54e+05 ... 3.696e+05 3.696e+05\n    ye           (elements) float64 6.199e+06 6.199e+06 ... 6.167e+06 6.167e+06\n    ze           (elements) float64 -10.5 -7.0 -5.0 -3.0 ... -7.0 -5.0 -3.0 -1.0\n    z_dynamic    (time, nodes) float64 -0.2711 -0.1232 0.02472 ... -1.888 0.1495\n    Temperature  (time, elements) float64 16.31 16.43 16.54 ... 16.35 16.07\n    Salinity     (time, elements) float64 23.19 22.63 22.32 ... 21.57 21.53
" }, "metadata": {}, "execution_count": 9 } ], "source": [ "xr_ds" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "xr_ds.to_netcdf(\"oresund_sigma_z.nc\")" ] }, { "source": [ "# Clean up" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.remove(\"oresund_sigma_z.nc\")" ] } ] }