示例#1
0
def invert_example(axes=[], show=True):
    """Test case for reverse engineering rough solar wind params

    We look here at the 1989 storm as studied by Nagatsuma (2015)
    and Boteler (2019)
    """
    import datetime as dt
    import spacepy.toolbox as tb
    # fetch doesn't respect the days (it grabs 1 month at a time)
    # but we'll give the days we're interested in for reference
    st_tup = (1989, 3, 12)
    en_tup = (1989, 3, 15)
    ky_dat = kyo.fetch('dst', st_tup, en_tup)
    st = dt.datetime(*st_tup)
    en = dt.datetime(*en_tup)
    inds = tb.tOverlapHalf([st, en], ky_dat['time'])
    vbz = inverseBurton(ky_dat['dst'][inds], rectify=True)
    vbz_OB = inverseOBrienMcPherron(ky_dat['dst'][inds])

    if not axes:
        fig, ax = plt.subplots(2, sharex=True, figsize=(10, 4.5))
    else:
        ax = axes
    ax[0].plot(ky_dat['time'][inds], vbz / 1e3, 'r-', label='Burton')
    ax[0].set_ylabel('v.B$_{south}$ [mV/m]')
    ax[0].plot(ky_dat['time'][inds], vbz_OB / 1e3, 'b-', label='O-M')
    ax[0].set_ylabel('v.B$_{south}$ [mV/m]')
    ax[0].legend()
    ax[1].plot(ky_dat['time'][inds], ky_dat['dst'][inds])
    ax[1].set_ylabel('Dst [nT]')
    ax[1].set_xlabel('1989')
    if show:
        plt.show()
示例#2
0
def initialSW():
    """Construct initial estimate of hourly solar wind parameters"""
    st = dt.datetime(1989, 3, 12)
    en = dt.datetime(1989, 3, 15)
    hourly = getKondrashovSW()
    # Keep IMF By and n, set V to Boteler/Nagatsuma
    t_ssc1 = dt.datetime(1989, 3, 13, 1, 27)
    t_ssc2 = dt.datetime(1989, 3, 13, 7, 43)
    t_cme = dt.datetime(1989, 3, 13, 16)
    t_turn = dt.datetime(1989, 3, 14, 2)
    # Set Bx positive, in accordance with ISEE-3 data, Vy/Vz->0
    hourly['Bx'] = dm.dmfilled(hourly['By'].shape, fillval=3)
    hourly['Vy'] = dm.dmfilled(hourly['By'].shape, fillval=0)
    hourly['Vz'] = dm.dmfilled(hourly['By'].shape, fillval=0)
    # Before first SSC
    inds_before_1 = tb.tOverlapHalf([dt.datetime(1989, 3, 12), t_ssc1],
                                    hourly['DateTime'])
    hourly['V_sw'][inds_before_1] = 400
    # Between first and ssecond SSC
    inds_between_12 = tb.tOverlapHalf([t_ssc1, t_ssc2], hourly['DateTime'])
    hourly['V_sw'][inds_between_12] = 550
    # IMF turns north around 1989-03-14T02:00:00 according to inverse Burton and Kondrashov
    inds_mainphase = tb.tOverlapHalf([t_ssc2, t_turn], hourly['DateTime'])
    hourly['V_sw'][inds_mainphase] = 983
    # Then have speed decay towards IMP-8 measurement which is ballpark 820 km/s
    inds_rest = tb.tOverlapHalf([t_turn, hourly['DateTime'][-1]],
                                hourly['DateTime'])
    hourly['V_sw'][inds_rest] = np.linspace(983, 820, len(inds_rest))
    # Now we have speed, estimate temperature
    hourly['Plasma_temp'] = emp.getExpectedSWTemp(hourly['V_sw'],
                                                  model='XB15',
                                                  units='K')
    inds_cme = tb.tOverlapHalf([t_cme, en], hourly['DateTime'])
    hourly['Plasma_temp'][
        inds_cme] /= 3  # reduce by factor of 3 for CME-like temp
    # Get "Kondrashov VBs" using V from Boteler/Nagatsuma
    hourly['VBs_K'] = 1e-3 * hourly['V_sw'] * rectify(
        -1 * hourly['Bz'])  # mV/m
    # Now get VBs from inverse Burton
    ky_dat = kyo.fetch('dst', (st.year, st.month, st.day),
                       (en.year, en.month, en.day))
    inds = tb.tOverlapHalf([st, en], ky_dat['time'])
    # Pressure correct here using n and V
    hourly = calc_P(hourly)
    hourly['Dst'] = ky_dat['dst'][inds]
    dst_star = emp.getDststar(hourly, model='OBrien')
    hourly['VBs_OB'] = 1e-3 * inverseOBrienMcPherron(dst_star)
    # Make new Bz from VBs_OB
    hourly['Bz_OB'] = -1e3 * hourly['VBs_OB'] / hourly['V_sw']  # nT

    return hourly
示例#3
0
                efw_file='*'+date+'*.cdf'
                gemf=glob.glob(efw_file)
                pyf=pycdf.CDF(gemf[0])
                epoch=pd.DatetimeIndex(pyf['epoch'][...])
                MLML=pyf['mlt_lshell_mlat'][...]
                dMLT=pd.DataFrame({'MLT':np.swapaxes(MLML, 1,0)[0]}, index=epoch)
                dL=pd.DataFrame({'L':np.swapaxes(MLML, 1,0)[1]}, index=epoch)
                dMLAT=pd.DataFrame({'MLAT':np.swapaxes(MLML,1,0)[2]},index=epoch)
                deDens=pd.DataFrame({'eDens':pyf['density'][...]}, index=epoch)
                rt = pd.period_range(date,periods=1441, freq='T').to_timestamp()
                eDens=np.array(deDens['eDens'].resample('1min', how='median').reindex(index=rt,fill_value=np.nan))
                L=np.array(dL['L'].resample('1min', how='median').reindex(index=rt,fill_value=np.nan))
                MLT=np.array(dMLT['MLT'].resample('1min', how='median').reindex(index=rt,fill_value=np.nan))
                MLAT=np.array(dMLAT['MLAT'].resample('1min', how='median').reindex(index=rt,fill_value=np.nan))
                # get Kp
                kyoto=spk.fetch('kp', dt1, dt1)
                day=datetime.datetime.strftime(dt1,'%d') # need to get day of month
                kp_arr=kyoto['kp'][(int(day)-1)*8: int(day)*8]
                kpC=0
                kpM=0
                nKP=np.zeros(1440)
                for iKP in range(1440):
                    nKP[iKP]=kp_arr[kpM]
                    kpC+=1
                    if kpC == 180:
                        kpC=0
                        kpM+=1

                Kp=np.array(nKP)
                #
                # great, now everything is on same time scale
示例#4
0
Created on Thu Jul  9 01:52:44 2020

@author: agnitm
"""

import numpy as np
import spacepy.pybats as pb
import spacepy.pybats.kyoto as kyoto
import matplotlib.pyplot as plt
import datetime as dt

time1 = dt.datetime(2010, 4, 4, 22, 0)
time2 = dt.datetime(2010, 4, 5, 10, 45)
dt = dt.timedelta(minutes=1)

kyoto_ae = kyoto.fetch('ae', time1, time2)

t = time1
aur_data = {}
aur_data['time'] = []
aur_data['AU'] = []
aur_data['AL'] = []

while (t <= time2):
    fname = (
        'AurIndex_superhi/MagGrid_superhires/mag_grid_e{:}{:0=2d}{:0=2d}' +
        '-{:0=2d}{:0=2d}{:0=2d}.out').format(t.year, t.month, t.day, t.hour,
                                             t.minute, t.second)
    print(fname)
    if fname != 'AurIndex_superhi/MagGrid_superhires/mag_grid_e20100405-083000.out':
        data = open(fname, 'r')
 # put into pandas arrays
 energy=np.swapaxes(pyf['HOPE_ENERGY_Ion'][...],1,0)
 epoch=pyf['Epoch_Ion'][...]
 MLT=pyf['MLT_Ion'][...]
 L=pyf['L_Ion'][...]
 EnergyDelta=np.swapaxes(pyf['ENERGY_Ion_DELTA'][...],1,0)[n1:n2]
 FLUX=pyf[name_species[iSpe]][...]
 FLUXl=np.swapaxes(FLUX,1,0)[n1:n2]
 dataFrames={}
 MLTdf={}
 Ldf={}
 Fluxdf={}
 Energydf={}
 Deltadf={}
 Densitydf=np.zeros(1440) # number of minutes in day
 kyoto=spk.fetch('kp', DT, DT)
 day=datetime.datetime.strftime(DT,'%d') # need to get day of month
 kp_arr=kyoto['kp'][(int(day)-1)*8: int(day)*8]
 kpC=0
 kpM=0
 nKP=np.zeros(1440)
 for iKP in range(1440):
      nKP[iKP]=kp_arr[kpM]
      kpC+=1
      if kpC == 180:
         kpC=0
         kpM+=1
      else:
         continue
 #
 # get the spacecraft potential
示例#6
0
from spacepy.pybats import kyoto as kt
from spacepy.pybats import ImfInput, apply_smart_timeticks

from smooth import smooth
from burton import burton_rk4

start = dt.datetime(1989, 3, 13, 0, 0, 0)
end   = dt.datetime(1989, 3, 16, 6, 0, 0)

# LOAD ALL RELEVANT DATA.
# Only load on first call.  THIS IS WHY WE USE RUN -I.

# Get observed Kyoto dst:
if ('dst' not in locals()) or ('dst' not in globals()):
    print('Fetching DST from Kyoto... please hold.')
    dst = kt.fetch('dst', [1989, 3], [1989, 3])
    dst['t'] = np.array([(x-start).total_seconds()/60.0 for x in dst['time']])
# Load omni data.
if ('omni' not in locals()) or ('omni' not in globals()):
    print('Loading OMNI data... please hold.')
    omni = read_ascii('./omni_min_3149.lst')
    omni['t'] = np.array([(x-start).total_seconds()/60.0 for x in omni['time']])

# Load SEA data.
sourcedir = '/Users/dwelling/projects/SEA_drivers/CME/SEA_MeanData/'
var = ['vx', 'rho', 'bx', 'by', 'bz', 'temp', 'dst']
fil = ['MeanNvx.mat', 'MeanNn.mat', 'MeanNBx.mat', 'MeanNBy.mat', 
       'MeanNBz.mat', 'MeanNT.mat', 'MeanNDst.mat']

sea = {}
sea['t'] = np.array(np.arange(6040.))
示例#7
0
def makeSW_v2():
    """Construct initial estimate of hourly solar wind parameters"""
    st = dt.datetime(1989, 3, 12)
    en = dt.datetime(1989, 3, 15)
    hourly = getKondrashovSW()
    # Keep IMF By and n, set V to Boteler/Nagatsuma
    t_ssc1 = dt.datetime(1989, 3, 13, 1, 27)
    t_ssc2 = dt.datetime(1989, 3, 13, 7, 43)
    t_cme = dt.datetime(1989, 3, 13, 16)
    t_turn = dt.datetime(1989, 3, 14, 2)
    # Set Bx positive, in accordance with ISEE-3 data, Vy/Vz->0
    hourly['Bx'] = dm.dmfilled(hourly['By'].shape, fillval=3)
    hourly['Vy'] = dm.dmfilled(hourly['By'].shape, fillval=0)
    hourly['Vz'] = dm.dmfilled(hourly['By'].shape, fillval=0)
    # Before first SSC
    inds_before_1 = tb.tOverlapHalf([dt.datetime(1989, 3, 12), t_ssc1],
                                    hourly['DateTime'])
    hourly['V_sw'][inds_before_1] = 400
    # Between first and ssecond SSC
    inds_between_12 = tb.tOverlapHalf([t_ssc1, t_ssc2], hourly['DateTime'])
    hourly['V_sw'][inds_between_12] = 550
    # IMF turns north around 1989-03-14T02:00:00 according to inverse Burton and Kondrashov
    inds_mainphase = tb.tOverlapHalf([t_ssc2, t_turn], hourly['DateTime'])
    hourly['V_sw'][inds_mainphase] = 983
    # Then have speed decay towards IMP-8 measurement which is ballpark 820 km/s
    inds_rest = tb.tOverlapHalf([t_turn, hourly['DateTime'][-1]],
                                hourly['DateTime'])
    hourly['V_sw'][inds_rest] = np.linspace(983, 820, len(inds_rest))
    # Now we have speed, estimate temperature
    hourly['Plasma_temp'] = emp.getExpectedSWTemp(hourly['V_sw'],
                                                  model='XB15',
                                                  units='K')
    inds_cme = tb.tOverlapHalf([t_cme, en], hourly['DateTime'])
    hourly['Plasma_temp'][
        inds_cme] /= 3  # reduce by factor of 3 for CME-like temp
    # Get "Kondrashov VBs" using V from Boteler/Nagatsuma
    hourly['VBs_K'] = 1e-3 * hourly['V_sw'] * rectify(
        -1 * hourly['Bz'])  # mV/m
    # Now get VBs from inverse Burton
    ky_dat = kyo.fetch('dst', (st.year, st.month, st.day),
                       (en.year, en.month, en.day))
    inds = tb.tOverlapHalf([st, en], ky_dat['time'])
    # Substitute density curve from Sept. 2017 (double shock)
    sep17 = pybats.ImfInput(
        filename=
        '/home/smorley/projects/github/advect1d/IMF_201709_advect_filt.dat',
        load=True)
    den_inds = tb.tOverlapHalf(
        [t_ssc1 - dt.timedelta(hours=25), hourly['DateTime'][-1]],
        hourly['DateTime'])
    nhours = len(den_inds)
    # Keep the opening 8 hours from Kondrashov (2017 event gets high)
    hourly['Den_P'][den_inds[9:]] = 2 + (sep17['rho'][::60][9:nhours] * 2)
    # After shocks, ensure number density doesn't drop below 10 (keeps M_A over 2)
    after_ssc2 = hourly['DateTime'] > t_ssc2
    under_lim = hourly['Den_P'] <= 10
    limit_inds = np.logical_and(after_ssc2, under_lim)
    hourly['Den_P'][limit_inds] = 10
    # Pressure correct here using n and V
    hourly = calc_P(hourly)
    hourly['Dst'] = ky_dat['dst'][inds]
    dst_star = emp.getDststar(hourly, model='OBrien')
    hourly['VBs_OB'] = 1e-3 * inverseOBrienMcPherron(dst_star)
    # Make new Bz from VBs_OB
    hourly['Bz_OB'] = -1e3 * hourly['VBs_OB'] / hourly['V_sw']  # nT

    return hourly
示例#8
0
                dMLAT = pd.DataFrame({'MLAT': np.swapaxes(MLML, 1, 0)[2]},
                                     index=epoch)
                deDens = pd.DataFrame({'eDens': pyf['density'][...]},
                                      index=epoch)
                rt = pd.period_range(date, periods=1441,
                                     freq='T').to_timestamp()
                eDens = np.array(deDens['eDens'].resample(
                    '1min', how='median').reindex(index=rt, fill_value=np.nan))
                L = np.array(dL['L'].resample('1min', how='median').reindex(
                    index=rt, fill_value=np.nan))
                MLT = np.array(dMLT['MLT'].resample(
                    '1min', how='median').reindex(index=rt, fill_value=np.nan))
                MLAT = np.array(dMLAT['MLAT'].resample(
                    '1min', how='median').reindex(index=rt, fill_value=np.nan))
                # get Kp
                kyoto = spk.fetch('kp', dt1, dt1)
                day = datetime.datetime.strftime(
                    dt1, '%d')  # need to get day of month
                kp_arr = kyoto['kp'][(int(day) - 1) * 8:int(day) * 8]
                kpC = 0
                kpM = 0
                nKP = np.zeros(1440)
                for iKP in range(1440):
                    nKP[iKP] = kp_arr[kpM]
                    kpC += 1
                    if kpC == 180:
                        kpC = 0
                        kpM += 1

                Kp = np.array(nKP)
                #
示例#9
0
    for i, l in enumerate(lines):
        parts = l.split()
        data['time'][i] = parse(' '.join(parts[:2]))
        data['symH'][i] = parts[-1]

    return data
        
if __name__ == '__main__':
    
    import matplotlib.pyplot as plt
    from spacepy.pybats import kyoto, apply_smart_timeticks, ImfInput

    infile = 'imf20000715.dat'

    # Obtain actual Dst.
    dst = kyoto.fetch('dst', (2000, 7), (2000, 7))
    symH= read_symH('WWW_aeasy00006705.dat')
    
    # Simple comparison with 5 minute timestep.
    tEul, dEul = burton_euler(infile)
    tRk4, dRk4 = burton_rk4(infile)

    fig= plt.figure(figsize=(8,5.5))
    ax = fig.add_subplot(111)
    ax.plot(dst['time'], dst['dst'], 'k--', symH['time'], symH['symH'], 'k-',
            tEul, dEul, 'b-', tRk4, dRk4, 'r-')
    ax.legend( ['$D_{ST}$', 'Sym-H', 'Burton (Euler)', 
                'Burton (RK4)'], loc='best')
    ax.set_ylabel('Disturbance ($nT$)')
    ax.grid()
    apply_smart_timeticks(ax, tEul, dolabel=True)