#! /bin/csh -f
#A simple script to make formatted lines for a latex table from the classify_j1. The output format is 
#1) new index
#2) observed wave
#3) RA
#4) dec
#5) flux
#6) observed EW
#7) counterp RA
#8) counterp dec
#9) transition
#10) z
#11) xray
set scname=$0
if($#argv != 1) then
   echo Usage: $scname:t 'file_j1'
   exit(1)
endif
#set class_map = ('Ly$\alpha$' '[OII]' 'H$\beta$' '[OIII]4959' '[OIII]5007' 'AGN')
set n_off = 194
set class_map = ('L$\alpha$' '[ OII ]' 'H$\beta$' '[ OIII ]4959' '[ OIII ]5007' 'CIV1549' 'H$\gamma$' 'H$\delta$' '[ NeIII ]3869' '[ NeV ]3426' 'MgII2798' 'CIII]1909')
set index_j = `cat $1 | awk '$1!="#"{split($0,a," "); print a[1]}'`
set ndat_j = $#index_j
set wave1 = `cat $1 | awk '$1!="#"{split($0,a," "); print a[4]}'`
set ra_e = `cat $1 | awk '$1!="#"{split($0,a," "); print a[2]}'`
set dec_e = `cat $1 | awk '$1!="#"{split($0,a," "); print a[3]}'`
set flux1 = `cat $1 | awk '$1!="#"{split($0,a," "); printf("%7.1f\n",a[6])}'`
set flux1_err = `cat $1 | awk '$1!="#"{split($0,a," "); printf("%7.1f\n",a[7])}'`
set EW1 = `cat $1 | awk '$1!="#"{split($0,a," "); if (a[9]>0.) printf("%7.1f\n",a[9]*a[4]/1215.67); else print ".."}'`
set EW1_err = `cat $1 | awk '$1!="#"{split($0,a," "); if (a[9]>0.) printf("%7.1f\n",a[10]*a[4]/1215.67); else print ".."}'`
set ra_c = `cat $1 | awk '$1!="#"{split($0,a," "); if (a[9]>0.) print a[15]; else print ".."}'`
set dec_c = `cat $1 | awk '$1!="#"{split($0,a," "); if (a[9]>0.) print a[16]; else print ".."}'`
set class_flag = `cat $1 | awk '$1!="#"{split($0,a," "); print a[8]}'`
set z1 = `cat $1 | awk '$1!="#"{split($0,a," "); if (a[8]==1) printf("%7.5f\n",a[4]/1215.67-1.); \
else if (a[8]==2) printf("%7.5f\n",a[4]/3727.-1.); \
else if (a[8]==3) printf("%7.5f\n",a[4]/4861.-1.); \
else if (a[8]==4) printf("%7.5f\n",a[4]/4959.-1.); \
else if (a[8]==5) printf("%7.5f\n",a[4]/5007.-1.); \
else if (a[8]==6) printf("%7.5f\n",a[4]/1549.-1.); \
else if (a[8]==7) printf("%7.5f\n",a[4]/4341.-1.); \
else if (a[8]==8) printf("%7.5f\n",a[4]/4102.-1.); \
else if (a[8]==9) printf("%7.5f\n",a[4]/3869.-1.); \
else if (a[8]==10) printf("%7.5f\n",a[4]/3426.-1.); \
else if (a[8]==11) printf("%7.5f\n",a[4]/2798.-1.); \
else if (a[8]==12) printf("%7.5f\n",a[4]/1909.-1.); else print "-99."}'`
set xray_val = `cat cut_xray.txt | awk '{if ($1=="No") print ".."; else print $2}'`
set n_big = 0
set cnt_n = 0
rm -f table_mk.out
while ($cnt_n < $ndat_j)
   @ cnt_n = $cnt_n + 1
   set ra1 = `/data1/grad56/jjadams/finder_code_delta/scripts/deg_to_ra.csh $ra_e[$cnt_n]`
   set dec1 = `/data1/grad56/jjadams/finder_code_delta/scripts/deg_to_dec.csh $dec_e[$cnt_n]`
   if ($ra_c[$cnt_n] != "..") then
      set ra2 = `/data1/grad56/jjadams/finder_code_delta/scripts/deg_to_ra.csh $ra_c[$cnt_n]`
      set dec2 = `/data1/grad56/jjadams/finder_code_delta/scripts/deg_to_dec.csh $dec_c[$cnt_n]`
      set EW2 = $EW1[$cnt_n]'$\pm$'$EW1_err[$cnt_n]
   else
      set ra2 = $ra_c[$cnt_n]
      set dec2 = $dec_c[$cnt_n]
      set EW2 = ".."
   endif
   set tru_test = `echo "$class_flag[$cnt_n]>0" | bc -l`
   if ($tru_test == "1") then
      @ n_big = $n_big + 1
      @ n_last = $n_big + $n_off
      echo $n_last"&"$wave1[$cnt_n]"&"$ra1"&"$dec1"&"$flux1[$cnt_n]'$\pm$'$flux1_err[$cnt_n]"&"$EW2"&"$ra2"&"$dec2"&"$class_map[$class_flag[$cnt_n]]"&"$z1[$cnt_n]"&"$xray_val[$cnt_n]"\\" >> table_mk.out
   else
#            echo $n_last"&"$wave1[$cnt_n]"&"$ra1"&"$dec1"&"$flux1[$cnt_n]"&"$EW2"&"$ra2"&"$dec2"&Unknown&"$z1[$cnt_n]"&TBD&-\\" >> table_mk.out
   endif
end
exit(0)
