Index: dynamico_lmdz/simple_physics/bash/beautify.sh
===================================================================
--- dynamico_lmdz/simple_physics/bash/beautify.sh	(revision 4234)
+++ dynamico_lmdz/simple_physics/bash/beautify.sh	(revision 4235)
@@ -1,3 +1,9 @@
 #!/bin/bash
+
+# NOTE : sed -i seems to work differently on GNU and OSX systems
+# a temporary file is used instead
+
+TMP=$(mktemp)
+
 for x in $* ; do
     # indent source file using emacs in batch mode
@@ -5,4 +11,7 @@
     emacs -batch $x --eval '(f90-mode)' --eval '(indent-region (point-min) (point-max) nil)' --eval '(delete-trailing-whitespace)' -f save-buffer 2>/dev/null
     # remove blank lines at end of file
-    sed -i '' -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $x 
+    sed  -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $x  > $TMP
+    cp -f $TMP $x
 done
+
+rm -f $TMP
Index: dynamico_lmdz/simple_physics/bash/check_F2003.sh
===================================================================
--- dynamico_lmdz/simple_physics/bash/check_F2003.sh	(revision 4235)
+++ dynamico_lmdz/simple_physics/bash/check_F2003.sh	(revision 4235)
@@ -0,0 +1,2 @@
+#!/bin/bash
+F90=gfortran F90FLAGS="-ffree-line-length-none -std=f2003 -Werror" make clean all
Index: dynamico_lmdz/simple_physics/bash/concatenate_all_code.sh
===================================================================
--- dynamico_lmdz/simple_physics/bash/concatenate_all_code.sh	(revision 4234)
+++ dynamico_lmdz/simple_physics/bash/concatenate_all_code.sh	(revision 4235)
@@ -4,4 +4,4 @@
 
 for src in $* ; do
-    awk "{ print \"$src \" \$0; }" physics/${src}.F90
+    awk "{ print \"$src \" NR \" \" \$0; }" physics/${src}.F90
 done
Index: dynamico_lmdz/simple_physics/bash/makedeps.sh
===================================================================
--- dynamico_lmdz/simple_physics/bash/makedeps.sh	(revision 4234)
+++ dynamico_lmdz/simple_physics/bash/makedeps.sh	(revision 4235)
@@ -3,8 +3,8 @@
 # input file $1 is output of bash/concatenate_all_code.sh, see phyparam/Makefile
 
-function find_use()
+function print_deps()
 {
-    grep '\bUSE\b' | grep -i -v '\bEND\b' | tr ',' ' ' | awk '{print "obj/" $1 ".o : " "obj/" $3 ".o" }'
+    tr ',' ' ' | awk '{print "obj/" $1 ".o : " "obj/" $4 ".o" }'
 }
 
-cat $1 | find_use
+grep '\bUSE\b' $1 | grep -i -v 'INTRINSIC' | grep -i -v '\bEND\b' | print_deps
Index: dynamico_lmdz/simple_physics/bash/remove_quote_in_comments
===================================================================
--- dynamico_lmdz/simple_physics/bash/remove_quote_in_comments	(revision 4234)
+++ 	(revision )
@@ -1,10 +1,0 @@
-#!/bin/bash
-
-# some quotes in French-language comments break strict conformance to F2003
-# this script removes them but could affect real strings ending with a single l or d after a space
-# => check with svn diff after applying
-
-for x in $* ; do 
-    sed -i -e "s/ l'/ l /g" $x
-    sed -i -e "s/ d'/ d /g" $x
-done
Index: dynamico_lmdz/simple_physics/bash/remove_quote_in_comments.sh
===================================================================
--- dynamico_lmdz/simple_physics/bash/remove_quote_in_comments.sh	(revision 4235)
+++ dynamico_lmdz/simple_physics/bash/remove_quote_in_comments.sh	(revision 4235)
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# some quotes in French-language comments break strict conformance to F2003
+# this script removes them but could affect real strings ending with a single l or d after a space
+# => check with svn diff after applying
+
+for x in $* ; do 
+    sed -i -e "s/ l'/ l /g" $x
+    sed -i -e "s/ d'/ d /g" $x
+done
Index: dynamico_lmdz/simple_physics/phyparam/DYNAMICO/icosa_phyparam_mod.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/DYNAMICO/icosa_phyparam_mod.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/DYNAMICO/icosa_phyparam_mod.F90	(revision 4235)
@@ -1,5 +1,19 @@
 MODULE icosa_phyparam_mod
+#include "use_logging.h"
+
+! FCM gets confused when external modules are USEd at module level
+! => USE statements to DYNAMICO modules go into subroutines
+
   IMPLICIT NONE
+  PRIVATE
   SAVE
+
+  LOGICAL :: firstcall = .TRUE.
+  LOGICAL, PARAMETER :: lastcall = .FALSE.
+
+  REAL, PARAMETER :: oneday = 86400. ! hard-coded                                                                                                                                                         
+  INTEGER, PARAMETER :: log_unit = 15
+
+  PUBLIC :: init_physics, physics
 
   CONTAINS
@@ -7,26 +21,49 @@
     SUBROUTINE init_physics
       ! DYNAMICO
-      USE mpipara,   ONLY : is_mpi_master
-      USE icosa,     ONLY : llm, g, radius, cpp, kappa 
-      USE time_mod,  ONLY : dt, itau_physics
+      USE mpipara,  ONLY : is_mpi_master
+      USE icosa,    ONLY : llm
+
+      USE icosa,     ONLY : g, radius, cpp, kappa 
       USE getin_mod, ONLY : getin
-      USE physics_interface_mod
+      USE physics_interface_mod, ONLY : inout => physics_inout
       ! phyparam
+      USE logging, ONLY : flush_plugin, dbtag, max_log_level
+      USE read_param_mod
       USE comgeomfi
       USE iniphyparam_mod
       INTEGER, PARAMETER :: dayref=0
-      INTEGER :: ngrid
+      CHARACTER(10) :: physics_log_level
+      INTEGER :: ngrid, lev
+      REAL    :: timestep
       REAL    :: unjours ! solar day in seconds
 
-      IF(is_mpi_master) WRITE(*,*) 'phyparam/init_physics called'
-      
-      ngrid = physics_inout%ngrid
+      flush_plugin => flush_log_
+
+      physics_log_level='INF'
+      CALL getin('physics_log_level', physics_log_level)
+      DO lev=1, SIZE(dbtag)
+         IF(dbtag(lev)==TRIM(physics_log_level)) THEN
+            max_log_level = lev
+            EXIT
+         END IF
+      END DO
+
+      read_paramr_plugin => read_paramr
+      read_parami_plugin => read_parami
+      read_paramb_plugin => read_paramb
+
+      WRITELOG(*,*) 'init_physics called'
+      WRITELOG(*,*) 'physics log level set to ', dbtag(max_log_level)
+      LOG_INFO('phyparam')
+
+      ngrid = inout%ngrid
+      timestep = inout%dt_phys
+
       unjours = 86400.
       CALL getin('unjours', unjours)
 
-      CALL init_comgeomfi(ngrid, llm, &
-           &              physics_inout%lon, physics_inout%lat)
+      CALL init_comgeomfi(ngrid, llm, inout%lon, inout%lat)
       CALL iniphyparam(ngrid, llm,                &
-           &           unjours, dayref, dt*itau_physics, & 
+           &           unjours, dayref, timestep, & 
            &           radius, g, cpp*kappa, cpp)
 
@@ -34,7 +71,114 @@
 
     SUBROUTINE physics
-      USE mpipara, ONLY : is_mpi_master
-      IF(is_mpi_master) PRINT *, 'phyparam/physics called'
+      USE mpipara,  ONLY : is_mpi_master
+      USE icosa,    ONLY : llm
+      USE physics_interface_mod, ONLY : inout => physics_inout
+      USE phyparam_mod
+      USE error_mod
+      REAL :: dps(inout%ngrid), play(inout%ngrid, llm), pphi(inout%ngrid, llm)
+      REAL :: timestep, time, jourvrai, gmtime
+      INTEGER :: l
+      IF(is_mpi_master) WRITE(log_unit,*) 'phyparam/physics called', SHAPE(inout%p), SHAPE(inout%pk)      
+
+      timestep = inout%dt_phys
+      time = timestep * inout%it
+      gmtime = time/oneday
+      jourvrai = FLOOR(gmtime)
+      gmtime   = gmtime - jourvrai
+
+      ! compute pressure and geopotential at full levels
+      CALL compute_play(inout%ngrid, llm, inout%p, play)
+      CALL compute_play(inout%ngrid, llm, inout%geopot, pphi)
+
+      ! substract surface geopotential
+      DO l=1,llm
+         pphi(:,l) = pphi(:,l) - inout%geopot(:,1)
+      END DO
+
+      IF(is_mpi_master) PRINT *, 'phyparam phi :', pphi(inout%ngrid/2+1, :)
+
+      CALL check_NaN('physics', 'ulon', inout%ulon)
+      CALL check_NaN('physics', 'ulat', inout%ulat)
+      CALL check_NaN('physics', 'temp', inout%temp)
+
+      ! go
+      CALL phyparam(inout%ngrid,llm,                       &
+           &        firstcall,lastcall,                    &
+           &        jourvrai, gmtime, timestep,            &
+           &        inout%p, play, pphi,                   &
+           &        inout%ulon,  inout%ulat,  inout%temp,  &
+           &        inout%dulon, inout%dulat, inout%dtemp, dps)
+
+      IF(is_mpi_master) PRINT *, 'phyparam dT :', inout%dtemp(inout%ngrid/2+1, :)
+
+      CALL check_NaN('physics', 'dulon', inout%dulon)
+      CALL check_NaN('physics', 'dulat', inout%dulat)
+      CALL check_NaN('physics', 'dtemp', inout%dtemp)
+
+      firstcall = .FALSE.
     END SUBROUTINE physics
 
+    SUBROUTINE compute_play(ngrid, llm, plev, play)
+      INTEGER, INTENT(IN) :: ngrid, llm
+      REAL, INTENT(IN)    :: plev(ngrid, llm+1) ! pressure at interfaces (half-levels)
+      REAL, INTENT(OUT)   :: play(ngrid, llm)   ! pressure in layers (full levels)
+      INTEGER :: ij, l
+      DO l = 1,llm
+         DO ij = 1,ngrid
+            play(ij,l) = .5*(plev(ij,l)+plev(ij,l+1))
+         END DO
+      END DO
+    END SUBROUTINE compute_play
+
+!------------------------------------------------------------------------------------
+!------------------------------- Infrastructure plugins -----------------------------
+
+!--------------------------------------- Logging ------------------------------------
+
+    SUBROUTINE flush_log_(lev, tag, buf)
+      USE mpipara, ONLY : is_mpi_master
+      USE logging, ONLY : dbtag
+      INTEGER, INTENT(IN) :: lev
+      CHARACTER(*), INTENT(IN) :: tag, buf(:)
+      CHARACTER(100) :: prefix
+      INTEGER :: i
+
+      IF(is_mpi_master) THEN
+         WRITE(prefix,*) '[', dbtag(lev), ' ', tag, ']'
+         DO i=1, SIZE(buf)
+            WRITE(log_unit,*) TRIM(prefix), TRIM(buf(i))
+         END DO
+         WRITE(log_unit, *) ''
+      END IF
+    END SUBROUTINE flush_log_
+
+!--------------------------------------- read_param ------------------------------------
+
+    SUBROUTINE read_paramr(name, defval, val, comment)
+      USE getin_mod, ONLY : getin
+      CHARACTER(*), INTENT(IN) :: name, comment
+      REAL, INTENT(IN)         :: defval
+      REAL, INTENT(OUT)        :: val
+      val = defval
+      CALL getin(name, val)
+    END SUBROUTINE read_paramr
+
+    SUBROUTINE read_parami(name, defval, val, comment)
+      USE getin_mod, ONLY : getin
+      CHARACTER(*), INTENT(IN) :: name, comment
+      INTEGER, INTENT(IN)      :: defval
+      INTEGER, INTENT(OUT)     :: val
+      val = defval
+      CALL getin(name, val)
+    END SUBROUTINE read_parami
+    
+    SUBROUTINE read_paramb(name, defval, val, comment)
+      USE getin_mod, ONLY : getin
+      CHARACTER(*), INTENT(IN) :: name, comment
+      LOGICAL, INTENT(IN)      :: defval
+      LOGICAL, INTENT(OUT)     :: val
+      val = defval
+      CALL getin(name, val)
+    END SUBROUTINE read_paramb
+
 END MODULE icosa_phyparam_mod
Index: dynamico_lmdz/simple_physics/phyparam/Makefile
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/Makefile	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/Makefile	(revision 4235)
@@ -11,5 +11,4 @@
 F90FLAGS ?= -ffree-line-length-none
 
-VPATH = obj
 NAMES = $(basename $(notdir $(wildcard physics/*.F90)))
 OBJECTS = $(addprefix obj/, $(addsuffix .o, $(NAMES)))
@@ -18,12 +17,12 @@
 
 obj :
+	@rm -rf obj include lib xml tmp *~ */*~
 	@mkdir obj include lib xml tmp
 
-deps : 
+clean :
+	@rm -rf obj include lib xml tmp *~ */*~
+	@mkdir obj include lib xml tmp
 	@../bash/concatenate_all_code.sh $(NAMES) > tmp/all_code
 	@../bash/makedeps.sh tmp/all_code >  Makefile.inc
-
-clean : deps
-	rm -rf obj include lib xml tmp *~ */*~
 
 nice:
@@ -41,3 +40,6 @@
 	@$(FFRONT) -M tmp tmp/$(SRC).f90 > xml/$(SRC).xml
 
+# dependencies between modules
+
+$(OBJECTS) : obj/logging.o
 -include Makefile.inc
Index: dynamico_lmdz/simple_physics/phyparam/check_F2003.sh
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/check_F2003.sh	(revision 4234)
+++ 	(revision )
@@ -1,2 +1,0 @@
-#!/bin/bash
-F90=gfortran F90FLAGS="-ffree-line-length-none -std=f2003 -Werror" make clean all
Index: dynamico_lmdz/simple_physics/phyparam/physics/callkeys.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/callkeys.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/physics/callkeys.F90	(revision 4235)
@@ -3,7 +3,7 @@
   SAVE
 
-  LOGICAL callrad,calldifv,calladj,callcond,callsoil,season,diurnal,lverbose
-  INTEGER iradia
-  REAL period_sort
+  LOGICAL ::  callrad, calldifv, calladj, callsoil, season, diurnal, lverbose
+  INTEGER :: iradia
+  REAL :: period_sort
 
 END MODULE callkeys
Index: dynamico_lmdz/simple_physics/phyparam/physics/error_mod.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/error_mod.F90	(revision 4235)
+++ dynamico_lmdz/simple_physics/phyparam/physics/error_mod.F90	(revision 4235)
@@ -0,0 +1,60 @@
+MODULE error_mod
+#include "use_logging.h"
+
+#ifndef XCODEML
+  USE, INTRINSIC :: IEEE_ARITHMETIC
+#endif
+
+  IMPLICIT NONE
+  PRIVATE
+
+  INTERFACE check_NaN
+     MODULE PROCEDURE check_NaN1, check_NaN2
+  END INTERFACE check_NaN
+
+  PUBLIC :: check_NaN
+
+CONTAINS
+
+
+  SUBROUTINE check_NaN1(caller, name, data)
+    CHARACTER(*), INTENT(IN) ::  caller, name
+    REAL, INTENT(IN) :: data(:)
+    LOGICAL :: isnan(SIZE(data,1))
+    INTEGER :: i
+#ifdef XCODEML
+    isnan = IEEE_IS_NAN(data)
+#endif
+    IF(ANY(isnan)) THEN
+       WRITELOG(*,*) 'In subroutine ', caller, ' array ', name, ' has NaN . Offending indices :'
+       DO i=1, SIZE(isnan,1)
+          IF(isnan(i)) THEN
+             WRITELOG(*,*) i
+          END IF
+       END DO
+       LOG_DBG('check_NaN')
+    END IF
+  END SUBROUTINE check_NaN1
+
+  SUBROUTINE check_NaN2(caller, name, data)
+    CHARACTER(*), INTENT(IN) ::  caller, name
+    REAL, INTENT(IN) :: data(:,:)
+    LOGICAL :: isnan(SIZE(data,1), SIZE(data,2))
+    INTEGER :: i,j
+#ifdef XCODEML
+    isnan = IEEE_IS_NAN(data)
+#endif
+    IF(ANY(isnan)) THEN
+       WRITELOG(*,*) 'In subroutine ', caller, ' array ', name, ' has NaN . Offending indices :'
+       DO i=1, SIZE(isnan,1)
+          DO j=1, SIZE(isnan,2)
+             IF(isnan(i,j)) THEN
+                WRITELOG(*,*) i,j
+             END IF
+          END DO
+       END DO
+       LOG_DBG('check_NaN')
+    END IF
+  END SUBROUTINE check_NaN2
+
+END MODULE error_mod
Index: dynamico_lmdz/simple_physics/phyparam/physics/iniphyparam_mod.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/iniphyparam_mod.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/physics/iniphyparam_mod.F90	(revision 4235)
@@ -54,12 +54,11 @@
     CALL read_param('coefir',.08      ,coefir,'coefir')
 
-    CALL read_param('callrad',.true.,callrad,'appel rayonnemen')
+    CALL read_param('callrad',.true.,callrad,'appel rayonnement')
     CALL read_param('calldifv',.true.,calldifv,'appel difv')
     CALL read_param('calladj',.true.,calladj,'appel adj')
-    CALL read_param('callcond',.true.,callcond,'appel cond')
     CALL read_param('callsoil',.true.,callsoil,'appel soil')
-    CALL read_param('season',.true.,season,'appel soil')
-    CALL read_param('diurnal',.false.,diurnal,'appel soil')
-    CALL read_param('lverbose',.true.,lverbose,'appel soil')
+    CALL read_param('season',.true.,season,'with seasonal cycle')
+    CALL read_param('diurnal',.false.,diurnal,'with diurnal cycle')
+    CALL read_param('lverbose',.true.,lverbose,'lverbose')
     CALL read_param('period_sort',1.,period_sort,'period sorties en jour')
 
Index: dynamico_lmdz/simple_physics/phyparam/physics/logging.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/logging.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/physics/logging.F90	(revision 4235)
@@ -42,4 +42,5 @@
   INTEGER, PARAMETER, PUBLIC :: log_level_fatal=1, log_level_error=2, log_level_warn=3, log_level_info=4, log_level_dbg=5
   INTEGER, PUBLIC :: max_log_level = log_level_info
+  CHARACTER(3), DIMENSION(log_level_dbg), PUBLIC :: dbtag = (/ 'FAT', 'ERR', 'WRN', 'INF', 'DBG' /)
 
   PUBLIC :: logging_buf, logging_bufsize, logging_lineno, flush_log, log_gridpoint, &
@@ -73,5 +74,4 @@
     INTEGER, INTENT(IN) :: lev
     CHARACTER(*), INTENT(IN) :: tag, buf(:)
-    CHARACTER(3), DIMENSION(log_level_dbg) :: dbtag = (/ 'FAT', 'ERR', 'WRN', 'INF', 'DBG' /)
     CHARACTER(100) :: prefix
     INTEGER :: i
Index: dynamico_lmdz/simple_physics/phyparam/physics/read_param_mod.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/read_param_mod.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/physics/read_param_mod.F90	(revision 4235)
@@ -43,5 +43,5 @@
   INTERFACE read_param
      PROCEDURE read_paramr, read_parami, read_paramb
-  END INTERFACE
+  END INTERFACE read_param
 
   PUBLIC :: read_param
@@ -86,5 +86,5 @@
     LOGICAL, INTENT(OUT)     :: val
 #ifndef XCODEML
-    IF(.NOT.ASSOCIATED(read_paramr_plugin)) THEN
+    IF(.NOT.ASSOCIATED(read_paramb_plugin)) THEN
        CALL missing_plugin('read_paramb','read_param_mod')
        val = defval
Index: dynamico_lmdz/simple_physics/phyparam/physics/writefield_mod.F90
===================================================================
--- dynamico_lmdz/simple_physics/phyparam/physics/writefield_mod.F90	(revision 4234)
+++ dynamico_lmdz/simple_physics/phyparam/physics/writefield_mod.F90	(revision 4235)
@@ -24,5 +24,5 @@
   INTERFACE writefield
      PROCEDURE writefield1, writefield2
-  END INTERFACE
+  END INTERFACE writefield
 
   PUBLIC :: writefield
