{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "790dc265-c833-49b2-8ded-46408a64bf6c",
   "metadata": {},
   "source": [
    "# Example: Divergence method"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee75d31-62ae-4b8b-a301-1638a550a4db",
   "metadata": {},
   "source": [
    "A brief example demonstrating the application of the divergence method to estimate CO2 and NO2 emissions of the Boxberg power plant in the SMARTCARB dataset. The example requires both the SMARTCARB Level-2 data and the wind fields that you be downloaded here: https://doi.org/10.5281/zenodo.4048227"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "014c64ef-954a-4676-82ee-99f9f61e7fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fb4e0ad-881d-44eb-895a-940e5b772ad0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import ddeq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "472db229-cc91-4841-a6d2-4c612986f2bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "ROOT = '/project/coco2/fileshare/WP4/SMARTCARB/'\n",
    "SMARTCARB_DATA_PATH = os.path.join(ROOT, 'level2')\n",
    "SMARTCARB_WIND_PATH = os.path.join(ROOT, 'wind_fields')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4173e35b-c0a4-4d24-a9b1-e8c5c77f9779",
   "metadata": {},
   "outputs": [],
   "source": [
    "sources = ddeq.misc.read_point_sources()\n",
    "sources = sources.sel(source=['Boxberg'])\n",
    "\n",
    "l2 = ddeq.smartcarb.Level2Dataset(SMARTCARB_DATA_PATH, 'ace', co2_noise_scenario='low', no2_noise_scenario='low',\n",
    "                                  make_no2_error_cloud_dependent=False)\n",
    "\n",
    "res = ddeq.div.estimate_emissions(\n",
    "    l2, SMARTCARB_WIND_PATH, sources,\n",
    "    wind_product='SMARTCARB', trace_gases=['CO2', 'NO2'],\n",
    "    start_date='2015-02-01', end_date='2015-05-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cc709fd-788a-466d-81da-7ac54e2be613",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ef6273-0d16-455c-b2da-534c7c1a82b3",
   "metadata": {},
   "source": [
    "The estimated emissions of Boxberg are 760.2$\\pm$61.56 kg/s and 0.5766$\\pm$0.06649 kg/s for CO2 and NO2 using low-noise CO2 and NO2 for constellation ace from 1 Feb to 1 May 2015."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbd749d5-6044-4397-9f62-2a54df818841",
   "metadata": {},
   "source": [
    "## Compare with true emissions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f818846b-8779-4ee2-84c9-6b84a251a2f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "times = pd.date_range('2015-02-01', '2015-05-01')\n",
    "co2_true = np.mean([ddeq.smartcarb.read_true_emissions(t, 'CO2', 'Boxberg').mean() for t in times])\n",
    "no2_true = np.mean([ddeq.smartcarb.read_true_emissions(t, 'NO2', 'Boxberg').mean() for t in times])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "436feceb-0ab6-4423-a573-ffa5bbdc3241",
   "metadata": {},
   "outputs": [],
   "source": [
    "co2_est = ddeq.misc.kgs_to_Mtyr(res.sel(source='Boxberg')['CO2_estimated_emissions'])\n",
    "co2_pre = ddeq.misc.kgs_to_Mtyr(res.sel(source='Boxberg')['CO2_estimated_emissions_precision'])\n",
    "\n",
    "no2_est = 1e3 * ddeq.misc.kgs_to_Mtyr(res.sel(source='Boxberg')['NO2_estimated_emissions'])\n",
    "no2_pre = 1e3 * ddeq.misc.kgs_to_Mtyr(res.sel(source='Boxberg')['NO2_estimated_emissions_precision'])\n",
    "\n",
    "print(f'CO2 estimate: {co2_est:.1f}±{co2_pre:.1f} Mt/a, True: {co2_true:.1f} Mt/a')\n",
    "print(f'NO2 estimate: {no2_est:.1f}±{no2_pre:.1f} kt/a, True: {no2_true:.1f} kt/a')"
   ]
  }
 ],
 "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
}
