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

source: branches/2013/dev_LOCEAN_2013/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 4205

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