#! /bin/sh


# 
#  Copyright (C) 2009  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#




MYNAME=evalpos

#
# Load parameters. Escape: spaces, (), and ['s
#
for pp in `pdump ${MYNAME} | cut -d= -f1 | grep -v '^# EOF'` 
do
  eval ${pp}=`echo $pp | pquery ${MYNAME} - "$@" | sed 's/[ ()\[]/\\\&/g'`
done


if test $verbose -gt 0
then
  printf "$0\n"
  printf "  %10s = %s\n" "infile" "$infile"
  printf "  %10s = %s\n" "ra" "$ra"
  printf "  %10s = %s\n" "dec" "$dec"
  printf "  %10s = %s\n" "output" $output
  printf "  %10s = %s\n" "tmpdir" $tmpdir
  printf "  %10s = %s\n" "verbose" $verbose
fi



#
# Get lis of files (1>&2 sends to stderr)
#
# (leave this check in place even though we don't use 'files')
#
files=`stk_build "$infile" stdout`
if test $? -ne 0
then
    echo "" 1>&2 
    echo "# ${MYNAME} : ERROR running stk_build" 1>&2
    echo "" 1>&2 
    exit 1
fi



#
# We need to use stk_read_num since the infile may have spaces
# (eg [bin sky...] ) which confuses say a 'for sens in $files' 
# kind of loop.
#
mm=`stk_count "${infile}" echo+`
nn=1

#
# Loop over files in stack
#
while test $nn -le $mm
do

  sen=`stk_read_num "${infile}" $nn echo+`

  if test $verbose -gt 1
  then
    echo "Processing file '$sen'"
  fi


  #
  # Convert ra/dec to x/y
  #
  punlearn dmcoords
  dmcoords "${sen}" asol=none op=cel ra="${ra}" dec="${dec}"
  if test $? -ne 0
  then
    echo "" 1>&2 
    echo "# ${MYNAME} : ERROR running dmcoords" 1>&2
    echo "" 1>&2 
    exit 1
  fi

  #
  # Save into a temporary file to use with dmimgpick
  #   The 3rd column is the output from dmimgpick; but we
  #   don't know the name of the input image block so we
  #   rename on the fly
  #
  pget dmcoords x y | tr -s "\012" " " > ${tmpdir}/$$_pos.dat

  if test $verbose -gt 1
  then
    echo "Using x y = `cat ${tmpdir}/$$_pos.dat`"
  fi

  value=`dmimgpick  ${tmpdir}/$$_pos.dat"[cols x=col1,y=col2]" "${sen}" - closest | dmkeypar -"[cols __output=#2]" __output echo+`
  if test $? -ne 0
  then
    echo "" 1>&2 
    echo "# ${MYNAME} : ERROR running dmimgpick / dmlist" 1>&2
    echo "" 1>&2 
    /bin/rm -f ${tmpdir}/$$_pos.dat
    exit 1
  fi
  /bin/rm -f ${tmpdir}/$$_pos.dat
  unit=`pget dmkeypar unit`

  #
  # Output value
  #
  chp=`basename "$sen"`
  printf "%s\t%s %s\t%g [%s]\n" "$chp" "${ra}" "${dec}" $value "$unit"
  pset ${MYNAME} output=$value  # only last one survives


  nn=`echo $nn + 1 | bc `

done


exit 0
