#!/bin/csh -f

# Script to process a specified number of HIRS L1b "orbits".

#---------------------------------------------------------------
# Location of HIRS HDF "L1b" input files.
  set HIRS_DIR = /data/richardf/HIRS/HIRS_L1b_METOPA_2009
# set HIRS_DIR = /data/richardf/HIRS/HIRS_L1b_N17_2009
# set HIRS_DIR = /data/richardf/HIRS/HIRS_L1b_N18_2008

# Location of corresponding collocation index files.
  set AVHRR_INDEX_DIR = /data/richardf/HIRS/avhrr_indecis/2009/METOP-A
# set AVHRR_INDEX_DIR = /data/richardf/HIRS/avhrr_indecis/2009/NOAA-17
# set AVHRR_INDEX_DIR = /data/richardf/HIRS/avhrr_indecis/2008

# Location of corresponding PATMOS-X (AVHRR) files.
  set PATMOS_DIR = /data/richardf/HIRS/patmos_l2/metop_a_2009
# set PATMOS_DIR = /data/richardf/HIRS/patmos_l2/N17_2009
# set PATMOS_DIR = /data/richardf/HIRS/patmos_l2/N18_2008

# Location of temperature/moisture profiles.
  set PROF_DIR = /data/dbotambekov/Ancil_Data/cfsr/hdf_05/2009

# Location of the executable file (src directory).
  set EXE_DIR = /home/richardf/src/HIRS_Processing/src

# Location of working directory.
  set RUN_DIR = /data/richardf/HIRS/run15

#---------------------------------------------------------------

  cd $RUN_DIR

#---------------------------------------------------------------

# Create list of HIRS input files.
  set num_HIRS_files = `ls -1 $HIRS_DIR/* | wc -l`
  set HIRS_file_list = `ls -1 $HIRS_DIR`
# echo HIRS input files:
# ls -l $HIRS_DIR
  echo Number of files to be processed: $num_HIRS_files
  echo

#---------------------------------------------------------------

# Link to HIRS forward model coefficients.
 
  ln -s /data/richardf/HIRS/original_hirs_FM_coeff/* .
# Shifted FM coeff's set to version of 12-21-12.
  ln -s /data/richardf/HIRS/shifted_hirs_FM_coeff/* .
    
#---------------------------------------------------------------

# Loop through HIRS orbit files.

#---------------------------------------------------------------

  @ j =    1

# while ($j <= $num_HIRS_files)
  while ($j <=   14)

#---------------------------------------------------------------

    echo Processing $HIRS_file_list[$j]

#---------------------------------------------------------------

#   Get HIRS beginning date and hour.
    echo $HIRS_file_list[$j] > a
    set beg_hour = `cut -c21-22 a`
    set beg_doy = `cut -c16-18 a`
#   echo Beginning Day of Year and UTC Hour: $beg_doy $beg_hour
    echo

#---------------------------------------------------------------

#   Construct collocation index and PATMOS-X L2 file names.
    set out_id = `cut -c10-30 a`
    set collocation_index_file = avhrr_indecis_${out_id}
    set PATMOS_L2_file = `ls -1 ${PATMOS_DIR}/*${out_id}*`

#---------------------------------------------------------------

#   Copy files to working directory.
    cp $HIRS_DIR/$HIRS_file_list[$j] .
    cp $AVHRR_INDEX_DIR/$collocation_index_file .

#---------------------------------------------------------------

#   Construct HIRS CSRB output file names.
    set out_id = `cut -c10-18 a`
    set csrb_file = ${RUN_DIR}/HIRS_CSRB_${out_id}_cfsr_v5.3.bin

#---------------------------------------------------------------


#   HIRS processing command line.

#   ./process_csrb_nceprean1.exe

#        HIRS input file
#        NCEP reanlysis1 input profile directory
#        HIRS/AVHRR collocation index file
#        Collocated AVHRR (PATMOS) L2 file
#        debug level index
#        shifted/unshifted forward model option 
#             1=unshifted (original), 2=shifted FM and Planck coefficients
#        CSRB file

    set debug = 0
    set shifted_FM_opt = 2

#   Echo command
    echo ${EXE_DIR}/process_csrb_cfsr.exe $HIRS_file_list[$j] $PROF_DIR \
         $collocation_index_file $PATMOS_L2_file $debug $shifted_FM_opt \
         $csrb_file

    ${EXE_DIR}/process_csrb_cfsr.exe $HIRS_file_list[$j] $PROF_DIR \
         $collocation_index_file $PATMOS_L2_file $debug $shifted_FM_opt \
         $csrb_file

#---------------------------------------------------------------

#   Remove files from working directory.
    rm $HIRS_file_list[$j]
    rm $collocation_index_file
    rm a

    @ j += 1
  
  end
#---------------------------------------------------------------

  rm hirs*.dat

  cd .

  exit 0
