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

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

Reading restart with XIOS works and is bit comparable with NEMO default restart. Tested in MO GO6 eORCA25 configuration - suite u-al584

File size: 108.3 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                   if(lwp) write(numout,*) trim(cdvar),'UPDATE'
1091                ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
1092                   ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
1093                   IF( icnt(3) == jpk ) THEN
1094                      CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
1095                      if(lwp) write(numout,*) trim(cdvar),'UPDATE'
1096                   ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
1097                      DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
1098                      DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
1099                      if(lwp) write(numout,*) trim(cdvar),' NO UPDATE'
1100                   ENDIF
1101                ENDIF
1102                !
1103             ELSE
1104                ! return if istop == nstop is false
1105                RETURN
1106             ENDIF
1107          ELSE
1108             ! return if statment idvar > 0 .AND. istop == nstop is false
1109             RETURN
1110          ENDIF
1111          !
1112       ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
1113#if defined key_iomput
1114!would be good to be able to check which context is active and swap only if current is not restart
1115          CALL iom_swap( TRIM(rxios_context) ) 
1116          IF( PRESENT(pv_r3d) ) THEN
1117             if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
1118             CALL xios_recv_field( trim(cdvar), pv_r3d)
1119             IF(idom /= jpdom_unknown ) then
1120                 CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
1121             ENDIF
1122          ELSEIF( PRESENT(pv_r2d) ) THEN
1123             if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
1124             CALL xios_recv_field( trim(cdvar), pv_r2d)
1125             IF(idom /= jpdom_unknown ) THEN
1126                 CALL lbc_lnk(pv_r2d,'Z',-999.,'no0')
1127             ENDIF
1128          ELSEIF( PRESENT(pv_r1d) ) THEN
1129             if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
1130             CALL xios_recv_field( trim(cdvar), pv_r1d)
1131          ENDIF
1132          CALL iom_swap( TRIM(cxios_context) )
1133#else
1134          istop = istop + 1 
1135          clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
1136#endif
1137       ENDIF
1138!some final adjustments
1139       ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
1140       IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
1141       IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
1142
1143       !--- Apply scale_factor and offset
1144       zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1145       zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1146       IF(     PRESENT(pv_r1d) ) THEN
1147          IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1148          IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1149       ELSEIF( PRESENT(pv_r2d) ) THEN
1150!CDIR COLLAPSE
1151          IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1152!CDIR COLLAPSE
1153          IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1154       ELSEIF( PRESENT(pv_r3d) ) THEN
1155!CDIR COLLAPSE
1156          IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1157!CDIR COLLAPSE
1158          IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1159       ENDIF
1160   END SUBROUTINE iom_get_123d
1161
1162
1163   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
1164      !!--------------------------------------------------------------------
1165      !!                   ***  SUBROUTINE iom_gettime  ***
1166      !!
1167      !! ** Purpose : read the time axis cdvar in the file
1168      !!--------------------------------------------------------------------
1169      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
1170      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
1171      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
1172      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
1173      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
1174      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
1175      !
1176      INTEGER, DIMENSION(1) :: kdimsz
1177      INTEGER            ::   idvar    ! id of the variable
1178      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
1179      CHARACTER(LEN=100) ::   clinfo   ! info character
1180      !---------------------------------------------------------------------
1181      !
1182      IF ( PRESENT(cdvar) ) THEN
1183         tname = cdvar
1184      ELSE
1185         tname = iom_file(kiomid)%uldname
1186      ENDIF
1187      IF( kiomid > 0 ) THEN
1188         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
1189         IF ( PRESENT(kntime) ) THEN
1190            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
1191            kntime = kdimsz(1)
1192         ELSE
1193            idvar = iom_varid( kiomid, tname )
1194         ENDIF
1195         !
1196         ptime(:) = 0. ! default definition
1197         IF( idvar > 0 ) THEN
1198            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
1199               IF( iom_file(kiomid)%luld(idvar) ) THEN
1200                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
1201                     SELECT CASE (iom_file(kiomid)%iolib)
1202                     CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime, cdunits, cdcalendar )
1203                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
1204                     CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
1205                     CASE DEFAULT   
1206                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1207                     END SELECT
1208                  ELSE
1209                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
1210                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
1211                  ENDIF
1212               ELSE
1213                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
1214               ENDIF
1215            ELSE
1216               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
1217            ENDIF
1218         ELSE
1219            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
1220         ENDIF
1221      ENDIF
1222      !
1223   END SUBROUTINE iom_gettime
1224
1225
1226   !!----------------------------------------------------------------------
1227   !!                   INTERFACE iom_getatt
1228   !!----------------------------------------------------------------------
1229   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
1230      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1231      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1232      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
1233      !
1234      IF( kiomid > 0 ) THEN
1235         IF( iom_file(kiomid)%nfid > 0 ) THEN
1236            SELECT CASE (iom_file(kiomid)%iolib)
1237            CASE (jpioipsl )   ;   CALL ctl_stop('iom_getatt: only nf90 available')
1238            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1239            CASE (jprstdimg)   ;   CALL ctl_stop('iom_getatt: only nf90 available')
1240            CASE DEFAULT   
1241               CALL ctl_stop( 'iom_g0d_att: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1242            END SELECT
1243         ENDIF
1244      ENDIF
1245   END SUBROUTINE iom_g0d_intatt
1246
1247
1248   !!----------------------------------------------------------------------
1249   !!                   INTERFACE iom_rstput
1250   !!----------------------------------------------------------------------
1251   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1252      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1253      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1254      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1255      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1256      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1257      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1258      INTEGER :: ivid   ! variable id
1259      IF( kiomid > 0 ) THEN
1260         IF( iom_file(kiomid)%nfid > 0 ) THEN
1261            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1262            SELECT CASE (iom_file(kiomid)%iolib)
1263            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1264            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1265            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
1266            CASE DEFAULT     
1267               CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1268            END SELECT
1269         ENDIF
1270      ENDIF
1271   END SUBROUTINE iom_rp0d
1272
1273   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1274      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1275      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1276      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1277      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1278      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1279      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1280      INTEGER :: ivid   ! variable id
1281      IF( kiomid > 0 ) THEN
1282         IF( iom_file(kiomid)%nfid > 0 ) THEN
1283            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1284            SELECT CASE (iom_file(kiomid)%iolib)
1285            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1286            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1287            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
1288            CASE DEFAULT     
1289               CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1290            END SELECT
1291         ENDIF
1292      ENDIF
1293   END SUBROUTINE iom_rp1d
1294
1295   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1296      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1297      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1298      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1299      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1300      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1301      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1302      INTEGER :: ivid   ! variable id
1303      IF( kiomid > 0 ) THEN
1304         IF( iom_file(kiomid)%nfid > 0 ) THEN
1305            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1306            SELECT CASE (iom_file(kiomid)%iolib)
1307            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1308            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1309            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
1310            CASE DEFAULT     
1311               CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1312            END SELECT
1313         ENDIF
1314      ENDIF
1315   END SUBROUTINE iom_rp2d
1316
1317   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1318      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1319      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1320      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1321      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1322      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1323      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1324      INTEGER :: ivid   ! variable id
1325      IF( kiomid > 0 ) THEN
1326         IF( iom_file(kiomid)%nfid > 0 ) THEN
1327            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1328            SELECT CASE (iom_file(kiomid)%iolib)
1329            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1330            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1331            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
1332            CASE DEFAULT     
1333               CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
1334            END SELECT
1335         ENDIF
1336      ENDIF
1337   END SUBROUTINE iom_rp3d
1338
1339
1340   !!----------------------------------------------------------------------
1341   !!                   INTERFACE iom_put
1342   !!----------------------------------------------------------------------
1343   SUBROUTINE iom_p0d( cdname, pfield0d )
1344      CHARACTER(LEN=*), INTENT(in) ::   cdname
1345      REAL(wp)        , INTENT(in) ::   pfield0d
1346      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1347#if defined key_iomput
1348      zz(:,:)=pfield0d
1349      CALL xios_send_field(cdname, zz)
1350      !CALL xios_send_field(cdname, (/pfield0d/))
1351#else
1352      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1353#endif
1354   END SUBROUTINE iom_p0d
1355
1356   SUBROUTINE iom_p1d( cdname, pfield1d )
1357      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1358      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1359#if defined key_iomput
1360      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1361#else
1362      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1363#endif
1364   END SUBROUTINE iom_p1d
1365
1366   SUBROUTINE iom_p2d( cdname, pfield2d )
1367      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1368      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1369#if defined key_iomput
1370      CALL xios_send_field(cdname, pfield2d)
1371#else
1372      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1373#endif
1374   END SUBROUTINE iom_p2d
1375
1376   SUBROUTINE iom_p3d( cdname, pfield3d )
1377      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1378      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1379#if defined key_iomput
1380      CALL xios_send_field(cdname, pfield3d)
1381#else
1382      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1383#endif
1384   END SUBROUTINE iom_p3d
1385   !!----------------------------------------------------------------------
1386
1387#if defined key_iomput
1388
1389   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1390      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1391      &                                    nvertex, bounds_lon, bounds_lat, area )
1392      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1393      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1394      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1395      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1396      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1397      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1398#if ! defined key_xios2
1399     LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1400#else
1401      LOGICAL,  DIMENSION(:) , OPTIONAL, INTENT(in) ::   mask
1402#endif
1403
1404#if ! defined key_xios2
1405      IF ( xios_is_valid_domain     (cdid) ) THEN
1406         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1407            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1408            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1409            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1410            &    bounds_lat=bounds_lat, area=area )
1411     ENDIF
1412      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1413         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1414            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1415            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1416            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1417            &    bounds_lat=bounds_lat, area=area )
1418      ENDIF
1419
1420#else
1421      IF ( xios_is_valid_domain     (cdid) ) THEN
1422         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1423            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1424            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1425            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1426      ENDIF
1427      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1428         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1429            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1430            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1431            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1432      ENDIF
1433#endif
1434      CALL xios_solve_inheritance()
1435
1436   END SUBROUTINE iom_set_domain_attr
1437
1438#if defined key_xios2
1439  SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj)
1440     CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1441     INTEGER                  , OPTIONAL, INTENT(in) ::   ibegin, jbegin, ni, nj
1442
1443     IF ( xios_is_valid_zoom_domain     (cdid) ) THEN
1444         CALL xios_set_zoom_domain_attr     ( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni,    &
1445           &   nj=nj)
1446    ENDIF
1447  END SUBROUTINE iom_set_zoom_domain_attr
1448#endif
1449
1450
1451   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1452      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1453      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1454      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1455      IF ( PRESENT(paxis) ) THEN
1456#if ! defined key_xios2
1457         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1458         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1459#else
1460         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1461         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1462#endif
1463      ENDIF
1464      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1465      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1466      CALL xios_solve_inheritance()
1467   END SUBROUTINE iom_set_axis_attr
1468
1469   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1470      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1471#if ! defined key_xios2
1472      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_op
1473      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_offset
1474#else
1475      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_op
1476      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_offset
1477#endif
1478      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1479    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1480      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1481    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1482      CALL xios_solve_inheritance()
1483   END SUBROUTINE iom_set_field_attr
1484
1485   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1486      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1487      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1488      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1489      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1490      CALL xios_solve_inheritance()
1491   END SUBROUTINE iom_set_file_attr
1492
1493
1494   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1495      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1496      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1497#if ! defined key_xios2
1498      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::    output_freq
1499#else
1500      TYPE(xios_duration)   ,OPTIONAL , INTENT(out) :: output_freq
1501#endif 
1502      LOGICAL                                 ::   llexist1,llexist2,llexist3
1503      !---------------------------------------------------------------------
1504      IF( PRESENT( name        ) )   name = ''          ! default values
1505      IF( PRESENT( name_suffix ) )   name_suffix = ''
1506#if ! defined key_xios2
1507      IF( PRESENT( output_freq ) )   output_freq = ''
1508#else
1509      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1510#endif
1511      IF ( xios_is_valid_file     (cdid) ) THEN
1512         CALL xios_solve_inheritance()
1513         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1514         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1515         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1516         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1517      ENDIF
1518      IF ( xios_is_valid_filegroup(cdid) ) THEN
1519         CALL xios_solve_inheritance()
1520         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1521         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1522         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1523         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1524      ENDIF
1525   END SUBROUTINE iom_get_file_attr
1526
1527
1528   SUBROUTINE iom_set_grid_attr( cdid, mask )
1529      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1530      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1531#if ! defined key_xios2
1532      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1533      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1534#else
1535      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1536      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1537#endif
1538      CALL xios_solve_inheritance()
1539   END SUBROUTINE iom_set_grid_attr
1540
1541   SUBROUTINE iom_setkt( kt, cdname )
1542      INTEGER         , INTENT(in) ::   kt 
1543      CHARACTER(LEN=*), INTENT(in) ::   cdname
1544      !     
1545      CALL iom_swap( cdname )   ! swap to cdname context
1546      CALL xios_update_calendar(kt)
1547      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1548      !
1549   END SUBROUTINE iom_setkt
1550
1551   SUBROUTINE iom_context_finalize( cdname )
1552      CHARACTER(LEN=*), INTENT(in) :: cdname
1553      !
1554      IF( xios_is_valid_context(cdname) ) THEN
1555         CALL iom_swap( cdname )   ! swap to cdname context
1556         CALL xios_context_finalize() ! finalize the context
1557         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1558      ENDIF
1559      !
1560   END SUBROUTINE iom_context_finalize
1561
1562
1563   SUBROUTINE set_grid( cdgrd, plon, plat, lmask )
1564      !!----------------------------------------------------------------------
1565      !!                     ***  ROUTINE set_grid  ***
1566      !!
1567      !! ** Purpose :   define horizontal grids
1568      !!
1569      !!----------------------------------------------------------------------
1570      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1571      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1572      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1573      !
1574      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1575      INTEGER  :: ni,nj
1576      LOGICAL :: lmask
1577     
1578      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1579
1580#if ! defined key_xios2
1581     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)
1582#else
1583     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)
1584#endif     
1585      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1586      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1587         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1588
1589      IF ( lmask ) THEN
1590         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1591         SELECT CASE ( cdgrd )
1592         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1593         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1594         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1595         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1596         END SELECT
1597         !
1598#if ! defined key_xios2
1599         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1600#else
1601         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
1602#endif 
1603         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1604      ENDIF
1605     
1606   END SUBROUTINE set_grid
1607
1608
1609   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1610      !!----------------------------------------------------------------------
1611      !!                   ***  ROUTINE set_grid_bounds  ***
1612      !!
1613      !! ** Purpose :   define horizontal grid corners
1614      !!
1615      !!----------------------------------------------------------------------
1616      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1617      !
1618      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1619      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1620      !
1621      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1622      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1623      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1624      !
1625      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1626      !                                                          ! represents the bottom-left corner of cell (i,j)
1627      INTEGER :: ji, jj, jn, ni, nj
1628
1629      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1630
1631      ! Offset of coordinate representing bottom-left corner
1632      SELECT CASE ( TRIM(cdgrd) )
1633         CASE ('T', 'W', 'N')
1634            icnr = -1 ; jcnr = -1
1635         CASE ('U')
1636            icnr =  0 ; jcnr = -1
1637         CASE ('V')
1638            icnr = -1 ; jcnr =  0
1639      END SELECT
1640
1641      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1642
1643      z_fld(:,:) = 1._wp
1644      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1645
1646      ! Cell vertices that can be defined
1647      DO jj = 2, jpjm1
1648         DO ji = 2, jpim1
1649            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1650            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1651            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1652            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1653            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1654            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1655            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1656            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1657         END DO
1658      END DO
1659
1660      ! Cell vertices on boundries
1661      DO jn = 1, 4
1662         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1663         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1664      END DO
1665
1666      ! Zero-size cells at closed boundaries if cell points provided,
1667      ! otherwise they are closed cells with unrealistic bounds
1668      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1669         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1670            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1671               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1672            END DO
1673         ENDIF
1674         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1675            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1676               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1677            END DO
1678         ENDIF
1679         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1680            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1681               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1682            END DO
1683         ENDIF
1684         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1685            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1686               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1687            END DO
1688         ENDIF
1689      ENDIF
1690
1691      ! Rotate cells at the north fold
1692      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1693         DO jj = 1, jpj
1694            DO ji = 1, jpi
1695               IF( z_fld(ji,jj) == -1. ) THEN
1696                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1697                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1698                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1699               ENDIF
1700            END DO
1701         END DO
1702
1703      ! Invert cells at the symmetric equator
1704      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1705         DO ji = 1, jpi
1706            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1707            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1708            z_bnds(:,ji,1,:) = z_rot(:,:)
1709         END DO
1710      ENDIF
1711
1712      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1713                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1714
1715      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1716
1717   END SUBROUTINE set_grid_bounds
1718
1719
1720   SUBROUTINE set_grid_znl( plat )
1721      !!----------------------------------------------------------------------
1722      !!                     ***  ROUTINE set_grid_znl  ***
1723      !!
1724      !! ** Purpose :   define grids for zonal mean
1725      !!
1726      !!----------------------------------------------------------------------
1727      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1728      !
1729      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
1730      INTEGER  :: ni,nj, ix, iy
1731
1732     
1733      ni=nlei-nldi+1 ; nj=nlej-nldj+1            ! define zonal mean domain (jpj*jpk)
1734      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0.
1735
1736      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1737#if ! defined key_xios2
1738      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1739      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1740      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1741         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1742      !
1743      CALL iom_set_domain_attr ('ptr', zoom_ibegin=ix, zoom_nj=jpjglo)
1744#else
1745! Pas teste : attention aux indices !
1746      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
1747      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1748      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1749         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1750       CALL iom_set_zoom_domain_attr ("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
1751#endif
1752
1753      CALL iom_update_file_name('ptr')
1754      !
1755   END SUBROUTINE set_grid_znl
1756
1757   SUBROUTINE set_scalar
1758      !!----------------------------------------------------------------------
1759      !!                     ***  ROUTINE set_scalar  ***
1760      !!
1761      !! ** Purpose :   define fake grids for scalar point
1762      !!
1763      !!----------------------------------------------------------------------
1764      REAL(wp), DIMENSION(1)   ::   zz = 1.
1765      !!----------------------------------------------------------------------
1766#if ! defined key_xios2
1767      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1768#else
1769      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
1770#endif
1771      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1772     
1773      zz=REAL(narea,wp)
1774      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1775     
1776   END SUBROUTINE set_scalar
1777
1778   SUBROUTINE set_1point
1779      !!----------------------------------------------------------------------
1780      !!                     ***  ROUTINE set_1point  ***
1781      !!
1782      !! ** Purpose :   define zoom grid for scalar fields
1783      !!
1784      !!----------------------------------------------------------------------
1785      REAL(wp), DIMENSION(1)   ::   zz = 1.
1786      INTEGER  :: ix, iy
1787      !!----------------------------------------------------------------------
1788      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  Nearest point to north pole should be ocean
1789      CALL iom_set_domain_attr('1point', zoom_ibegin=ix, zoom_jbegin=iy)
1790
1791   END SUBROUTINE set_1point
1792
1793
1794
1795   SUBROUTINE set_xmlatt
1796      !!----------------------------------------------------------------------
1797      !!                     ***  ROUTINE set_xmlatt  ***
1798      !!
1799      !! ** Purpose :   automatic definitions of some of the xml attributs...
1800      !!
1801      !!----------------------------------------------------------------------
1802      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1803      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1804      CHARACTER(len=1)               ::   cl1                      ! 1 character
1805      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1806      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1807      INTEGER                        ::   ji, jg                   ! loop counters
1808      INTEGER                        ::   ix, iy                   ! i-,j- index
1809      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1810      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1811      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1812      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1813      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1814      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1815#if  defined key_xios2
1816      TYPE(xios_duration)            ::   f_op, f_of
1817#endif
1818 
1819      !!----------------------------------------------------------------------
1820      !
1821      ! frequency of the call of iom_put (attribut: freq_op)
1822#if ! defined key_xios2
1823      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op=cl1//'ts', freq_offset='0ts')
1824      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op=cl1//'ts', freq_offset='0ts')
1825      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op=cl1//'ts', freq_offset='0ts')
1826      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op=cl1//'ts', freq_offset='0ts')
1827      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op=cl1//'ts', freq_offset='0ts')
1828#else
1829      f_op%timestep = 1        ;  f_of%timestep = 0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
1830      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
1831      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
1832      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('ptrc_T'          , freq_op=f_op, freq_offset=f_of)
1833      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('diad_T'          , freq_op=f_op, freq_offset=f_of)
1834#endif
1835       
1836      ! output file names (attribut: name)
1837      DO ji = 1, 9
1838         WRITE(cl1,'(i1)') ji 
1839         CALL iom_update_file_name('file'//cl1)
1840      END DO
1841      DO ji = 1, 99
1842         WRITE(cl2,'(i2.2)') ji 
1843         CALL iom_update_file_name('file'//cl2)
1844      END DO
1845      DO ji = 1, 999
1846         WRITE(cl3,'(i3.3)') ji 
1847         CALL iom_update_file_name('file'//cl3)
1848      END DO
1849
1850      ! Zooms...
1851      clgrd = (/ 'T', 'U', 'W' /) 
1852      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1853         cl1 = clgrd(jg)
1854         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1855         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1856#if ! defined key_xios2
1857         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1858#else
1859         CALL iom_set_zoom_domain_attr ('Eq'//cl1, jbegin=iy-1, ni=jpiglo)
1860#endif
1861         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1862         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1863         CALL iom_update_file_name('Eq'//cl1)
1864      END DO
1865      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1866      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1867      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1868      CALL set_mooring( zlontao, zlattao )
1869      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1870      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1871      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1872      CALL set_mooring( zlonrama, zlatrama )
1873      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1874      zlonpira = (/ -38.0, -23.0, -10.0 /)
1875      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1876      CALL set_mooring( zlonpira, zlatpira )
1877
1878     
1879   END SUBROUTINE set_xmlatt
1880
1881
1882   SUBROUTINE set_mooring( plon, plat)
1883      !!----------------------------------------------------------------------
1884      !!                     ***  ROUTINE set_mooring  ***
1885      !!
1886      !! ** Purpose :   automatic definitions of moorings xml attributs...
1887      !!
1888      !!----------------------------------------------------------------------
1889      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1890      !
1891!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1892      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1893      CHARACTER(len=256)            ::   clname                   ! file name
1894      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1895      CHARACTER(len=1)              ::   cl1                      ! 1 character
1896      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1897      INTEGER                       ::   ji, jj, jg               ! loop counters
1898      INTEGER                       ::   ix, iy                   ! i-,j- index
1899      REAL(wp)                      ::   zlon, zlat
1900      !!----------------------------------------------------------------------
1901      DO jg = 1, SIZE(clgrd)
1902         cl1 = clgrd(jg)
1903         DO ji = 1, SIZE(plon)
1904            DO jj = 1, SIZE(plat)
1905               zlon = plon(ji)
1906               zlat = plat(jj)
1907               ! modifications for RAMA moorings
1908               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1909               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1910               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1911               ! modifications for PIRATA moorings
1912               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1913               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1914               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1915               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1916               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1917               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1918               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1919               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1920               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1921               IF( zlon >= 0. ) THEN 
1922                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1923                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1924                  ENDIF
1925               ELSE             
1926                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1927                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1928                  ENDIF
1929               ENDIF
1930               IF( zlat >= 0. ) THEN 
1931                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1932                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1933                  ENDIF
1934               ELSE             
1935                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1936                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1937                  ENDIF
1938               ENDIF
1939               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1940#if ! defined key_xios2
1941               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1942#else
1943               CALL iom_set_zoom_domain_attr  (TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1)
1944#endif
1945               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1946               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1947               CALL iom_update_file_name(TRIM(clname)//cl1)
1948            END DO
1949         END DO
1950      END DO
1951     
1952   END SUBROUTINE set_mooring
1953
1954   
1955   SUBROUTINE iom_update_file_name( cdid )
1956      !!----------------------------------------------------------------------
1957      !!                     ***  ROUTINE iom_update_file_name  ***
1958      !!
1959      !! ** Purpose :   
1960      !!
1961      !!----------------------------------------------------------------------
1962      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1963      !
1964      CHARACTER(LEN=256) ::   clname
1965      CHARACTER(LEN=20)  ::   clfreq
1966      CHARACTER(LEN=20)  ::   cldate
1967      INTEGER            ::   idx
1968      INTEGER            ::   jn
1969      INTEGER            ::   itrlen
1970      INTEGER            ::   iyear, imonth, iday, isec
1971      REAL(wp)           ::   zsec
1972      LOGICAL            ::   llexist
1973#if  defined key_xios2
1974      TYPE(xios_duration)   ::   output_freq 
1975#endif     
1976      !!----------------------------------------------------------------------
1977
1978
1979      DO jn = 1,2
1980#if ! defined key_xios2
1981         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1982#else
1983         output_freq = xios_duration(0,0,0,0,0,0)
1984         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
1985#endif
1986         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1987
1988         IF ( TRIM(clname) /= '' ) THEN
1989
1990            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1991            DO WHILE ( idx /= 0 ) 
1992               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1993               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1994            END DO
1995
1996#if ! defined key_xios2
1997            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1998            DO WHILE ( idx /= 0 ) 
1999               IF ( TRIM(clfreq) /= '' ) THEN
2000                  itrlen = LEN_TRIM(clfreq)
2001                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
2002                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
2003               ELSE
2004                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2005                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
2006               ENDIF
2007               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2008            END DO
2009#else
2010            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2011            DO WHILE ( idx /= 0 ) 
2012              IF ( output_freq%timestep /= 0) THEN
2013                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
2014                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2015              ELSE IF ( output_freq%hour /= 0 ) THEN
2016                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
2017                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2018              ELSE IF ( output_freq%day /= 0 ) THEN
2019                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
2020                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2021              ELSE IF ( output_freq%month /= 0 ) THEN   
2022                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
2023                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2024              ELSE IF ( output_freq%year /= 0 ) THEN   
2025                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
2026                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2027              ELSE
2028                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2029                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
2030              ENDIF
2031              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
2032              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2033            END DO
2034#endif
2035            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2036            DO WHILE ( idx /= 0 ) 
2037               cldate = iom_sdate( fjulday - rdttra(1) / rday )
2038               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
2039               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2040            END DO
2041
2042            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2043            DO WHILE ( idx /= 0 ) 
2044               cldate = iom_sdate( fjulday - rdttra(1) / rday, ldfull = .TRUE. )
2045               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
2046               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2047            END DO
2048
2049            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2050            DO WHILE ( idx /= 0 ) 
2051               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
2052               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
2053               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2054            END DO
2055
2056            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2057            DO WHILE ( idx /= 0 ) 
2058               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
2059               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
2060               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2061            END DO
2062
2063            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
2064            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
2065            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
2066
2067         ENDIF
2068
2069      END DO
2070
2071   END SUBROUTINE iom_update_file_name
2072
2073
2074   FUNCTION iom_sdate( pjday, ld24, ldfull )
2075      !!----------------------------------------------------------------------
2076      !!                     ***  ROUTINE iom_sdate  ***
2077      !!
2078      !! ** Purpose :   send back the date corresponding to the given julian day
2079      !!
2080      !!----------------------------------------------------------------------
2081      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
2082      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
2083      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
2084      !
2085      CHARACTER(LEN=20) ::   iom_sdate
2086      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
2087      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
2088      REAL(wp)          ::   zsec
2089      LOGICAL           ::   ll24, llfull
2090      !
2091      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
2092      ELSE                       ;   ll24 = .FALSE.
2093      ENDIF
2094
2095      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
2096      ELSE                         ;   llfull = .FALSE.
2097      ENDIF
2098
2099      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
2100      isec = NINT(zsec)
2101
2102      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
2103         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
2104         isec = 86400
2105      ENDIF
2106
2107      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
2108      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
2109      ENDIF
2110     
2111!$AGRIF_DO_NOT_TREAT     
2112! Should be fixed in the conv
2113      IF( llfull ) THEN
2114         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
2115         ihour   = isec / 3600
2116         isec    = MOD(isec, 3600)
2117         iminute = isec / 60
2118         isec    = MOD(isec, 60)
2119         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
2120      ELSE
2121         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
2122      ENDIF
2123!$AGRIF_END_DO_NOT_TREAT     
2124
2125   END FUNCTION iom_sdate
2126
2127#else
2128
2129
2130   SUBROUTINE iom_setkt( kt, cdname )
2131      INTEGER         , INTENT(in)::   kt 
2132      CHARACTER(LEN=*), INTENT(in) ::   cdname
2133      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
2134   END SUBROUTINE iom_setkt
2135
2136   SUBROUTINE iom_context_finalize( cdname )
2137      CHARACTER(LEN=*), INTENT(in) ::   cdname
2138      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
2139   END SUBROUTINE iom_context_finalize
2140
2141#endif
2142
2143   LOGICAL FUNCTION iom_use( cdname )
2144      CHARACTER(LEN=*), INTENT(in) ::   cdname
2145#if defined key_iomput
2146      iom_use = xios_field_is_active( cdname )
2147#else
2148      iom_use = .FALSE.
2149#endif
2150   END FUNCTION iom_use
2151   
2152   !!======================================================================
2153END MODULE iom
Note: See TracBrowser for help on using the repository browser.