#!/bin/csh
# Plotting the Spectrum
# $theTab = Sequence Number
# Table is  Tab:Seq:Name:Vmag:SpType
######################################

#set names = `echo "$theTab" | tr : ' '`
set part  = `echo "$theTab" | acut -d: -f1 | tr _ ' '`
set seq   = `echo "$theTab" | acut -d: -f2 | tr _ ' '`
set sp    = `echo "$theTab" | acut -d: -f3 | tr _ ' '`
set Star  = `echo "$theTab" | acut -d: -f5 | tr _ ' '`

# Choice of color
set c = purple4
if ("$sp" =~ [BA]*) set c = cyan4
if ("$sp" =~ [FG]*) set c = orange3
if ("$sp" =~ [K]*) set c = red
if ("$sp" =~ [M]*) set c = red4

#################################################################
# Vgraph not defined ==> just the text for the plot
#################################################################
if (! $?Vgraph) then
 echo1 "\section*{Spectrophotometry of \object{$Star} ({\fg{$c}$sp}) [$part \#$seq]}"
 #echo "theTab='$theTab', seq=$seq, names=$names"
 cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
    -graph $argv:q
 exit 0
endif

################################################################
# Actual gif or ps plot (Vgraph contains gif or plot)  $1=m or c
################################################################

if (! $?plotarg) \
set  plotarg = "-f 0.04 -h 0.75 -w 0.75 -u 0.125 -r 0.20 --title-font-size 0.05"

fcat $part.dat | gawk -v seq=$seq -v part=$part '\
  BEGIN { p = substr(part, 5,1)+0; w = 10; if (p == 2) w=13; \
    cmin = 63; cmax = cmin + w*200; }\
  { if(NR<seq)next; \
    lam = substr($0,59,4); \
    # There is a bug there in the data file... \
    if ((substr($0,cmin,w)+0) >= 0) cmin += w ; \
    for (c=cmin; c<cmax; c += w) { \
      F = exp(2.3026*substr($0,c,10)); \
      printf("%8.0f %12.4e\n",lam, F); lam += 25;  \
    } exit\
 }' \
 | graph -T $Vgraph -C $plotarg -L "$part, #$seq" \
   --pen-colors 1=${c}:2=red3:3=green \
   -X '\*l[\oA]' -Y 'E [mW/m\sp2\ep/cm]' -x 3200 8200 $argv:q
#    --pen-colors 1=red:2=red3:3=green -m -1 -S 3 $argv:q
