pro rd_pnt, sttim, entim, pnt_data, infil=infil, flag=flag, filnam=filnam, ibyt=ibyt, indir=indir
;
;+
;NAME: 
;	rd_pnt
;PURPOSE:
;	Read the contents of the PNT data files
;INPUT:
;	sttim	- start time in any format
;	entim	- end time in any format
;OPTIONAL KEYWORD INPUT:
;	infil	- The user can specify the input file name
;		  instead of the start/end times
;	indir	- The input directory to use.  Default is $DIR_GEN_PNT
;OUTPUT:
;	pnt_data- an array structure
;KEYWORD OUTPUT:
;	flag	- Error word.
;			Value = 0 means ok
;			Value = 1 means could not find file(s)
;	filnam	- The name of the file read
;	ibyt	- The starting byte location in the file for the
;		  array of PNT data.  It is used by the ADS_INTO_PNT
;		  routine.
;		  "filnam" and "ibyt" should only be used when the
;		  time covered spans only one file
;HISTORY:
;	Written Feb-92 by M.Morrison
;	 5-Mar-92 (MDM) fix various bugs
;	 8-Mar-92 (MDM) Added "infil" option
;	20-apr-92 (JRL) Fixed "infil" option
;	23-Apr-92 (MDM) Minor adjustments to code
;	 5-May-92 (MDM) added "flag" option
;	22-May-92 (MDM) Fixed bug where trying to access the last orbit
;			of data in the PNT file.
;	18-Jul-92 (MDM) Added "filnam" and "ibyt" option
;	23-Sep-92 (MDM) Modification to grab the proper orbit when the
;			input time is exactly the start time of the orbit
;	23-Oct-92 (MDM) Revamp logic to select orbits to use.  Introducted
;			SEL_TIMRANGE.
;-
;
qdebug = 1
;
if (n_elements(indir) eq 0) then indir = '$DIR_GEN_PNT'
;
if (not keyword_set(infil)) then begin
    st_tarr = anytim2ex(sttim)
    en_tarr = anytim2ex(entim)
    ;
    st_week = ex2week(st_tarr)
    en_week = ex2week(en_tarr)
    ;
    qinfil = 0
end else begin
    st_week = 1
    en_week = 1

    st_before1st = 1
    en_afterlast = 1
    ist = 0

    qinfil = 1
end
    
;
flag = 1
pnt_struct
pnt_data = {pnt_data_rec}		;TODO - check version
;
for iweek=st_week, en_week do begin
    if (not qinfil) then begin
	filnam = string(st_tarr(6), iweek, format="('pnt', i2.2, '_', i2.2, 'a.01')")
	infil = concat_dir(indir, filnam)
    end
    if (qdebug) then print, infil
    if (not file_exist(infil)) then goto, skip
    ;
    flag = 0
    openr, lun, infil, /get_lun, /block
    rd_fheader, infil, fhead
    rd_pointer, infil, pointer
    rd_neworb_p, infil, neworb_p
    norb = n_elements(neworb_p)

    if (not qinfil) then begin
	ist = sel_timrange(neworb_p, st_tarr, st_before1st=st_before1st)
	ien = sel_timrange(neworb_p, en_tarr, en_afterlast=en_afterlast)
    end

    st_dset = neworb_p(ist).stEntry
    if (en_afterlast) then en_dset = fhead.ndatasets-1 else en_dset = neworb_p(ien).stEntry

    if (qdebug) then print, 'ist,ien,st_dset,en_dset', ist,ien,st_dset,en_dset
    ;
    n = (en_dset-st_dset + 1)
    pnt_data0 = replicate({pnt_data_rec}, n)	;TODO - get data version
    ;
    ibyt = pointer.data_section + (st_dset-1)*get_nbytes(pnt_data0(0))
    rdwrt, 'R', lun, ibyt, 0, pnt_data0
    ;
    if (iweek eq st_week) then pnt_data = pnt_data0 $
			else pnt_data = [pnt_data, pnt_data0]
    ;
    free_lun, lun
    skip:
end
filnam = infil
;
end
