{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd5c3005-f237-4495-9185-2d4d474cafd5",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Tutorial 2: Forecast after DA\n",
    "\n",
    "\n",
    "**Goal**: To run a cycled data assimilation experiment.\n",
    "[`cycled_exp.py`](https://github.com/lkugler/DART-WRF/blob/master/generate_free.py) contains an example which will be explained here:\n",
    "\n",
    "Now there are two options:\n",
    "1) To start a forecast from an existing forecast, i.e. from WRF restart files\n",
    "2) To start a forecast from defined thermodynamic profiles, i.e. from a `wrf_profile`\n",
    "\n",
    "\n",
    "### Restart a forecast\n",
    "To run a forecast from initial conditions of a previous forecasts, we import these modules\n",
    "```python\n",
    "import datetime as dt\n",
    "from dartwrf.workflows import WorkFlows\n",
    "```\n",
    "\n",
    "Let's say you want to run a forecast starting at 9 UTC until 12 UTC.\n",
    "Initial conditions shall be taken from a previous experiment in `/user/test/data/sim_archive/exp_abc` which was initialized at 6 UTC and there are WRF restart files for 9 UTC.\n",
    "Then the code would be\n",
    "\n",
    "```python\n",
    "prior_path_exp = '/user/test/data/sim_archive/exp_abc'\n",
    "prior_init_time = dt.datetime(2008,7,30,6)\n",
    "prior_valid_time = dt.datetime(2008,7,30,9)\n",
    "\n",
    "w = WorkFlows(exp_config='cfg.py', server_config='srvx1.py')\n",
    "\n",
    "begin = dt.datetime(2008, 7, 30, 9)\n",
    "end = dt.datetime(2008, 7, 30, 12)\n",
    "\n",
    "w.prepare_WRFrundir(begin)\n",
    "\n",
    "w.prepare_IC_from_prior(prior_path_exp, prior_init_time, prior_valid_time)\n",
    "\n",
    "w.run_ENS(begin=begin,  # start integration from here\n",
    "         end=end,      # integrate until here\n",
    "         output_restart_interval=9999,  # do not write WRF restart files\n",
    "         )\n",
    "```\n",
    "Note that we use predefined workflow functions like `run_ENS`.\n",
    "\n",
    "\n",
    "### Forecast run after Data Assimilation\n",
    "In order to continue after assimilation you need the posterior = prior (1) + increments (2)\n",
    "\n",
    "1. Set posterior = prior\n",
    "```python\n",
    "id = w.prepare_IC_from_prior(prior_path_exp, prior_init_time, prior_valid_time, depends_on=id)\n",
    "```\n",
    "\n",
    "2) Update posterior with increments from assimilation\n",
    "After this, the wrfrst files are updated with assimilation increments from DART output and copied to the WRF's run directories so you can continue to run the forecast ensemble.\n",
    "```python\n",
    "id = w.update_IC_from_DA(time, depends_on=id)\n",
    "```\n",
    "\n",
    "3) Define how long you want to run the forecast and when you want WRF restart files. Since they take a lot of space, we want as few as possible.\n",
    "\n",
    "```python\n",
    "timedelta_integrate = dt.timedelta(hours=5)\n",
    "output_restart_interval = 9999  # any value larger than the forecast duration\n",
    "```\n",
    "\n",
    "If you run DA in cycles of 15 minutes, it will be\n",
    "```python\n",
    "timedelta_integrate = dt.timedelta(hours=5)\n",
    "timedelta_btw_assim = dt.timedelta(minutes=15)\n",
    "output_restart_interval = timedelta_btw_assim.total_seconds()/60\n",
    "```\n",
    "\n",
    "\n",
    "3) Run WRF ensemble\n",
    "```python\n",
    "id = w.run_ENS(begin=time,  # start integration from here\n",
    "               end=time + timedelta_integrate,  # integrate until here\n",
    "               output_restart_interval=output_restart_interval,\n",
    "               depends_on=id)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "400244f1-098b-46ea-b29d-2226c7cbc827",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nwp 2023.1 - 3.10",
   "language": "python",
   "name": "nwp2023.1"
  },
  "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}