#!/bin/csh -f
set bestbin = $besthome/bestbin
#
set arg = ( $argv )
# pars the command line
#
# new commands
set minexposure = 0.000000000000000018 
set maxspeed = 3000000000
set maxtime = 999999.9
set dphimin = 0.2
set split = mean
set anomalous = n
set i2sig = 2
set usertime = 0
set resolution = 0
set phi = ( 0 0 )
set redundancy = 0
set const = ( 0 0 )
set comlimit = 0.99
#
#old commands
#
set narg = 1
set ev = 1
set dose = 0
set minitime = d
set dnafile = /dev/null
while ( $narg <= $#arg )
   switch ( $arg[$narg] )
      case -h
         set ev = 0
         goto help
	 breaksw
      case -g
	 set graph
	 breaksw
      case -m
         @ narg++
         if ( $narg > $#arg ) goto help
         set multyread = $arg[$narg]
	 breaksw
      case -l
         cat $besthome/detector-inf.dat
	 exit
	 breaksw
      case -o
         @ narg++
         if ( $narg > $#arg ) goto help
         set graph
	 set mtvout = $arg[$narg]
	 breaksw
      case -f
         @ narg++
	 if ( $narg > $#arg ) goto help
         set format = $arg[$narg]
         breaksw
      case -t 
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set exptime = $arg[$narg] 
	 breaksw
      case -s 
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set insca = $arg[$narg]
	 breaksw
      case -r
      case -resolution
      case -resolutio
      case -resoluti
      case -resolut
      case -resolu
      case -resol
      case -reso
      case -res
      case -re
      case --resolution
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set resolution  = $arg[$narg]
	 breaksw
      case -e
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set split  = $arg[$narg]
	 breaksw
      case -d
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set dose  = $arg[$narg]
	 breaksw
      case -w
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set dphimin = $arg[$narg]
	 breaksw
      case -a
	 set anomalous = y
	 breaksw	 
      case -ut
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set usertime = $arg[$narg]
	 breaksw
      case -phi
      case -ph
      case -p
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set phi = $arg[$narg]
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set phi = ( $phi $arg[$narg] )
	 breaksw
      case -R
      case -red
      case -redun
      case -redund
      case -redundan
      case -redundancy
      case -Red
      case -Redun
      case -Redund
      case -Redundancy
      case --Redundancy
      case --redundancy
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set redundancy = $arg[$narg]
	 breaksw
      case -constant
      case -const
      case -cons
      case -con
      case -co
      case -c
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set const = $arg[$narg]
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set const = ( $const $arg[$narg] )
	 breaksw
      case -Completeness
      case -completeness
      case -Complet
      case -complet
      case -Compl
      case -compl
      case -Comp
      case -comp
      case -C
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set comlimit = $arg[$narg]
	 breaksw
      case -q
	 set minitime = t 
	 breaksw
      case -v
         set verbose
	 set echo
	 breaksw
      case -i2s
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set i2sig  = $arg[$narg] 
	 breaksw
      case -mosflm
      case -mosfl
      case -mosf
      case -mos
      case -mo
#           mosfilm, no image input
	 set mosflm
	 if ( $?best_exposure_timer ) unsetenv best_exposure_timer
	 if ( $?best_multyread ) unsetenv best_multyread
	 breaksw
      case -dna
         @ narg++
	 if ( $narg > $#arg ) goto help
         set dnafile = $arg[$narg]
	 breaksw
      case -S
      case -Speed
      case -speed
      case -maxspeed
         @ narg++
	 if ( $narg > $#arg ) goto help
         set maxspeed  = $arg[$narg]
	 breaksw         
      case -T
      case -Total
      case -total
      case -maxtime
         @ narg++
	 if ( $narg > $#arg ) goto help
         set maxtime  = $arg[$narg]
	 breaksw         
      case -M
      case -Minexposure
      case -minexposure
         @ narg++
	 if ( $narg > $#arg ) goto help
         set minexposure = $arg[$narg]
	 breaksw         
      default:
         set image = $arg[$narg] 
	 @ narg++
	 set dotx
	 set ndotx = 0
	 while ( $narg <= $#arg )
	     set dotxadd  = $arg[$narg]
	     @ ndotx++
	     set dotx = ( $dotx $dotxadd )
	     @ narg++
	 end
   endsw
   @ narg++
end
#
# verify if all the necessary input is input:
# format, image, .x file(s) and exposure time
if !( $?image     )then
   if ( $?mosflm ) then
      echo 'ERROR: no mosflm files specified' >& /dev/tty
      goto help
   endif	
   echo 'ERROR: no image file specified' >& /dev/tty
   goto help
endif
if !( $?ndotx     ) then
   echo 'ERROR: no x file specified' >& /dev/tty
   goto help
endif
if  ( $ndotx < 1  )then
   if ( $?mosflm ) then
      echo 'ERROR: not enough mosflm files' >& /dev/tty
      goto help
   endif	
   echo 'ERROR: no x file specified' >& /dev/tty
   goto help
endif
if  ( ( $?mosflm ) &&  ( $ndotx < 2 )  )then
   echo 'ERROR: not enough mosflm files' >& /dev/tty
   goto help
endif
if !( $?format    ) then 
    if ! ( $?best_default_detector ) then
         echo 'ERROR: no detector specified' >& /dev/tty
         goto help
    endif
    set format = $best_default_detector
    echo Default detector $best_default_detector assumed.        
    if ! ( $?multyread   ) then
        if ( $?best_multyread ) then
            set multyread = `$best_multyread $image`
            if $status exit 1
	    if ( $multyread != 1 ) then
	       echo Multyread 2
	    endif
        else
            set multyread = 1
        endif
   endif
endif
if ! ( $?exptime ) then
   if ( $?best_exposure_timer ) then
       set exptime = `$best_exposure_timer $image`
       if $status exit 1
       echo Exposure time = $exptime seconds.
   else
       echo 'ERROR: no exposure time specified' >& /dev/tty
       goto help
   endif
endif
if !( $?multyread ) set multyread = 1
#
#
if !( $?SYMOP ) then
    setenv SYMOP ${CLIBD}/symop.lib
endif
#
if ( $?insca ) then
    if ( $?mosflm ) then
       echo 'ERROR: use either mosflm or scalepack' >& /dev/tty
       goto help
    endif		
    set ewtype = s
    set xsarg = ( $dotx[1] $insca )
else
    set ewtype = x
    set xsarg = ( $dotx )
endif
if ( $?mosflm ) set ewtype = m
# 
if ( $?graph ) then
    set ewmtv = ( $CCP4_SCR/wilson_err.mtv_$$ $CCP4_SCR/wilson.mtv_$$ )
    if $status exit 1
    set esmtv = ( $CCP4_SCR/phimax.mtv_$$ $CCP4_SCR/complit.mtv_$$)
    if $status exit 1
    onintr -
    set toclean = ( $ewmtv $esmtv ) 
else
    set ewmtv = ( /dev/null /dev/null )
    set esmtv = ( /dev/null /dev/null )
endif
#
if( $split == 'min' ) set split = mean 
# if( ( $splitoprtim != 'mean' & 
#
#  
# 0
set command =  $bestbin 
# 1
set command = ( $command $besthome/detector-inf.dat )
# 2
set command = ( $command  $format  )         
# 3
set command = ( $command  $exptime )                 
# 4
set command = ( $command  $multyread )                 
# 5 preset counts of initial image, 0 if time mode
set command = ( $command  $dose ) 

# 6 minimal allowed exposure time
set command = ( $command  $minexposure  )
# 7 maximal allowed rotation speed
set command = ( $command  $maxspeed )              
# 8 maximal allowed total data collection (if $minitime=t) or exposure (if d)
# used for calulation of maximum resolution only 
set command = ( $command  $maxtime  )
#
# 9 not used
set maxdose = 0
set command = ( $command  $maxdose  )
# 10 not used
set mincd = 0
set command = ( $command  $mincd )
# 11 
set command = ( $command  $minitime  )
# 12 x (x ) or s (scalepack) or m (mosflm) 
set command = ( $command  $ewtype )
# 13 minimum allowed rotation width
set command = ( $command  $dphimin ) 
# 14 split - none, mean, full
set command = ( $command  $split )
# 15 anomalous y or n 
set command = ( $command  $anomalous )

# 16 isigi
set command = ( $command  $i2sig )
# 17 not used
set damlim = 0
set command = ( $command  $damlim )
# 18 usertime
# maximal allowed total data collection (if $minitime=t) or exposure (if d)
# used for calulation of strategy (plan)
# if $usertime=0, $usertime is reset = $maxtime (#8) 
set command = ( $command  $usertime )
# 19 resolution limit for plan, if = 0
# calulated automatically by #18 
set command = ( $command  $resolution )
# 20,21 phi start and range from user
# if #21<=0, then #20,21 automatic
set command = ( $command  $phi )
# 22 redundancy, if 0 automatic
set command = ( $command  $redundancy )
# 23,24 invokes constant if #23> 0 and #19>0 and $24>0
set command = ( $command  $const )
# 25 completeness, default 0.99
set command = ( $command  $comlimit )
# 26 not used, must be none
set damfile = none
set command = ( $command  $damfile )
# 27 not used, must be none
set addcol = none
set command = ( $command  $addcol )  
# 28, 29, 30, 31 files for graphs
set command = ( $command  $esmtv $ewmtv )
# 32 dna xml file
set command = ( $command  $dnafile )

# 33 images or first mosflm file             
set command = ( $command  $image )                   
# 34-Infinity first x or second  mosflm till last x or sca or last mosflm 
set command = ( $command  $xsarg )                
#
$command

if $status goto cleenup
#
#
if ( $?graph )  then
   plotmtv  -nodate -geom 550x400+0+0 -pfg white -pbg black  $CCP4_SCR/*mtv_$$ >& /dev/null &
   sleep 2
endif
#
if ( $?mtvout ) then
    echo > $mtvout
    foreach x ( $toclean )
        if -rw $x cat $x | grep -v '\$ END' >> $mtvout
        if $status goto cleenup
    end
endif
#
set ev = 0
#
cleenup:
if ( $?toclean ) then
    foreach x ( $toclean )
        if -rw $x /bin/rm $x
        if $status exit 1
    end
endif
exit $ev
#
#
#
help:
echo usage:: `basename $0` "[ <options> ] <image file1.x {file2.x ...}> <-mo(sflm) file.dat file.par file0.hkl {file1.hkl ...}>"
echo
echo " -h  gives this help message"
echo " -t <exposure time in seconds>"
echo " -f <format> "
echo " -i2s  <I/SigI>, aimed I/SigI at highest resolution, default 2.0"
echo " -T(otal) <number>, maximum total exposure(dose), sec, (default long)"
echo " -S(peed) <number>, maximum rotation speed, deg/sec, (default fast)"
echo " -M(inexposure) <number>, minimum exposure per frame, sec, (default short)"
echo " -e <none|min|full>, anisotropy correction level, default <full> "
echo " -R(redundancy) <number>, aimed redundancy, default minimum"
echo " -C(ompliteness) <number>, aimed completeness, default 0.99"
echo " -a  anomalous on, default off"
#
echo " -w <number>, minimum rotation width per frame, deg., default 0.20"
echo " -r <aimed resolution in (Angstrom)>, default automatic (by -T), >= ref.frame >= 0.9"
echo " -d <preset counts>, default time mode"
echo " -q minimize total time, default minimize the absorbed dose"
echo " -c(onstant) <dphi> <exposuretime>, calculate stats for fixed (-r,-phi dependent) "
echo " -p(hi) <start> <range>, user defined rotation range, default auto"
echo " -m {<number of detector read outs>}"
echo " -s <.sca file name>, default - use .x file(s)"
echo " -l show configured image formats" 
echo " -g  some usefull plots generated (plotmtv required), default not"
echo " -o <file> plots generated and stored in <file>"
echo " -dna <file> xml formatted data stored in <file> "
exit $ev
#










