Index: /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmbkg.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmbkg.F90	(revision 8417)
+++ /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmbkg.F90	(revision 8418)
@@ -50,8 +50,19 @@
    USE ice
 #endif
+   USE asminc, ONLY: ln_avgbkg
    IMPLICIT NONE
    PRIVATE
    
    PUBLIC   asm_bkg_wri   !: Write out the background state
+
+  !! * variables for calculating time means
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   tn_tavg  , sn_tavg  
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   un_tavg  , vn_tavg
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   avt_tavg
+#if defined key_zdfgls || key_zdftke
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   en_tavg
+#endif
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:)   ::   sshn_tavg
+   REAL(wp),SAVE :: numtimes_tavg     ! No of times to average over
 
    !!----------------------------------------------------------------------
@@ -81,9 +92,73 @@
       INTEGER :: inum          ! File unit number
       REAL(wp) :: zdate        ! Date
+      INTEGER :: ierror
       !!-----------------------------------------------------------------------
 
-      !                                !-------------------------------------------
-      IF( kt == nitbkg_r ) THEN        ! Write out background at time step nitbkg_r
-         !                             !-----------------------------------========
+      ! If creating an averaged assim bkg, initialise on first timestep
+      IF ( ln_avgbkg .AND. kt == ( nn_it000 - 1) ) THEN
+         ! Allocate memory 
+         ALLOCATE( tn_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate tn_tavg' )   ;   RETURN
+         ENDIF
+         tn_tavg(:,:,:)=0
+         ALLOCATE( sn_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate sn_tavg' )   ;   RETURN
+         ENDIF
+         sn_tavg(:,:,:)=0
+         ALLOCATE( un_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate un_tavg' )   ;   RETURN
+         ENDIF
+         un_tavg(:,:,:)=0
+         ALLOCATE( vn_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate vn_tavg' )   ;   RETURN
+         ENDIF
+         vn_tavg(:,:,:)=0
+         ALLOCATE( sshn_tavg(jpi,jpj), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate sshn_tavg' )   ;   RETURN
+         ENDIF
+         sshn_tavg(:,:)=0
+#if defined key_zdftke
+         ALLOCATE( en_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate en_tavg' )   ;   RETURN
+         ENDIF
+         en_tavg(:,:,:)=0
+#endif
+         ALLOCATE( avt_tavg(jpi,jpj,jpk), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'asm_wri_bkg: unable to allocate avt_tavg' )   ;   RETURN
+         ENDIF
+         avt_tavg(:,:,:)=0
+         
+         numtimes_tavg = REAL ( nitavgbkg_r -  nn_it000 + 1 )
+      ENDIF   
+
+      ! If creating an averaged assim bkg, sum the contribution every timestep
+      IF ( ln_avgbkg ) THEN 
+         IF (lwp) THEN
+              WRITE(numout,*) 'asm_wri_bkg : Summing assim bkg fields at timestep ',kt
+              WRITE(numout,*) '~~~~~~~~~~~~ '
+         ENDIF
+
+         tn_tavg(:,:,:)        = tn_tavg(:,:,:) + tsn(:,:,:,jp_tem) / numtimes_tavg
+         sn_tavg(:,:,:)        = sn_tavg(:,:,:) + tsn(:,:,:,jp_sal) / numtimes_tavg
+         sshn_tavg(:,:)        = sshn_tavg(:,:) + sshn (:,:) / numtimes_tavg
+         un_tavg(:,:,:)        = un_tavg(:,:,:) + un(:,:,:) / numtimes_tavg
+         vn_tavg(:,:,:)        = vn_tavg(:,:,:) + vn(:,:,:) / numtimes_tavg
+         avt_tavg(:,:,:)        = avt_tavg(:,:,:) + avt(:,:,:) / numtimes_tavg
+#if defined key_zdftke
+         en_tavg(:,:,:)       = en_tavg(:,:,:) + en(:,:,:) / numtimes_tavg
+#endif
+      ENDIF
+     
+
+      ! Write out background at time step nitbkg_r or nitavgbkg_r
+      IF ( ( .NOT. ln_avgbkg .AND. (kt == nitbkg_r) ) .OR. &
+      &          ( ln_avgbkg .AND. (kt == nitavgbkg_r) ) ) THEN
          !
          WRITE(cl_asmbkg, FMT='(A,".nc")' ) TRIM( c_asmbkg )
@@ -97,30 +172,56 @@
             CALL iom_open( c_asmbkg, inum, ldwrt = .TRUE., kiolib = jprstlib)
             !
-            IF( nitbkg_r == nit000 - 1 ) THEN      ! Treat special case when nitbkg = 0
-               zdate = REAL( ndastp )
-#if defined key_zdftke
-               ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
-               IF(lwp) WRITE(numout,*) ' Reading TKE (en) from restart...'
-               CALL tke_rst( nit000, 'READ' )               ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
-
-#endif
+            !
+            ! Write the information
+            IF ( ln_avgbkg ) THEN
+               IF( nitavgbkg_r == nit000 - 1 ) THEN      ! Treat special case when nitavgbkg = 0
+                  zdate = REAL( ndastp )
+#if defined key_zdftke
+                  ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
+                  IF(lwp) WRITE(numout,*) ' Reading TKE (en) from restart...'
+                  CALL tke_rst( nit000, 'READ' )               ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
+
+#endif
+               ELSE
+                  zdate = REAL( ndastp )
+               ENDIF
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'rdastp' , zdate   )
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'un'     , un_tavg )
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'vn'     , vn_tavg )
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'tn'     , tn_tavg )
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'sn'     , sn_tavg )
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'sshn'   , sshn_tavg)
+#if defined key_zdftke
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'en'     , en_tavg )
+#endif
+               CALL iom_rstput( kt, nitavgbkg_r, inum, 'avt'    , avt_tavg)
+               !
             ELSE
-               zdate = REAL( ndastp )
+               IF( nitbkg_r == nit000 - 1 ) THEN      ! Treat special case when nitbkg = 0
+                  zdate = REAL( ndastp )
+#if defined key_zdftke
+                  ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
+                  IF(lwp) WRITE(numout,*) ' Reading TKE (en) from restart...'
+                  CALL tke_rst( nit000, 'READ' )               ! lk_zdftke=T :   Read turbulent kinetic energy ( en )
+
+#endif
+               ELSE
+                  zdate = REAL( ndastp )
+               ENDIF
+               CALL iom_rstput( kt, nitbkg_r, inum, 'rdastp' , zdate   )
+               CALL iom_rstput( kt, nitbkg_r, inum, 'un'     , un                )
+               CALL iom_rstput( kt, nitbkg_r, inum, 'vn'     , vn                )
+               CALL iom_rstput( kt, nitbkg_r, inum, 'tn'     , tsn(:,:,:,jp_tem) )
+               CALL iom_rstput( kt, nitbkg_r, inum, 'sn'     , tsn(:,:,:,jp_sal) )
+               CALL iom_rstput( kt, nitbkg_r, inum, 'sshn'   , sshn              )
+#if defined key_zdftke
+               CALL iom_rstput( kt, nitbkg_r, inum, 'en'     , en                )
+#endif
+               CALL iom_rstput( kt, nitbkg_r, inum, 'avt'    , avt               )
+               !
             ENDIF
-            !
-            !                                      ! Write the information
-            CALL iom_rstput( kt, nitbkg_r, inum, 'rdastp' , zdate             )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'un'     , un                )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'vn'     , vn                )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'tn'     , tsn(:,:,:,jp_tem) )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'sn'     , tsn(:,:,:,jp_sal) )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'sshn'   , sshn              )
-#if defined key_zdftke
-            CALL iom_rstput( kt, nitbkg_r, inum, 'en'     , en                )
-#endif
-!            CALL iom_rstput( kt, nitbkg_r, inum, 'gcx'    , gcx               )
-            CALL iom_rstput( kt, nitbkg_r, inum, 'avt'    , avt               )
-            !
+            
             CALL iom_close( inum )
+
          ENDIF
          !
@@ -154,5 +255,4 @@
             CALL iom_rstput( kt, nitdin_r, inum, 'tn'     , tsn(:,:,:,jp_tem) )
             CALL iom_rstput( kt, nitdin_r, inum, 'sn'     , tsn(:,:,:,jp_sal) )
-            CALL iom_rstput( kt, nitdin_r, inum, 'avt'    , avt     )
             CALL iom_rstput( kt, nitdin_r, inum, 'sshn'   , sshn              )
 #if defined key_lim2 || defined key_lim3
Index: /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asminc.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asminc.F90	(revision 8417)
+++ /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asminc.F90	(revision 8418)
@@ -61,4 +61,5 @@
 #endif
    LOGICAL, PUBLIC :: ln_bkgwri = .FALSE.      !: No output of the background state fields
+   LOGICAL, PUBLIC :: ln_avgbkg = .FALSE.      !: No output of the mean background state fields
    LOGICAL, PUBLIC :: ln_asmiau = .FALSE.      !: No applying forcing with an assimilation increment
    LOGICAL, PUBLIC :: ln_asmdin = .FALSE.      !: No direct initialization
@@ -84,4 +85,5 @@
    INTEGER , PUBLIC ::   nitiaustr   !: Time step of the start of the IAU interval 
    INTEGER , PUBLIC ::   nitiaufin   !: Time step of the end of the IAU interval
+   INTEGER , PUBLIC ::   nitavgbkg   !: Number of timesteps to average assim bkg [0,nitavgbkg]
    ! 
    INTEGER , PUBLIC ::   niaufn      !: Type of IAU weighing function: = 0   Constant weighting
@@ -123,4 +125,5 @@
       INTEGER :: iitiaustr_date  ! Date YYYYMMDD of IAU interval start time step
       INTEGER :: iitiaufin_date  ! Date YYYYMMDD of IAU interval final time step
+      INTEGER :: iitavgbkg_date  ! Date YYYYMMDD of end of assim bkg averaging period
       !
       REAL(wp) :: znorm        ! Normalization factor for IAU weights
@@ -133,10 +136,10 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   hdiv   ! 2D workspace
       !!
-      NAMELIST/nam_asminc/ ln_bkgwri,                                      &
+      NAMELIST/nam_asminc/ ln_bkgwri, ln_avgbkg,                           &
          &                 ln_trainc, ln_dyninc, ln_sshinc,                &
          &                 ln_asmdin, ln_asmiau,                           &
          &                 nitbkg, nitdin, nitiaustr, nitiaufin, niaufn,   &
          &                 ln_salfix, salfixmin, nn_divdmp,                &
-         &                 ln_seaiceinc, ln_temnofreeze
+         &                 ln_seaiceinc, ln_temnofreeze, nitavgbkg
       !!----------------------------------------------------------------------
 
@@ -163,4 +166,5 @@
          WRITE(numout,*) '   Namelist namasm : set assimilation increment parameters'
          WRITE(numout,*) '      Logical switch for writing out background state          ln_bkgwri = ', ln_bkgwri
+         WRITE(numout,*) '      Logical switch for writing mean background state         ln_avgbkg = ', ln_avgbkg
          WRITE(numout,*) '      Logical switch for applying tracer increments            ln_trainc = ', ln_trainc
          WRITE(numout,*) '      Logical switch for applying velocity increments          ln_dyninc = ', ln_dyninc
@@ -173,4 +177,5 @@
          WRITE(numout,*) '      Timestep of start of IAU interval in [0,nitend-nit000-1] nitiaustr = ', nitiaustr
          WRITE(numout,*) '      Timestep of end of IAU interval in [0,nitend-nit000-1]   nitiaufin = ', nitiaufin
+         WRITE(numout,*) '      Number of timesteps to average assim bkg [0,nitavgbkg]   nitavgbkg = ', nitavgbkg
          WRITE(numout,*) '      Type of IAU weighting function                           niaufn    = ', niaufn
          WRITE(numout,*) '      Logical switch for ensuring that the sa > salfixmin      ln_salfix = ', ln_salfix
@@ -182,4 +187,5 @@
       nitiaustr_r = nitiaustr + nit000 - 1  ! Start of IAU interval referenced to nit000
       nitiaufin_r = nitiaufin + nit000 - 1  ! End of IAU interval referenced to nit000
+      nitavgbkg_r = nitavgbkg + nit000 - 1  ! Averaging period referenced to nit000
 
       iiauper = nitiaufin_r - nitiaustr_r + 1  ! IAU interval length
@@ -191,4 +197,5 @@
       CALL calc_date( nit000, nitiaustr_r, ndate0, iitiaustr_date )     ! IAU start time referenced to ndate0
       CALL calc_date( nit000, nitiaufin_r, ndate0, iitiaufin_date )     ! IAU end time referenced to ndate0
+      CALL calc_date( nit000, nitavgbkg_r, ndate0, iitavgbkg_date )     ! End of assim bkg averaging period referenced to ndate0
       !
       IF(lwp) THEN
@@ -202,4 +209,5 @@
          WRITE(numout,*) '       nitiaustr_r = ', nitiaustr_r
          WRITE(numout,*) '       nitiaufin_r = ', nitiaufin_r
+         WRITE(numout,*) '       nitavgbkg_r = ', nitavgbkg_r
          WRITE(numout,*)
          WRITE(numout,*) '   Dates referenced to current cycle:'
@@ -211,4 +219,5 @@
          WRITE(numout,*) '       iitiaustr_date = ', iitiaustr_date
          WRITE(numout,*) '       iitiaufin_date = ', iitiaufin_date
+         WRITE(numout,*) '       iitavgbkg_date = ', iitavgbkg_date
       ENDIF
 
@@ -253,4 +262,9 @@
          & CALL ctl_stop( ' nitdin :',  &
          &                ' Background time step for Direct Initialization is outside', &
+         &                ' the cycle interval')
+
+      IF ( nitavgbkg_r > nitend ) &
+         & CALL ctl_stop( ' nitavgbkg_r :',  &
+         &                ' Assim bkg averaging period is outside', &
          &                ' the cycle interval')
 
Index: /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmpar.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmpar.F90	(revision 8417)
+++ /branches/UKMO/dev_r5518_GO6_package_avgback/NEMOGCM/NEMO/OPA_SRC/ASM/asmpar.F90	(revision 8418)
@@ -29,4 +29,5 @@
    INTEGER, PUBLIC :: nitiaufin_r   !: IAU final time step referenced to nit000
    INTEGER, PUBLIC :: nittrjfrq     !: Frequency of trajectory output for 4D-VAR
+   INTEGER, PUBLIC :: nitavgbkg_r   !: Averaging period for assim bkg referenced to nit000
 
    !!----------------------------------------------------------------------
