-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinel_slc_renamer
318 lines (296 loc) · 11.7 KB
/
sentinel_slc_renamer
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# Joaquín Escayo 2019 ([email protected])
# V 1.0 - Initial version, lot to re-do but works as intended. Not fool-proof, be careful with the inputs. (22/11/2017)
# V 1.1 - Some bugfixes. Added the option to remove VH polarization. (1/3/2018)
# V 1.2 - Big update: inteligent ZIP extraction keeping only selected polarizations. (27/10/2019)
# V 1.3 - Check for existing folders. (28/10/2019)
# V 1.4 - Fixed a bug that prevents to extract correctly the zip and saves the unzip log to the image folder (29/10/2019)
# TODO:
# A proper report file with details of every file decompressed extracted from manifest.safe (like geometry, relative orbit, etc).
# Check correct ZIP extraction (should be easy)
#########################
# SOME USEFUL FUNCTIONS #
#########################
containsElement () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1;} # Function to check if an element is part of an array
sentinel_unzip () { # Function to decompress Sentinel Image, uses 2 arguments (input file and output folder)
if [[ -z $1 ]] || [[ -z $2 ]]; then echo "You must specify file and output folder"; exit; fi
unzip -d $2 $1 &> $2/unzip.log
mv $2/*SAFE/* $2/
}
sentinel_unzip_pol () { # Function to decompress Sentinel Image, uses 3 arguments (input file, output folder and pol channel in that order)
if [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]]; then echo "You must specify file, output folder and pol channel"; exit; fi
unzip -d $2 $1 S1*/support/* S1*/preview/* S1*/measurement/*$3* S1*/annotation/*$3* S1*/S1* S1*/manifest.safe &> $2/unzip.log
mv $2/*SAFE/* $2/
}
lowercase () {
echo $1 | tr '[:upper:]' '[:lower:]'
}
#########################
# DIRECTORIES CHECK #
#########################
# Check of the input variable (directory)
if [ -z "$1" ]; then
echo "############# ERROR ###################################"
echo "No SLC directory used as input"
echo "Use slc_image_renamer.sh slc_directory"
echo "If you want to use current directory as input:"
echo "slc_image_renamer.sh ."
echo "Do not use special characters in the route (as spaces)"
echo "#######################################################"
exit
elif [ $1 == "." ]; then
DIR=$(pwd)
#checkign for absolute or relative path
elif [[ $(expr substr $1 1 1) == "/" ]]; then # absolute path as input
DIR=$1
else # relative path
DIR=$(pwd)/$1
fi
############################
# Variables initialization #
############################
totalno=0
totalsi=0
REPORT=$DIR/files
rm $REPORT &> /dev/null
MSLICES=() # Array to index multiple slices dates
touch $REPORT
echo "Generating list of files"
for i in $(find $DIR \( -name "S1A_*.zip" -or -name "S1B_*.zip" \)); do
SAT=$(basename $i | cut -d'_' -f1)
MODE=$(basename $i | cut -d'_' -f2)
TYPE=$(basename $i | cut -d'_' -f3)
POL=$(basename $i | cut -d'_' -f5)
DATE_TIME=$(basename $i | cut -d'_' -f6)
DATE=$(basename $i | cut -d'_' -f6 | cut -c1-8)
TIME=$(basename $i | cut -d'_' -f6 | cut -c10-15)
ID=$(basename $i | cut -d'_' -f10 | cut -d'.' -f1)
echo "$i,$(basename $i),$SAT,$MODE,$TYPE,$POL,$DATE_TIME,$DATE,$TIME,$ID" >> $REPORT
done
NIMAGES=$(cat $REPORT | wc -l)
if [ $(cat $REPORT | wc -l) == 0 ]; then # Check that at least one image is found
echo "No images found, please make sure that the"
echo "directory contains at least one SLC image"
exit
fi
sort --field-separator=',' --key=6 $REPORT -o $REPORT # sort file list by time and date and save in the same file
echo "The following files has been found:"
awk -F',' '{print $2}' $REPORT
#sed -i '1s/^/Full Path,File name,Satellite,Acquisition Mode,Processing level,Polarimetry,Date,Time,ID\n/' $REPORT
echo "Found $NIMAGES Sentinel-1 files in the input directory"
# Checking Polarisation on detected files
POL_TYPES=$(cut -d , -f6 $REPORT | sort | uniq)
POL_TYPES_NUM=$(cut -d , -f6 $REPORT | sort | uniq | wc -l)
# Different cases to address
if [ $POL_TYPES_NUM == "1" ]; then
# Most common cases
echo "One polarisation channel found in all images"
case $POL_TYPES in
1SSV)
echo "All files has single VV polarisation"
;;
1SSH)
echo "All files has single HH polarisation"
;;
1SDH)
echo "All files has dual HH+HV polarisation"
while [ true ]; do
echo "Select if you want to extract both polarisation channels or specify which one"
read -r -p "Enter HH, HV or both: " respuesta
respuesta=$(lowercase $respuesta)
case $respuesta in
hh)
echo "Extracting only HH channel"
POLI=SINGLE
CHANNEL=hh
break
;;
hv)
echo "Extracting only HV channel"
POLI=SINGLE
CHANNEL=hv
;;
both)
echo "Extracting both channels"
POLI=DUAL
break
;;
*)
echo "Posible options are HH, HV or both"
;;
esac
done
;;
1SDV)
echo "All files has dual VV+VH polarisation"
while [ true ]; do
echo "Select if you want to extract both polarisation channels or specify which one"
read -r -p "Enter VV, VH or both: " respuesta
respuesta=$(lowercase $respuesta)
case $respuesta in
vv)
echo "Extracting only VV channel"
POLI=SINGLE
CHANNEL=vv
break
;;
vh)
echo "Extracting only VH channel"
POLI=SINGLE
CHANNEL=vh
;;
both)
echo "Extracting both channels"
POLI=DUAL
break
;;
*)
echo "Posible options are HH, HV or both"
;;
esac
done
;;
*)
echo "Something went wrong"
;;
esac
else
echo "Multiple polarisation types detected"
case $POL_TYPES_NUM in
2)
echo "Two different polarisations detected: $POL_TYPES"
case $POL_TYPES in
"1SDH 1SSH")
echo "##############################################################"
echo "# WARNING #"
echo "##############################################################"
echo " Found files with HH and HH+HV."
echo " Only common polarisation will be extracted (HH)"
echo "##############################################################"
POLI=SINGLE
CHANNEL=hh
;;
"1SDV 1SSV")
echo "##############################################################"
echo "# WARNING #"
echo "##############################################################"
echo " Found files with VV and VV+VH."
echo " Only common polarisation will be extracted (VV)"
echo "##############################################################"
POLI=SINGLE
CHANNEL=vv
;;
*)
echo "Two different polarisations detected: $POL_TYPES with no common channel"
echo "##############################################################"
echo "# WARNING #"
echo "##############################################################"
echo " MULTIPLE POLARISATION MODES DETECTED"
echo " ALL WILL BE EXTRACTED, THIS CAN CAUSE PROBLEMS"
echo " IN INSAR PROCESSINGS"
echo " CHECK FILES AND REPORT WHEN FINISH"
echo "##############################################################"
;;
esac
;;
3)
echo "Four different polarisations detected: $POL_TYPES"
echo "##############################################################"
echo "# WARNING #"
echo "##############################################################"
echo " MULTIPLE POLARISATION MODES DETECTED"
echo " ALL WILL BE EXTRACTED, THIS CAN CAUSE PROBLEMS"
echo " IN INSAR PROCESSINGS"
echo " CHECK FILES AND REPORT WHEN FINISH"
;;
4)
echo "Four different polarisations detected: $POL_TYPES"
echo "##############################################################"
echo "# WARNING #"
echo "##############################################################"
echo " MULTIPLE POLARISATION MODES DETECTED"
echo " ALL WILL BE EXTRACTED, THIS CAN CAUSE PROBLEMS"
echo " IN INSAR PROCESSINGS"
echo " CHECK FILES AND REPORT WHEN FINISH"
esac
fi
read -n1 -r -p "Press any key to continue..." key
#############################
# Start to processing images#
#############################
for i in $(cat "files"); do
DATE=$(echo $i | awk -F',' '{print $8}')
DATE_TIME=$(echo $i | awk -F',' '{print $7}')
N_IMAGES=$(grep $DATE $REPORT | wc -l)
if [[ $(containsElement $DATE ${MSLICES[@]}; echo $?) == 1 ]];then # Check if it is a multiple slice image if it is a multiple slice file, skip it
echo "Satellite image: $(basename $(echo $i | awk -F',' '{print $1}'))"
POL=$(echo $i | awk -F',' '{print $6}' | cut -c3-4)
if [ $POL == "SV" ] || [ $POL == "SH" ] || [ $POLI == "SINGLE" ]; then
CPTDIR=$DIR/$DATE.sen1
elif [ $POLI == "DUAL" ]; then
CPTDIR=$DIR/$DATE.sen1.dp
elif [ $POL = "DV" ] || [ $POL == "DH" ]; then
CPTDIR=$DIR/$DATE.sen1.dp
fi
########################
# SINGLE IMAGE FOUND #
########################
if [ $N_IMAGES == 1 ]; then
FILE=$(echo $i | awk -F',' '{print $1}')
echo "Processing image $(echo $totalsi+1 | bc) of $NIMAGES"
echo "One image found for $DATE"
echo "Ouput folder: $CPTDIR"
# check if directory exists
if [ ! -d $CPTDIR ]; then
mkdir $CPTDIR
if [ $POL == "SV" ] || [ $POL == "SH" ] || [ $POLI = "DUAL" ]; then
sentinel_unzip $FILE $CPTDIR
elif [ $POLI == "SINGLE" ]; then
sentinel_unzip_pol $FILE $CPTDIR $CHANNEL
fi
totalsi=$((totalsi+1))
else
echo "Directory already exists, check that the image is already decompressed"
fi
echo "-------------------------------------------------------------------------"
########################
#MULTIPLE IMAGES FOUND #
########################
else
echo "Processing images $(echo $totalsi+1 | bc) to $(echo $totalsi+$N_IMAGES | bc) of the total $NIMAGES images"
echo "$DATE appears to have more than one slices"
MSLICES+=($DATE)
counter=1
if [ ! -d $CPTDIR ]; then
mkdir $CPTDIR
for i in $(grep $DATE $REPORT);do
FILE=$(echo $i | awk -F',' '{print $1}')
DATE_TIME=$(echo $i | awk -F',' '{print $7}')
echo "Processing $DATE_TIME slice #$counter"
echo "Output folder: $CPTDIR/slice$counter"
mkdir $CPTDIR/slice$counter
if [ $POL == "SV" ] || [ $POL == "SH" ] || [ $POLI = "DUAL" ]; then
sentinel_unzip $FILE $CPTDIR/slice$counter
elif [ $POLI == "SINGLE" ]; then
sentinel_unzip_pol $FILE $CPTDIR/slice$counter $CHANNEL
fi
counter=$((counter+1))
totalsi=$((totalsi+1))
done
else
echo "Directory already exists, check that the image is already decompressed"
fi
echo "-------------------------------------------------------------------------"
fi
fi
done
echo "A total of $totalsi SLC images has been processed"
if [ $totalsi == $(cat $REPORT | wc -l) ]; then
echo "All images has been processed"
else
echo "Some images has been not processed, please check"
fi
########################
# Clean temp files #
########################
#rm $REPORT