#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This computes a climatology of restarts. 
#
# History : Virginie Guemas - Initial version                   - April 2014 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#                                Arguments
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Name of the experiment from which to compute the climatology
expin='i00v'  
# Name tag given to the output climatology
expout='chis'
# Start month : MMDD 
MM=1031
# First year used to compute climatology
year0=1981
# Last year used to compute climatology
yearf=2010 
# ocean or ice ?
typ='ice'
# configuration : ORCA1_LIM2, ORCA1_LIM3, ORCA025_LIM2, ORCA025_LIM3 for ice
#                 ORCA1, ORCA1L46, ORCA025L46, ORCA025L75 for ocean
config='ORCA1_LIM2'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case $typ in
  'ocean') suf='.nc' ;;
  'ice') suf='_ice.nc' ;;
esac

for ((jmemb=0;jmemb<=4;jmemb++)) ; do
  for ((year=$year0;year<=$yearf;year++)) ; do
    cp /cfu/releases/ic/$typ/${config}/${expin}/${expin}_fc${jmemb}_${year}${MM}_restart${suf}.gz .
    gunzip ${expin}_fc${jmemb}_${year}${MM}_restart${suf}.gz 
  done
  ncea  ${expin}_fc${jmemb}_*${MM}_restart${suf}  ${expout}_fc${jmemb}_${MM}_restart${suf}
  gzip ${expout}_fc${jmemb}_${MM}_restart${suf}
  mv ${expout}_fc${jmemb}_${MM}_restart${suf}.gz /cfu/releases/ic/$typ/${config}/${expout}/
  rm -f ${expin}_fc${jmemb}_*${MM}_restart${suf}
done
