function goes2str, xrfile, loglun, plot=plot
;
;+
;   Name: goes2str
;
;   Purpose: convert goes xray tekplot file into Yohkoh type data structure
;
;   Input Parameters:
;      xrfile - goes xray plot file (assume tektronix format)
;      loglun - open logical unit to write status info (-1 is terminal)
;
;    Output Parameters:
;      function return value is {gxr_data_rec} array
;
;    Keyword Parameters:
;       plot - if set, tekplot file is displayed during vector generation

;    History - slf, 8-Sep-92 (digitization code adapted from tekplot.pro
;		              by T. Sakurai)
;              slf, 9-mar-93 trapped some TEK file corruption problems
;			     send mail to $MAIL_MK_GXT when encountered
;	       slf,10-mar-93 use file_append to log corrupt files
;			     only mail trouble report for new entries
;	       slf,15-oct-93 if file size < 1000 bytes, assume corrupt
;			     append to corrupt file log and exit
;              slf,25-aug-94 change structure order when goes6 died.
;-
;
filename=xrfile
b=0b
begin_graph  = '1d'xb
begin_text   = '1f'xb
clear_screen = '0c'xb
asciibase = '20'xb
xbase     = '40'xb
ybase     = '60'xb
x=intarr(1000)
y=intarr(1000)
x0=[0]
y0=[0]
xb=bytarr(2)
yb=bytarr(2)
text=''
mode=0

plotting=keyword_set(plot)
openr,lun,/get_lun,filename
stat=fstat(lun)
badlog=concat_dir('$DIR_SITE_LOGS','badTEK.log')

if stat.size le 600 then begin
;     file corrupt (probably) - message, log (if first time), mail
      free_lun,lun
      mess='Possible corrupt data file: ' + xrfile
      message,/info,mess
      file_append, badlog, mess , /uniq, nlines=nlines
      if nlines gt 0 then if getenv('$MAIL_MK_GXT') ne '' then $
	 mail, mess,  users='$MAIL_MK_GXT', $
	 subj='GOES TEK file corruption...'
      message,/info,'returning empty record...'
      return,{gxr_data_rec}
endif   

on_ioerror, lpr
message,/info,'Reading file: ' + filename 

while not eof(lun) do begin
	readu,lun,b
looptop:
	if b eq clear_screen then begin
		if plotting then plot, [0,900], [0,700], /nodata, $
		xstyle=12, ystyle=12
		mode = 0
	endif else begin
		if b eq begin_graph then begin
			mode=1
		endif else begin
			if b eq begin_text then begin
				text = ''
				mode=2
			endif else begin
				if mode eq 2 then begin
					if b lt asciibase then begin
						if text eq 'Finished' then text = ' ';only for goesplot
						if plotting then xyouts,x(0),y(0),text,charsize=1.3
						mode=0
					endif else begin
						text = text + string(b)
; intercept start time - slf
						if strpos(text,'Begin') eq 0 $
						   and string(b) eq 'Z' then $
						   Time0=text
					endelse
				endif else begin
					if mode eq 1 then begin
						yb(0) = b
						readu,lun,b
						if b lt ybase then begin
							mode = 0
							goto, looptop
						endif
						yb(1) = b
						readu,lun,b
						if b lt asciibase then begin
							mode = 0
							goto, looptop
						endif
						xb(0) = b
						readu,lun,b
						if b lt xbase then begin
							mode = 0
							goto, looptop
						endif
						xb(1) = b
						x(0) = 32*(xb(0)-asciibase) + (xb(1)-xbase)
						y(0) = 32*(yb(0)-asciibase) + (yb(1)-ybase)
						loc=0
						while 1 do begin
							readu,lun,b
							if (b lt asciibase) then begin
								mode = 0
								goto, escape
							endif
							yb(0) = b
							readu,lun,b
							if (b lt ybase) then begin
								mode = 0
								goto, escape
							endif
							yb(1) = b
							readu,lun,b
							if (b lt asciibase) then begin
								mode = 0
								goto, escape
							endif
							xb(0) = b
							readu,lun,b
							if (b lt xbase) then begin
								mode = 0
								goto, escape
							endif
							xb(1) = b
							loc = loc + 1
							x(loc) = 32*(xb(0)-asciibase) + (xb(1)-xbase)
							y(loc) = 32*(yb(0)-asciibase) + (yb(1)-ybase)
						endwhile
escape:
						if loc gt 0 then begin
							if plotting then $
							   oplot,x(0:loc),y(0:loc)
; slf, build x, y vectors for later conversion
						        x0=[x0,x(0:loc)]
							y0=[y0,y(0:loc)]
						endif
						mode = 0
						goto, looptop
					endif
				endelse
			endelse
		endelse
	endelse
endwhile
lpr:


xorig=x0
yorig=y0
; slf, Sept - determine 4 subvectors of interest
; 	      these are the two goes satellites, 2 energies each
;
; subvectors identified by x0 rollover points
dx0=deriv_arr(x0)
;rollovers=where(abs(dx0) ge .2*max(x0))	;empirical worst case
rollovers=where(dx0 le .2*min(dx0))	;empirical worst case
xroll=rollovers(n_elements(rollovers)-4:n_elements(rollovers)-1)


;
; determine x and y plot scaling (data coordinates)
; caution - assumes the tekplot program is not changed

xrange=[x0(1),x0(2)]
yrange=[y0(3),y0(4)]

; extract the 'interesting' portion of x and y vectors 
x0=x0(xroll(0):*) 
x0=x0-min(x0)
y0=y0(xroll(0):*)

xroll=xroll-xroll(0)
; 
; indices of subvectors
st=xroll			; one energy start subscript
st=st+1
sp=shift(xroll,-1)-1 		; one energy stop subscript
sp(3)=n_elements(x0)-1		; last element
 
; if optional input parameter, write some status info to open unit
if n_elements(loglun) ne 0 then begin
   printf,loglun,'file:',filename
   printf,loglun,'start interval:',st
   printf,loglun,'end interval:  ',sp
   printf,loglun,'n points:      ',sp-st
   printf,loglun,'xdata range:   ',xrange
   printf,loglun,'ydata range:   ',yrange
endif

; convert plot start time to Yohkoh convention
Zulu=strpos(time0,'Z')
hh=strmid(time0,Zulu-4,2)
mm=strmid(time0,Zulu-2,2)
date=strmid(time0,6,10)
; this could use some work - mabey later
strstart=cnvtimstr(date + hh + '/' + mm,/str)
exstart =cnvtimstr(date +  hh + '/' + mm)
; create the output structure template
outstr = replicate({GXR_Data_rec},max(x0))

npoints=sp-st
satellites=where(npoints gt 50,scnt)
case 1 of
   scnt eq 2: vorder=['G6LO','G6HI','G7LO','G7HI']
   else: vorder=['G7LO','G6LO','G7HI','G6HI']
endcase

tagsi=tag_index(outstr,vorder)		; get structure field position
;

; fill the structure - caution, uses tag positions
; slf, 9-mar-1993 - add some protection/warning against corrupt TEK files


for i=0,n_elements(vorder)-1 do begin
   outstr.(tagsi(i))= -1			; initial value
   if npoints(i) gt 50 then begin
      outstr(x0(st(i):sp(i))).(tagsi(i)) $  	; only where points defined
	   = y0(st(i):sp(i))			; put corresponding y
   endif else begin
;     file corrupt (probably) - message, log (if first time), mail
      mess='Possible corrupt data file: ' + xrfile
      message,/info,mess
      file_append, badlog, mess , /uniq, nlines=nlines
      if nlines gt 0 then if getenv('$MAIL_MK_GXT') ne '' then $
	 mail, mess,  users='$MAIL_MK_GXT', $
	 subj='GOES TEK file corruption...'
   endelse
endfor

; time tag structures to nearest second
nxs=n_elements(outstr)
secarr=findgen(nxs)
sectick=6.*60.*60./nxs
secarr=long(secarr*sectick)
; convert second array to Yohkoh internal format
intsarr=anytim2ints(rebin(exstart,7,nxs),offset=secarr)
;
; update output structure with Yohkoh times 
outstr.day = intsarr.day
outstr.time = intsarr.time
;
outstr.digitize=1		; set value indicating TEK derived data

free_lun,lun
;
return,outstr
end
