#!/bin/csh -f
#
#   Control script to submit mult_refine.com
#
#help
#help frealign_run_search - perform global parameter search
#help Usage: frealign_run_search
#help Control parameters must be set in local mparameters file
#
set working_directory	= `pwd`
if ( ! -e mparameters ) then
  echo ""
  echo "ERROR: no mparameters file."
  echo "Terminating..."
  echo ""
  exit
endif

\rm frealign.log >& /dev/null

set SCRATCH		= `grep scratch_dir mparameters | awk '{print $2}'`
if ( $status || $SCRATCH == "" ) then
  set SCRATCH		= ${working_directory}/scratch
endif
if ( ! -d $SCRATCH ) then
  mkdir $SCRATCH
endif

if ( ! -d $SCRATCH ) then
  echo "ERROR: cannot create scratch directory. Terminating..." >> frealign.log
  exit
endif

set cycle = ""
if ( -e $SCRATCH/pid.log ) then
if ( ${1} != "restart" ) then
set pid = `grep monitor_frealign.log $SCRATCH/pid.log | awk '{print $1}'`
set temp = `ps $pid | grep monitor_frea | grep -v grep | awk '{print $1}'`
if ( $temp != "" ) then
  echo ""
  echo "Frealign search/refinement already running."
  echo "Stop current process before starting new process."
  echo ""
  exit
endif
else
  set cycle = ${2}
endif
endif

set raw_images		= `grep raw_images_high mparameters | awk '{print $2}'`
if ( $raw_images == "" ) set raw_images = `grep raw_images mparameters | awk '{print $2}'`
set raw_images = `echo ${raw_images:r}`
ls $raw_images.* >& /dev/null
if ( $status ) then
  set raw_images = ${working_directory}/${raw_images}
endif
ls $raw_images.* >& /dev/null
if ( $status ) then
  echo ""
  echo "ERROR: particle image stack not found."
  echo "Terminating..."
  echo ""
  exit
endif

set cluster_type	= `grep cluster_type mparameters | awk '{print $2}'`

if (`echo ${cluster_type} | tr '[a-z]' '[A-Z]'` == "SGE") then
  which qstat >& /dev/null
  if ($status) then
    echo ""
    echo -n "No Sun Grid Engine available. Submit jobs to local machine (y/N)?"
    set answer = $<
    echo ""
    if ( $answer != "y" && $answer != "Y" ) exit
    echo none > $SCRATCH/cluster_type.log
  else
    echo SGE > $SCRATCH/cluster_type.log
  endif
endif

if (`echo ${cluster_type} | tr '[a-z]' '[A-Z]'` == "LSF") then
  bjobs >& /dev/null
  if ($status) then
    echo ""
    echo -n "No LSF system available. Submit jobs to local machine (y/N)?"
    set answer = $<
    echo ""
    if ( $answer != "y" && $answer != "Y" ) exit
    echo none > $SCRATCH/cluster_type.log
  else
    echo LSF > $SCRATCH/cluster_type.log
  endif
endif

if (`echo ${cluster_type} | tr '[a-z]' '[A-Z]'` == "SLURM") then
  squeue -u $USER >& /dev/null
  if ($status) then
    echo ""
    echo -n "No SLURM system available. Submit jobs to local machine (y/N)?"
    set answer = $<
    echo ""
    if ( $answer != "y" && $answer != "Y" ) exit
    echo none > $SCRATCH/cluster_type.log
  else
    echo SLURM > $SCRATCH/cluster_type.log
  endif
endif

if (`echo ${cluster_type} | tr '[a-z]' '[A-Z]'` == "PBS") then
  which qstat >& /dev/null
  if ($status) then
    echo ""
    echo -n "No PBS system available. Submit jobs to local machine (y/N)?"
    set answer = $<
    echo ""
    if ( $answer != "y" && $answer != "Y" ) exit
    echo none > $SCRATCH/cluster_type.log
  else
    echo PBS > $SCRATCH/cluster_type.log
  endif
endif

if (`echo ${cluster_type} | tr '[a-z]' '[A-Z]'` == "CONDOR") then
  condor_q >& /dev/null
  if ($status) then
    echo ""
    echo -n "No CONDOR system available. Submit jobs to local machine (y/N)?"
    set answer = $<
    echo ""
    if ( $answer != "y" && $answer != "Y" ) exit
    echo none > $SCRATCH/cluster_type.log
  else
    echo CONDOR > $SCRATCH/cluster_type.log
  endif
endif

cp mparameters $SCRATCH/mparameters_run

set mode	= `grep MODE $SCRATCH/mparameters_run | awk '{print $2}'`
if ( $mode < 1 ) then
  echo "ERROR: MODE must be 1, 2, 3 or 4." >> frealign.log
  echo "Terminating..." >> frealign.log
  exit
endif

set RSEARCH	= `grep res_search $SCRATCH/mparameters_run | awk '{print $2}'`
if ( $status ) then
  echo "ERROR: resolution for search not defined. Please" >> frealign.log
  echo "set res_search parameter in mparameters file." >> frealign.log
  echo "Terminating..." >> frealign.log
  exit
endif

set sym		= `grep Symmetry $SCRATCH/mparameters_run | awk '{print $2}'`
if ( $sym == "H" || $sym == "HP" ) then
  set sum = 0
  grep ALPHA $SCRATCH/mparameters_run >& /dev/null
  @ sum = $sum + $status
  grep RISE $SCRATCH/mparameters_run >& /dev/null
  @ sum = $sum + $status
  grep NSUBUNITS $SCRATCH/mparameters_run >& /dev/null
  @ sum = $sum + $status
  grep NSTARTS $SCRATCH/mparameters_run >& /dev/null
  @ sum = $sum + $status
  grep STIFFNESS $SCRATCH/mparameters_run >& /dev/null
  @ sum = $sum + $status
  if ( $sum ) then
    echo "ERROR: not all helical parameters defined. Please" >> frealign.log
    echo "include the following parameters in mparameters file:" >> frealign.log
    echo "ALPHA, RISE, NSUBUNITS, NSTARTS, STIFFNESS" >> frealign.log
    echo "Terminating..." >> frealign.log
    exit
  endif
endif

set bin_dir		= `grep frealign_bin_dir $SCRATCH/mparameters_run | awk '{print $2}'`
if ( $status || $bin_dir == "" ) then
  set bin_dir		= `which frealign_v9.exe`
  set bin_dir		= ${bin_dir:h}
endif

set start               = `grep start_process mparameters | awk '{print $2}'`
set data_input          = `grep data_input mparameters | awk '{print $2}'`
ls ${data_input}_${start}_r?.* > & /dev/null
if ( ! $status ) then
  echo ""
  echo "Previous par file(s)/map(s) already exists."
  echo -n "Overwrite (y/N)?"
  set answer = $<
  echo ""
  if ( $answer != "y" && $answer != "Y" ) exit
endif

echo "Starting search..." >> frealign.log
echo "Starting search..."

${bin_dir}/mult_search.com $cycle >& $SCRATCH/mult_search.log &
set pid = $!
${bin_dir}/monitor_frealign.com $pid >& $SCRATCH/monitor_frealign.log &
set pid = $!
echo $pid monitor_frealign.log > $SCRATCH/pid.log
echo ""
