<jupyter_start><jupyter_text>So it looks like the zonal modal power is largely in the most unstable mode first ($t \leq 50$; check with $t = 35$ for example), then gradually moves into the lower modes ($50 < t \leq 90$ or thereabouts), then goes into the low modes.<jupyter_code># uv power in modes

kt = 150
t_now, qq = read_qq(data_dir, parameters.nx, parameters.ny, kt)
qq = np.swapaxes(qq, 0, 1)
uu, vv, pp = spectral.main_invert(qq, fhb)
v_bt, v_bc = layers_to_modes(vv[:, :, 0], vv[:, :, 1], constants)
u_bt, u_bc = layers_to_modes(uu[:, :, 0], uu[:, :, 1], constants)

uv_bt_full = (v_bt - zonal_mode_extract(v_bt, 0)) * (u_bt - zonal_mode_extract(u_bt, 0))

num_modes = 30
misfit_L2 = np.zeros(num_modes)
power_L2  = np.zeros(num_modes)
for mode in range(len(misfit_L2)):
    uv_bt_mode = zonal_mode_extract(v_bt, mode, low_pass = True) * zonal_mode_extract(u_bt, mode, low_pass = True)
    misfit = (np.nanmean(uv_bt_full, axis = 1) - np.nanmean(uv_bt_mode, axis = 1)) ** 2
    misfit_L2[mode] = np.trapz(misfit, y_vec)
    
    uv_bt_mode = zonal_mode_extract(v_bt, mode) * zonal_mode_extract(u_bt, mode)
    power_L2[mode]  = np.trapz(np.trapz(uv_bt_mode ** 2, y_vec, axis = 0), x_vec)

fig = plt.figure(figsize = (12, 3))
plt.subplot(1, 2, 1)
plt.plot(np.arange(0, num_modes, 1), misfit_L2, 'rx-')
plt.ylabel(r"L2 misfit in zonal mean")
plt.xlabel(r"cumulative $k$")
plt.grid()

ax2 = plt.subplot(1, 2, 2)
plt.plot(np.arange(0, num_modes, 1), power_L2, 'rx-')
plt.ylabel(r"L2 power")
plt.xlabel(r"$k$")
plt.grid()
kt = 75

t_now, qq = read_qq(data_dir, parameters.nx, parameters.ny, kt)
qq = np.swapaxes(qq, 0, 1)
uu, vv, pp = spectral.main_invert(qq, fhb)
v_bt, v_bc = layers_to_modes(vv[:, :, 0], vv[:, :, 1], constants)
u_bt, u_bc = layers_to_modes(uu[:, :, 0], uu[:, :, 1], constants)
q_bt, q_bc = layers_to_modes(qq[:, :, 0], qq[:, :, 1], constants)

mode = 5
vq_lpass = np.nanmean(
    zonal_mode_extract(v_bt, mode, low_pass = True) * zonal_mode_extract(q_bt, mode, low_pass = True),
    axis = 1)
vq_hpass = vq_bt[:, kt] - vq_lpass
uv_lpass = np.nanmean(
    zonal_mode_extract(v_bt, mode, low_pass = True) * zonal_mode_extract(u_bt, mode, low_pass = True),
    axis = 1)
uv_hpass = uv_bt[:, kt] - uv_lpass

fig = plt.figure(figsize= (10, 6))
plt.subplot(2, 2, 1)
plt.plot(vq_bt[:, kt], y_vec, 'b-')
plt.title("$kt = %g$" % kt)
plt.grid()

plt.subplot(2, 2, 2)
plt.plot(vq_lpass, y_vec, 'r-')
plt.plot(vq_hpass, y_vec, 'g-')
plt.title("$kt = %g$" % kt)
plt.grid()

plt.subplot(2, 2, 3)
plt.plot(uv_bt[:, kt], y_vec, 'b-')
plt.title("$kt = %g$" % kt)
plt.grid()

plt.subplot(2, 2, 4)
plt.plot(uv_lpass, y_vec, 'r-')
plt.plot(uv_hpass, y_vec, 'g-')
plt.title("$kt = %g$" % kt)
plt.grid()

## IDEA: if the whole signal is 1, then work out a "power" partition as a function of wavenumber
#        hypothesis being that the dominant wavenumber above which most power is in decreases
#        as the dynamics go on (i.e. all the power goes to large scales)
kt = 400

t_now, qq = read_qq(data_dir, parameters.nx, parameters.ny, kt)
qq = np.swapaxes(qq, 0, 1)
uu, vv, pp = spectral.main_invert(qq, fhb)
v_bt, v_bc = layers_to_modes(vv[:, :, 0], vv[:, :, 1], constants)
u_bt, u_bc = layers_to_modes(uu[:, :, 0], uu[:, :, 1], constants)
q_bt, q_bc = layers_to_modes(qq[:, :, 0], qq[:, :, 1], constants)

num_modes = 30
lpass_power = np.zeros(num_modes)
hpass_power = np.zeros(num_modes)
rel_power = np.zeros(num_modes)
full_power = np.trapz(vq_bt[:, kt] ** 2, y_vec)

for mode in range(num_modes):
    vq_lpass = np.nanmean(
        zonal_mode_extract(v_bt, mode, low_pass = True) * zonal_mode_extract(q_bt, mode, low_pass = True),
        axis = 1)
    vq_hpass = vq_bt[:, kt] - vq_lpass
    lpass_power[mode] = np.trapz(vq_lpass ** 2, y_vec)
    hpass_power[mode] = np.trapz(vq_hpass ** 2, y_vec)
plt.plot(np.linspace(0, num_modes, num_modes), hpass_power / full_power, 'go-')
plt.plot(np.linspace(0, num_modes, num_modes), lpass_power / full_power, 'r-x')
rel_power = (lpass_power - hpass_power) / (lpass_power + hpass_power)
plt.plot(np.linspace(0, num_modes, num_modes), rel_power, 'b-v')
plt.grid()
rel_power > 0.9<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17307692307692307, 'fraction_numerical': 0.02403846153846154, 'mean_word_length': 4.585234899328859, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '629424', 'license': 'MIT', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1867, 'n_words': 433, 'path': 'wrapper/gen_eddy_flux_modal.ipynb', 'repo_name': 'julianmak/pydra', 'size': '4160'}