pro mk_pnt, infil0, outfil, week=week, run_time=run_time, append=append, usefid=usefid
;
;+
;NAME:
;	mk_pnt
;PURPOSE:
;	Generate the PNT data file from the ADA files
;INPUT:
;	infil0	- A string array with a list of the ADA file
;		  names.  It should include the directory
;		  path.  The inputfiles can be derived if the
;		  "week" option is used.
;	outfil	- The output filename to create.  The output
;		  file can be derived if the "week" option is used
;OPTIONAL KEYWORD INPUT:
;	week	- a string with the year/week if the format
;		  91_49 where 91 is the year, 49 is the week
;	append	- If set, try to just append the files in "infil"
;		  list which are not already in the PNT file.
;	usefid	- If set, use the FID time in the orbit pointer
;		  (so that it can be recognized when used with
;		  the /append switch)
;HISTORY:
;	Written 13-Feb-92 by M.Morrison
;	Ver 0.200
;	Ver 0.210 (MDM) - Made correction to STENTRY value in orbit pointer
;			  section (needed to be integer*4 and was off by 1)
;	Ver 0.30 (MDM)	- TFSS needed to have the proper bits extracted before
;			  taking the average
;	Ver 1.00 13-Mar-92 (MDM) - Adjusted extraction from ADA files to 
;			  not use the same datasets twice if they overlap
;			  in successive data files.
;	Ver 1.01 26-May-92 (MDM) - Changed so that if outfil is not passed
;			  the name is built based on the first file.
;	Ver 1.02 31-Jul-92 (MDM) - Removed copying SC_PNTG field into PNT
;			  structure - the ADS data is being written there and
;			  we want to avoid confusion
;       Ver 1.03 15-Oct-92 (MDM) - Modified to allow appending to an existing
;                                   PNT file.
;	Ver 1.04  9-Nov-92 (MDM) - CHMOD +W on the output file
;	Ver 1.05 11-Nov-92 (MDM) -  Moved NORBIT=0 command since if an append
;                                   option was overridden (Files out of order)
;                                   then it was appending to the ORBIT_ARR
;                                   incorrectly.
;                                 - Added /USEFID switch.  This will force the
;                                   ORBIT_ARRAY time/day entry to be the input
;                                   FileID.  This should only be used when
;                                   generating temporary OBS logs
;	Ver 1.06 11-Nov-92 (MDM)  - Use the LAST file in "infil" to figure
;				    out the week number when the output file
;				    is not specified.
;	Ver 1.07 19-Nov-92 (MDM) -  Patch to handle cases when there are no
;				    datasets in the ADA file (most likely the
;				    ADA file is garbage and was not finished)
;		 10-Feb-93 (MDM) -  Patched bug which was not allowing the
;				    input parameter "outfil" to be useable
;		 11-Feb-93 (MDM) -  Similar fix to 10-Feb-93
;		 29-Mar-93 (MDM) -  Adding debugging print statements
;	Ver 1.08  8-Jul-93 (MDM) -  Corrected the generation of the output file
;				    name when not specified.  It was sometimes
;				    using the weekID for the NEXT week.
;	Ver 1.09 14-Dec-93 (MDM) -  Modified to correct for the case where
;				    An input file is totally contained in a 
;				    previous file
;-
;
start_time = systime(1)
if (n_elements(infil0) ne 0) then infil = infil0
;
progVerNo = 1.090*1000
progName = 'MK_PNT.PRO'
;
if (n_elements(outfil) ne 0) then outfil_final = outfil
if (keyword_set(week)) then begin
    infil = findfile( concat_dir(finddir(week+'a'), 'ada*'))
    if (infil(0) eq '') then begin
	print, 'MK_PNT: No files found - stopping'
	stop
    end
    infil2 = findfile( concat_dir(finddir(week+'b'), 'ada*'))
    if (infil2(0) ne '') then infil = [infil, infil2]
    ;
    weekid = string(week, fix(progverno/1000), format="(a, 'a.', i2.2)")
    outfil       = concat_dir('$DIR_REFORMAT_SC', 'pnt'+weekid)
    outfil_final = concat_dir('$DIR_GEN_PNT', 'pnt'+weekid)
end
;
if (n_elements(outfil) eq 0) then begin
    ;rd_roadmap, infil(n_elements(infil)-1), roadmap
    rd_roadmap, infil(n_elements(infil)/2), roadmap
    tarr = anytim2ex(roadmap(0))
    week = ex2week(tarr)
    weekid = string(tarr(6), week, fix(progverno/1000), format="(i2.2, '_', i2.2, 'a.', i2.2)")
    outfil       = concat_dir('$DIR_REFORMAT_SC', 'pnt'+weekid)
    outfil_final = concat_dir('$DIR_GEN_PNT', 'pnt'+weekid)
end
;
;----- Call subroutines that define the structures
;
obs_struct
att_struct
pnt_struct, pnt_data=pnt_data
ref_struct, ref
;
;----- Diagnostic variables
;
qprint = 1
qdebug = 1
;
;---------------------------------------- General Stuff
;
lunout = 2	;output observing log
lun = 3		;for reading the data file
;
gen_struct, file_header=fhead
;
norbitmax       = 31*15
orbit_header    = {obs_neworb_hd_rec}
orbit_arr       = replicate({obs_newOrbit_Rec}, norbitmax)
;
qfirst_time = 1
first_time      = 0
first_day       = 0
last_time       = 0
last_day        = 0
;
rsiz = ref.PNT_Fil_Rec_Siz
;
qnew_outfil = 1
if (keyword_set(append) and file_exist(outfil_final)) then begin
    qnew_outfil = 0

    cmd = 'rm -f ' + outfil               ;remove any copy of the file that might already exist
    spawn, cmd
    cmd = 'cp ' + outfil_final + ' ' + outfil
    spawn, cmd

    openu, lunout, outfil, /block, /get_lun

    rd_pointer, lunout, fpoint
    rd_fheader, lunout, fheader
    rd_neworb_p, outfil, orbit_arr0, orbit_header
    norbit = orbit_header.nOrbitRec
    orbit_arr(0:norbit-1) = orbit_arr0
    bytout = fpoint.totbytes                            ;point at end of file
    bytout0 = fpoint.data_section
    ndatasets = fheader.ndatasets

    rd_pnt, orbit_arr(norbit-1), orbit_arr(norbit-1), last_roadmap	;get times for last entry to make sure there
									;is no overlap below - should get one orbit
									;with this command

    qfirst_time = 0
    first_time      = orbit_arr(0).time
    first_day       = orbit_arr(0).day

    break_file, infil, dsk_log, dir_in, filnam, ext
    fids = strmid(filnam, 3, 6)+ext
    tim2orbit, fid2ex(fids), wid=wid_arr			;added 11-Nov-92 - get all files for the same week
    ss = where(wid_arr eq wid_arr(n_elements(wid_arr)-1))
    infil = infil(ss)

    break_file, infil, dsk_log, dir_in, filnam, ext
    fids = strmid(filnam, 3, 6)+ext
    fids_already = ex2fid(anytim2ex(orbit_arr0))
    ss = where_arr(fids, fids_already)
    nn = n_elements(ss)
    if (max(ss) ne nn-1) then begin
	qnew_outfil = 1				;something was done out of order
						;want ss to start at 0 and increase by 1
						;0,1,2,3,4 is ok, 0,1,5,6,7 is not ok - want files 2,3,4 in order
	print, 'MK_PNT: Cannot simply append since there are files out of order'
	print, 'MK_PNT: Making a complete new file.
    end else begin
	if (n_elements(ss) eq n_elements(infil)) then begin
	    print, 'MK_PNT: No new files found.  Returning'
	    close, 2
	    return
	end
        infil = infil(nn:*)            ;only do the new files
	print, 'MK_PNT: Skipping the first ', nn, ' files'
    end
end
if (qnew_outfil) then begin
    print, 'Opening a new output file: ', outfil
    open_da_file, lunout, '', '', outfil, rsiz, bytout, fpoint, /nolower

    fpoint.opt_section	= bytout
    rdwrt, 'W', lunout, bytout, rsiz, Orbit_header, 1
    rdwrt, 'W', lunout, bytout, rsiz, Orbit_arr, 1
    fpoint.data_section	= bytout
    bytout0 = bytout	;first entry record number

    qfirst_time = 1
    ndatasets = 0L
    norbit          = 0		;moved here 11-Nov-92
end
;
conv_rate = [3, 0, 1, 3, 2, 3]		;gt_dp_rate gives 1:low, 2:med, 4:high
;
nfil = n_elements(infil)
for ifil=0,nfil-1 do begin
    if (qdebug) then print, 'MK_PNT working on file: ', infil(ifil)

    rd_fheader, infil(ifil), fhead0
    if (fhead0.ndatasets eq 0) then goto, skip		;mdm added 19-Nov-92
    rd_ada, infil(ifil), -1, index, data, roadmap
    ss = where(tag_names(data) eq 'SC_PNTG')
    qsc_pntg = (ss(0) ne -1)

    if (keyword_set(usefid)) then begin
	break_file, infil(ifil), dsk_log, dir_in, filnam, ext
	fid = strmid(filnam, 3, 6)+ext
	daytim = anytim2ints( fid2ex(fid) )
	orbit_arr(norbit).time = daytim.time
	orbit_arr(norbit).day  = daytim.day
    end else begin
	orbit_arr(norbit).time = index(0).gen.time
	orbit_arr(norbit).day  = index(0).gen.day
    end
    orbit_arr(norbit).stEntry = ndatasets + 1	;dataset # for which this orbit starts (start counting at 1)
    norbit = norbit + 1

    ist = 0
    if (ndatasets ne 0) then begin
	ltim = last_roadmap(n_elements(last_roadmap)-1)
	ist = tim2dset(roadmap, ltim)			;find where the time of the last dataset in
							;the previous file falls within the current file
							;This is needed because there is overlap in the ADA and CBA files.
	del = int2secarr(roadmap(ist), ltim)
	if (del eq 0) then ist = ist + 1		;if the times match, then move forward one dataset
	ist = ist(0)
	if (ist ge n_elements(index)) then begin
	    print, 'The input file is totally contained in a previous file'
	end else begin
	    if (qdebug) then print, 'Starting dataset # ', ist, ' == ', fmt_tim(index(ist))
	end
    end

    n = n_elements(index)
    for idset=ist,n-1 do if (gt_dp_mode(index(idset)) ne 12) then begin	;avoid night
	status = 0

	if (gt_dp_mode(index(idset)) eq 9) then status = status + 1	;set bit 1 - flare mode
	status = status + conv_rate( gt_dp_rate(index(idset))<5 )
	
	pnt_data.time	= index(idset).gen.time
	pnt_data.day	= index(idset).gen.day

	for i=0,2 do pnt_data.iru(i) = total(data(idset).iru(i,*))/8
	for i=0,1 do pnt_data.tfss(i) = total( mask(data(idset).tfss(i,*), 4, 12) )/4
	nx = data(idset).hxa_xnum(0)
	ny = data(idset).hxa_ynum(0)
	if ((nx gt 0) and (ny gt 0) and (nx lt 31) and (ny lt 31)) then begin	;avoid cases where no HXA addresses are available
	    ;TODO - average the two repeats per MF?
	    pnt_data.hxa(0)	= data(idset).hxa_addr(0,0)
	    pnt_data.hxa(1)	= data(idset).hxa_addr(nx-1, 0)
	    pnt_data.hxa(2)	= data(idset).hxa_addr(nx, 0) - 2048
	    pnt_data.hxa(3)	= data(idset).hxa_addr((nx+ny-1)<31, 0) - 2048		;-2048 to clear the set bit marking Y
	    status = status + 8		;set bit 3
	end
	;;if (qsc_pntg) then for i=0,2 do pnt_data.sc_pntg(i) = data(idset).sc_pntg(i)		;removed 31-Jul-92
	pnt_data.status	= status

	rdwrt, 'W', lunout, bytout, rsiz, pnt_data, 1
	ndatasets = ndatasets + 1

	if (qfirst_time) then begin
	    first_day = pnt_data.day
	    first_time = pnt_data.time
	    qfirst_time = 0
	end
	last_day  = pnt_data.day
	last_time = pnt_data.time
    end

    last_roadmap = roadmap
    skip:
end
;
maxsamps = get_nbytes(pnt_data)
totbytes = bytout
orbit_header.nOrbitRec = norbit
bytout = fpoint.opt_section
rdwrt, 'W', lunout, bytout, rsiz, orbit_header, 1
rdwrt, 'W', lunout, bytout, rsiz, orbit_arr, 1

;-------------------- Write out pointer and file header
;

fpoint.TotBytes		= totbytes

fhead.first_Time	= first_time
fhead.first_Day		= first_day
fhead.last_Time		= last_time
fhead.last_Day		= last_day

fhead.nDataSets		= nDataSets
fhead.maxSamps		= maxSamps

file_id = ' '	;TODO - make it something else?
wrt1p_fh, lunout, fpoint, fhead, rsiz, progName, progVerNo, file_id, 'PNT', 'All'
;
close, lunout
;
if (n_elements(outfil_final) ne 0) then if (outfil_final ne outfil) then begin
    cmd = 'rm -f ' + outfil_final               ;remove any copy of the file that might already exist
    spawn, cmd
    cmd = 'mv ' + outfil + ' ' + outfil_final
    spawn, cmd
    cmd = 'chmod +w ' + outfil_final
    spawn, cmd
    outfil = outfil_final
end
;
end_time = systime(1)
run_time = (end_time-start_time)/60.
print, 'MK_PNT took', run_time, ' minutes to run'
;
end
