#!/bin/csh
# $theTab = Sequence Number
# $1 = SpType
######################################

set Seq = `echo $theTab | acut -d/ -f1 | tr '_' ' ' `
set oRA = `echo $theTab | acut -d/ -f2 | tr '_' ' ' `
set oDE = `echo $theTab | acut -d/ -f3 | tr '_' ' ' `

#@ Nsp -= 1
#   fait Seq -1 

#if ("$file" =~ *co10*) then
#    set Line = '$CO(1-0)$'
#    set c = red4
#else
#    set Line = '$CO(2-1)$'
#    set c = blue3
#endif
 
#set Name = \
#   `asu -data -source=$theCat/table1 Source=$Source -out.max=1 -out=Name`

#set Nl = `echo $theTab | acut -d/ -f4 `
#set Nb  = `echo $theTab | acut -d/ -f5 `

#if ("$1" =~ "sp="* ) then
#    setenv Nb "`echo $1 | acut -d= -f2-`"; shift
#endif

#################################################################
# Vgraph not defined ==> just the text for the plot
#################################################################
if (! $?Vgraph) then
    echo1 "\section*{Spectrum No $Seq (oRA=$oRA, oDE=$oDE)}"
    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
################################################################

#set verbose
if (! $?plotarg) \
set  plotarg = "-f 0.05 -h 0.85 -w 0.75 -u 0.125 -r 0.20"

set nu0=0.2305380000000E+12

# Plot DeltaV
# RESTFREQ=0.8592626300000E+11   = 85926263000
# premiere ligne FREQ=
# deuxiemme ligne SPECTRUM=
#  if faut transformer en vitesse, on a VELOCITY dans la liste
#fits2a -par -head -k VELOCITY skip=$Seq sp/$file | head -52 | fgrep "VELOCITY=" | acut -c10-30 > v0
#  gawk -v v0=v0 '\
#  /^%RESTFREQ=/ {c=299792458; f=c/$2/1000; v0 =v0/1000} \
#



#    RESTFREQ=  0.2305380000000E+12 
#    VELO-LSR=  0.0000000000000E+00 

fits2a -par -head -k RESTFREQ skip=1 spectra.fit | fgrep "RESTFREQ=" | acut -c11-30 > nu0
echo2 "#---fits2a -x 1 -par spectra.fit | tr '=' ' ' | ..."
fits2a -par -head -k RESTFREQ start=$Seq  -max=1 spectra.fit | sed 's/=/= /' \
| gawk '\
#  /^IMAGFREQ=/{ print "#" $0; c=299792458; f=c/$2/1000; v0 =v0/1000} \
# /^RESTFREQ=/ {c=299792458; f=c/$2/1000 } \
# /^RESTFREQ=/ {c=299792458; nu0=$2 } \
# /^VELO-HEL=/ { v0=$2/1000 }\
  /^FREQ=/{for(i=2; i<=NF; i++) {X[i] = $i + nu0 }}\
  /^SPECTRUM=/{for(i=2; i<=1919; i++) printf "%.3f %12s\n", X[i]*1.e-9, $i }'\
| graph -T $Vgraph -C $plotarg -X 'Freq [GHz]' -Y 'T\sbmb\eb [K]' \
    --pen-colors 1=red2:4=red:3=green4:2=blue:5=purple3 $argv:q

