Index: LMDZ6/trunk/libf/phylmd/iostart.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/iostart.F90	(revision 3465)
+++ LMDZ6/trunk/libf/phylmd/iostart.F90	(revision 3506)
@@ -25,5 +25,5 @@
 
     PUBLIC get_field,get_var,put_field,put_var
-    PUBLIC Open_startphy,close_startphy,open_restartphy,close_restartphy
+    PUBLIC Open_startphy,close_startphy,open_restartphy,close_restartphy, enddef_restartphy
     
 CONTAINS
@@ -326,9 +326,19 @@
       ierr = NF90_DEF_DIM (nid_restart, "horizon_klevp1", klon_glo*klevp1, idim4)
 
-      ierr = NF90_ENDDEF(nid_restart)
+!      ierr = NF90_ENDDEF(nid_restart)
     ENDIF
 
   END SUBROUTINE open_restartphy
   
+  SUBROUTINE enddef_restartphy
+  USE netcdf
+  USE mod_phys_lmdz_para
+  IMPLICIT NONE
+    INTEGER          :: ierr
+
+    IF (is_master) ierr = NF90_ENDDEF(nid_restart)
+ 
+  END SUBROUTINE enddef_restartphy
+
   SUBROUTINE close_restartphy
   USE netcdf
@@ -337,42 +347,42 @@
     INTEGER          :: ierr
 
-    IF (is_mpi_root .AND. is_omp_root) THEN
-      ierr = NF90_CLOSE (nid_restart)
-    ENDIF
+    IF (is_master) ierr = NF90_CLOSE (nid_restart)
  
   END SUBROUTINE close_restartphy
 
   
-  SUBROUTINE put_field_r1(field_name,title,field)
-  IMPLICIT NONE
+  SUBROUTINE put_field_r1(pass, field_name,title,field)
+  IMPLICIT NONE
+  INTEGER, INTENT(IN)            :: pass
   CHARACTER(LEN=*),INTENT(IN)    :: field_name
   CHARACTER(LEN=*),INTENT(IN)    :: title
   REAL,INTENT(IN)                :: field(:)
-  
-    CALL put_field_rgen(field_name,title,field,1)
+    CALL put_field_rgen(pass, field_name,title,field,1)
   
   END SUBROUTINE put_field_r1
 
-  SUBROUTINE put_field_r2(field_name,title,field)
-  IMPLICIT NONE
+  SUBROUTINE put_field_r2(pass, field_name,title,field)
+  IMPLICIT NONE
+  INTEGER, INTENT(IN)            :: pass
   CHARACTER(LEN=*),INTENT(IN)    :: field_name
   CHARACTER(LEN=*),INTENT(IN)    :: title
   REAL,INTENT(IN)                :: field(:,:)
   
-    CALL put_field_rgen(field_name,title,field,size(field,2))
+    CALL put_field_rgen(pass, field_name,title,field,size(field,2))
   
   END SUBROUTINE put_field_r2
 
-  SUBROUTINE put_field_r3(field_name,title,field)
-  IMPLICIT NONE
+  SUBROUTINE put_field_r3(pass, field_name,title,field)
+  IMPLICIT NONE
+  INTEGER, INTENT(IN)            :: pass
   CHARACTER(LEN=*),INTENT(IN)    :: field_name
   CHARACTER(LEN=*),INTENT(IN)    :: title
   REAL,INTENT(IN)                :: field(:,:,:)
   
-    CALL put_field_rgen(field_name,title,field,size(field,2)*size(field,3))
+    CALL put_field_rgen(pass, field_name,title,field,size(field,2)*size(field,3))
   
   END SUBROUTINE put_field_r3
   
-  SUBROUTINE put_field_rgen(field_name,title,field,field_size)
+  SUBROUTINE put_field_rgen(pass, field_name,title,field,field_size)
   USE netcdf
   USE dimphy
@@ -381,4 +391,5 @@
   USE mod_phys_lmdz_para
   IMPLICIT NONE
+  INTEGER, INTENT(IN)            :: pass
   CHARACTER(LEN=*),INTENT(IN)    :: field_name
   CHARACTER(LEN=*),INTENT(IN)    :: title
@@ -386,24 +397,18 @@
   REAL,INTENT(IN)                :: field(klon,field_size)
   
-  REAL                           :: field_glo(klon_glo,field_size)
-  REAL                           :: field_glo_tmp(klon_glo,field_size)
-!  INTEGER,ALLOCATABLE            :: ind_cell_glo_glo(:)
-  INTEGER                        :: ind_cell_glo_glo(klon_glo)
+!  REAL                           :: field_glo(klon_glo,field_size)
+!  REAL                           :: field_glo_tmp(klon_glo,field_size)
+  REAL ,ALLOCATABLE              :: field_glo(:,:)
+  REAL ,ALLOCATABLE              :: field_glo_tmp(:,:)
+  INTEGER,ALLOCATABLE            :: ind_cell_glo_glo(:)
+!  INTEGER                        :: ind_cell_glo_glo(klon_glo)
   INTEGER                        :: ierr,i
   INTEGER                        :: nvarid
   INTEGER                        :: idim
    
-   
-!    IF (is_master) ALLOCATE(ind_cell_glo_glo(klon_glo))
-    CALL gather(ind_cell_glo,ind_cell_glo_glo)
-
-    CALL gather(field,field_glo_tmp)
+! first pass : definition   
+  IF (pass==1) THEN
     
     IF (is_master) THEN
-
-      DO i=1,klon_glo
-       field_glo(ind_cell_glo_glo(i),:)=field_glo_tmp(i,:)
-      ENDDO
-
 
       IF (field_size==1) THEN
@@ -418,5 +423,5 @@
       ENDIF
          
-      ierr = NF90_REDEF (nid_restart)
+!      ierr = NF90_REDEF (nid_restart)
 #ifdef NC_DOUBLE
       ierr = NF90_DEF_VAR (nid_restart, field_name, NF90_DOUBLE,(/ idim /),nvarid)
@@ -425,12 +430,40 @@
 #endif
       IF (LEN_TRIM(title) > 0) ierr = NF90_PUT_ATT (nid_restart,nvarid,"title", title)
-      ierr = NF90_ENDDEF(nid_restart)
-      ierr = NF90_PUT_VAR(nid_restart,nvarid,RESHAPE(field_glo,(/klon_glo*field_size/)))
-    ENDIF
-    
-   END SUBROUTINE put_field_rgen  
-  
-   SUBROUTINE put_var_r0(var_name,title,var)
+!      ierr = NF90_ENDDEF(nid_restart)
+     ENDIF
+
+! second pass : write     
+   ELSE IF (pass==2) THEN 
+    
+     IF (is_master) THEN
+       ALLOCATE(ind_cell_glo_glo(klon_glo))
+       ALLOCATE(field_glo(klon_glo,field_size))
+       ALLOCATE(field_glo_tmp(klon_glo,field_size))
+     ELSE
+       ALLOCATE(ind_cell_glo_glo(0))
+       ALLOCATE(field_glo(0,0))
+     ENDIF
+     
+     CALL gather(ind_cell_glo,ind_cell_glo_glo) 
+
+     CALL gather(field,field_glo_tmp)
+    
+     IF (is_master) THEN
+
+       DO i=1,klon_glo
+         field_glo(ind_cell_glo_glo(i),:)=field_glo_tmp(i,:)
+       ENDDO
+
+       ierr = NF90_INQ_VARID(nid_restart, field_name, nvarid)
+       ierr = NF90_PUT_VAR(nid_restart,nvarid,RESHAPE(field_glo,(/klon_glo*field_size/)))
+      ENDIF
+   ENDIF
+    
+ END SUBROUTINE put_field_rgen  
+  
+
+ SUBROUTINE put_var_r0(pass, var_name,title,var)
    IMPLICIT NONE
+     INTEGER, INTENT(IN)            :: pass
      CHARACTER(LEN=*),INTENT(IN) :: var_name
      CHARACTER(LEN=*),INTENT(IN) :: title
@@ -440,53 +473,57 @@
      varin(1)=var
      
-     CALL put_var_rgen(var_name,title,varin,size(varin))
+     CALL put_var_rgen(pass, var_name,title,varin,size(varin))
 
   END SUBROUTINE put_var_r0
 
 
-   SUBROUTINE put_var_r1(var_name,title,var)
+   SUBROUTINE put_var_r1(pass, var_name,title,var)
    IMPLICIT NONE
+     INTEGER, INTENT(IN)            :: pass
      CHARACTER(LEN=*),INTENT(IN) :: var_name
      CHARACTER(LEN=*),INTENT(IN) :: title
      REAL,INTENT(IN)             :: var(:)
      
-     CALL put_var_rgen(var_name,title,var,size(var))
+     CALL put_var_rgen(pass, var_name,title,var,size(var))
 
   END SUBROUTINE put_var_r1
  
-  SUBROUTINE put_var_r2(var_name,title,var)
+  SUBROUTINE put_var_r2(pass, var_name,title,var)
    IMPLICIT NONE
+     INTEGER, INTENT(IN)            :: pass
      CHARACTER(LEN=*),INTENT(IN) :: var_name
      CHARACTER(LEN=*),INTENT(IN) :: title
      REAL,INTENT(IN)             :: var(:,:)
      
-     CALL put_var_rgen(var_name,title,var,size(var))
+     CALL put_var_rgen(pass, var_name,title,var,size(var))
 
   END SUBROUTINE put_var_r2     
   
-  SUBROUTINE put_var_r3(var_name,title,var)
+  SUBROUTINE put_var_r3(pass, var_name,title,var)
    IMPLICIT NONE
+     INTEGER, INTENT(IN)            :: pass
      CHARACTER(LEN=*),INTENT(IN) :: var_name
      CHARACTER(LEN=*),INTENT(IN) :: title
      REAL,INTENT(IN)             :: var(:,:,:)
      
-     CALL put_var_rgen(var_name,title,var,size(var))
+     CALL put_var_rgen(pass, var_name,title,var,size(var))
 
   END SUBROUTINE put_var_r3
 
-  SUBROUTINE put_var_rgen(var_name,title,var,var_size)
+  SUBROUTINE put_var_rgen(pass, var_name,title,var,var_size)
   USE netcdf
   USE dimphy
   USE mod_phys_lmdz_para
   IMPLICIT NONE
-     CHARACTER(LEN=*),INTENT(IN) :: var_name
-     CHARACTER(LEN=*),INTENT(IN) :: title
-     INTEGER,INTENT(IN)          :: var_size
-     REAL,INTENT(IN)             :: var(var_size)
-     
-     INTEGER :: ierr
-     INTEGER :: nvarid
+    INTEGER, INTENT(IN)         :: pass
+    CHARACTER(LEN=*),INTENT(IN) :: var_name
+    CHARACTER(LEN=*),INTENT(IN) :: title
+    INTEGER,INTENT(IN)          :: var_size
+    REAL,INTENT(IN)             :: var(var_size)
+    
+    INTEGER :: ierr
+    INTEGER :: nvarid
          
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 
       IF (var_size/=length) THEN
@@ -494,17 +531,23 @@
         call abort_physic("", "", 1)
       ENDIF
-      
-      ierr = NF90_REDEF (nid_restart)
+
+     ! first pass : definition   
+      IF (pass==1) THEN
+        
+!      ierr = NF90_REDEF (nid_restart)
 
 #ifdef NC_DOUBLE
-      ierr = NF90_DEF_VAR (nid_restart, var_name, NF90_DOUBLE,(/ idim1 /),nvarid)
+        ierr = NF90_DEF_VAR (nid_restart, var_name, NF90_DOUBLE,(/ idim1 /),nvarid)
 #else
-      ierr = NF90_DEF_VAR (nid_restart, var_name, NF90_FLOAT,(/ idim1 /),nvarid)
+        ierr = NF90_DEF_VAR (nid_restart, var_name, NF90_FLOAT,(/ idim1 /),nvarid)
 #endif
-      IF (LEN_TRIM(title)>0) ierr = NF90_PUT_ATT (nid_restart,nvarid,"title", title)
-      ierr = NF90_ENDDEF(nid_restart)
-     
-      ierr = NF90_PUT_VAR(nid_restart,nvarid,var)
-
+        IF (LEN_TRIM(title)>0) ierr = NF90_PUT_ATT (nid_restart,nvarid,"title", title)
+!      ierr = NF90_ENDDEF(nid_restart)
+
+    ! second pass : write     
+      ELSE IF (pass==2) THEN 
+        ierr = NF90_INQ_VARID(nid_restart, var_name, nvarid)
+        ierr = NF90_PUT_VAR(nid_restart,nvarid,var)
+      ENDIF
     ENDIF
     
