コード例 #1
0
def make_boolean_arrays(dBdth, smstat, simtime, starttime, threshold):
    run20, t20 = tb.windowMean(dBdth,
                               time=simtime,
                               winsize=dt.timedelta(minutes=20),
                               overlap=dt.timedelta(0),
                               st_time=starttime,
                               op=np.max)
    predicted_event = np.asarray(run20) >= threshold

    Bdoth = np.array([
        linalg.norm(smstat['Bdot'][i, :2]) for i in range(len(smstat['Bdot']))
    ])
    obs20, obst20 = tb.windowMean(Bdoth,
                                  time=smstat['time'],
                                  winsize=dt.timedelta(minutes=20),
                                  overlap=dt.timedelta(0),
                                  st_time=starttime,
                                  op=np.max)

    obs_event = np.asarray(obs20) >= threshold

    minlen = min(len(run20), len(obs20))
    predicted_event = predicted_event[:minlen]
    obs_event = obs_event[:minlen]

    return predicted_event, obs_event
コード例 #2
0
 def test_windowMean_outputTimes(self):
     '''windowMean should return a known set of output times for a given set of input times and windows'''
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(hours=1)
         olap = datetime.timedelta(0)
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(minutes=n*15) for n in range(48)]
         #last time is datetime.datetime(2001, 1, 1, 11, 45), so last hourly bin should
         #cover 1100 to 1200 and be centered at 1130
         data= [10]*48
         outdata, outtime = tb.windowMean(data, time, winsize=wsize, overlap=olap, st_time=datetime.datetime(2001,1,1))
         od_ans = [10]*12
         ot_ans = [datetime.datetime(2001, 1, 1, 0, 30),
                   datetime.datetime(2001, 1, 1, 1, 30),
                   datetime.datetime(2001, 1, 1, 2, 30),
                   datetime.datetime(2001, 1, 1, 3, 30),
                   datetime.datetime(2001, 1, 1, 4, 30),
                   datetime.datetime(2001, 1, 1, 5, 30),
                   datetime.datetime(2001, 1, 1, 6, 30),
                   datetime.datetime(2001, 1, 1, 7, 30),
                   datetime.datetime(2001, 1, 1, 8, 30),
                   datetime.datetime(2001, 1, 1, 9, 30),
                   datetime.datetime(2001, 1, 1, 10, 30),
                   datetime.datetime(2001, 1, 1, 11, 30)]
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(ot_ans, outtime)
コード例 #3
0
 def test_windowMean3(self):
     """windowMean should give known results 3(regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         time[50:] = [val + datetime.timedelta(days=2) for val in time[50:]]
         outdata, outtime = tb.windowMean(data, time, winsize=wsize, overlap=olap, st_time=datetime.datetime(2001,1,1))
         od_ans = [ 15.,  15.,  15.,  15.,  15.,  numpy.nan,  numpy.nan,  15.,  15.,  15.,  15., 15., 15.]
         ot_ans = [datetime.datetime(2001, 1, 1, 12, 0),
                   datetime.datetime(2001, 1, 2, 0, 0),
                   datetime.datetime(2001, 1, 2, 12, 0),
                   datetime.datetime(2001, 1, 3, 0, 0),
                   datetime.datetime(2001, 1, 3, 12, 0),
                   datetime.datetime(2001, 1, 4, 0, 0),
                   datetime.datetime(2001, 1, 4, 12, 0),
                   datetime.datetime(2001, 1, 5, 0, 0),
                   datetime.datetime(2001, 1, 5, 12, 0),
                   datetime.datetime(2001, 1, 6, 0, 0),
                   datetime.datetime(2001, 1, 6, 12, 0),
                   datetime.datetime(2001, 1, 7, 0, 0),
                   datetime.datetime(2001, 1, 7, 12, 0),
                   ]
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(ot_ans, outtime)
コード例 #4
0
ファイル: test_toolbox.py プロジェクト: spacepy/spacepy
 def test_windowMean_2d(self):
     """windowMean flattens and extends the time var over all the other dims"""
     wsize = 2
     olap = 0
     data = numpy.arange(20).reshape(10,2)
     out = tb.windowMean(data, winsize=wsize, overlap=olap)
     ansd = [  1.5,   5.5,   9.5,  13.5]
     numpy.testing.assert_almost_equal(ansd, out[0])
     anst = [ 1.,  3.,  5.,  7.]
     numpy.testing.assert_almost_equal(anst, out[1])
コード例 #5
0
 def test_windowMean4(self):
     """windowMean should give known results 4(regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         time[50:] = [val + datetime.timedelta(days=2) for val in time[50:]]
         # now test the pointwise
         outdata, outtime = tb.windowMean(data, winsize=24, overlap=12)
         od_ans = [15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.0]
         ot_ans = [12.0, 24.0, 36.0, 48.0, 60.0, 72.0, 84.0]
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
コード例 #6
0
 def test_windowMean4(self):
     """windowMean should give known results 4(regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         time[50:] = [val + datetime.timedelta(days=2) for val in time[50:]]
         # now test the pointwise
         outdata, outtime = tb.windowMean(data, winsize=24, overlap=12)
         od_ans = [15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.0]
         ot_ans = [12.0, 24.0, 36.0, 48.0, 60.0, 72.0, 84.0]
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
コード例 #7
0
 def test_windowMean_op2(self):
     """windowMean should give expected sums with len as passed function"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(0)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         #four points lie on the 5th, so the sum for that day is 4
         outdata, outtime = tb.windowMean(data, time, winsize=wsize, overlap=olap, st_time=datetime.datetime(2001,1,1), op=len)
         od_ans = [24, 24, 24, 24, 4]
         ot_ans = [datetime.datetime(2001, 1, 1, 12, 0),
                   datetime.datetime(2001, 1, 2, 12, 0),
                   datetime.datetime(2001, 1, 3, 12, 0),
                   datetime.datetime(2001, 1, 4, 12, 0),
                   datetime.datetime(2001, 1, 5, 12, 0)]
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(ot_ans, outtime)
コード例 #8
0
 def test_windowMean_op(self):
     """windowMean should give known results (regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         outdata, outtime = tb.windowMean(data, time, winsize=wsize, overlap=olap, st_time=datetime.datetime(2001,1,1), op=len)
         od_ans = [24, 24, 24, 24, 24, 24, 24]
         ot_ans = [datetime.datetime(2001, 1, 1, 12, 0),
                   datetime.datetime(2001, 1, 2, 0, 0),
                   datetime.datetime(2001, 1, 2, 12, 0),
                   datetime.datetime(2001, 1, 3, 0, 0),
                   datetime.datetime(2001, 1, 3, 12, 0),
                   datetime.datetime(2001, 1, 4, 0, 0),
                   datetime.datetime(2001, 1, 4, 12, 0)]
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(ot_ans, outtime)
コード例 #9
0
 def test_windowMean5(self):
     """windowMean should give known results 5(regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         time[50:] = [val + datetime.timedelta(days=2) for val in time[50:]]
         # winsize tests
         outdata, outtime = tb.windowMean(data, winsize=24.6, overlap=12)
         od_ans, ot_ans = tb.windowMean(data, winsize=24.6, overlap=12)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         outdata, outtime = tb.windowMean(data, winsize=0.4)
         od_ans, ot_ans = tb.windowMean(data, winsize=1.0)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         outdata, outtime = tb.windowMean(data, winsize=1.0, overlap=2)
         od_ans, ot_ans = tb.windowMean(data, winsize=1.0, overlap=0)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(8, len(w))
コード例 #10
0
 def test_windowMean5(self):
     """windowMean should give known results 5(regression)"""
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter('always')
         wsize = datetime.timedelta(days=1)
         olap = datetime.timedelta(hours=12)
         data = [10, 20]*50
         time = [datetime.datetime(2001,1,1) + datetime.timedelta(hours=n, minutes = 30) for n in range(100)]
         time[50:] = [val + datetime.timedelta(days=2) for val in time[50:]]
         # winsize tests
         outdata, outtime = tb.windowMean(data, winsize=24.6, overlap=12)
         od_ans, ot_ans = tb.windowMean(data, winsize=24.6, overlap=12)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         outdata, outtime = tb.windowMean(data, winsize=0.4)
         od_ans, ot_ans = tb.windowMean(data, winsize=1.0)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         outdata, outtime = tb.windowMean(data, winsize=1.0, overlap=2)
         od_ans, ot_ans = tb.windowMean(data, winsize=1.0, overlap=0)
         numpy.testing.assert_almost_equal(ot_ans, outtime)
         numpy.testing.assert_almost_equal(od_ans, outdata)
         self.assertEqual(8, len(w))
コード例 #11
0
def create_dics(thresholds, stations, starttimes):
    dic = {}
    midlat_sats = [
        'BEL', 'CLF', 'FMC', 'HAD', 'MEA', 'OTT', 'SIT', 'THY', 'WNG', 'DOU',
        'FUR', 'HLP', 'PIN', 'STJ', 'UPS', 'BFE', 'ESK', 'GIM', 'NEW', 'PBQ',
        'SUA', 'VAL', 'FCC', 'IRT', 'NGK', 'RAL', 'TAR', 'VIC'
    ]
    highlat_sats = [
        'ABK', 'BLC', 'BRW', 'BJN', 'CBB', 'CMO', 'DNB', 'DOB', 'EAG', 'FSP',
        'SMI', 'HRN', 'IQA', 'STF', 'KEV', 'KUV', 'LER', 'LYR', 'NAQ', 'NAL',
        'NRD', 'NUR', 'OUJ', 'THL', 'RAN', 'RES', 'SVS', 'TAL', 'AMK', 'TIK',
        'YKC'
    ]

    print('Initializing Dictionary')
    for threshold in thresholds:
        dic[threshold] = {}
        for keywrd in ['hour', 'unsmoothed', '30min']:
            dic[threshold][keywrd] = {}
            dic[threshold][keywrd]['highlat'] = {}
            dic[threshold][keywrd]['midlat'] = {}
            for i in range(5)[1:]:
                dic[threshold][keywrd]['highlat'][str(i)] = {}
                dic[threshold][keywrd]['midlat'][str(i)] = {}

                dic[threshold][keywrd]['highlat'][str(i)]['obs'] = []
                dic[threshold][keywrd]['midlat'][str(i)]['obs'] = []

                dic[threshold][keywrd]['highlat'][str(i)]['predicted'] = []
                dic[threshold][keywrd]['midlat'][str(i)]['predicted'] = []
    proc_dic = {}
    for starttime in starttimes:
        proc_dic[starttime] = {}

        for station in stations:
            proc_dic[starttime][station] = {}
            for keywrd in ['hour', 'unsmoothed', '30min']:
                proc_dic[starttime][station][keywrd] = {}
                for threshold in thresholds:
                    proc_dic[starttime][station][keywrd][threshold] = {}

    print('Filling Processing Dictionary')
    for keywrd in ['hour', 'unsmoothed', '30min']:
        for starttime in starttimes:
            strstart = starttime
            date = starttime[:8]
            print('Date: ', date)
            starttime = dt.datetime.strptime(starttime, '%Y%m%d%H%M%S')
            if date == '20010831': date = '20010830'
            outputdir = './outputs/{}/'.format(date)

            blockPrint()
            smdata = supermag_parser.supermag_parser(
                './supermag_data/{0}-supermag.txt'.format(date))
            enablePrint()
            origdata = open_output_data(outputdir, keywrd, starttime)
            for stat in stations:
                if stat not in smdata.station: continue
                else:
                    smstat = smdata.station[stat]
                    dBdth, simtime = process_dBdth(origdata, stat)
                    mlt_len = len(smstat['mlt'])
                    stat_mlt20 = tb.windowMean(
                        smstat['mlt'],
                        time=smstat['time'][:mlt_len],
                        winsize=dt.timedelta(minutes=20),
                        overlap=dt.timedelta(0),
                        st_time=starttime,
                        op=np.max)
                    for threshold in thresholds:
                        predicted_event_sat, obs_event_sat = make_boolean_arrays(
                            dBdth, smstat, simtime, starttime, threshold)
                        proc_dic[strstart][station][keywrd][threshold][
                            'predicted_events'] = predicted_event_sat
                        proc_dic[strstart][station][keywrd][threshold][
                            'obs_events'] = obs_event_sat

                        proc_dic[strstart][station][keywrd][threshold][
                            'mlt'] = stat_mlt20[0]
                        proc_dic[strstart][station][keywrd][threshold][
                            'mlat'] = smstat['mlat']

    # proc_dic is filled with boolean arrays for each threshold level, event, and satellite
    # next, we need to seciton up the data into mlt sectors

    print('Filling Main Dictionary')

    for threshold in thresholds:
        for keywrd in ['hour', 'unsmoothed', '30min']:
            for stat in stations:
                if stat in highlat_sats or stat in midlat_sats:
                    #print(np.where(proc_dic[strstart][station][keywrd][threshold]['obs_events'] == True))
                    #print(np.where(proc_dic[strstart][station][keywrd][threshold]['predicted_events'] == True))
                    for i in range(
                            len(proc_dic[strstart][station][keywrd][threshold]
                                ['mlt'])):
                        curmlt = proc_dic[strstart][station][keywrd][
                            threshold]['mlt'][i]
                        print(curmlt)
                        flag = ''
                        if stat in highlat_sats:
                            flag = 'highlat'
                        else:
                            flag = 'midlat'

                        if 0 <= curmlt < 6:
                            dic[threshold][keywrd][flag]['1']['obs'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['obs_events'][i]
                            ]
                            dic[threshold][keywrd][flag]['1']['predicted'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['predicted_events'][i]
                            ]

                        elif 6 <= curmlt < 12:

                            dic[threshold][keywrd][flag]['2']['obs'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['obs_events'][i]
                            ]
                            dic[threshold][keywrd][flag]['2']['predicted'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['predicted_events'][i]
                            ]

                        elif 12 <= curmlt < 18:

                            dic[threshold][keywrd][flag]['3']['obs'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['obs_events'][i]
                            ]
                            dic[threshold][keywrd][flag]['3']['predicted'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['predicted_events'][i]
                            ]

                        elif 18 <= curmlt < 24:

                            dic[threshold][keywrd][flag]['4']['obs'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['obs_events'][i]
                            ]
                            dic[threshold][keywrd][flag]['4']['predicted'] += [
                                proc_dic[strstart][station][keywrd][threshold]
                                ['predicted_events'][i]
                            ]

    # now calculate tt for each sector to get metrics
    print('lets check differences')
    print(dic[0.3]['unsmoothed']['highlat']['1']['obs'] == dic[0.3]
          ['unsmoothed']['midlat']['1']['obs'])
    print(dic[0.3]['unsmoothed']['highlat']['2']['obs'] == dic[0.3]
          ['unsmoothed']['midlat']['2']['obs'])
    print(dic[0.3]['unsmoothed']['highlat']['3']['obs'] == dic[0.3]
          ['unsmoothed']['midlat']['3']['obs'])
    print('calculating statistics')
    for threshold in thresholds:
        for keywrd in ['hour', 'unsmoothed', '30min']:
            for i in range(5)[1:]:
                for flag in ['highlat', 'midlat']:
                    obs_events = dic[threshold][keywrd][flag][str(i)]['obs']
                    predic_events = dic[threshold][keywrd][flag][str(
                        i)]['predicted']
                    tt = verify.Contingency2x2.fromBoolean(
                        predic_events, obs_events)

                    sector_skill = tt.heidke(ci='bootstrap')
                    dic[threshold][keywrd][flag][str(
                        i)]['heidke'] = sector_skill[0]

    return proc_dic, dic
コード例 #12
0
stations = ['YKC', 'MEA', 'NEW', 'FRN', 'IQA', 'PBQ', 'OTT', 'FRD', 'VIC']
stations = [key for key in origdata.keys() if len(key)==3]

for stat in stations:
    if stat not in smdata.station: continue
    subset = origdata[stat]
    simtime = subset['time'][::6]
    dBdte = decimate(subset['dBdte'], 6)
    dBdtn = decimate(subset['dBdtn'], 6)
    dBdth = np.array([linalg.norm([dBdtn[i], dBdte[i]]) for i in range(len(dBdtn))])
    smstat = smdata.station[stat]
    Bdoth = np.array([linalg.norm(smstat['Bdot'][i,:2]) for i in range(len(smstat['Bdot']))])
    fig = plt.figure(figsize=(10,4))
    ax = fig.add_subplot(111)
    ax.plot(simtime, dBdth, 'b-', alpha=0.4)
    ax.plot(smstat['time'], Bdoth, 'r-', alpha=0.4)
    run20, t20 = tb.windowMean(dBdth, time=simtime, winsize=dt.timedelta(minutes=20), overlap=dt.timedelta(0), st_time=dt.datetime(2000,8,12,1), op=np.max)
    ax.plot(t20, run20, marker='o', color='b', linestyle='none', markersize=3, label='SWMF')
    obs20, t20 = tb.windowMean(Bdoth, time=smstat['time'], winsize=dt.timedelta(minutes=20), overlap=dt.timedelta(0), st_time=dt.datetime(2000,8,12,1), op=np.max)
    ax.plot(t20, obs20, marker='x', color='r', linestyle='none', markersize=3, label='Obs')
    ax.set_ylabel('1-min dB/dt$_{H}$ [nT/s]')
    ax.set_xlabel('2000-08-12')
    splot.applySmartTimeTicks(ax, subset['time'], dolimit=True)
    plt.legend()
    plt.title(stat)
    plt.tight_layout()
    #plt.show()
    plt.savefig('Aug2000_dBdth_{}.png'.format(stat))
    plt.close()
コード例 #13
0
def make_plots(smdata, stations, outputdir, date, starttime):
    unsmoothedfiles = glob.glob(outputdir + '/unsmoothed/mag*mag')
    hourlyfiles = glob.glob(outputdir + 'hour/mag*mag')
    files_30 = glob.glob(outputdir + '30min/mag*mag')

    #unsmoothedfiles = ['magnetometers_e20150316-160000.mag','magnetometers_e20150317-160000.mag','magnetometers_e20150318-160000.mag']
    #hourlyfiles = ['magnetometers_e20150316-160000_hour.mag','magnetometers_e20150317-160000_hour.mag']

    for stat in stations:
        fig, (ax1, ax2, ax3) = plt.subplots(figsize=(10, 9),
                                            nrows=3,
                                            sharey=True)
        fig2, (b1, b2, b3) = plt.subplots(figsize=(10, 9),
                                          nrows=3,
                                          sharey=True)

        count = 0
        for fname in unsmoothedfiles:
            origdata = spacepy.pybats.bats.MagFile(fname)
            origdata.calc_h()
            origdata.calc_dbdt()
            #if stat not in smdata.station: continue
            subset = origdata[stat]
            simtime = subset['time'][::6]
            dBdte = decimate(subset['dBdte'], 6)
            dBdtn = decimate(subset['dBdtn'], 6)
            dBdth = np.array(
                [linalg.norm([dBdtn[i], dBdte[i]]) for i in range(len(dBdtn))])

            dBe = decimate(subset['dBe'], 6)
            dBn = decimate(subset['dBn'], 6)
            Bh = np.array(
                [linalg.norm([dBn[i], dBe[i]]) for i in range(len(dBn))])

            b1.plot(simtime, Bh, 'b-', alpha=0.4)
            ax1.plot(simtime, dBdth, 'b-', alpha=0.4)

            run20, t20 = tb.windowMean(dBdth,
                                       time=simtime,
                                       winsize=dt.timedelta(minutes=20),
                                       overlap=dt.timedelta(0),
                                       st_time=starttime,
                                       op=np.max)
            brun20, bt20 = tb.windowMean(Bh,
                                         time=simtime,
                                         winsize=dt.timedelta(minutes=20),
                                         overlap=dt.timedelta(0),
                                         st_time=starttime,
                                         op=np.max)
            if count == 0:
                ax1.plot(t20,
                         run20,
                         marker='o',
                         color='b',
                         linestyle='none',
                         markersize=3,
                         label='SWMF Unsmoothed')
                b1.plot(bt20,
                        brun20,
                        marker='o',
                        color='b',
                        linestyle='none',
                        markersize=3,
                        label='SWMF Unsmoothed')
                count = 1
            else:
                ax1.plot(t20,
                         run20,
                         marker='o',
                         color='b',
                         linestyle='none',
                         markersize=3)
                b1.plot(bt20,
                        brun20,
                        marker='o',
                        color='b',
                        linestyle='none',
                        markersize=3)
        count = 0

        if len(hourlyfiles) > 0:
            for fname in hourlyfiles:
                origdata = spacepy.pybats.bats.MagFile(fname)
                origdata.calc_h()
                origdata.calc_dbdt()

                subset = origdata[stat]
                simtime = subset['time'][::6]
                dBdte = decimate(subset['dBdte'], 6)
                dBdtn = decimate(subset['dBdtn'], 6)
                dBdth = np.array([
                    linalg.norm([dBdtn[i], dBdte[i]])
                    for i in range(len(dBdtn))
                ])

                dBe = decimate(subset['dBe'], 6)
                dBn = decimate(subset['dBn'], 6)
                Bh = np.array(
                    [linalg.norm([dBn[i], dBe[i]]) for i in range(len(dBn))])

                ax3.plot(simtime, dBdth, 'b-', alpha=0.4)
                b3.plot(simtime, Bh, 'b-', alpha=0.4)

                run20, t20 = tb.windowMean(dBdth,
                                           time=simtime,
                                           winsize=dt.timedelta(minutes=20),
                                           overlap=dt.timedelta(0),
                                           st_time=starttime,
                                           op=np.max)
                brun20, bt20 = tb.windowMean(Bh,
                                             time=simtime,
                                             winsize=dt.timedelta(minutes=20),
                                             overlap=dt.timedelta(0),
                                             st_time=starttime,
                                             op=np.max)
                if count == 0:
                    ax3.plot(t20,
                             run20,
                             marker='o',
                             color='b',
                             linestyle='none',
                             markersize=3,
                             label='SWMF Hourly')
                    b3.plot(bt20,
                            brun20,
                            marker='o',
                            color='b',
                            linestyle='none',
                            markersize=3,
                            label='SWMF Hourly')

                    count = 1
                else:
                    ax3.plot(t20,
                             run20,
                             marker='o',
                             color='b',
                             linestyle='none',
                             markersize=3)
                    b3.plot(bt20,
                            brun20,
                            marker='o',
                            color='b',
                            linestyle='none',
                            markersize=3)
        count = 0
        if len(files_30) > 0:
            for fname in files_30:
                origdata = spacepy.pybats.bats.MagFile(fname)
                origdata.calc_h()
                origdata.calc_dbdt()

                subset = origdata[stat]
                simtime = subset['time'][::6]
                dBdte = decimate(subset['dBdte'], 6)
                dBdtn = decimate(subset['dBdtn'], 6)
                dBdth = np.array([
                    linalg.norm([dBdtn[i], dBdte[i]])
                    for i in range(len(dBdtn))
                ])

                dBe = decimate(subset['dBe'], 6)
                dBn = decimate(subset['dBn'], 6)
                Bh = np.array(
                    [linalg.norm([dBn[i], dBe[i]]) for i in range(len(dBn))])

                ax2.plot(simtime, dBdth, 'b-', alpha=0.4)
                b2.plot(simtime, Bh, 'b-', alpha=0.4)

                run20, t20 = tb.windowMean(dBdth,
                                           time=simtime,
                                           winsize=dt.timedelta(minutes=20),
                                           overlap=dt.timedelta(0),
                                           st_time=starttime,
                                           op=np.max)
                brun20, bt20 = tb.windowMean(Bh,
                                             time=simtime,
                                             winsize=dt.timedelta(minutes=20),
                                             overlap=dt.timedelta(0),
                                             st_time=starttime,
                                             op=np.max)
                if count == 0:
                    ax2.plot(t20,
                             run20,
                             marker='o',
                             color='b',
                             linestyle='none',
                             markersize=3,
                             label='SWMF 30min')
                    b2.plot(bt20,
                            brun20,
                            marker='o',
                            color='b',
                            linestyle='none',
                            markersize=3,
                            label='SWMF 30min')

                    count = 1
                else:
                    ax2.plot(t20,
                             run20,
                             marker='o',
                             color='b',
                             linestyle='none',
                             markersize=3)
                    b2.plot(bt20,
                            brun20,
                            marker='o',
                            color='b',
                            linestyle='none',
                            markersize=3)

        # add in observed
        if stat not in smdata.station: continue

        else:
            smstat = smdata.station[stat]

            Bdoth = np.array([
                linalg.norm(smstat['Bdot'][i, :2])
                for i in range(len(smstat['Bdot']))
            ])

            Bh = np.array([
                linalg.norm(smstat['B'][i, :2])
                for i in range(len(smstat['B']))
            ])

            ax1.plot(smstat['time'], Bdoth, 'r-', alpha=0.4)
            ax2.plot(smstat['time'], Bdoth, 'r-', alpha=0.4)
            ax3.plot(smstat['time'], Bdoth, 'r-', alpha=0.4)

            b1.plot(smstat['time'], Bh, 'r-', alpha=0.4)
            b2.plot(smstat['time'], Bh, 'r-', alpha=0.4)
            b3.plot(smstat['time'], Bh, 'r-', alpha=0.4)

            obs20, t20 = tb.windowMean(Bdoth,
                                       time=smstat['time'],
                                       winsize=dt.timedelta(minutes=20),
                                       overlap=dt.timedelta(0),
                                       st_time=starttime,
                                       op=np.max)
            bobs20, bt20 = tb.windowMean(Bh,
                                         time=smstat['time'],
                                         winsize=dt.timedelta(minutes=20),
                                         overlap=dt.timedelta(0),
                                         st_time=starttime,
                                         op=np.max)

            # Plot on dBdt plots
            ax1.plot(t20,
                     obs20,
                     marker='x',
                     color='r',
                     linestyle='none',
                     markersize=3,
                     label='Obs')
            ax2.plot(t20,
                     obs20,
                     marker='x',
                     color='r',
                     linestyle='none',
                     markersize=3,
                     label='Obs')
            ax3.plot(t20,
                     obs20,
                     marker='x',
                     color='r',
                     linestyle='none',
                     markersize=3,
                     label='Obs')

            # Plot on B plots
            b1.plot(bt20,
                    bobs20,
                    marker='x',
                    color='r',
                    linestyle='none',
                    markersize=3,
                    label='Obs')
            b2.plot(bt20,
                    bobs20,
                    marker='x',
                    color='r',
                    linestyle='none',
                    markersize=3,
                    label='Obs')
            b3.plot(bt20,
                    bobs20,
                    marker='x',
                    color='r',
                    linestyle='none',
                    markersize=3,
                    label='Obs')

        adjust_plots(ax1, '1-min dB/dt$_{H}$ [nT/s]', Zero=False, xlab=True)
        ax1.set_xlabel('Time')
        ax1.set_title('Unsmoothed')
        adjust_plots(ax2, '1-min dB/dt$_{H}$ [nT/s]', Zero=False, xlab=True)
        ax2.set_xlabel('Time')
        ax3.set_title('Hourly Smoothed')
        adjust_plots(ax3, '1-min dB/dt$_{H}$ [nT/s]', Zero=False, xlab=True)
        ax3.set_xlabel('Time')
        ax2.set_title('30min Smoothed')
        ax1.legend()
        ax2.legend()
        ax3.legend()

        adjust_plots(b1, '1-min B$_{H}$ [nT]', Zero=False, xlab=True)
        b1.set_xlabel('Time')
        b1.set_title('Unsmoothed')
        adjust_plots(b2, '1-min B$_{H}$ [nT]', Zero=False, xlab=True)
        b2.set_xlabel('Time')
        b3.set_title('Hourly Smoothed')
        adjust_plots(b3, '1-min B$_{H}$ [nT]', Zero=False, xlab=True)
        b3.set_xlabel('Time')
        b2.set_title('30min Smoothed')
        b1.legend()
        b2.legend()
        b3.legend()

        #splot.applySmartTimeTicks(ax, subset['time'], dolimit=True)

        fig.suptitle(stat)
        fig.tight_layout()
        fig.subplots_adjust(top=0.88)

        fig2.suptitle(stat)
        fig2.tight_layout()
        fig2.subplots_adjust(top=0.88)
        #plt.show()
        fig.savefig(
            '/Users/sgraf/Desktop/SWMF_analysis/plots/dBdt/{0}_dBdt_comp_obs_{1}.png'
            .format(date, stat))
        fig2.savefig(
            '/Users/sgraf/Desktop/SWMF_analysis/plots/dBdt/{0}_B_comp_obs_{1}.png'
            .format(date, stat))