#!/bin/bash
#Script to generate DataList file needed for analysis of SPO-DVR data
#Script to generate RoVib Datalist needed for analysis

#for the RoVib levels it is best to use the state with the highest quantum numbers --> more internal states are printed
RoVib="v1j0mj0.A0.0.5eV-1.4eV.out"
qnumax=1

v0endj=11
v1endj=7
WPs="B0.0.15eV-0.55eV.ReacProb A0.0.5eV-1.4eV.ReacProb" # .A.0.2eV-0.6eV.ReacProb .B.0.57eV-1.4eV.ReacProb"
# .0.2eV-0.75eV.out 0.05eV-0.25eV.out

if [ -f DataList ]; then
  rm DataList
fi
if [ -f RoVib.levels ]; then
  rm RoVib.levels
fi

for (( qnu=0; qnu <= $qnumax; qnu++ ))
do
  if [ $qnu -eq 0 ]; 
  then
    qjmax=$v0endj
  fi
  if [ $qnu -eq 1 ];
  then
    qjmax=$v1endj
  fi
  for (( qj=0; qj <= $qjmax; qj++ ))
  do
    for (( qmj=0; qmj <= $qj; qmj++ ))
    do
      echo "$qnu $qj $qmj 4 ../ReacProb/QCT.v${qnu}j${qj}mj${qmj} " >> DataList
      for WP in "B" "A"
      do
#now i  can loop over the states
exist=0
i=0
while [ $exist != 1 ];
do
  if [ "$WP" == "A" ]; 
  then 
    WPname=".0.5eV-1.4eV.ReacProb"
  fi
  if [ "$WP" == B ];
  then
    WPname=".0.15eV-0.55eV.ReacProb"
  fi
  

  file="v${qnu}j${qj}mj${qmj}.${WP}${i}${WPname}"
  echo ${file}
  if [ ! -f ../ReacProb/$file ]; 
  then
    ((i--))
    file="v${qnu}j${qj}mj${qmj}.${WP}${i}${WPname}"
    DIM=`tail -2 ../ReacProb/$file | head -1 | awk '{print $1}' `
    echo "$qnu $qj $qmj $DIM ../ReacProb/$file " >> DataList
    exist=1
#    if [ "$WP" == "A" ];
#    then 
#      RP=` head -1 ../ReacProb/${file} | awk '{print $3}'`
#      echo $RP
#      echo "0 0.05 $RP 0 " > ../ReacProb/bla
#      cat ../ReacProb/${file} >> ../ReacProb/bla
#      cat ../ReacProb/bla
#    
#      mv ../ReacProb/bla ../ReacProb/${file}
#    fi
  fi
  ((i++))
  
done
      done #WP
    done #mj
  done #j
done #v
cat DataList
exit 1
#########################################################################
#########################################################################
#now for extracting RoVib levels
starttable=`awk '/INFO FROM SETTING UP THE ASYMPTOTIC ROVIBRATIONAL STATES/ {print NR}' ../out/$RoVib` 
endtable=`awk '/INFO FROM SETTING UP THE DIFFRACTION BASIS/ {print NR}' ../out/$RoVib`
starttable=` echo "$starttable + 7" | bc `
endtable=` echo "$endtable -13" | bc `
#echo "$starttable   $endtable"

vmax=`awk ' NR=='$endtable' {print $1}' ../out/$RoVib`
jmax=`awk ' NR=='$endtable' {print $2}' ../out/$RoVib`
totstates=`echo "($vmax + 1) * $jmax " | bc `

echo "#RoVib levels from $RoVib"				>> RoVib.levels
echo "$vmax          #nu_max"					>> RoVib.levels
echo "$jmax          #j_max"					>> RoVib.levels
echo "$totstates     #total states "				>> RoVib.levels
while [ "$starttable" -le "$endtable" ];
do
  awk ' NR=='$starttable' {print $1, $2, $3}' ../out/$RoVib	>> RoVib.levels
  ((starttable++))
  ((starttable++))
done


