pro IDACUpdateProfiles,x,y,pinfo

	; If profiles window exists, update the profile(s),
	; otherwise make sure the state is saved correctly.

	;help,*pinfo,/struct
	;print,'In UpdateProfiles, pinfo.xlct:',(*pinfo).xlct
	;print,'                   pinfo.win :',(*pinfo).dwin
	;print,'                   pinfo.plf :',(*pinfo).pfl
	;print,'                   pinfo.pwin:',(*pinfo).pwin
	;print,'                  pinfo.pbase:',(*pinfo).pbase

	device,window_state=ws
	if ws[(*pinfo).pwin] eq 1 and $
		(*pinfo).pbase ne 0 then begin
		idDrawPfl = widget_info((*pinfo).pbase, $
			find_by_uname='IDACpflDraw')
		if idDrawPfl gt 0 then begin

			; Update the profile(s)

			s = size((*pinfo).ftsData)
			maxv = max((*pinfo).ftsData)
			minv = min((*pinfo).ftsData)  ; 0.
			origw = (*pinfo).dwin
			neww = (*pinfo).pwin
			nx = s[1]
			ny = s[2]
			vecx = findgen(nx)
			vecy = findgen(ny)
			wset,(*pinfo).pwin
			if (*pinfo).pfirst then begin
				; Doing new mode: draw axes
				erase

                ; CCB - figure out the data range
                plot,[0,1],[minv,maxv],/nodata,xs=4,ys=4
                drange = !y.crange

				case (*pinfo).pmode of

				1: begin	; Columns
					; x-axis=intensity, y-axis=row#
					ttl = 'Column Profile'
					xoff = drange(0) - .05*abs(drange[0])
					yoff = -0.05*(ny)
					plot,[minv,maxv],[0,ny-1],/nodata, $
						xstyle=4,ystyle=4
					; Draw x-axis: xoff is ignored
					axis,xoff,yoff,xaxis=0 , $
						;xrange = [minv,maxv], $
						xstyle=8, $
						charsize=.8, $
						xtitle='INTENSITY', $
						color=(*pinfo).pc3
					; Draw y-axis: yoff is ignored
					axis,xoff,yoff,yaxis=0 , $
						yrange=[0,ny-1], $
						ystyle=8, $
						charsize=.8, $
						ytitle='ROW', $
						color=(*pinfo).pc3
					end

				0: begin	; Rows
					; x-axis=column#, y-axis=intensity
					ttl = 'Row Profile'
					xoff = -0.05*(nx)
					yoff = drange[0] - .05*abs(drange[0])
					plot,[0,nx-1],[minv,maxv],/nodata, $
						xstyle=4,ystyle=4
					axis,xoff,yoff,xaxis=0 , $
						xrange = [0,nx-1] , $
						xstyle=8, $
						charsize=.8, $
						xtitle = 'COLUMN', $
						color=(*pinfo).pc3
					axis,xoff,yoff,yaxis=0, $
						;yrange=[minv,maxv], $
						ystyle=8, $
						charsize=.8, $
						ytitle='INTENSITY', $
						color=(*pinfo).pc3
					end
				else:
				endcase
				; "Erase" the previous title
				xyouts,.4,.95,(*pinfo).ttl,/normal,$
					color=(*pinfo).pc1,charsize=1.5
				; Draw the new title
				xyouts,.4,.95,ttl,/normal,charsize=1.5, $
					color=(*pinfo).pc3

				(*pinfo).ttl = ttl
			endif

			; Draw the thing

			if (x lt nx and y lt ny and x ge 0 $
				and y ge 0) then begin
				if not (*pinfo).pfirst then begin
					; "Erase" old vector and marker(+)
					oplot,(*pinfo).pvxo,(*pinfo).pvyo,col=(*pinfo).pc1
					plots,(*pinfo).pxo,(*pinfo).pyo,psym=1,$
						symsize=2,col=(*pinfo).pc1,thick=2
					empty
				endif else (*pinfo).pfirst = 0
			endif
			if (*pinfo).pmode then begin
				; Doing Column Profile
				; vecx is vector of data values at column x
				; vecy is findgen(ny), defined above
				vecx = (*pinfo).ftsData[x,*]
				; (*pinfo).px and .py are coordinates of
				; the (+) marker
				(*pinfo).px = (*pinfo).ftsData[x,y]
				;if((*pinfo).px lt 0.) then (*pinfo).px = 0.
				(*pinfo).py = y
			endif else begin
				; Doing Row Profile
				; vecx is findgen(nx), defined above
				; vecy is vector of data values at row y
				vecy = (*pinfo).ftsData[*,y]
				; (*pinfo).px and .py are coordinates of
				; the (+) marker
				(*pinfo).px = x
				(*pinfo).py = (*pinfo).ftsData[x,y]
				;if((*pinfo).py lt 0.) then (*pinfo).py = 0.
			endelse
			; Actually do the draw
			oplot,vecx,vecy,color=(*pinfo).pc3
			plots,(*pinfo).px,(*pinfo).py,psym=1,symsize=2,$
				color=(*pinfo).pc2,thick=2	; (+) Marker
			; Save vectors and marker coordinates
			(*pinfo).pvxo = vecx
			(*pinfo).pvyo = vecy
			(*pinfo).pxo = (*pinfo).px
			(*pinfo).pyo = (*pinfo).py
		endif
	endif else begin
		if (*pinfo).pfl eq 1 then begin
			(*pinfo).pfl = 0
			(*pinfo).pbase = 0L
		endif
	endelse

end
