New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in branches/UKMO/dev_r5518_Surge_Modelling/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_r5518_Surge_Modelling/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90

Last change on this file was 15722, checked in by clne, 2 years ago

Ugrade to XIOS2, needed for porting to new HPC

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