#!/bin/ksh
#
#@ shell            = /usr/bin/ksh
#@ class            = ns
#@ job_type         = serial
#@ job_name         = prepare_atm_perturb
#@ output           = $(job_name).$(jobid).log
#@ error            = $(job_name).$(jobid).log
#@ notification     = error
#@ resources        = ConsumableCpus(1) ConsumableMemory(1200mb)
#@ wall_clock_limit = 24:00:00
#@ queue
#

set -xuve
date
tar="/opt/freeware/bin/tar"

grid=T255L91
expid=e00l
src=ec:/c3l/errfiles/$expid
dst=ec:/c3m/perturb/atmos/$grid/$expid

cup=$(pwd)
list=$cup/$expid

# Gather the list of original perturb files
if [[ ! -a ${list} ]]; then
 emkdir -p $dst
 els -1 $src/* | cut -d '_' -f 4 | cut -d '.' -f 1 | sort | uniq >> $list
fi

tmp=$TMPDIR/prepare_atm_perturb_$expid
rm -rf $tmp
mkdir -p $tmp
cd $tmp

# Prepare and store the perturb file after reformatting properly
for l in $(cat $list); do
 ecp $src/??_inierr_${expid}_${l}.grb .
 $tar -cvzf ${expid}_${l}.tar.gz ??_inierr_${expid}_${l}.grb
 emv ${expid}_${l}.tar.gz $dst
 rm -f ??_inierr_${expid}_${l}.grb
 cat ${list} | sed -e '1d' > ${list}.tmp
 mv ${list}.tmp ${list}
done
rm ${list}
cd; rm -rf $tmp

date

