| 1 | #!/bin/bash |
|---|
| 2 | ######################################################################## |
|---|
| 3 | ######## Library of bash functions for the PEM launching script ######## |
|---|
| 4 | ######################################################################## |
|---|
| 5 | |
|---|
| 6 | # To end the launching script |
|---|
| 7 | endlaunch() { |
|---|
| 8 | # Restore the previous value of LC_NUMERIC |
|---|
| 9 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 10 | |
|---|
| 11 | date |
|---|
| 12 | echo "Success: the launching script for the PEM simulation completed normally!" |
|---|
| 13 | exit 0 |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | # To end the launching script with error |
|---|
| 17 | errlaunch() { |
|---|
| 18 | # Restore the previous value of LC_NUMERIC |
|---|
| 19 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 20 | |
|---|
| 21 | date |
|---|
| 22 | echo "Error: an issue occured in the launching script for the PEM simulation!" |
|---|
| 23 | exit 1 |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | # To check what is the job scheduler |
|---|
| 27 | function job_scheduler() { |
|---|
| 28 | if command -v squeue &> /dev/null; then |
|---|
| 29 | echo "SLURM is installed on $machine." |
|---|
| 30 | scheduler="SLURM" |
|---|
| 31 | name_job="#SBATCH --job-name=" |
|---|
| 32 | kill_job="scancel" |
|---|
| 33 | elif command -v qstat &> /dev/null; then |
|---|
| 34 | echo "PBS/TORQUE is installed on $machine." |
|---|
| 35 | scheduler="PBS" |
|---|
| 36 | name_job="#PBS -N " |
|---|
| 37 | kill_job="qdel" |
|---|
| 38 | else |
|---|
| 39 | echo "Error: neither SLURM nor TORQUE/PBS is installed on $machine!" |
|---|
| 40 | echo "You need to adapt the script to your job scheduler or set 'mode' to 0." |
|---|
| 41 | errlaunch |
|---|
| 42 | fi |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # To get the number of slopes for the simulation |
|---|
| 46 | get_nslope() { |
|---|
| 47 | ns=1 |
|---|
| 48 | if [ -f "startfi.nc" ]; then |
|---|
| 49 | ns=$(ncdump -h startfi.nc | sed -n 's/.*nslope = \([0-9]*\) ;.*/\1/p') |
|---|
| 50 | else |
|---|
| 51 | for f in run_PCM.def callphys.def; do |
|---|
| 52 | if [[ -f "$f" ]]; then |
|---|
| 53 | while IFS= read -r line; do |
|---|
| 54 | # Remove leading whitespace |
|---|
| 55 | trimmed=$(echo "$line" | sed 's/^[[:space:]]*//') |
|---|
| 56 | # Skip lines that are commented out |
|---|
| 57 | if [[ "$trimmed" == \#* ]]; then |
|---|
| 58 | continue |
|---|
| 59 | fi |
|---|
| 60 | # Check if line contains 'nslope = N' |
|---|
| 61 | if [[ "$trimmed" =~ ^nslope[[:space:]]*=[[:space:]]*([0-9]+) ]]; then |
|---|
| 62 | ns="${BASH_REMATCH[1]}" |
|---|
| 63 | break |
|---|
| 64 | fi |
|---|
| 65 | done < "$f" |
|---|
| 66 | [[ -n "$ns" ]] && break |
|---|
| 67 | fi |
|---|
| 68 | done |
|---|
| 69 | fi |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | # To modify the xml file according nslope |
|---|
| 73 | modify_xml() { |
|---|
| 74 | tmp="tmp_file_def.xml" |
|---|
| 75 | in_outdaily4pem=false |
|---|
| 76 | in_outyearly4pem=false |
|---|
| 77 | in_outdaily4pem_s=false |
|---|
| 78 | in_outyearly4pem_s=false |
|---|
| 79 | |
|---|
| 80 | sed -i 's/enabled="\.true\.\">/enabled=".false.">/g' file_def_physics_mars.xml |
|---|
| 81 | |
|---|
| 82 | while IFS= read -r line; do |
|---|
| 83 | # Detect file blocks |
|---|
| 84 | case "$line" in |
|---|
| 85 | *'<file id="outdaily4pem"'*) |
|---|
| 86 | in_outdaily4pem=true |
|---|
| 87 | ;; |
|---|
| 88 | *'<file id="outyearly4pem"'*) |
|---|
| 89 | in_outyearly4pem=true |
|---|
| 90 | ;; |
|---|
| 91 | *'<file id="outdaily4pem_s"'*) |
|---|
| 92 | in_outdaily4pem_s=true |
|---|
| 93 | ;; |
|---|
| 94 | *'<file id="outyearly4pem_s"'*) |
|---|
| 95 | in_outyearly4pem_s=true |
|---|
| 96 | ;; |
|---|
| 97 | esac |
|---|
| 98 | |
|---|
| 99 | # Handle enabled attribute |
|---|
| 100 | if [[ $line == *'enabled="'* ]]; then |
|---|
| 101 | if $in_outdaily4pem || $in_outyearly4pem; then |
|---|
| 102 | if [[ $ns -eq 1 ]]; then |
|---|
| 103 | line=' enabled=".true.">' |
|---|
| 104 | else |
|---|
| 105 | line=' enabled=".false.">' |
|---|
| 106 | fi |
|---|
| 107 | elif $in_outdaily4pem_s || $in_outyearly4pem_s; then |
|---|
| 108 | if [[ $ns -eq 1 ]]; then |
|---|
| 109 | line=' enabled=".false.">' |
|---|
| 110 | else |
|---|
| 111 | line=' enabled=".true.">' |
|---|
| 112 | fi |
|---|
| 113 | fi |
|---|
| 114 | fi |
|---|
| 115 | |
|---|
| 116 | # Handle slope variables |
|---|
| 117 | if ( $in_outdaily4pem_s || $in_outyearly4pem_s ) && [[ $line =~ slope([0-9]+) ]]; then |
|---|
| 118 | slope_id="${BASH_REMATCH[1]}" |
|---|
| 119 | if (( 10#$slope_id > ns )); then |
|---|
| 120 | # Ensure the line is commented |
|---|
| 121 | if [[ $line != "<!--"* ]]; then |
|---|
| 122 | line="<!-- $line -->" |
|---|
| 123 | fi |
|---|
| 124 | else |
|---|
| 125 | # Ensure the line is uncommented |
|---|
| 126 | if [[ $line == "<!--"* ]]; then |
|---|
| 127 | line="${line#<!-- }" # remove leading <!-- |
|---|
| 128 | line="${line% -->}" # remove trailing --> |
|---|
| 129 | fi |
|---|
| 130 | fi |
|---|
| 131 | fi |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | # Leaving the file block |
|---|
| 135 | case "$line" in |
|---|
| 136 | *'</file>'*) |
|---|
| 137 | in_outdaily4pem=false |
|---|
| 138 | in_outyearly4pem=false |
|---|
| 139 | in_outdaily4pem_s=false |
|---|
| 140 | in_outyearly4pem_s=false |
|---|
| 141 | ;; |
|---|
| 142 | esac |
|---|
| 143 | |
|---|
| 144 | echo "$line" >> "$tmp" |
|---|
| 145 | done < file_def_physics_mars.xml |
|---|
| 146 | |
|---|
| 147 | mv "$tmp" file_def_physics_mars.xml |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | # To check if a PCM run is one year |
|---|
| 151 | check_runyear() { |
|---|
| 152 | if [ -f "run_PCM.def" ]; then |
|---|
| 153 | year_sol=$(ncdump -v controle startfi.nc 2>/dev/null | \ |
|---|
| 154 | sed -n '/controle =/,/;/p' | tr -d '[:space:]' | \ |
|---|
| 155 | sed 's/.*=//; s/;//' | tr ',' '\n' | sed -n '14p') |
|---|
| 156 | else |
|---|
| 157 | year_sol=669 # Length of Martian year (sols) |
|---|
| 158 | fi |
|---|
| 159 | sol_in_file=$(awk -F'=' '/^[[:space:]]*(nday|ndt)[[:space:]]*=/ { |
|---|
| 160 | val=$2 |
|---|
| 161 | gsub(/^[[:space:]]+|[[:space:]]+$/,"",val) |
|---|
| 162 | print val |
|---|
| 163 | exit |
|---|
| 164 | }' run_PCM.def) |
|---|
| 165 | |
|---|
| 166 | if [ -z "$sol_in_file" ]; then |
|---|
| 167 | echo "Error: no length of year found in \"run_PCM.def\"!" |
|---|
| 168 | errlaunch |
|---|
| 169 | elif [ "$sol_in_file" -eq "$year_sol" ]; then |
|---|
| 170 | # Good: we do nothing |
|---|
| 171 | : |
|---|
| 172 | else |
|---|
| 173 | echo "Error: length of year mismatch between \"run_PCM.def\" ($sol_in_file) and \"startfi.nc\" ($year_sol)!" |
|---|
| 174 | errlaunch |
|---|
| 175 | fi |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | # To check if everything necessary for the launching script is ok |
|---|
| 179 | checklaunch() { |
|---|
| 180 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 181 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 182 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 183 | |
|---|
| 184 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| 185 | if [ $(echo "$n_mars_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 186 | echo "Error: 'n_mars_years' must be > 0!" |
|---|
| 187 | errlaunch |
|---|
| 188 | fi |
|---|
| 189 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 190 | if [ $(echo "$n_earth_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 191 | echo "Error: 'n_earth_years' must be > 0!" |
|---|
| 192 | errlaunch |
|---|
| 193 | fi |
|---|
| 194 | else |
|---|
| 195 | echo "Error: the number of years to be simulated is not set!" |
|---|
| 196 | errlaunch |
|---|
| 197 | fi |
|---|
| 198 | if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then |
|---|
| 199 | echo "Error: 'nPCM_ini' must be >= 2!" |
|---|
| 200 | errlaunch |
|---|
| 201 | fi |
|---|
| 202 | if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then |
|---|
| 203 | echo "Error: 'nPCM' must be >= 2!" |
|---|
| 204 | errlaunch |
|---|
| 205 | fi |
|---|
| 206 | if [ ! -f "PCMrun.job" ]; then |
|---|
| 207 | echo "Error: file \"PCMrun.job\" does not exist in $dir!" |
|---|
| 208 | errlaunch |
|---|
| 209 | fi |
|---|
| 210 | if [ ! -f "PEMrun.job" ]; then |
|---|
| 211 | echo "Error: file \"PEMrun.job\" does not exist in $dir!" |
|---|
| 212 | errlaunch |
|---|
| 213 | fi |
|---|
| 214 | if [ ! -f "run_PCM.def" ]; then |
|---|
| 215 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
|---|
| 216 | errlaunch |
|---|
| 217 | fi |
|---|
| 218 | if [ ! -f "run_PEM.def" ]; then |
|---|
| 219 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
|---|
| 220 | errlaunch |
|---|
| 221 | fi |
|---|
| 222 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 223 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 224 | errlaunch |
|---|
| 225 | fi |
|---|
| 226 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 227 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 228 | errlaunch |
|---|
| 229 | fi |
|---|
| 230 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 231 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 232 | errlaunch |
|---|
| 233 | fi |
|---|
| 234 | if [ ! -f "iodef.xml" ]; then |
|---|
| 235 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 236 | errlaunch |
|---|
| 237 | fi |
|---|
| 238 | if [ ! -d "logs" ]; then |
|---|
| 239 | mkdir logs |
|---|
| 240 | fi |
|---|
| 241 | if [ ! -d "starts" ]; then |
|---|
| 242 | mkdir starts |
|---|
| 243 | fi |
|---|
| 244 | if [ ! -d "diags" ]; then |
|---|
| 245 | mkdir diags |
|---|
| 246 | fi |
|---|
| 247 | if [ $mode -ne 0 ]; then |
|---|
| 248 | job_scheduler |
|---|
| 249 | fi |
|---|
| 250 | # Set automatically the XIOS output file for the PEM according to the number of slopes |
|---|
| 251 | get_nslope |
|---|
| 252 | modify_xml |
|---|
| 253 | # Check if a PCM run is one year |
|---|
| 254 | check_runyear |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | # To convert Earth years into Mars years |
|---|
| 258 | convertyears() { |
|---|
| 259 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 260 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 261 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 262 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 263 | if [ -v n_mars_years ]; then |
|---|
| 264 | n_myear=$n_mars_years |
|---|
| 265 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 266 | elif [ -v n_earth_years ]; then |
|---|
| 267 | n_myear=$(echo "$n_earth_years/$convert_years" | bc -l) |
|---|
| 268 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 269 | fi |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | # To initialize the launching script |
|---|
| 273 | initlaunch() { |
|---|
| 274 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user." |
|---|
| 275 | convertyears |
|---|
| 276 | i_myear=0. |
|---|
| 277 | iPEM=1 |
|---|
| 278 | iPCM=1 |
|---|
| 279 | if [ -f "startfi.nc" ]; then |
|---|
| 280 | cp startfi.nc starts/ |
|---|
| 281 | fi |
|---|
| 282 | if [ -f "start.nc" ]; then |
|---|
| 283 | cp start.nc starts/ |
|---|
| 284 | elif [ -f "start1D.txt" ]; then |
|---|
| 285 | cp start1D.txt starts/ |
|---|
| 286 | fi |
|---|
| 287 | if [ -f "startpem.nc" ]; then |
|---|
| 288 | cp startpem.nc starts/ |
|---|
| 289 | fi |
|---|
| 290 | |
|---|
| 291 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 292 | echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > launchPEM.info |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | # To submit the PCM runs |
|---|
| 296 | # arg1: launching mode |
|---|
| 297 | # arg2: counting method |
|---|
| 298 | # arg3: number of PCM runs to launch |
|---|
| 299 | # arg4: local number of the PCM run from which to start (optional) |
|---|
| 300 | submitPCM() { |
|---|
| 301 | find . -type f -name "PCMrun*.job" ! -name "PCMrun.job" -delete |
|---|
| 302 | ii=1 |
|---|
| 303 | if [ ! -z $4 ]; then |
|---|
| 304 | ii=$4 |
|---|
| 305 | fi |
|---|
| 306 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| 307 | echo "Run \"PCM $iPCM\" ($ii/$3)" |
|---|
| 308 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 309 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $3 + 2" | bc)/" PCMrun.job |
|---|
| 310 | ./PCMrun.job |
|---|
| 311 | if [ $? -ne 0 ]; then |
|---|
| 312 | errlaunch |
|---|
| 313 | fi |
|---|
| 314 | else # Mode: submitting jobs |
|---|
| 315 | cp PCMrun.job PCMrun${iPCM}.job |
|---|
| 316 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job |
|---|
| 317 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $3 + 2" | bc)/" PCMrun${iPCM}.job |
|---|
| 318 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 319 | jobID=$(sbatch --parsable PCMrun${iPCM}.job) |
|---|
| 320 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 321 | jobID=$(qsub PCMrun${iPCM}.job | cut -d. -f1) |
|---|
| 322 | fi |
|---|
| 323 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 324 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 325 | chmod +x kill_launchPEM.sh |
|---|
| 326 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| 327 | fi |
|---|
| 328 | ((iPCM++)) |
|---|
| 329 | if [ $2 -ne 0 ]; then # Counting: PCM years taken into account |
|---|
| 330 | i_myear=$(echo "$i_myear + 1." | bc -l) |
|---|
| 331 | fi |
|---|
| 332 | ((ii++)) |
|---|
| 333 | else |
|---|
| 334 | endlaunch |
|---|
| 335 | fi |
|---|
| 336 | for ((i = $ii; i <= $3; i++)); do |
|---|
| 337 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| 338 | echo "Run \"PCM $iPCM\" ($i/$3)" |
|---|
| 339 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 340 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $3 + 2" | bc)/" PCMrun.job |
|---|
| 341 | ./PCMrun.job |
|---|
| 342 | if [ $? -ne 0 ]; then |
|---|
| 343 | errlaunch |
|---|
| 344 | fi |
|---|
| 345 | else # Mode: submitting jobs |
|---|
| 346 | cp PCMrun.job PCMrun${iPCM}.job |
|---|
| 347 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job |
|---|
| 348 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $3 + 2" | bc)/" PCMrun${iPCM}.job |
|---|
| 349 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 350 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} PCMrun${iPCM}.job) |
|---|
| 351 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 352 | jobID=$(qsub -W depend=afterok:${jobID} PCMrun${iPCM}.job | cut -d. -f1) |
|---|
| 353 | fi |
|---|
| 354 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| 355 | fi |
|---|
| 356 | ((iPCM++)) |
|---|
| 357 | if [ $2 -ne 0 ]; then # Counting: PCM years taken into account |
|---|
| 358 | i_myear=$(echo "$i_myear + 1." | bc -l) |
|---|
| 359 | fi |
|---|
| 360 | else |
|---|
| 361 | endlaunch |
|---|
| 362 | fi |
|---|
| 363 | done |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | # To submit the PEM run |
|---|
| 367 | # arg1: launching mode |
|---|
| 368 | submitPEM() { |
|---|
| 369 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| 370 | echo "Run \"PEM $iPEM\"" |
|---|
| 371 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 372 | ./PEMrun.job |
|---|
| 373 | if [ $? -ne 0 ]; then |
|---|
| 374 | errlaunch |
|---|
| 375 | fi |
|---|
| 376 | else # Mode: submitting jobs |
|---|
| 377 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job |
|---|
| 378 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 379 | jobID=$(sbatch --parsable PEMrun.job) |
|---|
| 380 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 381 | jobID=$(qsub PEMrun.job | cut -d. -f1) |
|---|
| 382 | fi |
|---|
| 383 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 384 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 385 | chmod +x kill_launchPEM.sh |
|---|
| 386 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| 387 | fi |
|---|
| 388 | else |
|---|
| 389 | endlaunch |
|---|
| 390 | fi |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | # To make one cycle of PCM and PEM runs |
|---|
| 394 | # arg1: launching mode |
|---|
| 395 | # arg2: counting method |
|---|
| 396 | # arg3: number of PCM runs to launch |
|---|
| 397 | # arg4: local number of the PCM run from which to start (optional) |
|---|
| 398 | cyclelaunch() { |
|---|
| 399 | # PCM runs |
|---|
| 400 | submitPCM $1 $2 $3 $4 |
|---|
| 401 | |
|---|
| 402 | # PEM run |
|---|
| 403 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| 404 | echo "Run \"PEM $iPEM\"" |
|---|
| 405 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 406 | ./PEMrun.job |
|---|
| 407 | if [ $? -ne 0 ]; then |
|---|
| 408 | errlaunch |
|---|
| 409 | fi |
|---|
| 410 | else # Mode: submitting jobs |
|---|
| 411 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job |
|---|
| 412 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 413 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} PEMrun.job) |
|---|
| 414 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 415 | jobID=$(qsub -W depend=afterok:${jobID} PEMrun.job | cut -d. -f1) |
|---|
| 416 | fi |
|---|
| 417 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| 418 | fi |
|---|
| 419 | else |
|---|
| 420 | endlaunch |
|---|
| 421 | fi |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | # To clean files after the starting run of the relaunch |
|---|
| 425 | # arg1: file name prefix to clean |
|---|
| 426 | # arg2: file name extension to clean |
|---|
| 427 | # arg3: file number from which to clean |
|---|
| 428 | cleanfiles() { |
|---|
| 429 | prefix=$1 |
|---|
| 430 | extension=$2 |
|---|
| 431 | if [ -z "$extension" ]; then |
|---|
| 432 | for file in ${prefix}*; do |
|---|
| 433 | num=${file#$prefix} |
|---|
| 434 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 435 | rm $file |
|---|
| 436 | fi |
|---|
| 437 | done |
|---|
| 438 | else |
|---|
| 439 | for file in ${prefix}*${extension}; do |
|---|
| 440 | num=${file#$prefix} |
|---|
| 441 | num=${num%$extension} |
|---|
| 442 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 443 | rm $file |
|---|
| 444 | fi |
|---|
| 445 | done |
|---|
| 446 | fi |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | # To relaunch from PCM run |
|---|
| 450 | # arg1: launching mode |
|---|
| 451 | # arg2: counting method |
|---|
| 452 | relaunchPCM() { |
|---|
| 453 | iPCM=$(($irelaunch + 1)) |
|---|
| 454 | cleanfiles diags/diagfi .nc $irelaunch |
|---|
| 455 | cleanfiles diags/diagsoil .nc $irelaunch |
|---|
| 456 | cleanfiles diags/Xoutdaily4pem .nc $irelaunch |
|---|
| 457 | cleanfiles diags/Xoutyearly4pem .nc $irelaunch |
|---|
| 458 | cleanfiles logs/runPCM .log $irelaunch |
|---|
| 459 | cleanfiles starts/restart1D .txt $irelaunch |
|---|
| 460 | cleanfiles starts/restart .nc $irelaunch |
|---|
| 461 | cleanfiles starts/restartfi .nc $irelaunch |
|---|
| 462 | cp starts/restartfi${irelaunch}.nc startfi.nc |
|---|
| 463 | if [ -f "starts/restart${irelaunch}.nc" ]; then |
|---|
| 464 | cp starts/restart${irelaunch}.nc start.nc |
|---|
| 465 | elif [ -f "starts/restart1D${irelaunch}.txt" ]; then |
|---|
| 466 | cp starts/restart1D${irelaunch}.txt start1D.txt |
|---|
| 467 | fi |
|---|
| 468 | if [ $irelaunch -le $nPCM_ini ]; then |
|---|
| 469 | # PCM relaunch during the initialization cycle |
|---|
| 470 | iPEM=1 |
|---|
| 471 | if [ $2 -ne 0 ]; then # Counting: PCM years taken into account |
|---|
| 472 | i_myear=$irelaunch |
|---|
| 473 | else # Counting: only PEM years count |
|---|
| 474 | i_myear=0 |
|---|
| 475 | fi |
|---|
| 476 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" launchPEM.info |
|---|
| 477 | cleanfiles diags/diagpem .nc $(($iPEM - 1)) |
|---|
| 478 | cleanfiles diags/diagsoilpem .nc $(($iPEM - 1)) |
|---|
| 479 | cleanfiles logs/runPEM .log $(($iPEM - 1)) |
|---|
| 480 | cleanfiles starts/restart1D_postPEM .txt $(($iPEM - 1)) |
|---|
| 481 | cleanfiles starts/restart_postPEM .nc $(($iPEM - 1)) |
|---|
| 482 | cleanfiles starts/restartfi_postPEM .nc $(($iPEM - 1)) |
|---|
| 483 | cleanfiles starts/restartpem .nc $(($iPEM - 1)) |
|---|
| 484 | rm -f startpem.nc |
|---|
| 485 | if [ -f "starts/startpem.nc" ]; then |
|---|
| 486 | cp starts/startpem.nc . |
|---|
| 487 | fi |
|---|
| 488 | if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then |
|---|
| 489 | cp diags/Xoutdaily4pem${irelaunch}.nc Xoutdaily4pem_Y1.nc |
|---|
| 490 | cp diags/Xoutyearly4pem${irelaunch}.nc Xoutyearly4pem_Y1.nc |
|---|
| 491 | cyclelaunch $1 $2 $nPCM_ini $iPCM |
|---|
| 492 | elif [ $irelaunch -eq $nPCM_ini ]; then |
|---|
| 493 | cp diags/Xoutdaily4pem$(($irelaunch - 1)).nc Xoutdaily4pem_Y1.nc |
|---|
| 494 | cp diags/Xoutyearly4pem$(($irelaunch - 1)).nc Xoutyearly4pem_Y1.nc |
|---|
| 495 | cp diags/Xoutdaily4pem${irelaunch}.nc Xoutdaily4pem_Y2.nc |
|---|
| 496 | cp diags/Xoutyearly4pem${irelaunch}.nc Xoutyearly4pem_Y2.nc |
|---|
| 497 | submitPEM $1 # The next job is a PEM run |
|---|
| 498 | else |
|---|
| 499 | cyclelaunch $1 $2 $nPCM_ini $iPCM |
|---|
| 500 | fi |
|---|
| 501 | else |
|---|
| 502 | # PCM relaunch during a cycle |
|---|
| 503 | iPEM=$(echo "($iPCM - $nPCM_ini)/$nPCM + 1" | bc) |
|---|
| 504 | il=$(echo "($irelaunch - $nPCM_ini + 1)%$nPCM + 1" | bc) |
|---|
| 505 | if [ $2 -ne 0 ]; then # Counting: PCM years taken into account |
|---|
| 506 | i_myear=$(echo "$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "launchPEM.info") + $il" | bc -l) |
|---|
| 507 | else # Counting: only PEM years count |
|---|
| 508 | i_myear=$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "launchPEM.info") |
|---|
| 509 | fi |
|---|
| 510 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" launchPEM.info |
|---|
| 511 | cleanfiles diags/diagpem .nc $(($iPEM - 1)) |
|---|
| 512 | cleanfiles diags/diagsoilpem .nc $(($iPEM - 1)) |
|---|
| 513 | cleanfiles logs/runPEM .log $(($iPEM - 1)) |
|---|
| 514 | cleanfiles starts/restart1D_postPEM .txt $(($iPEM - 1)) |
|---|
| 515 | cleanfiles starts/restart_postPEM .nc $(($iPEM - 1)) |
|---|
| 516 | cleanfiles starts/restartfi_postPEM .nc $(($iPEM - 1)) |
|---|
| 517 | cleanfiles starts/restartpem .nc $(($iPEM - 1)) |
|---|
| 518 | cp starts/restartpem$(($iPEM - 1)).nc startpem.nc |
|---|
| 519 | if [ $il -eq $(($nPCM - 1)) ]; then # Second to last PCM run |
|---|
| 520 | cp diags/Xoutdaily4pem${irelaunch}.nc Xoutdaily4pem_Y1.nc |
|---|
| 521 | cp diags/Xoutyearly4pem${irelaunch}.nc Xoutyearly4pem_Y1.nc |
|---|
| 522 | cyclelaunch $1 $2 $nPCM $(($il + 1)) |
|---|
| 523 | elif [ $il -eq $nPCM ]; then # Last PCM run so the next job is a PEM run |
|---|
| 524 | cp diags/Xoutdaily4pem$(($irelaunch - 1)).nc Xoutdaily4pem_Y1.nc |
|---|
| 525 | cp diags/Xoutyearly4pem$(($irelaunch - 1)).nc Xoutyearly4pem_Y1.nc |
|---|
| 526 | cp diags/Xoutdaily4pem${irelaunch}.nc Xoutdaily4pem_Y2.nc |
|---|
| 527 | cp diags/Xoutyearly4pem${irelaunch}.nc Xoutyearly4pem_Y2.nc |
|---|
| 528 | submitPEM $1 |
|---|
| 529 | else |
|---|
| 530 | cyclelaunch $1 $2 $nPCM $(($il + 1)) |
|---|
| 531 | fi |
|---|
| 532 | fi |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | # To relaunch from PEM run |
|---|
| 536 | # arg1: launching mode |
|---|
| 537 | # arg2: counting method |
|---|
| 538 | relaunchPEM() { |
|---|
| 539 | iPEM=$(echo "$irelaunch + 1" | bc) |
|---|
| 540 | iPCM=$(echo "$nPCM_ini + $nPCM*($irelaunch - 1) + 1" | bc) |
|---|
| 541 | i_myear=$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "launchPEM.info") |
|---|
| 542 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" launchPEM.info |
|---|
| 543 | cleanfiles diags/diagfi .nc $(($iPCM - 1)) |
|---|
| 544 | cleanfiles diags/diagsoil .nc $(($iPCM - 1)) |
|---|
| 545 | cleanfiles logs/runPCM .log $(($iPCM - 1)) |
|---|
| 546 | cleanfiles starts/restart1D .txt $(($iPCM - 1)) |
|---|
| 547 | cleanfiles starts/restart .nc $(($iPCM - 1)) |
|---|
| 548 | cleanfiles starts/restartfi .nc $(($iPCM - 1)) |
|---|
| 549 | cleanfiles diags/Xoutdaily4pem .nc $(($iPCM - 1)) |
|---|
| 550 | cleanfiles diags/Xoutyearly4pem .nc $(($iPCM - 1)) |
|---|
| 551 | cleanfiles diags/diagpem .nc $irelaunch |
|---|
| 552 | cleanfiles diags/diagsoilpem .nc $irelaunch |
|---|
| 553 | cleanfiles logs/runPEM .log $irelaunch |
|---|
| 554 | cleanfiles starts/restart1D_postPEM .txt $irelaunch |
|---|
| 555 | cleanfiles starts/restart_postPEM .nc $irelaunch |
|---|
| 556 | cleanfiles starts/restartfi_postPEM .nc $irelaunch |
|---|
| 557 | cleanfiles starts/restartpem .nc $irelaunch |
|---|
| 558 | cp starts/restartpem${irelaunch}.nc startpem.nc |
|---|
| 559 | cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc |
|---|
| 560 | if [ -f "starts/restart_postPEM${irelaunch}.nc" ]; then |
|---|
| 561 | cp starts/restart_postPEM${irelaunch}.nc start.nc |
|---|
| 562 | elif [ -f "starts/restart1D_postPEM${irelaunch}.txt" ]; then |
|---|
| 563 | cp starts/restart1D_postPEM${irelaunch}.txt start1D.txt |
|---|
| 564 | fi |
|---|
| 565 | cyclelaunch $1 $2 $nPCM |
|---|
| 566 | } |
|---|