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.
Changeset 13016 for NEMO/branches/2020/dev_12905_xios_ancil/src/OCE/IOM/iom.F90 – NEMO

Ignore:
Timestamp:
2020-06-03T12:48:36+02:00 (4 years ago)
Author:
andmirek
Message:

Ticket #2475 implementation of new interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • NEMO/branches/2020/dev_12905_xios_ancil/src/OCE/IOM/iom.F90

    r12649 r13016  
    4646   USE lib_fortran  
    4747   USE diu_bulk, ONLY : ln_diurnal_only, ln_diurnal 
     48   USE netcdf 
     49   USE iom_nf90 
    4850 
    4951   IMPLICIT NONE 
     
    5860   PUBLIC iom_chkatt, iom_getatt, iom_putatt, iom_getszuld, iom_rstput, iom_delay_rst, iom_put 
    5961   PUBLIC iom_use, iom_context_finalize, iom_update_file_name, iom_miss_val 
     62   PUBLIC iom_dom_context 
    6063 
    6164   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d 
     
    6568   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 
    6669   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_sdate 
    67    PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active 
     70   PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active, iom_set_vars_active 
    6871# endif 
    6972   PUBLIC iom_set_rstw_var_active, iom_set_rstw_core, iom_set_rst_vars 
     
    306309 
    307310   END SUBROUTINE iom_init_closedef 
     311 
     312   SUBROUTINE iom_set_vars_active(cdfname, idnum) 
     313      !!--------------------------------------------------------------------- 
     314      !!                   ***  SUBROUTINE  iom_set_vars_active  *** 
     315      !! 
     316      !! ** Purpose :  define filename in XIOS context for reading file, 
     317      !!               enable variables present in restart file for reading with XIOS  
     318      !!               id of a file is assumed to be rrestart. 
     319      !!--------------------------------------------------------------------- 
     320 
     321!sets enabled = .TRUE. for each field in restart file 
     322   CHARACTER(len=*), INTENT(IN) :: cdfname 
     323   INTEGER         , INTENT(IN) :: idnum  
     324#if defined key_iomput 
     325   INTEGER                                    :: ndims, nvars, natts, unlimitedDimId, dimlen, xtype,mdims 
     326   TYPE(xios_field)                           :: field_hdl 
     327   TYPE(xios_file)                            :: file_hdl 
     328   TYPE(xios_filegroup)                       :: filegroup_hdl 
     329   INTEGER                                    :: dimids(4), jv,i, idim 
     330   CHARACTER(LEN=256)                         :: clinfo               ! info character 
     331   INTEGER, ALLOCATABLE                       :: indimlens(:) 
     332   CHARACTER(LEN=nf90_max_name), ALLOCATABLE  :: indimnames(:) 
     333   CHARACTER(LEN=nf90_max_name)               :: dimname, varname 
     334   LOGICAL                                    :: lmeta 
     335 
     336        meta(1) = "nav_lat" 
     337        meta(2) = "nav_lon" 
     338        meta(3) = "nav_lev" 
     339        meta(4) = "time_instant" 
     340        meta(5) = "time_instant_bounds" 
     341        meta(6) = "time_counter" 
     342        meta(7) = "time_counter_bounds" 
     343        meta(8) = "x" 
     344        meta(9) = "y" 
     345        meta(10) = "numcat" 
     346 
     347        clinfo = '          iom_set_vars_active, file: '//TRIM(cdfname) 
     348 
     349        IF(lwp) write(numout, *) TRIM(clinfo) 
     350        IF(lwp) CALL FLUSH(numout) 
     351!set name of the restart file and enable available fields 
     352        CALL xios_get_handle("file_definition", filegroup_hdl ) 
     353        CALL xios_add_child(filegroup_hdl, file_hdl, 'rrestart') 
     354        CALL xios_set_file_attr( "rrestart", name=trim(cdfname), type="one_file", & 
     355             par_access="collective", enabled=.TRUE., mode="read",                 & 
     356             output_freq=xios_timestep) 
     357 
     358        CALL iom_nf90_check( nf90_inquire(idnum, ndims, nvars, natts ), clinfo ) 
     359        ALLOCATE(indimlens(ndims), indimnames(ndims)) 
     360        CALL iom_nf90_check( nf90_inquire(idnum, unlimitedDimId = unlimitedDimId ), clinfo ) 
     361 
     362        DO idim = 1, ndims 
     363           CALL iom_nf90_check( nf90_inquire_dimension(idnum, idim, dimname, dimlen ), clinfo ) 
     364           indimlens(idim) = dimlen 
     365           indimnames(idim) = dimname 
     366        ENDDO 
     367 
     368        DO jv =1, nvars 
     369            lmeta = .FALSE. 
     370            CALL iom_nf90_check( nf90_inquire_variable(idnum, jv, varname, xtype, ndims, dimids, natts ), clinfo ) 
     371            DO i = 1, NMETA 
     372               IF(TRIM(varname) == TRIM(meta(i))) THEN 
     373                  lmeta = .TRUE. 
     374               ENDIF 
     375            ENDDO 
     376            IF(.NOT.lmeta) THEN 
     377               CALL xios_add_child(file_hdl, field_hdl, TRIM(varname)) 
     378               mdims = ndims 
     379 
     380               IF(ANY(dimids(1:ndims) == unlimitedDimId)) THEN 
     381                mdims = mdims - 1 
     382               ENDIF 
     383 
     384               IF(mdims == 3) THEN 
     385                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname),   & 
     386                                   domain_ref="grid_N", axis_ref=TRIM(iom_axis(indimlens(dimids(mdims)))), & 
     387                                   prec = 8, operation = "instant") 
     388               ELSEIF(mdims == 2) THEN 
     389                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), & 
     390                                          domain_ref="grid_N", prec = 8, operation = "instant")  
     391               ELSEIF(mdims == 1) THEN 
     392                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), & 
     393                                          axis_ref=TRIM(iom_axis(indimlens(dimids(mdims)))), prec = 8, operation = "instant") 
     394               ELSEIF(mdims == 0) THEN 
     395                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), & 
     396                                                scalar_ref = "grid_scalar", prec = 8, operation = "instant") 
     397               ELSE 
     398                      WRITE(ctmp1,*) 'iom_set_vars_active: variable ', TRIM(varname) ,' incorrect number of dimensions'  
     399                      CALL ctl_stop( 'iom_set_vars_active:', ctmp1 ) 
     400               ENDIF 
     401            ENDIF 
     402        ENDDO 
     403        DEALLOCATE(indimlens, indimnames) 
     404#endif 
     405   END SUBROUTINE iom_set_vars_active 
     406 
     407   FUNCTION iom_axis(idlev) result(axis_ref) 
     408     !!--------------------------------------------------------------------- 
     409      !!                   ***  FUNCTION  iom_axis  *** 
     410      !! 
     411      !! ** Purpose : Used for grid definition when XIOS is used to read/write 
     412      !!              restart or configuration data. Returns axis corresponding  
     413      !!              to the number of levels given as an input variable. Axes  
     414      !!              are defined in routine iom_set_rst_context or iom_dom_context 
     415      !!              depending on context 
     416      !!--------------------------------------------------------------------- 
     417    INTEGER, INTENT(IN) :: idlev 
     418    CHARACTER(len=lc)   :: axis_ref 
     419    CHARACTER(len=12)   :: str 
     420       IF(idlev == jpk) THEN 
     421         axis_ref="nav_lev" 
     422#if defined key_si3 
     423       ELSEIF(idlev == jpl) THEN 
     424         axis_ref="numcat" 
     425#endif          
     426       ELSE 
     427         write(str, *) idlev 
     428         CALL ctl_stop( 'iom_axis', 'Definition for axis with '//TRIM(ADJUSTL(str))//' levels missing') 
     429       ENDIF 
     430   END FUNCTION iom_axis 
    308431 
    309432   SUBROUTINE iom_set_rstw_var_active(field) 
     
    659782      ! 
    660783      CALL xios_set_current_context(nemo_hdl) 
     784       
    661785#endif 
    662786      ! 
     
    9801104      ELSE 
    9811105#if defined key_iomput 
    982          IF(lwp) WRITE(numout,*) 'XIOS RST READ (0D): ', trim(cdvar) 
    983          CALL iom_swap( TRIM(crxios_context) ) 
     1106         IF(lwp) WRITE(numout,*) 'XIOS READ (0D): ', trim(cdvar) 
    9841107         CALL xios_recv_field( trim(cdvar), pvar) 
    985          CALL iom_swap( TRIM(cxios_context) ) 
    9861108#else 
    9871109         WRITE(ctmp1,*) 'Can not use XIOS in iom_g0d, file: '//trim(clname)//', var:'//trim(cdvar) 
     
    13341456#if defined key_iomput 
    13351457!would be good to be able to check which context is active and swap only if current is not restart 
    1336          CALL iom_swap( TRIM(crxios_context) )  
    13371458         IF( PRESENT(pv_r3d) ) THEN 
    13381459            pv_r3d(:, :, :) = 0. 
    1339             if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar) 
     1460            if(lwp) write(numout,*) 'XIOS READ (3D): ',trim(cdvar) 
    13401461            CALL xios_recv_field( trim(cdvar), pv_r3d) 
    13411462            IF(idom /= jpdom_unknown ) then 
     
    13441465         ELSEIF( PRESENT(pv_r2d) ) THEN 
    13451466            pv_r2d(:, :) = 0. 
    1346             if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar) 
     1467            if(lwp) write(numout,*) 'XIOS READ (2D): ', trim(cdvar) 
    13471468            CALL xios_recv_field( trim(cdvar), pv_r2d) 
    13481469            IF(idom /= jpdom_unknown ) THEN 
     
    13511472         ELSEIF( PRESENT(pv_r1d) ) THEN 
    13521473            pv_r1d(:) = 0. 
    1353             if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar) 
     1474            if(lwp) write(numout,*) 'XIOS READ (1D): ', trim(cdvar) 
    13541475            CALL xios_recv_field( trim(cdvar), pv_r1d) 
    13551476         ENDIF 
    1356          CALL iom_swap( TRIM(cxios_context) ) 
    13571477#else 
    13581478         istop = istop + 1  
     
    13941514   END SUBROUTINE iom_get_var 
    13951515 
     1516   SUBROUTINE iom_dom_context(fdname, numr)   
     1517      !!----------------------------------------------------------------------- 
     1518      !!                  ***  FUNCTION  iom_getszuld  *** 
     1519      !! 
     1520      !! ** Purpose : initialize context for reading domain information 
     1521      !!----------------------------------------------------------------------- 
     1522      INTEGER, PARAMETER                   :: maxf = 48 
     1523      CHARACTER(len=*), INTENT(IN)         :: fdname 
     1524      INTEGER,          INTENT(IN)         :: numr 
     1525      !local variables 
     1526      CHARACTER(len=lc)                    :: cxname 
     1527      CHARACTER(len=lc)                    :: cfile 
     1528      TYPE(xios_domaingroup)               :: domaingroup_hdl  
     1529      TYPE(xios_domain)                    :: domain_hdl  
     1530      TYPE(xios_axisgroup)                 :: axisgroup_hdl  
     1531      TYPE(xios_axis)                      :: axis_hdl  
     1532      TYPE(xios_scalar)                    :: scalar_hdl  
     1533      TYPE(xios_scalargroup)               :: scalargroup_hdl  
     1534      TYPE(xios_field) :: field_hdl 
     1535      TYPE(xios_file) :: file_hdl 
     1536      TYPE(xios_filegroup) :: filegroup_hdl 
     1537      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0) 
     1538      INTEGER :: i 
     1539      INTEGER :: ni 
     1540      INTEGER :: nj 
     1541      !!----------------------------------------------------------------------- 
     1542 
     1543#if defined key_iomput 
     1544      cxname = "dom_context" 
     1545      IF( TRIM(Agrif_CFixed()) .NE. '0' ) THEN 
     1546         CALL xios_context_initialize(TRIM(Agrif_CFixed())//"_"//TRIM(cxname), mpi_comm_oce) 
     1547      ELSE 
     1548         CALL xios_context_initialize(TRIM(cxname), mpi_comm_oce) 
     1549      ENDIF 
     1550      CALL iom_swap( cxname ) 
     1551!calendar must be defined always 
     1552      CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1,1,1,00,00,00), & 
     1553          &                                    start_date = xios_date(1,1,1,0,0,0) ) 
     1554     CALL xios_get_handle("domain_definition",domaingroup_hdl)  
     1555     CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N")  
     1556     ni = nlei-nldi+1 
     1557     nj = nlej-nldj+1 
     1558     ! 
     1559     CALL iom_set_domain_attr("grid_N", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj) 
     1560     CALL iom_set_domain_attr("grid_N", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj) 
     1561  
     1562     CALL xios_get_handle("axis_definition",axisgroup_hdl)  
     1563     CALL xios_add_child(axisgroup_hdl, axis_hdl, "nav_lev")  
     1564!AGRIF fails to compile when unit= is in call to xios_set_axis_attr 
     1565!    CALL xios_set_axis_attr( "nav_lev", long_name="Vertical levels in meters", positive="down") 
     1566     CALL xios_set_axis_attr ("nav_lev", n_glo=jpk ) 
     1567 
     1568     CALL xios_get_handle("scalar_definition", scalargroup_hdl)  
     1569     CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar")  
     1570 
     1571     IF( TRIM(Agrif_CFixed()) == '0' ) THEN 
     1572        cfile = TRIM(fdname) 
     1573     ELSE 
     1574        cfile = TRIM(Agrif_CFixed())//"_"//TRIM(fdname) 
     1575     ENDIF 
     1576     CALL iom_set_vars_active(cfile, iom_file(numr)%nfid) 
     1577    ! set time step length 
     1578     dtime%second = rn_Dt 
     1579     CALL xios_set_timestep( dtime ) 
     1580 
     1581     CALL iom_init_closedef 
     1582#endif 
     1583   END SUBROUTINE iom_dom_context 
    13961584 
    13971585   FUNCTION iom_getszuld ( kiomid )   
Note: See TracChangeset for help on using the changeset viewer.