-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscidb.py
1972 lines (1818 loc) · 89.2 KB
/
scidb.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
import sys, re
try:
import win32com.client
hasCom = True
except ImportError:
hasCom = False
datConn = None
tmpConn = None
def stringOKForSpreadsheetName(st):
"""
Excel sheet names can't contain :\/?*[]
returns string with these replaced by underscore
If longer than 25 characters, returns only 1st 25
"""
#return st.translate('_', ':\/?*[]')[:maxLen] # no, needs 256 char translation list, and won't work with unicode
#st = re.sub(':\/?*[]', '_', st) # no, substitutes e.g '\n' with newline, and fails with unicode
# following is laborious and inefficient, but works
badChars = r':\/?*[]' # len=7, correct but if printed the '\' would be escaped and show as '\\'
s = st[:]
for bc in badChars:
s = s.replace(bc, '_')
return s[:25]
try:
datConn = sqlite3.connect('sci_data.db', isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
# importing the 'datetime' module declares some new SQLite field types: 'date' and 'timestamp'
# 'PARSE_DECLTYPES' acivates them
datConn.execute('pragma foreign_keys=ON') # enforce foreign keys
# check that foreign keys constraint was correctly set
rslt = datConn.execute('pragma foreign_keys')
# if foreign_keys is supported, should have one item that is either (1,) or (0,)
rl = [r for r in rslt] # comprehend it as a list
if len(rl) == 0:
print 'Foreign keys not supported in this version (' + sqlite3.sqlite_version + ') of sqlite. Not used in "sci_data.db".'
if rl[0] != (1,):
print 'Foreign keys supported, but not set in this connection to "sci_data.db"'
datConn.execute('pragma auto_vacuum=ON')
datConn.text_factory = str
datConn.row_factory = sqlite3.Row
datConn.create_function("SpreadsheetName", 1, stringOKForSpreadsheetName)
curD = datConn.cursor()
# assure the Equation of Time table exists and is filled
curD.execute(""" SELECT COUNT(*) FROM sqlite_master WHERE name = ? """, ('EqnOfTime', ))
ck = curD.fetchone()
if not bool(ck[0]): # assume if EqnOfTime table exists it is filled
print "about to create EqnOfTime table"
curD.execute("""
CREATE TABLE "EqnOfTime"
("DayOfYear" INTEGER NOT NULL PRIMARY KEY UNIQUE,
"MinutesCorrection" FLOAT)
""")
print "about to fill EqnOfTime table"
lTimes = [ (1, -3.20), (2, -3.67), (3, -4.13), (4, -4.60), (5, -5.05), (6, -5.50), (7, -5.95), (8, -6.38), (9, -6.82), (10, -7.23),
(11, -7.63), (12, -8.03), (13, -8.42), (14, -8.80), (15, -9.17), (16, -9.53), (17, -9.87), (18, -10.20), (19, -10.53), (20, -10.83),
(21, -11.13), (22, -11.42), (23, -11.68), (24, -11.95), (25, -12.20), (26, -12.43), (27, -12.65), (28, -12.85), (29, -13.05), (30, -13.23),
(31, -13.40), (32, -13.55), (33, -13.68), (34, -13.80), (35, -13.92), (36, -14.02), (37, -14.10), (38, -14.17), (39, -14.23), (40, -14.27),
(41, -14.30), (42, -14.32), (43, -14.33), (44, -14.32), (45, -14.30), (46, -14.27), (47, -14.22), (48, -14.17), (49, -14.10), (50, -14.02),
(51, -13.92), (52, -13.82), (53, -13.70), (54, -13.58), (55, -13.45), (56, -13.30), (57, -13.15), (58, -12.98), (59, -12.80), (60, -12.70),
(61, -12.57), (62, -12.38), (63, -12.18), (64, -11.97), (65, -11.75), (66, -11.30), (67, -11.28), (68, -11.05), (69, -10.80), (70, -10.55),
(71, -10.30), (72, -10.03), (73, -9.77), (74, -9.50), (75, -9.22), (76, -8.93), (77, -8.65), (78, -8.37), (79, -8.07), (80, -7.77),
(81, -7.47), (82, -7.17), (83, -6.87), (84, -6.57), (85, -6.27), (86, -5.97), (87, -5.67), (88, -5.35), (89, -5.03), (90, -4.73),
(91, -4.43), (92, -4.13), (93, -3.83), (94, -3.53), (95, -3.23), (96, -2.95), (97, -2.67), (98, -2.38), (99, -2.10), (100, -1.82),
(101, -1.53), (102, -1.27), (103, -1.00), (104, -0.73), (105, -0.48), (106, -0.23), (107, 0.02), (108, 0.25), (109, 0.48), (110, 0.72),
(111, 0.93), (112, 1.00), (113, 1.35), (114, 1.55), (115, 1.75), (116, 1.93), (117, 2.10), (118, 2.27), (119, 2.43), (120, 2.58),
(121, 2.72), (122, 2.85), (123, 2.98), (124, 3.10), (125, 3.20), (126, 3.30), (127, 3.38), (128, 3.45), (129, 3.52), (130, 3.58),
(131, 3.63), (132, 3.67), (133, 3.70), (134, 3.73), (135, 3.73), (136, 3.73), (137, 3.73), (138, 3.72), (139, 3.68), (140, 3.65),
(141, 3.62), (142, 3.57), (143, 3.50), (144, 3.40), (145, 3.35), (146, 3.27), (147, 3.17), (148, 3.05), (149, 2.93), (150, 2.82),
(151, 2.68), (152, 2.55), (153, 2.42), (154, 2.27), (155, 2.10), (156, 1.93), (157, 1.77), (158, 1.60), (159, 1.42), (160, 1.23),
(161, 1.05), (162, 0.85), (163, 0.65), (164, 0.45), (165, 0.25), (166, 0.05), (167, -0.17), (168, -0.38), (169, -0.60), (170, -0.82),
(171, -1.03), (172, -1.25), (173, -1.47), (174, -1.68), (175, -1.90), (176, -2.12), (177, -2.33), (178, -2.55), (179, -2.75), (180, -2.95),
(181, -3.15), (182, -3.35), (183, -3.55), (184, -3.75), (185, -3.95), (186, -4.13), (187, -4.32), (188, -4.48), (189, -4.65), (190, -4.82),
(191, -4.97), (192, -5.12), (193, -5.27), (194, -5.40), (195, -5.53), (196, -5.65), (197, -5.77), (198, -5.87), (199, -5.97), (200, -6.05),
(201, -6.13), (202, -6.20), (203, -6.25), (204, -6.30), (205, -6.33), (206, -6.37), (207, -6.40), (208, -6.42), (209, -6.42), (210, -6.40),
(211, -6.38), (212, -6.35), (213, -6.32), (214, -6.27), (215, -6.22), (216, -6.15), (217, -6.07), (218, -5.98), (219, -5.88), (220, -5.77),
(221, -5.65), (222, -5.52), (223, -5.38), (224, -5.23), (225, -5.08), (226, -4.92), (227, -4.73), (228, -4.55), (229, -4.35), (230, -4.15),
(231, -3.95), (232, -3.73), (233, -3.50), (234, -3.27), (235, -3.02), (236, -2.77), (237, -2.50), (238, -2.23), (239, -1.97), (240, -1.68),
(241, -1.40), (242, -1.12), (243, -0.82), (244, -0.52), (245, -0.20), (246, 0.12), (247, 0.43), (248, 0.75), (249, 1.08), (250, 1.42),
(251, 1.75), (252, 2.08), (253, 2.43), (254, 2.78), (255, 3.13), (256, 3.48), (257, 3.83), (258, 4.18), (259, 4.53), (260, 4.88),
(261, 5.23), (262, 5.58), (263, 5.93), (264, 6.30), (265, 6.67), (266, 7.02), (267, 7.37), (268, 7.72), (269, 8.07), (270, 8.42),
(271, 8.77), (272, 9.10), (273, 9.43), (274, 9.77), (275, 10.08), (276, 10.40), (277, 10.72), (278, 11.03), (279, 11.33), (280, 11.63),
(281, 11.93), (282, 12.22), (283, 12.50), (284, 12.77), (285, 13.03), (286, 13.30), (287, 13.55), (288, 13.78), (289, 14.02), (290, 14.23),
(291, 14.45), (292, 14.65), (293, 14.85), (294, 15.03), (295, 15.20), (296, 15.37), (297, 15.52), (298, 15.67), (299, 15.78), (300, 15.90),
(301, 16.02), (302, 16.10), (303, 16.18), (304, 16.25), (305, 16.30), (306, 16.33), (307, 16.37), (308, 16.38), (309, 16.38), (310, 16.37),
(311, 16.33), (312, 16.30), (313, 16.25), (314, 16.18), (315, 16.10), (316, 16.00), (317, 15.88), (318, 15.77), (319, 15.62), (320, 15.47),
(321, 15.30), (322, 15.12), (323, 14.93), (324, 14.72), (325, 14.50), (326, 14.27), (327, 14.02), (328, 13.75), (329, 13.47), (330, 13.18),
(331, 12.88), (332, 12.57), (333, 12.23), (334, 11.90), (335, 11.55), (336, 11.18), (337, 10.82), (338, 10.43), (339, 10.03), (340, 9.63),
(341, 9.22), (342, 8.80), (343, 8.37), (344, 7.93), (345, 7.48), (346, 7.03), (347, 6.57), (348, 6.10), (349, 5.63), (350, 5.15),
(351, 4.67), (352, 4.18), (353, 3.70), (354, 3.22), (355, 2.72), (356, 2.22), (357, 1.72), (358, 1.22), (359, 0.72), (360, 0.22),
(361, -0.28), (362, -0.78), (363, -1.27), (364, -1.75), (365, -2.23), (366, -2.72)]
datConn.execute("BEGIN DEFERRED TRANSACTION") # makes executemany much faster, when isolation_level=None
curD.executemany('INSERT INTO EqnOfTime(DayOfYear, MinutesCorrection) VALUES(?, ?)', lTimes)
datConn.execute("COMMIT TRANSACTION") # when isolation_level=None, don't routinely need to commit
print "EqnOfTime table created and filled"
curD.executescript("""
CREATE TABLE IF NOT EXISTS "InstrumentSpecs"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"InstrumentSpec" VARCHAR(255) NOT NULL UNIQUE);
CREATE TABLE IF NOT EXISTS "Loggers"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"LoggerSerialNumber" VARCHAR(32) NOT NULL UNIQUE,
"InstrumentSpecID" INTEGER,
FOREIGN KEY("InstrumentSpecID") REFERENCES InstrumentSpecs("ID")
);
CREATE TABLE IF NOT EXISTS "DeviceSpecs"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"DeviceSpec" VARCHAR(255) NOT NULL UNIQUE);
CREATE TABLE IF NOT EXISTS "Sensors"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"SensorSerialNumber" VARCHAR(32) UNIQUE,
"DeviceSpecID" INTEGER,
FOREIGN KEY("DeviceSpecID") REFERENCES DeviceSpecs("ID")
);
CREATE TABLE IF NOT EXISTS "DataTypes"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"TypeText" VARCHAR(50) NOT NULL UNIQUE);
CREATE TABLE IF NOT EXISTS "DataUnits"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"UnitsText" VARCHAR(50) NOT NULL UNIQUE );
CREATE TABLE IF NOT EXISTS "DataChannels"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"Column" INTEGER NOT NULL ,
"LoggerID" INTEGER NOT NULL ,
"SensorID" INTEGER NOT NULL,
"DataTypeID" INTEGER NOT NULL ,
"DataUnitsID" INTEGER NOT NULL ,
"UTC_Offset" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY("LoggerID") REFERENCES Loggers("ID"),
FOREIGN KEY("SensorID") REFERENCES Sensors("ID"),
FOREIGN KEY("DataTypeID") REFERENCES DataTypes("ID"),
FOREIGN KEY("DataUnitsID") REFERENCES DataUnits("ID")
);
CREATE UNIQUE INDEX IF NOT EXISTS "DataChannels_NoDup_ColLogSenTypUntTZ"
ON "DataChannels"
("Column" ASC, "LoggerID" ASC, "SensorID" ASC,
"DataTypeID" ASC, "DataUnitsID" ASC, "UTC_Offset" ASC);
CREATE TABLE IF NOT EXISTS "Data" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"UTTimestamp" timestamp NOT NULL ,
"ChannelID" INTEGER NOT NULL ,
"Value" FLOAT NOT NULL ,
"Use" BOOL NOT NULL DEFAULT 1,
CHECK (CAST(Value AS FLOAT) == Value),
FOREIGN KEY("ChannelID") REFERENCES DataChannels("ID")
);
CREATE UNIQUE INDEX IF NOT EXISTS "Data_NoDup_TimeChan"
ON "Data"
("UTTimestamp" ASC, "ChannelID" ASC);
CREATE TABLE IF NOT EXISTS "FieldSites" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"SiteName" VARCHAR(50) NOT NULL UNIQUE,
"LatitudeDecDegrees" FLOAT NOT NULL,
"LongitudeDecDegrees" FLOAT NOT NULL,
"UTC_Offset" INTEGER,
CHECK (CAST(LatitudeDecDegrees AS FLOAT) == LatitudeDecDegrees)
CHECK (CAST(LongitudeDecDegrees AS FLOAT) == LongitudeDecDegrees)
);
CREATE TABLE IF NOT EXISTS "Stations" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"StationName" VARCHAR(25) NOT NULL UNIQUE,
"SiteID" INTEGER,
"LatitudeDecDegrees" FLOAT,
"LongitudeDecDegrees" FLOAT,
FOREIGN KEY("SiteID") REFERENCES FieldSites("ID")
);
CREATE TABLE IF NOT EXISTS "DataSeries" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"DataSeriesDescription" VARCHAR(30) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS "ChannelSegments" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"ChannelID" INTEGER NOT NULL ,
"SegmentBegin" timestamp NOT NULL ,
"SegmentEnd" timestamp,
"StationID" INTEGER,
"SeriesID" INTEGER,
CHECK ("SegmentEnd" IS NULL or ("SegmentEnd" > "SegmentBegin"))
FOREIGN KEY("ChannelID") REFERENCES DataChannels("ID")
FOREIGN KEY("StationID") REFERENCES Stations("ID")
FOREIGN KEY("SeriesID") REFERENCES DataSeries("ID")
);
CREATE VIEW IF NOT EXISTS "ChannelsWithOneSegment"
AS SELECT ChannelSegments.ChannelID
FROM ChannelSegments
GROUP BY ChannelSegments.ChannelID
HAVING (((Count(ChannelSegments.ID))=1));
CREATE VIEW IF NOT EXISTS "OpenEndedSegments"
AS SELECT ChannelSegments.ID, ChannelSegments.SegmentBegin,
Min(FollowingSegments.SegmentBegin) AS NextSegBegin
FROM ChannelSegments LEFT JOIN ChannelSegments AS FollowingSegments
ON ChannelSegments.ChannelID = FollowingSegments.ChannelID
WHERE (((ChannelSegments.SegmentEnd) Is Null)
AND (ChannelSegments.SegmentBegin < FollowingSegments.SegmentBegin))
GROUP BY ChannelSegments.ID, ChannelSegments.SegmentBegin;
CREATE VIEW IF NOT EXISTS "OverlappingSegments"
AS SELECT ChannelSegments.ID,
ChannelSegments.SegmentBegin AS CurSegBegin,
ChannelSegments.SegmentEnd AS CurSegEnd,
PreviousSegments.SegmentBegin AS PrevSegBegin,
PreviousSegments.SegmentEnd AS PrevSegEnd
FROM ChannelSegments LEFT JOIN ChannelSegments AS PreviousSegments
ON ChannelSegments.ChannelID = PreviousSegments.ChannelID
WHERE (((PreviousSegments.SegmentEnd) NOT NULL)
AND ([PreviousSegments].[SegmentBegin]<[ChannelSegments].[SegmentBegin])
AND ([PreviousSegments].[SegmentEnd]>[ChannelSegments].[SegmentBegin]));
CREATE VIEW IF NOT EXISTS "MaxSegmentEnds"
AS SELECT ChannelSegments.ChannelID,
Max(ChannelSegments.SegmentEnd) AS MaxSegmEnd
FROM ChannelSegments
WHERE (((ChannelSegments.SegmentEnd) NOT NULL))
GROUP BY ChannelSegments.ChannelID;
CREATE VIEW IF NOT EXISTS "FirstDataTimeAfterSegmentEnds"
AS SELECT MaxSegmentEnds.ChannelID,
MaxSegmentEnds.MaxSegmEnd,
Min(Data.UTTimestamp) AS FirstAfterMaxEnd
FROM MaxSegmentEnds LEFT JOIN Data
ON MaxSegmentEnds.ChannelID = Data.ChannelID
WHERE (((Data.UTTimestamp)>(MaxSegmentEnds.MaxSegmEnd)))
GROUP BY MaxSegmentEnds.ChannelID, MaxSegmentEnds.MaxSegmEnd;
CREATE TABLE IF NOT EXISTS "OutputBooks" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"BookName" VARCHAR(30) NOT NULL UNIQUE,
"Longitude" FLOAT NOT NULL,
"HourOffset" INT NOT NULL,
"NumberOfTimeSlicesPerDay" INTEGER NOT NULL DEFAULT 1,
"OutputDataStart" date,
"OutputDataEnd" date,
"PutAllOutputRowsInOneSheet" BOOL NOT NULL DEFAULT 0,
"BlankRowBetweenDataBlocks" BOOL NOT NULL DEFAULT 0,
CHECK ("Longitude" >= -180)
CHECK ("Longitude" <= 180)
CHECK ("HourOffset" >= -12)
CHECK ("HourOffset" <= 12)
CHECK ("NumberOfTimeSlicesPerDay" >= 1)
CHECK (("OutputDataStart" is NULL) or ("OutputDataEnd" is NULL)
or ("OutputDataStart" <= "OutputDataEnd"))
);
CREATE TABLE IF NOT EXISTS "OutputSheets" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"BookID" INTEGER NOT NULL,
"WorksheetName" VARCHAR(20) NOT NULL,
"DataSetNickname" VARCHAR(50),
"ListingOrder" INTEGER NOT NULL,
FOREIGN KEY("BookID") REFERENCES OutputBooks("ID")
);
CREATE TABLE IF NOT EXISTS "OutputColumns" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"WorksheetID" INTEGER NOT NULL ,
"ColumnHeading" VARCHAR(20) NOT NULL,
"ColType" VARCHAR(10) NOT NULL,
"TimeSystem" VARCHAR(10) DEFAULT 'Clock Time',
"TimeIsInterval" BOOL NOT NULL DEFAULT 0,
"IntervalIsFrom" DATETIME,
"Constant" VARCHAR(10),
"Formula" VARCHAR(1000),
"AggType" VARCHAR(5),
"AggStationID" INTEGER,
"AggDataSeriesID" INTEGER,
"Format_Python" VARCHAR(20),
"Format_Excel" VARCHAR(20),
"ListingOrder" INTEGER NOT NULL,
CHECK ("ColType" IN ('Timestamp', 'Constant', 'Aggregate', 'Formula'))
CHECK ("TimeSystem" IN ('Clock Time', 'Solar Time'))
CHECK ("AggType" IN ('Avg', 'Min', 'Max', 'Count', 'Sum', 'StDev'))
CHECK (NOT (("ColType" = 'Timestamp') AND ("TimeSystem" IS NULL)))
CHECK (NOT (("TimeIsInterval" = 1) AND ("IntervalIsFrom" IS NULL)))
CHECK (NOT (("ColType" = 'Constant') AND ("Constant" IS NULL)))
CHECK (NOT (("ColType" = 'Aggregate') AND (("AggType" IS NULL)
OR ("AggStationID" IS NULL) OR ("AggDataSeriesID" IS NULL))))
CHECK (NOT (("ColType" = 'Formula') AND ("Formula" IS NULL)))
FOREIGN KEY("WorksheetID") REFERENCES OutputSheets("ID")
FOREIGN KEY("AggStationID") REFERENCES Stations ("ID")
FOREIGN KEY("AggDataSeriesID") REFERENCES DataSeries ("ID")
);
CREATE VIEW IF NOT EXISTS "DupOutputColumnsOnSheetCol"
AS SELECT OutputColumns.WorksheetID, OutputColumns.ListingOrder,
OutputColumns.ID, OutputColumns.ColumnHeading, OutputColumns.ColType,
OutputColumns.Constant, OutputColumns.AggStationID, OutputColumns.AggDataSeriesID,
OutputColumns.AggType, OutputColumns.Format_Python, OutputColumns.Format_Excel
FROM OutputColumns
WHERE (((OutputColumns.WorksheetID) In
(SELECT WorksheetID FROM OutputColumns As Tmp
GROUP BY WorksheetID, ListingOrder
HAVING Count(*)>1 And ListingOrder = OutputColumns.ListingOrder)))
ORDER BY OutputColumns.WorksheetID, OutputColumns.ListingOrder;
CREATE VIEW IF NOT EXISTS "DupOutputColumnsNotAggregate"
AS SELECT OutputBooks.BookName, OutputSheets.WorksheetName,
DupOutputColumnsOnSheetCol.ListingOrder
FROM (DupOutputColumnsOnSheetCol
LEFT JOIN OutputSheets ON DupOutputColumnsOnSheetCol.WorksheetID = OutputSheets.ID)
LEFT JOIN OutputBooks ON OutputSheets.BookID = OutputBooks.ID
WHERE (((DupOutputColumnsOnSheetCol.ColType)!="Aggregate"))
GROUP BY OutputBooks.BookName, OutputSheets.WorksheetName,
DupOutputColumnsOnSheetCol.ListingOrder;
CREATE VIEW IF NOT EXISTS "GrpOutputColumnsOnSheetCol"
AS SELECT Count(OutputColumns.ID) AS CountOfID, OutputColumns.WorksheetID, OutputColumns.ListingOrder
FROM OutputColumns
GROUP BY OutputColumns.WorksheetID, OutputColumns.ListingOrder
HAVING (((Count(OutputColumns.ID))>1))
ORDER BY Count(OutputColumns.ID) DESC;
CREATE VIEW IF NOT EXISTS "GrpDupOutputColumns"
AS SELECT Count(DupOutputColumnsOnSheetCol.ID) AS ReCountOfID,
DupOutputColumnsOnSheetCol.WorksheetID, DupOutputColumnsOnSheetCol.ListingOrder,
DupOutputColumnsOnSheetCol.ColumnHeading, DupOutputColumnsOnSheetCol.ColType,
DupOutputColumnsOnSheetCol.AggType,
DupOutputColumnsOnSheetCol.Format_Python, DupOutputColumnsOnSheetCol.Format_Excel
FROM DupOutputColumnsOnSheetCol
GROUP BY DupOutputColumnsOnSheetCol.WorksheetID, DupOutputColumnsOnSheetCol.ListingOrder,
DupOutputColumnsOnSheetCol.ColumnHeading, DupOutputColumnsOnSheetCol.ColType,
DupOutputColumnsOnSheetCol.AggType,
DupOutputColumnsOnSheetCol.Format_Python, DupOutputColumnsOnSheetCol.Format_Excel;
CREATE VIEW IF NOT EXISTS "DupOutputColumnsMismatch"
AS SELECT OutputBooks.BookName, OutputSheets.WorksheetName,
GrpDupOutputColumns.ListingOrder
FROM ((GrpOutputColumnsOnSheetCol
LEFT JOIN GrpDupOutputColumns
ON (GrpOutputColumnsOnSheetCol.ListingOrder = GrpDupOutputColumns.ListingOrder)
AND (GrpOutputColumnsOnSheetCol.WorksheetID = GrpDupOutputColumns.WorksheetID))
LEFT JOIN OutputSheets ON GrpDupOutputColumns.WorksheetID = OutputSheets.ID)
LEFT JOIN OutputBooks ON OutputSheets.BookID = OutputBooks.ID
WHERE (((GrpOutputColumnsOnSheetCol.CountOfID)!=[ReCountOfID]));
CREATE TABLE IF NOT EXISTS "NDVIcalc" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"CalcName" VARCHAR(50) NOT NULL UNIQUE ,
"ChartFromRefDay" BOOL NOT NULL DEFAULT 0 ,
"RefDay" date ,
"RefStationID" INTEGER ,
"IRRefSeriesID" INTEGER ,
"VISRefSeriesID" INTEGER ,
"UseRef" BOOL NOT NULL DEFAULT 1,
"IRDataSeriesID" INTEGER ,
"VisDataSeriesID" INTEGER ,
"IRFunction" VARCHAR(250) DEFAULT "=i" ,
"VISFunction" VARCHAR(250) DEFAULT "=v" ,
"PlusMinusCutoffHours" FLOAT NOT NULL DEFAULT 2 ,
"Opt1ClrDayVsSetTholds" BOOL NOT NULL DEFAULT 1 ,
"ClearDay" date ,
"ThresholdPctLow" INTEGER NOT NULL DEFAULT 75 ,
"ThresholdPctHigh" INTEGER NOT NULL DEFAULT 125 ,
"IRRefCutoff" FLOAT ,
"VISRefCutoff" FLOAT ,
"IRDatCutoff" FLOAT ,
"VISDatCutoff" FLOAT,
"UseOnlyValidNDVI" BOOL NOT NULL DEFAULT 0 ,
"NDVIvalidMin" FLOAT DEFAULT -1 ,
"NDVIvalidMax" FLOAT DEFAULT 1 ,
"CreateSummaries" BOOL NOT NULL DEFAULT 0,
"OutputSAS" BOOL NOT NULL DEFAULT 0,
"Normalize" BOOL NOT NULL DEFAULT 0,
"OutputFormat" INTEGER NOT NULL DEFAULT 2,
"OutputBaseName" VARCHAR(30) NOT NULL DEFAULT "NDVI" ,
"OutputFolder" VARCHAR(250),
CHECK ("PlusMinusCutoffHours" >= 0)
CHECK ("PlusMinusCutoffHours" <= 12)
CHECK (CAST(PlusMinusCutoffHours AS FLOAT) == PlusMinusCutoffHours)
CHECK ((PlusMinusCutoffHours is NULL) OR (CAST(PlusMinusCutoffHours AS FLOAT) == PlusMinusCutoffHours))
CHECK ("ThresholdPctLow" > 0)
CHECK ("ThresholdPctLow" < 100)
CHECK (CAST(ThresholdPctLow AS INTEGER) == ThresholdPctLow)
CHECK ("ThresholdPctHigh" > 100)
CHECK (CAST(ThresholdPctHigh AS INTEGER) == ThresholdPctHigh)
CHECK ((IRRefCutoff is NULL) OR (CAST(IRRefCutoff AS FLOAT) == IRRefCutoff))
CHECK ((VISRefCutoff is NULL) OR (CAST(VISRefCutoff AS FLOAT) == VISRefCutoff))
CHECK ((IRDatCutoff is NULL) OR (CAST(IRDatCutoff AS FLOAT) == IRDatCutoff))
CHECK ((VISDatCutoff is NULL) OR (CAST(VISDatCutoff AS FLOAT) == VISDatCutoff))
CHECK ((NDVIvalidMin is NULL) OR (CAST(NDVIvalidMin AS FLOAT) == NDVIvalidMin))
CHECK ((NDVIvalidMax is NULL) OR (CAST(NDVIvalidMax AS FLOAT) == NDVIvalidMax))
CHECK ("OutputFormat" >= 1)
CHECK ("OutputFormat" <= 3)
);
CREATE TABLE IF NOT EXISTS "NDVIcalcDates" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"CalcID" INTEGER NOT NULL ,
"CalcDate" date NOT NULL ,
FOREIGN KEY("CalcID") REFERENCES NDVIcalc("ID")
);
CREATE TABLE IF NOT EXISTS "NDVIcalcStations" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"CalcID" INTEGER NOT NULL ,
"StationID" INTEGER NOT NULL ,
FOREIGN KEY("CalcID") REFERENCES NDVIcalc("ID")
FOREIGN KEY("StationID") REFERENCES Stations("ID")
);
CREATE TABLE IF NOT EXISTS "tmpChanSegSeries" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"ChannelID" INTEGER NOT NULL,
"SeriesID" INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS "tmpChanSegSeries_NoDup"
ON "tmpChanSegSeries"
("ChannelID" ASC, "SeriesID" ASC);
CREATE TABLE IF NOT EXISTS "tmpSpectralData" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"Timestamp" timestamp,
"IRRef" FLOAT,
"VISRef" FLOAT,
"IRData" FLOAT,
"VISData" FLOAT,
"NearestTimestamp" timestamp
);
CREATE TABLE IF NOT EXISTS "tmpSpectralDataForUpdate" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"NearTimestamp" timestamp,
"ValForUpdate" FLOAT
);
CREATE TABLE IF NOT EXISTS "tmpSpectralDataToUpdate" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"SpectID" INTEGER,
"TimeDifference" FLOAT,
"ValForUpdate" FLOAT
);
CREATE TABLE IF NOT EXISTS "testBadStrings" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"LongName" VARCHAR(100) NOT NULL UNIQUE
);
""")
except sqlite3.Error, e:
print 'Error in "sci_data.db": %s' % e.args[0]
sys.exit(1)
try:
tmpConn = sqlite3.connect('tmp.db', isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
tmpConn.execute('pragma foreign_keys=ON')
# check that foreign keys constraint was correctly set
rslt = tmpConn.execute('pragma foreign_keys')
# if foreign_keys is supported, should have one item that is either (1,) or (0,)
rl = [r for r in rslt] # comprehend it as a list
if len(rl) == 0:
print 'Foreign keys not supported in this version (' + sqlite3.sqlite_version + ') of sqlite. Not used in "tmp.db".'
if rl[0] != (1,):
print 'Foreign keys supported, but not set in this connection to "tmp.db"'
tmpConn.execute('pragma auto_vacuum=ON')
tmpConn.text_factory = str
tmpConn.row_factory = sqlite3.Row
curT = tmpConn.cursor()
curT.executescript("""
CREATE TABLE IF NOT EXISTS "Text"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"Line" VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS "tmpLines" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"Line" VARCHAR(200)
);
CREATE TABLE IF NOT EXISTS "tmpParseLog"
("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"LogItem" VARCHAR(200));
""")
curT.execute("drop table if exists testDates")
curT.execute("create table if not exists testDates(note varchar[50], d date, ts timestamp)")
today = datetime.date.today()
now = datetime.datetime.now()
# curT.execute("insert into testDates(note, d, ts) values (?, ?, ?)", ('ck', today, now))
# curT.execute("select note, d, ts from testDates")
# rec = curT.fetchone()
# print today, "=>", rec['d'], type(rec['d'])
# print now, "=>", rec['ts'], type(rec['ts'])
# curT.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"')
# rec = curT.fetchone()
# print "current_date", rec['d'], type(rec['d'])
# print "current_timestamp", rec['ts'], type(rec['ts'])
except sqlite3.Error, e:
print 'Error in "tmp.db": %s' % e.args[0]
sys.exit(1)
def getSciDataCursor():
"""
Utility for deliving a separate sci_data database cursor, for example when
using threads, which cannot share a connection. Assumes all tables have been
set up correctly.
"""
try:
connection = sqlite3.connect('sci_data.db', isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
# importing the 'datetime' module declares some new SQLite field types: 'date' and 'timestamp'
# 'PARSE_DECLTYPES' acivates them
connection.execute('pragma foreign_keys=ON') # enforce foreign keys
# check that foreign keys constraint was correctly set
rslt = connection.execute('pragma foreign_keys')
# if foreign_keys is supported, should have one item that is either (1,) or (0,)
rl = [r for r in rslt] # comprehend it as a list
if len(rl) == 0:
print 'Foreign keys not supported in this version (' + sqlite3.sqlite_version + ') of sqlite. Not used in "sci_data.db".'
if rl[0] != (1,):
print 'Foreign keys supported, but not set in this connection to "sci_data.db"'
connection.execute('pragma auto_vacuum=ON')
connection.text_factory = str
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
return cursor
except sqlite3.Error, e:
print 'Error in "sci_data.db": %s' % e.args[0]
sys.exit(1)
return None
def cleanup(): # routine cleanup e.g. orphaned records that might remain after deletes from parent tables
curD.execute('DELETE FROM NDVIcalcDates WHERE CalcID NOT IN (SELECT ID FROM NDVIcalc)')
curD.execute('DELETE FROM NDVIcalcStations WHERE CalcID NOT IN (SELECT ID FROM NDVIcalc)')
def refreshDataDates(): # make externally callable
curD.executescript("""
DROP TABLE IF EXISTS "DataDates";
CREATE TABLE IF NOT EXISTS "DataDates"
AS SELECT date(Data.UTTimestamp) AS Date FROM Data
GROUP BY Date;
""")
refreshDataDates() # call it now
def getDatesList():
"""
Gets the list of dates that have records in the Data table.
Used for filling combo boxes, to select from dates that have existing data.
Returns a Python list of the dates.
"""
lstDates = []
curD.execute("SELECT Date FROM DataDates ORDER BY Date;")
recs = curD.fetchall()
for rec in recs:
lstDates.append(rec["Date"])
return lstDates
def lenOfVarcharTableField(stTable, stField):
"""
Returns the stated size of the given VARCHAR field of a given table
Returns -1 if not a VARCHAR field, -2 if a nonexistent field, -3 if a nonexistent table, -4 if multiple matches
SQLite does not enforce length limits, so this is a way to programmatically test.
Input is two strings:
stTable is the table
stField is the field in that table
"""
stSQL = "SELECT sql FROM sqlite_master WHERE type='table' and name=?"
curD.execute(stSQL, (stTable,))
rec = curD.fetchone()
if rec is None:
return -3
else:
stSQL = rec['sql']
# example of regular expression where field is 'BookName'
# ["']??BookName["']??\s+?VARCHAR\s*?[(]\s*?(?P<stRecLen>\d+)\s*?[)]
# stRE = r"""["']??""" + stField + r"""["']??\s+?VARCHAR\s*?[(]\s*?(?P<stRecLen>\d+)\s*?[)]"""
reRecLen = re.compile(r"""
["']?? # may or may not have single or double quotes around the field name
"""
+ stField +
r"""
["']?? # throughout, use non-greedy match format (extra '?')
\s+?VARCHAR\s*? # optional whitespace around 'VARCHAR'
[(]\s*? # optional whitespace within parentheses
(?P<stRecLen>\d+) # capture the digits as stRecLen
\s*?[)]
""", re.VERBOSE | re.IGNORECASE)
lLen = reRecLen.findall(stSQL)
# print "lLen in lenOfVarcharTableField:", lLen
if len(lLen) == 0:
return -2 # none found
if len(lLen) > 1:
return -4 # multiple found, invalid
return int(lLen[0])
def dictFromTableDefaults(stTable):
"""
Given a table, returns a dictionary with keys corresponding to all the fields.
If a field has a default, that is returned as the member value, otherwise None
"""
"""
the following 'cursor.description' format does not work right;
tuples are only the field name followed by 'None' seven times
stSQL = "SELECT * FROM NDVIcalc;"
rec = curD.execute(stSQL).fetchone()
print 'curD.description:', curD.description
"""
#following format fails
#fields = curD.execute('PRAGMA table_info(?)', (stTable,)).fetchall()
fields = curD.execute('PRAGMA table_info("' + stTable + '")').fetchall()
"""
positions in this tuple (zero based):
0 = numerical index of this field
1 = name of the field as a string
2 = data type of the field; not really, see SQLite docs
3 = whether the field is required
4 = default value, if any
5 = whether the field participates in the primary key
Example:
(0, 'ID', 'INTEGER', 1, None, 1)
(1, 'CalcName', 'VARCHAR(50)', 1, None, 0)
(2, 'ChartFromRefDay', 'BOOL', 1, '0', 0)
(3, 'RefDay', 'date', 0, None, 0)
(4, 'RefStationID', 'INTEGER', 0, None, 0)
...
(10, 'VISFunction', 'VARCHAR(50)', 0, '"=v"', 0)
(11, 'PlusMinusCutoffHours', 'FLOAT', 0, '2', 0)
(12, 'Opt1ClrDayVsSetTholds', 'BOOL', 1, '1', 0)
(13, 'ClearDay', 'date', 0, None, 0)
(14, 'ThresholdPctLow', 'FLOAT', 0, '0.75', 0)
"""
d = {}
for field in fields:
# print field
stFldNm = field[1]
if field[4] == None: # there is no default value
d[stFldNm] = None
else: # process the default value
if field[2].upper() == 'FLOAT': # the data type, as much as there is one in sqlite
val = float(field[4])
elif field[2][:3].upper() == 'INT':
val = int(field[4])
elif field[2][:4].upper() == 'BOOL':
val = int(field[4]) # convert to 0 or 1
# if field[4] == '1':
# val = True
# else:
# val = False
else: # text
val = re.sub(r'^"|"$', '', field[4]) # strip any quotes
d[stFldNm] = val
return d
def dictFromTableID(stTable, iID):
"""
Given a table and a number that is a value in the table's 'ID' field,
returns a dictionary with keys corresponding to all the fields.
The values are from the corresponding table fields.
If the record is not found, returns None
"""
stSQL = 'SELECT * FROM ' + stTable + ' WHERE ID = ?;'
curD.execute(stSQL, (iID,))
rec = curD.fetchone() # returns one record, or None
if rec == None:
return None
# d = copy.copy(recs) # this crashes
d = {}
for recName in rec.keys(): # copy the record into the dictionary
d[recName] = rec[recName]
return d
def dictIntoTable_InsertOrReplace(stTable, dict):
"""
Given a table and a dictionary that has keys corresponding to
all the table's field names, writes the dictionary to the table
as INSERT OR REPLACE.
If the ID already exists in the table, UPDATEs the record
If the ID does not exist in the table, INSERTs a new record
Returns the new or updated record ID, or None on failure
Normally, you would have generated the dictionary by the functions
'dictFromTableDefaults' or 'dictFromTableID'
"""
"""
Dictionary does not preserve order, so write SQL using the order the dictionary delivers.
Build an SQL string using named parameters e.g.:
'INSERT OR REPLACE INTO Stations (Longitude, Name, ID, SiteID, Latitude)
VALUES (:Longitude, :Name, :ID, :SiteID, :Latitude)
"""
lKs = []
lVs = []
for k in dict.keys():
lKs.append(k)
lVs.append(':' + k)
stKs = ', '.join(lKs)
stVs = ', '.join(lVs)
stSQL = 'INSERT OR REPLACE INTO ' + stTable + ' (' + stKs + ') ' \
' VALUES (' + stVs + ')'
# print stSQL
# for development, let any errors occur and display to output
# try:
curD.execute(stSQL, dict)
newRowID = curD.lastrowid
return newRowID
# except:
# return None
def removeRecsOfTableID(stTable, iID):
"""
Given a table and a number that is a value in the table's 'ID' field,
deletes any records that have that ID.
"""
stSQL = 'DELETE FROM ' + stTable + ' WHERE ID = ?;'
curD.execute(stSQL, (iID,))
def countTableFieldItems(stTable, stField, stItem=None):
"""
Tests whether an item is in the given field of a given table
Returns 0 if not, the number of matching records if so
Input is three strings:
stTable is the table
stField is the field in that table
stItem is the value to test
If stItem is None or left out, counts all records in the table
"""
if stItem == None:
stSQL = 'SELECT ' + stField + ' FROM ' + stTable + ';'
curD.execute(stSQL)
else:
stSQL = 'SELECT ' + stField + ' FROM ' + stTable + ' WHERE ' + stField + ' = ?;'
curD.execute(stSQL, (stItem,))
recs = curD.fetchall()
return len(recs)
def assureItemIsInTableField(stItem, stTable, stField):
"""
This is intended for use on tables that contain only two fields, a
text field of unique values, and an autonumber ID field which is the primary key
If the given text value is not in the table, this function inserts it and returns the ID.
If the value is already in the table, the function returns the existing ID.
Input is three strings:
stItem is the string value to test or insert
stTable is the table
stField is the field in that table
"""
try:
stSQL = 'INSERT INTO ' + stTable + '(' + stField + ') VALUES (?);'
curD.execute(stSQL, (stItem,))
datConn.commit()
return curD.lastrowid
except sqlite3.IntegrityError: # item is already in the table, get its ID
stSQL = 'SELECT ID FROM ' + stTable + ' WHERE (' + stField + ') = (?);'
curD.execute(stSQL, (stItem,))
t = curD.fetchone() # a tuple with one value
return t[0]
# except: # some other error
# return None
def assureChannelIsInDB(lChanList):
"""
Given a 7-membered list
The first item in each list will be the primary key in the Channels table.
The list is first created with this = 0.
The rest of the list is built up, then the list is sent to this function
that looks in the database.
If the function finds an existing channel, it fills in the primary key.
If it does not find an existing channel, it creates a new one and fills in the pk.
List item 7 will be "new" if the record is new, otherwise "existing"
The list contains the text values of (2)logger serial number, (3)sensor serial number,
(4)data type, and (5)data units, and the integer values of(1) column number, and (6)hour offset.
The function takes care of filling in all these in their respective tables.
When returned, the calling procedure can quickly insert data values
into the data table by using list item [0] for the channel pk.
"""
# set up dependent fields
lgrID = assureItemIsInTableField(lChanList[2], 'Loggers', 'LoggerSerialNumber')
senID = assureItemIsInTableField(lChanList[3], 'Sensors', 'SensorSerialNumber')
dtyID = assureItemIsInTableField(lChanList[4], 'DataTypes', 'TypeText')
dunID = assureItemIsInTableField(lChanList[5], 'DataUnits', 'UnitsText')
# set up channel
try:
stSQL = """
INSERT INTO DataChannels
(Column, LoggerID, SensorID, DataTypeID, DataUnitsID, UTC_Offset)
VALUES (?,?,?,?,?,?);
"""
curD.execute(stSQL, (lChanList[1], lgrID, senID, dtyID, dunID, lChanList[6]))
datConn.commit()
lChanList[0] = curD.lastrowid
lChanList[7] = 'new'
except sqlite3.IntegrityError: # this Channel is already there, get its ID
stSQL = """
SELECT ID FROM DataChannels
WHERE Column = ? AND LoggerID = ? AND SensorID = ? AND
DataTypeID = ? AND DataUnitsID = ? AND UTC_Offset = ?;
"""
curD.execute(stSQL, (lChanList[1], lgrID, senID, dtyID, dunID, lChanList[6]))
t = curD.fetchone() # a tuple with one value
lChanList[0] = t[0]
lChanList[7] = 'existing'
return lChanList
def autofixChannelSegments():
"""
A channel segment must have a start time, but end time can be null
End time null means "from start time on"
Queries are written to replace null end time with Now
A channel segment can have a start time that is the same as the stop time of the previous segment
Query logic should be written >= startTime and < stopTime
Similar to days' data, which are >= midnight and < next day's midnight
Still, automatic fixes try to set a one second gap between segments.
Actions for channels in these states:
Multiple segments
If a segment does not have an explicit stop time and there is(are) a later segment(s)
(which of course have start times, becaue that field is required):
Assign explicit stop time to earlier segment, at the start time of first later.
[Maybe allow overlaps, as long as user understands they occur]
[Maybe reserve checking for inconsistencies to segments the user has tagged with
Station and Series]
If start time is earlier than explicit stop time of previous (overlap):
If one segment (A) completely encompasses another (B), such that A.Start < B.Start and
A.End > B.End:
Delete the larger segment (A) [maybe change this rule]
Adjust start time of later segment to be stop time of earlier
If data exist for a channel after the latest explicit stop time, create a new segment
One segment:
If the segment has an explicit end: # assume user entered, do not adjust the segment
If there is data after the end:
Create a new segment with start time the previous segment's end time
Else the segment has no explicit end: # assume auto generated, and possibly still
being built, possibly with records being parsed out of chronological order
If there are data points earlier than start time, adjust start time to earliest
Zero segments:
Create one segment with Start as the earliest time in the channel
"""
# Check for open-ended segments; use a temporary table to avoid any problems with aggregates in query
# Each record (if there are any) will have the ID of a current open ended segment, with the
# the start time of the next following segment, which will be used to generate the end
# Make table, which may be empty; just as quick to make the table as to test the source query
curD.execute("DROP TABLE IF EXISTS tmpOpenEndedSegments;")
curD.execute("CREATE TABLE tmpOpenEndedSegments AS SELECT * FROM OpenEndedSegments;")
# then, this test for presence will be quick because the temp table is never large
curD.execute("SELECT COUNT(*) AS 'recCt' FROM tmpOpenEndedSegments;")
t = curD.fetchone() # a tuple with one value
if t[0] > 0: # there is at least one open ended segment
stSQL = """
UPDATE ChannelSegments
SET
SegmentEnd = (SELECT datetime(NextSegBegin, '-1 second')
FROM tmpOpenEndedSegments
WHERE ChannelSegments.ID = tmpOpenEndedSegments.ID)
WHERE
EXISTS (
SELECT *
FROM tmpOpenEndedSegments
WHERE ChannelSegments.ID = tmpOpenEndedSegments.ID
);
"""
curD.execute(stSQL)
# more validity checking here
# if a channel has only one segment, assure Start is earliest data for that channel
curD.execute("SELECT COUNT(*) AS 'recCt' FROM ChannelsWithOneSegment;")
t = curD.fetchone() # a tuple with one value
if t[0] > 0: # there is at least one channel that has a single segment
stSQL = """
UPDATE ChannelSegments
SET
SegmentBegin = (SELECT MIN(UTTimestamp)
FROM Data
WHERE Data.ChannelID = ChannelSegments.ChannelID)
WHERE
ChannelSegments.ChannelID IN (
SELECT ChannelID
FROM ChannelsWithOneSegment
);
"""
#curD.execute(stSQL)
# too slow; using Data table with 19,560,908 records, took 4,725.416 seconds (1.3 hours)
# this for an operation that seldom does anything
# implement a different way
# create a channel segment for any channel data after the latest explicit end time
# Make table, which may be empty; just as quick to make the table as to test the source query
#curD.execute("DROP TABLE IF EXISTS tmpFirstDataTimeAfterSegmentEnds;")
#curD.execute("CREATE TABLE tmpFirstDataTimeAfterSegmentEnds AS SELECT * FROM FirstDataTimeAfterSegmentEnds;")
# above, using Data table with 19,560,908 records, took 142.679 seconds
# too slow
# instead, create a Start after the max End if it doesn't already have a Start later than the max End
# regardless if there are data points in that time segment yet
stSQL = """
INSERT INTO ChannelSegments ( ChannelID, SegmentBegin )
SELECT MaxSegmentEnds.ChannelID, MaxSegmentEnds.MaxSegmEnd
FROM MaxSegmentEnds
WHERE (((MaxSegmentEnds.ChannelID) NOT IN (
SELECT MaxSegmentEnds.ChannelID
FROM MaxSegmentEnds LEFT JOIN ChannelSegments
ON MaxSegmentEnds.ChannelID = ChannelSegments.ChannelID
WHERE (([ChannelSegments].[SegmentBegin]>=[MaxSegmentEnds].[MaxSegmEnd]))))
);
"""
curD.execute(stSQL)
# finally, create a channel segment for any data channel that has none yet
stSQL = """
INSERT INTO ChannelSegments ( ChannelID, SegmentBegin )
SELECT Data.ChannelID, MIN(Data.UTTimestamp) AS MinOfUTTimestamp
FROM Data LEFT JOIN ChannelSegments ON Data.ChannelID = ChannelSegments.ChannelID
WHERE (((ChannelSegments.ChannelID) Is Null))
GROUP BY Data.ChannelID;
"""
curD.execute(stSQL) # can be rather slow, but necessary
# for Data table with 141,811 records, took 2.455ms
# created 15 segments
offerSeriesForChannels() # if a Series is auto available, fill in
def getTextFromTC(objTextControl, default = None):
"""
Given a wx text control, returns the text stripped of leading/trailing
whitespace and with duplicate whitespace removed.
If nothing is left returns the default, which is None if not explicitly given
"""
# clean up whitespace; remove leading/trailing & multiples
stS = " ".join(objTextControl.GetValue().split())
if stS == '':
return default
else:
return stS
def getIntFromTC(objTextControl, default = None):
"""
Given a wx text control, returns the contents converted to an integer if possible.
If not valid as an integer, returns the default, which is None if not explicitly given
"""
try:
return int(objTextControl.GetValue())
except:
return default
def getFloatFromTC(objTextControl, default = None):
"""
Given a wx text control, returns the contents converted to an float if possible.
If not valid as a float, returns the default, which is None if not explicitly given
"""
try:
return float(objTextControl.GetValue())
except:
return default
def getDateFromTC(objTextControl, default = None):
"""
Given a wx text control, returns the contents converted to a string in standard UNIX
date format 'yyyy-mm-dd' if possible.
If not valid as a date, returns the default, which is None if not explicitly given
"""
dt = wx.DateTime() # Uninitialized datetime
DateValid = dt.ParseDate(objTextControl.GetValue())
if DateValid != -1: # valid date
# store in standard format
return dt.Format('%Y-%m-%d')
else:
# try once more as datetime
DateTimeValid = dt.ParseDateTime(objTextControl.GetValue())
if DateTimeValid != -1: # valid datetime
# store in standard date format
return dt.Format('%Y-%m-%d')
else:
return default
def getDateTimeFromTC(objTextControl, default = None):
"""
Given a wx text control, returns the contents converted to a string in standard UNIX
datetime format 'yyyy-mm-dd hh:mm:ss' if possible.
If not valid as a date/time, returns the default, which is None if not explicitly given
"""
dt = wx.DateTime() # Uninitialized datetime