c
c     Copyright (c) 1999,2000, Compaq Computer Corporation
c
c     See the file "license.terms" for information on usage and redistribution
c     of this file, and for a DISCLAIMER OF ALL WARRANTIES.
c
      subroutine compare_integrate
      implicit none
c
c     Integrate quantity in x or y, store in an entry in multi_1d
c
c     Programmer: John Faricelli
c     Date: 5-Jun-86
c
      include 'const.h'
      include 'sysdep.cmn'
      include 'mesh.cmn'
      include 'data.cmn'
      include 'io.cmn'
      include 'bincodes.cmn'
c
c     locals
c
      integer answer, string_len
      character*10 buffer
      character*80 errfmt
c
      errfmt = '('' '')'
c
200   call clear_screen
      write(luttyo,errfmt)
      errfmt = '('' '')'
      call write_data_menu('Integrate and store quantity:')
      write(luttyo,105)
105   format(/' ','Request? ',$)
      read(luttyi,106,err=300,end=300) buffer
106   format(a)
      call upcase(buffer)
      if( string_len(buffer) .eq. 0 ) then
         answer = EXIT_MENU
      else if( buffer(1:1) .eq. 'Q' .or.  buffer(1:1) .eq. 'E' ) then
         answer = EXIT_MENU
      else
         read(buffer,*,err=300) answer
      endif
c
c    Error check:
c       1) Exit menu
c       2) Answer in range
c       3) Is requested data in binary file?
c
      if( answer .eq. EXIT_MENU ) then
         goto 1000
      else if ( answer .lt. 1 .or. answer .gt. max_menu_num ) then
         errfmt = '('' '',''Invalid input: Please try again...'')'
         call beep_terminal
      else if( .NOT. in_binary_file(answer) ) then
         errfmt = '('' '',''Data not available from binary file'')'
         call beep_terminal
      else if( data_is_trigrid ) then
         errfmt =
     1   '('' '',''Sorry, data on triangular grid not supported yet'')'
         call beep_terminal
      else if( is_data_1d(answer) ) then
         errfmt = '('' '',''Integral of 1D data is a single number!'')'
         call beep_terminal
      else
         call int_and_store(arrdat(arrind(menu_to_arr(answer))),
     1            ni, nj, does_data_jump_at_oxide(answer),
     2            is_data_in_oxide(answer),
     3            data_name_int_1d(answer), data_nickname(answer),
     4            data_units_int_1d(answer), is_data_log(answer))
      endif
      goto 200
c
c     illegal input
300   errfmt = '('' '',''Invalid input: Please try again...'')'
      call beep_terminal
      goto 200
c
1000  continue
      return
      end
c
      subroutine int_and_store(data_array, max_h, max_v,
     1           discontinuity, value_in_oxide, quantity, nickname,
     2           units, do_log)
      implicit none
c
      include 'const.h'
      include 'sysdep.cmn'
      include 'mesh.cmn'
      include 'data.cmn'
      include 'io.cmn'
      include 'compare.cmn'
c
      integer V_INT, H_INT
      parameter (V_INT = 1, H_INT = 2)
c
      integer max_h, max_v
      logical discontinuity, value_in_oxide, do_log
      real data_array(max_h, max_v)
      character*(*) quantity, units, nickname
c
c     locals
c
      real value
      real result(COMP_ARRAYSIZE), upper, lower, int_limits(2)
      integer entry, find_new_entry, type
      integer i, iupper, ilower
      logical novalue_in_oxide
      character*1 answer
c
      novalue_in_oxide = .NOT. value_in_oxide
c
c     check for available entry in multi-1d
      entry = find_new_entry()
      if( entry .eq. -1 ) then
         write(luttyo,10)
10       format(' ','Error: all data slots used up'/
     1          ' ','Use Clear or Delete to free up space')
         call get_char(answer,'Hit return to continue...',0)
         return
      endif
c
c     Ask user for h or v line
99    call get_char(answer,
     1     'Integrate horizontally or vertically (H or V)? ',0)
      call upcase(answer)
      if ( answer .eq. 'V' ) then
         type = V_INT
      else if ( answer .eq. 'H' ) then
         type = H_INT
      else
         write(luttyo,102) answer
102      format(' ','Invalid response: ', a)
         goto 99
      endif
c
      if( type .eq. V_INT ) then
c        Ask user for integration limits
         call get_int_limits(lower, upper, ilower,
     1        iupper, 'V', novalue_in_oxide)
         int_limits(1) = lower
         int_limits(2) = upper
c        Integrate sections
         do 300 i=1,ni
            call do_1d_integrate(data_array, max_h, max_v,
     1         lower, upper, ilower, iupper, i,
     2         novalue_in_oxide, 'V', value)
            result(i) = value
300      continue
         call store_in_slot(entry, result, ni, xyz(1,idisti),
     1                      0.0, charv,
     2                      quantity, nickname, units, COMP_INT_TYPE,
     3                      int_limits, do_log)
      else
         call get_int_limits(lower, upper, ilower,
     1        iupper, 'H', novalue_in_oxide)
         int_limits(1) = lower
         int_limits(2) = upper
         do 350 i=1,nj
            call do_1d_integrate(data_array, max_h, max_v,
     1         lower, upper, ilower, iupper, i,
     2         novalue_in_oxide, 'H', value)
            result(i) = value
350      continue
         call store_in_slot(entry, result, nj, xyz(1,idistj),
     1                      0.0, charh,
     2                      quantity, nickname, units, COMP_INT_TYPE,
     3                      int_limits, do_log)
      endif
      return
      end
