예제 #1
0
    def test_combine_evt1_files(self):

        #
        #--- make a list of all evt1 file of the year
        #
        year = 2000
        hdir = 'hrc_i_115'
        e_list = get_evt1_list(year, hdir)
        if len(e_list) == 0:
            print("Something wrong in getting files")
#
#--- combined all evt1 files of the year
#
        oname = 'test_combined_evt1.fits'
        hcf.combine_fits_files(e_list, oname)

        tstart = hcf.read_header_value(oname, 'tstart')
        tstop = hcf.read_header_value(oname, 'tstop')

        tstart = int(float(tstart))
        tstop = int(float(tstop))

        self.assertEquals(tstart, 65961070)
        self.assertEquals(tstop, 93190294)

        mcf.rm_files(oname)
예제 #2
0
def find_evt_stat(efits, chip_id=''):
    """
    find count rate, pha postion from hrc evt 1 file
    input   efits   --- hrc evt1 fits file name
            chip_id     --- chip_id for hrc_s_125 and hrc_s_90; default: 0
    output: date_obs    --- date of observation
            tstart      --- starting time in seconds from 1998.1.1
            tstop       --- stopping time in seconds from 1998.1.1
            avg         --- average of pha
            med         --- mediam of pha
            std         --- standard deviation of pha
            duration    --- duration of the evt1 data
            tcnt        --- the number of entries
            cnt_p_sec   --- counts per second
    """
    #
    #--- read values from header
    #
    date_obs = hcf.read_header_value(efits, 'date-obs')
    tstart = hcf.read_header_value(efits, 'tstart')
    tstop = hcf.read_header_value(efits, 'tstop')
    #
    #--- read data
    #
    [cols, tbdata] = hcf.read_fits_file(efits)
    #
    #--- if chip_id is given, select out the data for the chip_id
    #
    if chip_id != '':
        tbdata = hcf.select_data_with_condition(tbdata, 'chip_id', '==',
                                                chip_id)
#
#--- find stat of pha
#
    [avg, med, std, vmin, vmax, vcnt] = get_basic_stat(tbdata['pha'])
    #
    #--- find time related quantities
    #
    time = tbdata['time']
    [tavg, tmed, tstd, tmin, tmax, tcnt] = get_basic_stat(tbdata['time'])
    #
    #--- compute duration etc
    #
    duration = tmax - tmin
    if duration > 0:
        cnt_p_sec = tcnt / duration
    else:
        cnt_p_sec = 0

    return [date_obs, tstart, tstop, avg, med, std, duration, tcnt, cnt_p_sec]
예제 #3
0
def extrct_hrchk_data(start, stop):
    """
    extract hrchk fits file and get column data we need
    input:  start   --- starting time
            stop    --- stopping time
    output: out     --- pyfits data table format of extracted data from data with datamore: NEXT_IN_LINE 
    """
#
#--- extract hrchk fits files 
#
    dlist = hcf.run_arc5gl('retrieve', start, stop, dataset='flight', detector='hrc', level='0', filetype='hrchk')

    nil_list = []
    for fits in dlist:
#
#--- find datamode: we need only NEXT_IN_LINE case
#
        dmode = hcf.read_header_value(fits, 'DATAMODE')

        if dmode == 'NEXT_IN_LINE':
            nil_list.append(fits)

    if len(nil_list) > 0:
        dout = hcf.combine_and_select_data(nil_list, hk_col_list)
    else:
        dout = NULL
#
#--- empty out the temporary direcotry
#
    for ent in dlist:
        mcf.rm_file(ent)

    return dout
예제 #4
0
def add_header(infits, cname, value, datatype, comment):
    """
    if a header parameter does not exist, add it
    input:  infile      --- fits file name
            cname       --- parameter name
            value       --- parameter value
            datatype    --- data type
            comment     --- comment
    output: updated fits file
    """
    if hcf.read_header_value(infits, cname) == NULL:
        hcf.update_header(infits, cname, value=value, datatype=datatype, comment=comment)
예제 #5
0
def print_stat_results(tdir, sdir, chip_ind=''):
    """
    compute general statistiscs and dead_time correction and append to tables
    input:  tdir    --- the main directory e.g, 2010MAR
            sdir    --- the sub directory e.g., hrc_i_115
    output: <inst>_stat_results --- general stat result table. for hrc_s, _sec#_ is added
            <inst>_dead_time    --- dead time stat result table
    """
    #
    #--- hrc_s_125 and hrc_s_90 have three seciontions
    #
    if chip_ind == '':
        chip_list = ['']
    else:
        chip_list = [1, 2, 3]
#
#--- find evt1 file names
#
    ldir = tdir + sdir + '/'
    flist = hcf.get_file_list(ldir, 'evt1.fits.gz')

    for fits in flist:
        atemp = re.split('_evt1', fits)
        ss_file = atemp[0] + '_comb_ss0.fits.gz'
        hk_file = atemp[0] + '_comb_hk0.fits.gz'
        #
        #--- hrc_s_125 and hrc_s_90 needs to go sec 1 to 3, but others just spit out one result
        #
        tstart = hcf.read_header_value(fits, 'tstart')
        tstop = hcf.read_header_value(fits, 'tstop')

        [tstat, vstat, sstat, astat] = find_antico_rate(ss_file, tstart, tstop)

        [s2hvst_avg, s2hvlv_avg] = find_hvlv(hk_file)
        #
        #--- ephin data may not available after fall 2018 (added Jan 14, 2019)
        #
        try:
            [scint, sint_sig] = find_scint(tstart, tstop)
        except:
            [scint, sint_sig] = [0, 0]

        for chip_id in chip_list:

            [date_obs, tstart, tstop, avg, med, std, duration, tcnt, cnt_p_sec]   \
                                        = find_evt_stat(fits, chip_id)
            #
            #--- general statistics table output
            #
            line = date_obs + '\t'
            line = line + str(tstart) + '\t'
            line = line + str(tstop) + '\t'
            line = line + str(duration) + '\t'
            line = line + str(tcnt) + '\t'
            line = line + str(cnt_p_sec) + '\t'
            line = line + str(avg) + '\t'
            line = line + str(med) + '\t'
            line = line + str(std) + '\t'
            line = line + str(tstat[0]) + '\t'
            line = line + str(tstat[1]) + '\t'
            line = line + str(tstat[2]) + '\t'
            line = line + str(vstat[0]) + '\t'
            line = line + str(vstat[1]) + '\t'
            line = line + str(vstat[2]) + '\t'
            line = line + str(sstat[0]) + '\t'
            line = line + str(sstat[1]) + '\t'
            line = line + str(sstat[2]) + '\t'
            line = line + str(astat[0]) + '\t'
            line = line + str(astat[1]) + '\t'
            line = line + str(astat[2]) + '\t'
            line = line + str(s2hvst_avg) + '\t'
            line = line + str(s2hvlv_avg) + '\t'
            line = line + str(scint) + '\t'
            line = line + str(sint_sig) + '\n'
            #
            #--- dead time correction table output
            #
            if vstat[1] == 0:
                continue

            arate = cnt_p_sec / vstat[1]
            corr = duration * arate
            tcorr = 1.0 / corr

            arate = '%4.4f' % arate
            corr = '%4.1f' % corr
            tcorr = '%4.6f' % tcorr
            dura = '%6.1f' % duration

            line2 = str(int(tstart)) + '\t'
            line2 = line2 + date_obs + '\t'
            line2 = line2 + str(arate) + '\t'
            line2 = line2 + str(dura) + '\t'
            line2 = line2 + str(corr) + '\t'
            line2 = line2 + str(tcorr) + '\n'

            if chip_id == '':
                out = stat_dir + sdir + '_stat_results'
                out2 = stat_dir + sdir + '_dead_time'
            else:
                out = stat_dir + sdir + '_sec' + str(chip_id) + '_stat_results'
                out2 = stat_dir + sdir + '_sec' + str(chip_id) + '_dead_time'

            with open(out, 'a') as fo:
                fo.write(line)

            with open(out2, 'a') as fo:
                fo.write(line2)
예제 #6
0
def run_hrc_process(infits, inst, rsrfalv, wdthast):
    """
    create hrc evt1 file from evt0 file
    input:  infits  --- hrc evt0 fits file
            inst    --- instrument either hrc-i or hrc-s
            rsrfalv --- a value of estimated rsrfalv
            wdthast --- a value of estimated wdthast
    output: hrc evt1 fits file
    """
#
#--- find start time from the file
#
    start = hcf.read_header_value(infits, 'TSTART')
#
#--- set output file name
#
    outfits = infits.replace('evt0', 'evt1')
    outfits = outfits.replace('.gz', '')
#
#--- update header parameter values if needed
#
    add_header(infits, 'DETNAM',   value=inst,    datatype='string', comment='Detector')
    add_header(infits, 'WIDTHRES', value=wdthast, datatype='string', comment='tap-ringing correction')
    add_header(infits, 'RANGELEV', value=rsrfalv, datatype='short', \
               comment=' select between two possible calibration sets')
#
#--- set parameters depending on hrc i or hrc s
#
    mc   = re.search('hrc-i', inst)
    if mc is not None:
        paramlist = hrc_i_param(start)
    else:
        paramlist = hrc_s_param()
#
#--- set command and run hrc_process_events; first set ascds environment
#
    cmd1 = '/usr/bin/env PERL5LIB=""'
#
#--- process related environment settings
#
    cmd2 = ' punlearn ardlib;'
    cmd2 = cmd2 + ' pset hrc_process_events badfile=lev1_bad_evts.fits;'
    cmd2 = cmd2 + ' pset ardlib AXAF_HRC-I_BADPIX_FILE="' + house_keeping + 'hrci_bpix1.fits";'
    cmd2 = cmd2 + ' pset ardlib AXAF_HRC-S_BADPIX_FILE="' + house_keeping + 'hrcs_bpix1.fits";'
#
#--- actual command part
#
    cmd3 = " hrc_process_events "
    cmd3 = cmd3 + " infile="       + infits 
    cmd3 = cmd3 + " ampsatfile="   + paramlist[0]
    cmd3 = cmd3 + " ampsfcorfile=" + paramlist[1]
    cmd3 = cmd3 + " badpixfile="   + paramlist[2]
    cmd3 = cmd3 + " degapfile="    + paramlist[3]
    cmd3 = cmd3 + " evtflatfile="  + paramlist[4]
    cmd3 = cmd3 + " hypfile="      + paramlist[5]
    cmd3 = cmd3 + " obsfile="      + paramlist[6]
    cmd3 = cmd3 + " tapfile="      + paramlist[7]
    cmd3 = cmd3 + " gainfile="     + paramlist[8]
    cmd3 = cmd3 + " outfile="      + outfits
    cmd3 = cmd3 + " acaofffile=NONE"
    cmd3 = cmd3 + " clobber=yes"

    cmd  = cmd1 + cmd2 + cmd3

    try:
        bash(cmd, env=ascdsenv)
#
#--- zip the file
#
        cmd = 'gzip -f ' + outfits
        os.system(cmd)
#
#-- modify the output file name with "gz'
#
        name = outfits + '.gz'
    except:
        name = NULL

    return name