From df87f6e9e698c31641b25dc34b7f99bfb7c9a700 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Mon, 21 Sep 2015 14:07:40 +0200
Subject: [PATCH] lfi2cdf: open and close files in separate subroutines in all
 cases (cleaner)

---
 tools/lfi2cdf/src/lfi2cdf.f90   | 10 ++--------
 tools/lfi2cdf/src/mode_util.f90 | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/tools/lfi2cdf/src/lfi2cdf.f90 b/tools/lfi2cdf/src/lfi2cdf.f90
index 4b9ae944a..ca153fb22 100644
--- a/tools/lfi2cdf/src/lfi2cdf.f90
+++ b/tools/lfi2cdf/src/lfi2cdf.f90
@@ -19,9 +19,6 @@ subroutine  LFI2CDFMAIN(hinfile,iiflen,ooutname,houtfile,ioflen,hvarlist,ivlen,o
   INTEGER :: nbvar_tbw  ! number of variables to be written
   INTEGER :: nbvar      ! number of defined variables
   INTEGER :: first_level, current_level, last_level
-  INTEGER(KIND=LFI_INT) :: iresp,iverb,inap
-  CHARACTER(LEN=3)      :: suffix
-  CHARACTER(LEN=iiflen) :: filename
   TYPE(cdf_files) :: cdffiles
   TYPE(workfield), DIMENSION(:), POINTER :: tzreclist
 
@@ -87,7 +84,6 @@ subroutine  LFI2CDFMAIN(hinfile,iiflen,ooutname,houtfile,ioflen,hvarlist,ivlen,o
 
      ELSE
      !Treat several LFI files and merge into 1 NC file
-       iverb = 0 !Verbosity level for LFI
 
        !Determine first level (eg needed to find suffix of the variable name)
        read( hinfile(len(hinfile)-6:len(hinfile)-4) , "(I3)" ) first_level
@@ -103,13 +99,11 @@ subroutine  LFI2CDFMAIN(hinfile,iiflen,ooutname,houtfile,ioflen,hvarlist,ivlen,o
        DO current_level = first_level,last_level
          print *,'Treating level ',current_level
          IF (current_level/=first_level) THEN
-           write(suffix,'(I3.3)') current_level
-           filename=hinfile(1:len(hinfile)-7)//suffix//'.lfi'
-           CALL LFIOUV(iresp,ilu,ltrue,filename,'OLD',lfalse,lfalse,iverb,inap,nbvar_lfi)
+           CALL open_split_lfifile(ilu,hinfile,current_level)
            CALL read_data_lfi(ilu,hvarlist,nbvar,tzreclist,ibuflen,current_level)
          END IF
          CALL fill_ncdf(ilu,tzreclist,nbvar,ibuflen,cdffiles,current_level)
-         IF (current_level/=last_level) CALL LFIFER(iresp,ilu,'KEEP')
+         IF (current_level/=last_level) CALL close_split_lfifile(ilu)
        END DO
      END IF
 
diff --git a/tools/lfi2cdf/src/mode_util.f90 b/tools/lfi2cdf/src/mode_util.f90
index 9a1b0dd80..97b371080 100644
--- a/tools/lfi2cdf/src/mode_util.f90
+++ b/tools/lfi2cdf/src/mode_util.f90
@@ -1006,6 +1006,27 @@ print *,'lowmem: not tested!!!!!' (to be compared with no low mem version)
 
   END SUBROUTINE OPEN_FILES
 
+  SUBROUTINE OPEN_SPLIT_LFIFILE(ilu,hinfile,current_level)
+    INTEGER,          INTENT(IN) :: ilu
+    CHARACTER(LEN=*), INTENT(IN) :: hinfile
+    INTEGER,          INTENT(IN) :: current_level
+
+    INTEGER(KIND=LFI_INT) :: iresp,iverb,inap,nbvar
+
+    CHARACTER(LEN=3)      :: suffix
+    CHARACTER(LEN=:),ALLOCATABLE :: filename
+
+    iverb = 0 !Verbosity level for LFI
+
+    ALLOCATE(character(len=len(hinfile)) :: filename)
+
+    write(suffix,'(I3.3)') current_level
+    filename=hinfile(1:len(hinfile)-7)//suffix//'.lfi'
+    CALL LFIOUV(iresp,ilu,ltrue,filename,'OLD',lfalse,lfalse,iverb,inap,nbvar)
+
+    DEALLOCATE(filename)
+  END SUBROUTINE OPEN_SPLIT_LFIFILE
+
   SUBROUTINE OPEN_SPLIT_NCFILES(houtfile,nbvar,tpreclist,cdffiles,ohdf5)
     CHARACTER(LEN=*),              INTENT(IN)    :: houtfile
     INTEGER,                       INTENT(IN)    :: nbvar
@@ -1073,4 +1094,12 @@ print *,'lowmem: not tested!!!!!' (to be compared with no low mem version)
     
   END SUBROUTINE CLOSE_files
 
+  SUBROUTINE CLOSE_SPLIT_LFIFILE(ilu)
+    INTEGER, INTENT(IN) :: ilu
+
+    INTEGER(KIND=LFI_INT) :: iresp
+
+    CALL LFIFER(iresp,ilu,'KEEP')
+  END SUBROUTINE CLOSE_SPLIT_LFIFILE
+
 END MODULE mode_util
-- 
GitLab