#!/bin/ksh
#-----------------------------------------------------------------------------------------------
# NAME:  tigr_rttov.ksh
#
# PURPOSE:  This script uses forward radiative transfer using RTTOV9.1 using one TIGR2000 profile.
#
# INPUTS:
#          PLATFORM      =  Name of satellite platform (ie. 'noaa','dmsp','metop',etc.)
#          INST          =  Instrument name ('hirs','ssmi',etc.)
#          SATID         =  Satellite ID number ('XX = noaaXX, XX=FXX, etc.)
#          ZEANGLE       =  Local Satellite zenith angle
#          AZANGLE       =  Local satellite azimuthal angle
#          SURF_TYPE     =  Surface type (0=land, 1=sea, 2=ice/snow)
#          WATER_TYPE    =  Water type (0=fresh water,1=ocean water)
#          TIGR_PROF     =  Input tigr profile number.  These profiles have already been
#                           interpolated to RTTOV pressure levels.
#
# OUTPUTS:
#          {inst}_rttov_tigr_p{tigr_prof}.dat =  Output file containing RTTOV output for one tigr profile
# 
# CREATION HISTORY:
#                    Darren Jackson  CIRES/ESRL  January 2009
#                       March 2008. emissivity input changed to 0.95 as test.
#                       changed input_hirs.dat to input_hirs_emis95.dat.
#---------------------------------------------------------------------------------------------------
#
usage(){
  echo
  echo " run one RTTOV forward test"; 
  echo
  echo "usage  $0 platform instrument sat_id zenangle azangle surface_type profile_name";
  echo "  platform	platform name (noaa, goes, metop, dmsp, ...)";
  echo "  instrument    instrument name (hirs amsua ..)   ";
  echo "  sat_id        satellite number (xx for NOAAxx)   ";
  echo "  zenangle      zenithal angle (deg)   ";
  echo "  azangle       azimuthal angle (deg)   ";
  echo "  surface_type  surface type   (0=land, 1=sea, 2=ice/snow) ";
  echo "  water_type    water type (0=fresh water, 1=ocean water) ";
  echo "  tigr_prof     tigr profile number"
  echo "     ";
  echo "environment variables:"
  echo "  DATAIN  	directory for file channels and profiles "
  echo "  DATART	directory for coefficient files"
  echo
  echo "environment variables default values:"
  echo "  DATAIN  	../data "
  echo "  DATART	../rtcoef"
  echo
}
if [ $# -lt 7 ]
then
	usage
	exit 1
fi


# use function for rttov ids
. rttov_id.ksh

# Get arguments

PLATFORM=$1
INST=$2
SATID=$3
ZENANGLE=$4
AZANGLE=$5
SURF_TYPE=$6
WATER_TYPE=$7
TIGR_PROF=$8

# Following variables should have been initialised and exported
# in the calling script, else default values are appling
PROF_NAME='/satellite/data/djackson/misc/tigr/etl_formatted_soundings/tigr2000_v1.1_rttov_p'${TIGR_PROF}'.dat'
FILE_CHANNELS='input_'${INST}'.dat'

if [ $1 == "noaa" ] ; then
    PLATFORM_PFX=N
fi
if [ $1 == "metop" ] ; then
    PLATFORM_PFX=M
fi
if [ $1 == "dmsp" ] ; then
    PLATFORM_PFX=F
fi
OUT_FILE=${PLATFORM_PFX}${SATID}'_A'${ZENANGLE}'_P'${TIGR_PROF}'_'${INST}'_rttov91_tigr.dat'

DATAIN=${DATAIN:=../data}  
#DATART=${DATART:=../rtcoef_rttov7} 
#DATART=${DATART:=../rtcoef_rttov8/rttov8pred} 
DATART=${DATART:=../rtcoef_rttov9} 
DATAOUT=${DATAOUT:=/satellite/data/djackson/ssmi/rttov91_ssmi_sim} 

echo " "
echo " "

echo  "Platform   ${PLATFORM}"
echo  "Instrument ${INST}"
echo  "Satellite  ${SATID}"
echo  "File for channels ${FILE_CHANNELS}"
echo  "Zenith angle ${ZENANGLE}"
echo  "Azimuth angle ${AZANGLE}"
echo  "Surface type ${SURF_TYPE}"
echo  "TIGR profile name ${PROF_NAME}"

# call to function rttov_id
rttov_id ${PLATFORM} ${INST}

rm -f input.dat prof.dat
if [ ! -s ${DATAIN}/${FILE_CHANNELS} ] ; then
  echo "file ${FILE_CHANNELS}  does not exist on directory  ${DATAIN}"
  exit
fi
cp ${DATAIN}/${FILE_CHANNELS} input.dat
cp ${PROF_NAME} prof.dat

COEF_NAME=rtcoef_${PLATFORM}_${SATID}_${INST}.dat
COEF_NAME_BIN=rtcoef_${PLATFORM}_${SATID}_${INST}.bin

rm -f ${COEF_NAME}
rm -f ${COEF_NAME_BIN}
if [ -s  $DATART/${COEF_NAME} ] ; then
  ln -s $DATART/${COEF_NAME} ${COEF_NAME}
fi
if [ -s  $DATART/${COEF_NAME_BIN} ] ; then
  ln -s $DATART/${COEF_NAME_BIN} ${COEF_NAME_BIN}
fi

echo "FWD test ${PLATFORM} ${SATID} ${INST}"

#
# Run forward RT for TIGR profile
#

etl_rttov91.out <<EOF
${PLATFORM_N}   , platform name
${SATID}        , satellite number (NOAAxx = xx)
${INST_N}       , instrument number
0               , forward model only == 0, full gradient==1, radiance output ==2
${SURF_TYPE}    , Surface type (0=land, 1=sea, 2=ice/snow)
${WATER_TYPE}   , Water type (0=fresh water, 1=ocean water)
${ZENANGLE}     , Zenith angle
${AZANGLE}      , Azimuth angle
EOF

# Moves RTTOV output file to new file name
mv print.dat ${DATAOUT}/${OUT_FILE}

# Remove temporary coefficient files
rm -f ${COEF_NAME} ${COEF_NAME_BIN}

exit

