def Coherence_obs_5var():

    "Prepare the data for plotting"
    pan_obs_gapfill = load('%s/pan_obs_gapfill_good_stations' % workspace)
    rn = load('%s/Rn_0.2_0.5_good_stations' % (workspace))
    ist = 0
    for ibasin in xrange(0, 10):
        cohere_obs_basin = []
        data = scipy.io.loadmat('%s/%s_AP.mat' % (datadir, ibasin + 1))
        for istation in good_stations[ibasin]:
            # the PowerSpectrum method take the matrix as different segment, so shoule be a 1d array
            input = [
                Gapfill(data[v][0, istation][0:tstep].flatten()).flatten()
                for v in variables
            ]
            input.insert(1, rn[ist, :].flatten())
            panobs = pan_obs_gapfill[ist, :].flatten()
            # Compute the coherence
            cohere_obs_basin.append(
                vstack([
                    FFT.Coherence(v, panobs, sampling_frequency, 'linear')[1]
                    for v in input
                ]).reshape(1, 5, nf))
            ist = ist + 1

        # store basin average
        cohere_obs_basin = vstack(cohere_obs_basin)
        cohere_obs_basin.dump('%s/coherence_obs_5var_good_station_%s' %
                              (workspace, basinlongs[ibasin]))
        # cohere_obs_basin.dump('%s/coherence_obs_5var_test2048_%s' %(workspace, basinlongs[ibasin]))

    return
def Coherence_Frequency():

    data = scipy.io.loadmat('%s/1_AP.mat' % (datadir))
    input = data[variables[0]][0, 0][0:tstep].flatten()
    pan = data['pan'][0, 0][0:tstep].flatten()
    freq = FFT.Coherence(input, pan, sampling_frequency, 'linear')[0]

    return freq