Index: trunk/NEMO/OFF_SRC/DIA/dianam.F90
===================================================================
--- trunk/NEMO/OFF_SRC/DIA/dianam.F90	(revision 1672)
+++ trunk/NEMO/OFF_SRC/DIA/dianam.F90	(revision 1703)
@@ -4,21 +4,25 @@
    !! Ocean diagnostics:  Builds output file name
    !!=====================================================================
+   !! History :  OPA  ! 1999-02  (E. Guilyardi)  Creation for 30 days/month
+   !!   NEMO     1.0  ! 2002-06  (G. Madec)  F90: Free form and module
+   !!            3.2  ! 2009-11  (S. Masson) complete rewriting, works for all calendars...
+   !!----------------------------------------------------------------------
 
    !!----------------------------------------------------------------------
    !!   dia_nam       : Builds output file name
    !!----------------------------------------------------------------------
-   !! * Modules used
    USE dom_oce         ! ocean space and time domain
    USE phycst          ! physical constants
    USE in_out_manager  ! I/O manager
    USE daymod          ! calendar
+   USE ioipsl, ONLY :  ju2ymds    ! for calendar
 
    IMPLICIT NONE
    PRIVATE
 
-   !! * Routine accessibility
-   PUBLIC dia_nam   ! routine called by step.F90
+   PUBLIC dia_nam
+
    !!----------------------------------------------------------------------
-   !!   OPA 9.0 , LOCEAN-IPSL (2005) 
+   !! NEMO/OPA 3.2 , LOCEAN-IPSL (2009) 
    !! $Id$ 
    !! This software is governed by the CeCILL licence see modipsl/doc/NEMO_CeCILL.txt 
@@ -34,26 +38,24 @@
       !!
       !! ** Method  :   File name is a function of date and output frequency
-      !!      cdfnam=<cexper>_<clave>_<idtbeg>_<idtend>_grid_<cdsuff>
+      !!      cdfnam=<cexper>_<clave>_<idtbeg>_<idtend>_<cdsuff>
       !!      <clave> = averaging frequency (DA, MO, etc...)
       !!      <idtbeg>,<idtend> date of beginning and end of run
       !!
-      !! History :
-      !!        !  99-02  (E. Guilyardi)  Creation for 30 days/month
-      !!   8.5  !  02-06  (G. Madec)  F90: Free form and module
       !!----------------------------------------------------------------------
-      !! * Arguments
-      CHARACTER (len=*), INTENT( out ) ::   cdfnam   ! file name
-      CHARACTER (len=*), INTENT( in  ) ::   cdsuff   ! ???
-      INTEGER,           INTENT( in  ) ::   kfreq    ! ???
-
-      !! * Local declarations
-      CHARACTER (len=2) ::   clave
-      CHARACTER (len=3) ::   clave1
-      CHARACTER (len=5) ::   clout
-      INTEGER :: jt                       ! dummy loop indices
-      INTEGER :: ig, ijjmm, iout          ! temporary integers
-      INTEGER :: iyear1, imonth1, iday1   !    "          "
-      INTEGER :: iyear2, imonth2, iday2   !    "          "
-      REAL(wp) ::  z5j, znbsec, zdate1, zdate2, zdrun, zdt   ! temporary scalars
+      CHARACTER (len=*), INTENT(  out) ::   cdfnam           ! file name
+      CHARACTER (len=*), INTENT(in   ) ::   cdsuff           ! to be added at the end of the file name
+      INTEGER,           INTENT(in   ) ::   kfreq            ! output frequency (in time-step): < 0 for monthly outputs
+      !                                                                                         0 if no frequency
+      CHARACTER (len=20) ::   clfmt, clfmt0                       ! writing format
+      CHARACTER (len=20) ::   clave                               ! name for output frequency
+      CHARACTER (len=20) ::   cldate1                             ! date of the beginning of run
+      CHARACTER (len=20) ::   cldate2                             ! date of the end       of run
+      INTEGER            ::   iyear1, imonth1, iday1              ! year, month, day of the first day of the run
+      INTEGER            ::   iyear2, imonth2, iday2              ! year, month, day of the last  day of the run
+      INTEGER            ::   indg                                ! number of digits needed to write a number     
+      INTEGER            ::   inbsec, inbmn, inbhr, inbday, inbyr ! output frequency in seconds, minutes, hours, days and years
+      INTEGER            ::   iddss, ihhss, immss                 ! number of seconds in 1 day, 1 hour and 1 minute
+      REAL(wp)           ::   zsec1, zsec2                        ! not used
+      REAL(wp)           ::   zdrun, zjul                         ! temporary scalars
       !!----------------------------------------------------------------------
 
@@ -63,266 +65,58 @@
       IF(lwp) WRITE(numout,*)
 
-      ! 0. Initialisation
-      ! -----------------
+      ! name for output frequency
 
-      cdfnam = ''
-
-      !    number of seconds of the run
-
-      z5j = 5*rjjss
-      zdt = rdt
-      IF( nacc == 1 ) zdt = rdtmin
-      zdrun = FLOAT( nitend - nit000 ) * zdt
-
-      !  date of beginning of run
-
-      iyear1  = ndastp/10000
-      imonth1 = ndastp/100 - iyear1*100
-      iday1   = ndastp - imonth1*100 - iyear1*10000
-      IF( nleapy == 1) THEN 
-         ijjmm=0
-         IF( MOD( iyear1, 4 ) == 0 ) THEN
-            DO jt = 1, imonth1-1
-               ijjmm = ijjmm + nbiss(jt)
-            END DO
-         ELSE
-            DO jt = 1, imonth1-1
-               ijjmm = ijjmm + nobis(jt)
-            END DO
+      inbsec = kfreq * NINT( rdttra(1) )                                    ! output frequency in seconds
+      iddss = NINT( rday          )                                         ! number of seconds in 1 day
+      ihhss = NINT( rmmss * rhhmm )                                         ! number of seconds in 1 hour
+      immss = NINT( rmmss         )                                         ! number of seconds in 1 minute
+      clfmt0 = "('(a,i',i1,',a)')"                                          ! format '(a,ix,a)' with x to be defined
+      ! 
+      IF(          inbsec == 0           ) THEN   ;   clave = ''            ! no frequency
+      ELSEIF(      inbsec <  0           ) THEN   ;   clave = '_1m'         ! frequency in month
+      ELSEIF( MOD( inbsec, iddss  ) == 0 ) THEN                             ! frequency in days
+         inbday = inbsec / iddss
+         indg = INT(LOG10(REAL(inbday,wp))) + 1                             ! number of digits needed to write days    frequency
+         WRITE(clfmt, clfmt0) indg                ;   WRITE(clave, clfmt) '_', inbday, 'd'
+         IF( inbday == nmonth_len(nmonth) )           clave = '_1m'
+         IF( MOD( inbday, nyear_len(1) ) == 0 ) THEN                        ! frequency in years
+            inbyr = inbday / nyear_len(1)
+            indg  = INT(LOG10(REAL(inbyr ,wp))) + 1                         ! number of digits needed to write years   frequency
+            WRITE(clfmt, clfmt0) indg                ;   WRITE(clave, clfmt) '_', inbyr, 'y'
          ENDIF
-         ijjmm = ijjmm + (iyear1-1)/4
-         zdate1 = ( (iyear1-1)*365 + ijjmm +iday1-1 ) * rjjss   
-      ELSE IF( nleapy == 0 ) THEN
-         ijjmm = 0
-         DO jt = 1, imonth1-1
-            ijjmm = ijjmm + nobis(jt)
-         END DO
-         zdate1 = ( (iyear1-1)*raajj + ijjmm + iday1-1)* rjjss
-      ELSE 
-         zdate1 = ( (iyear1-1)*nleapy*raamo + (imonth1-1)*nleapy + iday1-1)* rjjss
+      ELSEIF( MOD( inbsec, ihhss ) == 0 ) THEN                              ! frequency in hours
+         inbhr = inbsec / ihhss
+         indg = INT(LOG10(REAL(inbhr ,wp))) + 1                             ! number of digits needed to write hours   frequency
+         WRITE(clfmt, clfmt0) indg                ;   WRITE(clave, clfmt) '_', inbhr, 'h'
+      ELSEIF( MOD( inbsec, immss ) == 0 ) THEN                              ! frequency in minutes
+         inbmn = inbsec / immss
+         indg = INT(LOG10(REAL(inbmn ,wp))) + 1                             ! number of digits needed to write minutes frequency
+         WRITE(clfmt, clfmt0) indg                ;   WRITE(clave, clfmt) '_', inbmn, 'mn'
+      ELSE                                                                  ! frequency in seconds
+         indg = INT(LOG10(REAL(inbsec,wp))) + 1                             ! number of digits needed to write seconds frequency
+         WRITE(clfmt, clfmt0) indg                ;   WRITE(clave, clfmt) '_', inbsec, 's'
       ENDIF
 
-      !  date of end of run (= date of beginning of next run)
+      ! date of the beginning and the end of the run
 
-      zdate2 = zdate1 + zdrun
-      IF( nleapy == 1 ) THEN 
-         iyear2 = zdate2/(365.25*rjjss)+1
-         ijjmm = INT(zdate2/rjjss)-365*(iyear2-1)-(iyear2-1)/4
-         IF( ijjmm < 0 ) THEN
-            iyear2 = iyear2-1
-            ijjmm = zdate2/rjjss-365.*(iyear2-1)-(iyear2-1)/4
-         ENDIF
-         IF( MOD( iyear2, 4 ) == 0 ) THEN
-            DO jt = 1, 12
-               ijjmm = ijjmm - nbiss(jt)
-               IF( ijjmm <= 0 ) go to 10
-            END DO
-            jt = 12
-10          CONTINUE
-            imonth2 = jt
-            ijjmm = 0
-            DO jt = 1, jt-1
-               ijjmm = ijjmm + nbiss(jt)
-            END DO
-         ELSE
-            DO jt = 1, 12
-               ijjmm = ijjmm - nobis(jt)
-               IF( ijjmm <= 0 ) go to 15
-            END DO
-            jt = 12
-15          CONTINUE
-            imonth2 = jt
-            ijjmm = 0
-            DO jt = 1, jt-1
-               ijjmm = ijjmm + nobis(jt)
-            END DO
-         ENDIF
-         iday2 = zdate2/rjjss-365.*(iyear2-1)-ijjmm+1-(iyear2-1)/4     
-      ELSE IF( nleapy == 0 ) THEN
-         iyear2 = zdate2/raass+1
-         ijjmm  = zdate2/rjjss-raajj*(iyear2-1)
-         DO jt = 1, 12
-            ijjmm = ijjmm - nobis(jt)
-            IF(ijjmm <= 0) go to 20
-         END DO
-         jt = 12
-20       CONTINUE
-         imonth2 = jt
-         ijjmm = 0
-         DO jt = 1, jt-1
-            ijjmm = ijjmm + nobis(jt)
-         END DO
-         iday2 = zdate2/rjjss-raajj*(iyear2-1)-ijjmm+1          
-      ELSE 
-         zdate2 = zdate2 / rjjss
-         imonth2 = zdate2/FLOAT(nleapy)
-         iday2 = zdate2 - imonth2*FLOAT(nleapy) + 1.
-         iyear2 = imonth2/12
-         imonth2 = imonth2 - iyear2*12
-         imonth2 = imonth2 + 1
-         iyear2 = iyear2 + 1
-         IF( iday2 == 0 ) THEN
-            iday2 = nleapy
-            imonth2 = imonth2 - 1
-            IF( imonth2 == 0 ) THEN
-               imonth2 = 12
-               iyear2 = iyear2 - 1
-            ENDIF
-         ENDIF
+      zdrun = rdttra(1) / rday * REAL( nitend - nit000, wp )                ! length of the run in days
+      zjul  = fjulday - rdttra(1) / rday
+      CALL ju2ymds( zjul        , iyear1, imonth1, iday1, zsec1 )           ! year/month/day of the beginning of run
+      CALL ju2ymds( zjul + zdrun, iyear2, imonth2, iday2, zsec2 )           ! year/month/day of the end       of run
+
+      IF( iyear2 < 10000 ) THEN   ;   clfmt = "(i4.4,2i2.2)"                ! format used to write the date 
+      ELSE                        ;   WRITE(clfmt, "('(i',i1,',2i2.2)')") INT(LOG10(REAL(iyear2,wp))) + 1
       ENDIF
 
-
-      ! 1. Define time averaging period <nn><type>
-      !    ---------------------------------------
-
-      iout = 0
-#if defined key_diainstant
-      clave = 'IN'
-      IF( iyear2 <= 99 ) THEN 
-         WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-      ELSE IF( iyear2 <= 999 ) THEN 
-         WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-      ELSE IF( iyear2 <= 9999 ) THEN 
-         WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-      ELSE
-         WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-      ENDIF
-#else
-
-      znbsec=kfreq*zdt
-      ! daily output
-      IF( znbsec == rjjss ) THEN
-         clave = '1d'
-         IF( iyear2 <= 99 ) THEN 
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN 
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN 
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-         ! 5 day output 
-      ELSE IF( znbsec == z5j ) THEN
-         clave='5d'
-         IF( iyear2 <= 99 ) THEN 
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN 
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN 
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-         ! monthly ouput 
-      ELSE IF( (znbsec == rmoss .AND. nleapy > 1) .OR.   &
-               (znbsec >= 28*rjjss .AND. znbsec <= 31*rjjss .AND. nleapy <= 1) ) THEN
-         clave = '1m'
-         IF( iyear2 <= 99 ) THEN 
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN 
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN 
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-         ! annual output
-      ELSE IF( (znbsec == raass .AND. nleapy > 1) .OR.   &
-               (znbsec >= 365*rjjss .AND. znbsec <= 366*rjjss .AND. nleapy <= 1) ) THEN
-         clave = '1y'
-         IF( iyear2 <= 99 ) THEN 
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN 
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN 
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-      ELSE IF( (znbsec == 5.*raass .AND. nleapy > 1) .OR.   &
-               (znbsec >= 5.*365*rjjss .AND. znbsec <= 5.*366*rjjss .AND. nleapy <= 1) ) THEN
-         clave = '5y'
-         IF( iyear2 <= 99 ) THEN
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-      ELSE IF( (znbsec == 10.*raass .AND. nleapy > 1) .OR.   &
-               (znbsec >= 10.*365*rjjss .AND. znbsec <= 10.*366*rjjss .AND. nleapy <= 1) ) THEN
-         clave1 = '10y'
-         iout = 1
-         IF( iyear2 <= 99 ) THEN
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-      ELSE
-
-         ! others
-         iout = kfreq
-         ig = 0
-         clout = ''
-         IF( iout <= 9 ) THEN 
-            ig = 1
-            WRITE(clout,'(i1.1)') iout
-         ELSE IF( iout <= 99 ) THEN 
-            ig = 2
-            WRITE(clout,'(i2.2)') iout
-         ELSE IF( iout <= 999 ) THEN 
-            ig = 3
-            WRITE(clout,'(i3.3)') iout
-         ELSE IF( iout <= 9999 ) THEN 
-            ig = 4
-            WRITE(clout,'(i4.4)') iout
-         ELSE
-            ig = 5
-            WRITE(clout,'(i5.5)') iout
-         ENDIF
-         clave = 'CU'
-         IF( iyear2 <= 99 ) THEN 
-            WRITE(cdfnam,9001) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 999 ) THEN 
-            WRITE(cdfnam,9002) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE IF( iyear2 <= 9999 ) THEN 
-            WRITE(cdfnam,9003) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ELSE
-            WRITE(cdfnam,9004) iyear1,imonth1,iday1,iyear2,imonth2,iday2
-         ENDIF
-      ENDIF
-#endif
-      IF( iout == 0 ) THEN 
-         cdfnam = TRIM(cexper)//"_"//clave//TRIM(cdfnam)//TRIM(cdsuff)
-      ELSE IF( iout == 1 .AND. kfreq > 1 ) THEN
-         cdfnam = TRIM(cexper)//"_"//clave1//TRIM(cdfnam)//TRIM(cdsuff)
-      ELSE
-         cdfnam = TRIM(cexper)//"_"//clave//TRIM(clout)//TRIM(cdfnam)//TRIM(cdsuff)
-      ENDIF
+      WRITE(cldate1, clfmt) iyear1, imonth1, iday1                          ! date of the beginning of run
+      WRITE(cldate2, clfmt) iyear2, imonth2, iday2                          ! date of the end       of run
+ 
+      cdfnam = TRIM(cexper)//TRIM(clave)//"_"//TRIM(cldate1)//"_"//TRIM(cldate2)//"_"//TRIM(cdsuff)
+#if defined key_agrif
+      if ( .NOT. Agrif_Root() ) cdfnam = TRIM(Agrif_CFixed())//'_'//TRIM(cdfnam)
+#endif    
 
       IF(lwp) WRITE(numout,*) cdfnam     
       IF(lwp) WRITE(numout,*)          
-
-      ! FORMATS
-
- 9001 FORMAT("_",I4.4,2I2.2,"_",I4.4,2I2.2,"_")
- 9002 FORMAT("_",I4.4,2I2.2,"_",I4.4,2I2.2,"_")
- 9003 FORMAT("_",I4.4,2I2.2,"_",I4.4,2I2.2,"_")
- 9004 FORMAT("_",I6.6,2I2.2,"_",I6.6,2I2.2,"_")
- 9011 FORMAT("_",I4.4,I2.2,"_",I4.4,I2.2,"_")
- 9012 FORMAT("_",I4.4,I2.2,"_",I4.4,I2.2,"_")
- 9013 FORMAT("_",I4.4,I2.2,"_",I4.4,I2.2,"_")
- 9014 FORMAT("_",I6.6,I2.2,"_",I6.6,I2.2,"_")
- 9021 FORMAT("_",I4.4,"_",I4.4,"_")
- 9022 FORMAT("_",I4.4,"_",I4.4,"_")
- 9023 FORMAT("_",I4.4,"_",I4.4,"_")
- 9024 FORMAT("_",I6.6,"_",I6.6,"_")
 
    END SUBROUTINE dia_nam
