示例#1
0
def spectrum_2d(M, indices=None):
    Fourier.compute_spectrum_2d(M, Dt=3)  #smooth on 3 time step.
    S_E = np.nanmean(M.S_E[..., indices], axis=2)
    graphes.color_plot(M.kx, M.ky, S_E, log=True, fignum=1)
    graphes.colorbar(label='$E_k$')
    figs = graphes.legende('$k_x$ (mm)', '$k_y$ (mm)', 'Energy Spectrum (log)')
    return figs
示例#2
0
def fit_core_size(x, y, Z, fignum=1, display=False):
    """
    Find the half width of a gaussian bump
    INPUT
    -----
    x : 2d np array 
        spatial coordinates (columns)
    y : 2d np array
        spatial coordinates (lines)
    Z : 2d np array
        data to be fitted (typically vorticity field )
    fignum : int
        figure number for the output. Default 1
    display : bool
    OUTPUT
    -----
    a : float
        parameter of the gaussian bump
    center : 2 elements np array
        center coordinates
    """
    ny, nx = Z.shape
    X, Y, data, center, factor = normalize(x, y, Z)
    R, theta = Smath.cart2pol(X, Y)

    a0 = 1
    fun = gaussian
    res = opt.minimize(distance_fit, a0, args=(fun, R, data))

    cond = ((center[0] > 5) and (center[0] < nx - 5) and (center[1] > 5)
            and (center[1] < ny - 5))
    if cond:
        a = np.sqrt(res.x)
    else:
        a = None

    if display:
        figs = {}
        graphes.graph(R, factor * data, fignum=3, label='ko')
        graphes.graph(R,
                      factor * gaussian(res.x, R),
                      label='r.',
                      fignum=fignum + 2)
        graphes.set_axis(0, 20, 0, factor * 1.1)
        figs.update(graphes.legende('r (mm)', 'Vorticity s^{-1})', ''))

        graphes.cla(fignum=fignum)
        graphes.color_plot(X, Y, factor * data, fignum=fignum + 3)
        graphes.colorbar()
        figs.update(
            graphes.legende('X (mm)',
                            'Y (mm)',
                            'Vorticity',
                            display=False,
                            cplot=True))
        return a, center, figs
    else:
        return a, center
示例#3
0
def vfield(sigma, modes, fignum=1):

    names = {
        'sigma': sigma,
        'n': modes
    }  #name:eval(name) for name in ['sigma','n','N_p']}# this piece of code does not work

    t = distribution(sigma, modes)

    B = 1.
    n = 100
    dx = 2 * B / n
    x = np.arange(-B, B, dx)
    y = 0.
    z = np.arange(-B, B, dx)
    P = np.asarray(np.meshgrid(x, y, z))

    dim = P.shape
    N = np.prod(dim[1:])
    X = np.reshape(P[0, ...], (N, ))
    Y = np.reshape(P[1, ...], (N, ))
    Z = np.reshape(P[2, ...], (N, ))

    Pos = [np.asarray([x0, y0, z0]) for x0, y0, z0 in zip(X, Y, Z)]

    V = biot.velocity_from_line(t.paths, Pos, Gamma=1, d=3)

    V_2d = np.reshape(V, (n, n, 3))
    E = np.sum(np.power(V_2d, 2), axis=2)

    subplot = [111]
    fig, axes = panel.make(subplot, fignum=fignum)
    fig.set_size_inches(10, 10)

    graphes.color_plot(x, z, E, fignum=fignum, vmin=0, vmax=0, log=True)
    c = graphes.colorbar()

    figs = {}
    figs.update(graphes.legende('X', 'Z', 'E'))

    graphes.save_figs(figs,
                      prefix='Random_path/V_Distributions/Fields/',
                      suffix=suf(names),
                      dpi=300,
                      display=True,
                      frmt='png')
示例#4
0
def time_average(M, indices=None):
    figs = {}
    fields, names, vmin, vmax, labels, units = std_fields()

    for j, field in enumerate(fields):
        Y = np.nanmean(getattr(M, field)[..., indices], axis=2)
        graphes.color_plot(M.x,
                           M.y,
                           Y,
                           fignum=j + 1,
                           vmin=vmin[j] / 5,
                           vmax=vmax[j] / 5)
        graphes.colorbar(label=names[j] + ' ' + units[j])
        figs.update(
            graphes.legende('X (mm)',
                            'Y (mm)',
                            'Time averaged ' + field,
                            cplot=True))
    return figs
示例#5
0
def positions(M,i,field='omega',display=False,sigma=1.,**kwargs):
    """
    find the maximum position of the vortex core at a given instant, using the field specified in argument
    """

    imin,imax,jmin,jmax,Zfilt,X,Y = position_index(M,i,field=field,display=display,sigma=sigma,**kwargs)
    
    deltax_max,deltay_max = SubPix2DGauss(Zfilt/np.max(Zfilt),imax,jmax,SubPixOffset=0)
    deltax_min,deltay_min = SubPix2DGauss(Zfilt/np.min(Zfilt),imin,jmin,SubPixOffset=0)
    
 #   print(deltax_max)
 #   print(deltax_min)
 #   print(deltay_min)
    
#    print(deltax_max)
    #if deltax_max == np.nan or deltax_min==np.nan:
    #    return np.nan,np.nan,np.nan,np.nan
    #else:
    xmin = X[imin,jmin]+deltax_min
    xmax = X[imax,jmax]+deltax_max
    ymin = Y[imin,jmin]-deltay_min
    ymax = Y[imax,jmax]-deltay_max


  #  print(Zfilt.shape)
  #  print(Y.shape)
  #  print(X.shape)
    
    if display:
        graphes.color_plot(X[:,5:],Y[:,5:],Zfilt/np.min(Zfilt),**kwargs)
        graphes.graph([xmin+10],[ymin],label='bo',**kwargs)
        graphes.graph([xmax+10],[ymax],label='ro',**kwargs)
 
  #  graphes.color_plot(X[:,5:],Y[:,5:],Zfilt/np.max(Zfilt),fignum=2)
    
    #input()
#    t.append(M.t[i])
#    Xmin.append(M.x[imin,jmin])
#    Xmax.append(M.x[imax,jmax])
#    Ymin.append(M.y[imin,jmin])
#    Ymax.append(M.y[imax,jmax])
    
    return xmin,xmax,ymin,ymax
示例#6
0
def position_index(M,i,field='omega',display=True,sigma=1.,**kwargs):    
    dx = M.x[0,1]-M.x[0,0]
    
#    x0 = M.x[0,5]
#    y0 = M.y[0,0]
    nx,ny,nt = M.shape()
    crop=True
    if crop:
        lin = slice(0,nx)
        col = slice(0,ny)
        X = M.x[lin,col]
        Y = M.y[lin,col]
    
    Zfilt = smoothing(M,i,field=field,sigma=sigma)
    if display:
        graphes.color_plot(X,Y,Zfilt,**kwargs)

    imax,jmax = np.unravel_index(np.argmax(Zfilt),Zfilt.shape)
    imin,jmin = np.unravel_index(np.argmin(Zfilt),Zfilt.shape)
    
    return imin,imax,jmin,jmax,Zfilt,X,Y
示例#7
0
def example_2():
    n = 100
    N = 50
    sigma = N / 5

    i = round(N / 2)
    j = round(N / 2)
    X = np.asarray([math.exp(-(k - i)**2 / (2 * sigma**2)) for k in range(N)])
    Y = np.asarray([math.exp(-(k - j)**2 / (2 * sigma**2)) for k in range(N)])

    Y = np.reshape(X, (N, 1))
    X = np.reshape(X, (1, N))
    x = np.arange(N)
    y = np.arange(N)
    #generate
    a_init = np.dot(Y, X)

    i = np.floor(np.random.rand(n) * N)
    j = np.floor(np.random.rand(n) * N)

    a_dump = np.dot(Y, X)
    for t in zip(i, j):
        a_dump[t] = np.nan
    graphes.color_plot(x, y, a_dump, fignum=1)

    #remove NaN values
    rm_nans([a_dump])

    graphes.color_plot(x, y, a_dump, fignum=2)
    graphes.color_plot(x, y, a_dump - a_init, fignum=3)
    print(np.max(a_dump - a_init))
示例#8
0
def vortex_maps(x, y, Z, dx=1, display_map=False):
    dZ1 = strain_tensor.strain_tensor(Z, d=2) / dx

    blist = np.arange(0.5, 6., 0.5)
    b0 = 2.5
    dZ2 = {}
    for b in blist:
        print('Compute vorticity with r=' + str(b))
        dZ2[b] = strain_tensor.strain_tensor_C(Z, d=2, b=b) / dx

    omega1, enstrophy2 = strain_tensor.vorticity(dZ1, d=2, norm=False)
    omega2, enstrophy2 = strain_tensor.vorticity(dZ2[b0], d=2, norm=False)

    eigen1 = strain_tensor.Lambda(dZ1, d=2)
    eigen2 = strain_tensor.Lambda(dZ2[b0], d=2)

    strain1 = np.sqrt(
        np.power(eigen1['Lambda_0'], 2) + np.power(eigen1['Lambda_1'], 2))
    strain2 = np.sqrt(
        np.power(eigen2['Lambda_0'], 2) + np.power(eigen2['Lambda_1'], 2))

    figs = {}
    #    dimensions = np.shape(omega1)

    #graphes.hist(np.reshape(omega,np.prod(dimensions)),fignum=1)
    #figs.update(graphes.legende('vorticity (s^-1)','PDF',''))

    #    graphes.set_axis(-2.5,2.5,0,40000)
    n = 3
    Xp1, Yp1 = np.meshgrid(x[n:-n], y[n:-n])
    Xp2, Yp2 = np.meshgrid(x, y)

    if display_map:
        for i in range(2):
            for j in range(2):
                graphes.color_plot(Xp1,
                                   Yp1,
                                   dZ1[..., i, j],
                                   fignum=i + j * 2 + 2)
                graphes.colorbar()
                title = 'dU_' + str(i) + '/x_' + str(j)
                figs.update(graphes.legende('X', 'Y', title, cplot=True))

        graphes.cla(6)
        graphes.color_plot(Xp1, Yp1, -omega1, fignum=6)
        graphes.colorbar()
        figs.update(graphes.legende('X', 'Y', 'Vorticity', cplot=True))

        graphes.color_plot(Xp1, Yp1, strain1, fignum=7)
        graphes.colorbar()
        figs.update(graphes.legende('X', 'Y', 'Strain', cplot=True))

        #dissipation
        dissipation1 = np.sum(np.power(dZ1, 2), axis=(2, 3))
        graphes.color_plot(Xp1, Yp1, dissipation1, fignum=8)
        graphes.colorbar()
        figs.update(graphes.legende('X', 'Y', 'Dissipation', cplot=True))

    i1 = Xp1.shape[0] // 2
    i2 = Xp2.shape[0] // 2

    Xp = Xp2[i2, :]
    omega_th = profile_omega(Xp, sigma=2.)
    graphes.graph(Xp1[i1, :], omega1[i1, :], label='b-', fignum=9)
    graphes.graph(Xp2[i2, :], omega2[i2, :], label='k-', fignum=9)
    graphes.graph(Xp, omega_th, label='r--', fignum=9)
    graphes.legende('r', 'vorticity', 'Radial vorticity profile')

    graphes.graph(Xp1[i1, :], strain1[i1, :], label='b.-', fignum=10)
    graphes.graph(Xp2[i2, :], strain2[i2, :], label='k^-', fignum=10)
    graphes.legende('r--', 'strain', 'Radial strain profile')

    j2 = Xp2.shape[1] // 2
    for b in blist:
        omega2, enstrophy2 = strain_tensor.vorticity(dZ2[b], d=2, norm=False)
        graphes.graph(Xp2[i2, :], omega2[i2, :], label='k-', fignum=11)
        graphes.legende('r', 'vorticity',
                        'Radial vorticity profile, various b')

        graphes.graph([b], [omega2[i2, j2]], label='k^', fignum=12)
        graphes.legende('Circle radius r (in box unit)', 'max. vorticity',
                        'Influence of contour size')

    print(Xp.shape)
    print(Xp2.shape)

    #    graphes.color_plot(X,Y,R,fignum=3,vmin=0,vmax=10)
    print(figs)
    return figs
示例#9
0
def display_fft_2d(kx, ky, S, fignum=1, vmin=1, vmax=7):
    #display in logscale
    S_log = np.log10(S)
    c = graphes.color_plot(kx, ky, S_log, vmin=vmin, vmax=vmax, fignum=fignum)

    graphes.clegende(c, '$E_k (a.u.)$')
示例#10
0
def single(filename, display=False):
    figs = {}
    savedir = '/Users/stephane/Documents/Postdoc_Chicago/Process_data/Scripts/Samples/Smoothing/'

    filesave, ext = os.path.splitext(filename)
    filesave = filesave + '_processed.hdf5'

    M = M_manip.load_Mdata_file(filename, data=True)
    M.get('E')
    M.get('omega')

    nx, ny, nt = M.shape()
    i = nt / 2

    # display raw energy
    if display:
        graphes.plt.close('all')
        graphes.Mplot(M, 'E', i, vmin=0, vmax=50000, fignum=1, colorbar=True)
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Energy without pre-processing',
                            cplot=True))

        #Display raw vorticity
        graphes.Mplot(M, 'omega', i, vmin=0, vmax=100, fignum=2, colorbar=True)
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Vorticity without pre-processing',
                            cplot=True))
        graphes.save_figs(figs, savedir=savedir, prefix=M.Id.get_id())

        graphes.Mplot(M, 'Ux', i, vmin=-200, vmax=200, fignum=3, colorbar=True)
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Ux after pre-processing',
                            cplot=True))

    M = cdata.remove_spurious(M, borne=1.5)
    M.get('E', force=True)
    M.get('omega', force=True)

    if display:
        graphes.Mplot(M, 'E', i, vmin=0, vmax=50000, fignum=4, colorbar=True)
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Energy after pre-processing',
                            cplot=True))

        #Display raw vorticity
        graphes.Mplot(M, 'omega', i, vmin=0, vmax=100, fignum=5, colorbar=True)
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Vorticity after pre-processing',
                            cplot=True))

        ### Gaussian filter
        omega_filt = track.smoothing(M, i, field='omega', sigma=2.5)
        graphes.color_plot(M.x, M.y, omega_filt, vmin=0, vmax=100, fignum=6)
        graphes.plt.colorbar()
        figs.update(
            graphes.legende('$x$ (mm)',
                            '$y$ (mm)',
                            'Vorticity after gaussian smoothing',
                            cplot=True))
        graphes.save_figs(figs, savedir=savedir, prefix=M.Id.get_id())
    ### Gaussian filter
#    omega_filt = filters.gaussian_filter(omega, sigma=2.)#[...,0]
#    graphes.color_plot(x,y,omega_filt,vmin=0,vmax=50,fignum=5)
#    graphes.plt.colorbar()
#    figs.update(graphes.legende('$x$ (mm)','$y$ (mm)','Vorticity after gaussian smoothing',cplot=True))
#   graphes.save_figs(figs,savedir=savedir)

    to_hdf5.write(M, filename=filesave, key='Mdata')
示例#11
0
def example(filename):
    figs = {}
    savedir = '/Users/stephane/Documents/Postdoc_Chicago/Process_data/Scripts/Example/Smoothing/'

    M = M_manip.load_Mdata_file(filename, data=True)
    M.get('E')
    M.get('omega')

    nx, ny, nt = M.shape()
    x, y = M.x, M.y

    i = nt / 2

    # display raw energy
    graphes.Mplot(M, 'E', i, vmin=0, vmax=50000, fignum=1, colorbar=True)
    figs.update(
        graphes.legende('$x$ (mm)',
                        '$y$ (mm)',
                        'Energy without pre-processing',
                        cplot=True))

    #Display raw vorticity
    graphes.Mplot(M, 'omega', i, vmin=0, vmax=80, fignum=2, colorbar=True)
    figs.update(
        graphes.legende('$x$ (mm)',
                        '$y$ (mm)',
                        'Vorticity without pre-processing',
                        cplot=True))

    # Look for bada data point in amplitude
    E = M.E[..., i]
    tup, errors = cdata.rm_baddata(E, borne=2.)
    xc = []
    yc = []
    for t in tup:
        xc.append([x[t[0], t[1]]])
        yc.append([y[t[0], t[1]]])
    graphes.graph(xc, yc, label='ro', fignum=2)
    print(figs)
    graphes.save_figs(figs, savedir=savedir)

    Ux, Uy = M.Ux[..., i], M.Uy[..., i]
    #replace component of the velocity
    Ux = cdata.replace_data(Ux, tup)[..., 0]
    Uy = cdata.replace_data(Uy, tup)[..., 0]
    U = np.transpose(np.asarray([Uy, Ux]), (1, 2, 0))

    dU = strain.strain_tensor_C(U, b=2.)
    omega, enstrophy = strain.vorticity(dU, d=2, norm=False)
    E = Ux**2 + Uy**2

    graphes.color_plot(x, y, omega, vmin=0, vmax=80, fignum=4)
    graphes.plt.colorbar()
    figs.update(
        graphes.legende('$x$ (mm)',
                        '$y$ (mm)',
                        'Vorticity after pre-processing',
                        cplot=True))

    graphes.color_plot(x, y, E, vmin=0, vmax=50000, fignum=3)
    graphes.plt.colorbar()
    figs.update(
        graphes.legende('$x$ (mm)',
                        '$y$ (mm)',
                        'Energy after pre-processing',
                        cplot=True))

    ### Gaussian filter
    omega_filt = filters.gaussian_filter(omega, sigma=2.)  #[...,0]
    graphes.color_plot(x, y, omega_filt, vmin=0, vmax=80, fignum=5)
    graphes.plt.colorbar()
    figs.update(
        graphes.legende('$x$ (mm)',
                        '$y$ (mm)',
                        'Vorticity after gaussian smoothing',
                        cplot=True))
    graphes.save_figs(figs, savedir=savedir)