Skip to content

Commit

Permalink
Update jupyter notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
vhertel committed Jul 4, 2021
1 parent 5ddece1 commit 5e93ee9
Showing 1 changed file with 79 additions and 63 deletions.
142 changes: 79 additions & 63 deletions lisflood-nb-dev.ipynb → lisflood-notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"\n",
"%matplotlib ipympl\n",
"\n",
"#import mpl_interactions.ipyplot as iplt\n",
"\n",
"import datetime\n",
"import xml.etree.ElementTree as ET\n",
"\n",
Expand All @@ -62,7 +60,6 @@
"import ipywidgets\n",
"import pandas as pd\n",
"from ipyfilechooser import FileChooser\n",
"#from carto import scale_bar\n",
"from matplotlib import rc\n",
"\n",
"rc('animation', html='jshtml')\n",
Expand All @@ -72,7 +69,8 @@
"import os\n",
"import netCDF4\n",
"import cartopy.io.img_tiles as cimgt\n",
"import functools # higher-order functions and operations"
"import functools\n",
"import warnings"
]
},
{
Expand All @@ -89,11 +87,10 @@
"id": "available-hamburg",
"metadata": {
"code_folding": [
0,
7,
215,
282,
290
219,
288,
296
],
"scrolled": false
},
Expand All @@ -111,6 +108,9 @@
" if fc[0].selected != None and fc[1].selected != None:\n",
"\n",
" global tree\n",
" global CalendarDayStart\n",
" global DtSec_xml\n",
" global DtSec_box\n",
" global StepStart\n",
" global StepStart_picker\n",
" global StepEnd\n",
Expand All @@ -120,33 +120,39 @@
" global optional_modules_xml\n",
" global module_checkboxes\n",
" global m\n",
" global CalendarDayStart\n",
" global DtSec_xml\n",
" global DtSec_slider\n",
" global marker\n",
" global coordinates\n",
"\n",
" # opens settings file of PRE-RUN ([0]) and RUN ([1]) in list\n",
" tree = [ET.parse(fc[0].selected), ET.parse(fc[1].selected)]\n",
" # gets root of XML\n",
" root = [tree[0].getroot(), tree[1].getroot()]\n",
"\n",
" # gets timestep and calendar day start from XML files\n",
" DtSec_xml = [root[0].find(\"./lfuser/group/textvar[@name='DtSec']\"),\n",
" root[1].find(\"./lfuser/group/textvar[@name='DtSec']\")]\n",
" # creates text box for timestep\n",
" DtSec_box = ipywidgets.BoundedIntText(value=DtSec_xml[0].attrib['value'],\n",
" min=1,\n",
" max=31536000,\n",
" step=60,\n",
" description='Timestep [s]:',\n",
" layout=ipywidgets.Layout(width='40%'),\n",
" style={'description_width': '25ex'})\n",
" date_str = root[1].find(\"./lfuser/group/textvar[@name='CalendarDayStart']\").attrib['value'].split(' ')[0]\n",
" time_str = root[1].find(\"./lfuser/group/textvar[@name='CalendarDayStart']\").attrib['value'].split(' ')[1]\n",
" CalendarDayStart = datetime.datetime(year=int(date_str.split('/')[2]),\n",
" month=int(date_str.split('/')[1]),\n",
" day=int(date_str.split('/')[0]),\n",
" hour=int(time_str.split(':')[0]),\n",
" minute=int(time_str.split(':')[1]))\n",
"\n",
" # finds simulation start date in XML files\n",
" StepStart = [root[0].find(\"./lfuser/group/textvar[@name='StepStart']\"),\n",
" root[1].find(\"./lfuser/group/textvar[@name='StepStart']\")]\n",
" # converts date format of StepStart\n",
" StepStart_iso = [\n",
" datetime.datetime.strptime(StepStart[0].attrib['value'].split()[0], \"%d/%m/%Y\").strftime(\"%Y-%m-%d\"),\n",
" datetime.datetime.strptime(StepStart[1].attrib['value'].split()[0], \"%d/%m/%Y\").strftime(\"%Y-%m-%d\")]\n",
"\n",
" # creates date picker widgets with date set to XML input\n",
" StepStart_picker = [ipywidgets.DatePicker(description='Pre Start Date',\n",
" value=datetime.date.fromisoformat(StepStart_iso[0]),\n",
Expand All @@ -172,13 +178,7 @@
" value=datetime.date.fromisoformat(StepEnd_iso[1]),\n",
" layout=ipywidgets.Layout(width='40%'),\n",
" style={'description_width': '25ex'})]\n",
" DtSec_slider = ipywidgets.BoundedIntText(value=DtSec_xml[0].attrib['value'],\n",
" min=1,\n",
" max=31536000,\n",
" step=60,\n",
" description='Timestep [s]:',\n",
" layout=ipywidgets.Layout(width='40%'),\n",
" style={'description_width': '25ex'})\n",
"\n",
"\n",
" # creates empty list for slider widgets\n",
" calibration_sliders = {}\n",
Expand Down Expand Up @@ -217,8 +217,9 @@
" style={'description_width': '0ex'})\n",
"\n",
" # reads coordinates from XML RUN file\n",
" coordinates = root[1].findall(\"./lfuser/group/textvar/[@name='Gauges']\")[0].attrib['value']\n",
" lon, lat = coordinates.split()\n",
" coordinates = [root[0].findall(\"./lfuser/group/textvar/[@name='Gauges']\")[0],\n",
" root[1].findall(\"./lfuser/group/textvar/[@name='Gauges']\")[0]]\n",
" lon, lat = coordinates[1].attrib['value'].split()\n",
" center = (lat, lon)\n",
" # creates interactive map with draggable marker\n",
" m = ipyleaflet.Map(zoom=10, center=center, scroll_wheel_zoom=True)\n",
Expand Down Expand Up @@ -267,7 +268,7 @@
" tabs.children = [ipywidgets.VBox([output_grid, m]),\n",
" ipywidgets.VBox(\n",
" [StepStart_picker[0], StepEnd_picker[0], StepStart_picker[1], StepEnd_picker[1],\n",
" DtSec_slider]),\n",
" DtSec_box]),\n",
" ipywidgets.VBox(list(calibration_sliders.values())),\n",
" ipywidgets.VBox([ipywidgets.GridBox([module_checkboxes['InitLisflood'],\n",
" module_checkboxes['InitLisfloodwithoutSplit'],\n",
Expand Down Expand Up @@ -313,8 +314,8 @@
" display(processingButton)\n",
"\n",
"\n",
"#\n",
"def on_processingButton_clicked(b):\n",
"# callback function to write input data to XML files and start processing of PRE-RUN and RUN\n",
"def on_processingButton_clicked(b): \n",
" # checks whether variable 'datasets' with reference to NetCDF files exists\n",
" # if yes, closes all NetCDF files to re-write them\n",
" # if no, creates empty list 'datasets'\n",
Expand Down Expand Up @@ -343,6 +344,7 @@
" #print('\\nVorher ', element.attrib['choice'], '\\nNachher', str(int(module_checkboxes[element.attrib['name']].value)), element.attrib)\n",
" element.attrib['choice'] = str(int(module_checkboxes[element.attrib['name']].value))\n",
"\n",
" # sets allowed combinations for SplitRouting\n",
" if module_checkboxes['SplitRouting'].value:\n",
" optional_modules_xml[0].findall(\"setoption[@name='SplitRouting']\")[0].attrib['choice'] = '1'\n",
" optional_modules_xml[0].findall(\"setoption[@name='InitLisflood']\")[0].attrib['choice'] = '1'\n",
Expand All @@ -358,24 +360,25 @@
" optional_modules_xml[1].findall(\"setoption[@name='InitLisflood']\")[0].attrib['choice'] = '0'\n",
" optional_modules_xml[1].findall(\"setoption[@name='InitLisfloodwithoutSplit']\")[0].attrib['choice'] = '0'\n",
"\n",
" # writes simulation start and stop date from user input section to both settings files\n",
" # writes simulation start and stop date, timestep and coordinates from user input section to both settings files\n",
" for i in range(len(tree)):\n",
" StepStart[i].attrib['value'] = '%s %s' % (\n",
" str(datetime.datetime.strptime(str(StepStart_picker[i].value), \"%Y-%m-%d\").strftime(\"%d/%m/%Y\")),\n",
" StepStart[i].attrib['value'].split()[1])\n",
" StepEnd[i].attrib['value'] = '%s %s' % (\n",
" str(datetime.datetime.strptime(str(StepEnd_picker[i].value), \"%Y-%m-%d\").strftime(\"%d/%m/%Y\")),\n",
" StepEnd[i].attrib['value'].split()[1])\n",
" DtSec_xml[i].attrib['value'] = str(DtSec_slider.value)\n",
" DtSec_xml[i].attrib['value'] = str(DtSec_box.value)\n",
" if module_checkboxes['repDischargeTs'].value == True: \n",
" coordinates[i].attrib['value'] = '{} {}'.format(marker.location[1], marker.location[0])\n",
" tree[i].write(fc[i].selected)\n",
"\n",
" # starts LISFLOOD PRE-RUN and RUN according to (new) input\n",
" path1 = fc[0].selected\n",
" path2 = fc[1].selected\n",
"\n",
" print('PRE-RUN\\n')\n",
" print('PRE-RUN...\\n')\n",
" !lisflood $path1\n",
" print('\\n\\n\\n\\nRUN\\n')\n",
" print('\\n\\n\\n\\nRUN...\\n')\n",
" !lisflood $path2\n",
" print(\"\\nDone\")\n",
"\n",
Expand All @@ -388,7 +391,7 @@
" m.layout.display = \"none\"\n",
"\n",
"\n",
"#\n",
"# callback function to prevent false input regarding SplitRouting\n",
"def on_SplitRouting_clicked(button):\n",
" if button['new']:\n",
" module_checkboxes['InitLisflood'].disabled = False\n",
Expand All @@ -399,9 +402,9 @@
" module_checkboxes['InitLisflood'].disabled = True\n",
" module_checkboxes['InitLisfloodwithoutSplit'].disabled = False\n",
"\n",
" ####################################################\n",
"\n",
"\n",
"####################################################\n",
"####################### CODE #######################\n",
"####################################################\n",
"\n",
Expand All @@ -417,7 +420,6 @@
" \"CalChanMan\": [0.1, 20, 0.01, '.2f'],\n",
" \"SnowMeltCoef\": [2.5, 6.5, 0.01, '.2f'],\n",
" \"CalChanMan2\": [0.1, 20, 0.01, '.2f'],\n",
" #\"LZThreshold\": [0, 30, 0.01, '.2f'],\n",
"}\n",
"\n",
"# creates list of file choosers for PRE-RUN and RUN settings file\n",
Expand Down Expand Up @@ -519,6 +521,7 @@
" global parameter_dropdown\n",
" global mm\n",
" global cbar\n",
" global datevar\n",
"\n",
" # list of paths to all spatial output files\n",
" paths_datasets = glob.glob('{}/*.nc'.format(path))\n",
Expand All @@ -539,9 +542,10 @@
" t_cal = datasets[0][1].variables['time'].calendar\n",
" except AttributeError:\n",
" t_cal = u\"gregorian\"\n",
" global datevar\n",
" datevar = []\n",
" datevar.append(netCDF4.num2date(datasets[0][1].variables['time'][:], # get values\n",
" with warnings.catch_warnings():\n",
" warnings.filterwarnings(\"ignore\")\n",
" datevar.append(netCDF4.num2date(datasets[0][1].variables['time'][:], # get values\n",
" units=datasets[0][1].variables['time'].units, # get unit\n",
" calendar=t_cal))\n",
"\n",
Expand All @@ -561,33 +565,39 @@
"\n",
" # get chosen parameter\n",
" parameter = parameter_dropdown.value\n",
"\n",
" # create figure with open street map\n",
" request = cimgt.OSM()\n",
" fig, ax = plt.subplots(subplot_kw=dict(projection=request.crs))\n",
" ax.add_image(request, 8)\n",
" cmap = plt.cm.cool\n",
" vmin = np.min(subfinder(datasets, parameter)[parameter][:])\n",
" vmax = np.percentile(subfinder(datasets, parameter)[parameter][:], 97.5)\n",
" mm = ax.pcolormesh(subfinder(datasets, parameter)['lon'][:],\n",
" subfinder(datasets, parameter)['lat'][:],\n",
" subfinder(datasets, parameter)[parameter][:][0],\n",
" vmin=vmin,\n",
" vmax=vmax,\n",
" transform=ccrs.PlateCarree(),\n",
" cmap=cmap,\n",
" alpha=0.5)\n",
" cbar = plt.colorbar(mm, shrink=0.7)\n",
" cbar.ax.tick_params(labelsize=17)\n",
"\n",
" # create figure\n",
" out_spatial = ipywidgets.Output()\n",
" with out_spatial:\n",
" # create figure with open street map\n",
" request = cimgt.OSM()\n",
" fig, ax = plt.subplots(figsize=(8,4), subplot_kw=dict(projection=request.crs))\n",
" ax.add_image(request, 8)\n",
" cmap = plt.cm.cool\n",
" vmin = np.min(subfinder(datasets, parameter)[parameter][:])\n",
" vmax = np.percentile(subfinder(datasets, parameter)[parameter][:], 97.5)\n",
" mm = ax.pcolormesh(subfinder(datasets, parameter)['lon'][:],\n",
" subfinder(datasets, parameter)['lat'][:],\n",
" subfinder(datasets, parameter)[parameter][:][0],\n",
" vmin=vmin,\n",
" vmax=vmax,\n",
" transform=ccrs.PlateCarree(),\n",
" cmap=cmap,\n",
" alpha=0.5)\n",
" cbar = plt.colorbar(mm, shrink=0.7)\n",
" cbar.ax.tick_params(labelsize=17)\n",
" \n",
" # update plot when user changes parameter in dropdown menu\n",
" ipywidgets.interact(update_parameter, parameter=parameter_dropdown);\n",
" ipywidgets.interactive(update_parameter, parameter=parameter_dropdown)\n",
" # update plot when user changes date slider\n",
" ipywidgets.interact(update_time, date=date_slider);\n",
" ipywidgets.interactive(update_time, date=date_slider)\n",
" # update plot when user starts simulation\n",
" ipywidgets.jslink((play, 'value'), (date_slider, 'value'))\n",
"\n",
" display(play)\n",
" # display outputs\n",
" display(ipywidgets.HTML(value = f\"<center><b><font size=5>{'Spatial Outputs'}</b></center>\"))\n",
" display(out_spatial)\n",
" display(ipywidgets.HBox([play, date_slider, parameter_dropdown]))\n",
" print('\\n\\n\\n\\n\\n\\n\\n\\n')\n",
" \n",
" # checks for 'dis_run.tss' file\n",
" if os.path.isfile('{}/dis_run.tss'.format(path)):\n",
Expand All @@ -609,14 +619,20 @@
" print('No reference data on CalendarDayStart to format x-axis of time series plot.')\n",
" # plots data\n",
" selection = alt.selection_multi(fields=['setting'], bind='legend')\n",
" chart = alt.Chart(df).mark_line(point=True).encode(\n",
" x='date:T',\n",
" y='value:Q',\n",
" color=alt.Color('setting', legend=alt.Legend(title=\"Setting\")),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.2))\n",
" ).add_selection(selection).interactive(bind_y=False)\n",
" chart = alt.Chart(df\n",
" ).mark_line(point=True\n",
" ).encode(x='date:T',\n",
" y='value:Q',\n",
" color=alt.Color('setting', legend=alt.Legend(title=\"Setting\")),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.2))\n",
" ).add_selection(selection\n",
" ).interactive(bind_y=False\n",
" ).properties(width=800, height=300)\n",
" # display outputs\n",
" display(ipywidgets.HTML(value = f\"<center><b><font size=5>{'Discharge Time Series'}</b></center>\"))\n",
" display(chart)\n",
"\n",
" \n",
"\n",
"####################################################\n",
"####################### CODE #######################\n",
Expand Down

0 comments on commit 5e93ee9

Please sign in to comment.