-
Notifications
You must be signed in to change notification settings - Fork 11
/
RatingCurveViewerToolManager.py
1324 lines (1025 loc) · 47.3 KB
/
RatingCurveViewerToolManager.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
# All works in this code have been curated by ECCC and licensed under the GNU General Public License v3.0.
# Read more: https://www.gnu.org/licenses/gpl-3.0.en.html
from RatingCurveViewerToolFrame import *
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from datetime import *
import os
import re
import copy
import math
import json
class RatingCurveViewerToolManager(object):
## Constructor: (mode, path, stnNum, disMeasManager, lang, gui)
## mode: displays certain print statements when set to "DEBUG"
## path: file path to default folder location of rating files
## stnNum: station number of the given station
## disMeasManager: handle on DischargeMeasurementManager, used to send and pull data
## lang: for displaying the dates in the Period of Applicability table
## gui: handle on gui
def __init__(self, mode, path, stnNum, disMeasManager, lang, gui, manager=None):
self.manager = manager
self.gui = gui
if self.gui is not None:
self.gui.manager = self
self.mode = mode
self.stnNum = stnNum
self.disMeasManager = disMeasManager
self.disMeasManager.RCVTMan = self
self.lang = lang
#variables to keep
self.extension = None
self.data = None
self.period = None
self.curveNum = None
self.histData = None
self.delHist = None # list of indexes of histData that are invalid (either no HG or Q)
self.path = path
# tuple structure
self.shiftDiffDisch = None
# Plotting vars
self.minList = [] ## For historical min values
self.maxList = [] ## For historical max values
self.qrta = None ## discharge curve points
self.hgta = None ## stage curve points
self.dischargeHist = None ## Historical Discharge values
self.stageHist = None ## Historical Stage values
self.validHistPoints = None ## Historical points (includes label, discharge, and stage
self._obsDisch = None ## observed Discharge value
self._obsStage = None ## observed Stage value
self.qrpa = None ## list of discharge (qr) rating points
self.hrpa = None ## list of stage (hg) rating points
self.Hr = None ## used for shift calculations
self.Qr = None ## used for discharge calculations
self.Shift = None ## shift
self.Qdiff = None ## discharge diff
self.comment = "QDiff(%) > 5%."
self.comment2 = "QDiff(%) < 5%."
self.ratingCurveList = []
self.rcIndex = 0
self.Init()
def Init(self):
if self.mode == "DEBUG":
print "RatingCurveViewerToolControl"
# self.locale = wx.Locale(self.lang)
@property
def obsDisch(self):
return self._obsDisch
@obsDisch.setter
def obsDisch(self, val):
self._obsDisch = float(val) if val != "" and val is not None else None
if self.gui is not None:
self.gui.SetObsDisch("" if self._obsDisch is None else str(self._obsDisch))
@property
def obsStage(self):
return self._obsStage
@obsStage.setter
def obsStage(self, val):
self._obsStage = float(val) if val != "" and val is not None else None
if self.gui is not None:
self.gui.SetObsStage("" if self._obsStage is None else str(self._obsStage))
def GetFormattedRatedDischarge(self):
return float(format(self.Qr, ".3g"))
def OpenRatingFile(self, filepath):
#filepath formatting for gui
splitFilepath = filepath.split("\\")
concatenatedFilepath = "...\\"
if len(splitFilepath) >= 2:
concatenatedFilepath += splitFilepath[-2] + "\\"
concatenatedFilepath += splitFilepath[-1]
if self.gui is not None:
self.gui.ratingInfoText.SetLabel(concatenatedFilepath)
#determine what to do based on the extension
extension = self.DetFileType(filepath)
if extension is not None:
if extension == "xml":
self.ParseRatingXMLFile(filepath)
elif extension == "txt":
self.ParseRatingTXTFile(filepath)
elif extension == 'json':
self.ParseRatingJsonFile(filepath)
def OpenHistDataFile(self, filepath):
#filepath formatting for gui
splitFilepath = filepath.split("\\")
concatenatedFilepath = "...\\"
if len(splitFilepath) >= 2:
concatenatedFilepath += splitFilepath[-2] + "\\"
concatenatedFilepath += splitFilepath[-1]
if self.gui is not None:
self.gui.histFieldDataText.SetLabel(concatenatedFilepath)
extension = self.DetFileType(filepath)
if extension is not None:
if extension == "csv":
self.ParseHistCSVFile(filepath)
def FindStationFile(self):
# try and grab station number from genInfoManager first
# updating the RatingCurve in the Fetch function will
# automatically load the historical data
self.FetchStageDischarge()
self.LoadRatingFile()
if self.gui is not None:
self.LoadHistoricalData()
def LoadRatingFile(self):
shouldClearGui = False
if self.stnNum is not "":
xmlfilepath = self.path + "\\" + self.stnNum + "_ratingcurves.xml"
jsonfilepath = self.path + "\\" + self.stnNum + "_ratingcurves.json"
txtfilepath = self.path + "\\" + self.stnNum + "_RatingTable.txt"
self.ratingCurveList = []
# print xmlfilepath
if os.path.isfile(xmlfilepath):
self.OpenRatingFile(os.path.abspath(xmlfilepath))
elif os.path.isfile(jsonfilepath):
# print jsonfilepath
self.OpenRatingFile(os.path.abspath(jsonfilepath))
elif os.path.isfile(txtfilepath):
self.OpenRatingFile(os.path.abspath(txtfilepath))
else:
shouldClearGui = True
else:
shouldClearGui = True
if self.gui is not None:
if shouldClearGui is True:
self.gui.EnableXML(False)
self.gui.ClearAllAppRows()
self.gui.EmptyCalculatedFields()
self.gui.SetRatingInfo("")
else:
self.gui.SetStatus("")
def LoadHistoricalData(self):
shouldClearGui = False
if self.stnNum is not "":
fvFilePath = self.path + "\\" + self.stnNum + "_FieldVisits.csv"
jsonfvFilePath = self.path + "\\" + self.stnNum + "_FieldVisits.csv"
csvfilepath = self.path + "\\" + self.stnNum + "_FieldVisitExport.csv"
print fvFilePath
if os.path.isfile(fvFilePath):
self.OpenHistDataFile(os.path.abspath(fvFilePath))
elif os.path.isfile(jsonfvFilePath):
self.OpenHistDataFile(os.path.abspath(jsonfvFilePath))
elif os.path.isfile(csvfilepath):
self.OpenHistDataFile(os.path.abspath(csvfilepath))
else:
shouldClearGui = True
else:
shouldClearGui = True
if self.gui is not None:
if shouldClearGui is True:
self.gui.ClearAllHistRows()
else:
self.gui.SetStatus("")
def DetFileType(self, filepath):
if self.mode == "DEBUG":
print "Determining File Type"
extension = filepath.strip().split('.')[-1]
if self.mode == "DEBUG":
print "RC file is ", extension
if extension.lower() == "txt" or extension.lower() == "xml" or extension.lower() == "json":
self.extension = extension
return extension
elif extension.lower() == "csv":
return extension
else:
self.extension = None
return None
#stores the XML tree in memory
#saved as self.data
#called when browse button is clicked
def ParseRatingXMLFile(self, filepath):
if self.mode == "DEBUG":
print "Parsing XML File"
# Changing data stuff, reset self.data to None
self.data = None
self.period = None
self.curveNum = None
self.shiftDiffDisch = None
#Clear old table
if self.gui is not None:
self.gui.ClearAllAppRows()
self.gui.EnableXML(True)
#Do Parsing
Curves = ElementTree.parse(filepath).getroot()
#Curve
data = []
for curveInd, curve in enumerate(Curves.findall('curve')):
periods = []
ps = curve.find('periods')
if ps is not None:
for perInd, period in enumerate(ps.findall('period')):
periods.append(period)
points = []
pts = curve.find('ratingpoints')
if pts is not None:
for ptsInd, pt in enumerate(pts.findall('ratingpoint')):
points.append(pt)
data.append((curve, periods, points))
data.reverse()
self.data = data
self.ratingCurveList = [str(x[0].get('id')) for x in data]
if self.gui is not None:
#Update the list on the GUI
#First format the dates
for i, curve in enumerate(data):
curveNum = str(curve[0].get('id'))
curve[1].reverse()
for j, period in enumerate(curve[1]):
curveFromDate = datetime.strptime(str(period.find('start').text), "%Y-%m-%dT%H:%M:%S")
curveToDate = datetime.strptime(str(period.find('end').text), "%Y-%m-%dT%H:%M:%S")
convertedFromDate = curveFromDate.strftime("%B %d %Y")
convertedToDate = curveToDate.strftime("%B %d %Y")
#Convert to "blank"
convertedFromDate = "" if convertedFromDate == "December 30 2382" else convertedFromDate
convertedToDate = "" if convertedToDate == "December 30 2382" else convertedToDate
curveNumStr = curveNum
if j > 0:
curveNumStr += " (" + str(j+1) + ")"
self.gui.AddRowToAppRange(curveNumStr, convertedFromDate, convertedToDate)
self.gui.SetCurveCombo(self.ratingCurveList, self.rcIndex)
def ParseRatingTXTFile(self, filepath):
if self.mode == "DEBUG":
print "Parsing TXT File"
# Changing data stuff, reset self.data to None
self.data = None
self.period = None
self.curveNum = None
if self.gui is not None:
#Clear xml table before disabling
self.gui.ClearAllAppRows()
self.gui.EnableXML(False)
textFile = open(filepath, "r")
data = textFile.readlines()
data = [x.split() for x in data]
for i, row in enumerate(data):
row[0] = float(row[0])
row[1] = float(row[1])
self.data = data
self.SetupCurveFromTxt()
self.ratingCurveList = []
if self.gui is not None:
self.gui.SetCurveCombo(self.ratingCurveList, self.rcIndex) # triggers hist values update
return self.ratingCurveList
def ParseRatingJsonFile(self, filepath):
if self.mode == "DEBUG":
print "Parsing Json File"
# print"under constraction!!"
# Changing data stuff, reset self.data to None
self.data = None
self.period = None
self.curveNum = None
self.shiftDiffDisch = None
self.periodData = []
self.curveIdList = []
# Clear old table
if self.gui is not None:
self.gui.ClearAllAppRows()
self.gui.EnableXML(True)
json_filepath = str(filepath.replace('\\', '\\\\'))
# print type(json_filepath),json_filepath
try:
with open(json_filepath) as curves_data:
Curves = json.load(curves_data)
self.data = Curves
# print Curves
except:
print "Json file can't read."
curves_data = Curves['RatingCurves']
# print len(curves_data)
curves_data.reverse()
for curv in curves_data:
curveId = str(curv['Id'])
curvePeriod = curv['PeriodsOfApplicability']
idNum = 1
self.ratingCurveList.append(curveId)
if self.gui is not None:
for curvPeriod in curvePeriod:
curveFromDate = datetime.strptime(str(curvPeriod['StartTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
if int(curveFromDate.strftime("%Y")) < 1800 or int(curveFromDate.strftime("%Y")) > 2500:
convertedFromDate = ''
else:
convertedFromDate = curveFromDate.strftime("%B %d %Y")
# curveFromDate = curvPeriod['StartTime']
curveToDate = datetime.strptime(str(curvPeriod['EndTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
if int(curveToDate.strftime("%Y")) < 1800 or int(curveToDate.strftime("%Y")) > 2500:
convertedToDate = ''
else:
convertedToDate = curveToDate.strftime("%B %d %Y")
# curveToDate = curvPeriod['EndTime']
if idNum != 1:
curveNewId = curveId + '(' + str(idNum) + ')'
else:
curveNewId = curveId
idNum = idNum + 1
# print curveNewId,convertedFromDate,convertedToDate
self.gui.AddRowToAppRange(curveNewId, convertedFromDate, convertedToDate)
curvPeriData = []
curvPeriData.append(str(curveNewId))
curvPeriData.append(str(convertedFromDate))
curvPeriData.append(str(convertedToDate))
self.periodData.append(curvPeriData)
self.gui.SetCurveCombo(self.ratingCurveList, self.rcIndex)
return self.ratingCurveList
def OnRCUpdate(self, selectedCurveIndex):
if self.gui is not None:
self.gui.ClearAllHistRows()
if self.histData is None:
return
print "Hist data is not None"
# iterate through each hist value
# for each, calc appropriate Shift and
for i, row in enumerate(reversed(self.histData)):
shift = None
error = None
# print row
date = row[0]
stage = row[1]
disch = row[2]
width = row[3]
area = row[4]
waterVelo = row[5]
remarks = row[6]
if disch == "" or stage == "":
shift = ""
error = ""
else:
if self.extension == "xml":
RCType = self.data[selectedCurveIndex][0].get('type')
points = self.data[selectedCurveIndex][2]
# Check bounds
hg_low = float(points[0].find('hg').text)
hg_high = float(points[-1].find('hg').text)
q_low = float(points[0].find('qr').text)
q_high = float(points[-1].find('qr').text)
if stage > hg_high or stage < hg_low \
or disch < q_low or disch > q_high:
shift = ""
error = ""
elif RCType == "Linear":
shift, error = self.CalcLinShiftQdiff(points, stage, disch)
elif RCType == "Logarithmic":
shift, error = self.CalcLogShiftQdiff(points, stage, disch)
elif self.extension == "txt":
points = self.data
# Check bounds
hg_low = float(points[0][1])
hg_high = float(points[-1][1])
q_low = float(points[0][0])
q_high = float(points[-1][0])
if stage > hg_high or stage < hg_low \
or disch < q_low or disch > q_high:
shift = ""
error = ""
else:
shift, error = self.CalcTxtShiftQdiff(points, stage, disch)
# json file
else:
RCType = self.data['RatingCurves'][selectedCurveIndex]['Type']
points = self.data['RatingCurves'][selectedCurveIndex]
# print points
# print RCType
# Check bounds
hg_low = float(points['BaseRatingTable'][0]['InputValue'])
hg_high = float(points['BaseRatingTable'][-1]['InputValue'])
q_low = float(points['BaseRatingTable'][0]['OutputValue'])
q_high = float(points['BaseRatingTable'][-1]['OutputValue'])
# print i, stage, hg_high, hg_low, disch, q_low, q_high
if stage > hg_high or stage < hg_low \
or disch < q_low or disch > q_high:
shift = ""
error = ""
elif RCType == "LinearTable":
# print "Linear"
shift, error = self.CalcLinShiftQdiffJson(points, stage, disch)
elif RCType == "LogarithmicTable":
# print "Logarithmic"
shift, error = self.CalcLogShiftQdiffJson(points, stage, disch)
self.gui.AddRowToHistData(date, str(stage), str(disch), str(width), str(area), str(waterVelo), str(error), str(shift), str(remarks))
if self.gui is not None:
### TODO look at if this function will work for json too? if yes then remove the condition
if self.extension == "xml":
self.CalculateShiftDisch(self.obsStage, self.obsDisch, selectedCurveIndex)
def ValidateQdiff(self, q, Qr):
if Qr == 0 or math.isnan(Qr):
return float('nan')
return float(format(((q - Qr)/Qr)*100,".3g"))
def CalcLinQrHr(self, points, hg, q):
a = None
b = None
a2 = None
b2 = None
hgFound = False
qFound = False
#a and b to calculate Qdiff
for i, point in enumerate(points):
hgpas = float(point.find('hg').text)
qpas = float(point.find('qr').text)
if hg <= hgpas and not hgFound:
equation = point.find('equation')
if equation is not None:
a = float(equation.find('a').text)
b = float(equation.find('b').text)
hgFound = True
if q <= qpas and not qFound:
equation = point.find('equation')
if equation is not None:
a2 = float(equation.find('a').text)
b2 = float(equation.find('b').text)
qFound = True
if qFound and hgFound:
break
# Do Calculations
Qr=(a * hg)+ b
Hr=(1/a2 * (q - b2))
return Qr, Hr
# TODO test if this will work if yes split later
def CalcLinShiftQdiffJson(self, points, hg, q):
a = None
b = None
a2 = None
b2 = None
# a and b to calculate Qdiff
# print len(points['BaseRatingTable'])
for i in range(len(points['BaseRatingTable'])):
hgpas = float(points['BaseRatingTable'][i]['InputValue'])
# print hgpas, hg
if hg < hgpas:
lowPoint = points['BaseRatingTable'][i - 1]
highPoint = points['BaseRatingTable'][i]
a = (highPoint['OutputValue'] - lowPoint['OutputValue']) / (
highPoint['InputValue'] - lowPoint['InputValue'])
b = highPoint['OutputValue'] - a * highPoint['InputValue']
break
# find a and b to calculate Shift
for i in range(len(points['BaseRatingTable'])):
qpas = float(points['BaseRatingTable'][i]['OutputValue'])
if q < qpas:
lowPoint = points['BaseRatingTable'][i - 1]
highPoint = points['BaseRatingTable'][i]
a2 = (highPoint['OutputValue'] - lowPoint['OutputValue']) / (
highPoint['InputValue'] - lowPoint['InputValue'])
b2 = highPoint['OutputValue'] - a2 * highPoint['InputValue']
break
# Do Calculations
# print a, hg, b
if a is not None and b is not None and a2 is not None and b2 is not None:
Qr = (a * hg) + b
Hr = (1 / a2 * (q - b2))
Shift = float(format(Hr - hg, ".3g"))
Qdiff = float(format(((q - Qr) / Qr) * 100, ".3g"))
else:
Shift = ''
Qdiff = ''
return Shift, Qdiff
def CalcLogQrHr(self, points, hg, q):
offset = None
beta = None
c = None
qoffset = None
qbeta = None
qc = None
hg1 = None
q1 = None
qrPoints = None
hrPoints = None
hgFound = False
qFound = False
for i, point in enumerate(points):
hg2 = float(point.find('hg').text)
q2 = float(point.find('qr').text)
if hg <= hg2 and not hgFound:
equation = point.find('equation')
if equation is not None:
offset = float(point.find('offset').text)
beta = float(equation.find('beta').text)
c = float(equation.find('c').text)
if math.isinf(beta) or math.isinf(c):
qrPoints = ( (hg1, q1), (hg2, q2) )
hgFound = True
if q <= q2 and not qFound:
equation = point.find('equation')
if equation is not None:
qoffset = float(point.find('offset').text)
qbeta = float(equation.find('beta').text)
qc = float(equation.find('c').text)
if math.isinf(qbeta) or math.isinf(qc):
hrPoints = ( (hg1, q1), (hg2, q2) )
qFound = True
if hgFound and qFound:
break
hg1 = hg2
q1 = q2
# Do the calculations
if qrPoints is None:
Qr = c * (hg - offset)**beta
else:
p1 = qrPoints[0]
p2 = qrPoints[1]
a = (p2[1] - p1[1]) / (p2[0] - p1[0])
b = p2[1] - a * p2[0]
Qr = (a*hg) + b
if hrPoints is None:
Hr = qoffset+(q/qc)**(1/qbeta)
else:
p1 = hrPoints[0]
p2 = hrPoints[1]
a2 = (p2[1] - p1[1]) / (p2[0] - p1[0])
b2 = p2[1] - a2 * p2[0]
Hr = (1/a2* (q-b2))
return Qr, Hr
def CalcLogQrHrJson(self, points, hg, q):
offset = None
beta = None
c = None
qoffset = None
qbeta = None
qc = None
offsetList = []
breakPointList = []
for offset_val in points['Offsets']:
offsetList.append(offset_val['Offset'])
try:
breakPointList.append(offset_val['InputValue'])
except:
pass
for i in range(len(points['BaseRatingTable'])):
hgpas = float(points['BaseRatingTable'][i]['InputValue'])
if hg <= hgpas:
lowPoint = points['BaseRatingTable'][i-1]
highPoint = points['BaseRatingTable'][i]
for nn in range(len(breakPointList)):
if highPoint['InputValue']<=breakPointList[nn]:
offset = offsetList[nn]
beta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-offset)/(lowPoint['InputValue']-offset))
c = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-offset, beta)
break
if offset is None:
offset = offsetList[-1]
beta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-offset)/(lowPoint['InputValue']-offset))
c = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-offset, beta)
break
# find offset, beta, c to calculate Shift
for i in range(len(points['BaseRatingTable'])):
qpas = float(points['BaseRatingTable'][i]['OutputValue'])
if q <= qpas:
lowPoint = points['BaseRatingTable'][i-1]
highPoint = points['BaseRatingTable'][i]
for nn in range(len(breakPointList)):
if highPoint['InputValue']<=breakPointList[nn]:
qoffset = offsetList[nn]
qbeta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-qoffset)/(lowPoint['InputValue']-qoffset))
qc = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-qoffset, qbeta)
break
if qoffset is None:
qoffset = offsetList[-1]
qbeta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-qoffset)/(lowPoint['InputValue']-qoffset))
qc = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-qoffset, qbeta)
break
# Do the calculations
if c is not None and qc is not None and offset is not None and qoffset is not None and beta is not None and qbeta is not None:
Qr=c * (hg - offset)**beta
Hr=qoffset+(q/qc)**(1/qbeta)
# print hgpas, qpas, offset, beta, qoffset, qbeta
return Qr, Hr
# TODO test it and if work maybe split it later
def CalcLogShiftQdiffJson(self, points, hg, q):
offset = None
beta = None
c = None
qoffset = None
qbeta = None
qc = None
offsetList = []
breakPointList = []
for offset_val in points['Offsets']:
offsetList.append(offset_val['Offset'])
try:
breakPointList.append(offset_val['InputValue'])
except:
pass
for i in range(len(points['BaseRatingTable'])):
hgpas = float(points['BaseRatingTable'][i]['InputValue'])
if hg < hgpas:
lowPoint = points['BaseRatingTable'][i-1]
highPoint = points['BaseRatingTable'][i]
for nn in range(len(breakPointList)):
if highPoint['InputValue']<=breakPointList[nn]:
offset = offsetList[nn]
beta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-offset)/(lowPoint['InputValue']-offset))
c = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-offset, beta)
if offset is None:
offset = offsetList[-1]
beta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-offset)/(lowPoint['InputValue']-offset))
c = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-offset, beta)
break
# find offset, beta, c to calculate Shift
for i in range(len(points['BaseRatingTable'])):
qpas = float(points['BaseRatingTable'][i]['OutputValue'])
if q < qpas:
lowPoint = points['BaseRatingTable'][i-1]
highPoint = points['BaseRatingTable'][i]
for nn in range(len(breakPointList)):
if highPoint['InputValue']<=breakPointList[nn]:
qoffset = offsetList[nn]
qbeta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-qoffset)/(lowPoint['InputValue']-qoffset))
qc = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-qoffset, beta)
if qoffset is None:
qoffset = offsetList[-1]
qbeta = math.log10(highPoint['OutputValue']/lowPoint['OutputValue'])/math.log10((highPoint['InputValue']-qoffset)/(lowPoint['InputValue']-qoffset))
qc = lowPoint['OutputValue']/math.pow(lowPoint['InputValue']-qoffset, beta)
break
# Do the calculations
if c is not None and qc is not None and offset is not None and qoffset is not None and beta is not None and qbeta is not None:
Qr=c * (hg - offset)**beta
Hr=qoffset+(q/qc)**(1/qbeta)
Shift = float(format(Hr-hg,".3g"))
Qdiff = float(format(((q-Qr)/Qr)*100,".3g"))
else:
Shift = ''
Qdiff = ''
# print hgpas, qpas, offset, beta, qoffset, qbeta
return Shift, Qdiff
def CalcLinShiftQdiff(self, points, hg, q):
Qr, Hr = self.CalcLinQrHr(points, hg, q)
Shift = float(format(Hr - hg,".3g"))
Qdiff = self.ValidateQdiff(q, Qr)
return Shift, Qdiff
def CalcLogShiftQdiff(self, points, hg, q):
Qr, Hr = self.CalcLogQrHr(points, hg, q)
Shift = float(format(Hr-hg,".3g"))
Qdiff = self.ValidateQdiff(q, Qr)
return Shift, Qdiff
def CalcTxtQrHr(self, points, hg, q):
# Qr calculations
qr_hg1 = qr_hg2 = qr_q1 = qr_q2 = None
# Hr calculations
hr_hg1 = hr_hg2 = hr_q1 = hr_q2 = None
hgFound = False
qFound = False
for i, row in enumerate(points):
qpas = row[0]
hgpas = row[1]
if hg <= hgpas and not hgFound:
qr_hg1 = self.data[i-1][1]
qr_q1 = self.data[i-1][0]
qr_hg2 = row[1]
qr_q2 = row[0]
hgFound = True
if q <= qpas and not qFound:
hr_hg1 = self.data[i-1][1]
hr_q1 = self.data[i-1][0]
hr_hg2 = row[1]
hr_q2 = row[0]
qFound = True
if hgFound and qFound:
break
a1 = (qr_q2 - qr_q1) / (qr_hg2 - qr_hg1)
b1 = qr_q2 - a1 * qr_hg2
a2 = (hr_q2 - hr_q1) / (hr_hg2 - hr_hg1)
b2 = hr_q2 - a2 * hr_hg2
# Do calculations
Qr = (a1*hg)+ b1
Hr = (1/a2* (q-b2))
return Qr, Hr
def CalcTxtShiftQdiff(self, points, hg, q):
Qr, Hr = self.CalcTxtQrHr(points, hg, q)
Shift = float(format(Hr-hg,".3g"))
Qdiff = self.ValidateQdiff(q, Qr)
return Shift, Qdiff
def ParseHistCSVFile(self, filepath):
if self.mode == "DEBUG":
print "Parsing CSV Historical Data File"
# Reset Historical Data
self.histData = None
self.delHist = None
self.minList = []
self.maxList = []
if self.gui is not None:
self.gui.ClearAllHistRows()
csvFile = open(filepath, "r")
histData = csvFile.readlines()[1:]
histData = [x.strip().split(',') for x in histData]
listToDel = []
for i, row in enumerate(histData):
# print "####################"
# print row
if row[1] == "" or row[2] == "":
listToDel.append(i)
row[1] = "" if row[1] == "" else float(row[1])
row[2] = "" if row[2] == "" else float(row[2])
if "min" in row[6].lower():
self.minList.append(row)
if "max" in row[6].lower():
self.maxList.append(row)
# delete invalid historical data values
self.delHist = listToDel
self.histData = histData
if self.gui is not None:
self.OnRCUpdate(self.gui.GetSelectedCurveIndex())
def CalculateShiftDisch(self, Hobserved, Qobserved, selectedCurveIndex):
if self.mode == "DEBUG":
print "Calculating Shift and Discharge"
errorMessage = None
if self.extension is None:
errorMessage = "Please use a valid Rating Curve File - it is an XML file best extracted from the ADET"
elif Hobserved == "":
#show error dialog
errorMessage = "Observed Stage is empty. Please enter a value."
elif Qobserved == "":
#show error dialog
errorMessage = "Observed Discharge is empty. Please enter a value."
elif self.extension == "xml":
if selectedCurveIndex < 0 or selectedCurveIndex >= len(self.ratingCurveList):
errorMessage = "Rating Curve file is invalid."
# TODO add checking for json file
# Check if anything went wrong
if errorMessage is not None:
if self.gui is not None:
self.gui.SetStatus(errorMessage)
return errorMessage
# Reset shift/disch calculations
self.shiftDiffDisch = None
self.ResetPlotVars()
# Store obsDisch, obsStage
self.obsDisch = Qobserved
self.obsStage = Hobserved
self.rcIndex = selectedCurveIndex
if self.gui is not None:
self.gui.ratingCurveCombo.SetSelection(self.rcIndex)
if self.extension == "xml":
return self.CalculateXMLShiftDisch(selectedCurveIndex)
elif self.extension == "txt":
return self.CalculateTXTShiftDisch(selectedCurveIndex)
elif self.extension == "json":
return self.CalculateJSONShiftDisch(selectedCurveIndex)
# Calculate shift and discharge for json file reading
def CalculateJSONShiftDisch(self, selectedCurveIndex):
RCType = self.data['RatingCurves'][selectedCurveIndex]['Type']
self.curveNum = self.data['RatingCurves'][selectedCurveIndex]['Id']
# points list
points = self.data['RatingCurves'][selectedCurveIndex]['BaseRatingTable']
calPoints = self.data['RatingCurves'][selectedCurveIndex]
# Determine Period (for historical data only)
start = None
end = None
for i, period in enumerate(self.data['RatingCurves'][selectedCurveIndex]['PeriodsOfApplicability']):
if i == 0:
start = datetime.strptime(str(period['StartTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
end = datetime.strptime(str(period['EndTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
else:
currStart = datetime.strptime(str(period['StartTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
currEnd = datetime.strptime(str(period['EndTime'])[0:-14], "%Y-%m-%dT%H:%M:%S")
if start > currStart:
start = currStart
if end < currEnd:
end = currEnd
if start is None or end is None:
self.period = None
else:
self.period = (start, end)
# assumes that the first point is lowest, last point is highest
# checks against high and low points first
hg_low = float(calPoints['BaseRatingTable'][0]['InputValue'])
hg_high = float(calPoints['BaseRatingTable'][-1]['InputValue'])
q_low = float(calPoints['BaseRatingTable'][0]['OutputValue'])
q_high = float(calPoints['BaseRatingTable'][-1]['OutputValue'])
errorMessage = self.ValidateCalculations(hg_low, hg_high, q_low, q_high)
if errorMessage is not None: