#!/bin/csh
########################################################
#   Plot of Ext / kR / k03 / ka
########################################################

if ("$1" =~ LIST_Y=* ) then
    set Ylist = "$1"; shift
else
    set Ylist = "LIST_Y=Ext;kR;kO3;ka;kR+kO3;all"
endif

if ("$1" =~ Y=*) then
    set Y = `echo "$1" | acut -d= -f2` ; shift
else if ($?theTab) then
    set Y = $theTab
else
    set Y = all
endif

# Colour for the various parameters
set param = "Ext=blue;kR=green4;kO3=cyan;ka=red4"

#################################################################
# Vgraph not defined ==> just the text for the plot
#################################################################
if (! $?Vgraph) then
    #echo1 "param='$param'"
    echo1 "\section*{Maunea Kea extinction: `graph_title '$param:q'`}"
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
	"$Ylist" Y="$Y" -graph $argv:q
    exit 0
endif

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

set  plotarg =  ( -f 0.05 -h 0.85 -w 0.75 -u 0.125 -r 0.20 )

set Xlab = '\*l[\oA]'
set Ylab = "extinction [mag/airmass]"

#tabmap  -form a -data -ascii "lambda $Y" ./snfext.dat \
#echo2 "tabmap  -form a -data $atm:q"
# Method: use AWK to generate the appropriate "tabmap" commands
#         with the relevant columns: lambda (X-axis) and the other parameter (Y)
# AWK uses as 1st line the list of parameters (columns except lambda)
#             2nd line the parameters to plot (form "p1+p2" for 2 parameters)
(echo "$param"; echo "$Y") | gawk -F\; -v q="'" '\
  NR==1{N=NF;for(i=1;i<=NF;i++) {split($i,a,"="); k[i]=a[1]; k[a[1]]=i } next }\
  {if($1=="all") { n=N; for(i=1;i<=N;i++) a[i]=k[i] }\
     else { n=split($1,a,"+") } \
   for(i=1;i<=n;i++) { print "# m = " k[a[i]]; \
     system("tabmap  -form a -data -ascii " q "lambda " a[i] q " ./snfext.dat")\
   }}' \
| graph -T $Vgraph -C $plotarg \
   -y - - - -X $Xlab:q -Y $Ylab:q --pen-colors "`graph_color '$param:q'`" \
   $argv:q
