#!/bin/csh
# To generate a plot for a SED from asu + sed4
#Required parameters:
###################################################
# title     = title of plot (first line) [includes \object]
# asu_query = conditions for asu e.g. (-source $theCat Name=M31)
# graph_title (if wished)
# colors    = color to use (purple3/blue/green3/red/red4)
# errflux   = relative error of flux when error missing (20%)
###################################################
# _sed4: output is 
#GHz 2.9406e+3=0.00${F100}{IRAS:100} Jy
#GHz 4.8471e+3=0.748[0.075]${F60}{IRAS:60} Jy
#GHz 12.663e+3=0.115[0.006]${F24}{Spitzer/MIPS:24} Jy
#GHz 38.083e+3=54.1[1.6]e-3${F8.0}{Spitzer/IRAC:8.0} Jy
#GHz 52.311e+3=21.3[0.6]e-3${F5.7}{Spitzer/IRAC:5.8} Jy
#GHz 66.724e+3=10.7[0.3]e-3${F4.5}{Spitzer/IRAC:4.5} Jy
#GHz 84.449e+3=15.1[0.5]e-3${F3.6}{Spitzer/IRAC:3.6} Jy
#GHz 1.2967e+6=3.33[0.33]e-3${FNUV}{GALEX:NUV} Jy
#GHz 1.9607e+6=2.25[0.23]e-3${FFUV}{GALEX:FUV} Jy
######################################################

# Verify asu_query is defined
if (! $?asu_query) then
    echo2 "#***[`pwd`] asu_query not defined"
    exit 1
endif
echo2 "#---sed4: $#argv args ($*); asu_query=$asu_query, theTab=$theTab"

#################################################################
# Menu: Wavelength or Frequency or Energy
#################################################################

set tt = /tmp/set$$ ; touch $tt
set Xlist = '\lambda;\nu;E'
if ("$1" =~ LIST_X*=* ) shift
#set Xlist = "Wavelength;Frequency;Energy"
if ("$1" =~ X*=*) then
    if ("$1" =~ X*=lam*) then
        echo "X=\lambda" >> $tt;
    else if ("$1" =~ X*=nu*) then
        echo "X=\nu" >> $tt;
    else
        echo "X=$1" | acut -d= -f1,3- >> $tt
    endif
    shift
else
    set X = '\lambda'
endif

# Convert definitions to csh
acut -d= -i"set " -f1 -i" = '" -f2- -i"'" $tt > $tt.s
#echo2 "#Sourcing:"; cat2 $tt.s
source $tt.s ; rm $tt $tt.s


#################################################################
# Vgraph not defined ==> just the plot introduction
##################################################################

if (! $?Vgraph) then
    if (! $?title) then
        set title = "SED($asu_query)"
    endif
    echo1 "\subsection*{\quad $title}"
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" \
        -form -htx "@X=$Xlist" X="$X"  -graph $argv:q
    if ("$theTab" =~ *@*rs=*) then
	if ($?REQUEST_URI) then
	    set action = `echo "$REQUEST_URI" | acut -d\? -f1`
	    set Center = "`echo '$theTab' | acut -d@ -f1`"
	    set Radius = "`echo '$theTab' | acut -d@ -f2 | acut -d= -f2`"
	    cat <<===Form
\Beg{FORM}{NAME="SEDplot" METHOD=GET ACTION="$action"}
\begin{tabular}{cc}{\em Look for}\quad&
\Beg{tabular}{CELLSPACING=0 CELLPADDING=5 \bg{LightCyan}}{||cc||}
{\small\em Target:}
  \tag{INPUT TYPE="text" name="-c" size="25" value="$Center"}
\quad
{\small\em Radius:}
\tag{INPUT TYPE="text" name="-c.rs" size="3" value="$Radius"} {\em''}&
\quad\tag{INPUT TYPE="submit" value=" Search " onClick="vizgo(this)"}
\End{tabular}\end{tabular}
\End{FORM}
===Form
        endif
    endif
    exit 0
endif

################################################################
# Actual gif or ps plot (Vgraph contains gif or plot)
#################################################################

if ($?graph_title) then
    set graph_L = ( "-L" "$graph_title")
    set graph_h = 0.75
else
    set graph_L = ()
    set graph_h = 0.825
endif

if (! $?plotarg) then
    set plotarg = ( -f 0.045 -h $graph_h -w 0.75 -u 0.125 -r 0.22 \
                    --title-font-size 0.0625 -S 19 0.025 )
endif

if (! $?colors) then
    set colors = "1=purple3:2=blue:3=green3:4=red:5=red4"
endif

if (! $?errflux) then
    set errflux = 0.2
endif

#echo2 "#---$#argv Arguments ($*)"
#set verbose

switch("$X")
   case "*lam*":
   case "W*":
      set Xa = '\*l [\*mm]'
      set iX = 2
      breaksw
   case "*nu*":
   case "F*":
      set Xa = '\*n [GHz]' 
      set iX = 0
      breaksw
   default:
      set Xa = 'E [eV]'
      set iX = 1
endsw

set cache_d = $Vroot/run/asu-sed
set cat_file = ()
set cache_file = /dev/null
if (-w $cache_d) then
    set cat_file = `dcache $cache_d file "$asu_query"`
    if ($#cat_file == 0) then
	dcache $cache_d add "$asu_query"
        set cache_file = `dcache $cache_d file "$asu_query"`
	set test1 = (test 1 -eq 0 )
    else
        set test1 = ( cat $cat_file )
    endif
else
    set test1 = (test 1 -eq 0 )
endif

echo2 "#--- $test1 || asu -data -f p -out=_sed4 $asu_query:q | tee $cache_file"
echo2 "#... | graph -T $Vgraph -C -I e $graph_L:q  --pen-colors $colors ..."
#echo2 "#--- asu -data -f p -out=_sed4 $asu_query:q ..."
# Default = 20% error
ls -l `which  asu` | cat2
( $test1 || asu -data -f p -out=_sed4 $asu_query:q | tee -a $cache_file) \
| grep '^GHz' \
| tee /tmp/sed4 \
| acut -d\$ -f1 | tr 'GHz=Jy[]' '        ' \
| gawk -v iX=$iX -v err="$errflux" '#Use x = a*Ghz + b/GHz \
  BEGIN{ a=1; if(iX==1) a=6.64e-25/1.6e-19; if(iX==2) { a=0; b=299792.458 }}\
  NF==2{ e=err*$2; if(e>0) { n++; \
    if(n==1) print "# m=-1 put[-1r](0.97,0.03) Assumed relative error =",err;\
    printf "%12g %12s %9g\n", a*$1+b/$1, $2, e }}\
  NF==3{ printf "%12g %12s %9g\n", a*$1+b/$1, $2, $3} \
  NF==4{ v=$2 $4; e=$3 $4; printf "%12g %12g %9g\n", a*$1+b/$1, v, e}' \
| tee /tmp/sed4a \
| graph -T $Vgraph -C -I e $graph_L:q  --pen-colors $colors \
  $plotarg -X "$Xa" -Y "Flux [Jy]" -m -6 $argv:q
exit 0
