Skip to content

Commit

Permalink
FMS2: Case-insensitive init_external_field
Browse files Browse the repository at this point in the history
The FMS1 implementation of init_external_field is case-insensitive, but
the FMS2 implementation is case-sensitive, which can cause errors in
older established input files.

This patch sweeps through the fields of the input files and checks for a
case-insensitive match (using lowercase()).  This requires an additional
open/close of the file.
  • Loading branch information
marshallward committed Jun 16, 2023
1 parent 35e3642 commit d5ce336
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions config_src/infra/FMS2/MOM_interp_infra.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module MOM_interp_infra
use MOM_io, only : axis_info
use MOM_io, only : get_var_axes_info
use MOM_time_manager, only : time_type
use horiz_interp_mod, only : horiz_interp_new, horiz_interp, horiz_interp_init, horiz_interp_type
use MOM_error_handler, only : MOM_error, FATAL
use MOM_string_functions, only : lowercase
use horiz_interp_mod, only : horiz_interp_new, horiz_interp, horiz_interp_init, horiz_interp_type
use netcdf_io_mod, only : FmsNetcdfFile_t, netcdf_file_open, netcdf_file_close
use netcdf_io_mod, only : get_num_variables, get_variable_names
use time_interp_external2_mod, only : time_interp_external
use time_interp_external2_mod, only : init_external_field, time_interp_external_init
use time_interp_external2_mod, only : get_external_field_size
Expand Down Expand Up @@ -262,16 +266,60 @@ function init_extern_field(file, fieldname, MOM_domain, domain, verbose, &
!! a model date of Feb 29. onto a common year on Feb. 28.
type(external_field) :: field !< Handle to external field

type(FmsNetcdfFile_t) :: extern_file
! Local instance of netCDF file used to locate case-insensitive field name
integer :: num_fields
! Number of fields in external file
character(len=256), allocatable :: extern_fieldnames(:)
! List of field names in file
! NOTE: length should NF90_MAX_NAME, but I don't know how to read it
character(len=:), allocatable :: label
! Case-insensitive match to fieldname in file
logical :: rc
! Return status
integer :: i
! Loop index

field%filename = file
field%label = fieldname

! FMS2's init_external_field is case sensitive, so we must replicate the
! case-insensitivity of FMS1. This requires opening the file twice.

rc = netcdf_file_open(extern_file, file, 'read')
if (.not. rc) then
call MOM_error(FATAL, 'init_extern_file: file ' // trim(file) &
// ' could not be opened.')
endif

! TODO: broadcast = .false.?
num_fields = get_num_variables(extern_file)

allocate(extern_fieldnames(num_fields))
call get_variable_names(extern_file, extern_fieldnames)

do i = 1, num_fields
if (lowercase(extern_fieldnames(i)) == lowercase(fieldname)) then
field%label = extern_fieldnames(i)
exit
endif
enddo

call netcdf_file_close(extern_file)

if (.not. allocated(field%label)) then
call MOM_error(FATAL, 'init_extern_field: field ' // trim(fieldname) &
// ' not found in ' // trim(file) // '.')
endif

! Pass to FMS2 implementation of init_external_field

if (present(MOM_Domain)) then
field%id = init_external_field(file, fieldname, domain=MOM_domain%mpp_domain, &
field%id = init_external_field(file, field%label, domain=MOM_domain%mpp_domain, &
verbose=verbose, ierr=ierr, ignore_axis_atts=ignore_axis_atts, &
correct_leap_year_inconsistency=correct_leap_year_inconsistency, &
ongrid=.true.)
else
field%id = init_external_field(file, fieldname, domain=domain, &
field%id = init_external_field(file, field%label, domain=domain, &
verbose=verbose, ierr=ierr, ignore_axis_atts=ignore_axis_atts, &
correct_leap_year_inconsistency=correct_leap_year_inconsistency, &
ongrid=.true.)
Expand Down

0 comments on commit d5ce336

Please sign in to comment.