From 3ceade9a1ff3a7a2b89f82d3024f7e7e1eef9604 Mon Sep 17 00:00:00 2001
From: Stefan Hagemann <stefan.hagemann@hereon.de>
Date: Tue, 25 Feb 2025 09:14:30 +0100
Subject: [PATCH] Writing regular restart files every month works.

---
 code/src/hd_driver.f90       | 14 +++++++++-----
 code/src/mo_time_control.f90 |  1 +
 scr/hd_run_settings.ksh      |  5 ++---
 scr/run_hdmodel.ksh          |  8 +++++++-
 4 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/code/src/hd_driver.f90 b/code/src/hd_driver.f90
index 19aeb85..6459804 100644
--- a/code/src/hd_driver.f90
+++ b/code/src/hd_driver.f90
@@ -360,10 +360,11 @@ write(nout,*) 'OTBhd_init_io'
      CALL hd_write_output
 
      ! write restart file
-     IF (step == istep+nstep) THEN
+     IF (step == istep+nstep) THEN                    ! End of simulation restart
        CALL hydrology_restart 
-     ELSE IF (step == istep+nstep_rest) THEN
+     ELSE IF (step == istep+nstep_rest) THEN          ! Dedicated or regular restart
        CALL hydrology_restart(date_rest, time_rest)
+       IF (irest_regular.GT.0) CALL update_regular_restart(irest_regular, nstep_rest, date_rest)
      ENDIF
 
      ! update model time step
@@ -593,7 +594,7 @@ CONTAINS
     !      time_end    End time of the run, HHMMSS or HH:MM:SS (Def.: none)
     !     date_rest    Date of the first restart file writing, format YYYYMMDD or YYYY-MM-DD (Def.: none)
     !     time_rest    Time of the first restart file writing, format HHMMSS or HH:MM:SS (Def.: none)
-    ! irest_regular    Switch for regular writing of restart files (0=none, 1=annual)
+    ! irest_regular    Switch for regular writing of restart files (0=none, 1=annual, 2=monthly)
     !
     !   runoff_file    file with input runoff data
     ! drainage_file    file with input drainage data
@@ -721,7 +722,8 @@ CONTAINS
         ENDIF
         nstep_rest = nstep_rest - 1      ! time_rest is exact time, not begin time of a timestep.
       ELSE     ! It is assumed that restart file shall be written after date_rest is calculated.
-        CALL calc_nstep(date_start, '00:00:00', date_rest, '00:00:00', nstep_rest)
+        time_rest = '00:00:00'
+        CALL calc_nstep(date_start, '00:00:00', date_rest, time_rest, nstep_rest)
         nstep_rest = nstep_rest + NINT(86400._dp / delta_time)    ! --> add no. of timesteps of one day.
         WRITE (message_text,*) 'time_rest is not set --> Restart will be written after date_rest is calculated'
         CALL message('config_hd', message_text)
@@ -759,7 +761,9 @@ CONTAINS
         WRITE(date_start(1:4), '(I4.4)') year1
         WRITE(date_start(6:7), '(I2.2)') month1
       ENDIF
-      CALL calc_nstep(date_start, '00:00:00', date_rest, '00:00:00', nstep_rest)
+      time_rest = '00:00:00'
+      CALL calc_nstep(date_start, '00:00:00', date_rest, time_rest, nstep_rest)
+      nstep_rest = nstep_rest - 1      ! time_rest is exact time, not begin time of a timestep.
     ENDIF
 
     ! Check coupling information
diff --git a/code/src/mo_time_control.f90 b/code/src/mo_time_control.f90
index 963bc49..2e13143 100644
--- a/code/src/mo_time_control.f90
+++ b/code/src/mo_time_control.f90
@@ -2468,6 +2468,7 @@ CONTAINS
     END SELECT
 
     CALL calc_nstep(date_old, '00:00:00', date_rest, '00:00:00', ndum)
+    ndum = ndum - 1      ! time_rest is exact time, not begin time of a timestep --> -1 step for previous step.
     nstep_rest = nstep_rest + ndum
 
   END SUBROUTINE update_regular_restart
diff --git a/scr/hd_run_settings.ksh b/scr/hd_run_settings.ksh
index 0e7c7ea..da34786 100644
--- a/scr/hd_run_settings.ksh
+++ b/scr/hd_run_settings.ksh
@@ -42,14 +42,14 @@ CFORM=nc        # Format of forcing files: 'srv' = Service Format (Default), 'nc
 ICOUPLE=0       # Coupling type: 0=no, 1=no interpolation, 2=interpolation in HD
 DNCOUPLE=${HDFILE}/nemo/hdcouple_hd_vs5_1_to_nemo_imode2.nc
 
-IWORK=5            # Run time: 1=1 year, 2=1 month, 3=year with 30 day months
+IWORK=1            # Run time: 1=1 year, 2=1 month, 3=year with 30 day months
                    #           4=as 1 but final year with nday_final days
                    #           5=Manually steered by settings in hd_subdaily_settings
 
 nday_final=212     # Jan-July: 90+91+31
 ndate_end=20210731 # end date of run for IWORK=4
 
-
+irest_regular=2    # Regular writing of restart file (0=no, 1=annual, 2=monthly)
 
 MM=01              # Start month
 # BIAS CORRECTION
@@ -65,7 +65,6 @@ esac
 DNREMAP="${EXPINP}_to_${RES_INP}"          # Remap file name
 #
 # Time steering of run using date_start and date_end - maybe edited for sub-annual simulations
-time_start=0 ; time_end=240000
 case $IWORK in
   2 ) date_start=${YYYY}${MM}01 ; date_end=${YYYY}${MM}31  ;;  # Editing of last day required 
   3 ) date_start=${YYYY}${MM}01 ; date_end=${YYYY}1230  ;; 
diff --git a/scr/run_hdmodel.ksh b/scr/run_hdmodel.ksh
index d60ff6a..f9cc818 100755
--- a/scr/run_hdmodel.ksh
+++ b/scr/run_hdmodel.ksh
@@ -392,9 +392,15 @@ cat >> namelist.hd << end_hdalone_ctl3
 end_hdalone_ctl3
 fi
 #
+if [ "$irest_regular" != "" ] ; then
 cat >> namelist.hd << end_hdalone_ctl4
-/
+  irest_regular = $irest_regular
 end_hdalone_ctl4
+fi
+#
+cat >> namelist.hd << end_hdalone_ctlz
+/
+end_hdalone_ctlz
 
 #.................... NOTE for IOUT of HD ..................................
 #!     ***   IOUT = Mittelungsartvariable, d.h. ueber wieviel Zeitschritte
-- 
GitLab