Index: /trunk/LMDZ.MARS/README
===================================================================
--- /trunk/LMDZ.MARS/README	(revision 319)
+++ /trunk/LMDZ.MARS/README	(revision 320)
@@ -1079,2 +1079,9 @@
 - Monin-Obukhov length is now output from surflay_interpol and written in diagfi if z_out not 0.
 - in calltherm_interface, defaut settings for qtransport_thermals and dtke_thermals
+
+== 20/10/2011 == EM
+- added FF's upgrade of writediagfi. Now, if at runtime there is a diagfi.def
+  file, it should contain the list of variables (1 per line) than will be put
+  in the diagfi.nc file. If there is no diagfi.def file, then all variables
+  are put in the diagfi.nc file (as was the case before).
+ 
Index: /trunk/LMDZ.MARS/libf/phymars/writediagfi.F
===================================================================
--- /trunk/LMDZ.MARS/libf/phymars/writediagfi.F	(revision 319)
+++ /trunk/LMDZ.MARS/libf/phymars/writediagfi.F	(revision 320)
@@ -22,4 +22,6 @@
 !  
 ! Modifs: Aug.2010 Ehouarn: enforce outputs to be real*4
+!         Oct 2011 Francois: enable having a 'diagfi.def' file to select
+!                            at runtime, which variables to put in file
 !
 !  parametres (input) :
@@ -53,8 +55,8 @@
 
 ! Arguments on input:
-      integer ngrid
-      character (len=*) :: nom,titre,unite
-      integer dim
-      real px(ngrid,llm)
+      integer,intent(in) :: ngrid
+      character (len=*),intent(in) :: nom,titre,unite
+      integer,intent(in) :: dim
+      real,intent(in) :: px(ngrid,llm)
 
 ! Local variables:
@@ -65,20 +67,15 @@
       real*4 dx0
 
-      real*4 date
+      real*4,save :: date
 
       REAL phis(ip1jmp1)
 
       integer irythme
-      integer ierr
+      integer ierr,ierr2
       integer iq
       integer i,j,l,zmax , ig0
 
-      integer zitau
-      character firstnom*20
-      SAVE firstnom
-      SAVE zitau
-      SAVE date
-      data firstnom /'1234567890'/
-      data zitau /0/
+      integer,save :: zitau=0
+      character(len=20),save :: firstnom='1234567890'
 
 ! Ajouts
@@ -86,7 +83,16 @@
       integer :: idim,varid
       integer :: nid
-      character (len =50):: fichnom
+      character(len=*),parameter :: fichnom="diagfi.nc"
       integer, dimension(4) :: id
       integer, dimension(4) :: edges,corner
+
+! Added to use diagfi.def to select output variable
+      logical,save :: diagfi_def
+      logical :: getout
+      integer,save :: n_nom_def
+      integer :: n
+      integer,parameter :: n_nom_def_max=99
+      character(len=20),save :: nom_def(n_nom_def_max)
+      logical,save :: firstcall=.true.
       
 #ifndef MESOSCALE
@@ -102,13 +108,50 @@
 !***************************************************************
 
+! At very first call, check if there is a "diagfi.def" to use and read it
+! -----------------------------------------------------------------------
+      IF (firstcall) THEN
+         firstcall=.false.
+
+  !      Open diagfi.def definition file if there is one:
+         open(99,file="diagfi.def",status='old',form='formatted',
+     s   iostat=ierr2)
+
+         if(ierr2.eq.0) then
+            diagfi_def=.true.
+            write(*,*) "******************"
+            write(*,*) "Reading diagfi.def"
+            write(*,*) "******************"
+            do n=1,n_nom_def_max
+              read(99,fmt='(a)',end=88) nom_def(n)
+              write(*,*) 'Output in diagfi: ', trim(nom_def(n))
+            end do 
+ 88         continue
+            if (n.ge.n_nom_def_max) then
+               write(*,*)"n_nom_def_max too small in writediagfi.F:",n
+               stop
+            end if 
+            n_nom_def=n-1
+            close(99)
+         else
+            diagfi_def=.false.
+         endif
+      END IF ! of IF (firstcall)
+
+! Get out of write_diagfi if there is diagfi.def AND variable not listed
+!  ---------------------------------------------------------------------
+      if (diagfi_def) then
+          getout=.true.
+          do n=1,n_nom_def
+             if(trim(nom_def(n)).eq.nom) getout=.false.
+          end do
+          if (getout) return
+      end if
 
 ! Initialisation of 'firstnom' and create/open the "diagfi.nc" NetCDF file
 ! ------------------------------------------------------------------------
-! (Au tout premier appel de la subroutine durant le run.)
-
-      fichnom="diagfi.nc"
-
-      if (firstnom.eq.'1234567890') then ! .true. for the very first call
-      !  to this subroutine; now set 'firstnom'
+! (at very first call to the subroutine, in accordance with diagfi.def)
+
+      if (firstnom.eq.'1234567890') then ! .true. for the very first valid
+      !   call to this subroutine; now set 'firstnom'
          firstnom = nom
          ! just to be sure, check that firstnom is large enough to hold nom
