def compute_surrogate_cov_eigvals(sd):
    
    # construct AR/SBC surrogates
    sd.construct_surrogate_with_noise()
    d = sd.surr_data()
    if COSINE_REWEIGHTING:
        d *= sd.qea_latitude_weights()
    sm_ar = pca_eigvals_gf(d)
    sm_ar = sm_ar[:NUM_EIGVALS]
    
    # construct fourier surrogates
    sd.construct_fourier1_surrogates()
    d = sd.surr_data()
    if COSINE_REWEIGHTING:
        d *= sd.qea_latitude_weights()
    sm_f = pca_eigvals_gf(d)
    sm_f = sm_f[:NUM_EIGVALS]

    # shuffle data (white noise surrogates)
    d = sd.data()
    N = d.shape[0]
    for i in range(d.shape[1]):
        for j in range(d.shape[2]):
            ndx = np.argsort(np.random.normal(size = (N,)))
            d[:, i, j] = d[ndx, i, j]
    if COSINE_REWEIGHTING:
        d = d * sd.qea_latitude_weights()
    sm_w1 = pca_eigvals_gf(d)
    sm_w1 = sm_w1[:NUM_EIGVALS]
    
    return sm_ar, sm_w1, sm_f
pool.close()
del pool
print("[%s] Constructed."  % (str(datetime.now())))

if USE_SURROGATE_MODEL:
    # HACK to replace original data with surrogates
    gf.d = sgf.sd.copy()
    sgf.d = sgf.sd.copy()
    print("Replaced synth model with surrogate model to check false positives.")

# analyze data & obtain eigvals and surrogates
print("[%s] Analyzing data ..." % (str(datetime.now())))
d = gf.data()
if COSINE_REWEIGHTING:
    d *= gf.qea_latitude_weights()
dlam = pca_eigvals_gf(d)[:NUM_EIGVALS]
    
print("[%s] Data analysis DONE." % (str(datetime.now())))

# <markdowncell>

# **Show the variance of the data (filtered)**

# <markdowncell>

# **Show a plot of the model orders**

# <codecell>

mo = sgf.model_orders()
plt = render_component_single(mo, gf.lats, gf.lons, plt_name = 'Model orders of AR surrogates')