# HG changeset patch # User climate # Date 1618338765 0 # Node ID 250554670f72ebbd85bbb455b6092d18a7059dc6 "planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811" diff -r 000000000000 -r 250554670f72 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue Apr 13 18:32:45 2021 +0000 @@ -0,0 +1,25 @@ + +# Copernicus Climate Data Store (C3S) + +This tool allows you to retrieve [Copernicus Climate Data Store (C3S)](https://cds.climate.copernicus.eu/#!/home). + +- Any user willing to use this tool needs to [create a new account](https://cds.climate.copernicus.eu/user/register?destination=%2F%23!%2Fhome). +- Compose your request directly on C3S and copy/paste it in the input field "Request" +- Users need to add their CDS API Key to Galay (see below). Documentation on where to get the CDS API key can be found [here](https://cds.climate.copernicus.eu/api-how-to). +- Be aware that for being able to download dataset from C3S, users also need to agree to their term of use (Licence to use Copernicus Products) on the C3S website. + +## Set up user credentials on Galaxy + +To enable users to set their credentials and provide their CDS API Key for this tool, +make sure the file `config/user_preferences_extra.yml` has the following section: + +``` + c3s_account: + description: Your CDS API Key (Copernicus Climate Change Service API Key) + inputs: + - name: c3s_cds_apikey + label: C3S CDS API Key + type: text + required: True +``` + diff -r 000000000000 -r 250554670f72 c3s.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c3s.sh Tue Apr 13 18:32:45 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 250554670f72 c3s.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c3s.xml Tue Apr 13 18:32:45 2021 +0000 @@ -0,0 +1,114 @@ + + for retrieveing climate data + + python + cdsapi + cdo + tar + unzip + + Preferences -> Manage Information" && + #else + cp '$cds_key_file' .cdsapirc && + #end if + #if str($is_file.has_req).strip() == 'yes' + python3 '$__tool_directory__/c3s_retrieve.py' + --request '$is_file.api_req_file' --output request.txt && + #else + python3 '$__tool_directory__/c3s_retrieve.py' + --request $req_from_paste --output request.txt && + #end if + bash $__tool_directory__/c3s.sh && + echo "C3S data retrieval is done" + ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + `_. +- Set your CDS 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 C3S 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 C3S, users also need to agree to their term of use (Licence to use Copernicus Products) on the C3S website. + +License: +~~~~~~~~ + +Generated using Copernicus Climate Change 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 250554670f72 c3s_retrieve.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c3s_retrieve.py Tue Apr 13 18:32:45 2021 +0000 @@ -0,0 +1,60 @@ +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() + +if path.isfile(args.request): + f = open(args.request, "r") + req = f.read() + f.close() + mapped_chars = { + '>': '__gt__', + '<': '__lt__', + "'": '__sq__', + '"': '__dq__', + '[': '__ob__', + ']': '__cb__', + '{': '__oc__', + '}': '__cc__', + '@': '__at__', + '#': '__pd__', + "": '__cn__' + } + + # 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].split('}')[0].replace('\n', '') + '}' +c3s_req_dict = ast.literal_eval(c3s_req) + +c3s_output = req.split('}')[1].split(',')[1].split(')')[0].strip(' "\'\t\r\n') + +f = open(args.output, "w") +f.write("dataset to retrieve: " + c3s_type + "\n") +f.write("request: " + c3s_req + "\n") +f.write("output filename: " + c3s_output) +f.close() + +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 250554670f72 test-data/input.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/input.txt Tue Apr 13 18:32:45 2021 +0000 @@ -0,0 +1,19 @@ +import cdsapi + +c = cdsapi.Client() + +c.retrieve( + 'reanalysis-era5-single-levels-monthly-means', + { + 'format': 'netcdf', + 'product_type': 'monthly_averaged_reanalysis', + 'variable': '2m_temperature', + 'year': '2020', + 'month': '12', + 'time': '00:00', + 'area': [ + 60, 10, 59.5, + 10.5, + ], + }, + 'download.nc')