#!/bin/bash


### ===========================================================================
##
## Default configuration
## ===========================================================================

### ===========================================================================
###
### You should not change anything below this line ....
###
### ===========================================================================

##
## Command line parameters
## ===========================================================================
set -o verbose
set -o xtrace
set -e


while [[ ${1} = -* ]] ; do
    case ${1} in
	( -- ) shift ; break ;;
	( -v | --verbose    ) set -o verbose   ;;
	( -x | --xtrace     ) set -o xtrace    ;;
	( -e                ) set -e           ;;
	( -V | --noverbose  ) set +o verbose   ;;
	( -X | --noxtrace   ) set +o xtrace    ;;
	( -E                ) set +e           ;;
	( -* ) echo ${Bold}"Unknown option : ${1}"${Norm} ; return 1 ;;
    esac
    shift
done

##
AtmFile=${1:-/ccc/store/cont003/dsm/p25sepul/IGCM_OUT/IPSLCM5A2/PROD/piControl/CM5A2.1.pi.00/ATM/Output/MO/CM5A2.1.pi.00_40100101_40191231_1M_histmth.nc}

dim_lat=$(ncdump -h ${AtmFile} | grep "lat *=" | awk '{print $3}' )
dim_lon=$(ncdump -h ${AtmFile} | grep "lon *=" | awk '{print $3}' )
(( dim_lat = dim_lat - 1 ))

GridFile=lmdz${dim_lon}x${dim_lat}_grid.nc

ncks --overwrite -h -d time_counter,0,1 -v lon,lat,aire,fract_oce,fract_sic ${AtmFile}   ${GridFile}
#ncwa --overwrite -h -a time_counter                     ${GridFile}  tmp_${GridFile} ; mv tmp_${GridFile} ${GridFile}

ncatted -h -a coordinates,aire,m,c,"lat lon"       ${GridFile}
ncatted -h -a coordinates,fract_oce,m,c,"lat lon"  ${GridFile}
ncatted -h -a coordinates,fract_sic,m,c,"lat lon"  ${GridFile}

ncks --overwrite -h --xcl -v time_counter,time_centered,time_counter_bounds ${GridFile} tmp_${GridFile} ; mv tmp_${GridFile} ${GridFile}

cat <<EOF >add_fields.nco
time_centered        [time_counter]               = {  43200.0, 129600.0 } ;
time_centered_bounds [time_counter, axis_nbounds] = { 0.0, 86400.0,  86400.0, 172800.0 } ;
fract_oce_plus_sic = fract_oce + fract_sic ;
where (fract_oce_plus_sic <   0.00001 )  fract_oce_plus_sic=0.0 ;
where (fract_oce_plus_sic >   0.99999 )  fract_oce_plus_sic=1.0 ;
// Fill masked values to land values
OceMask[time_counter,lat,lon] = 0.0 ;
where (fract_oce_plus_sic >  0.0 )  OceMask=1.0 ;
EOF

ncap2 -h --overwrite --script-file add_fields.nco ${GridFile} tmp_${GridFile}  ; mv tmp_${GridFile} ${GridFile}

ncatted -h \
	-a coordinates,OceMask,c,c,"time_centered lat lon"             \
	-a standard_name,time_centered,c,c,"time"                      \
	-a long_name,time_centered,c,c,"Time axis"                     \
	-a calendar,time_centered,c,c,"360_day"                        \
	-a units,time_centered,c,c,"seconds since 2000-01-01 00:00:00" \
	-a time_origin,time_centered,c,c,"2000-01-01 00:00:00"         \
	-a bounds,time_centered,c,c,"time_centered_bounds"             \
	dynamico450_grid.nc

ncks -O --mk_rec time_counter ${GridFile} tmp_${GridFile}  ; mv tmp_${GridFile} ${GridFile}

ncatted -h \
	-a long_name,aire,c,c,"Grid area"    \
	-a units,aire,c,c,"m^2"              \
	-a online_operation,aire,c,c,"once"  \
	-a coordinates,aire,c,c,"lat lon"    \
	${GridFile}

### ===========================================================================
rm -f add_fields.nco


### ===========================================================================
###
###                               That's all folk's !!!
###
### ===========================================================================

