Index: branches/2012/dev_MERCATOR_2012_rev3555/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90
===================================================================
--- branches/2012/dev_MERCATOR_2012_rev3555/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90	(revision 3582)
+++ branches/2012/dev_MERCATOR_2012_rev3555/NEMOGCM/NEMO/OPA_SRC/OBS/obs_oper.F90	(revision 3586)
@@ -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
