#!/bin/bash

set -xuve

#griddir=/gpfs/scratch/bsc32/bsc32051/pub/scripts/convert-tm5-grids
griddir=.

newdate=$1
#taxis="settaxis,21450101,00:00:00,1day"
taxis="settaxis,$newdate,00:00:00,1day"

gridname_ifs=L128
griddef_ifs=ICMGGa22e+000000 # copy from /gpfs/scratch/bsc32/bsc32051/pub/scripts/convert-tm5-grids
gridname_AMIP=AMIP
griddef_amip=grid-amip.txt # cdo griddes tosbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-3_gn_187001-201706.nc > grid-amip.txt
gridname_co2=CO2
griddef_ceds=grid-CEDS.txt # cdo griddes  > grid-CEDS.txt
griddef_carbon_tracker=grid-REG_1X1.txt # cdo griddes  > grid-REG_1X1.txt
model_ifs=IFS_TOY

# set vars according to model_mode
# TODO: They will be replaced by python YAML reader 
CO2FLUX=false
[ $model_mode = "co2box" ] && CO2BOX=true || CO2BOX=false
[ $model_mode = "co2flux_ceds" ] && { CO2FLUX_CEDS=true; CO2FLUX=true; } || CO2FLUX_CEDS=false
[ $model_mode = "co2flux_carbon_tracker" ] && { CO2FLUX_CARBON_TRACKER=true; CO2FLUX=true; }|| CO2FLUX_CARBON_TRACKER=false

if [[ ${CO2BOX} == true ]] ; then
    grids=""
    vars_amip=""
    vars_ifs=""
    model_amip=CO2BOX
    model_co2=CO2BOX
    vars_co2=""
    vars_co2_co2box="CO2BOX_CO2_cconc CO2BOX_CO2_econc"
    vars_co2_ifs="A_CO2_cconc A_CO2_econc"
    
# elif [[ ${CO2FLUX_CARBON_TRACKER} == true ]] ; then
#    grids="AMIP L128" #B128 A128 R128
#     vars_amip="AMIP_sst AMIP_sic"
#     vars_ifs="A_SST A_Ice_frac"
#     [[ ${CO2FLUX_CARBON_TRACKER} == true ]] && vars_ifs+=" A_CO2_veg A_CO2_fire A_CO2_oce"
#     model_amip=AMIPFORC
#     model_co2=AMIPFORC
#     [[ ${CO2FLUX_CARBON_TRACKER} == true ]] && vars_co2="AMIP_CO2_emis" || vars_co2=""
#     vars_co2_co2box=""
#     vars_co2_ifs=""

# elif [[ ${CO2FLUX_CARBON_TRACKER} == true ]] ; then
#     grids=" REG_1X1"
#     vars_amip=""
#     vars_ifs=" A_CO2_veg A_CO2_fire A_CO2_oce"
#     model_amip=AMIPFORC
#     model_co2=AMIPFORC
#     vars_co2="AMIP_CO2_emis"
#     vars_co2_co2box=""
#     vars_co2_ifs=""
else
    grids="AMIP L128" #B128 A128 R128
    [[ ${CO2FLUX_CEDS} == true ]] && grids+=" CEDS"
    [[ ${CO2FLUX_CARBON_TRACKER} == true ]] && grids+=" REG_1X1"
    vars_amip="AMIP_sst AMIP_sic"
    vars_ifs="A_SST A_Ice_frac"
    [[ ${CO2FLUX} == true ]] && vars_ifs+=" A_CO2_veg A_CO2_fire A_CO2_oce"
    model_amip=AMIPFORC
    model_co2=AMIPFORC
    [[ ${CO2FLUX} == true ]] && vars_co2="AMIP_CO2_emis" || vars_co2=""
    vars_co2_co2box=""
    vars_co2_ifs=""
fi

# copy from runtime dir the following files grids.nc areas.nc masks.nc
for gridname in ${grids}
do
    if [[ ${gridname} == AMIP ]] ; then
	format=nc
	suffix=".nc"
	griddef=${griddef_amip}
    elif [[ ${gridname} == CEDS ]] ; then
	format=nc
	suffix=".nc"
	griddef=${griddef_ceds}
    elif [[ ${gridname} == REG_1X1 ]] ; then
	format=nc
	suffix=".nc"
	griddef=${griddef_carbon_tracker}
    else
	format=grb1
	suffix=".grb"
	griddef=${griddef_ifs}
    fi
    cdo -f $format -setgrid,${griddir}/${griddef} -selvar,${gridname}.msk masks.nc masks_${gridname}${suffix}
    cdo -f $format -setgrid,${griddir}/${griddef} -selvar,${gridname}.srf areas.nc areas_${gridname}${suffix}
    if [[ ${format} == "grb1" ]] ; then
        for t in masks areas ; do
            cdo -f nc -setgridtype,regularnn ${t}_${gridname}.grb ${t}_${gridname}.nc
        done
    fi
done


for v in $vars_ifs
do
    cdo -O -f grb1 -r $taxis -setgrid,${griddir}/${griddef_ifs} ${v}_${model_ifs}_??.nc ${v}.grb
    cdo -O -f nc   -r $taxis -setgridtype,regular -chvar,var1,${v} ${v}.grb ${v}.nc
done

for v in $vars_amip
do
    HDF5_DISABLE_VERSION_CHECK=1 cdo -O -f nc -r $taxis -setgrid,${griddir}/${griddef_amip} ${v}_${model_amip}_??.nc ${v}.nc
done

for v in $vars_co2
do
    HDF5_DISABLE_VERSION_CHECK=1 cdo -O -f nc -r $taxis -setgrid,${griddir}/${griddef_ceds} ${v}_${model_co2}_??.nc ${v}.nc
done

for v in $vars_co2_co2box
do
    HDF5_DISABLE_VERSION_CHECK=1 cdo -O -f nc -r $taxis ${v}_${model_co2}_??.nc ${v}.nc
done
for v in $vars_co2_ifs
do
    HDF5_DISABLE_VERSION_CHECK=1 cdo -O -f nc -r $taxis ${v}_${model_ifs}_??.nc ${v}.nc
done

