New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.f90 in branches/UKMO/dev_r8864_restart_date/NEMOGCM/TOOLS/DOMAINcfg/src – NEMO

source: branches/UKMO/dev_r8864_restart_date/NEMOGCM/TOOLS/DOMAINcfg/src/iom.f90 @ 9235

Last change on this file since 9235 was 9235, checked in by davestorkey, 6 years ago

UKMO/dev_r8864_restart_date : clear SVN keywords.

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