#! /bin/bash
# Last edited on 2023-12-07 12:51:08 by stolfi

echo "$*" 1>&2

inname="$1"; shift;    # Name of EEG file minus ".txt".
ch="$1"; shift;        # Elecrode name, "C{NNN}" where {NNN} is 3 digits from "001" on.
val_vmax="$1"; shift;  # Max value for electrode plots.

eeg_file="${inname}.txt"

if [[ ! ( -s ${eeg_file} ) ]]; then
  echo "!! file ${eeg_file} not found, skipped" 1>&2; exit 0
fi
chan_plot_file="${inname}_${ch}.png"
echo "plotting electrode values ${eeg_file} --> ${chan_plot_file}" 1>&2

mkscale=$( echo '0.015*'"${val_vmax}"'' | bc -lq )
mkshift0=$( echo '-0.7*'"${val_vmax}"'' | bc -lq )
mkshift1=$( echo '-0.8*'"${val_vmax}"'' | bc -lq )
mkshift2=$( echo '-0.9*'"${val_vmax}"'' | bc -lq )
echo "mkscale = ${mkscale} mkshift = ${mkshift0} ${mkshift1} ${mkshift2}"

# Get number of frames {nt} and sampling rate {fsmp}::
nt=( `head -n 50 ${eeg_file} | egrep -e '^nt[ ]*[=]'` )
nt="${nt[2]}"
fsmp=( `head -n 50 ${eeg_file} | egrep -e '^fsmp[ ]*[=]'` )
fsmp="${fsmp[2]/[.]*/}"
echo "nt = ${nt} fsmp = ${fsmp}" 1>&2

# Choose the segment to plot in detail (a bit over 2 beats):
val_nplot0=$(( 2 * ( 490 * ${fsmp} / 1000 ) ))
val_nskip0=$(( ( ${nt} - ${val_nplot0} ) / 2 ))
val_hsize0=600

chnum="${ch/C/10#}"
chix=$(( ${chnum} - 1 ))
val_nskip1=0;   val_nplot1=0;   val_hsize1=1400;  # Plot whole file.
val_vsize=550 # Vert size of channel plots.
nmeeg_plot_bis_channels.sh \
    NOSHOW \
    ${eeg_file/.txt/} \
    0 ${val_vmax} \
    ${chix} ${chix} \
    ${val_nskip0} ${val_nplot0} ${val_hsize0} \
    ${val_nskip1} ${val_nplot1} ${val_hsize1} \
    ${val_vsize} \
    -outPrefix  ${chan_plot_file/.png/} \
    -marker 130 ${mkscale} ${mkshift0} \
    -marker 131 ${mkscale} ${mkshift1} \
    -marker 132 ${mkscale} ${mkshift2} \
  || rm -f ${chan_plot_file}
if [[ ! ( -s ${chan_plot_file} ) ]]; then 
  echo "** nmeeg_plot_bis_channels.sh failed, ${chan_plot_file} not created" 1>&2; exit 1;
fi
ls -l ${chan_plot_file} 

pwr_file="${inname}_pwr.txt"
pwr_plot_file="${inname}_${ch}_pwr.png"
echo "plotting spectrum ${pwr_file} --> ${pwr_plot_file}" 1>&2
rm -f ${pwr_plot_file}

# Determine the max freq to plot {pwr_fmax}:
pwr_fsmp=( `cat ${pwr_file} | egrep -e '^fsmp *='` )
echo "data ${pwr_fsmp[*]}" 1>&2
pwr_fmax=$(( ( ${pwr_fsmp[2]} + 1 ) / 2 ))  # Max frequency to plot in Hz.
echo "plot fmax = ${pwr_fmax}" 1>&2

pwr_vsize=550 # Vertical plot size in pixels for power spectra.
nmeeg_plot_spectra.sh \
    NOSHOW \
    "${pwr_file/.txt/}" \
    ${pwr_fmax} ${chix} ${chix} \
    ${pwr_vsize} \
    'linespoints pt 7' \
    -outPrefix "${pwr_plot_file/.png/}" \
  || rm -f ${pwr_plot_file}
if [[ ! ( -s ${pwr_plot_file} ) ]]; then
  echo "** nmeeg_plot_spectra.sh failed, ${pwr_plot_file} not created" 1>&2; exit 1;
fi
ls -l ${pwr_plot_file}

# Plot electrode histogram:
hist_data_file="${inname}_${ch}_hist.txt"
hist_plot_file="${inname}_${ch}_hist.png"
echo "plotting channel histogram ${eeg_file} --> ${hist_plot_file}" 1>&2
nmeeg_plot_electrode_histogram.sh \
    NOSHOW \
    ${ch} \
    ${hist_data_file/.txt/} \
    ${hist_plot_file/.png/} \
  || rm -f ${hist_plot_file}
if [[ ! ( -s ${hist_plot_file} ) ]]; then 
  echo "** nmeeg_plot_electrode_histogram.sh failed, ${hist_plot_file} not created" 1>&2; exit 1
fi
