!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module cubeio_header_iodesc
  use cubetools_header_interface
  use cubeio_header_interface
  use cubeio_desc
  use cubeio_messaging
  !
  public :: cubeio_header_put_toiodesc
  public :: cubeio_iodesc_import
  public :: cubeio_iodesc_unrecognized
  private
  !
contains
  !
  subroutine cubeio_header_put_toiodesc(dno,order,iodesc,verbose,error)
    use cubedag_node_type
    !-------------------------------------------------------------------
    ! From type(cube_header_t) to type(cubeio_desc_t) with desired order
    !-------------------------------------------------------------------
    class(cubedag_node_object_t), intent(inout) :: dno
    integer(kind=code_k),         intent(in)    :: order  ! code_order_*
    type(cubeio_desc_t),          intent(inout) :: iodesc
    logical,                      intent(in)    :: verbose
    logical,                      intent(inout) :: error
    !
    character(len=*), parameter :: rname='HEADER>PUT'
    !
    call cubeio_message(ioseve%trace,rname,'Welcome')
    !
    call dno%node%head2interf(error)
    if (error) return
    call cubeio_header_interface_transpose(dno%node%head,order,error)
    if (error) return
    call cubeio_iodesc_import(dno%node%head,iodesc,error)
    if (error) return
  end subroutine cubeio_header_put_toiodesc
  !
  !---------------------------------------------------------------------
  !
  subroutine cubeio_iodesc_import(in,iodesc,error)
    use gbl_format
    !-------------------------------------------------------------------
    ! From type(cube_header_interface_t) to type(cubeio_desc_t)
    !-------------------------------------------------------------------
    type(cube_header_interface_t), intent(in)    :: in
    type(cubeio_desc_t),           intent(inout) :: iodesc
    logical,                       intent(inout) :: error
    !
    character(len=*), parameter :: rname='IODESC>IMPORT'
    !
    call cubeio_message(ioseve%trace,rname,'Welcome')
    !
    iodesc%iscplx = in%array_type.eq.fmt_c4
    iodesc%nx = interf_get_dim(in%axset_ix)
    iodesc%ny = interf_get_dim(in%axset_iy)
    iodesc%nc = interf_get_dim(in%axset_ic)
    if (in%axset_ix.eq.1 .and. in%axset_iy.eq.2) then
      iodesc%order = code_cube_imaset
      ! NB: ix/iy/ic might not be 1/2/3 if degenerate dimensions are
      !     interleaved.
      iodesc%i1 = in%axset_ix
      iodesc%i2 = in%axset_iy
      if (in%axset_ic.eq.0) then
        ! Undefined => useless => set a valid dimension for sanity
        iodesc%i3 = in%axset_iy+1
      else
        iodesc%i3 = in%axset_ic
      endif
      iodesc%n1 = iodesc%nx
      iodesc%n2 = iodesc%ny
      iodesc%n3 = iodesc%nc
    elseif (in%axset_ic.eq.1) then
      iodesc%order = code_cube_speset
      iodesc%i1 = in%axset_ic
      if (in%axset_ix.eq.0) then
        ! Undefined => useless => set a valid dimension for sanity
        iodesc%i2 = in%axset_ic+1
      else
        iodesc%i2 = in%axset_ix
      endif
      if (in%axset_iy.eq.0) then
        ! Undefined => useless => set a valid dimension for sanity
        iodesc%i3 = in%axset_ix+1
      else
        iodesc%i3 = in%axset_iy
      endif
      iodesc%n1 = iodesc%nc
      iodesc%n2 = iodesc%nx
      iodesc%n3 = iodesc%ny
    else
      call cubeio_message(seve%w,rname,'Unrecognized order')
      call cubeio_iodesc_unrecognized(iodesc,error)
      if (error)  return
    endif
  contains
    function interf_get_dim(ii)
      integer(kind=data_k) :: interf_get_dim
      integer(kind=ndim_k), intent(in) :: ii
      if (ii.gt.0) then
        interf_get_dim = max(in%axset_dim(ii),1)
      else
        interf_get_dim = 1
      endif
    end function interf_get_dim
  end subroutine cubeio_iodesc_import
  !
  subroutine cubeio_iodesc_unrecognized(iodesc,error)
    !-------------------------------------------------------------------
    ! Set the descriptor as unrecognized order. Mostly unusable, except
    ! for basic operations like HEADER command
    !-------------------------------------------------------------------
    type(cubeio_desc_t), intent(inout) :: iodesc
    logical,             intent(inout) :: error
    !
    iodesc%order = code_cube_unkset
    iodesc%nx = 1
    iodesc%ny = 1
    iodesc%nc = 1
  end subroutine cubeio_iodesc_unrecognized
  !
end module cubeio_header_iodesc
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
