#!/bin/bash

# Process a Terra MODIS L1B 1KM granule using default and modified emissive LUTs.
# Liam.Gumley@ssec.wisc.edu 2011-06-07

echo
echo "(Installing and configuring MODISL1DB v1.7)"
echo

# Install MODISL1DB v1.7
mkdir modisl1db_1.7 &> /dev/null
cd modisl1db_1.7
curl -O ftp://samoa.gsfc.nasa.gov/seadas/modisl1db/modisl1db_1.7/modisl1db_linux.tar.gz
tar zxf modisl1db_linux.tar.gz

# Configure MODISL1DB
export DBHOME=$PWD
source $DBHOME/run/scripts/modisl1db_env.bash
export OCSSW_DEBUG="0"
modis_update_utcpole_leapsec.csh

echo
echo "(Processing Terra MODIS L0 to L1A and GEO)"
echo

# Get L0 data
mkdir data &> /dev/null
cd data
curl -O ftp://ftp.ssec.wisc.edu/pub/gumley/chrism/MOD00F.A2011187.1555.001.NRT

# Process L0 to L1A and GEO
modis_L1A.csh MOD00F.A2011187.1555.001.NRT -satellite terra -startnudge 0 -stopnudge 0
modis_GEO.csh T2011187155500.L1A_LAC -disable-dem

echo
echo "(Processing Terra MODIS L1A/GEO to L1B using default emissive LUT)"
echo

# Process L1A/GEO to L1B using default emissive LUT
modis_L1B.csh T2011187155500.L1A_LAC T2011187155500.GEO \
  -o MOD021KM.A2011187.1555.V6.1.6.2.hdf -delete-hkm -delete-qkm 2>&1 | tee MOD021KM.A2011187.1555.V6.1.6.2.log

echo
echo "(Processing Terra MODIS L1A/GEO to L1B using modified emissive LUT)"
echo

# Install modified emissive LUT
cd $DBHOME/run/var/modist/cal/OPER
curl -O ftp://ftp.ssec.wisc.edu/pub/gumley/chrism/MOD02_Emissive_LUTs.V6.1.6.2a.hdf
cd $DBHOME/run/scripts
cp modisl1db_env.bash modisl1db_env_V6.1.6.2a.bash
sed -i 's/MOD02_Emissive_LUTs.V6.1.6.2.hdf/MOD02_Emissive_LUTs.V6.1.6.2a.hdf/' modisl1db_env_V6.1.6.2a.bash
source modisl1db_env_V6.1.6.2a.bash

# Process L1A/GEO to L1B using modified emissive LUT
cd $DBHOME/data
modis_L1B.csh T2011187155500.L1A_LAC T2011187155500.GEO \
  -o MOD021KM.A2011187.1555.V6.1.6.2a.hdf -delete-hkm -delete-qkm 2>&1 | tee MOD021KM.A2011187.1555.V6.1.6.2a.log

echo
echo "(Processing complete: L1B product files are in modisl1db_1.7/data)"
echo

exit 0

