-
Notifications
You must be signed in to change notification settings - Fork 5
/
balto_gui.py
2852 lines (2554 loc) · 119 KB
/
balto_gui.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
"""
This module defines a class called "balto_gui" that can be used to
create a graphical user interface (GUI) for downloading data from
OpenDAP servers from and into a Jupyter notebook. If used with Binder,
this GUI runs in a browser window and does not require the user to
install anything on their computer. However, this module should be
included in the same directory as the Jupyter notebook.
"""
#------------------------------------------------------------------------
#
# Copyright (C) 2020-2022. Scott D. Peckham
#
#------------------------------------------------------------------------
from ipyleaflet import Map, basemaps, FullScreenControl
from ipyleaflet import MeasureControl, Rectangle
## from ipyleaflet import ScaleControl # (doesn't work)
from traitlets import Tuple
## import ipyleaflet as ipyl
import ipywidgets as widgets
from ipywidgets import Layout
from IPython.display import display, HTML
## from IPython.core.display import display
## from IPython.lib.display import display
import pydap.client # (for open_url, etc.)
import requests # (used by get_filenames() )
import json
import datetime # (used by get_duration() )
import copy
import numpy as np
import balto_plot as bp
#------------------------------------------------------------------------
#
# class balto_gui
# __init__()
# pix_str()
# show_gui()
# make_acc_gui()
# make_tab_gui()
# make_data_panel()
# reset_data_panel()
# make_map_panel()
# make_dates_panel()
# make_download_panel()
# make_prefs_panel()
# #--------------------------
# get_map_bounds()
# replace_map_bounds()
# replace_map_bounds2()
# update_map_bounds()
# zoom_out_to_new_bounds()
# --------------------------
# get_url_dir_filenames()
# update_filename_list()
# get_opendap_file_url()
# open_dataset()
# update_data_panel()
# --------------------------
# update_var_info()
# get_all_var_shortnames()
# get_all_var_longnames()
# get_all_var_units()
# --------------------------
# get_var_shortname()
# get_var_longname()
# get_var_units()
# get_var_shape()
# get_var_dimensions()
# get_var_dtype()
# get_var_attributes()
# get_var_time_attributes()
# -------------------------------
# update_datetime_panel()
# get_years_from_time_since()
# clear_datetime_notes()
# append_datetime_notes()
# list_to_string()
# -------------------------------
# pad_with_zeros()
# get_actual_time_units()
# get_time_delta_str()
# get_datetime_obj_from_str()
# get_datetime_obj_from_one_str()
# get_start_datetime_obj()
# get_end_datetime_obj()
# get_dt_from_datetime_str()
# split_datetime_str()
# split_date_str()
# split_time_str()
# get_datetime_from_time_since()
# get_time_since_from_datetime()
# get_month_difference()
# -------------------------------
# get_new_time_index_range()
# get_new_lat_index_range()
# get_new_lon_index_range()
# -------------------------------
# get_duration() ## not used yet
# ----------------------------
# get_download_format()
# clear_download_log()
# append_download_log()
# print_user_choices()
# download_data()
# show_grid()
# -------------------------------
# get_opendap_package() # (in prefs panel)
# ----------------------------
# get_abbreviated_var_name()
# get_possible_svo_names()
#
#------------------------------
# Example GES DISC opendap URL
#------------------------------
# https://gpm1.gesdisc.eosdis.nasa.gov/opendap/GPM_L3/GPM_3IMERGHHE.05/2014/091/
# 3B-HHR-E.MS.MRG.3IMERG.20140401-S000000-E002959.0000.V05B.HDF5.nc
# ?HQprecipitation[1999:2200][919:1049],lon[1999:2200],lat[919:1049]
#------------------------------------------------------------------------
class balto_gui:
#--------------------------------------------------------------------
def __init__(self):
self.version = '0.5'
self.user_var = None
self.default_url_dir = 'http://test.opendap.org/dap/data/nc/'
self.timeout_secs = 60 # (seconds)
#----------------------------------------------------------
# "full_box_width" = (label_width + widget_width)
# gui_width = left_label_width + mid_width + button_width
# The 2nd, label + widget box, is referred to as "next".
# (2 * half_widget_width) + left_label + next_label = 540
#----------------------------------------------------------
self.gui_width = 680
self.left_label_width = 120
self.next_label_width = 50
self.all_label_width = 170
self.full_box_width = 540
self.widget_width = (self.full_box_width - self.left_label_width)
# self.half_widget_width = (self.full_box_width - self.all_label_width)/2
# self.half_widget_width = 183
self.left_widget_width = 230
self.next_widget_width = 136
self.left_box_width = (self.left_label_width + self.left_widget_width)
self.next_box_width = (self.next_label_width + self.next_widget_width)
self.button_width = 70 # big enough for "Reset"
#-----------------------------------------------------
self.map_width = (self.gui_width - 40)
self.map_height = 230 # was 250
self.map_center_init = (20.0, 0)
self.add_fullscreen_control = True
self.add_scale_control = False # (doesn't work)
self.add_measure_control = True
#-----------------------------------------------------
self.gui_width_px = self.pix_str( self.gui_width )
self.map_width_px = self.pix_str( self.map_width )
self.map_height_px = self.pix_str( self.map_height )
#-----------------------------------------------------
self.date_width_px = '240px'
self.time_width_px = '180px'
self.hint_width_px = '120px'
#---------------------------------------------------
self.log_box_width_px = self.pix_str( self.full_box_width )
self.log_box_height_px = '200px'
#---------------------------------------------------
# These styles are used to control width of labels
# self.init_label_style is the initial default.
#---------------------------------------------------
llw_px = self.pix_str( self.left_label_width )
nlw_px = self.pix_str( self.next_label_width )
self.init_label_style = {'description_width': 'initial'}
self.left_label_style = {'description_width': llw_px}
self.next_label_style = {'description_width': nlw_px}
self.date_style = {'description_width': '70px'}
self.time_style = {'description_width': '70px'}
# __init__()
#--------------------------------------------------------------------
def pix_str(self, num):
return str(num) + 'px'
#--------------------------------------------------------------------
def show_gui(self, ACC_STYLE=False, SHOW_MAP=True):
#------------------------------------------------------
# Encountered a problem where there was some problem
# with ipyleaflets (used for the map panel) that
# prevented any part of the GUI from being displayed.
# The SHOW_MAP flag helps to test for this problem.
#------------------------------------------------------
#------------------------------------
# Create & display the complete GUI
#-----------------------------------
if (ACC_STYLE):
self.make_acc_gui()
else:
# Use the TAB style
self.make_tab_gui( SHOW_MAP=SHOW_MAP)
gui_output = widgets.Output()
display(self.gui, gui_output)
# show_gui()
#--------------------------------------------------------------------
def make_acc_gui(self):
gui_width_px = self.gui_width_px
self.make_data_panel()
self.make_map_panel()
self.make_datetime_panel()
self.make_download_panel()
self.make_prefs_panel()
#---------------------------
p0 = self.data_panel
p1 = self.map_panel
p2 = self.datetime_panel
p3 = self.download_panel
p4 = self.prefs_panel
#---------------------------
p0_title = 'Browse Data'
p1_title = 'Spatial Extent'
p2_title = 'Date Range'
p3_title = 'Download Data'
p4_title = 'Settings'
#-------------------------------------------------------
# selected_index=None causes all cells to be collapsed
#-------------------------------------------------------
acc = widgets.Accordion( children=[p0, p1, p2, p3, p4],
selected_index=None,
layout=Layout(width=gui_width_px) )
acc.set_title(0, p0_title)
acc.set_title(1, p1_title)
acc.set_title(2, p2_title)
acc.set_title(3, p3_title)
acc.set_title(4, p4_title)
# title = 'BALTO User Interface'
# L_tags = "<b><font size=5>"
# R_tags = "</font></b>"
# heading = (L_tags + title + R_tags)
pad = self.get_padding(1, HORIZONTAL=False) # 1 lines
head = widgets.HTML(value=f"<b><font size=4>BALTO User Interface</font></b>")
# head = widgets.Label('BALTO User Interface')
# self.gui = widgets.VBox([pad, head, acc]) # (top padding
self.gui = widgets.VBox([head, acc]) # (no top padding)
# make_acc_gui()
#--------------------------------------------------------------------
def make_tab_gui(self, SHOW_MAP=True):
#---------------------------------------------------------
# If there is a problem with ipyleaflet, it can prevent
# any part of the GUI from being displayed. You can
# set SHOW_MAP=False to remove the map to test for this.
#---------------------------------------------------------
gui_width_px = self.gui_width_px
self.make_data_panel()
self.make_map_panel( SHOW_MAP=SHOW_MAP )
self.make_datetime_panel()
self.make_download_panel()
self.make_prefs_panel()
#---------------------------
p0 = self.data_panel
p1 = self.map_panel
p2 = self.datetime_panel
p3 = self.download_panel
p4 = self.prefs_panel
#---------------------------
p0_title = 'Browse Data'
p1_title = 'Spatial Extent'
p2_title = 'Date Range'
p3_title = 'Download Data'
p4_title = 'Settings'
#-------------------------------------------------------
# selected_index=0 shows Browse Data panel
#-------------------------------------------------------
tab = widgets.Tab( children=[p0, p1, p2, p3, p4],
selected_index=0,
layout=Layout(width=gui_width_px) )
tab.set_title(0, p0_title)
tab.set_title(1, p1_title)
tab.set_title(2, p2_title)
tab.set_title(3, p3_title)
tab.set_title(4, p4_title)
#### tab.titles = [str(i) for i in range(len(children))]
# title = 'BALTO User Interface'
# L_tags = "<b><font size=5>"
# R_tags = "</font></b>"
# heading = (L_tags + title + R_tags)
pad = self.get_padding(1, HORIZONTAL=False) # 1 lines
head = widgets.HTML(value=f"<b><font size=5>BALTO User Interface</font></b>")
# head = widgets.Label('BALTO User Interface')
## self.gui = widgets.VBox([pad, head, acc])
self.gui = widgets.VBox([head, tab]) # (no padding above)
# make_tab_gui()
#--------------------------------------------------------------------
def get_padding(self, n, HORIZONTAL=True):
#-------------------------------
# Get some white space padding
#-------------------------------
if (HORIZONTAL):
#--------------------------------
# Use overloaded multiplication
#--------------------------------
## s = (' ' * n) # overloaded multiplication
s = "<p>" + (' ' * n) + "</p>"
pad = widgets.HTML( value=s )
else:
s = ("<br>" * n)
pad = widgets.HTML( value=s )
return pad
# get_padding()
#--------------------------------------------------------------------
def make_data_panel(self):
#-----------------------------------
# Browse data on an OpenDAP server
#-----------------------------------
left_style = self.left_label_style
next_style = self.next_label_style
full_width_px = self.pix_str( self.full_box_width )
left_width_px = self.pix_str( self.left_box_width )
next_width_px = self.pix_str( self.next_box_width )
btn_width_px = self.pix_str( self.button_width )
#---------------------------------------------------------
o1 = widgets.Text(description='OpenDAP URL Dir:',
value=self.default_url_dir,
disabled=False, style=left_style,
layout=Layout(width=full_width_px))
b1 = widgets.Button(description="Go", layout=Layout(width=btn_width_px))
o2 = widgets.Dropdown( description='Filename:',
options=[''], value='',
disabled=False, style=left_style,
layout=Layout(width=full_width_px) )
#------------------------------------------------------------------
oL = widgets.Text(description='Long name:', style=left_style,
value='', layout=Layout(width=full_width_px) )
## o3 = widgets.Select( description='Variable:',
o3 = widgets.Dropdown( description='Variable:',
options=[''], value='',
disabled=False, style=left_style,
layout=Layout(width=left_width_px) )
o4 = widgets.Text(description='Units:', style=next_style,
value='', layout=Layout(width=next_width_px) )
#------------------------------------------------------------------
o5 = widgets.Text(description='Dimensions:', style=left_style,
value='', layout=Layout(width=left_width_px) )
o6 = widgets.Text(description='Shape:', style=next_style,
value='', layout=Layout(width=next_width_px) )
#------------------------------------------------------------------
o7 = widgets.Text(description='Data type:', style=left_style,
value='', layout=Layout(width=full_width_px) )
o8 = widgets.Dropdown( description='Attributes:',
options=[''], value='',
disabled=False, style=left_style,
layout=Layout(width=full_width_px) )
o9 = widgets.Text(description='Status:', style=left_style,
value='Ready.', layout=Layout(width=full_width_px) )
b2 = widgets.Button(description="Reset", layout=Layout(width=btn_width_px))
## pd = widgets.HTML((' ' * 1)) # for padding
#-------------------------------
# Arrange widgets in the panel
#-------------------------------
url_box = widgets.HBox([o1, b1]) # directory + Go button
stat_box = widgets.HBox([o9, b2]) # status + Reset button
name_box = widgets.VBox([o3, o5])
## pad_box = widgets.VBox([pd, pd])
unit_box = widgets.VBox([o4, o6])
mid_box = widgets.HBox([name_box, unit_box])
## mid_box = widgets.HBox([name_box, pad_box, unit_box])
panel = widgets.VBox([url_box, o2, oL, mid_box, o7, o8, stat_box])
self.data_url_dir = o1 # on an OpenDAP server
self.data_filename = o2
self.data_var_long_name = oL
self.data_var_name = o3 # short_name
self.data_var_units = o4
self.data_var_dims = o5
self.data_var_shape = o6
self.data_var_type = o7
self.data_var_atts = o8
self.data_status = o9
self.data_panel = panel
#-----------------
# Event handlers
#-----------------------------------------------------
# Note: NEED to set names='value' here. If names
# keyword is omitted, only works intermittently.
#------------------------------------------------------------
# "on_click" handler function is passed b1 as argument.
# "observe" handler function is passed "change", which
# is a dictionary, as argument. See Traitlet events.
#------------------------------------------------------------
b1.on_click( self.update_filename_list )
b2.on_click( self.reset_data_panel )
o2.observe( self.update_data_panel, names=['options','value'] )
o3.observe( self.update_var_info, names=['options', 'value'] )
## o3.observe( self.update_var_info, names='value' )
## o2.observe( self.update_data_panel, names='All' )
## o3.observe( self.update_var_info, names='All' )
#-------------------------------------------------------
# It turned out this wasn't an issue, but interesting.
#-------------------------------------------------------
# Note: Method functions have type "method" instead
# of "function" and therefore can't be passed
# directly to widget handlers like "on_click".
# But we can use the "__func__" attribute.
#-------------------------------------------------------
# b1.on_click( self.update_filename_list.__func__ )
# o2.observe( self.update_data_panel.__func__ )
# o3.observe( self.update_var_info.__func__, names='value' )
# make_data_panel()
#--------------------------------------------------------------------
def reset_data_panel(self, caller_obj=None, KEEP_DIR=False):
#----------------------------------------------------
# Note: This is called by the "on_click" method of
# the "Reset" button beside the status box.
# In this case, type(caller_obj) =
# <class 'ipywidgets.widgets.widget_button.Button'>
#----------------------------------------------------
if not(KEEP_DIR):
self.data_url_dir.value = self.default_url_dir
self.data_filename.options = ['']
self.data_var_name.options = [''] # short names
self.data_var_long_name.value = ''
self.data_var_units.value = ''
self.data_var_shape.value = ''
self.data_var_dims.value = ''
self.data_var_type.value = ''
self.data_var_atts.options = ['']
self.data_status.value = 'Ready.'
#------------------------------------------
self.download_log.value = ''
# reset_data_panel()
#--------------------------------------------------------------------
def make_map_panel(self, SHOW_MAP=True):
map_width_px = self.map_width_px
map_height_px = self.map_height_px
btn_width_px = self.pix_str( self.button_width )
#--------------------------------------------------
# bm_style = {'description_width': '70px'} # for top
bbox_style = {'description_width': '100px'}
bbox_width_px = '260px'
#---------------------------------------
# Create the map width with ipyleaflet
# Center lat 20 looks better than 0.
#---------------------------------------
map_center = self.map_center_init # (lat, lon)
m = Map(center=map_center, zoom=1,
layout=Layout(width=map_width_px, height=map_height_px))
#----------------------
# Add more controls ?
#----------------------
if (self.add_fullscreen_control):
m.add_control( FullScreenControl( position='topright' ) )
#---------------------------------------------------------
# Cannot be imported. (2020-05-18)
# if (self.add_scale_control):
# m.add_control(ScaleControl( position='bottomleft' ))
#---------------------------------------------------------
if (self.add_measure_control):
measure = MeasureControl( position='bottomright',
active_color = 'orange',
primary_length_unit = 'kilometers')
m.add_control(measure)
measure.completed_color = 'red'
## measure.add_length_unit('yards', 1.09361, 4)
## measure.secondary_length_unit = 'yards'
## measure.add_area_unit('sqyards', 1.19599, 4)
## measure.secondary_area_unit = 'sqyards'
#-----------------------------------------------------
# Does "step=0.01" restrict accuracy of selection ??
#-----------------------------------------------------
w1 = widgets.BoundedFloatText(
value=-180, step=0.01, min=-360, max=360.0,
description='West edge lon:',
disabled=False, style=bbox_style,
layout=Layout(width=bbox_width_px) )
w2 = widgets.BoundedFloatText(
value=180, step=0.01, min=-360, max=360.0,
description='East edge lon:',
disabled=False, style=bbox_style,
layout=Layout(width=bbox_width_px) )
w3 = widgets.BoundedFloatText(
value=90, min=-90, max=90.0, step=0.01,
# description='North latitude:',
description='North edge lat:',
disabled=False, style=bbox_style,
layout=Layout(width=bbox_width_px) )
w4 = widgets.BoundedFloatText(
value=-90, min=-90, max=90.0, step=0.01,
# description='South latitude:',
description='South edge lat:',
disabled=False, style=bbox_style,
layout=Layout(width=bbox_width_px) )
pd = widgets.HTML((' ' * 2)) # for padding
b1 = widgets.Button(description="Update",
layout=Layout(width=btn_width_px))
b2 = widgets.Button(description="Reset",
layout=Layout(width=btn_width_px))
#---------------------
# Choose the basemap
#---------------------
options = self.get_basemap_list()
bm = widgets.Dropdown( description='Base map:',
options=options, value=options[0],
disabled=False, style=bbox_style,
layout=Layout(width='360px') )
#-----------------------------------
# Arrange the widgets in the panel
#-----------------------------------
lons = widgets.VBox([w1, w2])
lats = widgets.VBox([w3, w4])
pads = widgets.VBox([pd, pd])
btns = widgets.VBox([b1, b2])
bbox = widgets.HBox( [lons, lats, pads, btns])
#------------------------------------------------------
# Encountered a problem where there was some problem
# with ipyleaflets (used for the map panel) that
# prevented any part of the GUI from being displayed.
# The SHOW_MAP flag helps to test for this problem.
#------------------------------------------------------
if (SHOW_MAP):
panel = widgets.VBox( [m, bbox, bm] )
else:
panel = widgets.VBox( [bbox, bm] )
self.map_window = m
self.map_minlon = w1
self.map_maxlon = w2
self.map_maxlat = w3
self.map_minlat = w4
self.map_basemap = bm
self.map_panel = panel
## self.map_bounds = (-180, -90, 180, 90)
#-----------------
# Event handlers
#-----------------
bm.observe( self.change_base_map, names=['options','value'] )
m.on_interaction( self.replace_map_bounds )
m.observe( self.zoom_out_to_new_bounds, 'bounds' )
m.new_bounds = None # (used for "zoom to fit")
b1.on_click( self.update_map_bounds )
b2.on_click( self.reset_map_panel )
# make_map_panel()
#--------------------------------------------------------------------
def get_basemap_list(self):
basemap_list = [
'OpenStreetMap.Mapnik', 'OpenStreetMap.HOT', 'OpenTopoMap',
'Esri.WorldStreetMap', 'Esri.DeLorme', 'Esri.WorldTopoMap',
'Esri.WorldImagery', 'Esri.NatGeoWorldMap',
'NASAGIBS.ModisTerraTrueColorCR', 'NASAGIBS.ModisTerraBands367CR',
'NASAGIBS.ModisTerraBands721CR', 'NASAGIBS.ModisAquaTrueColorCR',
'NASAGIBS.ModisAquaBands721CR', 'NASAGIBS.ViirsTrueColorCR',
'NASAGIBS.ViirsEarthAtNight2012',
'Strava.All', 'Strava.Ride', 'Strava.Run', 'Strava.Water',
'Strava.Winter', 'Stamen.Terrain', 'Stamen.Toner',
'Stamen.Watercolor' ]
#---------------------------------
# 'HikeBike.HikeBike', 'MtbMap'
# 'OpenStreetMap.BlackAndWhite',
# 'OpenStreetMap.France',
#----------------------------------
return basemap_list
# get_basemap_list()
#--------------------------------------------------------------------
def change_base_map(self, caller_obj=None):
#--------------------------------------------------------
# Cannot directly change the basemap for some reason.
# self.map_window.basemap = basemaps.Esri.WorldStreetMap
# Need to call clear_layers(), then add_layer().
#---------------------------------------------------------
map_choice = self.map_basemap.value
self.map_window.clear_layers()
basemap_layer = eval( 'basemaps.' + map_choice )
self.map_window.add_layer( basemap_layer )
# For testing
# print('map_choice =', map_choice)
# print('Changed the basemap.')
# change_base_map()
#--------------------------------------------------------------------
def update_map_view(self, caller_obj=None):
pass
# update_map_view()
#--------------------------------------------------------------------
def reset_map_panel(self, caller_obj=None):
self.map_window.center = self.map_center_init
self.map_window.zoom = 1
self.map_minlon.value = '-225.0'
self.map_maxlon.value = '225.0'
self.map_minlat.value = '-51.6'
self.map_maxlat.value = '70.6'
# reset_map_panel()
#--------------------------------------------------------------------
def make_datetime_panel(self):
full_box_width_px = self.pix_str( self.full_box_width )
date_width_px = self.date_width_px
time_width_px = self.time_width_px
hint_width_px = self.hint_width_px
#-----------------------------------
date_style = self.date_style
time_style = self.time_style
d1 = widgets.DatePicker( description='Start Date:',
disabled=False, style=date_style,
layout=Layout(width=date_width_px) )
d2 = widgets.DatePicker( description='End Date:',
disabled=False, style=date_style,
layout=Layout(width=date_width_px) )
d3 = widgets.Text( description='Start Time:',
disabled=False, style=time_style,
layout=Layout(width=time_width_px) )
d4 = widgets.Text( description='End Time:',
disabled=False, style=time_style,
layout=Layout(width=time_width_px) )
d3.value = '00:00:00'
d4.value = '00:00:00'
#-------------------------------
# Add some padding on the left
#-------------------------------
## margin = '0px 0px 2px 10px' # top right bottom left
pp = widgets.HTML((' ' * 3)) # for padding
d5 = widgets.Label( '(hh:mm:ss, 24-hr)',
layout=Layout(width=hint_width_px) )
## layout=Layout(width=hint_width_px, margin=margin) )
## disabled=False, style=hint_style )
d6 = widgets.Label( '(hh:mm:ss, 24-hr)',
layout=Layout(width=hint_width_px) )
## layout=Layout(width=hint_width_px, margin=margin) )
## disabled=False, style=hint_style )
d7 = widgets.Dropdown( description='Attributes:',
options=[''], value='',
disabled=False, style=date_style,
layout=Layout(width=full_box_width_px) )
# d8 = widgets.Text( description='Notes:',
# disabled=False, style=self.date_style,
# layout=Layout(width=full_box_width_px) )
d8 = widgets.Textarea( description='Notes:', value='',
disabled=False, style=self.date_style,
layout=Layout(width=full_box_width_px, height='140px'))
dates = widgets.VBox([d1, d2])
times = widgets.VBox([d3, d4])
hints = widgets.VBox([d5, d6])
pad = widgets.VBox([pp, pp])
top = widgets.HBox([dates, times, pad, hints])
panel = widgets.VBox([top, d7, d8])
## panel = widgets.VBox([top, pp, d7, d8])
self.datetime_start_date = d1
self.datetime_start_time = d3
self.datetime_end_date = d2
self.datetime_end_time = d4
self.datetime_attributes = d7
self.datetime_notes = d8
self.datetime_panel = panel
# make_datetime_panel()
#--------------------------------------------------------------------
def make_download_panel(self):
init_style = self.init_label_style
f1 = widgets.Dropdown( description='Download Format:',
options=['HDF', 'netCDF', 'netCDF4', 'ASCII'],
value='netCDF',
disabled=False, style=init_style)
pad = widgets.HTML(value=f"<p> </p>") # padding
b3 = widgets.Button(description="Download")
h3 = widgets.HBox([f1, pad, b3])
#-----------------------------------
# Could use this for info messages
#-----------------------------------
# status = widgets.Text(description=' Status:', style=self.style0,
# layout=Layout(width='380px') )
width_px = self.log_box_width_px
height_px = self.log_box_height_px
log = widgets.Textarea( description='', value='',
disabled=False, style=init_style,
layout=Layout(width=width_px, height=height_px))
## panel = widgets.VBox([h3, status, log])
panel = widgets.VBox([h3, log])
self.download_format = f1
self.download_button = b3
self.download_log = log
self.download_panel = panel
#-----------------
# Event handlers
#-----------------
b3.on_click( self.download_data )
# make_download_panel()
#--------------------------------------------------------------------
def make_prefs_panel(self):
full_box_width_px = self.pix_str( self.full_box_width )
left_style = self.left_label_style
w1 = widgets.Dropdown( description='OpenDAP package:',
options=['pydap', 'netcdf4'],
value='pydap',
disabled=False, style=left_style)
ts = self.timeout_secs
t1 = widgets.BoundedIntText( description='Timeout:',
value=ts, min=10, max=1000,
step=1, disabled=False,
style=left_style)
t2 = widgets.Label( ' (seconds)',
layout=Layout(width='80px') )
w2 = widgets.HBox([t1, t2])
note = 'Under construction; preferences will go here.'
w3 = widgets.Textarea( description='Notes:', value=note,
disabled=False, style=left_style,
layout=Layout(width=full_box_width_px, height='50px'))
panel = widgets.VBox([w1, w2, w3])
self.prefs_package = w1
self.prefs_timeout = t1
self.prefs_notes = w2
self.prefs_panel = panel
# make_prefs_panel()
#--------------------------------------------------------------------
#--------------------------------------------------------------------
def get_map_bounds(self, FROM_MAP=True, style='sw_and_ne_corners'):
#-------------------------------------------------------
# Notes: ipyleaflet defines "bounds" as:
# [[minlat, maxlat], [minlon, maxlon]]
# matplotlib.imshow defines "extent" as:
# extent = [minlon, maxlon, minlat, maxlat]
#-------------------------------------------------------
# Return value is a list, not a tuple, but
# ok to use it like this:
# [minlon, minlat, maxlon, maxlat] = get_map_bounds().
#-------------------------------------------------------
if (FROM_MAP):
#------------------------------------
# Get the visible map bounds, after
# interaction such as pan or zoom
#------------------------------------
# bounds = self.map_window.bounds
# minlat = bounds[0][0]
# minlon = bounds[0][1]
# maxlat = bounds[1][0]
# maxlon = bounds[1][1]
#------------------------------------
# Is this more reliable ?
#------------------------------------
minlon = self.map_window.west
minlat = self.map_window.south
maxlon = self.map_window.east
maxlat = self.map_window.north
else:
#---------------------------------
# Get map bounds from text boxes
#---------------------------------
minlon = self.map_minlon.value
minlat = self.map_minlat.value
maxlon = self.map_maxlon.value
maxlat = self.map_maxlat.value
#------------------------------------------
# Return map bounds in different "styles"
#------------------------------------------
if (style == 'ipyleaflet'):
bounds = [[minlat, maxlat], [minlon, maxlon]]
elif (style == 'pyplot_imshow'):
bounds = [minlon, maxlon, minlat, maxlat]
elif (style == 'sw_and_ne_corner'):
bounds = [minlon, minlat, maxlon, maxlat]
else:
bounds = [minlon, minlat, maxlon, maxlat]
return bounds
# get_map_bounds()
#--------------------------------------------------------------------
def replace_map_bounds(self, event, type=None, coordinates=None):
#-------------------------------------------
# Get visible map bounds after interaction
# Called by m.on_interaction().
# Don't need to process separate events?
#-------------------------------------------
[minlon, minlat, maxlon, maxlat] = self.get_map_bounds()
#--------------------------------
# Save new values in text boxes
# Format with 8 decimal places.
#--------------------------------
self.map_minlon.value = "{:.8f}".format( minlon )
self.map_maxlon.value = "{:.8f}".format( maxlon )
self.map_maxlat.value = "{:.8f}".format( maxlat )
self.map_minlat.value = "{:.8f}".format( minlat )
# replace_map_bounds()
#--------------------------------------------------------------------
# def replace_map_bounds2(self, event, type=None, coordinates=None):
#
# # events: mouseup, mousedown, mousemove, mouseover,
# # mouseout, click, dblclick, preclick
# event = kwargs.get('type')
# # print('event = ', event)
# if (event == 'mouseup') or (event == 'mousemove') or \
# (event == 'click') or (event == 'dblclick'):
# w1.value = m.west
# w2.value = m.east
# w3.value = m.north
# w4.value = m.south
#
# # status.value = event
#
# # with output2:
# # print( event )
#
#--------------------------------------------------------------------
def update_map_bounds(self, caller_obj=None):
[bb_minlon, bb_minlat, bb_maxlon, bb_maxlat] = \
self.get_map_bounds( FROM_MAP = False )
bb_midlon = (bb_minlon + bb_maxlon) / 2
bb_midlat = (bb_minlat + bb_maxlat) / 2
bb_center = ( bb_midlat, bb_midlon )
# print('bb_minlon, bb_maxlon =', bb_minlon, bb_maxlon)
# print('bb_minlat, bb_maxlat =', bb_minlat, bb_maxlat)
#----------------------------------------------------------
zoom = self.map_window.max_zoom # (usually 18)
self.map_window.center = bb_center
self.map_window.zoom = zoom
## print('max_zoom =', self.map_window.max_zoom)
## print('map_window.bounds =', self.map_window.bounds )
#------------------------------------
# Add "new_bounds" attribute to map
#------------------------------------
new_bounds = ((bb_minlat, bb_minlon), (bb_maxlat, bb_maxlon))
self.map_window.new_bounds = Tuple()
self.map_window.new_bounds = new_bounds
# update_map_bounds()
#--------------------------------------------------------------------
def zoom_out_to_new_bounds(self, change=None):
# change owner is the widget that triggers the handler
m = change.owner
#-----------------------------------------
# If not zoomed all the way out already,
# and we have a target bounding box
#-----------------------------------------
if (m.zoom > 1 and m.new_bounds):
b = m.new_bounds
n = change.new
if (n[0][0] < b[0][0] and n[0][1] < b[0][1] and
n[1][0] > b[1][0] and n[1][1] > b[1][1]):
#---------------------------------------
# new_bounds are now within map window
# Show bounding box as a rectangle ?
# weight = line/stroke thickness
#---------------------------------------
# rectangle = Rectangle( bounds=b, fill=False, weight=4)
# ## fill_opacity=0.0, \ fill_color="#0033FF" )
# m.add_layer(rectangle)
#-----------------------
m.new_bounds = None # (remove target)
else:
# zoom out
m.zoom = m.zoom - 1
# zoom_out_to_new_bounds()
#--------------------------------------------------------------------
# def zoom_out_to_new_bounds_v0(self, caller_obj=None):
#
# [bb_minlon, bb_minlat, bb_maxlon, bb_maxlat] = \
# self.get_map_bounds( FROM_MAP = False )
# bb_midlon = (bb_minlon + bb_maxlon) / 2
# bb_midlat = (bb_minlat + bb_maxlat) / 2
# bb_center = ( bb_midlat, bb_midlon )
# print('bb_minlon, bb_maxlon =', bb_minlon, bb_maxlon)
# print('bb_minlat, bb_maxlat =', bb_minlat, bb_maxlat)
# zoom = self.map_window.max_zoom # (usually 18)
# zoom = zoom - 1
# ## print('max_zoom =', self.map_window.max_zoom)
#
# self.map_window.center = bb_center
# self.map_window.zoom = zoom
# print('map_window.bounds =', self.map_window.bounds )
# # bounds is read-only
# ## self.map_window.bounds = ((bb_midlat,bb_midlon),(bb_midlat,bb_midlon))
# while (True):
# # time.sleep(0.5) ######
# [minlon, minlat, maxlon, maxlat] = self.get_map_bounds()
# print('minlon, maxlon =', minlon, maxlon )
# print('minlat, maxlat =', minlat, maxlat )
# if (minlon < bb_minlon) and (maxlon > bb_maxlon) and \
# (minlat < bb_minlat) and (maxlat > bb_maxlat):
# break
# else:
# zoom -= 1
# if (zoom > 0):
# print('zoom =', zoom)
# self.map_window.zoom = zoom
# else:
# break
#
# [minlon, minlat, maxlon, maxlat] = self.get_map_bounds()
# print('minlon, maxlon =', minlon, maxlon )
# print('minlat, maxlat =', minlat, maxlat )
# if (minlon < bb_minlon) and (maxlon > bb_maxlon) and \
# (minlat < bb_minlat) and (maxlat > bb_maxlat):
# break
# else:
# zoom -= 1
# if (zoom > 0):
# print('zoom =', zoom)
# self.map_window.zoom = zoom
# else:
# break
#
# # zoom_out_to_new_bounds_v0
#--------------------------------------------------------------------
def get_url_dir_filenames(self):
#-----------------------------------------
# Construct a list of filenames that are
# available in the opendap url directory
#-----------------------------------------
r = requests.get( self.data_url_dir.value )
lines = r.text.splitlines()
# n_lines = len(lines)
filenames = list()
for line in lines:
if (".nc<" in line) or (".nc.gz<" in line):
parts = line.split('"')
filename = parts[1].replace('.dmr.html', '')
filenames.append( filename )
return filenames
# get_url_dir_filenames()
#--------------------------------------------------------------------
def get_url_dir_filenames_OLD(self):
#-----------------------------------------
# Construct a list of filenames that are
# available in the opendap url directory
#-----------------------------------------
r = requests.get( self.data_url_dir.value )
lines = r.text.splitlines()
# n_lines = len(lines)
filenames = list()
for line in lines:
if ('"sameAs": "http://' in line) and ('www' not in line):
line = line.replace('.html"', '')