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

source: branches/UKMO/dev_r5518_GO6_package_XIOS_read/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8009

Last change on this file since 8009 was 8009, checked in by andmirek, 7 years ago

remove prints

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