Index: IOIPSL/trunk/src/fliocom.f90
===================================================================
--- IOIPSL/trunk/src/fliocom.f90	(revision 953)
+++ IOIPSL/trunk/src/fliocom.f90	(revision 957)
@@ -62,5 +62,5 @@
 !! which contains the dimensions needed.
 !!
-!! SUBROUTINE fliocrfd (f_n,f_d_n,f_d_l,f_i,id_dom,cmode,c_f_n)
+!! SUBROUTINE fliocrfd (f_n,f_d_n,f_d_l,f_i,id_dom,mode,c_f_n)
 !!
 !! INPUT
@@ -86,5 +86,5 @@
 !!              This argument can be equal to FLIO_DOM_DEFAULT
 !!              (see "flio_dom_defset").
-!! (C) cmode  : String of (case insensitive) blank-separated words
+!! (C) mode   : String of (case insensitive) blank-separated words
 !!              defining the mode used to create the file.
 !!              Supported keywords : REPLACE, 32, 64
@@ -837,5 +837,5 @@
 !-
 !===
-SUBROUTINE fliocrfd (f_n,f_d_n,f_d_l,f_i,id_dom,cmode,c_f_n)
+SUBROUTINE fliocrfd (f_n,f_d_n,f_d_l,f_i,id_dom,mode,c_f_n)
 !---------------------------------------------------------------------
   IMPLICIT NONE
@@ -846,5 +846,5 @@
   INTEGER,INTENT(OUT) :: f_i
   INTEGER,OPTIONAL,INTENT(IN) :: id_dom
-  CHARACTER(LEN=*),OPTIONAL,INTENT(IN) :: cmode
+  CHARACTER(LEN=*),OPTIONAL,INTENT(IN) :: mode
   CHARACTER(LEN=*),OPTIONAL,INTENT(OUT) :: c_f_n
 !-
@@ -897,11 +897,11 @@
   i_opt(:)=-1
 !-
-  IF (PRESENT(cmode)) THEN
+  IF (PRESENT(mode)) THEN
 !---
-    IF (LEN_TRIM(cmode) > l_string) THEN
+    IF (LEN_TRIM(mode) > l_string) THEN
       CALL ipslerr (3,'fliocrfd', &
- &     '"cmode" argument','too long','to be treated')
-    ENDIF
-    c_string = cmode(:)
+ &     '"mode" argument','too long','to be treated')
+    ENDIF
+    c_string = mode(:)
     CALL strlowercase (c_string)
 !---
Index: IOIPSL/trunk/src/histcom.f90
===================================================================
--- IOIPSL/trunk/src/histcom.f90	(revision 953)
+++ IOIPSL/trunk/src/histcom.f90	(revision 957)
@@ -120,5 +120,5 @@
 TYPE :: T_D_F
 !-NETCDF IDs for file
-  INTEGER :: ncfid
+  INTEGER :: ncfid=-1
 !-Time variables
   INTEGER :: itau0=0
@@ -2413,5 +2413,5 @@
   INTEGER,INTENT(in),OPTIONAL :: file
 !-
-  INTEGER :: ifile,nfid,iret
+  INTEGER :: ifile,iret,i_s,i_e
 !-
   LOGICAL :: file_exists
@@ -2420,25 +2420,39 @@
   CALL ipsldbg (old_status=l_dbg)
 !-
-  IF (l_dbg) WRITE(*,*) 'Entering loop on files : ',nb_files
-!-
-! 1.The loop on files to synchronise
-!-
-  DO ifile=1,nb_files
-!-
-    IF (PRESENT(file)) THEN
-      file_exists = (ifile == file)
+  IF (l_dbg) THEN
+    WRITE(*,*) "->histsync"
+  ENDIF
+!-
+  IF (PRESENT(file)) THEN
+    IF ( (file >= 1).AND.(file <= nb_files) ) THEN
+      IF (W_F(ifile)%ncfid > 0) THEN
+        i_s = file
+        i_e = file
+      ELSE
+        i_s = 1
+        i_e = 0
+        CALL ipslerr (2,'histsync', &
+ &       'Unable to synchronise the file :','probably','not opened')
+      ENDIF
     ELSE
-      file_exists = .TRUE.
-    ENDIF
-!-
-    IF (file_exists) THEN
+      CALL ipslerr (3,'histsync','Invalid file identifier',' ',' ')
+    ENDIF
+  ELSE
+    i_s = 1
+    i_e = nb_files
+  ENDIF
+!-
+  DO ifile=i_s,i_e
+    IF (W_F(ifile)%ncfid > 0) THEN
       IF (l_dbg) THEN
-        WRITE(*,*) 'Synchronising specified file number :',ifile
+        WRITE(*,*) '  histsync - synchronising file number ',ifile
       ENDIF
-      nfid = W_F(ifile)%ncfid
-      iret = NF90_SYNC (nfid)
-    ENDIF
-!-
+      iret = NF90_SYNC(W_F(ifile)%ncfid)
+    ENDIF
   ENDDO
+!-
+  IF (l_dbg) THEN
+    WRITE(*,*) "<-histsync"
+  ENDIF
 !----------------------
 END SUBROUTINE histsync
@@ -2456,69 +2470,87 @@
   INTEGER,INTENT(in),OPTIONAL :: idf
 !-
-  INTEGER :: ifile,nfid,nvid,iret,iv
-  INTEGER :: start_loop,end_loop
-  CHARACTER(LEN=70) :: str70
+  INTEGER :: ifile,nfid,nvid,iret,iv,i_s,i_e
   LOGICAL :: l_dbg
 !---------------------------------------------------------------------
   CALL ipsldbg (old_status=l_dbg)
 !-
-  IF (l_dbg) WRITE(*,*) 'Entering loop on files :',nb_files
+  IF (l_dbg) THEN
+    WRITE(*,*) "->histclo"
+  ENDIF
 !-
   IF (PRESENT(idf)) THEN
-    start_loop = idf
-    end_loop = idf
-  ELSE
-    start_loop = 1
-    end_loop = nb_files
-  ENDIF
-!-
-  DO ifile=start_loop,end_loop
-    IF (l_dbg) WRITE(*,*) 'Closing specified file number :',ifile
-    nfid = W_F(ifile)%ncfid
-    iret = NF90_REDEF (nfid)
-!---
-!-- 1. Loop on the number of variables to add some final information
-!---
-    IF (l_dbg) WRITE(*,*) 'Entering loop on vars : ',W_F(ifile)%n_var
-    DO iv=1,W_F(ifile)%n_var
-!---- Extrema
-      IF (W_F(ifile)%W_V(iv)%hist_wrt_rng) THEN
-        IF (l_dbg) THEN
-          WRITE(*,*) 'min value for file :',ifile,' var n. :',iv, &
-         &           ' is : ',W_F(ifile)%W_V(iv)%hist_minmax(1)
-          WRITE(*,*) 'max value for file :',ifile,' var n. :',iv, &
-         &           ' is : ',W_F(ifile)%W_V(iv)%hist_minmax(2)
-        ENDIF
-        IF (W_F(ifile)%W_V(iv)%hist_calc_rng) THEN
-!-------- Put the min and max values on the file
-          nvid = W_F(ifile)%W_V(iv)%ncvid
-          IF (W_F(ifile)%W_V(iv)%v_typ == hist_r8) THEN
-            iret = NF90_PUT_ATT (nfid,nvid,'valid_min', &
- &                   REAL(W_F(ifile)%W_V(iv)%hist_minmax(1),KIND=8))
-            iret = NF90_PUT_ATT (nfid,nvid,'valid_max', &
- &                   REAL(W_F(ifile)%W_V(iv)%hist_minmax(2),KIND=8))
-          ELSE
-            iret = NF90_PUT_ATT (nfid,nvid,'valid_min', &
- &                   REAL(W_F(ifile)%W_V(iv)%hist_minmax(1),KIND=4))
-            iret = NF90_PUT_ATT (nfid,nvid,'valid_max', &
- &                   REAL(W_F(ifile)%W_V(iv)%hist_minmax(2),KIND=4))
+    IF ( (idf >= 1).AND.(idf <= nb_files) ) THEN
+      IF (W_F(ifile)%ncfid > 0) THEN
+        i_s = idf
+        i_e = idf
+      ELSE
+        i_s = 1
+        i_e = 0
+        CALL ipslerr (2,'histclo', &
+ &       'Unable to close the file :','probably','not opened')
+      ENDIF
+    ELSE
+      CALL ipslerr (3,'histclo','Invalid file identifier',' ',' ')
+    ENDIF
+  ELSE
+    i_s = 1
+    i_e = nb_files
+  ENDIF
+!-
+  DO ifile=i_s,i_e
+    IF (W_F(ifile)%ncfid > 0) THEN
+      IF (l_dbg) THEN
+        WRITE(*,*) '  histclo - closing specified file number :',ifile
+      ENDIF
+      nfid = W_F(ifile)%ncfid
+      iret = NF90_REDEF(nfid)
+!-----
+!---- 1. Loop on the number of variables to add some final information
+!-----
+      IF (l_dbg) THEN
+        WRITE(*,*) '  Entering loop on vars : ',W_F(ifile)%n_var
+      ENDIF
+      DO iv=1,W_F(ifile)%n_var
+!------ Extrema
+        IF (W_F(ifile)%W_V(iv)%hist_wrt_rng) THEN
+          IF (l_dbg) THEN
+            WRITE(*,*) 'min value for file :',ifile,' var n. :',iv, &
+ &                     ' is : ',W_F(ifile)%W_V(iv)%hist_minmax(1)
+            WRITE(*,*) 'max value for file :',ifile,' var n. :',iv, &
+ &                     ' is : ',W_F(ifile)%W_V(iv)%hist_minmax(2)
+          ENDIF
+          IF (W_F(ifile)%W_V(iv)%hist_calc_rng) THEN
+!---------- Put the min and max values on the file
+            nvid = W_F(ifile)%W_V(iv)%ncvid
+            IF (W_F(ifile)%W_V(iv)%v_typ == hist_r8) THEN
+              iret = NF90_PUT_ATT(nfid,nvid,'valid_min', &
+ &                     REAL(W_F(ifile)%W_V(iv)%hist_minmax(1),KIND=8))
+              iret = NF90_PUT_ATT(nfid,nvid,'valid_max', &
+ &                     REAL(W_F(ifile)%W_V(iv)%hist_minmax(2),KIND=8))
+            ELSE
+              iret = NF90_PUT_ATT(nfid,nvid,'valid_min', &
+ &                     REAL(W_F(ifile)%W_V(iv)%hist_minmax(1),KIND=4))
+              iret = NF90_PUT_ATT(nfid,nvid,'valid_max', &
+ &                     REAL(W_F(ifile)%W_V(iv)%hist_minmax(2),KIND=4))
+            ENDIF
           ENDIF
         ENDIF
-      ENDIF
-!---- Time-Buffers
-      IF (ASSOCIATED(W_F(ifile)%W_V(iv)%t_bf)) THEN
-        DEALLOCATE(W_F(ifile)%W_V(iv)%t_bf)
-      ENDIF
-    ENDDO
-!---
-!-- 2. Close the file
-!---
-    IF (l_dbg) WRITE(*,*) 'close file :',nfid
-    iret = NF90_CLOSE (nfid)
-    IF (iret /= NF90_NOERR) THEN
-      WRITE(str70,'("This file has been already closed :",I3)') ifile
-      CALL ipslerr (2,'histclo',str70,'','')
+!------ Time-Buffers
+        IF (ASSOCIATED(W_F(ifile)%W_V(iv)%t_bf)) THEN
+          DEALLOCATE(W_F(ifile)%W_V(iv)%t_bf)
+        ENDIF
+      ENDDO
+!-----
+!---- 2. Close the file
+!-----
+      IF (l_dbg) WRITE(*,*) '  close file :',nfid
+      iret = NF90_CLOSE(nfid)
+      W_F(ifile)%ncfid = -1
     ENDIF
   ENDDO
+!-
+  IF (l_dbg) THEN
+    WRITE(*,*) "<-histclo"
+  ENDIF
 !---------------------
 END SUBROUTINE histclo
