Skip to content

Commit

Permalink
feat: data_override namelist flag to enable YAML data tables (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc2761 authored Nov 9, 2023
1 parent c70718d commit b0dbc30
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
39 changes: 27 additions & 12 deletions data_override/include/data_override.inc
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ real(FMS_DATA_OVERRIDE_KIND_) :: min_glo_lon_lnd, max_glo_lon_lnd
real(FMS_DATA_OVERRIDE_KIND_) :: min_glo_lon_ice, max_glo_lon_ice
integer :: num_fields = 0 !< number of fields in override_array already processed

#ifdef use_yaml
type(data_type), dimension(:), allocatable :: data_table !< user-provided data table
#else
type(data_type), dimension(max_table) :: data_table !< user-provided data table
#endif

type(data_type) :: default_table
type(override_type), dimension(max_array) :: override_array !< to store processed fields
Expand All @@ -118,8 +114,9 @@ logical :: reproduce_null_char_bug = .false.
!! to reproduce the mpp_io bug where lat/lon_bnd were
!! not read correctly if null characters are present in
!! the netcdf file
logical :: use_data_table_yaml = .false.

namelist /data_override_nml/ debug_data_override, grid_center_bug, reproduce_null_char_bug
namelist /data_override_nml/ debug_data_override, grid_center_bug, reproduce_null_char_bug, use_data_table_yaml

public :: DATA_OVERRIDE_INIT_IMPL_, DATA_OVERRIDE_UNSET_ATM_, DATA_OVERRIDE_UNSET_OCN_, &
& DATA_OVERRIDE_UNSET_LND_, DATA_OVERRIDE_UNSET_ICE_, DATA_OVERRIDE_0D_, &
Expand Down Expand Up @@ -166,6 +163,12 @@ if (grid_center_bug) then
"that is no longer supported. Please remove this namelist variable.")
endif

if (use_data_table_yaml) then
call mpp_error(NOTE, "You are using YAML.")
else
call mpp_error(NOTE, "You are using the legacy table.")
end if

atm_on = PRESENT(Atm_domain_in)
ocn_on = PRESENT(Ocean_domain_in)
lnd_on = PRESENT(Land_domain_in)
Expand Down Expand Up @@ -197,12 +200,25 @@ endif
default_table%interpol_method = 'bilinear'

#ifdef use_yaml
call read_table_yaml(data_table)
if (use_data_table_yaml) then
call read_table_yaml(data_table)
else
allocate(data_table(max_table))
do i = 1, max_table
data_table(i) = default_table
enddo
call read_table(data_table)
end if
#else
do i = 1,max_table
data_table(i) = default_table
enddo
call read_table(data_table)
if (use_data_table_yaml) then
call mpp_error(FATAL, "compilation error, need to compile with `-Duse_yaml`")
else
allocate(data_table(max_table))
do i = 1, max_table
data_table(i) = default_table
enddo
call read_table(data_table)
end if
#endif

! Initialize override array
Expand Down Expand Up @@ -330,7 +346,6 @@ function count_ne_1(in_1, in_2, in_3)
count_ne_1 = .not.(in_1.NEQV.in_2.NEQV.in_3) .OR. (in_1.AND.in_2.AND.in_3)
end function count_ne_1

#ifndef use_yaml
subroutine read_table(data_table)
type(data_type), dimension(max_table), intent(inout) :: data_table

Expand Down Expand Up @@ -475,7 +490,7 @@ subroutine read_table(data_table)
if(io_status/=0) call mpp_error(FATAL, 'data_override_mod: Error in closing file data_table')
end subroutine read_table
#else
#ifdef use_yaml
subroutine read_table_yaml(data_table)
type(data_type), dimension(:), allocatable, intent(out) :: data_table
Expand Down
2 changes: 2 additions & 0 deletions test_fms/data_override/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LDADD = $(top_builddir)/libFMS/libFMS.la

# Build this test program.
check_PROGRAMS = \
test_data_override_init \
test_get_grid_v1_r4 \
test_get_grid_v1_r8 \
test_data_override_r4 \
Expand All @@ -38,6 +39,7 @@ check_PROGRAMS = \
test_data_override_ongrid_r8

# This is the source code for the test.
test_data_override_init_SOURCES = test_data_override_init.F90
test_data_override_r4_SOURCES = test_data_override.F90
test_data_override_r8_SOURCES = test_data_override.F90

Expand Down
49 changes: 49 additions & 0 deletions test_fms/data_override/test_data_override2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,53 @@ fi

done

# data_override with the default table (not setting namelist)
cat <<_EOF > data_table
"ICE", "sst_obs", "SST", "INPUT/sst_ice_clim.nc", .false., 300.0
_EOF

test_expect_success "data_override_init with the default table" '
mpirun -n 1 ./test_data_override_init
'
# data_override with yaml table (setting namelist to .True.)
cat <<_EOF > input.nml
&data_override_nml
use_data_table_yaml=.true.
/
_EOF

cat <<_EOF > data_table.yaml
data_table:
- gridname : OCN
fieldname_code : runoff
fieldname_file : runoff
file_name : INPUT/runoff.daitren.clim.1440x1080.v20180328.nc
interpol_method : none
factor : 1.0
_EOF

if [ ! -z $parser_skip ]; then
test_expect_failure "data_override_init with the yaml table" '
mpirun -n 1 ./test_data_override_init
'
else
test_expect_success "data_override_init with the yaml table" '
mpirun -n 1 ./test_data_override_init
'
fi
#data_override with default table (setting namelist to .True.)
cat <<_EOF > data_table
"ICE", "sst_obs", "SST", "INPUT/sst_ice_clim.nc", .true., 300.0
_EOF

cat <<_EOF > input.nml
&data_override_nml
use_data_table_yaml=.false.
/
_EOF

test_expect_success "data_override_init with the default table" '
mpirun -n 1 ./test_data_override_init
'

test_done
29 changes: 29 additions & 0 deletions test_fms/data_override/test_data_override_init.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the GFDL Flexible Modeling System (FMS).
!*
!* FMS is free software: you can redistribute it and/or modify it under
!* the terms of the GNU Lesser General Public License as published by
!* the Free Software Foundation, either version 3 of the License, or (at
!* your option) any later version.
!*
!* FMS is distributed in the hope that it will be useful, but WITHOUT
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
!* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
!* for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************

program test_data_override_init

use fms_mod, only: fms_init, fms_end
use data_override_mod

call fms_init()
call data_override_init
call fms_end()

end program test_data_override_init

0 comments on commit b0dbc30

Please sign in to comment.