MODULE backup_core !----------------------------------------------------------------------- ! NAME ! backup_core ! ! DESCRIPTION ! Intermediate backups of the PEM/PCM restart files. ! ! AUTHORS & DATE ! JB Clement, 03/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use numerics, only: di, k4 ! DECLARATION ! ----------- implicit none ! PARAMETERS ! ---------- integer(di), protected :: backup_rate = 0_di ! Backup rate in PEM timesteps (0 disables intermediate backups) ! VISIBILITY ! ---------- private :: copy_restart_if_present, suffix2filename contains !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ !======================================================================= SUBROUTINE set_backup_config(backup_rate_in) !----------------------------------------------------------------------- ! NAME ! set_backup_config ! ! DESCRIPTION ! Setter for "backup" configuration parameters. ! ! AUTHORS & DATE ! JB Clement, 03/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use utility, only: int2str use display, only: print_msg, LVL_NFO use stoppage, only: stop_clean ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- integer(di), intent(in) :: backup_rate_in ! CODE ! ---- backup_rate = backup_rate_in call print_msg('backup_rate = '//int2str(backup_rate),LVL_NFO) if (backup_rate < 0) call stop_clean(__FILE__,__LINE__,'''backup_rate'' must be >= 0!',1) END SUBROUTINE set_backup_config !======================================================================= !======================================================================= SUBROUTINE backup_restarts(backup_idt) !----------------------------------------------------------------------- ! NAME ! backup_restarts ! ! DESCRIPTION ! Duplicate restart files to timestep-tagged backup files. ! ! AUTHORS & DATE ! JB Clement, 03/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid use io_netcdf, only: start_name, start1D_name, startfi_name, startevo_name use display, only: print_msg, LVL_NFO use utility, only: int2str ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- integer(di), intent(in) :: backup_idt ! LOCAL VARIABLES ! --------------- character(:), allocatable :: suffix ! CODE ! ---- suffix = '_ts'//int2str(backup_idt) call print_msg('> Saving "restart" files at iteration = '//int2str(backup_idt),LVL_NFO) call copy_restart_if_present('re'//startevo_name,suffix2filename(startevo_name,suffix)) call copy_restart_if_present('re'//startfi_name,suffix2filename(startfi_name,suffix)) if (ngrid == 1) then call copy_restart_if_present('re'//start1D_name,suffix2filename(start1D_name,suffix)) else call copy_restart_if_present('re'//start_name,suffix2filename(start_name,suffix)) end if END SUBROUTINE backup_restarts !======================================================================= !======================================================================= SUBROUTINE copy_restart_if_present(src_name,dst_name) !----------------------------------------------------------------------- ! NAME ! copy_restart_if_present ! ! DESCRIPTION ! Copy file if present. Used for restart backup files. ! ! AUTHORS & DATE ! JB Clement, 03/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use stoppage, only: stop_clean ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- character(*), intent(in) :: src_name, dst_name ! LOCAL VARIABLES ! --------------- logical(k4) :: here integer(di) :: cstat ! CODE ! ---- inquire(file = src_name,exist = here) if (.not. here) return call execute_command_line('\cp -f '//src_name//' '//dst_name,cmdstat = cstat) if (cstat > 0) then call stop_clean(__FILE__,__LINE__,'command execution failed!',1) else if (cstat < 0) then call stop_clean(__FILE__,__LINE__,'command execution not supported!',1) end if END SUBROUTINE copy_restart_if_present !======================================================================= !======================================================================= FUNCTION suffix2filename(filename,suffix_in) RESULT(name_out) !----------------------------------------------------------------------- ! NAME ! suffix2filename ! ! DESCRIPTION ! Insert suffix before the extension of a filename. ! ! AUTHORS & DATE ! JB Clement, 03/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- character(*), intent(in) :: filename, suffix_in ! RESULT ! ------ character(:), allocatable :: name_out ! Filename with the suffix inserted ! LOCAL VARIABLES ! --------------- integer(di) :: ipos ! CODE ! ---- ipos = index(filename,'.',back = .true.) if (ipos > 0) then name_out = filename(:ipos - 1)//suffix_in//filename(ipos:) else name_out = filename//suffix_in end if END FUNCTION suffix2filename !======================================================================= END MODULE backup_core