#! /bin/csh 

# echo 'entering fakesat_system: case' $1

# set dir = `date +"%d-%h-%y"`
  set dir = .

#  echo 'dir: ' $dir

 if(! -d $dir)  mkdir $dir

#*****************************************
switch ($1)
#*****************************************

#*****************************************
case date:
#*****************************************

 /bin/rm .date

 echo $dir > .date

 echo 'creating the date file: '
 cat .date

breaksw

#*****************************************
case exit:
#*****************************************

echo ' '
echo '*** a fakesat process has finised ***'
echo ' '


breaksw

#*****************************************
case kill:
#*****************************************

#  echo 'case kill: '

 echo ' '
 echo 'fakesat jobs running: '
 echo ' '

 ps | grep fakesat | grep -v grep | grep -v menu | grep -v kill | grep -v sys | sort -r

 set a = `ps | grep fakesat | grep -v grep | grep -v menu | grep -v kill | grep -v sys| sort -r`

 echo  ' '
 echo 'killing job #: ' $a[1]
 echo ' '

 kill -9 $a[1]

 echo ' '
 echo 'fakesat jobs NOW running: '
 echo ' '

 ps | grep fakesat | grep -v grep | grep -v menu | sort -r

breaksw

#*****************************************
case ls_plot:
#*****************************************

#  echo 'case ls_plot: '

# /bin/ls -str1 $dir/fakesat_plot*
  /bin/ls -str1 $dir/plt*

breaksw

#*****************************************
case ls_def:
#*****************************************

#  echo 'case ls_def: '
# /bin/ls -str1 $dir/fakesat_def*
 /bin/ls -str1 $dir/def*

breaksw

#*****************************************
case ls_model:
#*****************************************

#  echo 'case ls_model: '
 /bin/ls -1 model

breaksw


#*****************************************
case ls_plot_num:
#*****************************************

#  echo 'case ls_plot_num: '

# /bin/ls -lc $dir/fakesat_plot*|wc -l
 /bin/ls -lc $dir/plt*|wc -l

breaksw

#*****************************************
case ls_def_num:
#*****************************************

# echo 'case ls_def_num: '

#  /bin/ls -lc $dir/fakesat_def*|wc -l
   /bin/ls -lc $dir/def*|wc -l

breaksw

#*****************************************
case proc:
#*****************************************

#  echo 'case proc: '
  ps |grep fakesat|grep -v gre |grep -v system

breaksw

#*****************************************
case stdiff:
#*****************************************

# echo 'case tdiff: '

if( -e .tmp-n) then
 /bin/rm .tmp-n
endif

cmp -s stations.cat .stations.cat.old

if($status) then
# echo 'case 1:'
 set n = 1
else
# echo 'case 2:'
 set n = 0
endif

echo $n > .tmp-n


breaksw


#*****************************************
case stdiff-old:
#*****************************************

 echo 'case tdiff: '

if( -e .tmp-time1) then
 /bin/rm .tmp-time1
endif

if( -e .tmp-time2) then
 /bin/rm .tmp-time2
endif

if( -e .tmp-n) then
 /bin/rm .tmp-n
endif


set t1 = `ls -l stations.cat`
set t2 = `ls -l .stations.cat.old`

# echo $t1
# echo $t2

echo $t1[5-7]  > .tmp-time1 
echo $t2[5-7]  > .tmp-time2 

# echo $t1[5-7] 
# echo $t2[5-7] 

# cat  .tmp-time1
# cat  .tmp-time2

set n = `diff .tmp-time1 .tmp-time2|wc -l`

# echo $n

echo $n > .tmp-n

# cat .tmp-n

breaksw

#*****************************************
case mv_def:
#*****************************************


# set a = `/bin/ls fakesat_def*`
# set a = `/bin/ls  def*  def*`

# keep def files is user directory

  set a = `/bin/ls  def*  `

 # echo ' mv_def: ' 
 # echo $a

 foreach i (`echo $a`)
#  echo 'moving ' $i ' to '$dir/$i
  /bin/mv -f $i $dir/$i
 end

breaksw

#*****************************************
case mv_plot:
#*****************************************

 echo ' mv_plot: ' 

 set a = `/bin/ls fakesat_plot*`
 set a = `/bin/ls plt*`

 echo $a

 foreach i (`echo $a`)
#  echo 'moving ' $i ' to '$dir/$i
  /bin/mv -f $i $dir/$i
 end

breaksw

#*****************************************
case visplot:
#*****************************************

echo ' '
echo '****************** WARNING **************************'
echo '*** This option requires the CALTECH VLBI package ***'
echo '*****************************************************'
echo ' '

set prog  = visplot
set found = 0

foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog) then
   set found = 1
   set found_dir = ${dir}
   break
 endif
endif
end

if($found == 1) then

 echo $prog 'is in the directory: ' $found_dir

visplot << EOD
 input OUTPUT
 amp
 phase -1
 close -1 
 plotf '/xw'
 np 10
 ut
EOD

else

echo ' '
echo '**************** ERROR *****************'
echo '*** cannot find CALTECH VLBI package ***'
echo '*** unable to perform required task  ***'
echo '****************************************'
echo ' '

endif

/bin/rm OUTPUT


breaksw


#*****************************************
case dirty_beam:
#*****************************************

/bin/rm invert.lis
/bin/rm BEAM
/bin/rm MAP

echo ' '
echo '****************** WARNING **************************'
echo '*** This option requires the CALTECH VLBI package ***'
echo '*****************************************************'
echo ' '

set prog  =  invert
set found = 0

foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog) then
   set found = 1
   set found_dir = ${dir}
   break
 endif
endif
end

if($found == 1) then

 echo $prog 'is in the directory: ' $found_dir

invert << EOD
 input OUTPUT
 xyint $2
 plotbeam
 plotf '/xw'
 maxp 20000
 nomap
EOD

grep  'Estimated beam ' invert.lis
grep Major invert.lis
grep Minor invert.lis
grep PA    invert.lis
grep Axial invert.lis


# cat invert.lis

/bin/rm invert.lis
/bin/rm BEAM
/bin/rm MAP
/bin/rm OUTPUT

else

echo ' '
echo '**************** ERROR *****************'
echo '*** cannot find CALTECH VLBI package ***'
echo '*** unable to perform required task  ***'
echo '****************************************'
echo ' '

endif


breaksw

#*****************************************
case clean_map:
#*****************************************


echo ' '
echo '****************** WARNING **************************'
echo '*** This option requires the CALTECH VLBI package ***'
echo '*****************************************************'
echo ' '

set prog1  =  invert
set prog2  =  clean
set prog3  =  mapstat
set prog4  =  mapplot
set found  = 0

foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog1 && -e $dir/$prog2 \
  && -e $dir/$prog3 && -e $dir/$prog4) then
   set found = 1
   set found_dir = ${dir}
   break
 endif
endif
end

if($found == 1) then

/bin/rm invert.lis
/bin/rm clean.lis
/bin/rm mapstat.lis
/bin/rm BEAM
/bin/rm MAP
/bin/rm CLEAN
/bin/rm MODEL

invert << EOD
 input OUTPUT
 xyint $2
 plotf '/xw'
 maxp 20000
/
EOD

echo ' '
grep 'Estimated beam' invert.lis
grep Minor       invert.lis
grep Major       invert.lis
grep Position    invert.lis
grep Axial       invert.lis
echo ' '


clean << EOD
 niter = 1000
 loopgain = 0.1
 cleanmap = 'CLEAN'
 plotf '/xw' 
! plotmap
 /
EOD

echo ' '
grep negative clean.lis
echo ' '

mapstat << EOD
 mapf CLEAN
 xrange 10,20
 yrange 10,20
 listf mapstat.lis
 /
EOD

set a = `grep RMS mapstat.lis`
set rms = $a[4]

echo  ' '
echo 'RMS noise off source (in Jy/beam) is: ' $rms
echo ' '

mapplot << EOD
 plotf '/xw'
 mapf CLEAN
 plev 0
 clev $rms
 levs -12,-6,-3,3,6,12,24,48,96,192,384,768,1536,3072,6144
/
EOD

/bin/rm invert.lis
/bin/rm clean.lis
/bin/rm mapstat.lis
/bin/rm BEAM
/bin/rm MAP
/bin/rm CLEAN
/bin/rm MODEL
/bin/rm OUTPUT

else

echo ' '
echo '**************** ERROR *****************'
echo '*** cannot find CALTECH VLBI package ***'
echo '*** unable to perform required task  ***'
echo '****************************************'
echo ' '

endif


breaksw

#*****************************************
case modplot:
#*****************************************

cho ' '
echo '****************** WARNING **************************'
echo '*** This option requires the CALTECH VLBI package ***'
echo '*****************************************************'
echo ' '

set prog1  =  invert
set prog2  =  modplot
set found  = 0

foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog1 && -e $dir/$prog2 ) then 
   set found = 1
   set found_dir = ${dir}
   break
 endif
endif
end

if($found == 1) then

/bin/rm invert.lis
/bin/rm clean.lis
/bin/rm mapstat.lis
/bin/rm BEAM
/bin/rm MAP
/bin/rm CLEAN
/bin/rm MODEL

echo ' '
echo 'using a cellsize (mas) of: ' $2

invert << EOD
 input OUTPUT
 xyint $2
 plotf '/xw'
 maxp 20000
/
EOD

echo ' '
grep 'Estimated beam' invert.lis
grep Minor       invert.lis
grep Major       invert.lis
grep Position    invert.lis
grep Axial       invert.lis
echo ' '

set b1 = `grep Major       invert.lis`
set b2 = `grep Minor       invert.lis`
set b3 = `grep Position    invert.lis`

set a      = `cat .model`

set size   =  $a[1]
set freq   =  $a[2]
set modfile = $a[3]

echo  '$a is ' $a
echo  '$a[1] is ' $a[1]
echo  '$a[2] is ' $a[2]
echo  '$a[3] is ' $a[3]

echo 'using Frequency: ' $freq
echo 'using modfile  : ' $modfile
echo 'using size     : ' $size



modplot << EOD
 lrtb -$size,$size,$size,-$size
 beam $b1[4],$b2[4],$b3[6]
 freq = $freq
 modfile = $modfile
 plotf '/xw'
 /
EOD


/bin/rm invert.lis
/bin/rm clean.lis
/bin/rm mapstat.lis
/bin/rm BEAM
/bin/rm MAP
/bin/rm MODEL
/bin/rm OUTPUT
/bin/rm .model

else

echo ' '
echo '**************** ERROR *****************'
echo '*** cannot find CALTECH VLBI package ***'
echo '*** unable to perform required task  ***'
echo '****************************************'
echo ' '

endif


breaksw

#*****************************************
case test:
#*****************************************

echo 'fakesat_system: test '

breaksw


#*****************************************
case help:
#*****************************************

echo ' '
echo 'fakesat_system: help '
echo ' '

set prog1  =  latex
set found1  = 0



foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog1) then 
   set found1 = 1
   set found_dir1 = ${dir}
   break
 endif
endif
end

set prog2  =  xdvi
set found2  = 0

foreach dir ($path)
if(-d $dir ) then
 if(-e ${dir}/$prog2) then 
   set found2 = 1
   set found_dir1 = ${dir}
   break
 endif
endif
end


if($found1 == 1 && $found2 == 1) then

  if(-e tex.tmp) then
   /bin/rm -r tex.tmp
  endif
 
  set a = `tail -2 help.tex | head -1`




  echo 'running latex ... ' 
  echo 'on: '$a[2]

  latex help.tex >& tex.tmp

# find out if there is an error with latexing file

  set line = (`grep type tex.tmp`)
  set n = ${#line}

  if( $n != 0 ) then
   echo  ' '
   echo '*** latex error message ***'
   cat tex.tmp
   echo ' '
   echo '*** unable to display help information ***'
   echo ' '
#   exit
  endif

  /bin/rm -r tex.tmp


# ! two calls to latex required to get toc correct

 if($2 == 2) then
  echo 'running latex a second time....'
  latex help.tex > /dev/null
 endif

  echo 'starting up xdvi ...'

  (xdvi help.dvi &) > /dev/null

else
 echo ' '
 echo '*** cannot provide help ***'

 if($found1 == 0) then
 echo '*** program: '$prog1' does not exist ***'
 endif

 if($found2 == 0) then
 echo '*** program: '$prog2' does not exist ***'
 endif

 echo ' '
endif

breaksw

#*****************************************
default:
#*****************************************

echo 'fakesat_system: case ' $1 ' not found'

breaksw

#*****************************************
endsw
#*****************************************



exit
