def xlim2tlim(left, right):
    left_t = spice_tools.et2pydatetime(
        spice_tools.time_at_pos(1187 * left, mccomas=True))
    right_t = spice_tools.et2pydatetime(
        spice_tools.time_at_pos(1187 * right, mccomas=True))

    return left_t, right_t
示例#2
0
def plot(filename, title, espec, rehearsal=False):
    plt.style.use('pluto-paper')
    rcParams[
        'figure.autolayout'] = False  # autolayout (default True in pluto-paper style) breaks these plots

    fig, ax = plt.subplots(figsize=figaspect(0.3))
    if rehearsal:
        cbar_CH4 = None
        cbar_He, cbar_H = N_colorbars(fig, ax, 2, fraction=0.1)
    else:
        cbar_CH4, cbar_He, cbar_H = N_colorbars(fig, ax, 3, fraction=0.1)

    plot_espec(fig,
               ax,
               cbar_H,
               cbar_He,
               cbar_CH4,
               espec,
               mccomas=True,
               rehearsal=rehearsal)

    ax.set_ylabel('Energy/Q (eV/q)')

    if rehearsal:
        ax.set_title(title)
        ax.set_xlabel('Time (UTC)')
        ax.xaxis.set_major_locator(HourLocator())
        ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
        ax.xaxis.set_minor_locator(MinuteLocator(byminute=range(0, 60, 10)))

    else:
        ax.set_title(title, pad=50)
        ax.set_xlabel('Time (UTC)')
        ax.set_xlabel('X ($R_p$)')
        ax.xaxis.set_major_locator(MultipleLocator(50))
        ax.xaxis.set_minor_locator(MultipleLocator(10))
        ax.set_xlim([-20, 105])

        time_axis = ax.twiny()
        time_axis.set_xlim(spice_tools.et2pydatetime(espec['times'][0]),
                           spice_tools.et2pydatetime(espec['times'][-1]))
        time_axis.set_xlabel('Time (UTC)')
        time_axis.xaxis.set_major_locator(HourLocator())
        time_axis.xaxis.set_major_formatter(DateFormatter('%H:%M'))
        time_axis.xaxis.set_minor_locator(
            MinuteLocator(byminute=range(0, 60, 10)))

    if args.show:
        plt.show()
    else:
        fig.savefig(filename, bbox_inches='tight')
示例#3
0
文件: pepssi.py 项目: npbarnes/PEPSSI
def load_and_plot(name):
    import matplotlib.pyplot as plt
    from matplotlib.colors import LogNorm
    import matplotlib.dates as mdates

    spec = np.load(name)
    times = [st.et2pydatetime(t) for t in spec['times']]
    responses = spec['responses']
    bins = spec['bins']

    centers = 0.5 * (bins[0, 0, 1:] + bins[0, 0, :-1])
    total_resps = np.sum(responses, axis=1)
    plt.pcolormesh(times, centers, total_resps.T, norm=LogNorm())
    plt.yscale('log')

    hours = mdates.HourLocator()
    hours_fmt = mdates.DateFormatter('%H:%M')
    minutes = mdates.MinuteLocator(byminute=[10, 20, 30, 40, 50])
    plt.gca().xaxis.set_major_locator(hours)
    plt.gca().xaxis.set_major_formatter(hours_fmt)
    plt.gca().xaxis.set_minor_locator(minutes)

    plt.figure()
    n = 23
    plt.plot(times, total_resps[:, n])
    plt.gca().xaxis.set_major_formatter(hours_fmt)

    plt.show()
示例#4
0
def find_fit_file(start_et):
    start_date = et2pydatetime(start_et)

    if start_date < datetime(year=2015, month=1, day=15):
        mission_phase = cruise_folder
    else:
        mission_phase = encounter_folder

    date_folders = [
        os.path.join(mission_phase, d) for d in os.listdir(mission_phase)
        if os.path.isdir(os.path.join(mission_phase, d))
    ]
    date_folders.sort(key=_extract_date)

    found = _find_nearest_before(date_folders, start_date)

    data_file, = [
        os.path.join(found, f) for f in os.listdir(found) if f.endswith('.fit')
    ]

    return fits.open(data_file)
示例#5
0
def plot_espec(fig,
               ax,
               cbar_H,
               cbar_He,
               cbar_CH4,
               espec,
               mccomas=False,
               rehearsal=False):
    if rehearsal:  # Use time axis instead of position
        X = [spice_tools.et2pydatetime(et) for et in espec['times']]
    else:
        X = espec['trajectory'][:, 0] / 1187.

    if mccomas and not rehearsal:
        X = -X

    Hhist, Hehist, CH4hist = color_coded_espec_pcolormesh(ax, X, espec)

    Hcb = fig.colorbar(Hhist, cax=cbar_H)
    Hecb = fig.colorbar(Hehist, cax=cbar_He, format="")
    if not rehearsal:  # There are no heavies in the rehearsal
        CH4cb = fig.colorbar(CH4hist, cax=cbar_CH4, format="")

    # add a colorbar title
    cb_pos = Hecb.ax.get_position()
    if rehearsal:
        center = cb_pos.xmax
    else:
        center = (cb_pos.xmin + cb_pos.xmax) / 2
    fig.text(center + 0.005,
             cb_pos.ymax + 0.01,
             "SCEM (Hz)",
             horizontalalignment='center',
             fontsize=15)

    if not mccomas:
        ax.invert_xaxis()

    ax.set_yscale('log')
示例#6
0
import numpy as np
import cPickle
import spice_tools
from swap import find_fit_file, bin_centers

# New Horizons Data
data = find_fit_file(spice_tools.rehearsal_start)
_t = data['time_label_spect'].data

raw_scem = data['scem_spect_hz'].data
times = [spice_tools.et2pydatetime(tt) for tt in _t['middle_et']]
positions = [
    spice_tools.pos_at_time(tt, mccomas=True) / 1187. for tt in _t['middle_et']
]
energies = data['energy_label_spect'].data[0][2:]

_sample_duration = (_t['stop_et'] - _t['start_et']) / len(energies)
assert np.allclose(_sample_duration, _sample_duration[0])
sample_duration = _sample_duration[0]

# SCEM counts with bins that have less than 3 counts per sample zeroed
hz_threshold = 3.0 / sample_duration
scem = np.where(raw_scem > hz_threshold, raw_scem, 0.)

# Simulation
with open('rehearsal_espec.pickle') as f:
    espec = cPickle.load(f)

sim_scem = (espec['H'] + espec['He']).T
sim_times = [spice_tools.et2pydatetime(tt) for tt in espec['times']]
sim_positions = espec['trajectory']
import numpy as np
import NH_tools
import spice_tools
from scipy.signal import convolve2d
from swap import find_fit_file
from espec import N_colorbars
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--save', nargs='?', const='synthetic_spectrograms.png')
args = parser.parse_args()
savename = args.save

data = find_fit_file(spice_tools.rehearsal_start)
t = data['time_label_spect'].data
times = [spice_tools.et2pydatetime(tt) for tt in t['middle_et']]
positions = [
    spice_tools.pos_at_time(tt, mccomas=True) / 1187. for tt in t['middle_et']
]
energies = data['energy_label_spect'].data[0][2:]

# suppress error and poor statistics
s_raw = data['scem_spect_hz'].data

sample_duration = (t['stop_et'] - t['start_et']) / len(energies)
s_sample = s_raw * sample_duration

samp = 3
s = np.where(s_sample > samp, s_raw, 0)
s = np.ma.masked_where(s == 0, s)