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

source: branches/2014/dev_r5134_UKMO4_CF_compliance/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5413

Last change on this file since 5413 was 5413, checked in by hadcv, 9 years ago

Make a correction to the scalar domain data attributes (no data was being output).

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