Example #1
0
 def setUp(self):
     # Files to open:
     self.file_single = os.path.join(spacepy_testing.datadir, 'pybats_test',
                                     'imf_single.dat')
     self.file_multi = os.path.join(spacepy_testing.datadir, 'pybats_test',
                                    'imf_multi.dat')
     self.sing = pb.ImfInput(self.file_single)
     self.mult = pb.ImfInput(self.file_multi)
Example #2
0
 def setUp(self):
     self.pth = os.path.dirname(os.path.abspath(__file__))
     # Files to open:
     self.file_single = os.path.join(self.pth, 'data', 'pybats_test',
                                     'imf_single.dat')
     self.file_multi = os.path.join(self.pth, 'data', 'pybats_test',
                                    'imf_multi.dat')
     self.sing = pb.ImfInput(self.file_single)
     self.mult = pb.ImfInput(self.file_multi)
Example #3
0
def main(infiles=None):
    # read SWMF ImfInput file
    # Originally written to examine data from simulations
    # by Morley, Welling and Woodroffe (2018). See data at
    # Zenodo (https://doi.org/10.5281/zenodo.1324562)
    infilename = 'Event5Ensembles/run_orig/IMF.dat'
    eventIMF = bats.ImfInput(filename=infilename)
    data1 = bats.LogFile('Event5Ensembles/run_orig/GM/IO2/log_e20100404-190000.log')

    # #read IMF for 10 events...
    # infiles = ['Event5Ensembles/run_{:03d}/IMF.dat'.format(n)
    #            for n in [32,4,36,10,13,17,18,20,24,29]]
    # read IMF files for all ensemble members
    if infiles is None:
        infiles = glob.glob('Event5Ensembles/run_???/IMF.dat')
        subsetlabel = False
    else:
        # got list of run directories from Dst/Kp plotter
        subsetlabel = True
        infiles = [os.path.join(d, 'IMF.dat') for d in infiles]
        nsubset = len(infiles)
    eventlist = [bats.ImfInput(filename=inf) for inf in infiles]

    tstart = eventIMF['time'][0]
    tstop = eventIMF['time'][-1]
    sym = kyo.KyotoSym(lines=bats.kyoto.symfetch(tstart, tstop))
    fig = plt.figure(figsize=(10, 5))
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)
    for ev in eventlist:
        gco = '{}'.format(np.random.randint(5, 50)/100.0)
        pred01B = Dst_Burton(sym['sym-h'][0], ev['ux'], ev['bz'], dt=1./60)
        pred01O = Dst_OBrien(sym['sym-h'][0], ev['ux'], ev['bz'], dt=1./60)
        ax1.plot(ev['time'], pred01B, c=gco, alpha=0.5)
        ax2.plot(ev['time'], pred01O, c=gco, alpha=0.5)
    # ax1.plot(sym['time'], sym['sym-h'], lw=1.5, c='crimson', label='Sym-H')
    evtime = spt.Ticktock(eventIMF['time']).RDT
    datime = spt.Ticktock(data1['time']).RDT
    simDst = tb.interpol(evtime, datime, data1['dst'])
    # ax1.plot(eventIMF['time'], simDst+11-(7.26*eventIMF['pram']), lw=1.5, c='seagreen', label='Sym-H (Press.Corr.)')
    # ax2.plot(sym['time'], sym['sym-h'], lw=1.5, c='crimson')
    # ax2.plot(eventIMF['time'], simDst+11-(7.26*eventIMF['pram']), lw=1.5, c='seagreen', label='Sym-H (Press.Corr.)')
    ax1.plot(data1['time'], data1['dst'], linewidth=1.5, color='crimson', alpha=0.65, label='SWMF')
    ax2.plot(data1['time'], data1['dst'], linewidth=1.5, color='crimson', alpha=0.65)
    ax1.legend()
    splot.applySmartTimeTicks(ax1, [tstart, tstop])
    splot.applySmartTimeTicks(ax2, [tstart, tstop], dolabel=True)
    ax1.set_ylabel('Sym-H [nT]')
    ax2.set_ylabel('Sym-H [nT]')
    ax1.text(0.05, 0.05, "Burton et al.", transform=ax1.transAxes)
    ax2.text(0.05, 0.05, "O'Brien et al.", transform=ax2.transAxes)
Example #4
0
def makeImfInput(indata, fname=None, keymap=None):
    """Make an SWMF IMF input file from an input SpaceData"""
    if keymap is None:
        keymap = {
            'DateTime': 'time',
            'Bx': 'bx',
            'By': 'by',
            'Bz_OB': 'bz',
            'V_sw': 'ux',
            'Vy': 'uy',
            'Vz': 'uz',
            'Den_P': 'rho',
            'Plasma_temp': 'temp',
        }
    numpts = indata['DateTime'].shape[0]
    swmfdata = pybats.ImfInput(filename=False, load=False, npoints=numpts)
    for key_o, newkey in keymap.items():
        if newkey == 'ux':
            swmfdata[newkey] = -1 * np.abs(dm.dmcopy(indata[key_o]))
        else:
            swmfdata[newkey] = dm.dmcopy(indata[key_o])
    swmfdata.attrs['coor'] = 'GSM'
    if fname is not None:
        swmfdata.write(fname)
    return swmfdata
Example #5
0
    def testSubMillisec(self):
        '''
        Test case where sub-millisecond time values can create problems on
        read/write.
        '''
        # Create an IMF object from scratch, fill with zeros.
        imf = pb.ImfInput()
        for key in imf:
            imf[key] = [0]

        # Add a sub-millisecond time:
        imf['time'] = [dt.datetime(2017, 9, 6, 16, 42, 36, 999600)]

        # Write and test for non-failure on re-read:
        imf.write('testme.tmp')
        imf2 = pb.ImfInput('testme.tmp')

        # Test for floor of sub-millisecond times:
        self.assertEqual(self.knownSubMilli, imf2['time'][0])
Example #6
0
    def testWrite(self):
        # Test that files are correctly written to file.

        from numpy.testing import assert_array_equal as assert_array

        # Save original file names:
        old_file_1 = self.sing.attrs['file']
        old_file_2 = self.mult.attrs['file']

        # Rename files, write:
        self.sing.attrs['file'] = './imf_sing.tmp'
        self.mult.attrs['file'] = './imf_mult.tmp'
        self.sing.write()
        self.mult.write()

        # Reopen files:
        sing = pb.ImfInput('./imf_sing.tmp')
        mult = pb.ImfInput('./imf_mult.tmp')

        # Ensure files were written correctly:
        for v in sing:
            assert_array(self.sing[v], sing[v])
        for v in mult:
            assert_array(self.mult[v], mult[v])
Example #7
0
 def setUp(self):
     self.sing = pb.ImfInput(self.file_single)
     self.mult = pb.ImfInput(self.file_multi)
Example #8
0
def process_imf(fpath, rate):
	orig_imf = pybats.ImfInput(fpath)
	interped_imf = interp_imf(orig_imf)
	proc_imf = 	imf_ds(interped_imf, rate)

	return proc_imf
Example #9
0
	plt.tight_layout()
	adjust_plots(a5, '$|V_{X}|$ ($km/s$)', Zero=False, xlab=True)

	return fig

def process_imf(fpath, rate):
	orig_imf = pybats.ImfInput(fpath)
	interped_imf = interp_imf(orig_imf)
	proc_imf = 	imf_ds(interped_imf, rate)

	return proc_imf

if __name__ == "__main__":
	rate = int(sys.argv[1])
	fpath = sys.argv[2]
	imf = pybats.ImfInput(fpath)
	new_imf = pybats.ImfInput(fpath)
	interped_imf = interp_imf(new_imf)
	new_imf = imf_ds(interped_imf, rate)

	five_imf = 	pybats.ImfInput('/Users/sgraf/Desktop/HydroQuebecRemix/swmf_input/Event20110805/IMF_5min.dat')
	thirty_imf = 	pybats.ImfInput('/Users/sgraf/Desktop/HydroQuebecRemix/swmf_input/Event20110805/IMF_30min.dat')
	hour_imf = pybats.ImfInput('/Users/sgraf/Desktop/HydroQuebecRemix/swmf_input/Event20110805/IMF_hourly.dat')
	interped_imf = pybats.ImfInput('/Users/sgraf/Desktop/HydroQuebecRemix/swmf_input/Event20110805/IMF_interpolated.dat')

	fig = plot_for_dan([interped_imf,five_imf,thirty_imf,hour_imf])
	plt.show()

	imf.quicklook().savefig('original_imf.png')

# No imf file given?  Let's try to find one.
if not imffile:
    if glob('imf*.dat'):
        imffile = glob('imf*.dat')[0]
    elif glob('IMF*.dat'):
        imffile = glob('IMF*.dat')[0]
    elif glob('../imf*.dat'):
        imffile = glob('../imf*.dat')[0]
    elif glob('../IMF*.dat'):
        imffile = glob('../IMF*.dat')[0]
    else:
        raise (ValueError("Could not find IMF file.  Use -i flag."))
    print('\tIMF File: using {}'.format(imffile))

# Obtain imf and log files.  Skip steady-state log files.
imf = pb.ImfInput(imffile)
for logname in glob(prefix + 'log_*.log'):
    log = pbs.BatsLog(logname, starttime=start)
    if log['runtime'][-1] != log['runtime'][0]: break

# Did we find a log file?
if 'log' not in locals():
    raise (ValueError("Did not find MHD Logfile (required)."))
print('\tLog File: using {}'.format(logname))

# Get observed DST.  Raise exception if we cannot.
if args.obsdst:
    if not log.fetch_obs_dst():
        raise ValueError('Failed to obtain observed Dst.  Is KyotoWDC down?')

imf.calc_pram()
Example #11
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
Example #12
0
        dt = iterate(state, t, outdata, l1data_tnum, output_x=output_x)
        t += dt
        i += 1

    # Convert timesteps to datetimes
    outdata['time'] = [
        starttime + timedelta(seconds=n) for n in outdata['time']
    ]

    # Set up pram and temp keys
    outdata['pram_1'] = np.multiply(outdata['ux'], outdata['ux'])
    outdata['pram_2'] = np.multiply(outdata['pram_1'], outdata['rho'])
    outdata['pram'] = 1.67621e-6 * outdata['pram_2']

    outdata['temp'] = outdata['T']

    # Set up dictionary
    imf = pybats.ImfInput(load=False)
    for key in imf.keys():
        imf[key] = dm.dmarray(outdata[key])

    # Write the IMF data to .dat file
    imf.write('IMF_data.dat')

    # Write the IMF data to .h5 file
    outhdf = dm.SpaceData()
    for key in outdata.keys():
        outhdf[key] = dm.dmarray(outdata[key])
    outhdf['time'].attrs['epoch'] = t0.isoformat()
    outhdf.toHDF5('advected.h5')
Example #13
0
                        dest='fname',
                        default='IMF_ev5.dat',
                        help='Input SWMF IMF filename. Default "IMF_ev5.dat"')
    parser.add_argument('-p',
                        '--path',
                        dest='path',
                        default='SWMF_inputs',
                        help='Path for input/output')
    options = parser.parse_args()

    np.random.seed(options.seed)  #set seed for repeatability

    #read SWMF ImfInput file
    infilename = os.path.join(options.path, options.fname)
    if os.path.isfile(infilename):
        eventIMF = swmf.ImfInput(filename=infilename)
    else:
        raise IOError(
            'Specified input file does not appear to exist or is not readable')

    Ntimes = len(eventIMF['ux'])  #3*1440 #N days at 1-min resolution
    generateInputs = True
    saveErrors = False

    varlist = ['Vx_GSE', 'Bz_GSM', 'By_GSM']
    Nvars = len(varlist)
    map_dict = {'Vx_GSE': 'ux', 'Bz_GSM': 'bz', 'By_GSM': 'by'}
    ylimdict = {
        'Vx_GSE': [-300, -800],
        'Bz_GSM': [-20, 20],
        'By_GSM': [-20, 20]