#!/bin/bash
set -vx

# Script that prepares the PCMDI-AMIP-1-1-7 files to be read by ce0l in LMDZ. 
# These new files are stored in IGCM/ATM/LIMIT/AMIP.v20220201/original
# and can be used to produce limit.nc files on model grid.
# The input files from input4MIPs should be copied before launching this script. See comments in section 0).
#
# Use previous revision 5011 of this script for preparing version PCMDI-AMIP-1-1-6 files.
# Done by J. Ghattas, 2020/03/03

dirold=`pwd`/original/360x180.orig
dirnew=`pwd`/original/360x180.new

# 0) Copy original files from ciclad Input4MIPS and save in $dirold
#/bdd/input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-7/ocean/mon/tosbcs/gn/v20220201/tosbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-7_gn_187001-202106.nc
#/bdd/input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-7/seaIce/mon/siconcbcs/gn/v20220201/siconcbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-7_gn_187001-202106.nc

# 1) Split files to yearly files
cd $dirold
cdo splityear tosbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-7_gn_187001-202106.nc      amipbc_sst_360x180_
cdo splityear siconcbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-7_gn_187001-202106.nc   amipbc_sic_360x180_


# 2) Rename variable name for sea-ice concentration into sicbcs
#    and 4) The time axis is set fixed instead of unlimited
mkdir -p $dirnew

Date_Begin=1870
Date_End=2021
year=${Date_Begin}
while [ ${year} -le ${Date_End} ] ; do
       echo year = $year
      filesic=amipbc_sic_360x180_${year}.nc
      filesst=amipbc_sst_360x180_${year}.nc

      echo "Rename variable sicbcs in file $dirold/$filesic "
      ncrename -v siconcbcs,sicbcs $dirold/${filesic} $dirnew/tmpsic.nc 
      echo "Copy sst file to new folder $dirold/$filesst "
      cp $dirold/${filesst} $dirnew/tmpsst.nc

      # The time axis is set fixed instead of unlimited and the files are transformend into netcdf-4 format. 
      # This is done so that the files can be read by XIOS. 
      echo "Change time axis"
      ncks -4 --no_rec_dmn time $dirnew/tmpsic.nc -o $dirnew/${filesic}
      ncks -4 --no_rec_dmn time $dirnew/tmpsst.nc -o $dirnew/${filesst}

      let year=${year}+1
      echo ""
      rm -f $dirnew/tmpsic.nc $dirnew/tmpsst.nc
done



# 3) Produce climatologie files for different periods
#
#    Climatologie files for the years 1979-2008
cd $dirnew

ncea amipbc_sst_360x180_1979.nc amipbc_sst_360x180_198* amipbc_sst_360x180_199* amipbc_sst_360x180_200[012345678]* amipbc_sst_360x180_1979_2008_clim.nc
ncea amipbc_sic_360x180_1979.nc amipbc_sic_360x180_198* amipbc_sic_360x180_199* amipbc_sic_360x180_200[012345678]* amipbc_sic_360x180_1979_2008_clim.nc


#    Climatologie files for the years 1991-2020
ncea amipbc_sst_360x180_199[123456789]* amipbc_sst_360x180_200* amipbc_sst_360x180_201* amipbc_sst_360x180_2020.nc amipbc_sst_360x180_1991_2020_clim.nc
ncea amipbc_sic_360x180_199[123456789]* amipbc_sic_360x180_200* amipbc_sic_360x180_201* amipbc_sic_360x180_2020.nc amipbc_sic_360x180_1991_2020_clim.nc


#    Climatologie files for the years 1870-1899
ncea amipbc_sic_360x180_18* amipbc_sst_360x180_1870_1899_clim.nc
ncea amipbc_sic_360x180_18* amipbc_sic_360x180_1870_1899_clim.nc
