In [1]:
import pandas
import seaborn as sns
from datetime import datetime
import matplotlib.pyplot as plt


/Users/schriste/anaconda/lib/python2.7/site-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))

In [2]:
%matplotlib inline
pandas.set_option("display.max_rows", 10)
sns.set(style="darkgrid")

Plotting ACE data


In [3]:
file = '/Users/schriste/Desktop/AC_H1_EPM_201207.txt'

In [4]:
file


Out[4]:
'/Users/schriste/Desktop/AC_H1_EPM_201207.txt'

In [5]:
ace_col_names = ['FP6P_.761-1.22MEV_IONS', 'FP7P_1.22-4.97MEV_IONS', 'UNC_FP6P_.761-1.22MEV_IONS', 'UNC_FP7P_1.22-4.97MEV_IONS']
names = ['date', 'hour'] + ace_col_names
ace_data = pandas.read_csv(file, skiprows=74, delim_whitespace=True, names = names)

In [6]:
ace_data = ace_data.truncate(after=len(ace_data)-5)

In [7]:
times = [datetime.strptime(t[0:-4], '%d-%m-%Y %H:%M:%S') for t in ace_data['date'] + ' ' + ace_data['hour']]

In [8]:
ace_data['times'] = times
ace_data = ace_data.set_index('times')

In [9]:
ace_data = ace_data.drop('hour',1)
ace_data = ace_data.drop('date',1)

In [10]:
for col in ace_col_names:
    ace_data[col] = ace_data[col].convert_objects(convert_numeric=True)

In [11]:
ace_data.dtypes


Out[11]:
FP6P_.761-1.22MEV_IONS        float64
FP7P_1.22-4.97MEV_IONS        float64
UNC_FP6P_.761-1.22MEV_IONS    float64
UNC_FP7P_1.22-4.97MEV_IONS    float64
dtype: object

In [12]:
for col in ace_col_names:
    ace_data[col][ace_data[col] < 0] = np.nan

In [13]:
palette = sns.color_palette()

In [14]:
plt.figure()
ace_data[ace_col_names[0]].plot()
ace_data[ace_col_names[1]].plot()
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title("EPAM/ACE Electron Proton Alpha Monitor")
plt.ylabel("Ion Flux")
plt.show()


Defining a general function for reading ISWA ENLIL dat files


In [15]:
def read_iswa_enlil_dat(file, col_names):
    names = ['year', 'month', 'day', 'hour', 'minute'] + col_names
    data = pandas.read_csv(file, skiprows=2, names=names, delim_whitespace=True)
    times_str = [str(int(t[1]['year'])) + '-' + str(int(t[1]['month'])).zfill(2) + '-' + str(int(t[1]['day'])).zfill(2) + ' ' + str(int(t[1]['hour'])).zfill(2) + ':' + str(int(t[1]['minute'])).zfill(2) for t in data.iterrows()]
    times = [datetime.strptime(t, '%Y-%m-%d %H:%M') for t in times_str]
    data['times'] = times
    data = data.set_index('times')
    data = data.drop('year',1)
    data = data.drop('month',1)
    data = data.drop('day',1)
    data = data.drop('hour',1)
    data = data.drop('minute',1)
    return data

Defining a general function for reading ISWA data files (GOES and KP)


In [16]:
def read_iswa_data_dat(file):
    data = pandas.read_csv(file, delim_whitespace=True)
    times_str = [str(t[0]) + ' ' + str(t[1]['Timestamp'])[0:-2] for t in data.iterrows()]
    times = [datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S') for str_time in times_str]
    data['times'] = times
    data = data.set_index('times')
    data = data.drop('Timestamp',1)
    return data

ENLIL data


In [17]:
file = '/Users/schriste/Desktop/20120712_193500_ENLIL_time_line.dat.txt'

In [18]:
col_names = ['B_enl','V_enl','n_enl','T_enl']
enlil_data = read_iswa_enlil_dat(file, col_names=col_names)

In [19]:
enlil_data


Out[19]:
B_enl V_enl n_enl T_enl
times
2012-07-11 00:00:00 5.03 430.6 8.0 42.179
2012-07-11 00:07:00 5.02 430.7 8.0 42.084
2012-07-11 00:14:00 5.00 430.8 7.9 41.989
2012-07-11 00:21:00 4.99 430.9 7.9 41.896
2012-07-11 00:28:00 4.98 431.0 7.9 41.803
... ... ... ... ...
2012-07-20 23:37:00 3.54 388.4 6.8 27.696
2012-07-20 23:44:00 3.53 388.4 6.8 27.681
2012-07-20 23:51:00 3.53 388.3 6.8 27.667
2012-07-20 23:58:00 3.53 388.2 6.8 27.652
2012-07-21 00:05:00 3.53 388.1 6.8 27.637

2171 rows × 4 columns


In [20]:
plt.figure()
enlil_data.plot(subplots=True, figsize=(10, 10))
plt.show()


<matplotlib.figure.Figure at 0x10a1e66d0>

Deriving the Kp index from ENLIL data


In [21]:
kp90exp=9.5-np.exp(2.17676-(0.000052001)*((enlil_data.V_enl)**1.3333)*((enlil_data.B_enl)**0.6666)*(np.sin((np.pi/2)/2)**(8/3.)))
kp135exp=9.5-np.exp(2.17676-(0.000052001)*((enlil_data.V_enl)**1.3333)*((enlil_data.B_enl)**0.6666)*(np.sin((3*np.pi/4)/2)**(8/3.)))
kp180exp=9.5-np.exp(2.17676-(0.000052001)*((enlil_data.V_enl)**1.3333)*((enlil_data.B_enl)**0.6666)*(np.sin((np.pi)/2)**(8/3.)))

In [22]:
plt.figure()
kp90exp.plot()
kp135exp.plot()
kp180exp.plot()
plt.title("ENLIL Predicted KP Index")
plt.ylabel("KP")
plt.show()


Reading in ENLIL Kp Indices


In [23]:
file = '/Users/schriste/Desktop/20120712_193500_ENLIL_Kp_timeline.dat.txt'

In [24]:
kp_col_names = ['Kp_90', 'Kp_135', 'Kp_180']
enlil_kp_data = read_iswa_enlil_dat(file, col_names=kp_col_names)

In [25]:
enlil_kp_data


Out[25]:
Kp_90 Kp_135 Kp_180
times
2012-07-11 00:00:00 2.406 3.401 3.813
2012-07-11 00:07:00 2.404 3.397 3.808
2012-07-11 00:14:00 2.402 3.393 3.804
2012-07-11 00:21:00 2.400 3.390 3.800
2012-07-11 00:28:00 2.398 3.386 3.795
... ... ... ...
2012-07-20 23:37:00 1.969 2.654 2.938
2012-07-20 23:44:00 1.969 2.653 2.937
2012-07-20 23:51:00 1.968 2.652 2.936
2012-07-20 23:58:00 1.967 2.651 2.935
2012-07-21 00:05:00 1.967 2.650 2.934

2171 rows × 3 columns


In [26]:
plt.figure()
for col in kp_col_names:
    enlil_kp_data[col].plot()
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title("ENLIL Predicted KP Index")
plt.ylabel("KP")
plt.show()


SunPy GOES Data

a current bug in our GOES data fetcher is that it cannot handle multiple days in the time range


In [27]:
from sunpy.lightcurve import GOESLightCurve
from sunpy.time import TimeRange


/Users/schriste/Dropbox/Developer/python/sunpy/sunpy/__init__.py:21: Warning: Missing version.py; you need to run setup.py
  warnings.warn('Missing version.py; you need to run setup.py', Warning)

In [28]:
tr = TimeRange(ace_data.index[0].to_datetime(), ace_data.index[-1].to_datetime())

In [29]:
tr


Out[29]:
    Start: 2012/07/01 00:05:00
    End:   2012/07/30 23:50:00
    Center:2012/07/15 23:57:30
    Duration:29 days or
           43185.0 minutes or
           2591100.0 seconds

In [30]:
goes = GOESLightCurve.create(tr)


/Users/schriste/Dropbox/Developer/python/sunpy/sunpy/lightcurve/lightcurve.py:253: RuntimeWarning: Using existing file rather than downloading, use overwrite=True to override.
  warnings.warn("Using existing file rather than downloading, use overwrite=True to override.", RuntimeWarning)

In [31]:
plt.figure()
ax = goes.data['xrsa'].plot()
plt.yscale('log')
ax = goes.data['xrsb'].plot()
plt.show()


ISWA GOES

note to self: had to manually delete blank lines at the end of file otherwise times have nan's and fails to parse


In [32]:
file = '/Users/schriste/Desktop/ISWA_GOES.txt'

In [33]:
iswa_goes = read_iswa_data_dat(file)

In [34]:
iswa_goes


Out[34]:
Long_Wave
times
2012-07-12 00:00:00 0.000001
2012-07-12 00:01:00 0.000001
2012-07-12 00:02:00 0.000001
2012-07-12 00:03:00 0.000001
2012-07-12 00:04:00 0.000001
... ...
2012-07-13 23:56:00 0.000001
2012-07-13 23:57:00 0.000001
2012-07-13 23:58:00 0.000001
2012-07-13 23:59:00 0.000001
2012-07-14 00:00:00 0.000001

2881 rows × 1 columns


In [35]:
iswa_goes['Long_Wave'][iswa_goes['Long_Wave'] < 0] = np.nan

In [36]:
plt.figure()
iswa_goes.plot()
plt.yscale('log')
plt.title('GOES 1-8 A (ISWA)')
plt.ylabel('Watts')
plt.show()


<matplotlib.figure.Figure at 0x109487a50>

ISWA KP


In [37]:
file = '/Users/schriste/Desktop/ISWA_KP.txt'

In [38]:
iswa_kp = read_iswa_data_dat(file)

In [39]:
plt.figure()
iswa_kp.plot()
plt.title('KP (ISWA)')
plt.ylabel('KP Index')
plt.show()


<matplotlib.figure.Figure at 0x10c5c0350>

In [40]:
iswa_kp


Out[40]:
KP
times
2012-07-12 01:30:00 3
2012-07-12 04:30:00 3
2012-07-12 07:30:00 4
2012-07-12 10:30:00 3
2012-07-12 13:30:00 2
... ...
2012-07-21 10:30:00 2
2012-07-21 13:30:00 1
2012-07-21 16:30:00 3
2012-07-21 19:30:00 3
2012-07-21 22:30:00 2

80 rows × 1 columns

ENLIL KP Evaluation


In [41]:
plt.figure()
iswa_kp.plot()
kp135exp.plot()
kp180exp.plot()
kp90exp.plot()
plt.legend()
plt.show()


<matplotlib.figure.Figure at 0x10c33d410>

Reading in STEREO B Level 2 IMPACT HET SEP

Documentation for the following file http://www.srl.caltech.edu/STEREO/Public/HET_public.html


In [116]:
file = '/Users/schriste/Desktop/BeH12Jul.1m.txt'

In [140]:
def read_stereob_impact(file):
    names = ['0', 'year', 'month', 'day', 'hhmm', 
             'eflux_0.7-1.4', 'eflux_0.7-1.4_uncert', 
             'eflux_1.4-2.8', 'eflux_1.4-2.8_uncert', 
             'eflux_2.8-4.0', 'eflux_2.8-4.0_uncert',
             'pflux_13.6-15.1', 'pflux_13.6-15.1_uncert',
             'pflux_14.9-17.1', 'pflux_14.9-17.1_uncert',
             'pflux_17.0-19.3', 'pflux_17.0-19.3_uncert',
             'pflux_20.8-23.8', 'pflux_20.8-23.8_uncert',
             'pflux_23.8-26.4', 'pflux_23.8-26.4_uncert',
             'pflux_26.3-29.7', 'pflux_26.3-29.7_uncert',
             'pflux_29.5-33.4', 'pflux_29.5-33.4_uncert',
             'pflux_33.4-35.8', 'pflux_33.4-35.8_uncert',
             'pflux_35.5-40.5', 'pflux_35.5-40.5_uncert',
             'pflux_40.0-60.0', 'pflux_40.0-60.0_uncert',
             'pflux_60.0-100.0', 'pflux_60.0-100.0_uncert']
    data = pandas.read_csv(file, delim_whitespace=True, skiprows=24, dtype={'hhmm': np.dtype('str')}, header=None, names=names[0:40])
    dates_str = [str(row[1][1]) + '-' + str(row[1][2]).zfill(1) + '-' + str(row[1][3]).zfill(2) + ' ' + row[1][4][0:2] + ':' + row[1][4][2:4] for row in data.iterrows()]
    times = [datetime.strptime(str_time, '%Y-%b-%d %H:%M') for str_time in dates_str]
    data = data.drop('year', 1)
    data = data.drop('month', 1)
    data = data.drop('hhmm', 1)
    data = data.drop('day', 1)
    data = data.drop('0', 1)
    data['times'] = times
    data = data.set_index('times')
    return data

In [141]:
data = read_stereob_impact(file)

In [145]:
cols = data.columns

In [143]:
data


Out[143]:
eflux_0.7-1.4 eflux_0.7-1.4_uncert eflux_1.4-2.8 eflux_1.4-2.8_uncert eflux_2.8-4.0 eflux_2.8-4.0_uncert pflux_13.6-15.1 pflux_13.6-15.1_uncert pflux_14.9-17.1 pflux_14.9-17.1_uncert ... pflux_29.5-33.4 pflux_29.5-33.4_uncert pflux_33.4-35.8 pflux_33.4-35.8_uncert pflux_35.5-40.5 pflux_35.5-40.5_uncert pflux_40.0-60.0 pflux_40.0-60.0_uncert pflux_60.0-100.0 pflux_60.0-100.0_uncert
times
2012-07-01 00:02:00 0.0392 0.0392 0.0000 0.0000 0.0229 0.0229 0.0000 0.0000 0.0000 0.0000 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00000 0.00000 0.001370 0.000971
2012-07-01 00:03:00 0.1180 0.0679 0.0392 0.0277 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00000 0.00000 0.000000 0.000000
2012-07-01 00:04:00 0.0392 0.0392 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00000 0.00000 0.000686 0.000686
2012-07-01 00:05:00 0.0000 0.0000 0.0000 0.0000 0.0229 0.0229 0.0000 0.0000 0.0000 0.0000 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00000 0.00000 0.000686 0.000686
2012-07-01 00:06:00 0.0392 0.0392 0.0196 0.0196 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00000 0.00000 0.000000 0.000000
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2012-07-31 23:55:00 0.6290 0.1570 0.2950 0.0761 0.0917 0.0459 0.0983 0.0439 0.0550 0.0275 ... 0.00705 0.00705 0.000 0.000 0.00000 0.00000 0.00550 0.00275 0.000000 0.000000
2012-07-31 23:56:00 0.6670 0.1620 0.1770 0.0589 0.0687 0.0397 0.1180 0.0481 0.1650 0.0476 ... 0.00000 0.00000 0.000 0.000 0.00572 0.00572 0.00000 0.00000 0.000687 0.000687
2012-07-31 23:57:00 0.5900 0.1520 0.2360 0.0682 0.0918 0.0459 0.0590 0.0341 0.0276 0.0195 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00413 0.00239 0.000000 0.000000
2012-07-31 23:58:00 0.7090 0.1670 0.2760 0.0737 0.0459 0.0325 0.1180 0.0482 0.0689 0.0308 ... 0.00000 0.00000 0.011 0.011 0.01150 0.00812 0.00000 0.00000 0.000689 0.000689
2012-07-31 23:59:00 0.5910 0.1530 0.2760 0.0737 0.0230 0.0230 0.0788 0.0394 0.1240 0.0414 ... 0.00000 0.00000 0.000 0.000 0.00000 0.00000 0.00138 0.00138 0.000000 0.000000

43718 rows × 28 columns


In [150]:
e_cols = ['eflux_0.7-1.4', 'eflux_1.4-2.8', 'eflux_2.8-4.0']
p_cols = ['pflux_13.6-15.1', 'pflux_14.9-17.1', 'pflux_17.0-19.3', 'pflux_20.8-23.8', 'pflux_23.8-26.4', 
          'pflux_26.3-29.7', 'pflux_29.5-33.4', 'pflux_33.4-35.8', 'pflux_35.5-40.5', 'pflux_40.0-60.0',  
          'pflux_60.0-100.0', ]

In [151]:
plt.figure()
for col in e_cols:
    data[col].plot()
plt.legend()
plt.title('Electrons')
plt.show()

plt.figure()
for col in p_cols:
    data[col].plot()
plt.title('Protons')
plt.legend()
plt.show()



In [159]:
data.resample('T')
plt.figure()
for col in e_cols:
    data[col].resample('H', how='mean').plot()
plt.legend()
plt.title('Electrons')
plt.show()



In [ ]: