{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4609a62e-d56b-4b3b-8e20-8bb59c8b27cd",
   "metadata": {},
   "source": [
    "# Example for unmixing emission plumes\n",
    "The notebook demonstrates how the `ddeq.dplume.unmix_plumes` function can be used to unmix overlapping plumes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f1a1dbb-4e2c-43aa-817e-e2b68e2b1363",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import cartopy.crs as ccrs\n",
    "import numpy as np\n",
    "import pandas as pandas\n",
    "import xarray\n",
    "\n",
    "# import and setup matplotlib\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['figure.dpi'] = 100\n",
    "\n",
    "import ucat\n",
    "import ddeq\n",
    "\n",
    "CRS = ccrs.UTM(32)\n",
    "WGS84 = ccrs.PlateCarree()\n",
    "\n",
    "DOMAIN = ddeq.misc.Domain.from_nml(os.path.join(os.path.dirname(ddeq.__file__),\n",
    "                                                'data', 'INPUT_ORG'))\n",
    "\n",
    "# set path to SMARTCARB dataset (modify to your file system)\n",
    "ROOT = '/project/coco2/fileshare/WP4/SMARTCARB/'\n",
    "EMISSIONS_DATA_PATH = os.path.join(ROOT, 'timeseries')\n",
    "SMARTCARB_DATA_PATH = os.path.join(ROOT, 'level2')\n",
    "SMARTCARB_MODEL_PATH = os.path.join(ROOT, 'COSMO')\n",
    "SMARTCARB_WIND_PATH = os.path.join(ROOT, 'wind_fields')\n",
    "\n",
    "# list of point sources\n",
    "sources = ddeq.misc.read_point_sources()\n",
    "sources"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0133d6f-a901-4d2c-acc2-17104be626ca",
   "metadata": {},
   "source": [
    "Read SMARTCARB satellite and wind data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "857a789f-5e8a-4886-be7e-6784f13ba040",
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = os.path.join(SMARTCARB_DATA_PATH,\n",
    "                        'Sentinel_7_CO2_2015110211_o4548_l1811.nc')\n",
    "\n",
    "data = ddeq.smartcarb.read_level2(filename, co2_noise_scenario='medium',\n",
    "                                  no2_noise_scenario='high',\n",
    "                                  co_noise_scenario='low',\n",
    "                                  only_observations=False)\n",
    "\n",
    "winds = ddeq.wind.read(pandas.Timestamp(data.time.to_pandas()), sources,\n",
    "                       product='SMARTCARB', data_path=SMARTCARB_WIND_PATH)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a05bf16-1354-4b18-a273-68b534e89c07",
   "metadata": {},
   "source": [
    "Run plume detection using settings that result in overlapping plumes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e36b0ae-4be5-4dbb-9a79-a7d8e85adc33",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = ddeq.dplume.detect_plumes(data,\n",
    "                                 sources.sel(source=['Berlin',\n",
    "                                                     'Janschwalde',\n",
    "                                                     'Schwarze Pumpe',\n",
    "                                                     'Boxberg']),\n",
    "                                 variable='NO2', variable_std='NO2_std',\n",
    "                                 filter_type='gaussian', filter_size=0.5, q=0.9\n",
    "                                )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1f9db63-30ce-460e-b907-8f7210754885",
   "metadata": {},
   "source": [
    "Visualize results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d38e00d7-7b6c-4b44-bf35-582150937224",
   "metadata": {},
   "outputs": [],
   "source": [
    "ddeq.vis.visualize(data, 'NO2', gas='NO2', domain=DOMAIN, winds=winds,\n",
    "                   do_zoom=True, show_clouds=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "295127d0-1d8a-4c7a-9424-d00552f39a7e",
   "metadata": {},
   "source": [
    "Unmix plumes and visualize result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c6bdcfd-d19b-48f4-a939-dc9c563f2544",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = ddeq.dplume.unmix_plumes(data, winds)\n",
    "ddeq.vis.visualize(data, 'NO2', gas='NO2', domain=DOMAIN, winds=winds,\n",
    "                   do_zoom=True, show_clouds=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3d1137c-44a4-41e8-b4f3-0e70892aa12d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
