changeset 0:fec3147b0206 draft

"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/climate-stripes commit aa08ea46712c1cfbaac2e6fcd07c93097751cad3"
author climate
date Sat, 05 Oct 2019 17:03:08 -0400
parents
children c6f2435d680b
files climate-stripes.xml climate_stripes.py test-data/T2Mclimatology.txt test-data/T2Mstripes.png test-data/T2Mstripes_with_axis.png
diffstat 5 files changed, 803 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/climate-stripes.xml	Sat Oct 05 17:03:08 2019 -0400
@@ -0,0 +1,162 @@
+<tool id="climate_stripes" name="climate stripes" version="1.0.0">
+    <description>from timeseries</description>
+    <requirements>
+        <requirement type="package" version="3">python</requirement>
+        <requirement type="package" version="3.1.1">matplotlib</requirement>
+        <requirement type="package" version="0.25.1">pandas</requirement>
+    </requirements>
+    <command detect_errors="exit_code"><![CDATA[
+       python3 '$__tool_directory__/climate_stripes.py'
+            '$ifilename' '$variable'
+            --cmap '$adv.colormap'
+            #if str($title).strip()
+                --title '$title'
+            #end if
+            #if str($adv.xname).strip()
+                --xname '$adv.xname'
+            #end if
+            #if str($adv.format_date).strip()
+                --format_date '$adv.format_date'
+            #end if
+            #if str($adv.format_plot).strip()
+                --format_plot '$adv.format_plot'
+            #end if
+            --output image.png
+    ]]></command>
+    <inputs>
+        <param name="ifilename" type="data" format="tabular" label="timeseries to plot"></param>
+        <param name="variable" type="text" value="global" label="column name to use for plotting" />
+        <param name="title" type="text" value="" label="plot title" />
+        <section name="adv" title="Advanced Options" expanded="false">
+            <param name="xname" type="text" value="" label="column name to use for x-axis" />
+            <param name="format_date" type="text" value="" label="format for input date/time column" />
+            <param name="format_plot" type="text" value="" label="format for plotting dates on the x-axis" />
+            <param name="colormap" type="select">
+                <option value="Accent">Accent</option>
+                <option value="Blues">Blues</option>
+                <option value="BrBG">BrBG</option>
+                <option value="BuGn">BuGn</option>
+                <option value="BuPu">BuPu</option>
+                <option value="CMRmap">CMRmap</option>
+                <option value="Dark2">Dark2</option>
+                <option value="GnBu">GnBu</option>
+                <option value="Greens">Greens</option>
+                <option value="Greys">Greys</option>
+                <option value="OrRd">OrRd</option>
+                <option value="Oranges">Oranges</option>
+                <option value="PRGn">PRGn</option>
+                <option value="Paired">Paired</option>
+                <option value="Pastel1">Pastel1</option>
+                <option value="Pastel2">Pastel2</option>
+                <option value="PiYG">PiYG</option>
+                <option value="PuBu">PuBu</option>
+                <option value="PuBuGn">PuBuGn</option>
+                <option value="PuOr">PuOr</option>
+                <option value="PuRd">PuRd</option>
+                <option value="Purples">Purples</option>
+                <option value="RdBu">RdBu</option>
+                <option value="RdGy">RdGy</option>
+                <option value="RdPu">RdPu</option>
+                <option value="RdBu_r" selected="true">RdBu_r</option>
+                <option value="RdGy_r">RdGy_r</option>
+                <option value="RdPu_r">RdPu_r</option>
+                <option value="RdYlBu">RdYlBu</option>
+                <option value="RdYlGn">RdYlGn</option>
+                <option value="Reds">Reds</option>
+                <option value="Set1">Set1</option>
+                <option value="Set2">Set2</option>
+                <option value="Set3">Set3</option>
+                <option value="Spectral">Spectral</option>
+                <option value="Wistia">Wistia</option>
+                <option value="YlGn">YlGn</option>
+                <option value="YlGnBu">YlGnBu</option>
+                <option value="YlOrBr">YlOrBr</option>
+                <option value="YlOrRd">YlOrRd</option>
+                <option value="afmhot">afmhot</option>
+                <option value="autumn">autumn</option>
+                <option value="binary">binary</option>
+                <option value="bone">bone</option>
+                <option value="brg">brg</option>
+                <option value="bwr">bwr</option>
+                <option value="cool">cool</option>
+                <option value="coolwarm">coolwarm</option>
+                <option value="copper">copper</option>
+                <option value="cubehelix">cubehelix</option>
+                <option value="flag">flag</option>
+                <option value="gist_earth">gist_earth</option>
+                <option value="gist_gray">gist_gray</option>
+                <option value="gist_heat">gist_heat</option>
+                <option value="gist_ncar">gist_ncar</option>
+                <option value="gist_rainbow">gist_rainbow</option>
+                <option value="gist_stern">gist_stern</option>
+                <option value="gist_yarg">gist_yarg</option>
+                <option value="gnuplot">gnuplot</option>
+                <option value="gnuplot2">gnuplot2</option>
+                <option value="gray">gray</option>
+                <option value="hot">hot</option>
+                <option value="hsv">hsv</option>
+                <option value="jet">jet</option>
+                <option value="nipy_spectral">nipy_spectral</option>
+                <option value="ocean">ocean</option>
+                <option value="pink">pink</option>
+                <option value="prism">prism</option>
+                <option value="rainbow">rainbow</option>
+                <option value="seismic">seismic</option>
+                <option value="spring">spring</option>
+                <option value="summer">summer</option>
+                <option value="tab10">tab10</option>
+                <option value="tab20">tab20</option>
+                <option value="tab20b">tab20b</option>
+                <option value="tab20c">tab20c</option>
+                <option value="terrain">terrain</option>
+                <option value="winter">winter</option>
+            </param>
+        </section>
+    </inputs>
+    <outputs>
+        <data name="ofilename" format="png" from_work_dir="image.png"/>
+    </outputs>
+    <tests>
+        <test>
+            <param name="ifilename" value="T2Mclimatology.txt" />
+            <param name="variable" value="global" />
+            <output name="ofilename" ftype="png" file="T2Mstripes.png" compare="sim_size" delta="50"/>
+        </test>
+        <test>
+            <param name="ifilename" value="T2Mclimatology.txt" />
+            <param name="variable" value="global" />
+            <param name="title" value="Surface Temperature stripes (1979-2019)" />
+            <param name="colormap" value="OrRd" />
+            <param name="xname" value="Month" />
+            <param name="format_date" value="XYXm" />
+            <param name="format_plot" value="XY" />
+            <output name="ofilename" ftype="png" file="T2Mstripes_with_axis.png" compare="sim_size" delta="50"/>
+        </test>
+    </tests>
+    <help><![CDATA[
+
+**Climate stripes**
+=========================================
+
+This tool generate stripes from timeseries and is often used to generate warming stripes.
+
+
+.. class:: infomark
+
+        The wrappers aims at creating stripes from a timeseries.
+        The input file must be in tabular format and must contain at least one column that is used
+        for creating stripes. By default, no title and no axis are plotted. An additional column
+        can be specified for date/time and its date and time format must then be specified
+        with an additional options.
+
+**What it does**
+----------------
+
+This tools creates an image (png format) corresponding to the visualization of a timeseries as
+stripes (see https://www.climate-lab-book.ac.uk/2018/warming-stripes/).
+By default, the colormap is ``RdBu_r`` and no axis nor title are plotted. These settings can be
+changed in *Advanced settings*.
+    ]]></help>
+    <citations>
+    </citations>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/climate_stripes.py	Sat Oct 05 17:03:08 2019 -0400
@@ -0,0 +1,152 @@
+#!/usr/bin/env python3
+#
+#
+# usage: climate_stripes.py [-h] [--cmap CMAP]
+#                           [--output OUTPUT]
+#                           [--xname XNAME]
+#                           [--format_plot FORMAT_PLOT]
+#                           [--format_date FORMAT_DATE]
+#                           [--title TITLE]
+#                           [--nxsplit NXSPLIT]
+#                          input varname
+#
+# positional arguments:
+#   input                 input filename with timeseries (tabular format)
+#   varname               column name to use for plotting (case sensitive)
+#
+# optional arguments:
+#   -h, --help            show this help message and exit
+#   --cmap CMAP           Specify which colormap to use for plotting
+#   --output OUTPUT       output filename to store resulting image (png format)
+#   --xname XNAME         column name to use for x-axis
+#   --format_plot FORMAT_PLOT
+#                         format for plotting dates on the x-axis
+#   --format_date FORMAT_DATE
+#                         format for input date/time column
+#   --title TITLE         plot title
+#   --nxsplit NXSPLIT     number of ticks on the x-axis
+#
+
+import argparse
+import warnings
+
+import matplotlib as mpl
+mpl.use('Agg')
+
+import matplotlib.pyplot as plt   # noqa: I202,E402
+
+import numpy as np  # noqa: I202,E402
+
+import pandas as pd  # noqa: I202,E402
+
+
+class Stripes ():
+    def __init__(self, input, valname, cmap, output, xname="",
+                 date_format='%Y%m',
+                 plot_format='%Y',
+                 nxsplit=10,
+                 title=""):
+        self.input = input
+        self.valname = valname
+        self.xname = xname
+        if not nxsplit:
+            self.nxsplit = 10
+        else:
+            self.nxsplit = nxsplit
+        if not cmap:
+            self.cmap = 'RdBu_r'
+        else:
+            self.cmap = cmap
+        if not output:
+            self.output = "stripes.png"
+        else:
+            self.output = output
+        self.title = title
+        if not date_format:
+            self.format = '%Y%m'
+        else:
+            self.format = date_format.replace('X', '%')
+        if not plot_format:
+            self.plot_format = self.format
+        else:
+            self.plot_format = plot_format.replace('X', '%')
+
+    def read_data(self):
+        self.data = pd.read_csv(self.input, sep='\t')
+
+    def create_stripes(self):
+        data = np.zeros((2, self.data[self.valname].shape[0]), dtype='float')
+        data[:] = np.NaN
+        data[0, :] = self.data[self.valname]
+        data[1, :] = self.data[self.valname]
+        fig = plt.figure(figsize=(10, 2))
+        ax = plt.subplot(111)
+        plt.pcolor(data, cmap=self.cmap,
+                   vmin=self.data[self.valname].min(),
+                   vmax=self.data[self.valname].max())
+        if self.title:
+            plt.title(self.title)
+        if self.xname:
+            nrange = self.data.index.values
+            n = int(np.floor((nrange.max() - nrange.min())/int(self.nxsplit)))
+            date_list = self.data[self.xname].loc[::n].apply(
+                             lambda x: pd.to_datetime(str(x),
+                                                      format=self.format))
+            date_list = [i.strftime(self.plot_format) for i in date_list]
+            nval = int(self.data[self.xname].loc[::n].shape[0])
+            ax.xaxis.set_major_locator(plt.MaxNLocator(nval))
+            ax.xaxis.set_ticklabels(date_list)
+            ax.xaxis.set_tick_params(rotation=45)
+        else:
+            ax.set_xticks([])
+        ax.set_yticks([])
+        fig.tight_layout()
+        fig.savefig(self.output)
+
+
+if __name__ == '__main__':
+    warnings.filterwarnings("ignore")
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        'input',
+        help='input filename with geographical coordinates (netCDF format)'
+    )
+    parser.add_argument(
+        'varname',
+        help='column name to use for plotting (case sensitive)'
+    )
+    parser.add_argument(
+        '--cmap',
+        help='Specify which colormap to use for plotting'
+    )
+    parser.add_argument(
+        '--output',
+        help='output filename to store resulting image (png format)'
+    )
+    parser.add_argument(
+        '--xname',
+        help='column name to use for x-axis'
+    )
+    parser.add_argument(
+        '--format_plot',
+        help='format for plotting dates on the x-axis'
+    )
+    parser.add_argument(
+        '--format_date',
+        help='format for input date/time column (default is Month d, yyyy)'
+    )
+    parser.add_argument(
+        '--title',
+        help='plot title'
+    )
+    parser.add_argument(
+        '--nxsplit',
+        help='number of ticks on the x-axis'
+    )
+    args = parser.parse_args()
+    stripes = Stripes(args.input, args.varname, args.cmap, args.output,
+                      xname=args.xname, date_format=args.format_date,
+                      plot_format=args.format_plot, title=args.title,
+                      nxsplit=args.nxsplit)
+    stripes.read_data()
+    stripes.create_stripes()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/T2Mclimatology.txt	Sat Oct 05 17:03:08 2019 -0400
@@ -0,0 +1,489 @@
+Month	global
+197901	-0.2309
+197902	-0.4906
+197903	-0.2922
+197904	-0.3259
+197905	-0.2858
+197906	-0.258
+197907	-0.3306
+197908	-0.2336
+197909	-0.1801
+197910	-0.1248
+197911	-0.147
+197912	0.0667
+198001	-0.1084
+198002	-0.0513
+198003	-0.0826
+198004	-0.0015
+198005	0.0697
+198006	-0.0799
+198007	-0.0425
+198008	-0.1085
+198009	-0.1715
+198010	-0.216
+198011	-0.192
+198012	-0.2736
+198101	0.0171
+198102	-0.0524
+198103	0.0305
+198104	-0.0305
+198105	-0.069
+198106	-0.0755
+198107	-0.0747
+198108	-0.0603
+198109	-0.1398
+198110	-0.1792
+198111	-0.1878
+198112	-0.0359
+198201	-0.3861
+198202	-0.2909
+198203	-0.4431
+198204	-0.3551
+198205	-0.2111
+198206	-0.2463
+198207	-0.1711
+198208	-0.2779
+198209	-0.2693
+198210	-0.2803
+198211	-0.2834
+198212	-0.0342
+198301	0.0396
+198302	0.004
+198303	0.006
+198304	-0.0941
+198305	0.0277
+198306	-0.092
+198307	-0.1496
+198308	-0.0738
+198309	-0.0007
+198310	-0.2356
+198311	-0.1167
+198312	-0.2251
+198401	-0.2111
+198402	-0.3605
+198403	-0.1985
+198404	-0.2865
+198405	-0.0539
+198406	-0.3321
+198407	-0.3649
+198408	-0.2626
+198409	-0.2898
+198410	-0.2823
+198411	-0.4153
+198412	-0.4044
+198501	-0.2315
+198502	-0.4976
+198503	-0.3365
+198504	-0.2901
+198505	-0.276
+198506	-0.2926
+198507	-0.3687
+198508	-0.2183
+198509	-0.3246
+198510	-0.4071
+198511	-0.3566
+198512	-0.3469
+198601	-0.2416
+198602	-0.1812
+198603	-0.2186
+198604	-0.1174
+198605	-0.1084
+198606	-0.2742
+198607	-0.2816
+198608	-0.2613
+198609	-0.3307
+198610	-0.3542
+198611	-0.264
+198612	-0.2482
+198701	-0.1421
+198702	-0.092
+198703	-0.2871
+198704	-0.1516
+198705	-0.1118
+198706	-0.0174
+198707	0.0778
+198708	-0.0949
+198709	-0.0279
+198710	-0.1066
+198711	-0.1359
+198712	0.0729
+198801	0.0599
+198802	-0.136
+198803	0.0022
+198804	-0.0064
+198805	0.0585
+198806	0.017
+198807	-0.0303
+198808	0.0094
+198809	0.0089
+198810	-0.1119
+198811	-0.3316
+198812	-0.1708
+198901	-0.4052
+198902	-0.1396
+198903	-0.1631
+198904	-0.1324
+198905	-0.2464
+198906	-0.2324
+198907	-0.0708
+198908	-0.1457
+198909	-0.0867
+198910	-0.1352
+198911	-0.2527
+198912	-0.041
+199001	-0.0391
+199002	-0.0777
+199003	0.2671
+199004	0.1302
+199005	0.1443
+199006	-0.0106
+199007	0.0889
+199008	0.0098
+199009	-0.0343
+199010	0.0814
+199011	0.1168
+199012	0.0494
+199101	0.0252
+199102	0.0746
+199103	-0.0587
+199104	0.0598
+199105	-0.0158
+199106	0.2017
+199107	0.1192
+199108	0.0362
+199109	-0.0056
+199110	-0.1087
+199111	-0.1209
+199112	-0.1381
+199201	-0.0193
+199202	-0.0535
+199203	-0.0529
+199204	-0.237
+199205	-0.1777
+199206	-0.1489
+199207	-0.2948
+199208	-0.3124
+199209	-0.4129
+199210	-0.3618
+199211	-0.3805
+199212	-0.2644
+199301	-0.1167
+199302	-0.1113
+199303	-0.1695
+199304	-0.1434
+199305	-0.1343
+199306	-0.1288
+199307	-0.1836
+199308	-0.2087
+199309	-0.3204
+199310	-0.1906
+199311	-0.3052
+199312	-0.154
+199401	-0.2047
+199402	-0.4391
+199403	-0.2242
+199404	-0.0979
+199405	-0.1875
+199406	-0.0802
+199407	-0.1083
+199408	-0.2086
+199409	-0.0425
+199410	-0.0524
+199411	-0.012
+199412	-0.0731
+199501	0.0619
+199502	0.2626
+199503	-0.0415
+199504	0.0632
+199505	-0.0742
+199506	0.0877
+199507	0.0624
+199508	0.0688
+199509	-0.0044
+199510	0.0447
+199511	0.0131
+199512	-0.1416
+199601	-0.1686
+199602	-0.0248
+199603	-0.1161
+199604	-0.1619
+199605	-0.2063
+199606	-0.142
+199607	-0.04
+199608	0.0642
+199609	-0.1313
+199610	-0.2007
+199611	0.0178
+199612	-0.0471
+199701	-0.1784
+199702	-0.1172
+199703	0.024
+199704	-0.1454
+199705	-0.0915
+199706	0.0938
+199707	-0.0057
+199708	0.0417
+199709	0.1316
+199710	0.1868
+199711	0.2191
+199712	0.151
+199801	0.143
+199802	0.3514
+199803	0.1589
+199804	0.3034
+199805	0.3391
+199806	0.357
+199807	0.2881
+199808	0.3172
+199809	0.1434
+199810	0.062
+199811	-0.0017
+199812	0.1115
+199901	-0.036
+199902	0.1493
+199903	-0.1965
+199904	-0.121
+199905	-0.1364
+199906	-0.0584
+199907	-0.0356
+199908	-0.0618
+199909	-0.0044
+199910	-0.0617
+199911	-0.0657
+199912	-0.0247
+200001	-0.1986
+200002	0.0608
+200003	-0.0243
+200004	0.0999
+200005	-0.0597
+200006	-0.0701
+200007	-0.0714
+200008	0.0007
+200009	-0.0425
+200010	-0.1561
+200011	-0.1091
+200012	-0.1434
+200101	-0.0388
+200102	-0.0855
+200103	0.0351
+200104	0.0292
+200105	0.1151
+200106	0.0628
+200107	0.1458
+200108	0.1248
+200109	0.1181
+200110	0.1336
+200111	0.3371
+200112	0.229
+200201	0.2862
+200202	0.1891
+200203	0.3853
+200204	0.1189
+200205	0.2409
+200206	0.197
+200207	0.2003
+200208	0.1291
+200209	0.2015
+200210	0.1587
+200211	0.1827
+200212	0.0767
+200301	0.2975
+200302	0.1098
+200303	0.0283
+200304	0.0625
+200305	0.1472
+200306	0.035
+200307	0.1191
+200308	0.2217
+200309	0.27
+200310	0.3437
+200311	0.1999
+200312	0.3762
+200401	0.1492
+200402	0.2243
+200403	0.1831
+200404	0.1682
+200405	-0.0494
+200406	0.0804
+200407	-0.1095
+200408	0.0141
+200409	0.1562
+200410	0.2464
+200411	0.3085
+200412	0.1032
+200501	0.3385
+200502	0.2033
+200503	0.2828
+200504	0.2531
+200505	0.2305
+200506	0.2503
+200507	0.2517
+200508	0.2053
+200509	0.3259
+200510	0.4115
+200511	0.3688
+200512	0.3
+200601	0.1472
+200602	0.2867
+200603	0.1829
+200604	0.1127
+200605	0.0892
+200606	0.255
+200607	0.1697
+200608	0.2467
+200609	0.2443
+200610	0.3775
+200611	0.3384
+200612	0.3942
+200701	0.5542
+200702	0.2852
+200703	0.195
+200704	0.3563
+200705	0.2448
+200706	0.15
+200707	0.1727
+200708	0.157
+200709	0.1064
+200710	0.2256
+200711	0.1986
+200712	0.1096
+200801	-0.1371
+200802	-0.1236
+200803	0.2131
+200804	0.0559
+200805	0.0557
+200806	-0.0246
+200807	0.1429
+200808	0.0967
+200809	0.1662
+200810	0.2767
+200811	0.2888
+200812	0.1549
+200901	0.2507
+200902	0.1625
+200903	0.0731
+200904	0.1225
+200905	0.1569
+200906	0.1782
+200907	0.304
+200908	0.2209
+200909	0.3
+200910	0.3196
+200911	0.3783
+200912	0.2669
+201001	0.3847
+201002	0.4194
+201003	0.4633
+201004	0.435
+201005	0.3593
+201006	0.2604
+201007	0.2182
+201008	0.2218
+201009	0.2953
+201010	0.356
+201011	0.3712
+201012	0.0971
+201101	0.1006
+201102	0.0213
+201103	0.1357
+201104	0.1962
+201105	0.1192
+201106	0.1943
+201107	0.3094
+201108	0.2865
+201109	0.2436
+201110	0.2692
+201111	0.1428
+201112	0.1609
+201201	0.0255
+201202	0.0183
+201203	0.0581
+201204	0.2369
+201205	0.3344
+201206	0.2933
+201207	0.2066
+201208	0.2521
+201209	0.3091
+201210	0.4587
+201211	0.4333
+201212	0.1667
+201301	0.3378
+201302	0.2129
+201303	0.2182
+201304	0.1461
+201305	0.1823
+201306	0.2813
+201307	0.1413
+201308	0.2304
+201309	0.3659
+201310	0.291
+201311	0.3871
+201312	0.3255
+201401	0.318
+201402	0.1015
+201403	0.2849
+201404	0.2921
+201405	0.3443
+201406	0.246
+201407	0.232
+201408	0.3116
+201409	0.3807
+201410	0.4032
+201411	0.2885
+201412	0.387
+201501	0.3835
+201502	0.396
+201503	0.4229
+201504	0.2391
+201505	0.3376
+201506	0.3601
+201507	0.32
+201508	0.4178
+201509	0.4631
+201510	0.6809
+201511	0.6232
+201512	0.738
+201601	0.742
+201602	0.885
+201603	0.8201
+201604	0.7121
+201605	0.5762
+201606	0.435
+201607	0.5268
+201608	0.5768
+201609	0.554
+201610	0.5812
+201611	0.6422
+201612	0.5341
+201701	0.5945
+201702	0.6967
+201703	0.6938
+201704	0.5108
+201705	0.5335
+201706	0.3757
+201707	0.4319
+201708	0.4715
+201709	0.4758
+201710	0.599
+201711	0.4853
+201712	0.5705
+201801	0.4317
+201802	0.4598
+201803	0.4674
+201804	0.4877
+201805	0.4128
+201806	0.3989
+201807	0.4446
+201808	0.384
+201809	0.3957
+201810	0.5942
+201811	0.4755
+201812	0.5296
+201901	0.4753
+201902	0.5056
+201903	0.702
+201904	0.6198
+201905	0.5177
+201906	0.5424
+201907	0.5631
+201908	0.5316
Binary file test-data/T2Mstripes.png has changed
Binary file test-data/T2Mstripes_with_axis.png has changed