-
Notifications
You must be signed in to change notification settings - Fork 2
/
S3_proc.sh
executable file
·173 lines (156 loc) · 3.96 KB
/
S3_proc.sh
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
set -x
function print_usage() {
echo ""
echo "./S3_proc.sh -i inpath -o outpath -X file.xml [-h -v -t]"
echo " -i: Path to folder containing S3?_*_EFR_*_002.SEN3 (unzipped S3 EFR) files"
echo " -o: Path where to store ouput"
echo " -X: Specify XML file"
echo " -v: Print verbose messages during processing"
echo " -t: Print timing messages during processing"
echo " -h: print this help"
echo ""
}
red='\033[0;31m'
orange='\033[0;33m'
green='\033[0;32m'
nc='\033[0m' # No Color
log_info() { echo -e "${green}[$(date --iso-8601=seconds)] [INFO] ${@}${nc}"; }
log_warn() { echo -e "${orange}[$(date --iso-8601=seconds)] [WARN] ${@}${nc}"; }
log_err() { echo -e "${red}[$(date --iso-8601=seconds)] [ERR] ${@}${nc}" 1>&2; }
trap ctrl_c INT # trap ctrl-c and call ctrl_c()
ctrl_c() {
log_err "CTRL-C caught"
log_err "Removing gpt progress..."
[[ -d ${dest} ]] && (
cd ${dest}
rm *_x.tif
)
}
debug() { if [[ ${debug:-} == 1 ]]; then
log_warn "debug:"
echo $@
fi; }
readonly t_0=$(date +%s)
declare t_last=$(date +%s)
timing() {
if [[ ${timing:-} ]]; then
log_info "Timing..."
local t_now=$(date +%s)
echo " Time since start:" $((${t_now} - ${t_0}))"s"
echo " Time since last:" $((${t_now} - ${t_last}))"s"
t_last=$(date +%s)
fi
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help)
print_usage
exit 1
;;
-i)
inpath="$2"
shift # past argument
shift # past value
;;
-o)
outpath="$2"
shift
shift
;;
-X)
xml="$2"
shift
shift
;;
-v | --verbose)
verbose=1
set -o xtrace
shift
;;
-t)
timing=1
shift
;;
*) # unknown option
positional+=("$1") # save it in an array for later. Pass on to dsget.sh
shift
;;
esac
done
if [[ -z ${inpath:-} ]]; then
log_err "-i not set"
print_usage
exit 1
fi
if [[ -z ${outpath:-} ]]; then
log_err "-o not set"
print_usage
exit 1
fi
if [[ -z ${xml:-} ]]; then
log_err "-X not set"
print_usage
exit 1
fi
for folder in $(ls ${inpath} | grep S3._OL_1_EFR); do
olci_folder=$(basename "${folder}")
olci_dts=$(echo "${olci_folder}" | rev | cut -d_ -f11 | rev)
dest=${outpath}/${olci_dts}
# skipping if scene already processed
if [[ -d "${dest}" ]]; then
if [[ -f "${dest}/r_TOA_01.tif" ]]; then
log_warn "${dest} already exists, scene skipped"
continue
fi
fi
# find nearest SLSTR folder. Timestamp is same or next minute.
olci_date=${olci_dts:0:8}
olci_time=${olci_dts:9:4}
olci_time1=$(date -d "${olci_date} ${olci_time} + 1 minute" "+%H%M")
olci_time2=$(date -d "${olci_date} ${olci_time} - 1 minute" "+%H%M")
fileroot="S3._SL_1_RBT____........T" # grep for acquisition not ingest time
# pick first nearby slstr
slstr_folder=$(ls ${inpath} | grep -E "${fileroot}${olci_time}|${fileroot}${olci_time1}|${fileroot}${olci_time2}" | head -n1 || true)
if [[ -z ${slstr_folder} ]]; then
log_err "No nearby SLSTR scene found"
continue
fi
log_info "${olci_folder}"
log_info "${slstr_folder}"
log_info "Generating ${dest}"
mkdir -p "${dest}"
log_info "gpt: Start"
timing
[[ $(which gpt) ]] || (
log_err "gpt not found"
exit 1
)
LD_LIBRARY_PATH=. gpt ${xml} \
-POLCIsource="${inpath}/${olci_folder}" \
-PSLSTRsource="${inpath}/${slstr_folder}" \
-PtargetFolder="${dest}" \
-Dsnap.log.level=ERROR \
-e || (
log_err "gpt error"
exit 1
)
# -Ds3tbx.reader.olci.pixelGeoCoding=true \
# -Ds3tbx.reader.slstrl1b.pixelGeoCodings=true \
log_info "gpt: Finished"
# # Discard out bad folders (defined as size > 10 GB)
# (cd ${dest}/../; du -sm * | awk '$1 > 10000 {print $2}' | xargs rm -fr)
if [[ ! -d "${dest}" ]]; then continue; fi # if we removed the directory, break out of the loop
resize=1000
log_info "Resampling to ${resize} m resolution..."
log_info "Aligning SLSTR to OLCI..."
grass -c ./mask.tif ${dest}/G_align --exec ./G_align.sh ${dest} ./mask.tif ${resize}
(cd ${dest} && rm *_x.tif)
(cd ${dest} && rm -fR G_align)
done
log_info "Finished: ${outpath}"
timing