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

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

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

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

Add fix for XIOS initialization in server mode for non-MPP NEMO (and include a related XIOS finalize fix for C1D).

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