#!/bin/bash

# Process Aqua MODIS Level 0 data to Level 1B
# Test data obtained from http://eosfes1.ssec.wisc.edu/data/MODIS
# Liam.Gumley@ssec.wisc.edu 2014/04/08

# Check arguments
if [ $# -ne 2 ]; then
  echo "Usage: run_aqua_modis_level1.bash aqua_pds_file aqua_gbad_file"
  exit 1
fi

# Get arguments
aqua_pds_file=$1
aqua_gbad_file=$2

# Check for input PDS file
if [ ! -r $aqua_pds_file ]; then
  echo "Input PDS file not found: "$aqua_pds_file
  exit 1
fi

# Check for input GBAD file
if [ ! -r $aqua_gbad_file ]; then
  echo "Input GBAD file not found: "$aqua_gbad_file
  exit 1
fi

#------------------------------------------------------------------------------
# SET UP
#------------------------------------------------------------------------------

# Set up OCSSW
export OCSSWROOT=$DBVM_HOME/apps/ocssw
export OCSSW_DEBUG=0 
source $OCSSWROOT/OCSSW_bash.env

# Set up GBAD
export GBADHOME=$DBVM_HOME/apps/gbad/SPA/gbad/algorithm
export PATH=$GBADHOME:$PATH

#------------------------------------------------------------------------------
# PROCESS AQUA MODIS
#------------------------------------------------------------------------------

echo ""
echo "Starting Aqua MODIS processing at "$(date)

# Aqua MODIS attitude and ephemeris extraction from GBAD Level 0 PDS file
touch configfile
echo aqua_main -packetfile $aqua_gbad_file -noradfile $GBADHOME/localdata/noradfile -attitudefile aqua.att -ephemerisfile aqua.eph -listconfig yes
aqua_main -packetfile $aqua_gbad_file -noradfile $GBADHOME/localdata/noradfile -attitudefile aqua.att -ephemerisfile aqua.eph -listconfig yes
if [ $? -ne 0 ]; then
  echo "Error in Aqua MODIS ATT/EPH processing"
  exit 1
fi

# Aqua MODIS Level 1A extraction
echo modis_L1A.py --log --verbose --mission=A --startnudge=5 --stopnudge=5 $aqua_pds_file 
modis_L1A.py --log --verbose --mission=A --startnudge=5 --stopnudge=5 $aqua_pds_file 
if [ $? -ne 0 ]; then
  echo "Error in Aqua MODIS L1A processing"
  exit 1
fi

# Get name of new Level 1A file
level1a_file=$(ls -1t A*.L1A_LAC | head -1)

# Aqua MODIS geolocation
echo modis_GEO.py --log --verbose --enable-dem --disable-download -a aqua.att -e aqua.eph $level1a_file 
modis_GEO.py --log --verbose --enable-dem --disable-download -a aqua.att -e aqua.eph $level1a_file 
if [ $? -ne 0 ]; then
  echo "Error in Aqua MODIS GEO processing"
  exit 1
fi

# Get name of new geolocation file
geo_file=$(ls -1t A*.GEO | head -1)

# Aqua MODIS calibration
echo modis_L1B.py --log --verbose $level1a_file $geo_file 
modis_L1B.py --log --verbose $level1a_file $geo_file 
if [ $? -ne 0 ]; then
  echo "Error in Aqua MODIS L1B processing"
  exit 1
fi

# Get name of new L1B 1KM file
level1b_file=$(ls -1t A*.L1B_LAC | head -1)

# Aqua MODIS destriping
export DESTRIPE_ROOT=$DBVM_HOME/apps/destripe/destripe
$DESTRIPE_ROOT/run_modis_destripe.csh aqua $level1b_file
if [ $? -ne 0 ]; then
  echo "Error in Aqua MODIS destripe processing"
  exit 1
fi

#------------------------------------------------------------------------------
# CLEAN UP AND EXIT
#------------------------------------------------------------------------------

rm $level1a_file *.pcf Log* aqua.* configfile
exit 0

