#!/bin/bash

#. ~/.bashrc

export LIVE_CH_HOME="/shadow2-extdata2/areyes/Projects/acpp_test_files/mock_live_data/data/ch_maps"
export WORK_CH_HOME="/shadow2-extdata2/areyes/Projects/acpp_test_files/data/ch_maps"
export SC_BAL_MAP_HOME="/shadow2-extdata2/areyes/Projects/acpp_test_files/data/sc_bal_mappings"

scList=( earth sdo stereoa stereob )

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

for crNum in ${crNumList[@]}; do
  echo ${SC_BAL_MAP_HOME}/cr${crNum}-${res}
  test ! -d ${SC_BAL_MAP_HOME}/cr${crNum}-${res} && continue

  for dir in `ls -1d ${SC_BAL_MAP_HOME}/cr${crNum}-${res}/*`; do
    name=`basename ${dir}`

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

    ch_map_file="${WORK_CH_HOME}/cr${crNum}-${res}/${name}/ch_pm_2d.hdf"
    if [[ ! -f "${ch_map_file}" ]]; then
      ch_map_file="${LIVE_CH_HOME}/cr${crNum}-${res}/${name}/ch_pm_2d.hdf"
    fi
    if [[ ! -f "${ch_map_file}" ]]; then
      echo "${ch_map_file} not found ...skipped" >&2
      continue
    fi

    for sc in ${scList[@]}; do
      if [[ ${sc} = "earth" || ${sc} = "sdo" ]]; then
        scCode="omni_m"
      else
        scCode=${sc}
      fi

      if [[ ${crNum} -lt 2050 && ( ${sc} = "stereoa" || ${sc} = "stereob" ) ]]
      then
        continue
      fi

      scBalMapFileName=`ls ${dir}/sc_${scCode}.dat 2> /dev/null`
      if [[ ${scBalMapFileName} = "" ]]; then
        echo "${dir} - ${sc} ...skipped" >&2
        continue
      fi

      echo "ps_create_cr_sc_r0_bal_mapping_view, ${crNum}, '${res}', '${name}', '${sc}' , '${ch_map_file}', '${SC_BAL_MAP_HOME}', OUTPUT_FILENAME='${SC_BAL_MAP_HOME}/cr${crNum}-${res}/${name}/sc_${sc}.gif'"
    done
  done
done

exit 0
