-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhapiplot.py
More file actions
779 lines (641 loc) · 32.2 KB
/
hapiplot.py
File metadata and controls
779 lines (641 loc) · 32.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import os
import hashlib
import json
import numpy as np
from matplotlib import rc_context
from matplotlib import rcParams
from hapiclient.hapitime import hapitime2datetime
from hapiclient.hapi import request2path
from hapiclient.hapi import cachedir
from hapiclient.util import log, warning
from hapiplot.plot.timeseries import timeseries
from hapiplot.plot.heatmap import heatmap
from hapiplot.plot.util import setopts
def hapiplot(*args, **kwargs):
"""Plot response from HAPI server.
Version: 0.2.3b0
Demos
-----
<https://github.com/hapi-server/client-python/blob/master/hapiclient/plot/hapiplot_test.py>
Usage
-----
data, meta = hapiplot(server, dataset, params, start, stop, **kwargs)
or
meta = hapiplot(data, meta, **kwargs)
where data and meta are return values from `hapi()`.
All parameters are plotted. If a parameter has a bins attribute,
it is plotted using `heatmap()`. Otherwise, it is plotted using
`timeseries()`.
Returns
-------
`data` is the same as that returned from `hapi()`.
`meta` is the same as that returned from `hapi()` with the additon of
meta['parameters'][i]['hapiplot']['figure'] is a reference to the
figure (e.g., plt.gcf()). Usage example:
>>> fig = meta['parameters'][i]['hapiplot']['figure']
>>> fig.set_facecolor('blue')
>>> fig.axes[0].set_ylabel('new y-label')
>>> fig.axes[0].set_title('new title\\nsubtitle\\nsubtitle')
>>> fig.tight_layout()
meta['parameters'][i]['hapiplot']['colorbar'] is a reference to the
colorbar on the figure (if parameter plotted as a heatmap)
meta['parameters'][i]['hapiplot']['image'] is PNG, PDF, or SVG data
and is included only if `returnimage=True`. Usage example:
>>> img = meta['parameters'][i]['hapiplot']['image']
>>> Image.open(io.BytesIO(img)).show()
>>> # or
>>> f = open('/tmp/a.png', 'wb')
>>> f.write(img)
>>> f.close()
See Also
---------
`hapi()`: Get data from a HAPI server
`timeseries()`: Used by `hapiplot()` to HAPI parameters with no `bins`
`heatmap()`: Used by `hapiplot()` to HAPI parameters with `bins`
<https://github.com/hapi-server/client-python-notebooks>
kwargs
------
* logging: [False] Display console messages
* usecache: [True] Use cached data
* tsopts: {} kwargs for the `timeseries()` function
* hmopts: {} kwargs for the `heatmap()` function
Other kwargs
------------
* returnimage: [False] If True, `hapiplot()` returns binary image data
* returnformat: [png], svg, or pdf
* cachedir: Directory to store images. Default is hapiclient.hapi.cachedir()
* useimagecache: [True] Used cached image (when returnimage=True)
* saveimage: [False] Save image to `cachedir`
* saveformat: [png], svg, or pdf
Example
--------
>>> server = 'http://hapi-server.org/servers/TestData2.0/hapi'
>>> dataset = 'dataset1'
>>> start = '1970-01-01T00:00:00'
>>> stop = '1970-01-02T00:00:00'
>>> params = 'scalar,vector'
>>> opts = {'logging': True}
>>>
>>> from hapiclient import hapiplot
>>> hapiplot(server, dataset, params, start, stop, **opts)
>>>
>>> # or
>>>
>>> from hapiclient import hapi, hapiplot
>>> data, meta = hapi(server, dataset, params, start, stop, **opts)
>>> hapiplot(data, meta, **opts)
"""
if len(args) == 5:
# For consistency with gallery and autoplot functions, allow usage of
# hapiplot(server, dataset, parameters, start, stop, **kwargs)
from hapiclient.hapi import hapiopts
from hapiclient.hapi import hapi
kwargs_allowed = hapiopts()
kwargs_reduced = {}
# Extract hapi() options from kwargs
for key, value in kwargs.items():
if key in kwargs_allowed:
kwargs_reduced[key] = value
data, meta = hapi(args[0], args[1], args[2], args[3], args[4], **kwargs_reduced)
meta = hapiplot(data, meta, **kwargs)
return data, meta
else:
data = args[0]
meta = args[1]
# Default options
opts = {
'logging': False,
'saveimage': False,
'returnimage': False,
'usecache': True,
'useimagecache': True,
'cachedir': cachedir(),
'backend': 'default',
'style': 'fast',
'title': '',
'ztitle': '',
'xlabel': '',
'ylabel': '',
'zlabel': '',
'logx': False,
'logy': False,
'logz': False,
'tsopts': {},
'hmopts': {},
'rcParams':
{
'savefig.dpi': 144,
'savefig.format': 'png',
'savefig.bbox': 'tight',
'savefig.transparent': False,
'figure.max_open_warning': 50,
'figure.figsize': (7, 3),
'figure.dpi': 144,
'axes.titlesize': 10,
"font.family": "serif",
"font.serif": rcParams['font.serif'],
"font.weight": "normal"
},
'_rcParams': {
'figure.bbox': 'standard'
}
}
# Override defaults
opts = setopts(opts, kwargs)
from hapiclient import __version__
log('Using hapi.py version %s' % __version__, opts)
from hapiplot import __version__
from matplotlib import __version__ as mpl_version
log('hapiplot version %s using Matplotlib version %s' % \
(__version__, mpl_version), opts)
# _rcParams are not actually rcParams:
# 'figure.bbox': 'standard',
# Set to 'tight' to have fig.tight_layout() called before figure shown.
if opts["saveimage"]:
# Create cache directory
dir = cachedir(opts['cachedir'], meta['x_server'])
if not os.path.exists(dir): os.makedirs(dir)
# Convert from NumPy array of byte literals to NumPy array of
# datetime objects.
timename = meta['parameters'][0]['name']
nodata = False
if len(data[timename]) == 0:
nodata = True
# Set time to request range to set x-axis
Time = hapitime2datetime(np.array([meta['x_time.min'], meta['x_time.max']]), allow_missing_Z=True)
else:
Time = hapitime2datetime(data[timename], allow_missing_Z=True)
if len(meta["parameters"]) == 1:
a = 0 # Time is only parameter
else:
a = 1 # Time plus one or more parameters
for i in range(a, len(meta["parameters"])):
meta["parameters"][i]['hapiplot'] = {}
name = meta["parameters"][i]["name"]
if len(data[name].shape) > 3:
# TODO: Implement more than 2 dimensions?
log("Parameter '%s' has more than two components. Skipping." % name, opts)
continue
# If parameter has a size with two elements, e.g., [N1, N2]
# create N2 plots.
if len(data[name].shape) == 3: # shape = (Time, N1, N2)
log("Parameter '%s' has 3 components. Creating one plot per component." % name, opts)
pidx = 1 # Primary (fastest varying) index is N2
sidx = 0 # Secondary index is N1
nplts = data[name].shape[1]
if data[name].shape[1] > data[name].shape[2]:
pidx = 0
sidx = 1
nplts = data[name].shape[2]
if opts['returnimage']:
warning('Only returning first image for parameter with size[1] > 1.')
nplts = 1
for j in range(nplts):
timename = meta['parameters'][0]['name']
# Name to indicate what is plotted
if pidx > sidx:
name_new = name + "[" + str(j) + ",:]"
else:
name_new = name + "[:," + str(j) + "]"
# Reduced data ND Array
datar = np.ndarray(shape=(data[name].shape[0]),
dtype=[
(timename, data.dtype[timename]),
(name_new, data[name].dtype.str,
data[name].shape[1+pidx])
])
datar[timename] = data[timename]
if pidx > sidx:
datar[name_new] = data[name][:, j, :]
else:
datar[name_new] = data[name][:, :, j]
# Copy metadata to create a reduced metadata object
metar = meta.copy() # Shallow copy
metar["parameters"] = []
# Create parameters array with elements of Time parameter ...
metar["parameters"].append(meta["parameters"][0])
# .... and this parameter
metar["parameters"].append(meta["parameters"][i].copy())
# Give new name to indicate it is a subset of full parameter
metar["parameters"][1]['name'] = name_new
metar["parameters"][1]['name_orig'] = name
# New size is N1 or N2
metar["parameters"][1]['size'] = [meta["parameters"][i]['size'][pidx]]
if 'units' in metar["parameters"][1]:
if type(meta["parameters"][i]['units']) == str or meta["parameters"][i]['units'] == None:
# Same units applies to all dimensions
metar["parameters"][1]["units"] = meta["parameters"][i]['units']
else:
metar["parameters"][1]["units"] = meta["parameters"][i]['units'][j]
if 'label' in metar["parameters"][1]:
if type(meta["parameters"][i]['label']) == str:
# Same label applies to all dimensions
metar["parameters"][1]["label"] = meta["parameters"][i]['label']
else:
metar["parameters"][1]["label"] = meta["parameters"][i]['label'][j]
# Extract bins corresponding to jth column of data[name]
if 'bins' in metar["parameters"][1]:
metar["parameters"][1]['bins'] = []
metar["parameters"][1]['bins'].append(meta["parameters"][i]['bins'][pidx])
bin_title = ""
if 'bins' in meta["parameters"][i] and len(meta["parameters"][i]['bins']) > 1:
bin_title = meta["parameters"][i]['bins'][sidx]['name']
if 'centers' in meta["parameters"][i]['bins'][sidx]:
bin_title = bin_title + " = " + str(meta["parameters"][i]['bins'][sidx]['centers'][j])
if 'units' in meta["parameters"][i]['bins'][sidx]:
if meta["parameters"][i]['bins'][sidx]['units'] is not None:
bin_title = bin_title + " [" + meta["parameters"][i]['bins'][sidx]['units'] + "]"
bin_title = "\n" + bin_title
opts['title'] = meta["x_server"] + "\n" + meta["x_dataset"] + " | " + name_new + bin_title
# rcParams is modified by setopts to have all rcParams.
# reset to original passed rcParams so that imagepath
# computes file name based on rcParams passed to hapiplot.
if 'rcParams' in kwargs:
opts['rcParams'] = kwargs['rcParams']
metar = hapiplot(datar, metar, **opts)
meta["parameters"][i]['hapiplot'] = metar["parameters"][1]['hapiplot']
return meta
# Return cached image (case where we are returning binary image data)
# imagepath() options. Only need filename under these conditions.
if opts['saveimage'] or (opts['returnimage'] and opts['useimagecache']):
# Will use given rc style parameters and style name to generate file name.
# Assumes rc parameters of style and hapiplot defaults never change.
styleParams = {}
fmt = opts['rcParams']['savefig.format']
if 'rcParams' in kwargs:
styleParams = kwargs['rcParams']
if 'savefig.format' in kwargs['rcParams']:
kwargs['rcParams']['savefig.format']
fnameimg = imagepath(meta, i, opts['cachedir'], styleParams, fmt)
if opts['useimagecache'] and opts['returnimage'] and os.path.isfile(fnameimg):
log('Returning cached binary image data in ' + fnameimg, opts)
meta["parameters"][i]['hapiplot']['imagefile'] = fnameimg
with open(fnameimg, "rb") as f:
meta["parameters"][i]['hapiplot']['image'] = f.read()
continue
log("Plotting parameter '%s'" % name, opts)
if opts['title'] != '':
title = opts['title']
else:
if 'name_orig' in meta["parameters"][i]:
title = meta["x_server"] + "\n" + meta["x_dataset"] + " | " + meta["parameters"][i]['name_orig']
else:
title = meta["x_server"] + "\n" + meta["x_dataset"] + " | " + name
as_heatmap = False
if 'size' in meta['parameters'][i] and meta['parameters'][i]['size'][0] > 10:
as_heatmap = True
if 'bins' in meta['parameters'][i]:
as_heatmap = True
if 'units' in meta["parameters"][i] and type(meta["parameters"][i]["units"]) == list:
if as_heatmap:
# TODO: Verify that all units not the same
warning("Not plotting %s as heatmap because components have different units." % meta["parameters"][i]["name"])
as_heatmap = False
if as_heatmap:
# Plot as heatmap
hmopts = {
'returnimage': opts['returnimage'],
'transparent': opts['rcParams']['savefig.transparent']
}
if meta["parameters"][i]["type"] == "string":
warning("Plots for only types double, integer, and isotime implemented. Not plotting %s." % meta["parameters"][i]["name"])
continue
if nodata:
if a == 0:
# Time is only parameter
z = np.full((2,), np.nan)
else:
if 'size' in meta['parameters'][i]:
z = np.full((2, meta['parameters'][i]['size'][0]), np.nan)
else:
z = np.full((2,), np.nan)
else:
z = np.asarray(data[name])
if 'fill' in meta["parameters"][i] and meta["parameters"][i]['fill']:
if meta["parameters"][i]["type"] == 'integer':
z = z.astype('<f8', copy=False)
z = fill2nan(z, meta["parameters"][i]['fill'])
units = meta["parameters"][i].get("units", "")
nl = ""
if len(name) + len(units) > 30:
nl = "\n"
#zlabel = name + nl + " [" + units + "]"
zlabel = ""
if units is not None:
zlabel = " [" + units + "]"
bins = np.arange(meta['parameters'][i]['size'][0])
bins_time_dependent = False
if 'bins' in meta['parameters'][i]:
if 'ranges' in meta["parameters"][i]['bins'][0]:
if isinstance(meta['parameters'][i]['bins'][0]['ranges'], str) is False:
bins = np.array(meta["parameters"][i]['bins'][0]["ranges"])
else:
bins_time_dependent = True
else:
if isinstance(meta['parameters'][i]['bins'][0]['centers'], str) is False:
bins = np.array(meta["parameters"][i]['bins'][0]["centers"])
else:
bins_time_dependent = True
if 'bins' in meta['parameters'][i] and not bins_time_dependent:
units = meta["parameters"][i]['bins'][0].get("units", None)
if units is None:
units = ""
name = meta["parameters"][i]['bins'][0]["name"]
ylabel = name + "[" + units + "]"
else:
ylabel = "bin #"
if bins_time_dependent:
ylabel = "bin #\n(vals are time dependent)"
dt = np.diff(Time)
dtu = np.unique(dt)
if len(dtu) > 1:
warning('Time values are not uniformly spaced. Bin width for '
'time will be based on time separation of consecutive time values.')
# Cadence != time bin width in general, so can't use cadence.
# See https://github.com/hapi-server/data-specification/issues/75
if 'timeStampLocation' in meta and meta['timeStampLocation'].lower() == "begin":
deltat = dt[0]
for t in range(0,Time.size):
Time[t] = Time[t] + dt[t]
elif 'timeStampLocation' in meta and meta['timeStampLocation'].lower() == "end":
for t in range(0,Time.size):
deltat = -dt[0]
Time[t] = Time[t] - dt[i]
else:
deltat = -dt[0]/2
for t in range(0,Time.size):
Time[t] = Time[t] + deltat
Time = np.append(Time, Time[-1] + dt[-1])
elif 'timeStampLocation' in meta:
if meta['timeStampLocation'].lower() == "begin":
Time = np.append(Time, Time[-1] + dtu[0])
elif meta['timeStampLocation'].lower() == "end":
Time = Time - dtu[0]
Time = np.append(Time, Time[-1] + dtu[0])
else:
Time = Time - dtu[0]/2
Time = np.append(Time, Time[-1] + dtu[0])
if opts['xlabel'] != '' and 'xlabel' not in opts['hmopts']:
hmopts['xlabel'] = opts['xlabel']
opts['hmopts']['ylabel'] = ylabel
if opts['ylabel'] != '' and 'ylabel' not in opts['hmopts']:
hmopts['ylabel'] = opts['ylabel']
opts['hmopts']['title'] = title
if opts['title'] != '' and 'title' not in opts['hmopts']:
hmopts['title'] = opts['title']
opts['hmopts']['zlabel'] = zlabel
if opts['zlabel'] != '' and 'zlabel' not in opts['hmopts']:
hmopts['zlabel'] = opts['zlabel']
if False:
opts['hmopts']['ztitle'] = ztitle
if opts['ztitle'] != '' and 'ztitle' not in opts['hmopts']:
hmopts['ztitle'] = opts['ztitle']
if opts['logx'] is not False:
hmopts['logx'] = True
if opts['logy'] is not False:
hmopts['logy'] = True
if opts['logz'] is not False:
hmopts['logz'] = True
for key, value in opts['hmopts'].items():
hmopts[key] = value
with rc_context(rc=opts['rcParams']):
fig, cb = heatmap(Time, bins, np.transpose(z), **hmopts)
meta["parameters"][i]['hapiplot']['figure'] = fig
meta["parameters"][i]['hapiplot']['colorbar'] = cb
else:
tsopts = {
'logging': opts['logging'],
'returnimage': opts['returnimage'],
'transparent': opts['rcParams']['savefig.transparent'],
'backend': opts['backend']
}
ptype = meta["parameters"][i]["type"]
if nodata:
if a == 0:
# Time is only parameter
y = np.full((2,), np.nan)
else:
if 'size' in meta['parameters'][i]:
y = np.full((2,meta['parameters'][i]['size'][0]), np.nan)
else:
y = np.full((2,), np.nan)
else:
if ptype == "isotime":
y = hapitime2datetime(data[name],allow_missing_Z=True)
elif ptype == 'string':
y = data[name].astype('U')
else:
y = np.asarray(data[name])
if 'fill' in meta["parameters"][i] and meta["parameters"][i]['fill']:
if nodata == False and ptype == 'isotime' or ptype == 'string':
Igood = y != meta["parameters"][i]['fill']
# Note that json reader returns fill to U not b.
Nremoved = data[name].size - Igood.size
if Nremoved > 0:
# TODO: Implement masking so connected line plots will
# show gaps as they do for NaN values.
warning('Parameter ' + name + ' is of type ' + ptype + ' and has '
+ str(Nremoved) + ' fill value(s). Masking is not implemented, '
'so removing fill elements before plotting.')
Time = Time[Igood]
y = y[Igood]
if ptype == 'integer':
y = y.astype('<f8', copy=False)
if ptype == 'integer' or ptype == 'double':
y = fill2nan(y, meta["parameters"][i]['fill'])
remove_mean = False
if 'uk/GIN_' in meta['x_server'] and (ptype == 'integer' or ptype == 'double'):
remove_mean = True
y_mean = np.nanmean(y, axis=0)
units = None
if 'units' in meta["parameters"][i] and meta["parameters"][i]['units']:
units = meta["parameters"][i]["units"]
nl = ""
if type(units) == str:
if len(name) + len(units) > 30:
nl = "\n" # TODO: Automatically figure out when this is needed.
ylabel = name
if units is not None and type(units) is not list:
ylabel = name + nl + " [" + units + "]"
if type(units) == list:
ylabel = name
if not 'legendlabels' in opts['tsopts']:
legendlabels = []
if 'size' in meta['parameters'][i]:
for l in range(0,meta['parameters'][i]['size'][0]):
bin_label = ''
bin_name = ''
col_name = ''
if 'bins' in meta['parameters'][i]:
bin_name = meta['parameters'][i]['bins'][0]['name']
if 'label' in meta['parameters'][i]['bins'][0]:
if type(meta['parameters'][i]['bins'][0]['label']) == str:
bin_name = meta['parameters'][i]['bins'][0]['label']
else:
bin_name = meta['parameters'][i]['bins'][0]['label'][l]
sep = ''
if 'centers' in meta['parameters'][i]['bins'][0] and 'ranges' in meta['parameters'][i]['bins'][0]:
bin_name = bin_name + ' bin with'
sep = ';'
bin_label = ''
if 'units' in meta['parameters'][i]['bins'][0]:
bin_units = meta['parameters'][i]['bins'][0]['units']
if type(bin_units) == list:
if type(bin_units[l]) == str and bin_units != '':
bin_units = ' [' + bin_units[l] + ']'
elif bin_units[l] == None:
bin_units = ' '
else:
bin_units = ' '
else:
if type(bin_units) == str and bin_units != '':
bin_units = ' [' + bin_units + ']'
else:
bin_units = ' '
if 'centers' in meta['parameters'][i]['bins'][0]:
if meta['parameters'][i]['bins'][0]['centers'][l] is not None:
bin_label = bin_label + ' center = ' + str(meta['parameters'][i]['bins'][0]['centers'][l]) + bin_units
#else:
# bin_label = bin_label + ' center = None'
if 'ranges' in meta['parameters'][i]['bins'][0]:
if type(meta['parameters'][i]['bins'][0]['ranges'][l]) == list:
if meta['parameters'][i]['bins'][0]['ranges'][l][0] and meta['parameters'][i]['bins'][0]['ranges'][l][1] is not None:
bin_label = bin_label + sep + ' range = [' + str(meta['parameters'][i]['bins'][0]['ranges'][l][0]) + ', ' + str(meta['parameters'][i]['bins'][0]['ranges'][l][1]) + ']' + bin_units
#else:
# bin_label = bin_label + sep + ' range = [None]'
if bin_label != '':
bin_label = 'bin: ' + bin_label
col_name = bin_name + '#%d' % l
if col_name == '':
col_name = 'col #%d' % l
if nodata:
col_name = col_name + " [no data in interval]"
if remove_mean:
if y_mean[l] > 0:
col_name = "{0:s} - {1:.2f}".format(col_name, y_mean[l])
if y_mean[l] < 0:
col_name = "{0:s} + {1:.2f}".format(col_name, -y_mean[l])
if 'label' in meta['parameters'][i] and \
type(meta['parameters'][i]['label']) == list and \
len(meta['parameters'][i]['label']) > l and \
meta['parameters'][i]['label'][l].strip() != '':
col_name = meta['parameters'][i]['label'][l]
if nodata:
col_name = col_name + " [no data in interval]"
else:
if remove_mean:
if y_mean[l] > 0:
col_name = "{0:s} - {1:.2f}".format(col_name, y_mean[l])
if y_mean[l] < 0:
col_name = "{0:s} + {1:.2f}".format(col_name, -y_mean[l])
if type(units) == list:
if len(units) == 1:
if units[0] != '':
legendlabels.append(col_name + ' [' + units[0] + '] ' + bin_label)
elif type(units[l]) == str and units[l] != '':
legendlabels.append(col_name + ' [' + units[l] + '] ' + bin_label)
elif units[l] == None:
legendlabels.append(col_name + ' ' + bin_label)
else:
legendlabels.append(col_name + ' ' + bin_label)
else:
# Units are on y label
legendlabels.append(col_name + ' ' + bin_label)
tsopts['legendlabels'] = legendlabels
# If xlabel in opts and opts['tsopts'], warn?
if opts['xlabel'] != '' and 'xlabel' not in opts['tsopts']:
tsopts['xlabel'] = opts['xlabel']
tsopts['ylabel'] = ylabel
if opts['ylabel'] != '' and 'ylabel' not in opts['tsopts']:
tsopts['ylabel'] = opts['ylabel']
tsopts['title'] = title
if opts['title'] != '' and 'title' not in opts['tsopts']:
tsopts['title'] = opts['title']
if opts['logx'] is not False and 'logx' not in opts['tsopts'] :
tsopts['logx'] = True
if opts['logy'] is not False and 'logy' not in opts['tsopts']:
tsopts['logy'] = True
# Apply tsopts
for key, value in opts['tsopts'].items():
tsopts[key] = value
if nodata == True:
tsopts['nodata'] = True
if remove_mean:
with rc_context(rc=opts['rcParams']):
fig = timeseries(Time, y-y_mean, **tsopts)
else:
with rc_context(rc=opts['rcParams']):
fig = timeseries(Time, y, **tsopts)
if remove_mean:
with rc_context(rc=opts['rcParams']):
fig = timeseries(Time, y-y_mean, **tsopts)
else:
with rc_context(rc=opts['rcParams']):
fig = timeseries(Time, y, **tsopts)
meta["parameters"][i]['hapiplot']['figure'] = fig
if opts['saveimage']:
log('Writing %s' % fnameimg, opts)
meta["parameters"][i]['hapiplot']['imagefile'] = fnameimg
else:
from io import BytesIO
fnameimg = BytesIO()
if opts['returnimage']:
with rc_context(rc=opts['rcParams']):
fig.canvas.print_figure(fnameimg)
if opts['saveimage']:
with open(fnameimg, mode='rb') as f:
meta["parameters"][i]['hapiplot']['image'] = f.read()
else:
meta["parameters"][i]['hapiplot']['image'] = fnameimg.getvalue()
else:
with rc_context(rc=opts['rcParams']):
fig.savefig(fnameimg)
# Two calls to fig.tight_layout() may be needed b/c of bug in PyQt:
# https://github.com/matplotlib/matplotlib/issues/10361
if opts['_rcParams']['figure.bbox'] == 'tight':
fig.tight_layout()
return meta
def imagepath(meta, i, cachedir, opts, fmt):
# The value of axis.prop_cycle is a cycler
# https://matplotlib.org/cycler/
# and can't be serialized by json.dumps.
if 'axes.prop_cycle' in opts:
opts['axes.prop_cycle'] = list(opts['axes.prop_cycle'])
try:
optsmd5 = hashlib.md5(json.dumps(opts, sort_keys=True).encode('utf8')).hexdigest()
except:
# Remove elements that can't be serialized.
for key in opts:
try:
json.dumps(opts[key])
except:
#print('Removed ' + key)
opts[key] = None
optsmd5 = hashlib.md5(json.dumps(opts, sort_keys=True).encode('utf8')).hexdigest()
fname = request2path(meta['x_server'],
meta['x_dataset'],
meta['parameters'][i]['name'],
meta['x_time.min'],
meta['x_time.max'],
cachedir)
return fname + "-" + optsmd5 + "." + fmt
def fill2mask(y, fill):
"""Create a masked array for a non-numeric fill value."""
# TODO: Write. Needed for ISOTime parameters with a fill value.
pass
def fill2nan(y, fill):
if fill.lower() == 'nan':
yfill = np.nan
else:
yfill = float(fill)
# Replace fills with NaN for plotting
# (so gaps shown in lines for time series an empty tiles for spectra)
y[y == yfill] = np.nan
# Catch case values in binary where, e.g., metadata says fill='-1E-31'
# but file has -9.999999848243207e+30. This happens when CDF data
# values stored as floats is converted to binary using double(value)
# because double(float('-1E31')) = -9.999999848243207e+30. Technically
# the server is not producing valid results b/c spec says fill values
# in file must match double(fill string in metadata).
y[y == np.float32(yfill)] = np.nan
return y