#! /bin/csh -f
#
#	client script to load miriad  or fits files into saoTNG (server mode)
#
if ($#argv == 0) then
    echo "Usage: $0 [-options] miriad_dataset [plane1] [plane2 ...]"
    echo "  Load selected planes from a miriad dataset into saotng"
    echo "  Options:"
    echo "      -minmax data_min data_max"
    echo "                  clipped display values from data_min to data_max"
    echo "      -movie"
    echo "                  start animating selected frames (pretty crude)"
    echo "      -wcs"
    echo "                  do not load WCS, even if it has one"
    echo ""
    echo "  Version 1.8a - 5-jul-07 PJT"
    echo ""
    echo "  Be sure 'saord' is in your environment. MIRIAD users can use the"
    echo "  command  'saord'  to load the SAO RD environment which contains"
    echo "  saotng. You need to start saotng manually yourself."
    echo ""
    exit 0
endif

set scalelims=0
set movie=0
set wcs=0
set dowcs=1

foreach dummy ($*)
  if ("$1" == "-minmax") then
    set datamin=$2
    set datamax=$3
    set scalelims=1
    shift argv; shift argv; shift argv;
  else if ("$1" == "-movie") then
    set movie=1
    shift argv
  else if ("$1" == "-wcs") then
    set dowcs=0
    shift argv
  else
    break
  endif
end

set name=$1
set dir=`pwd`
set sname=`uname -s`

if (X-$sname == X-Linux) then
  # NEMO has bswap
  set pipe="bswap - - 4"
  set pipes=(`which bswap`)
  if ($#pipes > 1) then
    echo "It does not look like you have bswap for $sname. You need it."
    exit 0
  endif
else
  set pipe=cat
endif

#   if the first char of the file is not / (absolute), make it absolute
#   since we don't know if saotng had been started in the current directory
if (`echo $name | cut -c1` != '/') then
  echo Assuming dir=$dir
  set name=$dir/$1
endif

shift argv

set title=SAOtng

# saotng >& /dev/null  &

set xpasets=(`which xpaset`)

if ($#xpasets > 1) then
  echo "It does not look like your environment has loaded in 'saord' yet"
  exit 0
endif


if (-d $name) then
    set ndim=`itemize in=$name/naxis | tail -1 | awk '{print $3}'`
    set n1=`itemize in=$name/naxis1 | tail -1 | awk '{print $3}'`
    set n2=`itemize in=$name/naxis2 | tail -1 | awk '{print $3}'`
    set dims=(`itemize in=$name | grep naxis | tail -n +2 | awk '{print $3}'`)
    echo "Found $ndim-dim cube, ($dims) : maps $n1 x $n2"
    if ($dowcs) then
        set wcs=/tmp/saotng$$.wcs
        # work around an earlier bug by adding NAXIS=2
        # note this method isn't robust, it doesn't really check the header
        echo "NAXIS  2" > $wcs
        echo CDELT1 `imhead in=$name key=cdelt1 | awk '{print $1*57.2957795131}'` >> $wcs
        echo CDELT2 `imhead in=$name key=cdelt2 | awk '{print $1*57.2957795131}'` >> $wcs
        echo CRVAL1 `imhead in=$name key=crval1 | awk '{print $1*57.2957795131}'` >> $wcs
        echo CRVAL2 `imhead in=$name key=crval2 | awk '{print $1*57.2957795131}'` >> $wcs
        echo CRPIX1 `imhead in=$name key=crpix1 | awk '{print $1}'` >> $wcs
        echo CRPIX2 `imhead in=$name key=crpix2 | awk '{print $1}'` >> $wcs
        echo CTYPE1 \'`imhead in=$name key=ctype1`\' >> $wcs
        echo CTYPE2 \'`imhead in=$name key=ctype2`\' >> $wcs
    endif
else if (-f $name) then
   set type=`file $name | awk '{print $2}'`
	# work around another new  solaris/umd bug
   if ($name:e == fits) set type=FITS
   if ($name:e == FITS) set type=FITS
   if ($name:e == fts) set type=FITS
   if ($name:e == FTS) set type=FITS
   echo Found type $type
   if ($type == FITS) then
   	# echo $name | xpaset $title fits $name
	cat $name | xpaset $title fits $name
   else if ($type == gzip) then
	# bit of a risk, not sure if it is really a FITS file ...
   	echo Displaying gz compressed $name FITS file
	zcat $name | xpaset $title fits $name
   else if ($type == compressed) then
	# bit of a risk, not sure if it is really a FITS file ...
   	echo Displaying Z compressed $name FITS file
        zcat $name | xpaset $title fits $name
   else if ($type == Nemo) then
        ccdfits $name - | xpaset $title fits $name
   else
	echo File type $type not understood by mirtng yet
	exit 1
   endif
   if ($scalelims) then
      echo "scalelims $datamin $datamax" | xpaset SAOtng
   endif
   exit 0
else 
   echo No file or directory with name $name found
   exit 1
endif


if ($#argv > 0) then
   set planes=($*)
else
   set planes=(1)
endif

set nplane=$#planes
set iplane=0

set frames=(`xpaget SAOtng frame | awk '{print $1}'`)
set frame=$frames

foreach p ($planes)
    @ iplane++

    set skip=`echo " $n1 $n2 $p " | awk '{printf("%d",($1*$2*($3-1)*4)+4)}'`

    echo $name $n1 $n2 -32 $skip 

    cat $name/image | $pipe | xpaset $title array  $name/image\[xdim=$n1,ydim=$n2,bitpix=-32,skip=$skip\]

#    echo array $name/image $n1 $n2 -32 $skip | $pipe | xpaset $title

    if ($scalelims) then
      echo "scalelims $datamin $datamax" | xpaset SAOtng
    endif

    if ($wcs != 0) then
      # ??? echo autoflip true | xpaset SAOtng   ??? does this work ?
      echo "Adding WCS from $wcs"
      cat $wcs | xpaset SAOtng wcs
      echo flip y | xpaset SAOtng 
    endif

    #  Don't do a 'new' for the last frame

    if ($nplane > 1 && $iplane < $nplane) then
        @ frame++
        set frames=($frames $frame)
        echo frame $frame | xpaset $title
    endif
end

if ($nplane > 1) then
   echo frame $frames[1] | xpaset $title
endif

if ($movie) then
  echo 'Animate: you should probably toggle the "Blink Frames" in control panel on'
  echo Using frames: $frames
  echo 'tcl set blinkFrames "'$frames'"' | xpaset SAOtng
endif

if (1) then
  rm -f $wcs
else
  echo Keeping $wcs
endif
