#bin/bash
ast=`echo $1 | tr A-Z a-z`
JD=$2
n=$3
st=$4
obs=$5
cmd="http://vo.imcce.fr/webservices/miriade/ephemcc_query.php?-name=a:$ast&-type=&-ep=$JD&-nbd=$n&-step=$st&-observer=$obs&-mime=text&-tscale=UTC&-theory=INPOP&-teph=1&-tcoor=1"
#echo $cmd
curl -s $cmd > .eph
#echo ASTEROID $ast START $JD "--" DATE TIME RA DEC Vmag PHA SolarElong
#                                                           V  ph el muRAcosDE ("/s)
#                                                                    |   muDE ("/s)    
#                                          $1$2 $3 $4 $5 $6 $7 $8 $9 $10 $11
#                                            13 25 41 57 73 80 88 96 107 118  column number
gawk -v asteroid=$ast 'BEGIN {FIELDWIDTHS="12 1 12 16 16 16  7  8  8  11  11 10"} 
     {split($1, date, "-")
      split($3, time, ":")
      split($4, ra, " ")
      split($5, de, " ")
      if ((date[1]>0) && ($7>0)) printf("%02d:%02d:%02d %+03d:%02d:%02d\n",
      ra[1],ra[2],ra[3],de[1],de[2],de[3])}' .eph >.coord
rm .eph
coord=`cat .coord`
rm .coord
/Applications/SAOImage\ DS9.app/Contents/MacOS/ds9 -dsssao coord $coord sexagesimal &
# ds9 -dsssao coord $coord sexagesimal &

# format of the output of MIRIADE (.eph file)
#         Date UTC              R.A            Dec.          Distance     V.Mag   Phase   Elong.  muRAcosDE    muDE     Dist_dot
#             h  m  s       h  m  s         o  '  "            au.                  o        o      "/s        "/s        km/s
#  2011-07-09T13:41:42.00   14 22  6.70451 -16 31 19.0882    1.506595241   13.98   26.62   111.78  0.859E-02 -0.163E-02   18.1453
