#!/bin/csh -f

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

#---------------------------------------------------------------
# Location of HIRS CTP processing script.
  set COLLOCATION_SCRIPT = /home/richardf/src/HIRS_Processing/HIRS_AVHRR_CTPs/process_hirs_orbits_cfsr_test.csh

# 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_200808
# set HIRS_DIR = /data/richardf/HIRS/HIRS_L1b_N18_200808
  set HIRS_DIR = /odyssey/trends/HIRS/HDF/noaa-18/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/200808
  set AVHRR_INDEX_DIR = /odyssey/trends/HIRS/collo/noaa-18/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/200808
# set PATMOS_DIR = /data/richardf/HIRS/patmos_l2/N18_200808_new
  set PATMOS_DIR = /odyssey/trends/clavrx/noaa-18/2008

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

# Location of HIRS monthly zonal CSRB files.
# set CSRB_DIR = /data/richardf/HIRS/HIRS_CSRB_monthly_files_v4.5/METOPA_200901
# set CSRB_DIR = /data/richardf/HIRS/HIRS_CSRB_monthly_files_v5.3/METOPA_200907
# set CSRB_DIR = /data/richardf/HIRS/HIRS_CSRB_monthly_files_v4.5/N17_200912
# set CSRB_DIR = /data/richardf/HIRS/HIRS_CSRB_monthly_files_v4.5/N18_200808
  set CSRB_DIR = /odyssey/trends/csrb/monthly/noaa-18/2008

# Location of processing directory (/tmp for SGE processing).
  set TMPDIR = /tmp
  set RUN_DIR = $TMPDIR

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

# 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 CSRB file.  Set once for entire month.
# HIRS CSRB file name (1 file per month).
# set csrb_file = METOPA_200901_zonal_monthly_HIRS_CSRBs_ncep1_v4.5.bin
# set csrb_file = HIRS_CSRB_M2_200907_monthly_zonal_mean_cfsr.bin
# set csrb_file = N17_200912_zonal_monthly_HIRS_CSRBs_ncep1_v4.5.bin
# set csrb_file = N18_200808_zonal_monthly_HIRS_CSRBs_ncep1_v4.5.bin
  set csrb_file = MEAN.MONTHLY.noaa-18.2008.01.bin
  ln -s ${CSRB_DIR}/$csrb_file $RUN_DIR

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

# Link to HIRS forward model coefficients.
# Set once for entire run.
 
  ln -s /data/richardf/HIRS/original_hirs_FM_coeff/* $RUN_DIR
# Shifted coeff's now set to versions of 12-21-12.
  ln -s /data/richardf/HIRS/shifted_hirs_FM_coeff/* $RUN_DIR

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

# Loop through HIRS orbit files.

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

  @ j =    1

# while ($j <= $num_HIRS_files)
# while ($j <= 443)
  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 collocation_index_file = ${AVHRR_INDEX_DIR}/NSS.HIRX.*${out_id}*.hdf
    set PATMOS_L2_file = `ls -1 ${PATMOS_DIR}/*${out_id}*`

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

#   Construct HIRS CTP output file names.
    set out_id = `cut -c10-30 a`
    set out_prd_file = HIRS_${out_id}_ncep1_v5.3.bin

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

    qsub -wd /tmp -S /bin/csh $COLLOCATION_SCRIPT ${HIRS_DIR} $HIRS_file_list[$j] \
         ${AVHRR_INDEX_DIR} $collocation_index_file $PATMOS_L2_file $PROF_DIR \
         $CSRB_DIR $csrb_file $out_prd_file $out_id

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

    @ j += 1
  
  end

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

  exit 0
