Skip to content
Snippets Groups Projects
produce_data_withOptional.ipynb 4.59 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "code",
Carsten Hinz's avatar
Carsten Hinz committed
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime as dt\n",
    "from collections import namedtuple\n",
    "from pathlib import Path\n",
    "\n",
    "from toargridding.toar_rest_client import AnalysisServiceDownload, Connection\n",
    "from toargridding.grids import RegularGrid\n",
    "from toargridding.gridding import get_gridded_toar_data\n",
    "from toargridding.metadata import TimeSample"
   ]
  },
  {
   "cell_type": "code",
Carsten Hinz's avatar
Carsten Hinz committed
   "execution_count": null,
   "metadata": {},
Carsten Hinz's avatar
Carsten Hinz committed
   "outputs": [],
   "source": [
Carsten Hinz's avatar
Carsten Hinz committed
    "#creation of request.\n",
    "\n",
    "Config = namedtuple(\"Config\", [\"grid\", \"time\", \"variables\", \"stats\",\"moreOptions\"])\n",
    "\n",
    "#moreOptions is implemented as a dict to add additional arguments to the query to the REST API\n",
    "#For example the field toar1_category with its possible values Urban, RuralLowElevation, RuralHighElevation and Unclassified can be added.\n",
    "#see page 18 in https://toar-data.fz-juelich.de/sphinx/TOAR_UG_Vol03_Database/build/latex/toardatabase--userguide.pdf\n",
    "#or type_of_area with urban, suburban and rural on page 20 can be used\n",
    "    #\"toar1_category\" : \"Urban\" #uncomment if wished:-)\n",
    "    #\"toar1_category\" : \"RuralLowElevation\" #uncomment if wished:-)\n",
    "    #\"toar1_category\" : \"RuralHighElevation\" #uncomment if wished:-)\n",
    "    #\"type_of_area\" : \"Urban\" #also test Rural, Suburban,\n",
    "    \"type_of_area\" : \"Rural\" #also test Rural, Suburban,\n",
    "    #\"type_of_area\" : \"Suburban\" #also test Rural, Suburban,\n",
    "grid = RegularGrid( lat_resolution=1.9, lon_resolution=2.5, )\n",
    "configs = dict()\n",
Carsten Hinz's avatar
Carsten Hinz committed
    "for year in range (0,19):\n",
    "    valid_data = Config(\n",
    "        grid,\n",
    "        TimeSample( start=dt(2000+year,1,1), end=dt(2000+year,12,31), sampling=\"daily\"),#possibly adopt range:-)\n",
    "        [\"mole_fraction_of_ozone_in_air\"],#variable name\n",
    "        #[ \"mean\", \"dma8epax\"],# will start one request after another other...\n",
    "        [ \"dma8epa_strict\" ],\n",
    "        details4Query\n",
    "    )\n",
    "    \n",
    "    configs[f\"test_ta{year}\"] = valid_data\n",
Carsten Hinz's avatar
Carsten Hinz committed
    "#testing access:\n",
    "#config = configs[\"test_ta2\"]\n",
    "#config.grid"
   ]
  },
  {
   "cell_type": "code",
Carsten Hinz's avatar
Carsten Hinz committed
   "execution_count": null,
   "metadata": {},
Carsten Hinz's avatar
Carsten Hinz committed
   "outputs": [],
   "source": [
    "#CAVE: this cell runs about 45minutes per requested year. therefore we increase the waiting duration to 1h per request.\n",
Carsten Hinz's avatar
Carsten Hinz committed
    "#the processing is done on the server of the TOAR database.\n",
    "#a restart of the cell continues the request to the REST API if the requested data are ready for download\n",
    "# The download can also take a few minutes\n",
    "\n",
    "stats_endpoint = \"https://toar-data.fz-juelich.de/api/v2/analysis/statistics/\"\n",
    "cache_basepath = Path(\"cache\")\n",
    "result_basepath = Path(\"results\")\n",
    "cache_basepath.mkdir(exist_ok=True)\n",
Carsten Hinz's avatar
Carsten Hinz committed
    "result_basepath.mkdir(exist_ok=True)\n",
    "analysis_service = AnalysisServiceDownload(stats_endpoint=stats_endpoint, cache_dir=cache_basepath, sample_dir=result_basepath, use_downloaded=True)\n",
    "Connection.DEBUG=True\n",
    "minutes = 5\n",
    "analysis_service.connection.wait_seconds = [minutes * 60 for i in range(5,61,minutes) ]\n",
    "for person, config in configs.items():\n",
    "    print(f\"\\nProcessing {person}:\")\n",
    "    print(f\"--------------------\")\n",
    "    datasets, metadatas = get_gridded_toar_data(\n",
    "        analysis_service=analysis_service,\n",
    "        grid=config.grid,\n",
    "        time=config.time,\n",
    "        variables=config.variables,\n",
    "        stats=config.stats,\n",
    "        **config.moreOptions\n",
    "    )\n",
    "\n",
    "    for dataset, metadata in zip(datasets, metadatas):\n",
    "        dataset.to_netcdf(result_basepath / f\"{metadata.get_id()}_{config.grid.get_id()}.nc\")\n",
Carsten Hinz's avatar
Carsten Hinz committed
    "        print(metadata.get_id())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "toargridding-8RVrxzmn-py3.11",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}