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

source: branches/2014/dev_r5134_UKMO4_CF_compliance/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5342

Last change on this file since 5342 was 5342, checked in by hadcv, 9 years ago

Addition of cell bounds in output files and other changes.

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