#!/bin/bash
#

set -xuve
date

grid=ORCA1L46
expid=m01x
src=/cfu/releases/ic/ocean/$grid/$expid
dst=/cfu/releases/ic/ocean/$grid/$expid.new
ref=/cfunas/exp/MERCATOR/GLORYS2V1/restarts/ocean/$grid/restart_glorys2v1_oce_19930504_$grid.nc
tmpdir=/scratch/masif
mkdir -p $dst

list=$PWD/$expid

if [[ ! -a ${list} ]]; then
 ls -1 $src | cut -d '.' -f 1 | sort | uniq >> $list
fi

tmp=$tmpdir/prepare_oce_ic_$expid
rm -rf $tmp
mkdir -p $tmp
cd $tmp

for l in $(cat $list); do
 cp -p $src/$l.nc.gz .
 gunzip $l.nc.gz
 ncwa -O -a t $l.nc $l.nc # removing t
 ncks -A -v z $ref $l.nc # appending z from ref.
 gzip -9 -f $l.nc
 mv $l.nc.gz $dst
 cat ${list} | sed -e '1d' > ${list}.tmp
 mv ${list}.tmp ${list}
done
rm ${list}
cd; rm -rf $tmp

date
