{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f21dd21e",
   "metadata": {},
   "source": [
    "# map_raster examples "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "307c52e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xarray as xr\n",
    "import matplotlib.pyplot as plt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "474d39c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tests.tools_test import fake_dataset,fake_ecmwf_0100_1h,build_footprint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1306e69",
   "metadata": {},
   "source": [
    "# I - Not crossing Meridian"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f241cca",
   "metadata": {},
   "source": [
    "## generate fake raster (ECMWF-like )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd842425",
   "metadata": {},
   "outputs": [],
   "source": [
    "crossAntiMeridian = False\n",
    "to180 = True\n",
    "\n",
    "\n",
    "\n",
    "raster = fake_ecmwf_0100_1h(to180=to180,with_nan=False)\n",
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(raster.x, raster.y, np.sqrt(raster.U10**2+raster.V10**2)) \n",
    "plt.title(\"simulated wind speed (m/s)\")\n",
    "plt.colorbar(label='m/s')\n",
    "plt.xlabel(\"Longitude\")\n",
    "plt.ylabel(\"Latitude\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9459b9d5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6f18b37d",
   "metadata": {},
   "source": [
    "## generate fake SAR data + footprint "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "676bfa3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = fake_dataset(cross_antimeridian=crossAntiMeridian)\n",
    "dataset\n",
    "\n",
    "footprint = build_footprint(dataset)\n",
    "\n",
    "xs, ys = np.array(footprint.exterior.xy[0]), footprint.exterior.xy[1]   \n",
    "xlims = (np.min(xs), np.max(xs))\n",
    "ylims = (np.min(ys), np.max(ys))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "825a10c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac222edb",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(dataset.sample, dataset.line, dataset.longitude) \n",
    "\n",
    "plt.title(\"Longitude\")\n",
    "plt.colorbar(label='degrees_east')\n",
    "plt.xlabel(\"Sample\")\n",
    "plt.ylabel(\"Line\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d9b0959",
   "metadata": {},
   "source": [
    "## map_raster main usage example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2954a6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mapraster.main import map_raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db99bdd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "rastered = map_raster(raster_ds=raster,originalDataset=dataset, footprint=footprint, cross_antimeridian=crossAntiMeridian)\n",
    "dataset[\"U10\"] = rastered[\"U10\"]\n",
    "dataset[\"V10\"] = rastered[\"V10\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6de3636",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.pcolormesh(dataset.longitude, dataset.latitude, np.sqrt(dataset.U10**2+dataset.V10**2))\n",
    "#plot footprint\n",
    "plt.plot(xs, ys, color='red', linewidth=2, label =  'footprint')\n",
    "plt.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41867d8b",
   "metadata": {},
   "source": [
    "# II - Crossing Meridian\n",
    "\n",
    "Need to activate crossAntimeridian = True and to display longitudes in [0,360]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc202067",
   "metadata": {},
   "outputs": [],
   "source": [
    "crossAntiMeridian = True\n",
    "to180 = False\n",
    "\n",
    "raster = fake_ecmwf_0100_1h(to180=to180,with_nan=False)\n",
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(raster.x, raster.y, np.sqrt(raster.U10**2+raster.V10**2)) \n",
    "plt.title(\"simulated wind speed (m/s)\")\n",
    "plt.colorbar(label='m/s')\n",
    "plt.xlabel(\"Longitude\")\n",
    "plt.ylabel(\"Latitude\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f75cf1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f59680d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = fake_dataset(cross_antimeridian=crossAntiMeridian)\n",
    "dataset\n",
    "\n",
    "footprint = build_footprint(dataset)\n",
    "\n",
    "xs, ys = np.array(footprint.exterior.xy[0])%360, footprint.exterior.xy[1]   \n",
    "xlims = (np.min(xs), np.max(xs))\n",
    "ylims = (np.min(ys), np.max(ys))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f21ec48",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(dataset.sample, dataset.line, dataset.longitude) \n",
    "\n",
    "plt.title(\"Longitude\")\n",
    "plt.colorbar(label='degrees_east')\n",
    "plt.xlabel(\"Sample\")\n",
    "plt.ylabel(\"Line\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f5be21c",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(raster.x, raster.y, np.sqrt(raster.U10**2+raster.V10**2), vmin = 6.76, vmax=6.8) \n",
    "plt.title(\"simulated wind speed (m/s)\")\n",
    "plt.colorbar(label='m/s')\n",
    "plt.xlabel(\"Longitude\")\n",
    "plt.ylabel(\"Latitude\")\n",
    "plt.xlim(np.min(xlims), np.max(xlims))\n",
    "plt.ylim(np.min(ylims), np.max(ylims))\n",
    "#plot footprint\n",
    "plt.plot(xs, ys, color='red', linewidth=2, label =  'footprint')\n",
    "plt.legend(loc = 'upper right')\n",
    "plt.show()\n",
    "\n",
    "\n",
    "\n",
    "rastered = map_raster(raster_ds=raster,originalDataset=dataset, footprint=footprint, cross_antimeridian=crossAntiMeridian)\n",
    "dataset[\"U10\"] = rastered[\"U10\"]\n",
    "dataset[\"V10\"] = rastered[\"V10\"]\n",
    "plt.figure(figsize=(6,3))\n",
    "\n",
    "plt.pcolormesh(dataset.longitude%360, dataset.latitude, np.sqrt(dataset.U10**2+dataset.V10**2), vmin = 6.76, vmax=6.8)\n",
    "#plot footprint\n",
    "plt.plot(xs, ys, color='red', linewidth=2, label =  'footprint')\n",
    "plt.title(\"Mapped wind speed (m/s)\") \n",
    "plt.colorbar(label='m/s')\n",
    "plt.legend(loc='upper right');\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5490e05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Same with Nans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "900e6653",
   "metadata": {},
   "outputs": [],
   "source": [
    "crossAntiMeridian = True\n",
    "to180 = False\n",
    "\n",
    "raster = fake_ecmwf_0100_1h(to180=to180,with_nan=True)\n",
    "plt.figure(figsize=(6,3))\n",
    "plt.pcolormesh(raster.x, raster.y, np.sqrt(raster.U10**2+raster.V10**2), vmin = 6.76, vmax=6.8) \n",
    "plt.title(\"simulated wind speed (m/s)\")\n",
    "plt.colorbar(label='m/s')\n",
    "plt.xlabel(\"Longitude\")\n",
    "plt.ylabel(\"Latitude\")\n",
    "plt.xlim(np.min(xlims), np.max(xlims))\n",
    "plt.ylim(np.min(ylims), np.max(ylims))\n",
    "#plot footprint\n",
    "plt.plot(xs, ys, color='red', linewidth=2, label =  'footprint')\n",
    "plt.legend(loc = 'upper right')\n",
    "plt.show()\n",
    "\n",
    "\n",
    "\n",
    "rastered = map_raster(raster_ds=raster,originalDataset=dataset, footprint=footprint, cross_antimeridian=crossAntiMeridian)\n",
    "dataset[\"U10\"] = rastered[\"U10\"]\n",
    "dataset[\"V10\"] = rastered[\"V10\"]\n",
    "plt.figure(figsize=(6,3))\n",
    "\n",
    "plt.pcolormesh(dataset.longitude%360, dataset.latitude, np.sqrt(dataset.U10**2+dataset.V10**2), vmin = 6.76, vmax=6.8)\n",
    "#plot footprint\n",
    "plt.plot(xs, ys, color='red', linewidth=2, label =  'footprint')\n",
    "plt.title(\"Mapped wind speed (m/s)\") \n",
    "plt.colorbar(label='m/s')\n",
    "plt.legend(loc='upper right');\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_xsar",
   "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
