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

source: branches/UKMO/icebergs_restart_single_file/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5879

Last change on this file since 5879 was 5879, checked in by davestorkey, 8 years ago

UKMO icebergs_single_restart_branch: Bug fixes. This version of the code now produces
bit-comparable results with old code if starting from multiple restarts or a single
restart file.

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