From d5fb0813fa13ffca323f8f04488c2ede6aa066f7 Mon Sep 17 00:00:00 2001
From: Stefan Hagemann <stefan.hagemann@hereon.de>
Date: Fri, 7 Mar 2025 15:57:03 +0100
Subject: [PATCH] Correct date/time metatinfo in restart file at simulation end

---
 code/src/hd_driver.f90       |  6 +++++-
 code/src/mo_hydrology.f90    |  8 ++++----
 code/src/mo_time_control.f90 | 21 +++++++++++++++++++++
 scr/hd_run_settings.ksh      |  2 +-
 4 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/code/src/hd_driver.f90 b/code/src/hd_driver.f90
index 6459804..d8113c7 100644
--- a/code/src/hd_driver.f90
+++ b/code/src/hd_driver.f90
@@ -361,7 +361,11 @@ write(nout,*) 'OTBhd_init_io'
 
      ! write restart file
      IF (step == istep+nstep) THEN                    ! End of simulation restart
-       CALL hydrology_restart 
+       IF (LEN_TRIM(date_rest).GT.0 .AND. step == istep+nstep_rest) THEN
+         CALL hydrology_restart(date_rest, time_rest)
+       ELSE
+         CALL hydrology_restart 
+       ENDIF
      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)
diff --git a/code/src/mo_hydrology.f90 b/code/src/mo_hydrology.f90
index 0c70610..2ec9444 100644
--- a/code/src/mo_hydrology.f90
+++ b/code/src/mo_hydrology.f90
@@ -57,7 +57,7 @@ MODULE mo_hydrology
                               delta_time, ev_puthd, get_interval_seconds, &
                               io_time_event,  &
                               initial_date, start_date, out_convert_date, inp_convert_date, &
-                              current_date, cdate2ymd, ctime2hms
+                              current_date, cdate2ymd, ctime2hms, calc_current_end_date
   USE mo_array_utils,   ONLY: dec_monotonic_closest_midpoint, &
                               inc_monotonic_closest_midpoint
   USE mo_coupling,      ONLY: set_grid_dimensions, set_local_partition, &
@@ -661,10 +661,10 @@ CONTAINS
         CALL IO_put_att_text (fileID, NF_GLOBAL, 'restart_date', date_rest)
         CALL IO_put_att_text (fileID, NF_GLOBAL, 'restart_time', time_rest)
       ELSE
-        CALL out_convert_date (current_date, yyyymmdd, hhmmss) 
-        CALL IO_put_att_int (fileID, NF_GLOBAL, 'current_date', yyyymmdd)
+        CALL calc_current_end_date (yyyymmdd, hhmmss) 
+        CALL IO_put_att_int (fileID, NF_GLOBAL, 'restart_date', yyyymmdd)
         WRITE(chms, '(I6.6)') hhmmss
-        CALL IO_put_att_text (fileID, NF_GLOBAL, 'current_time', chms)
+        CALL IO_put_att_text (fileID, NF_GLOBAL, 'restart_time', chms)
       ENDIF
 
       CALL IO_def_dim (fileID, 'lon', grid_hd%nlon, xdimid)
diff --git a/code/src/mo_time_control.f90 b/code/src/mo_time_control.f90
index 2e13143..58669c2 100644
--- a/code/src/mo_time_control.f90
+++ b/code/src/mo_time_control.f90
@@ -405,6 +405,7 @@ MODULE mo_time_control
   PUBLIC :: calc_nstep      ! Calculate the number of time steps between two ISO dates & times
   PUBLIC :: update_regular_restart  ! Updates the time step number nstep_rest 
                                     ! when the next regular restart file shall be written
+  PUBLIC :: calc_current_end_date   ! Calculate the date/time at the end of the current time step 
 
 CONTAINS
   !+
@@ -2473,6 +2474,26 @@ CONTAINS
 
   END SUBROUTINE update_regular_restart
 
+  !*****************************************************************************
+  SUBROUTINE calc_current_end_date(ymd_end, hms_end)
+  !*****************************************************************************
+
+    ! Calculate the date ymd_end and time hms_end at the end current date time step
+
+    INTEGER, INTENT(out)          :: ymd_end, hms_end
+
+    TYPE(time_days) :: date1
+    INTEGER :: i0 = 0
+    INTEGER :: idt
+
+    idt = NINT(delta_time)
+
+    date1 = current_date
+    CALL add_date(i0, idt, date1)
+    CALL out_convert_date (date1, ymd_end, hms_end)
+
+  END SUBROUTINE calc_current_end_date
+
   !+
   ! ------------------------------------------------------------------------------
   !
diff --git a/scr/hd_run_settings.ksh b/scr/hd_run_settings.ksh
index da34786..a131053 100644
--- a/scr/hd_run_settings.ksh
+++ b/scr/hd_run_settings.ksh
@@ -49,7 +49,7 @@ IWORK=1            # Run time: 1=1 year, 2=1 month, 3=year with 30 day months
 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)
+#irest_regular=2    # Regular writing of restart file (0=no, 1=annual, 2=monthly)
 
 MM=01              # Start month
 # BIAS CORRECTION
-- 
GitLab