def events_per_second_plot(flist, savefig=None, ax=None): log.info("Generating EXP per s plot") d = {} for f in tqdm(flist): tab = Table.read(f, hdu=1) date = tab.meta['DATE-OBS'] exposure = tab.meta['EXPOSURE'] table_length = len(tab) if exposure == 0: continue if date in d.keys(): d[date] += table_length / exposure else: d[date] = table_length / exposure if ax is None: fig, ax = plt.subplots(1, 1, figsize=(12, 6)) plt.subplots_adjust(top=.98, right=.98) ax = niutils.plotparams(ax) created_fig = True else: created_fig = False dates = list(d.keys()) format_specifier = '%Y-%m-%dT%H:%M:%S' dates_formatted = [ datetime.datetime.strptime(s, format_specifier) for s in dates ] temp = [] for i in range(len(dates_formatted)): dtemp = datetime.datetime(year=2017, month=11, day=1) if dates_formatted[i] < dtemp: temp.append(d[dates[i]]) print(len(temp)) print(np.median(temp)) print(np.mean(temp)) #Need to fix this df = pd.DataFrame({'dates': dates_formatted, 'vals': list(d.values())}) df.sort_values('dates', inplace=True) print(df) color = niutils.get_colors()['br_colors'][-1] ax.plot_date(df['dates'], df['vals'], color=color, ls='-') ax.xaxis.set_minor_locator(MonthLocator()) ax.set_xlabel("Observation Start Date", fontsize=20) ax.set_ylabel(r'Count Rate (s$^{-1}$)', fontsize=20) if (savefig is None) and (created_fig): plt.show() elif savefig is not None: fig.savefig(savefig) else: return ax
def plot_data(fnames, column, savefig=None): if niutils.check_iter(fnames): df = merge_tables(fnames) tab_for_format = fnames[0] ntab = len(fnames) else: df = read_table(fnames) tab_for_format = fname ntab = 1 if column is None: raise ValueError("Column can not be None") df = add_datetime_col(df) fig, ax = plt.subplots(1, 1, figsize=(12*ntab, 6)) ax = niutils.plotparams(ax) if not niutils.check_iter(column): column = [column] mask = [ np.where(df[column[i]] != -9999.9)[0] for i in range(len(column)) ] colors = niutils.get_colors()['br_colors'] colors = [colors[0], colors[2]] ax.plot(df['datetime'][mask[0]], df[column[0]][mask[0]], color=colors[0], label=column[0]) ax.set_xlabel("Date", fontsize=20) ax.set_ylabel(format_ace_column(column[0], table=tab_for_format), fontsize=20) if len(column) > 1: ax, ax1 = niutils.DoubleY(ax, colors=(colors[0], colors[1])) ax1.plot(df['datetime'][mask[1]], df[column[1]][mask[1]], color=colors[1], label=column[1]) ax1.set_ylabel(format_ace_column(column[1], table=tab_for_format), fontsize=20) if max(df['day']) > 180: ax.xaxis.set_minor_locator(MonthLocator()) else: ax.xaxis.set_major_locator(MonthLocator()) ax.xaxis.set_minor_locator(MonthLocator(bymonthday=15)) myfmt = DateFormatter('%Y-%m') ax.xaxis.set_major_formatter(myfmt) if savefig is None: plt.show() else: fig.savefig(savefig)
def plot_data_split(fnames, column, savefig=None): if niutils.check_iter(fnames): df_list = [read_table(f) for f in fnames] else: raise ValueError("Must input multiple filenames") if column is None: raise ValueError("Column can not be None") df_list = [add_datetime_col(df) for df in df_list] fig, ax = plt.subplots(len(df_list), 1, figsize=(12, 5.2*len(df_list))) plt.subplots_adjust(top=.98, bottom=.07) for a in ax: a = niutils.plotparams(a) if not niutils.check_iter(column): column = [column] colors = niutils.get_colors()['br_colors'] colors = [colors[0], colors[2]] for i in range(len(df_list)): df = df_list[i] mask = [ np.where(df[column[i]] != -9999.9)[0] for i in range(len(column)) ] ax[i].plot(df['datetime'][mask[0]], df[column[0]][mask[0]], color=colors[0], label=column[0]) ax[i].set_xlabel("Date", fontsize=20) ax[i].set_ylabel(format_ace_column(column[0], table=fnames[0]), fontsize=20) if len(column) > 1: ax[i], ax1 = niutils.DoubleY(ax[i], colors=(colors[0], colors[1])) ax1.plot(df['datetime'][mask[1]], df[column[1]][mask[1]], color=colors[1], label=column[1]) ax1.set_ylabel(format_ace_column(column[1], table=fnames[0]), fontsize=20) if max(df['day']) > 180: ax[i].xaxis.set_minor_locator(MonthLocator()) else: ax[i].xaxis.set_major_locator(MonthLocator()) ax[i].xaxis.set_minor_locator(MonthLocator(bymonthday=15)) myfmt = DateFormatter('%Y-%m') ax[i].xaxis.set_major_formatter(myfmt) if savefig is None: plt.show() else: fig.savefig(savefig)
def br_earth_hist(table, bins=50, save=None): tab = Table.read(table, hdu=1) fig, ax = plt.subplots(1, 1, figsize=(12, 6)) ax = niutils.plotparams(ax) plt.subplots_adjust(top=.98, right=.98) br_earth = [b for b in tab['BR_EARTH'] if b < 180] ax.hist(br_earth, bins=bins, edgecolor='black', color=niutils.get_colors()['br_colors'][-1]) ax.set_ylabel("N Rows", fontsize=20) ax.set_xlabel('Bright Earth Angle', fontsize=20) plt.setp(ax.get_yticklabels()[0], visible=False) if save is not None: fig.savefig(save) else: plt.show()
def mkf_hist(table, key, hdu=1, bins=50, pickle_file=None, save=None): if pickle_file is None: tab = Table.read(table, hdu=hdu) else: tab = pickle.load(open(pickle_file, 'rb')) fig, ax = plt.subplots(1, 1, figsize=(12, 6)) ax = niutils.plotparams(ax) assert (key in tab.colnames) ax.hist(tab[key], bins=bins, edgecolor='black', color=niutils.get_colors()['br_colors'][-1]) ax.set_ylabel("N Rows", fontsize=20) ax.set_xlabel(key, fontsize=20) if save is not None: fig.savefig(save) else: plt.show()
def paper_plot(mkf_list): """ Go through all mkfs find out which split tables are longest in each of the four categories Plot the earth elevation and bright earth angles """ fig, ax = plt.subplots(1, 1, figsize=(12, 12)) d = { 'increasing': ['', 0], 'decreasing': ['', 0], 'touches200': ['', 0], 'localextrema': ['', 0] } for mkf in tqdm(mkf_list): tab = Table.read(mkf, hdu=1) tab_list = dfsplit(tab, 2) for t in tab_list: if len(t) <= 5: continue if touches_200(t, 'BR_EARTH'): if len(t) > d['touches200'][1]: d['touches200'][0] = t d['touches200'][1] = len(t) elif always_increasing(t, 'BR_EARTH'): if len(t) > d['increasing'][1]: d['increasing'][0] = t d['increasing'][1] = len(t) elif always_decreasing(t, 'BR_EARTH'): if len(t) > d['decreasing'][1]: d['decreasing'][0] = t d['decreasing'][1] = len(t) elif local_extrema(t, 'BR_EARTH'): if len(t) > d['localextrema'][1]: d['localextrema'][0] = t d['localextrema'][1] = len(t) else: if len(groups) == 4: print(f'Problem with groups for {mkf}') ax = niutils.plotparams(ax) colors = niutils.get_colors()['br_colors'] darkcolors = niutils.get_colors()['br_colors_dark'] for i in range(len(d.keys())): t = d[list(d.keys())[i]][0] ax.plot(t['TIME'] - t['TIME'].min(), t['BR_EARTH'], color=colors[i], lw=2) ax.plot(t['TIME'] - t['TIME'].min(), t['ELV'], color=darkcolors[i], lw=2) plt.show()
def cumulative_exposure_plot(flist, savefig=None, ax=None): log.info("Generating cumulative exposure plot") d = {} format_specifier = '%Y-%m-%dT%H:%M:%S' for f in tqdm(flist): tab = Table.read(f, hdu=1) date = tab.meta['DATE-OBS'] exposure = tab.meta['EXPOSURE'] if exposure == 0: continue date_formatted = datetime.datetime.strptime(date, format_specifier) date_formatted = date_formatted.replace(hour=0, minute=0, second=0) if date_formatted in d.keys(): d[date_formatted] += exposure else: d[date_formatted] = exposure sorted_dates = list(d.keys()) sorted_dates.sort() mindate = min(list(d.keys())) maxdate = max(list(d.keys())) iterdate = mindate - datetime.timedelta(1) date_list = [] exposure_cumulative = [] while iterdate <= maxdate + datetime.timedelta(1): date_list.append(iterdate) if len(exposure_cumulative) == 0: exposure_next = 0 elif iterdate in d.keys(): exposure_next = exposure_cumulative[-1] + d[iterdate] else: exposure_next = exposure_cumulative[-1] exposure_cumulative.append(exposure_next) iterdate = iterdate + datetime.timedelta(1) if ax is None: fig, ax = plt.subplots(1, 1, figsize=(12, 6)) plt.subplots_adjust(top=.98, right=.98, bottom=.16) ax = niutils.plotparams(ax) created_fig = True else: created_fig = False exposure_cumulative = [e / (10e10) for e in exposure_cumulative] ax.plot(date_list, exposure_cumulative, color=niutils.get_colors()['3c50_color'], lw=3) ax.xaxis.set_minor_locator(MonthLocator()) ax.set_xlabel("Observation Start Date", fontsize=20) ax.set_ylabel(r'Cumulative Expsoure ($\times10^{10}$ s)', fontsize=20) if (savefig is None) and (created_fig): plt.show() elif savefig is not None: fig.savefig(savefig) else: return ax
def plot_bkgd_spectra(br_data_list, br_ranges, sa_ranges=None, environ=None, bkg3c50=None, two_panel=False, savefig=None, zoom_top=0.9, zoom_bottom=0.3, zoom_left=0.28, zoom_right=0.92): assert(len(br_data_list) == len(br_ranges)) br_data_list = reformat_input(br_data_list) if any( [ l > 1 for l in check_length(br_data_list)[1] ] ): assert(sa_ranges is not None) sa_ranges = reformat_input(sa_ranges) assert(check_length(br_data_list) == check_length(sa_ranges)) linestyles = ['-', '--', '-.', ':'] data_flat = [] br_ranges_flat = [] sa_ranges_flat = [] ls_flat = [] for i in range(len(br_data_list)): for j in range(len(br_data_list[i])): data_flat.append(br_data_list[i][j]) br_ranges_flat.append(br_ranges[i]) if sa_ranges is not None: sa_ranges_flat.append(sa_ranges[i][j]) ls_flat.append(linestyles[j]) colors_flat = [ niutils.map_colors()[r] for r in br_ranges_flat ] if sa_ranges is None: labels_flat = [ niutils.br_label(r) for r in br_ranges_flat ] else: labels_flat = [ f'{niutils.br_label(br_ranges_flat[i])}; {niutils.sa_label(sa_ranges_flat[i])}' for i in range(len(br_ranges_flat)) ] if environ is not None: data_flat.append(environ) colors_flat.append(niutils.get_colors()['environ_color']) labels_flat.append("Environmental Model") ls_flat.append('-') if bkg3c50 is not None: data_flat.append(bkg3c50) colors_flat.append(niutils.get_colors()['3c50_color']) labels_flat.append('3C50 Model') ls_flat.append('-') if two_panel: fig, (ax, ax1) = plt.subplots(2, 1, figsize=(16, 14)) plt.subplots_adjust(hspace=.1, top=.98, right=.98, bottom=.08, left=.08) ax_list = [ax, ax1] else: fig, ax = plt.subplots(1, 1, figsize=(15, 8)) ax_list = [ax] for a in ax_list: a = niutils.plotparams(a) markers = ['.', 'o'] for i in range(len(data_flat)): df = pd.read_csv(data_flat[i], skiprows=3, delimiter=" ", header=None) df.columns = ['energy', 'energy_err', 'counts', 'counts_err'] mi = 0 for a in ax_list: a.errorbar(df['energy'], df['counts'], xerr=df['energy_err'], yerr=df['counts_err'], ls=ls_flat[i], marker=markers[mi], color=colors_flat[i], label=labels_flat[i]) mi += 1 ax.legend(edgecolor='black', fontsize=20) for a in ax_list: a.set_ylabel(r'Normalized Counts (s$^{-1}$ keV$^{-1}$)', fontsize=20) a.set_xscale('log') a.set_yscale('log') a.set_xlabel('Energy (keV)', fontsize=20) ax.xaxis.set_major_formatter(mticker.FormatStrFormatter('%.1f')) if two_panel: ax1.xaxis.set_minor_formatter(mticker.FormatStrFormatter('%.01f')) ax1.set_xlim(left=zoom_left, right=zoom_right) ax1.set_ylim(bottom=zoom_bottom, top=zoom_top) con1 = ConnectionPatch(xyA=(ax1.get_xlim()[0], ax1.get_ylim()[1]), xyB=(ax1.get_xlim()[0], ax1.get_ylim()[0]), coordsA='data', coordsB='data', axesA=ax1, axesB=ax, color='black', ls='-') con2 = ConnectionPatch(xyA=(ax1.get_xlim()[1], ax1.get_ylim()[1]), xyB=(ax1.get_xlim()[1], ax1.get_ylim()[0]), coordsA='data', coordsB='data', axesA=ax1, axesB=ax, color='black', ls='-') rect = Rectangle((ax1.get_xlim()[0], ax1.get_ylim()[0]), (ax1.get_xlim()[1]-ax1.get_xlim()[0]), (ax1.get_ylim()[1]-ax1.get_ylim()[0]), facecolor='none', edgecolor='black', alpha=.5) ax.add_patch(rect) ax1.add_patch(con1) ax1.add_patch(con2) if savefig is not None: fig.savefig(savefig) if savefig.endswith('pdf'): fig.savefig(savefig.replace('pdf', 'png')) else: plt.show()