!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module cubego_browse
  use cubetools_parameters
  use cubetools_structure
  use cube_types
  use cubeadm_cubeid_types
  use cubesyntax_keyval_strg_types
  use cubetools_switch_types
  use cubetopology_spapos_types
  use cubego_messaging
  !
  public :: browse
  private
  !
  type :: browse_comm_t
     type(option_t),     pointer :: comm
     type(cubeid_arg_t), pointer :: cube
     type(spapos_comm_t)         :: pixel
     type(keyval_strg_comm_t)    :: catalog
     type(switch_comm_t)         :: interactive
   contains
     procedure, public  :: register => cubego_browse_register
     procedure, private :: parse    => cubego_browse_parse
     procedure, private :: main     => cubego_browse_main
  end type browse_comm_t
  type(browse_comm_t) :: browse
  !
  type :: browse_user_t
     type(cubeid_user_t)      :: cubeids
     type(spapos_user_t)      :: pixel
     type(keyval_strg_user_t) :: catalog
     type(switch_user_t)      :: interactive
   contains
     procedure, private :: toprog => cubego_browse_user_toprog
  end type browse_user_t
  !
  type browse_prog_t
     type(cube_t), pointer :: cube
     type(cubeid_prog_t)   :: cubeids
     type(spapos_prog_t)   :: pixel
     character(len=file_l) :: catalog
     type(switch_prog_t)   :: interactive
   contains
     procedure, private :: header => cubego_browse_prog_header
     procedure, private :: data   => cubego_browse_prog_data
  end type browse_prog_t
  !
contains
  !
  subroutine cubego_browse_command(line,error)
    use gkernel_interfaces
    !---------------------------------------------------------------------
    !
    !---------------------------------------------------------------------
    character(len=*), intent(in)    :: line
    logical,          intent(inout) :: error
    !
    type(browse_user_t) :: user
    character(len=*),parameter :: rname='BROWSE>COMMAND'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call browse%parse(line,user,error)
    if (error) return
    call browse%main(user,error)
    if (error) return
    ! Force insertion in SIC stack ! *** JP Why?
    if (sic_lire().eq.0) call sic_insert(line)
  end subroutine cubego_browse_command
  !
  !----------------------------------------------------------------------
  !
  subroutine cubego_browse_register(comm,error)
    use cubedag_allflags
    !---------------------------------------------------------------------
    ! 
    !---------------------------------------------------------------------
    class(browse_comm_t), intent(inout) :: comm
    logical,              intent(inout) :: error
    !
    type(cubeid_arg_t) :: cubearg
    character(len=*), parameter :: rname='BROWSE>REGISTER'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call cubetools_register_command(&
         'BROWSE','[cubeid]',&
         'Interactive browse of a cube spectrum',&
         'Open the spectrum browser for one cube.',&
         cubego_browse_command,&
         comm%comm,&
         error)
    if (error) return
    call cubearg%register(&
         'CUBEID',&
         'Identity of the cube to be browsed',&
         'If omitted, use the last matching cube (any family/flags) in&
         & the default index (DAG by default; see SET\INDEX).',&
         code_arg_optional,&
         [flag_any],&
         code_read,&! Indicative: data access done by procedure
         code_access_imaset_or_speset,&! Indicative: data access done by procedure
         comm%cube,&
         error)
    if (error) return
    !
    call comm%pixel%register(&
         'PIXEL',&
         'Define the position of the pixel to be browsed',&
         error)
    if (error) return
    call comm%catalog%register(&
         'CATALOG',&
         'Supersede the default line catalog filename',&
         'The file format is an ASCII table with frequencies [GHz] in first&
         & column and line names in second column. Protect names with single&
         & quotes if needed.',&
         strg_star,&
         strg_emp,&
         error)
    if (error) return
    call comm%interactive%register(&
         'INTERACTIVE',&
         'the interactive mode',&
         'ON',&
         strg_emp,&
         error)
    if (error) return
  end subroutine cubego_browse_register
  !
  subroutine cubego_browse_parse(comm,line,user,error)
    !---------------------------------------------------------------------
    ! 
    !---------------------------------------------------------------------
    class(browse_comm_t), intent(inout) :: comm
    character(len=*),     intent(in)    :: line
    type(browse_user_t),  intent(out)   :: user
    logical,              intent(inout) :: error
    !
    character(len=*), parameter :: rname='BROWSE>PARSE'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call cubeadm_cubeid_parse(line,comm%comm,user%cubeids,error)
    if (error) return
    call comm%pixel%parse(line,user%pixel,error)
    if (error) return
    call comm%catalog%parse(line,user%catalog,error)
    if (error) return
    call comm%interactive%parse(line,user%interactive,error)
    if (error) return
  end subroutine cubego_browse_parse
  !
  subroutine cubego_browse_main(comm,user,error)
    !---------------------------------------------------------------------
    ! 
    !---------------------------------------------------------------------
    class(browse_comm_t), intent(in)    :: comm
    type(browse_user_t),  intent(in)    :: user
    logical,              intent(inout) :: error
    !
    type(browse_prog_t) :: prog
    character(len=*), parameter :: rname='BROWSE>MAIN'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call user%toprog(comm,prog,error)
    if (error) return
    call prog%header(error)
    if (error) return
    call prog%data(error)
    if (error) return
  end subroutine cubego_browse_main
  !
  !----------------------------------------------------------------------
  !
  subroutine cubego_browse_user_toprog(user,comm,prog,error)
    use cubeadm_get
    !----------------------------------------------------------------------
    !
    !----------------------------------------------------------------------
    class(browse_user_t), intent(in)    :: user
    type(browse_comm_t),  intent(in)    :: comm    
    type(browse_prog_t),  intent(out)   :: prog
    logical,              intent(inout) :: error
    !
    character(len=*), parameter :: rname='BROWSE>USER>TOPROG'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call cubeadm_get_header(comm%cube,user%cubeids,prog%cube,error)
    if (error) return
    call cubeadm_cubeid_user2prog(comm%comm,user%cubeids,prog%cubeids,error)
    if (error) return
    call user%pixel%toprog(prog%cube,prog%pixel,error)
    if (error) return
    call user%catalog%toprog(comm%catalog,prog%catalog,error)
    if (error) return
    call prog%interactive%init(comm%interactive,error)
    if (error) return
    call user%interactive%toprog(comm%interactive,prog%interactive,error)
    if (error) return
  end subroutine cubego_browse_user_toprog
  !
  !----------------------------------------------------------------------
  !
  subroutine cubego_browse_prog_header(prog,error)
    use cubego_buffer
    use cubetools_userspace
    use cubetools_userstruct
    !---------------------------------------------------------------------
    !
    !---------------------------------------------------------------------
    class(browse_prog_t), intent(inout) :: prog
    logical,              intent(inout) :: error
    !
    type(userstruct_t) :: userstruct
    character(len=*), parameter :: rname='BROWSE>PROG>HEADER'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call prog%cubeids%defstruct(cubego,error)
    if (error) return
    call prog%pixel%userstruct('cubego%pixel',error)
    if (error) return
    ! catalog
    call userstruct%create('cubego%catalog',global,overwrite_prog,error)
    if (error) return
    call userstruct%set_member('file',prog%catalog,error)
    if (error) return
  end subroutine cubego_browse_prog_header
  !
  subroutine cubego_browse_prog_data(prog,error)
    use gkernel_interfaces
    !---------------------------------------------------------------------
    !
    !---------------------------------------------------------------------
    class(browse_prog_t), intent(in)    :: prog
    logical,              intent(inout) :: error
    !
    character(len=*), parameter :: rname='BROWSE>PROG>DATA'
    !
    call cubego_message(goseve%trace,rname,'Welcome')
    !
    call exec_program('@ q-browse.cube')
    if (prog%interactive%act) call exec_program('@ l-ctrl-master-loop')
  end subroutine cubego_browse_prog_data
end module cubego_browse
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
