subroutine nbi_cdf4s(ilun, action, zname, zdata, ndim, zvartype)
 
  use ezcdf
 
  !  read/write 4d data as 1d array from/to netcdf file (via ezcdf)
  !  note: on write, prior cdfdefvar call has to have been arranged by caller
 
  !  expecting to read real*8, but can handle old files containing real*4...
 
  use iso_c_binding, only: fp => c_double
  implicit none
 
  integer, intent(in) :: ilun        ! netcdf channel id
  character*1, intent(in) :: action  ! 'r' for read, 'w' for write
  character*(*), intent(in) :: zname ! name of item
  integer, intent(in) :: ndim        ! dimension of data as 1d array
 
  real(fp) :: zdata(ndim)                 ! the data (in or out dep. on action)

  character*(*), intent(in) :: zvartype  ! "r8" or "r4" according to what is
                                     ! in the file...
 
  real, dimension(:), allocatable :: zbufr4
  ! -----------------------------------------------
 
  if(action.eq.'R') then
 
     if(zvartype.eq.'R8') then
        call cdfgetvar(ilun,zname,zdata)
     else
        allocate(zbufr4(ndim))
        call cdfgetvar(ilun,zname,zbufr4)
        zdata=zbufr4
        deallocate(zbufr4)
     endif
 
  else if(action.eq.'W') then
 
     call cdfputvar(ilun,zname,zdata)
 
  else
     call nbi_message(' ?nbi_cdf4s:  illegal action code:  '//action)
  endif
 
end subroutine nbi_cdf4s
