Skip to content

Commit

Permalink
Add better error handling to read_var_sizes
Browse files Browse the repository at this point in the history
  Add better error handling to read_var_sizes when a missing file or missing
variable is provided as an argument.  Without this change the model fails with a
segmentation fault on line 768 of MOM_io.F90 if a bad file or variable name is
provided.  With this change, a useful error message is returned.  All answers
are bitwise identical in all cases that worked previously.
  • Loading branch information
Hallberg-NOAA authored and marshallward committed Apr 21, 2023
1 parent f9897c8 commit 7225642
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/framework/MOM_io.F90
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,13 @@ function num_timelevels(filename, varname, min_dims) result(n_time)

call get_var_sizes(filename, varname, ndims, sizes, match_case=.false., caller="num_timelevels")

n_time = sizes(ndims)
if (ndims > 0) n_time = sizes(ndims)

if (present(min_dims)) then
if (ndims < min_dims-1) then
write(msg, '(I3)') min_dims
call MOM_error(WARNING, "num_timelevels: variable "//trim(varname)//" in file "//&
trim(filename)//" has fewer than min_dims = "//trim(msg)//" dimensions.")
trim(filename)//" has fewer than min_dims = "//trim(msg)//" dimensions.")
n_time = -1
elseif (ndims == min_dims - 1) then
n_time = 0
Expand Down Expand Up @@ -861,12 +861,18 @@ subroutine read_var_sizes(filename, varname, ndims, sizes, match_case, caller, d
ncid = ncid_in
else
call open_file_to_read(filename, ncid, success=success)
if (.not.success) return
if (.not.success) then
call MOM_error(WARNING, "Unsuccessfully attempted to open file "//trim(filename))
return
endif
endif

! Get the dimension sizes of the variable varname.
call get_varid(varname, ncid, filename, varid, match_case=match_case, found=found)
if (.not.found) return
if (.not.found) then
call MOM_error(WARNING, "Could not find variable "//trim(varname)//" in file "//trim(filename))
return
endif

status = NF90_inquire_variable(ncid, varid, ndims=ndims)
if (status /= NF90_NOERR) then
Expand Down

0 comments on commit 7225642

Please sign in to comment.