Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/CONFIG/ORCA2_LIM/EXP00/namelist
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/CONFIG/ORCA2_LIM/EXP00/namelist	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/CONFIG/ORCA2_LIM/EXP00/namelist	(revision 3411)
@@ -894,5 +894,5 @@
 
    ln_sst     = .false.    ! Logical switch for SST observations              
-                           !     ln_reysst               Logical switch for Reynolds observations       
+   ln_reysst  = .false.    !     ln_reysst               Logical switch for Reynolds observations       
                            !     ln_ghrsst               Logical switch for GHRSST observations          
 
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/diaobs.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/diaobs.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/diaobs.F90	(revision 3411)
@@ -106,4 +106,6 @@
    LOGICAL, DIMENSION(:), ALLOCATABLE :: &
       & ld_velav     !: Velocity data is daily averaged
+   LOGICAL, DIMENSION(:), ALLOCATABLE :: &
+      & ld_sstnight  !: SST observation corresponds to night mean
 
    !!----------------------------------------------------------------------
@@ -737,6 +739,8 @@
          ALLOCATE(sstdata(nsstsets))
          ALLOCATE(sstdatqc(nsstsets))
+         ALLOCATE(ld_sstnight(nsstsets))
          sstdata(:)%nsurf=0
-         sstdatqc(:)%nsurf=0         
+         sstdatqc(:)%nsurf=0    
+         ld_sstnight(:)=.false.
 
          nsstsets = 0
@@ -745,4 +749,6 @@
 
             nsstsets = nsstsets + 1
+
+            ld_sstnight(nsstsets) = .TRUE.
 
             CALL obs_rea_sst_rey( reysstname, reysstfmt, sstdata(nsstsets), &
@@ -757,4 +763,6 @@
         
             nsstsets = nsstsets + 1
+
+            ld_sstnight(nsstsets) = .TRUE.
           
             CALL obs_rea_sst( 1, sstdata(nsstsets), jnumsst, &
@@ -774,4 +782,6 @@
             
                nsstsets = nsstsets + 1
+
+               ld_sstnight(nsstsets) = .TRUE.
             
                CALL obs_rea_sst( 0, sstdata(nsstsets), 1, &
@@ -1092,7 +1102,8 @@
       IF ( ln_sst ) THEN
          DO jsstset = 1, nsstsets
-            CALL obs_sst_opt( sstdatqc(jsstset),                 &
-               &              kstp, jpi, jpj, nit000, tsn(:,:,1,jp_tem), &
-               &              tmask(:,:,1), n2dint )
+            CALL obs_sst_opt( sstdatqc(jsstset),                &
+               &              kstp, jpi, jpj, nit000, idaystp,  &
+               &              tsn(:,:,1,jp_tem), tmask(:,:,1),  &
+               &              n2dint, ld_sstnight(jsstset) )
          END DO
       ENDIF
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90	(revision 3411)
@@ -614,7 +614,6 @@
    END SUBROUTINE obs_sla_opt
 
-   SUBROUTINE obs_sst_opt( sstdatqc, kt, kpi, kpj, kit000, &
-      &                    psstn, psstmask, k2dint )
-
+   SUBROUTINE obs_sst_opt( sstdatqc, kt, kpi, kpj, kit000, kdaystp, &
+      &                    psstn, psstmask, k2dint, ld_nightav )
       !!-----------------------------------------------------------------------
       !!
@@ -647,4 +646,5 @@
       !! * Modules used
       USE obs_surf_def  ! Definition of storage space for surface observations
+      USE sbcdcy
 
       IMPLICIT NONE
@@ -659,8 +659,9 @@
                                        !   (kit000-1 = restart time)
       INTEGER, INTENT(IN) :: k2dint    ! Horizontal interpolation type (see header)
+      INTEGER, INTENT(IN) :: kdaystp   ! Number of time steps per day  
       REAL(KIND=wp), INTENT(IN), DIMENSION(kpi,kpj) :: &
          & psstn,  &    ! Model SST field
          & psstmask     ! Land-sea mask
-         
+
       !! * Local declarations
       INTEGER :: ji
@@ -670,7 +671,16 @@
       INTEGER :: isst
       INTEGER :: iobs
+      INTEGER :: idayend
       REAL(KIND=wp) :: zlam
       REAL(KIND=wp) :: zphi
       REAL(KIND=wp) :: zext(1), zobsmask(1)
+      REAL(KIND=wp) :: zdaystp
+      INTEGER, DIMENSION(:,:), SAVE, ALLOCATABLE :: &
+         & icount_sstnight,      &
+         & imask_night
+      REAL(kind=wp), DIMENSION(:,:), SAVE, ALLOCATABLE :: &
+         & zintmp, &
+         & zouttmp, & 
+         & zmeanday    ! to compute model sst in region of 24h daylight (pole)
       REAL(kind=wp), DIMENSION(2,2,1) :: &
          & zweig
@@ -678,4 +688,5 @@
          & zmask, &
          & zsstl, &
+         & zsstm, &
          & zglam, &
          & zgphi
@@ -683,4 +694,5 @@
          & igrdi, &
          & igrdj
+      LOGICAL, INTENT(IN) :: ld_nightav
 
       !-----------------------------------------------------------------------
@@ -690,4 +702,66 @@
       inrc = kt - kit000 + 2
       isst = sstdatqc%nsstp(inrc)
+
+      IF ( ld_nightav ) THEN
+
+      ! Initialize array for night mean
+
+      IF ( kt .EQ. 0 ) THEN
+         ALLOCATE ( icount_sstnight(kpi,kpj) )
+         ALLOCATE ( imask_night(kpi,kpj) )
+         ALLOCATE ( zintmp(kpi,kpj) )
+         ALLOCATE ( zouttmp(kpi,kpj) )
+         ALLOCATE ( zmeanday(kpi,kpj) )
+         nday_qsr = -1   ! initialisation flag for nbc_dcy
+      ENDIF
+
+      ! Initialize daily mean for first timestep
+      idayend = MOD( kt - kit000 + 1, kdaystp )
+
+      ! Added kt == 0 test to catch restart case 
+      IF ( idayend == 1 .OR. kt == 0) THEN
+         IF (lwp) WRITE(numout,*) 'Reset sstdatqc%vdmean on time-step: ',kt
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               sstdatqc%vdmean(ji,jj) = 0.0
+               zmeanday(ji,jj) = 0.0
+               icount_sstnight(ji,jj) = 0
+            END DO
+         END DO
+      ENDIF
+
+      zintmp(:,:) = 0.0
+      zouttmp(:,:) = sbc_dcy( zintmp(:,:), .TRUE. )
+      imask_night(:,:) = INT( zouttmp(:,:) )
+
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            ! Increment the temperature field for computing night mean and counter
+            sstdatqc%vdmean(ji,jj) = sstdatqc%vdmean(ji,jj)  &
+                   &                        + psstn(ji,jj)*imask_night(ji,jj)
+            zmeanday(ji,jj)        = zmeanday(ji,jj) + psstn(ji,jj)
+            icount_sstnight(ji,jj) = icount_sstnight(ji,jj) + imask_night(ji,jj)
+         END DO
+      END DO
+   
+      ! Compute the daily mean at the end of day
+
+      zdaystp = 1.0 / REAL( kdaystp )
+
+      IF ( idayend == 0 ) THEN 
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               ! Test if "no night" point
+               IF ( icount_sstnight(ji,jj) .NE. 0 ) THEN
+                  sstdatqc%vdmean(ji,jj) = sstdatqc%vdmean(ji,jj) &
+                    &                        / icount_sstnight(ji,jj) 
+               ELSE
+                  sstdatqc%vdmean(ji,jj) = zmeanday(ji,jj) * zdaystp
+               ENDIF
+            END DO
+         END DO
+      ENDIF
+
+      ENDIF
 
       ! Get the data for interpolation
@@ -722,5 +796,17 @@
       CALL obs_int_comm_2d( 2, 2, isst, &
          &                  igrdi, igrdj, psstn, zsstl )
-      
+
+      ! At the end of the day get interpolated means
+      IF ( idayend == 0 .AND. ld_nightav ) THEN
+
+         ALLOCATE( &
+            & zsstm(2,2,isst)  &
+            & )
+
+         CALL obs_int_comm_2d( 2, 2, isst, igrdi, igrdj, &
+            &               sstdatqc%vdmean(:,:), zsstm )
+
+      ENDIF
+
       ! Loop over observations
 
@@ -756,6 +842,23 @@
             
          ! Interpolate the model SST to the observation point 
-         CALL obs_int_h2d( 1, 1,      &
+
+         IF ( ld_nightav ) THEN
+
+           IF ( idayend == 0 )  THEN
+               ! Daily averaged/diurnal cycle of SST  data
+               CALL obs_int_h2d( 1, 1,      & 
+                     &              zweig, zsstm(:,:,iobs), zext )
+            ELSE 
+               CALL ctl_stop( ' ld_nightav is set to true: a nonzero' //     &
+                     &           ' number of night SST data should' // &
+                     &           ' only occur at the end of a given day' )
+            ENDIF
+
+         ELSE
+
+            CALL obs_int_h2d( 1, 1,      &
             &              zweig, zsstl(:,:,iobs),  zext )
+
+         ENDIF
          
          sstdatqc%rmod(jobs,1) = zext(1)
@@ -772,4 +875,11 @@
          & zsstl  &
          & )
+
+      ! At the end of the day also get interpolated means
+      IF ( idayend == 0 .AND. ld_nightav ) THEN
+         DEALLOCATE( &
+            & zsstm  &
+            & )
+      ENDIF
       
       sstdatqc%nsurfup = sstdatqc%nsurfup + isst
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_seaice.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_seaice.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_seaice.F90	(revision 3411)
@@ -326,5 +326,6 @@
          &               iindx   )
       
-      CALL obs_surf_alloc( seaicedata, iobs, kvars, kextr, kstp )
+      CALL obs_surf_alloc( seaicedata, iobs, & 
+                           kvars, kextr, kstp, jpi, jpj )
       
       ! * Read obs/positions, QC, all variable and assign to seaicedata
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sla.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sla.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sla.F90	(revision 3411)
@@ -391,5 +391,6 @@
          &               iindx   )
       
-      CALL obs_surf_alloc( sladata, iobs, kvars, kextr, kstp )
+      CALL obs_surf_alloc( sladata, iobs, kvars, kextr, &
+         &                 jpi, jpj, kstp )
       
       ! * Read obs/positions, QC, all variable and assign to sladata
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sst.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sst.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_read_sst.F90	(revision 3411)
@@ -326,5 +326,5 @@
          &               iindx   )
       
-      CALL obs_surf_alloc( sstdata, iobs, kvars, kextr, kstp )
+      CALL obs_surf_alloc( sstdata, iobs, kvars, kextr, kstp, jpi, jpj )
       
       ! * Read obs/positions, QC, all variable and assign to sstdata
@@ -701,5 +701,5 @@
       ! Allocate obs_surf data structure for time sorted data
          
-      CALL obs_surf_alloc( sstdata, inumobs, kvars, kextra, kstp )
+      CALL obs_surf_alloc( sstdata, inumobs, kvars, kextra, kstp, jpi, jpj )
 
       pjul = pjulini + 1
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_surf_def.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_surf_def.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/OBS/obs_surf_def.F90	(revision 3411)
@@ -47,4 +47,6 @@
       INTEGER :: nextra     !: Number of extra fields at observation points
       INTEGER :: nstp       !: Number of time steps
+      INTEGER :: npi        !: Number of 3D grid points
+      INTEGER :: npj
       INTEGER :: nsurfup    !: Observation counter used in obs_oper
 
@@ -79,4 +81,7 @@
          & rext           !: Extra fields interpolated to observation points
 
+      REAL(KIND=wp), POINTER, DIMENSION(:,:) :: &
+         & vdmean         !: Time averaged of model field
+
       ! Arrays with size equal to the number of time steps in the window
 
@@ -103,5 +108,5 @@
 CONTAINS
    
-   SUBROUTINE obs_surf_alloc( surf, ksurf, kvar, kextra, kstp )
+   SUBROUTINE obs_surf_alloc( surf, ksurf, kvar, kextra, kstp, kpi, kpj )
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE obs_surf_alloc  ***
@@ -120,4 +125,6 @@
       INTEGER, INTENT(IN) :: kextra  ! Number of extra fields at observation points
       INTEGER, INTENT(IN) :: kstp    ! Number of time steps
+      INTEGER, INTENT(IN) :: kpi     ! Number of 3D grid points
+      INTEGER, INTENT(IN) :: kpj
 
       !!* Local variables
@@ -131,4 +138,6 @@
       surf%nvar     = kvar
       surf%nstp     = kstp
+      surf%npi      = kpi
+      surf%npj      = kpj
       
       ! Allocate arrays of number of surface data size
@@ -174,4 +183,10 @@
          & )
 
+      ! Allocate arrays of size number of grid points
+
+      ALLOCATE( &
+         & surf%vdmean(kpi,kpj) &
+         & )
+
       ! Set defaults for compression indices
       
@@ -242,4 +257,11 @@
          & )
 
+      ! Deallocate arrays of size number of grid points size times
+      ! number of variables
+
+      DEALLOCATE( &
+         & surf%vdmean &
+         & )
+
       ! Deallocate arrays of number of time step size
 
@@ -300,5 +322,5 @@
       IF ( lallocate ) THEN
          CALL obs_surf_alloc( newsurf,  insurf, surf%nvar, &
-            & surf%nextra, surf%nstp )
+            & surf%nextra, surf%nstp, surf%npi, surf%npj )
       ENDIF
 
Index: /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcdcy.F90
===================================================================
--- /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcdcy.F90	(revision 3410)
+++ /branches/2012/dev_r3342_MERCATOR7_SST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcdcy.F90	(revision 3411)
@@ -49,5 +49,5 @@
 
 
-   FUNCTION sbc_dcy( pqsrin ) RESULT( zqsrout )
+   FUNCTION sbc_dcy( pqsrin, l_mask ) RESULT( zqsrout )
       !!----------------------------------------------------------------------
       !!                  ***  ROUTINE sbc_dcy  ***
@@ -63,11 +63,14 @@
       !!              Part 1: a diurnally forced OGCM. Climate Dynamics 29:6, 575-590.
       !!----------------------------------------------------------------------
+      LOGICAL, OPTIONAL, INTENT(in) :: l_mask ! use the routine for night mask computation
       REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   pqsrin    ! input daily QSR flux 
       !!
       INTEGER  ::   ji, jj                                       ! dummy loop indices
+      INTEGER, DIMENSION(jpi,jpj) :: imask_night ! night mask
       REAL(wp) ::   ztwopi, zinvtwopi, zconvrad 
       REAL(wp) ::   zlo, zup, zlousd, zupusd
       REAL(wp) ::   zdsws, zdecrad, ztx, zsin, zcos
       REAL(wp) ::   ztmp, ztmp1, ztmp2, ztest
+      REAL(wp) ::   ztmpm, ztmpm1, ztmpm2
       REAL(wp), DIMENSION(jpi,jpj) ::   zqsrout                  ! output QSR flux with diurnal cycle
       !---------------------------statement functions------------------------
@@ -90,5 +93,5 @@
       zup = zlo + ( REAL(nn_fsbc, wp)     * rdttra(1) ) / rday
       !                                          
-      IF( nday_qsr == -1 ) THEN       ! first time step only               
+      IF( nday_qsr == -1 ) THEN       ! first time step only  
          IF(lwp) THEN
             WRITE(numout,*)
@@ -120,5 +123,5 @@
          zdecrad = (-23.5 * zconvrad) * COS( zdsws * ztwopi / REAL(nyear_len(1),wp) )
          ! Compute A and B needed to compute the time integral of the diurnal cycle
-        
+
          zsin = SIN( zdecrad )   ;   zcos = COS( zdecrad )
          DO jj = 1, jpj
@@ -129,5 +132,4 @@
             END DO  
          END DO  
-
          ! Compute the time of dawn and dusk
 
@@ -156,5 +158,4 @@
          rdawn(:,:) = MOD( (rdawn(:,:) + 1._wp), 1._wp )
          rdusk(:,:) = MOD( (rdusk(:,:) + 1._wp), 1._wp )
-
          !     2.2 Compute the scalling function:
          !         S* = the inverse of the time integral of the diurnal cycle from dawm to dusk
@@ -185,10 +186,11 @@
          !
       ENDIF 
-
          !     3. update qsr with the diurnal cycle
          !     ------------------------------------
 
+      imask_night(:,:) = 0
       DO jj = 1, jpj
          DO ji = 1, jpi
+            ztmpm = 0.0
             IF( ABS(rab(ji,jj)) < 1 ) THEN         ! day duration is less than 24h
                !
@@ -200,4 +202,6 @@
                   ztmp = fintegral(zlousd, zupusd, raa(ji,jj), rbb(ji,jj), rcc(ji,jj)) 
                   zqsrout(ji,jj) = pqsrin(ji,jj) * ztmp * rscal(ji,jj)
+                  ztmpm = zupusd - zlousd
+                  IF ( ztmpm .EQ. 0 ) imask_night(ji,jj) = 1
                   !
                ELSE                                         ! day time in two parts
@@ -205,9 +209,13 @@
                   zupusd = MIN(zup, rdusk(ji,jj))
                   ztmp1 = fintegral(zlousd, zupusd, raa(ji,jj), rbb(ji,jj), rcc(ji,jj)) 
+                  ztmpm1=zupusd-zlousd
                   zlousd = MAX(zlo, rdawn(ji,jj))
                   zupusd = MAX(zup, rdawn(ji,jj))
                   ztmp2 = fintegral(zlousd, zupusd, raa(ji,jj), rbb(ji,jj), rcc(ji,jj)) 
+                  ztmpm2 =zupusd-zlousd
                   ztmp = ztmp1 + ztmp2
+                  ztmpm = ztmpm1 + ztmpm2
                   zqsrout(ji,jj) = pqsrin(ji,jj) * ztmp * rscal(ji,jj)
+                  IF (ztmpm .EQ. 0.) imask_night(ji,jj) = 1
                ENDIF
             ELSE                                   ! 24h light or 24h night
@@ -216,11 +224,17 @@
                   ztmp = fintegral(zlo, zup, raa(ji,jj), rbb(ji,jj), rcc(ji,jj)) 
                   zqsrout(ji,jj) = pqsrin(ji,jj) * ztmp * rscal(ji,jj)
+                  imask_night(ji,jj) = 0
                   !
                ELSE                                         ! No day
                   zqsrout(ji,jj) = 0.e0
+                  imask_night(ji,jj) = 1
                ENDIF
             ENDIF
          END DO  
       END DO  
+      !
+      IF ( PRESENT(l_mask) .AND. l_mask ) THEN
+         zqsrout(:,:) = float(imask_night(:,:))
+      ENDIF
       !
       IF( nn_timing == 1 )  CALL timing_stop('sbc_dcy')
