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

source: branches/UKMO/dev_r7750_GO6_package_oasis_timers/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8904

Last change on this file since 8904 was 8904, checked in by andmirek, 6 years ago

#1978 new timers

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