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

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

remove unnecessary print from iom.F90

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