#! /usr/bin/tcsh -f
### FOR DEBUGGING
# set echo verbose
### GLOBAL SETTINGS ###################################
#set MODELDATADIR = /afs/ipp/mpc/EVAL/messy
set MODELDATADIR = /modeldata/ECHAM5/EVAL/messy
#######################################################
if ("$1" == "") then
echo "Usage: `basename $0` [-t] [-l] [-n <DIR>] [-g <DIR>] -T <RES>"
echo " "
echo ' -t : TEST MODE (i.e., START "mprenc" IN TEST MODE)'
echo " -l : LOCAL (OUTPUT DIRECTORY STRUCTURE IN .)"
echo " -n <DIR> : NAMELIST DIRECTORY"
echo " (default: ${MODELDATADIR}/nml)"
echo " -g <DIR> : LOG-FILE DIRECTORY"
echo ' (default: '${MODELDATADIR}'/log/`date +"%Y%m%d%H%M%S"`'
echo " -T <RES> : SPECTRAL HORIZONTAL RESOLUTION (T21, T42, ...)"
echo " "
echo "Example: `basename $0` -T T21 -n ./nml"
echo " "
echo ' * creates log-directory (log/`date +"%Y%m%d%H%M%S"`) in'
echo ' '$MODELDATADIR' OR . (if "-l" is specified)'
echo ' * updates/creates directory T21 in '
echo ' '$MODELDATADIR' OR . (if "-l" is specified)'
echo ' with subdirectory structure,'
echo ' including links for "misc"-directories'
echo ' * loops over all *.nc files in '
echo ' '$MODELDATADIR'/raw'
echo ' and '
echo ' - looks for a matching namelist-file in ./nml'
echo ' - extracts the matching namelist from the namelist-file'
echo ' - starts "mprenc" with this namelist'
echo " "
exit 1
endif
### OPEN u+rw(x), g+rw(x), o+r(x)
#umask 002
### DEFAULT SETTINGS
set testmode = 0
set localmode = 0
set TMPNML = tmp.nml
set NMLDIR = ${MODELDATADIR}/nml
set LOGDEF = log/`date +"%Y%m%d%H%M%S"`
### PARSE COMMAND LINE
while ("$1" != "")
switch ("$1")
case "-t":
set testmode = 1
breaksw
case "-l":
set localmode = 1
breaksw
case "-T":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-T" option missing\!'
exit 1
else
set HRES = $1
endif
breaksw
case "-n":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-n" option missing\!'
exit 1
else
set NMLDIR = $1
endif
breaksw
case "-g":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-g" option missing\!'
exit 1
else
set LOGDEF = $1
endif
breaksw
default:
echo 'ERROR: Unrecognized option: '$1
exit 1
endsw
shift
end
### TESTS
if (! ${?HRES}) then
echo 'ERROR: -T option must be specified\!'
exit 1
endif
### SOFTWARE SETUP
### MPRENC
set MPRENC_WHERE=(`where mprenc`)
if ( ${#MPRENC_WHERE} == 0) then
echo 'ERROR: mprenc not found!'
exit 1
else
set MPRENC=$MPRENC_WHERE[1]
endif
### GAWK
set GAWK_WHERE=(`where gawk`)
if ( ${#GAWK_WHERE} == 0) then
echo 'ERROR: gawk not found!'
exit 1
else
set GAWK=$GAWK_WHERE[1]
endif
### SETTING UP DIRECTORY STRUCTURE
set RAWMODELDATADIR = ${MODELDATADIR}/raw
if ($localmode == 0) then
set NEWMODELDATADIR = ${MODELDATADIR}/${HRES}
set LOGDIR = ${MODELDATADIR}/$LOGDEF
else
set NEWMODELDATADIR = ${HRES}
set LOGDIR = $LOGDEF
endif
### DIAGNOSTIC OUTPUT
echo "=================================================================="
echo "STARTING $0 ... at "`date`
echo " ................................................................."
echo " SOFTWARE SETUP:"
echo " MPRENC = "$MPRENC
echo " GAWK = "$GAWK
echo " INPUT SETUP:"
echo " NMLDIR = "$NMLDIR
echo " RAWMODELDATADIR = "$RAWMODELDATADIR
if ($localmode == 0) then
echo " OUTPUT SETUP:"
else
echo " OUTPUT SETUP (LOCAL):"
endif
echo " HRES = "$HRES
echo " LOGDIR = "$LOGDIR
echo " NEWMODELDATADIR = "$NEWMODELDATADIR
if ($testmode == 0) then
echo " MODE = PROCESS"
else
echo " MODE = TEST"
endif
echo " ................................................................."
### CHECK USER INPUT FOR CONSISTENCY
echo "CHECKING USER INPUT ..."
### CHECK NMLDIR
echo -n " ... checking for directory "$NMLDIR
if (-d $NMLDIR) then
echo " ... exists"
else
echo " ... ERROR: $NMLDIR does not exist!"
exit 1
endif
### CHECK LOGDIR
if ($localmode == 1) then
echo -n " ... checking for directory log"
if (-d log) then
echo " ... exists"
else
echo " ... new"
mkdir log
endif
endif
echo -n " ... checking for directory "$LOGDIR
if (-d $LOGDIR) then
echo " ... ERROR: $LOGDIR exists already!"
exit 1
else
echo " ... new "
mkdir $LOGDIR
endif
echo " ................................................................."
### CHECK SUBDIRECTORY FOR HORIZONTAL RESOLUTION
echo "CHECKING OUTPUT DIRECTORY STRUCTURE ..."
echo -n " ... "$NEWMODELDATADIR
if -d ${NEWMODELDATADIR} then
echo " ... exists"
else
if ($testmode == 0) then
echo " ... new"
mkdir ${NEWMODELDATADIR}
else
echo " ... <new>"
endif
endif
### MAKE SUBDIRECTORY-STRUCTURE FOR HORIZONTAL RESOLUTION
set WORKDIR = `pwd`
cd ${RAWMODELDATADIR}
set dirlist = (`find . -type d -print | sed 's|\./||g'`)
cd ${WORKDIR}
#
set ndir = ${#dirlist}
@ count = 2 ### SKIP '.'
while ($count <= $ndir)
set curdir = $dirlist[$count]
echo -n " ... "${NEWMODELDATADIR}/$curdir
### CREATE LINKS FOR misc DIRECTORIES
if (`basename $curdir` == 'misc') then
if (-l ${NEWMODELDATADIR}/$curdir) then
echo " ... link exists"
else
if ($testmode == 0) then
echo " ... new link to ../../raw/${curdir}"
ln -s ../../raw/${curdir} ${NEWMODELDATADIR}/${curdir}
else
echo " ... <new link to ../../raw/${curdir}>"
endif
endif
else
if (-d ${NEWMODELDATADIR}/$curdir) then
echo " ... exists"
else
if ($testmode == 0) then
echo " ... new"
mkdir ${NEWMODELDATADIR}/${curdir}
else
echo " ... <new>"
endif
endif
endif
@ count++
end
echo " ................................................................."
### GET LIST OF ALL netCDF-FILES
echo "CHECKING PRE-REGRIDDED FILES ..."
#
### GET LIST OF NAMELISTS
set WORKDIR = `pwd`
cd ${NMLDIR}
set nmlflist = (`find . -name \*.nml -print | sed 's|\./||g'`)
set nnml = ${#nmlflist}
cd ${WORKDIR}
#
### GET LIST OF netCDF FILES
set WORKDIR = `pwd`
cd ${RAWMODELDATADIR}
set ncflist = (`find . -name \*.nc -print | sed 's|\./||g'`)
set nnc = ${#ncflist}
cd ${WORKDIR}
#
### LOOP OVER ALL netCDF FILES
@ count_nc = 1
while ($count_nc <= $nnc)
set NCFILE = $ncflist[$count_nc]
set INFILE = ${RAWMODELDATADIR}/$NCFILE
set OUTFILE = ${NEWMODELDATADIR}/$NCFILE
### CHECK IF OUTPUT FILE EXISTS ALREADY
echo -n " ... "$OUTFILE
if ( -e $OUTFILE ) then
echo " ... exists"
else
### TRY TO CREATE NEW FILE / LINK
if (-l $INFILE) then
### INPUT FILE IS LINK
set linktarget = `\ls -l $INFILE | awk -F ">" '{print $2}'`
if ($testmode == 0) then
echo -n " ... new link to "$linktarget
ln -fs $linktarget $OUTFILE
### CHECK SUCCESS
if ( -e $OUTFILE ) then
echo " ... success"
else
echo " ... failed (target not present)"
endif
else
echo -n " ... <new link to $linktarget>"
### CHECK SUCCESS
if ( -e $OUTFILE ) then
echo " ... <success>"
else
echo " ... <failed>"
endif
endif
### END (INPUT FILE IS LINK)
else
### INPUT FILE IS FILE
if ($testmode == 0) then
echo -n " ... new"
else
echo -n " ... <new>"
endif
### LOOP OVER ALL NAMELISTS
set success = 0
@ count_nml = 1
while ($count_nml <= $nnml)
set curnml = $nmlflist[$count_nml]
set found = `sed 's|\\!.*||g' $NMLDIR/$curnml | grep -Ev '^ *$' | grep $NCFILE`
if ( "$found" != "" ) then
echo -n ' ... trying '$curnml
### EXTRACT NAMELIST
@ fcnt = 1
set fok = 0
while ( $fok == 0 )
echo -n " "$fcnt
cat $NMLDIR/$curnml |\
$GAWK 'BEGIN { f = 0 ; g = 0} \
{ if (match(tolower($1),"®rid") != 0) {f = f + 1} }\
{ if ( ($1 == "/") && (f == '$fcnt') ) {print ; g=1} }\
{ if ( (f == '$fcnt') && (g == 0) ) print } \
END { printf"\n" } ' >&! $TMPNML
set f2 = `sed 's|\\!.*||g' $TMPNML | grep -Ev '^ *$' | grep $NCFILE`
if ("$f2" != "") then
set fok = 1
else
rm -f $TMPNML
@ fcnt++
endif
end
set thisnml = $LOGDIR/`basename $NCFILE | sed 's|.nc|.nml|g'`
mv -f $TMPNML $thisnml
### SET LOG-FILE
set logfile = ${thisnml}.log
### START EPRENC
if ($testmode == 0) then
# START PRE-REGRIDDING
mprenc -i ${RAWMODELDATADIR} -v INPUTDIR_MESSY -T ${HRES} \
-n $thisnml -o ${NEWMODELDATADIR} >&! ${logfile}
else
# START PRE-REGRIDDING IN TEST-MODE
mprenc -i ${RAWMODELDATADIR} -v INPUTDIR_MESSY -T ${HRES} \
-n $thisnml -o . -t >&! ${logfile}
endif
### CHECK SUCCESS
if ( -e $OUTFILE ) then
set success = 1
@ count_nml = $nnml ### SKIP LOOP
endif
endif
@ count_nml++
end
if ($success == 1) then
echo " ... success"
else
if ($testmode == 0) then
echo " ... failed"
else
echo " ... <failed>"
endif
endif ### SUCCESS
endif ### INFILE IS FILE
endif ### OUTFILE EXISTS
@ count_nc++
end
echo " ................................................................."
echo "END $0"
echo "=================================================================="
exit 0