I am trying to plot various quantities between two specific dates from the Omni dataset. Here is an excerpt from my code:
import numpy as np
import matplotlib.pyplot as plt
from sunpy.net import Fido
from sunpy.net import attrs as a
def convert_to_datetime(year, day, hour,minute):
return datetime(int(year), 1, 1) + timedelta(days=day - 1, hours=hour, minutes=minute)
trange = a.Time(start_time, end_time)
dataset = a.cdaweb.Dataset("OMNI_HRO2_1MIN")
result = Fido.search(trange, dataset)
files = Fido.fetch(result)
cdf_data = cdflib.CDF(files[0])
var_list = ['Epoch', 'YR', 'Day','HR', 'Minute','Vx','Vy','Vz','BX_GSE','BY_GSE','BZ_GSE','proton_density']
data_dict = {}
for var in var_list:
data = cdf_data.varget(var)
data_dict[var] = data
data = pd.DataFrame(data_dict)
data['Calculated_Datetime'] = data.apply(lambda row: convert_to_datetime(row['YR'], row['Day'], row['HR'], row['Minute']), axis=1)
data.set_index('Calculated_Datetime', inplace=True)
data = data[start_time:end_time]
data['V'] = np.linalg.norm(np.vstack((data['Vx'], data['Vy'], data['Vz'])), axis=0)
data['B'] = np.linalg.norm(np.vstack((data['BX_GSE'], data['BY_GSE'], data['BZ_GSE'])), axis=0)
fig, ax = plt.subplots(len(Label.keys()), 1, figsize=figsize, sharex=True, )
for ax, col in zip(ax, Label.keys()):
ax.plot(data.index, data[col], linewidth=1,label='In-situ',linestyle='dotted',color='black')
The file downloads correctly, but the plot I obtain shows aberrant values for different quantities, resulting in vertical lines.
Could this be due to incorrect retrieval of the date (creation of my Calculated_Datetime
column) ?
I should mention that I used to get a perfect curve when I created my DataFrame with data = heliopy.data.omni.hro2_1min(start_time, end_time).to_dataframe()
, but now that heliopy is deprecated, I am turning to sunpy.
Thanks in advance