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

source: branches/UKMO/AMM15_v3_6_STABLE_package_reanalysis2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 11038

Last change on this file since 11038 was 11038, checked in by rrenshaw, 5 years ago

code fixes

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