#! /bin/csh
#++++++++++++++++
#.IDENTIFICATION get_res
#.LANGUAGE       C-shell
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   21-Nov-1999
#.VERSION  1.1   23-Nov-2007: Compile program to /tmp (multi-architecture)
#.PURPOSE        Compute Interpolations of Stark Effect
#.COMMENTS       Arguments: Lower / Upper / Temp(K) / Ne(cm^-3^)
#		### We assume to be located on the Catalog Directory ###
#Choose the quantum levels, Temperature (in \deg K) and density (in $cm^{-3}$)}
#----------------

set serial = ( "ly" "ba" "pa" )
set Serial = ( "Lyman" "Balmer" "Paschen" )
set compute = /tmp/p6098

# Compile the program!
test -x $compute || (set d = `pwd`; cd /tmp; f77 -o $compute $d/compute_77.f)
test -x $compute || echo2 "****Error f77 -o $compute $cwd/compute_77.f"

#echo "Content-Type: text/plain"; echo ""
#echo $#argv arguments: $argv:q
#printenv | sort
#exit 0

if (($#argv == 0) && ($?SCRIPT_NAME)) then

    # $a = parameters (low up T Ne) , $O = selection for line lower level
    if (! $?FORM_ARGS) then
	set FORM_ARGS = "2 9 10000 1.e14"
    endif
    if (! $?TEXT) then
	setenv TEXT "Computation of Stark profile for:"
    endif
    set a = `echo $FORM_ARGS`
    set O = (" selected" "" "")
    if ($a[1] > 1) set O = ("" $O:q)
    if ($a[1] > 2) set O = ("" $O:q)

    # Create a FORM 
    echo "{\par\large\em $TEXT}\\"
    #echo '\begin{FORM method="GET" action="'$SCRIPT_NAME'/get_res"}'
    echo '\tag{INPUT type="hidden" name="cat" value="VI/98A"}'
    echo 'Line lower level: \begin{SELECT name="low"}'
    echo "\tag{option$O[1]}Lyman\tag{option$O[2]}Balmer\tag{option$O[2]}Paschen"
    echo '\end{SELECT}'
    echo 'Line upper level: \tag{INPUT name="up" size=3 value=" '$a[2]'"} \\'
    echo 'Temperature     : \tag{INPUT name="T" size=9 value=" '$a[3]'"} \deg K'
    echo '\qquad '
    echo 'Density:   \tag{INPUT name="Ne" size=9 value=" '$a[4]'"} $cm^{-3}$ \\'
    echo '\par\tag{INPUT type=submit value=" COMPUTE Stark profile"}'
    echo '\quad\tag{INPUT type=reset  value=" Clear "}'
    #echo '\end{FORM}'
    exit 0
endif

##############################################
# Is it a plot ?
##############################################
setenv HTTP_PLOT        /viz-bin/nph-Plot
if ($?PATH_INFO) then
    set tmp = `echo $PATH_INFO | sed 's%/*g*e*t*_*r*e*s*[^/]*/*%%'`
    setenv Vgraph $tmp
    #echo "tmp=$tmp"
endif
if ($?Vgraph) then
    if ("$Vgraph" == "") unsetenv Vgraph
endif

if ($#argv < 4) then 
    echo "Usage: $0 lower upper Temp(K)  Ne(cm^-3^)"
    exit 1
endif

#echo Arguments: $*
# Transform the Argument if =
if ("$1" =~ *=* ) then
    switch("$1")
      case "*=L*":
	set argup   = 1 ; breaksw
      case "*=B*":
	set argup   = 2 ; breaksw
      case "*=P*":
	set argup   = 3 ; breaksw
    endsw
    set arglow  = `echo $2 | rev | acut -d= -f1 | rev`
    set argT    = `echo $3 | rev | acut -d= -f1 | rev`
    set argN    = `echo $4 | rev | acut -d= -f1 | rev`
    set argv = ( $argup $arglow $argT $argN )
endif
#echo Arguments: $argv
setenv FORM_ARGS "$*"

set d = $serial[$1]/$serial[$1]`gawk -v u=$2 'BEGIN{printf("%02d\n",u)}'`
set tt = /tmp/g-$$

if ($?Vgraph) then
    #echo "Content-type: text/plain"; echo ""
    #echo "Vgraph=$Vgraph<BR>"
    #printenv | sort
    #exit 0
    switch("$Vgraph")
    case "gif":
	echo "Content-type: image/gif"; echo ""
	breaksw
    case "ps":
	echo "Content-type: application/postscript"
	echo "Content-Disposition: inline; filename='$*.ps'"
	echo ""
	breaksw
    default:
	echo "Content-type: $Vgraph"; echo ""
	breaksw
    endsw
    cd $d
    if ("$5" == "c") then	### Center of Line
        (echo $3; echo $4; echo $tt.) | $compute |& gawk '\
          /^ *0\.0*E/{p=1; Dmax=$3; Smax=$4 }\
          {if(p<1)next; r=$3/Dmax; if (r<0.05) next; print $1,r; \
	     ++n; w[n]=$1; s[n]=$4/Smax }\
          END{print ""; for(i=1; i<=n; i++) print w[i],s[i]}' \
        | graph -T $Vgraph -C -f 0.05 -h 0.75 -w 0.75 -u 0.125 -r 0.15 \
         --title-font-size 0.05 -L 'Profile of Line Center' -X '\*D\*l [\oA]' \
         -Y 'Relative Profile' --bitmap-size 360x360
    else	################ Detuning ##########################
        (echo $3; echo $4; echo $tt.) | $compute |& gawk '\
          /^ *0\.0*E/{p=1;next}\
          {if(p<1)next; print $1,$3; ++n; w[n]=$1; s[n]=$4}\
          END{print ""; for(i=1; i<=n; i++) print w[i],s[i]}' \
        | graph -T $Vgraph -C -f 0.05 -h 0.75 -w 0.75 -u 0.125 -r 0.18 \
         --title-font-size 0.05 -L 'Detunings < 100\oA' -X '\*D\*l [\oA]' \
         -Y 'Profiles [s]' -l x -l y -x 1.e-6 100. --bitmap-size 360x360
    endif
    /bin/rm -f $tt.*
    exit 0
endif

##############################################
# Not a plot -- write the results
##############################################
if ($?SCRIPT_NAME) then
    echo "$*" | gawk '{printf("\\section{Stark profile %d-->%d", $2, $1); \
	 printf("\\quad T=%d{\\deg}K, \\quad N$_e$=%.2e$cm^{-3}$}\n",$3,$4)}'
    #echo "\section{Stark profile $2-->$1 "
    #echo "\quad T=$3{\deg}K, \quad N_e=$4"'$cm^{-3}$}'
    echo "\def\graph#1#2{\tag{IMG #1 SRC='$HTTP_PLOT/get_res/gif?\htarg{#2}'}}"
    echo "\def\psgraph#1{\A{$HTTP_PLOT/get_res/ps?#1}{Postscript Figure}}"
    echo "\begin{alltt}"
    set htx = 1
else
    set htx = 0
endif

if (! -d $d) then
    echo "****Quantum numbers $1 / $2 invalid (no $d directory)"
    exit 1
endif
cd $d

(echo $3; echo $4; echo $tt.) | $compute |& gawk -v htx=$htx '\
/^[* ]*FORTRAN /{if (htx>0) { print "{\\fg{red2}\\bf"; etxt = "}"}} \
/The plasma/ { if (htx>0) { print "{\\fg{OrangeRed}\\bf"; etxt = "}"}} \
/your input/ { if (htx>0) { print "{\\fg{OrangeRed}\\bf"; etxt = "}"}} \
END { print etxt; if (etxt != "") exit(1)} \
/AMBDA, /{print; getline; l0 = $1} \
/^ *$/{ print; if (l0 == 0) next; \
  if (htx>0) printf("\\hr {\\bf{}  lambda0=%d\\AA}\n", l0) ; \
  if (l0 > 0) { printf("{\\bf{}%s", "  Dlambda      Dnu    ") ; \
     print " Inu*Doppler Inu(Stark)}\n\\hr"} \
  l0 = 0 ; next} {print}' 
set stat = $status
/bin/rm -f $tt.*
cd ../..

if ($?SCRIPT_NAME) then
    echo "\end{alltt}"
    #echo '\par\thickrule\par {\bf Other parameters are possible}:\par'
    echo '\begin{FORM method="GET" action="'$SCRIPT_NAME'/get_res"}'
    echo '\Beg{tabular}{\bg{white} CELLPADDING=6}{c}'
    if ($stat == 0) then
      #echo "\par\thickrule\par"
      echo "      {\bf\fg{red}---- Doppler convolved}"
      echo "\quad {\bf\fg{green}---- Pure Stark}"
      echo "\\ \Beg{tabular}{\bg{white} CELLPADDING=6}{cc}"
      echo "\graph{}{VI/98\&$1\&$2\&$3\&$4}\br"
      echo "\psgraph{VI/98\&$1\&$2\&$3\&$4}"
      echo "&" #echo "\par\graph{}{VI/98\&$1\&$2\&$3\&$4}"
      echo "\graph{}{VI/98\&$1\&$2\&$3\&$4\&c}\br"
      echo "\psgraph{VI/98\&$1\&$2\&$3\&$4\&c}"
      echo "\End{tabular}"
    endif
    echo "\thickrule\par\Beg{tabular}{}{c}"
    setenv TEXT "Compute other Stark profile with parameters:"
    #echo '\Beg{tabular}{\bg{white} CELLPADDING=6}{c}'
    $0
    echo '\End{tabular}\End{tabular}\thickrule\par'
    echo '\end{FORM}'
endif
