From b055e9ed45066af376a1972370d54f8b966ea033 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Fri, 2 Sep 2016 16:27:50 +0200
Subject: [PATCH] Philippe 02/09/2016: added LBAK_BEG and LBAK_END to force
 backup at 1st and last timestep

---
 src/MNH/modd_fmout.f90 |  4 ++++
 src/MNH/modn_fmout.f90 |  2 +-
 src/MNH/set_grid.f90   | 16 +++++++++++++++-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/MNH/modd_fmout.f90 b/src/MNH/modd_fmout.f90
index 22a73cee8..c7072ec84 100644
--- a/src/MNH/modd_fmout.f90
+++ b/src/MNH/modd_fmout.f90
@@ -47,6 +47,10 @@ USE MODD_PARAMETERS
 !
 IMPLICIT NONE
 !
+LOGICAL,SAVE :: LBAK_BEG = .FALSE. ! Force a backup at the first timestep
+                                   ! of the segment for all models
+LOGICAL,SAVE :: LBAK_END = .FALSE. ! Force a backup at the last timestep
+                                   ! of the segment for all models
 REAL,SAVE,ALLOCATABLE,DIMENSION(:,:)  ::   XBAK_TIME 
 ! XBAK_TIME(m,i) array of 
 ! the increments in seconds from the beginning of the segment to the
diff --git a/src/MNH/modn_fmout.f90 b/src/MNH/modn_fmout.f90
index a80e3f261..3f3efbb8e 100644
--- a/src/MNH/modn_fmout.f90
+++ b/src/MNH/modn_fmout.f90
@@ -48,6 +48,6 @@ USE MODD_FMOUT
 !
 IMPLICIT NONE
 !
-NAMELIST/NAM_FMOUT/XBAK_TIME,NBAK_STEP
+NAMELIST/NAM_FMOUT/LBAK_BEG,LBAK_END,XBAK_TIME,NBAK_STEP
 !
 END MODULE MODN_FMOUT
diff --git a/src/MNH/set_grid.f90 b/src/MNH/set_grid.f90
index 0f0f7a9f7..d5aa74c13 100644
--- a/src/MNH/set_grid.f90
+++ b/src/MNH/set_grid.f90
@@ -575,13 +575,21 @@ DO IMI = 1, NMODEL
     END IF
   END DO
   !
-  !*       2.3.2 Group all backups in a common form
+  !*       2.3.2 Group all backups in a common form and add backups at beginning and end if requested
+  !
+  IF (LBAK_BEG) IBAK_NUMB = IBAK_NUMB + 1
+  IF (LBAK_END) IBAK_NUMB = IBAK_NUMB + 1
   !
   ALLOCATE(IBAK_STEP(IBAK_NUMB))
   IBAK_STEP(:) = NNEGUNDEF
   !
   IBAK_NUMB = 0
   !
+  IF (LBAK_BEG) THEN
+    IBAK_NUMB = IBAK_NUMB + 1
+    IBAK_STEP(IBAK_NUMB) = 1 ! 1 is the 1st step number
+  END IF
+  !
   DO JOUT = 1,JPOUTMAX
     IF (XBAK_TIME(IMI,JOUT) >= 0.) THEN
       IBAK_NUMB = IBAK_NUMB + 1
@@ -596,6 +604,12 @@ DO IMI = 1, NMODEL
     END IF
   END DO
   !
+  IF (LBAK_END) THEN
+    IBAK_NUMB = IBAK_NUMB + 1
+    IBAK_STEP(IBAK_NUMB) = NINT(XSEGLEN/DYN_MODEL(IMI)%XTSTEP)+1
+    IF (IMI == 1) IBAK_STEP(IBAK_NUMB) = IBAK_STEP(IBAK_NUMB) - ISUP
+  END IF
+  !
   !*       2.3.2 Find and remove duplicated entries
   !
   DO JOUT = 1,IBAK_NUMB
-- 
GitLab