#!/bin/csh -f
#
#* beamgen2 - Plot Array Response to Extended Structure.
#& mchw
#: user tool
#+
# beamgen2 makes an image from a model source using the ``uvgen'' task.
# The source model must be specified. (mirhelp uvgen for format).
#  beamgen2 plots the synthesised and cleaned images, and the flux density
#  as a function of radius. 
#  array is the name of a text file containing the positions of the antennas.
#  baseunit specifies the units for the antenna file. The default is
#  nanosec units in geocentric coordinates (baseunit=1).
#  ANTPOS files in geocentric coordinates obtained from fitted baselines
#  using the task BEE can be used (baseunit=1).
#--
#------------------------------------------------------
# script to plot uvtracks and synthesized beam patterns.
# History:
#   mchw 27jul93  documented.
#   pjt   4aug93  allow all pgplot /*ps devices, fixed /xw
#   mchw 06aug93  updated documentation..
#   mchw 10aug93  change keyword in uvtrack.
#   mchw 17aug94  Added harange.
#   mchw 22dec94   change to cell=1 imsize=128.
#   mchw 22dec95   change to cell=.2 imsize=128.
#   mchw 17jan96   Added cell size and defaults.
#   mchw 27jan96   Uses uvgen and uvplt instead of uvtrack.
#   mchw 27mar96   Plot Array Response to Extended Structure.
#   mchw 25apr96   Added baseunit keyword.
#   mchw 11mar98   Use UVGEN's  default for baseunit.
#   mchw 02jul98   updated corr keyword for uvgen.
#   mchw 06aug98   Added select input. Added systemp to suppress warning from uvgen.
#------------------------------------------------------

# define the valid printername that cause spooling
  set valid_printers=(/ps /vps /vcps /cps)

echo "***************************************************************"
echo "Plot Array Response to Extended Structure. (06-AUG-98 version)"
echo "***************************************************************"

# check inputs
  if($#argv<3) then
	echo " Usage: beamgen2  array  declination device source [ellim] [harange] [baseunit] [select]"
	echo "	 e.g. beamgen2 bima9_a.ant 30 /xw"
        echo " Inputs :"
	echo "   array"
	echo "          Antenna array used by uvgen task."
	echo "            e.g. $MIRCAT/*.ant" . No default.
	echo "   declination"
	echo "          Source declination in degrees. No default."
	echo "   device"
	echo "          PGPLOT device. No default."
	echo "          For devices $valid_printers the plot is sent to"
	echo "          the printer."
	echo "   source"
	echo "          The name of a text file containing the source components, one"
        echo "          component per line. The source components"
        echo "          are elliptical Gaussian components. Each line consists of at least"
        echo "          three and up to nine values:"
        echo "            flux,dra,ddec,bmaj,bmin,bpa,iflux,ipa,vflux"
        echo "          The default source file is $MIRCAT/uvgen.source"
	echo "   [ellim]"
	echo " 	        Elevation limit. Default is 15 degrees."
	echo "   [harange]"
	echo " 	        harange (start,stop,step) in hours."
	echo "		Default is to use the elevation limit, with step=0.2"
        echo "   [baseunit]"
        echo "          coordinate conversion factor to nanosec baseline units."
        echo "          e.g. baseunit=3.33564 for geocentric coordinates in meters."
        echo "          Default=1 for geocentric coordinates in nanosecs."
        echo "   [select]"
        echo "          Standard uvdata selection. Default is '-shadow(5)'."
        echo " "
	exit 1
  endif

  if($#argv<4) then
        set source=$MIRCAT/uvgen.source
  else
        set source=$4
  endif

  if($#argv<5) then
        set ellim=15
  else
        set ellim=$5
  endif

  if($#argv<6) then
        set harange=-6,6,.2
  else
        set harange=$6
  endif

 if($#argv<7) then
        set baseunit=1
  else   
        set baseunit=$7  
  endif  

  if($#argv<8) then
        set select='-shadow(5)'
  else
        set select=$8
  endif
 
  echo -n "Check if array file exists: "
  if (! -e $1) then
    set ary=$MIRCAT/$1
    if (! -e $ary) then
      echo ""
      echo "  Can neither find array file $1 nor $ary"
      echo "	copy $MIRCAT/*.ant to user area"
      echo "   	or run uvgen interactively to generate an antenna array"
      goto end
    endif
  else
    set ary=$1
  endif
  echo "found $ary"

# clean up datasets that are created by this script
  rm -rf $1.$2* >& /dev/null

# figure out if to spool or not
  set lpr=0
  foreach v ($valid_printers)
    if ($3 == $v) then
      set lpr=1
      break
    endif
  end
  if ($lpr) echo Spooling pgplot.ps to printer

# create tracks and plot them
rm uvgen.source
  uvgen ant=$ary baseunit=$baseunit radec=0,$2 ellim=$ellim out=$1.$2.uv harange=$harange source=$source systemp=1
#  uvplt  vis=$1.$2.uv axis=uc,vc options=nobase device=$3 select=$select
  if($lpr) lpr pgplot.ps

# uv -> map
  invert vis=$1.$2.uv map=$1.$2.map beam=$1.$2.beam imsize=256 sup=0 select=$select
  implot in=$1.$2.map device=$3 units=s
  if($lpr) lpr pgplot.ps
  clean map=$1.$2.map beam=$1.$2.beam niters=1000 out=$1.$2.cl
  restor map=$1.$2.map beam=$1.$2.beam  model=$1.$2.cl out=$1.$2.cm
  implot in=$1.$2.cm device=$3 units=s
  ellint in=$1.$2.cm log=log
  wip $MIRCAT/beamgen2.wip -x

  echo "-------- script finished -------"
  echo "VIS        in $1.$2.uv"
  echo "DIRTY MAP  in $1.$2.map"
  echo "DIRTY BEAM in $1.$2.beam"
  echo "CLEAN MAP  in $1.$2.cm"

end:
