#! /bin/bash -f
# Last edited on 2023-10-21 20:09:34 by stolfi

pcaDir="$1"; shift        # Directory with the PCA data files.
chnamesCommas="$1"; shift # Channel names separated by ','.
runs( "$@" )              # EEG file names. 

chnames=( `echo ${chnamesCommas} | tr ',' '\012'` )

# ---------------------------------------------------------------------
# Create the electrode interpolation basis:

width="0450"
height="0630"
# Just KILL the idiot who decided that '037' is octal not decimal!
width=`printf '%04d' $(( 10#${width} ))`
height=`printf '%04d' $(( 10#${height} ))`

# Interesting basis types: gauss:1:1:1, shepard:0:1:1, voronoi:0:0:2

btype=voronoi
interp=0
norm=0
msub=2

maskDir="${pcaDir}/${width}_${height}"
basisDir="${maskDir}/${btype}_i${interp}_n${norm}"

echo "Creating electrode image basis in directory ${basisDir}..." 1>&2
mkdir -pv ${basisDir}

rm -f ${maskDir}/*.{fni,png} 
rm -f ${basisDir}/*.{fni,png} 

nmeeg_make_imgbasis \
  -electrodes ${chnames[@]} \
  -type ${btype} \
  -interp ${interp} \
  -norm ${norm} \
  -scalpSize 150 180 150 \
  -imageSize ${width} ${height} -extent 0.95 \
  -subsample ${msub} \
  -outDir ${pcaDir}

# Convert images and show:

for fname in \
  ${maskDir}/msk.fni \
  ${maskDir}/elp.fni \
  ${basisDir}/*.fni \
; do \
  pname="${fname%.*}.png" ; \
  fni_to_png \
      -isMask T \
      -min -2.0 -max +2.0 \
      -maxval 65535 \
      -nanval 0 \
    < ${fname} \
    > ${pname} 
done

basisFiles=( ${basisDir}/*.png )
if [[ ${#basisFiles[@]} -ne 0 ]]; then
  display ${maskDir}/msk.png ${maskDir}/elp.png ${basisFiles[@]}
else
  echo "** no basis files generated" 1>&2; exit 1
fi
  

# ----------------------------------------------------------------------
# Display the component patterns as images:

eigFiles=( `cd ${pcaDir} && ls P*_eig.txt | sort` )

sop=( )
for eigFile in ${eigFiles[@]} ; do
  eigName="${eigFile/.txt/}"
  sop+=( "-setName" ${eigName} )
  frameDir="${pcaDir}/${eigName}"
  mkdir -pv ${frameDir}
done

nmeeg_animate \
    -electrodes ${chnames[@]} \
    -maskDir "${maskDir}" \
    -basisDir "${basisDir}" \
    -skip 0 -read 1 -step 1 \
    -inDir "${pcaDir}" \
    ${sop[@]} \
    -outDir "${pcaDir}"

nshown=0
for eigFile in ${eigFiles[@]} ; do
  eigName="${eigFile/.txt/}"
  frameDir="${pcaDir}/${eigName}"
  frames=( `ls ${frameDir}/f*.png | sort` )
  if [[ ${#frames[@]} -eq 0 ]]; then 
    echo "nmeeg_animate yielded no frames" 1>&2 ; exit 1
  elif [[ ${#frames[@]} -gt 1 ]]; then 
    echo "nmeeg_animate yielded multiple frames" 1>&2 ; exit 1
  fi
  eigRGBName="${pcaDir}/${eigName}_rgb.png"
  mv -v "${frames[0]}" ${eigRGBName}
  if [[ ( "/${show}" == "/SHOW" ) && ( ${nshown} -lt 5 ) ]]; then
    display -title '%f' ${eigRGBName}
    nshown=$(( ${nshown} + 1 ))
  fi
done

ls ${pcaDir}/P???_eig_rgb.png | sort | head -n 8  > .imgs
if [[ -s .imgs ]]; then
  convert `cat .imgs` -resize '50%' +append ${pcaDir}/pca.png
  if [[ -s ${pcaDir}/pca.png ]]; then
    display ${pcaDir}/pca.png
  else
    echo "** ${pcaDir}/pca.png not generated" 1>&2 
  fi
else
  echo "** no images geberated" 1>&2 
fi
