def run_status_bits(efile, iname, hrc_n, date): """ create two types of bit statistics tables input: efile --- a full path to the evt1 file iname --- a bit stat data file (cumulative) hrc_n --- a bit stat data file of the evt1 file date --- the date of the evt1 file output: iname and hrc_n """ # #--- read fits file # [cols, tbdata] = hcf.read_fits_file(efile) status = tbdata['status'] # #--- initialize status bit saver # stat = [] for i in range(0, 32): stat.append(0) # #--- read status bits and counts them # k = 0 for ent in status: for i in range(0, 32): if ent[i] == True: stat[i] += 1 dat_len = len(status) # #--- print out two different files. one for indivisual output and other #--- for cumulative output from all data # line = '\nChecking STATUS bits in file: {}'.format(efile) line = line + '\n\nChecked {} events'.format(len(status)) line = line + '\n\nBit | Description | Number of Times Set (Fraction)' line = line + '\n---------------------------------------------------------------------\n' line2 = date + '\t' + str(dat_len) for i in range(0, 32): line = line + ' {0:2d} | {1} | {2:12d} ({3:.3f})'.format( i + 1, descriptor[i], stat[i], 1.0 * stat[i] / dat_len) line = line + '\n' line2 = line2 + '\t' + str(stat[i]) with open(iname, 'w') as fo: fo.write(line) # #--- append the result to the data table # with open(hrc_n, 'a') as fo: fo.write(line2 + '\n') # #--- clean up the file # hcf.remove_duplicate_from_file(hrc_n)
def extract_hrc_fits_files(time_list): """ extract hrc evt0, secondary sci, hrchk files for given time period input: time_list --- a list of lists of start and stop time output: hrc evt0 files hrc secondary science fits files hrck fits files evt0_list --- a list of extracted hrc evt0 file names sci_list --- a list of extracted secondary sci fits files hk_list --- a list of extracted hrchk fits files """ # #--- extreact hrc evt0 files # sci_list = [] hk_list = [] evt0_list = extract_fits_files(time_list, 'hrc', '0', 'evt0') if len(evt0_list) == 0: return [[], [], []] # #--- make sure that the files extracted are in the given time periods # evt0_cleaned = [] for evt0_file in evt0_list: #tstart = hcf.read_header_value(evt0_file, 'tstart') #tstop = hcf.read_header_value(evt0_file, 'tstop') [cols, fdata] = hcf.read_fits_file(evt0_file) tdata = fdata['time'] tstart = tdata.min() tstop = tdata.max() chk = 0 for k in range(0, len(time_list[0])): cstart = time_list[0][k] cstop = time_list[1][k] if ((tstart >= cstart) and (tstop <= cstop)): evt0_cleaned.append(evt0_file) chk = 1 break if chk == 0: continue # #--- extract corresponding ss and hk files # atemp = re.split('hrcf', evt0_file) btemp = re.split('_', atemp[1]) head = 'hrcf' + btemp[0] [ss_name, hk_name] = extract_supple_fits_files([[cstart], [cstop]], head) sci_list.append(ss_name) hk_list.append(hk_name) return [evt0_cleaned, sci_list, hk_list]
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]
def create_data_period(start, stop, dlist, colname='', lgc='', val=''): """ create a pair of time lists which indicate when tscpos is in positive side input: start --- starting time in mm/dd/yy,hh:mm:ss or seconds from 1998.1.1 stop --- stopping time in mm/dd/yy,hh:mm:ss or seconds from 1998.1.1 dlist --- a list of fits file names colname --- if given, the following two must be given. default: '' lgc --- logical indicator such as "<=", "==". not effective unless colname is given val --- value of the condition. not effective unless colname is given output: start_list --- a list of starting time in format of seconds from 1998.1.1 stop_list --- a list of stopping tine in format of seconds from 1998.1.1 """ # #--- check time format # if (not isinstance(start, float)) and (not isinstance(start, int)): start = hcf.convertto1998sec(start) stop = hcf.convertto1998sec(stop) time_list = [] chk = 0 for fits in dlist: # #--- extract time and tscpos information from fits file # [cols_in, hdata] = hcf.read_fits_file(fits) # #--- if an extra condition is given, run the next # if colname != '': hdata = hcf.select_data_with_condition(hdata, colname, lgc, val) else: pass tlist = list(hdata.field('TIME')) if chk == 0: time_list = tlist chk = 1 else: time_list = time_list + tlist # #--- create two lists: starting time and ending time # time_list = sort_and_clean(time_list) t_lists = find_time_span(time_list, start, stop) return t_lists
def test_run_hrc_process(self): infits = house_keeping + '/Test_data/hrcf567231386N001_evt0.fits.gz' outfits = house_keeping + '/Test_data/hrcf567231386N001_evt1.fits.gz' inst = 'hrc-i' rsrfalv = 116 wdthast = 2 run_hrc_process(infits, inst, rsrfalv, wdthast) [cols, tbdata] = hcf.read_fits_file(outfits) crsv = list(tbdata['crsv']) self.assertEquals(crsv[:10], [13, 18, 25, 22, 58, 4, 21, 41, 30, 57]) mcf.rm_file(outfits)
def test_extract_data_and_combine(self): time_list = [[553651195], [553694395]] detector = 'hrc' level = 0 filetype = 'hrchk' name = 'test_hk.fits' comp = [43, 43, 43, 43, 43] extract_data_and_combine(time_list, detector, level, filetype, name) [cols, tbdata] = hcf.read_fits_file(name) tout = tbdata['SPTPAST'] self.assertEquals(list(tout[:5]), comp) mcf.rm_file(name)
def find_start_and_end(fits): """ find start and stop time of the data table in the fits file input: fits --- the name of the fits file output: start --- starting time in seconds from 1998.1.1 stop --- stopping time in seconds from 1998.1.1 """ [cols, tbdata] = hcf.read_fits_file(fits) time = tbdata['time'] try: start = numpy.min(time) stop = numpy.max(time) except: start = 0 stop = 0 return [start, stop]
def find_hvlv(hk_file): """ find values of s2hvst and shvlv from hrc hk files input: hk_file --- a hrc hk fits file name output: s2hvst_avg --- average of s2hvst s2hvlv_avg --- average of shvlv """ [cols, tbdata] = hcf.read_fits_file(hk_file) [s2hvst_avg, med, std, vmin, vmax, vcnt] = get_basic_stat(tbdata['s2hvst']) [s2hvlv_avg, med, std, vmin, vmax, vcnt] = get_basic_stat(tbdata['s2hvlv']) # #--- make sure that the values are integer # s2hvst_avg = int(s2hvst_avg + 0.5) s2hvlv_avg = int(s2hvlv_avg + 0.5) return [s2hvst_avg, s2hvlv_avg]
def find_antico_rate(ss_file, tstart, tstop): """ find a ratio of valid/total of anti-co data input: ss_file --- science fits file name tstart --- starting time in seconds from 1998.1.1 tstop --- stopping tine in seconds from 1998.1.1 output: tstat --- statistics of tlevart column vstat --- statistics of vlevart column sstat --- statistics of shevart column astat --- statistics of antico values see get_basic_stat for output (a list of statistics) """ [cols, tbdata] = hcf.read_fits_file(ss_file) tbdata = hcf.select_data_with_condition(tbdata, 'time', ">=", tstart) tbdata = hcf.select_data_with_condition(tbdata, 'time', "<=", tstop) # #--- remove outlyers; the first several entries are not reliable # tbdata = hcf.select_data_with_condition(tbdata, 'tlevart', ">", 0) tbdata = hcf.select_data_with_condition(tbdata, 'tlevart', "<", 500) tbdata = hcf.select_data_with_condition(tbdata, 'vlevart', ">", 0) tbdata = hcf.select_data_with_condition(tbdata, 'vlevart', "<", 300) tlevart = tbdata['tlevart'] vlevart = tbdata['vlevart'] shevart = tbdata['shevart'] # #--- compute antico values # antico = vlevart / tlevart #--- array / array gives array # #--- compute basic statistics for each column # tstat = get_basic_stat(tlevart) vstat = get_basic_stat(vlevart) sstat = get_basic_stat(shevart) astat = get_basic_stat(antico) # #--- output are in a list: [avg, med, std, min, max, cnt] # return [tstat, vstat, sstat, astat]
def find_scint(tstart, tstop): """ find integrated EPHIN flux input: tstart --- starting time in seconds from 1998.1.1 tstop --- stopping time in seconds from 1998.1.1 output; avg --- average of ephin flux std --- standard deviation of flux """ # #--- extract ephin data # hcf.run_arc5gl('retrieve', tstart, tstop, dataset='flight',detector='ephin',\ level=1,filetype='ephrates') # #--- create extracted fits file list # ldir = exc_dir + 'Temp_dir/' flist = hcf.get_file_list(ldir, 'lc1.fits.gz') # #--- combine all fits files into one # hcf.combine_fits_files(flist, 'ztemp.fits', azip=0) [cols, tbdata] = hcf.read_fits_file('ztemp.fits') # #--- limit data to between tstart and tstop # tbdata = hcf.select_data_with_condition(tbdata, 'time', ">=", tstart) tbdata = hcf.select_data_with_condition(tbdata, 'time', "<=", tstop) # #--- compute the basic stat # [avg, med, std, vmin, vmax, vcnt] = get_basic_stat(tbdata['scint']) cmd = 'rm ztemp.fits' + '*' os.system(cmd) if hcf.check_file_in_dir(ldir): cmd = 'rm ' + ldir + '*' os.system(cmd) return [avg, std]
def get_dead_period(obsid): """ extract time periods which is dtf <= 0.98 input: obsid --- obsid output: pstart --- starting time pstop --- stoppping time these are +/- 2 of the center value listed on dtf fits file """ # #--- get hrc dtf fits file # dtf = hcf.run_arc5gl(start='', stop='', obsid=obsid, operation='retrieve', level='1', filetype='dtf') # #--- read out time and dtf column from the fits file # tdata = hscf.read_fits_file(dtf) tout = hscf.extract_col_data(tdata[1], ['time', 'dtf']) mcf.rm_file(dtf) # #--- find the period which match the condition (dtf<=0.98) # pstart = [] pstop = [] for k in range(0, len(tout)): if tout[k][1] <= 0.98: time = int(float(tout[k][0])) # #--- take the period to be +/- 2 of the center value listed # pstart.append(time - 2) pstop.append(time + 2) return [pstart, pstop]
def estimate_rsrfalv_wdthast(fname): """ estimate RSRFALV and WDTHAST value from a hk fits file input: fname --- hk fits file output: rval --- estimated RSRFALV wval --- esimtated WDTHAST """ [cols, tbdata] = hcf.read_fits_file(fname) rdata = tbdata['rsrfalv'] if len(rdata) > 0: ravg = rdata.mean() rval = int(2.0 * (ravg - 127) + 0.5) else: rval = 116 wdata = tbdata['wdthast'] if len(wdata) > 0: wval = int(wdata.mean()) else: wval = 2 return [rval, wval]