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

source: branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 10207

Last change on this file since 10207 was 10207, checked in by cmao, 6 years ago

remove svn keyword

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