Exemplo n.º 1
0
def compute_PSF(filename):
    """
    Modelize the PSF using GROOT model for aniso and bandwidth, Gendron model for aliasing,
    dphi_highpass for fitting, noise extracted from datas. Non linearity not taken into account

    :parameters:
        filename: (str) : ROKET hdf5 file path
    :return:
        psf: (np.ndarray) : PSF
    """
    tic = time.time()
    spup = drax.get_pup(filename)
    Cab = compute_Cerr(filename)
    Cn = compute_Cn_cpu(filename)
    Ca = compute_Calias(filename)
    Cee = Cab + Cn + Ca
    otftel, otf2, psf, gpu = gamora.psf_rec_Vii(filename,
                                                fitting=False,
                                                cov=(Cee).astype(np.float32))
    otf_fit, psf_fit = compute_OTF_fitting(filename, otftel)
    psf = np.fft.fftshift(np.real(np.fft.ifft2(otf_fit * otf2 * otftel)))
    psf *= (psf.shape[0] * psf.shape[0] / float(np.where(spup)[0].shape[0]))
    tac = time.time()
    print("PSF computed in ", tac - tic, " seconds")

    return psf
Exemplo n.º 2
0
def add_fitting_to_psf(filename, otf, otffit):
    """
    Compute the PSF including the fitting OTF

    :parameters:
        otf: (np.ndarray[ndim=2, dtype=np.float32]): OTF
        otffit: (np.ndarray[ndim=2, dtype=np.float32]): Fitting error OTF
    :return:
        psf: (np.ndarray[ndim=2, dtype=np.float32]): PSF

    """
    print("\nAdding fitting to PSF...")
    spup = drax.get_pup(filename)
    psf = np.fft.fftshift(np.real(np.fft.ifft2(otffit * otf)))
    psf *= (psf.shape[0] * psf.shape[0] / float(np.where(spup)[0].shape[0]))

    return psf
Exemplo n.º 3
0
def compute_OTF_fitting(filename, otftel):
    """
    Modelize the OTF due to the fitting using dphi_highpass

    :parameters:
        filename: (str) : ROKET hdf5 file path
        otftel: (np.ndarray) : Telescope OTF
    :return:
        otf_fit: (np.ndarray) : Fitting OTF
        psf_fit (np.ndarray) : Fitting PSF
    """
    f = h5py.File(filename, 'r')
    r0 = f.attrs["_Param_atmos__r0"] * (f.attrs["_Param_target__Lambda"][0] /
                                        0.5)**(6. / 5.)
    ratio_lambda = 2 * np.pi / f.attrs["_Param_target__Lambda"][0]
    # Telescope OTF
    spup = drax.get_pup(filename)
    mradix = 2
    fft_size = mradix**int((np.log(2 * spup.shape[0]) / np.log(mradix)) + 1)
    mask = np.ones((fft_size, fft_size))
    mask[np.where(otftel < 1e-5)] = 0

    x = np.arange(fft_size) - fft_size / 2
    pixsize = f.attrs["_Param_tel__diam"] / f.attrs["_Param_geom__pupdiam"]
    x = x * pixsize
    r = np.sqrt(x[:, None] * x[:, None] + x[None, :] * x[None, :])
    tabx, taby = starlord.tabulateIj0()
    dphi = np.fft.fftshift(
        starlord.dphi_highpass(
            r, f.attrs["_Param_tel__diam"] /
            (f.attrs["_Param_dm__nact"][0] - 1), tabx, taby) *
        (1 / r0)**(5 / 3.))  # * den * ratio_lambda**2 * mask
    otf_fit = np.exp(-0.5 * dphi) * mask
    otf_fit = otf_fit / otf_fit.max()

    psf_fit = np.fft.fftshift(np.real(np.fft.ifft2(otftel * otf_fit)))
    psf_fit *= (fft_size * fft_size / float(np.where(spup)[0].shape[0]))

    f.close()
    return otf_fit, psf_fit
Exemplo n.º 4
0
def psf_rec_vii_cpu(filename):
    """
    PSF reconstruction using Vii functions (CPU version)

    :parameters:
        filename: (str): path to the ROKET file

    :return:
        otftel: (np.ndarray[ndim=2, dtype=np.float32]): OTF of the perfect telescope
        otf2: (np.ndarray[ndim=2, dtype=np.float32]): OTF due to residual phase error
        psf: (np.ndarray[ndim=2, dtype=np.float32]): LE PSF
    """

    f = h5py.File(filename, 'r')
    IF, T = drax.get_IF(filename)
    ratio_lambda = 2 * np.pi / f.attrs["_Param_target__Lambda"][0]
    # Telescope OTF
    print("Computing telescope OTF...")
    spup = drax.get_pup(filename)
    mradix = 2
    fft_size = mradix**int((np.log(2 * spup.shape[0]) / np.log(mradix)) + 1)
    pup = np.zeros((fft_size, fft_size))
    pup[:spup.shape[0], :spup.shape[0]] = spup
    pupfft = np.fft.fft2(pup)
    conjpupfft = np.conjugate(pupfft)
    otftel = np.real(np.fft.ifft2(pupfft * conjpupfft))
    den = 1. / otftel
    den[np.where(np.isinf(den))] = 0
    mask = np.ones((fft_size, fft_size))
    mask[np.where(otftel < 1e-5)] = 0
    otftel = otftel / otftel.max()
    print("Done")
    # Covariance matrix
    print("Computing covariance matrix...")
    err = drax.get_err(filename)
    P = f["P"][:]
    err = P.dot(err)
    Btt = f["Btt"][:]
    #modes = IF.T.dot(Btt)
    covmodes = err.dot(err.T) / err.shape[1]
    print("Done")
    # Vii algorithm
    print("Diagonalizing cov matrix...")
    e, V = np.linalg.eig(covmodes)
    print("Done")
    tmp = np.zeros((fft_size, fft_size))
    newmodek = tmp.copy()
    ind = np.where(pup)
    for k in range(err.shape[0]):
        #newmodek[ind] = IF.T.dot(V[:,k])
        #newmodek[ind] = modes.dot(V[:,k])
        tmp2 = Btt.dot(V[:, k])
        newmodek[ind] = IF.T.dot(tmp2[:-2])
        newmodek[ind] += T.T.dot(tmp2[-2:])
        term1 = np.real(np.fft.fft2(newmodek**2) * conjpupfft)
        term2 = np.abs(np.fft.fft2(newmodek))**2
        tmp += ((term1 - term2) * e[k])
        print(" Computing Vii : %d/%d\r" % (k, covmodes.shape[0]), end=' ')
    print("Vii computed")

    dphi = np.real(np.fft.ifft2(2 * tmp)) * den * mask * ratio_lambda**2
    otf2 = np.exp(-0.5 * dphi) * mask
    otf2 = otf2 / otf2.max()

    psf = np.fft.fftshift(np.real(np.fft.ifft2(otftel * otf2)))
    psf *= (fft_size * fft_size / float(np.where(pup)[0].shape[0]))

    f.close()
    return otftel, otf2, psf
Exemplo n.º 5
0
def psf_rec_Vii(filename, err=None, fitting=True, covmodes=None, cov=None):
    """
    PSF reconstruction using Vii functions with GPU acceleration.

    :parameters:
        filename: (str): path to the ROKET file
        err: (np.ndarray[ndim=2, dtype=np.float32]): (optionnal) Buffers of command error
        fitting: (bool): (optional) Add the fitting error to the PSF or not (True by default)
        covmodes: (np.ndarray[ndim=2, dtype=np.float32]): (optionnal) Error covariance matrix in the modal space
        cov: (np.ndarray[ndim=2, dtype=np.float32]): (optionnal) Error covariance matrix in the DM space

    :return:
        otftel: (np.ndarray[ndim=2, dtype=np.float32]): OTF of the perfect telescope
        otf2: (np.ndarray[ndim=2, dtype=np.float32]): OTF due to residual phase error
        psf: (np.ndarray[ndim=2, dtype=np.float32]): LE PSF
        gpu: (Gamora): Gamora GPU object (for manipulation and debug)
    """

    f = h5py.File(filename, 'r')
    spup = drax.get_pup(filename)
    # Sparse IF matrix
    IF, T = drax.get_IF(filename)
    # Covariance matrix
    P = f["P"][:]
    print("Projecting error buffer into modal space...")
    if ((err is None) and (cov is None)):
        err = drax.get_err(filename)
        err = P.dot(err)
    print("Computing covariance matrix...")
    if (cov is None):
        if (covmodes is None):
            covmodes = err.dot(err.T) / err.shape[1]
        else:
            covmodes = (P.dot(covmodes)).dot(P.T)
    else:
        covmodes = cov
    print("Done")
    Btt = f["Btt"][:]

    # Scale factor
    scale = float(2 * np.pi / f.attrs["_Param_target__Lambda"][0])
    # Init GPU
    gpu = Gamora(c, c.activeDevice, "Vii", Btt.shape[0], covmodes.shape[0],
                 f["noise"][:].shape[1], IF.data, IF.indices, IF.indptr, IF.data.size, T,
                 spup, spup.shape[0],
                 np.where(spup)[0].size, scale, Btt, covmodes)
    # Launch computation
    # gamora.set_eigenvals(e)
    # gamora.set_covmodes(V)
    tic = time.time()
    gpu.psf_rec_Vii()

    otftel = np.array(gpu.d_otftel)
    otf2 = np.array(gpu.d_otfVii)

    otftel /= otftel.max()
    if (list(f.keys()).count("psfortho") and fitting):
        print("\nAdding fitting to PSF...")
        psfortho = f["psfortho"][:]
        otffit = np.real(np.fft.fft2(psfortho))
        otffit /= otffit.max()
        psf = np.fft.fftshift(np.real(np.fft.ifft2(otffit * otf2)))
    else:
        psf = np.fft.fftshift(np.real(np.fft.ifft2(otftel * otf2)))

    psf *= (psf.shape[0] * psf.shape[0] / float(np.where(spup)[0].shape[0]))
    f.close()
    tac = time.time()
    print(" ")
    print("PSF renconstruction took ", tac - tic, " seconds")

    return otftel, otf2, psf, gpu