	PRO RdObsDat, fileNa, which, logRecPtr1, logRecPtr2, ukey, $
		key=key, maxLogRec=maxLogRec, fid=fid, sxtp=sxtp, $
		sxtf=sxtf, hxt=hxt, wbs=wbs, bcs=bcs
;	-------------------------------------------------------------
;+							22-Oct-91
;	NAME: 
;		RdObsDat
;	PURPOSE:
;		Read and return observing log data for the requested
;		times and key values.  Note: the procedure buffers 
;		the data requests into several reads and searchs 
;		(if requested).  Each buffered read is appended to
;		the prior read until the full request is satisfied.
;	CALLING SEQUENCE:
;		RdObsDat, fileNa, w, logRecPtr1, logRecPtr2, ukey, 
;		[key=key, maxLogRec=maxLogRec, fid=fid, sxtp=sxtp, 
;		sxtf=sxtf, hxt=hxt, wbs=wbs, bcs=bcs]
;	INPUT:
;		fileNa	observing log file name
;		which	reserved parameter for control of low-level
;			access routines.
;		logRecPtr1	Start Rec in units of 32-byte offset 
;		logRecPtr2	End Record in units of 32-byte offset
;		ukey		logical flag for presence of Key
;	Keyword/INPUT:
;		key 		list of search keys
;		maxLogRec	buffer size for reading the observing
;				log in units of 32-byte logical records.
;	Keyword/OUTPUT:
;		fid	file identification corresponding to the time 
;			tag on the data records.
;		sxtp	data-index for partial frame image vector
;		sxtf	data-index for Full frame image vector
;		hxt	data-index for HXT data
;		wbs	data-index for WBS data
;		bcs	data-index for BCS data
;	HISTORY:
;		written 14-June-91 by GAL
;		updated 12-Aug-91, for updated Rd_Obs routine.
;		updated 13-Aug-91, for w_h and bcs parameters
;		updated 20-Aug-91, for hxt and wbs as seperate items
;		updated 16-Oct-91 (MDM) to check to see if the output
;			vectors are defined by looking at how many
;			bytes the variable uses instead of using
;			n_elements so that it can be "re-initialized"
;			by setting it to a byte scaler
;		updated 22-Oct-91, GAL to call KeySea function instead of
;			KeySearch the procedure.
;-
;	-------------------------------------------------------------
;	ON_ERROR, 2	;force a return to caller if error occurs

;	If maxLogRec is undefined set to default value:
        IF (NOT (KEYWORD_SET(maxLogRec))) THEN BEGIN	;undefined
	  maxLogRec = 1000L		;maximum number of log rec
	ENDIF

	nrecs = logRecPtr2 - logRecPtr1 + 1	;number of read recs
	lpCount = nrecs/maxLogRec		;n times of maxLogRec
	rCount  = nrecs MOD maxLogRec		;remainder count
	IF (lpCount eq 0)THEN BEGIN
	  repeatcount = 1		;all log recs on 1st read call
	ENDIF ELSE BEGIN
	  IF (rCount eq 0)THEN BEGIN
	    repeatCount = lpCount	;whole number of read calls
	  ENDIF ELSE BEGIN
	    repeatCount = lpCount + 1	;n read calls plus remainder
	  ENDELSE
	ENDELSE

	ptr1 = logRecPtr1		;initialize pointers
	ptr2 = logRecPtr1			
	FOR i=0, repeatCount-1 DO BEGIN
	  IF (nrecs gt maxLogRec) THEN BEGIN	;define Ptr2
	    IF (i eq repeatCount-1) THEN BEGIN 	;last time thru or 1st
	      ptr2 = ptr2 + rCount -1
	    ENDIF ELSE BEGIN
	      ptr2 = maxLogRec + (maxLogRec * i) + logRecPtr1
	    ENDELSE
	  ENDIF ELSE BEGIN
	    ptr2 = logRecPtr1 + nrecs -1	;requested recs < max
	  ENDELSE
	  ptr1 = (i * maxLogRec) + logRecPtr1   
	  IF (i ne 0) THEN ptr1 = ptr1 + 1	;adjust ptr1

;	  Initialize the data present flags:
	  tfid = fix(0)
	  tsxtp= fix(0)
	  tsxtf= fix(0)
	  tw_h = fix(0)			;wbs and hxt from Rd_Obs
	  thxt = fix(0)
	  twbs = fix(0)
	  tbcs = fix(0)

	  ; Print, 'rdobsdat; memory loop count=',i,' Ptr1 and Ptr2=', $
	  ;	Ptr1, Ptr2

	  Rd_Obs, fileNa,which , fid=tfid, sxtp=tsxtp, sxtf=tsxtf, $
			w_h=tw_h, $
			bcs=tbcs, istart=Ptr1, iend=Ptr2

;	  Check data present flags:
	  itfid = SIZE(tfid)
	  itsxtp= SIZE(tsxtp)
	  itsxtf= SIZE(tsxtf)
	  itw_h = SIZE(tw_h)
	  itbcs = SIZE(tbcs)
;
	  
;	  IF tw_h logical defined split into hxt and wbs:
	  IF (itw_h(itw_h(0)+1) ne 2) THEN BEGIN	;data present
	    IF (N_Elements(hxt) eq 0) THEN BEGIN	;1st time
	      dum = CreHXTwbs(/hxt)		;create structure
	    ENDIF 
	    IF (N_Elements(wbs) eq 0) THEN BEGIN	;1st time
	      dum = CrehxtWBS(/wbs)		;create structure
	    ENDIF
	    Split_w_h, tw_h, HXT=thxt, WBS=twbs
          ENDIF
	  ithxt = SIZE(thxt)			;check data present flags:
	  itwbs = SIZE(twbs)
	      
	  IF (itwbs(itwbs(0)+1) ne 2) THEN BEGIN	;data present
	    IF uKey THEN BEGIN			;user key present
	      idx = KeySea(key, twbs)		;search for key
	      if (idx(0) ne -1) then twbs = twbs(idx) else twbs = 0l
	    ENDIF
	    itwbs = SIZE(twbs)		;recheck data present
	    IF (itwbs(itwbs(0)+1) ne 3) THEN BEGIN	;valid Key Search
	      IF (N_Elements(wbs) eq 0) THEN BEGIN
	        wbs = twbs
	      ENDIF ELSE BEGIN		;build vectors
	        wbs = [wbs,twbs]
	      ENDELSE
	    ENDIF
	  ENDIF	  
	  IF (ithxt(ithxt(0)+1) ne 2) THEN BEGIN	;data present
	    IF uKey THEN BEGIN			;user key present
	      idx = KeySea(key, thxt)		;search for key
	      if (idx(0) ne -1) then thxt = thxt(idx) else thxt = 0l
	    ENDIF
	    ithxt = SIZE(thxt)		;recheck data present
	    IF (ithxt(ithxt(0)+1) ne 3) THEN BEGIN	;valid Key Search
	      IF (get_nbytes(hxt) le 2) THEN BEGIN
	        hxt = thxt
	      ENDIF ELSE BEGIN		;build vectors
	        hxt = [hxt,thxt]
	      ENDELSE
	    ENDIF
	  ENDIF	  

	  IF (itfid(itfid(0)+1) ne 2) THEN BEGIN	;data present
	    IF (get_nbytes(fid) le 2) THEN BEGIN
	      fid = tfid				;1st time
	    ENDIF ELSE BEGIN
	      fid = [fid,tfid]				;append data
	    ENDELSE
	  ENDIF
	  IF (itsxtp(itsxtp(0)+1) ne 2) THEN BEGIN	;data present	
	    IF uKey THEN BEGIN			;user key present
	      idx = KeySea(key, tsxtp)		;search for key
	      if (idx(0) ne -1) then tsxtp = tsxtp(idx) else tsxtp = 0l
	    ENDIF
;	    STOP,'**** check tsxtp key search '
	    itsxtp = SIZE(tsxtp)		;recheck data present
	    IF (itsxtp(itsxtp(0)+1) ne 3) THEN BEGIN	;valid key search
	      IF (get_nbytes(sxtp) le 2) THEN BEGIN	
	        sxtp = tsxtp				;1st time
	      ENDIF ELSE BEGIN
	        sxtp= [sxtp,tsxtp]			;append data
print,'rdobsdat: accumulated SXTP records>>',n_elements(sxtp) 
	      ENDELSE
	    ENDIF
	  ENDIF
	  IF (itsxtf(itsxtf(0)+1) ne 2) THEN BEGIN	;data present
	    IF uKey THEN BEGIN			;user key present
	      idx = KeySea(key, tsxtf)		;search for key
	      if (idx(0) ne -1) then tsxtf = tsxtf(idx) else tsxtf = 0l
	    ENDIF
	    itsxtf = SIZE(tsxtf)		;recheck data present
	    IF (itsxtf(itsxtf(0)+1) ne 3) THEN BEGIN	;valid key Search
	      IF (get_nbytes(sxtf) le 2 ) THEN BEGIN
	        if itsxtf(0) ne 0 then sxtf = tsxtf
	      ENDIF ELSE BEGIN
	        sxtf= [sxtf,tsxtf]
print,'rdobsdat: accumulated SXTF records>>',n_elements(sxtf) 
	      ENDELSE
	    ENDIF
	  ENDIF
;		NOTE: SHOULD THE SAME SEARCH KEY BE USED FOR BOTH SXT*
;		AND W_H (or BCS).  HOW SHOULD THIS BE HANDLED.
;		PERHAPS BY USING THE data-vector-TYPE SWITCH ONE
;		CAN CONTROL THE USE OF SEARCHING.
;
	  IF (itbcs(itbcs(0)+1) ne 2) THEN BEGIN	;data present
	    IF uKey THEN BEGIN			;user key present
	      idx = KeySea( key, tbcs)		;search for key
	      if (idx(0) ne -1) then tbcs = tbcs(idx) else tbcs = 0l
	    ENDIF
	    itbcs = SIZE(tbcs)		;recheck data present
	    IF (itbcs(itbcs(0)+1) ne 3) THEN BEGIN	;valid Key Search
	      IF (get_nbytes(bcs) le 2) THEN BEGIN
	        bcs = tbcs
	      ENDIF ELSE BEGIN		;build vectors
	        bcs = [bcs,tbcs]
	      ENDELSE
	    ENDIF
	  ENDIF
	ENDFOR

	RETURN
	END
