Skip to content

Commit

Permalink
Fix reading and writing of 1-d logical global arrays
Browse files Browse the repository at this point in the history
I copied the implementation of ncd_io_{DIMS}d_{TYPE}_glob to
ncd_io_{DIMS}d_log_glob (with appropriate adjustments for the fact that
we're working with logical data). By looking at diffs between those
routines, I have confirmed that the two are now essentially identical in
their implementation.

I also made some very minor tweaks to ncd_io_{DIMS}d_{TYPE}_glob in
order to reconcile the diffs between these two routines.

This fixes ESCOMP#24 for real.
  • Loading branch information
billsacks committed Oct 28, 2018
1 parent 1ad136c commit 77e1777
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/main/ncdio_pio.F90.in
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ contains
subroutine ncd_io_{DIMS}d_log_glob(varname, data, flag, ncid, readvar, nt, posNOTonfile)
!
! !DESCRIPTION:
! netcdf I/O of global integer variable
! netcdf I/O of global logical variable
!
! !ARGUMENTS:
class(file_desc_t) , intent(inout) :: ncid ! netcdf file id
Expand All @@ -1212,10 +1212,12 @@ contains
integer :: start(2), count(2) ! output bounds
integer :: status ! error code
logical :: varpresent ! if true, variable is on tape
logical :: found ! if true, found lat/lon dims on file
integer :: idata
integer, pointer :: idata1d(:) ! Temporary integer data to send to file
character(len=32) :: vname ! variable error checking
type(var_desc_t) :: vardesc ! local vardesc pointer
integer :: ndims
character(len=*),parameter :: subname='ncd_io_{DIMS}d_log_glob'
!-----------------------------------------------------------------------

Expand All @@ -1225,48 +1227,73 @@ contains
if (flag == 'read') then

call ncd_inqvid(ncid, varname, varid, vardesc, readvar=varpresent)

call pio_seterrorhandling(ncid, PIO_BCAST_ERROR)
#if ({DIMS}==0)
if (varpresent) then
status = pio_get_var(ncid, vardesc, idata)
if (single_column .and. present(posNOTonfile) ) then
if ( .not. posNOTonfile )then
call shr_sys_abort(' ERROR: scalar var is NOT compatable with posNOTonfile = .false.'//&
errMsg(sourcefile, __LINE__))
end if
endif
#if ({DIMS}==0)
status = pio_get_var(ncid, varid, idata)
if ( status /= PIO_NOERR )then
call shr_sys_abort(' ERROR: reading variable: '//trim(varname)//' ' &
// errMsg(sourcefile, __LINE__))
end if
if ( idata == 0 )then
data = .false.
else if ( idata == 1 )then
data = .true.
else
call shr_sys_abort(' ERROR: bad integer value for logical data'//errMsg(sourcefile, __LINE__))
end if
end if
#else
if (varpresent) then
allocate(idata1d(size(data)))
data = (idata1d == 1)
if (single_column) then
call scam_field_offsets(ncid,'undefined', vardesc,&
start, count, found=found, posNOTonfile=posNOTonfile)
if ( found )then
status = pio_get_var(ncid, varid, start, count, idata1d)
else
status = pio_get_var(ncid, varid, idata1d)
end if
else
status = pio_get_var(ncid, varid, idata1d)
endif
if ( status /= PIO_NOERR )then
call shr_sys_abort(' ERROR: reading variable: '//trim(varname)//' ' &
// errMsg(sourcefile, __LINE__))
end if
if ( any(idata1d /= 0 .and. idata1d /= 1) )then
call shr_sys_abort(' ERROR: read in bad integer value(s) for logical data'//errMsg(sourcefile, __LINE__))
end if
data = (idata1d == 1)
deallocate(idata1d)
#endif
endif
#endif
call pio_seterrorhandling(ncid, PIO_INTERNAL_ERROR)
if (present(readvar)) readvar = varpresent

elseif (flag == 'write') then

ndims = {DIMS}
if(present(nt)) ndims=ndims+1
call ncd_inqvid (ncid, varname, varid, vardesc)
#if ({DIMS}==0)
start(1) = 1 ; count(1) = 1
if (present(nt)) start(1) = nt
call ncd_inqvid (ncid, varname, varid, vardesc)
allocate(idata1d(1))
if ( data )then
idata1d(1) = 1
else
idata1d(1) = 0
end if
status = pio_put_var(ncid, varid, start, count, idata1d)
status = pio_put_var(ncid, varid, start(1:1), count(1:1), idata1d)
deallocate(idata1d)
#else
#elif ({DIMS}==1)
start(1) = 1 ; count(1) = size(data)
start(2) = 1 ; count(2) = 1
if (present(nt)) start(2) = nt
Expand All @@ -1276,8 +1303,7 @@ contains
elsewhere
idata1d = 0
end where
call ncd_inqvid (ncid, varname, varid, vardesc)
status = pio_put_var(ncid, varid, start, count, idata1d)
status = pio_put_var(ncid, varid, start(1:ndims), count(1:ndims), idata1d)
deallocate( idata1d )
#endif

Expand All @@ -1303,17 +1329,16 @@ contains
logical , optional, intent(in) :: posNOTonfile ! position is NOT on this file
!
! !LOCAL VARIABLES:
integer :: m
integer :: varid ! netCDF variable id
integer :: start({DIMS}+1), count({DIMS}+1) ! output bounds
integer :: status ! error code
logical :: varpresent ! if true, variable is on tape
logical :: found ! if true, found lat/lon dims on file
character(len=32) :: vname ! variable error checking
type(var_desc_t) :: vardesc ! local vardesc pointer
{VTYPE} :: temp(1)
integer :: ndims
{VTYPE} :: temp(1)
character(len=*),parameter :: subname='ncd_io_{DIMS}d_{TYPE}_glob'
integer :: ndims
!-----------------------------------------------------------------------

start(:) = 0
Expand Down Expand Up @@ -1366,7 +1391,7 @@ contains
call ncd_inqvid (ncid, varname, varid, vardesc)
#if ({DIMS}==0)
start(1) = 1 ; count(1) = 1
if (present(nt)) start(1) = nt ; count(1) = 1
if (present(nt)) start(1) = nt
temp(1) = data
status = pio_put_var(ncid, varid, start(1:1), count(1:1), temp)
#elif ({DIMS}==1)
Expand All @@ -1392,7 +1417,7 @@ contains
end if
#endif

endif
endif ! flag

end subroutine ncd_io_{DIMS}d_{TYPE}_glob

Expand Down

0 comments on commit 77e1777

Please sign in to comment.