Index: trunk/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90
===================================================================
--- trunk/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90	(revision 2528)
+++ trunk/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90	(revision 2715)
@@ -4,7 +4,7 @@
    !! Ocean diagnostics:  ocean tracers trends
    !!=====================================================================
-   !! History :  9.0  !  2004-08  (C. Talandier) Original code
-   !!                 !  2005-04  (C. Deltel)    Add Asselin trend in the ML budget
-   !!      NEMO  3.3  !  2010-06  (C. Ethe) merge TRA-TRC 
+   !! History :  1.0  !  2004-08  (C. Talandier) Original code
+   !!            2.0  !  2005-04  (C. Deltel)    Add Asselin trend in the ML budget
+   !!            3.3  !  2010-06  (C. Ethe) merge TRA-TRC 
    !!----------------------------------------------------------------------
 #if  defined key_trdtra || defined key_trdmld || defined key_trdmld_trc 
@@ -12,16 +12,17 @@
    !!   trd_tra      : Call the trend to be computed
    !!----------------------------------------------------------------------
-   USE dom_oce            ! ocean domain 
-   USE trdmod_oce         ! ocean active mixed layer tracers trends 
-   USE trdmod             ! ocean active mixed layer tracers trends 
-   USE trdmod_trc         ! ocean passive mixed layer tracers trends 
+   USE dom_oce          ! ocean domain 
+   USE trdmod_oce       ! ocean active mixed layer tracers trends 
+   USE trdmod           ! ocean active mixed layer tracers trends 
+   USE trdmod_trc       ! ocean passive mixed layer tracers trends 
+   USE in_out_manager   ! I/O manager
+   USE lib_mpp          ! MPP library
 
    IMPLICIT NONE
    PRIVATE
 
-   PUBLIC trd_tra          ! called by all  traXX modules
+   PUBLIC   trd_tra          ! called by all  traXX modules
  
-   !! * Module declaration
-   REAL(wp), DIMENSION(jpi,jpj,jpk), SAVE :: trdtx, trdty, trdt  !:
+   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) :: trdtx, trdty, trdt  !:
 
    !! * Substitutions
@@ -29,10 +30,20 @@
 #  include "vectopt_loop_substitute.h90"
    !!----------------------------------------------------------------------
-   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
+   !! NEMO/OPA 4.0 , NEMO Consortium (2011)
    !! $Id$
-   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
-   !!----------------------------------------------------------------------
-
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
 CONTAINS
+
+   INTEGER FUNCTION trd_tra_alloc()
+      !!----------------------------------------------------------------------------
+      !!                  ***  FUNCTION trd_tra_alloc  ***
+      !!----------------------------------------------------------------------------
+      ALLOCATE( trdtx(jpi,jpj,jpk) , trdty(jpi,jpj,jpk) , trdt(jpi,jpj,jpk) , STAT= trd_tra_alloc )
+      !
+      IF( lk_mpp             )   CALL mpp_sum ( trd_tra_alloc )
+      IF( trd_tra_alloc /= 0 )   CALL ctl_warn('trd_tra_alloc: failed to allocate arrays')
+   END FUNCTION trd_tra_alloc
+
 
    SUBROUTINE trd_tra( kt, ctype, ktra, ktrd, ptrd, pun, ptra )
@@ -50,4 +61,7 @@
       !!        nn_ctls > 1  : use fixed level surface jk = nn_ctls
       !!----------------------------------------------------------------------
+      USE wrk_nemo, ONLY:   wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY:   ztrds => wrk_3d_10   ! 3D workspace
+      !
       INTEGER                         , INTENT(in)           ::  kt      ! time step
       CHARACTER(len=3)                , INTENT(in)           ::  ctype   ! tracers trends type 'TRA'/'TRC'
@@ -57,8 +71,14 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::  pun     ! velocity 
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::  ptra    ! Tracer variable 
-      !!
-      REAL(wp), DIMENSION(jpi,jpj,jpk) ::  ztrds    !  
-      !!----------------------------------------------------------------------
-
+      !!----------------------------------------------------------------------
+
+      IF( wrk_in_use(3, 10) ) THEN
+         CALL ctl_stop('trd_tra: requested workspace array unavailable')   ;   RETURN
+      ENDIF
+
+      IF( .NOT. ALLOCATED( trdtx ) ) THEN       ! allocate trdtra arrays
+         IF( trd_tra_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'trd_tra : unable to allocate arrays' )
+      ENDIF
+      
       ! Control of optional arguments
       IF( ctype == 'TRA' .AND. ktra == jp_tem ) THEN 
@@ -118,5 +138,8 @@
       ENDIF
       !
+      IF( wrk_not_released(3, 10) )   CALL ctl_stop('trd_tra: failed to release workspace array')
+      !
    END SUBROUTINE trd_tra
+
 
    SUBROUTINE trd_tra_adv( pf, pun, ptn, cdir, ptrd )
@@ -130,13 +153,13 @@
       !!                k-advective trends = -un. di+1[T] = -( dk+1[fi] - tn dk+1[un] )
       !!----------------------------------------------------------------------
-      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk)           ::   pf      ! advective flux in one direction
-      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk)           ::   pun     ! now velocity  in one direction
-      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk)           ::   ptn     ! now or before tracer 
-      CHARACTER(len=1), INTENT(in )                                   ::   cdir    ! X/Y/Z direction
-      REAL(wp)        , INTENT(out), DIMENSION(jpi,jpj,jpk)           ::   ptrd    ! advective trend in one direction
-      !!
-      INTEGER                          ::   ji, jj, jk   ! dummy loop indices
-      INTEGER                          ::   ii, ij, ik   ! index shift function of the direction
-      REAL(wp)                         ::   zbtr         ! temporary scalar
+      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk) ::   pf      ! advective flux in one direction
+      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk) ::   pun     ! now velocity  in one direction
+      REAL(wp)        , INTENT(in ), DIMENSION(jpi,jpj,jpk) ::   ptn     ! now or before tracer 
+      CHARACTER(len=1), INTENT(in )                         ::   cdir    ! X/Y/Z direction
+      REAL(wp)        , INTENT(out), DIMENSION(jpi,jpj,jpk) ::   ptrd    ! advective trend in one direction
+      !
+      INTEGER  ::   ji, jj, jk   ! dummy loop indices
+      INTEGER  ::   ii, ij, ik   ! index shift function of the direction
+      REAL(wp) ::   zbtr         ! local scalar
       !!----------------------------------------------------------------------
 
@@ -167,10 +190,8 @@
 #   else
    !!----------------------------------------------------------------------
-   !!   Default case :                                         Empty module
+   !!   Default case :          Dummy module           No trend diagnostics
    !!----------------------------------------------------------------------
    USE par_oce      ! ocean variables trends
-
 CONTAINS
-
    SUBROUTINE trd_tra( kt, ctype, ktra, ktrd, ptrd, pu, ptra )
       !!----------------------------------------------------------------------
@@ -182,11 +203,6 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::  pu      ! velocity 
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::  ptra    ! Tracer variable 
-      WRITE(*,*) 'trd_3d: You should not have seen this print! error ?', ptrd(1,1,1)
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', ptra(1,1,1)
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', pu(1,1,1)
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', ktrd
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', ktra
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', ctype
-      WRITE(*,*) ' "   ": You should not have seen this print! error ?', kt
+      WRITE(*,*) 'trd_3d: You should not have seen this print! error ?', ptrd(1,1,1), ptra(1,1,1), pu(1,1,1),   &
+         &                                                               ktrd, ktra, ctype, kt
    END SUBROUTINE trd_tra
 #   endif
