#!/bin/bash

# set input/working/output folders
in=/Users/lorenzo/Desktop/scenario_001
work=/Users/lorenzo/Desktop/scenario_001/tmp
out=/Users/lorenzo/Desktop/scenario_001/out2

# check if working folder exists, otherwise create it
if [[ ! -e $work ]]; then
  mkdir $work
else
  if [[ ! -d $work ]]; then
    echo $work is not a folder.
    exit 1
  fi
fi
# move to working folder
cd $work
if [[ $? != 0 ]]; then
  echo Cannot move to $work
  exit 1
fi

# check if output folder exists, otherwise create it
if [[ ! -e $out ]]; then
  mkdir $out
else
  if [[ ! -d $out ]]; then
    echo $out is not a folder.
    exit 1
  fi
fi

listfile=$in/v0_22.txt
# count number of lines in input file
nlines=`wc -l $listfile | awk '{print $1}'`

eqfile=$in/F4E_Equil_10520.eqdsk
# look for time value in equil file
base=`basename $eqfile .eqdsk`
tstr=`echo $base | grep "[[:digit:]]\{5\}" -o`
t=`echo "$tstr-10000" | bc`
# copy input files in working folder
cp $eqfile ${base}.eqdsk
cp $in/${base}.prf ${base}.prf

# set temporary file names
beambase="tmpbeam"
beamfile=${beambase}.txt

# set output file names
id=t${t}_scan_`basename $listfile .txt`
f7file=$out/f7_${id}.txt
f48file=$out/f48_${id}.txt
rm -f $out/log_${id}.txt
# add header in output files
echo "# =============================" > $f7file
echo "# `date`" >> $f7file
echo "# Launching conditions: `basename $listfile`" >> $f7file
echo "# time=$t" >> $f7file
echo "# =============================" >> $f7file
cp $f7file $f48file
appendheader=0

#loop on lines of launching conditions table
for (( iline=1; iline<=$nlines+1; iline++ )); do
  # search full, non-commented lines
  awkcommand='NR=='$iline' && NF>=13 {print $0}'
  line=`sed 's/#.*$//' $listfile | awk "$awkcommand"`
  if (( `echo $line | wc -w`>0 )); then
    # if valid line extract values
    ibeam=`echo $line | awk '{print $1}'`
    gamma=`echo $line | awk '{print -$2}'`
    alpha=`echo $line | awk '{print $3}'`
    beta=`echo $line | awk '{print $4}'`
    r0=`echo $line | awk '{print sqrt($5^2+$6^2)/10.0}'`
    z0=`echo $line | awk '{print $7/10.0}'`
    # create file for beam description to be read by gray
    echo 2 > $beamfile
    echo $line | \
    awk '{print -$2,$3-0.1,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' >> $beamfile
    echo $line | \
    awk '{print -$2,$3+0.1,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' >> $beamfile
    # replace dummy values in template for gray.data
    sed "s/-alfa-/$alpha/g; s/-beta-/$beta/g; \
         s/-x0-/$r0/g;      s/-z0-/$z0/g; \
         s/-beam-/$beambase/g; \
         s/-equil-/$base/g; s/-prf-/$base/g" $in/gray0beam2.data > gray.data
    # run gray
    gray >> $out/log_${id}.txt
    if (( $appendheader==0 )); then
      # append parameters to header if first run of the loop
      cat headers.txt >> $f7file
      cat headers.txt >> $f48file
      echo "#beam dgamma`head -n 1 fort.7 | sed 's/#//'`" >> $f7file
      echo "#beam dgamma`head -n 1 fort.48 | sed 's/#//'`" >> $f48file
      appendheader=1
    fi
    # add beam number and gamma values in first two columns
    awk 'NR>1 {print '$ibeam','$gamma',$0}' fort.7 >> $f7file
    awk 'NR>1 {print '$ibeam','$gamma',$0}' fort.48 >> $f48file
    echo "" >> $f48file
  else
    # if first block of data has already been written
    if (( $appendheader==1 )); then
      # write blank line in output when invalid (blank) line is found in input
      echo "" >> $f7file
      echo "" >> $f48file
    fi
  fi
done
#end of launching conditions loop

# remove files from working folder
rm -f ${base}.eqdsk ${base}.prf $beamfile fort.*

exit 0
#EOF