-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy_management_SE.py
945 lines (733 loc) · 41.1 KB
/
copy_management_SE.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
# copy the sediment input rasters at the *positions in the following database structure:
#(the *Einzel rasters were already calculated beforehand...*Summe and *Mittel have to be calculated sometimes before copying is possible...)
#(as and example the region Frienisberg is taken, but the structure stays the same for the
# regions Lobsigen, Schwanden, Seedorf and Suberg)
#
#Sedimenteintrag
# Frienisberg
# Frienisberg_1997_2007
# Periode
# *Einzel
# *Summe
# *Mittel
# Jahr
# 1997
# *Einzel
# *Summe
# 1998...
# Datum
# 1998_02_25
# *Einzel
# *Summe
# 1998_03_21...
# Erosionsform
# linear
# *Einzel
# *Summe
# flaechenhaft
# *Einzel
# *Summe
# Parzelle
# Parzelle_01
# *Einzel
# *Summe
# Parzelle_02...
# Frienisberg_2007_2017...(same as for Frienisberg_1997_2007)
import os
import xlrd
import arcpy
from arcpy.sa import *
import numpy as np
import pandas as pd
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
import sys
#########################################################################################################################
#insert your input paths here:
# where did you save the folder on your device, where the treelike folder structure starts?
#(in this Folder the subfolders "Frienisberg", "Lobsigen", "Schwanden", "Seedorf", "Suberg" already existed)
Gebiete = r"E:\David_Remund_Masterarbeit\alle_GE_MA\Gewässereintrag\Sedimenteintrag"
#Where did you store your raster data on your device?
sed_inp_dir = r"E:\David_Remund_Masterarbeit\alle_GE_MA\output_GE"
#where did you store your mean sediment input rasters on your device?
sed_mean_dir = r"E:\David_Remund_Masterarbeit\alle_GE_MA\mean_sediment_input"
#where did you store the helping script "build_df_from_arcpy_181119" on your device?
sys.path.insert(0, r'E:\David_Remund_Masterarbeit\alle_GE_MA\scripts_for_GE\geodata_analysis\Hilfsskripts')
import build_df_from_arcpy_181119 #ignore that python marks that line red!!!
#where did you store the vector data on your device?
SE_vec_copied = r"E:\David_Remund_Masterarbeit\alle_GE_MA\SE_vectors_copied"
###########################################################################################################################
######### code until here as to be in the python console to RUN the Subsections of Code successfully ###################
####tree branch periode
############################ copy sediment input rasters to .../Periode/Einzel ################################
count = 0
for sed_geb in os.listdir(sed_inp_dir):
path_origin = os.path.join(sed_inp_dir,sed_geb)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
#get a list of all destination path folders
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.listdir(os.path.join(Gebiete, GEB))
for sub_geb in sub_GEB:
#get destination name
destination_path = os.path.join(Gebiete,GEB,sub_geb,"Periode","Einzel")
print(destination_path)
if count == 0:
destination_list = [] + [destination_path]
count += 1
else:
destination_list.append(destination_path)
print(destination_list)
#copy e.g. sediment input rasters of Frienisberg_07_17_SE to new created folder Frienisberg/Frienisberg_2007_2017/Periode/Einzel
for org in origin_list:
for des in destination_list:
#for origin path, get e.g. Seedorf17 out of r'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\output_GE\\Seedorf_07_17_SE'
org_base = os.path.basename(org)
org_name = org_base.partition('_')[0] + org_base[-5:-3]
#for destination path, get e.g. Seedorf17 out of "E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Seedorf\\Seedorf_2007_2017\\Periode\\Einzel"
des_gb_p = os.path.normpath(des)
des_gb_p = des.split(os.sep)[6]
des_name = des_gb_p.partition('_')[0] + des_gb_p[-2:]
if org_name == des_name:
#list all rasters in origin_path
arcpy.env.workspace = org
rasList = arcpy.ListRasters()
for ras in rasList:
if os.path.exists(os.path.join(des, ras)):
print("path already exists")
else:
arcpy.Copy_management(ras,os.path.join(des,ras))
print(ras + " copied")
############################ copy sediment input rasters to .../Periode/Mittel ################################
#make a list of the paths of mean sediment input rasters
arcpy.env.workspace = sed_mean_dir
ras_list = arcpy.ListRasters()
count = 0
for ras in ras_list:
path_origin = os.path.join(sed_mean_dir, ras)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
# make a list of destination paths
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.listdir(os.path.join(Gebiete, GEB))
for sub_geb in sub_GEB:
#get destination name
destination_path = os.path.join(Gebiete,GEB,sub_geb,"Periode","Mittel")
if count == 0:
destination_list = [] + [destination_path]
count += 1
else:
destination_list.append(destination_path)
print(destination_list)
#copy e.g. sediment input rasters of Frienisberg_07_17_SE to new created folder Frienisberg/Frienisberg_2007_2017/Periode/Mittel
for org in origin_list:
for des in destination_list:
#for origin path, get e.g. Seedorf17 out of r'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\output_GE\\Seedorf_07_17_SE'
org = os.path.basename(org)
org_part = org.partition('_')[2]
org_name = org_part.partition('_')[0] + org[-9:-7]
#for destination path, get e.g. Seedorf17 out of "E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Seedorf\\Seedorf_2007_2017\\Periode\\Mittel"
des_gb_p = os.path.normpath(des)
des_gb_p = des.split(os.sep)[6]
des_name = des_gb_p.partition('_')[0] + des_gb_p[-2:]
#if orgin name (org_name) equals destination name (des_name, e.g. Seedorf17), then copy:
if org_name == des_name:
arcpy.env.workspace = org
if os.path.exists(os.path.join(des, os.path.basename(org))):
print("file already exists")
else:
arcpy.Copy_management(org,os.path.join(des, os.path.basename(org)))
print(org + " copied to " + os.path.join(des, os.path.basename(org)))
#################### sum up .../Peiode/Einzel rasters and copy sediment input rasters to .../Periode/Summe ###########
#make a list of raster-folders path e.g. .../Frienisberg_1997_2007/Periode/Einzel
count = 0
for geb in os.listdir(Gebiete):
path_geb = os.path.join(Gebiete,geb)
os.chdir(path_geb)
for GEB in os.listdir(path_geb):
path_origin = os.path.join(path_geb,GEB,"Periode","Einzel")
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
#for all rasters in e.g. .../Frienisberg_1997_2007/Periode/Einzel, calculate mean of all rasters and
#save sum raster in e.g. .../Frienisberg_1997_2007/Periode/Mittel
for org in origin_list:
# set output Cooridnate-System -> otherwise, after NumpyToRaster Conversion, the Coordinate System is "unknown"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("CH1903 LV03")
arcpy.env.workspace = org
rasterList = arcpy.ListRasters()
# pick first raster of list and extract location information out of it
locate_raster = Raster(rasterList[0])
# get lowerLeft and cellSize
lowerLeft = arcpy.Point(locate_raster.extent.XMin, locate_raster.extent.YMin)
cellSize = locate_raster.meanCellWidth
i = 0
for ras in rasterList:
# sum up all rasters [t/ha] of e.g. .../Frienisberg_1997_2007/Periode/Einzel after conversion to numpy array
arr = arcpy.RasterToNumPyArray(ras)
arr = arr.astype(float)
if i == 0:
outras1 = arr
i += 1
else:
outras1 = outras1 + arr
i += 1
# convert numpy array back to raster
sum_ras = arcpy.NumPyArrayToRaster(outras1, lowerLeft, cellSize, cellSize)
#extract out of the path .../Frienisberg_1997_2007/Periode/Einzel the string Frienisberg_97_07
org_name = org.split(os.sep)[6]
org_name = org_name.split('_')[0] + "_" + org_name[-7:-5] + "_" + org_name[-2:]
# set new_basename for output path
newbase_name = "sum_" + org_name + "_SE.tif"
print(newbase_name)
#get from path e.g. 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Periode\\Einzel'
# the part 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Periode'
# and add //Summe to the extracted path
destination_path = os.path.dirname(org)
destination_path = os.path.join(destination_path,"Summe",newbase_name)
# save sum_ras in the destination path if the destination file doesn't already exist
if os.path.exists(destination_path):
print("file already exists")
else:
sum_ras.save(destination_path)
####tree branch Jahr
############################ copy sediment input rasters to .../Jahr/e.g.1998/Einzel ################################
# make a list that consists of paths of SE_vectors_copies-subfolders
count = 0
for SE_vec in os.listdir(SE_vec_copied):
path_origin = os.path.join(SE_vec_copied, SE_vec)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
count = 0
#make a list of raster-folders path e.g. ...output_GE/Frienisberg_07_17_GE (sed_inp_dir has to be in Console)
for sed_geb in os.listdir(sed_inp_dir):
path_origin = os.path.join(sed_inp_dir,sed_geb)
if count == 0:
sed_inp_list = [] + [path_origin]
count += 1
else:
sed_inp_list.append(path_origin)
print(sed_inp_list)
for se in sed_inp_list:
for vec_gb_p in origin_list:
# list vectors in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\SE_vectors_copied\Frienisberg_07_17
arcpy.env.workspace = vec_gb_p
vec_List = arcpy.ListFeatureClasses()
# list rasters in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\output_GE\Frienisberg_07_17_GE
arcpy.env.workspace = se
se_List = arcpy.ListRasters()
# e.g. the event FEKRE31_M2_024_SE.tif is ONLY unique in a specific region -> the event FEKRE31_M2_024_SE.tif
# exists in Frienisberg_97_07 AND in Lobisgen_97_07
#
#therefore only copy a sediment input raster if the raster is in the same region as the correpsonding vector
if os.path.basename(se[:-3]) == os.path.basename(vec_gb_p):
for ras in se_List:
for vec in vec_List:
if ras[:-7] == vec[:-4]:
print(ras)
# extract Date form pandas dataframe and convert it to str
tab = build_df_from_arcpy_181119.build_df_from_arcpy(os.path.join(vec_gb_p, vec))
dat_1 = tab["Datum"][0]
# because some date are strings and some are Timestamps (I don't know why...were digitalized like this), do distinguish between those data types
# -> make strings out of them
if type(dat_1) == str:
dat_1 = dat_1
elif isinstance(dat_1, pd.Timestamp) == True:
dat_1 = str(pd.Timestamp.date(dat_1))
# extract year
year_1 = dat_1[:-6]
print(year_1)
# copy the corresponding raster to new directory: e.g.
# E:\David_Remund_Masterarbeit\alle_GE_MA\Gewässereintrag\Sedimenteintrag\Frienisberg\Frienisberg_1997_2007\Jahr\1998\Einzel
if os.path.basename(vec_gb_p)[-5:] == "07_17":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_2007_2017", "Jahr",
year_1,
"Einzel")
if os.path.basename(vec_gb_p)[-5:] == "97_07":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_1997_2007", "Jahr",
year_1,
"Einzel")
# copy the sediment input raster (ras) to the destination path folder
if os.path.exists(os.path.join(destination_path, ras)):
print("file already exists") # file can also be already copied in the same RUN session
else:
arcpy.Copy_management(ras, os.path.join(destination_path, ras))
print(ras + " copied to " + os.path.join(destination_path, ras))
############################ calculate yearly sum & copy sum raster to .../Jahr/e.g.1998/Summe ################
# note: the yearly sum in [t/ha] has the same value as the yearly mean in [t/ha*a] (because of division through 1a)
### calculate yearly sum of sediment input rasters in e.g. ...\Frienisberg_1997_2007\Jahr\1998\Einzel & copy
# the sum raster to e.g. ...\Frienisberg_1997_2007\Jahr\1998\Summe #####################################
#make a list of paths like e.g. ...\Frienisberg_1997_2007\Jahr\e.g.1998\Einzel
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.path.join(Gebiete, GEB)
for sub_geb in os.listdir(sub_GEB):
Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Jahr")
for Jahr in os.listdir(Jahr_path):
specific_Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Jahr",Jahr,"Einzel")
if count == 0:
specific_Jahr_list = [] + [specific_Jahr_path]
count += 1
else:
specific_Jahr_list.append(specific_Jahr_path)
print(specific_Jahr_list)
#sum up all sediment input rasters in folders with paths like e.g. ...\Frienisberg_1997_2007\Jahr\e.g.1998\Einzel
for SE_ras in specific_Jahr_list:
# set output Cooridnate-System -> otherwise, after NumpyToRaster Conversion, the Coordinate System is "unknown"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("CH1903 LV03")
arcpy.env.workspace = SE_ras
rasterList = arcpy.ListRasters()
# pick first raster of list and extract location information out of it
locate_raster = Raster(rasterList[0])
# get lowerLeft and cellSize
lowerLeft = arcpy.Point(locate_raster.extent.XMin, locate_raster.extent.YMin)
cellSize = locate_raster.meanCellWidth
i = 0
for ras in rasterList:
# sum up all rasters [t/ha] of e.g. .../Frienisberg_1997_2007/Jahr/e.g.1998/Einzel after conversion to numpy array
arr = arcpy.RasterToNumPyArray(ras)
arr = arr.astype(float)
if i == 0:
outras1 = arr
i += 1
else:
outras1 = outras1 + arr
i += 1
# convert numpy array back to raster
sum_ras = arcpy.NumPyArrayToRaster(outras1, lowerLeft, cellSize, cellSize)
##########set the new_basename e.g. sum_Frienisberg_97_07_1998_SE.tif ################
# extract out of the path .../Frienisberg_1997_2007/Jahr/1998/Einzel the string Frienisberg_97_07
gebperi_name = SE_ras.split(os.sep)[6]
gebperi_name = gebperi_name.split('_')[0] + "_" + gebperi_name[-7:-5] + "_" + gebperi_name[-2:]
# extract out of the path .../Frienisberg_1997_2007/Jahr/1998/Einzel the Jahr-string
year = SE_ras.split(os.sep)[8]
# set new_basename for output path
newbase_name = "sum_" + gebperi_name + "_"+ year + "_SE.tif"
print(newbase_name)
############# copy sum_raster to the destination path ###############
# get from path e.g. 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Jahr\\some year\\Einzel'
# the part 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Jahr\\some year'
# and add //Summe to the extracted path
destination_path = os.path.dirname(SE_ras)
destination_path = os.path.join(destination_path, "Summe", newbase_name)
# save sum_ras in the destination path if the destination file doesn't already exist
if os.path.exists(destination_path):
print("file already exists")
else:
sum_ras.save(destination_path)
print(newbase_name + " was copied to " + destination_path)
####tree branch Datum
############################ copy sediment input rasters to .../Datum/e.g.1998_02_25/Einzel ################################
# make a list that consists of paths of SE_vectors_copies-subfolders
count = 0
for SE_vec in os.listdir(SE_vec_copied):
path_origin = os.path.join(SE_vec_copied, SE_vec)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
count = 0
#make a list of raster-folders path e.g. ...output_GE/Frienisberg_07_17_GE (sed_inp_dir has to be in Console)
for sed_geb in os.listdir(sed_inp_dir):
path_origin = os.path.join(sed_inp_dir,sed_geb)
if count == 0:
sed_inp_list = [] + [path_origin]
count += 1
else:
sed_inp_list.append(path_origin)
print(sed_inp_list)
for se in sed_inp_list:
for vec_gb_p in origin_list:
# list vectors in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\SE_vectors_copied\Frienisberg_07_17
arcpy.env.workspace = vec_gb_p
vec_List = arcpy.ListFeatureClasses()
# list rasters in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\output_GE\Frienisberg_07_17_GE
arcpy.env.workspace = se
se_List = arcpy.ListRasters()
# e.g. the event FEKRE31_M2_024_SE.tif is ONLY unique in a specific region -> the event FEKRE31_M2_024_SE.tif
# exists in Frienisberg_97_07 AND in Lobisgen_97_07
#
#therefore only copy a sediment input raster if the raster is in the same region as the correpsonding vector
if os.path.basename(se[:-3]) == os.path.basename(vec_gb_p):
for ras in se_List:
for vec in vec_List:
if ras[:-7] == vec[:-4]:
print(ras)
# extract Date form pandas dataframe and convert it to str
tab = build_df_from_arcpy_181119.build_df_from_arcpy(os.path.join(vec_gb_p, vec))
dat_1 = tab["Datum"][0]
# because some date are strings and some are Timestamps (I don't know why...were digitalized like this), do distinguish between those data types
# -> make strings out of them
if type(dat_1) == str:
dat_1 = dat_1
elif isinstance(dat_1, pd.Timestamp) == True:
dat_1 = str(pd.Timestamp.date(dat_1))
dat_1 = dat_1.replace("-","_")
# copy the corresponding raster to new directory: e.g.
# E:\David_Remund_Masterarbeit\alle_GE_MA\Gewässereintrag\Sedimenteintrag\Frienisberg\Frienisberg_1997_2007\Datum\1998_08_10\Einzel
if os.path.basename(vec_gb_p)[-5:] == "07_17":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_2007_2017", "Datum",
dat_1,
"Einzel")
if os.path.basename(vec_gb_p)[-5:] == "97_07":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_1997_2007", "Datum",
dat_1,
"Einzel")
# copy the sediment input raster (ras) to the destination path folder
if os.path.exists(os.path.join(destination_path, ras)):
print("file already exists") # file can also be already copied in the same RUN session
else:
arcpy.Copy_management(ras, os.path.join(destination_path, ras))
print(ras + " copied to " + os.path.join(destination_path, ras))
############################ calculate date sum & copy sum raster to .../Datum/e.g.1998_02_13/Summe ################
### calculate date sum of sediment input rasters in e.g. ...\Frienisberg_1997_2007\Datum\1998_02_13\Einzel & copy
# the sum raster to e.g. ...\Frienisberg_1997_2007\Datum\1998_02_13\Summe #####################################
#make a list of paths like e.g. ...\Frienisberg_1997_2007\Datum\e.g.1998_02_13\Einzel
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.path.join(Gebiete, GEB)
for sub_geb in os.listdir(sub_GEB):
Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Datum")
for Datum in os.listdir(Jahr_path):
specific_Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Datum",Datum,"Einzel")
if count == 0:
specific_Jahr_list = [] + [specific_Jahr_path]
count += 1
else:
specific_Jahr_list.append(specific_Jahr_path)
print(specific_Jahr_list)
#sum up all sediment input rasters in folders with paths like e.g. ...\Frienisberg_1997_2007\Datum\e.g.1998_02_13\Einzel
for SE_ras in specific_Jahr_list:
# set output Cooridnate-System -> otherwise, after NumpyToRaster Conversion, the Coordinate System is "unknown"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("CH1903 LV03")
arcpy.env.workspace = SE_ras
rasterList = arcpy.ListRasters()
# pick first raster of list and extract location information out of it
locate_raster = Raster(rasterList[0])
# get lowerLeft and cellSize
lowerLeft = arcpy.Point(locate_raster.extent.XMin, locate_raster.extent.YMin)
cellSize = locate_raster.meanCellWidth
i = 0
for ras in rasterList:
# sum up all rasters [t/ha] of e.g. .../Frienisberg_1997_2007/Jahr/e.g.1998/Einzel after conversion to numpy array
arr = arcpy.RasterToNumPyArray(ras)
arr = arr.astype(float)
if i == 0:
outras1 = arr
i += 1
else:
outras1 = outras1 + arr
i += 1
# convert numpy array back to raster
sum_ras = arcpy.NumPyArrayToRaster(outras1, lowerLeft, cellSize, cellSize)
##########set the new_basename e.g. sum_Frienisberg_97_07_1998_SE.tif ################
# extract out of the path .../Frienisberg_1997_2007/Jahr/1998/Einzel the string Frienisberg_97_07
gebperi_name = SE_ras.split(os.sep)[6]
gebperi_name = gebperi_name.split('_')[0] + "_" + gebperi_name[-7:-5] + "_" + gebperi_name[-2:]
# extract out of the path .../Frienisberg_1997_2007/Jahr/1998/Einzel the Jahr-string
date = SE_ras.split(os.sep)[8]
# set new_basename for output path
newbase_name = "sum_" + gebperi_name + "_"+ date + "_SE.tif"
print(newbase_name)
############# copy sum_raster to the destination path ###############
# get from path e.g. 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Datum\\some date\\Einzel'
# the part 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Datum\\some date'
# and add //Summe to the extracted path
destination_path = os.path.dirname(SE_ras)
destination_path = os.path.join(destination_path, "Summe", newbase_name)
# save sum_ras in the destination path if the destination file doesn't already exist
if os.path.exists(destination_path):
print("file already exists")
else:
sum_ras.save(destination_path)
print(newbase_name + " was copied to " + destination_path)
####tree branch Erosionsform
############################ copy sediment input rasters to .../Erosionsform/linear or flaechenhaft/Einzel ############
# make a list that consists of paths of SE_vectors_copies-subfolders
count = 0
for SE_vec in os.listdir(SE_vec_copied):
path_origin = os.path.join(SE_vec_copied, SE_vec)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
count = 0
#make a list of raster-folders path e.g. ...output_GE/Frienisberg_07_17_GE (sed_inp_dir has to be in Console)
for sed_geb in os.listdir(sed_inp_dir):
path_origin = os.path.join(sed_inp_dir,sed_geb)
if count == 0:
sed_inp_list = [] + [path_origin]
count += 1
else:
sed_inp_list.append(path_origin)
print(sed_inp_list)
for se in sed_inp_list:
for vec_gb_p in origin_list:
# list vectors in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\SE_vectors_copied\Frienisberg_07_17
arcpy.env.workspace = vec_gb_p
vec_List = arcpy.ListFeatureClasses()
# list rasters in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\output_GE\Frienisberg_07_17_GE
arcpy.env.workspace = se
se_List = arcpy.ListRasters()
# e.g. the event FEKRE31_M2_024_SE.tif is ONLY unique in a specific region -> the event FEKRE31_M2_024_SE.tif
# exists in Frienisberg_97_07 AND in Lobisgen_97_07
#
#therefore only copy a sediment input raster if the raster is in the same region as the correpsonding vector
if os.path.basename(se[:-3]) == os.path.basename(vec_gb_p):
for ras in se_List:
for vec in vec_List:
if ras[:-7] == vec[:-4]:
print(ras)
# extract Erosionsform form pandas dataframe and convert it to str
tab = build_df_from_arcpy_181119.build_df_from_arcpy(os.path.join(vec_gb_p, vec))
EroForm = tab["EroForm"][0]
print(EroForm)
if EroForm == "RE":
EroForm = "linear"
if EroForm == "RNE":
EroForm = "linear"
if EroForm == "FRE":
EroForm = "linear"
if EroForm == "FE":
EroForm = "flaechenhaft"
if EroForm == "KRE":
EroForm = "flaechenhaft"
print(EroForm)
# copy the corresponding raster to new directory: e.g.
# E:\David_Remund_Masterarbeit\alle_GE_MA\Gewässereintrag\Sedimenteintrag\Frienisberg\Frienisberg_1997_2007\Erosionsform\linear oder flaechenhaft\Einzel
if os.path.basename(vec_gb_p)[-5:] == "07_17":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_2007_2017", "Erosionsform",
EroForm,
"Einzel")
if os.path.basename(vec_gb_p)[-5:] == "97_07":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_1997_2007", "Erosionsform",
EroForm,
"Einzel")
print(destination_path)
# copy the sediment input raster (ras) to the destination path folder
if os.path.exists(os.path.join(destination_path, ras)):
print("file already exists") # file can also be already copied in the same RUN session
else:
arcpy.Copy_management(ras, os.path.join(destination_path, ras))
print(ras + " copied to " + os.path.join(destination_path, ras))
####### calculate linear or areal sum & copy sum rasters to .../Erosionsform/linear or flaechenhaft/Summe ############
#make a list of paths like e.g. ...\Frienisberg_1997_2007\Erosionsform\linear oder flaechenhaft\Einzel
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.path.join(Gebiete, GEB)
for sub_geb in os.listdir(sub_GEB):
Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Erosionsform")
for EroF in os.listdir(Jahr_path):
specific_Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Erosionsform",EroF,"Einzel")
if count == 0:
specific_Jahr_list = [] + [specific_Jahr_path]
count += 1
else:
specific_Jahr_list.append(specific_Jahr_path)
print(specific_Jahr_list)
#remove ...\Seedorf_2007_2017\Erosionsform\flaechenhaft\Einzel out of the specific_Jahr_list (because there is no flaechenhaft sediment input)
del specific_Jahr_list[14]
print(len(specific_Jahr_list))
#sum up all sediment input rasters in folders with paths like e.g. ...\Frienisberg_1997_2007\Erosionsform\linear or flaechenhaft\Einzel
for SE_ras in specific_Jahr_list:
# set output Cooridnate-System -> otherwise, after NumpyToRaster Conversion, the Coordinate System is "unknown"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("CH1903 LV03")
arcpy.env.workspace = SE_ras
rasterList = arcpy.ListRasters()
# pick first raster of list and extract location information out of it
locate_raster = Raster(rasterList[0])
# get lowerLeft and cellSize
lowerLeft = arcpy.Point(locate_raster.extent.XMin, locate_raster.extent.YMin)
cellSize = locate_raster.meanCellWidth
i = 0
for ras in rasterList:
# sum up all rasters [t/ha] of e.g. .../Frienisberg_1997_2007/Jahr/e.g.1998/Einzel after conversion to numpy array
arr = arcpy.RasterToNumPyArray(ras)
arr = arr.astype(float)
if i == 0:
outras1 = arr
i += 1
else:
outras1 = outras1 + arr
i += 1
# convert numpy array back to raster
sum_ras = arcpy.NumPyArrayToRaster(outras1, lowerLeft, cellSize, cellSize)
##########set the new_basename e.g. sum_Frienisberg_97_07_linear(or flaechenhaft)_SE.tif ################
# extract out of the path e.g .../Frienisberg_1997_2007/Erosionsform/linear/Einzel the string Frienisberg_97_07
gebperi_name = SE_ras.split(os.sep)[6]
gebperi_name = gebperi_name.split('_')[0] + "_" + gebperi_name[-7:-5] + "_" + gebperi_name[-2:]
# extract out of the path e.g. .../Frienisberg_1997_2007/Erosionsform/linear/Einzel the linear (or flaechenhaft)-string
ero_Name = SE_ras.split(os.sep)[8]
# set new_basename for output path
newbase_name = "sum_" + gebperi_name + "_"+ ero_Name + "_SE.tif"
print(newbase_name)
############# copy sum_raster to the destination path ###############
# get from path e.g. 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Erosionsform\\linear or flaechenhaft\\Einzel'
# the part 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Erosionsform\\linear or flaechenhaft'
# and add //Summe to the extracted path
destination_path = os.path.dirname(SE_ras)
destination_path = os.path.join(destination_path, "Summe", newbase_name)
print(destination_path)
# save sum_ras in the destination path if the destination file doesn't already exist
if os.path.exists(destination_path):
print("file already exists")
else:
sum_ras.save(destination_path)
print(newbase_name + " was copied to " + destination_path)
####tree branch Parzelle
############################ copy sediment input rasters to .../Parzelle/Parzellen-Nr, e.g.01/Einzel ############
# make a list that consists of paths of SE_vectors_copies-subfolders
count = 0
for SE_vec in os.listdir(SE_vec_copied):
path_origin = os.path.join(SE_vec_copied, SE_vec)
if count == 0:
origin_list = [] + [path_origin]
count += 1
else:
origin_list.append(path_origin)
print(origin_list)
count = 0
#make a list of raster-folders path e.g. ...output_GE/Frienisberg_07_17_GE (sed_inp_dir has to be in Console)
for sed_geb in os.listdir(sed_inp_dir):
path_origin = os.path.join(sed_inp_dir,sed_geb)
if count == 0:
sed_inp_list = [] + [path_origin]
count += 1
else:
sed_inp_list.append(path_origin)
print(sed_inp_list)
for se in sed_inp_list:
for vec_gb_p in origin_list:
# list vectors in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\SE_vectors_copied\Frienisberg_07_17
arcpy.env.workspace = vec_gb_p
vec_List = arcpy.ListFeatureClasses()
# list rasters in e.g. E:\David_Remund_Masterarbeit\alle_GE_MA\output_GE\Frienisberg_07_17_GE
arcpy.env.workspace = se
se_List = arcpy.ListRasters()
# e.g. the event FEKRE31_M2_024_SE.tif is ONLY unique in a specific region -> the event FEKRE31_M2_024_SE.tif
# exists in Frienisberg_97_07 AND in Lobisgen_97_07
#
#therefore only copy a sediment input raster if the raster is in the same region as the correpsonding vector
if os.path.basename(se[:-3]) == os.path.basename(vec_gb_p):
for ras in se_List:
for vec in vec_List:
if ras[:-7] == vec[:-4]:
# extract Parzellen-Nr. form pandas dataframe and convert it to str
tab = build_df_from_arcpy_181119.build_df_from_arcpy(os.path.join(vec_gb_p, vec))
Parzellen_Nr = tab["Parzelle"][0]
Parzellen_Nr = str(Parzellen_Nr)
#there are 2 events that have field number 3536 and 4041 -> therefore: when Parzellen_Nr
#consists of 4 digits, only take the first 2 digits
if len(Parzellen_Nr) == 2:
Parzellen_Nr = Parzellen_Nr
if len(Parzellen_Nr) == 4:
Parzellen_Nr = Parzellen_Nr[:-2]
#write e.g. field number 1 as 01
Parzellen_Nr = Parzellen_Nr.zfill(2)
# copy the corresponding raster to new directory: e.g.
# E:\David_Remund_Masterarbeit\alle_GE_MA\Gewässereintrag\Sedimenteintrag\Frienisberg\Frienisberg_1997_2007\Parzelle\e.g. 01\Einzel
if os.path.basename(vec_gb_p)[-5:] == "07_17":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_2007_2017", "Parzelle",
"Parzelle_"+Parzellen_Nr,
"Einzel")
if os.path.basename(vec_gb_p)[-5:] == "97_07":
destination_path = os.path.join(Gebiete, os.path.basename(vec_gb_p)[:-6],
os.path.basename(vec_gb_p)[:-6] + "_1997_2007", "Parzelle",
"Parzelle_" + Parzellen_Nr,
"Einzel")
# copy the sediment input raster (ras) to the destination path folder
if os.path.exists(os.path.join(destination_path, ras)):
print("file already exists") # file can also be already copied in the same RUN session
else:
arcpy.Copy_management(ras, os.path.join(destination_path, ras))
print(ras + " copied to " + os.path.join(destination_path, ras))
############################ calculate Parzelle-sum & copy sum rasters to
############################ .../Parzelle/Parzellen-Nr, e.g.01/Summe #########
#make a list of paths like e.g. ...\Frienisberg_1997_2007\Parzelle\e.g.01\Einzel
count = 0
for GEB in os.listdir(Gebiete):
sub_GEB = os.path.join(Gebiete, GEB)
for sub_geb in os.listdir(sub_GEB):
Jahr_path = os.path.join(Gebiete,GEB,sub_geb,"Parzelle")
for Parz in os.listdir(Jahr_path):
parz_nr_path = os.path.join(Gebiete,GEB,sub_geb,"Parzelle",Parz,"Einzel")
if count == 0:
parz_nr_list = [] + [parz_nr_path]
count += 1
else:
parz_nr_list.append(parz_nr_path)
print(parz_nr_list)
#sum up all sediment input rasters in folders with paths like e.g. ...\Frienisberg_1997_2007\Erosionsform\linear or flaechenhaft\Einzel
for SE_ras in parz_nr_list:
# set output Cooridnate-System -> otherwise, after NumpyToRaster Conversion, the Coordinate System is "unknown"
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("CH1903 LV03")
arcpy.env.workspace = SE_ras
rasterList = arcpy.ListRasters()
# pick first raster of list and extract location information out of it
locate_raster = Raster(rasterList[0])
# get lowerLeft and cellSize
lowerLeft = arcpy.Point(locate_raster.extent.XMin, locate_raster.extent.YMin)
cellSize = locate_raster.meanCellWidth
i = 0
for ras in rasterList:
# sum up all rasters [t/ha] of e.g. .../Frienisberg_1997_2007/Jahr/e.g.1998/Einzel after conversion to numpy array
arr = arcpy.RasterToNumPyArray(ras)
arr = arr.astype(float)
if i == 0:
outras1 = arr
i += 1
else:
outras1 = outras1 + arr
i += 1
# convert numpy array back to raster
sum_ras = arcpy.NumPyArrayToRaster(outras1, lowerLeft, cellSize, cellSize)
##########set the new_basename e.g. sum_Frienisberg_97_07_P01_SE.tif ################
# extract out of the path e.g .../Frienisberg_1997_2007/Erosionsform/linear/Einzel the string Frienisberg_97_07
gebperi_name = SE_ras.split(os.sep)[6]
gebperi_name = gebperi_name.split('_')[0] + "_" + gebperi_name[-7:-5] + "_" + gebperi_name[-2:]
# extract out of the path e.g. .../Frienisberg_1997_2007/Parzelle/Parzelle_01/Einzel the 01-string
parz_Name = (SE_ras.split(os.sep)[8])[-2:]
# set new_basename for output path
newbase_name = "sum_" + gebperi_name + "_P"+ parz_Name + "_SE.tif"
print(newbase_name)
############# copy sum_raster to the destination path ###############
# get from path e.g. 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Parzelle\\Parzelle_01\\Einzel'
# the part 'E:\\David_Remund_Masterarbeit\\alle_GE_MA\\Gewässereintrag\\Sedimenteintrag\\Frienisberg\\Frienisberg_1997_2007\\Parzelle\\Parzelle_01'
# and add //Summe to the extracted path
destination_path = os.path.dirname(SE_ras)
destination_path = os.path.join(destination_path, "Summe", newbase_name)
print(destination_path)
# save sum_ras in the destination path if the destination file doesn't already exist
if os.path.exists(destination_path):
print("file already exists")
else:
sum_ras.save(destination_path)
print(newbase_name + " was copied to " + destination_path)