      subroutine label(kistrt,kistop,kfstrt,kfstop,xscale,yscale,
     &     over)
C for putting wavevector and energy labels on plot

      implicit none
      include 'arraysize.inc'		!! only used for physical constants
      include 'spur_color.inc'
      
c... passed variables
      real kistrt(2),kistop(2),kfstrt(2),kfstop(2)
      real xscale,yscale
      logical over

c... local variables

c0111Unused      character*1 key
cNotUsed      integer maxnpts
cNotUsed      parameter (maxnpts=99)
C      integer i
      real kilab(2),qlab(2),kflabstrt(2),kflabstop(2)
      real xbox(4),ybox(4)
      real en,val,truncval
      character*10 evalue
      character*18 elabel
      integer mm,pp,nc
      real qstrt(2),qstop(2),estrt,estop
      real enrnd
      real temp1, temp2
      integer l
      
C default values
C      data over/.true./

C generate values

      do l=1,2
         qstrt(l)=kistrt(l)-kfstrt(l)
         qstop(l)=kistop(l)-kfstop(l)
      enddo

      estrt=k2_to_E*(kistrt(1)**2+kistrt(2)**2
     &		      -kfstrt(1)**2-kfstrt(2)**2)
      estop=k2_to_E*(kistop(1)**2+kistop(2)**2
     &		      -kfstop(1)**2-kfstop(2)**2)
c02Apr      estrt=2.072*(kistrt(1)**2+kistrt(2)**2-kfstrt(1)**2-kfstrt(2)**2)
c02Apr      estop=2.072*(kistop(1)**2+kistop(2)**2-kfstop(1)**2-kfstop(2)**2)


C now we need to determine which vectors to label so that the
C labels look right

C calculate the nominal positions of the labels
C for ki the label is at a lower angle

      if (atan(kistop(2)/kistop(1)).lt.
     &     atan(kistrt(2)/kistrt(1))) then
         kilab(1)=kistop(1)/2+(kistop(2)/sqrt(kistop(1)**2.+
     &        kistop(2)**2.))*xscale*.055
         kilab(2)=kistop(2)/2-(kistop(1)/sqrt(kistop(1)**2.+
     &        kistop(2)**2.))*yscale*.055
      else
         kilab(1)=kistrt(1)/2+(kistrt(2)/sqrt(kistrt(1)**2.+
     &        kistrt(2)**2.))*xscale*.055
         kilab(2)=kistrt(2)/2-(kistrt(1)/sqrt(kistrt(1)**2.+
     &        kistrt(2)**2.))*yscale*.055
      endif

C      write(*,*) 'ki ',kistrt(1),kistrt(2),' and ',yscale     
C      write(*,*) 'ki label at ',kilab(1),kilab(2)

C for Q the label is at a higher angle

      if (atan(qstop(2)/qstop(1)).gt.
     &     atan(qstrt(2)/qstrt(1))) then
         qlab(1)=qstop(1)/2.5-(qstop(2)/sqrt(qstop(1)**2.+
     &        qstop(2)**2.))*xscale*.04
         qlab(2)=qstop(2)/2.5+(qstop(1)/sqrt(qstop(1)**2.+
     &        qstop(2)**2.))*yscale*.04
      else
         qlab(1)=qstrt(1)/2.5-(qstrt(2)/sqrt(qstrt(1)**2.+
     &        qstrt(2)**2.))*xscale*.04
         qlab(2)=qstrt(2)/2.5+(qstrt(1)/sqrt(qstrt(1)**2.+
     &        qstrt(2)**2.))*yscale*.04
      endif


C for kf use the outside midpoint
      kflabstrt(1)=(kistrt(1)+qstrt(1))/2-(kfstrt(2)/
     &     sqrt(kfstrt(1)**2+kfstrt(2)**2))*xscale*.05
      kflabstrt(2)=(kistrt(2)+qstrt(2))/2+(kfstrt(1)/
     &     sqrt(kfstrt(1)**2+kfstrt(2)**2))*yscale*.05

      kflabstop(1)=(kistop(1)+qstop(1))/2-(kfstop(2)/
     &     sqrt(kfstop(1)**2+kfstop(2)**2))*xscale*.05
      kflabstop(2)=(kistop(2)+qstop(2))/2+(kfstop(1)/
     &     sqrt(kfstop(1)**2+kfstop(2)**2))*yscale*.05
      
      if (kflabstrt(1)**2+kflabstrt(2)**2.lt.
     &     kflabstop(1)**2+kflabstop(2)**2) then
         kflabstrt(1)=kflabstop(1)
         kflabstrt(2)=kflabstop(2)
      endif
      
      if (over) then
         call pgsci(color_bkgr)
c01Dec         call pgsci(0)
         call pgsfs(1)
         call PGQTXT(qlab(1),qlab(2),0.0,0.5,'\fiQ',xbox,ybox)
         call pgrect(xbox(1),xbox(3),ybox(1),ybox(3))
      endif
      call pgsci(color_ink)
c01Dec      call pgsci(1)
      call PGPTXT(qlab(1),qlab(2),0.0,0.5,'\fiQ')
      
      if (over) then
         call pgsci(color_bkgr)
c01Dec         call pgsci(0)
         call pgsfs(1)
         call PGQTXT(kilab(1),kilab(2),
     &        0.0,0.5,'\fik\fn\di\u',xbox,ybox)
         call pgrect(xbox(1),xbox(3),ybox(1),ybox(3))
      endif
      call pgsci(color_ink)
c01Dec      call pgsci(1)
      call PGPTXT(kilab(1),kilab(2),0.0,0.5,'\fik\fn\di\u')
      
      if (over) then
         call pgsci(color_bkgr)
c01Dec         call pgsci(0)
         call pgsfs(1)
         call PGQTXT(kflabstrt(1),kflabstrt(2),
     &        0.0,0.5,'\fi-k\fn\df\u',xbox,ybox)
         call pgrect(xbox(1),xbox(3),ybox(1),ybox(3))
      endif
      call pgsci(color_ink)
c01Dec      call pgsci(1)
      call PGPTXT(kflabstrt(1),kflabstrt(2),0.0,0.5,'\fi-k\fn\df\u')
      
C energy labels - these should have no more than three sig figs

      en=estrt

C     use a while style loop to find pp and mm
C     i.e. the power and the mantissa


 109  pp=1   ! assume en= m.mm * 10^pp where pp is no greater than 1
             ! i.e. en <= 99.9 meV

C  gives a value rounded off to 2-decimal places
      enrnd=aint(100.0*(en+.005*abs(en)/en))/100.0

 110  val=enrnd*(10.0**float(-pp))
C val has full decimals
      truncval=aint(val)
C truncval only the most significant figure being checked

C this means that val is very close to an integer value
      if (abs(val-truncval).le..01) go to 111
      pp=pp-1
      go to 110

 111  mm=int(truncval)
      call pgnumb(mm,pp,1,evalue,nc)
      write(elabel,'(a8,a)') '\fiE\fn=',evalue(1:nc)
      
      if (over) then
         call pgsci(color_bkgr)
c01Dec         call pgsci(0)
         call pgsfs(1)
         if (en.eq.estrt) then
            temp1 = 1.1*kistrt(1)
            temp2 = 1.1*kistrt(2)
            call PGQTXT(temp1, temp2, 0.0,0.0,elabel,xbox,ybox)
c01Nov            call PGQTXT(1.1*kistrt(1),1.1*kistrt(2),
c01Nov     &           0.0,0.0,elabel,xbox,ybox)
            call pgrect(xbox(1),xbox(3),ybox(1),ybox(3))
         else
            temp1 = 1.1*kistop(1)
            temp2 = 1.1*kistop(2)
            call PGQTXT(temp1, temp2, 0.0,0.0,elabel,xbox,ybox)
c01Nov            call PGQTXT(1.1*kistop(1),1.1*kistop(2),
c01Nov     &           0.0,0.0,elabel,xbox,ybox)
            call pgrect(xbox(1),xbox(3),ybox(1),ybox(3))
         endif

      endif
      call pgsci(color_ink)
c01Dec      call pgsci(1)
      if (en.eq.estrt) then
         temp1 = 1.1*kistrt(1)
         temp2 = 1.1*kistrt(2)
         call PGPTXT(temp1, temp2, 0.0, 0.0, elabel)
c01Nov         call PGPTXT(1.1*kistrt(1),1.1*kistrt(2),
c01Nov     &        0.0,0.0,elabel)
      else
         temp1 = 1.1*kistop(1)
         temp2 = 1.1*kistop(2)
         call PGPTXT(temp1, temp2, 0.0, 0.0, elabel)
c01Nov         call PGPTXT(1.1*kistop(1),1.1*kistop(2),
c01Nov     &        0.0,0.0,elabel)
      endif

      if ((en.ne.estop)) then
         en=estop
         go to 109
      endif
      return
      end
