module n3newapi
 
  implicit none

  interface nus_read_2d
    module procedure nus_read_2d_i
    module procedure nus_read_2d_r
    module procedure nus_read_2d_d
  end interface

  interface nus_read_meridional
    module procedure nus_read_meridional_r

  end interface

  ! The interface of NUS_READ_2D_PACKED must be implicit,
  ! because the 5th argument value is used as various types.
  ! (Note that dummy arguments cannot be EQUIVALENCEd)
  external nus_read_2d_packed

  integer, parameter:: PACKINFOLEN = 1  ! TO BE MOVED

contains

  subroutine nus_read_2d_i(type1, type2, type3, basetime, m, v, e, z, &
  & value, vsize, iostat)
    character(*), intent(in):: type1, type2, type3, m, e, z
    integer, intent(in):: basetime, v, vsize
    integer, intent(out):: value(vsize)
    integer, intent(out):: iostat
    integer:: hshot
    integer:: packinfo(PACKINFOLEN)
    call nus_find_shot(type1, type2, type3, basetime, hshot, iostat)
    if (iostat /= 0) return
    call nus_read_2d_packed(hshot, m, v, e, z, value, vsize, packinfo, iostat)
    if (iostat /= 0) return
    call nus_unpack_i(value, vsize, packinfo, iostat)
    if (iostat /= 0) return
  end subroutine

  subroutine nus_read_2d_r(type1, type2, type3, basetime, m, v, e, z, &
  & value, vsize, iostat)
    character(*), intent(in):: type1, type2, type3, m, e, z
    integer, intent(in):: basetime, v, vsize
    real, intent(out):: value(vsize)
    integer, intent(out):: iostat
    integer:: hshot
    integer:: packinfo(PACKINFOLEN)
    call nus_find_shot(type1, type2, type3, basetime, hshot, iostat)
    if (iostat /= 0) return
    call nus_read_2d_packed(hshot, m, v, e, z, value, vsize, packinfo, iostat)
    if (iostat /= 0) return
    call nus_unpack_r(value, vsize, packinfo, iostat)
    if (iostat /= 0) return
  end subroutine

  subroutine nus_read_2d_d(type1, type2, type3, basetime, m, v, e, z, &
  & value, vsize, iostat)
    character(*), intent(in):: type1, type2, type3, m, e, z
    integer, intent(in):: basetime, v, vsize
    double precision, intent(out):: value(vsize)
    integer, intent(out):: iostat
    integer:: hshot
    integer:: packinfo(PACKINFOLEN)
    call nus_find_shot(type1, type2, type3, basetime, hshot, iostat)
    if (iostat /= 0) return
    call nus_read_2d_packed(hshot, m, v, e, z, value, vsize, packinfo, iostat)
    if (iostat /= 0) return
    call nus_unpack_d(value, vsize, packinfo, iostat)
    if (iostat /= 0) return
  end subroutine

  subroutine nus_read_meridional_r(type1, type2, type3, basetime, &
    & m, v, e, iy, value, vsize, iostat)
    implicit none
    character(*), intent(in):: type1, type2, type3, m, e
    integer, intent(in):: basetime, v, iy, vsize
    real, intent(out):: value(vsize)
    integer, intent(out):: iostat
  end subroutine

end module
