#! /bin/bash
# Last edited on 2023-12-07 15:45:49 by stolfi

topdir="$1"; shift
vmax="$1"; shift
vmax_lo="$1"; shift

subj="v01"

subjdir="${topdir}/${subj}"

# Get session file names:
sessions=( `cd ${subjdir} && ls -d [VRS]_[0-9][0-9][0-9][0-9] | sort` )
( cd ${subjdir} && wc -l  `echo ${sessions[@]} | tr ' ' '\012' | sed -e 's:$:/eeg.txt:g' ` )  1>&2

( cd ~/programs/c/NEUROMAT/nmeeg_plot_channels && make uninstall build install )
( cd ~/programs/c/NEUROMAT/nmeeg_spectrum      && make uninstall build install )
( cd ~/programs/c/NEUROMAT/nmeeg_plot_spectra  && make uninstall build install )

# Compute statistics, histograms:
for sess in ${sessions[@]}; do 
  echo "=== session ${sess} ===" 1>&2
  sessdir="${subjdir}/${sess}"
  rm -f ${sessdir}/*{.png,_{stats,hist,pwr}.txt}
  for Xband in X X_lo ; do 
    sess_eeg_file="${sessdir}/eeg${Xband/X/}.txt"
    if [[ ! ( -s ${sess_eeg_file} ) ]]; then
      if [[ "/${Xband}" == "/Z=X" ]]; then
        echo "** file ${sess_eeg_file} not found" 1>&2; exit 1
      else
        echo "!! file ${sess_eeg_file} not found, skipped" 1>&2; continue
      fi
    fi

    echo "processing ${sess_eeg_file}..." 1>&2
    # Get channel names:
    chnames=( `head -n 100 ${sess_eeg_file} | egrep -e '^channels *='` )
    chnames=( ${chnames[@]:2} )
    echo "chnames = ${chnames[*]}" 1>&2

    sess_stat_file="${sess_eeg_file/.txt/}_stats.txt"
    echo "computing statistics ${sess_eeg_file} --> ${sess_stat_file}" 1>&2
    nmeeg_get_stats \
        -verbose \
        -hStep 1 \
        -outPrefix ${sess_eeg_file/.txt/}_ \
      < ${sess_eeg_file} \
      > ${sess_stat_file} \
      || rm -f ${sess_stat_file}
    if [[ ! ( -s ${sess_stat_file} ) ]]; then 
      echo "** nmeeg_get_stats failed, ${sess_stat_file} not created" 1>&2; exit 1
    fi
    cat ${sess_stat_file} 1>&2

    sess_pwr_file="${sess_eeg_file/.txt/}_pwr.txt"
    echo "computing spectrum ${sess_eeg_file} --> ${sess_pwr_file}" 1>&2
    rm -f ${sess_pwr_file}
    nmeeg_spectrum \
        ${sess_pwr_file/_pwr.txt/} \
        0 0 \
        < ${sess_eeg_file} \
      || rm -f ${sess_pwr_file}
    if [[ ! ( -s ${sess_pwr_file} ) ]]; then 
      echo "** nmeeg_spectrum failed, ${sess_pwr_file} not created" 1>&2; exit 1;
    fi
    ls -l ${sess_pwr_file}

    # chnames_plot=( ${chnames[@]:0:130} )
    # chnames_plot=( ${chnames[@]:128:2} ${chnames[@]:8:3} ${chnames[@]:17:1} ${chnames[@]:21:1} ${chnames[@]:73:2} ${chnames[@]:81:1} )
    chnames_plot=( ${chnames[@]:128:2} )
    # Get proper "{vmax}: 
    if [[ ( "/${ch}" == "/C129" ) || ( "/${ch}" == "/C130" ) ]]; then
      band_vmax=100
    elif [[ "/${Xband}" == "/X" ]]; then
      band_vmax=${vmax}
    else
      band_vmax=${vmax_lo}
    fi
    echo "plotting channels of ${sess_eeg_file} vmax = ${vmax}" 1>&2
    for ch in ${chnames_plot[@]} ; do
      echo "... ${ch} ...................................................." 1>&2
      band_eeg_file="${sess_eeg_file/.txt/}${Xband/X/}.txt"
      do_plot_channel.sh "${sess_eeg_file/.txt/}" ${ch} ${band_vmax}
    done
  done
  if [[ "/${sess/[RSV]_/}" == "/0000" ]]; then
    ( display -title "${sess} ${ch} %f" ${sessdir}/*.png )
  fi
done
