Juan 8/12/2016: add management of LEN_HREC in MNH & SURFEX
[MNH-git_open_source-lfs.git] / src / SURFEX / read_isban.F90
index 5fe79ab..665a5b5 100644 (file)
@@ -41,7 +41,7 @@
 !!      A.L. Gibelin    04/2009 : BIOMASS and RESP_BIOMASS arrays 
 !!      A.L. Gibelin    06/2009 : Soil carbon variables for CNT option
 !!      B. Decharme  09/2012 : suppress NWG_LAYER (parallelization problems)
-!!        M.Moge    08/2015  reading SURFEX 3D fields one patch at a time for Z-parallel splitting with MNH
+!!     M.Moge    01/2016  using READ_SURF_FIELD2D/3D for 2D/3D surfex fields reads
 !!
 !-------------------------------------------------------------------------------
 !
@@ -64,6 +64,8 @@ USE MODD_SURF_PAR,       ONLY : XUNDEF, NUNDEF
 USE MODD_SNOW_PAR,       ONLY : XZ0SN
 !
 USE MODI_READ_SURF
+USE MODI_READ_SURF_FIELD3D
+USE MODI_READ_SURF_FIELD2D
 !
 USE MODI_READ_GR_SNOW
 !
@@ -85,7 +87,7 @@ INTEGER           :: ILU          ! 1D physical dimension
 !
 INTEGER           :: IRESP          ! Error code after redding
 !
- CHARACTER(LEN=12) :: YRECFM         ! Name of the article to be read
+ CHARACTER(LEN=LEN_HREC) :: YRECFM         ! Name of the article to be read
 !
  CHARACTER(LEN=4)  :: YLVL
  CHARACTER(LEN=8)  :: YPATCH
@@ -95,7 +97,6 @@ REAL, DIMENSION(:,:),ALLOCATABLE  :: ZWORK      ! 2D array to write data in file
 INTEGER :: IWORK   ! Work integer
 !
 INTEGER :: JP, JL, JNBIOMASS, JNLITTER, JNSOILCARB, JNLITTLEVS  ! loop counter on layers
-INTEGER :: JPATCH  ! loop counter on patches
 !
 INTEGER           :: IVERSION       ! surface version
 INTEGER           :: IBUGFIX
@@ -126,22 +127,8 @@ ENDIF
 !
 ALLOCATE(XTG(ILU,IWORK,NPATCH))
 !
-DO JL=1,IWORK
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    IF (JL >= 10) WRITE(YPATCH,'(I2,I4.4)') JL,JPATCH
-    IF (JL < 10)  WRITE(YPATCH,FMT='(I1,I4.4)') JL,JPATCH
-    YRECFM='TG'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-    XTG(:,JL,JPATCH)=ZWORK(:,JPATCH)
-  END DO
-#else
-  WRITE(YLVL,'(I4)') JL
-  YRECFM='TG'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-  CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-  XTG(:,JL,:)=ZWORK
-#endif
-END DO
+YRECFM='TG'
+CALL READ_SURF_FIELD3D(HPROGRAM,XTG,1,IWORK,YRECFM)
 !
 !
 !* soil liquid and ice water contents
@@ -152,81 +139,32 @@ ALLOCATE(XWGI(ILU,NGROUND_LAYER,NPATCH))
 XWG (:,:,:)=XUNDEF
 XWGI(:,:,:)=XUNDEF
 !
-DO JL=1,NGROUND_LAYER
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    IF (JL >= 10) WRITE(YPATCH,'(I2,I4.4)') JL,JPATCH
-    IF (JL < 10)  WRITE(YPATCH,FMT='(I1,I4.4)') JL,JPATCH
-    YRECFM='WG'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-    XWG(:,JL,JPATCH)=ZWORK(:,JPATCH)
-  END DO
-#else
-  WRITE(YLVL,'(I4)') JL
-  YRECFM='WG'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-   CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-   XWG(:,JL,:)=ZWORK
-#endif
-END DO
-!
-DO JL=1,NGROUND_LAYER
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    IF (JL >= 10) WRITE(YPATCH,'(I2,I4.4)') JL,JPATCH
-    IF (JL < 10)  WRITE(YPATCH,FMT='(I1,I4.4)') JL,JPATCH
-    YRECFM='WGI'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-    XWGI(:,JL,JPATCH)=ZWORK(:,JPATCH)
-  END DO
-#else
-  WRITE(YLVL,'(I4)') JL
-  YRECFM='WGI'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-  CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-  XWGI(:,JL,:)=ZWORK
-#endif
-END DO
+YRECFM='WG'
+CALL READ_SURF_FIELD3D(HPROGRAM,XWG,1,NGROUND_LAYER,YRECFM)
+!
+YRECFM='WGI'
+CALL READ_SURF_FIELD3D(HPROGRAM,XWGI,1,NGROUND_LAYER,YRECFM)
 !
 !* water intercepted on leaves
 !
 ALLOCATE(XWR(ILU,NPATCH))
 !
-YRECFM = 'WR'
-#ifdef MNH_PARALLEL
-DO JPATCH=1,NPATCH
-  WRITE(YRECFM,'(A2,I4.4)') 'WR',JPATCH
-  CALL READ_SURF(HPROGRAM,YRECFM,XWR(:,JPATCH),IRESP)
-END DO
-#else
- CALL READ_SURF(HPROGRAM,YRECFM,XWR(:,:),IRESP)
-#endif
+YRECFM='WR'
+CALL READ_SURF_FIELD2D(HPROGRAM,XWR,YRECFM)
 !
 !* roughness length of Flood water
 !
 IF(LFLOOD)THEN
   ALLOCATE(XZ0_FLOOD(ILU,NPATCH))
   YRECFM = 'Z0_FLOOD'
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A8,I4.4)') 'Z0_FLOOD',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XZ0_FLOOD(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XZ0_FLOOD(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XZ0_FLOOD,YRECFM)
 ENDIF
 !
 !* Leaf Area Index
 !
 IF (CPHOTO=='LAI' .OR. CPHOTO=='LST' .OR. CPHOTO=='NIT' .OR. CPHOTO=='NCB') THEN
   YRECFM = 'LAI'
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A3,I4.4)') 'LAI',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XLAI(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XLAI(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XLAI,YRECFM)
 END IF
 !
 !* snow mantel
@@ -243,14 +181,7 @@ IF(LGLACIER)THEN
   ALLOCATE(XICE_STO(ILU,NPATCH))
   IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=2) THEN
     YRECFM = 'ICE_STO'
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YRECFM,'(A7,I4.4)') 'ICE_STO',JPATCH
-      CALL READ_SURF(HPROGRAM,YRECFM,XICE_STO(:,JPATCH),IRESP)
-    END DO
-#else
-    CALL READ_SURF(HPROGRAM,YRECFM,XICE_STO(:,:),IRESP)
-#endif
+    CALL READ_SURF_FIELD2D(HPROGRAM,XICE_STO,YRECFM)
   ELSE
     XICE_STO(:,:) = 0.0
   ENDIF
@@ -279,14 +210,7 @@ END IF
 !
 YRECFM = 'RESA'
 XRESA(:,:) = 100.
-#ifdef MNH_PARALLEL
-DO JPATCH=1,NPATCH
-  WRITE(YRECFM,'(A4,I4.4)') 'RESA',JPATCH
-  CALL READ_SURF(HPROGRAM,YRECFM,XRESA(:,JPATCH),IRESP)
-END DO
-#else
- CALL READ_SURF(HPROGRAM,YRECFM,XRESA(:,:),IRESP)
-#endif
+CALL READ_SURF_FIELD2D(HPROGRAM,XRESA,YRECFM)
 !
 !* patch averaged radiative temperature (K)
 !
@@ -309,47 +233,19 @@ XLE(:,:) = XUNDEF
 IF (CPHOTO/='NON') THEN
   YRECFM = 'AN'
   XAN(:,:) = 0.
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A2,I4.4)') 'AN',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XAN(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XAN(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XAN,YRECFM)
   !
   YRECFM = 'ANDAY'
   XANDAY(:,:) = 0.
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A5,I4.4)') 'ANDAY',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XANDAY(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XANDAY(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XANDAY,YRECFM)
   !
   YRECFM = 'ANFM'
   XANFM(:,:) = XANFMINIT
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A4,I4.4)') 'ANFM',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XANFM(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XANFM(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XANFM,YRECFM)
   !
   YRECFM = 'LE_AGS'
   XLE(:,:) = 0.
-#ifdef MNH_PARALLEL
-  DO JPATCH=1,NPATCH
-    WRITE(YRECFM,'(A6,I4.4)') 'LE_AGS',JPATCH
-    CALL READ_SURF(HPROGRAM,YRECFM,XLE(:,JPATCH),IRESP)
-  END DO
-#else
-  CALL READ_SURF(HPROGRAM,YRECFM,XLE(:,:),IRESP)
-#endif
+  CALL READ_SURF_FIELD2D(HPROGRAM,XLE,YRECFM)
 END IF
 !
 IF (CPHOTO=='AGS' .OR. CPHOTO=='AST') THEN
@@ -365,106 +261,38 @@ ELSEIF (CPHOTO=='LAI' .OR. CPHOTO=='LST') THEN
 ELSEIF (CPHOTO=='NIT') THEN
   !
   XBIOMASS(:,:,:) = 0.
-  DO JNBIOMASS=1,NNBIOMASS
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I1,I4.4)') JNBIOMASS,JPATCH
-      IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-       YRECFM='BIOMA'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ELSE
-       YRECFM='BIOMASS'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ENDIF
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XBIOMASS(:,JNBIOMASS,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I1)') JNBIOMASS
-    IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-      YRECFM='BIOMA'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ELSE
-      YRECFM='BIOMASS'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ENDIF
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XBIOMASS(:,JNBIOMASS,:)=ZWORK
-#endif
-  END DO
-
+  IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
+    YRECFM='BIOMA'
+  ELSE
+    YRECFM='BIOMASS'
+  ENDIF
+  CALL READ_SURF_FIELD3D(HPROGRAM,XBIOMASS,1,NNBIOMASS,YRECFM)
+  !
   XRESP_BIOMASS(:,:,:) = 0.
-  DO JNBIOMASS=2,NNBIOMASS
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I1,I4.4)') JNBIOMASS,JPATCH
-      IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-       YRECFM='RESPI'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ELSE
-       YRECFM='RESP_BIOM'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ENDIF
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XRESP_BIOMASS(:,JNBIOMASS,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I1)') JNBIOMASS
-    IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-      YRECFM='RESPI'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ELSE
-      YRECFM='RESP_BIOM'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ENDIF    
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XRESP_BIOMASS(:,JNBIOMASS,:)=ZWORK
-#endif
-  END DO
-
+  IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
+    YRECFM='RESPI'
+  ELSE
+    YRECFM='RESP_BIOM'
+  ENDIF
+  CALL READ_SURF_FIELD3D(HPROGRAM,XRESP_BIOMASS,2,NNBIOMASS,YRECFM)
+  !
 ELSEIF (CPHOTO=='NCB') THEN
   !
   XBIOMASS(:,:,:) = 0.
-  DO JNBIOMASS=1,NNBIOMASS
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I1,I4.4)') JNBIOMASS,JPATCH
-      IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-       YRECFM='BIOMA'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ELSE
-       YRECFM='BIOMASS'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ENDIF
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XBIOMASS(:,JNBIOMASS,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I1)') JNBIOMASS
-    IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-      YRECFM='BIOMA'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ELSE
-      YRECFM='BIOMASS'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ENDIF    
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XBIOMASS(:,JNBIOMASS,:)=ZWORK
-#endif
-  END DO
-
+  IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
+    YRECFM='BIOMA'
+  ELSE
+    YRECFM='BIOMASS'
+  ENDIF
+  CALL READ_SURF_FIELD3D(HPROGRAM,XBIOMASS,1,NNBIOMASS,YRECFM)
+  !
   XRESP_BIOMASS(:,:,:) = 0.
-  DO JNBIOMASS=2,NNBIOMASS-2
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I1,I4.4)') JNBIOMASS,JPATCH
-      IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-       YRECFM='RESPI'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ELSE
-       YRECFM='RESP_BIOM'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      ENDIF
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XRESP_BIOMASS(:,JNBIOMASS,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I1)') JNBIOMASS
-    IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
-      YRECFM='RESPI'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ELSE
-      YRECFM='RESP_BIOM'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    ENDIF    
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XRESP_BIOMASS(:,JNBIOMASS,:)=ZWORK
-#endif
-  END DO
+  IF (IVERSION>7 .OR. IVERSION==7 .AND. IBUGFIX>=3) THEN
+    YRECFM='RESPI'
+  ELSE
+    YRECFM='RESP_BIOM'
+  ENDIF
+  CALL READ_SURF_FIELD3D(HPROGRAM,XRESP_BIOMASS,2,NNBIOMASS-2,YRECFM)
   !
 ENDIF
 !
@@ -482,55 +310,20 @@ IF (CRESPSL=='CNT') THEN
   XLITTER(:,:,:,:) = 0.
   DO JNLITTER=1,NNLITTER
     DO JNLITTLEVS=1,NNLITTLEVS
-#ifdef MNH_PARALLEL
-      DO JPATCH=1,NPATCH
-        WRITE(YPATCH,'(I1,A1,I1,I4.4)') JNLITTER,'_',JNLITTLEVS,JPATCH
-       YRECFM='LITTER'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-       CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-        XLITTER(:,JNLITTER,JNLITTLEVS,JPATCH)=ZWORK(:,JPATCH)
-      END DO
-#else
       WRITE(YLVL,'(I1,A1,I1)') JNLITTER,'_',JNLITTLEVS
       YRECFM='LITTER'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
+      CALL READ_SURF_FIELD2D(HPROGRAM,ZWORK(:,:),YRECFM)
       XLITTER(:,JNLITTER,JNLITTLEVS,:)=ZWORK
-#endif
     END DO
   END DO
 
   XSOILCARB(:,:,:) = 0.
-  DO JNSOILCARB=1,NNSOILCARB
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I4,I4.4)') JNSOILCARB,JPATCH
-      YRECFM='SOILCARB'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XSOILCARB(:,JNSOILCARB,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I4)') JNSOILCARB
-    YRECFM='SOILCARB'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XSOILCARB(:,JNSOILCARB,:)=ZWORK
-#endif
-  END DO
+  YRECFM='SOILCARB'
+  CALL READ_SURF_FIELD3D(HPROGRAM,XSOILCARB,1,NNSOILCARB,YRECFM)
 !
   XLIGNIN_STRUC(:,:,:) = 0.
-  DO JNLITTLEVS=1,NNLITTLEVS
-#ifdef MNH_PARALLEL
-    DO JPATCH=1,NPATCH
-      WRITE(YPATCH,'(I4,I4.4)') JNLITTLEVS,JPATCH
-      YRECFM='LIGNIN_STR'//ADJUSTL(YPATCH(:LEN_TRIM(YPATCH)))
-      CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,JPATCH),IRESP)
-      XLIGNIN_STRUC(:,JNLITTLEVS,JPATCH)=ZWORK(:,JPATCH)
-    END DO
-#else
-    WRITE(YLVL,'(I4)') JNLITTLEVS
-    YRECFM='LIGNIN_STR'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
-    CALL READ_SURF(HPROGRAM,YRECFM,ZWORK(:,:),IRESP)
-    XLIGNIN_STRUC(:,JNLITTLEVS,:)=ZWORK
-#endif
-  END DO
+  YRECFM='LIGNIN_STR'
+  CALL READ_SURF_FIELD3D(HPROGRAM,XLIGNIN_STRUC,1,NNLITTLEVS,YRECFM)
 !
 ENDIF
 !