{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import cdsapi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-06 15:26:13,012 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics.\n",
      "2025-02-06 15:26:13,013 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the \"Show API request code\" tool on the dataset Download Form to check you are using the correct syntax for your API request.\n"
     ]
    }
   ],
   "source": [
    "client = cdsapi.Client()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "dx = 2.\n",
    "grid = [dx, dx]\n",
    "area = [90-dx/2, -180+dx/2, -90+dx/2, 180]\n",
    "year = ['{}'.format(y) for y in range(1979, 2025)]\n",
    "month = ['{:02d}'.format(m) for m in range(1, 13)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "dl_dir = 'tmp/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Invariant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-06 15:26:19,827 INFO Request ID is ca038add-136c-4fc8-b79e-d90856d957e8\n",
      "2025-02-06 15:26:19,916 INFO status has been updated to accepted\n",
      "2025-02-06 15:26:25,097 INFO status has been updated to running\n",
      "2025-02-06 15:26:28,565 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames.\n",
      "2025-02-06 15:26:28,567 INFO status has been updated to successful\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "ed7dd521aabcaff1f195086cba0b86f3.nc:   0%|          | 0.00/96.2k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+++ Done!\n"
     ]
    }
   ],
   "source": [
    "filename = 'ERA5_UltraLowRes_Invariant.nc'\n",
    "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n",
    "request = {\n",
    "    \"product_type\": [\"monthly_averaged_reanalysis\"],\n",
    "    \"variable\": [\n",
    "        \"geopotential\",\n",
    "        \"land_sea_mask\"\n",
    "    ],\n",
    "    \"grid\": grid,\n",
    "    \"area\": area,\n",
    "    \"year\": [\"1979\"],\n",
    "    \"month\": [\"01\"],\n",
    "    \"time\": [\"00:00\"],\n",
    "    \"data_format\": \"netcdf\",\n",
    "    \"download_format\": \"unarchived\",\n",
    "}\n",
    "\n",
    "client.retrieve(dataset, request).download(dl_dir + filename)\n",
    "print('+++ Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Unfortunate necessary cleaning\n",
    "with xr.open_dataset(dl_dir + filename) as ds:\n",
    "    # ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) #, errors=\"ignore\")\n",
    "    \n",
    "    ds = ds.isel(valid_time=0).drop_vars([\"expver\", \"number\", \"valid_time\"])\n",
    "    ds['orography'] = ds['z']  / 9.80665\n",
    "    ds['orography'].attrs.update(long_name='Orography', units='m', standard_name='orography')\n",
    "    ds = ds.drop_vars([\"z\"])\n",
    "    ds['lsm'].attrs = {k: v for k, v in ds['lsm'].attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    \n",
    "    ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    ds.to_netcdf(filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Temp and precip "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-06 15:26:29,582 INFO Request ID is d754fd9e-9084-45c6-b931-876995a832ea\n",
      "2025-02-06 15:26:29,663 INFO status has been updated to accepted\n",
      "2025-02-06 15:26:37,090 INFO status has been updated to running\n",
      "2025-02-06 15:27:22,869 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames.\n",
      "2025-02-06 15:27:22,870 INFO status has been updated to successful\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "c6d1df5982f73d62909439fdb3d153b5.nc:   0%|          | 0.00/15.7M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+++ Done!\n"
     ]
    }
   ],
   "source": [
    "filename = 'ERA5_UltraLowRes_Monthly_t2m.nc'\n",
    "\n",
    "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n",
    "request = {\n",
    "    \"product_type\": [\"monthly_averaged_reanalysis\"],\n",
    "    \"variable\": [\"2m_temperature\"],\n",
    "    \"grid\": grid,\n",
    "    \"area\": area,\n",
    "    \"year\": year,\n",
    "    \"month\": month,\n",
    "    \"time\": [\"00:00\"],\n",
    "    \"data_format\": \"netcdf\",\n",
    "    \"download_format\": \"unarchived\",\n",
    "}\n",
    "\n",
    "client.retrieve(dataset, request).download(dl_dir + filename)\n",
    "print('+++ Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Unfortunate necessary cleaning\n",
    "with xr.open_dataset(dl_dir + filename) as ds:\n",
    "    ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) \n",
    "     \n",
    "    ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    for var in ds.variables.keys():\n",
    "        ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    \n",
    "    ds.to_netcdf(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-06 15:27:25,170 INFO Request ID is 1747df94-461a-4ffc-b159-39fb36daa332\n",
      "2025-02-06 15:27:25,246 INFO status has been updated to accepted\n",
      "2025-02-06 15:27:34,529 INFO status has been updated to running\n",
      "2025-02-06 15:29:20,819 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames.\n",
      "2025-02-06 15:29:20,820 INFO status has been updated to successful\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "7232e3eeccf7fdc5235eede62a2fdb54.nc:   0%|          | 0.00/15.2M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+++ Done!\n"
     ]
    }
   ],
   "source": [
    "filename = 'ERA5_UltraLowRes_Monthly_tp.nc'\n",
    "\n",
    "dataset = \"reanalysis-era5-single-levels-monthly-means\"\n",
    "request = {\n",
    "    \"product_type\": [\"monthly_averaged_reanalysis\"],\n",
    "    \"variable\": [\"total_precipitation\"],\n",
    "    \"grid\": grid,\n",
    "    \"area\": area,\n",
    "    \"year\": year,\n",
    "    \"month\": month,\n",
    "    \"time\": [\"00:00\"],\n",
    "    \"data_format\": \"netcdf\",\n",
    "    \"download_format\": \"unarchived\",\n",
    "}\n",
    "\n",
    "client.retrieve(dataset, request).download(dl_dir + filename)\n",
    "print('+++ Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Unfortunate necessary cleaning\n",
    "with xr.open_dataset(dl_dir + filename) as ds:\n",
    "    ds = ds.rename({\"valid_time\": \"time\"}).drop_vars([\"expver\", \"number\"]) \n",
    "     \n",
    "    ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    for var in ds.variables.keys():\n",
    "        ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith(\"GRIB_\")}\n",
    "    \n",
    "    ds.to_netcdf(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "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.8"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}