#!/bin/ksh

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# acis_step3_make_images_part2.ksh
# 
# INPUT:        acis_E300-10000_FLUXED_G2.fits
#               $REGION_FOR_EXCLUDE
#               $REGION_FOR_SPECTRUM
#               
# OUTPUT:       ${REGION_FOR_SPECTRUM}.jpg
# 
# CALLS:        ds9
#               xpaset
#
# PARAMS:       $REGION_FOR_SPECTRUM
#               $REGION_FOR_EXCLUDE
#               $IMG_SCALE
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - PRELIMINARY STUFF - - - - - - - - - - - - -

if [[ $SNRCAT_SCRIPT_PATH == "" ]]
then
  case $OSTYPE in
    solaris) SNRCAT_SCRIPT_PATH=/data/snrcat/development/bin ;;
    linux) SNRCAT_SCRIPT_PATH=/snrcat/development/bin ;;
    *) echo
       echo "   OS type $OSTYPE not supported at this time."
       echo 
       exit ;;
  esac
fi

echo $PATH | grep $SNRCAT_SCRIPT_PATH > /dev/null 2>&1

if [[ $? -ne 0 ]]
then
  PATH=$PATH:$SNRCAT_SCRIPT_PATH
fi

. setup.ksh
# this doesn't work with ciao 2.3
CIAO=/data/snrcat/local/ciao3.0/bin/ciao.ksh
ASCDS_OVERRIDE=1
. $CIAO

echo
echo "  >>> `basename $0`"

check_set REGION_FOR_SPECTRUM="$REGION_FOR_SPECTRUM"

check_file $REGION_FOR_SPECTRUM acis_E300-10000_FLUXED_G2.fits

echo
echo "  REGION_FOR_SPECTRUM = $REGION_FOR_SPECTRUM"
echo "  REGION_FOR_EXCLUDE = $REGION_FOR_EXCLUDE"
echo "  CLOBBER = $CLOBBER"
echo

# - - - - - - - - - - - - - - - -MAIN - - - - - - - - - - - - - - - -

IMAGEFILE="acis_E300-10000_FLUXED_G2.fits"

exclude_region=$REGION_FOR_EXCLUDE

if [[ -f "$exclude_region" ]]
then
  echo "excluding some region"
  max=`dmstat "$IMAGEFILE[exclude sky=region($exclude_region)][opt full]"| grep max:|gawk '{print $2}'`
  echo "fimgtrim $IMAGEFILE INDEF $max 0 $max $TMPDIR/$IMAGEFILE"
  fimgtrim $IMAGEFILE INDEF $max 0 $max $TMPDIR/$IMAGEFILE clobber=yes
  IMAGEFILE=$TMPDIR/$IMAGEFILE
fi

REGFILES=$REGION_FOR_SPECTRUM

# COMM="$DS9 -geometry 300x500"
echo $COMM
# $COMM &

# while :
# do
#   STATUS=`xpaaccess ds9`
#   if [[ $STATUS == "no" ]]
#   then
#     sleep 10
#   else
#     break
#   fi
# done

echo -n "xpaset -p ds9 cmap sls? "
read ans
#xpaset -p ds9 cmap sls
#    xpaset -p ds9 cmap invert yes
echo -n "xpaset -p ds9 scale $IMG_SCALE? "
read ans
#xpaset -p ds9 scale $IMG_SCALE
echo -n "xpaset -p ds9 file fits $IMAGEFILE? "
read ans
# xpaset -p ds9 file fits $IMAGEFILE
echo -n "xpaset -p ds9 zoom to fit? "
read ans
# xpaset -p ds9 zoom to fit

 for i in $REGFILES
 do
#   check_clobber "$i"".jpg"
#   if [[ $? -eq 0 ]]
#   then

    convert_region.ksh $i $TMPDIR/$i

    echo -n "xpaset -p ds9 regions load $TMPDIR/$i? "
    read ans
#     xpaset -p ds9 regions load $TMPDIR/$i
    if [[ $OSTYPE == "linux" ]]
    then
      echo -n "xpaset -p ds9 saveas jpeg $i.jpg? "
      read ans
#      xpaset -p ds9 saveas jpeg $i.jpg
    else
#      xpaset -p ds9 print destination file
#      xpaset -p ds9 print filename $i.ps
#      xpaset -p ds9 print

      OUTPS=${i}.tmp

      COMM="sh $SCRIPTPATH/pstrim.sh $OUTPS.ps ${i}.ps"
      echo $COMM
#      $COMM

      COMM="convert -density 150x150 -geometry 300x300 -antialias ${i}.ps[0] ${i}.jpg"
      echo $COMM
#      $COMM

#    fi

    echo -n "xpaset -p ds9 regions delete all? "
    read ans
#    xpaset -p ds9 regions delete all

    MADE="$MADE $i.jpg"

  fi
done

#xpaset -p ds9 quit

echo
echo "	Done! Made $MADE"
echo

# now set this back
CIAO=/data/snrcat/local/ciao2.3/bin/ciao.ksh
ASCDS_OVERRIDE=1
. $CIAO
