pro strousisol_raw,movin,movout,sh,msk

; Procedure to isolate penumbral grains in a 3-D sunspot movie.
; "Everything" except PGs is set to zero.
;Version for processing of P.Suetterlin's DOT data.
;Segmentation is done by Strous algorithm (STROAL.PRO)
;General mask is set individually
;for each frame, trying to outline the actual penumbra boundary.
;Then a mask to remove too dark BFs (i.e. UDs) is applied.
;Moreover, an external mask MSK can be used.
; MOVIN - input movie
; MOVOUT- output movie with isolated PGs (544 x 368 pix)
; SH    - distance (pix) between investigated points for STROAL (optional)
; MSK   - external mask (optional)
; Adapted from UDISOL2 and PGISOL2, 28 March 2000, Michal

on_error,1

;*************** PARAMETERS **********************************
; PETHR - intensity below which the bright BFs are removed
; GMSMO - smoothing parameter to define general mask (pix)
; GMTHO - threshold to define outer general mask

 pethr=600	; minimum allowed intensity (here Iph = 1219)
 gmsmo=31 	; 2.5" to smooth the penumbra
 gmtho=1070	; max. penumbral intensity in smoothed frame
;*************************************************************

if n_params() lt 3 then begin
	sh =1   ; STROAL parameter, default
	msk=1	; external mask, default
endif
if n_params() lt 4 then msk=1

smov=size(movin)
if smov(0) ne 3 then message,'Input must be a 3-D array!'
movout=intarr(544,368,smov(3))

for i=0,smov(3)-1 do begin

   oima=movin(*,*,i)			;original image

   ima=STROAL(oima,sh) gt 0		;segmentation mask
   ima=ima*oima				;segmented original image

   mge=SMOOTH(oima,gmsmo) lt gmtho	;outer general mask
   ima=ima*mge*msk			;masked image

   mpe=ima gt pethr	; mask to remove too faint BFs
   ima=ima*mpe		; too faint features eliminated

   movout(0,0,i)=ima(32:575,66:433)	;compose resulting movie

endfor

end