{ "cells": [ { "cell_type": "markdown", "source": [ "# Global Forecasting System - Meteorological forecast" ], "metadata": {} }, { "cell_type": "code", "execution_count": 1, "source": [ "from datetime import datetime, timedelta\n", "import xarray\n", "import numpy as np\n", "import pandas as pd\n", "from mikeio import Dfs2" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Let's try to download yesterday's forecast from the OpenDAP server." ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "now = datetime.now()\n", "forecast = datetime(now.year,now.month,now.day) - timedelta(days=1)\n", "dtstr = forecast.strftime(\"%Y%m%d\")\n", "hour = \"12\" # valid options are 00,06,12,18\n", "url = f\"https://nomads.ncep.noaa.gov/dods/gfs_0p25/gfs{dtstr}/gfs_0p25_{hour}z\"" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "# code used to generate the example file '../tests/testdata/gfs_wind.nc'\n", "\n", "# ds = xarray.open_dataset(url) # use this instead if you want new data\n", "# ds = ds.sel(lon=slice(10,15), lat=slice(30,40)).isel(time=slice(0,3)) # small subset for illustration\n", "# ds = ds[['msletmsl','ugrd10m','vgrd10m']].load()\n", "# ds.to_netcdf(\"../tests/testdata/gfs_wind.nc\") # save to disk as netcdf" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "fn = '../tests/testdata/gfs_wind.nc'\n", "ds = xarray.open_dataset(fn)\n", "#ds = xarray.open_dataset(url) # use this instead if you want new dataa" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "ds.time.values[0],ds.time.values[-1]" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(numpy.datetime64('2021-09-02T12:00:00.000000000'),\n", " numpy.datetime64('2021-09-02T18:00:00.000000000'))" ] }, "metadata": {}, "execution_count": 5 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 6, "source": [ "(ds.time.values[-1]- ds.time.values[0]).astype('timedelta64[D]')" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "numpy.timedelta64(0,'D')" ] }, "metadata": {}, "execution_count": 6 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "The forecast contains data for the coming 10 days." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Running a Mike 21 HD model, needs at least three variables of meteorlogical forcing\n", "* Mean Sea Level Pressure\n", "* U 10m\n", "* V 10m" ], "metadata": {} }, { "cell_type": "code", "execution_count": 7, "source": [ "ds.msletmsl" ], "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
<xarray.DataArray 'msletmsl' (time: 3, lat: 41, lon: 21)>\n", "array([[[101116.35 , 101134.555, ..., 101474.15 , 101485.35 ],\n", " [101122.95 , 101141.75 , ..., 101498.35 , 101509.15 ],\n", " ...,\n", " [101972.555, 102048.35 , ..., 101975.15 , 101966.15 ],\n", " [102029.555, 102074.95 , ..., 101970.75 , 101953.15 ]],\n", "\n", " [[100923.02 , 100932.43 , ..., 101305.43 , 101316.43 ],\n", " [100940.625, 100945.625, ..., 101329.83 , 101343.625],\n", " ...,\n", " [101875.43 , 101861.83 , ..., 101870.83 , 101875.83 ],\n", " [101912.43 , 101898.02 , ..., 101860.83 , 101876.83 ]],\n", "\n", " [[101001.36 , 101014.555, ..., 101453.76 , 101465.555],\n", " [101002.96 , 101017.96 , ..., 101513.36 , 101522.76 ],\n", " ...,\n", " [101853.36 , 101859.16 , ..., 101855.555, 101863.555],\n", " [101870.36 , 101869.96 , ..., 101866.36 , 101870.96 ]]], dtype=float32)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-09-02T12:00:00 ... 2021-09-02T18:00:00\n", " * lat (lat) float64 30.0 30.25 30.5 30.75 31.0 ... 39.25 39.5 39.75 40.0\n", " * lon (lon) float64 10.0 10.25 10.5 10.75 11.0 ... 14.25 14.5 14.75 15.0\n", "Attributes:\n", " long_name: ** mean sea level mslp (eta model reduction) [pa]
array([[[101116.35 , 101134.555, ..., 101474.15 , 101485.35 ],\n", " [101122.95 , 101141.75 , ..., 101498.35 , 101509.15 ],\n", " ...,\n", " [101972.555, 102048.35 , ..., 101975.15 , 101966.15 ],\n", " [102029.555, 102074.95 , ..., 101970.75 , 101953.15 ]],\n", "\n", " [[100923.02 , 100932.43 , ..., 101305.43 , 101316.43 ],\n", " [100940.625, 100945.625, ..., 101329.83 , 101343.625],\n", " ...,\n", " [101875.43 , 101861.83 , ..., 101870.83 , 101875.83 ],\n", " [101912.43 , 101898.02 , ..., 101860.83 , 101876.83 ]],\n", "\n", " [[101001.36 , 101014.555, ..., 101453.76 , 101465.555],\n", " [101002.96 , 101017.96 , ..., 101513.36 , 101522.76 ],\n", " ...,\n", " [101853.36 , 101859.16 , ..., 101855.555, 101863.555],\n", " [101870.36 , 101869.96 , ..., 101866.36 , 101870.96 ]]], dtype=float32)
array(['2021-09-02T12:00:00.000000000', '2021-09-02T15:00:00.000000000',\n", " '2021-09-02T18:00:00.000000000'], dtype='datetime64[ns]')
array([30. , 30.25, 30.5 , 30.75, 31. , 31.25, 31.5 , 31.75, 32. , 32.25,\n", " 32.5 , 32.75, 33. , 33.25, 33.5 , 33.75, 34. , 34.25, 34.5 , 34.75,\n", " 35. , 35.25, 35.5 , 35.75, 36. , 36.25, 36.5 , 36.75, 37. , 37.25,\n", " 37.5 , 37.75, 38. , 38.25, 38.5 , 38.75, 39. , 39.25, 39.5 , 39.75,\n", " 40. ])
array([10. , 10.25, 10.5 , 10.75, 11. , 11.25, 11.5 , 11.75, 12. , 12.25,\n", " 12.5 , 12.75, 13. , 13.25, 13.5 , 13.75, 14. , 14.25, 14.5 , 14.75,\n", " 15. ])
<xarray.DataArray 'ugrd10m' (time: 3, lat: 41, lon: 21)>\n", "array([[[ 1.021836, 1.151836, ..., -4.318164, -4.228164],\n", " [ 1.731836, 1.361836, ..., -4.408164, -4.138164],\n", " ...,\n", " [-2.328164, -0.578164, ..., 0.111836, 1.381836],\n", " [-1.368164, -0.248164, ..., 2.521836, 3.361836]],\n", "\n", " [[-1.186663, -0.896663, ..., -4.376663, -4.216662],\n", " [-0.516663, -0.106663, ..., -4.476663, -4.386662],\n", " ...,\n", " [-2.176662, -3.376662, ..., 2.673337, 3.193337],\n", " [-1.596663, -2.376662, ..., 2.923337, 3.493337]],\n", "\n", " [[-3.289988, -3.199988, ..., -5.399988, -5.479988],\n", " [-3.449988, -3.719988, ..., -7.419988, -6.809988],\n", " ...,\n", " [-2.629988, -3.189988, ..., 2.280012, 2.160012],\n", " [-1.719988, -2.939988, ..., 2.620012, 2.800012]]], dtype=float32)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-09-02T12:00:00 ... 2021-09-02T18:00:00\n", " * lat (lat) float64 30.0 30.25 30.5 30.75 31.0 ... 39.25 39.5 39.75 40.0\n", " * lon (lon) float64 10.0 10.25 10.5 10.75 11.0 ... 14.25 14.5 14.75 15.0\n", "Attributes:\n", " long_name: ** 10 m above ground u-component of wind [m/s]
array([[[ 1.021836, 1.151836, ..., -4.318164, -4.228164],\n", " [ 1.731836, 1.361836, ..., -4.408164, -4.138164],\n", " ...,\n", " [-2.328164, -0.578164, ..., 0.111836, 1.381836],\n", " [-1.368164, -0.248164, ..., 2.521836, 3.361836]],\n", "\n", " [[-1.186663, -0.896663, ..., -4.376663, -4.216662],\n", " [-0.516663, -0.106663, ..., -4.476663, -4.386662],\n", " ...,\n", " [-2.176662, -3.376662, ..., 2.673337, 3.193337],\n", " [-1.596663, -2.376662, ..., 2.923337, 3.493337]],\n", "\n", " [[-3.289988, -3.199988, ..., -5.399988, -5.479988],\n", " [-3.449988, -3.719988, ..., -7.419988, -6.809988],\n", " ...,\n", " [-2.629988, -3.189988, ..., 2.280012, 2.160012],\n", " [-1.719988, -2.939988, ..., 2.620012, 2.800012]]], dtype=float32)
array(['2021-09-02T12:00:00.000000000', '2021-09-02T15:00:00.000000000',\n", " '2021-09-02T18:00:00.000000000'], dtype='datetime64[ns]')
array([30. , 30.25, 30.5 , 30.75, 31. , 31.25, 31.5 , 31.75, 32. , 32.25,\n", " 32.5 , 32.75, 33. , 33.25, 33.5 , 33.75, 34. , 34.25, 34.5 , 34.75,\n", " 35. , 35.25, 35.5 , 35.75, 36. , 36.25, 36.5 , 36.75, 37. , 37.25,\n", " 37.5 , 37.75, 38. , 38.25, 38.5 , 38.75, 39. , 39.25, 39.5 , 39.75,\n", " 40. ])
array([10. , 10.25, 10.5 , 10.75, 11. , 11.25, 11.5 , 11.75, 12. , 12.25,\n", " 12.5 , 12.75, 13. , 13.25, 13.5 , 13.75, 14. , 14.25, 14.5 , 14.75,\n", " 15. ])
<xarray.DataArray 'vgrd10m' (time: 3, lat: 41, lon: 21)>\n", "array([[[-0.474941, -0.634941, ..., -1.244941, -1.254941],\n", " [-1.124941, -0.504941, ..., -1.014941, -1.224941],\n", " ...,\n", " [-0.454941, 1.145059, ..., 1.505059, 1.635059],\n", " [ 1.695059, 2.595058, ..., 1.645059, 1.805059]],\n", "\n", " [[-4.673393, -3.993393, ..., -2.993393, -3.323394],\n", " [-4.393394, -4.633393, ..., -2.723393, -3.213393],\n", " ...,\n", " [-0.593394, 0.456606, ..., 0.616606, -0.323394],\n", " [ 0.046606, 1.156606, ..., 0.066606, -1.213394]],\n", "\n", " [[-2.777568, -3.487568, ..., -4.187568, -4.807568],\n", " [-1.427568, -1.767568, ..., -5.827568, -6.177568],\n", " ...,\n", " [-1.707568, -1.177568, ..., -2.307568, -2.357568],\n", " [-2.577568, -1.327568, ..., -2.947568, -3.897568]]], dtype=float32)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-09-02T12:00:00 ... 2021-09-02T18:00:00\n", " * lat (lat) float64 30.0 30.25 30.5 30.75 31.0 ... 39.25 39.5 39.75 40.0\n", " * lon (lon) float64 10.0 10.25 10.5 10.75 11.0 ... 14.25 14.5 14.75 15.0\n", "Attributes:\n", " long_name: ** 10 m above ground v-component of wind [m/s]
array([[[-0.474941, -0.634941, ..., -1.244941, -1.254941],\n", " [-1.124941, -0.504941, ..., -1.014941, -1.224941],\n", " ...,\n", " [-0.454941, 1.145059, ..., 1.505059, 1.635059],\n", " [ 1.695059, 2.595058, ..., 1.645059, 1.805059]],\n", "\n", " [[-4.673393, -3.993393, ..., -2.993393, -3.323394],\n", " [-4.393394, -4.633393, ..., -2.723393, -3.213393],\n", " ...,\n", " [-0.593394, 0.456606, ..., 0.616606, -0.323394],\n", " [ 0.046606, 1.156606, ..., 0.066606, -1.213394]],\n", "\n", " [[-2.777568, -3.487568, ..., -4.187568, -4.807568],\n", " [-1.427568, -1.767568, ..., -5.827568, -6.177568],\n", " ...,\n", " [-1.707568, -1.177568, ..., -2.307568, -2.357568],\n", " [-2.577568, -1.327568, ..., -2.947568, -3.897568]]], dtype=float32)
array(['2021-09-02T12:00:00.000000000', '2021-09-02T15:00:00.000000000',\n", " '2021-09-02T18:00:00.000000000'], dtype='datetime64[ns]')
array([30. , 30.25, 30.5 , 30.75, 31. , 31.25, 31.5 , 31.75, 32. , 32.25,\n", " 32.5 , 32.75, 33. , 33.25, 33.5 , 33.75, 34. , 34.25, 34.5 , 34.75,\n", " 35. , 35.25, 35.5 , 35.75, 36. , 36.25, 36.5 , 36.75, 37. , 37.25,\n", " 37.5 , 37.75, 38. , 38.25, 38.5 , 38.75, 39. , 39.25, 39.5 , 39.75,\n", " 40. ])
array([10. , 10.25, 10.5 , 10.75, 11. , 11.25, 11.5 , 11.75, 12. , 12.25,\n", " 12.5 , 12.75, 13. , 13.25, 13.5 , 13.75, 14. , 14.25, 14.5 , 14.75,\n", " 15. ])