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

source: branches/2015/dev_r5803_NOC_WAD/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6141

Last change on this file since 6141 was 6141, checked in by acc, 9 years ago

Branch 2015/dev_r5803_NOC_WAD. Merge in dev_merge_2015 changes up to 6136. Conflicts all resolved

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