; $Id: zoom.pro,v 1.3 1993/05/21 17:36:58 dave Exp $
pro zoom_az_su, wlss=wlss, dat1=dat1, massiv=my_str, area=b, xsize=xs, ysize=ys, xtimes=xtimes, fact = fact, interp = interp, continuous = cont, $
        keep=keep, zoom_window=zoom_win, new_window=new_win, cdelt=cdelt
;modificirovannyj variant prcedury zoom by susan@sao.ru
;vyzyvaetsja iz  FITSRAT

; sdelan iz        ZOOM
;       ZOOM [, FACT = Fact, /INTERP, XSIZE = Xs, YSIZE = Ys, /CONTINUOUS, $
;               /KEEP, ZOOM_WINDOW=Zoom_Win, /NEW_WINDOW ]
;       FACT:   Zoom factor.  This parameter must be an integer.
;   CONTINUOUS: Set this keyword to make the zoom window track the mouse
;               without requiring the user to press the left mouse button.
;               This feature only works well on fast computers.
;         KEEP: Keep the zoom window after exiting the procedure.
;   NEW_WINDOW: Normally, if ZOOM is called with /KEEP and then called again,
;               it will use the same window to display the zoomed image.
;               Calling ZOOM with /NEW_WINDOW forces it to create a new window
;               for this purpose.
;       When ZOOM is reusing a zoom window from a previous call to ZOOM,/KEEP,
;       then the XSIZE and YSIZE parameters are reset to the actual size of the
;       window.
on_error,2              ;Return to caller if an error occurs
common zoom_window, zoom_w
;
xsize=xs
ysize=ys
n=size(my_str)
n1=n[1] & n2=n[2]
if n_elements(xs) le 0 then xs = 400;512
if n_elements(ys) le 0 then ys = 400;512
if n_elements(fact) le 0 then fact=4
if keyword_set(cont) then waitflg = 2 else waitflg = 3
ifact = fact
old_w = !d.window
if keyword_set(new_win) then zoom_w = -1        ;Don't use old window (if any)
if n_elements(zoom_w) eq 0 then zoom_w = -1             ;No zoom window yet
;
;  If an old window is to be used, then make sure it still exists.  (Added by
;  William Thompson, 20 May 1993.)
;
if zoom_w ge 0 then begin
        device, window_state=win_state
        if not win_state(zoom_w) then zoom_w = -1
endif
;
;  Make sure the parameters xs and ys agree with the size of the window, in
;  case a window is being reused from a previous call to ZOOM,/KEEP.  (Added by
;  William Thompson, 20 May 1993.)
;
IF ZOOM_W GE 0 THEN BEGIN
        OLD_WINDOW = !D.WINDOW
        WSET, ZOOM_W
        XS = !D.X_SIZE
        YS = !D.Y_SIZE
        WSET, OLD_WINDOW
ENDIF
tvcrs,1                 ;enable cursor
ierase = 0              ;erase zoom window flag
print,'Left for zoom center, Middle for new zoom factor, Right to quit'
again:
        tvrdc,x,y,waitflg,/dev  ;Wait for change
        ;x = x < n1 - xs
        ;y = y < n2 - ys

        case !err of
4:      goto, done
2:      if !d.name eq 'SUN' or !d.name eq 'X' then begin        ;Sun view?
                s  = ['New Zoom Factor:',strtrim(indgen(19)+2,2)]
                ifact = wmenu(s, init=ifact-1,title=0)+1
                tvcrs,x,y,/dev  ;Restore cursor
                ierase = 1
        endif else begin
                Read,'Current factor is',ifact+0,'.  Enter new factor: ',ifact
                if ifact le 0 then begin
                        ifact = 4
                        print,'Illegal Zoom factor.'
                        endif
                        ierase = 1      ;Clean out previous display
        endelse
else:   begin
        x0 = 0 > (x-xs/(ifact*2))       ;left edge from center
        y0 = 0 > (y-ys/(ifact*2))       ;bottom
        nx = xs/ifact                   ;Size of new image
        ny = ys/ifact
        nx = nx < (!d.x_vsize-x0)
        ny = ny < (!d.y_size-y0)
        x0 = x0 < (!d.x_vsize - nx)
        y0 = y0 < (!d.y_vsize - ny)
        a = tvrd(x0,y0,nx,ny)           ;Read image
my_sum=0;
;my_area=fltarr(xs,ys);
my_area=fltarr(xsize,ysize);
i=0&ii=0&j=0&jj=0&xend=x0+xs;
ye= y0+ys;
ii=0;

x=x < (n1-xsize/2.0-1)
y=y < (n2-ysize/2.0-1)
x= x > xsize/2.0
y= y > ysize/2.0
x0=x-xsize/2.0
y0=y-ysize/2.0
xe=x+xsize/2.0-1
ye=y+ysize/2.0-1
my_area(*,*)=my_str(x0:xe,y0:ye)

;print, my_area;
my_sum=total(my_area);
;cdelt1=3.75; cdelt2=3.75;
;cdelt1=1.148& cdelt2=1.148
cdelt1=4.91104& cdelt2=4.91104
my_flux=my_sum*0.05*(cdelt1*cdelt2)*4.84;
mmax = max(my_area, bbb, MIN=mmin, SUBSCRIPT_MIN=aaa)
indmax=ARRAY_INDICES(my_area, bbb)
indmin=ARRAY_INDICES(my_area, aaa)
xmax=indmax[0]+x0 & ymax=indmax[1]+y0
xmin=indmin[0]+x0 & ymin=indmin[1]+y0

print, '---------------------------------------'
;print, 'x=', x, ' y=', y, STRING(9B) , 'x"=', (x-n1/2)*cdelt, '  y"=', (y-n2/2)*cdelt, STRING(9B) , 'z=',my_str(x,y)
print, 'you pointed at: ', STRTRIM(my_str(x,y), 2), '(', STRTRIM(x,2), ',', STRTRIM(y,2), ')', '= (', STRTRIM(round((x-n1/2)*cdelt), 2), '",', STRTRIM(round((y-n2/2)*cdelt), 2), '")'
print, 'area max=', max(my_area, bbb), '(', STRTRIM(xmax,2), ',', STRTRIM(ymax,2), ')'
print, 'area min=', min(my_area, aaa), '(', STRTRIM(xmin,2), ',', STRTRIM(ymin,2), ')'
print, 'my_flux= ',my_flux, STRING(9B) , 'my_sum= ', my_sum;
                if zoom_w lt 0 then begin       ;Make new window?
                window,/free,xsize=xsize,ysize=ysize,title='Contour Area Image'
                zoom_w = !d.window
        endif else begin
                wset,zoom_w
                if ierase then erase            ;Erase it?
                ierase = 0
        endelse
        xss = nx * ifact; *xtimes      ;Make integer rebin factors
        yss = ny * ifact
        xsst=xss*xtimes
        b=rebin(a,xsst,yss,sample=1-keyword_set(interp))
bb=fltarr(xss,yss)
iit=0
for i=0, yss-1 do begin
bb(*,iit)=b(xsst/2-xss/2:xsst/2+xss/2-1,i);
iit=iit+1;
endfor;
b=bb
        ;b=b-min(b)
        ;b=(b/max(b))*2560.0
        ;tv,b
        ;contour,b, YTITLE='Ta', xstyle=1, ystyle=1, LEVELS=[50,80,110,150,200], /FILL
        ;contour,b, nlevels=5, C_ANNOTATION=[50,80,110,150,200], /overplot
        nlev=16
        V = FINDGEN(nlev) * (max(b)/nlev)+max(b)/nlev
title=dat1+' /'+ wlss+'cm'
tvscl, b
       ; contour,b, XTITLE=title, xstyle=1, ystyle=1, NLEVELS=nlev, /FILL
       ; contour,b, NLEVELS=nlev, /overplot

gifname=dat1+'_'+wlss+'kus.gif'
 out=bytscl(TVRD())
       tvlct,r,g,b,/get
;write_gif,gifname,out,r,g,b
        wset,old_w
        endcase
endcase
goto,again

done:
IF NOT KEYWORD_SET(KEEP) THEN BEGIN
        if zoom_w ge 0 then wdelete,zoom_w              ;Done with window
        ZOOM_W = -1
ENDIF
zoom_win = zoom_w       ;Return index of zoom window to user
end


