-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharcPycor_dem2dem_standalone.py
227 lines (179 loc) · 7.91 KB
/
arcPycor_dem2dem_standalone.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
'''
pycor_dem2dem.py
Description: Coregister slave DEM to master DEM using Nuth-Kaeaeb algorithm.
Reference: Nuth, C. and Kaeaeb, A. (2011): http://doi.org/10.5194/tc-5-271-2011
Inputs:
(1) master DEM
(2) slave DEM
(3) polygon shapefile of stable terrain
Outputs:
(1) x_bin (.csv; aspect)
(2) y_bin (.csv; dh/tan(slope))
(3) sigma_bin (.csv; standard deviation of y)
(4) shiftVec txt (shift vector)
History:
* 2014-11-8, Created
* 2019-5-7, path management
* 2019-5-11, delete unused variables, optimize the codes for converging
* 2023-5-26, set snap raster, test on ArcMap 10.8
Author: Yuzhe Wang
E-mail: [email protected]
Affiliation: Colledge of Geography and Environment, Shandong Normal University
'''
import os
import time
import shutil
import numpy as np
from scipy.optimize import curve_fit
import arcpy
from arcpy import env
from arcpy.sa import Raster, Slope, Aspect, ExtractByMask, Plus
startTime = time.clock()
if arcpy.CheckExtension("spatial")=="Available":
arcpy.CheckOutExtension("spatial")
else:
raise LicenseError
# Set environment workspace in current directory
path_script = os.path.dirname(os.path.abspath(__file__))
env.workspace = os.path.join(path_script, 'benchmark_data')
# Folder for outputs
dirOutputs = os.path.join(env.workspace, 'outputs')
if os.path.exists(dirOutputs):
shutil.rmtree(dirOutputs)
os.makedirs(dirOutputs)
# Specify master DEM, slave DEM, stable terrain and corrected DEM
DEM_master = "DEM_master.tif" # Set your master DEM
DEM_slave = "DEM_slave1.tif" # Set your slave DEM that is the DEM needed to be coregistered
stable_terrain = "OffGlacier.shp" # Set the stable terrain assumed no elevation change
corrected_DEM = 'DEM_corrected.tif' # Set the output DEM
# Initializations
iteration = 0
DEM_slave_before = DEM_slave
DEM_slave_after = DEM_slave
result_mean_dh = [0]
result_std_dh = [0]
ShiftX = [0]
ShiftY = [0]
file_shiftVec = os.path.join(dirOutputs, "shiftVec" + ".csv")
# Define CosineFitting function
def CosineFitting(x, a, b, c):
return a*np.cos(b - np.pi/180*x) + c
while 1:
iteration += 1
print "--------------------------------------------------------------"
print "Iteration {0} is running!".format(iteration)
# DEM difference [m]
dh = Raster(DEM_master) - Raster(DEM_slave_after)
if iteration==1:
dh.save(os.path.join(dirOutputs, "dh_init"))
# slope of the slave DEM [degree]
slp = Slope(DEM_slave_after, "DEGREE", "1")
# aspect of the slave DEM [degree]
asp = Aspect(DEM_slave_after)
# Mask 'dh' using statale terrain polygon
dh_mask = ExtractByMask(dh, stable_terrain)
# Mask 'slp' and 'asp' using 'dh_mask' in order to keep same georeference as 'dh_mask'
env.snapRaster = dh_mask
slp_mask = ExtractByMask(slp, dh_mask)
asp_mask = ExtractByMask(asp, dh_mask)
env.snapRaster = None
# delete dh, slp, and asp after extraction
del dh, slp, asp
# Raster to Array
dh_mask_arr = arcpy.RasterToNumPyArray(dh_mask, nodata_to_value=-32768)
slp_mask_arr = arcpy.RasterToNumPyArray(slp_mask, nodata_to_value=-32768)
asp_mask_arr = arcpy.RasterToNumPyArray(asp_mask, nodata_to_value=-32768)
del dh_mask, slp_mask, asp_mask
# Criteria: |dh| < 70 m and 5 < slope < 45.
# slope>5 is cited from Purinton&Bookhagen, 2018, Earth Surface Dynamics.
# slope<45 is cited from Berthier et al., 2019, Journal of Glaciology.
index1 = np.where((dh_mask_arr>-70) & (dh_mask_arr<70) & (slp_mask_arr>5) & (slp_mask_arr<45))
dh_mask_arr1 = dh_mask_arr[index1[0], index1[1]]
slp_mask_arr1 = slp_mask_arr[index1[0], index1[1]]
asp_mask_arr1 = asp_mask_arr[index1[0], index1[1]]
del dh_mask_arr, slp_mask_arr, asp_mask_arr, index1
# statistics of "dh"
mean_dh_mask1 = np.mean(dh_mask_arr1)
result_mean_dh.append(mean_dh_mask1)
std_dh_mask1 = np.std(dh_mask_arr1)
result_std_dh.append(std_dh_mask1)
print "MEAN(dh) of iteration {0}: {1:.1f}".format(iteration, mean_dh_mask1)
print "STD(dh) of iteration {0}: {1:.1f}".format(iteration, std_dh_mask1)
# Prepare the x and y values for curve fitting
x = asp_mask_arr1
y = dh_mask_arr1 / np.tan(np.pi*slp_mask_arr1/180)
del dh_mask_arr1, slp_mask_arr1, asp_mask_arr1
# Get the x, y values in bins
range_asp = range(0, 370, 10)
n = len(range_asp) - 1
x_bin = np.zeros(n)
y_bin = np.zeros(n)
sigma_bin = np.zeros(n)
for i in range(n):
index2 = np.where((x >= range_asp[i]) & (x < range_asp[i+1]))
x_bin[i] = range_asp[i] + 5
y_bin[i] = np.median(y[index2])
sigma_bin[i] = np.std(y[index2])
# Save the results (x, y, x_bin, y_bin, sigma_bin) in csv format
file_x = os.path.join(dirOutputs, "x" + str(iteration) + '.csv')
file_y = os.path.join(dirOutputs, "y" + str(iteration) + '.csv')
file_x_bin = os.path.join(dirOutputs, "x_bin" + str(iteration) + '.csv')
file_y_bin = os.path.join(dirOutputs, "y_bin" + str(iteration) + '.csv')
file_sigma_bin = os.path.join(dirOutputs, "sigma_bin" + str(iteration) + '.csv')
np.savetxt(file_x, x, delimiter=',')
np.savetxt(file_y, y, delimiter=',')
np.savetxt(file_x_bin, x_bin, delimiter=',')
np.savetxt(file_y_bin, y_bin, delimiter=',')
np.savetxt(file_sigma_bin, sigma_bin, delimiter=',')
del x, y
# curve fitting
p0 = [(np.max(y_bin) - np.min(y_bin))/2, 0.7, 0.4]
popt, pcov = curve_fit(CosineFitting, x_bin, y_bin, p0=p0)
ShiftX1 = popt[0]*np.sin(popt[1])
ShiftY1 = popt[0]*np.cos(popt[1])
ShiftX.append(ShiftX1)
ShiftY.append(ShiftY1)
del x_bin, y_bin
print "Parameter a of iteration {0}: {1:.1f}".format(iteration, popt[0])
print "Parameter b of iteration {0}: {1:.1f}".format(iteration, popt[1])
print "Parameter c of iteration {0}: {1:.1f}".format(iteration, popt[2])
print "Shift vector X of iteration {0}: {1:.1f}".format(iteration, ShiftX1)
print "Shift vector Y of iteration {0}: {1:.1f}".format(iteration, ShiftY1)
# Solve for parameters (a, b and c) iteratively until the improvement of std less than 2%
if iteration>1:
logic1 = abs(result_std_dh[iteration]) < 0.1
logic2 = abs(result_std_dh[iteration]) <= abs(result_std_dh[iteration-1])
logic3 = abs((result_std_dh[iteration-1] - result_std_dh[iteration])/(result_std_dh[iteration-1]+1e-4)) < 0.02
logic4 = logic2 and logic3
logic5 = iteration>=7
if logic1 or logic4 or logic5:
sum_ShiftX = np.sum(ShiftX)
sum_ShiftY = np.sum(ShiftY)
# final shift vector [unit: m]
shiftVec = [sum_ShiftX, sum_ShiftY, round(result_mean_dh[iteration-1],1)]
np.savetxt(file_shiftVec, shiftVec, delimiter=',')
print "********************Final Result********************"
print "Note: results are saved in {0}".format(dirOutputs)
print "The final shift X: {0:.1f}".format(sum_ShiftX)
print "The final shift Y: {0:.1f}".format(sum_ShiftY)
break
# Shift the slave DEM
DEM_slave_after = "DEM_shift" + str(iteration)
if arcpy.Exists(DEM_slave_after):
arcpy.Delete_management(DEM_slave_after)
arcpy.Shift_management(DEM_slave_before, DEM_slave_after, str(ShiftX1), str(ShiftY1))
DEM_slave_before = DEM_slave_after
# correct the shifted slave DEM
final_shifted_slave_dem = Raster("DEM_shift" + str(iteration-1))
mean_dh_offglacier = round(result_mean_dh[iteration-1], 1)
DEM_slave_correct = Plus(final_shifted_slave_dem, mean_dh_offglacier)
# save corrected slave DEM
if arcpy.Exists(corrected_DEM):
arcpy.Delete_management(corrected_DEM)
DEM_slave_correct.save(corrected_DEM)
print "The final shifted DEM: " + corrected_DEM
# delete shifted DEMs
for i in range(1,iteration):
arcpy.Delete_management("DEM_shift" + str(i))
endTime = time.clock()
print "Running time: {0} s".format(int(endTime-startTime))