#! /bin/bash
# Last edited on 2021-08-23 00:48:15 by stolfi

logdate="$1"; shift  # Date for log name prefix.

# Splits the raw binary-format 128-electrode session recordings in the "raw"
# directory into separate text-format EEG datasets, one for each experimental
# run, stored in the "raw-runs" directory.

# Assumes that input files are called "raw/${NAME}.raw" where ${NAME} is 
# "S10", "S11", etc.

# Each output file will be called "raw-runs/s${SSS}/r${BBB}${RR}.txt"
# where ${SSS} is the subject's numeric ID, ${BBB} is the block's
# numeric ID (001-008), and ${RR} is the the run's numeric ID in 
# the block (01--32).

# Writes a log file for each subject in "raw-runs/s${SSS}/extract_runs_${logdate}.log".

printf '# Script %s\n' "${0##*/}" 1>&2
printf '# Log date %s\n' "${logdate}" 1>&2
    
chnames=( `cat channel_names.txt | gawk '/^[ ]*[0-9]/{ print $3; }'` )
printf "assuming %d channels = %s\n" "${#chnames[@]}" "${chnames[*]}" 1>&2

mkdir -p raw-runs

( cd ~/programs/c/NEUROMAT/nmeeg_convert_raw && make uninstall build install )
( cd ~/programs/c/NEUROMAT/nmeeg_split_e128_gh2013 && make uninstall build install )

tmp="/tmp/$$"

function eegsplit(){
  origname="$1"; shift; # Name of raw source file, sans ".raw".
  subject="$1"; shift;  # Subject's ID number.
  nt_exp="$1"; shift;   # Frames expected in raw source file.
  
  # set -o xtrace
 
  nt_run_pre=1750  # Fixation frames to include in each run.
  nt_run_pos=1750  # Stimulus frames to include in each run.
  fsmp=500     # Sampling frequency in raw source file.
  
  tfile="${tmp}.dat"
  sdir="raw-runs/s${subject}"
  mkdir -p ${sdir}
  gfile="${sdir}/extract_runs_${logdate}.log"
  
  echo "=== processing raw/${origname}.raw ========" 1>&2
  echo "=== processing raw/${origname}.raw ========" >> ${gfile}
  
  /bin/rm -f ${gfile}
  echo "::::::::::::::::::::::::::::::::::::::::::::::::::::::::" >> ${gfile}
  echo "::: converting :::::::::::::::::::::::::::::::::::::::::" >> ${gfile}

  cat raw/${origname}.raw \
    | nmeeg_convert_raw \
        -unit 1.00 \
        -sourceFile ${origname} \
        -subject ${subject} \
    2>> ${gfile} > ${tfile}
    
  echo "::::::::::::::::::::::::::::::::::::::::::::::::::::::::" >> ${gfile}
  echo "::: splitting ::::::::::::::::::::::::::::::::::::::::::" >> ${gfile}
  
  cat ${tfile} \
    | nmeeg_split_e128_gh2013 \
        -firstBlock 01 \
        -firstRun 01 \
        -runsPerBlock 32 \
        -framesPerRun ${nt_run_pre} ${nt_run_pos} \
        -stimulusChannel HB   3.020 50.000 HB \
        -stimulusChannel HS   3.020 50.000 HS \
        -stimulusChannel QB   3.020 50.000 QB \
        -stimulusChannel QS   3.020 50.000 QS \
        -fixationChannel fix1 3.360 50.000 \
        -outDir raw-runs \
      >> ${gfile} 2>&1 
      
  egrep -e '([!*][!*])|(^extracted)' ${gfile}
}

# Splitting
# rm -f raw-runs/s???/r?????.txt
eegsplit S10   010 854829
eegsplit S11   011 839438
eegsplit S12   012 718583
eegsplit S13   013 811665
eegsplit S14   014 805027
eegsplit S15   015 806482
eegsplit S16   016 807825
eegsplit S17   017 820836
eegsplit S18   018 805513
eegsplit S19   019 808238
eegsplit S20   020 819405
eegsplit S21   021 912787
