def find_stat(dlist, time, interrupt_pos): """ compute statistics and return a formatted line input: dlist --- data list time --- time list interrupt_pos --- position index where the interruption started output formated data line: avg +/- std, max /max position, min/min position, the value at the interruption started """ try: # #--- find min, max and their postion in time # amin = min(dlist) amax = max(dlist) minind = dlist.index(amin) maxind = dlist.index(amax) minp = mcf.chandratime_to_yday(time[minind]) maxp = mcf.chandratime_to_yday(time[maxind]) # #--- average and std # a = numpy.array(dlist) avg = numpy.mean(a) std = numpy.std(a) # #--- format them # amin = '%2.3e' % amin amax = '%2.3e' % amax avg = '%2.3e' % avg std = '%2.3e' % std minp = round(minp, 3) maxp = round(maxp, 3) intp = '%2.3e' % dlist[interrupt_pos] line = str(avg) + ' +/- ' + str(std) + '\t' line = line + str(amax) + '\t' line = line + adjust_digit(maxp, 6, 3) + '\t\t' line = line + str(amin) + '\t' line = line + adjust_digit(minp, 6, 3) + '\t' line = line + adjust_digit(intp, 6, 3) + '\n' return line except: # #--- if the statistics failed, just return "zero's" # return '0 +/- 0, 0, 0, 0, 0, 0\n'
def get_rad_zone(period): """ read rad_zone_list input: period --- the period name of the interruption output: rad_zone --- a list of lists of radiation zone start time and stop time """ ifile = data_dir + 'rad_zone_list' data = mcf.read_data_file(ifile) rad_zone = '' for ent in data: atemp = re.split('\s+', ent) if atemp[0] == period: rad_zone = atemp[1] break if rad_zone == '': return [0, 0] else: # #--- if there are values, convert them into ydate (no year part kept) # start_list = [] stop_list = [] atemp = re.split(':', rad_zone) for line in atemp: atemp = re.split('\(', line) btemp = re.split('\)', atemp[1]) ent = re.split('\,', btemp[0]) start_list.append(mcf.chandratime_to_yday(ent[0])) stop_list.append(mcf.chandratime_to_yday(ent[1])) rad_zone = [start_list, stop_list] return rad_zone
def convert_time(time, format = 0): """ convert time format from seconds from 1998.1.1 to data of year or fractional year Input: time --- a list of time in seconds format --- if 0, convert into day of year, otherwise, fractional year Output: timeconverted --- a list of conveted time """ t_list = [] if format == 0: for ent in time: t_list.append(mcf.chandratime_to_yday(ent)) else: for ent in time: t_list.append(mcf.chandratime_to_fraq_year(ent)) return t_list
def plot_data(period, start, stop, time, le0, le1, le2, hes0, hes1, hes2, hesc, part2=''): """ plot xmm data input: period --- the name of the interruption period start --- interruption starting time stop --- interruttion ending time le0, le1, le2, hes0, hes1, hes2, hesc --- xmm data part2 --- indicator of the second pamel. if it is NOT "", it will create the second plot for the extended period output: <yyyymmdd>_xmm.png (and possibly <yyyymmdd>_xmm_pt2.png) """ # #--- find radiation periods in of the period # [rad_start, rad_stop] = get_rad_zone(period) # #--- set basic plotting parameters # fsize = 9 lsize = 1 psize = 2.0 resolution = 100 colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime',\ 'maroon', 'black', 'olive', 'yellow') markerList = ('o', '*', '+', '^', 's', 'D', \ '1', '2', '3', '4') # #--- convert start and stop time into ydate # ystart = mcf.chandratime_to_yday(start) ystop = mcf.chandratime_to_yday(stop) ytime = [] for ent in time: day = mcf.chandratime_to_yday(ent) ytime.append(day) # #--- set plotting period; if there are two plots for this period, second one start from where #--- the first plot ended # if part2 == '': xmin = ystart - 2.0 xmax = ystart + 3.0 else: xmin = ystart xmax = ystart + 5.0 # #--- find min and max of data and set y plotting range # ymin = min(le0) for dset in (le1, le2, hes0, hes1, hes2, hesc): tmax = min(dset) if ymin < ymin: ymin = tmin ymin *= 0.01 ymax = max(le0) for dset in (le1, le2, hes0, hes1, hes2, hesc): tmax = max(dset) if tmax > ymax: ymax = tmax ymax *= 1.1 # #--- set where to put the text # xdiff = xmax - xmin ydiff = ymax - ymin xtext = ystart + 0.01 * xdiff ytext = (ymax - 0.8 * ydiff) # #--- clean the plotting 'memory' # plt.close('all') mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) # #--- first panel # ax1 = plt.subplot(211) ax1.set_autoscale_on(False) ax1.set_xbound(xmin,xmax) ax1.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax1.set_ylim(ymin=ymin, ymax=ymax, auto=False) p1 = plt.semilogy(ytime, le0, color=colorList[0], lw =lsize , \ marker=markerList[0], markersize=psize, label='LE-0') p2 = plt.semilogy(ytime, le1, color=colorList[1], lw =lsize , \ marker=markerList[1], markersize=psize, label='LE-1') p3 = plt.semilogy(ytime, le2, color=colorList[2], lw =lsize , \ marker=markerList[2], markersize=psize, label='LE-2') if part2 == '': plt.text(xtext, ytext, r'Interruption', color='red') # #--- plot radiation zone markers # for i in range(0, len(rad_start)): plt.plot([rad_start[i], rad_stop[i]], [ymin ,ymin], color='red', lw='6') # #--- plot interruption start/stop markers # if part2 == '': plt.plot([ystart, ystart], [ymin, ymax], color='red', lw='3') plt.plot([ystop, ystop], [ymin, ymax], color='red', lw='3') else: plt.plot([ystop, ystop], [ymin, ymax], color='red', lw='3') # #--- add legend # ax1.legend(['LE-0', 'LE-1', 'LE-2']) ax1.set_ylabel('Counts/Sec', size=fsize) # #--- second panel # ax2 = plt.subplot(212) ax2.set_autoscale_on(False) ax2.set_xbound(xmin,xmax) ax2.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax2.set_ylim(ymin=ymin, ymax=ymax, auto=False) p4 = plt.semilogy(ytime, hes0, color=colorList[3], lw =lsize , \ marker=markerList[3], markersize=psize, label='HES-0') p5 = plt.semilogy(ytime, hes1, color=colorList[4], lw =lsize , \ marker=markerList[4], markersize=psize, label='HES-1') p6 = plt.semilogy(ytime, hes2, color=colorList[5], lw =lsize , \ marker=markerList[5], markersize=psize, label='HES-2') p7 = plt.semilogy(ytime, hesc, color=colorList[6], lw =lsize , \ marker=markerList[6], markersize=psize, label='HES-C') if part2 == '': plt.text(xtext, ytext, r'Interruption', color='red') # #--- radiation zone # for i in range(0, len(rad_start)): plt.plot([rad_start[i], rad_stop[i]], [ymin ,ymin], color='red', lw='6') # #--- interruption period # if part2 == '': plt.plot([ystart, ystart], [ymin, ymax], color='red', lw='3') plt.plot([ystop, ystop], [ymin, ymax], color='red', lw='3') else: plt.plot([ystop, ystop], [ymin, ymax], color='red', lw='3') # #--- add legend # ax2.legend(['HES-0','HES-1', 'HES-2', 'HES-C']) ax2.set_xlabel('Time (Day Of Year)', size=fsize) ax2.set_ylabel('Counts/Sec', size=fsize) # #--- x axis ticks are only shown in the bottom panel # for ax in ax1, ax2: if ax != ax2: for label in ax.get_xticklabels(): label.set_visible(False) else: pass # #-- save the plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) if part2 == '': outname = xmm_dir + str(period) + '_xmm.png' else: outname = xmm_dir + str(period) + '_xmm_pt2.png' plt.savefig(outname, format='png', dpi=resolution)
def generate_ephin_rate_plot(year): """ create ephin rate plots input: year --- year of the data mon --- month of the data <directory>/ephin_rate --- ephin data file ouput: <directory>/ephin_rate.png """ odir = web_dir + 'Plots/' + str(year) if not os.path.isdir(odir): cmd = 'mkdir -p ' + odir os.system(cmd) xname = 'Time (Day of Year: Year ' + str(year) + ')' yname = 'Count/Sec' stime = [] p4 = [] e150 = [] e300 = [] e1300 = [] for mon in m_list: directory = mon + str(year) ifile = data_dir + directory + '/ephin_rate' if not os.path.isfile(ifile): continue data = mcf.read_data_file(ifile) for ent in data: atemp = re.split('\s+', ent) try: val0 = int(float(atemp[0])) val0 = mcf.chandratime_to_yday(val0) val1 = float(atemp[1]) / 300.0 if val1 > 100000: continue val2 = float(atemp[2]) / 300.0 if val2 > 100000: continue val3 = float(atemp[3]) / 300.0 if val3 > 100000: continue val4 = float(atemp[4]) / 300.0 if val4 > 100000: continue except: continue stime.append(val0) p4.append(val1) e150.append(val2) e300.append(val3) e1300.append(val4) x_set_list = [stime, stime, stime, stime] y_set_list = [p4, e150, e300, e1300] yname_list = [yname, yname, yname, yname] title_list = ['P4', 'E150', 'E300', 'E1300'] outname = web_dir + 'Plots/' + str(year) + '/ephin_rate.png' plot_multi_panel(x_set_list, y_set_list, xname, yname_list, title_list, outname)
def generate_count_rate_plot(year): """ create count rate plots input: year --- the year of the data mon --- the month of the data output: <directory>/acis_dose_ccd<ccd>.png <directory>/acis_dose_ccd_5_7.png """ odir = web_dir + 'Plots/' + str(year) if not os.path.isdir(odir): cmd = 'mkdir -p ' + odir os.system(cmd) xname = 'Time (Day of Year: Year ' + str(year) + ')' yname = 'Count/Sec' # #--- save data sets for each ccd # data_x = [] data_y = [] # #--- plot count rates for each ccd # for ccd in range(0, 10): xdata = [] ydata = [] for mon in m_list: directory = mon + str(year) ifile = data_dir + directory + '/ccd' + str(ccd) if not os.path.isfile(ifile): continue # #--- get the data # data = mcf.read_data_file(ifile) for ent in data: atemp = re.split('\s+', ent) # #--- convert time to a year date and normalized y data to cnts/sec --- 300 sec cumurative # try: xval = float(atemp[0]) xval = mcf.chandratime_to_yday(xval) yval = float(atemp[1]) / 300.0 except: continue # #--- don't care to plot 'zero' values; so skip them # if yval == 0: continue if yval > 500: continue xdata.append(xval) ydata.append(yval) title = 'ACIS Count Rate: CCD' + str(ccd) outname = web_dir + 'Plots/' + str(year) + '/acis_dose_ccd' + str( ccd) + '.png' if len(xdata) < 1: cmd = 'cp ' + house_keeping + 'no_plot.png ' + outname os.system(cmd) else: plot_panel(xdata, ydata, xname, yname, title, outname)
def create_year_long_plot(year): """ create one year long plots for the given year. if the year is not given, use this year input: year --- year also read from <data_dir>/<slot name>_<#> output: <web_dir>/Plots/<year>/<slot name>_<#>.png """ # #--- if the year is not given, use this year, except the first 10 days of year #--- create the last year's plot # if year == '': year = int(float(time.strftime('%Y', time.gmtime()))) mday = int(float(time.strftime('%j', time.gmtime()))) if mday < 5: year -= 1 # #--- set starting and stopping time of the data interval # tstart = str(year) + ':001:00:00:00' tstart = Chandra.Time.DateTime(tstart).secs tstop = tstart + oneyear if mcf.is_leapyear(year): tstop += 86400.0 for m in range(0, 3): # #--- read data, separate into column data, and select the data only between time period specified # ifile = data_dir + data_list[m] data_set = mcf.read_data_file(ifile) data_set = mcf.separate_data_to_arrays(data_set) data_set = select_data_for_time_period(data_set, tstart, tstop) # #--- save time in day of year # time_list = [] for tval in data_set[0]: time_list.append(mcf.chandratime_to_yday(tval)) # #--- start plotting each slot # for k in range(1, len(data_set)): # #--- acacent_mtatr hold 2 sets of data # if m == 2: if k < 9: y_name = slot_name[m] + 'ynea_' + str(k - 2) else: y_name = slot_name[m] + 'znea_' + str(k - 9) else: y_name = slot_name[m] + '_' + str(k - 1) out_name = y_name + '.png' [t_list, d_list] = drop_nan(time_list, data_set[k]) xlabel = 'Day of Year (Year: ' + str(year) + ')' odir = web_dir + 'Plots/' + str(year) + '/' if not os.path.isdir(odir): cmd = 'mkdir -p ' + odir os.system(cmd) out_name = odir + out_name if len(t_list) < 1: cmd = 'cp ' + house_keeping + 'no_data.png' + ' ' + out_name os.system(cmd) continue plot_data(t_list, d_list, xlabel, y_name, out_name)