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


InFile=/ccc/work/cont003/igcmg/igcmg/IGCM/OCE/NEMO/ORCA025_LIM3_PISCES/v3.6_stable/mesh_mask_eORCA025.nc


ncks -4 -h -O -v glamt,glamu,glamv,glamf,gphit,gphiu,gphiv,gphif,tmask,umask,vmask,fmask,tmaskutil,umaskutil,vmaskutil,fmaskutil,e1t,e2t,e1u,e1f,e2u,e1v,e2v,e2f,e2f ${InFile} ORCA025_coordinates_mask.nc
ncrename -h -v glamt,nav_lon_grid_T -v gphit,nav_lat_grid_T ORCA025_coordinates_mask.nc
ncrename -h -v glamu,nav_lon_grid_U -v gphiu,nav_lat_grid_U ORCA025_coordinates_mask.nc
ncrename -h -v glamv,nav_lon_grid_V -v gphiv,nav_lat_grid_V ORCA025_coordinates_mask.nc
ncrename -h -v glamf,nav_lon_grid_F -v gphif,nav_lat_grid_F ORCA025_coordinates_mask.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 units,nav_lon_grid_T,c,c,"degrees_east"      \
	-a standard_name,nav_lon_grid_T,c,c,"Longitude" \
	-a long_name,nav_lon_grid_T,c,c,"Longitude"     \
	-a units,nav_lat_grid_T,c,c,"degrees_north"     \
	-a standard_name,nav_lat_grid_T,c,c,"Latitude"  \
	-a long_name,nav_lat_grid_T,c,c,"Latitude"      \
	-a units,nav_lon_grid_U,c,c,"degrees_east"      \
	-a standard_name,nav_lon_grid_U,c,c,"Longitude" \
	-a long_name,nav_lon_grid_U,c,c,"Longitude"     \
	-a units,nav_lat_grid_U,c,c,"degrees_north"     \
	-a standard_name,nav_lat_grid_U,c,c,"Latitude"  \
	-a long_name,nav_lat_grid_U,c,c,"Latitude"      \
	-a units,nav_lon_grid_V,c,c,"degrees_east"      \
	-a standard_name,nav_lon_grid_V,c,c,"Longitude" \
	-a long_name,nav_lon_grid_V,c,c,"Longitude"     \
	-a units,nav_lat_grid_V,c,c,"degrees_north"     \
	-a standard_name,nav_lat_grid_V,c,c,"Latitude"  \
	-a long_name,nav_lat_grid_V,c,c,"Latitude"      \
	-a units,nav_lon_grid_F,c,c,"degrees_east"      \
	-a standard_name,nav_lon_grid_F,c,c,"Longitude" \
	-a long_name,nav_lon_grid_F,c,c,"Longitude"     \
	-a units,nav_lat_grid_F,c,c,"degrees_north"     \
	-a standard_name,nav_lat_grid_F,c,c,"Latitude"  \
	-a long_name,nav_lat_grid_F,c,c,"Latitude"      \
	ORCA025_coordinates_mask.nc

ncwa -O -h -a t ORCA025_coordinates_mask.nc tmp_ORCA025_coordinates_mask.nc ; mv tmp_ORCA025_coordinates_mask.nc ORCA025_coordinates_mask.nc

cat <<EOF > add_mask.nco
area_grid_T [y,x] = e1t (:,:) * e2t (:,:)    ;
area_grid_U [y,x] = e1u (:,:) * e2u (:,:)    ;
area_grid_V [y,x] = e1v (:,:) * e2v (:,:)    ;
area_grid_F [y,x] = e1f (:,:) * e2f (:,:)    ;
mask_T      [y,x] = tmask     (0,:,:) * 1.0d ;
mask_U      [y,x] = umask     (0,:,:) * 1.0d ;
mask_V      [y,x] = vmask     (0,:,:) * 1.0d ;
mask_F      [y,x] = fmask     (0,:,:) * 1.0d ;
maskutil_T  [y,x] = tmaskutil   (:,:) * 1.0d ;
maskutil_U  [y,x] = umaskutil   (:,:) * 1.0d ;
maskutil_V  [y,x] = fmaskutil   (:,:) * 1.0d ;
maskutil_F  [y,x] = fmaskutil   (:,:) * 1.0d ;
//maskutil_T  [:,    0] = 0.0d ;
//maskutil_T  [:, 1441] = 0.0d ;
//maskutil_U  [:,    0] = 0.0d ;
//maskutil_U  [:, 1441] = 0.0d ;
//maskutil_V  [:,    0] = 0.0d ;
//maskutil_V  [:, 1441] = 0.0d ;
//maskutil_F  [:, 0   ] = 0.0d ;
//maskutil_F  [:, 1441] = 0.0d ;
EOF

ncap2 -h -S add_mask.nco ORCA025_coordinates_mask.nc tmp_ORCA025_coordinates_mask.nc ; mv tmp_ORCA025_coordinates_mask.nc ORCA025_coordinates_mask.nc

ncks -h -x --variable e1t,e2t,e1u,e2u,e1v,e2v,e1f,e2f,tmask,umask,vmask,fmask,tmaskutil,umaskutil,vmaskutil,fmaskutil ORCA025_coordinates_mask.nc tmp_ORCA025_coordinates_mask.nc ; 

#python creates_nemo_bounds.py
#mv tmp_ORCA025_coordinates_mask.nc ORCA025_coordinates_mask.nc

ncatted -h \
	-a coordinates,area_grid_T,c,c,"nav_lat_grid_T nav_lon_grid_T" \
	-a coordinates,area_grid_U,c,c,"nav_lat_grid_U nav_lon_grid_U" \
	-a coordinates,area_grid_V,c,c,"nav_lat_grid_V nav_lon_grid_V" \
	-a coordinates,area_grid_F,c,c,"nav_lat_grid_F nav_lon_grid_F" \
	-a units,area_grid_T,c,c,"m^2"                                 \
	-a units,area_grid_U,c,c,"m^2"                                 \
	-a units,area_grid_V,c,c,"m^2"                                 \
	-a units,area_grid_F,c,c,"m^2"                                 \
	-a coordinates,mask_T,c,c,"nav_lat_grid_T nav_lon_grid_T"      \
	-a coordinates,mask_U,c,c,"nav_lat_grid_U nav_lon_grid_U"      \
	-a coordinates,mask_V,c,c,"nav_lat_grid_V nav_lon_grid_V"      \
	-a coordinates,mask_F,c,c,"nav_lat_grid_F nav_lon_grid_F"      \
	-a coordinates,maskutil_T,c,c,"nav_lat_grid_T nav_lon_grid_T"  \
	-a coordinates,maskutil_U,c,c,"nav_lat_grid_U nav_lon_grid_U"  \
	-a coordinates,maskutil_V,c,c,"nav_lat_grid_V nav_lon_grid_V"  \
	-a coordinates,maskutil_F,c,c,"nav_lat_grid_F nav_lon_grid_F"  \
	-a cell_measures,mask_T,c,c,"area: area_grid_T"                \
	-a cell_measures,mask_U,c,c,"area: area_grid_U"                \
	-a cell_measures,mask_V,c,c,"area: area_grid_V"                \
	-a cell_measures,mask_F,c,c,"area: area_grid_F"                \
	-a cell_measures,maskutil_T,c,c,"area: area_grid_T"            \
	-a cell_measures,maskutil_U,c,c,"area: area_grid_U"            \
	-a cell_measures,maskutil_V,c,c,"area: area_grid_V"            \
	-a cell_measures,maskutil_F,c,c,"area: area_grid_F"            \
	-a bounds,nav_lon_grid_T,c,c,"bounds_lon_grid_T"               \
	-a bounds,nav_lat_grid_T,c,c,"bounds_lat_grid_T"               \
	-a bounds,nav_lon_grid_U,c,c,"bounds_lon_grid_U"               \
	-a bounds,nav_lat_grid_U,c,c,"bounds_lat_grid_U"               \
	-a bounds,nav_lon_grid_V,c,c,"bounds_lon_grid_V"               \
	-a bounds,nav_lat_grid_V,c,c,"bounds_lat_grid_V"               \
	-a bounds,nav_lon_grid_F,c,c,"bounds_lon_grid_F"               \
	-a bounds,nav_lat_grid_F,c,c,"bounds_lat_grid_F"               \
	ORCA025_coordinates_mask.nc

