From 0b2c88f3b5306a921aee56369eb06f76b7c62e9f Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Tue, 9 Jul 2024 13:11:27 +0200
Subject: [PATCH] Philippe 09/07/2024: bugfix: do not overwrite freshly
 allocated CCPLFILE pointer

---
 src/MNH/modd_lunitn.f90 | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/MNH/modd_lunitn.f90 b/src/MNH/modd_lunitn.f90
index f10bfbf10..cee189b4b 100644
--- a/src/MNH/modd_lunitn.f90
+++ b/src/MNH/modd_lunitn.f90
@@ -83,22 +83,21 @@ USE MODD_IO, ONLY : TFILE_OUTPUTLISTING
 !
 INTEGER, INTENT(IN) :: KFROM, KTO
 !
-!JUAN
+! Save current state for allocated arrays
+LUNIT_MODEL(KFROM)%CCPLFILE => CCPLFILE
+LUNIT_MODEL(KFROM)%TCPLFILE => TCPLFILE
+!
 IF (LUNIT_FIRST_CALL(KTO)) THEN
-ALLOCATE (LUNIT_MODEL(KTO)%CCPLFILE(NPCPLFILEMAX))
-LUNIT_MODEL(KTO)%CCPLFILE(:) = ''
-! TCPLFILE allocation is done in ini_cpl directly at the right size => commented here
-! ALLOCATE (LUNIT_MODEL(KTO)%TCPLFILE(NPCPLFILEMAX))
-LUNIT_FIRST_CALL(KTO) = .FALSE.
+  !Do this AFTER saving current state to prevent loss of the correct allocated pointers if KFROM==KTO
+  ALLOCATE ( LUNIT_MODEL(KTO)%CCPLFILE(NPCPLFILEMAX) )
+  LUNIT_MODEL(KTO)%CCPLFILE(:) = ''
+  ! TCPLFILE allocation is done in ini_cpl directly at the right size => commented here
+  ! ALLOCATE (LUNIT_MODEL(KTO)%TCPLFILE(NPCPLFILEMAX))
+  LUNIT_FIRST_CALL(KTO) = .FALSE.
 ENDIF
-!JUAN
 !
 TFILE_OUTPUTLISTING => LUNIT_MODEL(KTO)%TLUOUT
 !
-! Save current state for allocated arrays
-LUNIT_MODEL(KFROM)%CCPLFILE => CCPLFILE
-LUNIT_MODEL(KFROM)%TCPLFILE => TCPLFILE
-!
 ! Current model is set to model KTO
 CINIFILE=>LUNIT_MODEL(KTO)%CINIFILE
 TINIFILE=>LUNIT_MODEL(KTO)%TINIFILE
-- 
GitLab