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

source: branches/2015/dev_CMCC_merge_2015/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6051

Last change on this file since 6051 was 6051, checked in by lovato, 8 years ago

Merge branches/2015/dev_r5056_CMCC4_simplification (see ticket #1456)

  • Property svn:keywords set to Id
File size: 88.3 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      !---------------------------------------------------------------------
664      !
665      clname = iom_file(kiomid)%name   !   esier to read
666      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
667      ! local definition of the domain ?
668      idom = kdom
669      ! do we read the overlap
670      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
671      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
672      ! check kcount and kstart optionals parameters...
673      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
674      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
675      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown   ) CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown')
676
677      luse_jattr = .false.
678      IF( PRESENT(lrowattr) ) THEN
679         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
680         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
681      ENDIF
682      IF( luse_jattr ) THEN
683         SELECT CASE (iom_file(kiomid)%iolib)
684         CASE (jpnf90   )   
685             ! Ok
686         CASE DEFAULT   
687            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
688         END SELECT
689      ENDIF
690
691      ! Search for the variable in the data base (eventually actualize data)
692      istop = nstop
693      idvar = iom_varid( kiomid, cdvar )
694      !
695      IF( idvar > 0 ) THEN
696         ! to write iom_file(kiomid)%dimsz in a shorter way !
697         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
698         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
699         idmspc = inbdim                                   ! number of spatial dimensions in the file
700         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
701         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
702         !
703         ! update idom definition...
704         ! Identify the domain in case of jpdom_auto(glo/dta) definition
705         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
706            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
707            ELSE                               ;   idom = jpdom_data
708            ENDIF
709            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
710            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
711            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
712         ENDIF
713         ! Identify the domain in case of jpdom_local definition
714         IF( idom == jpdom_local ) THEN
715            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
716            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
717            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
718            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
719            ENDIF
720         ENDIF
721         !
722         ! check the consistency between input array and data rank in the file
723         !
724         ! initializations
725         itime = 1
726         IF( PRESENT(ktime) ) itime = ktime
727
728         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
729         WRITE(clrankpv, fmt='(i1)') irankpv
730         WRITE(cldmspc , fmt='(i1)') idmspc
731         !
732         IF(     idmspc <  irankpv ) THEN
733            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
734               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
735         ELSEIF( idmspc == irankpv ) THEN
736            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
737               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
738         ELSEIF( idmspc >  irankpv ) THEN
739               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
740                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
741                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
742                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
743                  idmspc = idmspc - 1
744               ELSE
745                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
746                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
747                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
748               ENDIF
749         ENDIF
750
751         !
752         ! definition of istart and icnt
753         !
754         icnt  (:) = 1
755         istart(:) = 1
756         istart(idmspc+1) = itime
757
758         IF(              PRESENT(kstart)       ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
759         ELSE
760            IF(           idom == jpdom_unknown ) THEN                                       ; icnt(1:idmspc) = idimsz(1:idmspc)
761            ELSE
762               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
763                  IF(     idom == jpdom_data    ) THEN
764                     jstartrow = 1
765                     IF( luse_jattr ) THEN
766                        CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
767                        jstartrow = MAX(1,jstartrow)
768                     ENDIF
769                     istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
770                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
771                  ENDIF
772                  ! we do not read the overlap                     -> we start to read at nldi, nldj
773! JMM + SM: ugly patch before getting the new version of lib_mpp)
774!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
775                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
776                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
777! JMM + SM: ugly patch before getting the new version of lib_mpp)
778!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
779                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
780                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
781                  ENDIF
782                  IF( PRESENT(pv_r3d) ) THEN
783                     IF( idom == jpdom_data ) THEN   ; icnt(3) = jpkdta
784                     ELSE                            ; icnt(3) = jpk
785                     ENDIF
786                  ENDIF
787               ENDIF
788            ENDIF
789         ENDIF
790
791         ! check that istart and icnt can be used with this file
792         !-
793         DO jl = 1, jpmax_dims
794            itmp = istart(jl)+icnt(jl)-1
795            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
796               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
797               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
798               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
799            ENDIF
800         END DO
801
802         ! check that icnt matches the input array
803         !-     
804         IF( idom == jpdom_unknown ) THEN
805            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
806            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
807            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
808            ctmp1 = 'd'
809         ELSE
810            IF( irankpv == 2 ) THEN
811! JMM + SM: ugly patch before getting the new version of lib_mpp)
812!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
813               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
814               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
815               ENDIF
816            ENDIF
817            IF( irankpv == 3 ) THEN 
818! JMM + SM: ugly patch before getting the new version of lib_mpp)
819!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
820               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
821               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
822               ENDIF
823            ENDIF
824         ENDIF
825         
826         DO jl = 1, irankpv
827            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
828            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
829         END DO
830
831      ENDIF
832
833      ! read the data
834      !-     
835      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
836         !
837         ! find the right index of the array to be read
838! JMM + SM: ugly patch before getting the new version of lib_mpp)
839!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
840!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
841!         ENDIF
842         IF( llnoov ) THEN
843            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
844            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
845            ENDIF
846         ELSE
847            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
848            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
849            ENDIF
850         ENDIF
851     
852         SELECT CASE (iom_file(kiomid)%iolib)
853         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
854            &                                         pv_r1d, pv_r2d, pv_r3d )
855         CASE DEFAULT
856            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
857         END SELECT
858
859         IF( istop == nstop ) THEN   ! no additional errors until this point...
860            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
861         
862            !--- overlap areas and extra hallows (mpp)
863            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
864               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
865            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
866               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
867               IF( icnt(3) == jpk ) THEN
868                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
869               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
870                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
871                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
872               ENDIF
873            ENDIF
874           
875            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
876            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
877            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
878   
879            !--- Apply scale_factor and offset
880            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
881            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
882            IF(     PRESENT(pv_r1d) ) THEN
883               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
884               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
885            ELSEIF( PRESENT(pv_r2d) ) THEN
886               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
887               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
888            ELSEIF( PRESENT(pv_r3d) ) THEN
889               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
890               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
891            ENDIF
892            !
893         ENDIF
894         !
895      ENDIF
896      !
897   END SUBROUTINE iom_get_123d
898
899
900   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
901      !!--------------------------------------------------------------------
902      !!                   ***  SUBROUTINE iom_gettime  ***
903      !!
904      !! ** Purpose : read the time axis cdvar in the file
905      !!--------------------------------------------------------------------
906      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
907      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
908      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
909      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
910      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
911      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
912      !
913      INTEGER, DIMENSION(1) :: kdimsz
914      INTEGER            ::   idvar    ! id of the variable
915      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
916      CHARACTER(LEN=100) ::   clinfo   ! info character
917      !---------------------------------------------------------------------
918      !
919      IF ( PRESENT(cdvar) ) THEN
920         tname = cdvar
921      ELSE
922         tname = iom_file(kiomid)%uldname
923      ENDIF
924      IF( kiomid > 0 ) THEN
925         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
926         IF ( PRESENT(kntime) ) THEN
927            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
928            kntime = kdimsz(1)
929         ELSE
930            idvar = iom_varid( kiomid, tname )
931         ENDIF
932         !
933         ptime(:) = 0. ! default definition
934         IF( idvar > 0 ) THEN
935            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
936               IF( iom_file(kiomid)%luld(idvar) ) THEN
937                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
938                     SELECT CASE (iom_file(kiomid)%iolib)
939                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
940                     CASE DEFAULT
941                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
942                     END SELECT
943                  ELSE
944                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
945                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
946                  ENDIF
947               ELSE
948                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
949               ENDIF
950            ELSE
951               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
952            ENDIF
953         ELSE
954            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
955         ENDIF
956      ENDIF
957      !
958   END SUBROUTINE iom_gettime
959
960
961   !!----------------------------------------------------------------------
962   !!                   INTERFACE iom_getatt
963   !!----------------------------------------------------------------------
964   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
965      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
966      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
967      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
968      !
969      IF( kiomid > 0 ) THEN
970         IF( iom_file(kiomid)%nfid > 0 ) THEN
971            SELECT CASE (iom_file(kiomid)%iolib)
972            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
973            CASE DEFAULT
974               CALL ctl_stop( 'iom_g0d_att: accepted IO library is only jpnf90' )
975            END SELECT
976         ENDIF
977      ENDIF
978   END SUBROUTINE iom_g0d_intatt
979
980
981   !!----------------------------------------------------------------------
982   !!                   INTERFACE iom_rstput
983   !!----------------------------------------------------------------------
984   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
985      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
986      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
987      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
988      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
989      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
990      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
991      INTEGER :: ivid   ! variable id
992      IF( kiomid > 0 ) THEN
993         IF( iom_file(kiomid)%nfid > 0 ) THEN
994            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
995            SELECT CASE (iom_file(kiomid)%iolib)
996            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
997            CASE DEFAULT
998               CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
999            END SELECT
1000         ENDIF
1001      ENDIF
1002   END SUBROUTINE iom_rp0d
1003
1004   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1005      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1006      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1007      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1008      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1009      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1010      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1011      INTEGER :: ivid   ! variable id
1012      IF( kiomid > 0 ) THEN
1013         IF( iom_file(kiomid)%nfid > 0 ) THEN
1014            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1015            SELECT CASE (iom_file(kiomid)%iolib)
1016            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1017            CASE DEFAULT
1018               CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1019            END SELECT
1020         ENDIF
1021      ENDIF
1022   END SUBROUTINE iom_rp1d
1023
1024   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1025      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1026      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1027      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1028      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1029      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1030      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1031      INTEGER :: ivid   ! variable id
1032      IF( kiomid > 0 ) THEN
1033         IF( iom_file(kiomid)%nfid > 0 ) THEN
1034            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1035            SELECT CASE (iom_file(kiomid)%iolib)
1036            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1037            CASE DEFAULT
1038               CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1039            END SELECT
1040         ENDIF
1041      ENDIF
1042   END SUBROUTINE iom_rp2d
1043
1044   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1045      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1046      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1047      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1048      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1049      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1050      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1051      INTEGER :: ivid   ! variable id
1052      IF( kiomid > 0 ) THEN
1053         IF( iom_file(kiomid)%nfid > 0 ) THEN
1054            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1055            SELECT CASE (iom_file(kiomid)%iolib)
1056            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1057            CASE DEFAULT
1058               CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1059            END SELECT
1060         ENDIF
1061      ENDIF
1062   END SUBROUTINE iom_rp3d
1063
1064
1065   !!----------------------------------------------------------------------
1066   !!                   INTERFACE iom_put
1067   !!----------------------------------------------------------------------
1068   SUBROUTINE iom_p0d( cdname, pfield0d )
1069      CHARACTER(LEN=*), INTENT(in) ::   cdname
1070      REAL(wp)        , INTENT(in) ::   pfield0d
1071      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1072#if defined key_iomput
1073      zz(:,:)=pfield0d
1074      CALL xios_send_field(cdname, zz)
1075      !CALL xios_send_field(cdname, (/pfield0d/))
1076#else
1077      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1078#endif
1079   END SUBROUTINE iom_p0d
1080
1081   SUBROUTINE iom_p1d( cdname, pfield1d )
1082      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1083      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1084#if defined key_iomput
1085      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1086#else
1087      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1088#endif
1089   END SUBROUTINE iom_p1d
1090
1091   SUBROUTINE iom_p2d( cdname, pfield2d )
1092      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1093      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1094#if defined key_iomput
1095      CALL xios_send_field(cdname, pfield2d)
1096#else
1097      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1098#endif
1099   END SUBROUTINE iom_p2d
1100
1101   SUBROUTINE iom_p3d( cdname, pfield3d )
1102      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1103      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1104#if defined key_iomput
1105      CALL xios_send_field(cdname, pfield3d)
1106#else
1107      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1108#endif
1109   END SUBROUTINE iom_p3d
1110   !!----------------------------------------------------------------------
1111
1112#if defined key_iomput
1113
1114   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1115      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1116      &                                    nvertex, bounds_lon, bounds_lat, area )
1117      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1118      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1119      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1120      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1121      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1122      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1123      LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1124
1125      IF ( xios_is_valid_domain     (cdid) ) THEN
1126         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1127            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1128            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1129            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1130            &    bounds_lat=bounds_lat, area=area )
1131      ENDIF
1132
1133      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1134         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1135            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1136            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1137            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1138            &    bounds_lat=bounds_lat, area=area )
1139      ENDIF
1140      CALL xios_solve_inheritance()
1141
1142   END SUBROUTINE iom_set_domain_attr
1143
1144
1145   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1146      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1147      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1148      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1149      IF ( PRESENT(paxis) ) THEN
1150         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1151         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1152      ENDIF
1153      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1154      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1155      CALL xios_solve_inheritance()
1156   END SUBROUTINE iom_set_axis_attr
1157
1158
1159   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1160      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1161      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_op
1162      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_offset
1163      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1164    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1165      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1166    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1167      CALL xios_solve_inheritance()
1168   END SUBROUTINE iom_set_field_attr
1169
1170
1171   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1172      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1173      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1174      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1175      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1176      CALL xios_solve_inheritance()
1177   END SUBROUTINE iom_set_file_attr
1178
1179
1180   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1181      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1182      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix, output_freq
1183      LOGICAL                                 ::   llexist1,llexist2,llexist3
1184      !---------------------------------------------------------------------
1185      IF( PRESENT( name        ) )   name = ''          ! default values
1186      IF( PRESENT( name_suffix ) )   name_suffix = ''
1187      IF( PRESENT( output_freq ) )   output_freq = ''
1188      IF ( xios_is_valid_file     (cdid) ) THEN
1189         CALL xios_solve_inheritance()
1190         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1191         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1192         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1193         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1194      ENDIF
1195      IF ( xios_is_valid_filegroup(cdid) ) THEN
1196         CALL xios_solve_inheritance()
1197         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1198         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1199         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1200         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1201      ENDIF
1202   END SUBROUTINE iom_get_file_attr
1203
1204
1205   SUBROUTINE iom_set_grid_attr( cdid, mask )
1206      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1207      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1208      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1209      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1210      CALL xios_solve_inheritance()
1211   END SUBROUTINE iom_set_grid_attr
1212
1213   SUBROUTINE iom_setkt( kt, cdname )
1214      INTEGER         , INTENT(in) ::   kt 
1215      CHARACTER(LEN=*), INTENT(in) ::   cdname
1216      !     
1217      CALL iom_swap( cdname )   ! swap to cdname context
1218      CALL xios_update_calendar(kt)
1219      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1220      !
1221   END SUBROUTINE iom_setkt
1222
1223   SUBROUTINE iom_context_finalize( cdname )
1224      CHARACTER(LEN=*), INTENT(in) :: cdname
1225      !
1226      IF( xios_is_valid_context(cdname) ) THEN
1227         CALL iom_swap( cdname )   ! swap to cdname context
1228         CALL xios_context_finalize() ! finalize the context
1229         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1230      ENDIF
1231      !
1232   END SUBROUTINE iom_context_finalize
1233
1234
1235   SUBROUTINE set_grid( cdgrd, plon, plat )
1236      !!----------------------------------------------------------------------
1237      !!                     ***  ROUTINE set_grid  ***
1238      !!
1239      !! ** Purpose :   define horizontal grids
1240      !!
1241      !!----------------------------------------------------------------------
1242      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1243      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1244      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1245      !
1246      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1247      INTEGER  :: ni,nj
1248     
1249      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1250
1251      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)
1252      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1253      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1254         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1255
1256      IF ( ln_mskland ) THEN
1257         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1258         SELECT CASE ( cdgrd )
1259         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1260         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1261         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1262         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1263         END SELECT
1264         !
1265         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1266         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1267      ENDIF
1268     
1269   END SUBROUTINE set_grid
1270
1271
1272   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1273      !!----------------------------------------------------------------------
1274      !!                   ***  ROUTINE set_grid_bounds  ***
1275      !!
1276      !! ** Purpose :   define horizontal grid corners
1277      !!
1278      !!----------------------------------------------------------------------
1279      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1280      !
1281      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1282      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1283      !
1284      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1285      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1286      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1287      !
1288      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1289      !                                                          ! represents the bottom-left corner of cell (i,j)
1290      INTEGER :: ji, jj, jn, ni, nj
1291
1292      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1293
1294      ! Offset of coordinate representing bottom-left corner
1295      SELECT CASE ( TRIM(cdgrd) )
1296         CASE ('T', 'W')
1297            icnr = -1 ; jcnr = -1
1298         CASE ('U')
1299            icnr =  0 ; jcnr = -1
1300         CASE ('V')
1301            icnr = -1 ; jcnr =  0
1302      END SELECT
1303
1304      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1305
1306      z_fld(:,:) = 1._wp
1307      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1308
1309      ! Cell vertices that can be defined
1310      DO jj = 2, jpjm1
1311         DO ji = 2, jpim1
1312            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1313            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1314            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1315            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1316            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1317            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1318            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1319            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1320         END DO
1321      END DO
1322
1323      ! Cell vertices on boundries
1324      DO jn = 1, 4
1325         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1326         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1327      END DO
1328
1329      ! Zero-size cells at closed boundaries if cell points provided,
1330      ! otherwise they are closed cells with unrealistic bounds
1331      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1332         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1333            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1334               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1335            END DO
1336         ENDIF
1337         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1338            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1339               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1340            END DO
1341         ENDIF
1342         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1343            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1344               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1345            END DO
1346         ENDIF
1347         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1348            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1349               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1350            END DO
1351         ENDIF
1352      ENDIF
1353
1354      ! Rotate cells at the north fold
1355      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1356         DO jj = 1, jpj
1357            DO ji = 1, jpi
1358               IF( z_fld(ji,jj) == -1. ) THEN
1359                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1360                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1361                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1362               ENDIF
1363            END DO
1364         END DO
1365
1366      ! Invert cells at the symmetric equator
1367      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1368         DO ji = 1, jpi
1369            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1370            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1371            z_bnds(:,ji,1,:) = z_rot(:,:)
1372         END DO
1373      ENDIF
1374
1375      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1376                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1377
1378      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1379
1380   END SUBROUTINE set_grid_bounds
1381
1382
1383   SUBROUTINE set_grid_znl( plat )
1384      !!----------------------------------------------------------------------
1385      !!                     ***  ROUTINE set_grid_znl  ***
1386      !!
1387      !! ** Purpose :   define grids for zonal mean
1388      !!
1389      !!----------------------------------------------------------------------
1390      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1391      !
1392      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
1393      INTEGER  :: ni,nj, ix, iy
1394
1395     
1396      ni=nlei-nldi+1 ; nj=nlej-nldj+1            ! define zonal mean domain (jpj*jpk)
1397      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0.
1398
1399      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1400      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1401      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1402         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1403      !
1404      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1405      CALL iom_set_domain_attr ('ptr', zoom_ibegin=ix, zoom_nj=jpjglo)
1406      CALL iom_update_file_name('ptr')
1407      !
1408   END SUBROUTINE set_grid_znl
1409
1410   SUBROUTINE set_scalar
1411      !!----------------------------------------------------------------------
1412      !!                     ***  ROUTINE set_scalar  ***
1413      !!
1414      !! ** Purpose :   define fake grids for scalar point
1415      !!
1416      !!----------------------------------------------------------------------
1417      REAL(wp), DIMENSION(1)   ::   zz = 1.
1418      !!----------------------------------------------------------------------
1419      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1420      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1421     
1422      zz=REAL(narea,wp)
1423      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1424
1425   END SUBROUTINE set_scalar
1426
1427
1428   SUBROUTINE set_xmlatt
1429      !!----------------------------------------------------------------------
1430      !!                     ***  ROUTINE set_xmlatt  ***
1431      !!
1432      !! ** Purpose :   automatic definitions of some of the xml attributs...
1433      !!
1434      !!----------------------------------------------------------------------
1435      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1436      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1437      CHARACTER(len=1)               ::   cl1                      ! 1 character
1438      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1439      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1440      INTEGER                        ::   ji, jg                   ! loop counters
1441      INTEGER                        ::   ix, iy                   ! i-,j- index
1442      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1443      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1444      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1445      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1446      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1447      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1448      !!----------------------------------------------------------------------
1449      !
1450      ! frequency of the call of iom_put (attribut: freq_op)
1451      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op = cl1//'ts', freq_offset='0ts')
1452      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op = cl1//'ts', freq_offset='0ts')
1453      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op = cl1//'ts', freq_offset='0ts')
1454      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1455      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1456       
1457      ! output file names (attribut: name)
1458      DO ji = 1, 9
1459         WRITE(cl1,'(i1)') ji 
1460         CALL iom_update_file_name('file'//cl1)
1461      END DO
1462      DO ji = 1, 99
1463         WRITE(cl2,'(i2.2)') ji 
1464         CALL iom_update_file_name('file'//cl2)
1465      END DO
1466      DO ji = 1, 999
1467         WRITE(cl3,'(i3.3)') ji 
1468         CALL iom_update_file_name('file'//cl3)
1469      END DO
1470
1471      ! Zooms...
1472      clgrd = (/ 'T', 'U', 'W' /) 
1473      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1474         cl1 = clgrd(jg)
1475         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1476         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1477         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1478         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1479         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1480         CALL iom_update_file_name('Eq'//cl1)
1481      END DO
1482      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1483      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1484      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1485      CALL set_mooring( zlontao, zlattao )
1486      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1487      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1488      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1489      CALL set_mooring( zlonrama, zlatrama )
1490      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1491      zlonpira = (/ -38.0, -23.0, -10.0 /)
1492      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1493      CALL set_mooring( zlonpira, zlatpira )
1494
1495     
1496   END SUBROUTINE set_xmlatt
1497
1498
1499   SUBROUTINE set_mooring( plon, plat)
1500      !!----------------------------------------------------------------------
1501      !!                     ***  ROUTINE set_mooring  ***
1502      !!
1503      !! ** Purpose :   automatic definitions of moorings xml attributs...
1504      !!
1505      !!----------------------------------------------------------------------
1506      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1507      !
1508!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1509      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1510      CHARACTER(len=256)            ::   clname                   ! file name
1511      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1512      CHARACTER(len=1)              ::   cl1                      ! 1 character
1513      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1514      INTEGER                       ::   ji, jj, jg               ! loop counters
1515      INTEGER                       ::   ix, iy                   ! i-,j- index
1516      REAL(wp)                      ::   zlon, zlat
1517      !!----------------------------------------------------------------------
1518      DO jg = 1, SIZE(clgrd)
1519         cl1 = clgrd(jg)
1520         DO ji = 1, SIZE(plon)
1521            DO jj = 1, SIZE(plat)
1522               zlon = plon(ji)
1523               zlat = plat(jj)
1524               ! modifications for RAMA moorings
1525               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1526               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1527               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1528               ! modifications for PIRATA moorings
1529               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1530               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1531               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1532               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1533               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1534               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1535               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1536               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1537               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1538               IF( zlon >= 0. ) THEN 
1539                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1540                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1541                  ENDIF
1542               ELSE             
1543                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1544                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1545                  ENDIF
1546               ENDIF
1547               IF( zlat >= 0. ) THEN 
1548                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1549                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1550                  ENDIF
1551               ELSE             
1552                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1553                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1554                  ENDIF
1555               ENDIF
1556               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1557               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1558               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1559               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1560               CALL iom_update_file_name(TRIM(clname)//cl1)
1561            END DO
1562         END DO
1563      END DO
1564     
1565   END SUBROUTINE set_mooring
1566
1567   
1568   SUBROUTINE iom_update_file_name( cdid )
1569      !!----------------------------------------------------------------------
1570      !!                     ***  ROUTINE iom_update_file_name  ***
1571      !!
1572      !! ** Purpose :   
1573      !!
1574      !!----------------------------------------------------------------------
1575      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1576      !
1577      CHARACTER(LEN=256) ::   clname
1578      CHARACTER(LEN=20)  ::   clfreq
1579      CHARACTER(LEN=20)  ::   cldate
1580      INTEGER            ::   idx
1581      INTEGER            ::   jn
1582      INTEGER            ::   itrlen
1583      INTEGER            ::   iyear, imonth, iday, isec
1584      REAL(wp)           ::   zsec
1585      LOGICAL            ::   llexist
1586      !!----------------------------------------------------------------------
1587
1588      DO jn = 1,2
1589
1590         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1591         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1592
1593         IF ( TRIM(clname) /= '' ) THEN
1594
1595            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1596            DO WHILE ( idx /= 0 ) 
1597               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1598               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1599            END DO
1600
1601            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1602            DO WHILE ( idx /= 0 ) 
1603               IF ( TRIM(clfreq) /= '' ) THEN
1604                  itrlen = LEN_TRIM(clfreq)
1605                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1606                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1607               ELSE
1608                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1609                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1610               ENDIF
1611               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1612            END DO
1613
1614            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1615            DO WHILE ( idx /= 0 ) 
1616               cldate = iom_sdate( fjulday - rdt / rday )
1617               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1618               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1619            END DO
1620
1621            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1622            DO WHILE ( idx /= 0 ) 
1623               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
1624               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1625               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1626            END DO
1627
1628            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1629            DO WHILE ( idx /= 0 ) 
1630               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1631               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1632               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1633            END DO
1634
1635            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1636            DO WHILE ( idx /= 0 ) 
1637               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1638               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1639               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1640            END DO
1641
1642            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
1643            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1644            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1645
1646         ENDIF
1647
1648      END DO
1649
1650   END SUBROUTINE iom_update_file_name
1651
1652
1653   FUNCTION iom_sdate( pjday, ld24, ldfull )
1654      !!----------------------------------------------------------------------
1655      !!                     ***  ROUTINE iom_sdate  ***
1656      !!
1657      !! ** Purpose :   send back the date corresponding to the given julian day
1658      !!
1659      !!----------------------------------------------------------------------
1660      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1661      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1662      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1663      !
1664      CHARACTER(LEN=20) ::   iom_sdate
1665      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1666      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1667      REAL(wp)          ::   zsec
1668      LOGICAL           ::   ll24, llfull
1669      !
1670      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1671      ELSE                       ;   ll24 = .FALSE.
1672      ENDIF
1673
1674      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1675      ELSE                         ;   llfull = .FALSE.
1676      ENDIF
1677
1678      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1679      isec = NINT(zsec)
1680
1681      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1682         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1683         isec = 86400
1684      ENDIF
1685
1686      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1687      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1688      ENDIF
1689     
1690!$AGRIF_DO_NOT_TREAT     
1691! Should be fixed in the conv
1692      IF( llfull ) THEN
1693         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1694         ihour   = isec / 3600
1695         isec    = MOD(isec, 3600)
1696         iminute = isec / 60
1697         isec    = MOD(isec, 60)
1698         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1699      ELSE
1700         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1701      ENDIF
1702!$AGRIF_END_DO_NOT_TREAT     
1703
1704   END FUNCTION iom_sdate
1705
1706#else
1707
1708
1709   SUBROUTINE iom_setkt( kt, cdname )
1710      INTEGER         , INTENT(in)::   kt 
1711      CHARACTER(LEN=*), INTENT(in) ::   cdname
1712      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1713   END SUBROUTINE iom_setkt
1714
1715   SUBROUTINE iom_context_finalize( cdname )
1716      CHARACTER(LEN=*), INTENT(in) ::   cdname
1717      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1718   END SUBROUTINE iom_context_finalize
1719
1720#endif
1721
1722   LOGICAL FUNCTION iom_use( cdname )
1723      CHARACTER(LEN=*), INTENT(in) ::   cdname
1724#if defined key_iomput
1725      iom_use = xios_field_is_active( cdname )
1726#else
1727      iom_use = .FALSE.
1728#endif
1729   END FUNCTION iom_use
1730   
1731   !!======================================================================
1732END MODULE iom
Note: See TracBrowser for help on using the repository browser.