print gf.d.shape
print gf.lons[0], gf.lons[-1]
print gf.lats[0], gf.lats[-1]
print gf.d.shape[1] * gf.d.shape[2]

# <codecell>

if USE_SURROGATE_MODEL:
    pool = Pool(POOL_SIZE)
    sgf = SurrGeoFieldAR([0, MAX_AR_ORDER], 'sbc')
    print("Running preparation of surrogates ...")
    sgf.copy_field(gf)
    sgf.prepare_surrogates(pool)
    sgf.construct_surrogate_with_noise()
    sgf.d = sgf.sd # hack to replace original data with surrogate
    print("Max AR order is %d ..." % sgf.max_ord)
    gf = sgf
    print("Replaced field with surrogate field.")
    pool.close()
    del pool
    
print("Analyzing data ...")
d = gf.data()
if COSINE_REWEIGHTING:
    d *= gf.qea_latitude_weights()
Ud, sd, Vtd = pca_components_gf(d)
Ud = Ud[:, :NUM_COMPONENTS]
if not ROTATE_NORMALIZED:
    Ud *= sd[np.newaxis, :NUM_COMPONENTS]
    
print gf.d.shape
print gf.lons[0], gf.lons[-1]
print gf.lats[0], gf.lats[-1]
print gf.d.shape[1] * gf.d.shape[2]

# <codecell>

if USE_SURROGATE_MODEL:
    pool = Pool(POOL_SIZE)
    sgf = SurrGeoFieldAR([0, MAX_AR_ORDER], 'sbc')
    print("Running preparation of surrogates ...")
    sgf.copy_field(gf)
    sgf.prepare_surrogates(pool)
    sgf.construct_surrogate_with_noise()
    sgf.d = sgf.sd  # hack to replace original data with surrogate
    print("Max AR order is %d ..." % sgf.max_ord)
    gf = sgf
    print("Replaced field with surrogate field.")
    pool.close()
    del pool

print("Analyzing data ...")
d = gf.data()
if COSINE_REWEIGHTING:
    d *= gf.qea_latitude_weights()
Ud, sd, Vtd = pca_components_gf(d)
Ud = Ud[:, :NUM_COMPONENTS]
if not ROTATE_NORMALIZED:
    Ud *= sd[np.newaxis, :NUM_COMPONENTS]
# load up the monthly SLP geo-field
print("[%s] Loading geo field..." % (str(datetime.now())))

gf = load_monthly_data_general("data/hgt.mon.mean.nc", "hgt",
                               date(1948, 1, 1), date(2012, 1, 1),
                               None, None, None, 5)


# load up the monthly SLP geo-field
if USE_MUVAR:
    print("[%s] Constructing F2 surrogate ..." % (str(datetime.now())))
    sgf = SurrGeoFieldAR()
    sgf.copy_field(gf)
    sgf.construct_fourier2_surrogates()
    sgf.d = sgf.sd.copy()

    # slide in fourier2 surrogate
    orig_gf = gf
    gf = sgf

# load up the monthly SLP geo-field
print("[%s] Done loading, data hase shape %s." % (str(datetime.now()), gf.d.shape))

# <markdowncell>

# **Surrogate construction**
# 
# The next cell constructs surrogates in three ways.  At this point I'm verifying the previous results obtained
# using residuals by feeding in normally distributed noise according to the model covariance matrix (noise variance in case of AR(k) models).
os.chdir('/home/martin/Projects/ndw-climate/')

# load up the monthly SLP geo-field
print("[%s] Loading geo field..." % (str(datetime.now())))

gf = load_monthly_data_general("data/hgt.mon.mean.nc", "hgt", date(1948, 1, 1),
                               date(2012, 1, 1), None, None, None, 5)

# load up the monthly SLP geo-field
if USE_MUVAR:
    print("[%s] Constructing F2 surrogate ..." % (str(datetime.now())))
    sgf = SurrGeoFieldAR()
    sgf.copy_field(gf)
    sgf.construct_fourier2_surrogates()
    sgf.d = sgf.sd.copy()

    # slide in fourier2 surrogate
    orig_gf = gf
    gf = sgf

# load up the monthly SLP geo-field
print("[%s] Done loading, data hase shape %s." %
      (str(datetime.now()), gf.d.shape))

# <markdowncell>

# **Surrogate construction**
#
# The next cell constructs surrogates in three ways.  At this point I'm verifying the previous results obtained
# using residuals by feeding in normally distributed noise according to the model covariance matrix (noise variance in case of AR(k) models).