; integ_att (interactive)
;
; Performs integration of all the peaks of the opened dbm assignment 
; table (it is not possible to perform this integration only on the 
; current zoom window, because of a bug in dbm described in CAVEAT). 
; The calulated volumes are put into the dbm dbm amplitude Different 
; integration methods can be used: 'Max intensity' gives the data-set 
; value at the peak maximum, 'sumrec' and 'amoeba' make use of the 
; commands SUMREC and INTEG respectively.
;
; see also: SUMREC SHOWPEAKS INTEG mult_integ

Print "Current project :"
pwd

if ($att['largest'] == 0) error "Database Empty !"
if ($dim != 2) error "Command available in 2D only !"

set list = 'Max Intensity,Sumrec,Amoeba'
if (!$arg) then

  formbox 'Peak integration' \
   'integ_att $integ_meth' \
   'Integration method' enum $list integ_meth 'Max Intensity' noreturn \
   *

else

  set integ_meth = $_

; determine the number of peaks in the assignment data-base
  set u = 0
  foreach i in att
    if ($i s! "LARGEST") then
      set u = (%+1)
    endif
  endfor

; calculate the sizes of the integration box in index unit
  set mm1 = (($freq_1_2d*$box_f1)*($si1_2d-1)/($specw_1_2d+$si1_2d))
  set mm2 = (($freq_2_2d*$box_f2)*($si2_2d-1)/($specw_2_2d+$si2_2d))

; eval noise
  zoom 0 
  alert 'First select an empty area to evaluate noise of each data-set, THEN click on Ok'
  set x1 = $zone[1]
  set x2 = $zone[2]
  set x3 = $zone[3]
  set x4 = $zone[4]
  evaln $x1 $x2 $x3 $x4

  if ($integ_meth s= "Max Intensity") then

   initinprogress $u
   set uu = 0
   for i = 1 to $att["LARGEST"]
    if (exist('att['//$i//']')) then
      set l = $att[$i]
      set f1 = (head($l)) \
      set f2 = (head(tail($l)))
      set f1i = (int(ptoi($f1,2,1)))
      set f2i = (int(ptoi($f2,2,2)))
      set t = (tail(tail(tail($l))))
; look for the peak maximum within the peak box limits
      set apk = (val2d($f1i,$f2i))
      set f1pix = (int(ptoi($f1,2,1)))
      set f2pix = (int(ptoi($f2,2,2)))
      while (1)
;      print ("f1pix";$f1pix;"f2pix";$f2pix)
        set crdf1[1] = $f1pix
        set crdf2[1] = ($f2pix+1)
        set crdf1[2] = $f1pix
        set crdf2[2] = ($f2pix-1)
        set crdf1[3] = ($f1pix-1)
        set crdf2[3] = $f2pix
        set crdf1[4] = ($f1pix+1)
        set crdf2[4] = $f2pix
        set mxtmp = (val2d($crdf1[1],$crdf2[1]))
        set newpix = 1
        for u = 1 to 4
           set tmp[$u] = (val2d($crdf1[$u],$crdf2[$u]))
; look for the maximum of array tmp
; and for its pixel coordinates
          if ($mxtmp < $tmp[$u]) then
             set newpix = $u
             set mxtmp = $tmp[$u]
          endif
;           print ("tmp[u]";$tmp[$u];"u";$u;"mxtmp";$mxtmp)
        endfor
        if ($mxtmp <= $apk) then
; exit from the while loop if the current apk is larger than all 
; the neighboors
          goto fin
        else
          set tmp1 = (abs($crdf1[$newpix]-$f1pix))
          set tmp2 = (abs($crdf2[$newpix]-$f2pix))
          if ( ($tmp1 > $mm1) | ($tmp2 > $mm2) ) then
; if the pixel is outside of the peak box, exit from the while loop 
            goto fin
          else
; continue the search
            set apk = $tmp[$newpix]
            set f1pix = $crdf1[$newpix]
            set f2pix = $crdf2[$newpix]
          endif
        endif
      endwhile
=fin
      set att[$i] = ($f1; $f2; ($apk-$shift); $t)
      set uu = (%+1)   inprogress $uu
    endif
   endfor

  elsif ($integ_meth s= "Sumrec") then

   initinprogress $u
   set uu = 0
   for i = 1 to $att["LARGEST"]
    if (exist('att['//$i//']')) then
      set l = $att[$i]
      set f1 = (head($l))
      set f2 = (head(tail($l)))
      set t = (tail(tail(tail($l))))
;      print ("sumrec";(($f1+($box_f1/2))//"p");(($f2+($box_f2/2))//"p");(($f1-($box_f1/2))//"p") ;(($f2-($box_f2/2))//"p") )
      sumrec (($f1+($box_f1/2))//"p") (($f2+($box_f2/2))//"p") (($f1-($box_f1/2))//"p")  (($f2-($box_f2/2))//"p") 
      set att[$i] = ($f1; $f2; $sumrec; $t)
      set uu = (%+1)   inprogress $uu
    endif
   endfor

  elsif ($integ_meth s= "Amoeba") then

   set tmp = ('tmp' // int(1000000*$random))
   att->fil $tmp
   pkread ($tmp//".atr") y
   integ %%
; open the att/pk lookup data-base
   dbopen ($tmp//".hash") hash
   initinprogress $u
   set uu = 0
   for i = 1 to $att["LARGEST"]
    if (exist('att['//$i//']')) then
      set l = $att[$i]
      set f1 = (head($l)) \
      set f2 = (head(tail($l)))
      set t = (tail(tail(tail($l))))
; get the peak index by the lookup db
      set ipk = $hash[$i]
      set att[$i] = ($f1; $f2; $pk2d_a[$ipk]; $t)
      set uu = (%+1)   inprogress $uu
    endif
   endfor
   sh ("/bin/rm"; $tmp//".atr")
   sh ("/bin/rm"; $tmp//".hash.dir")
   sh ("/bin/rm"; $tmp//".hash.pag")
  
  endif

endif






















