#!/bin/bash ## g.e # WRF_launch.bash 19791201000000 19810101000000 28 /d4/lflmd/etudes/WRF_LMDZ/WaquaL/WRF/control ## g.e # WRF_launch.bash 19791201000000 19810101000000 month /d4/lflmd/etudes/WRF_LMDZ/WaquaL/WRF/control if test $1 = '-h'; then echo "********************************" echo "*** Launching WRF simulation ***" echo "********************************" echo "WRF_launch.bash [inidate] [enddate] [freq] [odir]" echo " [inidate]: initial date [YYYY][MM][DD][HH][MI][SS]" echo " [enddate]: final date [YYYY][MM][DD][HH][MI][SS]" echo " [freq]: frequency restarts [DD] if it is 'month' real month frequency will be used" echo " [odir]: folder for the outputs" echo " [restart]: 'true' for a resume a simulation" else seconds_between_dates() { # Functino to provide the seconds between two dates date1=$1 date2=$2 # Date1 ## YMD1=${date1:0:8} hours1=${date1:8:2} minutes1=${date1:10:2} seconds1=${date1:12:2} hours1=`(expr $hours1 '*' 3600)` minutes1=`(expr $minutes1 '*' 60)` seconds1=`(expr $hours1 + $minutes1 + $seconds1)` sec1=`(date +%s -u -d"$YMD1 $seconds1 seconds")` # Date2 ## YMD2=${date2:0:8} hours2=${date2:8:2} minutes2=${date2:10:2} seconds2=${date2:12:2} hours2=`(expr $hours2 '*' 3600)` minutes2=`(expr $minutes2 '*' 60)` seconds2=`(expr $hours2 + $minutes2 + $seconds2)` sec2=`(date +%s -u -d"$YMD2 $seconds2 seconds")` seconds=`(expr $sec2 - $sec1)` echo $seconds } create_WRFnamelist() { # Function to create namelist.input from a nameslit WRF template # nlistmplt: template of the namelist.input # id: initial date ([YYYYMMDDHHMISS] format) # ed: final date ([YYYYMMDDHHMISS] format) # fr: frquency (in days) of the restarts # it: number of sub-period within the simulation experiment nlisttmplt=$1 id=$2 ed=$3 fr=$4 it=$5 frH=`expr ${fr} '*' 24` dThist=`cat ${nlisttmplt} | grep history_interval | awk '{print $3}' | tr ',' ' ' |\ awk '{print $1*60}'` secsPeriod=`seconds_between_dates ${id} ${ed}` Nouts=`expr ${secsPeriod} / ${dThist}` cp ${nlisttmplt} namelist.input sed -e 's/@iyear@/'${id:0:4}'/g' namelist.input >& tmpA sed -e 's/@imonth@/'${id:4:2}'/g' tmpA >& tmpB sed -e 's/@iday@/'${id:6:2}'/g' tmpB >& tmpA sed -e 's/@ihour@/'${id:8:2}'/g' tmpA >& tmpB sed -e 's/@iminute@/'${id:10:2}'/g' tmpB >& tmpA sed -e 's/@isecond@/'${id:12:2}'/g' tmpA >& tmpB sed -e 's/@eyear@/'${ed:0:4}'/g' tmpB >& tmpA sed -e 's/@emonth@/'${ed:4:2}'/g' tmpA >& tmpB sed -e 's/@eday@/'${ed:6:2}'/g' tmpB >& tmpA sed -e 's/@ehour@/'${ed:8:2}'/g' tmpA >& tmpB sed -e 's/@eminute@/'${ed:10:2}'/g' tmpB >& tmpA sed -e 's/@esecond@/'${ed:12:2}'/g' tmpA >& tmpB sed -e 's/@Nouts@/'${Nouts}'/g' tmpB >& tmpA sed -e 's/@freqH@/'${frH}'/g' tmpA >& tmpB if test ${it} -gt 1; then sed -e 's/@restart@/.true./g' tmpB >& namelist.input else sed -e 's/@restart@/.false./g' tmpB >& namelist.input fi rm tmpA tmpB } ######## ####### ## MAIN ####### rootsh=`pwd` errmsg='ERROR -- error -- ERROR -- error' inidate=$1 enddate=$2 freq=$3 odir=$4 if test $# -eq 5; then restart=$5 else restart='false' fi wrftmplt=${odir}'/namelist.input.template' ####### ###### #### ### ## # export PATH=/u/lflmd/bin/gcc_Python-2.7.5/bin:${PATH} export LD_LIBRARY_PATH=/u/lflmd/bin/gcc_netcdf-4.3.0/lib:/opt/intel/composer_xe_2011_sp1.9.293/compiler/lib/intel64:/opt/intel/composer_xe_2011_sp1.9.293/compiler/lib/intel64:/opt/intel/composer_xe_2011_sp1.9.293/mkl/lib/intel64:/opt/intel/composer_xe_2011_sp1.9.293/tbb/lib/intel64//cc4.1.0_libc2.4_kernel2.6.16.21:/usr/local/lib:/opt/intel/composer_xe_2011_sp1.9.293/debugger/lib/intel64:/opt/intel/composer_xe_2011_sp1.9.293/mpirt/lib/intel64 if test ! -f ${wrftmplt}; then echo $errmsg echo "File '"${wrftmplt}"' does not exist!!!!" exit fi cd ${odir} rm namelist.input >& /dev/null idate=${inidate} isubperiod=1 if test ${restart} == 'true'; then echo "Continuing the simulation from the last wrfrst" else rm -r ${odir}/wrfout rm run_wrf_??????????????-??????????????.log mkdir -p ${odir}/wrfout fi while test ${idate} -le ${enddate}; do if test ${freq} = 'month'; then nextdate=`date +%Y%m%d%H%M%S -d"${idate:0:8} 1 month"` secmonths=`seconds_between_dates ${idate} ${nextdate}` freqV=`expr ${secmonths} / 24 / 3600` else nextdate=`date +%Y%m%d%H%M%S -d"${idate:0:8} ${freq} days"` freqV=${freq} fi if test ${nextdate} -gt ${enddate}; then break; fi period=${idate}'-'${nextdate} # Checking for restart ## runperiod=true if test ${restart} == 'true' && test -f ${odir}/wrfout/wrfrst_d01_${nextdate:0:4}-${nextdate:4:2}-${nextdate:6:2}_${nextdate:8:2}:${nextdate:10:2}:${nextdate:12:2}; then runperiod=false else echo " resuming the simulation from: "${idate:0:4}-${idate:4:2}-${idate:6:2}_${idate:8:2}:${idate:10:2}:${idate:12:2} cp ${odir}/wrfout/wrfrst_d01_${idate:0:4}-${idate:4:2}-${idate:6:2}_${idate:8:2}:${idate:10:2}:${idate:12:2} ${odir} fi if ${runperiod}; then echo " "${isubperiod}" "${period} create_WRFnamelist ${wrftmplt} ${idate} ${nextdate} ${freqV} ${isubperiod} ./wrf.exe >& ${odir}/run_wrf_${period}.log ewrf=$? if test ${ewrf} -ne 0; then echo ${errmsg} echo " wrf.exe failed !!!" cat run_wrf_${period}.log exit fi # Moving outputs ## mv namelist.input ${odir}/wrfout/namelist.input_${period} mv namelist.output ${odir}/wrfout/namelist.output_${period} mv wrfout_d* ${odir}/wrfout mv run_wrf_${period}.log ${odir}/wrfout if test ${isubperiod} -gt 1; then mv wrfrst*${idate:0:4}-${idate:4:2}-${idate:6:2}* ${odir}/wrfout fi fi lastdate=${idate} idate=${nextdate} isubperiod=`expr ${isubperiod} + 1` # end of dates done isubperiod=`expr ${isubperiod} + 1` if test ${nextdate} -gt ${enddate} && test ${idate} -ne ${enddate}; then nextdate=${enddate} period=${idate}'-'${nextdate} echo "last: "${period} secmonths=`seconds_between_dates ${idate} ${nextdate}` freqV=`expr ${secmonths} / 24 / 3600` create_WRFnamelist ${wrftmplt} ${idate} ${nextdate} ${freqV} ${isubperiod} ./wrf.exe >& ${odir}/run_wrf_${period}.log ewrf=$? if test ${ewrf} -ne 0; then echo ${errmsg} echo " wrf.exe failed !!!" cat run_wrf_${period}.log exit fi # Moving outputs ## mv namelist.input ${odir}/wrfout/namelist.input_${period} mv namelist.output ${odir}/wrfout/namelist.output_${period} mv wrfout_d* ${odir}/wrfout mv run_wrf_${period}.log ${odir}/wrfout if test ${isubperiod} -gt 1; then mv wrfrst*${idate:0:4}-${idate:4:2}-${idate:6:2}* ${odir}/wrfout fi fi fi