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

source: branches/NERC/dev_r6998_ORCHESTRA/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 7029

Last change on this file since 7029 was 7029, checked in by jamesharle, 8 years ago

Adding ORCHESTRA configuration
Merging with branches/2016/dev_r5549_BDY_ZEROGRAD
Merging with branches/2016/dev_r5840_BDY_MSK
Merging with branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP

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