#$ -pe ac.alloc 1 -l h_cpu=1:00:00
#$ -M charles.seman@noaa.gov
#$ -m bes
#$ -o /home/$USER/workback/wrf/ana/output.$JOB_NAME.$JOB_ID
#$ -j y

########################################################################
#
#  1) Concatenates set of specified GrADS ieee binary files into a
#     specified output GrADS ieee binary file;  note, files are
#     concatenated in the order listed...
#  2) Contructs a GrADS ctl file for the concatenated GrADS ieee binary
#     file
#  3) Runs "tar" command on the set of specified GrADS files
#     (both ieee and ctl), putting the output into a user-specified
#     "tar" file, and copies the "tar" file to the GrADS file directory
#  4) Moves the set of specified GrADS files (both ieee and ctl)
#     from the GrADS file directory to a user-specified GrADS
#     directory on /net2
#
#-----------------------------------------------------------------------
#  Specify project file (contains user-specified GrADS files, etc)
#-----------------------------------------------------------------------
#
set SC_DIR = /home/cjs/GFDL_WRF.ana
set PROJECT = run_cat_files_project.KWAJEX_3D-dz_250m
#
########################################################################

if ( `gfdl_platform` != hpcs ) then
  echo " Note, designed to use "'$TMPDIR'", so must be run on HPCS..."
  echo " Logon to the AC computer and run jobscript there."
  echo " Exiting script..."
  echo
  exit 1
else
  set COMP_SYS = hpcs
endif

if ( $?JOB_NAME ) then
  set JOB_SCRIPT = $JOB_NAME    # batch
else
  set JOB_SCRIPT = $0           # interactive
endif
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo 'Starting JOBSCRIPT: '$JOB_SCRIPT
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'

set starting_time = `date`
echo
echo "DATE: " $starting_time
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo COMPUTER SYSTEM = $COMP_SYS
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo

#-----------------------------------------------------------------------
#  ...jobscript aliases
#-----------------------------------------------------------------------

alias dmput 'dmput -r'
alias check_dir '/home/cjs/bin/check_dir'

#-----------------------------------------------------------------------
#  ...define parameters for script...
#-----------------------------------------------------------------------

source $SC_DIR/$PROJECT

echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo  List INPUTS for this script...
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo " Source code directory:"
echo "   "$SC_DIR
echo " Project file:"
echo "   "$PROJECT
echo
echo " Working directory (WORK_DIR):"
echo "   "$WORK_DIR
echo " GrADS input/output directory (GRADS_DIR):"
echo "   "$GRADS_DIR
echo " Input GrADS data file ID (GRADS_FILE_ID):"
echo "   "$GRADS_FILE_ID
echo " Input GrADS data file variables (GRADS_FILE_VAR):"
echo "   "$GRADS_FILE_VAR
echo " Output GrADS data file ID (GRADS_CAT_FILE_ID):"
echo "   "$GRADS_CAT_FILE_ID
echo

if ( $#GRADS_FILE_ID == 1 ) then
  echo " Must have at least 2 input GrADS root filenames..."
  echo " Exiting script...."
  echo
  exit 1
endif

#-----------------------------------------------------------------------
#  ...go to WORK_DIR to do processing
#-----------------------------------------------------------------------

cd $WORK_DIR
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo Current working directory: $cwd
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
if ( $cwd == $TMPDIR ) then
  echo
  echo Working in '$TMPDIR', remove any old files using '"wipeftmp"'
  echo before getting files for current job...
  wipeftmp
endif

echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo 'Define local array containing GrADS filenames...'
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo

set GRADS_FILE = ( )    # initialize...

foreach var ( $GRADS_FILE_VAR )
  echo '--- '$var' ---'
  foreach file_id ( $GRADS_FILE_ID )
    set ctl_file = ${var}.${file_id}.ctl
    set ieee_file = ${var}.${file_id}.ieee
    echo $ctl_file' '$ieee_file
    set GRADS_FILE = ( $GRADS_FILE $ctl_file $ieee_file )
  end
end
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo '..finished defining GrADS filenames'
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo 'If necessary, dmget and copy GrADS files to WORK_DIR...'
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo

#------------------------------------------------------------------------
if ( $WORK_DIR != $GRADS_DIR ) then     # not working in GRADS_DIR...
#------------------------------------------------------------------------

  echo
  echo '...Not working in GRADS_DIR, need to copy files to working directory...'
  echo

  cd $GRADS_DIR
  echo
  echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo 'Current directory:  '$cwd
  echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if ( $DMGETS == on ) then
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    echo 'DMGETS parameter is "on"...'
    echo 'Check if we need to "dmget" the GRADS input files "GRADS_FILE"...'
    echo

    if ( $DMPUTS == off ) then  # determines type of dmget'ing
#
#  Not working in GRADS_DIR, DMGETS "on", and DMPUTS "off"...
#
#  We will want to do a single "dmget" for all the desired files that are
#  migrated, so we will first check to see which files need to be unmigrated...
#  Save only those GRADS_FILE's which are migrated, for dmget'ing later...
#
     foreach file ( $GRADS_FILE )
      set file_state = `dmls -Od $file`                         # Tim Yeager's technique
      if ( $file_state[1] == '(OFL)' ) then
        echo File $file
        echo is offline, add it to array to "dmget" it later...
        if ( $?GRADS_FILE_DMGET ) then
          set GRADS_FILE_DMGET = ( $GRADS_FILE_DMGET $file )    # add to array
        else
          set GRADS_FILE_DMGET = ( $file )                      # initialize dmget array
        endif
      else
        echo No need to dmget file $file as it is unmigrated and ready to copy...
      endif
     end
#
#  Do the dmget...
#
     if ( $?GRADS_FILE_DMGET ) then
       set nfdmget = $#GRADS_FILE_DMGET
       echo
       echo 'Number of GrADS file(s) to dmget: ' $nfdmget
       if ( $nfdmget == 1 ) then
         echo 'File to dmget: ' $GRADS_FILE_DMGET
       else
         echo 'File(s) to dmget:'
         @ n = 2
         while ( $n <= $nfdmget )
           @ n1 = $n - 1 ; @ n2 = $n
           echo $GRADS_FILE_DMGET[$n1] $GRADS_FILE_DMGET[$n2]
           @ n = $n + 2
         end
         if ( $n2 < $nfdmget ) then
           echo $GRADS_FILE_DMGET[$nfdmget]
         endif
       endif
       echo
       echo 'dmget above file(s)... '
       @ n = 1
       while ( $n <= $nfdmget )
         @ n1 = $n
         @ n2 = $n1 + 50
         if ( $n2 > $nfdmget ) then
           @ n2 = $nfdmget
         endif
         dmget $GRADS_FILE_DMGET[$n1-$n2]
         @ n = $n2 + 1
       end
       echo
     endif

     echo
     echo 'Copy the GrADS files to the WORK_DIR...'
     echo
     foreach file ( $GRADS_FILE )
       echo 'Copying file ' $file ' to ' $WORK_DIR
       cp -p $file $WORK_DIR
       echo '...finished copying file ' $file
     end

    else                # DMPUTS "on"
#
#  Not working in GRADS_DIR, DMGETS "on" and DMPUTS "on"...
#
#  PROCEDURE for this option combination:
#
#  For each GrADS file, check if it is offline, and if so, do the dmget,
#  copy the GrADS file to the WORK_DIR, and then immediately dmput it to
#  minimize unmigrated data on the ARCHIVE...
#
     echo
     echo 'Check, and if necessary, dmget a GrADS file,'
     echo 'copy it to WORK_DIR, and then dmput it.'
     echo 'Do one file at a time to minimize unmigrated GrADS files... '
     echo
     foreach file ( $GRADS_FILE )
       set file_state = `dmls -Od $file`        # Tim Yeager's technique
       if ( $file_state[1] == '(OFL)' ) then
         echo File $file
         echo is offline, "dmget" it...
         dmget $file
         echo Finished dmget for GrADS file: $file
       else
         echo No need to dmget file $file as it is unmigrated...
       endif
       echo 'Copying file ' $file ' to ' $WORK_DIR
       cp -p $file $WORK_DIR
       echo Copied GrADS file $file to $WORK_DIR
       dmput -r $file
       echo Finished dmput for GrADS file: $file
     end

    endif       # do DMPUTS?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  else          # WORK_DIR != GRADS_DIR, and DMGETS is "off"...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    echo
    echo 'DMGETS parameter is "off"...'
    echo

    foreach file ( $GRADS_FILE )
      if ( $DMCHK == on ) then
        set file_state = `dmls -Od $file`       # Tim Yeager's technique
        if ( $file_state[1] == '(OFL)' ) then
          echo WARNING:  Specified GrADS file $file
          echo is offline, so cannot copy it to $WORK_DIR
          echo Exiting jobscript.
          echo
          exit 1
        else
          echo GrADS file $file is unmigrated...
          echo Copying GrADS file $file to $WORK_DIR
          cp -p $file $WORK_DIR
          echo ... finished copying GrADS file $file
          if ( $DMPUTS == on ) then     # check if GrADS file should be "dmput"...
            dmput -r $file
            echo Finished dmput for GrADS file: $file
          endif
        endif
      else
          echo Copying GrADS file $file to $WORK_DIR
          cp -p $file $WORK_DIR
          echo ... finished copying GrADS file $file
          if ( $DMPUTS == on ) then     # check if GrADS file should be "dmput"...
            dmput -r $file
            echo Finished dmput for GrADS file: $file
          endif
      endif
      echo
    end

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  endif         # not working in GRADS_DIR, do DMGETS?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#  go back to "WORK_DIR"...
#
  cd $WORK_DIR

#------------------------------------------------------------------------
else            # working in GRADS_DIR (WORK_DIR is GRADS_DIR)...
#------------------------------------------------------------------------

  echo
  echo 'Working in GRADS_DIR...'
  echo
  echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo 'Current directory:  '$cwd
  echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if ( $DMGETS == on ) then
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   echo '--> Need only to make sure GrADS files are unmigrated...'
   echo
   echo 'CAUTION: you are working in directory ' $GRADS_DIR
   echo 'and specified GrADS files will be unmigrated for the duration'
   echo 'of this job...'
   echo
#
#  We will want to do a single "dmget" for all the desired migrated file(s)
#  so we will first check to see which file(s) need to be unmigrated... Save
#  only those GRADS_FILE's which are migrated, for dmget'ing later...
#
   foreach file ( $GRADS_FILE )
     set file_state = `dmls -Od $file`  # Tim Yeager's technique
     if ( $file_state[1] == '(OFL)' ) then
      echo File $file
      echo is offline, add it to array to "dmget" it later...
      if ( $?GRADS_FILE_DMGET ) then
       set GRADS_FILE_DMGET = ( $GRADS_FILE_DMGET $file )       # add to array
      else
       set GRADS_FILE_DMGET = ( $file ) # initialize dmget array
      endif
     else
      echo No need to dmget file $file as it is unmigrated and ready for processing...
     endif
   end

   if ( $?GRADS_FILE_DMGET ) then
     set nfdmget = $#GRADS_FILE_DMGET
     echo
     echo 'Number of GrADS file(s) to dmget: ' $nfdmget
     if ( $nfdmget == 1 ) then
       echo 'File to dmget: ' $GRADS_FILE_DMGET
     else
       echo 'File(s) to dmget:'
       @ n = 2
       while ( $n <= $nfdmget )
         @ n1 = $n - 1 ; @ n2 = $n
         echo $GRADS_FILE_DMGET[$n1] $GRADS_FILE_DMGET[$n2]
         @ n = $n + 2
       end
       if ( $n2 < $nfdmget ) then
         echo $GRADS_FILE_DMGET[$nfdmget]
       endif
     endif
     echo
     echo 'dmget above file(s)... '
     dmget $GRADS_FILE_DMGET
   endif

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  else
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   echo
   echo 'CAUTION: you are working in directory ' $GRADS_DIR
   echo 'and specified GrADS files will not be DMGET even if they'
   echo 'are migrated...'
   echo

   if ( $DMCHK == on ) then
    foreach file ( $GRADS_FILE )
      set file_state = `dmls -Od $file` # Tim Yeager's technique
      if ( $file_state[1] == '(OFL)' ) then
          echo WARNING:  Specified GrADS file $file
          echo is offline
          echo Exiting jobscript.
          exit 1
      endif
      echo
    end
   endif

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  endif         # working in GRADS_DIR, do DMGETS?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#------------------------------------------------------------------------
endif           # where is WORK_DIR?
#------------------------------------------------------------------------

if ( $cwd != $WORK_DIR ) then
  echo
  echo 'Current directory:  '$cwd
  echo 'does not match WORK_DIR:  '$WORK_DIR
  echo 'Exiting script "'$0'"...'
  echo
  exit 1
endif

echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo 'Contents in current directory:  '$cwd
echo
ls -tl
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo

#-----------------------------------------------------------------------
#  ...construct GrADS ieee cat and ctl files, variable-by-variable
#-----------------------------------------------------------------------

set CAT_FILE = ( )      # initialize...

foreach var ( $GRADS_FILE_VAR )
  set cat_ctl_file = ${var}.${GRADS_CAT_FILE_ID}.ctl
  set cat_ieee_file = ${var}.${GRADS_CAT_FILE_ID}.ieee
  echo
  echo '---------------------------------------------'
  echo '"cat" files for variable: '$var
  echo $cat_ctl_file
  echo $cat_ieee_file
  echo '---------------------------------------------'
  touch $cat_ieee_file  # initialize file
  @ ntsum = 0
  foreach file_id ( $GRADS_FILE_ID )
    #
    #  ...concatenate current GrADS ieee file onto "cat_ieee_file"
    #
    set ieee_file = ${var}.${file_id}.ieee
    cat $ieee_file >> $cat_ieee_file
    if ( $status != 0 ) then
      echo 'error detected;  exiting script...'
      echo
      exit 1
    endif
    echo $ieee_file
    echo '...concatenated onto GrADS ieee cat file'
    #
    #  ...add number of time levels in current ctl file onto running sum
    #
    set ctl_file = ${var}.${file_id}.ctl
    echo $ctl_file
    set nt = `grep tdef $ctl_file | cut -f2 -d' '`
    if ( $status != 0 ) then
      echo 'error detected;  exiting script...'
      echo
      exit 1
    endif
    @ ntsum = $ntsum + $nt
    echo '..."nt" = '$nt', running sum "ntsum" = '$ntsum
    echo '---------------------------------------------'
  end
  #
  #  ...construct "cat" ctl file
  #
  set ctl_file1 = ${var}.${GRADS_FILE_ID[1]}.ctl
  if ( -e $cat_ctl_file ) rm $cat_ctl_file
  echo "dset ^$cat_ieee_file" > $cat_ctl_file
  sed "/dset/d" $ctl_file1 >> $cat_ctl_file
  if ( -e ctl_tmp ) rm ctl_tmp
  sed -e "s/$nt/$ntsum/" $cat_ctl_file > ctl_tmp
  mv ctl_tmp $cat_ctl_file
  echo
  echo 'GrADS "cat" ctl file:'
  echo
  cat $cat_ctl_file
  echo
  if ( $status != 0 ) then
    echo 'error detected;  exiting script...'
    echo
    exit 1
  endif
  #
  #  ...copy "cat" files to I/O GrADS directory
  #
  if ( -e $GRADS_DIR/$cat_ctl_file ) rm $GRADS_DIR/$cat_ctl_file
  if ( -e $GRADS_DIR/$cat_ieee_file ) rm $GRADS_DIR/$cat_ieee_file
  cp -p $cat_ctl_file $cat_ieee_file $GRADS_DIR
  if ( $status != 0 ) then
    echo 'error detected;  exiting script...'
    echo
    exit 1
  endif
  #
  #  ...update "cat" files list
  #
  set CAT_FILE = ( $CAT_FILE $cat_ctl_file $cat_ieee_file )
end
echo
echo 'tar "cat" file component GrADS files...'
echo
cd $GRADS_DIR
set GRADS_TAR_FILE = all_var.${GRADS_CAT_FILE_ID}.tar
tar -cvf $GRADS_TAR_FILE $GRADS_FILE
if ( $status == 0 ) then
  if ( -e $GRADS_TAR_FILE ) then
    echo
    echo 'GrADS tar file:'
    ls -l $GRADS_TAR_FILE
    echo
    #
    #  ...move GrADS files to $NET2_DIR
    #
    #     check if storage directory "NET2_DIR" exists;
    #     if not make this directory...
    #
    check_dir $NET2_DIR
    mv $GRADS_FILE $NET2_DIR
    echo
    echo '"cat" component GrADS files moved to: '$NET2_DIR
    echo
    #
    #  ...copy jobscript, project files to $NET2_DIR
    #
    if ( $?JOB_ID ) then
      set describe = job_${JOB_ID}
    else
      set describe = ${starting_time[2]}_${starting_time[3]}_${starting_time[4]}_${starting_time[6]}
    endif
    cp -p $SC_DIR/$JOB_SCRIPT $NET2_DIR/${JOB_SCRIPT}-${describe}
    cp -p $SC_DIR/$PROJECT $NET2_DIR/${PROJECT}-${describe}
  endif
endif
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo $cwd
echo
ls -tl $GRADS_TAR_FILE $CAT_FILE
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo 'Finished JOBSCRIPT: '$JOB_SCRIPT
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
echo
echo "Ending Time:   " `date`
echo "Starting Time: " $starting_time
echo
echo
echo '***' Exit script. '***'
echo

exit 0
