# HG changeset patch # User climate # Date 1624127750 0 # Node ID b768e517a7d09cc1d9a0d5ba70766c39baaf7073 "planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/cads commit 778688ee8d14e22b00b648858ad9bd1c2f46b1e0" diff -r 000000000000 -r b768e517a7d0 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,25 @@ + +# Copernicus Atmosphere Data Store (ADS) + +This tool allows you to retrieve [Copernicus Atmosphere Data Store (ADS)](https://ads.atmosphere.copernicus.eu/cdsapp#!/home). + +- Any user willing to use this tool needs to [create a new account](https://ads.atmosphere.copernicus.eu/user/register?destination=/). +- Compose your request directly on ADS and copy/paste it in the input field "Request" +- Users need to add their ADS API Key to Galaxy (see below). Documentation on where to get the ADS API key can be found [here](https://ads.atmosphere.copernicus.eu/api-how-to). +- Be aware that for being able to download dataset from ADS, users also need to agree to their term of use (Licence to use Copernicus Products) on the ADS website. + +## Set up user credentials on Galaxy + +To enable users to set their credentials and provide their ADS API Key for this tool, +make sure the file `config/user_preferences_extra.yml` has the following section: + +``` + cads_account: + description: Your Copernicus ADS API Key (Copernicus Atmosphere Data Store API Key) + inputs: + - name: cads_apikey + label: Copernicus ADS API Key + type: password + required: True +``` + diff -r 000000000000 -r b768e517a7d0 cads.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cads.sh Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +if [ -f download.tar.gz ]; then + tar zxvf download.tar.gz + cat *.grib > tmp.grib + cdo setgridtype,regular tmp.grib tmpg.grib + cdo -f nc -t ecmwf copy tmpg.grib tmp.nc +elif [ -f download.zip ]; then + unzip download.zip + cat *.grib > tmp.grib + cdo setgridtype,regular tmp.grib tmpg.grib + cdo -f nc -t ecmwf copy tmpg.grib tmp.nc +elif [ -f download.grib ]; then + cdo -f nc -t ecmwf copy download.grib tmp.nc +elif [ -f download.nc ]; then + mv download.nc tmp.nc +else + echo "No data found! Check your request and/or credentials." +fi diff -r 000000000000 -r b768e517a7d0 cads.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cads.xml Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,111 @@ + + for retrieving data from the Atmosphere Monitoring Service + + python + cdsapi + cdo + tar + unzip + + Preferences -> Manage Information' && + echo 'Will try to use tool adsapirc file (used for testing)' && + cp '$__tool_directory__/adsapirc.cfg' .cdsapirc && + #else + cp '$cads_key_file' .cdsapirc && + #end if + + python3 '$__tool_directory__/cads_retrieve.py' + #if str($is_file.has_req).strip() == 'yes' + --request '$is_file.api_req_file' + #else + --request '$req_from_paste' + #end if + --output request.txt && + bash '$__tool_directory__/cads.sh' && + echo "Data retrieval from Copernicus Atmosphere Data Store is done" + ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + `_. +- Set your CADS API Key via: User -> Preferences -> Manage Information" +- Documentation on where to get the CDS API key can be found `here `_. +- Compose your request directly on Copernicus Atmosphere Data Store and copy/paste it in the input field "Request" or save it in a file. +- Be aware that for being able to download dataset from ADS, users also need to agree to their term of use (Licence to use Copernicus Products) on the ADS website. + +License: +~~~~~~~~ + +Generated using Copernicus Atmosphere Monitoring Service information [2021] +Neither the European Commission nor ECMWF is responsible for any use +that may be made of the Copernicus information or data it contains. + ]]> + + + + topic_3855 + topic_3318 + + + operation_2422 + operation_3357 + operation_0335 + + diff -r 000000000000 -r b768e517a7d0 cads_retrieve.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cads_retrieve.py Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,56 @@ +import argparse +import ast +from os import environ, path + +import cdsapi + +parser = argparse.ArgumentParser() +parser.add_argument("-i", "--request", type=str, help="input API request") +parser.add_argument("-o", "--output", type=str, help="output API request") +args = parser.parse_args() + +mapped_chars = { + '>': '__gt__', + '<': '__lt__', + "'": '__sq__', + '"': '__dq__', + '[': '__ob__', + ']': '__cb__', + '{': '__oc__', + '}': '__cc__', + '@': '__at__', + '#': '__pd__', + "": '__cn__' +} + +with open(args.request) as f: + req = f.read() + + # Unsanitize labels (element_identifiers are always sanitized by Galaxy) + for key, value in mapped_chars.items(): + req = req.replace(value, key) + +print("req = ", req) +c3s_type = req.split('c.retrieve')[1].split('(')[1].split(',')[0].strip(' "\'\t\r\n') + +c3s_req = '{' + req.split('{', 1)[1].rsplit('}', 1)[0].replace('\n', '') + '}' +c3s_req_dict = ast.literal_eval(c3s_req) + +c3s_output = req.rsplit('}', 1)[1].split(',')[1].split(')')[0].strip(' "\'\t\r\n') + +with open(args.output, "w") as f: + f.write(f'dataset to retrieve: {c3s_type}\nrequest: {c3s_req}\noutput filename: {c3s_output}') + +print("start retrieving data...") + +cdapi_file = path.join(environ.get('HOME'), '.cdsapirc') + +if path.isfile(cdapi_file): + c = cdsapi.Client() + + c.retrieve( + c3s_type, + c3s_req_dict, + c3s_output) + + print("data retrieval successful") diff -r 000000000000 -r b768e517a7d0 test-data/output.nc Binary file test-data/output.nc has changed diff -r 000000000000 -r b768e517a7d0 test-data/req-timeseries.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/req-timeseries.txt Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,19 @@ +import cdsapi + +c = cdsapi.Client() + +c.retrieve( + 'cams-solar-radiation-timeseries', + { + 'sky_type': 'clear', + 'location': { + 'latitude': 60, + 'longitude': 11, + }, + 'altitude': '-999.', + 'date': '2021-06-17/2021-06-17', + 'time_step': '15minute', + 'time_reference': 'universal_time', + 'format': 'netcdf', + }, + 'download.nc') diff -r 000000000000 -r b768e517a7d0 test-data/req.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/req.txt Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,21 @@ +import cdsapi + +c = cdsapi.Client() + +c.retrieve( + 'cams-europe-air-quality-forecasts', + { + 'variable': 'nitrogen_dioxide', + 'model': 'ensemble', + 'level': '0', + 'date': '2021-06-16/2021-06-16', + 'type': 'analysis', + 'time': '00:00', + 'leadtime_hour': '0', + 'format': 'netcdf', + 'area': [ + 59.96, 10.66, 59.87, + 10.9, + ], + }, + 'download.nc') diff -r 000000000000 -r b768e517a7d0 test-data/req_out.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/req_out.txt Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,3 @@ +dataset to retrieve: cams-europe-air-quality-forecasts +request: { 'variable': 'nitrogen_dioxide', 'model': 'ensemble', 'level': '0', 'date': '2021-06-16/2021-06-16', 'type': 'analysis', 'time': '00:00', 'leadtime_hour': '0', 'format': 'netcdf', 'area': [ 59.96, 10.66, 59.87, 10.9, ], } +output filename: download.nc \ No newline at end of file diff -r 000000000000 -r b768e517a7d0 test-data/timeseries.nc Binary file test-data/timeseries.nc has changed diff -r 000000000000 -r b768e517a7d0 test-data/timeseries_out.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/timeseries_out.txt Sat Jun 19 18:35:50 2021 +0000 @@ -0,0 +1,3 @@ +dataset to retrieve: cams-solar-radiation-timeseries +request: { 'sky_type': 'clear', 'location': { 'latitude': 60, 'longitude': 11, }, 'altitude': '-999.', 'date': '2021-06-17/2021-06-17', 'time_step': '15minute', 'time_reference': 'universal_time', 'format': 'netcdf', } +output filename: download.nc \ No newline at end of file