Skip to content

Commit 8b9df0a

Browse files
committed
update check_data to not error when variable is not found
1 parent c2a33d7 commit 8b9df0a

18 files changed

+495
-382
lines changed

src/data/write_init_files.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ def write_init_files(cap_database, ic_names, registry_constituents, vars_init_va
174174
outfile.include(filepath)
175175
# end for
176176

177+
all_req_vars = in_vars + out_vars
178+
# Remove duplicates but preserve order for testing
179+
unique_req_vars = list(OrderedDict.fromkeys(all_req_vars))
180+
177181
# Write public parameters:
178-
retvals = write_ic_params(outfile, in_vars, ic_names, registry_constituents)
182+
retvals = write_ic_params(outfile, unique_req_vars, ic_names, registry_constituents)
179183
ic_names, ic_max_len, stdname_max_len = retvals
180184

181185
# Write initial condition arrays:
182186
write_ic_arrays(outfile, ic_names, ic_max_len,
183-
stdname_max_len, in_vars, registry_constituents)
187+
stdname_max_len, unique_req_vars, registry_constituents)
184188

185189
# Add "contains" statement:
186190
outfile.end_module_header()
@@ -1190,8 +1194,6 @@ def write_phys_read_subroutine(outfile, host_dict, host_vars, host_imports,
11901194
outfile.write("end do", 2)
11911195
outfile.blank_line()
11921196

1193-
# start default case steps:
1194-
11951197
# End subroutine:
11961198
outfile.write("end subroutine physics_read_data", 1)
11971199

@@ -1252,8 +1254,8 @@ def write_phys_check_subroutine(outfile, host_dict, host_vars, host_imports,
12521254
call_str += f"timestep, {var_locname}, '{var_stdname}', "
12531255
call_str += "min_difference, min_relative_value, is_first, diff_found)"
12541256
else:
1255-
call_str = f"call endrun('Cannot check status of {var_locname}'" + \
1256-
f"//', {reason}')"
1257+
# For check field, don't endrun
1258+
call_str = "! do nothing - variable can't be checked"
12571259
# end if
12581260
# Add string to dictionary:
12591261
call_string_dict[call_string_key] = call_str
@@ -1388,7 +1390,7 @@ def write_phys_check_subroutine(outfile, host_dict, host_vars, host_imports,
13881390
"that they can be read from input file if need be:", 3)
13891391
outfile.write("call ccpp_physics_suite_variables(suite_names" + \
13901392
"(suite_idx), ccpp_required_data, errmsg, errflg, " + \
1391-
"input_vars=.true., output_vars=.false.)", 4)
1393+
"input_vars=.false., output_vars=.true.)", 4)
13921394
outfile.blank_line()
13931395

13941396
# Loop over required variables:
@@ -1397,37 +1399,48 @@ def write_phys_check_subroutine(outfile, host_dict, host_vars, host_imports,
13971399
outfile.write("do req_idx = 1, size(ccpp_required_data, 1)", 3)
13981400
outfile.blank_line()
13991401

1400-
# First check if the required variable is a constituent
1401-
outfile.comment("First check if the required variable is a constituent:", 4)
1402-
outfile.write("call const_get_index(ccpp_required_data(req_idx), constituent_idx, abort=.false., warning=.false.)", 4)
1403-
outfile.write("if (constituent_idx > -1) then", 4)
1404-
outfile.write("cycle", 5)
1405-
outfile.write("else", 4)
1406-
outfile.comment("The required variable is not a constituent. Check if the variable was read from a file", 5)
1407-
14081402
# Call input name search function:
1409-
outfile.comment("Find IC file input name array index for required variable:", 5)
1410-
outfile.write("call is_read_from_file(ccpp_required_data(req_idx), " + \
1411-
"is_read, stdnam_idx_out=name_idx)", 5)
1412-
outfile.write("if (.not. is_read) then", 5)
1413-
outfile.write("cycle", 6)
1414-
outfile.write("end if", 5)
1403+
outfile.comment("Find IC file input name array index for required variable:", 4)
1404+
outfile.write("name_idx = find_input_name_idx(ccpp_required_data(req_idx), .true., constituent_idx)", 4)
1405+
1406+
# Start select-case statement:
1407+
outfile.blank_line()
1408+
outfile.comment("Check for special index values:", 4)
1409+
outfile.write("select case (name_idx)", 4)
1410+
outfile.blank_line()
1411+
1412+
# Skip constituent variables:
1413+
outfile.write("case (const_idx)", 5)
1414+
outfile.blank_line()
1415+
outfile.comment("If variable is a constituent, then do nothing. We'll handle these later", 6)
1416+
outfile.blank_line()
1417+
1418+
# Generate error message if required variable isn't found:
1419+
outfile.write("case (no_exist_idx)", 5)
1420+
outfile.blank_line()
1421+
outfile.comment("If an index was never found, then do nothing. We won't try to check these.", 6)
1422+
outfile.blank_line()
1423+
1424+
# start default case steps:
1425+
outfile.write("case default", 5)
1426+
outfile.blank_line()
14151427

14161428
# Generate "check_field" calls:
1417-
outfile.comment("Check variable vs input check file:", 5)
1429+
outfile.comment("Check variable vs input check file:", 6)
14181430
outfile.blank_line()
1419-
outfile.write("select case (trim(phys_var_stdnames(name_idx)))", 5)
1431+
outfile.write("select case (trim(phys_var_stdnames(name_idx)))", 6)
14201432
for case_call, read_call in call_string_dict.items():
1421-
outfile.write(case_call, 5)
1422-
outfile.write(read_call, 6)
1433+
outfile.write(case_call, 6)
1434+
outfile.write(read_call, 7)
14231435
outfile.blank_line()
1424-
outfile.write("end select !check variables", 5)
1425-
outfile.write("if (diff_found) then", 5)
1426-
outfile.write("overall_diff_found = .true.", 6)
1427-
outfile.write("end if", 5)
1428-
outfile.write("end if !check if constituent", 4)
1436+
outfile.write("end select !check variables", 6)
1437+
outfile.write("if (diff_found) then", 6)
1438+
outfile.write("overall_diff_found = .true.", 7)
1439+
outfile.write("end if", 6)
14291440

14301441
# End select case and required variables loop:
1442+
outfile.write("end select !special indices", 4)
1443+
outfile.blank_line()
14311444
outfile.write("end do !Suite-required variables", 3)
14321445
outfile.blank_line()
14331446

test/unit/python/sample_files/write_init_files/phys_vars_init_check_simple.F90

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module phys_vars_init_check_simple
3333
integer, public, parameter :: PARAM = 2
3434
integer, public, parameter :: READ_FROM_FILE = 3
3535
! Total number of physics-related variables:
36-
integer, public, parameter :: phys_var_num = 2
36+
integer, public, parameter :: phys_var_num = 3
3737
integer, public, parameter :: phys_const_num = 16
3838

3939
!Max length of physics-related variable standard names:
@@ -45,7 +45,8 @@ module phys_vars_init_check_simple
4545
! Physics-related input variable standard names:
4646
character(len=25), public, protected :: phys_var_stdnames(phys_var_num) = (/ &
4747
'potential_temperature ', &
48-
'air_pressure_at_sea_level' /)
48+
'air_pressure_at_sea_level', &
49+
'tendency_of_peverwhee ' /)
4950

5051
character(len=36), public, protected :: phys_const_stdnames(phys_const_num) = (/ &
5152
"ccpp_constituent_minimum_values ", &
@@ -67,15 +68,18 @@ module phys_vars_init_check_simple
6768
!Array storing all registered IC file input names for each variable:
6869
character(len=5), public, protected :: input_var_names(1, phys_var_num) = reshape((/ &
6970
'theta', &
70-
'slp ' /), (/1, phys_var_num/))
71+
'slp ', &
72+
'ptend' /), (/1, phys_var_num/))
7173

7274
! Array indicating whether or not variable is protected:
7375
logical, public, protected :: protected_vars(phys_var_num)= (/ &
76+
.false., &
7477
.false., &
7578
.false. /)
7679

7780
! Variable state (UNINITIALIZED, INTIIALIZED, PARAM or READ_FROM_FILE):
7881
integer, public, protected :: initialized_vars(phys_var_num)= (/ &
82+
UNINITIALIZED, &
7983
UNINITIALIZED, &
8084
UNINITIALIZED /)
8185

test/unit/python/sample_files/write_init_files/physics_inputs_4D.F90

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,34 +304,40 @@ subroutine physics_check_data(file_name, suite_names, timestep, min_difference,
304304
do suite_idx = 1, size(suite_names, 1)
305305

306306
! Search for all needed CCPP input variables, so that they can be read from input file if need be:
307-
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.true., output_vars=.false.)
307+
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.false., output_vars=.true.)
308308

309309
! Loop over all required variables as specified by CCPP suite:
310310
do req_idx = 1, size(ccpp_required_data, 1)
311311

312-
! First check if the required variable is a constituent:
313-
call const_get_index(ccpp_required_data(req_idx), constituent_idx, abort=.false., warning=.false.)
314-
if (constituent_idx > -1) then
315-
cycle
316-
else
317-
! The required variable is not a constituent. Check if the variable was read from a file
318-
! Find IC file input name array index for required variable:
319-
call is_read_from_file(ccpp_required_data(req_idx), is_read, stdnam_idx_out=name_idx)
320-
if (.not. is_read) then
321-
cycle
322-
end if
323-
! Check variable vs input check file:
312+
! Find IC file input name array index for required variable:
313+
name_idx = find_input_name_idx(ccpp_required_data(req_idx), .true., constituent_idx)
314+
315+
! Check for special index values:
316+
select case (name_idx)
324317

325-
select case (trim(phys_var_stdnames(name_idx)))
326-
case ('potential_temperature')
327-
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
328-
min_relative_value, is_first, diff_found)
318+
case (const_idx)
319+
320+
! If variable is a constituent, then do nothing. We'll handle these later
321+
322+
case (no_exist_idx)
323+
324+
! If an index was never found, then do nothing. We won't try to check these.
325+
326+
case default
327+
328+
! Check variable vs input check file:
329+
330+
select case (trim(phys_var_stdnames(name_idx)))
331+
case ('potential_temperature')
332+
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
333+
min_relative_value, is_first, diff_found)
334+
335+
end select !check variables
336+
if (diff_found) then
337+
overall_diff_found = .true.
338+
end if
339+
end select !special indices
329340

330-
end select !check variables
331-
if (diff_found) then
332-
overall_diff_found = .true.
333-
end if
334-
end if !check if constituent
335341
end do !Suite-required variables
336342

337343
! Deallocate required variables array for use in next suite:

test/unit/python/sample_files/write_init_files/physics_inputs_bvd.F90

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,34 +304,40 @@ subroutine physics_check_data(file_name, suite_names, timestep, min_difference,
304304
do suite_idx = 1, size(suite_names, 1)
305305

306306
! Search for all needed CCPP input variables, so that they can be read from input file if need be:
307-
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.true., output_vars=.false.)
307+
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.false., output_vars=.true.)
308308

309309
! Loop over all required variables as specified by CCPP suite:
310310
do req_idx = 1, size(ccpp_required_data, 1)
311311

312-
! First check if the required variable is a constituent:
313-
call const_get_index(ccpp_required_data(req_idx), constituent_idx, abort=.false., warning=.false.)
314-
if (constituent_idx > -1) then
315-
cycle
316-
else
317-
! The required variable is not a constituent. Check if the variable was read from a file
318-
! Find IC file input name array index for required variable:
319-
call is_read_from_file(ccpp_required_data(req_idx), is_read, stdnam_idx_out=name_idx)
320-
if (.not. is_read) then
321-
cycle
322-
end if
323-
! Check variable vs input check file:
312+
! Find IC file input name array index for required variable:
313+
name_idx = find_input_name_idx(ccpp_required_data(req_idx), .true., constituent_idx)
314+
315+
! Check for special index values:
316+
select case (name_idx)
324317

325-
select case (trim(phys_var_stdnames(name_idx)))
326-
case ('potential_temperature')
327-
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
328-
min_relative_value, is_first, diff_found)
318+
case (const_idx)
319+
320+
! If variable is a constituent, then do nothing. We'll handle these later
321+
322+
case (no_exist_idx)
323+
324+
! If an index was never found, then do nothing. We won't try to check these.
325+
326+
case default
327+
328+
! Check variable vs input check file:
329+
330+
select case (trim(phys_var_stdnames(name_idx)))
331+
case ('potential_temperature')
332+
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
333+
min_relative_value, is_first, diff_found)
334+
335+
end select !check variables
336+
if (diff_found) then
337+
overall_diff_found = .true.
338+
end if
339+
end select !special indices
329340

330-
end select !check variables
331-
if (diff_found) then
332-
overall_diff_found = .true.
333-
end if
334-
end if !check if constituent
335341
end do !Suite-required variables
336342

337343
! Deallocate required variables array for use in next suite:

test/unit/python/sample_files/write_init_files/physics_inputs_cnst.F90

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,34 +304,40 @@ subroutine physics_check_data(file_name, suite_names, timestep, min_difference,
304304
do suite_idx = 1, size(suite_names, 1)
305305

306306
! Search for all needed CCPP input variables, so that they can be read from input file if need be:
307-
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.true., output_vars=.false.)
307+
call ccpp_physics_suite_variables(suite_names(suite_idx), ccpp_required_data, errmsg, errflg, input_vars=.false., output_vars=.true.)
308308

309309
! Loop over all required variables as specified by CCPP suite:
310310
do req_idx = 1, size(ccpp_required_data, 1)
311311

312-
! First check if the required variable is a constituent:
313-
call const_get_index(ccpp_required_data(req_idx), constituent_idx, abort=.false., warning=.false.)
314-
if (constituent_idx > -1) then
315-
cycle
316-
else
317-
! The required variable is not a constituent. Check if the variable was read from a file
318-
! Find IC file input name array index for required variable:
319-
call is_read_from_file(ccpp_required_data(req_idx), is_read, stdnam_idx_out=name_idx)
320-
if (.not. is_read) then
321-
cycle
322-
end if
323-
! Check variable vs input check file:
312+
! Find IC file input name array index for required variable:
313+
name_idx = find_input_name_idx(ccpp_required_data(req_idx), .true., constituent_idx)
314+
315+
! Check for special index values:
316+
select case (name_idx)
324317

325-
select case (trim(phys_var_stdnames(name_idx)))
326-
case ('potential_temperature')
327-
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
328-
min_relative_value, is_first, diff_found)
318+
case (const_idx)
319+
320+
! If variable is a constituent, then do nothing. We'll handle these later
321+
322+
case (no_exist_idx)
323+
324+
! If an index was never found, then do nothing. We won't try to check these.
325+
326+
case default
327+
328+
! Check variable vs input check file:
329+
330+
select case (trim(phys_var_stdnames(name_idx)))
331+
case ('potential_temperature')
332+
call check_field(file, input_var_names(:,name_idx), 'lev', timestep, theta, 'potential_temperature', min_difference, &
333+
min_relative_value, is_first, diff_found)
334+
335+
end select !check variables
336+
if (diff_found) then
337+
overall_diff_found = .true.
338+
end if
339+
end select !special indices
329340

330-
end select !check variables
331-
if (diff_found) then
332-
overall_diff_found = .true.
333-
end if
334-
end if !check if constituent
335341
end do !Suite-required variables
336342

337343
! Deallocate required variables array for use in next suite:

0 commit comments

Comments
 (0)