-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.py
1544 lines (1210 loc) · 56.5 KB
/
init.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
'''Initialization functions for the Bay Assessment Model (BAM)'''
# Python distribution modules
from os import cpu_count
from datetime import timedelta, datetime
from collections import OrderedDict as odict
strptime = datetime.strptime
# Community modules
from scipy import interpolate
from numpy import array as nparray
from numpy import zeros as npzeros
# Worker pool to parallelize generation of tide interpolators
from multiprocessing import Pool
# Library for reading ArcGIS shapefile see:
# https://github.com/GeospatialPython/pyshp
import shapefile
# Local modules
import basins
import shoals
# Kludge since multiprocessing can't handle embedded Tk
import pool_functions
#-----------------------------------------------------------
#
#-----------------------------------------------------------
def InitTimeBasins( model ):
'''Reset time variables, read in Basin parameters from the
basinInit file, initialize basin volumes based on the
initial water levels, and call any required initializations
for Rain, ET, Salinity, BCs.'''
if model.args.DEBUG_ALL :
print( '-> InitTimeBasins' )
if model.start_time > model.end_time :
msg = 'Init Error: start time is after end time.\n'
model.gui.Message( msg )
return
model.simulation_days = (model.end_time - model.start_time).days
model.state = model.status.Init
model.times.clear()
model.current_time = model.start_time
if not model.args.noGUI :
model.gui.current_time_label.set( str( model.current_time ) )
model.unix_time = ( model.current_time -
datetime(1970,1,1) ).total_seconds()
if not InitialBasinValues( model ) : # -bi basinInit file
msg = '\nBasin initialization failed.\n'
model.gui.Message( msg )
return
# Initialize basin volume, area, salt_mass based on intial water levels
for Basin in model.Basins.values() :
Basin.InitVolume()
Basin.plot_variables.clear()
# Call additional initialization methods if required
time_changed = ( model.previous_start_time != model.start_time or \
model.previous_end_time != model.end_time )
if not model.args.noTide and time_changed : # -nt
if not GetBasinTidalData( model ) : # -bt
errMsg = '\nGetBasinTidalData failed. See the console.\n'
raise Exception( errMsg )
if not model.args.noMeanSeaLevel and time_changed : # -nm
GetSeasonalMSL( model ) # -sm
if not model.args.noRain and time_changed : # -nr
GetBasinRainData( model ) # -br
if not model.args.noET_Amplify and time_changed : # -na
GetTemperatureData( model ) # -st
if not model.args.noET and time_changed : # -ne
GetETData( model ) # -et
if not model.args.noStageRunoff and time_changed : # -nR
GetBasinRunoffStageData( model ) # -bR
if time_changed :
GetBasinStageData( model ) # -bs
if not model.args.noDynamicBoundaryConditions and time_changed : # -db
GetBasinDynamicBCData( model ) # -bc
if model.args.fixedBoundaryConditions : # -fb
GetBasinFixedBoundaryCondition( model ) # -bf
if model.args.gaugeSalinity and time_changed : # -gs
GetBasinSalinityData( model ) # -sf
# If salinityInit is 'yes' (-si), then override salinity from the
# basinInit file (-bi) with the closest gauge data as mapped in the
# basinParameter (-bp) file.
if model.args.salinityInit.lower() == 'yes' :
GetBasinSalinityData ( model )
SetInitialBasinSalinity( model )
# Report simulation parameters
output_hours = model.outputInterval.days * 24 +\
model.outputInterval.seconds//3600
msg = str( model.start_time ) + ' to ' +\
str( model.end_time ) +\
' Δt ' + str( model.args.timestep ) + ' (s)' +\
' Output '+ str( output_hours ) +' (hr)' +\
' V\u209C\u2092\u2097 ' +\
str( round( model.args.velocity_tol, 4 )) + ' (m/s)\n'
model.gui.Message( msg )
if not model.args.noGUI :
model.gui.RenderBasins( init = True )
model.gui.PlotLegend( "InitTimeBasins" )
model.gui.canvas.draw()
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def CreateBasinsFromShapefile( model ):
'''Instantiate Basin objects into the Basins dictionary.
A record in a shapefile contains the attributes for each shape
in the collection of geometry. Records are stored in the dbf file.
The link between geometry and attributes is the foundation of GIS.
This critical link is implied by the order of shapes and corresponding
records in the shp geometry file and the dbf attribute file.'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinShapefileParams', flush = True )
# A temporary map to check for duplicate basin names
basinNameNumMap = {}
# Read the shapefile (-x)
# shapes() method returns a list of the shapefile's geometry
# iterRecords() returns an iterator to the shapefile records
# Each shape record contains the following attributes:
# bbox parts points shapeType
# points are vectors of xy
sf_basins = shapefile.Reader( model.args.basinShapeFile )
# A record from sf_basins.iterRecords() is a list of 4 strings:
# Area Perimeter Number Name
# ['8.310187961009e+007', '3.934640784246e+004', '5', 'Barnes Sound']
#
# Dual iteration over the records and shapes
for record, shape in zip( sf_basins.iterRecords(), sf_basins.shapes() ):
basin_xy = nparray( shape.points )
if len( record ) != 4 :
errMsg = 'Shapefile record has a length of ' + \
+ str( record.len() ) + '. It must be of length 4.' + \
' Record is: \n' + record
raise Exception( errMsg )
total_area = float( record[ 0 ] )
perimeter = float( record[ 1 ] )
number = int ( record[ 2 ] )
name = record[ 3 ]
# check for duplicate record
if number in model.Basins.keys() :
errMsg = 'Duplicate basin number [' + number + \
'] found in shapefile records.'
raise Exception( errMsg )
if name in basinNameNumMap.keys() :
errMsg = 'Duplicate basin name: ', name, ' [' + str(number) + \
'] found in shapefile records.'
raise Exception( errMsg )
# Instantiate and save the Basin object
model.Basins[ number ] = basins.Basin( model, name, number,
total_area,
perimeter, basin_xy )
basinNameNumMap[ name ] = number
if model.args.DEBUG_ALL :
print( record, flush = True )
# Add boundary Basin objects.
# Ensure boundary basins do not exist
for basin in [ 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 ] :
if basin in model.Basins.keys() :
errMsg = 'Duplicate boundary basin number [' + basin + \
'] found in shapefile records.'
raise Exception( errMsg )
# JP : These boundary basins are Hardcoded... Bogus!
# 10 Tidal Boundary basins
model.Basins[ 59 ] = basins.Basin( model, 'Gulf Tide 1', 59,0,0,None,True )
model.Basins[ 60 ] = basins.Basin( model, 'Gulf Tide 2', 60,0,0,None,True )
model.Basins[ 61 ] = basins.Basin( model, 'Gulf Tide 3', 61,0,0,None,True )
model.Basins[ 62 ] = basins.Basin( model, 'Gulf Tide 4', 62,0,0,None,True )
model.Basins[ 63 ] = basins.Basin( model, 'Ocean Tide 5',63,0,0,None,True )
model.Basins[ 64 ] = basins.Basin( model, 'Ocean Tide 6',64,0,0,None,True )
model.Basins[ 65 ] = basins.Basin( model, 'Ocean Tide 7',65,0,0,None,True )
model.Basins[ 66 ] = basins.Basin( model, 'Ocean Tide 8',66,0,0,None,True )
model.Basins[ 67 ] = basins.Basin( model, 'Ocean Tide 9',67,0,0,None,True )
model.Basins[ 68 ] = basins.Basin( model, 'Card Sound Tide 10',68,0,0,
None,True)
# 12 Everglades Boundary basins
model.Basins[ 69 ] = basins.Basin( model, 'EVER to Snake Bight',
69,0,0,None,True )
model.Basins[ 70 ] = basins.Basin( model, 'EVER to Rankin Lake',
70,0,0,None,True )
model.Basins[ 71 ] = basins.Basin( model, 'EVER to Rankin Bight',
71,0,0,None,True )
model.Basins[ 72 ] = basins.Basin( model, 'EVER to North Whipray',
72,0,0,None,True )
model.Basins[ 73 ] = basins.Basin( model, 'EVER to Terrapin Bay',
73,0,0,None,True )
model.Basins[ 74 ] = basins.Basin( model, 'EVER to Madeira Bay',
74,0,0,None,True )
model.Basins[ 75 ] = basins.Basin( model, 'EVER to Little Madeira Bay',
75,0,0,None,True )
model.Basins[ 76 ] = basins.Basin( model, 'EVER to Eagle Key',
76,0,0,None,True )
model.Basins[ 77 ] = basins.Basin( model, 'EVER to Joe Bay',
77,0,0,None,True )
model.Basins[ 78 ] = basins.Basin( model, 'EVER to Deer Key',
78,0,0,None,True )
model.Basins[ 79 ] = basins.Basin( model, 'EVER to Long Sound',
79,0,0,None,True )
model.Basins[ 80 ] = basins.Basin( model, 'EVER to Manatee Bay',
80,0,0,None,True )
model.Basins[ 81 ] = basins.Basin( model, 'EVER to Conchie Channel',
81,0,0,None,True )
model.Basins[ 82 ] = basins.Basin( model, 'EVER to Barnes Sound',
82,0,0,None,True )
if model.args.DEBUG_ALL :
for basin_num, Basin in model.Basins.items() :
print( str( basin_num ), Basin.name )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinAreaDepths( model ):
'''Read the GIS data with basin areas at each depth from
the basinDepth file (-bd)'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinAreaDepths', flush = True )
# The csv file has 12 columns, 1 = basin number
# 2 - 11 = area at each depth, 12 = land area,
# first row is header, get the depths from it
fd = open( model.args.path + model.args.basinDepth, 'r' )
rows = fd.readlines()
fd.close()
# Get header values (depths) from first row
row = rows[ 0 ] # first row of rows
words = row.split( ',' )
depths = npzeros( len( words ) - 2 )
# Skip first and last columns : range( 1, len( row ) - 1 )
j = 0
for i in range( 1, len( words ) - 1 ) :
depths[ j ] = int( words[ i ].strip( 'ft' ) )
j = j + 1
# Process each row of data, skip the header
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
basinNumber = int( words[ 0 ] ) # first element of first row
if model.args.DEBUG_ALL :
print( 'Basin', basinNumber, end = ': ', flush = True )
if basinNumber not in model.Basins.keys() :
continue
# For each depth save the area in the Basin.wet_area dict
for j in range( len( depths ) ) :
depth = depths[ j ]
area = words[ j + 1 ].strip() # j + 1 for first column
model.Basins[ basinNumber ].wet_area[ depth ] = float( area )
if model.args.DEBUG_ALL :
print( depth, '[', j, ']=', area,
end = ' ; ', sep = '', flush = True )
if model.args.DEBUG_ALL :
print( '', flush = True )
# Save the land area
model.Basins[ basinNumber ].land_area = \
float( words[ len( words ) - 1 ] )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinParameters( model ):
'''Read basin parameters such as Rain/ET/Runoff mappings (-bp)'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinParameters', flush = True )
for basin_num, Basin in model.Basins.items() :
print( basin_num, ' : ', Basin.name )
# The csv file has 5 columns, 1 = basin number, 2 = basin name
# 3 = [ Rain stations ], 4 = [ Rain scales ], 5 = Salinity station,
# 6 = ET Amplify
# first row is header
fd = open( model.args.path + model.args.basinParameters, 'r' )
rows = fd.readlines()
fd.close()
# Create a mapping of column index and variable name
header = rows[ 0 ].split( ',' )
words = [ word.strip() for word in header ]
var_column_map = dict()
for word in words :
var_column_map[ word ] = words.index( word )
# Validate the file has the correct columns
valid_columns = [ 'Basin', 'Name', 'Rain Gauge',
'Rain Scale', 'Gauge', 'ET Amplify' ]
for valid_column in valid_columns :
if valid_column not in words :
errMsg = 'GetBasinParameters: Basin parameters ' +\
model.args.basinParameters +\
' does not have column ', valid_column
raise Exception( errMsg )
# Process each row of data, skip the header
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split( ',' )
# Get the Basin object
basin_num = int( words[ var_column_map[ 'Basin' ] ] )
basin_name = words[ var_column_map[ 'Name' ] ].strip()
if basin_num not in model.Basins.keys() :
errMsg = 'GetBasinParameters: Failed to find basin ' +\
basin_name + ' number: ', basin_num
raise Exception( errMsg )
Basin = model.Basins[ basin_num ]
# Add list of rain stations and scales to the Basin object
rain_stations = \
words[ var_column_map[ 'Rain Gauge' ] ].strip('[] ').split()
_rain_scales = \
words[ var_column_map[ 'Rain Scale' ] ].strip('[] ').split()
rain_scales = list( map( float, _rain_scales ) )
if not model.args.noRain and not Basin.boundary_basin :
Basin.rain_stations = rain_stations
Basin.rain_scales = rain_scales
# Add salinity station to the Basin
salinity_station = words[ var_column_map['Gauge']].strip()
if model.args.gaugeSalinity or \
model.args.salinityInit.lower() == 'yes' or \
Basin.boundary_basin :
if salinity_station == 'None' :
pass
else :
Basin.salinity_station = salinity_station
if model.args.gaugeSalinity :
Basin.salinity_from_data = True
# Add ET Amplify flag to the Basin
Basin.ET_amplify = "True" in words[ var_column_map['ET Amplify'] ]
if model.args.DEBUG_ALL :
print( Basin.name, ' [', Basin.number, ']' )
print( '\t', Basin.rain_stations, ' : ', Basin.rain_scales )
print( '\t', Basin.salinity_station )
print( '\t', Basin.ET_amplify )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinTidalData( model ):
"""Read data according to the basinTide file (-bt)
A scipy interpolate.interp1d function for the tidal anomalies
is stored in the appropriate basin object.
Note that this takes a long time, and has been parallelized
with multiprocessing Pool (see below and pool_functions.py)."""
if model.args.DEBUG_ALL :
print( '\n-> GetBasinTidalData', flush = True )
msg = 'Reading Tidal Boundary timeseries, please wait...'
model.gui.Message( msg )
if not model.args.noGUI :
model.gui.canvas.draw()
# The csv file has 3 columns: 1 = basin number, 2 = type,
# 3 = data file name
fd = open( model.args.path + model.args.basinTide, 'r' )
rows = fd.readlines()
fd.close()
basinList = []
# Validate each row of data, skip the header
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
basin = int ( words[ 0 ] )
data_type = words[ 1 ].strip()
data_file = words[ 2 ].strip()
if basin not in model.Basins.keys() :
errMsg = 'GetBasinTidalData() Error: Basin ' +\
str( basin ) + ' not found in the Basins map.\n'
raise Exception( errMsg )
if not model.Basins[ basin ].boundary_basin :
errMsg = 'GetBasinTidalData() Error: Basin ' +\
model.Basins[ basin ].name +\
' is not a boundary_basin.\n'
raise Exception( errMsg )
if data_type not in [ 'flow', 'stage', 'None' ] :
errMsg = 'GetBasinTidalData() Error: Basin ' +\
model.Basins[ basin ].name + ' invalid data type: ' +\
data_type + '.\n'
raise Exception( errMsg )
if model.args.DEBUG_ALL :
print( '\tValidated: ', str( basin ), data_type, data_file )
basinList.append( basin )
# Process each row of data, skip the header
# ReadTideBoundaryData returns a tuple with basin number and
# scipy interpolate.interp1d function which can be called with
# a unix time (Epoch seconds) argument to get demeaned tidal elevations.
N_processors = cpu_count()
num_processes = None
if N_processors > 3 :
num_processes = 4
elif N_processors == 2 :
num_processes = 2
else :
num_processes = 1
pool = Pool( processes = num_processes )
#------------------------------------------------------------
# Kludge since multiprocessing can't serialize the Tk object
# embedded in the Model class object.
# Explicitly extract and pass args. (path, start, end):
path = model.args.path
start = model.start_time
# Add extra time to end_time for model.ReadTideBoundaryData
end = model.end_time + timedelta( hours = 3 )
# Create an iterable object of args to pass to Pool.map_async()
# rows are the list of lines from the basinTide file (-bt)
n_row = len( rows ) - 1
args = zip( rows[ 1 : ], [start] * n_row, [end] * n_row, [path] * n_row )
# map_async() : A variant of the map() method that returns a result object
# of class multiprocessing.pool.AsyncResult
results = pool.map_async( pool_functions.ReadTideBoundaryData, args )
#------------------------------------------------------------
# Must call AsyncResult.get() to spawn/wait for map_async() results
result = results.get()
msg = 'finished.\n'
err = True
# Save the boundary data function to each basin object
for tide_boundary_tuple in result :
if tide_boundary_tuple == None : # 'None'
continue
elif tide_boundary_tuple == False :
msg = '\n\n*** Error in ReadTideBoundaryData. ' +\
'Tides not initialized. ***\n\n'
err = False
break
else :
Basin = model.Basins[ tide_boundary_tuple[ 0 ] ]
Basin.boundary_function = tide_boundary_tuple[ 1 ]
if model.args.DEBUG_ALL :
print( Basin.name,
' [', tide_boundary_tuple[ 0 ], ']: Tide value ',
tide_boundary_tuple[ 1 ]( 1262305800 ) )
model.gui.Message( msg )
return( err )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetSeasonalMSL( model ):
"""Read data according to the seasonalMSL file (-sm)
A scipy interpolate.splrep() object for the MSL anomalies
is stored in seasonal_MSL_splrep. This spline represenation
is used with the unix_timestamp to generate interpolated values
via a call to interpolate.splev() in GetTides()"""
if model.args.DEBUG_ALL :
print( '\n-> GetSeasonalMSL', flush = True )
# The csv file has 2 columns: 1 = Date, 2 = anomaly
fd = open( model.args.path + model.args.seasonalMSL, 'r' )
rows = fd.readlines()
fd.close()
unix_times = []
values = []
# Process each row of data, skip the header
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
unix_time = ( strptime( words[ 0 ], '%Y-%m-%d' ) -
datetime(1970,1,1) ).total_seconds()
unix_times.append( unix_time )
values.append( float( words[ 1 ] ) )
# Create the scipy interpolate spline representation
model.seasonal_MSL_splrep = interpolate.splrep( unix_times, values, s=0 )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinRainData( model ):
'''Read daily rain data (-br)
Rain station to basin mappings are listed in the basinParameters
init file and stored in rain_stations{} in GetBasinParameters().
Populate rain_data dictionary'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinRainData', flush = True )
# The csv file has 18 columns, 1 = YYYY-MM-DD
# 2 - 18 = Daily cumulative rainfall in cm at:
# BK_cm_day, BA_cm_day, BN_cm_day, BS_cm_day, DK_cm_day, GB_cm_day,
# HC_cm_day, JK_cm_day, LB_cm_day, LM_cm_day, LR_cm_day, LS_cm_day,
# MK_cm_day, PK_cm_day, TC_cm_day, TR_cm_day, WB_cm_day
# first row is header
fd = open( model.args.path + model.args.basinRain, 'r' )
rows = fd.readlines()
fd.close()
# Create list of station names in the order of the header/columns
stations = []
words = rows[ 0 ].split(',')
for i in range( 1, len( words ) ) : # Skip the time column
stations.append( words[ i ].strip()[0:2] )
# Create list of datetimes
dates = []
for i in range( 1, len( rows ) ) : # Skip the header
row = rows[ i ]
words = row.split(',')
dates.append( strptime( words[ 0 ], '%Y-%m-%d' ) )
# Find index in dates for start_time & end_time
start_i, end_i = GetTimeIndex( 'Rain', dates,
model.start_time, model.end_time )
if model.args.DEBUG_ALL :
print( 'Rain data start: ', str( dates[ start_i ] ),str( start_i ),
' end: ', str( dates[ end_i ] ),str( end_i ) )
print( rows[ start_i ] )
print( rows[ end_i ] )
# The rain_data is a nested dictionary intended to minimize
# dictionary key lookups to access basin rainfall for a
# specific year month day. The key is an integer 3-tuple of
# ( Year, Month, Day ), values are a station_rain dictionary.
# Populate only data needed for the simulation timeframe
for i in range( start_i, end_i + 1 ) :
row = rows[ i+1 ]
words = row.split(',')
station_rain = dict()
for j in range( 1, len( words ) ) :
station_rain[ stations[ j-1 ] ] = float( words[ j ] )
date = dates[ i ]
key = ( date.year, date.month, date.day )
model.rain_data[ key ] = station_rain
if model.args.DEBUG_ALL :
print( model.rain_data )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinSalinityData( model ):
'''Read daily salinity data (-sf)
Salinity station to basin mappings are listed in the basinParameters
init file and stored in salinity_stations{} in GetBasinParameters().
Populate salinity_data dictionary'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinSalinityData', flush = True )
# The csv file has 22 columns, 1 = YYYY-MM-DD
# 2 - 23 = Daily mean salinty at:
# BA, BK, BN, BS, DK, GB, HC, JK, LB, LM, LR, LS, MK,
# PK, TC, TR, WB, MB, MD, TP, Gulf_1, Ocean_1
# First row is header
try :
fd = open( model.args.path + model.args.salinityFile, 'r' )
rows = fd.readlines()
fd.close()
except OSError as err :
msg = "\nGetBasinSalinityData: OS error: {0}\n".format( err )
model.gui.Message( msg )
return
# Create list of station names in the order of the header/columns
if len( model.salinity_stations ) == 0 :
words = rows[ 0 ].split(',')
for i in range( 1, len( words ) ) : # Skip the time column
word = words[ i ].strip()
model.salinity_stations.append( word.strip('"') )
# Create list of datetimes
dates = []
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
dates.append( strptime( words[ 0 ], '%Y-%m-%d' ) )
# Find index in dates for start_time & end_time
start_i, end_i = GetTimeIndex( 'Salinity', dates,
model.start_time, model.end_time )
if model.args.DEBUG_ALL :
print( 'Salinity data start: ',
str( dates[ start_i ] ),str( start_i ),
' end: ', str( dates[ end_i ] ),str( end_i ) )
print( rows[ start_i ] )
print( rows[ end_i ] )
# The salinity_data is a nested dictionary intended to minimize
# dictionary key lookups to access salinity for a
# specific year month day. The key is an integer 3-tuple of
# ( Year, Month, Day ), values are a station_salinity dictionary.
if model.salinity_data :
model.salinity_data.clear()
# Populate only data needed for the simulation timeframe
for i in range( start_i, end_i + 1 ) :
row = rows[ i+1 ]
words = row.split(',')
station_salinity = dict()
for j in range( 1, len( words ) ) :
if words[ j ] == 'NA' :
salinity_value = None
else:
salinity_value = float( words[ j ] )
station_salinity[ model.salinity_stations[ j-1 ] ] = salinity_value
date = dates[ i ]
key = ( date.year, date.month, date.day )
model.salinity_data[ key ] = station_salinity
if model.args.DEBUG_ALL :
print( model.salinity_data )
#-----------------------------------------------------------
#
#-----------------------------------------------------------
def SetInitialBasinSalinity( model ) :
'''Based on the basinParameter { station : basin } mapping,
assign the initial salinity values at start_time'''
if model.args.DEBUG_ALL :
print( '\n-> SetInitialBasinSalinity', flush = True )
key = ( model.start_time.year,
model.start_time.month,
model.start_time.day )
station_salinity_map = model.salinity_data[ key ]
for Basin in model.Basins.values() :
if Basin.salinity_station :
try:
salinity_gauge = \
float( station_salinity_map[ Basin.salinity_station ] )
except ( TypeError, ValueError ) :
# Salinity data can be None if no data available "NA"
salinity_gauge = 0
msg = '\nSetInitialBasinSalinity: WARNING:' +\
' Basin ' + Basin.name + ' has no available salinity' +\
' data to initialize Basin.salinity... setting to 0.\n'
model.gui.Message( msg )
Basin.salinity = salinity_gauge
if model.args.DEBUG_ALL :
print( Basin.name, ' [', str( Basin.number ),
'] : ', Basin.salinity )
# recompute salt_mass for all basins
for Basin in model.Basins.values() :
# salt_mass (g) = salinity (g/kg) * Vol (m^3) * rho (kg/m^3)
Basin.salt_mass = Basin.salinity * Basin.water_volume * 997
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetETData( model ):
'''Read daily ET data (-et)'''
if model.args.DEBUG_ALL :
print( '\n-> GetETData', flush = True )
# The csv file has 2 columns, 1 = YYYY-MM-DD, 2 = PET mm/day
# first row is header
fd = open( model.args.path + model.args.ET, 'r' )
rows = fd.readlines()
fd.close()
# Create list of datetimes
dates = []
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
dates.append( strptime( words[ 0 ], '%Y-%m-%d' ) )
# Find index in dates for start_time & end_time
start_i, end_i = GetTimeIndex( 'ET', dates,
model.start_time, model.end_time )
if model.args.DEBUG_ALL :
print( 'ET data start: ', str( dates[ start_i ] ), str( start_i ),
' end: ', str( dates[ end_i ] ), str( end_i ) )
print( rows[ start_i ] )
print( rows[ end_i ] )
# Populate only data needed for the simulation timeframe
for i in range( start_i, end_i + 1 ) :
row = rows[ i+1 ]
words = row.split(',')
# The key is an integer 3-tuple of ( Year, Month, Day )
# values are PET in mm/day.
date = dates[ i ]
key = ( date.year, date.month, date.day )
model.et_data[ key ] = float( words[1] )
if model.args.DEBUG_ALL :
print( model.et_data )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetTemperatureData( model ):
'''Read daily max temperature data (-st)'''
if model.args.DEBUG_ALL :
print( '\n-> GetTemperatureData', flush = True )
# The csv file has 2 columns, 1 = YYYY-MM-DD, 2 = MaxTemp (C)
# first row is header
fd = open( model.args.path + model.args.surfaceTemp, 'r' )
rows = fd.readlines()
fd.close()
# Create list of datetimes
dates = []
for i in range( 1, len( rows ) ) :
row = rows[ i ]
words = row.split(',')
dates.append( strptime( words[ 0 ], '%Y-%m-%d' ) )
# Find index in dates for start_time & end_time
start_i, end_i = GetTimeIndex( 'Temperature', dates,
model.start_time, model.end_time )
if model.args.DEBUG_ALL :
print( 'Temperature data start: ',
str( dates[ start_i ] ), str( start_i ),
' end: ', str( dates[ end_i ] ), str( end_i ) )
print( rows[ start_i ] )
print( rows[ end_i ] )
# Populate only data needed for the simulation timeframe
for i in range( start_i, end_i + 1 ) :
row = rows[ i+1 ]
words = row.split(',')
# The key is an integer 3-tuple of ( Year, Month, Day )
# values are PET in mm/day.
date = dates[ i ]
key = ( date.year, date.month, date.day )
model.temperature_data[ key ] = float( words[1] )
if model.args.DEBUG_ALL :
print( model.temperature_data )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinRunoffStageData( model ):
'''Read daily stage data for Everglades basins (-bR)
Mapping of EDEN stage data in basinStageRunoff to model basins
is specified in basinStageRunoffMap (-bS)
Populate runoff_stage_data dictionary'''
if model.args.DEBUG_ALL :
print( '\n-> GetBasinRunoffStageData', flush = True )
# Get mapping of EDEN stage station to model basins
fd = open( model.args.path + model.args.basinStageRunoffMap, 'r' )
rows = fd.readlines()
fd.close()
# Create a mapping of column index and variable name
header = rows[ 0 ].split( ',' )
words = [ word.strip() for word in header ]
var_column_map = dict()
for word in words :
var_column_map[ word ] = words.index( word )
# Validate the file has the correct columns
valid_columns = [ 'Source_Basin', 'EDEN_Station',
'Dest_Basin', 'Shoals' ]
for valid_column in valid_columns :
if valid_column not in words :
errMsg = 'EVER basin runoff ' + model.args.basinStageRunoffMap +\
' does not have ', valid_column
raise Exception( errMsg )
# Get Basin : EDEN station data mapping
# The basinStageRunoffMap also contains the shoals between the
# EVER boundary basins and model basins, and the destination
# basin in the model for the runoff.
for i in range( 1, len( rows ) ) : # Skip the header
row = rows[ i ]
words = row.split(',')
EVER_basin_num = int( words[ var_column_map['Source_Basin'] ] )
EVER_Basin = model.Basins[ EVER_basin_num ]
model.runoff_stage_basins[ EVER_Basin ] =\
words[ var_column_map['EDEN_Station'] ].strip()
model_basin_num = int( words[ var_column_map['Dest_Basin'] ] )
shoals = words[ var_column_map['Shoals'] ].strip('[] \n').split()
shoal_nums = list( map( int, shoals ) )
# Save list of Shoal objects in runoff_stage_shoals map
# { Basin Object : [ Shoal Objects ] }
model.runoff_stage_shoals[ model.Basins[ model_basin_num ] ] =\
[ model.Shoals[ shoal_num ] for shoal_num in shoal_nums ]
# Validate the Shoals : Basins
for Basin, Shoals in model.runoff_stage_shoals.items() :
for Shoal in Shoals :
if Basin is Shoal.Basin_A :
continue
elif Basin is Shoal.Basin_B :
continue
else :
errMsg = 'GetBasinRunoffStageData: Invalid Basin: ' +\
Basin.name + ' for shoal with A: ' +\
Shoal.Basin_A.name + ' B: ' + Shoal.Basin_B.name +\
' in runoff_stage_shoals map basinStageRunoffMap (-bS)'
raise ValueError( errMsg )
if model.args.DEBUG_ALL :
print( 'GetBasinRunoffStageData: runoff_stage_shoals:\n' )
print( model.runoff_stage_shoals )
# Load stage data into the runoff_stage_data dictionary
# The csv file has 9 columns, 1 = YYYY-MM-DD
# 2 - 9 = Daily EDEN stage in (m) offset to MSL anomaly:
# S22, S21, S20, S19, S18, S17, S16, S15
# first row is header
fd = open( model.args.path + model.args.basinStageRunoff, 'r' )
rows = fd.readlines()
fd.close()
# Create list of station names in the order of the header/columns
stations = []
words = rows[ 0 ].split(',')
for i in range( 1, len( words ) ) : # Skip the time column
stations.append( words[ i ].strip() )
# Create list of datetimes
dates = []
for i in range( 1, len( rows ) ) : # Skip the header
row = rows[ i ]
words = row.split(',')
dates.append( strptime( words[ 0 ], '%Y-%m-%d' ) )
# Find index in dates for start_time & end_time
start_i, end_i = GetTimeIndex( 'Runoff', dates,
model.start_time, model.end_time )
if model.args.DEBUG_ALL :
print( 'Runoff data start: ',str(dates[ start_i ]),str( start_i ),
' end: ', str(dates[ end_i ]),str( end_i ) )
print( rows[ start_i ] )
print( rows[ end_i ] )
# The runoff_stage_data is a nested dictionary intended to minimize
# dictionary key lookups to access basin stage for a
# specific year month day. The key is an integer 3-tuple of
# ( Year, Month, Day ), values are { station : stage }.
# Populate only data needed for the simulation timeframe
for i in range( start_i, end_i + 1 ) :
row = rows[ i+1 ]
words = row.split(',')
station_stage = dict()
for j in range( 1, len( words ) ) :
station_stage[ stations[ j-1 ] ] = float( words[ j ] )
date = dates[ i ]
key = ( date.year, date.month, date.day )
model.runoff_stage_data[ key ] = station_stage
if model.args.DEBUG_ALL :
print( model.runoff_stage_basins )
print( model.runoff_stage_data )
#----------------------------------------------------------------
#
#----------------------------------------------------------------
def GetBasinDynamicBCData( model ):
'''Read daily runoff flow or stage data (-db) from -bc file,
Populate dynamic_flow_boundary and/or dynamic_head_boundary dictionary'''
if model.args.DEBUG :
print( '\n-> GetBasinDynamicBCData', flush = True )
# Get mapping of model basins to BC timeseries
fd = open( model.args.path + model.args.basinBCFile, 'r' )
rows = fd.readlines()
fd.close()