Commit 41dc03e9 authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

Made swmfpy.spdf.get_omni_data faster

parent 519991ff
"""Tools to work with NASA SPDF.
Here are a collection of tools to get date from
NASA Goddard's Space Physics Data Facility.
__author__ = 'Qusai Al Shidi'
__email__ = ''
import urllib.request
import datetime as dt
from dateutil import rrule
def get_omni_data(time_from, time_to, **kwargs):
"""Retrieve omni solar wind data over http.
This will download omni data from
and put it into a dictionary. If your data is large, then make a csv and
time_from (datetime.datetime): The start time of the solar wind
data that you want to receive.
time_to (datetime.datetime): The end time of the solar wind data
you want to receive.
dict: This will be a list of *all* columns
available in the omni data set.
import datetime
import swmfpy.spdf
from = datetime.datetime(year=2000, month=1, day=1)
to = datetime.datetime(year=2000, month=2, day=15)
data = swmfpy.spdf.get_omni_data(from, to)
# This is straight from the format guide on spdf
col_names = ['ID for IMF spacecraft',
'ID for SW Plasma spacecraft',
'# of points in IMF averages',
'# of points in Plasma averages',
'Percent interp',
'Timeshift, sec',
'RMS, Timeshift',
'RMS, Phase front normal',
'Time btwn observations, sec',
'Field magnitude average, nT',
'Bx, nT (GSE, GSM)',
'By, nT (GSE)',
'Bz, nT (GSE)',
'By, nT (GSM)',
'Bz, nT (GSM)',
'RMS SD B scalar, nT',
'RMS SD field vector, nT',
'Flow speed, km/s',
'Vx Velocity, km/s, GSE',
'Vy Velocity, km/s, GSE',
'Vz Velocity, km/s, GSE',
'Proton Density, n/cc',
'Temperature, K',
'Flow pressure, nPa',
'Electric field, mV/m',
'Plasma beta',
'Alfven mach number',
'X(s/c), GSE, Re',
'Y(s/c), GSE, Re',
'Z(s/c), GSE, Re',
'BSN location, Xgse, Re',
'BSN location, Ygse, Re',
'BSN location, Zgse, Re']
# Set the url
omni_url = ''
if kwargs.get('high_res', True):
omni_url += 'high_res_omni/monthly_1min/'
# Initialize return dict
omni_data = {}
omni_data['Time [UT]'] = []
for name in col_names:
omni_data[name] = []
for date in rrule.rrule(rrule.MONTHLY, dtstart=time_from, until=time_to):
suffix = 'omni_min'
suffix += str(date.year) + str(date.month).zfill(2)
suffix += '.asc'
data = list(urllib.request.urlopen(omni_url+suffix))
# Parse omni data
for line in data:
cols = line.decode('ascii').split()
# Time uses day of year which must be parsed
year, doy, hour, minute = cols[0], cols[1], cols[2], cols[3]
time = dt.datetime.strptime(year + ' ' +
doy + ' ' +
hour + ' ' +
'%Y %j %H %M')
if time >= time_from and time <= time_to:
omni_data['Time [UT]'].append(time)
# Assign the data from after the time columns (0:3)
for num, value in enumerate(cols[4:len(col_names)+4]):
return omni_data
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment