{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "da8b7d76-64d5-4afe-a381-d21b23fe4c4c",
   "metadata": {},
   "source": [
    "# Example: CO2M CO2 swath with COSMO-GHG as background\n",
    "This example demonstrates how to create Figure 1 from  Hakkarainen et al. 2022 (https://doi.org/10.3389/frsen.2022.878731)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faea6ae1-2bda-4187-99e2-e74e745283a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cartopy.crs as ccrs\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import ddeq\n",
    "\n",
    "from ddeq.smartcarb import DOMAIN\n",
    "\n",
    "# Path to test dataset incl in ddeq\n",
    "DATA_PATH = os.path.join(os.path.dirname(ddeq.__file__), '..', '..', 'notebooks', 'data')\n",
    "\n",
    "sources = ddeq.misc.read_point_sources()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0aec0c82-4e33-4f0d-b64f-467571883c39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# read synthetic satellite observations\n",
    "filename = os.path.join(DATA_PATH, 'Sentinel_7_CO2_2015042311_o1670_l0483.nc')\n",
    "\n",
    "data_level2 = ddeq.smartcarb.read_level2(filename,\n",
    "                                         co2_noise_scenario='low',\n",
    "                                         no2_noise_scenario='high',\n",
    "                                         co_noise_scenario='low',\n",
    "                                         only_observations=False)\n",
    "\n",
    "# read cosmo fields\n",
    "time = pd.Timestamp(data_level2.time.to_pandas())\n",
    "filename = os.path.join(DATA_PATH, 'cosmo_2d_2015042311.nc')\n",
    "data_cosmo = ddeq.smartcarb.read_cosmo(filename, 'CO2')\n",
    "\n",
    "fig = ddeq.vis.visualize(data_level2, 'CO2', domain=DOMAIN,\n",
    "                         do_zoom=False, show_clouds=True,\n",
    "                         sources=sources, vmin=404, vmax=408,\n",
    "                         names=[\"Berlin\", \"Boxberg\", \"Janschwalde\",\n",
    "                                \"Lippendorf\", \"Schwarze Pumpe\", \"Turow\"],\n",
    "                         names_align='right',\n",
    "                         cmap=\"viridis\")\n",
    "\n",
    "fig = ddeq.vis.make_field_map(data_cosmo, trace_gas='CO2', fig=fig,\n",
    "                              domain=DOMAIN, vmin=404, vmax=408, cmap='viridis',\n",
    "                              alpha=data_cosmo['CLCT'] > 0.01, border=50.0,\n",
    "                              label='XCO$_2$ [ppm]')\n",
    "\n",
    "# study area\n",
    "x0, y0 = DOMAIN.proj.transform_point(12, 50.7, ccrs.PlateCarree())\n",
    "x1, y1 = DOMAIN.proj.transform_point(15.5, 52.7, ccrs.PlateCarree())\n",
    "\n",
    "box = plt.Rectangle((x0, y0), x1-x0, y1-y0,\n",
    "                    ec='black', fill=False, lw=2, ls='--')\n",
    "fig.axes[0].add_patch(box)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bab8958-2b03-4c56-9f33-a9cb5a038196",
   "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
}
