pro uvsparea, struc=struc, data=data, hd=hd, ave_sep=ave_sep, timfil=timfil, $
	      rollo=rollo, hard=hard, abscissa=abscissa, p_scal=p_scal, $
		       topper=topper, bottom=bottom, left=left, right=right, $
		       hsiz=hsiz, vsiz=vsiz, mypsym=mypsym, mytick=mytick, $
			ylohi=ylohi, xlohi=xlohi


common order1, diskexp, control, generic, comment

; Plot defined pixel area. If not repeated rasters print max. 

; Only if only one image is currently displayed without roll.
if ave_sep then widget_control, comment, set_value='UVSPAREA, ' + $
		     'Invalid to pick pixel with separate ' +   	  $
                     'images: average, then choose.', /append      	  $
 else if struc.st3.tempnd gt 1 then widget_control, comment, set_value=   $
		     'UVSPAREA, Invalid to pick pixel with multiple ' +  $
		     ' detectors: use interactive, pick one, '+       $
		     'then choose multiple detectors.', /append           $
 else if rollo then widget_control, comment, set_value='UVSPAREA, ' + $
		     'Invalid to pick pixel with roll correction: '  +    $
  		     'NO ROLL, then choose.', /append                     $
 else begin

    if hard ne 0 then goto,NOCURSOR1	    ; already have area
                   
    if struc.st5.imagewin ge 0 then $ 		; image window must be active
        uvspwin, struc=struc, typwin=struc.st5.imagewin  	$
    else begin
	widget_control, comment, set_value='UVSPAREA, ' + $
			'Invalid without an active image window.', /append 
        return
    endelse

    ; Read mouse clicks for user defined positions
    uvspcursor,struc=struc,left=left,right=right,topper=topper,bottom=bottom, $
			    hsiz=hsiz,vsiz=vsiz,type=0

    widget_control, comment, set_value= $
	'Area from (' + strtrim(left,2) + ',' + strtrim(bottom,2)  + $
	') to (' + strtrim(right,2) + ',' + strtrim(topper,2) +')', /append

   if struc.st1.tempnfr gt 1 then begin	     ; More then one raster repeat

      device, get_screen_size=sc
      xsiz = sc(0)-100 < hsiz*400  &  ysiz = sc(1)-100 < vsiz*200

      uvspwin, struc=struc, typwin=struc.st5.areawin, xwin=xsiz, ywin=ysiz
      struc.st5.areawin = !d.window
                  
                              
NOCURSOR1:
      from = struc.st1.tempfrst - 1 
      to   = from + struc.st1.tempnfr - 1 

      a = (data(left:right, bottom:topper, 0, 0, from:to, struc.st3.d1) + $
	     (data(left:right,bottom:topper,0,0, from:to, struc.st3.d2) * $
			struc.st4.sum) )   / (struc.st4.sum + 1) 

      absci = abscissa(from:to, struc.st3.d1:struc.st3.d2) 

      ytitle =  'Counts per '+ $
		 strtrim(string(float(hd,144),'(f7.3)'),2)  + ' seconds'

      if timfil then xtitle= 'Raster Start (UT)' $
		else xtitle= 'Raster Repeats'

       ;  If ylohi(1) is 0 then not doing manual y-axis.
       if ylohi(1) eq 0 $
	  then yrange = [ p_scal, max(a) ] $
     	  else yrange = ylohi

       !p.multi = [0,hsiz,vsiz]

;ASCII	writefile='N' & read,'DO YOU WANT TO WRITE AN ASCII FILE ? (Y/N) : ', WRITEFILE
;ASCII	if strupcase(writefile) eq 'Y' then writefile=1 else writefile=0
;ASCII	
;ASCII	if writefile then openw, lun, 'UVSP'+struc.st6.expnu+'.ASCII', /get_lun

       mtitle = strarr( (size(a))(1), (size(a))(2) )

       for yes = topper-bottom, 0, -1 do  $
	 for xes = 0, right-left do begin

;ASCII	if writefile then begin    			; each row
;ASCII	   pixelcoor =  'UVSP PIXEL (left,top)=(1,1) (' + $    
;ASCII	   string(left+xes+1,'(i3.3)') + ',' + $
;ASCII	   string( ( ((size(data))(2)) -(bottom+yes) ), '(i3.3)') + ')'
;ASCII	
;ASCII	   print, pixelcoor
;ASCII	   printf, lun, pixelcoor
;ASCII	
;ASCII	   for loop = 0, struc.st1.tempnfr-1 do $     ; each column
;ASCII	      printf, lun, atime(absci(loop) + $
;ASCII		   utime(strmid(struc.st9.utstart(0),0,9)), $
;ASCII		   /yohkoh) + '   ' + string(a(xes,yes,0,0,loop,0),'(i6.6)' )
;ASCII	endif

	   mtitle(xes,yes) = 'Pixel (' + strtrim(left+xes,2) + ','+ $
			     strtrim(bottom+yes,2) + ') (0,0=bottom/left)'

	   if timfil $
	      then utplot, absci, a(xes,yes,0,0,*,0)>p_scal, $
			strmid(struc.st9.utstart(0),0,9), /nolabel, /yohkoh, $
	                xstyle=1, yrange=yrange, psym=mypsym, ticklen=mytick, $
	 	        title=mtitle(xes,yes), xtitle=xtitle, ytype=p_scal, $
			xrange=xlohi $
	      else plot, absci, a(xes,yes,0,0,*,0)>p_scal, xstyle=1, $
			yrange=yrange, ytype=p_scal,  $
	 	        title=mtitle(xes,yes), xtitle=xtitle,   $
			xrange=xlohi, psym=mypsym, $
			ticklen=mytick

	 endfor

;ASCII	if writefile then begin
;ASCII	   printf, lun, 'EOF'
;ASCII	   free_lun, lun
;ASCII	endif

      !p.multi = 0                 	; reset

     ;
     ; Load the plot arrays into memory (common) for possible save 
     ; to file by user.
     ;

      uvspsave, data=a, hd=hd, abscissa=absci, $
       		start_time=struc.st9.utstart, ytitle=ytitle, $
       		xtitle=xtitle, mtitle=mtitle, type='AREA'

       xyouts,0.02,.5 , ytitle, orientation=90,alignment=.5,size=1.3,/normal 

       if hard gt 0 then xyouts,0.5,1.02, 'Ultraviolet Spectrometer / ' + $
	'Polarimeter Experiment ' + struc.st6.expnu, alignment=0.5, $
	/normal, size=1.3

   endif else begin		      ; Just a single repeat
      for i = topper,bottom,-1 do begin
         a = (data(left:right,i,0,0,0,struc.st3.d1) +   $
             (data(left:right,i,0,0,0,struc.st3.d2) * struc.st4.sum) )  $
				  / (struc.st4.sum + 1)
         widget_control, comment, set_value='UVSPAREA, Row ' + $
						strtrim(i,2), /append 
         widget_control, comment, set_value='UVSPAREA, '+ strtrim(a,2), /append
         endfor
   endelse

 

   a=0				; clear memory
endelse                       	

end
