-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_shaixuan_img_alt.py
210 lines (199 loc) · 10.7 KB
/
main_shaixuan_img_alt.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
# This program is to obtain remote sensing images and satellite altimetry data paris
# within a certain number of days of date difference, the time interval we set is 7 days
import os
from dateutil.parser import parse
from matplotlib.pylab import date2num
import shutil
def ICESat2_filter(img_path, cegao_path):
# Image sign, used to identify the input image type, currently supports Landsat 7/8 and Sentinel 2
img_sign = os.path.basename(os.path.dirname(img_path)).split('_')[1]
print(img_sign,':','ICESat2')
# Create a folder. Save matching images
img_pipei_path = os.path.join(os.path.dirname(img_path), 'match_ICE2')
if os.path.exists(img_pipei_path):
shutil.rmtree(img_pipei_path)
if not os.path.exists(img_pipei_path):
os.makedirs(img_pipei_path)
# Create a folder to store the matching height measurement data
cegao_pipei_path = os.path.join(os.path.dirname(cegao_path), img_sign, 'match_ICE2')
if os.path.exists(cegao_pipei_path):
shutil.rmtree(cegao_pipei_path)
if not os.path.exists(cegao_pipei_path):
os.makedirs(cegao_pipei_path)
# Additional matching images (because the height measurement data is relatively small)
img_pipei_extra_path = os.path.join(os.path.dirname(img_path), 'extra_match_ICE2')
if os.path.exists(img_pipei_extra_path):
shutil.rmtree(img_pipei_extra_path)
if not os.path.exists(img_pipei_extra_path):
os.makedirs(img_pipei_extra_path)
# Obtain the name of the image and altimetry data (the time of data acquisition is in the name)
img_name_list = os.listdir(img_path)
cegao_name_list = os.listdir(cegao_path)
# Traverse. Based on the altimetry data, select the image data within 7 days of the difference
if img_sign != 'Sentinel2':
for cegao_name in cegao_name_list:
# Count. Used to identify additional matching images, count>1 means additional matching
count = 0
for img_name in img_name_list:
# print(cegao_name)
# Calculate the time interval between the two types of data
# (convert a number similar to 20211109 into a date format)
# GEE downloaded Landsat video, [12:20] is the date. ICESat altimetry data, [6:14] is the date
date_residual = date2num(parse(str(img_name[12:20]))) - date2num(parse(str(cegao_name[6:14])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[12:20] + '.tif'))
print('Redundant image data')
else:
shutil.copy(os.path.join(cegao_path, cegao_name), os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[12:20] + '.tif'))
print(cegao_name[6:14], img_name[12:20])
img_name_list.remove(img_name)
else:
for cegao_name in cegao_name_list:
count = 0
for img_name in img_name_list:
date_residual = date2num(parse(str(img_name[:8]))) - date2num(parse(str(cegao_name[6:14])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[:8] + '.tif'))
print('Redundant image data')
else:
shutil.copy(os.path.join(cegao_path, cegao_name), os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[:8] + '.tif'))
print(cegao_name[6:14], img_name[:8])
img_name_list.remove(img_name)
print('Data filtering is complete!')
def Sentinel3_filter(img_path, cegao_path):
img_sign = os.path.basename(os.path.dirname(img_path)).split('_')[1]
print(img_sign,':','Sentinel3')
img_pipei_path = os.path.join(os.path.dirname(img_path), 'match_ST3')
if os.path.exists(img_pipei_path):
shutil.rmtree(img_pipei_path)
if not os.path.exists(img_pipei_path):
os.makedirs(img_pipei_path)
cegao_pipei_path = os.path.join(os.path.dirname(cegao_path), img_sign, 'match_ST3')
if os.path.exists(cegao_pipei_path):
shutil.rmtree(cegao_pipei_path)
if not os.path.exists(cegao_pipei_path):
os.makedirs(cegao_pipei_path)
img_pipei_extra_path = os.path.join(os.path.dirname(img_path), 'extra_match_ST3')
# print(img_pipei_extra_path)
if os.path.exists(img_pipei_extra_path):
shutil.rmtree(img_pipei_extra_path)
if not os.path.exists(img_pipei_extra_path):
os.makedirs(img_pipei_extra_path)
img_name_list = os.listdir(img_path)
cegao_name_list = os.listdir(cegao_path)
if img_sign != 'Sentinel2':
for cegao_name in cegao_name_list:
count = 0
for img_name in img_name_list:
date_residual = date2num(parse(str(img_name[12:20]))) - date2num(parse(str(cegao_name[16:24])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[12:20] + '.tif'))
print('Redundant image data')
else:
print(img_name[12:20], cegao_name[16:24])
shutil.copytree(os.path.join(cegao_path, cegao_name),
os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[12:20] + '.tif'))
img_name_list.remove(img_name)
else:
for cegao_name in cegao_name_list:
count = 0
for img_name in img_name_list:
date_residual = date2num(parse(str(img_name[:8]))) - date2num(parse(str(cegao_name[16:24])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[:8] + '.tif'))
print('Redundant image data')
else:
shutil.copytree(os.path.join(cegao_path, cegao_name), os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[:8] + '.tif'))
print(img_name[:8], cegao_name[16:24])
img_name_list.remove(img_name)
print('Data filtering is complete!')
def CryoSat2_filter(img_path, cegao_path):
img_sign = os.path.basename(os.path.dirname(img_path)).split('_')[1]
print(img_sign,':','CryoSat2')
img_pipei_path = os.path.join(os.path.dirname(img_path), 'match_CS')
if os.path.exists(img_pipei_path):
shutil.rmtree(img_pipei_path)
if not os.path.exists(img_pipei_path):
os.makedirs(img_pipei_path)
cegao_pipei_path = os.path.join(os.path.dirname(cegao_path), img_sign, 'match_CS')
if os.path.exists(cegao_pipei_path):
shutil.rmtree(cegao_pipei_path)
if not os.path.exists(cegao_pipei_path):
os.makedirs(cegao_pipei_path)
img_pipei_extra_path = os.path.join(os.path.dirname(img_path), 'extra_match_CS')
if os.path.exists(img_pipei_extra_path):
shutil.rmtree(img_pipei_extra_path)
if not os.path.exists(img_pipei_extra_path):
os.makedirs(img_pipei_extra_path)
img_name_list = os.listdir(img_path)
cegao_name_list = os.listdir(cegao_path)
if img_sign != 'Sentinel2':
for cegao_name in cegao_name_list:
count = 0
for img_name in img_name_list:
date_residual = date2num(parse(str(img_name[12:20]))) - date2num(parse(str(cegao_name[19:27])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[12:20] + '.tif'))
print('Redundant image data')
else:
shutil.copy(os.path.join(cegao_path, cegao_name), os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[12:20] + '.tif'))
print(img_name[12:20], cegao_name[19:27])
img_name_list.remove(img_name)
else:
for cegao_name in cegao_name_list:
count = 0
for img_name in img_name_list:
date_residual = date2num(parse(str(img_name[:8]))) - date2num(parse(str(cegao_name[19:27])))
if abs(date_residual) < 7:
count += 1
if count > 1:
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_extra_path, img_name[:8] + '.tif'))
print('Redundant image data')
else:
shutil.copy(os.path.join(cegao_path, cegao_name), os.path.join(cegao_pipei_path, cegao_name))
shutil.copy(os.path.join(img_path, img_name),
os.path.join(img_pipei_path, img_name[:8] + '.tif'))
print(cegao_name[19:27], img_name[:8])
img_name_list.remove(img_name)
print('Data filtering is complete!')
def all_filter(img_path, cegao_path):
cegao_name_Sign = os.listdir(cegao_path)[0][0:2]
print('Screening images and altimetry data...')
if cegao_name_Sign == 'CS':
CryoSat2_filter(img_path, cegao_path)
elif cegao_name_Sign == 'AT':
ICESat2_filter(img_path, cegao_path)
elif cegao_name_Sign == 'S3':
Sentinel3_filter(img_path, cegao_path)
else:
print(cegao_name_Sign)
if __name__ == '__main__':
img_path = 'J:/Remote_Sensing_Image/Dabuxun/Dabuxun_Sentinel2/data'
cegao_path = 'J:/Satellite_Altimeter_Data/Dabuxun/Sentinel3/data'
all_filter(img_path, cegao_path)