Exemplo n.º 1
0
    def test_corefit(self):
        df = helios.corefit(self.probe, self.starttime, self.endtime)
        check_data_output(df)

        starttime = datetime(2000, 1, 1, 0, 0, 0)
        endtime = datetime(2000, 1, 2, 0, 0, 0)
        with pytest.raises(RuntimeError):
            helios.corefit(self.probe, starttime, endtime)
Exemplo n.º 2
0
def fit_single_day(year, doy, probe, startdelta=None, enddelta=None):
    """
    Method to fit a single day of Helios distribution functions. This function
    is responsible for saving the results for the given day to a file.
    """
    starttime, endtime = helpers.doy2stime_etime(year, doy)
    if starttime.year != year:
        return
    parallel = True
    if args.plot:
        parallel = False
    if startdelta is not None:
        starttime += startdelta
        input('Manually setting starttime, press enter to continue')
    if enddelta is not None:
        endtime = starttime + enddelta
        input('Manually setting endtime, press enter to continue')

    try:
        corefit = helios.corefit(probe, starttime, endtime).data
        # The first corefit release has a sign error in the magnetic field.
        # If using v1, correct this
        if 'data_rate' not in corefit.keys():
            corefit[['Bx', 'By', 'Bz']] = corefit[['Bx', 'By', 'Bz']] * -1
    except RuntimeError:
        return
    distparams = helios.distparams(probe, starttime, endtime, verbose=True)
    distparams = distparams.sort_index()
    # Load a days worth of ion distribution functions
    try:
        dists_3D, I1as, I1bs, distparams = helpers_data.load_dists(
            probe, starttime, endtime)
    except RuntimeError as err:
        print(str(err))
        if 'No data available for times' in str(err):
            return
        raise

    rows = list(corefit.iterrows())
    if parallel:
        import multiprocessing
        npools = 4
        # Split up times
        rows = [rows[i::4] for i in range(npools)]
        # Add distribution functions to each list of rows
        rows = [(row, dists_3D, I1as, I1bs, distparams, probe) for row in rows]
        with multiprocessing.Pool(npools) as p:
            mapped = p.map(fit_rows, rows)
        fitlist = []
        for l in mapped:
            fitlist += l
    else:
        fitlist = fit_rows((rows, dists_3D, I1as, I1bs, distparams, probe))
    # End of a single day, put each day into its own DataFrame
    fits = pd.DataFrame(fitlist).set_index('Time', drop=True).sort_index()
    assert fits.shape[0] == corefit.shape[0]
    # Make directory to save fits
    fdir = output_dir / 'helios{}'.format(probe) / str(year)
    save_fits(fits, probe, year, doy, fdir)
def load_data(fitdir):
    fitdir = pathlib.Path(fitdir)
    dates = pd.read_csv('stream_times.csv', parse_dates=[1, 2])
    protons, alphas = [], []
    for _, row in dates.iterrows():
        # Import protons
        protons.append(helios.corefit(row['Probe'],
                                      row['Start'],
                                      row['End']).data)

        probe = row['Probe']
        protons[-1]['Probe'] = probe

        year = row['Start'].strftime('%Y')
        startdoy = int(row['Start'].strftime('%j'))
        enddoy = int(row['End'].strftime('%j'))
        # Import alphas
        this_alphas = []
        for doy in range(startdoy, enddoy + 1):
            this_alphas.append(pd.read_csv(
                fitdir /
                'helios{}'.format(probe) / '{}'.format(year) /
                'h{}_{}_{:03d}_alpha_fits.csv'.format(probe, year, doy),
                index_col=0, parse_dates=[0]))
            this_alphas[-1]['Probe'] = probe
        this_alphas = pd.concat(this_alphas)

        this_alphas = this_alphas[this_alphas.index > row['Start']]
        this_alphas = this_alphas[this_alphas.index < row['End']]

        print(this_alphas.index.min(), this_alphas.index.max(),
              protons[-1]['r_sun'].min(),
              protons[-1]['r_sun'].max())
        alphas.append(this_alphas)

    protons = pd.concat(protons)
    alphas = pd.concat(alphas)
    protons = helioshelp.calculate_derived(protons)

    def reindex(probe):
        this_p = protons[protons['Probe'] == probe]
        this_a = alphas[alphas['Probe'] == probe]
        return this_p.reindex(index=this_a.index)

    protons = pd.concat([reindex(probe) for probe in [1, 2]])
    alphas['r_sun'] = protons['r_sun']

    return protons, alphas
Exemplo n.º 4
0
def load_data():
    '''
    Load all the Helios corefit data
    '''
    starttime = datetime(1974, 1, 1, 0, 0, 0)
    endtime = datetime(1985, 1, 1, 0, 0, 0)

    # Import corefit data
    data = []
    for probe in ['1', '2']:
        corefit = helios.corefit(probe, starttime, endtime, try_download=False)
        corefit = add_probe_index(corefit, probe)
        data.append(corefit)
    corefit = pd.concat(data)
    print('Loaded corefit data')
    print('Start date:', corefit.index.min())
    print('End date:', corefit.index.max())
    print('Number of data points:', corefit.shape[0])
    print('Keys:')
    print(corefit.keys())
    return corefit
Exemplo n.º 5
0
"""
Importing data
==============

A short example showing how to import and plot plasma data.
"""

from datetime import datetime, timedelta
import heliopy.data.helios as helios
import matplotlib.pyplot as plt

starttime = datetime(1976, 4, 5, 0, 0, 0)
endtime = starttime + timedelta(hours=12)
probe = '2'

data = helios.corefit(probe, starttime, endtime)

print(data.keys())

fig, axs = plt.subplots(3, 1, sharex=True)
axs[0].plot(data['n_p'])
axs[1].plot(data['vp_x'])
axs[1].plot(data['vp_y'])
axs[1].plot(data['vp_z'])
axs[2].plot(data['Tp_perp'])
axs[2].plot(data['Tp_par'])

for ax in axs:
    ax.legend()
plt.show()
Exemplo n.º 6
0
import pandas as pd
import numpy as np
import astropy.units as u
import astropy.constants as const

from heliopy.data import helios

import vis_helpers as helpers
import helioshelp

probe = '1'
starttime = datetime(1975, 3, 7)
endtime = starttime + timedelta(days=20)

alphas = helpers.load_alphafit(probe, starttime, endtime)
protons = helios.corefit(probe, starttime, endtime, try_download=False).data
protons = protons.reindex(alphas.index)
protons = helioshelp.calculate_derived(protons)

alphas[['Bx', 'By', 'Bz', '|B|']] = protons[['Bx', 'By', 'Bz', '|B|']]
alphas['Beta_par'] = alphas['Ta_par'] * const.k_B.value * alphas['n_a'] * 1e6 / (alphas['|B|']**2 * 1e-18 / (2 * const.mu0))
alphas['Tani'] = alphas['Ta_perp'] / alphas['Ta_par']
print(alphas.keys())

akwargs = {'alpha': 1, 'color': 'C0', 's': 1}
pkwargs = {'alpha': 0.5, 'color': 'C3', 's': 1}
fig, axs = plt.subplots(nrows=7, sharex=True, figsize=(10, 12))

ax = axs[0]
ax.scatter(alphas.index, alphas['va_x'], **akwargs)
ax.scatter(protons.index, protons['vp_x'], **pkwargs)
Exemplo n.º 7
0
 def get_imported_data(self):
     data = helios.corefit(self.probe, self.start_datetime,
                           self.end_datetime)
     return data.data
Exemplo n.º 8
0
def helios_data(start_date: str = '27/01/1976', duration: int = 15, start_hour: int = 0, probe: int = 2):
    start_datetime = datetime.strptime(start_date + '/%i' % start_hour, '%d/%m/%Y/%H')
    end_datetime = start_datetime + timedelta(hours=duration)
    data = helios.corefit(probe, start_datetime, end_datetime)
    return data.data
Exemplo n.º 9
0
import pandas as pd
import numpy as np

from heliopy.data import helios

import vis_helpers as helpers
from helpers import mplhelp
from plot_fitted_dist_alphas import plot_dist_time

# Set probe and dates to compare here
probe = '2'
starttime = datetime(1976, 4, 15, 0, 0, 0)
endtime = starttime + timedelta(days=10)

alphas = helpers.load_alphafit(probe, starttime, endtime)
protons = helios.corefit(probe, starttime, endtime).data

print('Alpha points:', alphas.shape[0])

fig, axs = plt.subplots(nrows=6, sharex=True, figsize=(6, 10))

ax = axs[0]
ax.plot(protons['vp_x'])
ax.plot(alphas.index, alphas['va_x'])

ax = axs[1]
ax.plot(protons['vp_y'])
ax.plot(alphas['va_y'])

ax = axs[2]
ax.plot(protons['vp_z'])