{ "cells": [ { "cell_type": "markdown", "source": [ "# Aggregator examples\n", "Shows how dfs aggregator functions work" ], "metadata": {} }, { "cell_type": "code", "execution_count": 1, "source": [ "import numpy as np\n", "import mikeio\n", "from mikeio import aggregator\n", "from mikeio import Dfs2, Dfs1, Dfs0" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Input data" ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "dfs2file = \"../tests/testdata/eq.dfs2\"\n", "dfs2 = Dfs2(dfs2file)\n", "ds = dfs2.read()\n", "data2 = ds.data\n", "print(data2[0].shape)\n", "print(np.nanmean(data2[0][:,0,0]))\n", "print(np.nanmean(data2[0][0,:,0]))\n", "print(np.nanmax(data2[0][:,0,0]))\n", "#\n", "s1, s2, s3 = data2[0].shape\n", "vec=np.reshape(data2[0][:,:,0],newshape=(s1*s2,1))\n", "print(np.nanmean(vec))\n", "print(np.nanstd(vec))" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(25, 10, 20)\n", "0.6955797064304352\n", "0.44341139290481807\n", "0.9616825580596924\n", "0.53730055398494\n", "0.20780547246244235\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "s1, s2, s3 = data2[0].shape\n", "vec=np.reshape(data2[0][:,:,0],newshape=(s1*s2,1))" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Example 1\n", "make y-average of dfs2 (keeping x-axis)" ], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "dfs1file1 = \"../tests/testdata/eq_ax_x.dfs1\"\n", "aggregator.dfs2todfs1(dfs2file, dfs1file1)\n", "dfs1 = Dfs1(dfs1file1)\n", "ds1 = dfs1.read()\n", "data1 = ds1.data\n", "print(data1[0].shape)\n", "print(data1[0][0,0])" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(25, 20)\n", "0.4434114098548889\n" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Example 2: other axis\n", "Take average instead over x-axis " ], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "dfs1file2 = \"../tests/testdata/eq_ax_y.dfs1\"\n", "aggregator.dfs2todfs1(dfs2file, dfs1file2, axis=2)\n", "dfs1 = Dfs1(dfs1file2)\n", "ds1 = dfs1.read()\n", "data1 = ds1.data\n", "print(data1[0].shape)\n", "print(data1[0][0,0])" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(25, 10)\n", "0.5089341998100281\n" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Example 3: max\n", "Take max instead over y-axis" ], "metadata": {} }, { "cell_type": "code", "execution_count": 6, "source": [ "dfs1file3 = \"../tests/testdata/eq_ax_x_max.dfs1\"\n", "aggregator.dfs2todfs1(dfs2file, dfs1file3, axis=1, func=np.nanmax)\n", "\n", "dfs1 = Dfs1(dfs1file3)\n", "ds = dfs1.read()\n", "data1 = ds1.data\n", "print(data1[0].shape)\n", "print(data1[0][0,0])" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(25, 10)\n", "0.5089341998100281\n" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Example 4: aggregate to dfs0\n", "Calc mean of each time step" ], "metadata": {} }, { "cell_type": "code", "execution_count": 7, "source": [ "dfs0fileA = \"../tests/testdata/eq_mean.dfs0\"\n", "aggregator.dfstodfs0(dfs2file, dfs0fileA)\n", "dfs0 = Dfs0(dfs0fileA)\n", "ds = dfs0.read()\n", "ds" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\n", "Dimensions: (25,)\n", "Time: 2000-01-01 00:00:00 - 2000-01-02 00:00:00\n", "Items:\n", " 0: Item 1 (undefined)" ] }, "metadata": {}, "execution_count": 7 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Example 5: calc standard deviation pr step" ], "metadata": {} }, { "cell_type": "code", "execution_count": 8, "source": [ "dfs0fileB = \"../tests/testdata/eq_std.dfs0\"\n", "aggregator.dfstodfs0(dfs2file, dfs0fileB, np.nanstd)\n", "dfs0 = Dfs0(dfs0fileB)\n", "ds = dfs0.read()\n", "ds.data[0][0]" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0.2997477352619171" ] }, "metadata": {}, "execution_count": 8 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Clean up" ], "metadata": {} }, { "cell_type": "code", "execution_count": 9, "source": [ "import os\n", "os.remove(dfs1file1)\n", "os.remove(dfs1file2)\n", "os.remove(dfs1file3)\n", "os.remove(dfs0fileA)\n", "os.remove(dfs0fileB)" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [], "outputs": [], "metadata": {} } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.8.10 64-bit ('base': conda)" }, "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.8.10" }, "interpreter": { "hash": "fa576ebcd40e010bdc0ae86b06ce09151f3424f9e9aed6893ff04f39a9299d89" } }, "nbformat": 4, "nbformat_minor": 4 }