#!/bin/bash
set -e
set -x


InDir=/ccc/work/cont003/gencmip6/bedidil/InterpolToy/DATA/GRIDS


ncks -4 -h -O -v icoh.lon,icoh.lat,icoh.clo,icoh.cla ${InDir}/grids.nc dynamico450_grid.nc
ncrename -h -v icoh.lon,lon        dynamico450_grid.nc
ncrename -h -v icoh.lat,lat        dynamico450_grid.nc
ncrename -h -v icoh.clo,bounds_lon dynamico450_grid.nc
ncrename -h -v icoh.cla,bounds_lat dynamico450_grid.nc

ncatted -h\
	-a history,global,d,c,c,""      \
	-a Comment,global,d,c,c,""      \
	-a source,global,d,c,c,""       \
	-a group,global,d,c,c,""        \
	-a production,global,d,c,c,""   \
	-a cr,global,d,c,c,""           \
	-a grid,global,d,c,c,""         \
	-a Institution,global,d,c,c,""  \
	-a Model,global,d,c,c,""        \
	-a HOSTNAME,global,d,c,c,""     \
	-a LOGNAME,global,d,c,c,""      \
	-a name,global,d,c,c,""         \
	-a title,global,d,c,c,""        \
	-a timeStamp,global,d,c,c,""    \
	-a uuid,global,d,c,c,""         \
	-a bounds,lon,c,c,"bounds_lon"  \
	-a bounds,lat,c,c,"bounds_lat"       \
	-a units,lon,c,c,"degrees_east"      \
	-a standard_name,lon,c,c,"Longitude" \
	-a long_name,lon,c,c,"Longitude"     \
	-a units,lat,c,c,"degrees_north"     \
	-a standard_name,lat,c,c,"Latitude"  \
	-a long_name,lat,c,c,"Latitude"      \
	dynamico450_grid.nc

ncwa -h -a y_icoh       dynamico450_grid.nc tmp_dynamico450_grid.nc ; mv tmp_dynamico450_grid.nc dynamico450_grid.nc
ncatted -h \
	-a cell_methods,lon,d,c,""         \
	-a cell_methods,lat,d,c,""         \
	-a cell_methods,bounds_lon,d,c,""  \
	-a cell_methods,bounds_lat,d,c,""  \
	dynamico450_grid.nc

ncrename -h -d x_icoh,cell     dynamico450_grid.nc
ncrename -h -d crn_icoh,nvertex dynamico450_grid.nc

ncpdq -h --permute cell,nvertex  dynamico450_grid.nc tmp_dynamico450_grid.nc ; mv tmp_dynamico450_grid.nc dynamico450_grid.nc


cat <<EOF > add_mask.nco
defdim("time_counter", 2) ;
defdim("axis_nbounds", 2) ;
time_centered        [time_counter]               = {  43200.0, 129600.0 } ;
time_centered_bounds [time_counter, axis_nbounds] = { 0.0, 86400.0,  86400.0, 172800.0 } ;
aire                 [cell]                       = ( 3.14159*6400000)^2/2016012.0  ;
OceMask              [time_counter,cell]          = 0.0d ;
EOF

ncap2 -h -S add_mask.nco dynamico450_grid.nc tmp_dynamico450_grid.nc ; mv tmp_dynamico450_grid.nc dynamico450_grid.nc

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 dynamico450_grid.nc tmp_dynamico450_grid.nc ; mv tmp_dynamico450_grid.nc dynamico450_grid.nc

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"    \
	dynamico450_grid.nc
