我有一个 .cdf 文件,其中包含变量
Epoch, FEDU and L
。我想绘制一个在 x 轴上具有 Epoch
(代表时间)并变化 L
和 FEDU
的频谱图。我使用的数据位于text。我还尝试了连续三天的绘图。
我尝试过这个,但不知道如何插入
L
def plot_spectrogram(cdf_file, variable_name, datetime_values):
# Open the CDF file
cdf = cdflib.CDF(cdf_file)
# Read the variable data
data = cdf[variable_name][...]
# Define values to remove
values_to_remove = [-1.e+31, -1.00000e+31, -9.9999998e+30]
# Filter out the values
filtered_data = np.where(np.isin(data, values_to_remove), np.nan, data)
# Average or sum the filtered data across the alpha dimension
averaged_data = np.nanmean(filtered_data, axis=2) # Using np.nanmean to ignore NaN values
# Convert datetime objects to numerical timestamps for plotting
numerical_times = date2num(datetime_values)
# Plot the aggregated spectrogram
plt.figure(figsize=(10, 6))
plt.imshow(averaged_data.T, aspect='auto', origin='lower', cmap='rainbow', extent=[numerical_times[0], numerical_times[-1], 0, averaged_data.shape[1]])
plt.colorbar(label='Intensity ($cm^2$ s sr keV)')
plt.xlabel('Time')
plt.ylabel('Energy (keV)')
plt.title('Aggregated Spectrogram of {}'.format(variable_name))
plt.gca().xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M:%S')) # Format x-axis ticks as datetime
plt.xticks(rotation=15)
plt.tight_layout()
plt.show()
# Usage example
cdf_files = ['C:/Users/User/Desktop/AB/cdf/H1/rbspa_ect-elec-L3_20140923_v1.0.0.cdf',
'C:/Users/User/Desktop/AB/cdf/H1/rbspa_ect-elec-L3_20140924_v1.0.0.cdf',
'C:/Users/User/Desktop/AB/cdf/H1/rbspa_ect-elec-L3_20140925_v1.0.0.cdf']
# Concatenate spectrogram data from all files
datetime_values = []
for cdf_file in cdf_files:
cdf = cdflib.CDF(cdf_file)
epoch_values = cdf['Epoch'][...]
datetime_values.extend(cdflib.cdfepoch.encode(epoch_values))
cdf.close()
# Plot the concatenated spectrogram
plot_spectrogram(cdf_files[0], 'FEDU', datetime_values) # Assuming same variable name for all files
一种方法是针对选定的间隔或 L 值绘制多个频谱图,可以在单独的图中或作为一个图中的子图。
import cdflib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import date2num, DateFormatter
def plot_spectrogram_for_l_range(cdf_file, variable_name, datetime_values, l_values, l_range):
cdf = cdflib.CDF(cdf_file)
data = cdf[variable_name][...]
l_data = cdf[l_values][...]
values_to_remove = [-1.e+31, -1.00000e+31, -9.9999998e+30]
filtered_data = np.where(np.isin(data, values_to_remove), np.nan, data)
l_mask = (l_data >= l_range[0]) & (l_data <= l_range[1])
data_within_l = filtered_data[l_mask, :]
aggregated_data = np.nanmean(data_within_l, axis=0)
numerical_times = date2num(datetime_values)
numerical_times_within_l = numerical_times[l_mask]
plt.figure(figsize=(10, 6))
plt.imshow(aggregated_data.T, aspect='auto', origin='lower', cmap='rainbow', extent=[numerical_times_within_l[0], numerical_times_within_l[-1], 0, aggregated_data.shape[1]])
plt.colorbar(label='Intensity ($cm^2$ s sr keV)')
plt.xlabel('Time')
plt.ylabel('Energy (keV)')
plt.title(f'Aggregated Spectrogram of {variable_name} for L range {l_range}')
plt.gca().xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M:%S'))
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
l_range = [4, 6]
cdf_files = [r"******\Downloads\rbspa_rel03_ect-rept-sci-L3_20140101_v5.0.0.cdf"]
datetime_values = []
for cdf_file in cdf_files:
cdf = cdflib.CDF(cdf_file)
epoch_values = cdf['Epoch'][...]
datetime_values.extend(cdflib.cdfepoch.encode(epoch_values))
plot_spectrogram_for_l_range(cdf_files[0], 'FEDU', datetime_values, 'L', l_range)
这给出了