-
Notifications
You must be signed in to change notification settings - Fork 4
/
globalnews.py
1479 lines (1193 loc) · 58.1 KB
/
globalnews.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
""" globalnews.py
Global NEWS 2 model, GNE implementation.
This module is the master model execution controller; it imports
gncfg, gncore, and the actual "model" code (dissolved.py, etc).
AVAILABILITY, USE RESTRICTIONS, AND CONTACT INFORMATION
-------------------------------------------------------
The Global NEWS 2 model ("NEWS 2") and the Global NEWS modeling Environment (GNE)
were developed by the Global NEWS group and are available at our web site:
http://www.marine.rutgers.edu/globalnews/
We encourage its use for research and educational (non-commercial) purposes, but
we request that active users contact us to inform about how it is being applied.
Such feedback and reporting will improve our continued development of the model code.
Global NEWS is a work group of UNESCO's Intergovernmental Oceanographic Commission (IOC).
Use of NEWS 2 should be ackwnowledged by citing Mayorga et al (in review);
Beusen et al. (2009) and Billen and Garnier (2007) should also be cited if
the DSi model and the ICEP index, respectively, are also used.
For questions and additional information, please contact:
Emilio Mayorga, Ph.D. [email protected]
Applied Physics Laboratory, University of Washington
Seattle, WA USA
---------------------------------------
RECENT MODIFICATION HISTORY
1/18/2010: Improved documentation and some run-time messages.
10/16-24/2008: Added functionality to allow an optional 'All-Forms' model
after the Global NEWS nutrient form models are completed. This new model
or calculation can use results from any nutrient form output. It is coded
in the python code file allformmodel.py. It's enabled through the new
optional argument -a (or --afm).
1/28/2008: Added actual usage text to usage(); cleaned up run-time messages a bit
and added date-time statements at every major code block in main() and runmodel().
9/17-18/2007
"""
import sys
import os.path
import getopt
from operator import itemgetter
import time
import numpy as ny
from numpy import log, exp, log10
# Import Global NEWS Environment ("gne") modules
# Generic gne code files ideally should be placed in the "gnecode"
# subfolder, but they may also be placed at the same base folder
__gne = os.path.join('.', 'gnecode')
if os.path.exists(__gne): sys.path.insert(0, __gne)
from gnecode.gncfg import *
import gnecode.gngis2tbls as gngis2tbls
__version__ = '$Revision: 2010-01-18$'
def localtimestrf(frmtopt="datetime"):
''' Return the local date-time as a formatted string.
frmtopt="datetime" (default) Return a complete date-time string.
frmtopt="timeonly" Return Hour:Minute:Seconds only.
'''
if frmtopt == "datetime":
return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
elif frmtopt == "timeonly":
return "Time: " + time.strftime("%H:%M:%S", time.localtime())
def usage():
''' Print out string describing model usage and optional arguments.
'''
print("\nRun-time arguments/options:")
print("-h OR --help Show this help text.")
print("-v Run in verbose mode (print out all internal messages).")
print("<none> Run Global NEWS model using vars.cfg config file.")
print("-a OR --afm Run Global NEWS 'All-Forms' model *after* all individual")
print(" nutrient forms have been calculated (with allformmodel.py).")
print("-g OR --gis Run GIS pre-processor using gis2tbls.cfg config file.")
print("-g OR --gis Run GIS pre-processor using gis2tbls.cfg config file.")
print("-p OR --postproc Run post-processing: create new loads variables")
print(" for selected inputs and outputs, all basins.")
def main():
global FLAGS
print("\n***** Global NEWS modeling Environment (GNE) *****")
print(" " + __version__)
print(" " + localtimestrf())
print(" ==========================================")
# Passing arguments at command-line
# opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
try:
# hmm, not sure what's fed to args if opts already gets the arg value
opts, args = getopt.getopt(sys.argv[1:], "agphv", ["afm", "gis", "postproc", "help"])
# extract list of just the "options" (arguments), without arg. values
opt = list(map(itemgetter(0), opts))
except getopt.GetoptError:
# print help information, then exit:
usage()
sys.exit(2)
if "-h" in opt or "--help" in opt:
usage()
sys.exit()
FLAGS['verbose'] = False
if "-v" in opt:
FLAGS['verbose'] = True
if "-g" in opt or "--gis" in opt:
rungis2tbls()
elif "-p" in opt or "--postproc" in opt:
runpostproc()
else:
if "-a" in opt or "--afm" in opt:
FLAGS['AllFormModel'] = True
else:
FLAGS['AllFormModel'] = False
runmodel()
#elif "-s" in opt or "--smry" in opt:
# runsummary() # does not exist yet!
print(" ==========================================")
print("***** GNE Run completed *****")
print(" %s\n" % localtimestrf())
def runmodel():
''' Execute a Global NEWS run using pre-processed basin inputs
as configured in vars.cfg.
'''
global IN, OUT, FLAGS
print("*** Running Global NEWS Model\n")
cfg_simple_sections("MODEL")
print("*** Done reading general model run configurations (cfg_simple_sections()) ***")
print(" %s" % localtimestrf("timeonly"))
PopulateCfgVars(fname_cfg_var, "MODEL")
print("*** Done reading variable configurations (PopulateCfgVars()) ***")
print(" %s" % localtimestrf("timeonly"))
load_var_arrays(INDOC_TBL, INDOC, IN, OUT)
print("*** Done loading input data into memory (load_var_arrays()) ***")
print(" %s" % localtimestrf("timeonly"))
print("\n*** Ready to run models ... ***")
# run only nutrient-form (parameter) subsets specified in vars.cfg, as appropriate
params_dissolved = [p for p in RUN['p'] if p in PGRP['dissolved']]
params_particulate = [p for p in RUN['p'] if p in PGRP['particulate']]
print(" Nutrient forms (parameters) requested:")
print(" " + ",".join(params_dissolved) + " " + ",".join(params_particulate))
if len(params_dissolved):
model_dissolved(params_dissolved)
print("*** Done with dissolved sub-models ***")
else:
print("*** No dissolved forms (parameters) requested")
print(" %s" % localtimestrf("timeonly"))
if len(params_particulate):
model_particulate(params_particulate)
print("*** Done with particulate sub-models ***")
else:
print("*** No particulate forms (parameters) requested")
print(" %s" % localtimestrf("timeonly"))
if FLAGS['AllFormModel']:
import allformmodel
allformmodel.model()
print("*** Done with 'All-Form' model ***")
else:
print("*** No 'All-Form' model requested")
print(" %s" % localtimestrf("timeonly"))
write_var_arrays(OUTDOC_TBL, OUTDOC, OUT)
print("*** Done writing output to files (write_var_arrays()) ***")
def rungis2tbls():
global IN, OUT
print("\n***Running Global NEWS Pre-Processor (GIS > Tables)\n")
# print the time, too?
cfg_simple_sections("GIS2TBLS")
print("*** Done with cfg_simple_sections ***\n")
PopulateCfgVars(fname_cfg_gis, "GIS2TBLS")
print("*** Done with PopulateCfgVars ***\n")
gngis2tbls.LoadBasinIDArea(INDOC_TBL, IN, OUT)
print("*** Done with LoadBasinIDArea ***\n")
# loop through gis input grids for all items in INDOC
#print "INDOC keys:", INDOC.keys(), " INDOC_TBL keys:", INDOC_TBL.keys()
gngis2tbls.CalcBasinStats(INDOC_TBL, INDOC)
print("*** Done with CalcBasinStats ***\n")
gngis2tbls.BasinStatsToOUTvarArrays(OUTDOC_TBL, OUTDOC, OUT, IN)
print("*** Done with BasinStatsToOUTvarArrays ***\n")
# INSERT HERE A NEW FUNCTION (SEPARATE FILE) TO FREELY DEFINE GENERATED
# OUT VARS THAT ARE NOT ZONALSTATS. WILL NEED TO MODIFY
# BasinStatsToOUTvarArrays TO NOT TRIP OVER THOSE NEW OUT VARS
# Maybe this new function should be called *only* if there are any
# "qualifying" OUT vars?? Do that test here, in the new function, or
# in BasinStatsToOUTvarArrays() and have it return a value?
excldvarset = set(list(INDOC.keys()) + ['BasinID', 'BasinArea'])
genoutset = set(OUTDOC.keys()).difference(excldvarset)
if len(genoutset):
import gis2tbls
gis2tbls.newoutvars()
print("*** Done with gis2tbls ***\n")
else:
print("*** No gis2tbls variables requested\n")
write_var_arrays(OUTDOC_TBL, OUTDOC, OUT)
print("*** Done with write_var_arrays ***\n")
def runpostproc():
global IN, OUT
print("\n***Running Post-processing \n")
# print the time, too?
cfg_simple_sections("MODEL")
print("*** Done with cfg_simple_sections ***\n")
PopulateCfgVars(fname_cfg_var, "MODEL")
print("*** Done with PopulateCfgVars ***\n")
load_var_arrays(INDOC_TBL, INDOC, IN, OUT)
print("*** Done with load_var_arrays ***\n")
# run only parameter subsets specified in vars.cfg (? is this applicable?)
print(" Parameters requested: ", RUN['p'])
if len(RUN['p']):
import postprocess
postprocess.processvars(RUN['p'])
print("*** Done with postprocess ***\n")
else:
print("*** No postprocess parameters requested\n")
write_var_arrays(OUTDOC_TBL, OUTDOC, OUT)
print("*** Done with write_var_arrays ***\n")
# NOW WRITE OUT A SEPARATE SUMMARY FILE (*_smry.csv) WITH
# GLOBAL AND MAYBE REGIONAL (CONTINENTAL, ETC) TOTALS???
def ParseStrBlock(delims, str_in, strblock_d):
if delims[0] in str_in:
bl0, blf = (str_in.index(delims[0]), str_in.index(delims[1]))
# 0 = enclosed block, b = beginning block, f = final block
# strblock_d['b']<strblock_d['0']>strblock_d['f']
# where < and > represent the chosen delimeters, "delims"
strblock_d['0'] = str_in[:bl0]
strblock_d['b'] = str_in[bl0+1:blf]
strblock_d['f'] = str_in[blf+1:]
return True
else:
return False
def read_cfgfile(fname_cfg, sect_prefix):
""" Read and parse configuration files
Return dictionary of parsed section/options
For section names [prefix.suffix]: cfg_dict[sect_suffix][option] = optionvalue
For section names [prefix]: cfg_dict[option] = optionvalue
"""
import os, os.path
import configparser
cfg = configparser.SafeConfigParser()
# Prevent default behavior of returning options in all-lowercase
cfg.optionxform = str
fpath_cfg = os.path.join(fpath_dev, fname_cfg)
cfg.read(fpath_cfg)
# is any error checking needed??
cfg_dict = {}
for section in cfg.sections():
if "." in section:
spre,ssuf = section.split(".")
if spre == sect_prefix:
cfg_dict[ssuf] = {}
for opt,val in cfg.items(section):
cfg_dict[ssuf][opt] = val
if FLAGS['verbose']: print(" %s[%s][%s] = %s" %(sect_prefix, ssuf, opt, cfg_dict[ssuf][opt]))
elif section == sect_prefix:
for opt,val in cfg.items(section):
cfg_dict[opt] = val
if FLAGS['verbose']: print(" %s[%s] = %s" %(section, opt, cfg_dict[opt]))
return cfg_dict
def parse_cfg_ccal(ccal_cfg_d, ccal_d):
""" Populate global CCAL dictionary
"""
for k1 in list(ccal_cfg_d.keys()):
ccal_d[k1] = {}
for k2,val in list(ccal_cfg_d[k1].items()):
ccal_d[k1][k2] = float(val)
def parse_cfg_modelrun(mrun_cfg_d, run_d):
""" Populate global RUN dictionary
"""
# Parse variable p
vallst = mrun_cfg_d['p'].split(',')
# note: run_d['p'] must be stored as list, not single value
# perform variable test & expansion based on PGRP
if len(vallst) == 1:
# entry could be a single parameter or a parameter group
if vallst[0] in PGRP['all']:
run_d['p'] = vallst
elif vallst[0] in list(PGRP.keys()):
run_d['p'] = list(PGRP[vallst[0]]) # convert tuple to list
else:
if FLAGS['verbose']: print("Generic var name: parameter sepecifier %s not valid" % (vallst[0]))
else:
run_d['p'] = vallst
# Parse variable param_ord
run_d['param_ord'] = []
vallst = mrun_cfg_d['param_ord'].split(',')
for idx, v in enumerate(vallst):
if PGRP['all'][idx] in run_d['p']:
# assign order index, converting from base 1 to base 0
run_d['param_ord'].append(int(v) - 1)
if FLAGS['verbose']: print(run_d)
def parse_cfg_basrea(basarea_cfg_d, basarea_d):
""" Populate global BASAREA dictionary
"""
# Parse variable BasinAreas, populate global dictionary
if basarea_cfg_d['BasinAreas'] == "NO":
basarea_d['BasinAreas'] = \
{'FLAG':False, 'outtable':None, 'varname':None}
else:
vallst = basarea_cfg_d['BasinAreas'].split('|')
basarea_d['BasinAreas'] = \
{'FLAG':True, 'outtable':vallst[0], 'varname':vallst[1]}
if FLAGS['verbose']: print("BASAREA: ", basarea_cfg_d['BasinAreas'])
def parse_cfg_tbls(tbls, strsubs, doctbl_d, doctype):
""" parse tables
This generic function can populate either INDOC_TBL or OUTDOC_TBL
INDOC_TBL and OUTDOC_TBL must be already initialized/declared (in gncfg.py)
doctbl_d: INDOC_TBL or OUTDOC_TBL
doctype: "IN" or "OUT"
"""
import os.path
vlist_len = {"INGIS":2, "IN":2, "OUT":2}
# what's the difference between .items() and .iteritems()?
for tblname,val in list(tbls.items()):
vlist = val.split("|")
# later, the basis for doctype == "OUT" might be made optional?
if len(vlist) != vlist_len[doctype]:
if FLAGS['verbose']: print("%s: Tables must have exactly %d arguments.\n" %(tblname, vlist_len[doctype]))
# Now exit.
# add empty tblname key to dictionary
doctbl_d[tblname] = {}
# Add filepath item
# one optional string substitution is allowed for now; parse it if present
# allowing multiple string substitutions later will be very useful
# first test for the existence of strblock in strsubs?
strblock = {}
if ParseStrBlock("()", vlist[0], strblock):
doctbl_d[tblname]['filepath'] = strsubs[strblock['b']] + strblock['f']
if FLAGS['verbose']: print(tblname,doctbl_d[tblname]['filepath'])
else:
doctbl_d[tblname]['filepath'] = vlist[0]
if doctype == "IN":
# test to see if the file exists
if not os.path.exists(doctbl_d[tblname]['filepath']):
if FLAGS['verbose']: print("Table does not exist: %s\n" %(doctbl_d[tblname]['filepath']))
# Now exit.
# Add basis item (check for valid values)
basis_valid = ('basin', 'nation', 'table', 'grid')
if vlist[1] in basis_valid:
doctbl_d[tblname]['basis'] = vlist[1]
else:
if FLAGS['verbose']: print("Table %s: basis '%s' is not recognized; must be (%s).\n" \
%(tblname, vlist[1], ','.join(basis_valid)))
# Now exit.
# OUT tables can only have a 'basin' basis; for now, just ignore entry in cfg
elif doctype == "OUT":
doctbl_d[tblname]['basis'] = 'basin'
# initialize list of variables to be used
# and their left-right field output order (for OUT)
doctbl_d[tblname]['varlst'] = []
doctbl_d[tblname]['fieldorder'] = []
def parse_cfg_vars(vars, doctbl_d, doc_d, doctype):
""" Parse variables, mapping them to columns (fields) in csv tables.
"""
vlist_len = {"IN":3, "OUT":4, "INGIS":3}
dattype = {'IN':'tbl', 'OUT':'tbl', 'INGIS':'gis'}
for varname,val in list(vars.items()):
vlist = val.split("|")
if len(vlist) != vlist_len[doctype]:
# for doctype "OUT", flgwrite 'Y' is assumed if it's ommitted
if doctype == "OUT" and len(vlist) == vlist_len[doctype] - 1:
vlist.append('Y')
else:
if FLAGS['verbose']: print("%s: Variable must have exactly %d arguments.\n" \
%(varname, vlist_len[doctype]))
# Now exit.
strblock = {}
if doctype == "OUT":
# if doctype == "OUT", try to parse generic variables
if ParseStrBlock("<>", varname, strblock):
vargen = strblock['b'].split('|')
# "p": variable name used for "parameter" in .cfg file
if vargen[0] == 'p':
varname0, varnamef = (strblock['0'], strblock['f'])
params_in = vargen[1].split(',')
# perform variable test & expansion based on PGRP
if len(params_in) == 1:
# entry could be a single paramer or a parameter group
# ignore case mismatch?? (eg, DOC vs doc)
if params_in[0] in PGRP['all']:
# it's already a simple parameter name, so use as-is
params = params_in
elif params_in[0] in list(PGRP.keys()):
# extract parameters from param group, convert to list
params = list(PGRP[params_in[0]])
else:
print("Generic var name: parameter sepecifier %s not valid" % (params_in[0]))
else:
# should do error check, to ensure parameters are valid
params = params_in
else:
print("Error with generic var name!")
if FLAGS['verbose']: print(strblock['b'],params, "\n")
# Parse and expand fieldname, if it, too, is generic
# A generic varname doesn't require a corresponding generic fieldname
ParseStrBlock("<>", vlist[1], strblock)
if strblock['b'] == 'p':
IsGenericFldName = True
fldname0, fldnamef = (strblock['0'], strblock['f'])
else:
IsGenericFldName = False
if FLAGS['verbose']:
print(">WARNING: For generic var name, field name is not generic")
# limit params list by actual model run parameters in RUN['p']
params = [p for p in PGRP['all'] if p in params and p in RUN['p']]
# add expanded variables to doc_d based on the vargen block
for p in params:
# if doctype == "OUT", more than one srctable is allowed.
varname_p = varname0 + p + varnamef
if IsGenericFldName:
fldname_p = fldname0 + p + fldnamef
else:
fldname_p = vlist[1]
doctbld_docd_assign(dattype[doctype], doctbl_d, doc_d, \
varname_p, vlist[0], fldname_p, \
vlist[2], vlist[3], varname, p)
else:
doctbld_docd_assign(dattype[doctype], doctbl_d, doc_d, varname, \
vlist[0], vlist[1], vlist[2], vlist[3])
else:
# if doctype == "IN", parse specific vars only; just one srctable is allowed.
doctbld_docd_assign(dattype[doctype], doctbl_d, doc_d, varname, \
vlist[0], vlist[1], vlist[2])
# if doctype == "OUT", create dummy, 0-value, varlst-length list for fieldorder key
if doctype == "OUT":
for tblname in doctbl_d:
varlst0 = [0 for i in range(len(doctbl_d[tblname]['varlst']))]
doctbl_d[tblname]['fieldorder'] = varlst0
if FLAGS['verbose']:
for tblname in sorted(doctbl_d):
print(tblname, doctbl_d[tblname]['varlst'])
def doctbld_docd_assign(dattype, doctbld, docd, varname, srctable, fldname, \
fldtype, flgwrite='N', genvar=None, genvarparam=None):
docd[varname] = {}
docd[varname]['genvarname'] = genvar
docd[varname]['genvar_p'] = genvarparam
# split works fine even if there's just one srctable
docd[varname]['srctable'] = srctable.split(',')
docd[varname]['fieldname'] = fldname
docd[varname]['flgwrite'] = {'Y':True, 'N':False}[flgwrite]
if dattype == "tbl":
if fldtype in ('double', 'int'):
docd[varname]['fieldtype'] = fldtype
else:
print("Variable %s: fieldtype '%s' is not recognized; must be 'double' or 'int'.\n" %(varname, fldtype))
# Now exit.
elif dattype == "gis": # here, fldtype is really the cell fraction grid, if any
if fldtype == 'all' or fldtype.startswith('clfr'):
docd[varname]['fieldtype'] = fldtype
else:
print("Variable %s: fieldtype '%s' is not recognized; must be 'all' or 'clfr*'.\n" %(varname, fldtype))
# Now exit.
# add variable to corresponding variable list for table(s)
for tblname in docd[varname]['srctable']:
doctbld[tblname]['varlst'].append(varname)
if FLAGS['verbose']: print("docd assignments: ",varname,docd[varname]['genvarname'], \
docd[varname]['srctable'],docd[varname]['fieldname'],docd[varname]['fieldtype'])
def load_var_arrays(doctbl_d, doc_d, data_d, out_data_d):
""" Data array will be populated into data_d[varname]
Open each table, load each requested fields and map them to variables.
Variables will be numpy arrays.
"""
import csv
# =====================================================
# LOAD AND HANDLE BasinID VARIABLE
# Basin sub-setting functionality is included.
id_var = 'BasinID'
id_fldname, id_tblname = doc_d[id_var]['fieldname'], doc_d[id_var]['srctable'][0]
# read csv table and load BasinID variable
fp = open(doctbl_d[id_tblname]['filepath'], "r")
basinid_lst = [int(row[id_fldname]) for row in csv.DictReader(fp)]
fp.close()
# Convert to int32 numpy array and load into IN
data_d[id_var] = ny.array(basinid_lst, dtype="int32")
# Create set out of global BasinID's array
# For use in basin sub-setting as input variables/tables are read in (below)
setBasinID = set(data_d[id_var])
# Copy to OUT[id_var] numpy array
out_data_d[id_var] = data_d[id_var]
if FLAGS['verbose']: print(" Number of basins to be processed: %d" % data_d[id_var].size)
# =====================================================
# LOAD ALL OTHER INPUT VARIABLES, STEPPING THROUGH INDIVIDUAL INPUT TABLES
for tbl in list(doctbl_d.keys()):
if len(doctbl_d[tbl]['varlst']) > 0:
# Initialize data_d dict with all its keys (variables), as numpy arrays
# (maybe this data_d initialization should be done in parse_cfg_vars()?
for var in doctbl_d[tbl]['varlst']:
if var != id_var:
if doc_d[var]['fieldtype'] == "double":
data_d[var] = ny.array([], dtype="float64")
#data_d[var] = ny.array([], dtype="float32")
elif doc_d[var]['fieldtype'] == "int":
data_d[var] = ny.array([], dtype="int32")
# create temporary dict {fieldname:varname} for current table (tbl),
# based on doctbl_d[tbl]['varlst'] and doc_d[varname]['fieldname']
# Then map fieldname to varname, and use in the block below
tbl_fld_var = {}
for var in doctbl_d[tbl]['varlst']:
if var != id_var:
tbl_fld_var[doc_d[var]['fieldname']] = var
if FLAGS['verbose']: print("tbl_fld_var: ", tbl_fld_var)
# Read the first row (field names)
fp = open(doctbl_d[tbl]['filepath'], "r")
fld_names_reader = csv.reader(fp)
fld_names = next(fld_names_reader)
fp.close()
# Set basinid_fld using the header (field names) reader list
# id_fldnames_lst is a list of expected basin id field names
id_fldnames_lst = ['basinid', 'BASINID']
if id_fldnames_lst[0] in fld_names:
basinid_fld = id_fldnames_lst[0]
elif id_fldnames_lst[1] in fld_names:
basinid_fld = id_fldnames_lst[1]
else:
print(" !!! Input table does not have a valid basin ID column name !!!\n")
# read csv table and load requested fields
fp = open(doctbl_d[tbl]['filepath'], "r")
csvreader = csv.DictReader(fp)
# SHOULD THERE BE A TEST TO ENSURE THAT ALL REQUESTED FIELDS
# ARE PRESENT IN THE FILE? OTHERWISE, ABORT WITH ERROR MESSAGE
for row in csvreader:
for fld,v in list(row.items()):
if int(row[basinid_fld]) in setBasinID and fld in list(tbl_fld_var.keys()):
# load requested fields into the data_d dict by
# concatenating each row value to the numpy arrays
# This test is done for every single row! It may be more
# efficient to extract indices for the requested fields, then
# just use those indices directly into the row dictionary.
# And maybe use simple reader and not DictRead, for efficiency.
var = tbl_fld_var[fld]
if var != id_var:
if doc_d[var]['fieldtype'] == "double":
data_d[var] = ny.r_[data_d[var], float(v)]
elif doc_d[var]['fieldtype'] == "int":
data_d[var] = ny.r_[data_d[var], int(v)]
fp.close()
if FLAGS['verbose']:
for var in doctbl_d[tbl]['varlst']:
print(" ", var, "(", data_d[var].size, "):", data_d[var])
def write_var_arrays(doctbl_d, doc_d, data_d):
"""
To write out variables to csv tables, use the compact
writerows() csv writer method. One way to prepare the output numpy arrays
for writing is to use zip(). eg, for 3 numpy arrays:
a = ny.array([1,2])
b = ny.array([10,20])
c = ny.array([5,7])
zip(a,b,c)
[(1, 10, 5), (2, 20, 7)]
fp = open("myfile.csv", "wb") # without the 'b', blank lines may be inserted
writer = csv.writer(fp)
writer.writerow(["fld1","fld2","fld3"])
writer.writerows(zip(a,b,c))
Getting the field order just right is a big deal.
"""
import csv
for tbl in doctbl_d:
if len(doctbl_d[tbl]['varlst']) > 0:
fldnameseq = []
writeseq = []
# remove vars that were never written out to the OUT dict
# either by choice or by oversight. Alternatively, write out None?
orderedflds = dict(list(zip(doctbl_d[tbl]['fieldorder'], doctbl_d[tbl]['varlst'])))
for ord,var in sorted(orderedflds.items()):
if var in data_d and \
var in doc_d and doc_d[var]['flgwrite']:
fldnameseq.append(doc_d[var]['fieldname'])
writeseq.append("data_d['" + var + "']")
# write requested fields to csv table
fp = open(doctbl_d[tbl]['filepath'], "w", newline='')
csvwriter = csv.writer(fp)
csvwriter.writerow(fldnameseq)
csvwriter.writerows(eval("zip(" + ",".join(writeseq) + ")"))
fp.close()
def cfg_outvars_order(fname_cfg_var, doctbl_d, doc_d):
"""
Re-read vars.cfg\[OUT.VARS] using raw file access to extract the order of
each variable (ConfigParser doesn't return the order).
1. Open cfg file and go to [OUT.VARS] line
2. Read through all lines in that section (until EOF or start of next [] section)
3. Throw away comment and blank lines (lines with nothing but white space)
4. Step through var lines.
5. For explicit vars, increment field order index
6. For generic vars, expand into multiple vars, assign field order index based
on previous var field order and parameter relative indices
(build generic-var handling functionality latere, after developing the
simpler field order extraction tools for explicit vars)
7. Match each var name to a key in OUTDOC dict
8. Update OUTDOC_TBL and OUTDOC dicts with the field order
"""
# create a dictionary of genvarname vs. var names list from doc_d
genvars_d = {}
for var in doc_d:
gv = doc_d[var]['genvarname']
if gv:
gvpar = doc_d[var]['genvar_p']
if gv in genvars_d:
genvars_d[gv]['p'].append(gvpar)
genvars_d[gv]['var'].append(var)
else:
genvars_d[gv] = {}
genvars_d[gv]['p'] = [gvpar]
genvars_d[gv]['var'] = [var]
# sort lists in the correct relative order using RUN['p'] & RUN['param_ord']
for gv in genvars_d:
if FLAGS['verbose']: print("genvars: ", gv, genvars_d[gv]['p'], genvars_d[gv]['var'])
genvars_d[gv]['p'].sort(key=lambda p: RUN['param_ord'][RUN['p'].index(p)])
genvars_d[gv]['var'].sort(key=lambda v: \
RUN['param_ord'][RUN['p'].index(doc_d[v]['genvar_p'])])
if FLAGS['verbose']: print("genvars sorted: ", gv, genvars_d[gv]['p'], genvars_d[gv]['var'])
# open file, suck in all lines, close file
f = open(fname_cfg_var, "r")
cfg_lines = f.readlines()
f.close()
# remove leading/trailing whitespace and \n, remove blank and comment lines
# Then go to sect_name line and extract only lines below it
cfg_lines = [line.strip() for line in cfg_lines]
cfg_lines = [ln for ln in cfg_lines if ln != '' and not ln.startswith("#")]
sect_name = "[OUT.VARS]"
sect_idx = cfg_lines.index(sect_name)
sect_lines = cfg_lines[sect_idx+1:]
# maybe it's best to first search for EOF or the next section header?
# though *initially*, I can safely assume this section is the last one!
# assign field order for each var/tbl
# use enumerate to extract var index (order) concurrently
# extract varname from each var line (stripping leading/trailing whitespace)
# THIS SCHEME PROBABLY BREAKS DOWN WITH MORE COMPLEX SETS OF TABLES & VARS!
# ord_offset WILL PROBABLY GET OUT OF WHACK. EVALUATE AND FIX IT.
# or maybe it's ok, just that the assigned fieldorder values are non-consecutive?
ord_offset = 0
for ordidx,line in enumerate(sect_lines):
var = line.split("=")[0].strip()
if var in genvars_d:
# expand generic var names
for varexp in genvars_d[var]['var']:
for tblname in doc_d[varexp]['srctable']:
varidx = doctbl_d[tblname]['varlst'].index(varexp)
doctbl_d[tblname]['fieldorder'][varidx] = ordidx + ord_offset
ord_offset += 1
elif var in doc_d:
for tblname in doc_d[var]['srctable']:
varidx = doctbl_d[tblname]['varlst'].index(var)
doctbl_d[tblname]['fieldorder'][varidx] = ordidx + ord_offset
else:
# Generic variable name was not expanded because it's not being used.
# The RUN parameters requested don't match the var parameter block.
# Decrease order offset to maintain a proper sequental order index
ord_offset -= 1
def ExportVar(varsave, varname, param=None):
''' Export variable (array) to the gloabl OUT dictionary, to make it
available for writing out to files. If variable is actually a literal
(single value) and not a full-length numpy array, expand it to full-length
float32 numpy array using IN['BasinID'] to extract the length.
If param is not None, the variable has a generic name and must be
expanded.
'''
global OUT
# expand to full-length array if variable is a literal or has length == 1
# Better to use numpy tools? fill() after ny.empty(), or repeat()
# [BUT ONLY IF IN['BasinID'].size > 1 AND dtype IS NOT A STRING (IE, NOT srcMax
# ARRAYS)!!! IF IN['BasinID'].size == 1, VARIABLE STILL NEEDS TO BE CONVERTED
# TO NUMPY ARRAY IF IT'S A SCALAR!]
ExpandToArrayFlg = False
if ny.isscalar(varsave) or len(varsave) == 1:
# Expand only if scalar or array is not a numpy string-type
# (There are no foreseeable cases of string scalars that would be exported)
# But must first test if variable is a numpy array!
if isinstance(varsave, ny.ndarray):
if 'string' not in varsave.dtype.name:
ExpandToArrayFlg = True
else:
ExpandToArrayFlg = True
if ExpandToArrayFlg:
varsave = ny.array(varsave, dtype='float64') \
* ny.ones(IN['BasinID'].size)
#varsave = ny.array(varsave, dtype='float32') \
if param == None:
OUT[varname] = varsave
else:
# generic var name
OUT[param + varname] = varsave
# The handling of generic var names will only work if the parameter
# is the first element of the var name!! That's very limiting and
# will need to be generalized
def cfg_simple_sections(steptype):
global CCAL, RUN, BASAREA
if steptype == "MODEL":
sect_dict = read_cfgfile(fname_cfg_cal, "CAL")
parse_cfg_ccal(sect_dict, CCAL)
sect_dict = read_cfgfile(fname_cfg_var, "MODELRUN")
parse_cfg_modelrun(sect_dict, RUN)
elif steptype == "GIS2TBLS":
sect_dict = read_cfgfile(fname_cfg_gis, "CREATEBASAREAS")
parse_cfg_basrea(sect_dict, BASAREA)
def PopulateCfgVars(fname_cfg, steptype):
""" Parse config file(s), load data into vars
steptype: "GIS2TBLS" or "MODEL"
"""
global INDOC_TBL, INDOC, IN, OUTDOC_TBL, OUTDOC, OUT
strsubs = read_cfgfile(fname_cfg_gen, "STRSUBS")
# IN sections and dictionaries
sel = {'MODEL': ('IN','TBLS','VARS'), \
'GIS2TBLS': ('INGIS','FILES','GISVARS')}
in_dict = read_cfgfile(fname_cfg, "IN")
parse_cfg_tbls(in_dict[sel[steptype][1]], strsubs, INDOC_TBL, sel[steptype][0])
parse_cfg_vars(in_dict[sel[steptype][2]], INDOC_TBL, INDOC, sel[steptype][0])
# OUT sections and dictionaries
out_dict = read_cfgfile(fname_cfg, "OUT")
parse_cfg_tbls(out_dict['TBLS'], strsubs, OUTDOC_TBL, "OUT")
parse_cfg_vars(out_dict['VARS'], OUTDOC_TBL, OUTDOC, "OUT")
cfg_outvars_order(fname_cfg, OUTDOC_TBL, OUTDOC)
# ---- copied from dissolved.py -----
R_MIN = 0.003 # 3 mm/yr
def model_dissolved(run_forms):
""" Dissolved nutrient form sub-models.
"""
# nutrient form list is limited to dissolved and actual
# model run forms ('parameters') in RUN['F']
for F in run_forms:
# DSi is implemented as a separate, isolated function.
if F == 'DIC':
Yld_F = DICModel() # Currently an empty function.
elif F == 'DSi':
Yld_F = DSiModel()
else:
# POINT (SEWAGE) SOURCES
RSpnt_E, FEpnt_F = Calc_PointSources(F)
RSpnt_F = FEpnt_F * RSpnt_E
# DIFFUSE (NON-POINT) SOURCES
# Calculate FEws_F, FEws_nat,F, f_F(Rnat)
FEws_F, FEws_nat_F, f_F = Calc_WSExportFunc(F)
# Diffuse River Sources: explicit
WSdif_ant_E, WSdif_nat_E = Calc_WSdifExplicit(F)
RSdif_expl_ant_F = FEws_F * WSdif_ant_E
RSdif_expl_nat_F = FEws_nat_F * WSdif_nat_E
# Diffuse River Sources: export coefficient
RSdif_ec_F = Calc_RSdifExportCoeff(f_F, F)
if F == 'DOC':
RSdif_ec_ant_F = 0.0
RSdif_ec_nat_F = RSdif_ec_F
else:
Agfr = IN['agric'] / 100.0
RSdif_ec_ant_F = Agfr * RSdif_ec_F
RSdif_ec_nat_F = (1 - Agfr) * RSdif_ec_F
# Total diffuse River Sources: anthropogenic vs. natural
RSdif_ant_F = RSdif_expl_ant_F + RSdif_ec_ant_F
RSdif_nat_F = RSdif_expl_nat_F + RSdif_ec_nat_F
# DISSOLVED YIELD (kg/km2/yr)
FEriv_F = Calc_FEriv(F)
#RS_ant_F = RSpnt_F + RSdif_ant_F
#RSdif_F = RSdif_ant_F + RSdif_nat_F
Yld_F = FEriv_F * (RSpnt_F + (RSdif_ant_F + RSdif_nat_F))
# SAVE VARIABLES FOR (OPTIONAL) EXPORT TO FILE
# Some are saved into global OUT dictionary mainly for reuse in SourceContrib()
ExportVar(RSdif_ec_ant_F, 'RSdif_ec_ant', F)
ExportVar(RSdif_ec_nat_F, 'RSdif_ec_nat', F)
ExportVar(RSdif_ant_F, 'RSdif_ant', F)
ExportVar(RSdif_nat_F, 'RSdif_nat', F)
ExportVar(RSpnt_F, 'RSpnt', F)
# CALCULATE SOURCE CONTRIBUTIONS
SourceContrib(F)
# DISSOLVED LOAD (ton/yr, or Mg/yr)
Ld_F = Yld_F * IN['A'] / 1000.0
# SAVE VARIABLES FOR (OPTIONAL) EXPORT TO FILE
ExportVar(Yld_F, 'Yld', F)
ExportVar(Ld_F, 'Ld', F)
def DSiModel():
""" DSi model. A multiple linear regression model, from Beusen et al. (2009)
"""
# Runoff must be in meters/yr, but current input is in mm/yr
Rnat = IN['Rnat'] / 1000.0
# Truncate (clip) input (independent) variables to the acceptable range
# before using in T_DSiyldNAT equation
IndepVarsTruncate = lambda var,minval,maxval: IN[var].clip(min=minval,max=maxval)
Precip = IndepVarsTruncate('precip',0.519952,10.9972)
GaezSlope = IndepVarsTruncate('gslope',1.95543000000000,41.9841000000000)
BulkDens = IndepVarsTruncate('bulkdens',0.93677500000000,1.6459700000000)
VolcLithFr = IndepVarsTruncate('frvolclith',0.00000000000000,0.7621180000000)
# CALCULATE BOX-COX TRANSFORMED ("T_") YIELD (ton SiO2/km2/yr), pre-dam
T_Yld_DSi_pred = 1.60765821573*log(Precip) + 0.03099949128*GaezSlope + \
-2.59587868714*BulkDens + 1.69157777610*VolcLithFr + 2.56118766111
# CALCULATE UN-TRANSFORMED YIELD (ton SiO2/km2/yr)
boxcox_lambda = 0.068623051867468
Yld_DSi_pred = (boxcox_lambda*T_Yld_DSi_pred + 1) ** (1/boxcox_lambda)
# Set Yld_DSi_pred to 0 in arid basins (Rnat < R_MIN)
Yld_DSi_pred[Rnat < R_MIN] = 0.0
# Set yield to 0 if bulk density is ~ 0
Yld_DSi_pred[IN['bulkdens'] < 0.01] = 0.0
# Unit conversion of yield, to kg Si/km2/yr
Yld_DSi_pred *= 1000*(28.09/60.09)
# Apply reservoir retention term (currently from TSS only)
D_TSS = IN['D_TSS']
Yld_DSi = (1 - D_TSS) * Yld_DSi_pred
return Yld_DSi
def DICModel():
""" DIC model. Function currently empty; DIC model development is being
done elsewhere.
"""
print("Blank DIC model function. DIC model development is being done elsewhere.")
return 0
def Calc_PointSources(F):
""" Calculate point source/sewage variables.
"""
ccalf = CCAL[F]
# element (N, P, C)
E = F[-1]
# FEpnt_F
if F == 'DIN':
# IN['hwfrem_N_aspct'] is in %, not 0-1
hwfrem_E = IN['hwfrem_N_aspct'] / 100.0
hwfrem_E_max = 0.8
FEpnt_F = 0.485 + 0.255*(hwfrem_E/hwfrem_E_max)
else:
FEpnt_F = ccalf['c']
# RSpntExc_E
if E in ('N', 'P'):
RSpntExc_E = IN['RSpntExc_'+E]
else:
RSpntExc_E = 0.0
# RSpntDet_E
if E == 'P':
RSpntDet_E = IN['RSpntDet_P']
else:
RSpntDet_E = 0.0
# RSpnt_E (total)
RSpnt_E = RSpntExc_E + RSpntDet_E
# SAVE VARIABLES FOR (OPTIONAL) EXPORT TO FILE
# Some are saved into global OUT dictionary mainly for reuse in SourceContrib()
ExportVar(RSpntExc_E, 'RSpntExc_'+E, F)
ExportVar(RSpntDet_E, 'RSpntDet_'+E, F)
ExportVar(RSpnt_E, 'RSpnt_'+E, F)