# The files created by this script are different from the files
# created by the original script of Josefine Ghattas: the order of
# latitudes and vertical level coordinates are different.

# Author: Lionel Guez

set -xe

for var in u v ta r
do
    ncks --overwrite --dimension=time,0,8 $var.201101.aphei.GLOBAL_075.nc \
	${var}_ERAI_packed.nc
    ncpdq --overwrite --unpack ${var}_ERAI_packed.nc ${var}_ERAI.nc
done

for grille in grille_u grille_v grille_s
do
    ncks --overwrite --variable=$grille grilles_gcm.nc $grille.nc
done

cdo remapbil,grille_u.nc -chname,u,UWND u_ERAI.nc u.nc
cdo remapbil,grille_v.nc -chname,v,VWND v_ERAI.nc v.nc
cdo remapbil,grille_s.nc -chname,ta,AIR ta_ERAI.nc T.nc
cdo remapbil,grille_s.nc -chname,r,RH r_ERAI.nc hur.nc

for var in u v T hur
do
    ncks --overwrite --dimension=time,0,4 $var.nc ${var}_day_1.nc
    ncks --overwrite --dimension=time,4,8 $var.nc ${var}_day_2.nc
done

rm @(u|v|T|hur).nc grille_[uvs].nc @(u|v|ta|r)_ERAI?(_packed).nc
