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

source: trunk/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 4528

Last change on this file since 4528 was 4292, checked in by cetlod, 10 years ago

dev_MERGE_2013 : 1st step of the merge, see ticket #1185

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