#!/bin/bash
#
# Purpose: Re-directs the output of all members Reichler & Kim indices
# into one file per variable
# Author: Martin Ménégoz
#         Updated by Francois Massonnet
#         Last update: November 2017


set -o nounset
set -o errexit


if [ $# -ne 4 ]
then
  echo "Usage:   ./ensemble.sh EXP YEARSTART YEAREND"
  echo "Example: ./ensemble.sh e00x fc0 1990 2000"
  exit 1
fi

exp=$1
year1=$2
year2=$3

. ./config.sh

# Printing scores into one file per variable (easier to read later by R)
for var in t2m msl qnet tp ewss nsss SST SSS SICE ; do
  echo $var
  cat ${MAINDIR}/EC-Earth_diags/${exp}/fc*/PI3_RK08_${exp}_${year1}_${year2}.txt | grep $var | awk {'print $2'} > ${exp}_${year1}_${year2}_${var}.txt
done

for var in T U V Q ; do
  echo $var
  cat ${MAINDIR}/EC-Earth_diags/${exp}/fc*/PI3_RK08_${exp}_${year1}_${year2}.txt | grep $var | grep zonal | awk {'print $2'} > ${exp}_${year1}_${year2}_${var}.txt
done
