#!/bin/csh
# Plotting the Spectrum
# $theTab = FITS file to plot
# $1 = SpType
######################################

# Convert the file name into number, molecule
set Name = `echo $theTab | acut -d/ -f1 `
set File = `echo $theTab | acut -d/ -f2 `
set line = `echo $File | acut -d. -f1 `

if ("$line" =~ CH3OH*) then
   set Line = '$CH_3OH'
else if ("$line" =~ H2CO303-202*) then 
   set Line = 'para-$H_2CO$ $3_03$-$2_02$ '
else if ("$line" =~ H2CO321-220*) then 
   set Line = 'para-$H_2CO$ $3_21$-$2_20$ '
else 
   set Line = 'para-$H_2CO$ $3_22$-$2_21$ '
endif

#################################################################
# Vgraph not defined ==> just the text for the plot
#################################################################
if (! $?Vgraph) then
    set V = "\vizier{-source=$theCat/table1\&Name=$Name}{$Name}"
#   echo1 "\section*{$V IRAM30m reduced spectra in T_MB_ for $int[$No] } "
    echo1 "\section*{$Line spectrum } "
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" \
	-cap:"<&Cat.file $theCat/sp/$File|Spectrum in FITS>" \
        -form -htx -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"

# fits2a options:
# Velocity is: v/c ={Delta}{nu}/{nu}

#  Freq = $1
#  Tmb = $2
# les VELO-LSR et RESTFREQ chnqgent pour chaque object
# { n = $1+nu0;  printf "%11.3f %10s\n", n/100000, $2 }' \
# { v = v0-$1*f;  printf "%11.3f %10s\n", $1+nu0, $2 }' \
set v0 = `fits2a -TOC sp/$File | fgrep "VELO-LSR" | acut -c10-30`
set nu0 = `fits2a -TOC sp/$File | fgrep "RESTFREQ" | acut -c10-30`


fits2a sp/$File | gawk -v v0=$v0 -v nu0=$nu0 '\
  BEGIN {c=299792458; f=c/nu0/1000 } \
  { v=v0/1000+$1*f; printf "%11.3f %10s\n", v, $2 }' \
| tee /tmp/Jan.27      \
| graph -T $Vgraph -C $plotarg -X 'Velocity [km/s] ' -Y 'Ta* [K]' \
  --pen-colors 1=$c $argv:q
#    printf "%11.1f %10s\n", x, $i }}' \
