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/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 7593

Last change on this file since 7593 was 7593, checked in by hadjt, 7 years ago

Added xios output for DIA/diadct.F90 NOOS 25hr mean output (not touched hourly mean).
Added namelist switched for DIA/diaregmean.F90 to control output of PEA, MLD, and SBC variables

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