forked from Open-MSS/MSS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpl_qtwidget.py
1691 lines (1432 loc) · 67 KB
/
mpl_qtwidget.py
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
mslib.msui.mpl_qtwidget
~~~~~~~~~~~~~~~~~~~~~~~
Definitions of Matplotlib widgets for Qt Designer.
This file is part of MSS.
:copyright: Copyright 2008-2014 Deutsches Zentrum fuer Luft- und Raumfahrt e.V.
:copyright: Copyright 2011-2014 Marc Rautenhaus (mr)
:copyright: Copyright 2016-2023 by the MSS team, see AUTHORS.
:license: APACHE-2.0, see LICENSE for details.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Parts of the code have been adapted from Chapter 6 of Sandro Tosi,
# 'Matplotlib for Python Developers'.
from datetime import datetime
import enum
import os
import six
import logging
import numpy as np
import matplotlib
from fs import open_fs
from fslib.fs_filepicker import getSaveFileNameAndFilter
from matplotlib import cbook, figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT, FigureCanvasQTAgg
import matplotlib.backend_bases
from PyQt5 import QtCore, QtWidgets, QtGui
from mslib.utils.thermolib import convert_pressure_to_vertical_axis_measure
from mslib.utils import thermolib, FatalUserError
from mslib.utils.config import config_loader, save_settings_qsettings, load_settings_qsettings
from mslib.utils.units import units
from mslib.msui import mpl_pathinteractor as mpl_pi
from mslib.msui import mpl_map
from mslib.msui.icons import icons
from mslib.utils.loggerdef import configure_mpl_logger
PIL_IMAGE_ORIGIN = "upper"
LAST_SAVE_DIRECTORY = config_loader(dataset="data_dir")
matplotlib.rcParams['savefig.directory'] = LAST_SAVE_DIRECTORY
_DEFAULT_SETTINGS_TOPVIEW = {
"draw_graticule": True,
"draw_coastlines": True,
"fill_waterbodies": True,
"fill_continents": True,
"draw_flighttrack": True,
"draw_marker": True,
"label_flighttrack": True,
"tov_plot_title_size": "default",
"tov_axes_label_size": "default",
"colour_water": ((153 / 255.), (255 / 255.), (255 / 255.), (255 / 255.)),
"colour_land": ((204 / 255.), (153 / 255.), (102 / 255.), (255 / 255.)),
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1)}
_DEFAULT_SETTINGS_SIDEVIEW = {
"vertical_extent": (1050, 180),
"vertical_axis": "pressure",
"secondary_axis": "no secondary axis",
"plot_title_size": "default",
"axes_label_size": "default",
"flightlevels": [300],
"draw_flightlevels": True,
"draw_flighttrack": True,
"fill_flighttrack": True,
"label_flighttrack": True,
"draw_verticals": True,
"draw_marker": True,
"draw_ceiling": True,
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1),
"colour_ft_fill": (0, 0, 1, 0.15),
"colour_ceiling": (0, 0, 1, 0.15)}
_DEFAULT_SETTINGS_LINEARVIEW = {
"plot_title_size": "default",
"axes_label_size": "default"}
mpl_logger = configure_mpl_logger()
class ViewPlotter:
def __init__(self, fig=None, ax=None, settings_tag=None, settings=None):
# setup Matplotlib Figure and Axis
self.fig, self.ax = fig, ax
self.settings = settings
self.settings_tag = settings_tag
if self.fig is None:
assert ax is None
self.fig = figure.Figure(facecolor="w") # 0.75
if self.ax is None:
self.ax = self.fig.add_subplot(111, zorder=99)
def draw_metadata(self, title="", init_time=None, valid_time=None,
level=None, style=None):
if style:
title += f" ({style})"
if level:
title += f" at {level}"
if isinstance(valid_time, datetime) and isinstance(init_time, datetime):
time_step = valid_time - init_time
else:
time_step = None
if isinstance(valid_time, datetime):
valid_time = valid_time.strftime('%a %Y-%m-%d %H:%M UTC')
if isinstance(init_time, datetime):
init_time = init_time.strftime('%a %Y-%m-%d %H:%M UTC')
# Add valid time / init time information to the title.
if valid_time:
if init_time:
if time_step is not None:
title += f"\nValid: {valid_time} (step {((time_step.days * 86400 + time_step.seconds) // 3600):d}" \
f" hrs from {init_time})"
else:
title += f"\nValid: {valid_time} (initialisation: {init_time})"
else:
title += f"\nValid: {valid_time}"
# Set title.
self.ax.set_title(title, horizontalalignment='left', x=0)
def get_plot_size_in_px(self):
"""Determines the size of the current figure in pixels.
Returns the tuple width, height.
"""
# (bounds = left, bottom, width, height)
ax_bounds = self.ax.bbox.bounds
width = int(round(ax_bounds[2]))
height = int(round(ax_bounds[3]))
return width, height
def get_settings(self):
"""Retrieve dictionary of plotting settings.
Returns:
dict: dictionary of settings.
"""
return self.settings
def set_settings(self, settings, save=False):
"""Update local settings influencing the plotting
Args:
settings (dict): Dictionary of string/value pairs
"""
if settings is not None:
self.settings.update(settings)
if save:
self.save_settings()
def load_settings(self):
self.settings = load_settings_qsettings(self.settings_tag, self.settings)
def save_settings(self):
save_settings_qsettings(self.settings_tag, self.settings)
class TopViewPlotter(ViewPlotter):
def __init__(self, fig=None, ax=None, settings=None):
super().__init__(fig, ax, settings_tag="topview", settings=_DEFAULT_SETTINGS_TOPVIEW)
self.map = None
self.legimg = None
self.legax = None
# stores the topview plot title size(tov_pts) and topview axes label size(tov_als),initially as None.
self.tov_pts = None
self.tov_als = None
# Sets the default fontsize parameters' values for topview from MSSDefaultConfig.
self.topview_size_settings = config_loader(dataset="topview")
self.load_settings()
self.set_settings(settings)
self.ax.figure.canvas.draw()
def init_map(self, **kwargs):
self.map = mpl_map.MapCanvas(appearance=self.get_settings(),
resolution="l", area_thresh=1000., ax=self.ax,
**kwargs)
# Sets the selected fontsize only if draw_graticule box from topview options is checked in.
if self.settings["draw_graticule"]:
try:
self.map._draw_auto_graticule(self.tov_als)
except Exception as ex:
logging.error("ERROR: cannot plot graticule (message: %s - '%s')", type(ex), ex)
else:
self.map.set_graticule_visible(False)
self.ax.set_autoscale_on(False)
self.ax.set_title("Top view", fontsize=self.tov_pts, horizontalalignment="left", x=0)
def getBBOX(self, bbox_units=None):
axis = self.ax.axis()
if bbox_units is not None:
self.map.bbox_units = bbox_units
if self.map.bbox_units == "degree":
# Convert the current axis corners to lat/lon coordinates.
axis0, axis2 = self.map(axis[0], axis[2], inverse=True)
axis1, axis3 = self.map(axis[1], axis[3], inverse=True)
bbox = (axis0, axis2, axis1, axis3)
elif self.map.bbox_units.startswith("meter"):
center_x, center_y = self.map(
*(float(_x) for _x in self.map.bbox_units[6:-1].split(",")))
bbox = (axis[0] - center_x, axis[2] - center_y, axis[1] - center_x, axis[3] - center_y)
else:
bbox = axis[0], axis[2], axis[1], axis[3]
return bbox
def clear_figure(self):
logging.debug("Removing image")
if self.map.image is not None:
self.map.image.remove()
self.map.image = None
self.ax.set_title("Top view", horizontalalignment="left", x=0)
self.ax.figure.canvas.draw()
def set_settings(self, settings, save=False):
"""Apply settings from dictionary 'settings' to the view.
If settings is None, apply default settings.
"""
super().set_settings(settings, save)
# Stores the exact value of fontsize for topview plot title size(tov_pts)
self.tov_pts = (self.topview_size_settings["plot_title_size"]
if self.settings["tov_plot_title_size"] == "default"
else int(self.settings["tov_plot_title_size"]))
# Stores the exact value of fontsize for topview axes label size(tov_als)
self.tov_als = (self.topview_size_settings["axes_label_size"]
if self.settings["tov_axes_label_size"] == "default"
else int(self.settings["tov_axes_label_size"]))
ax = self.ax
if self.map is not None:
self.map.set_coastlines_visible(self.settings["draw_coastlines"])
self.map.set_fillcontinents_visible(visible=self.settings["fill_continents"],
land_color=self.settings["colour_land"],
lake_color=self.settings["colour_water"])
self.map.set_mapboundary_visible(visible=self.settings["fill_waterbodies"],
bg_color=self.settings["colour_water"])
# Updates plot title size as selected from combobox labelled plot title size.
ax.set_autoscale_on(False)
ax.set_title("Top view", fontsize=self.tov_pts, horizontalalignment="left", x=0)
# Updates graticule ticklabels/labels fontsize if draw_graticule is True.
if self.settings["draw_graticule"]:
self.map.set_graticule_visible(False)
self.map._draw_auto_graticule(self.tov_als)
else:
self.map.set_graticule_visible(self.settings["draw_graticule"])
def redraw_map(self, kwargs_update=None):
"""Redraw map canvas.
Executed on clicked() of btMapRedraw.
See MapCanvas.update_with_coordinate_change(). After the map redraw,
coordinates of all objects overlain on the map have to be updated.
"""
# 2) UPDATE MAP.
self.map.update_with_coordinate_change(kwargs_update)
# Sets the graticule ticklabels/labels fontsize for topview when map is redrawn.
if self.settings["draw_graticule"]:
self.map.set_graticule_visible(False)
self.map._draw_auto_graticule(self.tov_als)
else:
self.plotter.map.set_graticule_visible(self.settings["draw_graticule"])
self.ax.figure.canvas.draw() # this one is required to trigger a
# drawevent to update the background
# self.draw_metadata() ; It is not needed here, since below here already plot title is being set.
# Setting fontsize for topview plot title when map is redrawn.
self.ax.set_title("Top view", fontsize=self.tov_pts, horizontalalignment='left', x=0)
self.ax.figure.canvas.draw()
def draw_image(self, img):
"""Draw the image img on the current plot.
"""
logging.debug("plotting image..")
self.wms_image = self.map.imshow(img, interpolation="nearest", origin=PIL_IMAGE_ORIGIN)
# NOTE: imshow always draws the images to the lowest z-level of the
# plot.
# See these mailing list entries:
# http://www.mail-archive.com/matplotlib-devel@lists.sourceforge.net/msg05955.html
# http://old.nabble.com/Re%3A--Matplotlib-users--imshow-zorder-tt19047314.html#a19047314
#
# Question: Is this an issue for us or do we always want the images in the back
# anyhow? At least we need to remove filled continents here.
# self.map.set_fillcontinents_visible(False)
# ** UPDATE 2011/01/14 ** seems to work with version 1.0!
logging.debug("done.")
def draw_legend(self, img):
"""Draw the legend graphics img on the current plot.
Adds new axes to the plot that accomodate the legend.
"""
# If the method is called with a "None" image, the current legend
# graphic should be removed (if one exists).
if self.legimg is not None:
logging.debug("removing image %s", self.legimg)
self.legimg.remove()
self.legimg = None
if img is not None:
# The size of the legend axes needs to be given in relative figure
# coordinates. To determine those from the legend graphics size in
# pixels, we need to determine the size of the currently displayed
# figure in pixels.
figsize_px = self.fig.get_size_inches() * self.fig.get_dpi()
ax_extent_x = float(img.size[0]) / figsize_px[0]
ax_extent_y = float(img.size[1]) / figsize_px[1]
# If no legend axes have been created, do so now.
if self.legax is None:
# Main axes instance of mplwidget has zorder 99.
self.legax = self.fig.add_axes([1 - ax_extent_x, 0.01, ax_extent_x, ax_extent_y],
frameon=False,
xticks=[], yticks=[],
label="ax2", zorder=0)
self.legax.patch.set_facecolor("None")
# If axes exist, adjust their position.
else:
self.legax.set_position([1 - ax_extent_x, 0.01, ax_extent_x, ax_extent_y])
# Plot the new legimg in the legax axes.
self.legimg = self.legax.imshow(img, origin=PIL_IMAGE_ORIGIN, aspect="equal", interpolation="nearest")
self.ax.figure.canvas.draw()
class SideViewPlotter(ViewPlotter):
_pres_maj = np.concatenate([np.arange(top * 10, top, -top) for top in (10000, 1000, 100, 10)] + [[10]])
_pres_min = np.concatenate([np.arange(top * 10, top, -top // 10) for top in (10000, 1000, 100, 10)] + [[10]])
_pres_maj = np.concatenate([np.arange(top * 10, top, -top) for top in (10000, 1000, 100, 10)] + [[10]])
_pres_min = np.concatenate([np.arange(top * 10, top, -top // 10) for top in (10000, 1000, 100, 10)] + [[10]])
def __init__(self, fig=None, ax=None, settings=None, numlabels=None, num_interpolation_points=None):
"""
Arguments:
model -- WaypointsTableModel defining the vertical section.
"""
if numlabels is None:
numlabels = config_loader(dataset='num_labels')
if num_interpolation_points is None:
num_interpolation_points = config_loader(dataset='num_interpolation_points')
super().__init__(fig, ax, settings_tag="sideview", settings=_DEFAULT_SETTINGS_SIDEVIEW)
self.load_settings()
self.set_settings(settings)
self.numlabels = numlabels
self.num_interpolation_points = num_interpolation_points
self.ax2 = self.ax.twinx()
self.ax.patch.set_facecolor("None")
self.ax2.patch.set_facecolor("None")
# Main axes instance of mplwidget has zorder 99.
self.imgax = self.fig.add_axes(
self.ax.get_position(), frameon=True, xticks=[], yticks=[], label="imgax", zorder=0)
self.vertical_lines = []
# Sets the default value of sideview fontsize settings from MSSDefaultConfig.
self.sideview_size_settings = config_loader(dataset="sideview")
# Draw a number of flight level lines.
self.flightlevels = []
self.fl_label_list = []
self.image = None
self.update_vertical_extent_from_settings(init=True)
def _determine_ticks_labels(self, typ):
if typ == "no secondary axis":
major_ticks = [] * units.pascal
minor_ticks = [] * units.pascal
labels = []
ylabel = ""
elif typ == "pressure":
# Compute the position of major and minor ticks. Major ticks are labelled.
major_ticks = self._pres_maj[(self._pres_maj <= self.p_bot) & (self._pres_maj >= self.p_top)]
minor_ticks = self._pres_min[(self._pres_min <= self.p_bot) & (self._pres_min >= self.p_top)]
labels = [f"{int(_x / 100)}"
if (_x / 100) - int(_x / 100) == 0 else f"{float(_x / 100)}" for _x in major_ticks]
if len(labels) > 20:
labels = ["" if x.split(".")[-1][0] in "975" else x for x in labels]
elif len(labels) > 10:
labels = ["" if x.split(".")[-1][0] in "9" else x for x in labels]
ylabel = "pressure (hPa)"
elif typ == "pressure altitude":
bot_km = thermolib.pressure2flightlevel(self.p_bot * units.Pa).to(units.km).magnitude
top_km = thermolib.pressure2flightlevel(self.p_top * units.Pa).to(units.km).magnitude
ma_dist, mi_dist = 4, 1.0
if (top_km - bot_km) <= 20:
ma_dist, mi_dist = 1, 0.5
elif (top_km - bot_km) <= 40:
ma_dist, mi_dist = 2, 0.5
major_heights = np.arange(0, top_km + 1, ma_dist)
minor_heights = np.arange(0, top_km + 1, mi_dist)
major_ticks = thermolib.flightlevel2pressure(major_heights * units.km).magnitude
minor_ticks = thermolib.flightlevel2pressure(minor_heights * units.km).magnitude
labels = major_heights
ylabel = "pressure altitude (km)"
elif typ == "flight level":
bot_km = thermolib.pressure2flightlevel(self.p_bot * units.Pa).to(units.km).magnitude
top_km = thermolib.pressure2flightlevel(self.p_top * units.Pa).to(units.km).magnitude
ma_dist, mi_dist = 50, 10
if (top_km - bot_km) <= 10:
ma_dist, mi_dist = 20, 10
elif (top_km - bot_km) <= 40:
ma_dist, mi_dist = 40, 10
major_fl = np.arange(0, 2132, ma_dist)
minor_fl = np.arange(0, 2132, mi_dist)
major_ticks = thermolib.flightlevel2pressure(major_fl * units.hft).magnitude
minor_ticks = thermolib.flightlevel2pressure(minor_fl * units.hft).magnitude
labels = major_fl
ylabel = "flight level (hft)"
else:
raise RuntimeError(f"Unsupported vertical axis type: '{typ}'")
return ylabel, major_ticks, minor_ticks, labels
def setup_side_view(self):
"""Set up a vertical section view.
Vertical cross section code (log-p axis etc.) taken from
mss_batch_production/visualisation/mpl_vsec.py.
"""
self.ax.set_title("vertical flight profile", horizontalalignment="left", x=0)
self.ax.grid(visible=True)
self.ax.set_xlabel("lat/lon")
for ax in (self.ax, self.ax2):
ax.set_yscale("log")
ax.set_ylim(self.p_bot, self.p_top)
self.redraw_yaxis()
def redraw_yaxis(self):
""" Redraws the y-axis on map after setting the values from sideview options dialog box
and also updates the sizes for map title and x and y axes labels and ticklabels"""
vaxis = self.settings["vertical_axis"]
vaxis2 = self.settings["secondary_axis"]
# Sets fontsize value for x axis ticklabel.
axes_label_size = (self.sideview_size_settings["axes_label_size"]
if self.settings["axes_label_size"] == "default"
else int(self.settings["axes_label_size"]))
# Sets fontsize value for plot title and axes title/label
plot_title_size = (self.sideview_size_settings["plot_title_size"]
if self.settings["plot_title_size"] == "default"
else int(self.settings["plot_title_size"]))
# Updates the fontsize of the x-axis ticklabels of sideview.
self.ax.tick_params(axis='x', labelsize=axes_label_size)
# Updates the fontsize of plot title and x-axis title of sideview.
self.ax.set_title("vertical flight profile", fontsize=plot_title_size, horizontalalignment="left", x=0)
self.ax.set_xlabel("lat/lon", fontsize=plot_title_size)
for ax, typ in zip((self.ax, self.ax2), (vaxis, vaxis2)):
ylabel, major_ticks, minor_ticks, labels = self._determine_ticks_labels(typ)
major_ticks_units = getattr(major_ticks, "units", None)
if ax.yaxis.units is None and major_ticks_units is not None:
ax.yaxis.set_units(major_ticks_units)
ax.set_ylabel(ylabel, fontsize=plot_title_size)
ax.set_yticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks, minor=False)
ax.set_yticklabels([], minor=True)
ax.set_yticklabels(labels, minor=False, fontsize=axes_label_size)
ax.set_ylim(self.p_bot, self.p_top)
if vaxis2 == "no secondary axis":
self.fig.subplots_adjust(left=0.08, right=0.96, top=0.9, bottom=0.14)
self.imgax.set_position(self.ax.get_position())
else:
self.fig.subplots_adjust(left=0.08, right=0.92, top=0.9, bottom=0.14)
self.imgax.set_position(self.ax.get_position())
def redraw_xaxis(self, lats, lons, times, times_visible):
"""Redraw the x-axis of the side view on path changes. Also remove
a vertical section image if one exists, as it is invalid after
a path change.
"""
logging.debug("redrawing x-axis")
# Re-label x-axis.
self.ax.set_xlim(0, len(lats) - 1)
# Set xticks so that they display lat/lon. Plot "numlabels" labels.
lat_inds = np.arange(len(lats))
tick_index_step = len(lat_inds) // self.numlabels
self.ax.set_xticks(lat_inds[::tick_index_step])
if times_visible:
self.ax.set_xticklabels([f'{d[0]:2.1f}, {d[1]:2.1f}\n{d[2].strftime("%H:%M")}Z'
for d in zip(lats[::tick_index_step],
lons[::tick_index_step],
times[::tick_index_step])],
rotation=25, horizontalalignment="right")
else:
self.ax.set_xticklabels([f"{d[0]:2.1f}, {d[1]:2.1f}"
for d in zip(lats[::tick_index_step],
lons[::tick_index_step])],
rotation=25, horizontalalignment="right")
self.ax.figure.canvas.draw()
def draw_vertical_lines(self, highlight, lats, lons):
# Remove all vertical lines
for line in self.vertical_lines:
try:
line.remove()
except ValueError as e:
logging.debug("Vertical line was somehow already removed:\n%s", e)
self.vertical_lines = []
# Add vertical lines
if self.settings["draw_verticals"]:
ipoint = 0
for i, (lat, lon) in enumerate(zip(lats, lons)):
if (ipoint < len(highlight) and
np.hypot(lat - highlight[ipoint][0],
lon - highlight[ipoint][1]) < 2E-10):
self.vertical_lines.append(
self.ax.axvline(i, color='k', linewidth=2, linestyle='--', alpha=0.5))
ipoint += 1
self.fig.canvas.draw()
def getBBOX(self):
"""Get the bounding box of the view (returns a 4-tuple
x1, y1(p_bot[hPa]), x2, y2(p_top[hPa])).
"""
# Get the bounding box of the current view
# (bbox = llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat; i.e. for the side
# view bbox = x1, y1(p_bot), x2, y2(p_top)).
axis = self.ax.axis()
num_interpolation_points = self.num_interpolation_points
num_labels = self.numlabels
# Return a tuple (num_interpolation_points, p_bot[hPa],
# num_labels, p_top[hPa]) as BBOX.
bbox = (num_interpolation_points, (axis[2] / 100),
num_labels, (axis[3] / 100))
return bbox
def clear_figure(self):
logging.debug("path of side view has changed.. removing invalidated "
"image (if existent) and redrawing.")
if self.image is not None:
self.image.remove()
self.image = None
self.ax.set_title("vertical flight profile", horizontalalignment="left", x=0)
self.ax.figure.canvas.draw()
def draw_image(self, img):
"""Draw the image img on the current plot.
NOTE: The image is plotted in a separate axes object that is located
below the axes that display the flight profile. This is necessary
because imshow() does not work with logarithmic axes.
"""
logging.debug("plotting vertical section image..")
ix, iy = img.size
logging.debug(" image size is %dx%d px, format is '%s'", ix, iy, img.format)
# If an image is currently displayed, remove it from the plot.
if self.image is not None:
self.image.remove()
# Plot the new image in the image axes and adjust the axes limits.
self.image = self.imgax.imshow(
img, interpolation="nearest", aspect="auto", origin=PIL_IMAGE_ORIGIN)
self.imgax.set_xlim(0, ix - 1)
self.imgax.set_ylim(iy - 1, 0)
self.ax.figure.canvas.draw()
logging.debug("done.")
def update_vertical_extent_from_settings(self, init=False):
""" Checks for current units of axis and convert the upper and lower limit
to pa(pascals) for the internal computation by code """
if not init:
p_bot_old = self.p_bot
p_top_old = self.p_top
if self.settings["vertical_axis"] == "pressure altitude":
self.p_bot = thermolib.flightlevel2pressure(self.settings["vertical_extent"][0] * units.km).magnitude
self.p_top = thermolib.flightlevel2pressure(self.settings["vertical_extent"][1] * units.km).magnitude
elif self.settings["vertical_axis"] == "flight level":
self.p_bot = thermolib.flightlevel2pressure(self.settings["vertical_extent"][0] * units.hft).magnitude
self.p_top = thermolib.flightlevel2pressure(self.settings["vertical_extent"][1] * units.hft).magnitude
else:
self.p_bot = self.settings["vertical_extent"][0] * 100
self.p_top = self.settings["vertical_extent"][1] * 100
if not init:
if (p_bot_old != self.p_bot) or (p_top_old != self.p_top):
if self.image is not None:
self.image.remove()
self.image = None
self.setup_side_view()
else:
self.redraw_yaxis()
class LinearViewPlotter(ViewPlotter):
"""Specialised MplCanvas that draws a linear view of a
flight track / list of waypoints.
"""
def __init__(self, model=None, numlabels=None, settings=None):
"""
Arguments:
model -- WaypointsTableModel defining the linear section.
"""
if numlabels is None:
numlabels = config_loader(dataset='num_labels')
super().__init__(settings_tag="linearview", settings=_DEFAULT_SETTINGS_LINEARVIEW)
self.load_settings()
# Sets the default values of plot sizes from MissionSupportDefaultConfig.
self.linearview_size_settings = config_loader(dataset="linearview")
self.set_settings(settings)
# Setup the plot.
self.numlabels = numlabels
self.setup_linear_view()
# If a waypoints model has been passed, create an interactor on it.
self.waypoints_interactor = None
self.waypoints_model = None
self.vertical_lines = []
self.basename = "linearview"
def setup_linear_view(self):
"""Set up a linear section view.
"""
self.fig.subplots_adjust(left=0.08, right=0.96, top=0.9, bottom=0.14)
def clear_figure(self):
logging.debug("path of linear view has changed.. removing invalidated plots")
self.fig.clf()
self.ax = self.fig.add_subplot(111, zorder=99)
self.ax.figure.patch.set_visible(False)
self.fig.canvas.draw()
def redraw_xaxis(self, lats, lons):
# Re-label x-axis.
self.ax.set_xlim(0, len(lats) - 1)
# Set xticks so that they display lat/lon. Plot "numlabels" labels.
lat_inds = np.arange(len(lats))
tick_index_step = len(lat_inds) // self.numlabels
self.ax.set_xticks(lat_inds[::tick_index_step])
self.ax.set_xticklabels([f'{d[0]:2.1f}, {d[1]:2.1f}'
for d in zip(lats[::tick_index_step],
lons[::tick_index_step])],
rotation=25, horizontalalignment="right")
# Remove all vertical lines
for line in self.vertical_lines:
try:
line.remove()
except ValueError as e:
logging.debug("Vertical line was somehow already removed:\n%s", e)
self.vertical_lines = []
def draw_vertical_lines(self, highlight, lats, lons):
# draw vertical lines
self.vertical_lines = []
ipoint = 0
for i, (lat, lon) in enumerate(zip(lats, lons)):
if (ipoint < len(highlight) and np.hypot(lat - highlight[ipoint][0],
lon - highlight[ipoint][1]) < 2E-10):
self.vertical_lines.append(self.ax.axvline(i, color='k', linewidth=2, linestyle='--', alpha=0.5))
ipoint += 1
self.fig.tight_layout()
self.fig.subplots_adjust(top=0.85, bottom=0.20)
self.fig.canvas.draw()
def draw_legend(self, img):
if img is not None:
logging.error("Legends not supported in LinearView mode!")
raise NotImplementedError
def draw_image(self, xmls, colors=None, scales=None):
self.clear_figure()
offset = 40
self.ax.patch.set_visible(False)
for i, xml in enumerate(xmls):
data = xml.find("Data")
values = [float(value) for value in data.text.split(",")]
unit = data.attrib["unit"]
numpoints = int(data.attrib["num_waypoints"])
if colors:
color = colors[i] if len(colors) > i else colors[-1]
else:
color = "#00AAFF"
if scales:
scale = scales[i] if len(scales) > i else scales[-1]
else:
scale = "linear"
par = self.ax.twinx() if i > 0 else self.ax
par.set_yscale(scale)
par.plot(range(numpoints), values, color)
if i > 0:
par.spines["right"].set_position(("outward", (i - 1) * offset))
if unit:
par.set_ylabel(unit)
par.yaxis.label.set_color(color.replace("0x", "#"))
def set_settings(self, settings, save=False):
"""
Apply settings from options ui to the linear view
"""
super().set_settings(settings, save)
pts = (self.linearview_size_settings["plot_title_size"] if self.settings["plot_title_size"] == "default"
else int(self.settings["plot_title_size"]))
als = (self.linearview_size_settings["axes_label_size"] if self.settings["axes_label_size"] == "default"
else int(self.settings["axes_label_size"]))
self.ax.tick_params(axis='both', labelsize=als)
self.ax.set_title("Linear flight profile", fontsize=pts, horizontalalignment='left', x=0)
self.ax.figure.canvas.draw()
class MplCanvas(FigureCanvasQTAgg):
"""Class to represent the FigureCanvasQTAgg widget.
Main axes instance has zorder 99 (important when additional
axes are added).
"""
def __init__(self, plotter):
self.default_filename = "_image"
self.plotter = plotter
# initialization of the canvas
super().__init__(self.plotter.fig)
# we define the widget as expandable
super().setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
# notify the system of updated policy
super().updateGeometry()
def get_default_filename(self):
"""
defines the image file name for storing from a view
return: default png filename of a view
"""
result = self.basename + self.default_filename
if len(result) > 100:
result = result[:100]
return result + ".png"
def draw_metadata(self, title="", init_time=None, valid_time=None,
level=None, style=None):
"""Draw a title indicating the init and valid time of the
image that has been drawn, and the vertical elevation level.
"""
self.default_filename = ""
if title:
self.default_filename += f"_{title.split()[0]:>5}"
if level:
self.default_filename += f"_{level.split()[0]}"
self.plotter.draw_metadata(title, init_time, valid_time, level, style)
self.draw()
# without the repaint the title is not properly updated
self.repaint()
def get_plot_size_in_px(self):
"""Determines the size of the current figure in pixels.
Returns the tuple width, height.
"""
return self.plotter.get_plot_size_in_px()
def get_settings(self):
return self.plotter.get_settings()
def set_settings(self, settings, save=False):
"""
Apply settings from options ui to the linear view
"""
self.plotter.set_settings(settings, save)
def _getSaveFileName(parent, title="Choose a filename to save to", filename="test.png",
filters=" Images (*.png)"):
_dirname, _name = os.path.split(filename)
_dirname = os.path.join(_dirname, "")
return getSaveFileNameAndFilter(parent, fs_url=_dirname, file_pattern=filters,
title=title, default_filename=_name, show_save_action=True)
save_figure_original = NavigationToolbar2QT.save_figure
def save_figure(self, *args):
"""
saves the figure dependent to the filepicker_default
"""
picker_type = config_loader(dataset="filepicker_default")
if picker_type in ["default", "qt"]:
save_figure_original(self, *args)
elif picker_type == "fs":
filetypes = self.canvas.get_supported_filetypes_grouped()
sorted_filetypes = sorted(six.iteritems(filetypes))
startpath = matplotlib.rcParams.get('savefig.directory', LAST_SAVE_DIRECTORY)
startpath = os.path.expanduser(startpath)
start = os.path.join(startpath, self.canvas.get_default_filename())
filters = []
for name, exts in sorted_filetypes:
exts_list = " ".join(['*.%s' % ext for ext in exts])
filter_value = '%s (%s)' % (name, exts_list)
filters.append(filter_value)
fname, filter_value = _getSaveFileName(self.parent,
title="Choose a filename to save to",
filename=start, filters=filters)
if fname is not None:
if not fname.endswith(filter[1:]):
fname = filter.replace('*', fname)
if startpath == '':
# explicitly missing key or empty str signals to use cwd
matplotlib.rcParams['savefig.directory'] = startpath
else:
# save dir for next time
savefig_dir = os.path.dirname(six.text_type(fname))
matplotlib.rcParams['savefig.directory'] = savefig_dir
try:
_dirname, _name = os.path.split(fname)
_fs = open_fs(_dirname)
with _fs.open(_name, 'wb') as source:
self.canvas.print_figure(source, format=filter.replace('*.', ''))
except Exception as e:
QtWidgets.QMessageBox.critical(
self, "Error saving file", six.text_type(e),
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
else:
raise FatalUserError(f"Unknown file picker type '{picker_type}'")
# Patch matplotlib function
NavigationToolbar2QT.save_figure = save_figure
class _Mode(str, enum.Enum):
"""
Override _Mode of backend_base to include our tools.
"""
NONE = ""
PAN = "pan/zoom"
ZOOM = "zoom rect"
INSERT_WP = "insert waypoint"
DELETE_WP = "delete waypoint"
MOVE_WP = "move waypoint"
def __str__(self):
return self.value
@property
def _navigate_mode(self):
return self.name if self is not _Mode.NONE else None
matplotlib.backend_bases._Mode = _Mode
class NavigationToolbar(NavigationToolbar2QT):
"""
parts of this class have been copied from the NavigationToolbar2QT class.
According to https://matplotlib.org/users/license.html we shall
summarise our changes to matplotlib code:
We copied small parts of the given implementation of the navigation
toolbar class to allow for our custom waypoint buttons. Our code extends
the matplotlib toolbar to allow for less or additional buttons and properly
update all plots and elements in case the pan or zoom elements were
triggered by the user.
"""
def __init__(self, canvas, parent, sideview=False, coordinates=True):
self.sideview = sideview
if sideview:
self.toolitems = [
_x for _x in self.toolitems if _x[0] in ('Save',)]
self.set_history_buttons = lambda: None
else:
self.toolitems = [
_x for _x in self.toolitems if
_x[0] in (None, 'Home', 'Back', 'Forward', 'Pan', 'Zoom', 'Save')]
self.toolitems.extend([
(None, None, None, None),
('Mv WP', 'Move waypoints', "wp_move", 'move_wp'),
('Ins WP', 'Insert waypoints', "wp_insert", 'insert_wp'),
('Del WP', 'Delete waypoints', "wp_delete", 'delete_wp'),
])
super().__init__(canvas, parent, coordinates)
self._actions["move_wp"].setCheckable(True)
self._actions["insert_wp"].setCheckable(True)
self._actions["delete_wp"].setCheckable(True)
self.setIconSize(QtCore.QSize(24, 24))
self.layout().setSpacing(12)
self.canvas = canvas
self.no_push_history = False
def _icon(self, name, *args):
"""
wrapper around base method to inject our own icons.
"""
myname = icons("32x32", name)
if os.path.exists(myname):
return QtGui.QIcon(myname)
else:
return super()._icon(name, *args)
def _zoom_pan_handler(self, event):
"""
extend zoom_pan_handler of base class with our own tools
"""
super()._zoom_pan_handler(event)
if event.name == "button_press_event":
if self.mode in (_Mode.INSERT_WP, _Mode.MOVE_WP, _Mode.DELETE_WP):
self.canvas.waypoints_interactor.button_press_callback(event)
elif event.name == "button_release_event":
if self.mode == _Mode.INSERT_WP:
self.canvas.waypoints_interactor.button_release_insert_callback(event)
elif self.mode == _Mode.MOVE_WP:
self.canvas.waypoints_interactor.button_release_move_callback(event)
elif self.mode == _Mode.DELETE_WP:
self.canvas.waypoints_interactor.button_release_delete_callback(event)
def clear_history(self):
self._nav_stack.clear()
self.push_current()
self.set_history_buttons()
def push_current(self):
"""Push the current view limits and position onto the stack."""
if self.sideview:
super().push_current()
elif self.no_push_history:
pass
else:
self._nav_stack.push(self.canvas.map.kwargs.copy())
self.set_history_buttons()
def _update_view(self):
"""
Update the viewlim and position from the view and position stack for
each axes.
"""
if self.sideview:
super()._update_view()
else:
nav_info = self._nav_stack()
if nav_info is None:
return
self.canvas.redraw_map(nav_info)
def insert_wp(self, *args):
"""
activate insert_wp tool
"""
if self.mode == _Mode.INSERT_WP:
self.mode = _Mode.NONE
self.canvas.widgetlock.release(self)
else:
self.mode = _Mode.INSERT_WP
self.canvas.widgetlock(self)
for a in self.canvas.figure.get_axes():
a.set_navigate_mode(self.mode._navigate_mode)