Mercurial > repos > climate > climate_stripes
comparison climate_stripes.py @ 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fec3147b0206 |
---|---|
1 #!/usr/bin/env python3 | |
2 # | |
3 # | |
4 # usage: climate_stripes.py [-h] [--cmap CMAP] | |
5 # [--output OUTPUT] | |
6 # [--xname XNAME] | |
7 # [--format_plot FORMAT_PLOT] | |
8 # [--format_date FORMAT_DATE] | |
9 # [--title TITLE] | |
10 # [--nxsplit NXSPLIT] | |
11 # input varname | |
12 # | |
13 # positional arguments: | |
14 # input input filename with timeseries (tabular format) | |
15 # varname column name to use for plotting (case sensitive) | |
16 # | |
17 # optional arguments: | |
18 # -h, --help show this help message and exit | |
19 # --cmap CMAP Specify which colormap to use for plotting | |
20 # --output OUTPUT output filename to store resulting image (png format) | |
21 # --xname XNAME column name to use for x-axis | |
22 # --format_plot FORMAT_PLOT | |
23 # format for plotting dates on the x-axis | |
24 # --format_date FORMAT_DATE | |
25 # format for input date/time column | |
26 # --title TITLE plot title | |
27 # --nxsplit NXSPLIT number of ticks on the x-axis | |
28 # | |
29 | |
30 import argparse | |
31 import warnings | |
32 | |
33 import matplotlib as mpl | |
34 mpl.use('Agg') | |
35 | |
36 import matplotlib.pyplot as plt # noqa: I202,E402 | |
37 | |
38 import numpy as np # noqa: I202,E402 | |
39 | |
40 import pandas as pd # noqa: I202,E402 | |
41 | |
42 | |
43 class Stripes (): | |
44 def __init__(self, input, valname, cmap, output, xname="", | |
45 date_format='%Y%m', | |
46 plot_format='%Y', | |
47 nxsplit=10, | |
48 title=""): | |
49 self.input = input | |
50 self.valname = valname | |
51 self.xname = xname | |
52 if not nxsplit: | |
53 self.nxsplit = 10 | |
54 else: | |
55 self.nxsplit = nxsplit | |
56 if not cmap: | |
57 self.cmap = 'RdBu_r' | |
58 else: | |
59 self.cmap = cmap | |
60 if not output: | |
61 self.output = "stripes.png" | |
62 else: | |
63 self.output = output | |
64 self.title = title | |
65 if not date_format: | |
66 self.format = '%Y%m' | |
67 else: | |
68 self.format = date_format.replace('X', '%') | |
69 if not plot_format: | |
70 self.plot_format = self.format | |
71 else: | |
72 self.plot_format = plot_format.replace('X', '%') | |
73 | |
74 def read_data(self): | |
75 self.data = pd.read_csv(self.input, sep='\t') | |
76 | |
77 def create_stripes(self): | |
78 data = np.zeros((2, self.data[self.valname].shape[0]), dtype='float') | |
79 data[:] = np.NaN | |
80 data[0, :] = self.data[self.valname] | |
81 data[1, :] = self.data[self.valname] | |
82 fig = plt.figure(figsize=(10, 2)) | |
83 ax = plt.subplot(111) | |
84 plt.pcolor(data, cmap=self.cmap, | |
85 vmin=self.data[self.valname].min(), | |
86 vmax=self.data[self.valname].max()) | |
87 if self.title: | |
88 plt.title(self.title) | |
89 if self.xname: | |
90 nrange = self.data.index.values | |
91 n = int(np.floor((nrange.max() - nrange.min())/int(self.nxsplit))) | |
92 date_list = self.data[self.xname].loc[::n].apply( | |
93 lambda x: pd.to_datetime(str(x), | |
94 format=self.format)) | |
95 date_list = [i.strftime(self.plot_format) for i in date_list] | |
96 nval = int(self.data[self.xname].loc[::n].shape[0]) | |
97 ax.xaxis.set_major_locator(plt.MaxNLocator(nval)) | |
98 ax.xaxis.set_ticklabels(date_list) | |
99 ax.xaxis.set_tick_params(rotation=45) | |
100 else: | |
101 ax.set_xticks([]) | |
102 ax.set_yticks([]) | |
103 fig.tight_layout() | |
104 fig.savefig(self.output) | |
105 | |
106 | |
107 if __name__ == '__main__': | |
108 warnings.filterwarnings("ignore") | |
109 parser = argparse.ArgumentParser() | |
110 parser.add_argument( | |
111 'input', | |
112 help='input filename with geographical coordinates (netCDF format)' | |
113 ) | |
114 parser.add_argument( | |
115 'varname', | |
116 help='column name to use for plotting (case sensitive)' | |
117 ) | |
118 parser.add_argument( | |
119 '--cmap', | |
120 help='Specify which colormap to use for plotting' | |
121 ) | |
122 parser.add_argument( | |
123 '--output', | |
124 help='output filename to store resulting image (png format)' | |
125 ) | |
126 parser.add_argument( | |
127 '--xname', | |
128 help='column name to use for x-axis' | |
129 ) | |
130 parser.add_argument( | |
131 '--format_plot', | |
132 help='format for plotting dates on the x-axis' | |
133 ) | |
134 parser.add_argument( | |
135 '--format_date', | |
136 help='format for input date/time column (default is Month d, yyyy)' | |
137 ) | |
138 parser.add_argument( | |
139 '--title', | |
140 help='plot title' | |
141 ) | |
142 parser.add_argument( | |
143 '--nxsplit', | |
144 help='number of ticks on the x-axis' | |
145 ) | |
146 args = parser.parse_args() | |
147 stripes = Stripes(args.input, args.varname, args.cmap, args.output, | |
148 xname=args.xname, date_format=args.format_date, | |
149 plot_format=args.format_plot, title=args.title, | |
150 nxsplit=args.nxsplit) | |
151 stripes.read_data() | |
152 stripes.create_stripes() |