Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module io_benchmark_setup_mod
use fs_continuity_mod, only: Wtheta
use function_space_mod, only: function_space_type
use function_space_collection_mod, only: function_space_collection
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES, &
CONVENTION_UGRID
use lfric_xios_read_mod, only: read_field_generic
use lfric_xios_write_mod, only: write_field_generic
use linked_list_mod, only: linked_list_type
Expand Down Expand Up @@ -101,7 +102,8 @@ subroutine setup_io_benchmark_files(file_list, modeldb)
io_mode=FILE_MODE_WRITE, &
operation=OPERATION_TIMESERIES, &
freq=diagnostic_frequency, &
fields_in_file=io_benchmark_fields ) )
fields_in_file=io_benchmark_fields, &
file_convention=CONVENTION_UGRID ) )

nullify(io_benchmark_fields)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ program lfric_xios_cyclic_temporal_test
use lfric_xios_action_mod, only: advance
use lfric_xios_context_mod, only: lfric_xios_context_type
use lfric_xios_driver_mod, only: lfric_xios_initialise, lfric_xios_finalise
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES, &
CONVENTION_UGRID
use linked_list_mod, only: linked_list_type
use log_mod, only: log_event, log_level_info
use test_db_mod, only: test_db_type
Expand Down Expand Up @@ -53,12 +54,14 @@ program lfric_xios_cyclic_temporal_test
operation=OPERATION_TIMESERIES, &
freq=file_freq, &
cyclic=.true., &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )
call file_list%insert_item( lfric_xios_file_type( "lfric_xios_cyclic_output", &
xios_id="lfric_xios_cyclic_output", &
io_mode=FILE_MODE_WRITE, &
operation=OPERATION_TIMESERIES, &
freq=1, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )

call io_context%initialise_xios_context( test_db%comm, &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ program lfric_xios_temporal_interp_test
use lfric_xios_action_mod, only: advance
use lfric_xios_context_mod, only: lfric_xios_context_type
use lfric_xios_driver_mod, only: lfric_xios_initialise, lfric_xios_finalise
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES, &
CONVENTION_UGRID
use lfric_xios_constants_mod, only: lx_second
use linked_list_mod, only: linked_list_type
use log_mod, only: log_event, log_level_info
Expand Down Expand Up @@ -65,12 +66,14 @@ program lfric_xios_temporal_interp_test
freq=file_freq, &
cyclic=.true., &
update_freq=20*lx_second, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )
call file_list%insert_item( lfric_xios_file_type( "lfric_xios_interp_output", &
xios_id="lfric_xios_interp_output", &
io_mode=FILE_MODE_WRITE, &
operation=OPERATION_TIMESERIES, &
freq=1, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )

call io_context%initialise_xios_context( test_db%comm, &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ program lfric_xios_temporal_iodef_test
use lfric_xios_action_mod, only: advance
use lfric_xios_context_mod, only: lfric_xios_context_type
use lfric_xios_driver_mod, only: lfric_xios_initialise, lfric_xios_finalise
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES, &
CONVENTION_UGRID
use linked_list_mod, only: linked_list_type
use log_mod, only: log_event, log_level_info
use test_db_mod, only: test_db_type
Expand Down Expand Up @@ -47,12 +48,14 @@ program lfric_xios_temporal_iodef_test
xios_id="lfric_xios_temporal_input", &
io_mode=FILE_MODE_READ, &
operation=OPERATION_TIMESERIES, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )
call file_list%insert_item( lfric_xios_file_type( "lfric_xios_temporal_output", &
xios_id="lfric_xios_temporal_output", &
io_mode=FILE_MODE_WRITE, &
operation=OPERATION_TIMESERIES, &
freq=1, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )

call io_context%initialise_xios_context( test_db%comm, &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ program lfric_xios_temporal_test
use lfric_xios_action_mod, only: advance
use lfric_xios_context_mod, only: lfric_xios_context_type
use lfric_xios_driver_mod, only: lfric_xios_initialise, lfric_xios_finalise
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES
use lfric_xios_file_mod, only: lfric_xios_file_type, OPERATION_TIMESERIES, &
CONVENTION_UGRID
use linked_list_mod, only: linked_list_type
use log_mod, only: log_event, log_level_info
use test_db_mod, only: test_db_type
Expand Down Expand Up @@ -51,12 +52,14 @@ program lfric_xios_temporal_test
io_mode=FILE_MODE_READ, &
operation=OPERATION_TIMESERIES, &
freq=file_freq, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )
call file_list%insert_item( lfric_xios_file_type( "lfric_xios_temporal_output", &
xios_id="lfric_xios_temporal_output", &
io_mode=FILE_MODE_WRITE, &
operation=OPERATION_TIMESERIES, &
freq=1, &
file_convention=CONVENTION_UGRID, &
fields_in_file=test_db%temporal_fields ) )

call io_context%initialise_xios_context( test_db%comm, &
Expand Down
25 changes: 20 additions & 5 deletions components/lfric-xios/source/lfric_xios_field_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,21 @@ end function lfric_xios_field_constructor

!> Registers a representation of the model field with the associated XIOS field
!> group
subroutine register(self)
subroutine register(self, ugrid)

implicit none

class(lfric_xios_field_type), intent(inout) :: self
logical, optional, intent(in) :: ugrid

type(xios_fieldgroup) :: fieldgroup_hdl
type(xios_domain) :: domain
type(xios_axis) :: vert_axis, ndata_axis
type(xios_gridgroup) :: grid_definition
type(xios_grid) :: new_grid
character(str_def) :: domain_id, axis_id, grid_id
character(str_def) :: domain_id, axis_id, grid_id, grid_tag

logical :: ugrid_format

type(mesh_type), pointer :: mesh => null()
type(function_space_type), pointer :: vspace => null()
Expand All @@ -136,6 +139,11 @@ subroutine register(self)
"] with field group ["//trim(self%fieldgroup_id)//"]", &
log_level_trace )

ugrid_format = .true.
if (present(ugrid)) then
ugrid_format = ugrid
end if

! If this field is already registered with XIOS, get the existing handle,
! otherwise a new one will be created below as part of `xios_add_child`.
if (xios_is_valid_field(self%xios_id)) then
Expand Down Expand Up @@ -168,6 +176,13 @@ subroutine register(self)
axis_id ="vert_axis_half_levels"
end select

! Use CF domains if not UGRID
grid_tag = "_grid"
if (.not. ugrid_format) then
domain_id = trim(domain_id)//"_cf"
grid_tag = "_cf_grid"
end if

mesh => self%model_field%get_mesh()
! If field had only a single layer of DoFs then define this using a domain,
! otherwise use a grid.
Expand All @@ -179,12 +194,12 @@ subroutine register(self)

! Create grid ID using function space information.
if (vspace%get_ndata() == 1) then
grid_id = trim(name_from_functionspace(self%model_field%which_function_space()))//"_grid"
grid_id = trim(name_from_functionspace(self%model_field%which_function_space()))//trim(grid_tag)
else if (mesh%get_nlayers() == 1) then
grid_id = trim(domain_id)//"_"//char(vspace%get_ndata())//"_grid"
grid_id = trim(domain_id)//"_"//char(vspace%get_ndata())//trim(grid_tag)
else
grid_id = trim(name_from_functionspace(self%model_field%which_function_space()))// &
"_ndata_"//char(vspace%get_ndata())//"_grid"
"_ndata_"//char(vspace%get_ndata())//trim(grid_tag)
end if

! If this grid does not already exist, create it.
Expand Down
42 changes: 31 additions & 11 deletions components/lfric-xios/source/lfric_xios_file_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ subroutine register_with_context(self)
type(xios_duration) :: timestep_duration
type(xios_date) :: start_date

integer(i_def) :: i, record_offset
logical :: output_freq_defined
integer(i_def) :: i, record_offset, convention_def_enum
logical :: convention_defined_xml, output_freq_defined
character(str_def) :: iodef_file_convention

call log_event( "Registering XIOS file ["//trim(self%xios_id)//"]", &
log_level_trace )
Expand All @@ -366,20 +367,39 @@ subroutine register_with_context(self)

! Set I/O mode (no need for case defalt as we will fall back to XIOS's
! default behaviour)
call xios_set_attr( self%handle, type="one_file" )
call xios_set_attr(self%handle, type="one_file")
select case(self%io_mode)
case (FILE_MODE_READ)
call xios_set_attr( self%handle, mode="read" )
call xios_set_attr(self%handle, mode="read")
case (FILE_MODE_WRITE)
call xios_set_attr( self%handle, mode="write" )
call xios_set_attr(self%handle, mode="write")
end select

! Set XIOS file convention
select case(self%file_convention)
! Set XIOS file convention definition

! Check if file convention has been defined in iodef.xml config
call xios_is_defined_file_attr(self%xios_id, convention=convention_defined_xml)

if (convention_defined_xml) then
call xios_get_file_attr(self%xios_id, convention=iodef_file_convention)
if (trim(iodef_file_convention) == "CF") then
convention_def_enum = CONVENTION_CF
else if (trim(iodef_file_convention) == "UGRID") then
convention_def_enum = CONVENTION_UGRID
end if
else if (self%file_convention /= undef_file_convention) then
convention_def_enum = self%file_convention
else ! Default to UGRID if no convention provided
convention_def_enum = CONVENTION_UGRID
end if
Comment on lines +380 to +394

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise that it's unlikely that these if-tests wouldn't catch all cases, but it would still be good to have an else option to catch any potential errors, rather than let the code continue without setting the file convention.

@EdHone Ed Hone (EdHone) Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a confusing bit of code, but there's a reason why we can't include an else condition. This bit of logic is essentially setting some object attributes after the constructor, based on XIOS configuration. The way the logic is constructed it takes the value in the iodef.xml file if it is set, then the value from the object if it's not set, and then finally defaults to UGRID if nothing is set.

As I write this I realise that this logic is slightly all over the place, we probably shouldn't be changing the object's attribute after it's been set in the constructor so I will restructure it slightly to more accurately reflect the desired behaviour


select case(convention_def_enum)
case (CONVENTION_CF)
call xios_set_attr( self%handle, convention="CF" )
call xios_set_attr(self%handle, convention="CF")
call xios_set_attr(self%handle, convention_str="CF-1.12")
case (CONVENTION_UGRID)
call xios_set_attr( self%handle, convention="UGRID" )
call xios_set_attr(self%handle, convention="UGRID")
call xios_set_attr(self%handle, convention_str="CF-1.12 UGRID-1.0")
end select

! Create CF-compliant time description
Expand Down Expand Up @@ -431,7 +451,7 @@ subroutine register_with_context(self)
if (self%mode_is_read()) then
call xios_set_attr(self%handle, read_metadata_par=.true.)
call xios_set_attr(file_fields, read_access=.true.)
end if
end if

! Set up fields in file
if (allocated(self%fields)) then
Expand All @@ -446,7 +466,7 @@ subroutine register_with_context(self)

! Iterate over field collection and register fields
do i = 1, size(self%fields)
call self%fields(i)%register()
call self%fields(i)%register(ugrid=(self%file_convention == CONVENTION_UGRID))
end do

! Set up time axis if needed
Expand Down
4 changes: 2 additions & 2 deletions components/lfric-xios/source/lfric_xios_format_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module lfric_xios_format_mod
!> @param[in] fpxy Field proxy of LFRic field to be formatted
!> @param[in] m Number of rows in input matrix
!> @param[in] n Number of columns in input matrix
!> @param[in] legacy Use legacy checkpoint domains?
!> @param[in] legacy Use legacy domains?
!>
subroutine format_field(xios_data, field_name, fpxy, m, n, legacy)
implicit none
Expand Down Expand Up @@ -119,7 +119,7 @@ end subroutine format_field
!> @param[in] fpxy Field proxy of LFRic field to be read
!> @param[in] m Number of rows in field data matrix
!> @param[in] n Number of columns in field data matrix
!> @param[in] legacy Use legacy checkpoint domains?
!> @param[in] legacy Use legacy domains?
!>
subroutine inverse_format_field(xios_data, field_name, fpxy, m, n, legacy)
implicit none
Expand Down
35 changes: 24 additions & 11 deletions components/lfric-xios/source/lfric_xios_metafile_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ module lfric_xios_metafile_mod

contains

!> @brief Support for legacy checkpoints
!> @brief Support for legacy I/O
!> @details This affects lbc and gungho prognostics, which have traditionally been
!> using the legacy checkpoint domains checkpoint_W3, checkpoint_W2 etc.
!> using the legacy domains legacy_W3, legacy_W2 etc.
!> @param[in] field XIOS field object
!> @param[in] field_id XIOS fielld id
subroutine handle_legacy_fields(field, field_id)
Expand All @@ -64,9 +64,9 @@ subroutine handle_legacy_fields(field, field_id)
type(xios_field), intent(in) :: field
character(*), intent(in) :: field_id

character(20), parameter :: Wtheta = 'checkpoint_Wtheta'
character(20), parameter :: W3 = 'checkpoint_W3'
character(20), parameter :: W2 = 'checkpoint_W2'
character(20), parameter :: Wtheta = 'legacy_Wtheta'
character(20), parameter :: W3 = 'legacy_W3'
character(20), parameter :: W2 = 'legacy_W2'

character(20), parameter :: wtheta_fields(7) = [character(20) :: &
'theta', 'm_v', 'm_cl', 'm_r', 'm_ci', 'm_s', 'm_g']
Expand Down Expand Up @@ -187,8 +187,8 @@ end function get_field_precision
!> @param[in] mode ID prefix to be used, .e.g, "checkpoint_"
!> @param[in] operation XIOS field operation, e.g., "once"
!> @param[in] id_as_name Use dictionary field ID as field name?
!> @param[in] legacy Use legacy checkpointing domain?
subroutine add_field(metafile, dict_field_id, mode, operation, id_as_name, legacy)
!> @param[in] legacy Use legacy domains for checkpointing
subroutine add_field( metafile, dict_field_id, mode, operation, id_as_name, legacy )
implicit none
type(metafile_type), intent(in) :: metafile(:)
character(*), intent(in) :: dict_field_id
Expand All @@ -214,7 +214,7 @@ subroutine add_field(metafile, dict_field_id, mode, operation, id_as_name, legac
character(str_def) :: axis_ref
integer(i_def) :: prec
logical(l_def) :: use_id_as_name
logical(l_def) :: use_legacy
logical(l_def) :: use_ugrid_ckp, use_legacy
integer(i_def) :: i

use_id_as_name = .false.
Expand All @@ -223,6 +223,10 @@ subroutine add_field(metafile, dict_field_id, mode, operation, id_as_name, legac
use_legacy = .false.
if (present(legacy)) use_legacy = legacy

! Flag to use UGRID for checkpointing, disabled until XIOS supports UGRID
! for full set of checkpoint fields
use_ugrid_ckp = .false.

do i = 1, size(metafile)
file_id = metafile(i)%get_id()
if (mode == CHECKPOINTING ) then
Expand Down Expand Up @@ -262,14 +266,23 @@ subroutine add_field(metafile, dict_field_id, mode, operation, id_as_name, legac
if (use_legacy) then
call handle_legacy_fields(field, dict_field_id)
else
! Set correct domain/grid IDs for fields
grid_ref = get_field_grid_ref(dict_field_id)
if (grid_ref /= '') then
call xios_set_attr(field, grid_ref=grid_ref)
if (use_ugrid_ckp) then
call xios_set_attr(field, grid_ref=trim(grid_ref))
else
call xios_set_attr(field, grid_ref=trim(grid_ref)//"_cf")
end if
else
domain_ref = get_field_domain_ref(dict_field_id)
axis_ref = get_field_axis_ref(dict_field_id)
if (domain_ref /= '') call xios_set_attr(field, domain_ref=domain_ref)
if (axis_ref /= '') call xios_set_attr(field, axis_ref=axis_ref)
if (use_ugrid_ckp) then
if (domain_ref /= '') call xios_set_attr(field, domain_ref=trim(domain_ref))
else
if (domain_ref /= '') call xios_set_attr(field, domain_ref=trim(domain_ref)//"_cf")
end if
if (axis_ref /= '') call xios_set_attr(field, axis_ref=trim(axis_ref))
end if
end if
end if
Expand Down
4 changes: 2 additions & 2 deletions components/lfric-xios/source/lfric_xios_read_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ subroutine read_field_generic(xios_field_name, field_proxy)
call log_event( "Reading from XIOS field [" // trim(xios_field_name // "]"), &
LOG_LEVEL_TRACE )

! detect field with legacy checkpointing domain
legacy = (index(get_field_domain_ref(xios_field_name), 'checkpoint_') == 1)
! detect field with legacy domain
legacy = (index(get_field_domain_ref(xios_field_name), 'legacy_') == 1)

! sanity check
if (.not. legacy .and. .not. (hdim*vdim == undf)) then
Expand Down
Loading
Loading