def correctXrayTestMirror(d,ifs,shade=None,dx=None,azweight=.015,smax=5.,\
                          bounds=None):
    """
    Get distortion on same grid as IFs and run correction.
    Rebin result onto original distortion grid and apply.
    dx should be on IF grid size
    """
    #Rebin to IF grid
    d2 = man.newGridSize(d, np.shape(ifs[0]))

    #Handle shademask
    if shade is None:
        shade = np.ones(np.shape(d2))

    #Run correction
    volt = slv.correctDistortion(d2,ifs,shade,\
                                            dx=dx,azweight=azweight,\
                                            smax=smax,bounds=bounds)
    # Compute the correction on the same scale as the original
    # data. This correction will need to be added to the original
    # data to yield the final corrected figure.
    ifs2 = ifs.transpose(1, 2, 0)
    cor2 = np.dot(ifs2, volt)
    cor3 = man.newGridSize(cor2, np.shape(d), method='linear')
    #Handle shademask
    cor2[shade == 0] = np.nan
    cornan = man.newGridSize(cor2, np.shape(d), method='linear')
    cor3[np.isnan(cornan)] = np.nan

    return cor3, volt
def correctHFDFC3(d,ifs,shade=None,dx=None,azweight=.015,smax=5.,\
                          bounds=None):
    """
    Get distortion on same grid as IFs and run correction.
    Rebin result onto original distortion grid and apply.
    dx should be on IF grid size.
    Needs update on doc string!
    """
    #Rebin to IF grid
    d2 = man.newGridSize(d, np.shape(ifs[0]))

    #Handle shademask
    if shade is None:
        shade = np.ones(np.shape(d2))

    #Run correction
    volt = slv.correctDistortion(d2,ifs,shade,\
                                            dx=dx,azweight=azweight,\
                                            smax=smax,bounds=bounds)

    #Add correction to original data
    ifs2 = ifs.transpose(1, 2, 0)
    cor2 = np.dot(ifs2, volt)
    #Handle shademask
    cor2[shade == 0] = np.nan
    cor3 = man.newGridSize(cor2, np.shape(d), method='linear')

    return cor3, volt
def reshapeMeasToCorrection(raw_correction, shape_match, mask_fraction):
    # Loading the as-measured correction and processing it appropriately to be stripped of
    # exterior NaNs, bump positive, and have best fit cylinder removed (like dist_map and the ifs).
    # This raw correction has its own distinct shape of order 120 by 100.

    # Creating a perimeter shademask consistent with the size of the measured change.
    meas_shade = eva.slv.createShadePerimeter(shape(raw_correction),
                                              axialFraction=mask_fraction,
                                              azFraction=mask_fraction)

    # Now making the measured relative change directly comparable to the area of the
    # distortion map we are trying to correct by putting the shade mask in place, and
    # then interpolating to the size of dist_map.
    rel_change = copy(raw_correction)
    rel_change[meas_shade == 0] = NaN
    rel_change = man.newGridSize(rel_change, shape_match)
    return rel_change
def plot_correction_inline_vlad(input_dist,fc,cor,dx,first_title = '',second_title = '',third_title = '',
                             cbar_label = '',global_title = '',save_file = None,dist_merit = None,vbounds = None,\
                             fc_merit = None,cor_merit = None,
                             merit1_label = 'PSF E68', merit2_label = 'PSF HPD',merit1_unit = 'asec.',merit2_unit = 'asec.'):
    '''
    '''
    fig = plt.figure(figsize=(18, 5))
    gs = gridspec.GridSpec(1, 3)
    ax1 = fig.add_subplot(gs[0])
    ax2 = fig.add_subplot(gs[1])
    ax3 = fig.add_subplot(gs[2])

    plot_dist = man.stripnans(input_dist - nanmean(input_dist))
    plot_fc = man.newGridSize(man.stripnans(fc - nanmean(fc)),
                              shape(plot_dist))
    plot_cor = man.newGridSize(man.stripnans(cor - nanmean(cor)),
                               shape(plot_dist))

    extent = [
        -shape(plot_dist)[0] / 2 * dx,
        shape(plot_dist)[0] / 2 * dx, -shape(plot_dist)[0] / 2 * dx,
        shape(plot_dist)[0] / 2 * dx
    ]

    #if vbounds is None:
    #    vmin,vmax = nanmin([plot_dist,plot_fc,plot_cor]),nanmax([plot_dist,plot_fc,plot_cor])
    #else:
    #    [vmin,vmax] = vbounds

    mirror_subplot_vlad(plot_dist,
                        ax1,
                        first_title,
                        cbar_label,
                        extent=extent,
                        merit=dist_merit,
                        merit1_label=merit1_label,
                        merit2_label=merit2_label,
                        merit1_unit=merit1_unit,
                        merit2_unit=merit2_unit)
    mirror_subplot_vlad(plot_fc,
                        ax2,
                        second_title,
                        cbar_label,
                        extent=extent,
                        merit=fc_merit,
                        merit1_label=merit1_label,
                        merit2_label=merit2_label,
                        merit1_unit=merit1_unit,
                        merit2_unit=merit2_unit)
    mirror_subplot_vlad(plot_cor,
                        ax3,
                        third_title,
                        cbar_label,
                        extent=extent,
                        merit=cor_merit,
                        merit1_label=merit1_label,
                        merit2_label=merit2_label,
                        merit1_unit=merit1_unit,
                        merit2_unit=merit2_unit)

    fig.subplots_adjust(top=0.74, hspace=0.3, wspace=0.5)

    plt.suptitle(global_title, fontsize=20)

    if save_file != None:
        plt.savefig(save_file)
        plt.close()
    return plot_dist, plot_fc, plot_cor
def stripWithShade(dist, shade):
    output = copy(dist)
    output = man.newGridSize(dist, shape(shade))
    output[shade == 0] = NaN
    return man.stripnans(output)
def plot_measured_correction_sixfig(input_dist,
                                    theo_corr,
                                    meas_corr0,
                                    meas_corr1,
                                    dx,
                                    first_title='',
                                    second_title='',
                                    third_title='',
                                    fourth_title='',
                                    fifth_title='',
                                    sixth_title='',
                                    cbar_label='',
                                    global_title='',
                                    save_file=None,
                                    dist_merit=None,
                                    meas_corr_merit0=None,
                                    meas_corr_merit1=None,
                                    vbounds=None):
    '''
    '''
    fig = plt.figure(figsize=(12, 16))
    gs = gridspec.GridSpec(3, 2)
    ax1 = fig.add_subplot(gs[0])
    ax2 = fig.add_subplot(gs[1])
    ax3 = fig.add_subplot(gs[2])
    ax4 = fig.add_subplot(gs[3])
    ax5 = fig.add_subplot(gs[4])
    ax6 = fig.add_subplot(gs[5])

    plot_dist = man.stripnans(input_dist - nanmean(input_dist))
    plot_theo_corr = man.newGridSize(
        man.stripnans(theo_corr - nanmean(theo_corr)), shape(plot_dist))
    plot_meas_corr0 = man.newGridSize(
        man.stripnans(meas_corr0 - nanmean(meas_corr0)), shape(plot_dist))
    plot_meas_corr1 = man.newGridSize(
        man.stripnans(meas_corr1 - nanmean(meas_corr1)), shape(plot_dist))

    extent = [
        -shape(plot_theo_corr)[0] / 2 * dx,
        shape(plot_theo_corr)[0] / 2 * dx, -shape(plot_theo_corr)[0] / 2 * dx,
        shape(plot_theo_corr)[0] / 2 * dx
    ]
    if vbounds == None:
        vmin = nanmin([
            plot_dist, plot_theo_corr, plot_meas_corr0,
            plot_dist + plot_meas_corr0, plot_meas_corr1,
            plot_dist + plot_meas_corr1
        ]),
        vmax = nanmax([
            plot_dist, plot_theo_corr, plot_meas_corr,
            plot_dist + plot_meas_corr, plot_meas_corr1,
            plot_dist + plot_meas_corr1
        ])
    else:
        [vmin, vmax] = vbounds

    mirror_subplot(plot_dist,
                   ax1,
                   first_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=dist_merit)
    mirror_subplot(plot_theo_corr,
                   ax2,
                   second_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=None)
    mirror_subplot(plot_meas_corr0,
                   ax3,
                   third_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=None)
    mirror_subplot(plot_meas_corr0 + plot_dist,
                   ax4,
                   fourth_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=meas_corr_merit0)
    mirror_subplot(plot_meas_corr1,
                   ax5,
                   fifth_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=None)
    mirror_subplot(plot_meas_corr1 + plot_dist,
                   ax6,
                   sixth_title,
                   cbar_label,
                   extent=extent,
                   vmin=vmin,
                   vmax=vmax,
                   merit=meas_corr_merit1)

    fig.subplots_adjust(hspace=0.4, wspace=0.3)

    plt.suptitle(global_title, fontsize=20)

    if save_file != None:
        plt.savefig(save_file)
        plt.close()

    return fig, (ax1, ax2, ax3, ax4, ax5, ax6)
Exemple #7
0
def iterCorr(dist_map,ifs,perimeter,dx, \
             filebase = '',max_volts = 10.0):
    '''
    Performs an iterative correction to the HFDFC3 mirror to converge on the best correction available to the
    distortion map provided in dist_map.
    Inputs:
    dist_map - the distortion to be corrected. Assumed to be the same shape as shape(ifs[i]), the shape of the measured
    HFDFC3 influence functions - may not need to be?
    ifs - the measured influence functions of HFDFC at 5.0 V. These have been processed so as not to have NaNs in the
    center so that the optimizer will work.
    perimeter - the outer perimeter we'd like to exclude from correction considerations.
    Outputs:
    '''
    n = 0

    def make_bounds(cur_volts):
        bounds = []
        for i in range(np.shape(ifs)[0]):
            bounds.append((0 - cur_volts[i], 1 - cur_volts[i]))
        return bounds

    # Defining the shade mask for the area we wish to iteratively correct.
    mask_fraction = perimeter * 2 / 101.6
    shademask = eva.slv.createShadePerimeter(np.shape(ifs[0]),
                                             axialFraction=mask_fraction,
                                             azFraction=mask_fraction)

    # Saving the distortion map at the same format as the correction.
    dist_map_resized = man.newGridSize(dist_map, np.shape(ifs[0]))
    hdu = pyfits.PrimaryHDU(dist_map_resized)
    hdu.writeto(filebase + '_DistortionToCorrect_Iter' + str(n) + '.fits',
                clobber=True)

    # Performing the first computation of the correction to the distortion map,
    # and saving the first set of optimal voltages. "correction" will be masked,
    # not have best fit cylinder removed, be masked by the shademask, and have
    # shape equal to dist_map.
    correction, opt_volts = eva.correctHFDFC3(dist_map,
                                              ifs,
                                              shade=shademask,
                                              dx=dx,
                                              smax=1.0)
    orig_volts = np.copy(opt_volts)

    # Saving the optimal voltages.
    np.savetxt(filebase + '_OptVolts_Iter' + str(n) + '.txt', opt_volts)

    # Saving the computed correction.
    hdu = pyfits.PrimaryHDU(correction)
    hdu.writeto(filebase + '_Correction_Iter' + str(n) + '.fits', clobber=True)
    print 'CORRECTION CALCULATED'

    # Measuring the relative change induced by applying the calculated set of optimal voltages.
    figure_filepath = measChangeFromVolts(opt_volts * max_volts, n, filebase)

    # Reading the measured change resulting from that voltage application, processing the measurement,
    # and scaling it to the size of dist_map.
    rel_change = reshapeMeasToDistMap(figure_filepath, dist_map, mask_fraction)
    first_meas_change = np.copy(rel_change)

    print 'RELATIVE CHANGE MEASURED'
    # Entering the iterative state while the criteria are unfulfilled.
    while n < 5:
        n = n + 1
        residual = dist_map + rel_change

        hdu = pyfits.PrimaryHDU(residual)
        hdu.writeto(filebase + '_DistortionToCorrect_Iter' + str(n) + '.fits',
                    clobber=True)

        iter_corr, volt_adjust = eva.correctHFDFC3(
            residual,
            ifs,
            shade=shademask,
            dx=dx,
            bounds=make_bounds(opt_volts))

        hdu = pyfits.PrimaryHDU(iter_corr)
        hdu.writeto(filebase + '_Correction_Iter' + str(n) + '.fits',
                    clobber=True)

        opt_volts = opt_volts + volt_adjust
        np.savetxt(filebase + '_OptVolts_Iter' + str(n) + '.txt', opt_volts)

        # Computing the first set of optimal voltages:
        iter_figure_filepath = measChangeFromVolts(opt_volts * max_volts, n,
                                                   filebase)
        rel_change = reshapeMeasToDistMap(iter_figure_filepath, dist_map,
                                          mask_fraction)

    best_fit_distortion = np.copy(rel_change)

    return dist_map, correction, orig_volts, first_meas_change, residual, iter_corr, volt_adjust, rel_change