#!/bin/csh -fx


#####################################################################
# Script for extracting fields from EC NR T511L91 grib data and creating
# binary data required for simulating OSSE data later.
# Due to the memory limits, each run will only create data for an half month
# (change set dd below for different data period)
# Programmer:  R. Yang
#############################
#  set variables
set yyyy=2005
set mm=07
echo 'yyyy, mm', $yyyy,$mm
set progsfcbase=/output/ECMWF/Nature/prog/Y${yyyy}/M${mm}/ecmwf_rd_oper_fc.prog.sfc_256.20050501_12z+
set diagsfcbase=/output/ECMWF/Nature/diag/Y${yyyy}/M${mm}/ecmwf_rd_oper_fc.diag.sfc_256.20050501_12z+
set progetabase=/output/ECMWF/Nature/prog/Y${yyyy}/M${mm}/ecmwf_rd_oper_fc.prog.eta_256.20050501_12z+

echo '$progsfcbase=', $progsfcbase
echo '$diagsfcbase=', $diagsfcbase
echo '$progetabase=', $progetabase
#############################
set dd=(01 02 03 04 05 06 07 08 09 10 11 12 13 14 15)
#set dd=(16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
set hh=(00 03 06 09 12 15 18 21)
 foreach x ($dd)
    foreach y ($hh)

set progsfcfl=${progsfcbase}${yyyy}${mm}${x}_${y}z.grb
set diagsfcfl=${diagsfcbase}${yyyy}${mm}${x}_${y}z.grb
set progetafl=${progetabase}${yyyy}${mm}${x}_${y}z.grb

echo '$progsfcfl=', $progsfcfl
echo '$diagsfcfl=', $diagsfcfl
echo '$progetafl=', $progetafl

#-------------------------------------------------
#  start to get gribex data and extract the data
#-------------------------------------------------
# (I) extract surface prognostic fields
#..............................................
rm -f progsfcdata
echo `ls -l $progsfcfl`
ln -s $progsfcfl progsfcdata
./agrsfchtps  -i progsfcdata

mv fort.50   pres_${yyyy}${mm}${x}${y}z.bin

rm -f progsfcdata


#-------------------------------------------------
# (II) extract surface diagnostic fields
#-------------------------------------------------
rm -f sfcdata
#  check whether the file exist
echo `ls -l $diagsfcfl`
ln -s $diagsfcfl sfcdata
./agrsfc -i sfcdata
mv fort.50 surf_${yyyy}${mm}${x}${y}z.bin
rm -f sfcdata
#-------------------------------------------------
# (III) extract 3d prognostic fields
#-------------------------------------------------
rm -f mldata
echo `ls -l $progetafl`
ln -s $progetafl mldata
./agrmlf -i mldata
mv fort.40 udat_${yyyy}${mm}${x}${y}z.bin
mv fort.41 vdat_${yyyy}${mm}${x}${y}z.bin
mv fort.42 tdat_${yyyy}${mm}${x}${y}z.bin
mv fort.43 qdat_${yyyy}${mm}${x}${y}z.bin
mv fort.44 odat_${yyyy}${mm}${x}${y}z.bin

rm -f mldata

    end
 end
#
exit 0
