-
Notifications
You must be signed in to change notification settings - Fork 10
/
Collect_Summary_Products.py
2696 lines (2441 loc) · 99.1 KB
/
Collect_Summary_Products.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
# -------------------------------------------------------------------------------
# Name: Collect Summary Products
# Purpose: Collects any *.ai, *.png, or *.pdf files and automatically copies them
# into the proper structure
#
# Author: Braden Anderson
#
# Created: 12/2018
# -------------------------------------------------------------------------------
import shutil
from SupportingFunctions import make_folder
from numpy import median, mean
import os
import xlsxwriter
import arcpy
def main(project_folder, stream_network, watershed_name, excel_file_name=None, dams_shapefile=None, output_folder=None):
"""
The main function that creates and populates the excel file
:param project_folder: The BRAT Project Folder
:param stream_network: The BRAT input network to be summarized
:param watershed_name: The name of the watershed to be summarized
:param excel_file_name: The name of the excel doc
:param dams_shapefile: Shapefile containing dams for the entire area
:param output_folder: Folder where the excel doc will be output
:return:
"""
if excel_file_name is None:
excel_file_name = "BRAT_Summary_Tables"
if not excel_file_name.endswith(".xlsx"):
excel_file_name += ".xlsx"
stream_network = stream_network.replace("'", "")
if dams_shapefile is not None:
dams_shapefile = dams_shapefile.replace("'", "")
summary_prods_folder = os.path.join(project_folder, "SummaryProducts")
if not os.path.exists(summary_prods_folder):
summary_prods_folder = make_folder(project_folder, "SummaryProducts")
if output_folder is None:
output_folder = make_folder(summary_prods_folder, "SummaryTables")
create_folder_structure(project_folder, summary_prods_folder)
if stream_network.count(';') > 0:
stream_network = merge_networks(summary_prods_folder, stream_network)
if dams_shapefile is not None:
if dams_shapefile.count(';') > 0:
dams_shapefile = merge_dams(summary_prods_folder, dams_shapefile)
fields = [f.name for f in arcpy.ListFields(stream_network)]
create_excel_file(excel_file_name, stream_network, output_folder, watershed_name, fields, dams_shapefile)
def split_multi_inputs(multi_input_parameter):
"""
Splits an ArcMap Toolbox Multi-Value parameter into a Python list object.
:param multi_input_parameter: ArcMap Multi-Value inputs are semi-colon delimited text strings.
:return: The split list
"""
try:
# Remove single quotes
multi_input_parameter = multi_input_parameter.replace("'", "")
# split input tables by semicolon ";"
return multi_input_parameter.split(";")
# TODO There should be no bare exceptions
except:
raise Exception("Could not split multi-input")
def merge_networks(summary_prods_folder, stream_network):
"""
Merges all network inputs into one shapefile
:param summary_prods_folder: The folder where all summary products are output
:param stream_network: The BRAT input network to be summarized
:return: File with all networks merged.
"""
merged_file = os.path.join(summary_prods_folder, "MergedNetwork.shp")
to_merge = split_multi_inputs(stream_network)
arcpy.CreateFeatureclass_management(summary_prods_folder, "MergedNetwork.shp", None, to_merge[0])
arcpy.Append_management(to_merge, merged_file, "NO_TEST")
return merged_file
def merge_dams(summary_prods_folder, dams_shapefiles):
"""
Merges all dam inputs into one shapefile
:param summary_prods_folder: The folder where all summary products are output
:param dams_shapefiles: All dams shapefiles
:return: File with all dams merged
"""
merged_file = os.path.join(summary_prods_folder, "MergedDams.shp")
to_merge = split_multi_inputs(dams_shapefiles)
arcpy.CreateFeatureclass_management(summary_prods_folder, "MergedDams.shp", None, to_merge[0])
arcpy.Append_management(to_merge, merged_file, "NO_TEST")
return merged_file
def create_excel_file(excel_file_name, stream_network, summary_prods_folder, watershed_name, fields, dams_shapefile):
"""
Creates the excel file to be populate.
:param excel_file_name: The name of the excel doc
:param stream_network: The BRAT input network to be summarized
:param summary_prods_folder: The folder where all summary products are output
:param watershed_name: The name of the watershed to be summarized
:param fields: A list of every field present in the network shapefile
:param dams_shapefile: Shapefile containing dams for the entire area
:return:
"""
workbook = xlsxwriter.Workbook(os.path.join(summary_prods_folder, excel_file_name))
write_excel_file(workbook, stream_network, watershed_name, fields, dams_shapefile)
workbook.close()
def write_excel_file(workbook, stream_network, watershed_name, fields, dams_shapefile):
"""
Writes the entire Excel file
:param workbook: The current Excel Workbook
:param stream_network: The BRAT input network to be summarized
:param watershed_name: The name of the watershed to be summarized
:param fields: A list of every field present in the network shapefile
:param dams_shapefile: Shapefile containing dams for the entire area
:return:
"""
summary_worksheet = workbook.add_worksheet("Watershed Summary")
write_summary_worksheet(summary_worksheet, stream_network, watershed_name, workbook, fields, dams_shapefile)
if 'DamStrat' in fields:
strategy_map_worksheet = workbook.add_worksheet("TNC Strategy Map")
write_strategy_map_worksheet(strategy_map_worksheet, stream_network, watershed_name, workbook)
if 'oCC_EX' in fields:
density_correlations_worksheet = workbook.add_worksheet("Density Correlations")
write_density_correlations_worksheet(density_correlations_worksheet, stream_network, watershed_name, workbook)
exist_build_cap_worksheet = workbook.add_worksheet("Existing Dam Building Capacity")
write_exist_build_cap_worksheet(exist_build_cap_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Existing dam building capacity worksheet could not be built because oCC_EX not in fields")
if 'mCC_EX_CT' in fields:
exist_complex_worksheet = workbook.add_worksheet("Existing Dam Complex Size")
write_exist_complex_worksheet(exist_complex_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Existing dam complex size worksheet could not be built because mCC_EX_CT not in fields")
if 'oCC_HPE' in fields:
hist_build_cap_worksheet = workbook.add_worksheet("Historic Dam Building Capacity")
write_hist_build_cap_worksheet(hist_build_cap_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Historic dam builiding capacity worksheet could not be built because oCC_HPE not in fields")
if 'mCC_HPE_CT' in fields:
hist_complex_worksheet = workbook.add_worksheet("Historic Dam Complex Size")
write_hist_complex_worksheet(hist_complex_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Existing dam complex size worksheet could not be built because mCC_HPE_CT not in fields")
if 'mCC_HPE_CT' in fields and 'mCC_EX_CT' in fields:
hist_vs_exist_worksheet = workbook.add_worksheet("Existing vs. Historic Capacity")
write_hist_vs_exist_worksheet(hist_vs_exist_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning(
"Existing vs. Historic worksheet could not be built because mCC_EX_CT or mCC_HPE_CT not in fields")
if 'oPBRC_CR' in fields:
cons_rest_worksheet = workbook.add_worksheet("Conservation Restoration")
write_conservation_restoration(cons_rest_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Conservation restoration worksheet could not be built because oPBRC_CR not in fields")
if 'oPBRC_UD' in fields:
unsuitable_worksheet = workbook.add_worksheet("Unsuitable or Limited")
write_unsuitable_worksheet(unsuitable_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning(
"Unsuitable/limited dam opportunities worksheet could not be built because oPBRC_UD not in fields")
if 'oPBRC_UI' in fields:
risk_worksheet = workbook.add_worksheet("Undesirable Dams")
write_risk_worksheet(risk_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Risk worksheet could not be built because oPBRC_UI not in fields")
if 'ConsVRest' in fields:
strategies_worksheet = workbook.add_worksheet("Management Strategies")
write_strategies_worksheet(strategies_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Strategies worksheet could not be built because ConsVRest not in fields")
if 'mCC_EXvHPE' in fields:
historic_remaining_worksheet = workbook.add_worksheet("% Historic Capacity Remaining")
write_historic_remaining_worksheet(historic_remaining_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("% Historic Capacity Remaining")
if 'BRATvSurv' in fields:
validation_worksheet = workbook.add_worksheet("Predicted vs. Surveyed")
write_validation_worksheet(validation_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Predicted vs. surveyed worksheet could not be built because BRATvSurv not in fields")
if 'e_DamCt' in fields:
electivity_worksheet = workbook.add_worksheet("Electivity Index")
write_electivity_worksheet(electivity_worksheet, stream_network, watershed_name, workbook)
else:
arcpy.AddWarning("Electivity index worksheet could not be built because e_DamCt not in fields")
def make_capacity_table(output_network, mcc_hpe):
"""
Creates a table containing all capacity information [Currently unused]
:param output_network: The network being summarized
:param mcc_hpe: The mcc_hpe field
:return:
"""
brat_table = arcpy.da.TableToNumPyArray(output_network,
['iGeo_Len', 'mCC_EX_CT', 'oCC_EX', 'ExCategor', 'oCC_HPE', 'mCC_HPE_CT',
'HpeCategor'], skip_nulls=True)
tot_length = brat_table['iGeo_Len'].sum()
total_ex_capacity = brat_table['mCC_EX_CT'].sum()
total_hpe_capacity = brat_table[mcc_hpe].sum()
capacity_table = []
ex_pervasive = add_capacity_category(brat_table, 'Existing', 'Pervasive', tot_length)
# ex_frequent_pervasive = add_capacity_category(brat_table, 'Existing', 'Frequent-Pervasive', tot_length)
ex_frequent = add_capacity_category(brat_table, 'Existing', 'Frequent', tot_length)
# ex_occasional_frequent = add_capacity_category(brat_table, 'Existing', 'Occasional-Frequent', tot_length)
ex_occasional = add_capacity_category(brat_table, 'Existing', 'Occasional', tot_length)
# ex_rare_occasional = add_capacity_category(brat_table, 'Existing', 'Rare-Occasional', tot_length)
ex_rare = add_capacity_category(brat_table, 'Existing', 'Rare', tot_length)
# ex_none_rare = add_capacity_category(brat_table, 'Existing', 'None-Rare', tot_length)
ex_none = add_capacity_category(brat_table, 'Existing', 'None', tot_length)
hist_pervasive = add_capacity_category(brat_table, 'Historic', 'Pervasive', tot_length)
# hist_frequent_pervasive = add_capacity_category(brat_table, 'Historic', 'Frequent-Pervasive', tot_length)
hist_frequent = add_capacity_category(brat_table, 'Historic', 'Frequent', tot_length)
# hist_occasional_frequent = add_capacity_category(brat_table, 'Historic', 'Occasional-Frequent', tot_length)
hist_occasional = add_capacity_category(brat_table, 'Historic', 'Occasional', tot_length)
# hist_rare_occasional = add_capacity_category(brat_table, 'Historic', 'Rare-Occasional', tot_length)
hist_rare = add_capacity_category(brat_table, 'Historic', 'Rare', tot_length)
# hist_none_rare = add_capacity_category(brat_table, 'Historic', 'None-Rare', tot_length)
hist_none = add_capacity_category(brat_table, 'Historic', 'None', tot_length)
def add_capacity_category(brat_table, capacity_type, category, tot_length):
"""
Adds capacity categories to a table [Currently unused]
:param brat_table: The BRAT table created in make_capacity_table
:param capacity_type: Capacity type (Exisiting or Historic)
:param category: Capacity Category
:param tot_length: Total length of network
:return: Network length, Network length (km), Proportion of network in category, estimated dams
"""
if capacity_type == 'Existing':
cat_tbl = brat_table[brat_table['ExCategor'] == category]
else:
cat_tbl = brat_table[brat_table['HpeCategor'] == category]
length = cat_tbl['iGeo_Len'].sum()
length_km = length / 1000
network_prop = 100 * length / tot_length
est_dams = cat_tbl['mCC_EX_CT'].sum()
return length, length_km, network_prop, est_dams
def write_categories_complex(worksheet, watershed_name):
"""
Writing the side headers for complex size
:param worksheet: The current Excel Worksheet
:param watershed_name: The name of the watershed to be summarized
:return:
"""
column_size_a = worksheet.set_column('A:A', column_calc(30, watershed_name))
row = 2
col = 0
worksheet.write(row, col, "No Dams", column_size_a)
row += 1
worksheet.write(row, col, "Single Dam")
row += 1
worksheet.write(row, col, "Small Complex (2-3 Dams)")
row += 1
worksheet.write(row, col, "Medium Complex (4-5 dams)")
row += 1
worksheet.write(row, col, "Large Complex (>5 dams)")
row += 1
worksheet.write(row, col, "Total")
def write_categories_build_cap(worksheet, watershed_name):
"""
Writing the side headers for capacity
:param worksheet: The current Excel Worksheet
:param watershed_name: The name of the watershed to be summarized
:return:
"""
column_size_a = worksheet.set_column('A:A', column_calc(30, watershed_name))
row = 2
col = 0
worksheet.write(row, col, "None: 0", column_size_a)
row += 1
worksheet.write(row, col, "Rare: 0 - 1")
row += 1
worksheet.write(row, col, "Occasional: 1 - 5")
row += 1
worksheet.write(row, col, "Frequent: 5 - 15")
row += 1
worksheet.write(row, col, "Pervasive: 15 - 40")
row += 1
worksheet.write(row, col, "Total")
def write_categories_hist_vs_exist(worksheet, watershed_name):
"""
Writing the side headers for historic vs existing
:param worksheet: The current Excel Worksheet
:param watershed_name: The name of the watershed to be summarized
:return:
"""
column_size_a = worksheet.set_column('A:A', column_calc(30, watershed_name))
row = 3
col = 0
worksheet.write(row, col, "None: 0", column_size_a)
row += 1
worksheet.write(row, col, "Rare: 0 - 1")
row += 1
worksheet.write(row, col, "Occasional: 1 - 5")
row += 1
worksheet.write(row, col, "Frequent: 5 - 15")
row += 1
worksheet.write(row, col, "Pervasive: 15 - 40")
row += 1
worksheet.write(row, col, "Total")
def write_data(data1, data2, data3, data4, data5, total_length, worksheet, workbook):
"""
Writes data into an excel sheet, as long as it fits the specific 5 category format
:param data1: Values for the first category
:param data2: Values for the second category
:param data3: Values for the third category
:param data4: Values for the fourth category
:param data5: Values for the fifth category
:param total_length: Total Stream Length
:param worksheet: The current Excel Worksheet
:param workbook: The current Excel Workbook
:return:
"""
km_to_miles_ratio = 0.62137
data1 = data1 / 1000
data2 = data2 / 1000
data3 = data3 / 1000
data4 = data4 / 1000
data5 = data5 / 1000
# Set the column size.
worksheet.set_column('B:B', 20)
worksheet.set_column('C:C', 20)
header_format = workbook.add_format()
header_format.set_align('center')
header_format.set_bold()
worksheet.set_row(0, None, header_format)
worksheet.set_row(1, None, header_format)
# Adds the percent sign and puts it in percent form.
percent_format = workbook.add_format({'num_format': '0.00%'})
percent = worksheet.set_column('D:D', 10, percent_format)
# Formats to not show decimal places
cell_format1 = workbook.add_format()
cell_format1.set_num_format(0x03)
cell_format1.set_align('right')
col = 1
row = 2
worksheet.write(row, col, data1, cell_format1)
col += 1
worksheet.write(row, col, data1 * km_to_miles_ratio, cell_format1)
col += 1
worksheet.write(row, col, data1 / total_length, percent)
col = 1
row = 3
worksheet.write(row, col, data2, cell_format1)
col += 1
worksheet.write(row, col, data2 * km_to_miles_ratio, cell_format1)
col += 1
worksheet.write(row, col, data2 / total_length, percent)
col = 1
row = 4
worksheet.write(row, col, data3, cell_format1)
col += 1
worksheet.write(row, col, data3 * km_to_miles_ratio, cell_format1)
col += 1
worksheet.write(row, col, data3 / total_length, percent)
col = 1
row = 5
worksheet.write(row, col, data4, cell_format1)
col += 1
worksheet.write(row, col, data4 * km_to_miles_ratio, cell_format1)
col += 1
worksheet.write(row, col, data4 / total_length, percent)
col = 1
row = 6
worksheet.write(row, col, data5, cell_format1)
col += 1
worksheet.write(row, col, data5 * km_to_miles_ratio, cell_format1)
col += 1
worksheet.write(row, col, data5 / total_length, percent)
# Calculating Total for Stream Length(Km)
worksheet.write(7, 1, '=SUM(B3:B7)', cell_format1)
# Calculating Total for Stream Length (mi)
worksheet.write(7, 2, '=SUM(C3:C7)', cell_format1)
# Calculating total percentage.
worksheet.write(7, 3, '=SUM(D3:D7)', percent)
def search_cursor(fields, data, total, stream_network, is_complex, is_capacity_total, worksheet, workbook):
"""
Collects data for complex and capacity
:param fields: All fields in the network
:param data: A list containing five zeros to populate with data
:param total: A total that keeps track of total network length
:param stream_network: The BRAT input network to be summarized
:param is_complex: Bool that tells if this is going to calculate complex size stats
:param is_capacity_total: Bool that tells if this is going to calculate capacity total stats
:param worksheet: The current Excel Worksheet
:param workbook: The current Excel Workbook
:return: 5 data points, or prints them directly to the excel doc if calculating complex size stats
"""
split_input = stream_network.split(";")
if is_capacity_total:
fields.append("SHAPE@Length")
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for capacity, dam_complex_size, length in cursor:
if capacity == 0:
data[0] += capacity * (length/1000)
elif capacity <= 1:
data[1] += capacity * (length/1000)
elif capacity <= 5:
data[2] += capacity * (length/1000)
elif capacity <= 15:
data[3] += capacity * (length/1000)
else:
data[4] += capacity * (length/1000)
return data
elif is_complex:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, dam_complex_size in cursor:
total += length
if dam_complex_size == 0:
data[0] += length
elif dam_complex_size <= 1:
data[1] += length
elif dam_complex_size <= 3:
data[2] += length
elif dam_complex_size <= 5:
data[3] += length
else:
data[4] += length
total = total / 1000
write_data(data[0], data[1], data[2], data[3], data[4], total, worksheet, workbook)
else:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, capacity in cursor:
total += length
if capacity == 0:
data[0] += length/1000
elif capacity <= 1:
data[1] += length/1000
elif capacity <= 5:
data[2] += length/1000
elif capacity <= 15:
data[3] += length/1000
else:
data[4] += length/1000
return data
def column_calc(minimum, watershed):
"""
Returns the size that a column should be, making sure it has enough room to fit the data and the watershed name
:param minimum: The minimum length needed to fit the data
:param watershed: The watershed name
:return: The length that the colun should be
"""
if minimum > (len(watershed) + 10):
return minimum
else:
return len(watershed) + 10
def write_summary_worksheet(worksheet, stream_network, watershed_name, workbook, fields_list, dams):
"""
Writes the entire summary worksheet
:param worksheet: The current Excel Worksheet
:param stream_network: The BRAT input network to be summarized
:param watershed_name: The name of the watershed to be summarized
:param workbook: The current Excel Workbook
:param fields_list: A list of every field present in the network shapefile
:param dams: Shapefile containing dams for the entire area
:return:
"""
# formatting
column_size_a = worksheet.set_column('A:A', column_calc(40, watershed_name))
worksheet.set_column('B:B', 10)
worksheet.set_column('C:C', 5)
worksheet.set_column('D:D', 65)
header_format = workbook.add_format()
header_format.set_align('center')
header_format.set_bold()
worksheet.set_row(0, None, header_format)
worksheet.set_row(1, None, header_format)
percent_format = workbook.add_format({'num_format': '0.00%'})
percent_format.set_align('right')
percent1 = worksheet.set_column('E:E', 8, percent_format)
color = workbook.add_format()
color.set_bg_color('C0C0C0')
cell_format1 = workbook.add_format()
cell_format1.set_num_format(0x03)
cell_format1.set_align('right')
# categories
row = 0
col = 0
worksheet.write(row, col, watershed_name, column_size_a)
row += 2
worksheet.write(row, col, "Total Stream Length (Km)")
row += 1
worksheet.write(row, col, "Total Stream Length (mi)")
row += 1
worksheet.write(row, col, "Existing Complex Size")
row += 1
worksheet.write(row, col, "Historic Complex Size")
row += 1
worksheet.write(row, col, "Existing Capacity")
row += 1
worksheet.write(row, col, "Historic Capacity")
row += 1
worksheet.write(row, col, "Existing Vegetation Capacity")
row += 1
worksheet.write(row, col, "Historic Vegetation Capacity")
# row += 1
# worksheet.write(row, col, "Total Length (Km) Observed > 80% Predicted")
row += 1
worksheet.write(row, col, "Number Dams Snapped")
row += 1
worksheet.write(row, col, "Total Dam Count")
row = 6
col = 3
worksheet.write(row, col, "% Reaches Within Capacity Estimate")
row += 1
worksheet.write(row, col, "% Network \"Easiest - Low-Hanging Fruit\"")
row += 1
worksheet.write(row, col, "% Network \"Dam Building Possible\"")
row += 1
worksheet.write(row, col, "% Network \"Negligible Risk\"")
# row += 1
# worksheet.write(row, col, "% Observed > 80% Predicted")
row += 1
worksheet.write(row, col, "% Total Dams Snapped to Network")
split_input = stream_network.split(";")
# total stream lengths
total_stream_length_km = 0.0
fields = ['SHAPE@Length']
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, in cursor:
total_stream_length_km += length
total_stream_length_km /= 1000
total_stream_length_mi = total_stream_length_km / 1.609344
# total complex sizes
fields = ['SHAPE@Length', "mCC_EX_CT"]
if fields[1] in fields_list:
total_existing_complex = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, dam_complex_size in cursor:
total_existing_complex += dam_complex_size
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_existing_complex = "N/A"
fields = ['SHAPE@Length', "mCC_HPE_CT"]
if fields[1] in fields_list:
total_historic_complex = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, dam_complex_size in cursor:
total_historic_complex += dam_complex_size
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_historic_complex = "N/A"
# total vegetation capacity
fields = ['SHAPE@Length', "oVC_EX"]
if fields[1] in fields_list:
total_existing_veg = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, density in cursor:
total_existing_veg += ((length / 1000) * density)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_existing_veg = "N/A"
fields = ['SHAPE@Length', "oVC_Hpe"]
if fields[1] in fields_list:
total_historic_veg = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, density in cursor:
total_historic_veg += ((length / 1000) * density)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_historic_veg = "N/A"
# Existing Capacity
fields = ['SHAPE@Length', "oCC_EX"]
if fields[1] in fields_list:
total_existing_capacity = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, density in cursor:
total_existing_capacity += ((length / 1000) * density)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_existing_capacity = "N/A"
# Historic Capacity
fields = ['SHAPE@Length', "oCC_HPE"]
if fields[1] in fields_list:
total_historic_capacity = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, density in cursor:
total_historic_capacity += ((length / 1000) * density)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
total_historic_capacity = "N/A"
# observed vs. predicted
fields = ['SHAPE@Length', "e_DamDens", "oCC_EX"]
if (fields[1] in fields_list) and (fields[2] in fields_list):
total_surveyed_greater_length = 0.0
total_surveyed_greater_count = 0.0
reach_count = 0.0
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, surveyed, predicted in cursor:
reach_count += 1
if surveyed > (predicted * .8):
total_surveyed_greater_length += (length / 1000)
total_surveyed_greater_count += 1
else:
pass
# total_surveyed_greater_percent = float(total_surveyed_greater_count) / float(reach_count)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
# total_surveyed_greater_percent = "N/A"
# total_surveyed_greater_length = "N/A"
# dams snapped
if dams is not None:
dam_fields = [f.name for f in arcpy.ListFields(dams)]
if "Snapped" in dam_fields:
total_snapped = 0.0
not_snapped = 0.0
percent_snapped = 0.0
with arcpy.da.SearchCursor(dams, "Snapped") as cursor:
for snapped in cursor:
if snapped[0] == "Not snapped to network":
not_snapped += 1
elif snapped[0] == "Snapped to network":
total_snapped += 1
else:
pass
if float(total_snapped) + float(not_snapped) > 0:
percent_snapped = float(total_snapped) / (float(total_snapped) + float(not_snapped))
else:
arcpy.AddWarning("Could not complete summary worksheet: \"Snapped\" field in Dams shapefile missing")
total_snapped = "N/A"
percent_snapped = "N/A"
else:
arcpy.AddWarning("Could not complete summary worksheet: Dams shapefile missing")
total_snapped = "N/A"
percent_snapped = "N/A"
# dam census density and dams count
if dams is not None:
dam_census_count = 0.0
dam_census_density = 0.0
with arcpy.da.SearchCursor(dams, 'SHAPE@Length') as cursor:
for length in cursor:
dam_census_density += 1
dam_census_count += 1
# dam_census_density /= total_stream_length_km
else:
arcpy.AddWarning("Could not complete summary worksheet: Dams shapefile missing")
dam_census_count = "N/A"
# dam_census_density = "N/A"
# percent estimated correctly
estimate_right = 0
estimate_wrong = 0
estimate_wrong_short = 0
fields = ['SHAPE@Length', 'BRATvSurv', 'e_DamCt']
if fields[1] in fields_list and fields[2] in fields_list:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, bratvsurv, damCount in cursor:
if damCount == 0:
pass
elif bratvsurv >= 1:
estimate_right += 1
elif bratvsurv == -1:
estimate_right += 1
# elif damCount == 0:
# estimate_right += 1
else:
estimate_wrong += 1
if length < 150:
estimate_wrong_short += 1
if float(estimate_wrong)+float(estimate_right) == 0:
percent_correct_estimate = "N/A"
else:
percent_correct_estimate = float(estimate_right) / (float(estimate_wrong) + float(estimate_right))
if estimate_wrong is not 0:
percent_under = float(estimate_wrong_short) / float(estimate_wrong)
else:
percent_under = "N/A"
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
percent_correct_estimate = "N/A"
percent_under = "N/A"
# percent network "Easiest-Low Hanging Fruit"
fields = ['SHAPE@Length', "oPBRC_CR"]
easiest_length = 0.0
if fields[1] in fields_list:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, category in cursor:
if category == "Easiest - Low-Hanging Fruit":
easiest_length += length
else:
pass
percent_easiest = easiest_length / (total_stream_length_km * 1000)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
percent_easiest = "N/A"
# percent network "Dam Building Possible"
fields = ['SHAPE@Length', "oPBRC_UD"]
possible_length = 0.0
if fields[1] in fields_list:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, category in cursor:
if category == "Dam Building Possible":
possible_length += length
else:
pass
percent_possible = possible_length / (total_stream_length_km * 1000)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
percent_possible = "N/A"
# percent network "Negligible Risk"
fields = ['SHAPE@Length', "oPBRC_UI"]
negligibleLength = 0.0
if fields[1] in fields_list:
for streams in split_input:
with arcpy.da.SearchCursor(streams, fields) as cursor:
for length, category in cursor:
if category == "Negligible Risk":
negligibleLength += length
else:
pass
percent_negligible = negligibleLength / (total_stream_length_km * 1000)
else:
arcpy.AddWarning("Could not complete summary worksheet: {0} not in fields.".format(fields[1]))
percent_negligible = "N/A"
# output all calculations
row = 2
col = 1
worksheet.write(row, col, total_stream_length_km, cell_format1)
row += 1
worksheet.write(row, col, total_stream_length_mi, cell_format1)
row += 1
worksheet.write(row, col, total_existing_complex, cell_format1)
row += 1
worksheet.write(row, col, total_historic_complex, cell_format1)
row += 1
worksheet.write(row, col, total_existing_capacity, cell_format1)
row += 1
worksheet.write(row, col, total_historic_capacity, cell_format1)
row += 1
worksheet.write(row, col, total_existing_veg, cell_format1)
row += 1
worksheet.write(row, col, total_historic_veg, cell_format1)
# row += 1
# worksheet.write(row, col, totalSurveyedGreaterLength, cell_format1)
row += 1
worksheet.write(row, col, total_snapped, cell_format1)
row += 1
worksheet.write(row, col, dam_census_count, cell_format1)
row = 6
col = 4
worksheet.write(row, col, percent_correct_estimate, percent1)
row += 1
worksheet.write(row, col, percent_easiest, percent1)
row += 1
worksheet.write(row, col, percent_possible, percent1)
row += 1
worksheet.write(row, col, percent_negligible, percent1)
# row += 1
# worksheet.write(row, col, totalSurveyedGreaterPercent, percent1)
row += 1
worksheet.write(row, col, percent_snapped, percent1)
row = 11
col = 3
if estimate_wrong > 0:
worksheet.write(row, col, (str(estimate_wrong_short) + " / " + str(
estimate_wrong) + " reaches above capacity estimate were less than 150m"))
col += 1
worksheet.write(row, col, percent_under, percent1)
else:
worksheet.write(row, col, "No reaches were overestimated")
col += 1
worksheet.write(row, col, "N/A")
def write_strategy_map_worksheet(worksheet, stream_network, watershed_name, workbook):
"""
Writes the entire Strategy Map worksheet
:param worksheet: The current Excel Worksheet
:param stream_network: The BRAT input network to be summarized
:param watershed_name: The name of the watershed to be summarized
:param workbook: The current Excel Workbook
:return:
"""
worksheet.set_column('A:A', column_calc(50, watershed_name))
worksheet.set_column('B:B', 20)
worksheet.set_column('C:C', 20)
worksheet.set_column('D:D', 15)
header_format = workbook.add_format()
header_format.set_align('center')
header_format.set_bold()
worksheet.set_row(0, None, header_format)
worksheet.set_row(1, None, header_format)
percent_format = workbook.add_format({'num_format': '0.00%'})
worksheet.set_column('D:D', 10, percent_format)
color = workbook.add_format()
color.set_bg_color('#C0C0C0')
cell_format1 = workbook.add_format()
cell_format1.set_num_format(0x03)
cell_format1.set_align('right')
split_input = stream_network.split(";")
row = 0
col = 0
worksheet.write(row, col, watershed_name)
row += 1
worksheet.write(row, col, "DamStrat")
col += 1
worksheet.write(row, col, "Stream Length (Km)")
col += 1
worksheet.write(row, col, "Stream Length (mi)")
col += 1
worksheet.write(row, col, "Percent")
total = 0
category_list = ['1. Beaver conservation',
'2. Highest restoration potential - translocation',
'3. High restoration potential',
'3a. Vegetation restoration first-priority',
'4. Medium-low restoration potential',
'4a. Vegetation restoration first-priority',
'5. Restoration with infrastructure modification',
'6. Restoration with urban or agricultural modification',
'Other']
count_list = [0, 0, 0, 0, 0, 0, 0, 0, 0]
for streams in split_input:
with arcpy.da.SearchCursor(streams, ['SHAPE@Length', 'DamStrat']) as cursor:
for length, category in cursor:
total += length
for counter, match in enumerate(category_list):
if category == match:
count_list[counter] += (length/1000)
row = 2
col = 0
for category in category_list:
worksheet.write(row, col, category)
row += 1
col += 1
row = 2
for kilometers in count_list:
worksheet.write(row, col, kilometers, cell_format1)
row += 1
row = 2
col += 1
for miles in count_list:
miles *= 0.62137
worksheet.write(row, col, miles, cell_format1)
row += 1
col += 1
row = 2
for cellCount, percent in enumerate(count_list):
worksheet.write(row, col, "=B{}/B12".format(cellCount+3), percent_format)
row += 1
col = 0
worksheet.write(row, col, "Total")
col += 1
worksheet.write(row, col, "=SUM(B{}:B{})".format(3, 11), cell_format1)
col += 1
worksheet.write(row, col, "=SUM(C{}:C{})".format(3, 11), cell_format1)
col += 1
worksheet.write(row, col, "N/A")
def write_density_correlations_worksheet(worksheet, stream_network, watershed_name, workbook):
"""
Writes the entire Density Correlations worksheet
:param worksheet: The current Excel Worksheet
:param stream_network: The BRAT input network to be summarized
:param watershed_name: The name of the watershed to be summarized
:param workbook: The current Excel Workbook
:return:
"""
# formatting
worksheet.set_column('A:A', column_calc(25, watershed_name))
worksheet.set_column('B:B', 20)
worksheet.set_column('C:C', 11)
header_format = workbook.add_format()
header_format.set_align('center')
header_format.set_bold()
cell_format1 = workbook.add_format()
cell_format1.set_num_format(0x04)
cell_format1.set_align('right')
split_input = stream_network.split(";")
# These lists hold all of the data that needs to be printed
categories = ['Low Flow Values', 'High Flow Values', 'Stream Slope Values', 'Stream Power Low', 'Stream Power High']
fields = ['iHyd_QLow', 'iHyd_Q2', 'iGeo_Slope', 'iHyd_SPLow', 'iHyd_SP2']
column0 = [watershed_name, '']
column1 = ['oCC_EX', '']
column2 = ['', '', fields[0], '', '', '', fields[1], '', '', '', fields[2],
'', '', '', fields[3], '', '', '', fields[4]]
for field, category in zip(fields, categories):
column0.append(category)
column1.append("Mean Dam Density")
data_list = []
density_list = []
for streams in split_input:
with arcpy.da.SearchCursor(streams, [field, 'oCC_EX']) as cursor:
for data, density in cursor:
data_list.append(data)
density_list.append(density)
data_list, density_list = zip(*sorted(zip(data_list, density_list)))
total_length = len(data_list)
bin1 = total_length/3
bin2 = (total_length/3) + (total_length/3)
bin3 = total_length