-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_series_v2.py
255 lines (233 loc) · 11.5 KB
/
time_series_v2.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
import os
from os.path import *
from osgeo import gdal
import numpy as np
'''def do_raster_stats(array_stack,stat):
#print('Doing dstack')
#raster_stack = np.dstack(list_of_arrays)
if stat=='std':
print('calculating std raster')
out_array = np.nanstd(array_stack, axis=2)
elif stat=='mean':
print('calculating mean raster')
out_array = np.nanmean(array_stack, axis=2)
elif stat=='median':
print('calculating median raster')
out_array = np.nanmedian(array_stack, axis=2)
elif stat=='range':
print('calculating range raster')
max_array = np.nanmax(array_stack, axis=2)
min_array = np.nanmin(array_stack, axis=2)
out_array = max_array-min_array
else:
print('something wrong in do_raster_stats')
exit(10)
return(out_array)'''
def main():
work_dir = r'D:\s2_change\tiles'
gran = "T49QDD"
gran_dir = join(work_dir, gran)
time_series_dir = join(gran_dir, 'time_series')
time_stats_dir = join(gran_dir,'time_stats')
if exists(time_stats_dir) == False:
os.mkdir(time_stats_dir)
for stack in os.listdir(time_series_dir):
if join(time_series_dir, stack).endswith('stack.tif') or join(time_series_dir, stack).endswith('stack.vrt'):
bx_stack = join(time_series_dir, stack)
stat = bx_stack.split('_')[-2]
mean_raster = join(time_stats_dir, gran + "_" + stat + '_mean.tif')
median_raster = join(time_stats_dir, gran + "_" + stat + '_median.tif')
std_rastar = join(time_stats_dir, gran + "_" + stat + '_std.tif')
#range_raster = join(time_stats_dir, gran + "_" + stat + '_range.tif')
#output_path = join(temporal_slope_dir, outname)
if not exists(mean_raster):
ds = gdal.Open(bx_stack)
srs_prj = ds.GetProjection()
geoTransform = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
num_bands = ds.RasterCount
# setup output
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(mean_raster,
xsize,
ysize,
1,
gdal.GDT_Int16,
)
dst_ds.SetGeoTransform(geoTransform)
dst_ds.SetProjection(srs_prj)
dst_band = dst_ds.GetRasterBand(1)
dst_band.SetNoDataValue(-32768)
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
#both methods work! dstack, then axis=2, orrrr no dstack and jus[t take axis=0 for the list of arrays
#1#raster_stack = np.dstack(array_layers)
#2#arr_mean = np.nanmean(raster_stack, axis=2) # get mean array of all layers
arr_mean = np.nanmean(array_layers, axis=0) # get mean array of all layers
dst_band.WriteArray(arr_mean, x, y)
if not exists(median_raster):
ds = gdal.Open(bx_stack)
srs_prj = ds.GetProjection()
geoTransform = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
num_bands = ds.RasterCount
# setup output
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(median_raster,
xsize,
ysize,
1,
gdal.GDT_Int16,
)
dst_ds.SetGeoTransform(geoTransform)
dst_ds.SetProjection(srs_prj)
dst_band = dst_ds.GetRasterBand(1)
dst_band.SetNoDataValue(-32768)
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
#both methods work! dstack, then axis=2, orrrr no dstack and jus[t take axis=0 for the list of arrays
#1#raster_stack = np.dstack(array_layers)
#2#arr_mean = np.nanmean(raster_stack, axis=2) # get mean array of all layers
arr_med = np.nanmedian(array_layers, axis=0) # get mean array of all layers
dst_band.WriteArray(arr_med, x, y)
if not exists(std_rastar):
ds = gdal.Open(bx_stack)
srs_prj = ds.GetProjection()
geoTransform = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
num_bands = ds.RasterCount
# setup output
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(std_rastar,
xsize,
ysize,
1,
gdal.GDT_Int16,
)
dst_ds.SetGeoTransform(geoTransform)
dst_ds.SetProjection(srs_prj)
dst_band = dst_ds.GetRasterBand(1)
dst_band.SetNoDataValue(-32768)
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
#both methods work! dstack, then axis=2, orrrr no dstack and jus[t take axis=0 for the list of arrays
#1#raster_stack = np.dstack(array_layers)
#2#arr_mean = np.nanmean(raster_stack, axis=2) # get mean array of all layers
arr_std = np.nanstd(array_layers, axis=0) # get mean array of all layers
dst_band.WriteArray(arr_std, x, y)
if __name__ == "__main__":
main()