def burst_selections(file, type, tstart=None, tstop=None, **kwargs):
    '''
    Retrieve burst selections and write them to a file.
    
    Parameters
    ----------
    file : str
        Name of the file to be written. If the file exists,
        `tstart` will be amended and new data will be appended
        to the end.
    type : str
        Type of selections to retrieve. Options include:
        'abs', 'sitl+back', or 'gls'. See
        `pymms.selections.selections` for more.
    tstart : `datetime.datetime`
        Start of the data interval. If `file` exists, the
        start time will default to the end of the last
        interval recorded. The end of the data interval
        is the current time.
    \*\*kwargs
        Any keyword accepted by `pymms.selections.selections`
    '''
    file = pathlib.Path(file)
    old_data = []
    if not file.parent.exists():
        file.parent.mkdir()
    if file.exists():
        old_data = get_start_time(file)
        tstart = old_data[-1].tstop
    if tstop is None:
        tstop = dt.datetime.now()
    
    # Get the data interval. If the file exists, start from
    # the last time in the file. Go through the current time.
    
    # Read the data
    new_data = sel.selections(type, tstart, tstop, **kwargs)
    i = 0
    while new_data[i].tstart < tstart:
        i += 1
    old_data.extend(new_data[i:])
    
    # Write the data
    sel.write_csv(file, old_data)
示例#2
0
文件: data.py 项目: Astech34/pymms
    def combined_dataframe(self):
        '''
        Combines all dataframes, downsamples to DES, which as the time index with the longest sampling period (`4.5s`).
        After that, multi-instrument metafeatures are calculated.

        Returns:
            df
        '''

        afg_df = self.afg_data()
        edp_df = self.edp_data()
        dis_df = self.dis_data()
        des_df = self.des_data()

        # Resample data
        afg_df = afg_df.reindex(des_df.index, method='nearest')
        edp_df = edp_df.reindex(des_df.index, method='nearest')
        dis_df = dis_df.reindex(des_df.index, method='nearest')

        # Merge dataframes
        df = des_df
        df = df.join(dis_df, how='outer')
        df = df.join(afg_df, how='outer')
        df = df.join(edp_df, how='outer')

        # Metafeatures
        df['T_ratio'] = df['Ti_scalar'] / df['Te_scalar']
        df['plasma_beta'] = (df['Pe_scalar'] + df['Pi_scalar']) / df['P_B']

        if self.include_selections:
            """Download SITL selections"""

            selections_path = Path(
                tempfile.gettempdir()) / 'all_selections.csv'
            data = selections_api.selections('sitl+back', self.start_date,
                                             self.end_date)
            selections_api.write_csv(selections_path, data)
            """Mark datapoints selected by a SITL as selected"""

            selections = pd.read_csv(selections_path,
                                     infer_datetime_format=True,
                                     parse_dates=[0, 1])
            selections.dropna()

            if self.include_partials:
                selections = selections[selections['discussion'].str.
                                        contains("MP", na=False)
                                        | selections['discussion'].str.
                                        contains("magnetopause", na=False)]
            else:
                selections = selections[selections['discussion'].str.
                                        contains("MP", na=False)
                                        | selections['discussion'].str.
                                        contains("magnetopause", na=False)
                                        & ~selections['discussion'].str.
                                        contains("magnetopause", na=False)]

            # Create column to denote whether an observation is selected by SITLs
            df['selected'] = False

            # Set selected to be True if the observation is in a date range of a selection
            date_col = df.index
            cond_series = df['selected']
            for start, end in zip(selections['start_time'],
                                  selections['stop_time']):
                cond_series |= (start <= date_col) & (date_col <= end)
            if self.verbose:
                print(df.loc[cond_series, 'selected'])
            df.loc[cond_series, 'selected'] = True

        return df
示例#3
0
def plot_burst_selections(sc, start_date, end_date,
                          figsize=(5.5, 7)):
    mode = 'srvy'
    level = 'l2'

    # FGM
    b_vname = '_'.join((sc, 'fgm', 'b', 'gse', mode, level))
    mms = api.MrMMS_SDC_API(sc, 'fgm', mode, level,
                            start_date=start_date, end_date=end_date)
    files = mms.download_files()
    files = api.sort_files(files)[0]

    fgm_data = from_cdflib(files, b_vname,
                           start_date, end_date)
    fgm_data[fgm_data['LABL_PTR_1']]['data'] = ['Bx', 'By', 'Bz', '|B|']
    
    # FPI DIS
    fpi_mode = 'fast'
    ni_vname = '_'.join((sc, 'dis', 'numberdensity', fpi_mode))
    espec_i_vname = '_'.join((sc, 'dis', 'energyspectr', 'omni', fpi_mode))
    mms = api.MrMMS_SDC_API(sc, 'fpi', fpi_mode, level,
                            optdesc='dis-moms',
                            start_date=start_date, end_date=end_date)
    files = mms.download_files()
    files = api.sort_files(files)[0]

    ni_data = from_cdflib(files, ni_vname,
                          start_date, end_date)
    especi_data = from_cdflib(files, espec_i_vname,
                              start_date, end_date)

    # FPI DES
    ne_vname = '_'.join((sc, 'des', 'numberdensity', fpi_mode))
    espec_e_vname = '_'.join((sc, 'des', 'energyspectr', 'omni', fpi_mode))
    mms = api.MrMMS_SDC_API(sc, 'fpi', fpi_mode, level,
                            optdesc='des-moms',
                            start_date=start_date, end_date=end_date)
    files = mms.download_files()
    files = api.sort_files(files)[0]
    ne_data = from_cdflib(files, ne_vname,
                          start_date, end_date)
    espece_data = from_cdflib(files, espec_e_vname,
                              start_date, end_date)


    # Grab selections
    abs_data = sel.selections('abs', start_date, end_date)
    sitl_data = sel.selections('sitl+back', start_date, end_date)
    gls_data = sel.selections('mp-dl-unh', start_date, end_date)

    # SITL data time series
    t_abs = []
    x_abs = []
    for selection in abs_data:
        t_abs.extend([selection.tstart, selection.tstart,
                      selection.tstop, selection.tstop])
        x_abs.extend([0, selection.fom, selection.fom, 0])
    if len(abs_data) == 0:
        t_abs = [start_date, end_date]
        x_abs = [0, 0]
    abs = {'data': x_abs,
           'DEPEND_0': 't',
           't': {'data': t_abs}}

    t_sitl = []
    x_sitl = []
    for selection in sitl_data:
        t_sitl.extend([selection.tstart, selection.tstart,
                       selection.tstop, selection.tstop])
        x_sitl.extend([0, selection.fom, selection.fom, 0])
    if len(sitl_data) == 0:
        t_sitl = [start_date, end_date]
        x_sitl = [0, 0]
    sitl = {'data': x_sitl,
            'DEPEND_0': 't',
            't': {'data': t_sitl}}

    t_gls = []
    x_gls = []
    for selection in gls_data:
        t_gls.extend([selection.tstart, selection.tstart,
                      selection.tstop, selection.tstop])
        x_gls.extend([0, selection.fom, selection.fom, 0])
    if len(gls_data) == 0:
        t_gls = [start_date, end_date]
        x_gls = [0, 0]
    gls = {'data': x_gls,
           'DEPEND_0': 't',
           't': {'data': t_gls}}

    # Setup plot
    nrows = 7
    ncols = 1
    fig, axes = plt.subplots(nrows=nrows, ncols=ncols,
                             figsize=figsize, squeeze=False)
    locator = mdates.AutoDateLocator()
    formatter = mdates.ConciseDateFormatter(locator)

    # Plot FGM
    plot_2D(especi_data, axes[0,0])
    axes[0,0].set_title(sc.upper())
    fig.axes[-1].set_label('DEF')
    axes[0,0].set_ylabel('$E_{ion}$\n(eV)')
    axes[0,0].set_xticks([])
    axes[0,0].set_xlabel('')

    plot_2D(espece_data, axes[1,0])
    fig.axes[-1].set_label('DEF\nLog_{10}(keV/(cm^2 s sr keV))')
    axes[1,0].set_ylabel('$E_{e-}$\n(eV)')
    axes[1,0].set_xticks([])
    axes[1,0].set_xlabel('')
    axes[1,0].set_title('')

    plot_1D(fgm_data, axes[2,0])
    axes[2,0].set_ylabel('B\n(nT)')
    axes[2,0].set_xticks([])
    axes[2,0].set_xlabel('')
    axes[2,0].set_title('')

    plot_1D(ni_data, axes[3,0])
    axes[3,0].set_ylabel('$N_{i}$\n($cm^{-3}$)')
    axes[3,0].set_xticks([])
    axes[3,0].set_xlabel('')
    axes[3,0].set_title('')

    plot_1D(abs, axes[4,0])
    axes[4,0].set_ylabel('ABS')
    axes[4,0].set_xticks([])
    axes[4,0].set_xlabel('')
    axes[4,0].set_title('')

    plot_1D(gls, axes[5,0])
    axes[5,0].set_ylabel('GLS')
    axes[5,0].set_ylim(0, 200)
    axes[5,0].set_xticks([])
    axes[5,0].set_xlabel('')
    axes[5,0].set_title('')

    plot_1D(sitl, axes[6,0])
    axes[6,0].set_ylabel('SITL')
    axes[6,0].set_title('')
    axes[6,0].xaxis.set_major_locator(locator)
    axes[6,0].xaxis.set_major_formatter(formatter)
    for tick in axes[6,0].get_xticklabels():
        tick.set_rotation(45)
    
    # Set a common time range
    plt.setp(axes, xlim=mdates.date2num([start_date, end_date]))
    plt.subplots_adjust(left=0.15, right=0.85, top=0.93)
    return fig, axes