run.dates.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. # Function leap days calculates the number of leap days (29th of Februrary) in
  3. # a time intervall between two dates.
  4. #
  5. # Usage leap_days START_DATE END_DATE
  6. function leap_days()
  7. {
  8. local ld=0
  9. local frstYYYY=$(date -ud "$1" +%Y)
  10. local lastYYYY=$(date -ud "$2" +%Y)
  11. set +e
  12. # Check first year for leap day between start and end date
  13. $(date -ud "${frstYYYY}-02-29" > /dev/null 2>&1) \
  14. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  15. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  16. && (( ld++ ))
  17. # Check intermediate years for leap day
  18. for (( y=(( ${frstYYYY}+1 )); y<=(( ${lastYYYY}-1 )); y++ ))
  19. do
  20. $(date -ud "$y-02-29" > /dev/null 2>&1) && (( ld++ ))
  21. done
  22. # Check last year (if different from first year) for leap day between start
  23. # and end date
  24. (( $lastYYYY > $frstYYYY )) \
  25. && $(date -ud "${lastYYYY}-02-29" > /dev/null 2>&1) \
  26. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  27. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  28. && (( ld++ ))
  29. set -e
  30. echo "$ld"
  31. }
  32. set -ueo pipefail
  33. nemo_src_dir=${HOME}/modeles/NEMO/3.6
  34. ini_data_dir=/scratch/pbarriat/DATA/nemo
  35. nem_grid=ORCA2L31
  36. # Simulation start and end date. Use any (reasonable) syntax you want.
  37. run_start_date="1990-01-01"
  38. run_duration="4 years"
  39. # Restart frequency. Use any (reasonable) number and time unit you want.
  40. # For runs without restart, leave this variable empty or set to 0
  41. rst_freq="1 year"
  42. # Number of restart legs to be run in one go
  43. run_num_legs=2
  44. # Directories
  45. #start_dir=${SLURM_SUBMIT_DIR}
  46. start_dir=${PWD}
  47. ctrl_file_dir=${start_dir}/ctrl
  48. # This file is used to store information about restarts
  49. run_dir="./rundir"
  50. ece_info_file="ece.info"
  51. nemo_src_dir=$HOME/modeles/NEMO/3.6
  52. nem_config_name="ORCA2_LIM3"
  53. nem_exe_file=${nemo_src_dir}/CONFIG/${nem_config_name}/BLD/bin/nemo.exe
  54. xio_exe_file=${nemo_src_dir}/EXTERNAL/xios-1.0/bin/xios_server.exe
  55. # Make sure run directory exists with the necessary stuff and move there
  56. mkdir -p $run_dir && cd $_
  57. cp ${nem_exe_file} .
  58. cp ${xio_exe_file} .
  59. # Configure paths for NEMO IC /!\ filenames cannot contain white spaces
  60. ic_files=("coordinates.nc"
  61. "bathy_meter.nc"
  62. "runoff_core_monthly.nc runoffs_depth"
  63. "ahmcoef.nc"
  64. "mask_itf.nc"
  65. "K1rowdrg.nc"
  66. "M2rowdrg.nc"
  67. "data_1m_potential_temperature_nomask.nc temperature.nc"
  68. "data_1m_salinity_nomask.nc salinity"
  69. "subbasins.nc"
  70. "resto.nc"
  71. "chlorophyll.nc"
  72. "geothermal_heating.nc")
  73. for file in "${ic_files[@]}"
  74. do
  75. ln -sf ${ini_data_dir}/INIT/${nem_grid}/$file
  76. done
  77. # Normalize date formats
  78. run_start_date=$(date -uR -d "${run_start_date}")
  79. run_end_date="${run_start_date} + ${run_duration}"
  80. run_end_date=$(date -uR -d "${run_end_date}")
  81. for (( ; run_num_legs>0 ; run_num_legs-- ))
  82. do
  83. # Initialize variables using restart file if it exists
  84. [[ -r ${ece_info_file} ]] && source ${ece_info_file}
  85. leg_start_date=${leg_end_date:-$run_start_date}
  86. leg_number=$((${leg_number:=0}+1))
  87. # Compute leg end-date and trim if necessary
  88. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq:=$run_duration}")
  89. (( $(date -d "${leg_end_date}" +%s) > $(date -d "${run_end_date}" +%s) )) && leg_end_date=${run_end_date}
  90. # Check whether there is some work left to do
  91. if (( $(date -d "${leg_start_date}" +%s) >= $(date -d "${run_end_date}" +%s) )) ;
  92. then
  93. echo "Leg start date equal to or after end of simulation."
  94. echo "Nothing left to do. Exiting."
  95. exit 0
  96. fi
  97. # Some time variables needed later
  98. leg_length_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${leg_start_date}" +%s) ))
  99. leg_start_sec=$(( $(date -d "${leg_start_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  100. leg_end_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  101. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  102. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  103. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  104. # Correct for leap days because NEMO standalone uses no-leap calendar
  105. leg_length_sec=$(( leg_length_sec - $(leap_days "${leg_start_date}" "${leg_end_date}")*24*3600 ))
  106. leg_start_sec=$(( leg_start_sec - $(leap_days "${run_start_date}" "${leg_start_date}")*24*3600 ))
  107. leg_end_sec=$(( leg_end_sec - $(leap_days "${run_start_date}" "${leg_end_date}")*24*3600 ))
  108. # ---------------------------------------------------------------------
  109. # *** Remove all leftover output files from previous legs
  110. # ---------------------------------------------------------------------
  111. t1=$(date +%s)
  112. sleep 1
  113. t2=$(date +%s)
  114. tr=$(date -d "0 -$t1 sec + $t2 sec" +%T)
  115. # -------------------------------------------------------------------------
  116. # *** Write the restart control file
  117. # -------------------------------------------------------------------------
  118. echo "#" | tee -a ${ece_info_file}
  119. echo "# Finished leg at `date '+%F %T'` after ${tr} (hh:mm:ss)" \
  120. | tee -a ${ece_info_file}
  121. echo "leg_number=${leg_number}" | tee -a ${ece_info_file}
  122. echo "leg_start_date=\"${leg_start_date}\"" | tee -a ${ece_info_file}
  123. echo "leg_end_date=\"${leg_end_date}\"" | tee -a ${ece_info_file}
  124. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  125. done # loop over legs
  126. exit 0