#!/bin/bash
#
#################################################
# batch settings for prep run on Vilje at NTNU
#################################################
#PBS -N prep_erai044
#PBS -l select=2:ncpus=32:mpiprocs=12
#PBS -l walltime=10:50:00
#PBS -o /work/bhuang/cclm-sp_2.4/chain/work/erai044/joblogs/prep/prep_erai044_2007_01.o.o
#PBS -e /work/bhuang/cclm-sp_2.4/chain/work/erai044/joblogs/prep/prep_erai044_2007_01.o.e
#PBS -m e
#PBS -M bo.huang@ntnu.no
#PBS -A ntnu919

set -e

echo START 2007 01  >> /work/bhuang/cclm-sp_2.4/chain/work/erai044/joblogs/prep/finish_joblist
#################################################
# transfer input from the archive
# and preprocess data if necessary
#################################################

#------------
if [ 1 -eq 1 ]
then
#------------
  # especially for the starter package test
  cp /work/bhuang/cclm-sp/data/gcm/eraint/2007_01/* /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01
  chmod u+w /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01/*

#... Check whether the short (yyyymmddhh) or the long (yyyymmddhhmmss) file name convention
#... is required for the INT2LM and CCLM I/O in the simulation.
#... This is only needed if the input files for INT2LM are in yyyymmddhh, 
#...      but the output is required in yyyymmddhhmmss
  HELPVAR=2007010100
  if [ ${#HELPVAR} -gt 10 ]
  then
    cd /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01
    FILELIST=$(ls -1)
    for FILE in ${FILELIST}
    do
         SHORTFILE=$(basename ${FILE} .nc)
         LONGFILE=${SHORTFILE}0000.nc
         mv ${FILE} ${LONGFILE}
    done
    cd /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044 
  fi

  #... check if all necessary input files exist
  if [ $(/work/bhuang/cclm-sp_2.4/src/cfu/bin/cfu check_files 2007010100 2007020100 06:00:00 cas caf .nc /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01 T 0) -eq 1 ]
  then
    echo Error in prep  > /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Date 2007 / 01 >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Error during preprocessing >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Not all input files found >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    cat check_files.log >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
  if [ -n "bo.huang@ntnu.no" ]
    then
    mailx -s "SUBCHAIN erai044 abort due to errors" bo.huang@ntnu.no < /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    fi
      rm check_files.log
    exit
  fi

  rm -rf /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/tmp/erai0442007_01

  #... submit INT2LM job
  cd /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044 ; subchain int2lm 2007010100

#------------
else
#------------

#... the following line is just an example. Adjust this line for copying your int2lm input data.
  tar -C /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01 -xf /work/bhuang/cclm-sp/data/gcm/eraint/year2007/laf200701.ERAINT.tar

  #... check if all necessary input files exist
 if [[ 2007010100 -eq 2007010100 && ! -f /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01/laf2007010100.nc ]]
 then
    echo Error in prep  > /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Date 2007 / 01 >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Error during preprocessing >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01/laf2007010100.nc not found >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    if [ -n "bo.huang@ntnu.no" ]
    then
      mailx -s "SUBCHAIN erai044 abort due to errors" bo.huang@ntnu.no < /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    fi
    exit
 fi

 if [ $(/work/bhuang/cclm-sp_2.4/src/cfu/bin/cfu check_files 2007010100 2007020100 06:00:00 lbfd laf .nc /work/bhuang/cclm-sp_2.4/chain/scratch/erai044/output/prep/2007_01 T 0) -eq 1 ]
  then
    echo Error in prep  > /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Date 2007 / 01 >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Error during preprocessing >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    echo Not all input files found >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    cat check_files.log >> /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    if [ -n "bo.huang@ntnu.no" ]
    then
      mailx -s "SUBCHAIN erai044 abort due to errors" bo.huang@ntnu.no < /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/error_message
    fi
      rm check_files.log
    exit
  fi

  ### submit a CCLM job if
  ### it is the first PREP job in the chain OR
  ### the in parallel runnig CCLM job was faster than the PREP job
  DATELOG=$(cat /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044/date.log | cut -c1-10)
  if [[ 2007010100 -eq 2007010100 || 2007010100 -eq ${DATELOG} ]]
  then
    #################################################
    # submit CCLM job
    #################################################
    cd /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/erai044 ; subchain cclm
  fi

#------------
fi
#------------

echo "END  " 2007 01 >> /work/bhuang/cclm-sp_2.4/chain/work/erai044/joblogs/prep/finish_joblist

