#!/bin/bash

set -exv

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 
                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 
                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 



