{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CDS API plugin for EODAG\n", "\n", "In this tutorial we will show you how to use eodag to download data from providers using [CdsApi](https://eodag.readthedocs.io/en/latest/plugins_reference/generated/eodag.plugins.apis.cds.CdsApi.html) `eodag` plugin. You can currently find two providers that uses it, `cop_ads` and `cop_cds`. For this tutorial we will use `cop_ads`, but `cop_cds` is used the same way. The API plugin and this tutorial have been developed in the context of DOMINO-X." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from eodag import EODataAccessGateway, setup_logging\n", "\n", "setup_logging(0) # 0: nothing, 1: only progress bars, 2: INFO, 3: DEBUG\n", "dag = EODataAccessGateway()\n", "dag.set_preferred_provider(\"cop_ads\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Search (build download request)\n", "\n", "There are two use case, a search for a product already configured in EODAG, or a search for a dataset not already configured, where you will have a little more to do.\n", "\n", "For performance purpose, we can add a `variable` (Temperature, `temperature`) and one `model_level` to the request because `CAMS_EAC4` is configured to request for some pre-configured values. Check the dataset available values to make your selection.\n", "\n", "> Note: specifying your own variables will completely overwrite default values configured for `CAMS_EAC4` product type.\n", "\n", "### Search from an existing product type:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 product built EOProduct(id=CAMS_EAC4_20210101_9fa1614f23014d2fb17c38927a5e87ecb3dcf6aa, provider=cop_ads),\n", " having variable=['dust_aerosol_0.03-0.55um_mixing_ratio', 'dust_aerosol_0.55-0.9um_mixing_ratio', 'dust_aerosol_0.9-20um_mixing_ratio', 'dust_aerosol_optical_depth_550nm', 'hydrophilic_black_carbon_aerosol_mixing_ratio', 'hydrophilic_organic_matter_aerosol_mixing_ratio', 'hydrophobic_black_carbon_aerosol_mixing_ratio', 'hydrophobic_organic_matter_aerosol_mixing_ratio', 'sea_salt_aerosol_0.03-0.5um_mixing_ratio', 'sea_salt_aerosol_0.5-5um_mixing_ratio', 'sea_salt_aerosol_5-20um_mixing_ratio', 'sea_salt_aerosol_optical_depth_550nm', 'sulphate_aerosol_optical_depth_550nm']\n", "and model_level=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60']\n", "\n", "1 product built EOProduct(id=CAMS_EAC4_20210101_63a610e702b9132d1fca308e390ebd89f336f812, provider=cop_ads),\n", " having variable=temperature\n", "and model_level=1\n", "\n" ] } ], "source": [ "# Request for all parameters\n", "products_from_product_type, total_count = dag.search(\n", " start=\"2021-01-01\",\n", " end=\"2021-01-02\",\n", " productType=\"CAMS_EAC4\",\n", ")\n", "print(\n", " \"%s product built %s,\\n having variable=%s\\nand model_level=%s\\n\"\n", " % (\n", " total_count,\n", " products_from_product_type[0],\n", " products_from_product_type[0].properties[\"variable\"],\n", " products_from_product_type[0].properties[\"model_level\"],\n", " )\n", ")\n", "# Request for temperature on one model level\n", "products_from_product_type, total_count = dag.search(\n", " start=\"2021-01-01\",\n", " end=\"2021-01-02\",\n", " productType=\"CAMS_EAC4\",\n", " variable=\"temperature\",\n", " model_level=\"1\",\n", ")\n", "print(\n", " \"%s product built %s,\\n having variable=%s\\nand model_level=%s\\n\"\n", " % (\n", " total_count,\n", " products_from_product_type[0],\n", " products_from_product_type[0].properties[\"variable\"],\n", " products_from_product_type[0].properties[\"model_level\"],\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Search using a custom request:\n", "\n", "Here we use a set of custom parameters corresponding to `CAMS_EAC4`, which should result to the same request sent to ads." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Request using productType or directly ADS parameters result to the\n", " same downloadLink https://ads.atmosphere.copernicus.eu/cdsapp#!/dataset/cams-global-reanalysis-eac4?date=2021-01-01/2021-01-01&area=90.0/-180.0/-90.0/180.0&class=mc&dataset=cams-global-reanalysis-eac4&expver=0001&format=netcdf&model_level=1&step=0&stream=oper&time=00:00&variable=temperature\n" ] } ], "source": [ "ads_req_params = {\n", " \"dataset\": \"cams-global-reanalysis-eac4\",\n", " \"stream\": \"oper\",\n", " \"class\": \"mc\",\n", " \"expver\": \"0001\",\n", " \"step\": 0,\n", " \"variable\": \"temperature\",\n", " \"model_level\": \"1\",\n", " \"time\": \"00:00\",\n", " \"format\": \"netcdf\",\n", "}\n", "\n", "products_from_ads_req, total_count = dag.search(\n", " start=\"2021-01-01\",\n", " end=\"2021-01-02\",\n", " **ads_req_params,\n", ")\n", "# downloadLink property must be the same with the two request methods,\n", "# as they are built from the same ADS request arguments\n", "if (\n", " products_from_ads_req[0].properties[\"downloadLink\"]\n", " == products_from_product_type[0].properties[\"downloadLink\"]\n", "):\n", " print(\n", " \"Request using productType or directly ADS parameters result to the\\n\",\n", " \"same downloadLink %s\"\n", " % (\n", " products_from_ads_req[0].properties[\"downloadLink\"],\n", " )\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Send product retrieval request and download when available\n", "\n", "- download performed using ADS credentials set in `~/.config/eodag/eodag.yml` as for other EO providers:\n", "```yaml\n", " cop_ads:\n", " priority:\n", " api:\n", " outputs_prefix: /data/eodag_data\n", " credentials:\n", " username: my-ads-uid\n", " password: my-ads-api-key\n", "```\n", "- you can check your request status from https://ads.atmosphere.copernicus.eu/cdsapp#!/yourrequests\n", "- See [support](https://ads.atmosphere.copernicus.eu/cdsapp#!/usersupport) for any problem related to the provider" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/data/eodag_data/CAMS_EAC4_20210101_63a610e702b9132d1fca308e390ebd89f336f812.nc'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "product_path = dag.download(products_from_product_type[0])\n", "product_path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Open dataset with `xarray` and plot using `matplotlib`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (longitude: 480, latitude: 241, time: 1)\n", "Coordinates:\n", " * longitude (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2\n", " * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0\n", " * time (time) datetime64[ns] 2021-01-01\n", "Data variables:\n", " t (time, latitude, longitude) float32 257.2 257.2 ... 243.7 243.7\n", "Attributes:\n", " Conventions: CF-1.6\n", " history: 2022-09-13 14:38:48 GMT by grib_to_netcdf-2.25.1: /opt/ecmw...