#!/bin/bash

# . ~/.bashrc

#export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin"

#export DYLD_LIBRARY_PATH=""

# GNU Fortran
#export PATH="/usr/local/gfortran/bin:${PATH}"
#export DYLD_LIBRARY_PATH="/usr/local/gfortran/lib:${DYLD_LIBRARY_PATH}"

# GNU MPI
#export PATH="${HOME}/Projects/tmp/deps.gnu/openmpi/bin:${PATH}"
#export DYLD_LIBRARY_PATH="${HOME}/Projects/tmp/deps.gnu/openmpi/lib:${DYLD_LIBRARY_PATH}"

# HDF5
#export PATH="${HOME}/Projects/tmp/deps.gnu/hdf5/bin:${PATH}"
#export DYLD_LIBRARY_PATH="${HOME}/Projects/tmp/deps.gnu/hdf5/lib:${DYLD_LIBRARY_PATH}"

# IDL
#export IDL_DIR="/Applications/exelis/idl"
#export PATH="${IDL_DIR}/bin:${PATH}"

# ImageMagick
export MAGICK_HOME="/usr/local/ImageMagick"
export PATH="${MAGICK_HOME}/bin:${PATH}"
export DYLD_LIBRARY_PATH="${MAGICK_HOME}/lib:${DYLD_LIBRARY_PATH}"

# Tools
#export PS_TOOLS_HOME="/usr/local/ps_tools"
#test -r ${PS_TOOLS_HOME}/init.sh && . ${PS_TOOLS_HOME}/init.sh

# export OMP_NUM_THREADS="4"

# export RUNS_HOME="/Library/WebServer/Documents/data/runs"
#export RUNS_HOME="/Volumes/shadow-2/Users/janvier/Projects/corhel_5.1.0_runs"
#export IMGS_HOME="/Users/janvier/Sites/data/emission_images"
export IMGS_HOME="/shadow2-extdata2/areyes/Projects/auto-corhel-post-process/emission_images_test"
export RUNS_HOME="/shadow2-extdata1/mhdweb/all_runs/corhel_6_runs"


crNumList=( `ps_print_seq_num.sh "$1"` )
res=$2
nameList=( ${3//,/ } )

for crNum in ${crNumList[@]}; do
  nNames=0

  for dir in `ls -1d ${RUNS_HOME}/cr${crNum}-${res}/*_mast_mas_std_0?01`; do
    name=`basename ${dir}`
    echo "ls -1d ${RUNS_HOME}/cr${crNum}-${res}/*_mast_mas_std_0?01"
    echo ${name}
    echo "ps_get_item.sh ${name} ${nameList[@]}"

    if [[ ${#nameList[@]} -gt 0 \
          && `ps_get_item.sh ${name} ${nameList[@]}` = "" ]]; then
      continue
    fi

    runDirName="${RUNS_HOME}/cr${crNum}-${res}/${name}/corona"
    runDirName="${RUNS_HOME}/cr${crNum}-${res}/${name}/cor/mhd"
    imgDirName="${IMGS_HOME}/cr${crNum}-${res}/${name}"

    diagFileName="${IMGS_HOME}/${crNum}.out"

    if [[ ${nNames} -gt 0 ]]; then
      printf "\n" >> ${diagFileName} 2>&1
      printf "\n" >> ${diagFileName} 2>&1
    fi

    ps_create_emission_images.sh \
        -cr_num ${crNum} -cor_data ${runDirName} -cor_periodic -res 240,512 \
        -output ${imgDirName} >> ${diagFileName} 2>&1

    nNames=$((nNames + 1))
  done

  echo "Number of names for Carrington rotation ${crNum}: ${nNames}"
done
