source: CONFIG/UNIFORM/v6_rc0/IPSLCM6/SOURCES/NEMO/iom.F90

Last change on this file was 2544, checked in by aclsce, 9 years ago

IPSLCM6_rc0 :

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