!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module cubefield_language
  use cubetools_parameters
  use cubetools_structure
  use cubefield_messaging
  !
  use cubefield_divergence
  use cubefield_gradient
  use cubefield_gradthin
  use cubefield_hessian
  use cubefield_incline
  use cubefield_minmax
  use cubefield_observe
  use cubefield_sht
  use cubefield_rotate
  !
  public :: cubefield_register_language
  private
  !
  integer(kind=lang_k) :: langid
  !
contains
  !
  subroutine cubefield_register_language(error)
    !----------------------------------------------------------------------
    ! Register the FIELD\ language
    !----------------------------------------------------------------------
    logical, intent(inout) :: error
    !
    call cubetools_register_language(&
         'FIELD',&
         'J.Pety, S.Bardeau, V.deSouzaMagalhaes',&
         'Analyze, transform, and project scalar or vector fields',&
         'FIELD lets you derive, transform, and project scalar or vector&
         & fields stored in images or cubes.'//strg_bl//&
         'Behavior:'//strg_cr//&
         '- GRADIENT writes DF_DX, DF_DY, AMPLITUDE, and ANGLE from a&
         & 2D field, then DIVERGENCE and HESSIAN turn those gradient&
         & products into second-order diagnostics.'//strg_cr//&
         '- GRADTHIN and SHT consume gradient amplitude/angle products&
         & to build thinned-edge masks and Hough-transform&
         & accumulators.'//strg_cr//&
         '- INCLINE and OBSERVE operate on the same 3D density/velocity&
         & cube quartet: INCLINE rotates the state cubes and OBSERVE&
         & projects them into COLDENS and VCENTROID maps.'//strg_cr//&
         '- MINMAX detects strict local minima/maxima in image slices,&
         & and ROTATE applies a local angle-field rotation to 2D vector&
         & components.'//strg_bl//&
         'Typical workflow:'//strg_cr//&
         'CUBE> FIELD\GRADIENT field'//strg_cr//&
         'CUBE> FIELD\GRADTHIN gradamp gradang'//strg_cr//&
         'CUBE> FIELD\SHT gradamp gradang'//strg_cr//&
         'CUBE> FIELD\INCLINE voldens vx vy vz /ANGLE 30deg'//strg_cr//&
         'CUBE> FIELD\OBSERVE voldens vx vy vz'//strg_bl//&
         'Note:'//strg_cr//&
         ' Shared region help is available on /SIZE ?, /CENTER ?, and&
         & /RANGE ? where supported.',&
         'gag_doc:hlp/cube-help-field.hlp',&
         cubefield_execute_command,&
         langid,&
         error)
    if (error) return
    !----------------------------------------------------------------------
    !
    call divergence%register(error)
    if (error) return
    call gradient%register(error)
    if (error) return
    call gradthin%register(error)
    if (error) return
    call hessian%register(error)
    if (error) return
    call incline%register(error)
    if (error) return
    call cubefield_minmax_register(error)
    if (error) return
    call observe%register(error)
    if (error) return
    call sht%register(error)
    if (error) return
    call rotate%register(error)
    if (error) return
    !
    call cubetools_register_dict(error)
    if (error) return
  end subroutine cubefield_register_language
  !
  subroutine cubefield_execute_command(line,comm,error)
    use cubeadm_opened
    use cubeadm_timing
    !----------------------------------------------------------------------
    ! Execute a command of the FIELD\ language
    !----------------------------------------------------------------------
    character(len=*), intent(in)    :: line
    character(len=*), intent(in)    :: comm
    logical,          intent(inout) :: error
    !
    character(len=*), parameter :: rname='EXECUTE>COMMAND'
    !
    error = .false.
    if (comm.eq.'FIELD?') then
       call cubetools_list_language_commands(langid,error)
       if (error) return
    else
       call cubeadm_timing_init()
       call cubetools_execute_command(line,langid,comm,error)
       if (error) continue ! To ensure error recovery in the call to cubeadm_finish_all
       call cubeadm_finish_all(comm,line,error)
       if (error) continue ! To ensure error recovery in timing
       call cubeadm_timing_final(comm)
    endif
  end subroutine cubefield_execute_command
end module cubefield_language
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
