Skip to content

Commit

Permalink
+Partial consolidation of netcdf calls in MOM_io
Browse files Browse the repository at this point in the history
  Took preliminary steps to consolidate the direct calls to netcdf in
MOM_io.F90. Renamed check_grid_def to verify_variable_units and moved it to
MOM_io, where it can be used more widely than just in MOM_regridding.F90.  Also
split get_var_sizes and get_varid out of num_timelevels and made them publicly
visible.  All answers are bitwise identical, but there are new public interfaces
and the renaming of one routine.
  • Loading branch information
Hallberg-NOAA committed Jan 28, 2021
1 parent aee4df2 commit 2f5a0c8
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 133 deletions.
61 changes: 2 additions & 59 deletions src/ALE/MOM_regridding.F90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module MOM_regridding
use MOM_error_handler, only : MOM_error, FATAL, WARNING
use MOM_file_parser, only : param_file_type, get_param, log_param
use MOM_io, only : file_exists, field_exists, field_size, MOM_read_data
use MOM_io, only : slasher
use MOM_io, only : verify_variable_units, slasher
use MOM_unit_scaling, only : unit_scale_type
use MOM_variables, only : ocean_grid_type, thermo_var_ptrs
use MOM_verticalGrid, only : verticalGrid_type
Expand All @@ -30,9 +30,6 @@ module MOM_regridding
use coord_slight, only : init_coord_slight, slight_CS, set_slight_params, build_slight_column, end_coord_slight
use coord_adapt, only : init_coord_adapt, adapt_CS, set_adapt_params, build_adapt_column, end_coord_adapt

! Direct netcdf calls are used by check_grid_def()
use netcdf, only : NF90_open, NF90_inq_varid, NF90_get_att, NF90_NOERR, NF90_NOWRITE

implicit none ; private

#include <MOM_memory.h>
Expand Down Expand Up @@ -371,7 +368,7 @@ subroutine initialize_regridding(CS, GV, US, max_depth, param_file, mdl, coord_m
endif
if (index(trim(varName),'interfaces=')==1) then
varName=trim(varName(12:))
call check_grid_def(filename, varName, expected_units, message, ierr)
call verify_variable_units(filename, varName, expected_units, message, ierr)
if (ierr) call MOM_error(FATAL,trim(mdl)//", initialize_regridding: "//&
"Unsupported format in grid definition '"//trim(filename)//"'. Error message "//trim(message))
call field_size(trim(fileName), trim(varName), nzf)
Expand Down Expand Up @@ -734,61 +731,7 @@ subroutine initialize_regridding(CS, GV, US, max_depth, param_file, mdl, coord_m
if (allocated(dz)) deallocate(dz)
end subroutine initialize_regridding

!> Do some basic checks on the vertical grid definition file, variable
subroutine check_grid_def(filename, varname, expected_units, msg, ierr)
character(len=*), intent(in) :: filename !< File name
character(len=*), intent(in) :: varname !< Variable name
character(len=*), intent(in) :: expected_units !< Expected units of variable
character(len=*), intent(inout) :: msg !< Message to use for errors
logical, intent(out) :: ierr !< True if an error occurs
! Local variables
character (len=200) :: units, long_name
integer :: ncid, status, intid, vid
integer :: i

ierr = .false.
status = NF90_OPEN(trim(filename), NF90_NOWRITE, ncid)
if (status /= NF90_NOERR) then
ierr = .true.
msg = 'File not found: '//trim(filename)
return
endif

status = NF90_INQ_VARID(ncid, trim(varname), vid)
if (status /= NF90_NOERR) then
ierr = .true.
msg = 'Var not found: '//trim(varname)
return
endif

status = NF90_GET_ATT(ncid, vid, "units", units)
if (status /= NF90_NOERR) then
ierr = .true.
msg = 'Attribute not found: units'
return
endif
! NF90_GET_ATT can return attributes with null characters, which TRIM will not truncate.
! This loop replaces any null characters with a space so that the following check between
! the read units and the expected units will pass
do i=1,LEN_TRIM(units)
if (units(i:i) == CHAR(0)) units(i:i) = " "
enddo

if (trim(units) /= trim(expected_units)) then
if (trim(expected_units) == "meters") then
if (trim(units) /= "m") then
ierr = .true.
endif
else
ierr = .true.
endif
endif

if (ierr) then
msg = 'Units incorrect: '//trim(units)//' /= '//trim(expected_units)
endif

end subroutine check_grid_def

!> Deallocation of regridding memory
subroutine end_regridding(CS)
Expand Down
Loading

0 comments on commit 2f5a0c8

Please sign in to comment.