#!/bin/bash

rm -rf shifted_*.nc *interpT.nc full_rotate.nc output_rotate_uv.nc

fileu_filled='mean_vsum_NAA10KM_1m_20000101_20001231_grid_U.nc'
filev_filled='mean_vsum_NAA10KM_1m_20000101_20001231_grid_V.nc'

fileu_shifted='shifted_U.nc'
filev_shifted='shifted_V.nc'

fileUT='UinterpT.nc'
fileVT='VinterpT.nc'

file_full_rotate='full_rotate.nc'
file_uv='output_rotate_uv.nc'


NCORE=1

# gridU to gridT
time cdo -P  $NCORE shiftx ${fileu_filled} ${fileu_shifted}
time cdo -P  $NCORE ensmean ${fileu_filled} ${fileu_shifted} ${fileUT}

# gridV to gridT 
time cdo -P  $NCORE shifty ${filev_filled} ${filev_shifted}
time cdo -P  $NCORE ensmean ${filev_filled} ${filev_shifted} ${fileVT}

# extract angle components
if [ ! -f angles.nc ] ; then
  ncks -v gcost,gsint /home/a21468/PY/mesh_mask_nemo4.nc angles.nc
fi

# merge all variable into one file to operate
time cdo -P  $NCORE merge ${fileUT} ${fileVT} angles.nc $file_full_rotate

# rotate the velocity components 
time cdo -P  $NCORE -f nc4 -expr,'uocetr_effT=uocetr_eff*gcost-vocetr_eff*gsint;vocetr_effT=uocetr_eff*gsint+vocetr_eff*gcost'  full_rotate.nc output_rotate_uv.nc 
###$file_full_rotate $fileuv
