process-LCtgz.sh 1.4 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
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
#!/usr/bin/env bash
# https://www.mapbox.com/blog/processing-landsat-8/
# http://landsat.usgs.gov/Landsat8_Using_Product.php
#
# landsat-8 tifs are "digital numbers" (counts),
# maybe it will help if we create top-of-atmosphere reflectance (or radiance)?
# the MTL file has scaling factors:
# LC80230282015349LGN00_MTL.txt

set -u
set -e

BANDS=(1 2 3 4 5 6 7 8 9 10 11)

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/consts.sh"

LOG=${LOGS}/process-LCtgz.log
exec >> $LOG 2>&1

WMSUPLOAD=${DIR}/wmsupload

SCENE=$1
echo "PROCESSING: $1"

BASE=${SCENE##*/} # filename w/ extension
STRIP=${BASE%%.tar.gz} # filename w/o extension

YEAR=${STRIP:9:4}
DOY=${STRIP:13:3}
YMD=`date -d "Jan 1 $YEAR + $DOY days - 1 day" "+%Y%m%d"`
PART=${STRIP:2:7}

BASE=${STRIP##*/} # filename w/ extension
STRIP=${BASE%%.tar.gz} # filename w/o extension

for band in ${BANDS[@]}; do  # FIXME: for when we do multiple bands...
  MTL="${STRIP}_MTL.txt"
  BN="${STRIP}_B${band}.TIF"
  tar xzf $SCENE $BN $MTL
  
  vmul=$(grep "RADIANCE_MULT_BAND_${band} =" $MTL | cut -d " " -f 7)
  vadd=$(grep "RADIANCE_ADD_BAND_${band} ="  $MTL | cut -d " " -f 7)
  
  # calculate TOA radiance (Watts/( m2 * srad * μm))
  # https://landsat.usgs.gov/Landsat8_Using_Product.php
  product="nickb-lsat-TOArad-b${band}_${YMD}_120000.part${PART}.tif"
  gdal_calc.py -A $BN --outfile=${product} --calc="(A*${vmul})+${vadd}"

  $WMSUPLOAD ${product}
done