Mercurial > repos > climate > psy_maps
comparison psymap_simple.py @ 1:706666d912d5 draft
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/psy-maps commit 4803d769c7b0b37fa80c69bd7327f6789cd3c6bf"
author | climate |
---|---|
date | Sat, 05 Oct 2019 17:11:18 -0400 |
parents | db8d76da4174 |
children | e6d1e9d6b399 |
comparison
equal
deleted
inserted
replaced
0:db8d76da4174 | 1:706666d912d5 |
---|---|
15 # optional arguments: | 15 # optional arguments: |
16 # -h, --help show this help message and exit | 16 # -h, --help show this help message and exit |
17 # --proj PROJ Specify the projection on which we draw | 17 # --proj PROJ Specify the projection on which we draw |
18 # --cmap CMAP Specify which colormap to use for plotting | 18 # --cmap CMAP Specify which colormap to use for plotting |
19 # --output OUTPUT output filename to store resulting image (png format) | 19 # --output OUTPUT output filename to store resulting image (png format) |
20 # --time TIMES time index from the file for multiple plots ("0 1 2 3") | |
21 # --nrow NROW number of rows for multiple plot grid | |
22 # --ncol NCOL number of columns for multiple plot grid | |
23 # --format date format such as %Y (for year) %B (for month), etc. | |
24 # --title plot or subplot title | |
20 # -v, --verbose switch on verbose mode | 25 # -v, --verbose switch on verbose mode |
21 # | 26 # |
22 | 27 |
23 import argparse | 28 import argparse |
24 import warnings | 29 import warnings |
27 import matplotlib as mpl | 32 import matplotlib as mpl |
28 mpl.use('Agg') | 33 mpl.use('Agg') |
29 from matplotlib import pyplot # noqa: I202,E402 | 34 from matplotlib import pyplot # noqa: I202,E402 |
30 | 35 |
31 import psyplot.project as psy # noqa: I202,E402 | 36 import psyplot.project as psy # noqa: I202,E402 |
37 from psyplot import rcParams # noqa: I202,E402 | |
32 | 38 |
33 | 39 |
34 class PsyPlot (): | 40 class PsyPlot (): |
35 def __init__(self, input, proj, varname, cmap, output, | 41 def __init__(self, input, proj, varname, cmap, output, verbose=False, |
36 verbose=False | 42 time=[], nrow=1, ncol=1, format="%B %e, %Y", |
37 ): | 43 title=""): |
38 self.input = input | 44 self.input = input |
39 self.proj = proj | 45 self.proj = proj |
40 self.varname = varname | 46 self.varname = varname |
41 self.cmap = cmap | 47 self.cmap = cmap |
48 self.time = time | |
49 if format is None: | |
50 self.format = "" | |
51 else: | |
52 self.format = format.replace('X', '%') | |
53 if title is None: | |
54 self.title = "" | |
55 else: | |
56 self.title = title | |
57 if ncol is None: | |
58 self.ncol = 1 | |
59 else: | |
60 self.ncol = int(ncol) | |
61 if nrow is None: | |
62 self.nrow = 1 | |
63 else: | |
64 self.nrow = int(nrow) | |
42 if output is None: | 65 if output is None: |
43 self.output = Path(input).stem + '.png' | 66 self.output = Path(input).stem + '.png' |
44 else: | 67 else: |
45 self.output = output | 68 self.output = output |
46 self.verbose = verbose | 69 self.verbose = verbose |
47 if verbose: | 70 if verbose: |
48 print("input: ", self.input) | 71 print("input: ", self.input) |
49 print("proj: ", self.proj) | 72 print("proj: ", self.proj) |
50 print("varname: ", self.varname) | 73 print("varname: ", self.varname) |
51 print("cmap: ", self.cmap) | 74 print("cmap: ", self.cmap) |
75 print("time: ", self.time) | |
76 print("ncol: ", self.ncol) | |
77 print("nrow: ", self.nrow) | |
78 print("title: ", self.title) | |
79 print("date format: ", self.format) | |
52 print("output: ", self.output) | 80 print("output: ", self.output) |
53 | 81 |
54 def plot(self): | 82 def plot(self): |
83 if self.title and self.format: | |
84 title = self.title + "\n" + self.format | |
85 elif not self.title and self.format: | |
86 title = self.format | |
87 elif self.title and not self.format: | |
88 title = self.title | |
89 else: | |
90 title = '%(long_name)s' | |
91 | |
55 if self.cmap is None and self.proj is None: | 92 if self.cmap is None and self.proj is None: |
56 print("op1") | 93 psy.plot.mapplot(self.input, name=self.varname, |
57 psy.plot.mapplot(self.input, name=self.varname, | 94 title=title, |
58 clabel='{desc}') | 95 clabel='{desc}') |
59 elif self.proj is None or self.proj == '': | 96 elif self.proj is None or not self.proj: |
60 print("op2") | 97 psy.plot.mapplot(self.input, name=self.varname, |
61 psy.plot.mapplot(self.input, name=self.varname, | 98 title=title, |
62 cmap=self.cmap, clabel='{desc}') | 99 cmap=self.cmap, clabel='{desc}') |
63 elif self.cmap is None or self.cmap == '': | 100 elif self.cmap is None or not self.cmap: |
64 print("op3") | |
65 psy.plot.mapplot(self.input, name=self.varname, | 101 psy.plot.mapplot(self.input, name=self.varname, |
66 projection=self.proj, | 102 projection=self.proj, |
103 title=title, | |
67 clabel='{desc}') | 104 clabel='{desc}') |
68 else: | 105 else: |
69 print("op4") | |
70 psy.plot.mapplot(self.input, name=self.varname, | 106 psy.plot.mapplot(self.input, name=self.varname, |
71 cmap=self.cmap, | 107 cmap=self.cmap, |
72 projection=self.proj, | 108 projection=self.proj, |
109 title=title, | |
73 clabel='{desc}') | 110 clabel='{desc}') |
74 | 111 |
75 pyplot.savefig(self.output) | 112 pyplot.savefig(self.output) |
76 | 113 |
77 | 114 def multiple_plot(self): |
78 def psymap_plot(input, proj, varname, cmap, output, verbose): | 115 if not self.format: |
116 self.format = "%B %e, %Y" | |
117 | |
118 if not self.title: | |
119 title = self.format | |
120 else: | |
121 title = self.title + "\n" + self.format | |
122 mpl.rcParams['figure.figsize'] = [20, 8] | |
123 mpl.rcParams.update({'font.size': 8}) | |
124 rcParams.update({'plotter.maps.grid_labelsize': 8.0}) | |
125 if self.cmap is None and self.proj is None: | |
126 m = psy.plot.mapplot(self.input, name=self.varname, | |
127 title=title, | |
128 ax=(self.nrow, self.ncol), | |
129 time=self.time, sort=['time'], | |
130 clabel='{desc}') | |
131 m.share(keys='bounds') | |
132 elif self.proj is None or not self.proj: | |
133 m = psy.plot.mapplot(self.input, name=self.varname, | |
134 title=title, | |
135 ax=(self.nrow, self.ncol), | |
136 time=self.time, sort=['time'], | |
137 cmap=self.cmap, clabel='{desc}') | |
138 m.share(keys='bounds') | |
139 elif self.cmap is None or not self.cmap: | |
140 m = psy.plot.mapplot(self.input, name=self.varname, | |
141 projection=self.proj, | |
142 ax=(self.nrow, self.ncol), | |
143 time=self.time, sort=['time'], | |
144 title=title, | |
145 clabel='{desc}') | |
146 m.share(keys='bounds') | |
147 else: | |
148 m = psy.plot.mapplot(self.input, name=self.varname, | |
149 cmap=self.cmap, | |
150 projection=self.proj, | |
151 ax=(self.nrow, self.ncol), | |
152 time=self.time, sort=['time'], | |
153 title=title, | |
154 clabel='{desc}') | |
155 m.share(keys='bounds') | |
156 | |
157 pyplot.savefig(self.output) | |
158 | |
159 | |
160 def psymap_plot(input, proj, varname, cmap, output, verbose, time, | |
161 nrow, ncol, format, title): | |
79 """Generate plot from input filename""" | 162 """Generate plot from input filename""" |
80 | 163 |
81 p = PsyPlot(input, proj, varname, cmap, output, verbose) | 164 p = PsyPlot(input, proj, varname, cmap, output, verbose, time, |
82 p.plot() | 165 nrow, ncol, format, title) |
166 if len(time) == 0: | |
167 p.plot() | |
168 else: | |
169 p.multiple_plot() | |
83 | 170 |
84 | 171 |
85 if __name__ == '__main__': | 172 if __name__ == '__main__': |
86 warnings.filterwarnings("ignore") | 173 warnings.filterwarnings("ignore") |
87 parser = argparse.ArgumentParser() | 174 parser = argparse.ArgumentParser() |
103 help='Specify which colormap to use for plotting' | 190 help='Specify which colormap to use for plotting' |
104 ) | 191 ) |
105 parser.add_argument( | 192 parser.add_argument( |
106 '--output', | 193 '--output', |
107 help='output filename to store resulting image (png format)' | 194 help='output filename to store resulting image (png format)' |
195 ) | |
196 parser.add_argument( | |
197 '--time', | |
198 help='list of times to plot for multiple plots' | |
199 ) | |
200 parser.add_argument( | |
201 '--format', | |
202 help='format for date/time (default is Month d, yyyy)' | |
203 ) | |
204 parser.add_argument( | |
205 '--title', | |
206 help='plot title' | |
207 ) | |
208 parser.add_argument( | |
209 '--ncol', | |
210 help='number of columns for multiple plots' | |
211 ) | |
212 parser.add_argument( | |
213 '--nrow', | |
214 help='number of rows for multiple plots' | |
108 ) | 215 ) |
109 parser.add_argument( | 216 parser.add_argument( |
110 "-v", "--verbose", | 217 "-v", "--verbose", |
111 help="switch on verbose mode", | 218 help="switch on verbose mode", |
112 action="store_true") | 219 action="store_true") |
113 args = parser.parse_args() | 220 args = parser.parse_args() |
114 | 221 |
222 if args.time is None: | |
223 time = [] | |
224 else: | |
225 time = list(map(int, args.time.split(","))) | |
115 psymap_plot(args.input, args.proj, args.varname, args.cmap, | 226 psymap_plot(args.input, args.proj, args.varname, args.cmap, |
116 args.output, args.verbose) | 227 args.output, args.verbose, time, |
228 args.nrow, args.ncol, args.format, args.title) |