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

source: branches/NERC/dev_r5549_BDY_ZEROGRAD/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6808

Last change on this file since 6808 was 6808, checked in by jamesharle, 8 years ago

merge with trunk@6232 for consistency with SSB code

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