-
Notifications
You must be signed in to change notification settings - Fork 1
/
NDVI.py
1945 lines (1746 loc) · 88 KB
/
NDVI.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
import wx, sqlite3, datetime, copy, csv
import os, sys, re, cPickle, datetime
import scidb
import wx.lib.scrolledpanel as scrolled, wx.grid
import multiprocessing
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
from wx.lib.wordwrap import wordwrap
try:
import win32com.client
hasCom = True
except ImportError:
hasCom = False
try:
from floatcanvas import NavCanvas, FloatCanvas, Resources
except ImportError: # if it's not there locally, try the wxPython lib.
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
import wx.lib.colourdb
ID_DATES_LIST = wx.NewId()
ID_STATIONS_LIST = wx.NewId()
sFmt = '%Y-%m-%d %H:%M:%S'
class ndviDatesList(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, *arg, **kw):
wx.ListCtrl.__init__(self, *arg, **kw)
ListCtrlAutoWidthMixin.__init__(self)
self.setResizeColumn(0) # 1st column will take up any extra spaces
self.InsertColumn(0, 'Dates')
stSQL = 'SELECT Date FROM DataDates;'
rows = scidb.curD.execute(stSQL).fetchall()
for row in rows:
self.Append((row['Date'],))
class ndviStationsList(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, *arg, **kw):
wx.ListCtrl.__init__(self, *arg, **kw)
ListCtrlAutoWidthMixin.__init__(self)
self.setResizeColumn(0) # 1st column will take up any extra spaces
self.InsertColumn(0, 'Stations')
stSQL = 'SELECT ID, StationName FROM Stations;'
scidb.fillListctrlFromSQL(self, stSQL)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, lambda evt: self.onClick_StaList(evt))
def onClick_StaList(self, event):
currentItem = event.m_itemIndex
i = self.GetItemData(currentItem)
print 'stations list clicked: "', event.GetText(), '", record ID:', i
print 'recID using selection: ', self.GetItemData(self.GetFocusedItem())
class NDVIPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.InitUI()
def InitUI(self):
#horizontal split means the split goes across
#vertical split means the split goes up and down
vSplit = wx.SplitterWindow(self, -1)
hSplit = wx.SplitterWindow(vSplit, -1)
optsSplit = wx.SplitterWindow(hSplit, -1)
ndviDatesPanel = wx.Panel(optsSplit, -1)
self.InitDatesPanel(ndviDatesPanel)
ndviStationsPanel = wx.Panel(optsSplit, -1)
self.InitStationsPanel(ndviStationsPanel)
optsSplit.SplitVertically(ndviDatesPanel, ndviStationsPanel, sashPosition=100)
optsSplit.SetMinimumPaneSize(100)
optsSplit.SetSashGravity(0.5)
previewPanel = wx.Panel(hSplit, -1)
self.InitPreviewPanel(previewPanel)
hSplit.SplitHorizontally(optsSplit, previewPanel)
hSplit.SetMinimumPaneSize(100)
hSplit.SetSashGravity(0.5)
self.ndviSetupPanel = scrolled.ScrolledPanel(vSplit, -1)
self.InitNDVISetupPanel(self.ndviSetupPanel)
vSplit.SplitVertically(self.ndviSetupPanel, hSplit, sashPosition=600)
vSplit.SetMinimumPaneSize(100)
vSplit.SetSashGravity(0.5)
OverallSizer = wx.BoxSizer(wx.HORIZONTAL)
OverallSizer.Add(vSplit, 1, wx.EXPAND)
self.SetSizer(OverallSizer)
def InitNDVISetupPanel(self, pnl):
self.LayoutNDVISetupPanel(pnl)
# get existing or blank record
# stSQL = "SELECT * FROM NDVIcalc WHERE ID = ?;"
# rec = scidb.curD.execute(stSQL, (0,)).fetchone()
# stSQL = "SELECT * FROM NDVIcalc;"
# rec = scidb.curD.execute(stSQL).fetchone()
# if rec == None:
# print "no records yet in 'NDVIcalc'"
self.calcDict = scidb.dictFromTableDefaults('NDVIcalc')
# else:
# calcDict = copy.copy(rec) # this crashes
# print 'scidb.curD.description:', scidb.curD.description
# self.calcDict = {}
# for recName in rec.keys():
# self.calcDict[recName] = rec[recName]
# print "self.calcDict:", self.calcDict
self.FillNDVISetupPanelFromCalcDict()
def LayoutNDVISetupPanel(self, pnl):
pnl.SetBackgroundColour(wx.WHITE)
iLinespan = 5
stpSiz = wx.GridBagSizer(1, 1)
gRow = 0
stpLabel = wx.StaticText(pnl, -1, 'Set up NDVI calculations here')
stpSiz.Add(stpLabel, pos=(gRow, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Retrieve panel:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.cbxGetPanel = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
stpSiz.Add(self.cbxGetPanel, pos=(gRow, 1), span=(1, 3), flag=wx.LEFT, border=5)
self.cbxGetPanel.Bind(wx.EVT_COMBOBOX, lambda evt: self.onCbxRetrievePanel(evt))
self.refresh_cbxPanelsChoices(-1)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Name for this panel'),
pos=(gRow, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcCalcName = wx.TextCtrl(pnl)
stpSiz.Add(self.tcCalcName, pos=(gRow, 2), span=(1, 3),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
# following is rarely used, not implemented yet
# gRow += 1
# self.ckChartFromRefDay = wx.CheckBox(pnl, label="Chart using")
# stpSiz.Add(self.ckChartFromRefDay, pos=(gRow, 0), span=(1, 2), flag=wx.LEFT|wx.BOTTOM, border=5)
# self.tcRefDay = wx.TextCtrl(pnl)
# stpSiz.Add(self.tcRefDay, pos=(gRow, 2), span=(1, 1),
# flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
# stpSiz.Add(wx.StaticText(pnl, -1, 'as day 1'),
# pos=(gRow, 3), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Reference station:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.ckUseRef = wx.CheckBox(pnl, label="Use Reference")
stpSiz.Add(self.ckUseRef, pos=(gRow, 1), span=(1, 1),
flag=wx.ALIGN_LEFT|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.ckUseRef.SetValue(True)
gRow += 1
stSQLStations = 'SELECT ID, StationName FROM Stations;'
self.cbxRefStationID = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
scidb.fillComboboxFromSQL(self.cbxRefStationID, stSQLStations)
stpSiz.Add(self.cbxRefStationID, pos=(gRow, 0), span=(1, 4), flag=wx.LEFT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'For the reference station:'),
pos=(gRow, 0), span=(1, 3), flag=wx.LEFT, border=30)
stSQLSeries = 'SELECT ID, DataSeriesDescription FROM DataSeries;'
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'IR is in:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.cbxIRRefSeriesID = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
scidb.fillComboboxFromSQL(self.cbxIRRefSeriesID, stSQLSeries)
stpSiz.Add(self.cbxIRRefSeriesID, pos=(gRow, 1), span=(1, 3), flag=wx.LEFT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'VIS is in:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.cbxVISRefSeriesID = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
scidb.fillComboboxFromSQL(self.cbxVISRefSeriesID, stSQLSeries)
stpSiz.Add(self.cbxVISRefSeriesID, pos=(gRow, 1), span=(1, 3), flag=wx.LEFT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'For the other stations (selected to the right):'),
pos=(gRow, 0), span=(1, 4), flag=wx.LEFT, border=30)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'IR is in:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.cbxIRDataSeriesID = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
scidb.fillComboboxFromSQL(self.cbxIRDataSeriesID, stSQLSeries)
stpSiz.Add(self.cbxIRDataSeriesID, pos=(gRow, 1), span=(1, 3), flag=wx.LEFT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'VIS is in:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.cbxVisDataSeriesID = wx.ComboBox(pnl, -1, style=wx.CB_READONLY)
scidb.fillComboboxFromSQL(self.cbxVisDataSeriesID, stSQLSeries)
stpSiz.Add(self.cbxVisDataSeriesID, pos=(gRow, 1), span=(1, 3), flag=wx.LEFT, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stAboutFns = 'Functions to process raw bands into IR and VIS signals. Use "i" and "v" for ' \
'raw IR- and VIS-containing bands respectively.'
stWr = wordwrap(stAboutFns, 450, wx.ClientDC(pnl))
stpSiz.Add(wx.StaticText(pnl, -1, stWr),
pos=(gRow, 0), span=(1, 5), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'IR:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcIRFunction = wx.TextCtrl(pnl)
stpSiz.Add(self.tcIRFunction, pos=(gRow, 1), span=(1, 4),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'VIS:'),
pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcVISFunction = wx.TextCtrl(pnl)
stpSiz.Add(self.tcVISFunction, pos=(gRow, 1), span=(1, 4),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Cutoff, hours +/- solar noon:'),
pos=(gRow, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcPlusMinusCutoffHours = wx.TextCtrl(pnl)
stpSiz.Add(self.tcPlusMinusCutoffHours, pos=(gRow, 2), span=(1, 1),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Only include readings from'),
pos=(gRow, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcThresholdPctLow = wx.TextCtrl(pnl)
stpSiz.Add(self.tcThresholdPctLow, pos=(gRow, 2), span=(1, 1),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
stpSiz.Add(wx.StaticText(pnl, -1, 'to'),
pos=(gRow, 3), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcThresholdPctHigh = wx.TextCtrl(pnl)
stpSiz.Add(self.tcThresholdPctHigh, pos=(gRow, 4), span=(1, 1),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'percent of solar maximum on the clear day:'),
pos=(gRow, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcClearDay = wx.TextCtrl(pnl)
stpSiz.Add(self.tcClearDay, pos=(gRow, 2), span=(1, 2),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
self.ckUseOnlyValidNDVI = wx.CheckBox(pnl, label="Use only")
stpSiz.Add(self.ckUseOnlyValidNDVI, pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcNDVIvalidMin = wx.TextCtrl(pnl)
stpSiz.Add(self.tcNDVIvalidMin, pos=(gRow, 1), span=(1, 1),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
stpSiz.Add(wx.StaticText(pnl, -1, '<= NDVI <='),
pos=(gRow, 2), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
self.tcNDVIvalidMax = wx.TextCtrl(pnl)
stpSiz.Add(self.tcNDVIvalidMax, pos=(gRow, 3), span=(1, 1),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
self.ckIncludeSummaries = wx.CheckBox(pnl,
label="Create daily summaries. (Excel output will include charts)")
stpSiz.Add(self.ckIncludeSummaries, pos=(gRow, 0), span=(1, 4),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
self.ckIncludeSummaries.SetValue(False)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
self.ckIncludeSAS = wx.CheckBox(pnl,
label="Also output data for SAS")
stpSiz.Add(self.ckIncludeSAS, pos=(gRow, 0), span=(1, 4),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
self.ckIncludeSAS.SetValue(False)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
self.ckNormalize = wx.CheckBox(pnl,
label="Normalize NDVI into range 0-to-1, i.e. view only relative changes")
stpSiz.Add(self.ckNormalize, pos=(gRow, 0), span=(1, 4),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
self.ckNormalize.SetValue(False)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, iLinespan), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticText(pnl, -1, 'Output As:'),
pos=(gRow, 0), span=(1, 2), flag=wx.LEFT|wx.BOTTOM, border=5)
stAboutOutput = ' Excel uses the Base Name for a file, the other options create a ' \
'set of files in the Base Name folder. With Excel, you will see the dataset as ' \
'it builds. Other options do not show as the files build on disk.'
stWrO = wordwrap(stAboutOutput, 450, wx.ClientDC(pnl))
stpSiz.Add(wx.StaticText(pnl, -1, stWrO),
pos=(gRow, 2), span=(4, 3), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
iRBLeftBorderWd = 20
gRow += 1
self.rbExcel = wx.RadioButton(pnl, label='Excel workbook', style=wx.RB_GROUP)
stpSiz.Add(self.rbExcel, pos=(gRow, 0), span=(1, 2), flag=wx.ALIGN_LEFT|wx.LEFT, border=iRBLeftBorderWd)
gRow += 1
self.rbTabDelim = wx.RadioButton(pnl, label='Tab-delimited text')
stpSiz.Add(self.rbTabDelim, pos=(gRow, 0), span=(1, 2), flag=wx.ALIGN_LEFT|wx.LEFT, border=iRBLeftBorderWd)
gRow += 1
self.rbCommaDelim = wx.RadioButton(pnl, label='Comma-separated values ("CSV")')
stpSiz.Add(self.rbCommaDelim, pos=(gRow, 0), span=(1, 2), flag=wx.ALIGN_LEFT|wx.LEFT, border=iRBLeftBorderWd)
gRow += 1
stpSiz.Add((0, 10), pos=(gRow, 0), span=(1, 3)) # some space
gRow += 1
# self.btnBrowseDir = wx.Button(self, label="Dir", size=(90, 28))
self.btnBrowseDir = wx.Button(pnl, label="Dir", size=(-1, -1))
self.btnBrowseDir.Bind(wx.EVT_BUTTON, lambda evt: self.onClick_BtnGetDir(evt))
stpSiz.Add(self.btnBrowseDir, pos=(gRow, 0), flag=wx.LEFT, border=5)
stpSiz.Add(wx.StaticText(pnl, -1, 'Base Name, file or folder:'),
pos=(gRow, 1), span=(1, 1), flag=wx.ALIGN_RIGHT|wx.LEFT, border=5)
self.tcBaseName = wx.TextCtrl(pnl, -1)
stpSiz.Add(self.tcBaseName, pos=(gRow, 2), span=(1, 3),
flag=wx.ALIGN_LEFT|wx.EXPAND, border=5)
gRow += 1
self.tcDir = wx.TextCtrl(pnl, -1, style=wx.TE_MULTILINE)
stpSiz.Add(self.tcDir, pos=(gRow, 0), span=(2, 5),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM|wx.EXPAND, border=5)
gRow += 1 # space for the 2 grid rows for tcDir
self.tcDir.SetValue('(save output in default directory)')
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, 5), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, 5), flag=wx.EXPAND)
gRow += 1
sheetNoteBlocking1 = wx.StaticText(pnl, -1, 'NOTE: Making a dataset will block this application until complete.')
# sheetNoteBlocking1.SetFont(bolded)
stpSiz.Add(sheetNoteBlocking1, pos=(gRow, 0), span=(1, 5), flag=wx.TOP|wx.LEFT, border=5)
gRow += 1
sheetNoteBlocking2 = wx.StaticText(pnl, -1, ' A full dataset can take a long time.')
# sheetNoteBlocking2.SetFont(bolded)
stpSiz.Add(sheetNoteBlocking2, pos=(gRow, 0), span=(1, 5), flag=wx.LEFT|wx.BOTTOM, border=5)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, 5), flag=wx.EXPAND)
gRow += 1
stpSiz.Add(wx.StaticLine(pnl), pos=(gRow, 0), span=(1, 5), flag=wx.EXPAND)
gRow += 1
self.ckPreview = wx.CheckBox(pnl, label="Preview, Rows:")
stpSiz.Add(self.ckPreview, pos=(gRow, 0), span=(1, 1),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
self.ckPreview.SetValue(True)
self.ckPreview.Bind(wx.EVT_CHECKBOX, self.onCkPreview)
self.spinPvwRows = wx.SpinCtrl(pnl, -1, '', size=(50,-1))
self.spinPvwRows.SetRange(1,100)
self.spinPvwRows.SetValue(10)
stpSiz.Add(self.spinPvwRows, pos=(gRow, 1), span=(1, 1),
flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
# stpSiz.Add(wx.StaticText(self, -1, ' Rows'),
# pos=(gRow, 2), flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=5)
self.btnMake = wx.Button(pnl, label="Make", size=(90, 28))
self.btnMake.Bind(wx.EVT_BUTTON, lambda evt: self.onClick_BtnMake(evt))
stpSiz.Add(self.btnMake, pos=(gRow, 3), flag=wx.LEFT|wx.BOTTOM, border=5)
gRow += 1
self.tcProgress = wx.TextCtrl(pnl, -1, style=wx.TE_MULTILINE|wx.TE_READONLY)
stpSiz.Add(self.tcProgress, pos=(gRow, 0), span=(4, 5),
flag=wx.ALIGN_LEFT|wx.TOP|wx.LEFT|wx.BOTTOM|wx.EXPAND, border=5)
gRow += 1 # space for the 4 grid rows for tcProgress
gRow += 1
gRow += 1
# offer some estimate diagnostics
self.tcProgress.AppendText('Time estimate for full dataset: ')
fEstSecsPerQuery = 0.2
# possibly call an estimation function here
self.tcProgress.AppendText('(estimation not implemented yet)')
gRow += 1
self.btnSavePnl = wx.Button(pnl, label="Save\npanel", size=(-1, -1))
self.btnSavePnl.Bind(wx.EVT_BUTTON, lambda evt: self.onClick_BtnSavePnl(evt))
stpSiz.Add(self.btnSavePnl, pos=(gRow, 0), flag=wx.LEFT|wx.BOTTOM, border=5)
stpSiz.Add(wx.StaticText(pnl, -1, 'Tasks:'),
pos=(gRow, 1), span=(1, 1), flag=wx.ALIGN_RIGHT|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
lTasks = ['Make this NDVI dataset','Copy the current panel, which you can then vary']
self.cbxTasks = wx.ComboBox(pnl, -1, choices=lTasks, style=wx.CB_READONLY)
self.cbxTasks.Bind(wx.EVT_COMBOBOX, lambda evt: self.onCbxTasks(evt))
stpSiz.Add(self.cbxTasks, pos=(gRow, 2), span=(1, 3), flag=wx.LEFT, border=5)
pnl.SetSizer(stpSiz)
pnl.SetAutoLayout(1)
pnl.SetupScrolling()
def onCkPreview(self, evt):
if self.ckPreview.GetValue():
self.spinPvwRows.Enable(True)
else:
self.spinPvwRows.Enable(False)
def onClick_BtnGetDir(self, event):
"""
Get the directory to save the file we are about to make
"""
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
#| wx.DD_DIR_MUST_EXIST
#| wx.DD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
self.tcDir.SetValue(dlg.GetPath())
# print "You chose %s" % dlg.GetPath()
dlg.Destroy()
def onClick_BtnMake(self, event):
"""
Make the dataset
"""
pass
return # for testing, stop here
# test if our file save info is valid
stDir = self.tcDir.GetValue()
if not os.path.exists(stDir):
stDir = os.path.expanduser('~') # user doesn't like? next time choose one
self.tcDir.SetValue(stDir)
stBaseName = self.tcBaseName.GetValue()
# if stBaseName[0] == '(': # assume '(default filename)' was sitting there
# stBaseName = self.stItemName #default
# stBaseName = "".join(x for x in stBaseName if x.isalnum())
# self.tcBaseName.SetValue(stBaseName)
stSavePath = os.path.join(stDir, stBaseName)
print "stSavePath:", stSavePath
if self.rbExcel.GetValue():
stSavePath = stSavePath + '.xlsx'
if self.rbTabDelim.GetValue():
stSavePath = stSavePath + '.txt'
if self.rbCommaDelim.GetValue():
stSavePath = stSavePath + '.csv'
if os.path.isfile(stSavePath):
stMsg = '"' + stSavePath + '" already exists. Overwrite?'
dlg = wx.MessageDialog(self, stMsg, 'File Exists', wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
# print "result of Yes/No dialog:", result
if result == wx.ID_YES:
try:
os.remove(stSavePath)
except:
wx.MessageBox("Can't delete old file. Is it still open?", 'Info',
wx.OK | wx.ICON_INFORMATION)
return
else:
return
if self.rbExcel.GetValue():
self.makeExcel()
return # end of if Excel
else: # one of the text formats
if self.sourceTable == 'OutputSheets': # going to create only one file, from this one sheet
shDict = self.lShs[0]
self.makeTextFile(0, shDict) # explictily pass the sheet dictionary
return
if self.sourceTable == 'OutputBooks': # process the whole book
if self.bkDict['PutAllOutputRowsInOneSheet'] == 1:
self.makeTextFile(1, None) # flag to compile the whole book into one file, no shDict needed
else:
iNumSheets = len(self.lShs)
iSheetCt = 0
for shDict in self.lShs:
iSheetCt += 1
stMsg = 'Doing sheet "' + shDict['WorksheetName'] + '", ' + str(iSheetCt) + ' of ' + str(iNumSheets)
self.tcOutputOptInfo.SetValue(stMsg)
self.makeTextFile(0, shDict) # explictily pass each sheet dictionary
return
return # end of if text file(s)
def makeExcel(self):
"""
Make an Excel workbook
"""
if hasCom == False: # we tested for this at the top of this module
wx.MessageBox('This operating system cannot make Excel files', 'Info',
wx.OK | wx.ICON_INFORMATION)
return
try:
oXL = win32com.client.Dispatch("Excel.Application")
oXL.Visible = 1
except:
wx.MessageBox('Excel is not on this computer', 'Info',
wx.OK | wx.ICON_INFORMATION)
return
bXL = oXL.Workbooks.Add()
#remove any extra sheets
while bXL.Sheets.Count > 1:
# print "Workbook has this many sheets:", bXL.Sheets.Count
bXL.Sheets(1).Delete()
shXL = bXL.Sheets(1)
boolSheetReady = True
# before we go any further, try saving file
stDir = self.tcDir.GetValue()
stBaseName = self.tcBaseName.GetValue()
stSavePath = os.path.join(stDir, stBaseName) + '.xlsx'
if os.path.isfile(stSavePath):
stMsg = '"' + stSavePath + '" already exists. Overwrite?'
dlg = wx.MessageDialog(self, stMsg, 'File Exists', wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
# print "result of Yes/No dialog:", result
if result == wx.ID_YES:
try:
os.remove(stSavePath)
except:
wx.MessageBox("Can't delete old file. Is it still open?", 'Info',
wx.OK | wx.ICON_INFORMATION)
return
else:
return
try:
bXL.SaveAs(stSavePath) # make sure there's nothing invalid about the filename
except:
wx.MessageBox('Can not save file "' + stSavePath + '"', 'Info',
wx.OK | wx.ICON_INFORMATION)
return
self.tcOutputOptInfo.SetValue(' Creating Excel file "' + stSavePath + '"\n')
for shDict in self.lShs:
if boolSheetReady == False:
self.tcOutputOptInfo.SetValue(self.tcOutputOptInfo.GetValue() + 'Sheet "' + shXL.Name + '"\n')
print shXL.Name
sPrevShNm = shXL.Name
#shXL = bXL.Sheets.Add() # this works
#print shXL.Name # this works, and is the expected new sheet 1st in the book
#shXL.Move(After=bXL.Sheets(sPrevShNm)) # this moves sheet to a new book, then crashes
oXL.Worksheets.Add(After=oXL.Sheets(sPrevShNm)) # creats sheet, but 1st in the book
#worksheets.Add(After=worksheets(worksheets.Count)) # creats sheet, but 1st in the book
shXL = oXL.ActiveSheet
print shXL.Name
boolSheetReady = True
iSheetID = shDict['ID']
dsRow = 1
dsCol = 1
# name the worksheet
# shXL = bXL.Sheets(1)
shXL.Name = shDict['WorksheetName']
boolSheetReady = False # sheet has been used, next loop will add a new one
#set up the column headings
stSQL = "SELECT Count(ID) AS CountOfID, ColumnHeading, " \
"AggType, Format_Excel, ListingOrder " \
"FROM OutputColumns GROUP BY WorksheetID, ColumnHeading, " \
"AggType, Format_Excel, ListingOrder " \
"HAVING WorksheetID = ? " \
"ORDER BY ListingOrder;"
scidb.curD.execute(stSQL, (iSheetID,))
recs = scidb.curD.fetchall()
for rec in recs:
shXL.Cells(dsRow,rec['ListingOrder']).Value = rec['ColumnHeading']
#apply column formats
if rec['Format_Excel'] != None:
try:
shXL.Columns(rec['ListingOrder']).NumberFormat = rec['Format_Excel']
except:
print 'Invalid Excel format, column ' + str(rec['ListingOrder'])
if self.ckPreview.GetValue():
iNumRowsToPreview = self.spinPvwRows.GetValue()
else:
iNumRowsToPreview = 1000000 # improve this
iPreviewCt = 0
# use the row generator
# waitForExcel = wx.BusyInfo("Making Excel output")
sheetRows = scidb.generateSheetRows(iSheetID, formatValues = False)
for dataRow in sheetRows:
# yielded object is list with as many members as there are grid columns
iPreviewCt += 1
if iPreviewCt > iNumRowsToPreview:
break
dsRow += 1
for dsCol in range(len(dataRow)):
shXL.Cells(dsRow,dsCol+1).Value = dataRow[dsCol]
# del waitForExcel
shXL.Columns.AutoFit()
bXL.Save()
# oXL.Cells(1,1).Value = "Hello"
def makeTextFile(self, combineSheets, shDict):
"""
Make a text file
"""
# wx.MessageBox('Called makeTextFile function', 'Info',
# wx.OK | wx.ICON_INFORMATION)
stDir = self.tcDir.GetValue()
stBaseName = self.tcBaseName.GetValue()
stBasePath = os.path.join(stDir, stBaseName)
if combineSheets == 0: # not making the whole book into one file, use base name for folder and add filename
if not os.path.exists(stBasePath):
try:
os.makedirs(stBasePath)
except:
wx.MessageBox('Can not create folder "' + stBasePath + '"', 'Info',
wx.OK | wx.ICON_INFORMATION)
return
stSavePath = os.path.join(stBasePath, shDict['WorksheetName'])
else:
stSavePath = stBasePath
if self.rbTabDelim.GetValue():
stSavePath = stSavePath + '.txt'
if self.rbCommaDelim.GetValue():
stSavePath = stSavePath + '.csv'
if os.path.isfile(stSavePath):
stMsg = '"' + stSavePath + '" already exists. Overwrite?'
dlg = wx.MessageDialog(self, stMsg, 'File Exists', wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
# print "result of Yes/No dialog:", result
if result == wx.ID_YES:
try:
os.remove(stSavePath)
except:
wx.MessageBox("Can't delete old file. Is it still open?", 'Info',
wx.OK | wx.ICON_INFORMATION)
return
else:
return
try: # before we go any further
# make sure there's nothing invalid about the filename
fOut = open(stSavePath, 'wb')
except:
wx.MessageBox('Can not create file "' + stSavePath + '"', 'Info',
wx.OK | wx.ICON_INFORMATION)
return
if self.rbTabDelim.GetValue():
wr = csv.writer(fOut, delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
if self.rbCommaDelim.GetValue():
wr = csv.writer(fOut, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
self.tcProgress.SetValue('Setting up column headings')
# item unless book is specified to go all in one block (not implemented yet)
# get the number of columns
stSQL = 'SELECT MAX(CAST(ListingOrder AS INTEGER)) AS Ct ' \
'FROM OutputColumns WHERE WorksheetID = ?'
scidb.curD.execute(stSQL, (shDict['ID'],))
rec = scidb.curD.fetchone()
lColHds = ['' for x in range(rec['Ct'])]
#set up the column headings
stSQL = "SELECT Count(ID) AS CountOfID, " \
"ColumnHeading, AggType, ListingOrder " \
"FROM OutputColumns " \
"GROUP BY WorksheetID, ColumnHeading, AggType, ListingOrder " \
"HAVING WorksheetID = ? " \
"ORDER BY ListingOrder;"
scidb.curD.execute(stSQL, (shDict['ID'],))
recs = scidb.curD.fetchall()
for rec in recs:
lColHds[rec['ListingOrder']-1] = rec['ColumnHeading']
wr.writerow(lColHds)
lMsg = []
lMsg.append('')
lMsg.append('')
lMsg.append(' rows written to "')
lMsg.append(stSavePath)
lMsg.append('".')
if self.ckPreview.GetValue():
iNumRowsToDo = self.spinPvwRows.GetValue()
lMsg[0] = 'Preview of '
lMsg[1] = str(iNumRowsToDo)
else:
iNumRowsToDo = 1000000 # improve this
lMsg[0] = 'Total of '
iRowCt = 0
# get a count
iNumRowsInDataset = scidb.countOfSheetRows(shDict['ID'])
# use the row generator
sheetRows = scidb.generateSheetRows(shDict['ID'])
for dataRow in sheetRows:
# yielded object is list with as many members as there are columns
iRowCt += 1
if iRowCt > iNumRowsToDo:
break
self.tcProgress.SetValue('Doing row %d of %d' % (iRowCt, iNumRowsInDataset))
wx.Yield() # allow window updates to occur
wr.writerow(dataRow)
lMsg[1] = str(iRowCt)
fOut.close()
wx.MessageBox("".join(lMsg), 'Info',
wx.OK | wx.ICON_INFORMATION)
return # end of if CSV
def FillNDVISetupPanelFromCalcDict(self):
"""
Fills the NDVI Setup panel from calcDict.
Must explicitly clear a panel control if corresponding dictionary item is None because
the panel may already have controls filled in from previous work.
If all comboboxes in the panel correspond the dictionary items, maybe use a form like this:
for object in self.parent.GetChildren():
if type(object) == wx._controls.ComboBox:
object.SetSelection(-1)
"""
if self.calcDict['CalcName'] == None:
self.tcCalcName.SetValue('')
else:
self.tcCalcName.SetValue('%s' % self.calcDict['CalcName'])
# following is rarely used, not implemented yet
# self.ckChartFromRefDay.SetValue(self.calcDict['ChartFromRefDay'])
# if self.calcDict['RefDay'] != None:
# self.tcRefDay.SetValue('%s' % self.calcDict['RefDay'])
if self.calcDict['RefStationID'] == None:
self.cbxRefStationID.SetSelection(-1)
else:
scidb.setComboboxToClientData(self.cbxRefStationID, self.calcDict['RefStationID'])
if self.calcDict['IRRefSeriesID'] == None:
self.cbxIRRefSeriesID.SetSelection(-1)
else:
scidb.setComboboxToClientData(self.cbxIRRefSeriesID, self.calcDict['IRRefSeriesID'])
if self.calcDict['VISRefSeriesID'] == None:
self.cbxVISRefSeriesID.SetSelection(-1)
else:
scidb.setComboboxToClientData(self.cbxVISRefSeriesID, self.calcDict['VISRefSeriesID'])
self.ckUseRef.SetValue(self.calcDict['UseRef'])
if self.calcDict['IRDataSeriesID'] == None:
self.cbxIRDataSeriesID.SetSelection(-1)
else:
scidb.setComboboxToClientData(self.cbxIRDataSeriesID, self.calcDict['IRDataSeriesID'])
if self.calcDict['VisDataSeriesID'] == None:
self.cbxVisDataSeriesID.SetSelection(-1)
else:
scidb.setComboboxToClientData(self.cbxVisDataSeriesID, self.calcDict['VisDataSeriesID'])
if self.calcDict['IRFunction'] == None:
self.tcIRFunction.SetValue('=i')
else:
self.tcIRFunction.SetValue('%s' % self.calcDict['IRFunction'])
if self.calcDict['VISFunction'] == None:
self.tcVISFunction.SetValue('=v')
else:
self.tcVISFunction.SetValue('%s' % self.calcDict['VISFunction'])
if self.calcDict['PlusMinusCutoffHours'] == None:
self.tcPlusMinusCutoffHours.SetValue('2')
else:
self.tcPlusMinusCutoffHours.SetValue('%.1f' % self.calcDict['PlusMinusCutoffHours'])
# Opt1ClrDayVsSetTholds ignored, always use percent thresholds of clear day
if self.calcDict['ClearDay'] == None:
self.tcClearDay.SetValue('')
else:
self.tcClearDay.SetValue('%s' % self.calcDict['ClearDay'])
if self.calcDict['ThresholdPctLow'] == None:
self.tcThresholdPctLow.SetValue('75')
else:
self.tcThresholdPctLow.SetValue('%d' % self.calcDict['ThresholdPctLow'])
if self.calcDict['ThresholdPctHigh'] == None:
self.tcThresholdPctHigh.SetValue('125')
else:
self.tcThresholdPctHigh.SetValue('%d' % self.calcDict['ThresholdPctHigh'])
# not implemented: IRRefCutoff, VISRefCutoff, IRDatCutoff, VISDatCutoff
self.ckUseOnlyValidNDVI.SetValue(self.calcDict['UseOnlyValidNDVI'])
if self.calcDict['NDVIvalidMin'] == None:
self.tcNDVIvalidMin.SetValue('-1')
else:
self.tcNDVIvalidMin.SetValue('%.2f' % self.calcDict['NDVIvalidMin'])
if self.calcDict['NDVIvalidMax'] == None:
self.tcNDVIvalidMax.SetValue('1')
else:
self.tcNDVIvalidMax.SetValue('%.2f' % self.calcDict['NDVIvalidMax'])
self.ckIncludeSummaries.SetValue(self.calcDict['CreateSummaries'])
self.ckIncludeSAS.SetValue(self.calcDict['OutputSAS'])
self.ckNormalize.SetValue(self.calcDict['Normalize'])
# set radio buttons, default false
self.rbExcel.SetValue(0)
self.rbTabDelim.SetValue(0)
self.rbCommaDelim.SetValue(0)
if self.calcDict['OutputFormat'] == 1:
self.rbExcel.SetValue(1)
if self.calcDict['OutputFormat'] == 2:
self.rbTabDelim.SetValue(1)
if self.calcDict['OutputFormat'] == 3:
self.rbCommaDelim.SetValue(1)
if self.calcDict['OutputBaseName'] == None:
self.tcBaseName.SetValue('')
else:
self.tcBaseName.SetValue('%s' % self.calcDict['OutputBaseName'])
if self.calcDict['OutputFolder'] == None:
self.tcDir.SetValue('(save output in default directory)')
else:
self.tcDir.SetValue('%s' % self.calcDict['OutputFolder'])
def InitDatesPanel(self, pnl):
pnl.SetBackgroundColour('#0FFF0F')
dtSiz = wx.GridBagSizer(0, 0)
dtSiz.AddGrowableCol(0)
gRow = 0
# datesLabel = wx.StaticText(pnl, -1, "Dates")
# dtSiz.Add(datesLabel, pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM|wx.EXPAND, border=5)
# gRow += 1
self.datesList = ndviDatesList(pnl, id = ID_DATES_LIST, style = wx.LC_REPORT)
dtSiz.Add(self.datesList, pos=(gRow, 0), span=(1, 1), flag=wx.EXPAND, border=0)
dtSiz.AddGrowableRow(gRow)
pnl.SetSizer(dtSiz)
pnl.SetAutoLayout(1)
def InitStationsPanel(self, pnl):
pnl.SetBackgroundColour('#FF0FFF')
# stationsLabel = wx.StaticText(pnl, -1, "stations will be here")
stSiz = wx.GridBagSizer(0, 0)
stSiz.AddGrowableCol(0)
gRow = 0
# stationsLabel = wx.StaticText(pnl, -1, "Stations")
# stSiz.Add(stationsLabel, pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM|wx.EXPAND, border=5)
# gRow += 1
self.stationsList = ndviStationsList(pnl, id = ID_STATIONS_LIST, style = wx.LC_REPORT)
stSiz.Add(self.stationsList, pos=(gRow, 0), span=(1, 1), flag=wx.EXPAND, border=0)
stSiz.AddGrowableRow(gRow)
pnl.SetSizer(stSiz)
pnl.SetAutoLayout(1)
def InitPreviewPanel(self, pnl):
pnl.SetBackgroundColour('#FFFFFF')
pvSiz = wx.GridBagSizer(0, 0)
pvSiz.AddGrowableCol(0)
gRow = 0
self.pvLabel = wx.StaticText(pnl, -1, "Select Ref station, IR series, then float over dates to preview")
pvSiz.Add(self.pvLabel, pos=(gRow, 0), span=(1, 1), flag=wx.TOP|wx.LEFT|wx.BOTTOM|wx.EXPAND, border=5)
gRow += 1
# Add the FloatCanvas canvas
self.NC = NavCanvas.NavCanvas(pnl,
Debug = 0,
BackgroundColor = "WHITE")
self.Canvas = self.NC.Canvas # reference the contained FloatCanvas
pvSiz.Add(self.NC, pos=(gRow, 0), span=(1, 1), flag=wx.EXPAND, border=0)
pvSiz.AddGrowableRow(gRow)
pnl.SetSizer(pvSiz)
pnl.SetAutoLayout(1)
def refresh_cbxPanelsChoices(self, event):
self.cbxGetPanel.Clear()
stSQLPanels = 'SELECT ID, CalcName FROM NDVIcalc;'
scidb.fillComboboxFromSQL(self.cbxGetPanel, stSQLPanels)
def onClick_BtnSavePnl(self, event):
"""
"""
self.SavePanel()
def SavePanel(self):
self.FillDictFromNDVISetupPanel()
# print 'self.calcDict:', self.calcDict
print 'existing record ID:', self.calcDict['ID']
self.calcNameToValidate = self.calcDict['CalcName']
self.callsValidation = 'save' # flag for which fn calls validation
if not self.validateCalcName():
return
# save the basic NDVI panel data
recID = scidb.dictIntoTable_InsertOrReplace('NDVIcalc', self.calcDict)
self.calcDict['ID'] = recID
print 'new record ID:', self.calcDict['ID']
self.refresh_cbxPanelsChoices(-1)
# update the corresponding Dates and Stations from the selection lists
# for Dates; clear any old, get new from list, and insert in DB
stSQLDelDates = 'DELETE FROM NDVIcalcDates WHERE CalcID = ?'
scidb.curD.execute(stSQLDelDates, (recID,))
lSelectedDates = scidb.getListCtrlSelectionsAsTextList(self.datesList)
print 'selected dates', lSelectedDates
stSQLInsertDates = 'INSERT INTO NDVIcalcDates(CalcID, CalcDate) VALUES(?, ?)'
lInsertDates = [(recID, d) for d in lSelectedDates]
scidb.datConn.execute("BEGIN DEFERRED TRANSACTION") # much faster
scidb.curD.executemany(stSQLInsertDates, lInsertDates)
scidb.datConn.execute("COMMIT TRANSACTION")
# for Stations; clear any old, get new from list, and insert in DB
stSQLDelStas = 'DELETE FROM NDVIcalcStations WHERE CalcID = ?'
scidb.curD.execute(stSQLDelStas, (recID,))
lSelectedStationKeys = scidb.getListCtrlSelectionsAsKeysList(self.stationsList)
print 'selected station keys', lSelectedStationKeys
stSQLInsertStas = 'INSERT INTO NDVIcalcStations(CalcID, StationID) VALUES(?, ?)'
lInsertStas = [(recID, s) for s in lSelectedStationKeys]
scidb.datConn.execute("BEGIN DEFERRED TRANSACTION") # much faster
scidb.curD.executemany(stSQLInsertStas, lInsertStas)
scidb.datConn.execute("COMMIT TRANSACTION")
def validateCalcName(self):
"""
Called both on normal Save of panel, and when Duplicating a panel
"""
if self.calcNameToValidate == None:
wx.MessageBox('Need a Name for this Panel', 'Missing',
wx.OK | wx.ICON_INFORMATION)
if self.callsValidation == 'save': # flag for which fn called validation
# called in normal Save from main panel
self.tcCalcName.SetValue('')
self.tcCalcName.SetFocus()
# self.Scroll(0, 0) # at the top
return 0
# check length
maxLen = scidb.lenOfVarcharTableField('NDVIcalc', 'CalcName')
if maxLen < 1:
wx.MessageBox('Error %d getting [NDVIcalc].[CalcName] field length.' % maxLen, 'Error',
wx.OK | wx.ICON_INFORMATION)
return 0
if len(self.calcNameToValidate) > maxLen:
if self.callsValidation == 'save':
wx.MessageBox('Max length for Panel name is %d characters.\n\nIf trimmed version is acceptable, retry.' % maxLen, 'Invalid',
wx.OK | wx.ICON_INFORMATION)
self.calcNameToValidate = self.calcNameToValidate[:(maxLen)]
self.tcCalcName.SetValue(self.calcNameToValidate)
self.tcCalcName.SetFocus()
# self.Scroll(0, 0) # scroll to the top
if self.callsValidation == 'copy':
wx.MessageBox('Max length for Panel name is %d characters.' % maxLen, 'Invalid',
wx.OK | wx.ICON_INFORMATION)
return 0
# check for existing CalcName
stSQLCk = 'SELECT ID FROM NDVIcalc WHERE CalcName = ?'
rec = scidb.curD.execute(stSQLCk, (self.calcNameToValidate, )).fetchone()
if rec != None:
if self.callsValidation == 'copy':
# if there is any record at all, disallow attempt to copy onto same name
wx.MessageBox('There is already a panel named "' + self.calcNameToValidate + '"', 'Duplicate',
wx.OK | wx.ICON_INFORMATION)
return 0
if rec['ID'] != self.calcDict['ID']:
wx.MessageBox('There is already another panel named "' + self.calcNameToValidate + '"', 'Duplicate',
wx.OK | wx.ICON_INFORMATION)
if self.callsValidation == 'save':
self.tcCalcName.SetValue(self.calcNameToValidate)
self.tcCalcName.SetFocus()
self.Scroll(0, 0) # at the top
return 0
return 1
def FillDictFromNDVISetupPanel(self):
self.calcDict['CalcName'] = scidb.getTextFromTC(self.tcCalcName)
self.calcDict['RefStationID'] = scidb.getComboboxIndex(self.cbxRefStationID)
self.calcDict['IRRefSeriesID'] = scidb.getComboboxIndex(self.cbxIRRefSeriesID)
self.calcDict['VISRefSeriesID'] = scidb.getComboboxIndex(self.cbxVISRefSeriesID)
self.calcDict['UseRef'] = scidb.getBoolFromCB(self.ckUseRef)
self.calcDict['IRDataSeriesID'] = scidb.getComboboxIndex(self.cbxIRDataSeriesID)
self.calcDict['VisDataSeriesID'] = scidb.getComboboxIndex(self.cbxVisDataSeriesID)
self.calcDict['IRFunction'] = scidb.getTextFromTC(self.tcIRFunction, default = '=i')
self.calcDict['VISFunction'] = scidb.getTextFromTC(self.tcVISFunction, default = '=v')
self.calcDict['PlusMinusCutoffHours'] = scidb.getFloatFromTC(self.tcPlusMinusCutoffHours, default = 2)
self.calcDict['ClearDay'] = scidb.getDateFromTC(self.tcClearDay) # if DateTime format, retrieve record will fail
self.calcDict['ThresholdPctLow'] = scidb.getIntFromTC(self.tcThresholdPctLow, default = 75)
self.calcDict['ThresholdPctHigh'] = scidb.getIntFromTC(self.tcThresholdPctHigh, default = 125)
# not implemented: IRRefCutoff, VISRefCutoff, IRDatCutoff, VISDatCutoff
self.calcDict['UseOnlyValidNDVI'] = scidb.getBoolFromCB(self.ckUseOnlyValidNDVI)
self.calcDict['NDVIvalidMin'] = scidb.getFloatFromTC(self.tcNDVIvalidMin, default = -1)
self.calcDict['NDVIvalidMax'] = scidb.getFloatFromTC(self.tcNDVIvalidMax, default = 1)
self.calcDict['CreateSummaries'] = scidb.getBoolFromCB(self.ckIncludeSummaries)
self.calcDict['OutputSAS'] = scidb.getBoolFromCB(self.ckIncludeSAS)
self.calcDict['Normalize'] = scidb.getBoolFromCB(self.ckNormalize)
# get output format from radio buttons
if self.rbExcel.GetValue():