pro destre_f,logarr,wid,nref,reb
;
; DESTRETCHING of a movie stored in "save2" files.
; Main for DEFORMO (procedures called by DEFORMO are SMOOTHE, DISTORTI,
; MEAN, SCONVOL, ODD, (KCONVOL) (Oslo)).
; LOGARR - selection array containing frame numbers (column 0).
;
; WID - FWHM of Gaussian window in determination of displacement map.
; (reasonable value: 32[16] pix, i.e. 2").
;
; Reference image: the floating average of movie frames.
; NREF gives the number of frames to be averaged
; (reasonable value: 5 - 7 frames). Better if NREF is an odd number.
;
; REB is down-rebining parameter for image correlation (1 or 2).
;
; Version 28.3.95
;
; ---------- UNSHARP MASKING PARAMETERS (for DEFORMO) ---------------

smo = 13  ; U.M. SMOOTHING PARAMETER (17[9]~1", 25[13]~1.5")
umq =0.5  ; U.M. COEFFICIENT (0: no masking, 1: total difference)

; -------------------------------------------------------------------

; Input and output filenames, number of frames etc.

st=''
read,'INPUT  filenames (default = spotgs): ',st
if st eq '' then st1='spotgs.' else st1=st+'.'	   ; st1 - input string
read,'OUTPUT filenames (default = spotde): ',st
if st eq '' then st2='spotde.' else st2=st+'.'	   ; st2 - output string 

slo=size(logarr)
nima=fix(slo(2))
print,'number of frames:',nima
nref=nref>3
nref=nref<nima
nref=fix(nref)		; to be sure

; Read the first frame to know the size

seq=strtrim(string(fix(logarr(0,0))),2)
print,'reading  '+seq
restore2,st1+seq,im1
sim=size(im1)
print,'image size:',sim(1),sim(2)

stack=fltarr(sim(1),sim(2),nref)   ; array sliding along the series

; Fill stack by the first nref frames

stack(0,0,0)=im1
for i=1,nref-1 do begin
   seq=strtrim(string(fix(logarr(0,i))),2)
   print,'reading  '+seq
   restore2,st1+seq,im1
   stack(0,0,i)=im1
endfor
   

for i=0,nima-1 do begin	      ;-----loop------------------------------

; Find the actual limits of stack with respect to the series

   ll=i-fix(nref/2)>0		  ; lower limit
   ul=ll+nref-1			  ; upper limit
   if ul gt (nima-1) then begin   ; end-of-series case
	ul=nima-1
	ll=ul-nref+1
   endif

; Beginning-of-series

   if (ul le (nref-1)) then begin
	ref=total(stack,3)/nref		; reference image
;	print,'begin ',ll,ul
	j=i				; live frame in the stack
   endif

; Add-and-shift procedure (middle part)

   if (ul gt (nref-1)) and (i le (nima-nref+fix(nref/2))) then begin
	stack=shift(stack,0,0,-1)	; shift to left
	seq=strtrim(string(fix(logarr(0,ul))),2)
	print,'reading  '+seq
	restore2,st1+seq,im1		; new frame on stack top
        stack(0,0,nref-1)=im1
	ref=total(stack,3)/nref		; reference image
;	print,'middle',ll,ul
        j=fix(nref/2)			; live frame in middle-of-stack
   endif

; End-of-series

   if i gt (nima+fix(nref/2)-nref) then begin
	ref=total(stack,3)/nref		; reference image
;	print,'end...',ll,ul
	j=i-nima+nref			; live frame in the stack
   endif
	
; Destretching

   print,'processing frame no.',i,'    stack',j
   DEFORMO,stack(*,*,j),ref,war,smo,umq,wid,reb,/interp

   if sim(3) eq 1 then war=bytscl(war)	; conversion to bytes
   if sim(3) eq 2 then war=NINT(war)	; conversion to integer

   sou=strtrim(string(fix(logarr(0,i))),2)
   save2,st2+sou,war

endfor				;-----end of loop------------------------
print,'End of program'
end