# -*-Python-*-
# Created by eldond at 2018 Aug 10 11:42
"""
This script demonstrates how the OMFITelm class can be used
defaultVars parameters
----------------------
:param device: string
:param shots: list of ints
:param detection_settings: dict (see OMFITelm help)
:param filter_settings: dict (see OMFITelm help)
:param pointname_to_filter: string
:param plot_time_zoom: two element iterable containing numbers in ms
"""
defaultVars(
device='DIII-D',
shots=[154749, 161558],
detection_settings=dict(smoother='butter_smooth', time_window=[0, 1e4]),
filter_settings=dict(elm_phase_range=[0.5, 1.0], elm_since_accept_above=25.0),
pointname_to_filter='tste_core',
plot_time_zoom=[2000, 2200],
)
demo_loc = "root['OUTPUTS']['OMFITelm_demo']"
print("Creating space for OMFITelm demo results in {}...".format(demo_loc))
out = root['OUTPUTS'].setdefault('OMFITelm_demo', OMFITtree())
fig, axs = plt.subplots(3, len(shots), sharex=True, sharey='row')
axs[0, 0].set_ylabel(pointname_to_filter)
for ax in axs[-1, :]:
ax.set_xlabel('Time (ms)')
for i, shot in enumerate(shots):
# Step 1: Create an instance of the OMFITelm class with the settings you want
elmp = out['elmp_{}'.format(shot)] = OMFITelm(
device=device, shot=shot, detection_settings=detection_settings, filter_settings=filter_settings
)
# Step 2: Run ELM detection
elmp.detect()
print("Initialized ELM detector for {}#{} and saved to {}['elmp_{}']".format(device, shot, demo_loc, shot))
# Step 3: Pick some data to filter and get the relevant timebase
f = OMFITmdsValue(server=device, shot=shot, TDI=pointname_to_filter)
out[pointname_to_filter] = f
times_to_filter = f.dim_of(0)
print('Gathered demonstration data to filter: pointname = {}'.format(pointname_to_filter))
# Step 4: Use the OMFITelm class instance to get a flag for which samples are okay according to ELM settings
name = '{}_elm_flag_{}'.format(pointname_to_filter, shot)
flag = out[name] = elmp.filter(times_to_filter)
print("Calculated ELM flag to go with {} and saved to {}['{}']".format(pointname_to_filter, demo_loc, name))
# Step 5: Do stuff with the filtered data
y = f.data()
y2 = f.data()
if len(shape(y2)) == 2:
j = len(y2[:, 0])
y2 = y2[j // 2, :]
y = y[j // 2, :]
y2[~flag] = NaN
axs[0, i].plot(times_to_filter, y, color='r', alpha=0.5, lw=1, marker='x')
axs[0, i].plot(times_to_filter, y2, color='b', lw=2, marker='.')
axs[0, i].set_title('{}#{}'.format(device, shot))
# Use OMFITelm's built in plots as needed
elmp.elm_frequency_plot(fig=fig, axs=[axs[1, i], axs[2, i]], overlay=True, time_range_for_mean=True, time_range=plot_time_zoom)
elmp.elm_detection_plot(
time_zoom=plot_time_zoom, fig=fig, axs=[axs[1, i]], show_details=False, show_phase=False, show_more_timing=False
)
for ax in axs.flatten():
cornernote(ax=ax, remove=True)