module wf_util
  use sep
  implicit none

  !----------------------------------------------------------------
  type, public        :: axis
     integer          :: n
     real             :: o,d
     character(len=2) :: l
  end type axis
  !----------------------------------------------------------------
  integer, external :: auxin
contains
  !----------------------------------------------------------------
  !! load axis info from a header file
  subroutine hinaxis(file,a,i)
    character(len=*), intent(in)  :: file
    integer,          intent(in)  :: i
    type(axis),       intent(out) :: a
    character(len=128)            :: buf

    write(buf,"(a,i1)") 'n',i; call from_aux(trim(file),trim(buf),a%n,1 )
    write(buf,"(a,i1)") 'o',i; call from_aux(trim(file),trim(buf),a%o,0.)
    write(buf,"(a,i1)") 'd',i; call from_aux(trim(file),trim(buf),a%d,1.)
  end subroutine hinaxis
  !----------------------------------------------------------------
  !! write axis info to a header file
  subroutine houaxis(file,a,i)
    character(len=*), intent(in)  :: file
    integer,          intent(in)  :: i
    type(axis),       intent(in)  :: a
    character(len=128)            :: buf

    write(buf, "(a,i1)") 'n'    ,i; call to_history(trim(buf),     a%n, trim(file))
    write(buf, "(a,i1)") 'o'    ,i; call to_history(trim(buf),     a%o, trim(file))
    write(buf, "(a,i1)") 'd'    ,i; call to_history(trim(buf),     a%d, trim(file))
    write(buf, "(a,i1)") 'label',i; call to_history(trim(buf),trim(a%l),trim(file))
  end subroutine houaxis
  !----------------------------------------------------------------
  !! read axis info from an aux file
  subroutine ainaxis(file,a,i)
    character(len=*), intent(in)  :: file
    integer,          intent(in)  :: i
    type(axis),       intent(in)  :: a
    character(len=128)            :: buf

    write(buf, "(a,i1)") 'n'    ,i; call auxpar(trim(buf),'i',a%n, trim(file))
    write(buf, "(a,i1)") 'o'    ,i; call auxpar(trim(buf),'f',a%o, trim(file))
    write(buf, "(a,i1)") 'd'    ,i; call auxpar(trim(buf),'f',a%d, trim(file))
    write(buf, "(a,i1)") 'label',i; call auxpar(trim(buf),'s',a%l, trim(file))
  end subroutine ainaxis
  !----------------------------------------------------------------
  !! write axis info to an aux file
  subroutine aouaxis(file,a,i)
    character(len=*), intent(in)  :: file
    integer,          intent(in)  :: i
    type(axis),       intent(in)  :: a
    character(len=128)            :: buf

    write(buf, "(a,i1)") 'n'    ,i; call auxputch(trim(buf),'i',     a%n, trim(file))
    write(buf, "(a,i1)") 'o'    ,i; call auxputch(trim(buf),'f',     a%o, trim(file))
    write(buf, "(a,i1)") 'd'    ,i; call auxputch(trim(buf),'f',     a%d, trim(file))
    write(buf, "(a,i1)") 'label',i; call auxputch(trim(buf),'s',trim(a%l),trim(file))
  end subroutine aouaxis
  !----------------------------------------------------------------  
  !! load axis info from a parameter file
  subroutine pinaxis(a)
    type(axis),       intent(out) :: a
    character(len=128)            :: buf

    write(buf,"('a',a,a)") trim(a%l),'_n'
    call from_param(trim(buf),a%n,1  )
    write(buf,"('a',a,a)") trim(a%l),'_o'
    call from_param(trim(buf),a%o,0.0)
    write(buf,"('a',a,a)") trim(a%l),'_d'
    call from_param(trim(buf),a%d,1.0)

  end subroutine pinaxis
  !----------------------------------------------------------------  
  subroutine printaxis(a)
    type(axis),       intent(in)  :: a
    write(0,*) "n=",a%n,"o=",a%o,"d=",a%d,"l=",a%l
  end subroutine printaxis
  !----------------------------------------------------------------  
  subroutine matchaxis(a1,a2)
    type(axis) :: a1,a2

    if(a1%n /= a2%n) call seperr("axis mismatch")

    if(a1%n /= 1) then
       if(a1%o /= a2%o .or. a1%d /= a2%d) then
          call printaxis(a1)
          call printaxis(a2)
          call seperr("axis mismatch")
       end if
    end if
  end subroutine matchaxis
  !----------------------------------------------------------------
  subroutine wfline()
    write(0,'(a)')'================================================'
  end subroutine wfline
  !----------------------------------------------------------------
  !! check if a tag is associated with an actual file on the disk
  logical function wfisfile(tag) result(is)
    character(len=128), intent(in) :: tag

    is = auxin(trim(tag)) /=-1
    call auxclose(tag)
  end function wfisfile
  !----------------------------------------------------------------
end module wf_util













