-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemporal_slope_v3.py
210 lines (196 loc) · 9.1 KB
/
temporal_slope_v3.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
import os
from os.path import *
from osgeo import gdal
import numpy as np
import time
work_dir = r'E:\work\change_detect_solo'
gran = "T18SUJ"
gran_dir = join(work_dir, gran)
time_series_dir = join(gran_dir, 'time_series')
temporal_slope_dir = join(gran_dir, 'temp_slope')
if not exists(temporal_slope_dir):
os.mkdir(temporal_slope_dir)
start_time = time.time()
###Set up y variable for linear regression
for stack in os.listdir(time_series_dir):
if join(time_series_dir,stack).endswith('stack.tif'):#'stack.tif'):
bx_stack = join(time_series_dir,stack)
outname = stack[:-4] + "tempslope.tif"#"tempslope.tif"
output_path = join(temporal_slope_dir, outname)
if not exists(output_path):
ds = gdal.Open(bx_stack)
srs_prj = ds.GetProjection()
geoTransform = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
num_bands = ds.RasterCount
#setup x axis(time)
xi = np.arange(1, num_bands + 1)
xi_mean = np.mean(xi)
xi_x = xi - xi_mean # "time" each input date
xi_x2 = xi_x * xi_x # squared
sum_x2 = np.sum(xi_x2)
# setup output
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(output_path,
xsize,
ysize,
1,
gdal.GDT_Int16,
)
dst_ds.SetGeoTransform(geoTransform)
dst_ds.SetProjection(srs_prj)
dst_band = dst_ds.GetRasterBand(1)
dst_band.SetNoDataValue(-9999)
print('chipping')
block_xsize = 512
block_ysize = 512
minx = geoTransform[0]
miny = geoTransform[3]
step_x = geoTransform[1]
step_y = geoTransform[5]
count_x = 0
count_y = 0
for x in range(0, xsize, block_xsize):
#print(f'block x: {x}')
if x + block_xsize < xsize:
cols = block_xsize
x_off = minx + (count_x * step_x)
count_x = count_x + block_xsize
else:
cols = xsize - x
x_off = minx + (count_x * step_x)
count_x = count_x + x
count_y = 0
for y in range(0, ysize, block_ysize):
print(f'block xy: {x} {y}')
if y + block_ysize < ysize:
rows = block_ysize
y_off = miny + (count_y * step_y)
count_y = count_y + block_ysize
else:
rows = ysize - y
y_off = miny + (count_y * step_y)
count_y = count_y + y
# this is where calculations/data maniuplations happen
#print(f'Going through stack \n{bx_stack}')
array_layers = []
for i in range(1, num_bands+1):
#print("layer number: {}".format(i))
band = ds.GetRasterBand(i).ReadAsArray(x,y,cols,rows).astype('float32')
arr = np.where(band <= 0, np.nan, band)
array_layers.append(arr)
#get avg for all arrays
#arr_mean = np.nanmean(array_layers, axis=0) # get mean array of all layers
med_arr = np.nanmedian(array_layers, axis=0)
std_arr = np.nanstd(array_layers, axis=0)
std_arrX2 = std_arr * 2
#y = a + bx
for k in range(len(array_layers)):
#array_layers[k][np.isnan(array_layers[k])] = arr_mean[np.isnan(array_layers[k])]
filter = np.where(array_layers[k] - med_arr > std_arrX2, med_arr, array_layers[k])#remove outliers
filter[np.isnan(array_layers[k])] = med_arr[np.isnan(array_layers[k])]
yi_y = filter - med_arr
x_y = yi_y * xi_x[k] # (xi-x)*(yi-y)
# need sum...
if k == 0:
sum_xy = x_y
else:
sum_xy = x_y + sum_xy
# y=a+bx
outSlope = sum_xy / sum_x2 # slope
# a = arr_mean - b*(num_bands) #intercept
dst_band.WriteArray(outSlope, x, y)
print(f'done with {bx_stack}\n{output_path} should be done')
print("--- %s seconds ---" % (time.time() - start_time))
start_time2 = time.time()
if join(time_series_dir, stack).endswith('stack.vrt'):
bx_stack = join(time_series_dir, stack)
outname = stack[:-4] + "tempslope.tif"
output_path = join(temporal_slope_dir, outname)
if not exists(output_path):
ds = gdal.Open(bx_stack)
srs_prj = ds.GetProjection()
geoTransform = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
num_bands = ds.RasterCount
# setup x axis(time)
xi = np.arange(1, num_bands + 1)
xi_mean = np.mean(xi)
xi_x = xi - xi_mean # "time" each input date
xi_x2 = xi_x * xi_x # squared
sum_x2 = np.sum(xi_x2)
# setup output
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(output_path,
xsize,
ysize,
1,
gdal.GDT_Int16,
)
dst_ds.SetGeoTransform(geoTransform)
dst_ds.SetProjection(srs_prj)
dst_band = dst_ds.GetRasterBand(1)
dst_band.SetNoDataValue(-9999)
print('chipping')
block_xsize = 512
block_ysize = 512
minx = geoTransform[0]
miny = geoTransform[3]
step_x = geoTransform[1]
step_y = geoTransform[5]
count_x = 0
count_y = 0
for x in range(0, xsize, block_xsize):
if x + block_xsize < xsize:
cols = block_xsize
x_off = minx + (count_x * step_x)
count_x = count_x + block_xsize
else:
cols = xsize - x
x_off = minx + (count_x * step_x)
count_x = count_x + x
count_y = 0
for y in range(0, ysize, block_ysize):
print(f'block xy: {x} {y}')
if y + block_ysize < ysize:
rows = block_ysize
y_off = miny + (count_y * step_y)
count_y = count_y + block_ysize
else:
rows = ysize - y
y_off = miny + (count_y * step_y)
count_y = count_y + y
# this is where calculations/data maniuplations happen
#print(f'Going through stack \n{bx_stack}')
array_layers = []
for i in range(1, num_bands + 1):
# print("layer number: {}".format(i))
band = ds.GetRasterBand(i).ReadAsArray(x, y, cols, rows).astype('float32')
arr = np.where(band == -9999, np.nan, band)
array_layers.append(arr)
# get avg for all arrays
med_arr = np.nanmedian(array_layers, axis=0)
std_arr = np.nanstd(array_layers, axis=0)
std_arrX2 = std_arr * 2
# y = a + bx
for k in range(len(array_layers)):
# array_layers[k][np.isnan(array_layers[k])] = arr_mean[np.isnan(array_layers[k])]
filter = np.where(array_layers[k] - med_arr > std_arrX2, med_arr,
array_layers[k]) # remove outliers
filter[np.isnan(array_layers[k])] = med_arr[np.isnan(array_layers[k])]
yi_y = filter - med_arr
x_y = yi_y * xi_x[k] # (xi-x)*(yi-y)
# need sum...
# need sum...
if k == 0:
sum_xy = x_y
else:
sum_xy = x_y + sum_xy
# y=a+bx
outSlope = sum_xy / sum_x2 # slope
# a = arr_mean - b*(num_bands) #intercept
dst_band.WriteArray(outSlope, x, y)
print(f'done with {bx_stack}\n{output_path} should be done')
print("--- %s seconds ---" % (time.time() - start_time2))