#! /bin/csh 
# run_post_batch.csh   S.J. Ormiston   2013-03-18
#-------------------------------------------------------------------#
# This script runs cfx post in batch to extract data using a cse file
# that the user has recorded to create a number of csv files. The script
# operates on a number of results files that are contained in a text
# file that is given on the command line.
# 
# The results files list is created using:
# % ls -1 *.res > resfiles.lst
#-------------------------------------------------------------------#
# Arguments:   $1   filename of list of results files to process
# i.e. usage: %  ./run_post_batch.csh resfiles.lst
#
# The usage above assumes one has done: 
# % chmod u+x run_post_batch.csh
#-------------------------------------------------------------------#
# Requirement: The following script must be in the same directory:
# cnvrt_cfx_csv_to_gp.csh
# This second script must be made executable by doing:
# % chmod u+x cnvrt_cfx_csv_to_gp.csh
#-------------------------------------------------------------------#
# This script does the following:
# 1. renames the processed csv files that the state files created with
#    a fixed generic name to have names that match the results file
#    that they came from
# 2. Converts those renamed csv files to be compatible with gnuplot
#    using cnvrt_cfx_csv_to_gp.csh
#-------------------------------------------------------------------#
# IMPORTANT ***
# The names of the files that are renamed and converted below 
# must be the same as the names of the files that are created 
# by the .cse file used.
#-------------------------------------------------------------------#
# name of state file to use
setenv STATE_FILE "general-process.cst"
# name of session file to use
setenv CSE_FILE "save-files.cse"
#
# list of csv files to rename after processing
# (if there are a lot of files to process, create more LISTx_csv
# variables and add them to the list after LIST1_csv below)
setenv LIST1_csv "tstar-horiz-profile.csv vstar-horiz-profile.csv"
# list of text files from table exports to rename after processing
setenv LIST1_txt "sumtable.txt"
#
   if ( $1 == "") then
      echo " Must give an argument: filename of list of files"
   else
#
      if ( -e $1) then
#--------use an external file with list of filenames to process to create the list
         setenv LIST_res "`cat $1`"
#--------loop through all results files in the list
         foreach resfile ( $LIST_res )
            if ( -e $resfile) then
#--------------process the results file to create csv files
               echo " *** About to use cfx5post to process: $resfile"
#
               cfx5post -batch $CSE_FILE -res $resfile -state $STATE_FILE
#
#--------------loop to convert and rename csv files
#              (add other list variables in the next line if needed)
               foreach csvfile ( $LIST1_csv)
                  if ( -e $csvfile) then
#--------------------rename original csv file to have run name prefix
                     /bin/mv $csvfile $resfile:r-$csvfile
#--------------------convert to gnuplot format (creates filename-gr.dat file)
                     ./cnvrt_cfx_csv_to_gp.csh $resfile:r-$csvfile
                 else 
                     echo "Couldn't find $csvfile"
                  endif 
               end
#--------------loop to rename table txt files
               foreach txtfile ( $LIST1_txt)
                  if ( -e $txtfile) then
#--------------------rename original csv file to have run name prefix and csv filetype
                     /bin/mv $txtfile $resfile:r-$txtfile:r.csv
                 else 
                     echo "Couldn't find $txtfile"
                  endif 
               end
            else
               echo "Couldn't find $resfile"
            endif
         end
      else
         echo " Cannot find file: $1"
      endif
#
   endif
#
