for i in range(NUM_SURR):
         jobQ.put(1)
 for i in range(WORKERS):
     jobQ.put(None)
 a = g_surrs.get_seasonality(DETREND=True)
 sg = SurrogateField()
 sg.copy_field(g_surrs)
 if AMPLITUDE:
     a_amp = g_surrs_amp.get_seasonality(True)
     sg_amp = SurrogateField()
     sg_amp.copy_field(g_surrs_amp)
 else:
     sg_amp = None
     a_amp = None
 if SURR_TYPE == 'AR':
     sg.prepare_AR_surrogates()
     if AMPLITUDE:
         sg_amp.prepare_AR_surrogates()
 workers = [
     Process(target=_cond_difference_surrogates,
             args=(sg, sg_amp, g_surrs, a, a_amp, start_cut, jobQ,
                   resQ, ndx_season)) for iota in range(WORKERS)
 ]
 for w in workers:
     w.start()
 tot = 0
 while surr_completed < NUM_SURR:
     # get result
     diff, meanVar, cond_means_surr, maxmin = resQ.get()
     if CONDITION:
         if (maxmin > 2) and (maxmin < 6):
else:
    job_args = [(i, j, phase_data[:, i, j], g.data[:, i, j], phase_bins)
                for i in range(g.lats.shape[0])
                for j in range(g.lons.shape[0])]
job_result = map_func(_get_cond_means, job_args)
del job_args, phase_data
# map results
for i, j, cmm, cmv in job_result:
    bins_data[i, j, :] = cmm
    bins_data_var[i, j, :] = cmv
del job_result

# if surrogate type is AR, exploit the pool to prepare the AR model
if SURR_TYPE == 'AR' or SURR_TYPE == 'ALL':
    log("Creating AR(1) model and computing residuals...")
    sg.prepare_AR_surrogates(pool)

if pool is not None:
    pool.close()
    pool.join()
    del pool

amp_data = np.zeros(g.get_spatial_dims())
for i in range(g.lats.shape[0]):
    for j in range(g.lons.shape[0]):
        amp_data[i, j] = np.std(g.data[:, i, j], ddof=1)

log("Analysis on data done. Saving file...")
## save file in case something will go wrong with surrogates..
# from variance to standard deviation
# if ECA:
     for i in range(NUM_SURR):
         jobQ.put(1)
 for i in range(WORKERS):
     jobQ.put(None)
 a = g_surrs.get_seasonality(DETREND=True)
 sg = SurrogateField()
 sg.copy_field(g_surrs)
 if AMPLITUDE:
     a_amp = g_surrs_amp.get_seasonality(True)
     sg_amp = SurrogateField()
     sg_amp.copy_field(g_surrs_amp)
 else:
     sg_amp = None
     a_amp = None
 if SURR_TYPE == "AR":
     sg.prepare_AR_surrogates()
     if AMPLITUDE:
         sg_amp.prepare_AR_surrogates()
 workers = [
     Process(
         target=_cond_difference_surrogates,
         args=(sg, sg_amp, g_surrs, a, a_amp, start_cut, jobQ, resQ, ndx_season),
     )
     for iota in range(WORKERS)
 ]
 for w in workers:
     w.start()
 tot = 0
 while surr_completed < NUM_SURR:
     # get result
     diff, meanVar, cond_means_surr, maxmin = resQ.get()
    job_args = [ (i, j, phase_data[:, i, j], amp_data[:, i, j], phase_bins) for i in range(g.lats.shape[0]) for j in range(g.lons.shape[0]) ]
else:
    job_args = [ (i, j, phase_data[:, i, j], g.data[:, i, j], phase_bins) for i in range(g.lats.shape[0]) for j in range(g.lons.shape[0]) ]
job_result = map_func(_get_cond_means, job_args)
del job_args, phase_data
# map results
for i, j, cmm, cmv in job_result:
    bins_data[i, j, :] = cmm
    bins_data_var[i, j, :] = cmv
del job_result


# if surrogate type is AR, exploit the pool to prepare the AR model
if SURR_TYPE == 'AR' or SURR_TYPE == 'ALL':
    log("Creating AR(1) model and computing residuals...")
    sg.prepare_AR_surrogates(pool)

if pool is not None:
    pool.close()
    pool.join()
    del pool

amp_data = np.zeros(g.get_spatial_dims())
for i in range(g.lats.shape[0]):
    for j in range(g.lons.shape[0]):
        amp_data[i, j] = np.std(g.data[:, i, j], ddof = 1)
        
log("Analysis on data done. Saving file...")
## save file in case something will go wrong with surrogates..
# from variance to standard deviation
# if ECA: