Index: /trunk/NEMO/OPA_SRC/SBC/fldread.F90
===================================================================
--- /trunk/NEMO/OPA_SRC/SBC/fldread.F90	(revision 1627)
+++ /trunk/NEMO/OPA_SRC/SBC/fldread.F90	(revision 1628)
@@ -30,5 +30,5 @@
       LOGICAL             ::   ln_tint     ! time interpolation or not (T/F)
       LOGICAL             ::   ln_clim     ! climatology or not (T/F)
-      CHARACTER(len = 7)  ::   cltype      ! type of data file 'monthly' or yearly'
+      CHARACTER(len = 7)  ::   cltype      ! type of data file 'daily', 'monthly' or yearly'
       CHARACTER(len = 34) ::   wname       ! generic name of a NetCDF weights file to be used, blank if not
       CHARACTER(len = 34) ::   vcomp       ! symbolic component name if a vector that needs rotation
@@ -44,5 +44,5 @@
       LOGICAL                         ::   ln_tint      ! time interpolation or not (T/F)
       LOGICAL                         ::   ln_clim      ! climatology or not (T/F)
-      CHARACTER(len = 7)              ::   cltype       ! type of data file 'monthly' or yearly'
+      CHARACTER(len = 7)              ::   cltype       ! type of data file 'daily', 'monthly' or yearly'
       INTEGER                         ::   num          ! iom id of the jpfld files to be read
       REAL(wp)                        ::   swap_sec     ! swapping time in second since Jan. 1st 00h of nit000 year
@@ -124,6 +124,7 @@
       REAL(wp) ::   zreclast   ! last record to be read in the current year file
       REAL(wp) ::   zsecend    ! number of second since Jan. 1st 00h of nit000 year at nitend
-      LOGICAL  ::   llnxtyr    ! open next year file?
-      LOGICAL  ::   llstop     ! stop is the file is not existing
+      LOGICAL  ::   llnxtyr    ! open next year  file?
+      LOGICAL  ::   llnxtmth   ! open next month file?
+      LOGICAL  ::   llstop     ! stop is the file does not exist
       REAL(wp) ::   ztinta     ! ratio applied to after  records when doing time interpolation
       REAL(wp) ::   ztintb     ! ratio applied to before records when doing time interpolation
@@ -150,36 +151,42 @@
             CALL fld_rec( sd(jf) )
 
-            ! do we have to change the year/month of the forcing field?? 
+            ! do we have to change the year/month/day of the forcing field?? 
             IF( sd(jf)%ln_tint ) THEN
-               ! if we do time interpolation we will need to open next year/month file before the end of the current year/month
-               ! if it is the case, we are still before the end of the year/month when calling fld_rec so sd(jf)%rec_a(1) will 
-               ! be larger than the record number that should be read for current year/month (for ex. 13 for monthly mean file)
+               ! if we do time interpolation we will need to open next year/month/day file before the end of the current one
+               ! if so, we are still before the end of the year/month/day when calling fld_rec so sd(jf)%rec_a(1) will be
+               ! larger than the record number that should be read for current year/month/day (for ex. 13 for monthly mean file)
 
                ! last record to be read in the current file
                IF( sd(jf)%freqh == -1. ) THEN             ;   zreclast = 12.
                ELSE                             
-                  IF( sd(jf)%cltype == 'monthly' ) THEN   ;   zreclast = 24. / sd(jf)%freqh * REAL( nmonth_len(nmonth), wp )
-                  ELSE                                    ;   zreclast = 24. / sd(jf)%freqh * REAL( nyear_len(     1 ), wp )
+                  IF(     sd(jf)%cltype == 'monthly'   ) THEN  ;   zreclast = 24. / sd(jf)%freqh * REAL( nmonth_len(nmonth), wp )
+                  ELSEIF( sd(jf)%cltype == 'daily'     ) THEN  ;   zreclast = 24. / sd(jf)%freqh
+                  ELSE                                         ;   zreclast = 24. / sd(jf)%freqh * REAL( nyear_len(     1 ), wp )
                   ENDIF
                ENDIF
               
-               ! do we need next year data?
+               ! do we need next file data?
                IF( sd(jf)%rec_a(1) > zreclast ) THEN
 
-                  sd(jf)%rec_a(1) = 1.              ! force to read the first record of the next year
+                  sd(jf)%rec_a(1) = 1.              ! force to read the first record of the next file
 
                   IF( .NOT. sd(jf)%ln_clim ) THEN   ! close the current file and open a new one.
-                     
-                     llnxtyr  = sd(jf)%cltype /= 'monthly' .OR. nmonth == 12   ! do we need to open next year file?
-                     ! if the run finishes at the end of the current year/month, we do accept that next year/month file does 
-                     ! not exist. If the run continue farther than the current year/month, next year/month file must exist
+
+                     llnxtmth = sd(jf)%cltype == 'monthly' .OR. nday == nmonth_len(nmonth)      ! open next month file?
+                     llnxtyr  = sd(jf)%cltype == 'yearly'  .OR. (nmonth == 12 .AND. llnxtmth)   ! open next year  file?
+
+                     ! if the run finishes at the end of the current year/month/day, we will allow next year/month/day file to be
+                     ! not present. If the run continue further than the current year/month/day, next year/month/day file must exist
                      zsecend = rsec_year + sec1jan000 + REAL(nitend - kt, wp) * rdttra(1)   ! second at the end of the run 
                      llstop = zsecend > sd(jf)%swap_sec                                 ! read more than 1 record of next year
 
-                     CALL fld_clopn( sd(jf), nyear + COUNT((/llnxtyr/)), nmonth + 1 - 12 * COUNT((/llnxtyr/)), llstop )
-
-                     IF( sd(jf)%num == 0 .AND. .NOT. llstop ) THEN    ! next year file is not existing
-                        CALL ctl_warn('next year/month file: '//TRIM(sd(jf)%clname)//' not existing -> back to current year/month')
-                        CALL fld_clopn( sd(jf), nyear, nmonth )       ! back to the current year/month
+                     CALL fld_clopn( sd(jf), nyear  + COUNT((/llnxtyr /))                                           ,         &
+                        &                    nmonth + COUNT((/llnxtmth/)) - 12                 * COUNT((/llnxtyr /)),         &
+                        &                    nday   + 1                   - nmonth_len(nmonth) * COUNT((/llnxtmth/)), llstop )
+
+                     IF( sd(jf)%num == 0 .AND. .NOT. llstop ) THEN    ! next year file does not exist
+                        CALL ctl_warn('next year/month/day file: '//TRIM(sd(jf)%clname)//     &
+                                &     ' not present -> back to current year/month/day')
+                        CALL fld_clopn( sd(jf), nyear, nmonth, nday )       ! back to the current year/month/day
                         sd(jf)%rec_a(1) = zreclast     ! force to read the last record to be read in the current year file
                      ENDIF
@@ -189,8 +196,8 @@
         
             ELSE
-               ! if we are not doing time interpolation, we must change the year/month of the file just afer switching
-               ! to the NEW year/month. If it is the case, we are at the beginning of the year/month when calling fld_rec
-               ! so sd(jf)%rec_a(1) = 1
-               IF( sd(jf)%rec_a(1) == 1 .AND. .NOT. sd(jf)%ln_clim )   CALL fld_clopn( sd(jf), nyear, nmonth )
+               ! if we are not doing time interpolation, we must change the year/month/day of the file just after switching
+               ! to the NEW year/month/day. If it is the case, we are at the beginning of the year/month/day when calling 
+               ! fld_rec so sd(jf)%rec_a(1) = 1
+               IF( sd(jf)%rec_a(1) == 1 .AND. .NOT. sd(jf)%ln_clim )   CALL fld_clopn( sd(jf), nyear, nmonth, nday )
             ENDIF
 
@@ -309,5 +316,6 @@
       LOGICAL :: llprevyr       ! are we reading previous year  file?
       LOGICAL :: llprevmth      ! are we reading previous month file?
-      LOGICAL :: llprev         ! llprevyr .OR. llprevmth
+      LOGICAL :: llprevday      ! are we reading previous day   file?
+      LOGICAL :: llprev         ! llprevyr .OR. llprevmth .OR. llprevday
       INTEGER :: idvar          ! variable id 
       INTEGER :: inrec          ! number of record existing for this variable
@@ -321,4 +329,5 @@
       llprevyr  = .FALSE.
       llprevmth = .FALSE.
+      llprevday = .FALSE.
             
       ! define record informations
@@ -329,28 +338,41 @@
          IF( sdjf%rec_b(1) == 0.e0 ) THEN   ! we redefine record sdjf%rec_b(1) with the last record of previous year file
             IF( sdjf%freqh == -1. ) THEN    ! monthly mean
-               sdjf%rec_b(1) = 12.          ! force to read december mean
-               llprevyr = .NOT. sdjf%ln_clim
+               IF( sdjf%cltype == 'monthly' ) THEN   ! monthly file
+                  sdjf%rec_b(1) = 1.                                                       ! force to read the unique record
+                  llprevmth = .NOT. sdjf%ln_clim                                           ! use previous month file?
+                  llprevyr  = llprevmth .AND. nmonth == 1                                  ! use previous year  file?
+               ELSE                                  ! yearly file
+                  sdjf%rec_b(1) = 12.                                                      ! force to read december mean
+                  llprevyr = .NOT. sdjf%ln_clim                                            ! use previous year  file?
+               ENDIF
             ELSE   
                IF( sdjf%cltype == 'monthly' ) THEN   ! monthly file
-                  sdjf%rec_b(1) = 24. / sdjf%freqh * REAL( nmonth_len(nmonth-1), wp )   ! last record of previous month
-                  llprevmth = .NOT. sdjf%ln_clim                                        ! use previous month file?
-                  llprevyr  = .NOT. sdjf%ln_clim .AND. nmonth == 1                      ! use previous year  file?
+                  sdjf%rec_b(1) = 24. / sdjf%freqh * REAL( nmonth_len(nmonth-1), wp )      ! last record of previous month
+                  llprevmth = .NOT. sdjf%ln_clim                                           ! use previous month file?
+                  llprevyr  = llprevmth .AND. nmonth == 1                                  ! use previous year  file?
+               ELSEIF( sdjf%cltype == 'daily' ) THEN ! daily file
+                  sdjf%rec_b(1) = 24. / sdjf%freqh                                         ! last record of previous day
+                  llprevday = .NOT. sdjf%ln_clim                                           ! use previous day   file?
+                  llprevmth = llprevday .AND. nday   == 1                                  ! use previous month file?
+                  llprevyr  = llprevmth .AND. nmonth == 1                                  ! use previous year  file?
                ELSE                                  ! yearly file
-                  sdjf%rec_b(1) = 24. / sdjf%freqh * REAL( nyear_len(0), wp )           ! last record of year month
-                  llprevyr  = .NOT. sdjf%ln_clim                                        ! use previous year  file?
+                  sdjf%rec_b(1) = 24. / sdjf%freqh * REAL( nyear_len(0), wp )              ! last record of year month
+                  llprevyr = .NOT. sdjf%ln_clim                                            ! use previous year  file?
                ENDIF
             ENDIF
          ENDIF
-         llprev = llprevyr .OR. llprevmth
-
-         CALL fld_clopn( sdjf, nyear - COUNT((/llprevyr/)), nmonth - COUNT((/llprevmth/)) + 12 * COUNT((/llprevyr/)), .NOT. llprev )
-
-         ! if previous year/month file is not existing, we switch to the current year/month
+         llprev = llprevyr .OR. llprevmth .OR. llprevday
+
+         CALL fld_clopn( sdjf, nyear  - COUNT((/llprevyr /))                                              ,               &
+            &                  nmonth - COUNT((/llprevmth/)) + 12                   * COUNT((/llprevyr /)),               &
+            &                  nday   - COUNT((/llprevday/)) + nmonth_len(nmonth-1) * COUNT((/llprevmth/)), .NOT. llprev )
+         
+         ! if previous year/month/day file does not exist, we switch to the current year/month/day
          IF( llprev .AND. sdjf%num == 0 ) THEN
-            CALL ctl_warn( 'previous year/month file: '//TRIM(sdjf%clname)//' not existing -> back to current year/month' )
-            ! we force to read the first record of the current year/month instead of last record of previous year/month
+            CALL ctl_warn( 'previous year/month/day file: '//TRIM(sdjf%clname)//' not present -> back to current year/month/day')
+            ! we force to read the first record of the current year/month/day instead of last record of previous year/month/day
             llprev = .false.
             sdjf%rec_b(1) = 1.
-            CALL fld_clopn( sdjf, nyear, nmonth )
+            CALL fld_clopn( sdjf, nyear, nmonth, nday )
          ENDIF
          
@@ -378,5 +400,5 @@
       ENDIF
 
-      IF( sdjf%num == 0 )   CALL fld_clopn( sdjf, nyear, nmonth )   ! make sure current year/month file is opened
+      IF( sdjf%num == 0 )   CALL fld_clopn( sdjf, nyear, nmonth, nday )   ! make sure current year/month/day file is opened
 
       sdjf%swap_sec = rsec_year + sec1jan000 - 1.   ! force read/update the after data in the following part of fld_read 
@@ -433,6 +455,7 @@
          zfreq_sec = sdjf%freqh * 3600.   ! frequency mean (in seconds)
          ! number of second since the beginning of the file
-         IF( sdjf%cltype == 'monthly' ) THEN   ;   ztmp = rsec_month   ! since Jan 1 of the current year
-         ELSE                                  ;   ztmp = rsec_year    ! since the first day of the current month
+         IF(     sdjf%cltype == 'monthly' ) THEN   ;   ztmp = rsec_month   ! since 00h on the first day of the current month
+         ELSEIF( sdjf%cltype == 'daily'   ) THEN   ;   ztmp = rsec_day     ! since 00h of the current day
+         ELSE                                      ;   ztmp = rsec_year    ! since 00h on Jan 1 of the current year
          ENDIF
          IF( sdjf%ln_tint ) THEN                ! time interpolation, shift by 1/2 record
@@ -469,12 +492,16 @@
          ! after record index and second since Jan. 1st 00h of nit000 year
          sdjf%rec_a(:) = (/ zrec, zfreq_sec * ( zrec - 0.5 ) + sec1jan000 /)
-         IF( sdjf%cltype == 'monthly' )   &   ! add the number of second between Jan 1 and the end of previous month
+         IF( sdjf%cltype == 'monthly' )   &   ! add the number of seconds between 00h Jan 1 and the end of previous month
             sdjf%rec_a(2) = sdjf%rec_a(2) + rday * REAL(SUM(nmonth_len(1:nmonth -1)), wp)   ! ok if nmonth=1
+         IF( sdjf%cltype == 'daily'   )   &   ! add the number of seconds between 00h Jan 1 and the end of previous day
+            sdjf%rec_a(2) = sdjf%rec_a(2) + rday * ( nday_year - 1 )
 
          ! before record index and second since Jan. 1st 00h of nit000 year
          zrec = zrec - 1.                           ! move back to previous record
          sdjf%rec_b(:) = (/ zrec, zfreq_sec * ( zrec - 0.5 ) + sec1jan000 /)
-         IF( sdjf%cltype == 'monthly' )   &   ! add the number of second between Jan 1 and the end of previous month
+         IF( sdjf%cltype == 'monthly' )   &   ! add the number of seconds between 00h Jan 1 and the end of previous month
             sdjf%rec_b(2) = sdjf%rec_b(2) + rday * REAL(SUM(nmonth_len(1:nmonth -1)), wp)   ! ok if nmonth=1
+         IF( sdjf%cltype == 'daily'   )   &   ! add the number of seconds between 00h Jan 1 and the end of previous day
+            sdjf%rec_b(2) = sdjf%rec_b(2) + rday * ( nday_year - 1 )
 
          ! swapping time in second since Jan. 1st 00h of nit000 year
@@ -488,5 +515,5 @@
 
 
-   SUBROUTINE fld_clopn( sdjf, kyear, kmonth, ldstop )
+   SUBROUTINE fld_clopn( sdjf, kyear, kmonth, kday, ldstop )
       !!---------------------------------------------------------------------
       !!                    ***  ROUTINE fld_clopn  ***
@@ -499,4 +526,5 @@
       INTEGER  , INTENT(in   )           ::   kyear    ! year value
       INTEGER  , INTENT(in   )           ::   kmonth   ! month value
+      INTEGER  , INTENT(in   )           ::   kday     ! day value
       LOGICAL  , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
 
@@ -504,5 +532,6 @@
       ! build the new filename if not climatological data
       IF( .NOT. sdjf%ln_clim ) THEN   ;   WRITE(sdjf%clname, '(a,"_y",i4.4)' ) TRIM( sdjf%clrootname ), kyear    ! add year
-         IF( sdjf%cltype == 'monthly' )   WRITE(sdjf%clname, '(a,"m",i2.2)'  ) TRIM( sdjf%clname     ), kmonth   ! add month
+         IF( sdjf%cltype /= 'yearly' )    WRITE(sdjf%clname, '(a,"m" ,i2.2)' ) TRIM( sdjf%clname     ), kmonth   ! add month
+         IF( sdjf%cltype == 'daily'  )    WRITE(sdjf%clname, '(a,"d" ,i2.2)' ) TRIM( sdjf%clname     ), kday     ! add day
       ENDIF
       CALL iom_open( sdjf%clname, sdjf%num, ldstop = ldstop, ldiof =  LEN(TRIM(sdjf%wgtname)) > 0 )
@@ -536,5 +565,5 @@
          sdf(jf)%ln_clim    = sdf_n(jf)%ln_clim
          IF( sdf(jf)%freqh == -1. ) THEN   ;    sdf(jf)%cltype = 'yearly'
-         ELSE                               ;    sdf(jf)%cltype = sdf_n(jf)%cltype
+         ELSE                              ;    sdf(jf)%cltype = sdf_n(jf)%cltype
          ENDIF
          sdf(jf)%wgtname = " "
