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/2016/dev_r6409_SIMPLIF_2_usrdef_tools/NEMOGCM/TOOLS/DOMAINcfg/src – NEMO

source: branches/2016/dev_r6409_SIMPLIF_2_usrdef_tools/NEMOGCM/TOOLS/DOMAINcfg/src/iom.F90 @ 6881

Last change on this file since 6881 was 6881, checked in by flavoni, 8 years ago

first commit to create in TOOLS domain_cfg.nc files

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