def makeDark(stackForDark=None, outPutFileName=None,verbose=True):
    """
    takes a list or array of arrays and makes a dark. Return an array with the dark. If outPutFileName is provided, it saves the dark in an npz
    """
    try: 
        dark = irUtils.medianStack(stackForDark)
        print "Making dark hot pixel mask"
        if verbose:
            plotArray(dark,title='Dark',origin='upper')
    except:
        print "####    Error with the stack of dark. makeDark takes a list of array of arrays to make the dark   ####"
        sys.exit(0)
    try:
        np.savez(darkPath,dark=dark)
    except: 
        if outPutFileName==None:
            print "No output file name provided. Dark not saved"
        else:
            print "Wrong output path. Dark not saved"
    return dark
def makeFlat(flatStack=None, dark=None, outPutFileName=None, badPixelMask=None, crop=False, cropColsForMedian=[(0,19),(60,79)], cropRowsForMedian=[(25,49)], verbose=True):
    """
    Input:
    flatStack: array or list of arrays to make the flat
    dark: dark. If none the flat is not dark subtracteed
    outPutFileName: if not provided returns the flat array without saving it
    crop: if False uses the entire array to calculate the median, otherwise it crops the columns in the range specified by cropForMedian
    cropColsForMedian, cropRowsForMedian: are tuples with each element being the range of columns/rows to be cropped when taking the median for the flat
    10/1/2017: currently crops the high frequency boards (bad performances) and FL2 (not working on Faceless)
    Returns:
    dictionary with 'weights' and 'flat'
    """
    
    try:
        flat = irUtils.medianStack(flatStack)
        print "Loading flat frame"
    except:
        print "No valid flat stack provided. Exiting"
        sys.exit(0)
    
    if verbose:
        plotArray(flat,title='NotdarkSubFlat')
    
    if dark==None:
        #dark subtract the flat
        print "Subtracting dark"
        flatSub=flat-dark
    else:
        print "Dark=None, will make a flat without dark subtraction"
        flatSub=flat
    
    flatSub[np.where(flatSub < 0.0)]=np.nan
    croppedFrame = np.copy(flatSub)
    if crop: 
        if cropColsForMedian!=None:
            cropColsForMedian=np.array(cropColsForMedian)
            for iCrop, cropInd in enumerate(cropColsForMedian):
                croppedFrame[:,cropInd[0]:cropInd[1]+1]=np.nan
        if cropRowsForMedian!=None:
            cropRowsForMedian=np.array(cropRowsForMedian)
            for iCrop, cropInd in enumerate(cropRowsForMedian):
                croppedFrame[cropInd[0]:cropInd[1]+1,:]=np.nan
    #Apply bad pixel mask 
    if badPixelMask!=None:
         print "Applying bad pixel mask"
         croppedFrame[badPixelMask!=0] = np.nan
    if verbose:
        plotArray(croppedFrame,title='Cropped flat frame') #Plot the cropped flat frame
    med = np.nanmedian(croppedFrame.flatten())
    print 'median', med
    
    flatSub[flatSub==0]=1
    weights = med/flatSub #calculate the weights by dividing the median by the dark-subtracted flat frame
    if verbose: 
        plotArray(weights,title='Weights') #Plot the weights
   
    try: 
        np.savez(outPutFileName,weights = weights,flat=flatSub)
    except:
        if outPutFileName==None:
            print 'No output file name provided. Not saving flat'
        else:
            print 'Output file name not valid. Not saving flat'
    dict={}
    dict['flat']=flat
    dict['weights']=weights
    return dict
            originalPreciseYs >= 0) & (preciseYs <= 125)
        ax.scatter(xs[goodMask],
                   ys[goodMask],
                   label=label,
                   color=colors[iLabel])
        for (x, y) in zip(xs[goodMask], ys[goodMask]):
            if 0 <= x and x < nCols and 0 <= y and y < nRows:
                if grid[y, x] != 0:
                    grid[y, x] = 10
                else:
                    grid[y, x] += iLabel + 1
    ax.legend(loc='best')
    ylims = ax.get_ylim()
    ax.set_ylim(ylims[1], ylims[0])

    plotArray(grid, origin='upper')

    #now try to clean up overlapping pixels
    for iLabel, (label, filename) in enumerate(zip(labels, filenames)):
        print iLabel
        yOffset = yOffsets[iLabel]
        resIds = locationData[iLabel][:, 0]
        flags = locationData[iLabel][:, 1]
        xs = np.floor(locationData[iLabel][:, 2])
        ys = np.floor(locationData[iLabel][:, 3]) + yOffset
        ys[xs == -1] = -1
        preciseXs = locationData[iLabel][:, 2]
        preciseYs = locationData[iLabel][:, 3] + yOffset
        fails = np.array(locationData[iLabel][:, 1], dtype=np.bool)
        originalPreciseYs = locationData[iLabel][:, 3]
        goodMask = (~fails) & (preciseXs >= 0) & (originalPreciseYs >= 0)
示例#4
0
        preciseXs = segmentLocationData[:,2]
        preciseYs = segmentLocationData[:,3]+yOffset
        originalPreciseYs = segmentLocationData[:,3]
        goodMask = (~fails) & (preciseXs >= 0) & (originalPreciseYs >= 0)
        ax.scatter(xs[goodMask],ys[goodMask],label=label,color=colors[iLabel])
        for (x,y) in zip(xs[goodMask],ys[goodMask]):
            if 0 <= x and x < nCols and 0 <= y and y < nRows:
                if grid[y,x] != 0:
                    grid[y,x] = 10
                else:
                    grid[y,x] += iLabel+1
    ax.legend(loc='best')
    ylims = ax.get_ylim()
    ax.set_ylim(ylims[1],ylims[0])

    plotArray(grid,origin='upper')

    #now try to clean up overlapping pixels
    for iLabel,(label,filename) in enumerate(zip(labels,filenames)):
        print iLabel
        yOffset = yOffsets[iLabel]
        resIds = locationData[iLabel][:,0]
        xs = np.floor(locationData[iLabel][:,2])
        ys = np.floor(locationData[iLabel][:,3]) + yOffset
        ys[xs==-1] = -1
        preciseXs = locationData[iLabel][:,2]
        preciseYs = locationData[iLabel][:,3]+yOffset
        fails = np.array(locationData[iLabel][:,1],dtype=np.bool)
        originalPreciseYs = locationData[iLabel][:,3]
        goodMask = (~fails) & (preciseXs >= 0) & (originalPreciseYs >= 0)
        print 'good',np.sum(goodMask)
示例#5
0
dPath = os.path.join(rPath,date)
flatPath = os.path.join(dPath,flatFileName)

baseFN = flatFileName.split('.')[0]
outFN = baseFN+'_weights.npz'
outPath = os.path.join(dPath,outFN)

try:
    flatData = np.load(flatPath)
except:
    print "couldn't find flat stack .npz file at path:"
    print flatPath
    sys.exit(0)

flatFrame = flatData['final']
plotArray(flatFrame,title='flat frame')

########
# This cropping assumes poor FL2 performance and
# poor high frequency performance, a la Faceless from 2017a,b.
# This is a good place to play around to change crappy weights
# at fringes of the array.
#######
croppedFrame = np.copy(flatFrame)

if cropFLs:
    croppedFrame[25:50,::] = np.nan
if cropHF:
    croppedFrame[::,:20] = np.nan
    croppedFrame[::,59:] = np.nan
示例#6
0
rWvls = np.array([808, 920, 980, 1120, 1310])
rmasks = np.array([
    rdata['good_fits_808nm'], rdata['good_fits_920nm'],
    rdata['good_fits_980nm'], rdata['good_fits_1120nm'],
    rdata['good_fits_1310nm']
])

dataDir = '/mnt/data0/ScienceDataIMGs/LabData/20170712/'
for i in range(len(wvls)):
    #find nearest "good pix" mask from Nick's R data
    wvlDiffs = np.abs(rWvls - wvls[i])
    nearestWvl = rWvls[np.where(wvlDiffs == np.min(wvlDiffs))][0]
    print nearestWvl
    nearestMask = rmasks[np.where(rWvls == nearestWvl)][0].transpose()

    if i == 0: plotArray(nearestMask)
    stack = loadIMGStack(dataDir,
                         light[i][0],
                         light[i][1],
                         nCols=80,
                         nRows=125)
    darkstack = loadIMGStack(dataDir,
                             dark[i][0],
                             dark[i][1],
                             nCols=80,
                             nRows=125)

    bpm = dhpm.makeMask(run='LabData',
                        date='20170712',
                        startTimeStamp=dark[i][0],
                        stopTimeStamp=dark[i][1],
示例#7
0
def makeDHPMask(stack=None, outputFileName=None, verbose=False, sigma=3, maxCut=2450, coldCut=False, manualArray=None):
    '''
    MaxCut sets level for initial hot pixel cut. Everything with cps>maxCut -> np.nan
    Sigma determines level for second cut. Everything with cps>mean+sigma*stdev -> np.nan
    If coldCut=True, third cut where everything with cps<mean-sigma*stdev -> np.nan
    manualArray is a way to give a list of bad pixels manually, in format [[row,col],[row,col],...]


    '''
    medStack = irUtils.medianStack(stack)

    if verbose:
        try:
            plotArray(medStack,title='Median Dark Stack')
        except:
            plt.matshow(medStack)
            plt.show()

    #initial masking, take out anything with cps > maxCut
    mask = np.zeros(np.shape(medStack),dtype=int)
    mask[np.where(medStack>=maxCut)]=1

    if verbose:
        try:
            plotArray(mask, title='cps>%i == 1'%maxCut)
        except:
            plt.matshow(mask)
            plt.show()

    medStack[np.where(mask==1)]=np.nan
    medStack[np.where(medStack==0)]=np.nan
    if verbose:
        try:
            plotArray(medStack, title='Median Stack with mask 1')
        except:
            plt.matshow(medStack)
            plt.show()

    #second round of masking, where cps > mean+sigma*std
    mask2 = np.zeros(np.shape(medStack),dtype=int)
    mask2[np.where(medStack>=np.nanmean(medStack)+sigma*np.nanstd(medStack))]=1

    #if coldCut is true, also mask cps < mean-sigma*std
    if coldCut==True:
        mask2[np.where(medStack<=np.nanmean(medStack)-sigma*np.nanstd(medStack))]=1

    if verbose:
        try:
            plotArray(mask2, title='cps>mean+%i-sigma == 1'%sigma)
        except:
            plt.matshow(mask2)
            plt.show()

    medStack[np.where(mask2==1)]=np.nan
    if verbose:
        try:
            plotArray(medStack, title='Median Stack with mask 2')
        except:
            plt.matshow(medStack)
            plt.show()

    finalMask = mask+mask2

    # provide an easy means to pipe in an array of manually identified pixels
    if manualArray is not None:
        for pix in manualArray:
            finalMask[pix[0],pix[1]]=1


    if verbose:
        try:
            plotArray(finalMask, title='Final mask')
        except:
            plt.matshow(finalMask)
            plt.show()
        print("Number masked pixels = ", len(np.array(np.where(finalMask==1)).flatten()))

    return finalMask
示例#8
0
#fits reading test
import numpy as np
import pyfits
from arrayPopup import plotArray

def readFITS(filename):
    f = pyfits.open(filename)
    scidata = np.array(f[0].data)
    return scidata

if __name__ == "__main__":
    fname = '/mnt/data0/ProcessedData/seth/imageStacks/PAL2016b/SAO42642_8Dithers_3xSamp_allHPM_20161122.fits'
    im = readFITS(fname)
    plotArray(im,title='loaded fits image',origin='upper',vmin=0)
示例#9
0
def makeMask(run=None, date=None, basePath=None,startTimeStamp=None, stopTimeStamp=None, verbose=False, sigma=3, maxCut=2450, coldCut=False, manualArray=None):
    '''
    MaxCut sets level for initial hot pixel cut. Everything with cps>maxCut -> np.nan
    Sigma determines level for second cut. Everything with cps>mean+sigma*stdev -> np.nan
    If coldCut=True, third cut where everything with cps<mean-sigma*stdev -> np.nan
    manualArray is a way to give a list of bad pixels manually, in format [[row,col],[row,col],...]


    '''

    try:
        dataPath = basePath+str(run)+os.path.sep+str(date)+os.path.sep
        stack = loadIMGStack(dataPath, startTimeStamp, stopTimeStamp, nCols=nCols, nRows=nRows)
    except:
        print("Could not find dark data in ScienceData path, checking ramdisk")
        dataPath = '/mnt/ramdisk/'
        stack = loadIMGStack(dataPath, startTimeStamp, stopTimeStamp, nCols=nCols, nRows=nRows)

    medStack = ir.utils.medianStack(stack)

    if verbose:
        try:
            plotArray(medStack,title='Median Dark Stack')
        except:
            plt.matshow(medStack)
            plt.show()

    #initial masking, take out anything with cps > maxCut
    mask = np.zeros(np.shape(medStack),dtype=int)
    mask[np.where(medStack>=maxCut)]=1

    if verbose:
        try:
            plotArray(mask, title='cps>%i == 1'%maxCut)
        except:
            plt.matshow(mask)
            plt.show()

    medStack[np.where(mask==1)]=np.nan
    medStack[np.where(medStack==0)]=np.nan
    if verbose:
        try:
            plotArray(medStack, title='Median Stack with mask 1')
        except:
            plt.matshow(medStack)
            plt.show()

    #second round of masking, where cps > mean+sigma*std
    mask2 = np.zeros(np.shape(medStack),dtype=int)
    mask2[np.where(medStack>=np.nanmean(medStack)+sigma*np.nanstd(medStack))]=1

    #if coldCut is true, also mask cps < mean-sigma*std
    if coldCut==True:
        mask2[np.where(medStack<=np.nanmean(medStack)-sigma*np.nanstd(medStack))]=1

    if verbose:
        try:
            plotArray(mask2, title='cps>mean+%i-sigma == 1'%sigma)
        except:
            plt.matshow(mask2)
            plt.show()

    medStack[np.where(mask2==1)]=np.nan
    if verbose:
        try:
            plotArray(medStack, title='Median Stack with mask 2')
        except:
            plt.matshow(medStack)
            plt.show()

    finalMask = mask+mask2

    # provide an easy means to pipe in an array of manually identified pixels
    if manualArray is not None:
        for pix in manualArray:
            finalMask[pix[0],pix[1]]=1


    if verbose:
        try:
            plotArray(finalMask, title='Final mask')
        except:
            plt.matshow(finalMask)
            plt.show()
        print("Number masked pixels = ", len(np.array(np.where(finalMask==1)).flatten()))

    return finalMask
示例#10
0
def plotMask(mask):
    try:
        plotArray(mask, title='hp Mask')
    except:
        plt.matshow(mask)
        plt.show()
示例#11
0
                                         wvlRange=None)
        print('Running getPixelCountImage on ', firstSec, 'seconds to ',
              intTime, 'seconds of data on all wavelengths')
    processedIm = np.transpose(img['image']) / intTime
    print(np.shape(processedIm))
    processedIm = processedIm[50:124, 0:80]

    roughShiftsX.append(dXs[i])
    roughShiftsY.append(dYs[i])
    centroidsX.append(refPointX - dXs[i])
    centroidsY.append(refPointY - dYs[i])

    #plot an example of the UNmasked image for inspection
    if i == 0:
        plotArray(processedIm,
                  title='Dither Pos %i' % i,
                  origin='upper',
                  vmin=0)
    #cut out cold/dead pixels
    processedIm[np.where(processedIm <= coldCut)] = np.nan

    #plot an example of the masked image for inspection
    if i == 0:
        plotArray(processedIm,
                  title='Dither Pos %i ColdPix Masked' % i,
                  origin='upper',
                  vmin=0)

    #pad frame with margin for shifting and stacking
    paddedFrame = irUtils.embedInLargerArray(processedIm,
                                             frameSize=padFraction)
    outfile = h5dir + outfileName + str(i)
示例#12
0
           print('Running getPixelCountImage on ',firstSec,'seconds to ',intTime,'seconds of data from wavelength ',wvlStart,'to ',wvlStop)
        else:
           img = obsfile.getPixelCountImage(firstSec =0, integrationTime=intTime,applyWeight=False,flagToUse = 0,wvlRange = None)
           print('Running getPixelCountImage on ',firstSec,'seconds to ',intTime,'seconds of data on all wavelengths')
        processedIm = np.transpose(img['image'])/intTime
        print(np.shape(processedIm))
        processedIm=processedIm[50:124, 0:80]

        roughShiftsX.append(dXs[i])
        roughShiftsY.append(dYs[i])
        centroidsX.append(refPointX-dXs[i])
        centroidsY.append(refPointY-dYs[i])

       #plot an example of the UNmasked image for inspection
        if i==0:
            plotArray(processedIm,title='Dither Pos %i'%i,origin='upper',vmin=0)
        #cut out cold/dead pixels
        processedIm[np.where(processedIm<=coldCut)]=np.nan

        #plot an example of the masked image for inspection
        if i==0:
            plotArray(processedIm,title='Dither Pos %i ColdPix Masked'%i,origin='upper',vmin=0)

        #pad frame with margin for shifting and stacking
        paddedFrame = irUtils.embedInLargerArray(processedIm,frameSize=padFraction)
        outfile=h5dir+outfileName+str(i)
        np.save(outfile, paddedFrame)

        #apply rough dX and dY shift to frame
        print("Shifting dither %i, frame %i by x=%i, y=%i"%(i,0,dXs[i], dYs[i]))
        shiftedFrame = irUtils.rotateShiftImage(paddedFrame,0,dXs[i],dYs[i])
示例#13
0
def stackCube(h5File, npzFile, verbose=True):
    #load npz file with cubes and timestamps
    npzDict = loadCubeStack(npzFile)
    cubeTimes = npzDict['times']
    cubeCubes = npzDict['cubes']
    cubeWBEs = npzDict['wvlBinEdges']
    #define bin center wavelengths (in nm)
    wbWidths = np.diff(cubeWBEs)
    centers = cubeWBEs[:-1] + wbWidths / 2.0
    nWvlBins = len(centers)
    if verbose: print "Loaded npz file..."

    #load h5 file with centroids, image resampling info, dark frames, and timestamps
    h5Dict = loadH5Stack(h5File)
    h5Times = h5Dict['times']
    centXs = h5Dict['centX']
    centYs = h5Dict['centY']
    hotPix = h5Dict['hpm']
    #get image stacking info from params dictionary
    paramsDict = h5Dict['params']
    padFraction = np.float(paramsDict['padFraction'][0])
    upSample = paramsDict['upSample'][0]
    doHPM = bool(paramsDict['doHPM'][0])
    coldCut = paramsDict['coldCut'][0]
    if verbose: print "Loaded h5 file..."

    if cubeTimes.all() == h5Times.all():
        print "Timestamps match. Carrying on with stacking..."
    else:
        print "Timestamp mismatch between two files!!"
        print cubeTimes
        print h5Times
        sys.exit(0)

    if doHPM:
        hpMask = hotPix[0]

    cubeStack = []
    for i in range(nWvlBins):
        cubeStack.append([])
    finalCube = []
    finalTimes = []

    for t in np.arange(len(cubeTimes)):
        time = cubeTimes[t]
        cube = cubeCubes[t]
        centX = centXs[t]
        centY = centYs[t]
        finalTimes.append(cubeTimes[t])
        for w in np.arange(nWvlBins):
            im = np.array(cube[:, :, w], float)
            im = np.transpose(im)

            #apply hp mask to image
            if doHPM:
                im[np.where(hpMask == 1)] = np.nan

            #cut out cold/dead pixels
            im[np.where(im <= coldCut)] = np.nan

            #pad frame with margin for shifting and stacking
            paddedFrame = irUtils.embedInLargerArray(im, frameSize=padFraction)

            #upSample frame for sub-pixel registration with fitting code
            upSampledFrame = irUtils.upSampleIm(paddedFrame, upSample)
            #conserve flux. Break each pixel into upSample^2 sub pixels,
            #each subpixel should have 1/(upSample^2) the flux of the original pixel
            upSampledFrame /= float(upSample * upSample)

            ### UPDATE WITH DX AND DY DETERMINED FROM ACTUAL STARTING POINT
            ### SHIFTS ALL TO 0 RIGHT NOW
            #apply dX and dY shift to frame
            dX = centX * -1. * upSample
            dY = centY * -1. * upSample
            if verbose:
                print "Shifting timestamp %i, wvl %i by x=%2.2f, y=%2.2f" % (
                    t, w, dX, dY)
            shiftedFrame = irUtils.rotateShiftImage(upSampledFrame, 0, dX, dY)

            cubeStack[w].append(shiftedFrame)

    cubeStack = np.array(cubeStack)
    for n in np.arange(nWvlBins):
        finalCube.append(irUtils.medianStack(cubeStack[n]))
        if verbose:
            plotArray(finalCube[n], title="%i nm" % centers[n])
    finalCube = np.array(finalCube)

    return {
        'finalCube': finalCube,
        'wvls': centers,
        'cubeStack': cubeStack,
        'times': finalTimes
    }
示例#14
0
                tauArray[row,col]=np.nan
            else:
                tauArray[row,col]=tau_corr
            #add another check here for hot pixel masks to force those to NAN as well

        except:
            print "Failed to get tau_corr, setting Tau to NAN"
            tauArray[row,col]=np.nan
       
        
        
        ax1.legend(loc=4)
        ax2.legend()
        ax3.legend(loc=0)
        plt.show()
plotArray(tauArray*1000,title='Tau_C [ms]',origin='upper',vmin=0)


'''

    #take light curve and bin into histogram of intensities
    intHistBinEdges = None
    nBinsPerUnitInt = 0.5
    nBins = 30
    pixCutOffIntensities=[]
    listToHist = lc
    intRange = np.array([np.min(listToHist),np.max(listToHist)])
    intBins = (intRange[1]-intRange[0])//nBinsPerUnitInt
    
    intHist,intHistBinEdges = np.histogram(listToHist,bins=nBins,range=intRange)
    intHist = np.array(intHist)/float(len(lc))
        print 'Usage: {} tstampStart tstampEnd'.format(sys.argv[0])
        exit(0)
    else:
        startTstamp = int(sys.argv[1])
        endTstamp = int(sys.argv[2])

    darkStart = 1469354906
    darkEnd = 1469354926
    darkStack = loadImageStack(darkStart, darkEnd)
    darkFrame = medianFrame(darkStack)

    imageStack = loadImageStack(startTstamp, endTstamp)
    #option to flatten stack to median combined frame
    mf = medianFrame(imageStack)

    FITSfile = str(startTstamp) + '_to_' + str(
        endTstamp) + '_goodBeammapOnly.fits'
    path = outpath + FITSfile
    writeFits(imageStack, path)

    if not mf == None:
        medianFile = str(startTstamp) + '_to_' + str(
            endTstamp) + '_goodBeammapOnly_median.fits'
        path = outpath + medianFile
        writeFits(mf, path)
        if verbose:
            plotArray(image=mf)
            #form = PopUp(showMe=False,title='B')
            #form.plotArray(np.arange(9).reshape(3,3))
            #form.show()
示例#16
0
        #append upsampled, padded frame to array for storage into next part of code
        ditherFrames.append(upSampledFrame)

    print "Loaded dither position %i"%i

shiftedFrames = np.array(ditherFrames)
#if fitPos==True, do second round of shifting using mpfit correlation
#using x and y pos from earlier as guess
if fitPos==True:
    reshiftedFrames=[]
    
    if refFile!=None and os.path.exists(refFile):
        refIm = readFITS(refFile)
        print "Loaded %s for fitting"%refFile
        plotArray(refIm,title='loaded reference FITS',origin='upper',vmin=0)
    else:
        refIm=shiftedFrames[0]

    cnt=0
    for im in shiftedFrames:
        print "\n\n------------------------------\n"
        print "Fitting frame %i of %i"%(cnt,len(shiftedFrames))
        pGuess=[0,1,1]
        pLowLimit=[-1,(dXs.min()-5)*upSample,(dYs.min()-5)*upSample]
        pUpLimit=[1,(dXs.max()+5)*upSample,(dYs.max()+5)*upSample]
        print "guess", pGuess, "limits", pLowLimit, pUpLimit

        #mask out background structure, only fit on known object location
        maskRad=apMaskRadPrim
        pMask = aperture(xPos[0]*upSample,yPos[0]*upSample,maskRad*upSample, numRows*upSample, numCols*upSample)
示例#17
0
fails = np.array(flags, dtype=np.bool)
#define mask of good pixels
goodMask = (~fails)

#look through goodMask positions and add each pixel to their corresponding location in the grid
#if a grid position already has
for (x, y) in zip(xs[goodMask], ys[goodMask]):
    grid[y, x] += 1

#plot initial "good" grid with number of resonators assigned to each location
fig, ax = plt.subplots(1, 1)
ax.legend(loc='best')
ylims = ax.get_ylim()
ax.set_ylim(ylims[1], ylims[0])
plotArray(grid,
          title='Original "good" flagged pixel locations',
          origin='upper')

#setup dictionary for storing new output data
for i in range(len(ids)):
    resId = ids[i]
    distVector = (preciseXs[i] - (xs[i] + .5), preciseYs[i] - (ys[i] + .5))
    distMag = np.sqrt(distVector[0]**2 + distVector[1]**2)
    gridDicts.append({
        'x': xs[i],
        'y': ys[i],
        'preciseX': preciseXs[i],
        'preciseY': preciseYs[i],
        'distMag': distMag,
        'resId': resId,
        'fail': flags[i]
示例#18
0
def stackCube(h5File,npzFile, verbose=True):
    #load npz file with cubes and timestamps
    npzDict = loadCubeStack(npzFile)
    cubeTimes = npzDict['times']
    cubeCubes = npzDict['cubes']
    cubeWBEs = npzDict['wvlBinEdges']
    #define bin center wavelengths (in nm)
    wbWidths = np.diff(cubeWBEs)
    centers = cubeWBEs[:-1]+wbWidths/2.0
    nWvlBins = len(centers)
    if verbose: print "Loaded npz file..."

    #load h5 file with centroids, image resampling info, dark frames, and timestamps
    h5Dict = loadH5Stack(h5File)
    h5Times = h5Dict['times']
    centXs = h5Dict['centX']
    centYs = h5Dict['centY']
    hotPix = h5Dict['hpm']
    #get image stacking info from params dictionary
    paramsDict = h5Dict['params']
    padFraction = np.float(paramsDict['padFraction'][0])
    upSample = paramsDict['upSample'][0]
    doHPM = bool(paramsDict['doHPM'][0])
    coldCut = paramsDict['coldCut'][0]
    if verbose: print "Loaded h5 file..."

    if cubeTimes.all() == h5Times.all():
        print "Timestamps match. Carrying on with stacking..."
    else:
        print "Timestamp mismatch between two files!!"
        print cubeTimes
        print h5Times
        sys.exit(0)

    if doHPM:
        hpMask = hotPix[0]

    cubeStack = []
    for i in range(nWvlBins): cubeStack.append([])
    finalCube = []
    finalTimes = []

    for t in np.arange(len(cubeTimes)):
        time = cubeTimes[t]
        cube = cubeCubes[t]
        centX = centXs[t]
        centY = centYs[t]
        finalTimes.append(cubeTimes[t])
        for w in np.arange(nWvlBins):
            im = np.array(cube[:,:,w],float)
            im = np.transpose(im)
            
            #apply hp mask to image
            if doHPM:
                im[np.where(hpMask==1)]=np.nan

            #cut out cold/dead pixels
            im[np.where(im<=coldCut)]=np.nan

            #pad frame with margin for shifting and stacking
            paddedFrame = irUtils.embedInLargerArray(im,frameSize=padFraction)

            #upSample frame for sub-pixel registration with fitting code
            upSampledFrame = irUtils.upSampleIm(paddedFrame,upSample)
            #conserve flux. Break each pixel into upSample^2 sub pixels, 
            #each subpixel should have 1/(upSample^2) the flux of the original pixel
            upSampledFrame/=float(upSample*upSample)

            ### UPDATE WITH DX AND DY DETERMINED FROM ACTUAL STARTING POINT
            ### SHIFTS ALL TO 0 RIGHT NOW
            #apply dX and dY shift to frame
            dX = centX*-1.*upSample
            dY = centY*-1.*upSample
            if verbose:
                print "Shifting timestamp %i, wvl %i by x=%2.2f, y=%2.2f"%(t,w, dX, dY)
            shiftedFrame = irUtils.rotateShiftImage(upSampledFrame,0,dX,dY)

            cubeStack[w].append(shiftedFrame)

    cubeStack = np.array(cubeStack)
    for n in np.arange(nWvlBins):
        finalCube.append(irUtils.medianStack(cubeStack[n]))
        if verbose:
            plotArray(finalCube[n],title="%i nm"%centers[n])
    finalCube = np.array(finalCube)

    return {'finalCube':finalCube,'wvls':centers, 'cubeStack':cubeStack, 'times':finalTimes}
示例#19
0
    hpmfile=np.load(hpmPath)
    darkHPM=hpmfile['darkHPM']
    print "Found HPM File"
else:
    print "Could not find existing hot pix mask, generating one from dark..."
    #try:
    print 'maxCut',maxCut 
    darkHPMImg = dhpm.makeMask(run=run, date=date, basePath=imgDir,startTimeStamp=darkSpanImg[0], stopTimeStamp=darkSpanImg[1], coldCut=True, maxCut=maxCut,sigma=sigma,manualArray=None)
    hpFN='darkHPMImg_'+target+'.npz'  #Save the hot pixel mask into the output directory
    hpPath = os.path.join(outputDir,hpFN)
    np.savez(hpPath,darkHPMImg = darkHPMImg)
    print hpPath
    #except:
    #      print "Failed to generate dark mask. Turning off hot pixel masking"
        #  doHPM = False
plotArray(darkHPMImg, title='Hot Pixel Mask Image', origin='upper')



if calibPath[0]!='0':
    hpmPath=os.path.join(calibPath,"darkHPM_"+target+".npz")
    hpmfile=np.load(hpmPath)
    darkHPM=hpmfile['darkHPM']
    print "Found HPM File"
else:
    print "Could not find existing hot pix mask, generating one from dark..."
    try:
       darkHPMFlat = dhpm.makeMask(run=run, date=date, basePath=imgDir,startTimeStamp=darkSpanFlat[0], stopTimeStamp=darkSpanFlat[1], coldCut=True, maxCut=maxCut,sigma=sigma,manualArray=None)
       hpFN='darkHPMFlat_'+target+'.npz'  #Save the hot pixel mask into the output directory
       hpPath = os.path.join(outputDir,hpFN)
       np.savez(hpPath,darkHPMFlat = darkHPMFlat)