#!/bin/bash

exfor_dir="/opt/EXFOR/C4all"
data_dir="${HOME}/EXFOR/data"

usage="
Usage: `basename $0` [OPTIONS]
Retract data from the X4 data library in C4 format.
The options recognized are:
 -h, --help                        display this help and exit
 --exfor_dir=<C4 library folder>   give the folder where the X4 library in C4 format is installed.
                                   NOTE: If no value is given the script will look in folder ${exfor_dir}
 --data_dir=<data folder>          give the folder the output will be written.
                                   NOTE: If no value is given the script will write in home folder ${data_dir}
 --reaction=<reaction>             give the reaction to extract.
                                   NOTE: The reactions should be in the form \"pg\", where the first letter is
                                   the beam and the rest are the ejectiles
 --entry=<entry>                   give an entry to extract the specific datasets.
                                   NOTE: If the entry given is not unique (i.e. you give only the first 4 characters)
                                   then all the entries that match will be extracted
 --dataset=<dataset>               give a specific dataset to extract.
                                   NOTE: If the dataset given is not unique (i.e. you give less characters)
                                   then all the datest that match will be extracted.
"

for i in "$@" ; do
   case $i in
      -h|--help)
         echo "${usage}"
         exit 0
      ;;
      --exfor_dir=*)
         exfor_dir="${i#*=}"
      ;;
      --data_dir=*)
         data_dir="${i#*=}"
      ;;
      --reaction=*)
         AskedReaction="${i#*=}"
      ;;
      --entry=*)
         AskedEntry="${i#*=}"
      ;;
      --dataset=*)
         AskedDataset="${i#*=}"
      ;;
      *)
         echo "Unknown flag ${i}. Please run with -h or --help to see usage"
	 exit 1
      ;;
   esac
done

if [ ! -d ${exfor_dir} ] ; then
   echo "The exfor library in C4 format is not installed in folder ${exfor_dir}"
   echo "Please install it there, or define a different folder with --exfor_dir flag"
   exit 1
elif [ ! -f ${exfor_dir}/*.tbl ] ; then
   echo "The C4 table file doesn't exist in folder ${exfor_dir}"
   echo "Please check your installation of EXFOR"
   exit 1
elif [ ! -f ${exfor_dir}/*.xc4 ] ; then
   echo "The C4 library file doesn't exist in folder ${exfor_dir}"
   echo "Please check your installation of EXFOR"
   exit 1
fi
if [ ! ${exfor_version} ] ; then exfor_version=`ls ${exfor_dir}/*.tbl | sed "s/.\+C4\-\([0-9\-]\+\).\+/\1/g"` ; fi

exfor_c4file_name="C4-${exfor_version}.xc4"
exfor_c4file="${exfor_dir}/${exfor_c4file_name}"
exfor_c4table="${exfor_dir}/${exfor_c4file_name}.tbl"

subent=".........."

updated="............."


#prj="....."		#Projectile

target="......"		#Target in the for ZA i.e. 28058 for 58Ni

#mf="...."
#mf="..[\ ]3"		#for cross section data
#mf="..[\ ]4"		#for elastic data

#for mt in "..[\ ]3" "..[\ ]4" ".[\ ][5-9][0-9]" ".[\ ]16" ".[\ ]17" ".[\ ]18" ".[\ ]22" ".[\ ]28" ".[\ ]29" ".[\ ]37" ".[\ ]44" ".102" ".103" ".6[0-4][0-9]" ".104" ".6[5-9][0-9]" ".105" ".7[0-4][0-9]" ".106" ".7[5-9][0-9]" ".107" ".8[0-4][0-9]" ".111" ".112" ".201" ".202" ".203" ".204" ".205" ".206" ".207" ; do
#for mte in "non" "n" "n" "2n" "3n" "f" "na" "np" "n2a" "4n" "n2p" "g" "p" "p" "d" "d" "t" "t" "3He" "3He" "a" "a" "2p" "pa" "xn" "xg" "xp" "xd" "xt" "xh" "xa" ; do
#mt="...."		#Emiting particle
#mt="..[\ ]2"		#for a,el  need mf=4
#mt="..[\ ]3"		#for a,non
#mt="..[\ ]4"		#for n discrete+continuum
#mt=".[\ ][5-9][0-9]"	#for n 50-90 discrete 91 continuum
#mt=".[\ ]16"		#for 2n
#mt=".[\ ]17"		#for 3n
#mt=".[\ ]18"		#for fission
#mt=".[\ ]22"		#for na
#mt=".[\ ]28"		#for np
#mt=".[\ ]29"		#for n2a
#mt=".[\ ]37"		#for 4n
#mt=".[\ ]44"		#for n2p
#mt=".102"		#for g
#mt=".103"		#for p discrete+continuum
#mt=".6[0-4][0-9]"	#for p 600-648 discrete 649 continuum
#mt=".104"		#for d discrete+continuum
#mt=".6[5-9][0-9]"	#for d 650-698 discrete 699 continuum
#mt=".105"		#for t discrete+continuum
#mt=".7[0-4][0-9]"	#for t 700-748 discrete 749 continuum
#mt=".106"		#for 3He discrete+continuum
#mt=".7[5-9][0-9]"	#for 3He 750-798 discrete 799 continuum
#mt=".107"		#for a discrete+continuum
#mt=".8[0-4][0-9]"	#for a 800-848 discrete 849 continuum
#mt=".111"		#for 2p
#mt=".112"		#for pa
#mt=".201"		#for xn
#mt=".202"		#for xg
#mt=".203"		#for xp
#mt=".204"		#for xd
#mt=".205"		#for xt
#mt=".206"		#for xh
#mt=".207"		#for xa

#rm -rf ${data_dir}
if [ ! -d ${data_dir} ] ; then mkdir -p ${data_dir}/By_Mass ; mkdir -p ${data_dir}/By_Reaction ; fi
cd ${data_dir}
rm -rf exfor_extract.log
rm -rf exfor_extract.dum

if [ ! ${AskedEntry} ] && [ ! ${AskedDataset} ] ; then
   mf="..[\ ]3"		#for cross section data
   declare -A projectiles
   projectiles=([p]=".1001" [a]=".2004" [g]="....0" [n]="....1")
   mtes=( "non" "n" "n" "2n" "3n" "f" "na" "np" "n2a" "4n" "n2p" "g" "p" "p" "d" "d" "t" "t" "3He" "3He" "a" "a" "2p" "pa" "xn" "xg" "xp" "xd" "xt" "xh" "xa" )
   mts=( "..[\ ]3" "..[\ ]4" ".[\ ][5-9][0-9]" ".[\ ]16" ".[\ ]17" ".[\ ]18" ".[\ ]22" ".[\ ]28" ".[\ ]29" ".[\ ]37" ".[\ ]44" ".102" ".103" ".6[0-4][0-9]" ".104" ".6[5-9][0-9]" ".105" ".7[0-4][0-9]" ".106" ".7[5-9][0-9]" ".107" ".8[0-4][0-9]" ".111" ".112" ".201" ".202" ".203" ".204" ".205" ".206" ".207" )
   for projectile in "${!projectiles[@]}" ; do
      prj=${projectiles[${projectile}]}
      for index in "${!mtes[@]}" ; do
         mte=${mtes[${index}]}
         mt=${mts[${index}]}
         if [ ${AskedReaction} ] && [ "${AskedReaction}" != "${projectile}${mte}" ] ; then
            continue
         elif [ ! ${AskedReaction} ] && [ "${projectile}" == "p" ] && ( [ "${mte}" != "g" ] ) ; then
            continue
         elif [ ! ${AskedReaction} ] && [ "${projectile}" == "a" ] && ( [ "${mte}" != "p" ] && [ "${mte}" != "a" ] && [ "${mte}" != "n" ] && [ "${mte}" != "g" ] ) ; then
            continue
         elif [ ! ${AskedReaction} ] && [ "${projectile}" == "n" ] && ( [ "${mte}" != "p" ] && [ "${mte}" != "a" ] && [ "${mte}" != "g" ] ) ; then
            continue
         elif [ ! ${AskedReaction} ] && [ "${projectile}" == "g" ] && ( [ "${mte}" != "p" ] && [ "${mte}" != "a" ] && [ "${mte}" != "n" ] ) ; then
            continue
         fi
         grep "^${subent}${updated}${prj}${target}${mf}${mt}" ${exfor_c4table} | cut -c1-9 | sed "s/$/\ ${projectile}${mte}/" >> exfor_extract.dum
      done
   done

   if [ ! ${AskedReaction} ] ; then
      unset mt
      unset mte
      prj=".2004"		#for alpha particles
      mf="..[\ ]4"		#for elastic data
      mt=( "..[\ ]2" )
      mte=( "eldif" )
      grep "^${subent}${updated}${prj}${target}${mf}${mt[0]}" ${exfor_c4table} | cut -c1-9 | sed "s/$/\ a${mte[0]}/" >> exfor_extract.dum
   fi
else
   if [ ${AskedEntry} ] ; then
      subent=${AskedEntry}
   else
      subent=${AskedDataset}
   fi
   grep "^${subent}" ${exfor_c4table} | sed "s/^\(.........\).*(\(.*\),\(.*\)).*/\1\ \L\2\L\3/" >> exfor_extract.dum
fi

sort exfor_extract.dum > exfor_extract.log
rm -rf exfor_extract.dum

ext_index=0
ext_total=`wc -l exfor_extract.log | cut -d" " -f1`
while IFS=" " read line reaction ; do
   ext_index=$((${ext_index} + 1))
   entry=$(echo ${line} | cut -c1-5 | sed "s/\ //g")
   dataset=${entry}$(echo ${line} | cut -c6-9 | sed "s/\ //g")
   if [ "${entry}" != "${entry_read}" ] ; then
      if [ -f ${entry_read}.dum ] ; then rm -rf ${entry_read}.dum ; fi
      sed -n "/^\#ENTRY.*\ ${entry}/,/^\#\/ENTRY/p" ${exfor_c4file} > ${entry}.dum
      unset author
      unset year
      author=`sed -n "/^\#AUTHOR1/{p;q;}" ${entry}.dum | sed "s/#AUTHOR1\ \+//" | sed "s/[\+].*//" | sed "s/.*[\.\ ]//" | sed "s/[\ \n\r]//g"`
#      author=`sed -n "/^\#AUTHOR1/{p;q;}" ${entry}.dum | sed "s/.*[\.\ ]\([a-Z]+\)$/\1/" | sed "s/[\+\ \n\r]//g"`
      year=`sed -n "/^\#YEAR/{p;q;}" ${entry}.dum | sed "s/.*[0-9][0-9]\([0-9][0-9]\).*/\1/"`
      entry_read="${entry}"
   fi
   sed -n "/^\#DATASET.*\ ${dataset}/,/^\#\/DATASET/p" ${entry}.dum > ${dataset}.dum
   targ=`sed -n "/^\#REACTION/{p;q;}" ${dataset}.dum | sed "s/.*-\([A-Z]*\)-\([0-9]*\)[-\0]*(.*/\2\L\u\1/"`
   if [ ! -d By_Reaction/${reaction}/${targ} ] ; then mkdir -p By_Reaction/${reaction}/${targ} ; fi
   if [ ! -d By_Mass/${targ}/${reaction} ] ; then mkdir -p By_Mass/${targ}/${reaction} ; fi
   sed -i "/^\#[^\ ^\-]/d" ${dataset}.dum
   paste -d"\0" <(cut -c1,23-31 ${dataset}.dum) <(cut -c41-49 ${dataset}.dum) <(cut -c32-40 ${dataset}.dum) <(cut -c50-58 ${dataset}.dum) <(cut -c12,20-22,59-97 ${dataset}.dum) > By_Reaction/${reaction}/${targ}/${author}-${year}_${reaction}_${dataset}.dat
   ln -s -r By_Reaction/${reaction}/${targ}/${author}-${year}_${reaction}_${dataset}.dat By_Mass/${targ}/${reaction}/${author}-${year}_${reaction}_${dataset}.dat
   rm -rf ${dataset}.dum
   echo -ne "Extracting dataset ${dataset} (${targ}(${reaction})) (${ext_index}/${ext_total})                                                                        \r"
done < exfor_extract.log
rm -rf *.dum

exit 0
