#!/bin/bash

############################################################################
# Usage: seadas_benchmark.bash  
#
# This script processes a MODIS Aqua 5 minute granule from L0 to
# Level-3 SMI and creates a benchmark log file. If log files exist
# from previous runs a new log file will be created with a successive
# L0_seadas_benchmark.log.# filename.
############################################################################


if [ ""$OCDATAROOT = "" ]; then
  echo "`basename $0`: ERROR: The OCDATAROOT environment variable is not set. Exiting."
  exit 1
fi
if [ ! -d ${OCDATAROOT}/modis ]; then
  echo "`basename $0`: ERROR: The ${OCDATAROOT}/modis/ directory doesn't exist."
  echo ""
  echo "Please make sure you have downloaded and installed the SeaDAS"
  echo "processing data support files. Exiting."
  echo ""
  exit 1
fi

logfile="L0_seadas_benchmark.log"
n=2
while [ -f $logfile ]; do
  logfile="L0_seadas_benchmark.log".$n
  n=`echo "$n +1" |bc`
done

rm -f .tmpreal .tmpuser .tmpsys .tmptimes
rm -f A2006167181000.L1A A2006167181000.GEO
rm -f A2006167181000.L1B_HKM A2006167181000.L1B_QKM
rm -f A2006167181000.L1B_LAC A2006167181000.L2.nc
rm -f A2006167181000.L3b_space.nc A2006167181000.L3b_time.nc
rm -f A2006167181000.smi_chlor_a_mn.nc

time_cmd='/usr/bin/time -p'

# Determine the OS to dictate how we will deal with stdout later...
# Also determine release of OS

if [[ $OCSSW_ARCH == "linux" || $OCSSW_ARCH == 'linux_64' ]]; then
  if [ -f /etc/fedora-release ]; then
    release=`cat /etc/fedora-release`
  elif [ -f /etc/lsb-release ]; then
    release=`grep "DISTRIB_DESCRIPTION=" /etc/lsb-release |cut -c22- |tr -d '"'`
  elif [ -f /etc/redhat-release ]; then
    release=`cat /etc/redhat-release`
  else
    release="Unable to determine Linux distro"
  fi
elif [[ $OCSSW_ARCH == "macosx_intel" || $OCSSW_ARCH == "macosx_ppc" ]]; then
  release=`sw_vers |grep ProductVersion | cut -c17-`
else
  echo "Your operating system '$osname' doesn't seem to be supported. Attempting to continue.."
  release="undetermined"
fi

echo "===============================================================" |tee $logfile
echo "SeaDAS Benchmarking Script - MODIS processing" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile
echo "`date`" |tee -a $logfile
echo "SeaDAS running on `uname -srpm`" |tee -a $logfile
echo "OS Release: $release" |tee -a $logfile

echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating L1A file (modis_L1A.py)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "modis_L1A.py A2006167181000.L0 -o A2006167181000.L1A" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd modis_L1A.py A2006167181000.L0 -o A2006167181000.L1A 2>&1 |tee -a $logfile


echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating GEO file (modis_GEO.py)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "modis_GEO.py A2006167181000.L1A" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd modis_GEO.py A2006167181000.L1A 2>&1 |tee -a $logfile

echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating L1B file (modis_L1B.py)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "modis_L1B.py A2006167181000.L1A" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd modis_L1B.py A2006167181000.L1A 2>&1 |tee -a $logfile


echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating L2 file (l2gen)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "l2gen ifile=A2006167181000.L1B_LAC geofile=A2006167181000.GEO ofile=A2006167181000.L2.nc l2prod=chlor_a" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd l2gen ifile=A2006167181000.L1B_LAC geofile=A2006167181000.GEO ofile=A2006167181000.L2.nc l2prod=chlor_a 2>&1 |tee -a $logfile


echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating 2km spatially binned file (l2bin)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "l2bin ifile=A2006167181000.L2.nc ofile=A2006167181000.L3b_space.nc resolution=1 area_weighting=1" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd l2bin ifile=A2006167181000.L2.nc ofile=A2006167181000.L3b_space.nc resolution=1 area_weighting=1 2>&1 |tee -a $logfile


echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating time binned file (l3bin)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "l3bin ifile=A2006167181000.L3b_space.nc ofile=A2006167181000.L3b_time.nc" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd l3bin ifile=A2006167181000.L3b_space.nc ofile=A2006167181000.L3b_time.nc 2>&1 |tee -a $logfile

echo "" |tee -a $logfile
echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "Creating standard mapped image (l3mapgen)..." |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

echo "l3mapgen ifile=A2006167181000.L3b_time.nc ofile=A2006167181000.smi_chlor_a_mn.nc resolution=1km interp=area" |tee -a $logfile
echo "" |tee -a $logfile

$time_cmd l3mapgen ifile=A2006167181000.L3b_time.nc ofile=A2006167181000.smi_chlor_a_mn.nc resolution=2km interp=area  2>&1 |tee -a $logfile


# Calculate times for each stage of processing
grep "^real " $logfile |awk '{ print $2 }' >.tmpreal
grep "^user " $logfile |awk '{ print $2 }' >.tmpuser
grep "^sys " $logfile |awk '{ print $2 }' >.tmpsys
echo "`sed -n 1p .tmpuser` + `sed -n 1p .tmpsys`" |bc >.tmptimes
echo "`sed -n 2p .tmpuser` + `sed -n 2p .tmpsys`" |bc >>.tmptimes
echo "`sed -n 3p .tmpuser` + `sed -n 3p .tmpsys`" |bc >>.tmptimes
echo "`sed -n 4p .tmpuser` + `sed -n 4p .tmpsys`" |bc >>.tmptimes
echo "`sed -n 5p .tmpuser` + `sed -n 5p .tmpsys`" |bc >>.tmptimes
echo "`sed -n 6p .tmpuser` + `sed -n 6p .tmpsys`" |bc >>.tmptimes
echo "`sed -n 7p .tmpuser` + `sed -n 7p .tmpsys`" |bc >>.tmptimes

rl1atime=`sed -n 1p .tmpreal`
rgeotime=`sed -n 2p .tmpreal`
rl1btime=`sed -n 3p .tmpreal`
rl2gentime=`sed -n 4p .tmpreal`
rl2bintime=`sed -n 5p .tmpreal`
rl3bintime=`sed -n 6p .tmpreal`
rsmitime=`sed -n 7p .tmpreal`
rtotal_time=`echo "$rl1atime + $rgeotime + $rl1btime + $rl2gentime + $rl2bintime + $rl3bintime + $rsmitime" |bc`

l1atime=`sed -n 1p .tmptimes`
geotime=`sed -n 2p .tmptimes`
l1btime=`sed -n 3p .tmptimes`
l2gentime=`sed -n 4p .tmptimes`
l2bintime=`sed -n 5p .tmptimes`
l3bintime=`sed -n 6p .tmptimes`
smitime=`sed -n 7p .tmptimes`
total_time=`echo "$l1atime + $geotime + $l1btime + $l2gentime + $l2bintime + $l3bintime + $smitime" |bc`

rm -f .tmpreal .tmpuser .tmpsys .tmptimes

if [[ $OCSSW_ARCH == "macosx_intel" || $OCSSW_ARCH == "macosx_ppc" ]]; then
  echo "" |tee -a $logfile
  echo "" |tee -a $logfile
  echo "===============================================================" |tee -a $logfile
  echo "SYSTEM SPECS" |tee -a $logfile
  echo "===============================================================" |tee -a $logfile
  echo "" |tee -a $logfile
  echo "SeaDAS running on `uname -srpm`" |tee -a $logfile
  echo "OS Release: $release" |tee -a $logfile
  system_profiler SPHardwareDataType |grep -v Serial |grep -v UUID |grep -v SMC \
    |grep -v ROM |grep -v Hardware |grep -v Sudden |grep -v State |grep -v "^$" \
    |sed "s/  //g" |tee -a $logfile
fi


echo "" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "SeaDAS MODIS PROCESSING TIMES" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "CPU TIMES (user + sys):" |tee -a $logfile
echo "========================" |tee -a $logfile
echo "L0 to L1A       : ${l1atime}s" |tee -a $logfile
echo "L1A to GEO      : ${geotime}s" |tee -a $logfile
echo "L1A to L1B      : ${l1btime}s" |tee -a $logfile
echo "L1B to L2       : ${l2gentime}s" |tee -a $logfile
echo "L2 to L3b space : ${l2bintime}s" |tee -a $logfile
echo "L3b to L3b time : ${l3bintime}s" |tee -a $logfile
echo "L3b to SMI      : ${smitime}s" |tee -a $logfile
echo "========================" |tee -a $logfile
echo "TOTAL CPU TIME  : ${total_time}s" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "WALL CLOCK TIMES (real):" |tee -a $logfile
echo "========================" |tee -a $logfile
echo "L0 to L1A       : ${rl1atime}s" |tee -a $logfile
echo "L1A to GEO      : ${rgeotime}s" |tee -a $logfile
echo "L1A to L1B      : ${rl1btime}s" |tee -a $logfile
echo "L1B to L2       : ${rl2gentime}s" |tee -a $logfile
echo "L2 to L3b space : ${rl2bintime}s" |tee -a $logfile
echo "L3b to L3b time : ${rl3bintime}s" |tee -a $logfile
echo "L3b to SMI      : ${rsmitime}s" |tee -a $logfile
echo "========================" |tee -a $logfile
echo "TOTAL REAL TIME : ${rtotal_time}s" |tee -a $logfile
echo "===============================================================" |tee -a $logfile
echo "" |tee -a $logfile

