#! /bin/csh -f
#
# make a complete TRANSP input dataset, starting with a traditional,
# Ufile-based namelist.  Caution: MDSplus based namelists will not work!
#
#   arguments:  <tokamak> <runid>
#     <tokamak> should be the usual TRANSP 3- or 4-character tokamak id
#     string, such as "D3D" or "TFTR".
#
#   expect to find:  <runid>TR.DAT in current working directory.
#
# this procedure does the following:
#
#   0.  check arguments; check existence of <runid>TR.DAT; exit on error.
#   1.  remove any subdirectory <runid>_tmp that might exist.
#   2.  create a clean <runid>_tmp subdirectory.
#   3.  copy the namelist into this subdirectory.
#       (3a: copy <runid>AUX.TMP & <runid>CC.TMP files as well, if found).
#   4.  extract INPUTDIR from the namelist (if it exists); use this
#       (or its absence) to determine directory containing Ufiles;
#       (exit with message if directory does not exist).
#   5.  scan namelist for Ufile references; assemble list.
#   6.  copy each Ufile into <runid>_tmp
#       (report error) if any are found.
#       (exit if there are any errors).
#   7.  write directory contents into <runid>_tmp.tar
#       (7a: <runid>_<tok>.REQUEST also gets written)
#   8.  gzip the tar file.
#   9.  remove any subdirectory <runid>_tmp that might exist.
#
# NB dmc 11-12-03 -- this might not work correctly if there are "tab"
# characters in the namelist; have not tested for this...
#
# NOTE final contents of tar file:
#
# Normal
#    <runid>_<tok>.REQUEST
#    <runid>_tmp/<runid>TR.DAT
#    <runid>_tmp/**all the Ufiles**
#    <runid>_tmp/<runid>CC.TMP
#    <runid>_tmp/<runid>AUX.TMP
#
# If TREES:
#    <runid>_<tok>.REQUEST
#    <runid>_tmp/tree triplets 
#    <runid>_tmp/<runid>AUX.TMP
#
#---------------------------------------------------------------
#  step 0:
#
  set PGM = "datprep"
  set ERR = "$PGM -E- "
  set WAR = "$PGM -W- "
  set INF = "$PGM -I- "
  if ($#argv < 2 ) then
    echo " $ERR  syntax:  datprep <tokamak-id> <runid> [<tree> <tree_path>]"
    exit 1
  endif
#
  set runid = $2
  set tokid = $1
# Check if want Tree Files
  if ($#argv == 4 ) then
    set mdsid = $3
    set tree_path = $4 
    echo "tree_path=$tree_path" 
    if (! -d $tree_path) then
       echo " $ERR $tree_path does not exist"
       exit 1
    endif
    set tree = `basename $tree_path`
    echo $tree
    set tree_file=$tree_path/${tree}_${mdsid}.tree
    if (! -f $tree_file) then
       echo " $ERR $tree_file does not exist"
       exit 1
    endif
    set TREES=Y
  endif
#
  set namelist = ${runid}TR.DAT
  if ( ! -f $namelist ) then
    echo " $ERR  not found:  $namelist"
    exit 1
  endif
#
  set ccfile = ${runid}CC.TMP
  set auxfile = ${runid}AUX.TMP
#
  set subdir = ${runid}_tmp
#
#---------------------------------------------------------------
#  step 1:
#
  rm -r -f $subdir
#
#---------------------------------------------------------------
#  step 2:
#
  mkdir $subdir
#
#---------------------------------------------------------------
#  step 3:
#
  cp $namelist $subdir
#
#
  if ( -f $auxfile ) then
    cp $auxfile $subdir
  else
    echo " $WAR $auxfile not found."
  endif
# 
  if ($?TREES) then
     goto DO_TREES
  endif
#
  if ( -f $ccfile ) then
    cp $ccfile $subdir
  else
    echo " $WAR $ccfile not found."
  endif
#
#---------------------------------------------------------------
#  step 4:
#     strip comments; find "INPUTDIR = " lines; pick off values; strip quotes
#
  cat $namelist |  sed 's#\ *\!.*##' | \
    grep -i '^\ *inputdir\ *=.*' | \
    sed 's#^.*=\ *##' | sed "s#\ *'\ *##g" | sed 's#\ *"\ *##g' \
       > $subdir/input.tmp
#
  set inputd_list = `cat $subdir/input.tmp`
  rm $subdir/input.tmp
#
# if list is empty, input directory should be $DATADIR/$tokid
#
  set ilen = $#inputd_list
  set relpath = FALSE
  if ( $ilen == 0 ) then
    set relpath = TRUE
    if ( $?DATADIR == 0 ) then
      echo " $ERR  INPUTDIR defaulted in namelist and "DATADIR" is undefined."
      exit 1
    endif
  else
    if ( $ilen > 1 ) then
      echo " $WAR multiple INPUTDIR values in namelist, last one used: $inputd_list"
    endif
    set zpath = $inputd_list[$ilen]
    set tpath = `echo $zpath | sed 's#^/##'`
    if ( "$tpath" == "$zpath" ) then
      set relpath = TRUE
      if ( $?DATADIR == 0 ) then
        echo " $ERR  in namelist:  INPUTDIR = $zpath "
        echo " $ERR  INPUTDIR specifies relative path but "DATADIR" is undefined."
        exit 1
      endif
    endif
  endif
#
  if ( "$relpath" == "TRUE" ) then
    if ( $ilen == 0 ) then
      set zpath = $DATADIR/$tokid
    else
      set zpath = $DATADIR/$tokid/$zpath
    endif
  endif
#
  if ( ! -d $zpath ) then
    echo " $ERR  in namelist:  INPUTDIR leads to path:  $zpath"
    echo " $ERR  indicated path (directory) not found."
    exit 1
  endif
#
#  alternate path -- could be used for generic data e.g. A.RPL file.
#
  set zpath_alt = $zpath
  if ( $?DATADIR == 1 ) then
    set zpath_alt = $DATADIR/$tokid
  endif
#---------------------------------------------------------------
#  step 5:  get Ufile list...
#
  cat $namelist |  sed 's#\ *\!.*##' | \
    grep -i '^\ *nshot\ *=.*' | \
    sed 's#^.*=\ *##' | sed "s#\ ##g" \
       > $subdir/nshot.tmp
#
  set nshot = `cat $subdir/nshot.tmp`
  rm $subdir/nshot.tmp
  if ( $#nshot == 0 ) then
    echo " $ERR  failed to find NSHOT in namelist."
    exit 1
  else
    set nshot = $nshot[$#nshot]
  endif
#
# filename prefix definitions
#
  cat $namelist | sed 's#\ *\!.*##' | \
    grep -i '^\ *pre.*=' | \
    sed 's#\ ##g' | sort > $subdir/pre.tmp
#
# just the prefixes -- forced uppercase
#
  cat $subdir/pre.tmp | awk -F\= '{print $1}' | \
    sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
    > $subdir/prenam.tmp
#
# just the values -- quotes stripped
#
  cat $subdir/pre.tmp | awk -F\= '{print $2}' | sed 's#"##g' | sed "s#'##g" \
    > $subdir/preval.tmp
#
# filename extension definitions
  cat $namelist | sed 's#\ *\!.*##' | \
    grep -i '^\ *ext.*=' | \
    sed 's#\ ##g' | sort > $subdir/ext.tmp
#
# just the suffixes -- forced uppercase
#
  cat $subdir/ext.tmp | awk -F\= '{print $1}' | \
    sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
    > $subdir/extnam.tmp
#
# just the values -- quote stripped
#
  cat $subdir/ext.tmp | awk -F\= '{print $2}' | sed 's#"##g' | sed "s#'##g" \
    > $subdir/extval.tmp
#
# fetch results, cleanup files
#
  rm $subdir/pre.tmp
  rm $subdir/ext.tmp
#
  set pre_names = `cat $subdir/prenam.tmp`
  rm $subdir/prenam.tmp
#
  set pre_vals = `cat $subdir/preval.tmp`
  rm $subdir/preval.tmp
#
  set ext_names = `cat $subdir/extnam.tmp`
  rm $subdir/extnam.tmp
#
  set ext_vals = `cat $subdir/extval.tmp`
  rm $subdir/extval.tmp
#
  if ( $#pre_names != $#ext_names ) then
    echo " $WAR  number of namelist Ufile prefixes, suffixes do not match."
  endif
#
# build Ufile list
#
  set ufile_list = ()
#
  set match_check = ()
  @ iext = 0
  while ( $iext < $#ext_names )
    @ iext++
    set match_check = ( $match_check F )
  end
#
  @ ierr = 0
  @ ipre = 0
  @ iext_save = 0

  # loop through prefixes found
  while ( $ipre < $#pre_names ) 
    @ ipre++
    set prename = $pre_names[$ipre]
    set tri = `echo $prename | sed 's#^PRE##'`
    set sufname = EXT$tri
  
    #loop through suffixes found
    @ iext = $iext_save
    @ imatch = 0
    while ( $iext < $#ext_names ) 
      @ iext++
      set test = $ext_names[$iext]
      if ( "$test" == "$sufname" ) then
        # match found
        @ iext_save = $iext
        @ imatch = $iext
        set match_check[$imatch] = T
        @ iext = $#ext_names
      endif
    end

    if ( $imatch == 0 ) then
      @ ierr++
      echo " $ERR  namelist lacks matching suffix for prefix: $prename"
    else
      set prefix = $pre_vals[$ipre]
      set suffix = $ext_vals[$imatch]
      set ufile = $zpath/$prefix$nshot.$suffix
      set ufile0 = $ufile
      if ( ! -f $ufile ) then
        set ufile = $zpath/$prefix.$suffix
        if ( ! -f $ufile ) then
          set ufile = $zpath_alt/$prefix.$suffix
        endif
        if ( ! -f $ufile ) then
          set prefixu = `toupper $prefix`
          set suffixu = `toupper $suffix`
          set ufile = $zpath/$prefixu$nshot.$suffixu
        endif

      endif
      if ( ! -f $ufile ) then
        @ ierr++
        echo " $ERR  Ufile not found: $ufile "
      else
        set ufile_list = ( $ufile_list $ufile )
      endif        
    endif
  end

  # check that all suffixes were matched...
  @ iext = 0
  while ( $iext < $#ext_names )
    @ iext++
    if ( $match_check[$iext] == "F" ) then
      @ ierr++
      echo " $ERR  namelist lacks matching prefix for suffix: $ext_names[$iext]"
    endif
  end

  if ( $ierr > 0 ) then
    echo " $ERR  Ufile processing errors encountered."
    exit 1
  endif

#---------------------------------------------------------------
#  step 6:  copy the Ufiles -- known to exist...
#
  @ iuf = 0
  while ( $iuf < $#ufile_list )
    @ iuf++
    cp $ufile_list[$iuf] $subdir
  end
#
# step 6.1
# Check if parallel job
## End 05/04/2010 CLF: obsolete
# grep -i "^\ *nbi_pserve\ *=.*" $namelist > /dev/null 
# if ( $status == 0 ) then
#   hostname | grep -i 'pppl\.gov'
#   if ( $status ) then 
## Need proxy 
#     if ($?X509_USER_PROXY) then
#        set proxy=$X509_USER_PROXY
#     else
#        set proxy=/tmp/x509up_u`id -u`
#     endif
#     tr_ckproxy $proxy
#     if ( $status ) then
#        echo " $ERR  No valid proxy file found"
#        exit 1
#     endif
#     cp  -fp $proxy $subdir/$runid.x509
#   endif
# endif
## End 05/04/2010 CLF
 goto TEMPLATE
#
#---------------------------------------------------------------
#  step 6-A:  copy the Tree files
#
DO_TREES:
    cp $tree_file $subdir
    cp $tree_path/${tree}_${mdsid}.characteristics $subdir
    cp $tree_path/${tree}_${mdsid}.datafile $subdir
# Need proxy
# 01/06/2010 CLF: eliminated proxy requirement
#    set proxy=x509up_u`id -u`
#    if ($?X509_USER_PROXY) then
#       cp  -p $X509_USER_PROXY $subdir/$proxy
#    else if (-f /tmp/$proxy) then
#       cp -p /tmp/$proxy $subdir/$proxy
#    else
#       echo " $ERR  No proxy file found"
#    endif
    
#---------------------------------------------------------------
#  step 7:  check if user provides customized template
#
# ls will likely not find one of *.RFTMP or *.CQL3DTMP files which is ok
#
TEMPLATE:
  ck_tmplate $runid $tokid start
  if ( $status >= 2 ) then
     set LIST = `ls ${runid}_*.RFTMP ${runid}_*.CQL3DTMP`
     @ itmp = 0
     while ( $itmp < $#LIST )
        @ itmp++
        echo $LIST[$itmp]
        set t_file =  `cat $LIST[$itmp]`
        if ( ! -f $t_file ) then
           echo " $ERR $tfile not found."
        else
           cp $t_file $subdir/
        endif
        rm -f $LIST[$itmp]
     end
  endif
#---------------------------------------------------------------
#  step 8:  tar up the ufiles/namelist dataset
#
TAR_FILE:
  set tarfile = ${runid}_${tokid}_tmp.tar
  rm -f ${tarfile}* >& /dev/null
  tar cf $tarfile ${runid}_${tokid}.REQUEST $subdir/* 
#
#---------------------------------------------------------------
#  step 8:  compress the tar file
#
  gzip $tarfile
#
#---------------------------------------------------------------
#  step 9:  remove the temporary subdirectory
#
  rm -r -f $subdir
  echo " $INF  created $tarfile"
#
#---------------------------------------------------------------
  exit 0
