Repository 'cds_essential_variability'
hg clone https://toolshed.g2.bx.psu.edu/repos/climate/cds_essential_variability

Changeset 0:5c0ab9932311 (2019-05-03)
Next changeset 1:8b30be2cef81 (2019-05-07)
Commit message:
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
added:
README.md
ecv_retrieve.py
essential_climate_variables.xml
test-data/prep_seaice.nc
test-data/soil_moisture.nc
b
diff -r 000000000000 -r 5c0ab9932311 README.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md Fri May 03 15:11:01 2019 -0400
[
@@ -0,0 +1,17 @@
+
+# Copernicus Essential Climate Variables for assessing climate variability
+
+This tool allows you to retrieve Copernicus **Essential Climate Variables** for assessing climate variability from the [Copernicus Climate Data Store](https://cds.climate.copernicus.eu/#!/home).
+
+Copernicus ECV for assessing climate variability is available from the Copernicus portal at https://cds.climate.copernicus.eu/cdsapp#!/dataset/ecv-for-climate-change?tab=overview
+
+To be able to retrieve data, you would need to register and get your [CDS API key](https://cds.climate.copernicus.eu/api-how-to).
+
+The CDS API Key needs to be:
+  - located in a file called `.cdsapirc`in the HOME area 
+    (as defined the `HOME` environment variable).
+  - or passed in an enviroment variable called `GALAXY_COPERNICUS_CDSAPIRC`. 
+    When passed in `GALAXY_COPERNICUS_CDSAPIRC`, make sure the key does not contain
+    the string `key: ` but the key itself only (starting with a number). The file
+    `.cdsapirc` will then be created and placed in the HOME area (using HOME
+    environment variable).
b
diff -r 000000000000 -r 5c0ab9932311 ecv_retrieve.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ecv_retrieve.py Fri May 03 15:11:01 2019 -0400
[
@@ -0,0 +1,145 @@
+# Retrieve Copernicus ECV
+# (Essential climate Variables)
+
+import argparse
+import os
+import shutil
+import tarfile
+import tempfile
+import warnings
+
+import cdsapi
+
+
+class ECV ():
+    def __init__(self, archive, variable, product_type, year,
+                 month, time_aggregation, area, format, output,
+                 verbose=False
+                 ):
+        self.archive = archive
+        self.variable = variable.split(',')
+        if product_type == '':
+            self.product_type = 'climatology'
+        else:
+            self.product_type = product_type
+        if year == '':
+            self.year = '2019'
+        else:
+            self.year = year.split(',')
+        if month == '':
+            self.month = '01'
+        else:
+            self.month = month.split(',')
+        if time_aggregation == '':
+            self.time_aggregation = '1_month'
+        else:
+            self.time_aggregation = time_aggregation
+        self.area = area
+        if format == '':
+            self.format = 'tgz'
+        else:
+            self.format = format
+        if output == '':
+            self.outputfile = "donwload." + self.format
+        else:
+            self.outputfile = output
+        if verbose:
+            print("archive: ", self.archive)
+            print("variable: ", self.variable)
+            print("year: ", self.year)
+            print("month: ", self.month)
+        self.cdsapi = cdsapi.Client()
+
+    def retrieve(self):
+
+        self.cdsapi.retrieve(
+            self.archive, {
+                'variable': self.variable,
+                'year': self.year,
+                'month': self.month,
+                'area': self.area,
+                'format': self.format,
+                'product_type': self.product_type,
+                'time_aggregation': self.time_aggregation,
+                          },
+                self.outputfile)
+
+    def checktar(self):
+        is_grib = False
+        with open(self.outputfile, 'rb') as ofile:
+            is_grib = ofile.read(4)
+        if (is_grib == b'GRIB' and self.format == 'tgz'):
+            # we create a tgz to be consistent
+            newfilename = tempfile.NamedTemporaryFile()
+            gribfile = os.path.basename(newfilename.name) + '.grib'
+            shutil.copyfile(self.outputfile, gribfile)
+            newfilename.close()
+            tar = tarfile.open(self.outputfile, 'w:gz')
+            tar.add(gribfile)
+            tar.close()
+
+
+if __name__ == '__main__':
+    warnings.filterwarnings("ignore")
+    parser = argparse.ArgumentParser()
+
+    remove_apikey = False
+    current_pwd = os.environ['HOME']
+    if 'GALAXY_COPERNICUS_CDSAPIRC_KEY' in os.environ and \
+       not os.path.isfile('.cdsapirc'):
+        with open(".cdsapirc", "w+") as apikey:
+            apikey.write("url: https://cds.climate.copernicus.eu/api/v2\n")
+            apikey.write(
+                  "key: " + os.environ['GALAXY_COPERNICUS_CDSAPIRC_KEY'])
+            remove_apikey = True
+
+    parser.add_argument(
+        'archive',
+        help='Archive name'
+    )
+    parser.add_argument(
+        'variable',
+        help='Specify which variable to retrieve'
+    )
+    parser.add_argument(
+        '--product_type',
+        help='Type of product (climatology or anomaly)'
+    )
+    parser.add_argument(
+        '--year',
+        help='Year(s) to retrieve.'
+    )
+    parser.add_argument(
+        '--month',
+        help='List of months to retrieve.'
+    )
+    parser.add_argument(
+        '--time_aggregation',
+        help='Time range over which data is aggregated (monthly/yearly).'
+    )
+    parser.add_argument(
+        '--area',
+        help='Desired sub-area to extract (North/West/South/East)'
+    )
+    parser.add_argument(
+        '--format',
+        help='Output file format (GRIB or netCDF or tgz)'
+    )
+    parser.add_argument(
+        '--output',
+        help='output filename'
+    )
+    parser.add_argument(
+        "-v", "--verbose",
+        help="switch on verbose mode",
+        action="store_true")
+    args = parser.parse_args()
+
+    p = ECV(args.archive, args.variable, args.product_type,
+            args.year, args.month, args.time_aggregation, args.area,
+            args.format, args.output, args.verbose)
+    p.retrieve()
+    p.checktar()
+    # remove api key file if it was created
+    if remove_apikey and os.getcwd() == current_pwd:
+        os.remove(os.path.join(current_pwd, '.cdsapirc'))
b
diff -r 000000000000 -r 5c0ab9932311 essential_climate_variables.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/essential_climate_variables.xml Fri May 03 15:11:01 2019 -0400
[
b'@@ -0,0 +1,167 @@\n+<tool id="cds_essential_variability" name="Copernicus Essential Climate Variables" version="0.1.4">\n+    <description>for assessing climate variability</description>\n+    <requirements>\n+        <requirement type="package" version="3">python</requirement>\n+        <requirement type="package" version="0.1.4">cdsapi</requirement>\n+        <requirement type="package" version="1.9.6">cdo</requirement>\n+    </requirements>\n+    <command detect_errors="exit_code"><![CDATA[\n+       HOME=`pwd`  &&\n+       python3 \'$__tool_directory__/ecv_retrieve.py\'\n+            \'ecv-for-climate-change\' \'$variable\'\n+            --year \'$year\'\n+        #if str($time_aggregation.selector) == \'1_month\'\n+            --month \'$time_aggregation.month\'\n+        #else\n+            --month \'01\'\n+        #end if\n+           --time_aggregation \'$time_aggregation.selector\'\n+           --product_type \'$product_type\'\n+           --format \'tgz\' --output \'tmp.tgz\'\n+           --verbose                                       &&\n+           tar zxvf tmp.tgz                                &&\n+           cat *.grib > tmp.grib                           &&\n+           cdo -f nc -t ecmwf copy tmp.grib tmp.nc\n+    ]]></command>\n+    <inputs>\n+        <param name="variable" type="select" multiple="true" label="Variable(s)">\n+            <option value="surface_air_temperature" selected="true">Surface air temperature</option>\n+            <option value="surface_air_relative_humidity" selected="true">Surface air relative humidity</option>\n+            <option value="0_7cm_volumetric_soil_moisture" selected="true">0-7cm volumetric soil moisture</option>\n+            <option value="precipitation" selected="true">Precipitation</option>\n+            <option value="sea_ice" selected="true">Sea-ice</option>\n+        </param>\n+        <param name="product_type" type="select" label="Select type of data">\n+            <option value="climatology" selected="true">Climatology</option>\n+            <option value="anomaly">Anomaly</option>\n+        </param>\n+        <conditional name="time_aggregation">\n+            <param name="selector" type="select" label="Time aggregation">\n+                <option value="12_month" selected="true">yearly</option>\n+                <option value="1_month">monthly</option>\n+            </param>\n+            <when value="1_month">\n+                <param name="month" type="select" multiple="true" label="Select month(s)">\n+                    <option value="01" selected="true">January</option>\n+                    <option value="02" selected="true">February</option>\n+                    <option value="03" selected="true">March</option>\n+                    <option value="04" selected="true">April</option>\n+                    <option value="05" selected="true">May</option>\n+                    <option value="06" selected="true">June</option>\n+                    <option value="07" selected="true">July</option>\n+                    <option value="08" selected="true">August</option>\n+                    <option value="09" selected="true">September</option>\n+                    <option value="10" selected="true">October</option>\n+                    <option value="11" selected="true">November</option>\n+                    <option value="12" selected="true">December</option>\n+                </param>\n+            </when>\n+        </conditional>\n+\n+        <param name="year" type="select" multiple="true"  label="Select year(s)">\n+            <option value="1979">1979</option>\n+            <option value="1980">1980</option>\n+            <option value="1981">1981</option>\n+            <option value="1982">1982</option>\n+            <option value="1983">1983</option>\n+            <option value="1984">1984</option>\n+            <option value="1985">1985</option>\n+            <option value="1986">1986</option>\n+            <option value="1987">1987</option>\n+            <option value="1988">1988</option>\n+            <option value="1989">1989</option'..b'n>\n+            <option value="2003">2003</option>\n+            <option value="2004">2004</option>\n+            <option value="2005">2005</option>\n+            <option value="2006">2006</option>\n+            <option value="2007">2007</option>\n+            <option value="2008">2008</option>\n+            <option value="2009">2009</option>\n+            <option value="2010">2010</option>\n+            <option value="2011">2011</option>\n+            <option value="2012">2012</option>\n+            <option value="2013">2013</option>\n+            <option value="2014">2014</option>\n+            <option value="2015">2015</option>\n+            <option value="2016">2016</option>\n+            <option value="2017">2017</option>\n+            <option value="2018">2018</option>\n+            <option value="2019" selected="true">2019</option>\n+        </param>\n+    </inputs>\n+    <outputs>\n+        <data name="ofilename" format="netcdf" from_work_dir="tmp.nc"/>\n+    </outputs>\n+    <tests>\n+        <test>\n+            <param name="variable" value="0_7cm_volumetric_soil_moisture"/>\n+            <param name="product_type" value="climatology"/>\n+            <param name="time_aggregation" value="1_month"/>\n+            <param name="month" value="12"/>\n+            <param name="year" value="2018"/>\n+            <output name="ofilename" ftype="netcdf" file="soil_moisture.nc" compare="sim_size" delta="100"/>\n+        </test>\n+        <test>\n+            <param name="variable" value="precipitation,sea_ice"/>\n+            <param name="product_type" value="anomaly"/>\n+            <param name="time_aggregation" value="12_month"/>\n+            <param name="year" value="2017"/>\n+            <output name="ofilename" ftype="netcdf" file="prep_seaice.nc" compare="sim_size" delta="100"/>\n+        </test>\n+    </tests>\n+    <help><![CDATA[\n+\n+**Copernicus Essential climate variables for assessment of climate variability from 1979 to present**\n+=======================================================================================================\n+\n+This tool is a wrapper to retrieve Copernicus Essential Climate Variables.\n+It corresponds to the European Contribution to the Monitoring of Essential Climate Variables from Space.\n+An Essential Climate Variable is a physical, chemical or biological variable or a group of linked variables\n+that critically contributes to the characterization of Earth\xe2\x80\x99 s climate.\n+\n+The Essential Climate Variables for assessment of climate variability from 1979 to present dataset contains\n+a selection of climatologies and monthly anomalies of Essential Climate Variables (ECVs) suitable for\n+monitoring and assessment of climate variability and change. Selection criteria are based on accuracy\n+and temporal consistency on monthly to decadal time scales. The ECV data products in this set have been\n+estimated from multiple sources and, depending on the source, may have been adjusted to account for biases\n+and other known deficiencies. Data sources and adjustment methods used are described in the Product User\n+Guide, as are various particulars such as the baseline periods used to calculate monthly climatologies and\n+the corresponding anomalies.\n+\n+\n+- ECV Factsheets: https://gcos.wmo.int/en/essential-climate-variables/ecv-factsheets\n+- Copernicus Climate Data Store documentation on Essential Climate Variables:\n+  https://cds.climate.copernicus.eu/cdsapp#!/dataset/ecv-for-climate-change?tab=overview\n+\n+  See climatedata.wmo.int/ for more information on Essential Climate Variables.\n+\n+.. class:: infomark\n+\n+   It allows to retrieve estimates of Essential Climate Variables (ECVs)\n+   derived from Earth observations.\n+\n+License:\n+~~~~~~~~\n+   Copernicus License V1.1\n+\n+   Generated using Copernicus Climate Change Service information [2019].\n+   Neither the European Commission nor ECMWF is responsible for any use \n+   that may be made of the Copernicus information or data it contains.\n+\n+    ]]></help>\n+    <citations>\n+    </citations>\n+</tool>\n'
b
diff -r 000000000000 -r 5c0ab9932311 test-data/prep_seaice.nc
b
Binary file test-data/prep_seaice.nc has changed
b
diff -r 000000000000 -r 5c0ab9932311 test-data/soil_moisture.nc
b
Binary file test-data/soil_moisture.nc has changed