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/UKMO/dev_r5021_nn_etau_revision/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_r5021_nn_etau_revision/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5240

Last change on this file since 5240 was 5240, checked in by davestorkey, 9 years ago

Update UKMO nn_etau_revision branch with trunk changes to rev 5107.

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