New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in branches/2016/dev_r6409_SIMPLIF_2_usrdef_tools/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/2016/dev_r6409_SIMPLIF_2_usrdef_tools/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6827

Last change on this file since 6827 was 6827, checked in by flavoni, 8 years ago

#1692 - branch SIMPLIF_2_usrdef: commit routines Fortran to create domain_cfg.nc file, to have domain (input) files for new simplification branch. To be moved in TOOLS directory

  • Property svn:keywords set to Id
File size: 86.1 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!            3.6  ! 2014-15  DIMG format removed
11   !!--------------------------------------------------------------------
12
13   !!--------------------------------------------------------------------
14   !!   iom_open       : open a file read only
15   !!   iom_close      : close a file or all files opened by iom
16   !!   iom_get        : read a field (interfaced to several routines)
17   !!   iom_gettime    : read the time axis cdvar in the file
18   !!   iom_varid      : get the id of a variable in a file
19   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
20   !!--------------------------------------------------------------------
21   USE dom_oce         ! ocean space and time domain
22   USE lbclnk          ! lateal boundary condition / mpp exchanges
23   USE iom_def         ! iom variables definitions
24   USE iom_nf90        ! NetCDF format with native NetCDF library
25   USE in_out_manager  ! I/O manager
26   USE lib_mpp         ! MPP library
27   USE domngb          ! ocean space and time domain
28   USE phycst          ! physical constants
29 !SF  USE dianam          ! build name of file
30   USE xios
31   USE ioipsl, ONLY :  ju2ymds    ! for calendar
32
33   IMPLICIT NONE
34   PUBLIC   !   must be public to be able to access iom_def through iom
35   
36#if defined key_iomput
37   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
38#else
39   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
40#endif
41   PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
42   PUBLIC iom_getatt, iom_use, iom_context_finalize
43
44   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
45   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
46   PRIVATE iom_p1d, iom_p2d, iom_p3d
47#if defined key_iomput
48   PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
49   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
50# endif
51
52   INTERFACE iom_get
53      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
54   END INTERFACE
55   INTERFACE iom_getatt
56      MODULE PROCEDURE iom_g0d_intatt
57   END INTERFACE
58   INTERFACE iom_rstput
59      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
60   END INTERFACE
61  INTERFACE iom_put
62     MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d
63  END INTERFACE
64
65   !!----------------------------------------------------------------------
66   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
67   !! $Id$
68   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
69   !!----------------------------------------------------------------------
70
71CONTAINS
72
73   SUBROUTINE iom_init( cdname ) 
74      !!----------------------------------------------------------------------
75      !!                     ***  ROUTINE   ***
76      !!
77      !! ** Purpose :   
78      !!
79      !!----------------------------------------------------------------------
80      CHARACTER(len=*), INTENT(in)  :: cdname
81#if defined key_iomput
82      TYPE(xios_time)   :: dtime    = xios_time(0, 0, 0, 0, 0, 0)
83      CHARACTER(len=19) :: cldate 
84      CHARACTER(len=10) :: clname
85      INTEGER           ::   ji
86      !
87      REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: z_bnds
88      !!----------------------------------------------------------------------
89
90      ALLOCATE( z_bnds(jpk,2) )
91
92      clname = cdname
93      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
94      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
95      CALL iom_swap( cdname )
96
97      ! calendar parameters
98      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
99      CASE ( 1)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "Gregorian")
100      CASE ( 0)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "NoLeap")
101      CASE (30)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "D360")
102      END SELECT
103      WRITE(cldate,"(i4.4,'-',i2.2,'-',i2.2,' ',i2.2,':',i2.2,':00')") nyear,nmonth,nday,nhour,nminute
104      CALL xios_set_context_attr(TRIM(clname), start_date=cldate )
105
106      ! horizontal grid definition
107      CALL set_scalar
108
109      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN 
110         CALL set_grid( "T", glamt, gphit ) 
111         CALL set_grid( "U", glamu, gphiu )
112         CALL set_grid( "V", glamv, gphiv )
113         CALL set_grid( "W", glamt, gphit )
114         CALL set_grid_znl( gphit )
115         !
116         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
117            CALL iom_set_domain_attr("grid_T", area = e1e2t(nldi:nlei, nldj:nlej))
118            CALL iom_set_domain_attr("grid_U", area = e1e2u(nldi:nlei, nldj:nlej))
119            CALL iom_set_domain_attr("grid_V", area = e1e2v(nldi:nlei, nldj:nlej))
120            CALL iom_set_domain_attr("grid_W", area = e1e2t(nldi:nlei, nldj:nlej))
121            CALL set_grid_bounds( "T", glamf, gphif, glamt, gphit )
122            CALL set_grid_bounds( "U", glamv, gphiv, glamu, gphiu )
123            CALL set_grid_bounds( "V", glamu, gphiu, glamv, gphiv )
124            CALL set_grid_bounds( "W", glamf, gphif, glamt, gphit )
125         ENDIF
126      ENDIF
127
128      ! vertical grid definition
129      CALL iom_set_axis_attr( "deptht", gdept_1d )
130      CALL iom_set_axis_attr( "depthu", gdept_1d )
131      CALL iom_set_axis_attr( "depthv", gdept_1d )
132      CALL iom_set_axis_attr( "depthw", gdepw_1d )
133
134      ! Add vertical grid bounds
135      z_bnds(:      ,1) = gdepw_1d(:)
136      z_bnds(1:jpkm1,2) = gdepw_1d(2:jpk)
137      z_bnds(jpk:   ,2) = gdepw_1d(jpk) + e3t_1d(jpk)
138      CALL iom_set_axis_attr( "deptht", bounds=z_bnds )
139      CALL iom_set_axis_attr( "depthu", bounds=z_bnds )
140      CALL iom_set_axis_attr( "depthv", bounds=z_bnds )
141      z_bnds(:    ,2) = gdept_1d(:)
142      z_bnds(2:jpk,1) = gdept_1d(1:jpkm1)
143      z_bnds(1    ,1) = gdept_1d(1) - e3w_1d(1)
144      CALL iom_set_axis_attr( "depthw", bounds=z_bnds )
145
146      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
147      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
148     
149      ! automatic definitions of some of the xml attributs
150      CALL set_xmlatt
151
152      ! end file definition
153      dtime%second = rdt
154      CALL xios_set_timestep(dtime)
155      CALL xios_close_context_definition()
156     
157      CALL xios_update_calendar(0)
158
159      DEALLOCATE( z_bnds )
160
161#endif
162     
163   END SUBROUTINE iom_init
164
165
166   SUBROUTINE iom_swap( cdname )
167      !!---------------------------------------------------------------------
168      !!                   ***  SUBROUTINE  iom_swap  ***
169      !!
170      !! ** Purpose :  swap context between different agrif grid for xmlio_server
171      !!---------------------------------------------------------------------
172      CHARACTER(len=*), INTENT(in) :: cdname
173#if defined key_iomput
174      TYPE(xios_context) :: nemo_hdl
175
176      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
177        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
178      ELSE
179        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
180      ENDIF
181      !
182      CALL xios_set_current_context(nemo_hdl)
183#endif
184      !
185   END SUBROUTINE iom_swap
186
187
188   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
189      !!---------------------------------------------------------------------
190      !!                   ***  SUBROUTINE  iom_open  ***
191      !!
192      !! ** Purpose :  open an input file (return 0 if not found)
193      !!---------------------------------------------------------------------
194      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
195      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
196      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
197      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
198      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
199      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
200      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
201
202      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
203      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
204      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc"
205      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
206      CHARACTER(LEN=256)    ::   clinfo    ! info character
207      LOGICAL               ::   llok      ! check the existence
208      LOGICAL               ::   llwrt     ! local definition of ldwrt
209      LOGICAL               ::   llnoov    ! local definition to read overlap
210      LOGICAL               ::   llstop    ! local definition of ldstop
211      LOGICAL               ::   lliof     ! local definition of ldiof
212      INTEGER               ::   iolib     ! library do we use to open the file
213      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
214      INTEGER               ::   iln, ils  ! lengths of character
215      INTEGER               ::   idom      ! type of domain
216      INTEGER               ::   istop     !
217      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
218      ! local number of points for x,y dimensions
219      ! position of first local point for x,y dimensions
220      ! position of last local point for x,y dimensions
221      ! start halo size for x,y dimensions
222      ! end halo size for x,y dimensions
223      !---------------------------------------------------------------------
224      ! Initializations and control
225      ! =============
226      kiomid = -1
227      clinfo = '                    iom_open ~~~  '
228      istop = nstop
229      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
230      ! (could be done when defining iom_file in f95 but not in f90)
231      IF( Agrif_Root() ) THEN
232         IF( iom_open_init == 0 ) THEN
233            iom_file(:)%nfid = 0
234            iom_open_init = 1
235         ENDIF
236      ENDIF
237      ! do we read or write the file?
238      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
239      ELSE                        ;   llwrt = .FALSE.
240      ENDIF
241      ! do we call ctl_stop if we try to open a non-existing file in read mode?
242      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
243      ELSE                         ;   llstop = .TRUE.
244      ENDIF
245      ! what library do we use to open the file?
246      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
247      ELSE                         ;   iolib = jpnf90
248      ENDIF
249      ! are we using interpolation on the fly?
250      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
251      ELSE                        ;   lliof = .FALSE.
252      ENDIF
253      ! do we read the overlap
254      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
255      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
256      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
257      ! =============
258      clname   = trim(cdname)
259      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
260         iln    = INDEX(clname,'/') 
261         cltmpn = clname(1:iln)
262         clname = clname(iln+1:LEN_TRIM(clname))
263         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
264      ENDIF
265      ! which suffix should we use?
266      SELECT CASE (iolib)
267      CASE (jpnf90   ) ;   clsuffix = '.nc'
268      CASE DEFAULT     ;   clsuffix = ''
269         CALL ctl_stop( TRIM(clinfo), 'accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
270      END SELECT
271      ! Add the suffix if needed
272      iln = LEN_TRIM(clname)
273      ils = LEN_TRIM(clsuffix)
274      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
275         &   clname = TRIM(clname)//TRIM(clsuffix)
276      cltmpn = clname   ! store this name
277      ! try to find if the file to be opened already exist
278      ! =============
279      INQUIRE( FILE = clname, EXIST = llok )
280      IF( .NOT.llok ) THEN
281         ! we try to add the cpu number to the name
282         WRITE(clcpu,*) narea-1
283
284         clcpu  = TRIM(ADJUSTL(clcpu))
285         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
286         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
287         icnt = 0
288         INQUIRE( FILE = clname, EXIST = llok ) 
289         ! we try different formats for the cpu number by adding 0
290         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
291            clcpu  = "0"//trim(clcpu)
292            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
293            INQUIRE( FILE = clname, EXIST = llok )
294            icnt = icnt + 1
295         END DO
296      ENDIF
297      IF( llwrt ) THEN
298         ! check the domain definition
299! JMM + SM: ugly patch before getting the new version of lib_mpp)
300!         idom = jpdom_local_noovlap   ! default definition
301         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
302         ELSE                ;   idom = jpdom_local_full      ! default definition
303         ENDIF
304         IF( PRESENT(kdom) )   idom = kdom
305         ! create the domain informations
306         ! =============
307         SELECT CASE (idom)
308         CASE (jpdom_local_full)
309            idompar(:,1) = (/ jpi             , jpj              /)
310            idompar(:,2) = (/ nimpp           , njmpp            /)
311            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
312            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
313            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
314         CASE (jpdom_local_noextra)
315            idompar(:,1) = (/ nlci            , nlcj             /)
316            idompar(:,2) = (/ nimpp           , njmpp            /)
317            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
318            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
319            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
320         CASE (jpdom_local_noovlap)
321            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
322            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
323            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
324            idompar(:,4) = (/ 0               , 0                /)
325            idompar(:,5) = (/ 0               , 0                /)
326         CASE DEFAULT
327            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
328         END SELECT
329      ENDIF
330      ! Open the NetCDF file
331      ! =============
332      ! do we have some free file identifier?
333      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
334         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
335      ! if no file was found...
336      IF( .NOT. llok ) THEN
337         IF( .NOT. llwrt ) THEN   ! we are in read mode
338            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
339            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
340            ENDIF
341         ELSE                     ! we are in write mode so we
342            clname = cltmpn       ! get back the file name without the cpu number
343         ENDIF
344      ELSE
345         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
346            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
347            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
348         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
349            clname = cltmpn       ! overwrite so get back the file name without the cpu number
350         ENDIF
351      ENDIF
352      IF( istop == nstop ) THEN   ! no error within this routine
353         SELECT CASE (iolib)
354         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
355         CASE DEFAULT
356            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
357         END SELECT
358      ENDIF
359      !
360   END SUBROUTINE iom_open
361
362
363   SUBROUTINE iom_close( kiomid )
364      !!--------------------------------------------------------------------
365      !!                   ***  SUBROUTINE  iom_close  ***
366      !!
367      !! ** Purpose : close an input file, or all files opened by iom
368      !!--------------------------------------------------------------------
369      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
370      !                                              ! return 0 when file is properly closed
371      !                                              ! No argument: all files opened by iom are closed
372
373      INTEGER ::   jf         ! dummy loop indices
374      INTEGER ::   i_s, i_e   ! temporary integer
375      CHARACTER(LEN=100)    ::   clinfo    ! info character
376      !---------------------------------------------------------------------
377      !
378      clinfo = '                    iom_close ~~~  '
379      IF( PRESENT(kiomid) ) THEN
380         i_s = kiomid
381         i_e = kiomid
382      ELSE
383         i_s = 1
384         i_e = jpmax_files
385      ENDIF
386
387      IF( i_s > 0 ) THEN
388         DO jf = i_s, i_e
389            IF( iom_file(jf)%nfid > 0 ) THEN
390               SELECT CASE (iom_file(jf)%iolib)
391               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
392               CASE DEFAULT
393                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
394               END SELECT
395               iom_file(jf)%nfid       = 0          ! free the id
396               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
397               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
398            ELSEIF( PRESENT(kiomid) ) THEN
399               WRITE(ctmp1,*) '--->',  kiomid
400               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
401            ENDIF
402         END DO
403      ENDIF
404      !   
405   END SUBROUTINE iom_close
406
407
408   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
409      !!-----------------------------------------------------------------------
410      !!                  ***  FUNCTION  iom_varid  ***
411      !!
412      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
413      !!-----------------------------------------------------------------------
414      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
415      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
416      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
417      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
418      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
419      !
420      INTEGER                        ::   iom_varid, iiv, i_nvd
421      LOGICAL                        ::   ll_fnd
422      CHARACTER(LEN=100)             ::   clinfo                   ! info character
423      LOGICAL                        ::   llstop                   ! local definition of ldstop
424      !!-----------------------------------------------------------------------
425      iom_varid = 0                         ! default definition
426      ! do we call ctl_stop if we look for non-existing variable?
427      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
428      ELSE                         ;   llstop = .TRUE.
429      ENDIF
430      !
431      IF( kiomid > 0 ) THEN
432         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
433         IF( iom_file(kiomid)%nfid == 0 ) THEN
434            CALL ctl_stop( trim(clinfo), 'the file is not open' )
435         ELSE
436            ll_fnd  = .FALSE.
437            iiv = 0
438            !
439            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
440               iiv = iiv + 1
441               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
442            END DO
443            !
444            IF( .NOT.ll_fnd ) THEN
445               iiv = iiv + 1
446               IF( iiv <= jpmax_vars ) THEN
447                  SELECT CASE (iom_file(kiomid)%iolib)
448                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
449                  CASE DEFAULT
450                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
451                  END SELECT
452               ELSE
453                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
454                        &                         'increase the parameter jpmax_vars')
455               ENDIF
456               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
457            ELSE
458               iom_varid = iiv
459               IF( PRESENT(kdimsz) ) THEN
460                  i_nvd = iom_file(kiomid)%ndims(iiv)
461                  IF( i_nvd == size(kdimsz) ) THEN
462                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
463                  ELSE
464                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
465                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
466                  ENDIF
467               ENDIF
468               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
469            ENDIF
470         ENDIF
471      ENDIF
472      !
473   END FUNCTION iom_varid
474
475
476   !!----------------------------------------------------------------------
477   !!                   INTERFACE iom_get
478   !!----------------------------------------------------------------------
479   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
480      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
481      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
482      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
483      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
484      !
485      INTEGER                                         ::   idvar     ! variable id
486      INTEGER                                         ::   idmspc    ! number of spatial dimensions
487      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
488      CHARACTER(LEN=100)                              ::   clinfo    ! info character
489      CHARACTER(LEN=100)                              ::   clname    ! file name
490      CHARACTER(LEN=1)                                ::   cldmspc   !
491      !
492      itime = 1
493      IF( PRESENT(ktime) ) itime = ktime
494      !
495      clname = iom_file(kiomid)%name
496      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
497      !
498      IF( kiomid > 0 ) THEN
499         idvar = iom_varid( kiomid, cdvar )
500         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
501            idmspc = iom_file ( kiomid )%ndims( idvar )
502            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
503            WRITE(cldmspc , fmt='(i1)') idmspc
504            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
505                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
506                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
507            SELECT CASE (iom_file(kiomid)%iolib)
508            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
509            CASE DEFAULT
510               CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
511            END SELECT
512         ENDIF
513      ENDIF
514   END SUBROUTINE iom_g0d
515
516   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
517      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
518      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
519      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
520      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
521      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
522      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
523      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
524      !
525      IF( kiomid > 0 ) THEN
526         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
527              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
528      ENDIF
529   END SUBROUTINE iom_g1d
530
531   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
532      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
533      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
534      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
535      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
536      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
537      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
538      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
539      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
540                                                                               ! look for and use a file attribute
541                                                                               ! called open_ocean_jstart to set the start
542                                                                               ! value for the 2nd dimension (netcdf only)
543      !
544      IF( kiomid > 0 ) THEN
545         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
546              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
547              &                                                     lrowattr=lrowattr )
548      ENDIF
549   END SUBROUTINE iom_g2d
550
551   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
552      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
553      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
554      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
555      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
556      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
557      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
558      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
559      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
560                                                                                 ! look for and use a file attribute
561                                                                                 ! called open_ocean_jstart to set the start
562                                                                                 ! value for the 2nd dimension (netcdf only)
563      !
564      IF( kiomid > 0 ) THEN
565         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
566              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
567              &                                                     lrowattr=lrowattr )
568      ENDIF
569   END SUBROUTINE iom_g3d
570   !!----------------------------------------------------------------------
571
572   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
573         &                  pv_r1d, pv_r2d, pv_r3d,   &
574         &                  ktime , kstart, kcount,   &
575         &                  lrowattr                )
576      !!-----------------------------------------------------------------------
577      !!                  ***  ROUTINE  iom_get_123d  ***
578      !!
579      !! ** Purpose : read a 1D/2D/3D variable
580      !!
581      !! ** Method : read ONE record at each CALL
582      !!-----------------------------------------------------------------------
583      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
584      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
585      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
586      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
587      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
588      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
589      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
590      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
591      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
592      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
593                                                                           ! look for and use a file attribute
594                                                                           ! called open_ocean_jstart to set the start
595                                                                           ! value for the 2nd dimension (netcdf only)
596      !
597      LOGICAL                        ::   llnoov      ! local definition to read overlap
598      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
599      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
600      INTEGER                        ::   jl          ! loop on number of dimension
601      INTEGER                        ::   idom        ! type of domain
602      INTEGER                        ::   idvar       ! id of the variable
603      INTEGER                        ::   inbdim      ! number of dimensions of the variable
604      INTEGER                        ::   idmspc      ! number of spatial dimensions
605      INTEGER                        ::   itime       ! record number
606      INTEGER                        ::   istop       ! temporary value of nstop
607      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
608      INTEGER                        ::   ji, jj      ! loop counters
609      INTEGER                        ::   irankpv     !
610      INTEGER                        ::   ind1, ind2  ! substring index
611      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
612      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
613      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
614      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
615      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
616      INTEGER                        ::   itmp        ! temporary integer
617      CHARACTER(LEN=256)             ::   clinfo      ! info character
618      CHARACTER(LEN=256)             ::   clname      ! file name
619      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
620      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
621      !---------------------------------------------------------------------
622      !
623      clname = iom_file(kiomid)%name   !   esier to read
624      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
625      ! local definition of the domain ?
626      idom = kdom
627      ! do we read the overlap
628      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
629      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
630      ! check kcount and kstart optionals parameters...
631      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
632      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
633      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
634     &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
635
636      luse_jattr = .false.
637      IF( PRESENT(lrowattr) ) THEN
638         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
639         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
640      ENDIF
641      IF( luse_jattr ) THEN
642         SELECT CASE (iom_file(kiomid)%iolib)
643         CASE (jpnf90   )   
644             ! Ok
645         CASE DEFAULT   
646            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
647         END SELECT
648      ENDIF
649
650      ! Search for the variable in the data base (eventually actualize data)
651      istop = nstop
652      idvar = iom_varid( kiomid, cdvar )
653      !
654      IF( idvar > 0 ) THEN
655         ! to write iom_file(kiomid)%dimsz in a shorter way !
656         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
657         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
658         idmspc = inbdim                                   ! number of spatial dimensions in the file
659         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
660         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
661         !
662         ! update idom definition...
663         ! Identify the domain in case of jpdom_auto(glo/dta) definition
664         IF( idom == jpdom_autoglo_xy ) THEN
665            ll_depth_spec = .TRUE.
666            idom = jpdom_autoglo
667         ELSE
668            ll_depth_spec = .FALSE.
669         ENDIF
670         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
671            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
672            ELSE                               ;   idom = jpdom_data
673            ENDIF
674            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
675            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
676            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
677         ENDIF
678         ! Identify the domain in case of jpdom_local definition
679         IF( idom == jpdom_local ) THEN
680            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
681            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
682            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
683            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
684            ENDIF
685         ENDIF
686         !
687         ! check the consistency between input array and data rank in the file
688         !
689         ! initializations
690         itime = 1
691         IF( PRESENT(ktime) ) itime = ktime
692
693         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
694         WRITE(clrankpv, fmt='(i1)') irankpv
695         WRITE(cldmspc , fmt='(i1)') idmspc
696         !
697         IF(     idmspc <  irankpv ) THEN
698            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
699               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
700         ELSEIF( idmspc == irankpv ) THEN
701            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
702               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
703         ELSEIF( idmspc >  irankpv ) THEN
704               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
705                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
706                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
707                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
708                  idmspc = idmspc - 1
709               ELSE
710                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
711                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
712                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
713               ENDIF
714         ENDIF
715
716         !
717         ! definition of istart and icnt
718         !
719         icnt  (:) = 1
720         istart(:) = 1
721         istart(idmspc+1) = itime
722
723         IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
724         ELSE
725            IF(           idom == jpdom_unknown ) THEN                                                ; icnt(1:idmspc) = idimsz(1:idmspc)
726            ELSE
727               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
728                  IF(     idom == jpdom_data    ) THEN
729                     jstartrow = 1
730                     IF( luse_jattr ) THEN
731                        CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
732                        jstartrow = MAX(1,jstartrow)
733                     ENDIF
734                     istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
735                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
736                  ENDIF
737                  ! we do not read the overlap                     -> we start to read at nldi, nldj
738! JMM + SM: ugly patch before getting the new version of lib_mpp)
739!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
740                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
741                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
742! JMM + SM: ugly patch before getting the new version of lib_mpp)
743!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
744                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
745                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
746                  ENDIF
747                  IF( PRESENT(pv_r3d) ) THEN
748                     IF( idom == jpdom_data ) THEN                                  ; icnt(3) = jpkdta
749                     ELSE IF( ll_depth_spec .AND. PRESENT(kstart) ) THEN            ; istart(3) = kstart(3); icnt(3) = kcount(3)
750                     ELSE                                                           ; icnt(3) = jpk
751                     ENDIF
752                  ENDIF
753               ENDIF
754            ENDIF
755         ENDIF
756
757         ! check that istart and icnt can be used with this file
758         !-
759         DO jl = 1, jpmax_dims
760            itmp = istart(jl)+icnt(jl)-1
761            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
762               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
763               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
764               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
765            ENDIF
766         END DO
767
768         ! check that icnt matches the input array
769         !-     
770         IF( idom == jpdom_unknown ) THEN
771            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
772            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
773            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
774            ctmp1 = 'd'
775         ELSE
776            IF( irankpv == 2 ) THEN
777! JMM + SM: ugly patch before getting the new version of lib_mpp)
778!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
779               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
780               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
781               ENDIF
782            ENDIF
783            IF( irankpv == 3 ) THEN 
784! JMM + SM: ugly patch before getting the new version of lib_mpp)
785!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
786               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
787               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
788               ENDIF
789            ENDIF
790         ENDIF
791         
792         DO jl = 1, irankpv
793            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
794            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
795         END DO
796
797      ENDIF
798
799      ! read the data
800      !-     
801      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
802         !
803         ! find the right index of the array to be read
804! JMM + SM: ugly patch before getting the new version of lib_mpp)
805!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
806!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
807!         ENDIF
808         IF( llnoov ) THEN
809            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
810            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
811            ENDIF
812         ELSE
813            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
814            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
815            ENDIF
816         ENDIF
817     
818         SELECT CASE (iom_file(kiomid)%iolib)
819         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
820            &                                         pv_r1d, pv_r2d, pv_r3d )
821         CASE DEFAULT
822            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
823         END SELECT
824
825         IF( istop == nstop ) THEN   ! no additional errors until this point...
826            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
827         
828            !--- overlap areas and extra hallows (mpp)
829            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
830               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
831            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
832               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
833               IF( icnt(3) == jpk ) THEN
834                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
835               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
836                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
837                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
838               ENDIF
839            ENDIF
840           
841            !--- Apply scale_factor and offset
842            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
843            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
844            IF(     PRESENT(pv_r1d) ) THEN
845               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
846               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
847            ELSEIF( PRESENT(pv_r2d) ) THEN
848               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
849               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
850            ELSEIF( PRESENT(pv_r3d) ) THEN
851               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
852               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
853            ENDIF
854            !
855         ENDIF
856         !
857      ENDIF
858      !
859   END SUBROUTINE iom_get_123d
860
861
862   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
863      !!--------------------------------------------------------------------
864      !!                   ***  SUBROUTINE iom_gettime  ***
865      !!
866      !! ** Purpose : read the time axis cdvar in the file
867      !!--------------------------------------------------------------------
868      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
869      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
870      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
871      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
872      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
873      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
874      !
875      INTEGER, DIMENSION(1) :: kdimsz
876      INTEGER            ::   idvar    ! id of the variable
877      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
878      CHARACTER(LEN=100) ::   clinfo   ! info character
879      !---------------------------------------------------------------------
880      !
881      IF ( PRESENT(cdvar) ) THEN
882         tname = cdvar
883      ELSE
884         tname = iom_file(kiomid)%uldname
885      ENDIF
886      IF( kiomid > 0 ) THEN
887         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
888         IF ( PRESENT(kntime) ) THEN
889            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
890            kntime = kdimsz(1)
891         ELSE
892            idvar = iom_varid( kiomid, tname )
893         ENDIF
894         !
895         ptime(:) = 0. ! default definition
896         IF( idvar > 0 ) THEN
897            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
898               IF( iom_file(kiomid)%luld(idvar) ) THEN
899                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
900                     SELECT CASE (iom_file(kiomid)%iolib)
901                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
902                     CASE DEFAULT
903                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
904                     END SELECT
905                  ELSE
906                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
907                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
908                  ENDIF
909               ELSE
910                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
911               ENDIF
912            ELSE
913               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
914            ENDIF
915         ELSE
916            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
917         ENDIF
918      ENDIF
919      !
920   END SUBROUTINE iom_gettime
921
922
923   !!----------------------------------------------------------------------
924   !!                   INTERFACE iom_getatt
925   !!----------------------------------------------------------------------
926   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
927      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
928      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
929      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
930      !
931      IF( kiomid > 0 ) THEN
932         IF( iom_file(kiomid)%nfid > 0 ) THEN
933            SELECT CASE (iom_file(kiomid)%iolib)
934            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
935            CASE DEFAULT
936               CALL ctl_stop( 'iom_g0d_att: accepted IO library is only jpnf90' )
937            END SELECT
938         ENDIF
939      ENDIF
940   END SUBROUTINE iom_g0d_intatt
941
942
943   !!----------------------------------------------------------------------
944   !!                   INTERFACE iom_rstput
945   !!----------------------------------------------------------------------
946   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
947      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
948      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
949      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
950      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
951      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
952      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
953      INTEGER :: ivid   ! variable id
954      IF( kiomid > 0 ) THEN
955         IF( iom_file(kiomid)%nfid > 0 ) THEN
956            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
957            SELECT CASE (iom_file(kiomid)%iolib)
958            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
959            CASE DEFAULT
960               CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
961            END SELECT
962         ENDIF
963      ENDIF
964   END SUBROUTINE iom_rp0d
965
966   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
967      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
968      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
969      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
970      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
971      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
972      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
973      INTEGER :: ivid   ! variable id
974      IF( kiomid > 0 ) THEN
975         IF( iom_file(kiomid)%nfid > 0 ) THEN
976            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
977            SELECT CASE (iom_file(kiomid)%iolib)
978            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
979            CASE DEFAULT
980               CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
981            END SELECT
982         ENDIF
983      ENDIF
984   END SUBROUTINE iom_rp1d
985
986   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
987      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
988      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
989      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
990      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
991      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
992      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
993      INTEGER :: ivid   ! variable id
994      IF( kiomid > 0 ) THEN
995         IF( iom_file(kiomid)%nfid > 0 ) THEN
996            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
997            SELECT CASE (iom_file(kiomid)%iolib)
998            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
999            CASE DEFAULT
1000               CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1001            END SELECT
1002         ENDIF
1003      ENDIF
1004   END SUBROUTINE iom_rp2d
1005
1006   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1007      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1008      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1009      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1010      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1011      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1012      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1013      INTEGER :: ivid   ! variable id
1014      IF( kiomid > 0 ) THEN
1015         IF( iom_file(kiomid)%nfid > 0 ) THEN
1016            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1017            SELECT CASE (iom_file(kiomid)%iolib)
1018            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1019            CASE DEFAULT
1020               CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1021            END SELECT
1022         ENDIF
1023      ENDIF
1024   END SUBROUTINE iom_rp3d
1025
1026
1027   !!----------------------------------------------------------------------
1028   !!                   INTERFACE iom_put
1029   !!----------------------------------------------------------------------
1030   SUBROUTINE iom_p0d( cdname, pfield0d )
1031      CHARACTER(LEN=*), INTENT(in) ::   cdname
1032      REAL(wp)        , INTENT(in) ::   pfield0d
1033      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1034#if defined key_iomput
1035      zz(:,:)=pfield0d
1036      CALL xios_send_field(cdname, zz)
1037      !CALL xios_send_field(cdname, (/pfield0d/))
1038#else
1039      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1040#endif
1041   END SUBROUTINE iom_p0d
1042
1043   SUBROUTINE iom_p1d( cdname, pfield1d )
1044      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1045      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1046#if defined key_iomput
1047      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1048#else
1049      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1050#endif
1051   END SUBROUTINE iom_p1d
1052
1053   SUBROUTINE iom_p2d( cdname, pfield2d )
1054      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1055      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1056#if defined key_iomput
1057      CALL xios_send_field(cdname, pfield2d)
1058#else
1059      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1060#endif
1061   END SUBROUTINE iom_p2d
1062
1063   SUBROUTINE iom_p3d( cdname, pfield3d )
1064      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1065      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1066#if defined key_iomput
1067      CALL xios_send_field(cdname, pfield3d)
1068#else
1069      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1070#endif
1071   END SUBROUTINE iom_p3d
1072   !!----------------------------------------------------------------------
1073
1074#if defined key_iomput
1075
1076   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1077      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1078      &                                    nvertex, bounds_lon, bounds_lat, area )
1079      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1080      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1081      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1082      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1083      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1084      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1085      LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1086
1087      IF ( xios_is_valid_domain     (cdid) ) THEN
1088         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1089            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1090            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1091            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1092            &    bounds_lat=bounds_lat, area=area )
1093      ENDIF
1094
1095      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1096         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1097            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1098            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1099            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1100            &    bounds_lat=bounds_lat, area=area )
1101      ENDIF
1102      CALL xios_solve_inheritance()
1103
1104   END SUBROUTINE iom_set_domain_attr
1105
1106
1107   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1108      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1109      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1110      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1111      IF ( PRESENT(paxis) ) THEN
1112         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1113         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1114      ENDIF
1115      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1116      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1117      CALL xios_solve_inheritance()
1118   END SUBROUTINE iom_set_axis_attr
1119
1120
1121   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1122      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1123      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_op
1124      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_offset
1125      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1126    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1127      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1128    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1129      CALL xios_solve_inheritance()
1130   END SUBROUTINE iom_set_field_attr
1131
1132
1133   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1134      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1135      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1136      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1137      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1138      CALL xios_solve_inheritance()
1139   END SUBROUTINE iom_set_file_attr
1140
1141
1142   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1143      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1144      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix, output_freq
1145      LOGICAL                                 ::   llexist1,llexist2,llexist3
1146      !---------------------------------------------------------------------
1147      IF( PRESENT( name        ) )   name = ''          ! default values
1148      IF( PRESENT( name_suffix ) )   name_suffix = ''
1149      IF( PRESENT( output_freq ) )   output_freq = ''
1150      IF ( xios_is_valid_file     (cdid) ) THEN
1151         CALL xios_solve_inheritance()
1152         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1153         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1154         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1155         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1156      ENDIF
1157      IF ( xios_is_valid_filegroup(cdid) ) THEN
1158         CALL xios_solve_inheritance()
1159         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1160         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1161         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1162         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1163      ENDIF
1164   END SUBROUTINE iom_get_file_attr
1165
1166
1167   SUBROUTINE iom_set_grid_attr( cdid, mask )
1168      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1169      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1170      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1171      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1172      CALL xios_solve_inheritance()
1173   END SUBROUTINE iom_set_grid_attr
1174
1175   SUBROUTINE iom_setkt( kt, cdname )
1176      INTEGER         , INTENT(in) ::   kt 
1177      CHARACTER(LEN=*), INTENT(in) ::   cdname
1178      !     
1179      CALL iom_swap( cdname )   ! swap to cdname context
1180      CALL xios_update_calendar(kt)
1181      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1182      !
1183   END SUBROUTINE iom_setkt
1184
1185   SUBROUTINE iom_context_finalize( cdname )
1186      CHARACTER(LEN=*), INTENT(in) :: cdname
1187      !
1188      IF( xios_is_valid_context(cdname) ) THEN
1189         CALL iom_swap( cdname )   ! swap to cdname context
1190         CALL xios_context_finalize() ! finalize the context
1191         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1192      ENDIF
1193      !
1194   END SUBROUTINE iom_context_finalize
1195
1196
1197   SUBROUTINE set_grid( cdgrd, plon, plat )
1198      !!----------------------------------------------------------------------
1199      !!                     ***  ROUTINE set_grid  ***
1200      !!
1201      !! ** Purpose :   define horizontal grids
1202      !!
1203      !!----------------------------------------------------------------------
1204      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1205      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1206      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1207      !
1208      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1209      INTEGER  :: ni,nj
1210     
1211      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1212
1213      CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1214      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1215      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1216         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1217
1218      IF ( ln_mskland ) THEN
1219         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1220         SELECT CASE ( cdgrd )
1221         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1222         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1223         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1224         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1225         END SELECT
1226         !
1227         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1228         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1229      ENDIF
1230     
1231   END SUBROUTINE set_grid
1232
1233
1234   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1235      !!----------------------------------------------------------------------
1236      !!                   ***  ROUTINE set_grid_bounds  ***
1237      !!
1238      !! ** Purpose :   define horizontal grid corners
1239      !!
1240      !!----------------------------------------------------------------------
1241      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1242      !
1243      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1244      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1245      !
1246      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1247      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1248      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1249      !
1250      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1251      !                                                          ! represents the bottom-left corner of cell (i,j)
1252      INTEGER :: ji, jj, jn, ni, nj
1253
1254      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1255
1256      ! Offset of coordinate representing bottom-left corner
1257      SELECT CASE ( TRIM(cdgrd) )
1258         CASE ('T', 'W')
1259            icnr = -1 ; jcnr = -1
1260         CASE ('U')
1261            icnr =  0 ; jcnr = -1
1262         CASE ('V')
1263            icnr = -1 ; jcnr =  0
1264      END SELECT
1265
1266      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1267
1268      z_fld(:,:) = 1._wp
1269      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1270
1271      ! Cell vertices that can be defined
1272      DO jj = 2, jpjm1
1273         DO ji = 2, jpim1
1274            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1275            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1276            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1277            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1278            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1279            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1280            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1281            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1282         END DO
1283      END DO
1284
1285      ! Cell vertices on boundries
1286      DO jn = 1, 4
1287         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1288         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1289      END DO
1290
1291      ! Zero-size cells at closed boundaries if cell points provided,
1292      ! otherwise they are closed cells with unrealistic bounds
1293      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1294         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1295            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1296               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1297            END DO
1298         ENDIF
1299         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1300            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1301               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1302            END DO
1303         ENDIF
1304         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1305            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1306               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1307            END DO
1308         ENDIF
1309         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1310            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1311               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1312            END DO
1313         ENDIF
1314      ENDIF
1315
1316      ! Rotate cells at the north fold
1317      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1318         DO jj = 1, jpj
1319            DO ji = 1, jpi
1320               IF( z_fld(ji,jj) == -1. ) THEN
1321                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1322                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1323                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1324               ENDIF
1325            END DO
1326         END DO
1327
1328      ! Invert cells at the symmetric equator
1329      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1330         DO ji = 1, jpi
1331            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1332            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1333            z_bnds(:,ji,1,:) = z_rot(:,:)
1334         END DO
1335      ENDIF
1336
1337      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1338                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1339
1340      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1341
1342   END SUBROUTINE set_grid_bounds
1343
1344
1345   SUBROUTINE set_grid_znl( plat )
1346      !!----------------------------------------------------------------------
1347      !!                     ***  ROUTINE set_grid_znl  ***
1348      !!
1349      !! ** Purpose :   define grids for zonal mean
1350      !!
1351      !!----------------------------------------------------------------------
1352      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1353      !
1354      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
1355      INTEGER  :: ni,nj, ix, iy
1356
1357     
1358      ni=nlei-nldi+1 ; nj=nlej-nldj+1            ! define zonal mean domain (jpj*jpk)
1359      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0.
1360
1361      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1362      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1363      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1364         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1365      !
1366      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1367      CALL iom_set_domain_attr ('ptr', zoom_ibegin=ix, zoom_nj=jpjglo)
1368      CALL iom_update_file_name('ptr')
1369      !
1370   END SUBROUTINE set_grid_znl
1371
1372   SUBROUTINE set_scalar
1373      !!----------------------------------------------------------------------
1374      !!                     ***  ROUTINE set_scalar  ***
1375      !!
1376      !! ** Purpose :   define fake grids for scalar point
1377      !!
1378      !!----------------------------------------------------------------------
1379      REAL(wp), DIMENSION(1)   ::   zz = 1.
1380      !!----------------------------------------------------------------------
1381      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1382      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1383     
1384      zz=REAL(narea,wp)
1385      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1386
1387   END SUBROUTINE set_scalar
1388
1389
1390   SUBROUTINE set_xmlatt
1391      !!----------------------------------------------------------------------
1392      !!                     ***  ROUTINE set_xmlatt  ***
1393      !!
1394      !! ** Purpose :   automatic definitions of some of the xml attributs...
1395      !!
1396      !!----------------------------------------------------------------------
1397      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1398      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1399      CHARACTER(len=1)               ::   cl1                      ! 1 character
1400      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1401      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1402      INTEGER                        ::   ji, jg                   ! loop counters
1403      INTEGER                        ::   ix, iy                   ! i-,j- index
1404      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1405      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1406      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1407      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1408      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1409      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1410      !!----------------------------------------------------------------------
1411      !
1412      ! frequency of the call of iom_put (attribut: freq_op)
1413      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op = cl1//'ts', freq_offset='0ts')
1414       
1415      ! output file names (attribut: name)
1416      DO ji = 1, 9
1417         WRITE(cl1,'(i1)') ji 
1418         CALL iom_update_file_name('file'//cl1)
1419      END DO
1420      DO ji = 1, 99
1421         WRITE(cl2,'(i2.2)') ji 
1422         CALL iom_update_file_name('file'//cl2)
1423      END DO
1424      DO ji = 1, 999
1425         WRITE(cl3,'(i3.3)') ji 
1426         CALL iom_update_file_name('file'//cl3)
1427      END DO
1428
1429      ! Zooms...
1430      clgrd = (/ 'T', 'U', 'W' /) 
1431      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1432         cl1 = clgrd(jg)
1433         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1434         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1435         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1436         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1437         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1438         CALL iom_update_file_name('Eq'//cl1)
1439      END DO
1440      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1441      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1442      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1443      CALL set_mooring( zlontao, zlattao )
1444      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1445      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1446      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1447      CALL set_mooring( zlonrama, zlatrama )
1448      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1449      zlonpira = (/ -38.0, -23.0, -10.0 /)
1450      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1451      CALL set_mooring( zlonpira, zlatpira )
1452
1453     
1454   END SUBROUTINE set_xmlatt
1455
1456
1457   SUBROUTINE set_mooring( plon, plat)
1458      !!----------------------------------------------------------------------
1459      !!                     ***  ROUTINE set_mooring  ***
1460      !!
1461      !! ** Purpose :   automatic definitions of moorings xml attributs...
1462      !!
1463      !!----------------------------------------------------------------------
1464      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1465      !
1466!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1467      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1468      CHARACTER(len=256)            ::   clname                   ! file name
1469      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1470      CHARACTER(len=1)              ::   cl1                      ! 1 character
1471      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1472      INTEGER                       ::   ji, jj, jg               ! loop counters
1473      INTEGER                       ::   ix, iy                   ! i-,j- index
1474      REAL(wp)                      ::   zlon, zlat
1475      !!----------------------------------------------------------------------
1476      DO jg = 1, SIZE(clgrd)
1477         cl1 = clgrd(jg)
1478         DO ji = 1, SIZE(plon)
1479            DO jj = 1, SIZE(plat)
1480               zlon = plon(ji)
1481               zlat = plat(jj)
1482               ! modifications for RAMA moorings
1483               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1484               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1485               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1486               ! modifications for PIRATA moorings
1487               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1488               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1489               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1490               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1491               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1492               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1493               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1494               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1495               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1496               IF( zlon >= 0. ) THEN 
1497                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1498                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1499                  ENDIF
1500               ELSE             
1501                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1502                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1503                  ENDIF
1504               ENDIF
1505               IF( zlat >= 0. ) THEN 
1506                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1507                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1508                  ENDIF
1509               ELSE             
1510                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1511                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1512                  ENDIF
1513               ENDIF
1514               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1515               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1516               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1517               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1518               CALL iom_update_file_name(TRIM(clname)//cl1)
1519            END DO
1520         END DO
1521      END DO
1522     
1523   END SUBROUTINE set_mooring
1524
1525   
1526   SUBROUTINE iom_update_file_name( cdid )
1527      !!----------------------------------------------------------------------
1528      !!                     ***  ROUTINE iom_update_file_name  ***
1529      !!
1530      !! ** Purpose :   
1531      !!
1532      !!----------------------------------------------------------------------
1533      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1534      !
1535      CHARACTER(LEN=256) ::   clname
1536      CHARACTER(LEN=20)  ::   clfreq
1537      CHARACTER(LEN=20)  ::   cldate
1538      INTEGER            ::   idx
1539      INTEGER            ::   jn
1540      INTEGER            ::   itrlen
1541      INTEGER            ::   iyear, imonth, iday, isec
1542      REAL(wp)           ::   zsec
1543      LOGICAL            ::   llexist
1544      !!----------------------------------------------------------------------
1545
1546      DO jn = 1,2
1547
1548         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1549         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1550
1551         IF ( TRIM(clname) /= '' ) THEN
1552
1553            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1554            DO WHILE ( idx /= 0 ) 
1555               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1556               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1557            END DO
1558
1559            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1560            DO WHILE ( idx /= 0 ) 
1561               IF ( TRIM(clfreq) /= '' ) THEN
1562                  itrlen = LEN_TRIM(clfreq)
1563                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1564                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1565               ELSE
1566                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1567                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1568               ENDIF
1569               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1570            END DO
1571
1572            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1573            DO WHILE ( idx /= 0 ) 
1574               cldate = iom_sdate( fjulday - rdt / rday )
1575               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1576               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1577            END DO
1578
1579            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1580            DO WHILE ( idx /= 0 ) 
1581               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
1582               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1583               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1584            END DO
1585
1586            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1587            DO WHILE ( idx /= 0 ) 
1588               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1589               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1590               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1591            END DO
1592
1593            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1594            DO WHILE ( idx /= 0 ) 
1595               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1596               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1597               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1598            END DO
1599
1600            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
1601            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1602            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1603
1604         ENDIF
1605
1606      END DO
1607
1608   END SUBROUTINE iom_update_file_name
1609
1610
1611   FUNCTION iom_sdate( pjday, ld24, ldfull )
1612      !!----------------------------------------------------------------------
1613      !!                     ***  ROUTINE iom_sdate  ***
1614      !!
1615      !! ** Purpose :   send back the date corresponding to the given julian day
1616      !!
1617      !!----------------------------------------------------------------------
1618      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1619      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1620      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1621      !
1622      CHARACTER(LEN=20) ::   iom_sdate
1623      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1624      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1625      REAL(wp)          ::   zsec
1626      LOGICAL           ::   ll24, llfull
1627      !
1628      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1629      ELSE                       ;   ll24 = .FALSE.
1630      ENDIF
1631
1632      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1633      ELSE                         ;   llfull = .FALSE.
1634      ENDIF
1635
1636      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1637      isec = NINT(zsec)
1638
1639      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1640         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1641         isec = 86400
1642      ENDIF
1643
1644      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1645      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1646      ENDIF
1647     
1648!$AGRIF_DO_NOT_TREAT     
1649! Should be fixed in the conv
1650      IF( llfull ) THEN
1651         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1652         ihour   = isec / 3600
1653         isec    = MOD(isec, 3600)
1654         iminute = isec / 60
1655         isec    = MOD(isec, 60)
1656         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1657      ELSE
1658         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1659      ENDIF
1660!$AGRIF_END_DO_NOT_TREAT     
1661
1662   END FUNCTION iom_sdate
1663
1664#else
1665
1666
1667   SUBROUTINE iom_setkt( kt, cdname )
1668      INTEGER         , INTENT(in)::   kt 
1669      CHARACTER(LEN=*), INTENT(in) ::   cdname
1670      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1671   END SUBROUTINE iom_setkt
1672
1673   SUBROUTINE iom_context_finalize( cdname )
1674      CHARACTER(LEN=*), INTENT(in) ::   cdname
1675      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1676   END SUBROUTINE iom_context_finalize
1677
1678#endif
1679
1680   LOGICAL FUNCTION iom_use( cdname )
1681      CHARACTER(LEN=*), INTENT(in) ::   cdname
1682#if defined key_iomput
1683      iom_use = xios_field_is_active( cdname )
1684#else
1685      iom_use = .FALSE.
1686#endif
1687   END FUNCTION iom_use
1688   
1689   !!======================================================================
1690END MODULE iom
Note: See TracBrowser for help on using the repository browser.