      subroutine getung
      include 'screen.inc'
      include 'parmtr.inc'
      integer*4 maxdat
      parameter (maxdat=20)
      character*6 formt
      character*45 label(maxdat)
      common /plots/ label
      real temp
      integer*4 nobjts,iobjts,icode,isave,i,systmp,iwindw
      integer*2 irowt,icolt,istart,iend,scnpos(mxstat)
      logical filext,edtmod
      logical yesno
      external yesno
      data systmp/12/
      data scnpos/43,62,76/
c
      call cls
c
c   get number of objects to read in
c
      irow=5
      icol=1
      call putcur(irow,icol,ivpage,ierror)
      line='Begin entry of ungrouped data: perpendicular distances,'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='sighting distances, or sighting angles, depending on the '
     1   //'data status.'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='Enter the file name containing ungrouped measurements.'//
     1   '  A carriage return'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='or blank(s) implies that the user will enter data '//
     1   'interactively from keyboard.'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='Labels must be in columns 1-30, perpendicular distances '//
     1   'in 31-35,'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='sighting distances in 36-40, and sighting angles '//
     1   'in 41-45, depending'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
      call putcur(irow,icol,ivpage,ierror)
      line='on which of the data status options '//
     1   '(PERP, SIGH, or ANGL) were specified.'
      call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      irow=irow+1
 5    call putcur(irow,icol,ivpage,ierror)
      line='File name: '
      call wchars(ivpage,line,12,icolor,ibckgd,0,ierror)
      line=' '
      call getbuf(line,nchar,66,iatt)
      filext=.false.
c
c   if filext = .true. then read data from a file
c   else use interactive input.
c
      if (line.ne.' ') then
         inquire(file=line,exist=filext)
         if (.not. filext) then
            call putcur(irow+1,icol,ivpage,ierror)
            call wchars(ivpage,'The file '//line(1:nchars)//
     1         ' does not exist.  Re-enter.',36+nchars,
     2         icolor,ibckgd,0,ierror)
            call scroll(1,0,irow,12,irow,80,7,ierror)
            go to 5
         else
            open(unit=systmp,file=line,status='old')
         endif
      else
         irow=irow+1
         call putcur(irow,icol,ivpage,ibckgd)
         line='Data are to be entered from the keyboard.'
         call wchars(ivpage,line,79,icolor,ibckgd,0,ierror)
      endif
c
c   data entry
c
      if (.not. filext) then
         irow=irow+2
 10      call putcur(irow,icol,ivpage,ierror)
         line='Number of individual objects sighted is: '
         call wchars(ivpage,line,41,icolor,ibckgd,0,ierror)
         line=' '
         call getbuf(line,nchar,10,iatt)
         nobjts=0
         if (nchar.eq.0 .or. line.eq.' ') go to 11
         read(unit=line,fmt='(f10.0)',err=11) temp
         nobjts=int(temp)
 11      if (nobjts.lt.1 .or. nobjts.gt.maxobj) then
            call scroll(1,0,irow,49,irow,80,7,ierror)
            call putcur(irow+1,icol,ivpage,ierror)
            write(line,'(a,i5,a)')
     1         'Number of objects must be  0 < nobjects <',
     1         maxobj,'.  Re-enter.'
            call wchars(ivpage,line,70,icolor,ibckgd,0,ierror)
            go to 10
         endif
      else
         nobjts=maxobj
      endif
c
c   put heading at top of table to read in objects and measurements
c
      call cls
      icol=1
      irow=1
      write(line,'(a,1x,a,5x,a,5x,a,5x,a)') 'No.',
     1   '         Object Label         ',
     2   'Perpend. Dis.','Sighting Dis.','Angle'
      call putcur(irow,icol,ivpage,ierror)
      call wchars(ivpage,line,80,icolor,ibckgd,0,ierror)
      irow=2
      call putcur(irow,icol,ivpage,ierror)
      write(line,'(3a1,1x,30a1,5x,13a1,5x,13a1,5x,5a1)') ('-',i=1,64)
      call wchars(ivpage,line,80,icolor,ibckgd,0,ierror)
c   loop to read in data over all objects
c   A do loop is not used because I need to tinker with the total
c   number of objects, i.e., detect the end of file on an input
c   data set.  nobjts is set when an eof is encountered.
      edtmod=.false.
      iwindw=1
      isave=0
 20   isave=isave+1
         if (isave.gt.nobjts) go to 37
         i=isave
         iobjts=mod(isave-1,20)+1
         irow=irow+1
c   enter object number in table
         call putcur(irow,1,ivpage,ierror)
         write(line,'(i3)') isave
         call wchars(ivpage,line,3,icolor,ibckgd,0,ierror)
         label(iobjts)=' '
         if (filext) then
            read(unit=systmp,fmt='(a45)',end=34) label(iobjts)
            go to 35
c           come here when an end of file is found on the input file
 34         close(unit=systmp,status='keep')
c           blank out the line that was being created
c           when the eof was found
            call scroll(1,0,irow,1,irow,80,7,ierror)
c           set the number of objects read
            isave=isave-1
            nobjts=isave
            i=isave
            iobjts=iobjts-1
c           If the screen was just corrected, i.e., the number of objects
c           is exactly divisible by 20, don't redo the screen correction.
            if (iobjts.eq.0) return
c           otherwise branch to the screen correction section
            go to 43
 35         if (label(iobjts)(1:30).eq.' ') then
               write(line,'(a,i4)') 'Object number',i
               label(iobjts)(1:30)=line(1:30)
            endif
            call putcur(irow,5,ivpage,ierror)
            call wchars(ivpage,label(iobjts)(1:30),30,
     1         icolor,ibckgd,0,ierror)
            do 41 icode=1,mxstat
               istart=(icode-1)*5+31
               iend=icode*5+30
               if (statop(icode).eq.0) then
                   label(iobjts)(istart:iend)=' '
               else
                  if (label(iobjts)(istart:iend).eq.' ')
     1               label(iobjts)(istart:iend)='  0.0'
                  temp=-1.
                  line=label(iobjts)(istart:iend)
                  read(line,'(f5.0)',err=42) temp
 42               if (temp.lt.0. .or.
     1               (icode.eq.4 .and. temp.gt.360.) ) then
                     call putcur(irow,scnpos(icode)-6,ivpage,ierror)
                     call wchars(ivpage,'Error'//char(26),6,
     1                  icolor,ibckgd,1,ierror)
                  endif
                  if (pntran) then
                     temp=temp**2*pi
                  endif
                  if (temp.ge.10000.) then
                     formt='(f6.0)'
                  else if (temp.ge.1000.) then
                     formt='(f5.0)'
                  else if (temp.ge.100.) then
                     formt='(f5.1)'
                  else if (temp.ge.10.) then
                     formt='(f5.2)'
                  else if (temp.ge.1.) then
                     formt='(f5.3)'
                  else
                     formt='(f5.4)'
                  endif
                  write(line,formt) temp
                  label(iobjts)(istart:iend)=line(1:5)
               endif
               call putcur(irow,scnpos(icode),ivpage,ierror)
               call wchars(ivpage,label(iobjts)(istart:iend),5,
     1            icolor,ibckgd,0,ierror)
 41            continue
         else
c   loop to read label, perpendicular distance, sighting distance, and
c   sighting angle
 23      do 22 icode=0,3
            if (icode.gt.0) then
               if (statop(icode).eq.0) go to 22
            endif
 21         if (icode.eq.0) then
               line='Enter up to 30 characters for label: '
            else if (icode.eq.1) then
               write(line,'(a,a,a,i4,a)')
     1            'Enter perpendicular distance in ',
     1            disunt(1:ndisut),' for object number',i,': '
            else if (icode.eq.2) then
               write(line,'(a,a,a,i4,a)') 'Enter sighting distance in ',
     1            disunt(1:ndisut),' for object number',i,': '
            else if (icode.eq.3) then
               write(line,'(a,a,i4,a)') 'Enter sighting angle in ',
     1            'degrees for object number',i,':  '
            endif
            icolt=index(line,':')+2
            call putcur(24,5,ivpage,ierror)
            call wchars(ivpage,line,icolt,icolor,ibckgd,0,ierror)
            if (icode.eq.0) then
               call getbuf(line,nchar,30,iatt)
               temp=1.
               if (nchar.eq.0) then
                  write(line,'(a,i4)') 'Object number',i
               endif
            else
               call getbuf(line,nchar,5,iatt)
               temp=0.
            endif
            if (nchar.eq.0 .or. icode.eq.0) go to 28
            read(line,'(f5.0)',err=28) temp
 28         if (temp.lt.0 .or. temp.ge.100000.) then
               call putcur(25,5,ivpage,ierror)
               call wchars(ivpage,'Incorrect input, re-enter.',26,
     1            icolor,ibckgd,0,ierror)
               call scroll(1,0,24,icolt,24,80,7,ierror)
               go to 21
            else
               call scroll(1,0,24,1,25,80,7,ierror)
               if (pntran) then
                  temp=temp**2*pi
               endif
               if (temp.ge.10000.) then
                  formt='(f6.0)'
               else if (temp.ge.1000.) then
                  formt='(f5.0)'
               else if (temp.ge.100.) then
                  formt='(f5.1)'
               else if (temp.ge.10.) then
                  formt='(f5.2)'
               else if (temp.ge.1.) then
                  formt='(f5.3)'
               else
                  formt='(f5.4)'
               endif
               if (icode.eq.0) then
                  label(iobjts)=line(1:30)
                  call putcur(irow,5,ivpage,ierror)
                  call wchars(ivpage,line,30,icolor,ibckgd,0,ierror)
               else
                  istart=(icode-1)*5+31
                  iend=icode*5+30
                  write(line,formt) temp
                  label(iobjts)(istart:iend)=line(1:5)
                  call putcur(irow,scnpos(icode),ivpage,ierror)
                  call wchars(ivpage,line,5,icolor,ibckgd,0,ierror)
               endif
            endif
 22         continue
         endif
c
c   section to request changes in table when 20 entries are in place
c   or the last observation has been read from file or entered from
c   keyboard
c
 43      if ((isave.eq.nobjts) .or. edtmod .or. (iobjts.eq.20)) then
            edtmod=.true.
            call scroll(1,0,23,1,25,80,7,ierror)
            irow=23
            icol=5
            line=
     1      'Everything above OK, and ready to proceed to next page?'
            if (yesno(0)) then
               call scroll(1,0,3,1,25,80,7,ierror)
               irow=2
               do 33 iobjts=1,mod(isave-1,20)+1
 33               write(iout,'(a)') label(iobjts)
               edtmod=.false.
               iwindw=iwindw+20
               go to 20
            else
 26            call scroll(1,0,23,1,25,80,7,ierror)
               call putcur(23,5,ivpage,ierror)
               line='Which object above should be changed: '
               call wchars(ivpage,line,38,icolor,ibckgd,0,ierror)
               call getbuf(line,nchar,3,iatt)
               i=0
               read(line,'(i3)',err=27) i
 27            if (i.ge.iwindw .and. i.le.isave) then
                  iobjts=mod(i-1,20)+1
                  irow=iobjts+2
                  call scroll(1,0,irow,4,irow,80,7,ierror)
                  go to 23
               else
                  call scroll(1,0,23,29,23,80,7,ierror)
                  call putcur(24,5,ivpage,ierror)
                  call wchars(ivpage,'Improper input. Re-enter.',30,
     1              icolor,ibckgd,0,ierror)
                  go to 26
               endif
            endif
         endif
         if (isave.lt.nobjts) go to 20
 37   return
      end
