; dosy3d type size thres 3dname
;
; implements the complete DOSY processing for 2D
;
; DOSY data are in-memory, diffusion dimension is in F1,
; NMR spectra in F2 and F3
; The final spectrum is written in the cache system.
;
; type : tabulated or regular   : determines the type of sampling
; size : size after Laplace inversion
; thres : multiplier used to determine the processing threshold
;               from the noise level
; 3dname : name of the 3D spectrum
;
; see also :  INVLAP, INVTLAP, dosy2d, dosy.g, dosy_setup

gm/test3d

message "Enter type (regular/tabulated)"
set type = $_
set type = (toupper($type))
if ($type s! "REGULAR" & $type s! "TABULATED")   error "Wrong DOSY type"

message "Enter final size"
set sz = $_
if ($sz < 4) error "Size too small"
if ($sz > $MEM_PRO_1D) error "Size too large'

message "Enter noise factor used for thresholding"
set thresh = $_

message "Enter the name of the final spectrum"
set 3dname = $_

; compute sizes to be used before and after
set szbf = $si1_3d

if ($sz > $szbf) then
;        chsize $sz %
        set szaf = $sz
else
        set szaf = $szbf
endif

; open the final spectrum
newfilec $3dname 3d $szaf % % % % % % % % % % % % 0

initinprogress ($si2_3d*$si3_3d)

; do the processing
col 1
for i = 1 to $si2_3d
   for j = 1 to $si3_3d
        inprogress $i
        if (val3d(1,$i,$j) > $thresh * $noise) then
                vert $i $j dim 1
                chsize $szbf
                if ($type s= "TABULATED") then
                        invtlap $sz
                else
                        invlap $sz
                endif
		chsize $szaf
        else
                dim 1 chsize $szaf zero
        endif
        dim 1 putc f1 $i $j 1 $szaf
   endfor
endfor
dim 2

quit

