#!/bin/bash -e

set -o errexit
set -o nounset 

#########################################################
## Compute climatology for the period 1982-2016 (OSTIA)
## Resolution: 1 deg
##
## Ver1.0: Created by Mariko Koseki, 06/05/2025
#########################################################

#module load NCO/4.8.1-foss-2019a


CRTDIR=$(pwd)

MNTDIR='monthly'
CLMDIR='climatology'
GRD100='1deg'

cd ${MNTDIR}/${GRD100}

PWDDIR=$(pwd)
echo $PWDDIR


for month in {01..12}
do
    ## Compute the average 1982-2016 ##
    ncea {1982..2016}_${month}.nc SST_avg_${month}-1982-2016.nc

    ## Create a link file for ICE ##
    #ln -sf SST_avg_${month}-1982-2016.nc ICEC_avg_${month}-1982-2016.nc

    ## Update permisssions ##
    chmod 755 SST_avg_${month}-1982-2016.nc || true
    chgrp nn9039k SST_avg_${month}-1982-2016.nc || true

    ### Move NetCDF file to monthly directory ###
    mv  SST_avg_${month}-1982-2016.nc ${CRTDIR}/${CLMDIR}/${GRD100}/SST_avg_${month}-1982-2016.nc
    echo "done"
    echo "file has been moved to monthly folder"
    echo " "
done
echo "Completed!!"
