;+
; NAME:			polariz_display
;
; PURPOSE:		Widget Interface for do_polariz
;
; CATEGORY:
; CALLING SEQUENCE:	
; INPUTS:		NONE
;		
;
; OPTIONAL INPUTS:	
;
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:        POLARIZ_DATA
; SIDE EFFECTS:
; RESTRICTIONS:
;      Requires JULDAT and DAYCNV from the IDL ASTRON Library
; PROCEDURE:
;
; MODIFICATION HISTORY:
;	WRITTEN BY:	Dennis Wang, Interferometrics/NRL, 1996
;       29 Sep 97       - Save File header changes
;                          1. Date-OBS and TIME-OBS are now the average of
;                             3 individual DATE-OBS and TIME-OBS 
;                          2. Added as comment Exposure times of all 3 exposures; 
;                          3. Added as comment offset bias 
;                          4. Added file permission check prior to writing file
;       4 Oct 99        Added roll to position angle calculation
;      27 Oct 99        Changed PICKFILE to DIALOG_PICKFILE for IDL5.x 
;       1 Feb 00        Removed FIXC3ZERO in polariz_calc call - DW
;      15 Aug 2000      Fixed NaN values in % Pol when field stop mask is used 
;      12 Oct 2000      Added Non Tang Angle display to Pol Angle Button
;      01 Mar 2001      Fixed Raw.Clr Button Case statement
;                       Added B0 Displays
;
;	@(#)polariz_display.pro	1.16 03/05/01 NRL LASCO IDL LIBRARY
;-
;

; CODE MODIFICATIONS MADE ABOVE THIS COMMENT WILL BE LOST.
; DO NOT REMOVE THIS COMMENT: BEGIN HEADER

; DO NOT REMOVE THIS COMMENT: END HEADER
; CODE MODIFICATIONS MADE BELOW THIS COMMENT WILL BE LOST.

; CODE MODIFICATIONS MADE ABOVE THIS COMMENT WILL BE LOST.
; DO NOT REMOVE THIS COMMENT: BEGIN PDMENU3

PRO PDMENU3_Event, Event

  COMMON DRAW5_Comm, POLAR_DISPLAY_MAIN, LABEL10, PDMENU3, DRAW5_Id, DRAW6_Id,LABEL11
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
  COMMON POLARIZ_RTAN,sun_x,sun_y,jr,jt,qr,ur,sun_radius
  COMMON EVENTS,redraw

  CASE Event.Value OF 

  'Pol Angle.Pol Angle': BEGIN
    ;PRINT, 'Event for Polarization Angle'
    WSET, Draw5_ID
    current_display = 1
    SHOW,current_display
    redraw = Event
    END
  'Pol Angle.Non Tang': BEGIN
    ;PRINT, 'Event for Non Tangent Polarization Angle'
    WSET, Draw5_ID
    current_display = 24 
    SHOW,current_display
    redraw = Event
    END
  'B0.B0': BEGIN
    ;PRINT, 'Event for Total Intensity'
    WSET, Draw5_ID
    current_display = 2
    SHOW,current_display
    redraw = Event
    END
  'B0.Clr': BEGIN
    ;PRINT, 'Event for Total Intensity'
    WSET, Draw5_ID
    current_display = 25
    SHOW,current_display
    redraw = Event
    END
  'B0.B0-Clr': BEGIN
    ;PRINT, 'Event for Total Intensity'
    WSET, Draw5_ID
    current_display = 26
    SHOW,current_display
    redraw = Event
    END
  'B0.% (B0-Clr)/Clr': BEGIN
    ;PRINT, 'Event for Total Intensity'
    WSET, Draw5_ID
    current_display = 27
    SHOW,current_display
    redraw = Event
    END
  'UnPolarized ': BEGIN
    ;PRINT, 'Event for UnPolarized Intensity'
    WSET, Draw5_ID
    current_display = 3
    SHOW,current_display
    redraw = Event
    END
  'Polarized.Polarized Intensity': BEGIN
    ;PRINT, 'Event for Polarized Intensity'
    WSET, Draw5_ID
    current_display = 4
    SHOW,current_display
    redraw = Event
    END
  'Polarized.LOGPolarized Intensity': BEGIN
    ;PRINT, 'Event for LOG Polarized Intensity'
    WSET, Draw5_ID
    current_display = 22 
    SHOW,current_display
    redraw = Event
    END
  'Polarized.% Polarized/Unpolarized': BEGIN
    PRINT, 'Event for % Polarized/Unpolarized'
    WSET, Draw5_ID
    current_display = 13
    SHOW,current_display
    redraw = Event
    END
  'Polarized.% Polarized': BEGIN
    ;PRINT, 'Event for % Polarized Intensity'
    WSET, Draw5_ID
    current_display = 14
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Q': BEGIN
    WSET, Draw5_ID
    current_display = 7
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes U': BEGIN
    WSET, Draw5_ID
    current_display = 8
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Jy': BEGIN
    ;PRINT, 'Event for Jy'
    WSET, Draw5_ID
    current_display = 5
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Jz': BEGIN
    ;PRINT, 'Event for Jz'
    WSET, Draw5_ID
    current_display = 6
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Qt': BEGIN
    WSET, Draw5_ID
    current_display = 9
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Ut': BEGIN
    PRINT, 'Event for R-Tang.Stokes U'
    WSET, Draw5_ID
    current_display =10 
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Jradial': BEGIN
    PRINT, 'Event for R-Tang.Jradial'
    WSET, Draw5_ID
    current_display = 11
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Jtang': BEGIN
    PRINT, 'Event for R-Tang.Jtang'
    WSET, Draw5_ID
    current_display = 12
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Q%': BEGIN
    WSET, Draw5_ID
    current_display = 18
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes U%': BEGIN
    WSET, Draw5_ID
    current_display = 19
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Qt%': BEGIN
    WSET, Draw5_ID
    current_display = 20
    SHOW,current_display
    redraw = Event
    END
  'Stokes.Stokes Ut%': BEGIN
    WSET, Draw5_ID
    current_display = 21
    SHOW,current_display
    redraw = Event
    END
  'Raw.Clr': BEGIN
    WSET, Draw5_ID
    current_display = 23
    SHOW,current_display
    END
  'Raw.+60': BEGIN
    WSET, Draw5_ID
    current_display = 15
    SHOW,current_display
    END
  'Raw.  0': BEGIN
    WSET, Draw5_ID
    current_display = 16
    SHOW,current_display
    END
  'Raw.-60': BEGIN
    WSET, Draw5_ID
    current_display = 17
    SHOW,current_display
    END
  'XLoadCT': BEGIN
    ;PRINT, 'Event for XLoadCT'
    WSET, Draw5_ID
    xloadct
    END
  'Histogram': BEGIN
    ;PRINT, 'Event for Histogram'
    WSET, Draw5_ID
    percent = 0
    if(current_display eq 13 or $
       current_display eq 14 or $
       current_display eq 18 or $
       current_display eq 19 or $
       current_display eq 20 or $
       current_display eq 21 ) then percent = 1
    HISTO,percent
    END
  'Print.To GIF File': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,2,/gif
    print,'Saved GIF file'
    END
  'Print.To fuji3000': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'fuji3000'
    display_parm
    ps_setup,1,'fuji3000'
    END
  'Print.To color': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'color'
    display_parm
    ps_setup,1,'color'
    END
  'Print.To lasco-phaser': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'lasco-phaser',/color_prt
    display_parm
    ps_setup,1,'lasco-phaser',/color_prt
    END
  'Print.To qms-247': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'qms-247',/color_prt
    display_parm
    ps_setup,1,'lasco-phaser',/color_prt
    END
  'Print.To hp4-256': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'hp4-256'
    display_parm
    ps_setup,1,'hp4-256'
    END
  'Print.To px': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0,'px',/color_prt
    display_parm
    ps_setup,1,'px'
    END
  'Print.To File': BEGIN
    ;PRINT, 'Event for Print'
    WSET, Draw5_ID
    ps_setup,0
    display_parm
    ps_setup,3
    END
  'Recalc.Filter Coeff':BEGIN
    polariz_calc,jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,0,ptf,vig
    WSET, Draw5_ID
    SHOW,current_display
    END
  'Recalc.Point Filter Toggle':BEGIN
    if(ptf eq 0) then BEGIN
     ptf = 1
     print,'Point Filter ON'
    ENDIF ELSE BEGIN
     ptf = 0
     print,'Point Filter OFF'
    ENDELSE
    polariz_calc,jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,0,ptf,vig
    WSET, Draw5_ID
    SHOW,current_display
    END
  'Recalc.Vignetting Toggle':BEGIN
    if(vig eq 0) then BEGIN
     vig = 1
     print,'Vignetting ON'
    ENDIF ELSE BEGIN
     vig = 0
     print,'Vignetting OFF'
    ENDELSE
    polariz_calc,jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,0,ptf,vig
    WSET, Draw5_ID
    SHOW,current_display
    END
  'Recalc.FIXC3 Toggle':BEGIN
    if(fixc3zero eq 0) then BEGIN
     fixc3zero = 1
     print,'FIXC3 ON'
    ENDIF ELSE BEGIN
     fixc3zero = 0
     print,'FIXC3 OFF'
    ENDELSE
    polariz_calc,jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,0,ptf,vig
    WSET, Draw5_ID
    SHOW,current_display
    END
  'Save': BEGIN
    ;PRINT, 'Event for Save'
; to make simple FITS header

    CHOOSER,a,label

    polariz_hdr,hdr,out_file,expt                    ; float

    if(ptf) then fxaddpar,hdr,'COMMENT','PtFilter'
    if(vig) then fxaddpar,hdr,'COMMENT','VigFunc'
    fxaddpar,hdr,'POLAR',id

    savefile=strtrim(sxpar(h1,'DETECTOR'))+'-'+id+'-'+out_file
    fxaddpar,hdr,'FILENAME',savefile
    infiles = i1+' '+i2+' '+i3
    savefile = dialog_pickfile(/WRITE,FILTER='*.fts',GET_PATH=savepath,$
                               PATH=savepath,FILE=savefile)
;    savefile=savepath+savefile ;Not needed in IDL5.x -AV

    IF (CHECK_PERMISSION(savefile) EQ 0) THEN BEGIN
      PRINT, 'Error Saving File.'
    ENDIF ELSE BEGIN
     print,'OK to write file of current Display ',id,current_display

     label = 'POLARIZ='+label
     fxaddpar,hdr,'COMMENT',label

     IF(current_display eq 1 ) then BEGIN 
      fxaddpar,hdr,'BUNIT','RADIANS'
      fxwrite,savefile,hdr,float(a)
     ENDIF ELSE IF(current_display ge 2 and current_display le 12 ) then BEGIN 
      IF( vig and (camera eq 2 or camera eq 3)) THEN BEGIN
print,'Writing: MSB',vig,camera
       fxaddpar,hdr,'BUNIT','MSB' 
       fxwrite,savefile,hdr,float(a)                
      ENDIF ELSE BEGIN
print,'Writing: DN',vig,camera
       fxaddpar,hdr,'BUNIT','DN'
       fxwrite,savefile,hdr,float(a*expt)           ;convert to DN 
      ENDELSE
     ENDIF ELSE IF(current_display ne 15 and $
       current_display ne 16 and $
       current_display ne 17 ) then BEGIN
        fxaddpar,hdr,'BUNIT','UNITLESS'
        fxwrite,savefile,hdr,float(a)
     ENDIF
     ENDELSE
     print,'Save Done'
    END
  'Quit': BEGIN
    ;PRINT, 'Event for Quit'
    WIDGET_CONTROL,POLAR_DISPLAY_MAIN,/DESTROY
    END
ENDCASE
END

pro display_parm
COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
COMMON POLARIZ_RTAN,sun_x,sun_y,jr,jt,qr,ur,sun_radius

    device,/times,/bold,font_size=24,/isolatin1
    CHOOSER,a,label
    landscape_layout,a
    tvscl,a
    xyouts,0.5,-0.05,label,/normal,align=0.5,font=0

    label_img = date+' '+time+' - '+i1+' '+i2+' '+i3
    xyouts,0.5,-0.1,label_img,/normal,align=0.5,font=0
END

PRO CHOOSER,a,label
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_RTAN,sun_x,sun_y,jr,jt,qr,ur,sun_radius
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
 
      sz = size(aa)
      a = fltarr(sz(1),sz(2))

      CASE current_display OF
       1: BEGIN
          a = mu
          id='mu'
          label='Polarization Angle'
          END
       2: BEGIN
          a = t
          id='TI'
          label='Total Intensity'
          END
       3: BEGIN
          a =  un
          id='UP'
          label='UnPolarized Intensity'
          END
       4: BEGIN
          a = cc
          id='PB'
          label='Polarized Intensity'
          END
       5: BEGIN
          a = jy
          id='JY'
          label='Jy'
          END
       6: BEGIN
          a = jz
          id='JZ'
          label='Jz'
          END
       7: BEGIN
          a = aa
          id='Qs'
          label='Stokes Q'
          END
       8: BEGIN
          a = bb
          id='Us'
          label='Stokes U'
          END
       9: BEGIN
          a = qr
          id='Qt'
          label='Stokes Qt'
          END
      10: BEGIN
           a = ur
          id='Ut'
          label='Stokes Ut'
          END
      11: BEGIN
          a = jr
          id='Jr'
          label='Jradial'
          END
      12: BEGIN
          a = jt
          id='Jt'
          label='Jtang'
          END
      13: BEGIN
	  w = where(un gt 0.0)
          a(w) = 100.0 * cc(w)/un(w)
          a = a < 100.0 > 0.0
          id='PU'
          label='% Polarized/UnPolarized'
          END
      14: BEGIN
	  w = where(t gt 0.0)
          a(w) = 100.0 * cc(w)/t(w)
          a = a < 100.0 > 0.0
          id='%P'
          label='% Polarized'
          END
      15: BEGIN
          a = jraw1
          id='+60'
          label='+60 degrees'
          END
      16: BEGIN
          a = jraw2
          id='0'
          label='0 degrees'
          END
      17: BEGIN
          a = jraw3
          id='-60'
          label='-60 degrees'
          END
      18: BEGIN
	  w = where(t gt 0.0)
          a(w) = 100.0 * aa(w)/t(w)
          a = a < 100.0 > 0.0
          id='Q%'
          label='Stokes Q%'
          END
      19: BEGIN
	  w = where(t gt 0.0)
          a(w) = 100. * bb(w)/t(w)
          a = a < 100.0 > 0.0
          id='U%'
          label='Stokes U%'
          END
      20: BEGIN
	  w = where(t gt 0.0)
          a(w) = 100. * qr(w)/t(w)
          a = a < 100.0 > 0.0
          id='Qt%'
          label='Stokes Qt %'
          END
      21: BEGIN
	  w = where(t gt 0.0)
          a(w) = 100. * ur(w)/t(w)
          a = a < 100.0 > 0.0
          id='Ut%'
          label='Stokes Ut %'
          END
      22: BEGIN
          a = alog10(cc)
          id='lPB'
          label='Log pB'
          END
      23: BEGIN
          a = jraw0
          id='clr60'
          label='Clear Pol'
          END
      24: BEGIN
	  w = where (cmu gt 180.0)
          a = cmu * (!PI/180.0)
	  a(w) = a(w) - !PI
	  a = a - mu
          id='dmu'
          label='Non Tangential Angle'
	  END
      25: BEGIN
	  a = b0
          id='Clr'
          label='Clr'
          END
      26: BEGIN
	  a = t - b0
          id='B0-Clr'
          label='B0 - Clr'
          END
      27: BEGIN
	  a = 100.0 * (t - b0)/b0
	  w = where (b0 eq 0,wcount)
	  if (wcount ne 0) then a(w) = 0.0
	  w = where (a gt 100.0,wcount)
	  if (wcount ne 0) then a(w) = 100.0
          id='%(B0-Clr)/Clr'
          label='(B0 - Clr)/Clr %'
          END
      ENDCASE
END

PRO SHOW,display
  COMMON DRAW5_Comm, POLAR_DISPLAY_MAIN, LABEL10, PDMENU3, DRAW5_Id, DRAW6_Id,LABEL11
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
    CHOOSER,a,label
    if(display eq 13 or $
       display eq 14 or $
       display eq 18 or $
       display eq 19 or $
       display eq 20 or $
       display eq 21 ) THEN BEGIN
        tvscl,a < 100.0 > 0.0 
    ENDIF ELSE IF (display eq 22) THEN BEGIN
     a = bytscl(alog10(a),-11.0,-9.0) 
     tv,a
    ENDIF ELSE tvscl,a
    outstr = label+' - '+date+' '+time+' '+i1+' '+i2+' '+i3
    if(display eq 15) then outstr = label+' - '+date+' '+time+' '+i1
    if(display eq 16) then outstr = label+' - '+date+' '+time+' '+i2
    if(display eq 17) then outstr = label+' - '+date+' '+time+' '+i3
    xyouts,.05,.02,/NORMAL,outstr
    WIDGET_CONTROL,LABEL10,SET_VALUE=outstr
END

PRO HISTO,percent
 CHOOSER,a,label
 min_val = min(a,max=max_val)
 if(percent and min_val lt 0) then min_val = 0.0
 if(percent and max_val gt 100.0) then max_val = 100.0
 minmax=string(format='(A6,1x,F10.1,1x,A6,1x,F10.1)',' Min = ',min_val,' Max = ',max_val)
 plot,histogram(a),TITLE='Histogram of '+label+minmax,XTIT='DN',YTIT='Num of Pixels'
END

; DO NOT REMOVE THIS COMMENT: END PDMENU3
; CODE MODIFICATIONS MADE BELOW THIS COMMENT WILL BE LOST.

PRO DRAW5_Event,Event
  COMMON DRAW5_Comm, POLAR_DISPLAY_MAIN, LABEL10, PDMENU3, DRAW5_Id, DRAW6_Id,LABEL11
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_RTAN,sun_x,sun_y,jr,jt,qr,ur,sun_radius
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
  COMMON ZOOM_VAR,zoom_img,x1,x2,y1,y2

; Print,Event.type,Event.x,Event.y,Event.press,Event.release

  case Event.press OF
   0: BEGIN
      END
   1: BEGIN                       ; Left Button
      x1 = Event.x - roi_pixel          ; default = 20 pixels
      x2 = Event.x + roi_pixel
      y1 = Event.y - roi_pixel
      y2 = Event.y + roi_pixel

      arrsize = size(mu)
      if(x1 lt 0 ) then x1 = 0
      if(y1 lt 0 ) then y1 = 0
      if(x2 gt (arrsize(1)-1)) then x2 = arrsize(1)-1
      if(y2 gt (arrsize(2)-1)) then y2 = arrsize(2)-1

      CHOOSER,zoom_img,label
      n_elem = n_elements( zoom_img(x1:x2,y1:y2) )
      val = zoom_img(Event.x,Event.y)
      ave = total( zoom_img(x1:x2,y1:y2))/n_elem
      dist = sqrt((Event.x - sun_x)^2 + (Event.y - sun_y)^2)

      rsun = dist * get_sec_pixel(lasco_hdr)/sun_radius
      if(lasco_hdr.detector eq 'C2') then arcsec = c2_distortion( [ (Event.x - sun_x), (Event.y - sun_y)])
      if(lasco_hdr.detector eq 'C3') then arcsec = c3_distortion( [ (Event.x - sun_x), (Event.y - sun_y)])
      dist_arcsec = sqrt(arcsec(0)*arcsec(0) + arcsec(1)*arcsec(1))

      if(Event.x le sun_x and Event.y gt sun_y) then angle = -90.0 + (180.0/!PI) * atan ((Event.y - sun_y),(Event.x - sun_x)) 
      if(Event.x le sun_x and Event.y le sun_y) then angle = 270.0 + (180.0/!PI) * atan ((Event.y - sun_y),(Event.x - sun_x)) 
      if(Event.x gt sun_x) then angle = 270.0 + (180.0/!PI) * atan ((Event.y - sun_y),(Event.x - sun_x)) 

     outstr ='X: '+strtrim(string(Event.x),2)+ '('+strtrim(string(arcsec(0)),2)+' arcsec)'+ $
	     '  Y: ' +strtrim(string(Event.y),2)+ '('+strtrim(string(arcsec(1)),2)+' arcsec)'+ $
	     '  R: '+strtrim(string(dist),2)+ $ 
      '(pixels) '+strtrim(string(rsun),2)+' Rsun  P.A.: '+ $
      strtrim(string(angle),2)+'(degrees) = '

     Print,Event.x,Event.y,arcsec(0),arcsec(1),dist,angle,val
     if( current_display eq 13 or $
         current_display eq 14 or $
         current_display eq 18 or $
         current_display eq 19 or $
         current_display eq 20 or $
         current_display eq 21 or $
         current_display eq 27) then outstr =outstr + strtrim(string(val),2)+'%  Ave: '+strtrim(string(ave),2)+'%' $ 
      ELSE if(current_display eq 1 or current_display eq 24) then  $
       outstr = outstr + strtrim(string(val),2)+' radians  Ave: '+strtrim(string(ave),2)+' radians'  $
      ELSE BEGIN
       IF(vig) THEN outstr = outstr + strtrim(string(val),2)+' MSB Ave: '+strtrim(string(ave),2) $
       ELSE  outstr =outstr + strtrim(string(val),2)+'  Ave: '+strtrim(string(ave),2)
      ENDELSE
  WIDGET_CONTROL,LABEL11,SET_VALUE=outstr
  END
  2: BEGIN                                   	; Middle Button
      x1 = Event.x - roi_pixel        		; default = 20 pixels
      x2 = Event.x + roi_pixel
      y1 = Event.y - roi_pixel
      y2 = Event.y + roi_pixel

      arrsize = size(mu)
      if(x1 lt 0 ) then x1 = 0
      if(y1 lt 0 ) then y1 = 0
      if(x2 gt (arrsize(1)-1)) then x2 = arrsize(1)-1
      if(y2 gt (arrsize(2)-1)) then y2 = arrsize(2)-1

      CHOOSER,zoom_img,label
      n_elem = n_elements( zoom_img(x1:x2,y1:y2) )

      zoom_img = zoom_img(x1:x2,y1:y2)
      zoom_img = congrid(zoom_img,512,512)

      WSET, Draw6_ID
      tvscl, zoom_img
     END
  4: BEGIN                                   ; Right Button
      default_roi = 64            ; 128 pixels
      x1 = Event.x - 63
      x2 = Event.x + 64
      y1 = Event.y - 63
      y2 = Event.y + 64

      arrsize = size(mu)
      if(x1 lt 0 ) then x1 = 0
      if(y1 lt 0 ) then y1 = 0
      if(x2 gt (arrsize(1)-1)) then x2 = arrsize(1)-1
      if(y2 gt (arrsize(2)-1)) then y2 = arrsize(2)-1

      CHOOSER,zoom_img,label

      n_elem = n_elements( zoom_img(x1:x2,y1:y2) )

      zoom_img = zoom_img(x1:x2,y1:y2)
      zoom_img = congrid(zoom_img,512,512)

      WSET, Draw6_ID
      tvscl, zoom_img 
     END
  ENDCASE                               ; Which Mouse Button
END

PRO DRAW6_Event,Event
  COMMON DRAW5_Comm, POLAR_DISPLAY_MAIN, LABEL10, PDMENU3, DRAW5_Id, DRAW6_Id,LABEL11
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr
  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time
  COMMON POLARIZ_RTAN,sun_x,sun_y,jr,jt,qr,ur,sun_radius
  COMMON ZOOM_VAR,zoom_img,x1,x2,y1,y2

  case Event.press OF
   0: BEGIN
      END
   1: BEGIN                       ; Left Button

      val = zoom_img(Event.x,Event.y)
      zoom_size = size(zoom_img)
      orig_x = x1 + float(Event.x) * (x2 - x1)/zoom_size(1)
      orig_y = y1 + float(Event.y) * (y2 - y1)/zoom_size(2)
      dist = sqrt((orig_x - sun_x)^2 + (orig_y - sun_y)^2)

      rsun = dist * get_sec_pixel(lasco_hdr)/sun_radius
      roll = get_solar_roll(lasco_hdr)

      if(lasco_hdr.detector eq 'C2') then arcsec = c2_distortion( [ (orig_x - sun_x), (orig_y - sun_y)])
      if(lasco_hdr.detector eq 'C3') then arcsec = c3_distortion( [ (orig_x - sun_x), (orig_y - sun_y)])
      dist_arcsec = sqrt(arcsec(0)*arcsec(0) + arcsec(1)*arcsec(1))

      if(orig_x le sun_x and orig_y gt sun_y) then angle = -90.0 + (180.0/!PI) * atan ((orig_y - sun_y),(orig_x - sun_x))  
      if(orig_x le sun_x and orig_y le sun_y) then angle = 270.0 + (180.0/!PI) * atan ((orig_y - sun_y),(orig_x - sun_x)) 
      if(orig_x gt sun_x) then angle = 270.0 + (180.0/!PI) * atan ((orig_y - sun_y),(orig_x - sun_x)) 
      angle = angle + roll

     outstr ='X: '+strtrim(string(orig_x),2)+ '('+strtrim(string(arcsec(0)),2)+' arcsec)'+ $
	     '  Y: ' +strtrim(string(orig_y),2)+ '('+strtrim(string(arcsec(1)),2)+' arcsec)'+ $
	     '  R: '+strtrim(string(dist),2)+ $ 
      '(pixels) '+strtrim(string(rsun),2)+' Rsun  P.A.: '+ $
      strtrim(string(angle),2)+'(degrees) = '

     ;outstr ='X: '+strtrim(string(orig_x),2)+'  Y: '+strtrim(string(orig_y),2)+'  R: '+strtrim(string(dist),2)+'(pixels) '+strtrim(string(rsun),2)+' Rsun  P.A.: '+strtrim(string(angle),2)+'(degrees) = '
     PRINT,x1,y1,x2,y2,zoom_size(1),zoom_size(2)
     PRINT,Event.x,Event.y,orig_x,orig_y,dist,angle,val
     if( current_display eq 13 or $
         current_display eq 14 or $
         current_display eq 18 or $
         current_display eq 19 or $
         current_display eq 20 or $
         current_display eq 21 or $
         current_display eq 27) then outstr =outstr + strtrim(string(val),2)+'%' $ 
      ELSE if(current_display eq 1) then  $
       outstr = outstr + strtrim(string(val),2)+' radians'  $
      ELSE BEGIN
       IF(vig) THEN outstr = outstr + strtrim(string(val),2)+' MSB ' $
       ELSE  outstr =outstr + strtrim(string(val),2)
      ENDELSE
  WIDGET_CONTROL,LABEL11,SET_VALUE=outstr
  END
 ENDCASE
END

; CODE MODIFICATIONS MADE ABOVE THIS COMMENT WILL BE LOST.
; DO NOT REMOVE THIS COMMENT: BEGIN POLAR_DISPLAY_MAIN

PRO POLAR_DISPLAY_MAIN_Event, Event

  WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev

  CASE Ev OF 

  ; Event for PDMENU3
  'PDMENU3': PDMENU3_Event, Event
  'DRAW5': BEGIN
      ;Print, 'Event for DRAW5'
      DRAW5_Event, Event
      END
  'DRAW6': BEGIN
      ;Print, 'Event for DRAW6'
      DRAW6_Event, Event
      END
  ENDCASE
END

; DO NOT REMOVE THIS COMMENT: END POLAR_DISPLAY_MAIN
; CODE MODIFICATIONS MADE BELOW THIS COMMENT WILL BE LOST.

PRO polariz_display, j0,j1,j2,j3,filter0,filter1,filter2,filter3,passed_vig,passed_ptf,passed_fixc3,roi,GROUP=Group
  COMMON POLARIZ_DATA,camera,b0,aa,bb,cc,t,un,mu,cmu,jy,jz,i0,i1,i2,i3,pathname,h0,h1,h2,h3,corr1,corr2,corr3,pexptime,bias,lasco_hdr

  COMMON POLARIZ_DISPLAY, jraw0,jraw1,jraw2,jraw3,f0,f1,f2,f3,current_display,ptf,vig,fixc3zero,roi_pixel,id, savepath, outstr, date, time

  COMMON DRAW5_Comm, POLAR_DISPLAY_MAIN, LABEL10, PDMENU3, DRAW5_Id, DRAW6_Id,LABEL11
  IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0

  jraw0 = j0
  jraw1 = j1
  jraw2 = j2
  jraw3 = j3
  f0 = filter0
  f1 = filter1
  f2 = filter2
  f3 = filter3
  roi_pixel = roi
  ;print,'polariz_display',f1,f2,f3

  junk   = { CW_PDMENU_S, flags:0, name:'' }

  POLAR_DISPLAY_MAIN = WIDGET_BASE(GROUP_LEADER=Group, $
      COLUMN=1, $
      MAP=1, $
      TITLE='Polarized Light Display', $
      UVALUE='POLAR_DISPLAY_MAIN')

  BASE2 = WIDGET_BASE(POLAR_DISPLAY_MAIN, $
      COLUMN=1, $
      MAP=1, $
      UVALUE='BASE2')

  MenuDesc123 = [ $
      { CW_PDMENU_S,       1, 'Pol Angle' }, $ ;        0
      { CW_PDMENU_S,       0, 'Pol Angle' }, $ ;        0
      { CW_PDMENU_S,       2, 'Non Tang' }, $ ;        0
      { CW_PDMENU_S,       1, 'B0' }, $ ;        1
      { CW_PDMENU_S,       0, 'B0' }, $ ;        1
      { CW_PDMENU_S,       0, 'Clr' }, $ ;        1
      { CW_PDMENU_S,       0, 'B0-Clr' }, $ ;        1
      { CW_PDMENU_S,       2, '% (B0-Clr)/Clr' }, $ ;        1
      { CW_PDMENU_S,       0, 'UnPolarized ' }, $ ;        2
      { CW_PDMENU_S,       1, 'Polarized' }, $ ;        3
      { CW_PDMENU_S,       0, 'Polarized Intensity' }, $ ;        3
      { CW_PDMENU_S,       0, 'LOGPolarized Intensity' }, $ ;        3
      { CW_PDMENU_S,       0, '% Polarized/Unpolarized' }, $ ;        3
      { CW_PDMENU_S,       2, '% Polarized' }, $ ;        3
      { CW_PDMENU_S,       1, 'Stokes' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes Q' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes U' }, $ ;        4
      { CW_PDMENU_S,       0, 'Jy' }, $ ;        4
      { CW_PDMENU_S,       0, 'Jz' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes Qt' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes Ut' }, $ ;        4
      { CW_PDMENU_S,       0, 'Jradial' }, $ ;        4
      { CW_PDMENU_S,       0, 'Jtang' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes Q%' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes U%' }, $ ;        4
      { CW_PDMENU_S,       0, 'Stokes Qt%' }, $ ;        4
      { CW_PDMENU_S,       2, 'Stokes Ut%' }, $ ;        4
      { CW_PDMENU_S,       1, 'Raw' }, $ ;        5
      { CW_PDMENU_S,       0, 'Clr' }, $ ;        5
      { CW_PDMENU_S,       0, '+60' }, $ ;        5
      { CW_PDMENU_S,       0, '  0' }, $ ;        5
      { CW_PDMENU_S,       2, '-60' }, $ ;        5
      { CW_PDMENU_S,       0, 'XLoadCT' }, $ ;        6
      { CW_PDMENU_S,       0, 'Histogram' }, $ ;        7
      { CW_PDMENU_S,       1, 'Print' }, $ ;        8
      { CW_PDMENU_S,       0, 'To GIF File' }, $ ;        8
      { CW_PDMENU_S,       0, 'To fuji3000' }, $ ;        8
      { CW_PDMENU_S,       0, 'To color' }, $ ;        8
      { CW_PDMENU_S,       0, 'To lasco-phaser' }, $ ;        8
      { CW_PDMENU_S,       0, 'To qms-247' }, $ ;        8
      { CW_PDMENU_S,       0, 'To hp4-256' }, $ ;        8
      { CW_PDMENU_S,       0, 'To px' }, $ ;        8
      { CW_PDMENU_S,       2, 'To PS File' }, $ ;        8
      { CW_PDMENU_S,       1, 'Recalc' }, $ ;        9
      { CW_PDMENU_S,       0, 'Filter Coeff' }, $ ;        9
      { CW_PDMENU_S,       0, 'Point Filter Toggle' }, $ ;        9
      { CW_PDMENU_S,       0, 'Vignetting Toggle' }, $ ;        9
      { CW_PDMENU_S,       2, 'FIXC3 Toggle' }, $ ;        9
      { CW_PDMENU_S,       0, 'Save' }, $ ;       10 
      { CW_PDMENU_S,       2, 'Quit' } $  ;     11

  ]

  PDMENU3 = CW_PDMENU( BASE2, MenuDesc123, /RETURN_FULL_NAME, $
      UVALUE='PDMENU3')

  LABEL10 = WIDGET_LABEL( BASE2, $
      UVALUE='LABEL10', $
      /DYNAMIC_RESIZE, $
      VALUE='Polarization Data')

  BASE3 = WIDGET_BASE(BASE2, $
      ROW=1, $
      MAP=1, $
      UVALUE='BASE3')

 s = size(aa)
 xs = s(1)
 ys = s(2)

  DRAW5 = WIDGET_DRAW( BASE3, $
      RETAIN=2, $
      UVALUE='DRAW5', $
      /BUTTON_EVENTS, $
      XSIZE=xs, $
      YSIZE=ys)

  DRAW6 = WIDGET_DRAW( BASE3, $
      RETAIN=2, $
      UVALUE='DRAW6', $
      /BUTTON_EVENTS, $
      XSIZE=xs, $
      YSIZE=ys)

  LABEL11 = WIDGET_LABEL( BASE2, $
      UVALUE='LABEL11', $
      /DYNAMIC_RESIZE, $
      VALUE='Press L Mouse Button to Read Value, Mid, R Button for Zoom')

  WIDGET_CONTROL, POLAR_DISPLAY_MAIN, /REALIZE

  ; Get drawable window index

  WIDGET_CONTROL, DRAW5, GET_VALUE=DRAW5_Id
  WIDGET_CONTROL, DRAW6, GET_VALUE=DRAW6_Id

  WSET, Draw5_ID
  tvscl,mu
  current_display = 1
  ptf = passed_ptf
  vig = passed_vig
  fixc3zero = passed_fixc3
  id='mu'
  savepath='./'
  date=strtrim(sxpar(h1,'DATE-OBS'))
  time=strtrim(sxpar(h1,'TIME-OBS'))
  outstr = 'Polarization Angle - '+date+' '+time+' '+i1+' '+i2+' '+i3
  xyouts,.05,.02,/NORMAL,outstr
  WIDGET_CONTROL,LABEL10,SET_VALUE=outstr

  XMANAGER, 'POLAR_DISPLAY_MAIN', POLAR_DISPLAY_MAIN
END