#!/bin/bash
#SBATCH -n 1
#SBATCH -t 10-12:00:00
#SBATCH -J copyORAP5_ele
#SBATCH --mem=60000
#SBATCH -o copy_orap5-%j.log
 

set -evx
TMPDIR=/scratch/$(whoami)/$$
mkdir $TMPDIR
cd $TMPDIR

dir=/esnas/reconstructions/ecmwf/orap5/restarts/ 
outdir=/esnas/releases/ic/ocean/ORCA025L75/ORAP5
curdir=`pwd`

for year in {2004..2004} ; do 
# May start dates 
	echo $year 
	dirin=${dir}/${year} 
	if [ -e $dirin  ] ; then   
		cd $dirin 
		## need to manually check for the file with the closest date to 0430,
		## WITH THE CORRECT SIZE (some files are half sized and hence not suitable)
                for file in $( ls   g0tx_${year}0428* ) ; do 
			inname=$(echo $file | cut -f1 -d"." )
			ofile=ORAP5_fc0_${year}0430_restart.nc.gz
			if [ ! -e  ${outdir}/${ofile}  ]  ; then   
			cd $curdir 
			gunzip -c   ${dirin}/$file  >  ${inname}.tar  
			tar xfv ${inname}.tar  
			gzip -c ${inname}.nc >  ${outdir}/${ofile} 
			rm -rf  ${curdir}/g0tx*tar  ${curdir}/g0t*nc 
		fi 
	done 
	fi 



# November start dates 
	echo $year 
	dirin=${dir}/${year} 
	if [ -e $dirin  ] ; then   
		cd $dirin 
		## need to manually check for the file with the closest date to 1031,
		## WITH THE CORRECT SIZE (some files are half sized and hence not suitable)
                for file in $( ls   g0tx_${year}1030* ) ; do 
			inname=$(echo $file | cut -f1 -d"." )
			ofile=ORAP5_fc0_${year}1031_restart.nc.gz
			if [ ! -e  ${outdir}/${ofile}  ]  ; then   
			cd $curdir 
			gunzip -c   ${dirin}/$file  >  ${inname}.tar  
			tar xfv ${inname}.tar  
			gzip -c ${inname}.nc >  ${outdir}/${ofile} 
			rm -rf  ${curdir}/g0tx*tar  ${curdir}/g0t*nc 
		fi 
	done 
	fi 
done 




rm -rf $TMPDIR
