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

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

Undo the change to scalar output made at [5145]; it does not work for some cases, including XIOS parallel write

  • Property svn:keywords set to Id
File size: 89.7 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      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1105#if defined key_iomput
1106     zz(:,:)=pfield0d
1107     CALL xios_send_field(cdname, zz)
1108      !CALL xios_send_field(cdname, (/pfield0d/))
1109#else
1110      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1111#endif
1112   END SUBROUTINE iom_p0d
1113
1114   SUBROUTINE iom_p1d( cdname, pfield1d )
1115      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1116      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1117#if defined key_iomput
1118      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1119#else
1120      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1121#endif
1122   END SUBROUTINE iom_p1d
1123
1124   SUBROUTINE iom_p2d( cdname, pfield2d )
1125      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1126      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1127#if defined key_iomput
1128      CALL xios_send_field(cdname, pfield2d)
1129#else
1130      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1131#endif
1132   END SUBROUTINE iom_p2d
1133
1134   SUBROUTINE iom_p3d( cdname, pfield3d )
1135      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1136      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1137#if defined key_iomput
1138      CALL xios_send_field(cdname, pfield3d)
1139#else
1140      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1141#endif
1142   END SUBROUTINE iom_p3d
1143   !!----------------------------------------------------------------------
1144
1145#if defined key_iomput
1146
1147   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1148      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1149      &                                    nvertex, bounds_lon, bounds_lat, area )
1150      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1151      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1152      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1153      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1154      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1155      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1156      LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1157
1158      IF ( xios_is_valid_domain     (cdid) ) THEN
1159         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1160            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1161            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1162            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1163            &    bounds_lat=bounds_lat, area=area )
1164      ENDIF
1165
1166      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1167         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1168            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1169            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1170            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1171            &    bounds_lat=bounds_lat, area=area )
1172      ENDIF
1173      CALL xios_solve_inheritance()
1174
1175   END SUBROUTINE iom_set_domain_attr
1176
1177
1178   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1179      CHARACTER(LEN=*)                  , INTENT(in) ::   cdid
1180      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1181      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1182      IF ( PRESENT(paxis) ) THEN
1183         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1184         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1185      ENDIF
1186      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1187      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1188      CALL xios_solve_inheritance()
1189   END SUBROUTINE iom_set_axis_attr
1190
1191
1192   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1193      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1194      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_op
1195      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_offset
1196      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1197      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
1198      CALL xios_solve_inheritance()
1199   END SUBROUTINE iom_set_field_attr
1200
1201
1202   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1203      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1204      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1205      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1206      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1207      CALL xios_solve_inheritance()
1208   END SUBROUTINE iom_set_file_attr
1209
1210
1211   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1212      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1213      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix, output_freq
1214      LOGICAL                                 ::   llexist1,llexist2,llexist3
1215      !---------------------------------------------------------------------
1216      IF( PRESENT( name        ) )   name = ''          ! default values
1217      IF( PRESENT( name_suffix ) )   name_suffix = ''
1218      IF( PRESENT( output_freq ) )   output_freq = ''
1219      IF ( xios_is_valid_file     (cdid) ) THEN
1220         CALL xios_solve_inheritance()
1221         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1222         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1223         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1224         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1225      ENDIF
1226      IF ( xios_is_valid_filegroup(cdid) ) THEN
1227         CALL xios_solve_inheritance()
1228         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1229         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1230         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1231         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1232      ENDIF
1233   END SUBROUTINE iom_get_file_attr
1234
1235
1236   SUBROUTINE iom_set_grid_attr( cdid, mask )
1237      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1238      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1239      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1240      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1241      CALL xios_solve_inheritance()
1242   END SUBROUTINE iom_set_grid_attr
1243
1244   SUBROUTINE iom_setkt( kt, cdname )
1245      INTEGER         , INTENT(in) ::   kt 
1246      CHARACTER(LEN=*), INTENT(in) ::   cdname
1247      !     
1248      CALL iom_swap( cdname )   ! swap to cdname context
1249      CALL xios_update_calendar(kt)
1250      IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1251      !
1252   END SUBROUTINE iom_setkt
1253
1254   SUBROUTINE iom_context_finalize( cdname )
1255      CHARACTER(LEN=*), INTENT(in) :: cdname
1256      !
1257      IF( xios_is_valid_context(cdname) ) THEN
1258         CALL iom_swap( cdname )   ! swap to cdname context
1259         CALL xios_context_finalize() ! finalize the context
1260         IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1261      ENDIF
1262      !
1263   END SUBROUTINE iom_context_finalize
1264
1265
1266   SUBROUTINE set_grid( cdgrd, plon, plat )
1267      !!----------------------------------------------------------------------
1268      !!                     ***  ROUTINE set_grid  ***
1269      !!
1270      !! ** Purpose :   define horizontal grids
1271      !!
1272      !!----------------------------------------------------------------------
1273      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1274      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1275      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1276      !
1277      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1278      INTEGER  :: ni,nj
1279     
1280      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1281
1282      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)
1283      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1284      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1285         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1286
1287      IF ( ln_mskland ) THEN
1288         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1289         SELECT CASE ( cdgrd )
1290         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1291         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1292         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1293         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1294         END SELECT
1295         !
1296         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1297         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1298      ENDIF
1299     
1300   END SUBROUTINE set_grid
1301
1302
1303   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1304      !!----------------------------------------------------------------------
1305      !!                   ***  ROUTINE set_grid_bounds  ***
1306      !!
1307      !! ** Purpose :   define horizontal grid corners
1308      !!
1309      !!----------------------------------------------------------------------
1310      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1311      !
1312      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1313      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1314      !
1315      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1316      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1317      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1318      !
1319      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1320      !                                                          ! represents the bottom-left corner of cell (i,j)
1321      INTEGER :: ji, jj, jn, ni, nj
1322
1323      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1324
1325      ! Offset of coordinate representing bottom-left corner
1326      SELECT CASE ( TRIM(cdgrd) )
1327         CASE ('T', 'W')
1328            icnr = -1 ; jcnr = -1
1329         CASE ('U')
1330            icnr =  0 ; jcnr = -1
1331         CASE ('V')
1332            icnr = -1 ; jcnr =  0
1333      END SELECT
1334
1335      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1336
1337      z_fld(:,:) = 1._wp
1338      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1339
1340      ! Cell vertices that can be defined
1341      DO jj = 2, jpjm1
1342         DO ji = 2, jpim1
1343            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1344            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1345            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1346            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1347            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1348            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1349            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1350            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1351         END DO
1352      END DO
1353
1354      ! Cell vertices on boundries
1355      DO jn = 1, 4
1356         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1357         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1358      END DO
1359
1360      ! Zero-size cells at closed boundaries if cell points provided,
1361      ! otherwise they are closed cells with unrealistic bounds
1362      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1363         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1364            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1365               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1366            END DO
1367         ENDIF
1368         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1369            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1370               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1371            END DO
1372         ENDIF
1373         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1374            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1375               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1376            END DO
1377         ENDIF
1378         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1379            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1380               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1381            END DO
1382         ENDIF
1383      ENDIF
1384
1385      ! Rotate cells at the north fold
1386      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1387         DO jj = 1, jpj
1388            DO ji = 1, jpi
1389               IF( z_fld(ji,jj) == -1. ) THEN
1390                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1391                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1392                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1393               ENDIF
1394            END DO
1395         END DO
1396
1397      ! Invert cells at the symmetric equator
1398      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1399         DO ji = 1, jpi
1400            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1401            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1402            z_bnds(:,ji,1,:) = z_rot(:,:)
1403         END DO
1404      ENDIF
1405
1406      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1407                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1408
1409      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1410
1411   END SUBROUTINE set_grid_bounds
1412
1413
1414   SUBROUTINE set_scalar
1415      !!----------------------------------------------------------------------
1416      !!                     ***  ROUTINE set_scalar  ***
1417      !!
1418      !! ** Purpose :   define fake grids for scalar point
1419      !!
1420      !!----------------------------------------------------------------------
1421      REAL(wp), DIMENSION(1)   ::   zz = 1.
1422      !!----------------------------------------------------------------------
1423      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1424      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1425     
1426      zz=REAL(narea,wp)
1427      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1428
1429   END SUBROUTINE set_scalar
1430
1431
1432   SUBROUTINE set_xmlatt
1433      !!----------------------------------------------------------------------
1434      !!                     ***  ROUTINE set_xmlatt  ***
1435      !!
1436      !! ** Purpose :   automatic definitions of some of the xml attributs...
1437      !!
1438      !!----------------------------------------------------------------------
1439      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1440      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1441      CHARACTER(len=1)               ::   cl1                      ! 1 character
1442      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1443      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1444      INTEGER                        ::   ji, jg                   ! loop counters
1445      INTEGER                        ::   ix, iy                   ! i-,j- index
1446      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1447      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1448      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1449      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1450      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1451      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1452      !!----------------------------------------------------------------------
1453      !
1454      ! frequency of the call of iom_put (attribut: freq_op)
1455      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op = cl1//'ts', freq_offset='0ts')
1456      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op = cl1//'ts', freq_offset='0ts')
1457      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op = cl1//'ts', freq_offset='0ts')
1458      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1459      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1460       
1461      ! output file names (attribut: name)
1462      DO ji = 1, 9
1463         WRITE(cl1,'(i1)') ji 
1464         CALL iom_update_file_name('file'//cl1)
1465      END DO
1466      DO ji = 1, 99
1467         WRITE(cl2,'(i2.2)') ji 
1468         CALL iom_update_file_name('file'//cl2)
1469      END DO
1470      DO ji = 1, 999
1471         WRITE(cl3,'(i3.3)') ji 
1472         CALL iom_update_file_name('file'//cl3)
1473      END DO
1474
1475      ! Zooms...
1476      clgrd = (/ 'T', 'U', 'W' /) 
1477      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1478         cl1 = clgrd(jg)
1479         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1480         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1481         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1482         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1483         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1484         CALL iom_update_file_name('Eq'//cl1)
1485      END DO
1486      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1487      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1488      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1489      CALL set_mooring( zlontao, zlattao )
1490      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1491      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1492      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1493      CALL set_mooring( zlonrama, zlatrama )
1494      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1495      zlonpira = (/ -38.0, -23.0, -10.0 /)
1496      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1497      CALL set_mooring( zlonpira, zlatpira )
1498
1499      ! diaptr : zonal mean
1500      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1501      CALL iom_set_domain_attr ('ptr', zoom_ibegin=ix, zoom_nj=jpjglo)
1502      CALL iom_update_file_name('ptr')
1503      !
1504     
1505   END SUBROUTINE set_xmlatt
1506
1507
1508   SUBROUTINE set_mooring( plon, plat)
1509      !!----------------------------------------------------------------------
1510      !!                     ***  ROUTINE set_mooring  ***
1511      !!
1512      !! ** Purpose :   automatic definitions of moorings xml attributs...
1513      !!
1514      !!----------------------------------------------------------------------
1515      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1516      !
1517!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1518      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1519      CHARACTER(len=256)            ::   clname                   ! file name
1520      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1521      CHARACTER(len=1)              ::   cl1                      ! 1 character
1522      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1523      INTEGER                       ::   ji, jj, jg               ! loop counters
1524      INTEGER                       ::   ix, iy                   ! i-,j- index
1525      REAL(wp)                      ::   zlon, zlat
1526      !!----------------------------------------------------------------------
1527      DO jg = 1, SIZE(clgrd)
1528         cl1 = clgrd(jg)
1529         DO ji = 1, SIZE(plon)
1530            DO jj = 1, SIZE(plat)
1531               zlon = plon(ji)
1532               zlat = plat(jj)
1533               ! modifications for RAMA moorings
1534               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1535               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1536               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1537               ! modifications for PIRATA moorings
1538               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1539               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1540               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1541               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1542               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1543               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1544               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1545               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1546               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1547               IF( zlon >= 0. ) THEN 
1548                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1549                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1550                  ENDIF
1551               ELSE             
1552                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1553                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1554                  ENDIF
1555               ENDIF
1556               IF( zlat >= 0. ) THEN 
1557                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1558                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1559                  ENDIF
1560               ELSE             
1561                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1562                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1563                  ENDIF
1564               ENDIF
1565               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1566               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1567               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1568               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1569               CALL iom_update_file_name(TRIM(clname)//cl1)
1570            END DO
1571         END DO
1572      END DO
1573     
1574   END SUBROUTINE set_mooring
1575
1576   
1577   SUBROUTINE iom_update_file_name( cdid )
1578      !!----------------------------------------------------------------------
1579      !!                     ***  ROUTINE iom_update_file_name  ***
1580      !!
1581      !! ** Purpose :   
1582      !!
1583      !!----------------------------------------------------------------------
1584      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1585      !
1586      CHARACTER(LEN=256) ::   clname
1587      CHARACTER(LEN=20)  ::   clfreq
1588      CHARACTER(LEN=20)  ::   cldate
1589      INTEGER            ::   idx
1590      INTEGER            ::   jn
1591      INTEGER            ::   itrlen
1592      INTEGER            ::   iyear, imonth, iday, isec
1593      REAL(wp)           ::   zsec
1594      LOGICAL            ::   llexist
1595      !!----------------------------------------------------------------------
1596
1597      DO jn = 1,2
1598
1599         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1600         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1601
1602         IF ( TRIM(clname) /= '' ) THEN
1603
1604            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1605            DO WHILE ( idx /= 0 ) 
1606               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1607               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1608            END DO
1609
1610            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1611            DO WHILE ( idx /= 0 ) 
1612               IF ( TRIM(clfreq) /= '' ) THEN
1613                  itrlen = LEN_TRIM(clfreq)
1614                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1615                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1616               ELSE
1617                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1618                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1619               ENDIF
1620               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1621            END DO
1622
1623            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1624            DO WHILE ( idx /= 0 ) 
1625               cldate = iom_sdate( fjulday - rdttra(1) / rday )
1626               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1627               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1628            END DO
1629
1630            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1631            DO WHILE ( idx /= 0 ) 
1632               cldate = iom_sdate( fjulday - rdttra(1) / rday, ldfull = .TRUE. )
1633               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1634               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1635            END DO
1636
1637            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1638            DO WHILE ( idx /= 0 ) 
1639               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1640               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1641               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1642            END DO
1643
1644            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1645            DO WHILE ( idx /= 0 ) 
1646               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1647               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1648               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1649            END DO
1650
1651            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1652            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1653
1654         ENDIF
1655
1656      END DO
1657
1658   END SUBROUTINE iom_update_file_name
1659
1660
1661   FUNCTION iom_sdate( pjday, ld24, ldfull )
1662      !!----------------------------------------------------------------------
1663      !!                     ***  ROUTINE iom_sdate  ***
1664      !!
1665      !! ** Purpose :   send back the date corresponding to the given julian day
1666      !!
1667      !!----------------------------------------------------------------------
1668      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1669      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1670      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1671      !
1672      CHARACTER(LEN=20) ::   iom_sdate
1673      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1674      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1675      REAL(wp)          ::   zsec
1676      LOGICAL           ::   ll24, llfull
1677      !
1678      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1679      ELSE                       ;   ll24 = .FALSE.
1680      ENDIF
1681
1682      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1683      ELSE                         ;   llfull = .FALSE.
1684      ENDIF
1685
1686      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1687      isec = NINT(zsec)
1688
1689      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1690         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1691         isec = 86400
1692      ENDIF
1693
1694      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1695      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1696      ENDIF
1697     
1698      IF( llfull ) THEN
1699         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1700         ihour   = isec / 3600
1701         isec    = MOD(isec, 3600)
1702         iminute = isec / 60
1703         isec    = MOD(isec, 60)
1704         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1705      ELSE
1706         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1707      ENDIF
1708
1709   END FUNCTION iom_sdate
1710
1711#else
1712
1713
1714   SUBROUTINE iom_setkt( kt, cdname )
1715      INTEGER         , INTENT(in)::   kt 
1716      CHARACTER(LEN=*), INTENT(in) ::   cdname
1717      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1718   END SUBROUTINE iom_setkt
1719
1720   SUBROUTINE iom_context_finalize( cdname )
1721      CHARACTER(LEN=*), INTENT(in) ::   cdname
1722      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1723   END SUBROUTINE iom_context_finalize
1724
1725#endif
1726
1727   LOGICAL FUNCTION iom_use( cdname )
1728      CHARACTER(LEN=*), INTENT(in) ::   cdname
1729#if defined key_iomput
1730      iom_use = xios_field_is_active( cdname )
1731#else
1732      iom_use = .FALSE.
1733#endif
1734   END FUNCTION iom_use
1735   
1736   !!======================================================================
1737END MODULE iom
Note: See TracBrowser for help on using the repository browser.