-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_bands_time_v2.py
155 lines (140 loc) · 6.26 KB
/
stack_bands_time_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
import os
from os.path import *
from osgeo import gdal
work_dir = r"E:\work\change_detect_solo"
gran = "T20LMR"
gran_dir = join(work_dir, gran)
time_series_dir = join(gran_dir, 'time_series')
if not os.path.exists(time_series_dir):
os.mkdir(time_series_dir)
print('creating tmp bands')
for month in os.listdir(gran_dir):
if len(month) == 6 and month.startswith('2'):
month_dir = join(gran_dir, month)
gran_month = gran + "_" + month
masked_tif = join(month_dir, gran_month + '_min_mosaic.tif')
if not exists(masked_tif):
print('no {}'.format(masked_tif))
exit(100)
b1_vrt = join(month_dir, gran + '_b1_tmp.vrt')
if not exists(b1_vrt):
print('creating tmp b1_vrt')
gdal.BuildVRT(b1_vrt, masked_tif, bandList='1', resolution='highest')
b2_vrt = join(month_dir, gran + '_b2_tmp.vrt')
if not exists(b2_vrt):
print('creating tmp b2_vrt')
gdal.BuildVRT(b2_vrt, masked_tif, bandList='2', resolution='highest')
b3_vrt = join(month_dir, gran + '_b3_tmp.vrt')
if not exists(b3_vrt):
print('creating tmp b3_vrt')
gdal.BuildVRT(b3_vrt, masked_tif, bandList='3', resolution='highest')
b4_vrt = join(month_dir, gran + '_b4_tmp.vrt')
if not exists(b4_vrt):
print('creating tmp b4_vrt')
gdal.BuildVRT(b4_vrt, masked_tif, bandList='4', resolution='highest')
b5_vrt = join(month_dir, gran + '_b5_tmp.vrt')
if not exists(b5_vrt):
print('creating tmp b5_vrt')
gdal.BuildVRT(b5_vrt, masked_tif, bandList='5', resolution='highest')
b6_vrt = join(month_dir, gran + '_b6_tmp.vrt')
if not exists(b6_vrt):
print('creating tmp b6_vrt')
gdal.BuildVRT(b6_vrt, masked_tif, bandList='6', resolution='highest')
# create lists
print('putting tmp_bands in lists')
band_1_list = []
band_2_list = []
band_3_list = []
band_4_list = []
band_5_list = []
band_6_list = []
for month in os.listdir(gran_dir):
if len(month) == 6 and month.startswith('2'):
for tmp in os.listdir(join(gran_dir, month)):
if tmp.endswith('b1_tmp.vrt'):
band_1_list.append(join(gran_dir, month, tmp))
if tmp.endswith('b2_tmp.vrt'):
band_2_list.append(join(gran_dir, month, tmp))
if tmp.endswith('b3_tmp.vrt'):
band_3_list.append(join(gran_dir, month, tmp))
if tmp.endswith('b4_tmp.vrt'):
band_4_list.append(join(gran_dir, month, tmp))
if tmp.endswith('b5_tmp.vrt'):
band_5_list.append(join(gran_dir, month, tmp))
if tmp.endswith('b6_tmp.vrt'):
band_6_list.append(join(gran_dir, month, tmp))
band_1_list.sort()
band_2_list.sort()
band_3_list.sort()
band_4_list.sort()
band_5_list.sort()
band_6_list.sort()
print('Done looping through months and creating lists')
b1_stack = join(time_series_dir, gran + "_b1_stack_tmp.vrt")
b2_stack = join(time_series_dir, gran + "_b2_stack_tmp.vrt")
b3_stack = join(time_series_dir, gran + "_b3_stack_tmp.vrt")
b4_stack = join(time_series_dir, gran + "_b4_stack_tmp.vrt")
b5_stack = join(time_series_dir, gran + "_b5_stack_tmp.vrt")
b6_stack = join(time_series_dir, gran + "_b6_stack_tmp.vrt")
# creating vrt band stacks for each band
print('create vrt band stacks (1 band for every month)')
if not exists(b1_stack):
print('stacking b1 blue')
gdal.BuildVRT(b1_stack, band_1_list, separate='separate', resolution='highest')
if not exists(b2_stack):
print('stacking b2 green')
gdal.BuildVRT(b2_stack, band_2_list, separate='separate', resolution='highest')
if not exists(b3_stack):
print('stacking b3 red')
gdal.BuildVRT(b3_stack, band_3_list, separate='separate', resolution='highest')
if not exists(b4_stack):
print('stacking b4 nir')
gdal.BuildVRT(b4_stack, band_4_list, separate='separate', resolution='highest')
if not exists(b5_stack):
print('stacking b5 swir 1')
gdal.BuildVRT(b5_stack, band_5_list, separate='separate', resolution='highest')
if not exists(b6_stack):
print('stacking b6 swir 2')
gdal.BuildVRT(b6_stack, band_6_list, separate='separate', resolution='highest')
# now create final tifs
print('gdal_translate to get tifs')
b1_stack_tif = join(time_series_dir, gran + "_b1_stack.tif")
b2_stack_tif = join(time_series_dir, gran + "_b2_stack.tif")
b3_stack_tif = join(time_series_dir, gran + "_b3_stack.tif")
b4_stack_tif = join(time_series_dir, gran + "_b4_stack.tif")
b5_stack_tif = join(time_series_dir, gran + "_b5_stack.tif")
b6_stack_tif = join(time_series_dir, gran + "_b6_stack.tif")
if not exists(b1_stack_tif):
print('translating b1')
gdal.Translate(b1_stack_tif, b1_stack, format='GTiff', outputType=gdal.GDT_Int16)
if not exists(b2_stack_tif):
print('translating b2')
gdal.Translate(b2_stack_tif, b2_stack, format='GTiff', outputType=gdal.GDT_Int16)
if not exists(b3_stack_tif):
print('translating b3')
gdal.Translate(b3_stack_tif, b3_stack, format='GTiff', outputType=gdal.GDT_Int16)
if not exists(b4_stack_tif):
print('translating b4')
gdal.Translate(b4_stack_tif, b4_stack, format='GTiff', outputType=gdal.GDT_Int16)
if not exists(b5_stack_tif):
print('translating b5')
gdal.Translate(b5_stack_tif, b5_stack, format='GTiff', outputType=gdal.GDT_Int16)
if not exists(b6_stack_tif):
print('translating b6')
gdal.Translate(b6_stack_tif, b6_stack, format='GTiff', outputType=gdal.GDT_Int16)
# done creating time series stacks of each band, probably not the most efficient
# now to go back and delete all the tmps
print('deleting all tmp files')
for month in os.listdir(gran_dir):
if len(month) == 8 and month.startswith('2'):
month_dir = join(gran_dir, month)
gran_month = gran + "_" + month
for tmp in os.listdir(month_dir):
if tmp.endswith('tmp.vrt'):
print('removing {}'.format(tmp))
os.remove(join(month_dir, tmp))
for tmp in os.listdir(time_series_dir):
if tmp.endswith('_tmp.vrt'):
print('removing {}'.format(tmp))
os.remove(join(time_series_dir, tmp))
print('all done!')