changeset 0:250554670f72 draft

"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
author climate
date Tue, 13 Apr 2021 18:32:45 +0000
parents
children bd153c794da1
files README.md c3s.sh c3s.xml c3s_retrieve.py test-data/input.txt
diffstat 5 files changed, 237 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
+```
+
--- /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
--- /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 @@
+<tool id="c3s" name="Copernicus Climate Data Store" version="0.1.0">
+    <description>for retrieveing climate data</description>
+    <requirements>
+        <requirement type="package" version="3">python</requirement>
+        <requirement type="package" version="0.5.1">cdsapi</requirement>
+        <requirement type="package" version="1.9.9">cdo</requirement>
+        <requirement type="package" version="1.34">tar</requirement>
+        <requirement type="package" version="6.0">unzip</requirement>
+    </requirements>
+    <command detect_errors="exit_code"><![CDATA[
+       export HOME=`pwd`  &&
+       #set $c3s_cds_apikey = $__user__.extra_preferences.get('c3s_account|c3s_cds_apikey', "")
+       #if $c3s_cds_apikey == ""
+	    echo "Error. Set your credentials via: User -> 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"
+    ]]></command>
+    <configfiles>
+        <configfile name="cds_key_file"><![CDATA[
+#set $c3s_cds_apikey = $__user__.extra_preferences.get('c3s_account|c3s_cds_apikey', "")
+         url: https://cds.climate.copernicus.eu/api/v2
+         key: $c3s_cds_apikey
+        ]]></configfile>
+        <configfile name="req_from_paste"><![CDATA[
+       #if str($is_file.has_req).strip() == 'no'
+$is_file.api_req_text
+       #end if
+        ]]></configfile>
+    </configfiles>
+    <inputs>
+        <conditional name="is_file">
+            <param name="has_req" type="select" label="C3S request is a given as a filename">
+                <option value="yes" selected="true">yes</option>
+                <option value="no">no</option>
+            </param>
+	   <when value="yes">
+	        <param name="api_req_file" type="data" label="API Request filename" format="txt"/>
+           </when>
+            <when value="no">
+	        <param name="api_req_text" type="text" label="Paste API Request" area="true" />
+           </when>
+        </conditional>
+    </inputs>
+    <outputs>
+        <data name="request" format="txt" from_work_dir="request.txt"/>
+        <data name="ofilename" format="netcdf" from_work_dir="tmp.nc"/>
+    </outputs>
+    <tests>
+        <test>
+            <param name="api_req_file" value="input.txt" />
+            <output name="request" ftype="txt">
+              <assert_contents>
+                  <has_text text="dataset to retrieve: reanalysis-era5-single-levels-monthly-means" />
+                  <has_text text="'format': 'netcdf'" />
+                  <has_text text="'product_type': 'monthly_averaged_reanalysis'" />
+                  <has_text text="'variable': '2m_temperature'" />
+                  <has_text text="'year': '2020'" />
+                  <has_text text="'month': '12'" />
+                  <has_text text="'time': '00:00'" />
+		  <has_text text="'area': [" />
+	          <has_text text="60," />
+		  <has_text text="10," />
+		  <has_text text="59.5," />            
+		  <has_text text="10.5," />
+		  <has_text text="]" />
+		  <has_text text="output filename: download.nc" />
+              </assert_contents>
+            </output>
+        </test>
+    </tests>
+    <help><![CDATA[
+
+**Copernicus Climate Data Store (C3S)**
+=======================================================================================================
+
+This tool is a wrapper to retrieve data from the Copernicus Climate Data Store.
+
+- It allows to retrieve data from the Copernicus climate Data Store.
+- Any user willing to use this tool needs to `create a new account <https://cds.climate.copernicus.eu/user/register?destination=%2F%23!%2Fhome>`_.
+- Set your CDS API Key via: User -> Preferences -> Manage Information"
+- Documentation on where to get the CDS API key can be found `here <https://cds.climate.copernicus.eu/api-how-to>`_.
+- 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.
+    ]]></help>
+    <citations>
+
+    </citations>
+    <edam_topics>
+      <edam_topic>topic_3855</edam_topic>
+      <edam_topic>topic_3318</edam_topic>
+    </edam_topics>
+    <edam_operations>
+      <edam_operation>operation_2422</edam_operation>
+      <edam_operation>operation_3357</edam_operation>
+      <edam_operation>operation_0335</edam_operation>
+    </edam_operations>
+</tool>
--- /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")
--- /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')