#!/bin/bash
# -----------------------------------------------------------------------------
# Creates plots of time-series comparisons between simulation and observation
# data from OMNI_M, STEREO-Ahead and STEREO-Behind spacecrafts.
# -----------------------------------------------------------------------------

# Fink.
. /sw/bin/init.sh

# Fortran.
export PATH="/usr/local/gfortran/bin:${PATH}"
export DYLD_LIBRARY_PATH="/usr/local/gfortran/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

# Miscellaneous scripts.
export PATH="${HOME}/Projects/scripts/csh:${PATH}"
export PATH="${HOME}/Projects/scripts/sh:${PATH}"


declare -ir STATUS_OK=0
declare -ir STATUS_ERROR=1

declare -ir MIN_CR_NO=1625

declare -r LIVE_RUN_HOME="/Library/WebServer/Documents/data/runs"

declare -r UNDEFINED_VAR_ERR_MSG="
ERROR: Undefined variable: %s.
"
declare -r INVALID_ARG_ERR_MSG="
ERROR: Invalid input argument: %s.
"
declare -r INVALID_CR_NOS_ERR_MSG="
ERROR: Invalid input Carrington rotation number(s): %s.
"
declare -r RUN_DIR_NOT_FOUND_ERR_MSG="
ERROR: Run directory not found: %s.
"
declare -r CREATE_TS_COMP_ERR_MSG="
ERROR: Failed to generate time-series comparisons: %s.
"

if [[ -z "${RUN_HOME}" ]]; then
  RUN_HOME="${LIVE_RUN_HOME}"
fi

if [[ -z "${TS_COMP_HOME}" ]]; then
  printf "${UNDEFINED_VAR_ERR_MSG}" "TS_COMP_HOME" >&2
  exit ${STATUS_ERROR}
elif [[ -z "${SC_DATA_HOME}" ]]; then
  printf "${UNDEFINED_VAR_ERR_MSG}" "SC_DATA_HOME" >&2
  exit ${STATUS_ERROR}
fi

# -----------------------------------------------------------------------------

print_usage_msg() {
  printf "
 Script     : mkweb_ts_comparisons.sh.
 Description: Creates plots of time-series comparisons between simulation and
              observation data from OMNI_M, STEREO-Ahead and STEREO-Behind
              spacecrafts.

 Usage: mkweb_ts_comparisons.sh
          --cr_nos <seq_of_ints>
          --res <str>
          --names <seq_of_strs>

 cr_nos: Carrington rotation number(s).
 res   : Resolution.
 names : Simulation name(s).
 \n"
}

init_vars_cli_args() {
  declare cli_cr_nos
  declare cli_res
  declare cli_names

  declare arg
  declare -i i=1

  while [[ ${i} -le $# ]]; do
    arg="${!i}"
    i=$((i + 1))

    if [[ "${arg}" = "--cr_nos" ]]; then
      cli_cr_nos="${!i}"
    elif [[ "${arg}" = "--res" ]]; then
      cli_res="${!i}"
    elif [[ "${arg}" = "--names" ]]; then
      cli_names="${!i}"
    else
      printf "${INVALID_ARG_ERR_MSG}" "${arg}" >&2
      return ${STATUS_ERROR}
    fi
    i=$((i + 1))
  done

  if [[ -z "${cli_cr_nos}" || -z "${cli_res}" ]]; then
    print_usage_msg
    return ${STATUS_ERROR}
  fi

  declare -a cli_cr_no_list
  declare -i cli_cr_no

  cli_cr_no_list=( $(ps_print_seq_num.sh "${cli_cr_nos}") )
  if [[ $? -ne ${STATUS_OK} ]]; then
    printf "${INVALID_CR_NOS_ERR_MSG}" "${cli_cr_nos}" >&2
    return ${STATUS_ERROR}
  else
    for cli_cr_no in ${cli_cr_no_list[@]}; do
      if [[ ${cli_cr_no} -lt ${MIN_CR_NO} ]]; then
        printf "${INVALID_CR_NOS_ERR_MSG}" "${cli_cr_no}" >&2
        return ${STATUS_ERROR}
      fi
    done
  fi
  cr_no_list=( ${cli_cr_no_list[@]} )

  res="${cli_res}"

  name_list=( ${cli_names//,/ } )

  return ${STATUS_OK}
}

create_ts_comp() {
  declare cr_no="$1"
  declare res="$2"
  declare name="$3"

  declare run_path="${RUN_HOME}/cr${cr_no}-${res}/${name}"
  declare hel_run_path
  declare ts_comp_path
  declare sc_data_path

  hel_run_path="${run_path}/hel/mhd_shifted"
  if [[ ! -d "${hel_run_path}" ]]; then
    hel_run_path="${run_path}/helio/mhd_shifted"
  fi
  if [[ ! -d "${hel_run_path}" ]]; then
    hel_run_path="${run_path}/helio"
  fi
  if [[ ! -d "${hel_run_path}" ]]; then
    printf "${RUN_DIR_NOT_FOUND_ERR_MSG}" "cr${cr_no}-${res}/${name}/<hel...>"
    return ${STATUS_ERROR}
  fi

  ts_comp_path="${TS_COMP_HOME}/cr${cr_no}-${res}/${name}"
  sc_data_path="${SC_DATA_HOME}/cr${cr_no}"
  echo "${ts_comp_path}"
  echo "${sc_data_path}"

  ps_create_ts_comparisons.sh \
    -cr_num "${cr_no}" \
    -hel_data "${hel_run_path}" \
    -sc_data "${sc_data_path}" \
    -img_template "${ts_comp_path}/ts"
  return $?
}

main() {
  if [[ "$1" = "-h" || "$1" = "--help" ]]; then
    print_usage_msg
    return ${STATUS_OK}
  fi

  declare -a cr_no_list
  declare res
  declare -a name_list

  init_vars_cli_args "$@"
  [[ $? -ne ${STATUS_OK} ]] && return ${STATUS_ERROR}

  declare -i cr_no
  declare -a run_path_list
  declare run_path
  declare name

  for cr_no in ${cr_no_list[@]}; do
    run_path_list=( $(ls -1d ${RUN_HOME}/cr${cr_no}-${res}/* 2> /dev/null) )
    if [[ ${#run_path_list[@]} -eq 0 ]]; then
      printf "${RUN_DIR_NOT_FOUND_ERR_MSG}" "cr${cr_no}-${res}"
      return ${STATUS_ERROR}
    fi

    for run_path in ${run_path_list[@]}; do
      name="$(basename "${run_path}")"
      if [[ ${#name_list[@]} -gt 0 && \
            $(ps_get_item_index.sh "${name}" ${name_list[@]}) -eq -1 ]]; then
        continue
      fi

      create_ts_comp "${cr_no}" "${res}" "${name}"
      if [[ $? -ne ${STATUS_OK} ]]; then
        printf "${CREATE_TS_COMP_ERR_MSG}" "cr${cr_no}-${res}/${name}" >&2
        return ${STATUS_ERROR}
      fi
      printf "\n"
    done
  done

  return ${STATUS_OK}
}

# -----------------------------------------------------------------------------

main "$@"
exit $?
