;+
;NAME:
pro loadoptext,file,time,OG,verbose=verbose
;KEYWORDS:	Yohkoh Operation
;PURPOSE:
;	load OP from the file generated by OP_FIRST_GUESS.
;CALLING SEQUENCE:
;	loadoptext,'op61021d.txt',time,og
;	print,gt_day(time,/str)
;	print,gt_time(time,/str)
;	print,og
;INPUTS:
;	file:	file name
;OUTPUTS:
;	time:	DAY and TIME (array of STRUCT    = -> ANYTIM2INTS)
;	og:	OG ID (byte array)
;HISTORY:
;	Ver 1.0 96-10-22	Created by TY
;	Ver 1.01 96-10-25	Modify some bugs
;	Ver 1.1 97-02-21
;-

;Verbose Flag
	if keyword_set(verbose) then verbose=1b else verbose=0b

;Interpret OP file name
;	if strlen(file) ne 12 then message,/trace,'Illegal File Name.'
;	dir=getenv('DIR_GEN_ORBIT')+'/opog/'
;	a=findfile(dir+file)
;	if a(0) eq '' then message,/trace,'Cannot find ['+dir+file+'].'
	a=findfile(file)
	if a(0) eq '' then begin
		dir=getenv('DIR_GEN_ORBIT')+'/opog/'
		a=findfile(dir+file)
		if a(0) eq '' then message,/trace,'Cannot find ['+file+'].'
		file=dir+file
	endif
	a=rd_tfile(file)

;Get OP Start Time
	startL=3				;Line of the Starttime comment
	startIDstr='Start time             :'	;Header of the Starttime comment
	nm=strlen(startIDstr)
	if startIDstr ne strmid(a(startL),0,nm) then message,/trace,'Illegal File Format.'
	start=anytim2ints(strmid(a(startL),nm,strlen(a(startL))-nm))

;Get CE, TIME, and OG number
	p=8					;Line of the OP Start NOP
	ID=0					;CE Number
	nullbyte=bytarr(16)
	nulltime=anytim2ints(strarr(16))
	OG=nullbyte				;allocate memory for OG
	time=nulltime				;allocate memory for OG time
	while p lt n_elements(a) do begin
		IDs=string(ID,format='(I3)')	;CE string
		if strmid(a(p),0,3) eq IDs then begin
			t=byte(strmid(a(p),5,6))	;Get OG time string
			tel=byte(':')			;Insert ':'
			ts=string([t(0),t(1),tel,t(2),t(3),tel,t(4),t(5)])
			time(ID)=anytim2ints(ts)
			OG(ID)=fix(strmid(a(p),12,3))	;Get OG ID
			ID=ID+1				;CE counter
			if ID eq n_elements(OG) then begin
				OG=[OG,nullbyte]	;allocate memory
				time=[time,nulltime]	;allocate memory
			endif
		endif
		p=p+1				;Line counter
	endwhile
	IDN=ID
	time=time(0:IDN-1)			;reallocate memory
	OG=OG(0:IDN-1)				;reallocate memory

;Set DAY to the time structure
	day=start.day		;Get the day of the OP start time
	t=-1			;reference time
	for i=0,IDN-1 do begin
		if time(i).time lt t then day=day+1
		t=time(i).time
		time(i).day=day
		if verbose then print,gt_day(time(i),/str)+' '+gt_time(time(i),/str)+' '+string(OG(i),format='(I3)')
	endfor
end

