def kepler_bkg_multi_prf_2d(params, *args):

    # arguments

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]

    # parameters

    nsrc = (len(params) - 1) / 3
    f = empty((nsrc))
    y = empty((nsrc))
    x = empty((nsrc))
    b = params[nsrc * 3]
    model = zeros((prfDimY + 1, prfDimX + 1))
    for i in range(nsrc):
        f[i] = params[i]
        y[i] = params[nsrc + i]
        x[i] = params[nsrc * 2 + i]

        # interpolate PRF centroid to new pixel position

        tmp = shift(prf, [y[i], x[i]], order=1, mode='constant')

        # extract the PRF model within the data limits

        model = model + tmp[prfY0:prfY0 + prfDimY,
                            prfX0:prfX0 + prfDimX] * f[i]

# rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model, [shape(data)[0], shape(data)[1]], interpolation,
                    True, False)
    model = model / prfDelY / prfDelX
    model = model + b

    # calculate the sum squared difference between data and model

    residual = nansum(square(data - model))

    # write out parameters

    if verbose:
        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model) / data)), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
def kepler_prf_1d(params, *args):

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]
    fy, fx, y, x = params

    # integrate along X and Y

    dataY = data.sum(axis=1)
    dataX = data.sum(axis=0)
    prfY = prf.sum(axis=1)
    prfX = prf.sum(axis=0)

    # interpolate PRF centroid to new pixel position

    modelY = shift(prfY, [y], order=1, mode='constant')
    modelX = shift(prfX, [x], order=1, mode='constant')

    # extract the PRF model within the data limits

    modelY = modelY[prfY0:prfY0 + prfDimY]
    modelX = modelX[prfX0:prfX0 + prfDimX]

    # rebin the PRF image to the same size and dimension of the data image

    modelY = rebin2D(modelY, [numpy.shape(data)[0]], interpolation, True,
                     False)
    modelY = modelY / prfDelY
    modelX = rebin2D(modelX, [numpy.shape(data)[1]], interpolation, True,
                     False)
    modelX = modelX / prfDelX

    # calculate the sum squared difference between data and model

    residualY = nansum(square(dataY - modelY * fy))
    residualX = nansum(square(dataX - modelX * fx))
    return residualY + residualX
예제 #3
0
파일: kepfunc.py 프로젝트: rodluger/PyKE
def kepler_bkg_multi_prf_2d(params,*args):

# arguments

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]

# parameters
    
    nsrc = (len(params) - 1) / 3
    f = empty((nsrc))
    y = empty((nsrc))
    x = empty((nsrc))
    b = params[nsrc*3]
    model = zeros((prfDimY+1,prfDimX+1))
    for i in range(nsrc):
        f[i] = params[i]
        y[i] = params[nsrc+i]
        x[i] = params[nsrc*2+i]    

# interpolate PRF centroid to new pixel position

        tmp = shift(prf,[y[i],x[i]],order=1,mode='constant')

# extract the PRF model within the data limits

        model = model + tmp[prfY0:prfY0+prfDimY,prfX0:prfX0+prfDimX] * f[i]

# rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model,[shape(data)[0],shape(data)[1]],interpolation,True,False)
    model = model / prfDelY / prfDelX
    model = model + b

# calculate the sum squared difference between data and model

    residual = nansum(square(data - model))

# write out parameters

    if verbose:
        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model) / data)), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5 
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
예제 #4
0
파일: kepfunc.py 프로젝트: rodluger/PyKE
def kepler_prf_1d(params,*args):

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]
    fy,fx,y,x = params

# integrate along X and Y

    dataY = data.sum(axis=1)
    dataX = data.sum(axis=0)
    prfY = prf.sum(axis=1) 
    prfX = prf.sum(axis=0)

# interpolate PRF centroid to new pixel position

    modelY = shift(prfY,[y],order=1,mode='constant')
    modelX = shift(prfX,[x],order=1,mode='constant')

# extract the PRF model within the data limits

    modelY = modelY[prfY0:prfY0+prfDimY]
    modelX = modelX[prfX0:prfX0+prfDimX]

# rebin the PRF image to the same size and dimension of the data image

    modelY = rebin2D(modelY,[numpy.shape(data)[0]],interpolation,True,False)
    modelY = modelY / prfDelY
    modelX = rebin2D(modelX,[numpy.shape(data)[1]],interpolation,True,False)
    modelX = modelX / prfDelX

# calculate the sum squared difference between data and model

    residualY = nansum(square(dataY - modelY * fy))
    residualX = nansum(square(dataX - modelX * fx))
    return residualY + residualX
def kepler_prf_2d(params, *args):

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]
    f, y, x = params

    # interpolate PRF centroid to new pixel position

    model = shift(prf, [y, x], order=1, mode='constant')

    # extract the PRF model within the data limits

    model = model[prfY0:prfY0 + prfDimY, prfX0:prfX0 + prfDimX]

    # rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model, [numpy.shape(data)[0],
                            numpy.shape(data)[1]], interpolation, True, False)
    model = model / prfDelY / prfDelX

    # calculate the sum squared difference between data and model

    residual = nansum(square(data - model * f))

    # write out parameters

    if verbose:
        #        txt = '\rFlux = %.2f e-/s ' % f
        #        txt += 'X = %.4f pix ' % (x * prfDelX)
        #        txt += 'Y = %.4f pix ' % (y * prfDelY)
        #        txt += ' ' * 5
        #        sys.stdout.write(txt)
        #        sys.stdout.flush()

        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model * f) / absolute(data))), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
예제 #6
0
파일: kepfunc.py 프로젝트: rodluger/PyKE
def kepler_prf_2d(params,*args):

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    prfDimY = args[4]
    prfDimX = args[5]
    prfY0 = args[6]
    prfX0 = args[7]
    interpolation = args[8]
    verbose = args[9]
    f,y,x = params

# interpolate PRF centroid to new pixel position

    model = shift(prf,[y,x],order=1,mode='constant')

# extract the PRF model within the data limits

    model = model[prfY0:prfY0+prfDimY,prfX0:prfX0+prfDimX]

# rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model,[numpy.shape(data)[0],numpy.shape(data)[1]],interpolation,True,False)
    model = model / prfDelY / prfDelX

# calculate the sum squared difference between data and model

    residual = nansum(square(data - model * f))

# write out parameters

    if verbose:
#        txt = '\rFlux = %.2f e-/s ' % f
#        txt += 'X = %.4f pix ' % (x * prfDelX)
#        txt += 'Y = %.4f pix ' % (y * prfDelY)
#        txt += ' ' * 5
#        sys.stdout.write(txt)
#        sys.stdout.flush()

        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model * f) / absolute(data))), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5 
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
예제 #7
0
파일: kepfunc.py 프로젝트: rodluger/PyKE
def kepler_focus_multi_prf_2d(params,*args):

# arguments

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    datDimX = args[4]
    datDimY = args[5]
    interpolation = args[6]
    verbose = args[7]

# parameters
    
    nsrc = (len(params) - 2) / 3
    f = empty((nsrc))
    y = empty((nsrc))
    x = empty((nsrc))
    b = params[nsrc*3]
    w = params[nsrc*3+1]
    if w > 1.5:
        w = 1.5
    elif w < 1.0:
        w = 1.0
    for i in range(nsrc):
        f[i] = params[i]
        y[i] = params[nsrc+i]
        x[i] = params[nsrc*2+i]    

# dimensions of data image if it had PRF-sized pixels

    prfDimY = datDimY / prfDelY / w
    prfDimX = datDimX / prfDelX / w
    print((w, prfDimY, prfDimX))

# location of the data image centered on the PRF image (in PRF pixel units)

    prfY0 = (shape(prf)[0] - prfDimY) / 2
    prfX0 = (shape(prf)[1] - prfDimX) / 2

# iterate over each source identified in the mask, build model

    DY = 0.0; DX = 0.0
    if int(prfDimY) % 2 == 0: DY = 1.0
    if int(prfDimX) % 2 == 0: DX = 1.0
    model = zeros((prfDimY+DY,prfDimX+DX))
    for i in range(nsrc):

# interpolate PRF centroid to new pixel position

        tmp = shift(prf,[y[i]/w,x[i]/w],order=1,mode='constant')

# extract the PRF model within the data limits

        model = model + tmp[prfY0:prfY0+prfDimY,prfX0:prfX0+prfDimX] * f[i]

# rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model,[shape(data)[0],shape(data)[1]],interpolation,True,False)
    model = model / prfDelY / prfDelX / w / w

# add background to model

    model = model + b

# calculate the sum squared difference between data and model

    residual = nansum(square(data - model))

# write out parameters

    if verbose:
        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model) / data)), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5 
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
def kepler_focus_multi_prf_2d(params, *args):

    # arguments

    data = args[0]
    prf = args[1]
    prfDelY = args[2]
    prfDelX = args[3]
    datDimX = args[4]
    datDimY = args[5]
    interpolation = args[6]
    verbose = args[7]

    # parameters

    nsrc = (len(params) - 2) / 3
    f = empty((nsrc))
    y = empty((nsrc))
    x = empty((nsrc))
    b = params[nsrc * 3]
    w = params[nsrc * 3 + 1]
    if w > 1.5:
        w = 1.5
    elif w < 1.0:
        w = 1.0
    for i in range(nsrc):
        f[i] = params[i]
        y[i] = params[nsrc + i]
        x[i] = params[nsrc * 2 + i]

# dimensions of data image if it had PRF-sized pixels

    prfDimY = datDimY / prfDelY / w
    prfDimX = datDimX / prfDelX / w
    print w, prfDimY, prfDimX

    # location of the data image centered on the PRF image (in PRF pixel units)

    prfY0 = (shape(prf)[0] - prfDimY) / 2
    prfX0 = (shape(prf)[1] - prfDimX) / 2

    # iterate over each source identified in the mask, build model

    DY = 0.0
    DX = 0.0
    if int(prfDimY) % 2 == 0: DY = 1.0
    if int(prfDimX) % 2 == 0: DX = 1.0
    model = zeros((prfDimY + DY, prfDimX + DX))
    for i in range(nsrc):

        # interpolate PRF centroid to new pixel position

        tmp = shift(prf, [y[i] / w, x[i] / w], order=1, mode='constant')

        # extract the PRF model within the data limits

        model = model + tmp[prfY0:prfY0 + prfDimY,
                            prfX0:prfX0 + prfDimX] * f[i]

# rebin the PRF image to the same size and dimension of the data image

    model = rebin2D(model, [shape(data)[0], shape(data)[1]], interpolation,
                    True, False)
    model = model / prfDelY / prfDelX / w / w

    # add background to model

    model = model + b

    # calculate the sum squared difference between data and model

    residual = nansum(square(data - model))

    # write out parameters

    if verbose:
        txt = '\rPearson\'s chisq = %d for %d dof' % \
            (int(nansum(square(data - model) / data)), (shape(data)[0] * shape(data)[1] - len(params)))
        txt += ' ' * 5
        sys.stdout.write(txt)
        sys.stdout.flush()

    return residual
예제 #9
0
파일: kepfit.py 프로젝트: Sleekery/PyKE
def test(flux,ydim,xdim,column,row,prfn,crval1p,crval2p,cdelt1p,cdelt2p,interpolation,
                tolerance,ftol,fluxes,columns,rows,type,verbose,logfile):

# construct input summed image

    status = 0
    if status == 0:
        imgflux = empty((ydim,xdim))
        n = 0
        for i in xrange(ydim):
            for j in xrange(xdim):
                imgflux[i,j] = flux[n]
                n += 1

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]),dtype='float32')
        prfWeight = zeros((5),dtype='float32')
        for i in xrange(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 + (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
            prf = prf / nansum(prf)

# dimensions of data image

    if status == 0:
        datDimY = shape(imgflux)[0]
        datDimX = shape(imgflux)[1]

# dimensions of data image if it had PRF-sized pixels

    if status == 0:
        prfDimY = datDimY / cdelt1p[0]
        prfDimX = datDimX / cdelt2p[0]

# center of the data image (in CCD pixel units)

    if status == 0:
        datCenY = row + float(datDimY) / 2 - 0.5
        datCenX = column + float(datDimX) / 2 - 0.5

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfY0 = (shape(prf)[0] - prfDimY) / 2
        prfX0 = (shape(prf)[1] - prfDimX) / 2

# initial guess for fit parameters

    if status == 0:
        guess = []
        try:
            f = fluxes.strip().split(',')
            y = rows.strip().split(',')
            x = columns.strip().split(',')
            for i in xrange(len(f)):
                f[i] = float(f[i]) * numpy.nanmax(flux)
        except:
            f = fluxes
            y = rows
            x = columns
        for i in xrange(len(f)):
            try:
                guess.append(float(f[i]))
            except:
                message = 'ERROR -- KEPPRF: Fluxes must be floating point numbers'
                status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in xrange(len(y)):
                try:
                    guess.append((float(y[i]) - datCenY) / cdelt2p[0])
                except:
                    message = 'ERROR -- KEPPRF: Rows must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            for i in xrange(len(x)):
                try:
                    guess.append((float(x[i]) - datCenX) / cdelt1p[0])
                except:
                    message = 'ERROR -- KEPPRF: Columns must be floating point numbers'
                    status = kepmsg.err(logfile,message,verbose)
        if status == 0:
            if len(x) != len(y) or len(x) != len(f):
                message = 'ERROR -- KEPFIT:FITMULTIPRF: Guesses for rows, columns and '
                message += 'fluxes must have the same number of sources'
                status = kepmsg.err(logfile,message,verbose)
        
# fit input image with model

    if status == 0:
        f = []
        x = []
        y = []
        nsrc = len(guess) / 3
        args = (imgflux,prf,cdelt1p[0],cdelt2p[0],prfDimY,prfDimX,prfY0,prfX0,
                interpolation,verbose)
        if type == '2D' and nsrc == 1:
            ans = fmin_powell(kepfunc.kepler_prf_2d,guess,args=args,xtol=tolerance,
                                  ftol=ftol,disp=False)
            f.append(ans[0])
            y.append(ans[1])
            x.append(ans[2]) 
        elif type == '1D' and nsrc == 1:
            guess.insert(0,guess[0])
            ans = fmin_powell(kepfunc.kepler_prf_1d,guess,args=args,xtol=tolerance,
                                  ftol=ftol,disp=False)
            f.append((ans[0] + ans[1]) / 2)
            y.append(ans[2])
            x.append(ans[3]) 
        else:
            ans = fmin_powell(kepfunc.kepler_multi_prf_2d,guess,args=args,xtol=tolerance,
                              ftol=ftol,disp=False)
            for i in xrange(nsrc):
                f.append(ans[i])
                y.append(ans[nsrc+i])
                x.append(ans[nsrc*2+i]) 

# calculate best-fit model

    if status == 0:
        prfMod = numpy.zeros((prfDimY+1,prfDimX+1))
        for i in xrange(nsrc):
            prfTmp = shift(prf,[y[i],x[i]],order=1,mode='constant')
            prfTmp = prfTmp[prfY0:prfY0+prfDimY,prfX0:prfX0+prfDimX]
            prfMod = prfMod + prfTmp * f[i]
        prfFit = rebin2D(prfMod,[numpy.shape(imgflux)[0],numpy.shape(imgflux)[1]],
                         interpolation,True,False) / cdelt1p[0] / cdelt2p[0]

# calculate residual between data and model

    if status == 0:
        prfRes = imgflux - prfFit

# convert PRF pixels sizes to CCD pixel sizes

    if status == 0:
        for i in xrange(nsrc):
            y[i] = y[i] * cdelt1p[0] + datCenY
            x[i] = x[i] * cdelt2p[0] + datCenX

    return f, y, x, prfMod, prfFit, prfRes
예제 #10
0
파일: kepfit.py 프로젝트: Sleekery/PyKE
def fitPRF(flux,ydim,xdim,column,row,prfn,crval1p,crval2p,cdelt1p,cdelt2p,interpolation,
           tolerance,guess,type,verbose):

# construct input summed image

    status = 0
    if status == 0:
        imgflux = empty((ydim,xdim))
        n = 0
        for i in range(ydim):
            for j in range(xdim):
                imgflux[i,j] = flux[n]
                n += 1

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]),dtype='float32')
        prfWeight = zeros((5),dtype='float32')
        for i in xrange(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 + (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
            prf = prf / nansum(prf)

# dimensions of data image

    if status == 0:
        datDimY = shape(imgflux)[0]
        datDimX = shape(imgflux)[1]

# dimensions of data image if it had PRF-sized pixels

    if status == 0:
        prfDimY = datDimY / cdelt1p[0]
        prfDimX = datDimX / cdelt2p[0]

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfY0 = (shape(prf)[0] - prfDimY) / 2
        prfX0 = (shape(prf)[1] - prfDimX) / 2

# fit input image with model

    if status == 0:
        args = (imgflux,prf,cdelt1p[0],cdelt2p[0],prfDimY,prfDimX,prfY0,prfX0,
                interpolation,verbose)
        if type == '2D':
            [f,y,x] = fmin_powell(kepfunc.kepler_prf_2d,guess,args=args,xtol=tolerance,
                                  ftol=1.0,disp=False)
        elif type == '1D':
            guess.insert(0,guess[0])
            [fy,fx,y,x] = fmin_powell(kepfunc.kepler_prf_1d,guess,args=args,xtol=tolerance,
                                  ftol=1.0,disp=False)
            f = (fx + fy) / 2 

# calculate best-fit model

    if status == 0:
        prfMod = shift(prf,[y,x],order=1,mode='constant')
        prfMod = prfMod[prfY0:prfY0+prfDimY,prfX0:prfX0+prfDimX]
        prfFit = rebin2D(prfMod,[numpy.shape(imgflux)[0],numpy.shape(imgflux)[1]],
                                  interpolation,True,False)
        prfFit = prfFit * f / cdelt1p[0] / cdelt2p[0]

# calculate residual between data and model

    if status == 0:
        prfRes = imgflux - prfFit

    return f, y * cdelt1p[0], x * cdelt2p[0], prfMod, prfFit, prfRes
예제 #11
0
def test(flux, ydim, xdim, column, row, prfn, crval1p, crval2p, cdelt1p,
         cdelt2p, interpolation, tolerance, ftol, fluxes, columns, rows, type,
         verbose, logfile):

    # construct input summed image

    status = 0
    if status == 0:
        imgflux = empty((ydim, xdim))
        n = 0
        for i in range(ydim):
            for j in range(xdim):
                imgflux[i, j] = flux[n]
                n += 1

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]), dtype='float32')
        prfWeight = zeros((5), dtype='float32')
        for i in range(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 +
                                (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
            prf = prf / nansum(prf)

# dimensions of data image

    if status == 0:
        datDimY = shape(imgflux)[0]
        datDimX = shape(imgflux)[1]

# dimensions of data image if it had PRF-sized pixels

    if status == 0:
        prfDimY = datDimY / cdelt1p[0]
        prfDimX = datDimX / cdelt2p[0]

# center of the data image (in CCD pixel units)

    if status == 0:
        datCenY = row + float(datDimY) / 2 - 0.5
        datCenX = column + float(datDimX) / 2 - 0.5

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfY0 = (shape(prf)[0] - prfDimY) / 2
        prfX0 = (shape(prf)[1] - prfDimX) / 2

# initial guess for fit parameters

    if status == 0:
        guess = []
        try:
            f = fluxes.strip().split(',')
            y = rows.strip().split(',')
            x = columns.strip().split(',')
            for i in range(len(f)):
                f[i] = float(f[i]) * numpy.nanmax(flux)
        except:
            f = fluxes
            y = rows
            x = columns
        for i in range(len(f)):
            try:
                guess.append(float(f[i]))
            except:
                message = 'ERROR -- KEPPRF: Fluxes must be floating point numbers'
                status = kepmsg.err(logfile, message, verbose)
        if status == 0:
            for i in range(len(y)):
                try:
                    guess.append((float(y[i]) - datCenY) / cdelt2p[0])
                except:
                    message = 'ERROR -- KEPPRF: Rows must be floating point numbers'
                    status = kepmsg.err(logfile, message, verbose)
        if status == 0:
            for i in range(len(x)):
                try:
                    guess.append((float(x[i]) - datCenX) / cdelt1p[0])
                except:
                    message = 'ERROR -- KEPPRF: Columns must be floating point numbers'
                    status = kepmsg.err(logfile, message, verbose)
        if status == 0:
            if len(x) != len(y) or len(x) != len(f):
                message = 'ERROR -- KEPFIT:FITMULTIPRF: Guesses for rows, columns and '
                message += 'fluxes must have the same number of sources'
                status = kepmsg.err(logfile, message, verbose)

# fit input image with model

    if status == 0:
        f = []
        x = []
        y = []
        nsrc = len(guess) / 3
        args = (imgflux, prf, cdelt1p[0], cdelt2p[0], prfDimY, prfDimX, prfY0,
                prfX0, interpolation, verbose)
        if type == '2D' and nsrc == 1:
            ans = fmin_powell(kepfunc.kepler_prf_2d,
                              guess,
                              args=args,
                              xtol=tolerance,
                              ftol=ftol,
                              disp=False)
            f.append(ans[0])
            y.append(ans[1])
            x.append(ans[2])
        elif type == '1D' and nsrc == 1:
            guess.insert(0, guess[0])
            ans = fmin_powell(kepfunc.kepler_prf_1d,
                              guess,
                              args=args,
                              xtol=tolerance,
                              ftol=ftol,
                              disp=False)
            f.append((ans[0] + ans[1]) / 2)
            y.append(ans[2])
            x.append(ans[3])
        else:
            ans = fmin_powell(kepfunc.kepler_multi_prf_2d,
                              guess,
                              args=args,
                              xtol=tolerance,
                              ftol=ftol,
                              disp=False)
            for i in range(nsrc):
                f.append(ans[i])
                y.append(ans[nsrc + i])
                x.append(ans[nsrc * 2 + i])

# calculate best-fit model

    if status == 0:
        prfMod = numpy.zeros((prfDimY + 1, prfDimX + 1))
        for i in range(nsrc):
            prfTmp = shift(prf, [y[i], x[i]], order=1, mode='constant')
            prfTmp = prfTmp[prfY0:prfY0 + prfDimY, prfX0:prfX0 + prfDimX]
            prfMod = prfMod + prfTmp * f[i]
        prfFit = rebin2D(prfMod,
                         [numpy.shape(imgflux)[0],
                          numpy.shape(imgflux)[1]], interpolation, True,
                         False) / cdelt1p[0] / cdelt2p[0]

# calculate residual between data and model

    if status == 0:
        prfRes = imgflux - prfFit

# convert PRF pixels sizes to CCD pixel sizes

    if status == 0:
        for i in range(nsrc):
            y[i] = y[i] * cdelt1p[0] + datCenY
            x[i] = x[i] * cdelt2p[0] + datCenX

    return f, y, x, prfMod, prfFit, prfRes
예제 #12
0
def fitPRF(flux, ydim, xdim, column, row, prfn, crval1p, crval2p, cdelt1p,
           cdelt2p, interpolation, tolerance, guess, type, verbose):

    # construct input summed image

    status = 0
    if status == 0:
        imgflux = empty((ydim, xdim))
        n = 0
        for i in range(ydim):
            for j in range(xdim):
                imgflux[i, j] = flux[n]
                n += 1

# interpolate the calibrated PRF shape to the target position

    if status == 0:
        prf = zeros(shape(prfn[0]), dtype='float32')
        prfWeight = zeros((5), dtype='float32')
        for i in range(5):
            prfWeight[i] = sqrt((column - crval1p[i])**2 +
                                (row - crval2p[i])**2)
            if prfWeight[i] == 0.0:
                prfWeight[i] = 1.0e6
            prf = prf + prfn[i] / prfWeight[i]
            prf = prf / nansum(prf)

# dimensions of data image

    if status == 0:
        datDimY = shape(imgflux)[0]
        datDimX = shape(imgflux)[1]

# dimensions of data image if it had PRF-sized pixels

    if status == 0:
        prfDimY = datDimY / cdelt1p[0]
        prfDimX = datDimX / cdelt2p[0]

# location of the data image centered on the PRF image (in PRF pixel units)

    if status == 0:
        prfY0 = (shape(prf)[0] - prfDimY) / 2
        prfX0 = (shape(prf)[1] - prfDimX) / 2

# fit input image with model

    if status == 0:
        args = (imgflux, prf, cdelt1p[0], cdelt2p[0], prfDimY, prfDimX, prfY0,
                prfX0, interpolation, verbose)
        if type == '2D':
            [f, y, x] = fmin_powell(kepfunc.kepler_prf_2d,
                                    guess,
                                    args=args,
                                    xtol=tolerance,
                                    ftol=1.0,
                                    disp=False)
        elif type == '1D':
            guess.insert(0, guess[0])
            [fy, fx, y, x] = fmin_powell(kepfunc.kepler_prf_1d,
                                         guess,
                                         args=args,
                                         xtol=tolerance,
                                         ftol=1.0,
                                         disp=False)
            f = (fx + fy) / 2

# calculate best-fit model

    if status == 0:
        prfMod = shift(prf, [y, x], order=1, mode='constant')
        prfMod = prfMod[prfY0:prfY0 + prfDimY, prfX0:prfX0 + prfDimX]
        prfFit = rebin2D(prfMod,
                         [numpy.shape(imgflux)[0],
                          numpy.shape(imgflux)[1]], interpolation, True, False)
        prfFit = prfFit * f / cdelt1p[0] / cdelt2p[0]

# calculate residual between data and model

    if status == 0:
        prfRes = imgflux - prfFit

    return f, y * cdelt1p[0], x * cdelt2p[0], prfMod, prfFit, prfRes