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

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in branches/2016/dev_r6325_SIMPLIF_1/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/2016/dev_r6325_SIMPLIF_1/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6347

Last change on this file since 6347 was 6347, checked in by gm, 8 years ago

#1683: SIMPLIF-1 : Phase with the v3.6_Stable (DOC+ZDF+traqsr+lbedo)

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