Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/bin/csh# ---------------------------------------------------------------------# Usage# ---------------------------------------------------------------------# Write usage informationif ( (${#argv} == 0) ) thenechoecho "-----------------------------------------------------------------------------------"echo "Usage: "echoecho " 1. lagranto {caseid[.label]} startdate enddate {startf} {select} [flags] "echoecho " caseid : case (name of case directories; label appears in output directoy)"echo " local (everything is run in local directory)"echo " interim (take wind fields from Era-Interim)"echo " analysis (take wind fields from ECMWF operational analysis)"echo " forecast (take wind fields from ECMWF operational forecast)"echoecho " startdate : start date in format yyyymmdd_hh(mm)"echoecho " enddate : end date in format yyyymmdd_hh(mm)"echoecho " startf : [ file | criterion ] with starting coordinates (lon,lat,p)"echoecho " select : [ file | criterion ] for trajectory selection (set 'nil' for none)"echoecho " flags : -o outfile (name of output trajectory file) "echo " -j (jumping flag for crossing lower boundary)"echo " -v filename (name of tracing file)"echo " -r filename (name of region file)"echo " -noclean (do not clean trajectory directory after run)"echo " -prep (prepare run script, but do not run lagranto)"echo " -log (logfile is written to screen instead of logfile)"echo " -changet (change times on netCDF files realtive to startdate)"echoecho " 2. lagranto [ -open | -remove ] caseid[.label] "echoecho " -open : change to the run directory [open a new Xterm window]"echo " -remove : remove the whole run directory"echoecho "Examples:"echoecho "-----------------------------------------------------------------------------------"exit 0endif# Check whether LAGRANTO was set with setenvset test = `setenv | grep LAGRANTO`if ( "${test}" == "" ) thenecho "---------------------------------------------------------------------"echo "Please set environmental variable <LAGRANTO> to your source directory"echo 'csh > setenv LAGRANTO ${TO_YOUR_LAGRANTO_INSTALLATION}'echo "---------------------------------------------------------------------"exit 1endif# ---------------------------------------------------------------------# Set some general parameters# ---------------------------------------------------------------------# Set the prefix of the primary and secondary data filesset charp = 'P'set chars = 'S'# Set the name of the run scriptset runscript = "runscript"# Remember the calling directoryset calldir = $PWD# ---------------------------------------------------------------------# Parameter handling# ---------------------------------------------------------------------# Write titleechoecho '========================================================='echo ' *** START OF LAGRANTO *** 'echo# Save the complete argument listset lagranto_call = "$*"echo '---- HANDLE ARGUMENTS -----------------------------------'echo# Get fixed argumentsif (${#argv} == 1) thenset mode = '-open'set caseid = "$1"shiftelse if (${#argv} == 2) thenset mode = "$1"set caseid = "$2"shiftshiftelseset caseid = "$1"set startdate = "$2"set enddate = "$3"set startf = "$4"set select = "$5"set mode = "runlagranto"shiftshiftshiftshiftshiftendif# Split <caseid> into base and appendixset casebase = `(echo $caseid | awk -F "." '{print $1}')`set appendix = `(echo $caseid | awk -F "." '{print $2}')`if ( "${appendix}" != "" ) thenset appendix = ".${appendix}"endif# Set the directories (depending on <caseid>)if ( "${casebase}" == "local" ) thenset cdfdir = $PWDset tradir = $PWDelse if ( "${casebase}" == "interim" ) thenset cdfdir = /net/dansgaard/atmosdyn/erainterim/cdf/set tradir = $PWDelse if ( "${casebase}" == "analysis" ) thenset cdfdir = 'TO_BE_SPECIFIED'set tradir = $PWDecho "ERROR: caseid (analysis) not yet implemented"exit 1else if ( "${casebase}" == "forecast" ) thenset cdfdir = 'TO_BE_SPECIFIED'set tradir = $PWDecho "ERROR: caseid (forecast) not yet implemented"exit 1elseset cdfdir = ${HOME}/cdf/${casebase}set tradir = ${HOME}/tra/${casebase}endif# Change to the run directory if requestedif ( "${mode}" == "-open" ) thenset count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1echo "[${count}] ${folder}"endechoecho "Please select directory [0...${count}]"set id = $<set count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1if ( "${count}" == "${id}" ) thencd ${folder}xterm &endifendexit 0endif# Remove the whole run directory if requestedif ( "${mode}" == "-remove" ) thenset count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1echo "[${count}] ${folder}"endechoecho "Please select directory [0...${count}, all]"set id = $<set count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1if ( ( "${count}" == "${id}" ) | ( "${id}" == "all" ) ) then\rm -r ${folder}endifendexit 0endif# Show the trajectory fileif ( "${mode}" == "-show" ) thenset count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1echo "[${count}] ${folder}"endechoecho "Please select directory [0...${count}]"set id = $<set count = 0foreach folder ( ${tradir}/ntr_*_${caseid}_* )@ count = ${count} + 1if ( "${count}" == "${id}" ) thencd ${folder}set filename = ` sed -ne '9,9p' runscript.sh | cut -c 2-`${LAGRANTO}/goodies/trainfo.sh ${filename} list | lessendifendexit 0endif# Decide whether <select> is a file or an explicit criterionset flag_select = 'file'set test = `echo ${select} | grep ':' | wc -c`if ( "${test}" != "0" ) thenset flag_select = 'criterion'endif# Decide whether <startf> is a file or an explicit criterionset flag_startf = 'file'set test = `echo ${startf} | grep ' ' | wc -c`if ( "${test}" != "0" ) thenset flag_startf = 'point'endifset test = `echo ${startf} | grep '@' | wc -c`if ( "${test}" != "0" ) thenset flag_startf = 'criterion'endif# Write some status informationecho " Case ID : ${caseid}"echo " Start date : ${startdate}"echo " End date : ${enddate}"if ( "${flag_startf}" == "criterion" ) thenecho " Start file : ${startf} [criterion] -> startf.criterion [file]"else if ( "${flag_startf}" == "file" ) thenecho " Start points : ${startf} [file]"else if ( "${flag_startf}" == "point" ) thenecho " Start points : ${startf} [point] -> startf [file]"endifif ( "${flag_select}" == "criterion" ) thenecho " Selection : ${select} [criterion] -> selectf.criterion [file]"elseecho " Selection : ${select} [file]"endifecho# Set defaults for flagsset jumpflag = ""set tracefile = "tracevars"set outfile = "lsl_${startdate}"set keepflag = ""set prepflag = ""set logfile = "${runscript}.logfile"set regionf = "regionf"set changet = "false"# Handle flagswhile ( $#argv > 0 )switch ( $argv[1] )echo $argv[1]case -jset jumpflag = "-j"echo " Flag '-j [jump]' -> True "echobreakswcase -prepset prepflag = "prep"echo " Flag '-prep [prepare only]' -> ${prepflag} "echobreakswcase -nocleanset keepflag = "keep"echo " Flag '-noclean [no cleaning]' -> ${keepflag} "echobreakswcase -oset outfile = $argv[2]echo " Flag '-o [output]' -> ${outfile} "echobreakswcase -vset tracefile = $argv[2]echo " Flag '-v [tracevars]' -> ${tracefile} "echoshiftbreakswcase -rset regionfile = $argv[2]echo " Flag '-r [regionfile]' -> ${regionfile} "echoshiftbreakswcase -logset logfile = "/dev/stdout"echo " Flag '-v [log]' -> ${logfile} "breakswcase -changetset changet = "true"echo " Flag '-changet [change times]'-> True "echobreakswendswshift;end# Decide whether a tracevars file is given or notif ( -f ${tradir}/${tracefile} ) thenset flag_tracevars = 'file'elseset flag_tracevars = 'nil'endifechoif ( "${flag_tracevars}" == "file" ) thenecho " Trace Vars : ${tracefile} [file]"elseecho " Trace Vars : [no fields are traced]"endifecho# Set the name of the run directory (forward/backward distinction)set dir = 'f'set range = `${LAGRANTO}/bin/gettidiff.sh ${enddate} ${startdate}`if ( ${range} < 0 ) thenset dir = 'b'set range = `${LAGRANTO}/bin/gettidiff.sh ${startdate} ${enddate}`endifset rundir = ${tradir}/ntr_${startdate}_${dir}${range}_${caseid}if ( "${flag_startf}" == "criterion" ) thenset rundir = "${rundir}_startf"else if ( "${flag_startf}" == "point" ) thenset rundir = "${rundir}_startf"else if ( "${flag_startf}" == "file" ) thenset rundir = "${rundir}_${startf}"endifif ( "${flag_select}" != "file" ) thenset rundir = "${rundir}_selectf"elseset rundir = "${rundir}_${select}"endif# Write directory namesecho '---- DIRECTORIES ----------------------------------------'echoecho " CDF = ${cdfdir}"echo " TRA = ${tradir}"echo " RUN = ${rundir}"# Check whether CDF and TRA directories existif ( ! -d ${cdfdir} ) thenecho 'Cannot find CDF directory ... Stop'exit 1endifif ( ! -d ${tradir} ) thenecho 'Cannot find TRA directory ... Stop'exit 1endif# Create the run directoryif ( ! -d ${rundir} ) thenmkdir ${rundir}endifif ( ! -d ${rundir} ) thenecho 'Cannot create RUN directory ... Stop'exit 1endif# ---------------------------------------------------------------------# Handle the time specifier - startdate, enddate# ---------------------------------------------------------------------echoecho '---- TIME RANGE -----------------------------------------'echo# Check format of start and end date - must be the sameset ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`set ne=`echo $enddate | sed -e 's/_[0-9]*//' | wc -c`if ( $ns != $ne ) thenecho " ERROR: start and end date must be in the same format ***"exit 1endifif ( $ns != 9 ) thenecho " ERROR: Date format must be yyyymmdd ***"exit 1endifset ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`set ne=`echo $enddate | sed -e 's/[0-9]*_//' | wc -c`if ( $ns != $ne ) thenecho " ERROR: start and end date must be in the same format ***"exit 1endifif ( ( $ns != 5 ) & ( $ns != 3 ) ) thenecho " ERROR: Time format must be hh(mm) ***"exit 1endif# Split the start and end date into <yymmdd_hh and mm>set startdate_ymdh = `echo $startdate | cut -c 1-11`set startdate_min = `echo $startdate | cut -c 12-13`if ( $startdate_min == "" ) set startdate_min = 00set enddate_ymdh = `echo $enddate | cut -c 1-11`set enddate_min = `echo $enddate | cut -c 12-13`if ( $enddate_min == "" ) set enddate_min = 00# Get the time difference between <start_ymdh> and <end_ymdh> date# Decide whether trajectoriesare forward or backwardset timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`if ( $timediff_hh == 0 ) thenif ( $enddate_min > $startdate_min ) thenset direction = fset idir = 1elseset direction = bset idir = -1endifelse if ( $timediff_hh > 0 ) thenset direction = fset idir = 1elseset direction = bset idir = -1@ timediff_hh = $idir * $timediff_hhendif# Get also minutes for time difference, if <start_min> or <end_min> != 0set timediff_mm=if ( $startdate_min != 00 || $enddate_min != 00 ) then@ min = ( $enddate_min - $startdate_min )if ( $min == 0 ) thenset timediff_mm=else if ( $min > 0 ) thenif ( $idir == 1 ) thenset timediff_mm=$minelse@ timediff_hh --@ timediff_mm = 60 - $minendifelseif ( $idir == 1 ) then@ timediff_hh --@ timediff_mm = 60 + $minelse@ timediff_mm = 0 - $minendifendifendif# Set the reference date equal to the startdateset refdate=${startdate}# Write status informationecho "Time range : ${startdate} -> ${enddate}"if ( ${timediff_mm} != "" ) thenecho "Time difference : ${timediff_hh} h ${timediff_mm} min"elseecho "Time difference : ${timediff_hh} h"endifecho "Direction : ${direction} (${idir})"echo "Reference date : ${refdate}"# ---------------------------------------------------------------------# Decide which files are needed and check availability# ---------------------------------------------------------------------echoecho '---- INPUT FILES ----------------------------------------'echo# Change to input data (cdf) directorycd ${cdfdir}echo# Take the time increment from flag list ('nil', if not defined)set timeinc = 'nil'# Find a first data file (if possible corresponding to start/end date# If starttime is not a data time, take the first file in the direectoryif ( $direction == "f" ) thenset file=${charp}${startdate_ymdh}elseset file=${charp}${enddate_ymdh}endifif ( ! -f $file ) thenset file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`endif# Determine timeinc (the time difference in hours between two data file)# if not already defined with option -iif ( ${timeinc} == "nil" ) thenset date1=`echo $file | cut -c 2-12`set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`@ n ++set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`endifif ( $timeinc == 0 ) thenecho " ERROR: cannot set the time increment between input files ***"exit 1endif# Search the first file to useset flag=0set td=foreach i ( ${charp}????????_?? )set date = `echo $i | cut -c 2-12`set td1 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`set td2 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) thenset datfiles=$dateif ( $td1 < $timeinc ) set td=$td1if ( $td2 < $timeinc ) set td=$td2if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++goto label2endifend# if no P/T-files are available for the specified time period, then $td is# still undefinedif ( $td == "" ) thenecho " ERROR: no data files available for the specified time period"exit 1endif# Everything is fine so far: proceedlabel2:# Calculate the number of further files@ num = ( $timediff_hh + $td ) / $timeinc + 1@ dum1 = ( $num - 1 ) * $timeinc@ dum2 = $timediff_hh + $tdif ( $dum1 != $dum2 ) @ num ++# Get a list of all needed filesset numfiles=$numset sfiles=1while ( $num > 1 )set date=`${LAGRANTO}/goodies/newtime $date $timeinc`if ( ! -f ${charp}${date} ) thenecho " ERROR: file with primary data is missing for $date"exit 1else if ( ! -f ${chars}${date} ) thenset sfiles=0set datfiles=`echo $datfiles $date`elseset datfiles=`echo $datfiles $date`endif@ num --end# Write some status informationecho "Primary file prefix : ${charp}"echo "Secondary file prefix : ${chars}"echo "Time increment for input files : ${timeinc}"echo "# input files : ${numfiles}"echo "${charp} files availability : 1"echo "${chars} files availability : ${sfiles}"echo "First input file : $datfiles[1] "echo "Last input file : $datfiles[$numfiles] "# ---------------------------------------------------------------------# Prepare run directory - copy all needed files# ---------------------------------------------------------------------echoecho '---- PREPARE RUN DIRECTORY ------------------------------'echo# Change to run directorycd ${rundir}echo# Link cdf input filesforeach date ( $datfiles )if ( -f ${cdfdir}/${charp}${date} ) thenif ( ! -f ${charp}${date} ) thenln -s ${cdfdir}/${charp}${date} ${charp}${date}echo "Linking ${date} "elseecho "Existing ${date} "endifendifif ( -f ${cdfdir}/${chars}${date} ) thenif ( ! -f ${chars}${date} ) thenln -sf ${cdfdir}/${chars}${date} ${chars}${date}endifendifend# Change times on netCDF files relative to reference dateif ( "${changet}" == "true" ) thenechoforeach i ( $datfiles )${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}endif ( ${sfiles} == 1 ) thenechoforeach i ( $datfiles )${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}endendifendif# Get the constants file for each fileset first = 1foreach date ( $datfiles )set cstname = `ncdump -h ${charp}${date} | grep constants_file_name`set cstname = `echo \'${cstname}\' | awk -F\" '{print $2}'`if ( -f ${cdfdir}/${cstname} ) thenif ( ! -f ${cstname} ) thenln -s ${cdfdir}/${cstname} ${cstname}echo "Linking constants file ${cstname}"set first = 0else if ( "${first}" == "1" ) thenecho "Existing constants file ${cstname}"set first = 0endifendifend# Copy the tracevars fileif ( "${flag_tracevars}" == "file" ) thenif ( -f ${tradir}/${tracefile} ) then\cp ${tradir}/${tracefile} .endifendif# Copy the start file/criterionif ( "${flag_startf}" == "criterion" ) then\rm -f startf.criterionecho ${startf} > startf.criterionset startf = "startf.criterion"else if ( "${flag_startf}" == "file" ) thenif ( -f ${tradir}/${startf} ) then\cp ${tradir}/${startf} .endifelse if ( "${flag_startf}" == "point" ) then\rm -f startfecho ${startf} > startfset startf = "startf"set flag_startf = "file"endif# Copy the select file/criterionif ( "${flag_select}" == "criterion" ) then\rm -f select.criterionecho ${select} > select.criterionset select = "select.criterion"elseif ( -f ${tradir}/${select} ) then\cp ${tradir}/${select} .endifendif# ---------------------------------------------------------------------# Prepare the run script# ---------------------------------------------------------------------echoecho '---- PREPARE RUN SCRIPT ---------------------------------'echo# Remove runscript if it already existsif ( -f ${runscript}.sh ) then\rm -f ${runscript}.shendif# Write headerecho '#\!/bin/csh' >! ${runscript}.shecho "#" >> ${runscript}.shecho "#----- Calling command" >> ${runscript}.shecho "#" >> ${runscript}.shecho "# lagranto ${lagranto_call} " >> ${runscript}.shecho "#" >> ${runscript}.shecho "#----- Output file " >> ${runscript}.shecho "#" >> ${runscript}.shecho "# ${outfile} " >> ${runscript}.shecho "#" >> ${runscript}.sh# Decide whether <create_startf> has to be runif ( "${flag_startf}" == "criterion" ) thenecho "#------ Run <startf>" >> ${runscript}.shecho "#" >> ${runscript}.shecho ${LAGRANTO}/bin/create.sh ${startdate} startf \"`cat startf.criterion`\" -t ${tracefile} -regionf ${regionf} >> ${runscript}.shecho "#" >> ${runscript}.shset startf = "startf"endif# Check whether startfile is readycat >> ${runscript}.sh <<EOF#------ Abort if no startf is available#if ( ! -f ${startf} ) thenecho " ERROR: no start file available .... Stop"exit 1endifEOF# Decide wich intermediate format is used (needed for efficient communication between different programs)set iform1 = ""set iform2 = ""if ( "${tracefile}" != "" ) set iform1 = ".4"if ( "${select}" != "nil" ) set iform2 = ".4"# Remove all trajectory files which might already be therecat >> ${runscript}.sh <<EOF##------ Remove existing trajectory files#if ( -f ${outfile}${iform1} ) then\rm -f ${outfile}${iform1}endifif ( -f ${outfile}${iform2} ) then\rm -f ${outfile}${iform2}endifif ( -f ${outfile} ) then\rm -f ${outfile}endifEOF# Write call to <caltra> into runscriptecho "#" >> ${runscript}.shecho "#------ Run <caltra>" >> ${runscript}.shecho "#" >> ${runscript}.shecho ${LAGRANTO}/bin/caltra.sh ${startdate} ${enddate} startf ${outfile}${iform1} ${jumpflag} >> ${runscript}.sh# Check whether caltra was successfulcat >> ${runscript}.sh <<EOF##------ Abort if caltra was not successful#if ( ! -f ${outfile}${iform1} ) thenecho " ERROR: caltra failed .... Stop"exit 1endifEOF# Decide whether <trace> has to be runif ( "${tracefile}" != "" ) thenecho "#" >> ${runscript}.shecho "#------ Run <trace>" >> ${runscript}.shecho "#" >> ${runscript}.shecho ${LAGRANTO}/bin/trace.sh ${outfile}${iform1} ${outfile}${iform2} -v ${tracefile} >> ${runscript}.shendif# Check whether trace was successfulcat >> ${runscript}.sh <<EOF##------ Abort if trace was not successful#if ( ! -f ${outfile}${iform2} ) thenecho " ERROR: trace failed .... Stop"exit 1endifEOF# Decide whether <select> has to be runif ( "${select}" != "nil" ) thenecho "#" >> ${runscript}.shecho "#------ Run <select>" >> ${runscript}.shecho "#" >> ${runscript}.shecho ${LAGRANTO}/bin/select.sh ${outfile}${iform2} ${outfile} \"`cat ${select}`\" -regionf ${regionf} >> ${runscript}.shecho "#" >> ${runscript}.shendif# Show runscript on screenmore ${runscript}.sh# Change runscript to executablechmod u+x ${runscript}.sh# Decide whether to stop at this place (only preparation for Lagranto run)if ( "${prepflag}" == "prep" ) thenechoecho '---- READY TO RUN ----- ---------------------------------'echoecho " Would you like to change to the rundirectory [y/n] "set id = $<if ( ( "${id}" == "y" ) | ( "${id}" == "Y" ) ) thencd ${rundir}xterm &endifexit 0endif# Define environment variable for the caseechoecho '---- REMEMBER MOST RECENT CALL --------------------------'echocd ${calldir}if ( -f lagranto.param ) \rm lagranto.paramecho "${rundir}" >! lagranto.paramecho "${outfile}" >> lagranto.paramcd ${rundir}echoecho " run directory + output file -> lagranto.param"# ---------------------------------------------------------------------# Run lagranto# ---------------------------------------------------------------------echoecho '---- RUN LAGRANTO ---------------------------------------'echoif ( "${logfile}" != "/dev/stdout" ) thenecho "log goes to ${logfile}"endif# Remove logfileif ( "${logfile}" != "/dev/stdout" ) then\rm -f ${logfile}endif# Run it${rundir}/${runscript}.sh > ${logfile}# Check for errors in logfileif ( "${logfile}" != "/dev/stdout" ) thengrep ERROR ${logfile}endif# ---------------------------------------------------------------------# Make clean and finish lagranto run# ---------------------------------------------------------------------# Remove unneeded files\rm -f fort.9# Remove files if requestedif ( "${keepflag}" != "keep" ) thenechoecho '---- MAKING CLEAN ---------------------------------------'echocd ${rundir}set runscript = "${runscript}.sh"foreach file ( `\ls -L1` )set id = "0"if ( "${file}" == "${outfile}" ) set id="1"if ( "${file}" == "${runscript}" ) set id="1"if ( "${file}" == "${logfile}" ) set id="1"if ( "${id}" == "0" ) \rm -f ${file}endendif# Everything is fine!echoecho ' *** END OF LAGRANTO *** 'echo '========================================================='echoexit 0