| 1 | #!/bin/bash |
|---|
| 2 | ## g.e. # WRF+LMDZ_launch.bash 19790101000000 19800101000000 28 /d4/lflmd/etudes/WRF_LMDZ/test_phylmd/AR40.0/control |
|---|
| 3 | ## g.e. # WRF+LMDZ_launch.bash 19790101000000 19800101000000 month /d4/lflmd/etudes/WRF_LMDZ/test_phylmd/AR40.0/control |
|---|
| 4 | if test $1 = '-h'; then |
|---|
| 5 | echo "*************************************" |
|---|
| 6 | echo "*** Launching WRF+LMDZ simulation ***" |
|---|
| 7 | echo "*************************************" |
|---|
| 8 | echo "WRF+LMDZ_launch.bash [inidate] [enddate] [freq] [odir]" |
|---|
| 9 | echo " [inidate]: initial date [YYYY][MM][DD][HH][MI][SS]" |
|---|
| 10 | echo " [enddate]: final date [YYYY][MM][DD][HH][MI][SS]" |
|---|
| 11 | echo " [freq]: frequency restarts [DD] if it is 'month' real month frequency will be used" |
|---|
| 12 | echo " [odir]: folder for the outputs" |
|---|
| 13 | echo " [restart]: 'true' for a resume a simulation" |
|---|
| 14 | else |
|---|
| 15 | |
|---|
| 16 | checking_date() { |
|---|
| 17 | # Function to check date (in [YYYY][MM][DD][HH][MI][SS] format) |
|---|
| 18 | fname='checking_date' |
|---|
| 19 | |
|---|
| 20 | errfunc='ERROR -- error -- ERROR -- error' |
|---|
| 21 | |
|---|
| 22 | datev=$1 |
|---|
| 23 | |
|---|
| 24 | Ldate=`expr length ${datev}` |
|---|
| 25 | if test ${Ldate} -ne 14; then |
|---|
| 26 | echo ${errfunc} |
|---|
| 27 | echo " "${fname}": date '"${datev}"' does not have the appropriated length: " \ |
|---|
| 28 | ${Ldate}" should be: 14 !!!!" |
|---|
| 29 | exit |
|---|
| 30 | fi |
|---|
| 31 | |
|---|
| 32 | yr=${datev:0:4} |
|---|
| 33 | mon=${datev:4:2} |
|---|
| 34 | day=${datev:6:2} |
|---|
| 35 | YMD1=${datev:0:8} |
|---|
| 36 | hour=${datev:8:2} |
|---|
| 37 | min=${datev:10:2} |
|---|
| 38 | sec=${datev:12:2} |
|---|
| 39 | |
|---|
| 40 | datefmt=${yr}'/'${mon}'/'${day}' '${hour}':'${min}':'${sec} |
|---|
| 41 | # Checking individually |
|---|
| 42 | # |
|---|
| 43 | # yr |
|---|
| 44 | chkyr=`date -d"${yr}0101"` |
|---|
| 45 | if test $? -ne 0; then |
|---|
| 46 | echo ${errfunc} |
|---|
| 47 | echo " "${fname}": "${datefmt}" wrong year '"${yr}"' !!!!" |
|---|
| 48 | exit |
|---|
| 49 | fi |
|---|
| 50 | # mon |
|---|
| 51 | chkyr=`date -d"${yr}${mon}01"` |
|---|
| 52 | if test $? -ne 0; then |
|---|
| 53 | echo ${errfunc} |
|---|
| 54 | echo " "${fname}": "${datefmt}" wrong month '"${mon}"' !!!!" |
|---|
| 55 | exit |
|---|
| 56 | fi |
|---|
| 57 | # day |
|---|
| 58 | chkyr=`date -d"${yr}${mon}${day}"` |
|---|
| 59 | if test $? -ne 0; then |
|---|
| 60 | echo ${errfunc} |
|---|
| 61 | echo " "${fname}": "${datefmt}" wrong day '"${day}"' !!!!" |
|---|
| 62 | exit |
|---|
| 63 | fi |
|---|
| 64 | # hour |
|---|
| 65 | if test ${hour} -gt 23; then |
|---|
| 66 | echo ${errfunc} |
|---|
| 67 | echo " "${fname}": "${datefmt}" wrong hour '"${hour}"' !!!!" |
|---|
| 68 | exit |
|---|
| 69 | fi |
|---|
| 70 | # minute |
|---|
| 71 | if test ${min} -gt 59; then |
|---|
| 72 | echo ${errfunc} |
|---|
| 73 | echo " "${fname}": "${datefmt}" wrong minute '"${min}"' !!!!" |
|---|
| 74 | exit |
|---|
| 75 | fi |
|---|
| 76 | # second |
|---|
| 77 | if test ${sec} -gt 59; then |
|---|
| 78 | echo ${errfunc} |
|---|
| 79 | echo " "${fname}": "${datefmt}" wrong second '"${sec}"' !!!!" |
|---|
| 80 | exit |
|---|
| 81 | fi |
|---|
| 82 | # echo ${datefmt} |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | seconds_between_dates() { |
|---|
| 86 | # Functino to provide the seconds between two dates |
|---|
| 87 | date1=$1 |
|---|
| 88 | date2=$2 |
|---|
| 89 | |
|---|
| 90 | # Date1 |
|---|
| 91 | ## |
|---|
| 92 | YMD1=${date1:0:8} |
|---|
| 93 | hours1=${date1:8:2} |
|---|
| 94 | minutes1=${date1:10:2} |
|---|
| 95 | seconds1=${date1:12:2} |
|---|
| 96 | hours1=`(expr $hours1 '*' 3600)` |
|---|
| 97 | minutes1=`(expr $minutes1 '*' 60)` |
|---|
| 98 | seconds1=`(expr $hours1 + $minutes1 + $seconds1)` |
|---|
| 99 | sec1=`(date +%s -u -d"$YMD1 $seconds1 seconds")` |
|---|
| 100 | |
|---|
| 101 | # Date2 |
|---|
| 102 | ## |
|---|
| 103 | YMD2=${date2:0:8} |
|---|
| 104 | hours2=${date2:8:2} |
|---|
| 105 | minutes2=${date2:10:2} |
|---|
| 106 | seconds2=${date2:12:2} |
|---|
| 107 | hours2=`(expr $hours2 '*' 3600)` |
|---|
| 108 | minutes2=`(expr $minutes2 '*' 60)` |
|---|
| 109 | seconds2=`(expr $hours2 + $minutes2 + $seconds2)` |
|---|
| 110 | sec2=`(date +%s -u -d"$YMD2 $seconds2 seconds")` |
|---|
| 111 | seconds=`(expr $sec2 - $sec1)` |
|---|
| 112 | |
|---|
| 113 | echo $seconds |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | create_WRFnamelist() { |
|---|
| 117 | # Function to create namelist.input from a nameslit WRF template |
|---|
| 118 | # nlistmplt: template of the namelist.input |
|---|
| 119 | # id: initial date ([YYYYMMDDHHMISS] format) |
|---|
| 120 | # ed: final date ([YYYYMMDDHHMISS] format) |
|---|
| 121 | # fr: frquency (in days) of the restarts |
|---|
| 122 | # it: number of sub-period within the simulation experiment |
|---|
| 123 | |
|---|
| 124 | nlisttmplt=$1 |
|---|
| 125 | id=$2 |
|---|
| 126 | ed=$3 |
|---|
| 127 | fr=$4 |
|---|
| 128 | it=$5 |
|---|
| 129 | |
|---|
| 130 | frH=`expr ${fr} '*' 24` |
|---|
| 131 | |
|---|
| 132 | dThist=`cat ${nlisttmplt} | grep history_interval | awk '{print $3}' | tr ',' ' ' |\ |
|---|
| 133 | awk '{print $1*60}'` |
|---|
| 134 | |
|---|
| 135 | secsPeriod=`seconds_between_dates ${id} ${ed}` |
|---|
| 136 | Nouts=`expr ${secsPeriod} / ${dThist}` |
|---|
| 137 | |
|---|
| 138 | cp ${nlisttmplt} namelist.input |
|---|
| 139 | sed -e 's/@iyear@/'${id:0:4}'/g' namelist.input >& tmpA |
|---|
| 140 | sed -e 's/@imonth@/'${id:4:2}'/g' tmpA >& tmpB |
|---|
| 141 | sed -e 's/@iday@/'${id:6:2}'/g' tmpB >& tmpA |
|---|
| 142 | sed -e 's/@ihour@/'${id:8:2}'/g' tmpA >& tmpB |
|---|
| 143 | sed -e 's/@iminute@/'${id:10:2}'/g' tmpB >& tmpA |
|---|
| 144 | sed -e 's/@isecond@/'${id:12:2}'/g' tmpA >& tmpB |
|---|
| 145 | |
|---|
| 146 | sed -e 's/@eyear@/'${ed:0:4}'/g' tmpB >& tmpA |
|---|
| 147 | sed -e 's/@emonth@/'${ed:4:2}'/g' tmpA >& tmpB |
|---|
| 148 | sed -e 's/@eday@/'${ed:6:2}'/g' tmpB >& tmpA |
|---|
| 149 | sed -e 's/@ehour@/'${ed:8:2}'/g' tmpA >& tmpB |
|---|
| 150 | sed -e 's/@eminute@/'${ed:10:2}'/g' tmpB >& tmpA |
|---|
| 151 | sed -e 's/@esecond@/'${ed:12:2}'/g' tmpA >& tmpB |
|---|
| 152 | sed -e 's/@Nouts@/'${Nouts}'/g' tmpB >& tmpA |
|---|
| 153 | sed -e 's/@freqH@/'${frH}'/g' tmpA >& tmpB |
|---|
| 154 | if test ${it} -gt 1; then |
|---|
| 155 | sed -e 's/@restart@/.true./g' tmpB >& namelist.input |
|---|
| 156 | else |
|---|
| 157 | sed -e 's/@restart@/.false./g' tmpB >& namelist.input |
|---|
| 158 | fi |
|---|
| 159 | |
|---|
| 160 | rm tmpA tmpB |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | create_LMDZnamelist() { |
|---|
| 164 | # Function to create run.def from a LMDZ template |
|---|
| 165 | # nlistmplt: template of the run.def |
|---|
| 166 | # id: initial date (in [YYYY][MM][DD][HH][MI][SS] format) |
|---|
| 167 | # fr: frquency (in days) of the restarts |
|---|
| 168 | |
|---|
| 169 | nlisttmplt=$1 |
|---|
| 170 | id=$2 |
|---|
| 171 | fr=$3 |
|---|
| 172 | |
|---|
| 173 | # dd=`echo ${id:6:2} | awk '{print $1 + 0}'` |
|---|
| 174 | # day=`printf %d ${dd}` |
|---|
| 175 | |
|---|
| 176 | day=`date -u "+%j" -d"${id:0:8}"` |
|---|
| 177 | |
|---|
| 178 | cp ${nlisttmplt} run.def |
|---|
| 179 | sed -e 's/@freq@/'${fr}'/g' run.def >& tmpA |
|---|
| 180 | sed -e 's/@iyear@/'${id:0:4}'/g' tmpA >& tmpB |
|---|
| 181 | sed -e 's/@iday@/'${day}'/g' tmpB >& run.def |
|---|
| 182 | |
|---|
| 183 | cp config.def.template tmpA |
|---|
| 184 | sed -e 's/@daysmonth@/'${fr}'/g' tmpA >& config.def |
|---|
| 185 | |
|---|
| 186 | rm tmpA tmpB |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | ######## ####### |
|---|
| 190 | ## MAIN |
|---|
| 191 | ####### |
|---|
| 192 | |
|---|
| 193 | rootsh=`pwd` |
|---|
| 194 | errmsg='ERROR -- error -- ERROR -- error' |
|---|
| 195 | |
|---|
| 196 | inidate=$1 |
|---|
| 197 | enddate=$2 |
|---|
| 198 | freq=$3 |
|---|
| 199 | odir=$4 |
|---|
| 200 | if test $# -eq 5; then |
|---|
| 201 | restart=$5 |
|---|
| 202 | else |
|---|
| 203 | restart='false' |
|---|
| 204 | fi |
|---|
| 205 | |
|---|
| 206 | wrftmplt=${odir}'/namelist.input.template' |
|---|
| 207 | lmdztmplt=${odir}'/run.def.template' |
|---|
| 208 | |
|---|
| 209 | ####### ###### #### ### ## # |
|---|
| 210 | |
|---|
| 211 | export PATH=/u/lflmd/bin/gcc_Python-2.7.5/bin:${PATH} |
|---|
| 212 | 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 |
|---|
| 213 | |
|---|
| 214 | if test ! -f ${wrftmplt}; then |
|---|
| 215 | echo $errmsg |
|---|
| 216 | echo "File '"${wrftmplt}"' does not exist!!!!" |
|---|
| 217 | exit |
|---|
| 218 | fi |
|---|
| 219 | |
|---|
| 220 | if test ! -f ${lmdztmplt}; then |
|---|
| 221 | echo $errmsg |
|---|
| 222 | echo "File '"${lmdztmplt}"' does not exist!!!!" |
|---|
| 223 | exit |
|---|
| 224 | fi |
|---|
| 225 | |
|---|
| 226 | idate=${inidate} |
|---|
| 227 | if test ${restart} = 'true'; then |
|---|
| 228 | isubperiod=2 |
|---|
| 229 | else |
|---|
| 230 | isubperiod=1 |
|---|
| 231 | fi |
|---|
| 232 | |
|---|
| 233 | checking_date ${idate} |
|---|
| 234 | checking_date ${enddate} |
|---|
| 235 | |
|---|
| 236 | if test ${restart} = 'true'; then |
|---|
| 237 | echo "Continuing the simulation from the last wrfrst" |
|---|
| 238 | else |
|---|
| 239 | rm -r ${odir}/wrfout |
|---|
| 240 | rm -r ${odir}/lmdzout |
|---|
| 241 | rm run_wrf_??????????????-??????????????.log |
|---|
| 242 | mkdir -p ${odir}/wrfout |
|---|
| 243 | mkdir -p ${odir}/lmdzout |
|---|
| 244 | fi |
|---|
| 245 | |
|---|
| 246 | while test ${idate} -le ${enddate}; do |
|---|
| 247 | if test ${freq} = 'month'; then |
|---|
| 248 | nextdate=`date +%Y%m%d%H%M%S -d"${idate:0:8} 1 month"` |
|---|
| 249 | secmonths=`seconds_between_dates ${idate} ${nextdate}` |
|---|
| 250 | freqV=`expr ${secmonths} / 24 / 3600` |
|---|
| 251 | else |
|---|
| 252 | nextdate=`date +%Y%m%d%H%M%S -d"${idate:0:8} ${freq} days"` |
|---|
| 253 | freqV=${freq} |
|---|
| 254 | fi |
|---|
| 255 | if test ${nextdate} -gt ${enddate}; then break; fi |
|---|
| 256 | period=${idate}'-'${nextdate} |
|---|
| 257 | # Checking for restart |
|---|
| 258 | ## |
|---|
| 259 | runperiod=true |
|---|
| 260 | 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 |
|---|
| 261 | runperiod=false |
|---|
| 262 | else |
|---|
| 263 | runperiod=true |
|---|
| 264 | cp ${odir}/wrfout/wrfrst_d01_${idate:0:4}-${idate:4:2}-${idate:6:2}_${idate:8:2}:${idate:10:2}:${idate:12:2} ${odir} |
|---|
| 265 | echo " resuming the simulation from: "${idate:0:4}-${idate:4:2}-${idate:6:2}_${idate:8:2}:${idate:10:2}:${idate:12:2} |
|---|
| 266 | fi |
|---|
| 267 | |
|---|
| 268 | if ${runperiod}; then |
|---|
| 269 | echo " "${isubperiod}" "${period} |
|---|
| 270 | |
|---|
| 271 | create_WRFnamelist ${wrftmplt} ${idate} ${nextdate} ${freqV} ${isubperiod} |
|---|
| 272 | create_LMDZnamelist ${lmdztmplt} ${idate} ${freqV} |
|---|
| 273 | |
|---|
| 274 | ./wrf.exe >& ${odir}/run_wrf_${period}.log |
|---|
| 275 | ewrf=$? |
|---|
| 276 | |
|---|
| 277 | if test ${ewrf} -ne 0; then |
|---|
| 278 | echo ${errmsg} |
|---|
| 279 | echo " wrf.exe failed !!!" |
|---|
| 280 | cat run_wrf_${period}.log |
|---|
| 281 | exit |
|---|
| 282 | fi |
|---|
| 283 | # Moving outputs |
|---|
| 284 | ## |
|---|
| 285 | mv namelist.input ${odir}/wrfout/namelist.input_${period} |
|---|
| 286 | mv namelist.output ${odir}/wrfout/namelist.output_${period} |
|---|
| 287 | mv wrfout_d* ${odir}/wrfout |
|---|
| 288 | mv run_wrf_${period}.log ${odir}/wrfout |
|---|
| 289 | mv run.def ${odir}/lmdzout/run.def_${period} |
|---|
| 290 | mv histmth.nc ${odir}/lmdzout/histmth_${period}.nc |
|---|
| 291 | mv histday.nc ${odir}/lmdzout/histday_${period}.nc |
|---|
| 292 | mv histins.nc ${odir}/lmdzout/histins_${period}.nc |
|---|
| 293 | |
|---|
| 294 | if test ${isubperiod} -gt 1; then |
|---|
| 295 | mv wrfrst*${idate:0:4}-${idate:4:2}-${idate:6:2}* ${odir}/wrfout |
|---|
| 296 | fi |
|---|
| 297 | fi |
|---|
| 298 | |
|---|
| 299 | lastdate=${idate} |
|---|
| 300 | idate=${nextdate} |
|---|
| 301 | isubperiod=`expr ${isubperiod} + 1` |
|---|
| 302 | |
|---|
| 303 | # end of dates |
|---|
| 304 | done |
|---|
| 305 | |
|---|
| 306 | isubperiod=`expr ${isubperiod} + 1` |
|---|
| 307 | if test ${nextdate} -gt ${enddate} && test ${idate} -ne ${enddate}; then |
|---|
| 308 | nextdate=${enddate} |
|---|
| 309 | period=${idate}'-'${nextdate} |
|---|
| 310 | echo "last: "${period} |
|---|
| 311 | secmonths=`seconds_between_dates ${idate} ${nextdate}` |
|---|
| 312 | freqV=`expr ${secmonths} / 24 / 3600` |
|---|
| 313 | create_WRFnamelist ${wrftmplt} ${idate} ${nextdate} ${freqV} ${isubperiod} |
|---|
| 314 | create_LMDZnamelist ${lmdztmplt} ${idate} ${freqV} |
|---|
| 315 | |
|---|
| 316 | ./wrf.exe >& ${odir}/run_wrf_${period}.log |
|---|
| 317 | ewrf=$? |
|---|
| 318 | if test ${ewrf} -ne 0; then |
|---|
| 319 | echo ${errmsg} |
|---|
| 320 | echo " wrf.exe failed !!!" |
|---|
| 321 | cat run_wrf_${period}.log |
|---|
| 322 | exit |
|---|
| 323 | fi |
|---|
| 324 | # Moving outputs |
|---|
| 325 | ## |
|---|
| 326 | mv namelist.input ${odir}/wrfout/namelist.input_${period} |
|---|
| 327 | mv namelist.output ${odir}/wrfout/namelist.output_${period} |
|---|
| 328 | mv wrfout_d* ${odir}/wrfout |
|---|
| 329 | mv run_wrf_${period}.log ${odir}/wrfout |
|---|
| 330 | mv run.def ${odir}/lmdzout/run.def_${period} |
|---|
| 331 | mv histmth.nc ${odir}/lmdzout/histmth_${period}.nc |
|---|
| 332 | mv histday.nc ${odir}/lmdzout/histday_${period}.nc |
|---|
| 333 | mv histins.nc ${odir}/lmdzout/histins_${period}.nc |
|---|
| 334 | |
|---|
| 335 | if test ${isubperiod} -gt 1; then |
|---|
| 336 | mv wrfrst*${idate:0:4}-${idate:4:2}-${idate:6:2}* ${odir}/wrfout |
|---|
| 337 | fi |
|---|
| 338 | fi |
|---|
| 339 | |
|---|
| 340 | # Post-process LMDZ hist files |
|---|
| 341 | ## |
|---|
| 342 | |
|---|
| 343 | dimx=`cat ${odir}/wrfout/namelist.input_${period} | grep e_we | awk '{print $3}' | tr ',' ' ' | awk '{print $1 - 1}'` |
|---|
| 344 | dimy=`cat ${odir}/wrfout/namelist.input_${period} | grep e_sn | awk '{print $3}' | tr ',' ' ' | awk '{print $1 - 1}'` |
|---|
| 345 | for file in ${odir}/lmdzout/hist*.nc; do |
|---|
| 346 | filen=`basename ${file}` |
|---|
| 347 | filenH=`echo ${filen} | tr '.' ' ' | awk '{print $1}'` |
|---|
| 348 | python ${HOME}/PY/WRFLMDZ_regularout.py -f ${file} -d ${dimx},${dimy} -o ${odir}/lmdzout/${filenH}_reg.nc |
|---|
| 349 | rm ${file} |
|---|
| 350 | d1=`echo ${filen} | tr '_' ' ' | awk '{print $2}' | tr '-' ' ' | awk '{print $1}'` |
|---|
| 351 | totsecs=`seconds_between_dates 19491201000000 ${d1}` |
|---|
| 352 | |
|---|
| 353 | python ${HOME}/climatescripts/python/nc_var.py -f ${odir}/lmdzout/${filenH}_reg.nc -o valmod -S sumc,${totsecs} -v time |
|---|
| 354 | python ${HOME}/climatescripts/python/nc_var.py -f ${odir}/lmdzout/${filenH}_reg.nc -o addvattrk -S 'units|seconds!since!1949-12-01!00:00:00|S' -v time |
|---|
| 355 | done |
|---|
| 356 | |
|---|
| 357 | fi |
|---|