#! /bin/csh -f
#This is a simple script to gather all the LAEs in COSMOS, combine with 
#the IRAC data (but deweight it), run eazy, and copy the files 
#to uniquely (HPS) named files.
set scname=$0
if($#argv != 0) then
   echo Usage: $scname:t 'Nothing'
   exit(1)
endif
set nfile = `cat bu.EW1.classify_j1.out | awk '$9==1&&$13>0{print $0}' | wc -l`
set all_zs = `cat bu.EW1.classify_j1.out | awk '$9==1&&$13>0{print $4/1215.67-1.}'`
set cntr_num = `cat bu.EW1.classify_j1.out | awk '$9==1&&$13>0{print $13}'`
set indx_num = `cat bu.EW1.classify_j1.out | awk '$9==1&&$13>0{print NR}'`
echo "# id z_spec f_CFHTu e_CFHTu f_SubB e_SubB f_SubV e_SubV f_Subrp e_Subrp f_Subip e_Subip f_Subzp e_Subzp f_UKIRTJ e_UKIRTJ f_CFHTK e_CFHTK f_IRAC1 e_IRAC1 f_IRAC2 e_IRAC2 f_IRAC3 e_IRAC3 f_IRAC4 e_IRAC4" > /data1/grad56/jjadams/eazy-1.00/inputs/COSMOS_jja.cat
set cnt_n = 0
while ($cnt_n < $nfile)
   @ cnt_n = $cnt_n + 1
   if ($indx_num[$cnt_n]<10) then
      set spn_fl = "000"$indx_num[$cnt_n]
   else if ($indx_num[$cnt_n]<100) then
      set spn_fl = "00"$indx_num[$cnt_n]
   else if ($indx_num[$cnt_n]<1000) then
      set spn_fl = "0"$indx_num[$cnt_n]
   else
      set spn_fl = $indx_num[$cnt_n]
   endif
   set spn_num = `cat SEDs/nspawn_${spn_fl}.cat | awk '$1==var1{print NR}' var1=$cntr_num[$cnt_n]`
   set IRC1F1 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC1.txt | awk 'NR==var1{print $1}' var1=$cntr_num[$cnt_n]`
   set IRC1F2 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC1.txt | awk 'NR==var1{print $2*1.E10}' var1=$cntr_num[$cnt_n]`
   set IRC2F1 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC2.txt | awk 'NR==var1{print $1}' var1=$cntr_num[$cnt_n]`
   set IRC2F2 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC2.txt | awk 'NR==var1{print $2*1.E10}' var1=$cntr_num[$cnt_n]`
   set IRC3F1 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC3.txt | awk 'NR==var1{print $1}' var1=$cntr_num[$cnt_n]`
   set IRC3F2 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC3.txt | awk 'NR==var1{print $2*1.E10}' var1=$cntr_num[$cnt_n]`
   set IRC4F1 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC4.txt | awk 'NR==var1{print $1}' var1=$cntr_num[$cnt_n]`
   set IRC4F2 = `cat /data1/grad56/jjadams/vp_aux/progs/fc_iracC4.txt | awk 'NR==var1{print $2*1.E10}' var1=$cntr_num[$cnt_n]`
   cat SEDs/spawn_${spn_fl}.cat | awk 'NR==var1{print cnt_n,z_now,$0,spC1F1,spC1F2,spC2F1,spC2F2,spC3F1,spC3F2,spC4F1,spC4F2}' \
var1=$spn_num cnt_n=$cnt_n z_now=$all_zs[$cnt_n] spC1F1=$IRC1F1 spC1F2=$IRC1F2 spC2F1=$IRC2F1 spC2F2=$IRC2F2 \
spC3F1=$IRC3F1 spC3F2=$IRC3F2 spC4F1=$IRC4F1 spC4F2=$IRC4F2 >> /data1/grad56/jjadams/eazy-1.00/inputs/COSMOS_jja.cat
end
cd /data1/grad56/jjadams/eazy-1.00/inputs
../src/eazy
set cnt_n = 0
while ($cnt_n < $nfile)
   @ cnt_n = $cnt_n + 1
   set HPS_num = `cat /san/graduate/jjadams/baseline_Nov09_detections/pap_cat3.out | awk '$0=="COSMOS "var1{print NR}' var1=$indx_num[$cnt_n]`
   mv OUTPUT/${cnt_n}.obs_sed ../Karl_LAESEDs/HPS${HPS_num}.obs_sed
   mv OUTPUT/${cnt_n}.temp_sed ../Karl_LAESEDs/HPS${HPS_num}.temp_sed
end

exit(0)
