#! /usr/bin/tcsh -f
### THIS SHELLSCRIPT USES
### - NCREGRID
### - GAWK
### - SED
### - NCTOOLS
### TO PRE-REGRID MESSy - INPUT DATA TO VARIOUS HORIZONTAL RESOLUTIONS.
###
### THE RESULTING FILES CAN BE USED FOR A FASTER ECHAM5/MESSy-START
### IN ALL CASES, WHERE NCREGRID IS USED ONLINE!
###
### THE PRESENT SCRIPT DOES A RENAMING OF ALL VARIABLES IN THE OUTPUT-FILE
### BACK TO THE NAMES IN THE INPUT FILE;
### FURTHERMORE INPUT AND OUTPUT FILENAME ARE IDENTICAL.
###
### THEREFORE VERY SIMILAR MESSy (SUBMODEL-)NAMELIST-FILES CAN BE USED
### FOR ALL RESOLUTIONS WITH ONLY MINOR CHANGES:
### (1) If the regrid-namelist contains the entries
### i_latr = ...
### and/or
### i_lonr = ...
### the resulting imported data field would be wrong!
### Reason: The pre-regridding of the data puts the fields onto the
### horizontal ECHAM5 grid with longitudes in the interval
### [0, 360] !
### During the data import (runtime) the namelist entry 'i_lonr'
### would be used again, however, for the wrong interval, e.g.,
### [-180,180].
### (2) Furthermore, after pre-regridding
### i_latr = -90.0, 90.0,
### must be specified in the namelist, especially, if i_lati
### is not present.
### Reason: The ECHAM5 grid in latitudinal direction is not
### equidistant. NCREGRID automatically completes the
### input grid (from latitude mids to latitude interfaces),
### missing a fraction of the polar regions ...
### (3) Variables of type IXF must be imported as type INT after
### pre-regridding.
### THE RESPECTIVELY UPDATED NAMELIST FILE IS ALSO OUTPUT.
###
### TO USE PRE-REGRIDDED DATA, SET 'USE_PREREGRID=.true.' in 'xmessy' !
###
### Version : 0.5b
### Author : Patrick Joeckel, MPICH, July 2003
###
### FOR DEBUGGING
# set echo verbose
set VERSION = "0.5b"
set DATE = `date`
### GLOBAL SETTINGS ####################################################
### 1) TEMPORARY FILES:
set TMPSHELL = tmp.tcsh
set TMPGAWK = tmp.gawk
set TMPNML = tmp.nml
set TMPLIST = tmp.list
### 2) ECHAM5 SPECIFIC SETTINGS:
###
#set GRDDIR_BASE = /afs/ipp/mpc/EVAL/echam5_ini/echam5.3.01
set GRDDIR_BASE = /modeldata/ECHAM5/EVAL/echam5_ini/echam5.3.01
set GRDNAME = TSLCLIM.nc
### THE EXACT GRDFILE NEEDS THE RESOLUTION SPECIFIED BY THE USER:
### -> THEREFORE IT WILL BE DEFINED BELOW
#set GRDFILE = $GRDDIR_BASE/$RES/${RES}_$GRDNAME
### -> AXIS NAMES IN GRDFILE:
set GRDLATM = "lat"
set GRDLONM = "lon"
######################################################################
### PARSE COMMAND LINE ###############################################
if ( "$1" == "" ) then
echo ' '
echo 'Usage:'
echo " `basename $0` [-t] [-k] [-o <DIR>] [-m <FILE>] -i <DIR> -v <VAR> -T <RES> -n <FILE>"
echo ' -t : TEST MODE'
echo ' -k : KEEP TEMPORARY FILES'
echo ' -o DIR : OUTPUT BASE DIRECTORY FOR netCDF FILES'
echo ' NOTES: 1) DEFAULT: .'
echo ' 2) THIS DIRECTORY WITH SUBMODEL SPECIFIC'
echo ' SUBDIRECTORY MUST ALREADY EXIST'
echo ' (SEE infile ENTRY in namelist-file)'
echo ' 3) THIS DIRECTORY MUST BE DIFFERENT'
echo ' FROM THE INPUT BASE DIRECTORY'
echo ' -m FILE : MODIFIED NAMELIST FILE'
echo ' -i DIR : INPUT BASE DIRECTORY (RAW DATA)'
echo ' -v VAR : INPUT DIRECTORY VARIABLE-NAME TO SUBSTITUTE'
echo ' -T RES : RESOLUTION (T21, T42, ...)'
echo ' -n FILE : NAMELIST FILE FOR NCREGRID'
echo ' '
echo ' '
echo 'Example:'
echo " `basename $0` -i /modeldata/ECHAM5/messy/raw -v INPUTDIR_MESSY -T T21 -n xxxx.nml -o /modeldata/ECHAM5/messy/T21 -m nml_pre/xxxx.nml"
echo " * creates $TMPGAWK"
echo " * calls gawk -f $TMPGAWK xxxx.nml which"
echo " # creates $TMPNML from xxxx.nml"
echo ' - with "$INPUTDIR_MESSY" replaced by'
echo ' "/modeldata/ECHAM5/messy/raw"'
echo ' - with "/modeldata/ECHAM5/messy/T21/xxxx" as output-path,'
echo ' whereby "xxxx" is the same subdirectory name'
echo ' as in the "infile" of "xxxx.nml"'
echo ' - the output filename is the "infile"-name of "xxxx.nml"'
echo " # creates $TMPSHELL"
echo ' - for the renaming of the netCDF-variables'
echo ' and -dimensions to the original name'
echo " * calls ncregrid $TMPNML for the re-gridding"
echo " * calls source $TMPSHELL for the re-naming"
echo ' * transfers the modified namelist-file to "nml_pre/xxxx.nml"'
echo ' * checks if output files are present for all "infile"s'
echo ' * deletes all temporary files'
echo ' '
exit 1
endif
### DEFAULT SETTINGS
set testmode = 0
set keeptmp = 0
set ODIR = .
set MFILE = ""
while ("$1" != "")
switch ("$1")
case "-t":
set testmode = 1
breaksw
case "-k":
set keeptmp = 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 RES = $1
endif
breaksw
case "-v":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-v" option missing\!'
exit 1
else
set VAR = $1
endif
breaksw
case "-o":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-o" option missing\!'
exit 1
else
set ODIR = $1
endif
if (! -d $ODIR) then
echo 'ERROR: Directory does not exist: '$ODIR
exit 1
endif
breaksw
case "-m":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-m" option missing\!'
exit 1
else
set MFILE = $1
endif
if (! -d `dirname $MFILE`) then
echo 'ERROR: Directory does not exist: '`dirname $MFILE`
exit 1
endif
breaksw
case "-i":
shift
if ( ("$1" == '') || \
(`echo $1 | awk '{print substr($1,1,1)}'` == "-") ) then
echo 'Error: Argument of "-i" option missing\!'
exit 1
else
set IDIR = $1
endif
if (! -d $IDIR) then
echo 'ERROR: Directory does not exist: '$IDIR
exit 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 NMLFILE = $1
endif
if (! -e $NMLFILE) then
echo 'ERROR: File does not exist: '$NMLFILE
exit 1
endif
breaksw
default:
echo 'ERROR: Unrecognized option: '$1
exit 1
endsw
shift
end
### CEHCKS
if (! ${?IDIR}) then
echo 'ERROR: -i option must be specified\!'
exit 1
endif
if (! ${?VAR}) then
echo 'ERROR: -v option must be specified\!'
exit 1
endif
if (! ${?NMLFILE}) then
echo 'ERROR: -n option must be specified\!'
exit 1
endif
if (! ${?RES}) then
echo 'ERROR: -T option must be specified\!'
exit 1
endif
#########################################################################
### -> GRDFILE FOR NCREGRID (OFFLINE):
set GRDFILE = $GRDDIR_BASE/$RES/${RES}_$GRDNAME
### INITIALIZE SOFTWARE
echo 'INITIALIZING SOFTWARE ..........................................'
### GAWK
set GAWK_WHERE=(`where gawk`)
if ( ${#GAWK_WHERE} == 0) then
echo 'ERROR: gawk not found!'
exit 1
else
set GAWK=$GAWK_WHERE[1]
echo ' -> '$GAWK
endif
### NCDUMP
set NCDUMP_WHERE=(`where ncdump`)
if ( ${#NCDUMP_WHERE} == 0) then
echo 'ERROR: ncdump not found!'
exit 1
else
set NCDUMP=$NCDUMP_WHERE[1]
echo ' -> '$NCDUMP
endif
### NCREGRID
set NCREGRID_WHERE=(`where ncregrid`)
if ( ${#NCREGRID_WHERE} == 0) then
echo 'ERROR: ncregrid not found!'
exit 1
else
set NCREGRID=$NCREGRID_WHERE[1]
echo ' -> '$NCREGRID
endif
### NCVARRENAME
set NCVARRENAME_WHERE=(`where ncvarrename`)
if ( ${#NCVARRENAME_WHERE} == 0) then
echo 'ERROR: ncvarrename not found!'
exit 1
else
set NCVARRENAME=$NCVARRENAME_WHERE[1]
echo ' -> '$NCVARRENAME
endif
### NCDIMRENAME
set NCDIMRENAME_WHERE=(`where ncdimrename`)
if ( ${#NCDIMRENAME_WHERE} == 0) then
echo 'ERROR: ncdimrename not found!'
exit 1
else
set NCDIMRENAME=$NCDIMRENAME_WHERE[1]
echo ' -> '$NCDIMRENAME
endif
### NCATTPUT
set NCATTPUT_WHERE=(`where ncattput`)
if ( ${#NCATTPUT_WHERE} == 0) then
echo 'ERROR: ncattput not found!'
exit 1
else
set NCATTPUT=$NCATTPUT_WHERE[1]
echo ' -> '$NCATTPUT
endif
echo '................................................................'
### DIAGNOSTIC OUTPUT
echo '................................................................'
echo 'SCRIPT : '$0
echo 'VERSION : '$VERSION
echo 'DATE : '$DATE
echo 'GRID-DIR BASENAME : '$GRDDIR_BASE
echo 'GRID INFORMATION FILE : '$GRDFILE
echo 'INPUT DIRECTORY BASE : '$IDIR
echo 'NML-INPUTDIR VARIABLE : $'$VAR
echo 'RESOLUTION : '$RES
echo 'NAMELIST-FILE : '$NMLFILE
echo 'OUTPUT DIRECTORY BASE : '$ODIR
echo 'MODIFIED NAMELIST-FILE : '$MFILE
if ($testmode == 1) then
echo 'MODE : TEST'
else
echo 'MODE : PROCESS'
endif
if ($keeptmp == 1) then
echo 'KEEP TMP-FILES : YES'
else
echo 'KEEP TMP-FILES : NO'
endif
echo '................................................................'
### CREATE GAWK-SCRIPT
cat >&! $TMPGAWK <<EOF
BEGIN {rg = 0 ;
SHELLFILE = "$TMPSHELL" ;
OUTFILE = "";
INFILE = "";
LILONM = 0;
LILONI = 0;
LILATM = 0;
LILATI = 0;
LITIMEM = 0;
print "#! /usr/local/bin/tcsh -f\n\n" > SHELLFILE
}
{
gsub("'","\"")
if (toupper(\$1) == "®RID") {rg = 1}
if ((toupper(\$1) == "/") && (rg == 1)) {
rg = 0 ;
print "outfile = \""OUTFILE"\","
print "grdfile = \"$GRDFILE\"," ;
if ((LILATM == 1) || (LILATI == 1))
{ print "g_latm = \"$GRDLATM\"," ;
print "g_latr = -90.0,90.0," ;
}
if ((LILONM == 1) || (LILONI == 1))
{ print "g_lonm = \"$GRDLONM\","} ;
if (LILATM == 1)
{ if (ILATM != "$GRDLATM")
{
print "ncvarrename -i " OUTFILE " -v $GRDLATM -new " ILATM \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim $GRDLATM -new " ILATM \
>> SHELLFILE
}
if (LILATI == 1)
{
if (ILATI != "${GRDLATM}_I")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLATM}_I -new " ILATI \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLATM}_I -new " ILATI \
>> SHELLFILE
}
}
else
{
if (ILATM != "${GRDLATM}")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLATM}_I -new " ILATM "_I" \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLATM}_I -new " ILATM "_I" \
>> SHELLFILE
}
}
}
else
{
if (LILATI == 1)
{
if (ILATI != "${GRDLATM}_I")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLATM}_I -new " ILATI \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLATM}_I -new " ILATI \
>> SHELLFILE
print "ncvarrename -i " OUTFILE " -v ${GRDLATM} -new " ILATI "_M" \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLATM} -new " ILATI "_M" \
>> SHELLFILE
}
}
}
if (LILONM == 1)
{ if (ILONM != "$GRDLONM")
{
print "ncvarrename -i " OUTFILE " -v $GRDLONM -new " ILONM \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim $GRDLONM -new " ILONM \
>> SHELLFILE
}
if (LILONI == 1)
{
if (ILONI != "${GRDLONM}_I")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLONM}_I -new " ILONI \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLONM}_I -new " ILONI \
>> SHELLFILE
}
}
else
{
if (ILONM != "${GRDLONM}")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLONM}_I -new " ILONM "_I" \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLONM}_I -new " ILONM "_I" \
>> SHELLFILE
}
}
}
else
{
if (LILONI == 1)
{
if (ILONI != "${GRDLONM}_I")
{
print "ncvarrename -i " OUTFILE " -v ${GRDLONM}_I -new " ILONI \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLONM}_I -new " ILONI \
>> SHELLFILE
print "ncvarrename -i " OUTFILE " -v ${GRDLONM} -new " ILONI "_M" \
>> SHELLFILE
print "ncdimrename -i " OUTFILE " -dim ${GRDLONM} -new " ILONI "_M" \
>> SHELLFILE
}
}
}
print "ncattput -i " OUTFILE \
" -v NC_GLOBAL -a mprenc_version -d NC_CHAR -l -1 "\
"\"$VERSION\"" >> SHELLFILE
print "ncattput -i " OUTFILE \
" -v NC_GLOBAL -a mprenc_date -d NC_CHAR -l -1 "\
"\"$DATE\"" >> SHELLFILE
if (LITIMEM == 1)
{
COMMAND = "$NCDUMP -v"ITIMEM" "INFILE \
" | grep "ITIMEM \
" | $GAWK '{if (NR == 1)" \
" { split(\$0,c,\"[=;/() ]+\");" \
" if (c[2] == \"UNLIMITED\")" \
" {print c[3]} else {print c[2]} } }'"
STATUS = (COMMAND | getline) ;
close(COMMAND) ;
NT = \$0 ; print "i_t = 1,1,"NT"," ;
}
print "/" ;
OUTFILE = "";
INFILE = "";
LILONM = 0;
LILONI = 0;
LILATM = 0;
LILATI = 0;
LITIMEM = 0;
}
if (rg == 1) {
pos = index(\$0, "!")
if (pos == 0)
line = \$0
else
line = substr(\$0,1,pos-1)
endif
gsub("\\\\\$$VAR","$IDIR",line) ;
split(line,a,"[= ,\"]+") ;
if (tolower(a[1]) == "infile")
{
i = split(a[2],b,"[\\"/]") ;
OUTFILE = "$ODIR/"b[i-1]"/"b[i] ;
INFILE = "$IDIR/"b[i-1]"/"b[i] ;
} ;
if (tolower(a[1]) == "i_lonm")
{ ILONM = a[2]; LILONM = 1 } ;
if (tolower(a[1]) == "i_loni")
{ ILONI = a[2]; LILONI = 1 } ;
if (tolower(a[1]) == "i_latm")
{ ILATM = a[2]; LILATM = 1 } ;
if (tolower(a[1]) == "i_lati")
{ ILATI = a[2]; LILATI = 1 } ;
if (tolower(a[1]) == "i_timem")
{ ITIMEM = a[2]; LITIMEM = 1 } ;
if ( (tolower(a[1]) != "var") && (tolower(a[1]) != "outfile") \
&& (tolower(a[1]) != "i_t") )
{print line} ;
}
}
EOF
### CREATE NCREGRID NAMELIST(s)
$GAWK -f $TMPGAWK $NMLFILE >&! $TMPNML
### START NCREGRID
if ($testmode == 0) then
$NCREGRID $TMPNML
else
echo ' --> TESTMODE : SKIP REGRIDDING'
endif
### REANAME VARIABLES / DIMENSIONS
if ($testmode == 0) then
echo '................................................................'
echo ' --> RENAMING START'
source $TMPSHELL
echo ' --> RENAMING END '
echo '................................................................'
else
echo ' --> TESTMODE : SKIP RENAMING'
endif
### PROCESS NAMELIST FILE
if ($MFILE != "") then
if ($testmode == 0) then
echo '................................................................'
echo ' --> NAMELIST FILE MODIFICATION START'
set nml_base = `basename $NMLFILE`
echo ' ... MODIFYING FILE '$NMLFILE' TO '$MFILE
cat $NMLFILE | sed 's|i_latr|\!i_latr|g' \
| sed 's|i_lonr|\!i_lonr|g' \
| sed 's|:IXF|:INT|g' \
| sed '/®rid/a\ i_latr = -90.0,90.0,' \
>! $MFILE
cat $MFILE
echo ' --> NAMELIST FILE MODIFICATION END'
echo '................................................................'
else
echo ' --> TESTMODE : SKIP MODIFICATION OF NAMELIST FILE'
endif
endif
### CHECK RESULT
if ($testmode == 0) then
echo '................................................................'
echo ' --> CHECKING START'
cat $NMLFILE \
| sed 's|\!.*||g' | grep -Ev '^ *$' | grep -i "infile" \
| sed 's|[,"'\'' ]||g' | awk -F '=' '{print $2}' \
| sed 's|\$'$VAR'|'$IDIR'|g' >! $TMPLIST
set srclist = (`cat $TMPLIST`)
#
set nmax = ${#srclist}
@ count = 1
while ($count <= $nmax)
set ifile = $srclist[$count]
set ofile = `echo $ifile | sed 's|'$IDIR'|'$ODIR'|g'`
echo -n ' '$ifile' -> '$ofile' ... '
if ((-e $ifile) && (-e $ofile)) then
echo ' OK '
else
if (! -e $ifile) then
echo ' input file missing'
else
echo ' output file missing'
endif
endif
@ count++
end
#
if ($nmax == 0) then
echo ' no active "infile"-specification found in '$NMLFILE
endif
#
echo ' --> CHECKING END'
echo '................................................................'
else
echo ' --> TESTMODE : SKIP CHECKING RESULT'
echo '................................................................'
endif
### CLEAN UP
if ($keeptmp == 0) then
rm -f $TMPSHELL
rm -f $TMPGAWK
rm -f $TMPNML
rm -f $TMPLIST
else
echo '................................................................'
echo 'KEEPING TEMPORARY FILES: '
echo ' ... '$TMPGAWK
echo ' ... '$TMPSHELL
echo ' ... '$TMPNML
if ($testmode == 0) then
echo ' ... '$TMPLIST
endif
echo '................................................................'
endif
exit 0