#!/bin/bash
#
# This script do the post-processing of the 15 month
# 3D-AMIP like ARPEGE simulation
# preparing the nc files to evaluate the metrics
#
# It just take the 12 last months of the simulation
# and store it into ${ppeddir}/${simu}/${var}.nc
# 
#
#################################################################

if [ $# != 4 ] ; then
bash postpro_fhstyle.sh --help 
exit 1
fi

while (( $# > 0 )) ; do
        case $1 in
          -origdir)   origdir=$2; shift ; shift ;;
          -ppeddir)   ppeddir=$2; shift ; shift ;;
          -h|-help|--help) echo Usage: $0 "[-origdir origdir] [-ppedir ppedir]" ; exit 1 ;;
        esac
done

freqm=mon 

function selANN {
  # This function select the usefull months (after the 3 months spin-up)
  fin=$1
  simu=$2
  var=$3
  cdo seltimestep,4/15 $fin ${ppeddir}/${simu}/${var}.nc
}

for simu in $(ls $origdir) ; do
    echo $simu
    mkdir -p postprocessed/${simu}
    ### We start by evaluate annual mean of a list of variables
    for var in rsdt rsut rsutcs rlut rlutcs rlds rsds rlus rsus hfls hfss pr clt ta hus ua cltrad ts tas gn_fnet_sea gn_sftsf hur hurs huss cl uas va vas wap psl ; do
      fin=$(ls ${origdir}/${simu}/*${freqm}*_${var}_*.nc)
      selANN $fin $simu $var > /dev/null 2>&1 
    done
done

