forked from ufs-community/UFS_UTILS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error handling in program filter_topo (ufs-community#553)
Update error handler routine ("handle_err") to use "error stop" instead of "stop" to return non-zero error code on failure. Add print of "FATAL ERROR" to standard output as per NCO requirements. Increase length of file name variables to handle whole path names. Move routines "handle_err" and "read_namelist" to a new module (utils.F90) to facilitate unit testing. Create unit test for routine "read_namelist". Fixes ufs-community#547.
- Loading branch information
1 parent
560d5c7
commit 529b168
Showing
8 changed files
with
181 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
!> @file | ||
!! @brief Utility routines. | ||
!! @author GFDL programmer | ||
|
||
!> Module that contains general utility routines. | ||
!! | ||
!! @author GFDL programmer | ||
module utils | ||
|
||
implicit none | ||
|
||
public | ||
|
||
character(len=512) :: topo_file = "orog" !< Path/name of the topography (or orography) file. | ||
character(len=128) :: topo_field = "orog_filt" !< NetCDF record name of the filtered | ||
!! topography (or orography). | ||
character(len=128) :: mask_field = "slmsk" !< NetCDF record name of the land/sea mask. | ||
character(len=512) :: grid_file = "atmos_mosaic.nc" !< Path/name of the grid mosaic file. | ||
|
||
logical :: zero_ocean = .true. !< If true, no diffusive flux into water/ocean | ||
!! area (preserve islands). | ||
logical :: nested = .false. !< If true, process a global grid with a nest. | ||
logical :: regional = .false. !< If true, process a stand-alone regional grid. | ||
|
||
integer :: grid_type = 0 !< Grid type. 0 for a gnomonic grid. | ||
|
||
real :: stretch_fac = 1.0 !< Grid stretching factor. | ||
real :: res = 48. !< The 'CRES' resolution. | ||
|
||
contains | ||
|
||
!> Read the program namelist file. Then, write the namelist | ||
!! variables to standard output. | ||
!! | ||
!! @author GFDL Programmer | ||
subroutine read_namelist | ||
|
||
implicit none | ||
|
||
integer :: stdunit = 6, unit=7, io_status | ||
logical :: opened | ||
|
||
namelist /filter_topo_nml/ topo_file, topo_field, mask_field, grid_file, zero_ocean, & | ||
stretch_fac, res, nested, grid_type, regional | ||
|
||
do | ||
inquire( unit=unit, opened=opened ) | ||
if( .NOT.opened )exit | ||
unit = unit + 1 | ||
if( unit.EQ.100 )call handle_err(-1, 'Unable to locate unit number.' ) | ||
end do | ||
|
||
open( unit=unit, file='input.nml', iostat=io_status ) | ||
read( unit,filter_topo_nml, iostat=io_status ) | ||
close(unit) | ||
|
||
if (io_status > 0) call handle_err(-1, 'Error reading input.nml') | ||
|
||
write (stdunit, nml=filter_topo_nml) | ||
|
||
end subroutine read_namelist | ||
|
||
!> Prints an error message to standard output, | ||
!! then halts program execution with a | ||
!! bad status. | ||
!! | ||
!! @param[in] status Error status code. | ||
!! @param[in] string Error message. | ||
!! @author GFDL Programmer | ||
subroutine handle_err(status, string) | ||
|
||
implicit none | ||
|
||
#include <netcdf.inc> | ||
|
||
integer, intent(in) :: status | ||
character(len=*), intent(in) :: string | ||
character(len=256) :: errmsg | ||
|
||
if (status .ne. nf_noerr) then | ||
errmsg = nf_strerror(status) | ||
errmsg = trim(errmsg) // " " // trim(string) | ||
print *, "FATAL ERROR:" | ||
print *, trim(errmsg) | ||
error stop 'Stopped' | ||
endif | ||
|
||
end subroutine handle_err | ||
|
||
end module utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This is the cmake build file for the tests directory of the | ||
# UFS_UTILS project. | ||
# | ||
# George Gayno, Ed Hartnett | ||
|
||
if(CMAKE_Fortran_COMPILER_ID MATCHES "^(Intel)$") | ||
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -r8 -assume byterecl") | ||
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$") | ||
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-0 -fdefault-real-8") | ||
endif() | ||
|
||
include_directories(${PROJECT_SOURCE_DIR}) | ||
|
||
# Copy necessary test files from the source data directory to the | ||
# build data directory. | ||
execute_process( COMMAND ${CMAKE_COMMAND} -E copy | ||
${CMAKE_CURRENT_SOURCE_DIR}/data/input.nml ${CMAKE_CURRENT_BINARY_DIR}/input.nml) | ||
execute_process( COMMAND ${CMAKE_COMMAND} -E copy | ||
${CMAKE_CURRENT_SOURCE_DIR}/LSanSuppress.supp ${CMAKE_CURRENT_BINARY_DIR}/LSanSuppress.supp) | ||
|
||
add_executable(ftst_read_filter_topo_nml ftst_readnml.F90) | ||
add_test(NAME filter_topo-ftst_read_namelist COMMAND ftst_read_filter_topo_nml) | ||
target_link_libraries(ftst_read_filter_topo_nml filter_topo_lib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
leak:ESMCI | ||
leak:esmf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
&filter_topo_nml | ||
topo_file="/dir1/dir2/orography.nc" | ||
topo_field="orog_filter" | ||
mask_field="landmask" | ||
grid_file="/dir1/dir2/dir3/mosaic.nc" | ||
zero_ocean=.false. | ||
stretch_fac=2.0 | ||
res=96. | ||
nested=.true. | ||
grid_type=1 | ||
regional=.true. | ||
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
! Unit test for filter_topo routine "read_namelist". | ||
! | ||
! Reads a sample namelist from file input.nml. | ||
! If any namelist variable does not match expected values, | ||
! the test fails. | ||
! | ||
! Author George Gayno 7/23/2021 | ||
|
||
program readnml | ||
|
||
use utils | ||
|
||
implicit none | ||
|
||
print*, "Starting test of filter_topo routine read_namelist." | ||
print*, "Testing with file input.nml..." | ||
|
||
call read_namelist() | ||
|
||
if (trim(topo_file) /= "/dir1/dir2/orography.nc") stop 2 | ||
if (trim(topo_field) /= "orog_filter") stop 4 | ||
if (trim(mask_field) /= "landmask") stop 6 | ||
if (trim(grid_file) /= "/dir1/dir2/dir3/mosaic.nc") stop 8 | ||
if (zero_ocean) stop 10 | ||
if (stretch_fac /= 2.0) stop 14 | ||
if (res /= 96.0) stop 14 | ||
if (.not. nested) stop 16 | ||
if (grid_type /= 1) stop 18 | ||
if (.not. regional) stop 20 | ||
|
||
print*, "OK" | ||
print*, "SUCCESS!" | ||
|
||
end program readnml |