computeLatestClearObservation.sh 2.54 KB
Newer Older
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
#!/bin/bash
#
# Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Let-it-snow (LIS)
#
#     https://gitlab.orfeo-toolbox.org/remote_modules/let-it-snow
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Script to make a composite of the latest clear-sky observations over the past nDays since now
# usage: ./computeLatestClearObservation.sh tileId inputDir outputDir nDays
# example: ./computeLatestClearObservation.sh 31TCH /work/OT/siaa/Theia/Neige/Theia/ /tmp/ 20
# output: geotiff file with colormap cyan=snow, grey=no-snow, white=cloud, black=no-data

27
# Tile ID (ex. 31TCH)
28
29
30
31
32
33
34
35
36
tileId=$1
# Input directory where to look for the products
inputDir=$2
# Output directory
outputDir=$3
# Number of days to go back
nDays=$4
# Today in yyyymmdd format
today=$(date +"%Y%m%d")
37
# Name of the output composite
38
outputFn="${outputDir}/LCO_${tileId}_${today}.tif"
39
# Temporary file
40
tmpFn="${outputDir}/tmp_${tileId}_${today}.tif"
41
# Get date list of the n latest days
42
dateList=$(for i in $(seq $nDays); do date -d "$today -$i day" +"%Y%m%d"; done)
43
# Search matching products in the tile folder and store in an array
44
productList=($(for d in ${dateList}; do find ${inputDir} -name "SENTINEL2[A,B]_${d}*${tileId}*SNW_R2.tif"; done))
45
# Test if the product list is empty
46
47
if [ -z "$productList" ]
then
48
    # No products available: exit
49
50
51
    echo "could not find products matching these dates: "$dateList
    exit 1
else
52
    # Load OTB
53
    module load otb/7.0
54
    # Number of products to be composited
55
    nProduct=${#productList[@]}
56
    # Init band math expression
57
    cmd=im1b1
58
    # Build band math expression
59
    for i in $(seq $((nProduct-1))); do cmd=$cmd" <= 100 ? im${i}b1 : im$((i+1))b1" ; done
60
    # Execute band math
61
    otbcli_BandMath -progress true -il ${productList[@]} -out "$tmpFn" uint8 -exp "$cmd"
62
    # Apply color table to composite
63
    otbcli_ColorMapping -progress true -in "$tmpFn" -out "$outputFn""?&gdal:co:COMPRESS=DEFLATE" uint8 -method.custom.lut "/work/OT/siaa/Theia/hpc_scripts/LIS_SEB_style_OTB.txt"
64
    # Delete temporary file
65
66
    rm "$tmpFn"
fi