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 @ 8014

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

enable setting restat file name

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