From 3719b8c02f0b1ba126837717566405edeeb01b84 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Wed, 21 Sep 2022 15:08:51 +0200
Subject: [PATCH] Philippe 21/09/2022: move allocations of XHAT_BOUND and
 XHATM_BOUND to STORE_GRID_1DIR_BOUNDS

---
 src/MNH/ini_modeln.f90      |  2 --
 src/MNH/ini_spectren.f90    |  2 --
 src/MNH/prep_ideal_case.f90 |  1 -
 src/MNH/read_hgridn.f90     |  2 --
 src/MNH/set_grid.f90        | 66 ++++++++++++++++++++++---------------
 src/MNH/spawn_grid2.f90     |  8 ++---
 src/MNH/spawn_model2.f90    |  1 -
 7 files changed, 43 insertions(+), 39 deletions(-)

diff --git a/src/MNH/ini_modeln.f90 b/src/MNH/ini_modeln.f90
index a3de7b35a..9a4aed5cd 100644
--- a/src/MNH/ini_modeln.f90
+++ b/src/MNH/ini_modeln.f90
@@ -1013,8 +1013,6 @@ ALLOCATE(XDIRCOSXW(IIU,IJU))
 ALLOCATE(XDIRCOSYW(IIU,IJU))
 ALLOCATE(XCOSSLOPE(IIU,IJU))
 ALLOCATE(XSINSLOPE(IIU,IJU))
-ALLOCATE(XHAT_BOUND (NHAT_BOUND_SIZE))
-ALLOCATE(XHATM_BOUND(NHAT_BOUND_SIZE))
 !
 ALLOCATE(XDXX(IIU,IJU,IKU))
 ALLOCATE(XDYY(IIU,IJU,IKU))
diff --git a/src/MNH/ini_spectren.f90 b/src/MNH/ini_spectren.f90
index a98810782..29b2679f0 100644
--- a/src/MNH/ini_spectren.f90
+++ b/src/MNH/ini_spectren.f90
@@ -269,8 +269,6 @@ ALLOCATE(XZSMT(IIU,IJU))
 ALLOCATE(XZZ(IIU,IJU,IKU))
 ALLOCATE(XZHAT(IKU))
 ALLOCATE(XZHATM(IKU))
-ALLOCATE(XHAT_BOUND (NHAT_BOUND_SIZE))
-ALLOCATE(XHATM_BOUND(NHAT_BOUND_SIZE))
 !
 ALLOCATE(XDXX(IIU,IJU,IKU))
 ALLOCATE(XDYY(IIU,IJU,IKU))
diff --git a/src/MNH/prep_ideal_case.f90 b/src/MNH/prep_ideal_case.f90
index 2a892ab01..b4faa5ac1 100644
--- a/src/MNH/prep_ideal_case.f90
+++ b/src/MNH/prep_ideal_case.f90
@@ -904,7 +904,6 @@ IF(.NOT. L1D) LHORELAX_SV(1:NSV)=.TRUE.
 !
 !*       4.1  Vertical Spatial grid 
 !
-ALLOCATE( XHAT_BOUND(NHAT_BOUND_SIZE), XHATM_BOUND(NHAT_BOUND_SIZE) )
 CALL READ_VER_GRID(TZEXPREFILE)
 !
 !*       4.2  Initialize parallel variables and compute array's dimensions
diff --git a/src/MNH/read_hgridn.f90 b/src/MNH/read_hgridn.f90
index 4afe7ed32..cfccf88ba 100644
--- a/src/MNH/read_hgridn.f90
+++ b/src/MNH/read_hgridn.f90
@@ -254,8 +254,6 @@ CALL IO_Field_read(TPFMFILE,'YHAT',XYHAT)
 
 IF ( .NOT. ASSOCIATED(XXHATM) ) ALLOCATE( XXHATM(SIZE( XXHAT )) )
 IF ( .NOT. ASSOCIATED(XYHATM) ) ALLOCATE( XYHATM(SIZE( XYHAT )) )
-IF ( .NOT. ASSOCIATED(XHAT_BOUND)  ) ALLOCATE( XHAT_BOUND (NHAT_BOUND_SIZE) )
-IF ( .NOT. ASSOCIATED(XHATM_BOUND) ) ALLOCATE( XHATM_BOUND(NHAT_BOUND_SIZE) )
 
 ! Interpolations of positions to mass points
 CALL INTERP_HORGRID_TO_MASSPOINTS( XXHAT, XYHAT, XXHATM, XYHATM )
diff --git a/src/MNH/set_grid.f90 b/src/MNH/set_grid.f90
index e2cb21e46..e2d28d064 100644
--- a/src/MNH/set_grid.f90
+++ b/src/MNH/set_grid.f90
@@ -207,8 +207,8 @@ REAL, DIMENSION(:),     INTENT(OUT) :: PDXHAT    ! horizontal stretching in x
 REAL, DIMENSION(:),     INTENT(OUT) :: PDYHAT    ! horizontal stretching in y
 REAL, DIMENSION(:),     INTENT(OUT) :: PXHATM    ! Position x in the conformal plane or on the cartesian plane at mass points
 REAL, DIMENSION(:),     INTENT(OUT) :: PYHATM    ! Position y in the conformal plane or on the cartesian plane at mass points
-REAL, DIMENSION(:),     INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-REAL, DIMENSION(:),     INTENT(INOUT) :: PHATM_BOUND ! Boundaries of global domain in the conformal or cartesian plane at mass pts
+REAL, DIMENSION(:),     POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+REAL, DIMENSION(:),     POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 REAL, DIMENSION(:,:),   INTENT(OUT) :: PMAP      ! Map factor
 !
 REAL, DIMENSION(:,:),   INTENT(OUT) :: PZS       ! orography
@@ -284,7 +284,8 @@ IF (.NOT.LCARTESIAN) THEN
     CALL IO_Field_read(TPINIFILE,'LONORI',PLONORI)
     CALL IO_Field_read(TPINIFILE,'LATORI',PLATORI)
   !
-  ELSE                     
+  ELSE
+    ! If file comes from MesoNH < 4.6.0
     CALL FIND_FIELD_ID_FROM_MNHNAME('LONORI',IID,IRESP)
     TZFIELD = TFIELDMETADATA( TFIELDLIST(IID) )
     TZFIELD%CMNHNAME = 'LONOR'
@@ -519,7 +520,7 @@ END SUBROUTINE INTERP_VERGRID_TO_MASSPOINTS
 SUBROUTINE STORE_GRID_1DIR_BOUNDS( HDIR, PHAT, PHATM, PHAT_BOUND, PHATM_BOUND )
 
   USE MODD_GRID_n
-  USE MODD_PARAMETERS,     ONLY: JPHEXT, JPVEXT
+  USE MODD_PARAMETERS,     ONLY: JPHEXT, JPVEXT, XNEGUNDEF
 
   USE MODE_ALLOCBUFFER_ll, ONLY: ALLOCBUFFER_ll
   USE MODE_GATHER_ll,      ONLY: GATHERALL_FIELD_ll
@@ -527,11 +528,11 @@ SUBROUTINE STORE_GRID_1DIR_BOUNDS( HDIR, PHAT, PHATM, PHAT_BOUND, PHATM_BOUND )
 
   IMPLICIT NONE
 
-  CHARACTER(LEN=1),                 INTENT(IN)    :: HDIR        ! Direction ('X', 'Y' or 'Z')
-  REAL, DIMENSION(:), TARGET,       INTENT(IN)    :: PHAT        ! Position x, y or z in the conformal or cartesian plane
-  REAL, DIMENSION(:), TARGET,       INTENT(IN)    :: PHATM       ! id at mass points
-  REAL, DIMENSION(NHAT_BOUND_SIZE), INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-  REAL, DIMENSION(NHAT_BOUND_SIZE), INTENT(INOUT) :: PHATM_BOUND ! id at mass points
+  CHARACTER(LEN=1),            INTENT(IN)    :: HDIR        ! Direction ('X', 'Y' or 'Z')
+  REAL, DIMENSION(:), TARGET,  INTENT(IN)    :: PHAT        ! Position x, y or z in the conformal or cartesian plane
+  REAL, DIMENSION(:), TARGET,  INTENT(IN)    :: PHATM       ! id at mass points
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 
   INTEGER                     :: IERR
   LOGICAL                     :: GALLOC, GALLOCM
@@ -540,9 +541,20 @@ SUBROUTINE STORE_GRID_1DIR_BOUNDS( HDIR, PHAT, PHATM, PHAT_BOUND, PHATM_BOUND )
 
   GALLOC  = .FALSE.
   GALLOCM = .FALSE.
+
   ZHAT_GLOB  => NULL()
   ZHATM_GLOB => NULL()
 
+  IF ( .NOT. ASSOCIATED( PHAT_BOUND ) ) THEN
+    ALLOCATE( PHAT_BOUND(NHAT_BOUND_SIZE) )
+    PHAT_BOUND(:) = XNEGUNDEF
+  END IF
+
+  IF ( .NOT. ASSOCIATED( PHATM_BOUND ) ) THEN
+    ALLOCATE( PHATM_BOUND(NHAT_BOUND_SIZE) )
+    PHATM_BOUND(:) = XNEGUNDEF
+  END IF
+
   SELECT CASE (HDIR)
     CASE ( 'X' )
       CALL ALLOCBUFFER_ll( ZHAT_GLOB,  PHAT,  'XX', GALLOC  )
@@ -613,14 +625,14 @@ SUBROUTINE STORE_GRID_BOUNDS( PXHAT, PYHAT, PZHAT, PXHATM, PYHATM, PZHATM, PHAT_
 
   IMPLICIT NONE
 
-  REAL, DIMENSION(:), INTENT(IN)    :: PXHAT  ! Position x in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PYHAT  ! Position y in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PZHAT  ! Position y in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PXHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(IN)    :: PYHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(IN)    :: PZHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHATM_BOUND ! id at mass points
+  REAL, DIMENSION(:),              INTENT(IN)    :: PXHAT       ! Position x in the conformal or cartesian plane
+  REAL, DIMENSION(:),              INTENT(IN)    :: PYHAT       ! Position y in the conformal or cartesian plane
+  REAL, DIMENSION(:),              INTENT(IN)    :: PZHAT       ! Position y in the conformal or cartesian plane
+  REAL, DIMENSION(:),              INTENT(IN)    :: PXHATM      ! idem at mass points
+  REAL, DIMENSION(:),              INTENT(IN)    :: PYHATM      ! idem at mass points
+  REAL, DIMENSION(:),              INTENT(IN)    :: PZHATM      ! idem at mass points
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 
   CALL STORE_GRID_1DIR_BOUNDS( 'X', PXHAT, PXHATM, PHAT_BOUND, PHATM_BOUND )
   CALL STORE_GRID_1DIR_BOUNDS( 'Y', PYHAT, PYHATM, PHAT_BOUND, PHATM_BOUND )
@@ -635,12 +647,12 @@ SUBROUTINE STORE_HORGRID_BOUNDS( PXHAT, PYHAT, PXHATM, PYHATM, PHAT_BOUND, PHATM
 
   IMPLICIT NONE
 
-  REAL, DIMENSION(:), INTENT(IN)    :: PXHAT  ! Position x in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PYHAT  ! Position y in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PXHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(IN)    :: PYHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHATM_BOUND ! id at mass points
+  REAL, DIMENSION(:),          INTENT(IN)    :: PXHAT       ! Position x in the conformal or cartesian plane
+  REAL, DIMENSION(:),          INTENT(IN)    :: PYHAT       ! Position y in the conformal or cartesian plane
+  REAL, DIMENSION(:),          INTENT(IN)    :: PXHATM      ! idem at mass points
+  REAL, DIMENSION(:),          INTENT(IN)    :: PYHATM      ! idem at mass points
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 
   CALL STORE_GRID_1DIR_BOUNDS( 'X', PXHAT, PXHATM, PHAT_BOUND, PHATM_BOUND )
   CALL STORE_GRID_1DIR_BOUNDS( 'Y', PYHAT, PYHATM, PHAT_BOUND, PHATM_BOUND )
@@ -654,10 +666,10 @@ SUBROUTINE STORE_VERGRID_BOUNDS( PZHAT, PZHATM, PHAT_BOUND, PHATM_BOUND )
 
   IMPLICIT NONE
 
-  REAL, DIMENSION(:), INTENT(IN)    :: PZHAT  ! Position y in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(IN)    :: PZHATM ! id at mass points
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-  REAL, DIMENSION(:), INTENT(INOUT) :: PHATM_BOUND ! id at mass points
+  REAL, DIMENSION(:),          INTENT(IN)    :: PZHAT       ! Position z in the conformal or cartesian plane
+  REAL, DIMENSION(:),          INTENT(IN)    :: PZHATM      ! idem at mass points
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+  REAL, DIMENSION(:), POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 
   CALL STORE_GRID_1DIR_BOUNDS( 'Z', PZHAT, PZHATM, PHAT_BOUND, PHATM_BOUND )
 
diff --git a/src/MNH/spawn_grid2.f90 b/src/MNH/spawn_grid2.f90
index 2bbc35dd8..332519faa 100644
--- a/src/MNH/spawn_grid2.f90
+++ b/src/MNH/spawn_grid2.f90
@@ -30,8 +30,8 @@ REAL, DIMENSION(:),   INTENT(OUT) :: PXHAT,PYHAT,PZHAT ! positions x,y,z in the
                                      ! conformal plane or on the cartesian plane
 REAL, DIMENSION(:),   INTENT(OUT) :: PXHATM, PYHATM, PZHATM ! positions x,y in the
                                      ! conformal plane or on the cartesian plane at mass points
-REAL, DIMENSION(:),   INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-REAL, DIMENSION(:),   INTENT(INOUT) :: PHATM_BOUND ! Boundaries of global domain in the conformal or cartesian plane at mass points
+REAL, DIMENSION(:),   POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+REAL, DIMENSION(:),   POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 REAL,                 INTENT(OUT)   :: PZTOP             ! model top (m)
 LOGICAL,              INTENT(OUT)   :: OSLEVE            ! flag for SLEVE coordinate
 REAL,                 INTENT(OUT)   :: PLEN1             ! Decay scale for smooth topography
@@ -196,8 +196,8 @@ REAL, DIMENSION(:),   INTENT(OUT) :: PXHAT,PYHAT,PZHAT ! positions x,y,z in the
                                      ! conformal plane or on the cartesian plane
 REAL, DIMENSION(:),   INTENT(OUT) :: PXHATM, PYHATM, PZHATM ! positions x,y in the
                                      ! conformal plane or on the cartesian plane at mass points
-REAL, DIMENSION(:),   INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
-REAL, DIMENSION(:),   INTENT(INOUT) :: PHATM_BOUND ! Boundaries of global domain in the conformal or cartesian plane at mass points
+REAL, DIMENSION(:),   POINTER, INTENT(INOUT) :: PHAT_BOUND  ! Boundaries of global domain in the conformal or cartesian plane
+REAL, DIMENSION(:),   POINTER, INTENT(INOUT) :: PHATM_BOUND ! idem at mass points
 REAL,                 INTENT(OUT)   :: PZTOP             ! model top (m)
 LOGICAL,              INTENT(OUT)   :: OSLEVE            ! flag for SLEVE coordinate
 REAL,                 INTENT(OUT)   :: PLEN1             ! Decay scale for smooth topography
diff --git a/src/MNH/spawn_model2.f90 b/src/MNH/spawn_model2.f90
index a824c074b..fe3baed67 100644
--- a/src/MNH/spawn_model2.f90
+++ b/src/MNH/spawn_model2.f90
@@ -736,7 +736,6 @@ END IF
 !
 ALLOCATE(XXHAT(IIU),XYHAT(IJU),XZHAT(IKU))
 ALLOCATE(XXHATM(IIU),XYHATM(IJU),XZHATM(IKU))
-ALLOCATE( XHAT_BOUND(NHAT_BOUND_SIZE), XHATM_BOUND(NHAT_BOUND_SIZE) )
 ALLOCATE(XZTOP)
 ALLOCATE(XMAP(IIU,IJU))
 ALLOCATE(XLAT(IIU,IJU))
-- 
GitLab