Exemple #1
0
def test_DEC(coords, coord_types, ra, dec, new_dec, out_dec):
    my_wcs = makeWCS(coords=coords, coord_types=coord_types)
    image = AstroImage(wcs=my_wcs)
    assert image.ra == ra
    assert image.dec == dec
    image.dec = new_dec
    assert image.dec == out_dec
Exemple #2
0
def test_addHistory(history):
    image = AstroImage()
    for item in history:
        image.addHistory(item)
    for item in history:
        assert item in image.history
    for item in image.history:
        assert item in history
Exemple #3
0
def test_PA(pa_in, pa, scale, new_pa, pa_out):
    image = AstroImage(pa=pa_in, scale=scale)
    assert abs(image.pa - pa) < 1.e-4
    assert abs(image.xscale - scale[0]) < 1.e-4
    assert abs(image.yscale - scale[1]) < 1.e-4
    image.pa = new_pa
    assert abs(image.pa - pa_out) < 1.e-4
    assert abs(image.xscale - scale[0]) < 1.e-4
    assert abs(image.yscale - scale[1]) < 1.e-4
Exemple #4
0
def test_initDataFromFits(file, inputs, results):
    image = AstroImage.initDataFromFits(file, **inputs)
    fits = pyfits.open(file)
    data = fits[inputs["ext"]].data
    fits.close()
    verifyParameters(image, results)
    verifyData(image.data, data)
Exemple #5
0
def test_initFromPoints(xs, ys, rates, datasize, inputs, results):
    image = AstroImage.initFromPoints(xs, ys, rates, **inputs)
    verifyParameters(image, results)
    dat = numpy.zeros((datasize[1], datasize[0]), dtype='float32')
    dat[0, 3] = 3.
    dat[2, 7] = 1.3e-7
    dat[7, 9] = 2.9e5
    dat[12, 17] = 0.2
    dat[14, 22] = 0.7
    verifyData(image.data, dat)
Exemple #6
0
def test_addCatalogue(inputs, catalogue, final):
    image = AstroImage(**inputs)
    out_cat = image.addCatalogue(catalogue)
    if os.path.exists(out_cat):
        os.remove(out_cat)
    verifyData(image.data, final)
Exemple #7
0
def test_convolve(input, kernel, result):
    input_image = AstroImage(data=input)
    kernel_image = AstroImage(data=kernel)
    input_image.convolve(kernel_image)
    verifyData(input_image.data, result)
Exemple #8
0
def test_updateHeader(header_in, header_out):
    image = AstroImage()
    for k, v in header_in:
        image.updateHeader(k, v)
    for k, v in header_out:
        assert image.header[k] == v
S3dir = os.path.join(PPOL_dir, 'S3_Astrometry')

# This is the location where all pyPol data will be saved
pyPol_data = 'C:\\Users\\Jordan\\FITS_data\\Mimir_data\\pyPol_data'

# This is the directory where the 2MASS tiles of the targets have been saved
# Go to "http://hachi.ipac.caltech.edu/" to download 2MASS tiles
TMASSdir = "C:\\Users\\Jordan\\Libraries\\python\\Mimir_pyPol\\2MASSimages"

# Setup new directory for background subtracted data
bkgSubDir = os.path.join(pyPol_data, 'bkgSubtracted')
if (not os.path.isdir(bkgSubDir)):
    os.mkdir(bkgSubDir, 0o755)

# Read in Kokopelli mask generated in previous step
kokopelliMask = (AstroImage('kokopelliMask.fits').arr != 0)

# Read in the indexFile data and select the filenames
indexFile = os.path.join(pyPol_data, 'reducedFileIndex.csv')
fileIndex = Table.read(indexFile, format='csv')

# Grab the file basenames for later use
fileIndexFileNames = np.array(
    [os.path.basename(file1) for file1 in fileIndex['Filename'].data])

# Modify the fileIndex to include rejections by residual value
if 'Background Cut' not in fileIndex.keys():
    fileIndex.add_column(
        Column(name='Background Cut', data=np.repeat(0, len(fileIndex))))

# Determine which parts of the fileIndex pertain to science images
# Setup new directory for polarimetry data
polarimetryDir = os.path.join(pyPol_data, 'Polarimetry')
if (not os.path.isdir(polarimetryDir)):
    os.mkdir(polarimetryDir, 0o755)

stokesDir = os.path.join(polarimetryDir, 'stokesImgs')
if (not os.path.isdir(stokesDir)):
    os.mkdir(stokesDir, 0o755)

# Setup Mimir detector properties
read_noise = 19.0 # electrons
effective_gain = 8.21 # electrons/ADU

# Read in the Kokopelli mask
if os.path.isfile('kokopelliMask.fits'):
    kokopelliMask = AstroImage('kokopelliMask.fits')
else:
    print('Kokopelli Mask is not built... go back and build it with step 2.')
    pdb.set_trace()
################################################################################
# READ IN CALIBRATION DATA
################################################################################
# Read in the instrumental U and Q images (and associated uncertainties)
# First do H-band
# Q images
H_calDir      = os.path.join(PPOLcodeDir, 'H-Pol_Pinst_Default')
H_Q_instFile  = os.path.join(H_calDir, 'q_inst.fits')
H_sQ_instFile = os.path.join(H_calDir, 'q_sig_inst.fits')
H_Q_instImg   = AstroImage(H_Q_instFile)
H_sQ_instImg  = AstroImage(H_sQ_instFile)
H_Q_instImg.sigma = H_sQ_instImg.arr.copy()
    outFile = os.path.join(polAngDir, "_".join([thisTarget, thisWaveband, thisPolAng]) + ".fits")

    if os.path.isfile(outFile):
        print("File " + "_".join([thisTarget, thisWaveband, thisPolAng]) + " already exists... skipping to next group")
        continue

    numImgs = len(group)
    print("\nProcessing {0} images for".format(numImgs))
    print("\tTarget         : {0}".format(thisTarget))
    print("\tWaveband       : {0}".format(thisWaveband))
    print("\tPolaroid Angle : {0}".format(thisPolAng))

    # Read in the files of this group
    imgList = []
    for file1 in group["Filename"]:
        tmpImg = AstroImage(file1)

        # Test if this file has an associated mask
        maskFile = os.path.join(maskDir, os.path.basename(file1))
        if os.path.isfile(maskFile):
            # Read in any associated mask and add it to the arr attribute
            tmpMask = AstroImage(maskFile)

            # Mask the image by setting masked values to "np.NaN"
            tmpImg.arr[np.where(tmpMask.arr == 1)] = np.NaN

        # Grab the polaroid angle position from the header
        polPos = str(tmpImg.header["POLPOS"])

        # Check that the polPos value is correct
        if polPos != thisPolAng:
Exemple #12
0
def test_crop(input, size, offset, result):
    im1 = AstroImage(data=input)
    im1.crop(size[0], size[1], offset[0], offset[1])
    verifyData(im1.data, result)
Exemple #13
0
def test_addWithAlignment(in1, in2, inputs, result):
    im1 = AstroImage(data=in1)
    im2 = AstroImage(data=in2, **inputs)
    im1.addWithAlignment(im2)
    verifyData(im1.data, result)
Exemple #14
0
def test_creation(inputs, results):
    image = AstroImage(**inputs)
    data = numpy.zeros((image.ysize, image.xsize))
    verifyParameters(image, results)
    verifyData(image.data, data)
        onTargetTargets = (group['Target'])[np.where(imgOnTarget)]

        # Strip the leading and trailing spaces on the target names
        for i, target in enumerate(onTargetTargets):
            onTargetTargets[i] = target.strip()

        # Add the information to the fileList and targetList variables
        fileList.extend(onTargetFiles)
        targetList.extend(onTargetTargets)

#*************************************
# Now prepare to plot the first images
#*************************************

# Read in an image for masking
prevImg = AstroImage(fileList[imgNum - 1])
thisImg = AstroImage(fileList[imgNum])
nextImg = AstroImage(fileList[imgNum + 1])

# Log the targets of the curent panes
prevTarget = targetList[imgNum - 1]
thisTarget = targetList[imgNum]
nextTarget = targetList[imgNum + 1]

###
# For some reason the prevTarget, thisTarget, and nextTaret
# variables are not accessible from the event managers the way that
# prevImg, thisImg, and nextImg are.
# I have definitely declared them to be global variables...
# Perhaps they're getting treated as local variables
# because they are modified elsewhere???
        continue

    numImgs      = len(group)
    print('\nProcessing {0} images for'.format(numImgs))
    print('\tTarget         : {0}'.format(thisTarget))
    print('\tWaveband       : {0}'.format(thisWaveband))
    print('\tPolaroid Angle : {0}'.format(thisPolAng))

    # Read in the files of this group
    imgList  = []
    bkgTable = Table(names=('Night', 'Time', 'Bkg'), dtype=('i8','f8','f8'))
    for row in group:
        if row['ABBA'] == 'A':
            # Read on-target image into imgList
            file1 = row['Filename']
            tmpImg  = AstroImage(file1)

            # Test if this file has an associated mask
            maskFile = os.path.join(maskDir,os.path.basename(file1))
            if os.path.isfile(maskFile):
                # Read in any associated mask and add it to the arr attribute
                tmpMask = AstroImage(maskFile)

                # Mask the image by setting masked values to "np.NaN"
                tmpImg.arr[np.where(tmpMask.arr == 1)] = np.NaN

            # Grab the polaroid angle position from the header
            polPos = str(tmpImg.header['POLPOS'])

            # Check that the polPos value is correct
            if polPos != thisPolAng:
Exemple #17
0
    print('\tTarget   : {0}'.format(thisTarget))
    print('\tWaveband : {0}'.format(thisWaveband))

    # Read in the polAng image
    fileCheck = True
    polAngImgs = []
    polAngs = []
    for polAng in range(0, 601, 200):
        fileName = os.path.join(
            polAngDir, '_'.join([thisTarget, thisWaveband,
                                 str(polAng)]) + '.fits')

        # If the file exists, then read it into a dictionary
        if os.path.isfile(fileName):
            polAngs.append(polAng)
            polAngImgs.append(AstroImage(fileName))
        else:
            fileCheck = False

    # Check that all the necessarry files are present
    if not fileCheck:
        print("\tSome required Polaroid Angle files are missing.")
        continue

    # Use the "align_stack" method to align the newly created image list
    print('\nAligning images\n')
    polAngImgs = image_tools.align_images(polAngImgs,
                                          mode='cross_correlate',
                                          subPixel=True,
                                          padding=np.nan)
    "Orion_Cal_R_0117": {"night": 20150117, "numbers": (465, 488)},
    "Orion_Cal_V_0117": {"night": 20150117, "numbers": (489, 512)},
    "Taurus_Cal_V_0118": {"night": 20150118, "numbers": (277, 300)},
    "Taurus_Cal_R_0118": {"night": 20150118, "numbers": (301, 324)},
    "Orion_Cal_V_0119": {"night": 20150119, "numbers": (1, 24)},
    "Orion_Cal_R_0119": {"night": 20150119, "numbers": (25, 48)},
}

pyBDP_reduced = os.path.join(pyBDP_data, "pyBDP_reduced_images")

for group in groupDict.keys():
    print("Processing group {0}".format(group))
    night = "{0:d}".format(groupDict[group]["night"])
    startNum, stopNum = groupDict[group]["numbers"]
    # Loop through all the file numbers (be sure to include the LAST file!)
    for fileNum in range(startNum, stopNum + 1):
        # Construct the filename for this file
        strNum = "{0:03d}".format(fileNum)
        fileName = ".".join([str(night), strNum, "fits"])
        thisFile = os.path.join(pyBDP_reduced, fileName)

        if os.path.isfile(thisFile):
            # Read in the file
            thisImg = AstroImage(thisFile)

            # Modify the header to match the group name and re-write
            thisImg.header["OBJECT"] = group
            thisImg.write()

print("Done!")
Exemple #19
0
def test_rotate(input, angle, output):
    image = AstroImage(data=input)
    image.rotate(angle)
    verifyData(image.data, output)
Exemple #20
0
TMASSdir  = "C:\\Users\\Jordan\\Libraries\\python\\Mimir_pyPol\\2MASSimages"

# This dictionary specifies the angle range (degrees East of North) to include
# in producing radial-brightness proflies. This is especially for NGC2023
angleDict = {'NGC2023': (0, 360)}

# This is the location of the supersky images
bkgImagesDir = os.path.join(pyPol_data, 'bkgImages')

# Setup new directory for background subtracted data
bkgSubDir = os.path.join(pyPol_data, 'bkgSubtracted')
if (not os.path.isdir(bkgSubDir)):
    os.mkdir(bkgSubDir, 0o755)

# Read in Kokopelli mask generated in previous step
kokopelliMask = (AstroImage('kokopelliMask.fits').arr != 0)

# Read in the indexFile data and select the filenames
indexFile = os.path.join(pyPol_data, 'reducedFileIndex.csv')
fileIndex = Table.read(indexFile, format='csv')

# Check if the fileIndex already has a "Background" column.
# If not, then add one.
if 'Background' not in fileIndex.keys():
    fileIndex.add_column(Column(name='Background',
                                data = np.repeat(-99.9, len(fileIndex))))

# Determine which parts of the fileIndex pertain to science images
useFiles = np.where(fileIndex['Use'] == 1)
badFiles = np.where(fileIndex['Use'] == 0)
Exemple #21
0
def test_addWithOffset(in1, in2, offset, inputs, result):
    im1 = AstroImage(data=in1, **inputs)
    im2 = AstroImage(data=in2)
    im1.addWithOffset(im2, offset[0], offset[1])
    verifyData(im1.data, result)
Exemple #22
0
# Begin by initalizing some arrays to store the image classifications
telRA    = []
telDec   = []
name     = []
waveBand = []
HWPang   = []
binType  = []
expTime  = []
night    = []
fileCounter = 0
percentage  = 0

#Loop through each file in the fileList variable
for file in fileList:
    # Read in the image
    tmpImg = AstroImage(file)

    # Grab the RA and Dec from the header
    # Parse the pointing for this file
    tmpRA    = coord.Angle(tmpImg.header['TELRA'], unit=u.hour)
    tmpDec   = coord.Angle(tmpImg.header['TELDEC'], unit=u.degree)
    telRA.append(tmpRA.degree)
    telDec.append(tmpDec.degree)

    # Classify each file type and binning
    tmpName = tmpImg.header['OBJECT']
    if len(tmpName) < 1:
        tmpName = 'blank'
    name.append(tmpName)

    # Parse the HWP number
Exemple #23
0
def test_rescale(input, scale, result):
    im1 = AstroImage(data=input)
    im1.rescale(scale)
    verifyData(im1.data, result)
    im1.rescale([1., 1.])
    verifyData(im1.data, input)
def on_key(event):
    global fileList, targetList, fig, imgNum, brushSize
    global maskDir, maskImg
    global prevImg,   thisImg,   nextImg
    global prevAxImg, thisAxImg, nextAxImg
    global prevTarget, thisTarget, nextTarget
    global prevMin,   thisMin,   nextMin
    global prevMax,   thisMax,   nextMax
    global prevLabel, thisLabel, nextLabel

    # Handle brush sizing
    if event.key == '1':
        brushSize = 1
    elif event.key == '2':
        brushSize = 2
    elif event.key == '3':
        brushSize = 3
    elif event.key == '4':
        brushSize = 4
    elif event.key == '5':
        brushSize = 5
    elif event.key == '6':
        brushSize = 6

    # Increment the image number
    if event.key == 'right' or event.key == 'left':
        if event.key == 'right':
            #Advance to the next image
            imgNum += 1

            # Read in the new files
            prevImg = thisImg
            thisImg = nextImg
            nextImg = AstroImage(fileList[(imgNum + 1) % len(fileList)])

            # Update target info
            prevTarget = thisTarget
            thisTarget = nextTarget
            nextTarget = targetList[(imgNum + 1) % len(fileList)]

            # Compute new image display minima
            prevMin = thisMin
            thisMin = nextMin
            nextMin = np.median(nextImg.arr) - 0.25*np.std(nextImg.arr)

            # Compute new image display maxima
            prevMax = thisMax
            thisMax = nextMax
            nextMax = np.median(nextImg.arr) + 2*np.std(nextImg.arr)

        if event.key == 'left':
            #Move back to the previous image
            imgNum -= 1

            # Read in the new files
            nextImg = thisImg
            thisImg = prevImg
            prevImg = AstroImage(fileList[(imgNum - 1) % len(fileList)])

            # Update target info
            nextTarget = thisTarget
            thisTarget = prevTarget
            prevTarget = targetList[(imgNum - 1) % len(fileList)]

            # Compute new image display minima
            nextMin = thisMin
            thisMin = prevMin
            prevMin = np.median(prevImg.arr) - 0.25*np.std(prevImg.arr)

            # Compute new image display maxima
            nextMax = thisMax
            thisMax = prevMax
            prevMax = np.median(prevImg.arr) + 2*np.std(prevImg.arr)

        #*******************************
        # Update the displayed mask
        #*******************************

        # Check which mask files might be usable...
        prevMaskFile = os.path.join(maskDir,
            os.path.basename(prevImg.filename))
        thisMaskFile = os.path.join(maskDir,
            os.path.basename(thisImg.filename))
        nextMaskFile = os.path.join(maskDir,
            os.path.basename(nextImg.filename))
        if os.path.isfile(thisMaskFile):
            # If the mask for this file exists, use it
            print('using this mask: ',os.path.basename(thisMaskFile))
            maskImg = AstroImage(thisMaskFile)
        elif os.path.isfile(prevMaskFile) and (prevTarget == thisTarget):
            # Otherwise check for the mask for the previous file
            print('using previous mask: ',os.path.basename(prevMaskFile))
            maskImg = AstroImage(prevMaskFile)
        elif os.path.isfile(nextMaskFile) and (nextTarget == thisTarget):
            # Then check for the mask of the next file
            print('using next mask: ',os.path.basename(nextMaskFile))
            maskImg = AstroImage(nextMaskFile)
        else:
            # If none of those files exist, build a blank slate
            # Build a mask template (0 = not masked, 1 = masked)
            maskImg       = thisImg.copy()
            maskImg.filename = thisMaskFile
            maskImg.arr   = maskImg.arr.astype(np.int16) * np.int16(0)
            maskImg.dtype = np.byte
            maskImg.header['BITPIX'] = 16

        # Update contour plot (clear old lines redo contouring)
        axarr[1].collections = []
        axarr[1].contour(xx, yy, maskImg.arr, levels=[0.5], colors='white', alpha = 0.2)

        # Reassign image display limits
        prevAxImg.set_clim(vmin = prevMin, vmax = prevMax)
        thisAxImg.set_clim(vmin = thisMin, vmax = thisMax)
        nextAxImg.set_clim(vmin = nextMin, vmax = nextMax)

        # Display the new images
        prevAxImg.set_data(prevImg.arr)
        thisAxImg.set_data(thisImg.arr)
        nextAxImg.set_data(nextImg.arr)

        # Update the annotation
        axList = fig.get_axes()
        axList[1].set_title(os.path.basename(thisImg.filename))

        prevStr   = (str(prevImg.header['OBJECT']) + '\n' +
                     str(prevImg.header['FILTNME3'] + '\n' +
                     str(prevImg.header['POLPOS'])))
        thisStr   = (str(thisImg.header['OBJECT']) + '\n' +
                     str(thisImg.header['FILTNME3'] + '\n' +
                     str(thisImg.header['POLPOS'])))
        nextStr   = (str(nextImg.header['OBJECT']) + '\n' +
                     str(nextImg.header['FILTNME3'] + '\n' +
                     str(nextImg.header['POLPOS'])))
        prevLabel.set_text(prevStr)
        thisLabel.set_text(thisStr)
        nextLabel.set_text(nextStr)

        # Update the display
        fig.canvas.draw()

    # Save the generated mask
    if event.key == 'enter':
        # Make sure the header has the right values
        maskImg.header = thisImg.header

        # Write the mask to disk
        print('Writing mask for file ', os.path.basename(maskImg.filename))
        maskImg.write()

    # Clear out the mask values
    if event.key == 'backspace':
        # Clear out the mask array
        maskImg.arr = maskImg.arr * np.byte(0)

        # Update contour plot (clear old lines redo contouring)
        axarr[1].collections = []
        axarr[1].contour(xx, yy, maskImg.arr, levels=[0.5], colors='white', alpha = 0.2)

        # Update the display
        fig.canvas.draw()
Exemple #25
0
def test_bin(input, bin, result):
    im1 = AstroImage(data=input)
    im1.bin(bin[0], bin[1])
    verifyData(im1.data, result)
Exemple #26
0
def test_initFromProfile(x, y, f, n, re, phi, ar, dat, inputs, results):
    image = AstroImage.initFromProfile(x, y, f, n, re, phi, ar, **inputs)
    verifyParameters(image, results)
    verifyData(image.data, dat)
                                 'in_path':'/path/to/somewhere','cat_path':'/path/to/somewhere/else',
                                 'out_path':'/path/to/somewhere/else','poisson':True,'flat':False,
                                 'dark':False,'cosmic':False,'version':"9.5"}
                            )
                        ]

@pytest.mark.parametrize(("obs","inputs","results"), creation_data)
def test_creation(obs,inputs,results):
    obs = ObservationModule(obs,**inputs)
    verifyParameters(obs,results)

prep_data = [
                (
                    {'input_image':os.path.join(currentdir,"iabf01c5q_flt.fits"),'image_scale':0.3,
                     'image_wcs':True,'image_ext':1},
                    AstroImage.initFromFits(os.path.join(currentdir,"iabf01c5q_flt.fits"),ext=1)
                ),
                (
                    {'input_image':os.path.join(currentdir,"iabf01c5q_flt.fits"),'image_scale':1.2,
                     'image_wcs':False,'image_ext':1},
                    AstroImage.initDataFromFits(os.path.join(currentdir,"iabf01c5q_flt.fits"),ext=1,
                                                             scale=[1.2,1.2])
                )
            ]

@pytest.mark.parametrize(("in_img","astro_img"), prep_data)
def test_prepImage(in_img,astro_img):
    obs = ObservationModule({})
    obs.prepImage(in_img)
    test_AstroImage.verifyImage(obs.images[in_img['input_image']],astro_img)
    test_AstroImage.verifyData(obs.images[in_img['input_image']].data,astro_img.data)
# this is where the user specifies where the raw data is stored
# and some of the subdirectory structure to find the actual .FITS images
#==============================================================================
# This is the location of all pyBDP data (index, calibration images, reduced...)
pyBDP_data = 'C:\\Users\\Jordan\\FITS_data\\PRISM_data\\pyBDP_data'

# This is the location where all pyPol data will be saved
pyPol_data = 'C:\\Users\\Jordan\\FITS_data\\PRISM_data\\pyPol_data'

# This is the location of the pyBDP processed Data
pyBDP_reducedDir = os.path.join(pyBDP_data, 'pyBDP_reduced_images')

# Read in the indexFile data and select the filenames
indexFile = os.path.join(pyPol_data, 'reducedFileIndex.csv')
fileIndex = Table.read(indexFile, format='ascii.csv')
fileList  = fileIndex['Filename']

# Loop through each file and perform its astrometry method
for file in fileList:
    # Read in the file
    tmpImg  = AstroImage(file)

    # Do the astrometry with Atrometry.net
    success = tmpImg.astrometry()
    if success:
        # If the astrometry was solved, then proceed to write the astro
        tmpImg.write()
    else:
        print('Failed to get astrometry for ')
        print(file)