def MatchNSubtract(TargetImg,Template,OutputImage,fitgeometry="general"):
    """ Creates OutputImage =  TargetImg - Template after scaling and matching Template to TargetImg.
    fitgeometry can be set to 'rotate' when the Template is also TIRSPEC data
    Otherwise if Template is 2MASS or other instrument set it as 'general'  """
    
    AlignedImg = os.path.splitext(TargetImg)[0]+"_"+os.path.basename(Template)
    AlignedImg = os.path.splitext(AlignedImg)[0][:115]+'.fits' # Reduce filename length for iraf geopmap
    TransformDBfile = AlignImage(TargetImg,Template,AlignedImg,fitgeometry=fitgeometry)
    
    # Now get the Good sky region coordinates
    SkyCoordsFile = os.path.splitext(TargetImg)[0]+'_BlankSky.coo'
    if not os.path.isfile(SkyCoordsFile) :
        iraf.display(TargetImg,1)
        print ('For taking coordinates of good sky. Press _x_ over blank sky areas.')
        imx=iraf.imexam(Stdout=1)
        with open(SkyCoordsFile,'w') as foo :    #Creating blank sky coords files
            for line in imx :               
                foo.write(line.split()[0] +'  '+line.split()[1]+'\n')

    # Now get the regions in the image whose brightness has to be cancelled by scaling
    FluxCoordsFile = os.path.splitext(TargetImg)[0]+'_FluxRegions.coo'
    if not os.path.isfile(FluxCoordsFile) :
        iraf.display(TargetImg,1)
        print ('Press _x_ over areas you want to minimise the residual flux after subtraction')
        imx=iraf.imexam(Stdout=1)
        with open(FluxCoordsFile,'w') as foo :    #Creating Flux ares which we have to remove in subtraction
            for line in imx :               
                foo.write(line.split()[0] +'  '+line.split()[1]+'\n')

    #Now we first has to remove background from both the images.
    TargetSkySubtractedFile = os.path.splitext(TargetImg)[0]+'_SkyS.fits'
    if not os.path.isfile(TargetSkySubtractedFile):
        skyvalue = SkySubtractImage(TargetImg,TargetSkySubtractedFile,SkyCoordsFile)
    else:
        print('Warning: Using old {0} file'.format(TargetSkySubtractedFile))

    AlignedSkySubtractedFile = os.path.splitext(AlignedImg)[0]+'_SkyS.fits'
    if not os.path.isfile(AlignedSkySubtractedFile):
        skyvalue = SkySubtractImage(AlignedImg,AlignedSkySubtractedFile,SkyCoordsFile)
    else:
        print('Warning: Using old {0} file'.format(AlignedSkySubtractedFile))

    #We shall now extract the totel Flux in each tiles from both the images
    TargetFluxinTiles = ExtractTiles(TargetSkySubtractedFile,FluxCoordsFile,Summeryfunction=np.sum,hsize=7*1.5)
    TemplateFluxinTiles = ExtractTiles(AlignedSkySubtractedFile,FluxCoordsFile,Summeryfunction=np.sum,hsize=7*1.5)
    
    def DiffSquareSum(x):
        return np.sum([(targetF - x*templateF)**2 for targetF,templateF in zip(TargetFluxinTiles,TemplateFluxinTiles)])
    
    res = scipy.optimize.minimize_scalar(DiffSquareSum)
    Scale = res.x
    print('Scaling to match the fluxes is {0}'.format(Scale))
    iraf.imarith(operand1=AlignedSkySubtractedFile,op="*",operand2=Scale,result=os.path.splitext(AlignedSkySubtractedFile)[0]+'M.fits')

    iraf.imarith(operand1=TargetSkySubtractedFile,op="-",operand2=os.path.splitext(AlignedSkySubtractedFile)[0]+'M.fits',result=OutputImage)
Example #2
0
def kait_disp():

    inlis=iraffiles('*_001.sub.fit')
    for image in inlis:
        root=image[:len(image)-12]
        iraf.display("%s_001.fit" % root, 1)
        if os.path.exists('%s.z.can' % root):
            iraf.tvmark(1,'%s.z.can' % root, radii=10)
        if os.path.exists('%s.mp.cor' % root):
            iraf.tvmark(1,'%s.mp.cor' % root, radii=10)
        iraf.display('%s.fit' % root,2)
        iraf.display('%s_001.sub.fit' % root, 3)
        if os.path.exists('%s.z.cans' % root):
            iraf.tvmark(3, '%s.z.cans' % root, radii=10)
        iraf.imexam()
Example #3
0
def kait_disp():

    inlis = iraffiles('*_001.sub.fit')
    for image in inlis:
        root = image[:len(image) - 12]
        iraf.display("%s_001.fit" % root, 1)
        if os.path.exists('%s.z.can' % root):
            iraf.tvmark(1, '%s.z.can' % root, radii=10)
        if os.path.exists('%s.mp.cor' % root):
            iraf.tvmark(1, '%s.mp.cor' % root, radii=10)
        iraf.display('%s.fit' % root, 2)
        iraf.display('%s_001.sub.fit' % root, 3)
        if os.path.exists('%s.z.cans' % root):
            iraf.tvmark(3, '%s.z.cans' % root, radii=10)
        iraf.imexam()
Example #4
0
def getFWHM(fitsFile, cooFile, fwhmType="Moffat"):
	if fwhmType == "Moffat":
		col = -2
	elif fwhmType == "Gaussian":
		col = -3
	else: # direct
		col = -1
	iraf.imexam.setParam("imagecur",cooFile)
	iraf.imexam.setParam("use_display","no")
	results = iraf.imexam(fitsFile, frame=1, Stdout=1, Stdin=cr)
	fwhm = results[3].split()[col]
	if fwhm == "INDEF":
		print "Please select another FWHM type:"
		print "0 Gaussian",results[3].split()[-3]
		print "1 Moffat",results[3].split()[-2]
		print "2 Direct",results[3].split()[-1]
		entry = raw_input("New fwhm:")
		if entry == "":
			col = -3
		elif entry == "1":
			col = -2
		else:
			col = -2
		fwhm = results[3].split()[col]
	xx = results[2].split()[0]
	yy = results[2].split()[1]
	return fwhm, xx, yy
def CreateMatchingXYCoords(TargetImg,Template,MatchingXYcoordsFile):
    """ Creates Xref Yref X Y, a 4 coulumn matched coordinates file MatchingXYcoordsFile."""
    iraf.display(TargetImg,1)
    print('Choose _atleast_ 9 matching stars')
    print('First Select maximum number of stars from this Target image by pressing _a_ on good stars. Press q to finish.')
    imxTarget = iraf.imexam(Stdout=1)
    iraf.display(Template,1)
    print('Press q to read from '+os.path.splitext(Template)[0]+'_TemplXY.coo'+'. OR Select exactly same stars in same order from this Template image by pressing _a_ and then press q to finish.')
    imxTemplate = iraf.imexam(Stdout=1)
    if len(imxTemplate) == 0: # Default to the predefinted coo file
        with open(os.path.splitext(Template)[0]+'_TemplXY.coo') as defaultcoo:
            imxTemplate = [line.rstrip() for line in defaultcoo]

    with open(MatchingXYcoordsFile,'w') as foo :    #Creating XYXY file
        i=2
        while i < len(imxTemplate) :               
            foo.write(imxTarget[i].split()[0] +' '+imxTarget[i].split()[1]+' '+imxTemplate[i].split()[0]+' '+imxTemplate[i].split()[1]+'\n')
            i=i+2
    print("Xref Yref X Y Table created in "+MatchingXYcoordsFile)
Example #6
0
def getFWHMfromFITScoo(fitsFile,cooFileFull):	
	cr=['\n']
	# Only use the first coordinate
	cooFile=cooFileFull+".short"
	os.system(" head -n 1 "+cooFileFull+" > "+cooFile)
	iraf.tv()
	iraf.imexam.setParam("imagecur",cooFile)
	iraf.imexam.setParam("use_display","no")
	results=iraf.imexam(fitsFile,frame=1,Stdout=1,Stdin=cr)
	# Line 0: headers for coords; 1: coords; 2: header for FWHM etc; we want 3
	# Gaussian is -3rd element, Moffat -2, direct -1
	moffatFWHM = results[3].split()[-2]
	return moffatFWHM	
Example #7
0
def source_list(filename):
    print('*****************************')
    print('now selecting the sources')
    iraf.display(filename, 1)
    print(
        'Please do the following:\n1. Press enter here and then click on any source in the DS9 window. \n2. Press (comma) in the middle of source(s) to get FWHM.\n3. Press q to quit. \n '
    )
    imx = iraf.imexam(Stdout=1)
    xval = np.zeros(len(imx))
    yval = np.zeros(len(imx))
    for i in range(1, len(imx)):
        xval[i] = eval(imx[i].split()[0])
        yval[i] = eval(imx[i].split()[1])
    with open('co_ordinates_list.txt', 'w') as f:
        for i in range(1, len(imx)):
            print(xval[i], '\t', yval[i], file=f)

    return (xval, yval)
Example #8
0
def iraf_fwhm():
    print(' We need to get the FWHM using IRAF tasks.')
    print(' Opening DS9 for image display.')
    os.system("ds9 &")
    filename = input('Enter the filename:')
    iraf.display(filename, 1)
    print(
        'Please do the following:\n1. Press enter here and then click on any source in the DS9 window. \n2. Press (comma) in the middle of source(s) to get FWHM.\n3. Press q to quit. \n '
    )
    imx = iraf.imexam(Stdout=1)
    sum1 = 0
    if (imx[1].split()[10] == 'INDEF'):
        fwhm = 8.0
        print('Fwhm:', 'INDEF', 'but taken as 8.0')
    else:
        for i in range(1, len(imx)):
            token = i
            sum1 += eval(imx[i].split()[10])
        fwhm = sum1 / token
        print("Average FWHM in pixels:", fwhm, ' in arc seconds:', 0.53 * fwhm)
    return (fwhm, filename)
Example #9
0
def photometry(filenames):
    """
        Do photometry of the files
        
    """
    for item in range(len(filenames)):
        print('Photometry on file', filenames[item][0])
        iraf.imstat(filenames[item][0])
        iraf.display(filenames[item][0], 1)
        print(
            'Press , (comma) on your target to get the coord and then q, but DONT press r \n'
        )
        imx = iraf.imexam(Stdout=1)
        # If there is an error like "# ^SyntaxError: unexpected EOF while parsing" then make imx[2] instead imx[3] in all places. It is mainly a file reading problem
        Xref = eval(
            imx[1].split()
            [0])  #Using the first star as ref star for cose shift calculation
        Yref = eval(imx[1].split()[1])
        if (imx[1].split()[10] == 'INDEF'):
            fwhm = 4.0
            print('fwhm:', 'INDEF', 'but taken as 4.0')
        else:
            fwhm = eval(imx[1].split()[10])
            print('fwhm:', fwhm)
        print('Reference cord:', Xref, Yref)
        file1 = open('coord_list', 'w')
        file1.write('{0:0.3f}\t{1:0.3f}\n'.format(Xref, Yref))
        file1.close()
        Xref1, Yref1 = np.loadtxt('coord_list')
        print('Xref1, Yref1', Xref1, Yref1)
        do_phot(filenames[item][0],
                'coord_list',
                fwhm=fwhm,
                sky_annulus=10.0,
                width_sky=10.,
                zeropoint=25.)

    print('All photometry is done. Enjoy! Check .phot file')
Example #10
0
def run_imexam(imageFilename, centerCoords, xStart, yStart, xStop, yStop):
	'''
	method calls iraf's imexam method on the area defined by the four
	
	parameter imageFilename - 
		the full path filename of the image on which to invoke imexam
	parameter centerCoords - 
		the coordinates of the initial guess of the center of the galaxy
	parameter xStart, xStop, yStart, yStop - 
		two points on the original image defining an area to run minmax on
		start is the top left, stop is the bottom right
	
	returns - list of string image info in the form 
				[line, col, mag, radius, b_a, PA]
	'''
		
	
	# string representing the part of the image to run minmax and imexam on
	areaStr = ('[' + str(int(xStart)) + ':' + str(int(xStop)) + "," +
					 str(int(yStart)) + ':' + str(int(yStop)) + ']')

	# writes the output of the minmax function of iraf to a file for later use. 
	coordsFilename = "coords"
	
	# this is t prevent parallel processes from overwriting the coords.tmp file
	while os.path.isfile(coordsFilename + ".tmp"):
		coordsFilename = coordsFilename + "x"
	coordsFilename = coordsFilename + ".tmp"
	
	# create coords[x*].tmp for writing max coordinate to pass to imexam
	os.system('touch '+ coordsFilename)
	write_coords = open(coordsFilename, 'w')
	write_coords.write(centerCoords[0] + " " + centerCoords[1])
	write_coords.close()	
	
	# run imexam passing coords.tmp, 
	# data is returned in the last two elements of the return array of strings
	imexam_out = iraf.imexam(imageFilename + areaStr, 
						use_display=0, imagecur=coordsFilename, Stdout=1)[-2:]

	# ['COL	LINE X Y', 
	# 'R MAG FLUX SKY PEAK E PA BETA ENCLOSED MOFFAT DIRECT']
	
	# delete coords.tmp, no longer needed
	os.system('rm ' + coordsFilename) 
	
	xy = imexam_out[0].strip().split()
	data = imexam_out[1].strip().split()
	
	col = float(xy[0])							#this gives x value 
	line = float(xy[1])							#this gives y value
	radius = float(data[0])						#this gives the radius
	mag = float(data[1])						#this gives the magnitude
	
	# these might be indef, if so then set to default values
	try:
		b_a = 1.0 - float(data[5])				# b/a=1-e
	except:
		print("using default values for b/a")
		b_a = 1.0
	
	#this gives the position angle. iraf measures up from x. Galfit down from y
	try:
		PA = float(data[6]) - 90.0				
	except:
		print("using default values for position angle")
		PA = 0.0
	
	return [line, col, mag, radius, b_a, PA]
Example #11
0
print "Position the pointer on the top of a diamond, and press"
print "  --> Keystroke  a  to define star coordinates ..."
print "Then, afterwards ..."
print "  --> Keystroke  q  to obtain the focus offset value ..."
print "Finally, Keystroke 'q' to exit this task .... "

while (1):

    # Delete any existing logfile.
    try:
        os.unlink(logfilenm)
    except OSError:
        pass

    # Start imexam with logfile output
    iraf.imexam(keeplog='yes', logfile=logfilenm, Stdout=1)

    # Read logfile and remove comment lines
    file = open(logfilenm, 'rU').readlines()
    lines = [f for f in file if not f.startswith('#')]

    # If logfile is empty, user has pressed 'q' with marking
    # any stars which means we want to exit task.
    if (len(lines) < 1):
        sys.exit()

    # Get (x,y) values for last two measurements
    (x1, y1, _) = lines[-1].split(None, 2)

    # find other 3 stars and compute focus offset
    x1 = float(x1)
Example #12
0
import sys
import os
sys.path.append(pymodules)
from ds9 import *
from postprocess import writeTalker

#####################
####### MAIN ########
#####################

display = DS9(fifopath)

if (display.ds9_exists(ds9_title) == 0):
    print "Error: No ALFOSC DS9 instance found!"
    writeTalker('postprocess.imexam', "[DEBUG]: No ALFOSC DS9 instance found!")
    sys.exit(1)

#
# Start up IRAF and imexam. This requires a valid login.cl in directory 'irafdir'
#
os.chdir(irafdir)
from pyraf import iraf
os.chdir(workdir)

#iraf.mscred

writeTalker('postprocess.imexam', "[DEBUG]: Starting imexam")

iraf.imexam(keeplog='no')
#iraf.mscexam(keeplog='no')
Example #13
0
    def interactiveImexam(self, image, frame):
        '''
		Can be used to call interactive IRAF task imexam for a given frame. DS9 must be
		running and a right frame should be given.
		'''
        I.imexam(image, frame=frame)
Example #14
0
from glob import glob
import numpy as np
import ds9

# Initialize IRAF with ccdred
iraf.noao.digiphot(Stdout=1)

from configLocal import IMGDIR, OUTPUTDIR, FILTERS

#d = ds9.ds9()


refImage =os.path.join(OUTPUTDIR, "object","V","Nov30098.fits")

#iraf.display(refImage,1) 
imx=iraf.imexam(refImage, frame = 1, Stdout=1, mode="h")

print("imx")
print(imx)

 









Example #15
0
         os.system(
             "echo '#   COL    LINE   COORDINATES      R    MAG    FLUX     SKY    PEAK    E   PA BETA ENCLOSED   MOFFAT DIRECT' >> tmp.imex_output"
         )
         os.system(
             "echo '999.  999. 999. 999.  INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF' >> tmp.imex_output"
         )
     else:
         os.system(
             "echo '999.  999. 999. 999.  INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF' >> tmp.imex_output"
         )
 else:
     try:
         iraf.imexam(input=img,
                     frame=1,
                     logfile='tmp.imex_output',
                     keeplog='yes',
                     imagecur='tmp.one',
                     wcs='logical',
                     use_disp='no')
     except:
         if not os.path.isfile('tmp.imex_output'):
             os.system("echo '# [1] " + str(img) + " - " +
                       str(coordinatelist) +
                       "\n' > tmp.imex_output")
             os.system(
                 "echo '#   COL    LINE   COORDINATES      R    MAG    FLUX     SKY    PEAK    E   PA BETA ENCLOSED   MOFFAT DIRECT' >> tmp.imex_output"
             )
             os.system(
                 "echo '999.  999. 999. 999.  INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF' >> tmp.imex_output"
             )
         else:
def photometry_pipeline(image, diff, path, extent, output_prefix, extension, num_check):
    imageFile = path+image
    diffFile = path+diff
    # take care of some house keeping, checking dao_params.txt exists and removing
    # files generated by previous iraf sessions that could interfere.
    house_keeping(path, imageFile, diffFile)
    
    # load image data
    data, image, hdulist, size, mid_point = load_image_data(imageFile, extent, extension=extension)
    filter = hdulist[extension].header["HIERARCH FPA.FILTERID"].split(".")[0]
    
    # get the seeing in pixels
    seeing_in_pix = hdulist[extension].header["CHIP.SEEING"]
    
    # find sources
    sources, bkg_sigma = find_sources(imageFile, data, seeing_in_pix, threshold=5.)
    
    # get positions of detected sources
    sources, positions, transient_position, transient_index = get_detection_positions(sources, extent, mid_point)
    
    # select quality detections
    quality_detections = apply_ML(data, sources)
    print
    print "[*] Quality detections: "
    for i, index in enumerate(quality_detections):
        print "   [*]",
        print i, index, sources['xcentroid'][index], sources['ycentroid'][index]
    
    # build a dictionary of quality reference stars.
    reference_dict = cross_match_sdss(imageFile, extension, sources, quality_detections, extent)

    # assert there are enough remaining stars to get a sensible measurement
    try:
        assert len(reference_dict.keys()) >= 9+num_check #DEBUGGING change this to something like 9
    except AssertionError:
        print "[!] %d reference stars is not enough for a reliable measurement." % (len(reference_dict.keys()))
        print "[!] Moving onto next image."
        return 0

    # perform PSF photometry with daophot
    zp_estimate, psfimage = doaphot_psf_photometry(path, imageFile, extent, extension)
    
    # calculate scatter in measurents compared to SDSS
    measurement_dict = {}
    for line in open(imageFile+".als.1","r").readlines():
        if "#" in line or "\\" not in line:
            continue
        data = line.rstrip().split()
        measurement_dict[int(data[0])] = {"mag":float(data[3]), "mag_err":float(data[4]), \
            "pos":(float(data[1]), float(data[2]))}

    # calculate sdss refence offset from zero point estimate (default is 25 for photpars)
    median_diff, diff_sig, sorted_rkeys = calculate_zeropoint_offset(reference_dict, measurement_dict, num_check)
    
    # check zero point offset against sdss test stars
    check_zeropoint_offset(reference_dict, measurement_dict, median_diff, diff_sig, num_check, sorted_rkeys)
    
    # calculate the zero point
    print "[+] Zero-point : %.3f +/- %.3f" % (zp_estimate+median_diff, diff_sig)
    zp_output = open("/".join(path.split("/")[:-2])+"/"+output_prefix+"_zero-point.txt", "a+")
    zp_output.write("%s %s %s %s %s\n" % (imageFile, hdulist[0].header["MJD-OBS"], \
                                          filter, zp_estimate+median_diff, diff_sig))
    zp_output.close()
                                          
    # now perform photometry on difference image
    
    # first get location of transient in diff, using intial guesstimate of location in transient_position
    diff_pos_output = open(diffFile+".coo","w")
    diff_pos_output.write("%s %s\n"%(transient_position[0], transient_position[1]))
    diff_pos_output.close()
    imx = iraf.imexam(imageFile, frame=1, use_display=0, defkey="a", imagecur=diffFile+".coo", Stdout=1)
    i = 2
    while i < len(imx):
        transient_position = (eval(imx[i].split()[0]), eval(imx[i].split()[1]))
        i = i+2
    diff_pos_output = open(diffFile+".coo","w")
    diff_pos_output.write("%s %s\n"%(transient_position[0], transient_position[1]))
    diff_pos_output.close()
    
    # perform photometry on difference image
    run_phot(diffFile, diffFile+".coo")
    run_allstar(diffFile,psfimage)
    try:
        for line in open(diffFile+".als.1","r").readlines():
            if "#" in line or "\\" not in line:
                continue
            data = line.rstrip().split()
            diff_mag = float(data[3])
            diff_mag_err = float(data[4])
            diff_pos = (float(data[1]), float(data[2]))
        
        diff_mag = diff_mag+median_diff
        diff_mag_err = np.sqrt(diff_mag_err*diff_mag_err + diff_sig*diff_sig)
        print "[+] Transient magnitude from diff: %.3f +/- %.3f" % (diff_mag, diff_mag_err)
        diff_output = open("/".join(path.split("/")[:-2])+"/"+output_prefix+"_diff_mags.txt","a+")
        diff_output.write("%s %s %s %s %s\n" % (diffFile, hdulist[0].header["MJD-OBS"], filter, diff_mag, diff_mag_err))
        diff_output.close()
    except IOError:
        print "[!] %s not found. No difference image measurements found." % (diffFile+".als.1")
        print "[*] Exiting."
        sys.exit(0)
Example #17
0
def alignimages(im1, im2):
    runsex = 1  #run sextractor to get object catalogs
    auto = 0.
    t = im1.split('.')
    prefixim1 = t[0]
    t = prefixim1.split('pre')
    fieldid = t[0]  #saves cl1018 or whatever as fieldid
    t = im2.split('.')
    prefixim2 = t[0]

    filexy1 = prefixim1 + '-xy.cat'
    filexy2 = prefixim2 + '-xy.cat'
    print "aligning images for ", fieldid
    if runsex > 0:

        runsextractor(im1, auto)
        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy1, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

        print "running sextractor on ", im2
        runsextractor(im2, auto)

        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy2, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

    match = fieldid + '-match'
    dbase = fieldid + '-match-geomap'
    iraf.display(im1, 1, fill='yes')
    iraf.display(im2, 2, fill='yes')
    reffile = fieldid + 'refpoints'  #make these individually.  First line w/3points in gmos image (x1 y1 x2 y2 x3 y3). 2nd line w/same points in ediscs I-band
    flag = raw_input("Should we create a refpoints file (0=no, 1=yes)?\n")
    flag = int(flag)
    if flag > 0.1:
        s = 'cp cl1018refpoints ' + reffile
        os.system(s)
        print "open ", reffile, " in emacs, delete 2 lines"
        print "Enter x1 y1 x2 y2  x3 y3 for ref image (gmos)"
        print "Enter x1 y1 x2 y2  x3 y3 for 2nd image (vlt)"
        iraf.imexam()

    iraf.xyxymatch(filexy2,
                   filexy1,
                   match,
                   tolerance=15,
                   refpoints=reffile,
                   matching='tolerance',
                   interactive='no')
    iraf.imgets(image=im1, param='i_naxis1')
    t = iraf.imgets.value
    im1xmax = (float(t))
    iraf.imgets(image=im1, param='i_naxis2')
    t = iraf.imgets.value
    im1ymax = (float(t))

    iraf.geomap(input=match,
                database=dbase,
                function='polynomial',
                xmin=1,
                xmax=im1xmax,
                ymin=1,
                ymax=im1ymax,
                xxorder=4,
                xyorder=4,
                yyorder=4,
                yxorder=4,
                transform='gmos',
                interactive='yes')
    transformim = 1
    if transformim > 0:
        outim2 = 'g' + im2
        iraf.geotran(im2, output=outim2, database=dbase, transform='gmos')
        iraf.display(im2, 1, fill='yes')
        iraf.display(outim2, 2, fill='yes')
        iraf.display(im1, 3, fill='yes')
Example #18
0
from ds9 import *
from postprocess import writeTalker


#####################
####### MAIN ########
#####################

display = DS9(fifopath)

if (display.ds9_exists(ds9_title) == 0):
  print "Error: No ALFOSC DS9 instance found!"
  writeTalker('postprocess.imexam',"[DEBUG]: No ALFOSC DS9 instance found!")
  sys.exit(1)


#
# Start up IRAF and imexam. This requires a valid login.cl in directory 'irafdir'
#
os.chdir(irafdir)
from pyraf import iraf
os.chdir(workdir)

#iraf.mscred

writeTalker('postprocess.imexam',"[DEBUG]: Starting imexam")

iraf.imexam(keeplog='no')
#iraf.mscexam(keeplog='no')

Example #19
0
def checkmorph(cluster):
    print "Checking morphologies for ", cluster
    infile1 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.funnySpirals.dat'
    in1 = open(infile1, 'r')
    infile2 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.funnySpirals'
    in2 = open(infile2, 'r')
    lines1 = in1.readlines()
    lines2 = in2.readlines()
    Nfunny = len(lines1)
    bothlines1 = zip(lines1, lines2)
    in1.close()
    in2.close()
    infile1 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.noBsteinSpirals.dat'
    in1 = open(infile1, 'r')
    infile2 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.noBsteinSpirals'
    in2 = open(infile2, 'r')
    lines1 = in1.readlines()
    lines2 = in2.readlines()
    nBstein = len(lines1)
    bothlines2 = zip(lines1, lines2)
    in1.close()
    in2.close()
    infile1 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.noMorph.dat'
    in1 = open(infile1, 'r')
    infile2 = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.noMorph'
    in2 = open(infile2, 'r')
    lines1 = in1.readlines()
    lines2 = in2.readlines()
    NnoMorph = len(lines1)
    bothlines3 = zip(lines1, lines2)
    in1.close()
    in2.close()
    newmorphagc = []
    newmorphclass = []
    allfiles = bothlines1 + bothlines2 + bothlines3
    print 'Our classification system: \n 1=Elliptical\n 2=S0 \n 3=Spiral\n 4=Irregular\n 5=Peculiar\n 6=No Galaxy\n 7=Low Surface Brightnes\n'
    i = 0
    for line1, line2 in allfiles:
        line1 = line1.rstrip()
        if i == 0:
            print "Galaxies where our class and Bstein class disagree"
        if i == Nfunny:
            print "Galaxies with no Bstein class"
        if i == (Nfunny + nBstein):
            print "Galaxies that are on our 24um image but not classified"
        iraf.display(line1, contrast=0.01, frame=1)
        print "current classifications: ", line2
        iraf.imexam(line1, frame=1)
        flag = str(
            raw_input(
                'Are you happy with the our classification?  y=yes n=no x=quit '
            ))
        flag = str(flag)
        print 'this is what I think you typed ', flag
        if flag.find('n') > -1:
            repeatflag = 1
            while repeatflag:
                newmorph = str(
                    raw_input(
                        'Enter new morphology. \n 1=Elliptical\n 2=S0 \n 3=Spiral\n 4=Irregular\n 5=Peculiar\n 6=No Galaxy\n 7=Low Surface Brightnes\n'
                    ))
                print 'this is what I think you typed ', newmorph
                flag2 = str(
                    raw_input(
                        'Are you happy with this classification?  y=yes n=no x=quit '
                    ))
                if flag2.find('y') > -1:
                    repeatflag = 0
                if flag2.find('x') > -1:
                    return
            t = line2.split()
            agc = t[0]
            newmorphagc.append(agc)
            newmorphclass.append(newmorph)
        elif flag.find('x') > -1:
            print 'quitting'
            return
        ngal = +1
    in1.close()
    in2.close()
    outfile = '/home/rfinn/research/LocalClusters/MorphologyF2011/' + cluster + '.F2011morph.dat'
    out1 = open(outfile, 'w')
    for i in range(len(newmorphagc)):
        s = str(newmorphagc[i]) + ' ' + str(newmorphclass[i]) + ' \n'
        out1.write(s)
    out1.close()
def photometry_pipeline(image, diff, path, extent, output_prefix, extension,
                        num_check):
    imageFile = path + image
    diffFile = path + diff
    # take care of some house keeping, checking dao_params.txt exists and removing
    # files generated by previous iraf sessions that could interfere.
    house_keeping(path, imageFile, diffFile)

    # load image data
    data, image, hdulist, size, mid_point = load_image_data(
        imageFile, extent, extension=extension)
    filter = hdulist[extension].header["HIERARCH FPA.FILTERID"].split(".")[0]

    # get the seeing in pixels
    seeing_in_pix = hdulist[extension].header["CHIP.SEEING"]

    # find sources
    sources, bkg_sigma = find_sources(imageFile,
                                      data,
                                      seeing_in_pix,
                                      threshold=5.)

    # get positions of detected sources
    sources, positions, transient_position, transient_index = get_detection_positions(
        sources, extent, mid_point)

    # select quality detections
    quality_detections = apply_ML(data, sources)
    print
    print "[*] Quality detections: "
    for i, index in enumerate(quality_detections):
        print "   [*]",
        print i, index, sources['xcentroid'][index], sources['ycentroid'][
            index]

    # build a dictionary of quality reference stars.
    reference_dict = cross_match_sdss(imageFile, extension, sources,
                                      quality_detections, extent)

    # assert there are enough remaining stars to get a sensible measurement
    try:
        assert len(reference_dict.keys(
        )) >= 9 + num_check  #DEBUGGING change this to something like 9
    except AssertionError:
        print "[!] %d reference stars is not enough for a reliable measurement." % (
            len(reference_dict.keys()))
        print "[!] Moving onto next image."
        return 0

    # perform PSF photometry with daophot
    zp_estimate, psfimage = doaphot_psf_photometry(path, imageFile, extent,
                                                   extension)

    # calculate scatter in measurents compared to SDSS
    measurement_dict = {}
    for line in open(imageFile + ".als.1", "r").readlines():
        if "#" in line or "\\" not in line:
            continue
        data = line.rstrip().split()
        measurement_dict[int(data[0])] = {"mag":float(data[3]), "mag_err":float(data[4]), \
            "pos":(float(data[1]), float(data[2]))}

    # calculate sdss refence offset from zero point estimate (default is 25 for photpars)
    median_diff, diff_sig, sorted_rkeys = calculate_zeropoint_offset(
        reference_dict, measurement_dict, num_check)

    # check zero point offset against sdss test stars
    check_zeropoint_offset(reference_dict, measurement_dict, median_diff,
                           diff_sig, num_check, sorted_rkeys)

    # calculate the zero point
    print "[+] Zero-point : %.3f +/- %.3f" % (zp_estimate + median_diff,
                                              diff_sig)
    zp_output = open(
        "/".join(path.split("/")[:-2]) + "/" + output_prefix +
        "_zero-point.txt", "a+")
    zp_output.write("%s %s %s %s %s\n" % (imageFile, hdulist[0].header["MJD-OBS"], \
                                          filter, zp_estimate+median_diff, diff_sig))
    zp_output.close()

    # now perform photometry on difference image

    # first get location of transient in diff, using intial guesstimate of location in transient_position
    diff_pos_output = open(diffFile + ".coo", "w")
    diff_pos_output.write("%s %s\n" %
                          (transient_position[0], transient_position[1]))
    diff_pos_output.close()
    imx = iraf.imexam(imageFile,
                      frame=1,
                      use_display=0,
                      defkey="a",
                      imagecur=diffFile + ".coo",
                      Stdout=1)
    i = 2
    while i < len(imx):
        transient_position = (eval(imx[i].split()[0]), eval(imx[i].split()[1]))
        i = i + 2
    diff_pos_output = open(diffFile + ".coo", "w")
    diff_pos_output.write("%s %s\n" %
                          (transient_position[0], transient_position[1]))
    diff_pos_output.close()

    # perform photometry on difference image
    run_phot(diffFile, diffFile + ".coo")
    run_allstar(diffFile, psfimage)
    try:
        for line in open(diffFile + ".als.1", "r").readlines():
            if "#" in line or "\\" not in line:
                continue
            data = line.rstrip().split()
            diff_mag = float(data[3])
            diff_mag_err = float(data[4])
            diff_pos = (float(data[1]), float(data[2]))

        diff_mag = diff_mag + median_diff
        diff_mag_err = np.sqrt(diff_mag_err * diff_mag_err +
                               diff_sig * diff_sig)
        print "[+] Transient magnitude from diff: %.3f +/- %.3f" % (
            diff_mag, diff_mag_err)
        diff_output = open(
            "/".join(path.split("/")[:-2]) + "/" + output_prefix +
            "_diff_mags.txt", "a+")
        diff_output.write("%s %s %s %s %s\n" %
                          (diffFile, hdulist[0].header["MJD-OBS"], filter,
                           diff_mag, diff_mag_err))
        diff_output.close()
    except IOError:
        print "[!] %s not found. No difference image measurements found." % (
            diffFile + ".als.1")
        print "[*] Exiting."
        sys.exit(0)
Example #21
0
                      '" --filename=imgs.txt --width 600 --height 250 &')

            print(
                'What do you want to do? Enter a = Align ; c = Combine ; s = Weighted average of Exptime ; i = Inspect ; e = Exit and go to Next filter directory'
            )
            choice = raw_input('Enter your Choice (a,c,s,i or e) :')
            if (choice == "e"):
                break  # breaking to move to next filter image directory
            elif (choice == "i"):
                while 1:
                    imgEX = input(
                        'Enter the Sl.No# of Image you want to Examine [Enter a large number not in image list to exit] : '
                    )
                    if imgEX >= len(images): break
                    iraf.display(images[imgEX], 1)
                    iraf.imexam()
            elif (choice == "c"):  # Simply combining
                imgINP = map(
                    int,
                    raw_input(
                        'Enter the Sl.No#s of all Images to combine [ Eg: 1,2,3 ] : '
                    ).split(','))
                i = len(imgINP)
                if i < 2: break  # minimum 2 images to combine are not there..
                for j in imgINP:  #Sanity check
                    if j >= len(images):
                        print('Wrong serial number ' + str(j) +
                              ' given.. exiting the combine task..')
                        break
                inpVAR = ','.join([images[j] for j in imgINP])
                name = raw_input(
Example #22
0
def growth(inimg, outdata, rmin=1, rmax=50, rbin=1, salgor='median', annu=50, dannu=5, skyconst=0.0):

    # check input image
    if not os.access(inimg, os.R_OK):
        print >> sys.stderr, 'cannot read input image (%s)' % inimg
        return 1

    # check output
    if os.access(outdata, os.R_OK):
        print >> sys.stderr, 'operation would overwrite existing data (%s)' % outdata
        return 1
    else:
        fout = open(outdata,'w')
    
    # open ds9 and display image
    d = ds9()
    d.set('zoom to fit')

    # unlearn IRAF commands 
    iraf.unlearn('display')
    iraf.unlearn('rimexam')
    iraf.unlearn('imexam')
    iraf.unlearn('hedit')
    
    # display image
    d.set('regions delete all')
    iraf.display(inimg, 1)

    # imexam
    print '\nSelect Object --> type a , Quit --> type q on the ds9 image'
    print 'Caution: do not type any key except a or q'
    ret = iraf.imexam(inimg, 1, Stdout=1)
    
    # display result
    for j in range(len(ret)):
        print ret[j]
    print '\n'
            
    # parse results
    param1 = ret[len(ret)-2].split()
    param2 = ret[len(ret)-1].split()
    if len(param1) == 4 and len(param2) == 11:
        if isfloat(param1[0]):
            xobj = float(param1[0])
        if isfloat(param1[0]):
            yobj = float(param1[1])
        if isfloat(param2[4]):
            peak = float(param2[4])
        if isfloat(param2[9]):
            fwhm = float(param2[9])
        else :
            print >> sys.stderr, 'failed to pick up object in %s' % inimg
            return 1

    # read data into memory
    im = pyfits.open(inimg)
    im_data = im[0].data
    im.close()
    
    x = np.ones(im_data.shape[0]*im_data.shape[1]).reshape(im_data.shape[0],im_data.shape[1])* np.arange(im_data.shape[1]) + 1
    y = (np.ones(im_data.shape[1]*im_data.shape[0]).reshape(im_data.shape[1],im_data.shape[0]) * np.arange(im_data.shape[0]) + 1).T
    r = np.sqrt((x-xobj)*(x-xobj) + (y-yobj)*(y-yobj))

    # sky estimation
    if salgor == "constant":
        sky = skyconst
    else:
        sky_idx = np.logical_and(r>=annu, r<annu+dannu)
        sky = np.median(im_data[sky_idx])
    
    # growth curve
    rap_arr = []
    pix_arr = []
    flux_arr = []
    rap = rmin
    while rap <= annu + dannu:
        phot_idx = np.where(r<=rap)
        total_pix = len(im_data[phot_idx])
        flux = np.sum(im_data[phot_idx]) - total_pix * sky 
        pix_arr.append(total_pix)
        rap_arr.append(rap)
        flux_arr.append(flux)
        rap = rap + rbin
        
    # update sky level
    if salgor != "constant":
        npix1 = int(annu**2 * math.pi)
        npix2 = int((annu + dannu)**2 * math.pi)
        f_idx = np.logical_and(np.array(pix_arr)>npix1, np.array(pix_arr)<=npix2)
        aa, bb, rr, _, _ = stats.linregress(np.array(pix_arr)[f_idx], np.array(flux_arr)[f_idx])
        sky = sky + aa 

        print 'Sky level = %f [ADU/pix]' % sky

    # growth curve
    rap_arr = []
    pix_arr = []
    flux_arr = []
    rap = rmin
    while rap <= rmax:
        phot_idx = np.where(r<=rap)
        total_pix = len(im_data[phot_idx])
        flux = np.sum(im_data[phot_idx]) - total_pix * sky 
        fout.write('%d %f\n' % (rap, flux))
        pix_arr.append(total_pix)
        rap_arr.append(rap)
        flux_arr.append(flux)
        rap = rap + rbin
    fout.close()
    
    return rap_arr, flux_arr
Example #23
0
import os, shutil, re
from pyraf import iraf
from glob import glob
import numpy as np
import ds9

# Initialize IRAF with ccdred
iraf.noao.digiphot(Stdout=1)

from configLocal import IMGDIR, OUTPUTDIR, FILTERS

#d = ds9.ds9()

refImage = os.path.join(OUTPUTDIR, "object", "V", "Nov30098.fits")

#iraf.display(refImage,1)
imx = iraf.imexam(refImage, frame=1, Stdout=1, mode="h")

print("imx")
print(imx)
Example #24
0
	def interactiveImexam(self, image, frame):
		'''
		Can be used to call interactive IRAF task imexam for a given frame. DS9 must be
		running and a right frame should be given.
		'''
		I.imexam(image, frame=frame)
            imgs_txt.write(str(i)+' '+img+' '+str(Obj)+' '+str(Exptime)+' '+str(Commentx)+' '+str(Comment)+' '+str(Filter)+'\n')
            i=i+1
    #   Now list is ready, continuing with what to do
        imgs_txt.close()

        junk = os.system(DISPLAY_MENU_CMD.format(**{"imgdir":imgdir,"imglist":IMGListFile}))

        print ('What do you want to do? Enter a = Align ; c = Combine ; s = Weighted average of Exptime ; i = Inspect ; e = Exit and go to Next filter directory')
        choice=raw_input('Enter your Choice (a,c,s,i or e) :')
        if ( choice == "e" ) : break   # breaking to move to next filter image directory
        elif (choice == "i") :
            while 1 :
                imgEX=input('Enter the Sl.No# of Image you want to Examine [Enter a large number not in image list to exit] : ')
                if imgEX >= len(images) : break
                iraf.display(images[imgEX],1)
                iraf.imexam()
        elif (choice == "c" ) :  # Simply combining 
            imgINP=map(int, raw_input('Enter the Sl.No#s of all Images to combine [ Eg: 1,2,3 ] : ').split(','))
            i=len(imgINP)
            if i < 2 : break    # minimum 2 images to combine are not there..
            for j in imgINP : #Sanity check
                if j >= len(images) : 
                    print('Wrong serial number '+str(j)+' given.. exiting the combine task..')
                    break
            inpVAR=','.join([images[j] for j in imgINP])
            name=raw_input('Enter the name for combined image without .fits extension : ')
            iraf.imcombine(input=inpVAR, output=name+'.fits',combine="average")

        elif (choice == "s" ) :  # Summing and finally dividing by total EXPTIME 
            imgINP=map(int, raw_input('Enter the Sl.No#s of all Images to average by weighted sum of EXPTIME [ Eg: 1,2,3 ] : ').split(','))
            i=len(imgINP)
Example #26
0
iraf.files(path + 'SCIENCE/al*',Stdout = 'imagelist')

f = open('imagelist')
images = f.readlines()
f.close()
indx = images[0][::-1].index('/') # find where the image name starts 
image0 = images[0][-indx:-1]
imageF = images[-1][-indx:-1]
print image0,imageF

iraf.disp(path + 'SCIENCE/' + imageF,2)
iraf.disp(path + 'SCIENCE/' + image0,1)
print 'Press \'a\' on asteroid in Frame 1, change to Frame 2, press \'a\' on asteroid. q to exit.'
os.system('rm rawcoords')
iraf.imexam(Stdout = 'rawcoords')
rawcoords = numpy.loadtxt('rawcoords')
x0 = rawcoords[0][0] 
y0 = rawcoords[0][1] 
xF = rawcoords[2][0] 
yF = rawcoords[2][1] 

os.system('rm MJD')
iraf.hedit('@imagelist','MJD-OBS','.',Stdout = 'MJD')

MJDfile = open('MJD')
MJDlines = MJDfile.readlines()
MJD = []
for i in MJDlines:
	MJD.append(i.split()[len(i.split())-1])
Example #27
0
    # Now we plot it to show how it looks like in a logarithmic scale.
    # In order to make it easier to visualize something, it is better
    # to do an histogram and set the more appropiate limits to the plot
    plt.ion()
    plt.show()
    plt.imshow(image_data, cmap='gray', norm=LogNorm())
    if not os.path.exists(path + 'central_coordinates.txt'):
        print(
            'Please, look for the coordinates in the images and put here the correspondent value in pixels! (imexam task, press w + A in ds9!) '
        )
        iraf.cd(path)
        print('Moving to the directory... ' + os.getcwd())
        iraf.set(stdimage='imt4096')
        iraf.imexam(file_name + '[1]',
                    frame='1',
                    logfile='central_coordinates.txt',
                    wcs='image')
        iraf.cd(basicPath + 'scripts/')
        print('Moving back to the directory... ' + os.getcwd())

    if not os.path.exists(path + 'example_cutout.fits'):
        true_center = np.genfromtxt(path + 'central_coordinates.txt')
        xpos = true_center[
            0]  #input('What is the position of the nucleus in pixels X?: ')
        ypos = true_center[
            1]  #input('What is the position of the nucleus in pixels Y?: ')
        ra_pos = true_center[2]
        dec_pos = true_center[3]
        true_coords = SkyCoord(ra=ra_pos * u.degree,
                               dec=dec_pos * u.degree,
                               frame='fk5')
Example #28
0
def calcoffset(inlist, inpref='', trace=True, review=False):

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr,int):
        return 1 

    # check output geomap file
    gmp_arr = []
    for i in range(len(inimg_arr)):
        # geomap input file name 
        fname,ext = os.path.splitext(inimg_arr[i])
        gmp_arr.append(fname + '.gmp')

        if os.access(gmp_arr[i], os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing file (%s)' % gmp_arr[i]
            return 1


    # open ds9 
    d = ds9()
    d.set('regions delete all')

    # unlearn IRAF commands
    iraf.reset(stdimage='imt1024')
    iraf.unlearn('display')
    iraf.unlearn('rimexam')
    iraf.unlearn('imexam')
    iraf.unlearn('hedit')
    
    # review rimexam parameters
    if review:
        iraf.epar('rimexam')

    # prefix for temporary file 
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # save imexam parameters into temporary files
    tmp_par = tmp_prefix + '.par'
    iraf.rimexam.saveParList(filename=tmp_par)
    
    # region file for the first image
    tmp0_reg = tmp_prefix+'_0.reg'

    # measure position 
    i = 0
    k = 0
    xarr = []
    yarr = []
    peak_arr = []
    fwhm_arr = []
    gsx0 = 0.0
    gsy0 = 0.0
    nobj = 1
    trace0 = trace
    for i in range(len(inimg_arr)):
        # initialize trace parameter 
        trace = trace0

        # get dithering position from header 
        im = pyfits.open(inimg_arr[i], mode='update')
        try:
            ao_mode = im[0].header['D_MODE']
            ao_loop = im[0].header['D_LOOP']
        except KeyError:
            trace = False

        # read guide star coordinates
        gsx = 0.0
        gsy = 0.0
        if ao_loop.lower() == 'on':
            if ao_mode.lower().find('lgs') == -1:
                try:
                    gsx = float(im[0].header['D_AU1GSX'])
                    gsy = float(im[0].header['D_AU1GSY'])
                except KeyError:
                    trace = False
            else:
                try:
                    gsx = float(im[0].header['D_AU2GSX'])
                    gsy = float(im[0].header['D_AU2GSY'])
                except KeyError:
                    trace = False
        else:
            trace = False

        if k == 0:
            gsx0 = gsx
            gsy0 = gsy

        # load rimexam parameters
        iraf.rimexam.setParList(ParList=tmp_par)

        # display image
        d.set('regions delete all')
        iraf.display(inimg_arr[i], 1)

        # automatic object pickup
        calc_ng = 1
        if k >0 and trace == True:
            xobj = []
            yobj = []
            peak = []
            fwhm = []

            # guess object position 
            tmp_reg = tmp_prefix+'.reg'
            if os.access(tmp_reg, os.R_OK):
                os.remove(tmp_reg)
            freg = open(tmp_reg, 'w')
            for ii in range(nobj):
                xg = xref[ii] + (gsx - gsx0)
                yg = yref[ii] + (gsy - gsy0)            
                freg.write('image; circle %.3f %.3f 10 # color=blue text={%d}\n' % (xg, yg, (ii+1)))
            freg.close()
            d.set('regions load %s' % tmp_reg)

            # check object position guess
            OutOfRange = 0
            for ii in range(nobj):
                xg = xref[ii] + (gsx - gsx0)
                yg = yref[ii] + (gsy - gsy0)
                if xg <= 0 or xg >= 1024 or yg <= 0 or yg >= 1024:
                    OutOfRange = 1
            
            # pickup objects
            if OutOfRange == 0:
                if os.access(tmp_reg, os.R_OK):
                    os.remove(tmp_reg)
                freg = open(tmp_reg, 'w')
                for ii in range(nobj):
                    xg = xref[ii] + (gsx - gsx0)
                    yg = yref[ii] + (gsy - gsy0)            

                    tmp_coo = tmp_prefix+'_coo.dat'
                    if os.access(tmp_coo, os.R_OK):
                        os.remove(tmp_coo)
                    fcoo = open(tmp_coo, 'w')
                    fcoo.write('%.3f %.3f a\n' % (xg, yg))
                    fcoo.close()
                    imexam_ng = 1
                    auto_skip = 0
                    while imexam_ng == 1:
                        try:
                            ret = iraf.imexam(inimg_arr[i],1,imagecu=tmp_coo, use_dis='no', Stdout=1)
                            imexam_ng = 0
                            auto_skip = 0
                        except:
                            print '\nIRAF imexam failed for object No. %d' % (ii+1) 
                            print 'Try again by changing rimexam parameters'
                            print 'Hit return to enter rimexam parameter setting window'
                            print 'Type \'q\' to skip this object.'
                    
                            check = ''
                            while check.lower() != 'q' and check.lower() != 'rimexam':
                                check = raw_input('Hit return or type \'q\':')
                                if check.lower() == '' or check.lower() == 'rimexam':
                                    check = 'rimexam'
                                    print 'Push Save&Quit button to quit from the parameter setting window'
                                    iraf.epar('rimexam')
                                    imexam_ng = 1
                                elif check.lower() == 'q':
                                    check = 'q'
                                    auto_skip = 1
                                    imexam_ng = 0
                                else :
                                    print 'Error: unknown answer (%s)' % (check)
                
                    os.remove(tmp_coo)

                    if auto_skip == 0:
                        # display result
                        for j in range(len(ret)):
                            print ret[j]
                        print '\n'

                        # parse results
                        param1 = ret[len(ret)-2].split()
                        param2 = ret[len(ret)-1].split()
                        if len(param1) == 4 and len(param2) == 11:
                            if isfloat(param1[0]):
                                xobj.append(float(param1[0]))
                            if isfloat(param1[0]):
                                yobj.append(float(param1[1]))
                            if isfloat(param2[4]):
                                peak.append(float(param2[4]))
                            else:
                                peak.append(-9999.0)
                            if isfloat(param2[9]):
                                fwhm.append(float(param2[9]))
                            else:
                                fwhm.append(-9999.0)
                            freg.write('image; circle %.3f %.3f 5 # color=red\n' % (xobj[ii], yobj[ii]))
                
                        else :
                            xobj.append(-9999.0)
                            yobj.append(-9999.0)
                            peak.append(-9999.0)
                            fwhm.append(-9999.0)
                    else:
                        xobj.append(-9999.0)
                        yobj.append(-9999.0)
                        peak.append(-9999.0)
                        fwhm.append(-9999.0)

                freg.close()
                d.set('regions load %s' % tmp_reg)
                os.remove(tmp_reg)

                check = raw_input('Is this position okay? (yes/no/skip)')
                if check.lower() == '' or check.lower() == 'yes':
                    calc_ng = 0

                    # update reference points
                    nskip = 0
                    for ii in range(len(xobj)):
                        if xobj[ii] < 0:
                            nskip += 1
                    if nobj == len(xobj) and nskip == 0:
                        gsx0 = gsx
                        gsy0 = gsy 
                        xref = xobj
                        yref = yobj 
                elif check.lower() == 'skip':
                    calc_ng = 0
                    skip_image = 1
                    xobj = []
                    yobj = []
                    peak = []
                    fwhm = []
                    for ii in range(nobj):
                        xobj.append(-9999.0)
                        yobj.append(-9999.0)
                        peak.append(-9999.0)
                        fwhm.append(-9999.0)
                else :
                    calc_ng = 1
            else:
                print ""
                print "Warning:"
                print "Estimated position is out of range"
                print "Pick up object manually\n"
                calc_ng = 1
                
        # calculate position and fwhm
        if calc_ng == 1:

            skip_image = 0
            imexam_ng = 1
            calc_ok = 0
            while calc_ok != 1:
                # display image
                d.set('regions delete all')
                iraf.display(inimg_arr[i], 1)
                # show position of the objects in the first image
                if k != 0:
                    if not trace:
                        d.set('regions load %s' % tmp0_reg)

                    xobj = []
                    yobj = []
                    peak = []
                    fwhm = []
                    skip_obj = 0
                    n_skip_obj = 0
                    for nn in range(nobj):
                        imexam_ng  = 1
                        skip_obj = 0

                        if trace:
                            xg = xref[nn] + (gsx - gsx0)
                            yg = yref[nn] + (gsy - gsy0)            

                            tmp_reg = tmp_prefix+'.reg'
                            if os.access(tmp_reg, os.R_OK):
                                os.remove(tmp_reg)
                            freg = open(tmp_reg, 'w')
                            freg.write('image; circle %.3f %.3f 20 # color=blue text={%d}\n' % (xg, yg, (nn+1)))
                            freg.close()
                            d.set('regions delete all')
                            d.set('regions load %s' % tmp_reg)

                        while imexam_ng == 1:
                            print '\n'
                            print '##### Pickup object No. %d / %d #####' % (nn+1, nobj)
                            print '\n'
                            print '\nSelect Object --> type a , Quit --> type q on the ds9 image'
                            print 'To skip this image type q on the ds9 image and then this image will not be used'
                            print 'Caution: do not type any key except a or q'
                            try :
                                ret = iraf.imexam(inimg_arr[i],1,Stdout=1)
                                if len(ret) == 0:
                                    skip_obj = 1
                                while len(ret) < 4 and skip_obj == 0:
                                    print '\nSelect Object --> type a , Quit --> type q on the ds9 image'
                                    print 'To skip this image type q on the ds9 image and then this image will not be used'
                                    print 'Caution: do not type any key except a or q'
                                    ret = iraf.imexam(inimg_arr[i],1,Stdout=1)
                                    if len(ret) == 0:
                                        skip_obj = 1
                                imexam_ng = 0
                            except:
                                print '\nIRAF imexam failed' 
                                print 'Try again by changing rimexam parameters'
                                print 'Hit return to enter rimexam parameter setting window'
                                print 'To skip this image type \'q\' and then this image will not be used.'
                            
                                check = ''
                                while check.lower() != 'q' and check.lower() != 'rimexam':
                                    check = raw_input('Hit return or type \'q\':')
                                    if check.lower() == '' or check.lower() == 'rimexam':
                                        check = 'rimexam'
                                        print 'Push Save\&Quit to quit from the parameter setting window'
                                        iraf.epar('rimexam')
                                        imexam_ng = 1
                                    elif check.lower() == 'q':
                                        check = 'q'
                                        skip_obj = 1
                                        imexam_ng = 0
                                    else :
                                        print 'Error: unknown answer (%s)' % (check)
                        
                        if skip_obj == 0:
                            # display result
                            for j in range(len(ret)):
                                print ret[j]
                            print '\n'
            
                            # parse results
                            for ii in range(len(ret)):
                                if not ret[ii].startswith('#') and len(ret[ii].split()) > 2:
                                    param = ret[ii].split()
                                    if len(param) == 4:
                                        if isfloat(param[0]):
                                            xobj_tmp = float(param[0])
                                        if isfloat(param[1]):
                                            yobj_tmp = float(param[1])
                                    elif len(param) == 11:
                                        if isfloat(param[4]):
                                            peak_tmp = float(param[4])
                                        else:
                                            peak_tmp = -9999.0
                                        if isfloat(param[9]):
                                            fwhm_tmp = float(param[9])
                                        else:
                                            fwhm_tmp = -9999.0
                                    else:
                                        print >> sys.stderr, 'failed to pick up object in %s' % inimg
                                        remove_temp_all(tmp_prefix)
                                        return 1
                            xobj.append(xobj_tmp)
                            yobj.append(yobj_tmp)
                            peak.append(peak_tmp)
                            fwhm.append(fwhm_tmp)
                        else:
                            xobj.append(-9999.0)
                            yobj.append(-9999.0)
                            peak.append(-9999.0)
                            fwhm.append(-9999.0)
                            n_skip_obj += 1
                        
                        # check number of skipped objects 
                        if n_skip_obj == nobj:
                            skip_image = 1
                            calc_ok = 1
                        else:
                            skip_image = 0
                else:
                    xobj = []
                    yobj = []
                    peak = []
                    fwhm = []
                    while imexam_ng == 1:
                        print '\nSelect Object --> type a , Quit --> type q on the ds9 image'
                        print 'To skip this image type q on the ds9 image and then this image will not be used'
                        print 'Caution: do not type any key except a or q'
                        try :
                            ret = iraf.imexam(inimg_arr[i],1,Stdout=1)
                            if len(ret) == 0:
                                skip_image = 1
                            while len(ret) < 4 and skip_image == 0:
                                print '\nSelect Object --> type a , Quit --> type q on the ds9 image'
                                print 'To skip this image type q on the ds9 image and then this image will not be used'
                                print 'Caution: do not type any key except a or q'
                                ret = iraf.imexam(inimg_arr[i],1,Stdout=1)
                                if len(ret) == 0:
                                    skip_image = 1
                            imexam_ng = 0
                        except:
                            print '\nIRAF imexam failed' 
                            print 'Try again by changing rimexam parameters'
                            print 'Hit return to enter rimexam parameter setting window'
                            print 'To skip this image type \'q\' and then this image will not be used.'
                            
                            check = ''
                            while check.lower() != 'q' and check.lower() != 'rimexam':
                                check = raw_input('Hit return or type \'q\':')
                                if check.lower() == '' or check.lower() == 'rimexam':
                                    check = 'rimexam'
                                    print 'Push Save\&Quit to quit from the parameter setting window'
                                    iraf.epar('rimexam')
                                    imexam_ng = 1
                                elif check.lower() == 'q':
                                    check = 'q'
                                    skip_image = 1
                                    imexam_ng = 0
                                else :
                                    print 'Error: unknown answer (%s)' % (check)

                    if skip_image == 0:
                        # display result
                        for j in range(len(ret)):
                            print ret[j]
                        print '\n'
            
                        # parse results
                        for ii in range(len(ret)):
                            if not ret[ii].startswith('#') and len(ret[ii].split()) > 2:
                                param = ret[ii].split()
                                if len(param) == 4:
                                    if isfloat(param[0]):
                                        xobj.append(float(param[0]))
                                    if isfloat(param[1]):
                                        yobj.append(float(param[1]))
                                elif len(param) == 11:
                                    if isfloat(param[4]):
                                        peak.append(float(param[4]))
                                    else:
                                        peak.append(-9999.0)
                                    if isfloat(param[9]):
                                        fwhm.append(float(param[9]))
                                    else:
                                        fwhm.append(-9999.0)
                                else:
                                    print >> sys.stderr, 'failed to pick up object in %s' % inimg
                                    remove_temp_all(tmp_prefix)
                                    return 1

                        # check consistency 
                        nobj = len(xobj)
                        if nobj != len(yobj) or nobj != len(peak) or nobj != len(fwhm):
                            print >> sys.stderr, 'Number of the recorded objects is inconsistent'
                            remove_temp_all(tmp_prefix)
                            return 1

                        xref = xobj
                        yref = yobj
                        
                        # save dummy values for the skipped frames at the beginning 
                        if i != 0:
                            for ii in range(i):
                                xarr[ii] = []
                                yarr[ii] = []
                                for jj in range(nobj):
                                    xarr[ii].append(-9999.0)
                                    yarr[ii].append(-9999.0)
                                    peak_arr[ii].append(-9999.0)
                                    fwhm_arr[ii].append(-9999.0)

                        # save position of the objects in the first image
                        if os.access(tmp0_reg, os.R_OK):
                            os.remove(tmp0_reg)
                        freg0 = open(tmp0_reg, 'w')
                        for ii in range(nobj):
                            freg0.write('image; point(%.3f,%.3f) # point=x text={%d}\n' % (xobj[ii], yobj[ii], (ii+1)))
                        freg0.close()
                    else:
                        xobj.append(-9999.0)
                        yobj.append(-9999.0)
                        peak.append(-9999.0)
                        fwhm.append(-9999.0)
                        calc_ok = 1

                if skip_image == 0:
                    # show results on ds9
                    tmp_reg = tmp_prefix+'.reg'
                    if os.access(tmp_reg, os.R_OK):
                        os.remove(tmp_reg)
                    freg = open(tmp_reg, 'w')
                    if len(xobj) > 0:
                        for ii in range(nobj):
                            if xobj[ii] >= 0:
                                freg.write('image; circle %.3f %.3f 5 # color=red text={%d}\n' % (xobj[ii], yobj[ii], (ii+1)))
                    freg.close()
                    d.set('regions load %s' % tmp_reg)
                    os.remove(tmp_reg)

                    print 'Is this position okay? '
                    print '<options>'
                    print ' yes or return : accept this postion'
                    print ' no or n       : measure position again'
                    print ' r or rimexam  : change rimexam parameters'
                    print ' q or quit     : skip this image'
                    check = raw_input('')
                    if check.lower() == '' or check.lower() == 'yes':
                        calc_ok = 1
                        xref = xobj
                        yref = yobj
                        gsx0 = gsx
                        gsy0 = gsy
                        
                    elif check.lower() == 'r' or check.lower() == 'rimexam':
                        print 'Push Save\&Quit to quit from the parameter setting window\n'
                        iraf.epar('rimexam')
                        calc_ok = 0
                        imexam_ng = 1
                    elif check.lower() == 'q' or check.lower() == 'quit':
                        print 'Skip image (%s)' % inimg_arr[i]
                        xobj = []
                        yobj = []
                        peak = []
                        fwhm = []
                        if k == 0:
                            xobj.append(-9999.0)
                            yobj.append(-9999.0)
                            peak.append(-9999.0)
                            fwhm.append(-9999.0)
                        else:
                            for ii in range(nobj):
                                xobj.append(-9999.0)
                                yobj.append(-9999.0)
                                peak.append(-9999.0)
                                fwhm.append(-9999.0)
                        calc_ok = 1
                    else : 
                        imexam_ng = 1
                        calc_ok = 0

        # save position into array 
        xarr.append(xobj)
        yarr.append(yobj)
        peak_arr.append(peak)
        fwhm_arr.append(fwhm)

        # close image handler 
        im.close()

        # increment counter
        if skip_image == 0:
            k += 1
        
    # remove all temporary files
    remove_temp_all(tmp_prefix)
    
    # save measured coordinates into geomap input file and fits header 
    for i in range(len(inimg_arr)):

        # open image handler 
        im = pyfits.open(inimg_arr[i], mode='update')

        # open file handler 
        fgmp = open(gmp_arr[i], 'w')

        for j in range(nobj):
            # record imexam results into header
            key = 'xc%d' % (j+1)
            im[0].header.update(key,xarr[i][j])
            key = 'yc%d' % (j+1)
            im[0].header.update(key,yarr[i][j])
            key = 'peak%d' % (j+1)
            im[0].header.update(key,peak_arr[i][j])
            key = 'fwhm%d' % (j+1)
            im[0].header.update(key,fwhm_arr[i][j])

            if xarr[i][j] >= 0:
                fgmp.write('%.3f %.3f %.3f %.3f\n' % (xref[j],yref[j],xarr[i][j],yarr[i][j]))
            else:
                fgmp.write('#%.3f %.3f %.3f %.3f\n' % (xref[j],yref[j],xarr[i][j],yarr[i][j]))

        # close image handler
        im.close()

        # close file handler 
        fgmp.close()

    return 0
Example #29
0
cwd = os.getcwd()
os.chdir(os.getenv("HOME"))
from pyraf import iraf
os.chdir(cwd)

f = open('aligned_list')
images = f.readlines()
f.close()

print 'Press \'a\' on the asteroid in all frames. Press \'q\' to load the next frame.'
os.system('rm astrcoords')
f = open('astrcoords','a')

for i in range(0,len(images)):

	iraf.disp('../SCIENCE/' + images[i][0:-1],1)
	os.system('rm tmpcoords')
	iraf.imexam(Stdout = 'tmpcoords')
	g = numpy.loadtxt('tmpcoords')
	f.write('%d %d\n' % (g[0][0],g[0][1]))
#if i != len(images)-1:
#	iraf.disp(images[-1][0:-1],1)
#	os.system('rm tmpcoords')
#	iraf.imexam(Stdout = 'tmpcoords')
#	g = numpy.loadtxt('tmpcoords')
#	f.write('%d %d\n' % (round(x0-g[0][0],0),round(y0-g[0][1],0)))
f.close()
os.system('rm tmpcoords')

Example #30
0
def fwhm_computation(img, tmpfiltercoo, stars, system, interactive):
    from snoopy2 import src
    import snoopy2
    import string, os
    from numpy import compress
    from numpy import array
    from numpy import average
    from numpy import mean
    from numpy import median
    from numpy import std
    from pyraf import iraf
    from numpy import log10
    _telescope = src.telescope(img)
    _header = src.read_parameter(_telescope, system)
    _xdimen = src.xdimen(img, _telescope)
    _ydimen = src.ydimen(img, _telescope)
    _filter = src.filter(img, _header, _telescope)
    try:
        filter = src.filtername(_telescope, _filter, system)
    except:
        filter = _filter
    _exptime = src.exptime(img, _header, _telescope)

    iraf.delete("tmp.imex_output", verify='no')
    stars_pos = []
    alllines = []

    ff = open(tmpfiltercoo, 'r')
    for j in range(0, 3):
        exl = ff.readline()
    for j in range(len(stars)):
        alllines.append(ff.readline())
        xx = string.split(alllines[-1])[0:2]
        os.system('echo ' + xx[0] + ' ' + xx[1] + '> tmp.two')
        pp = iraf.imexam(input=img,
                         frame=1,
                         logfile='',
                         keeplog='no',
                         imagecur='tmp.two',
                         defkey='m',
                         wcs='logical',
                         use_disp='no',
                         Stdout=1)
        if not 1. <= float(xx[0]) <= float(_xdimen) or not 1. <= float(
                xx[1]) <= float(_ydimen):
            stars_pos.append(0)
        elif string.split(pp[1])[-1] == string.split(pp[1])[-2]:
            stars_pos.append(0)
        else:
            stars_pos.append(1)
    ff.close()
    ff = open('tmp.' + img + 'good.coo', 'w')
    for i in range(len(stars_pos)):
        if stars_pos[i]:
            ff.write(alllines[i])
    ff.close()
    for i in range(len(alllines)):
        if stars_pos[i]:
            ff = open('tmp.one', 'w')
            xx = string.split(alllines[i])[0:2]
            ff.write(xx[0] + ' ' + xx[1] + '  a')
            ff.close()
            try:
                iraf.imexam(input=img,
                            frame=1,
                            logfile='tmp.imex_output',
                            keeplog='yes',
                            imagecur='tmp.one',
                            wcs='logical',
                            use_disp='no')
            except:
                if not os.path.isfile('tmp.imex_output'):
                    os.system("echo '# [1] " + str(img) + " - " +
                              str(coordinatelist) + "' > tmp.imex_output")
                    os.system(
                        "echo '#   COL    LINE   COORDINATES      R    MAG    FLUX     SKY    PEAK    E   PA BETA ENCLOSED   MOFFAT DIRECT' >> tmp.imex_output"
                    )
                    os.system(
                        "echo '999.  999. 999. 999.  INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF' >> tmp.imex_output"
                    )
                else:
                    os.system(
                        "echo '999.  999. 999. 999.  INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF INDEF' >> tmp.imex_output"
                    )

    _fwhm0 = iraf.fields('tmp.imex_output', '15', Stdout=1)
    _mag = iraf.fields('tmp.imex_output', '6', Stdout=1)
    _fwhm, _magime = [], []
    j = 0
    for i in range(len(stars)):
        if stars_pos[i]:
            try:
                _magime.append(float(_mag[j]) + 2.5 * log10(_exptime))
            except:
                _magime.append(float(9999))
            try:
                _fwhm.append(float(_fwhm0[j]))
            except:
                _fwhm.append(float(9999))
            j = j + 1
        else:
            _magime.append(float(9999))
            _fwhm.append(float(9999))

    fwhm_ave = compress((array(_fwhm) < 999), _fwhm)

    fwhm_ave1 = compress(
        (average(fwhm_ave) - 2 * std(fwhm_ave) < array(fwhm_ave)) &
        (array(fwhm_ave) < average(fwhm_ave) + std(fwhm_ave) * 2),
        array(fwhm_ave))
    _fwhm_ave = mean(
        compress((average(fwhm_ave1) - 2 * std(fwhm_ave1) < array(fwhm_ave1)) &
                 (array(fwhm_ave1) < average(fwhm_ave1) + std(fwhm_ave1) * 2),
                 array(fwhm_ave1)))

    fwhm_ave22 = compress(
        (average(fwhm_ave) - 2 * std(fwhm_ave) < array(fwhm_ave)) &
        (array(fwhm_ave) < average(fwhm_ave) + std(fwhm_ave) * 2),
        array(fwhm_ave))
    _fwhm_ave2 = median(
        compress(
            (average(fwhm_ave22) - 2 * std(fwhm_ave22) < array(fwhm_ave22)) &
            (array(fwhm_ave22) < average(fwhm_ave22) + std(fwhm_ave22) * 2),
            array(fwhm_ave22)))

    checkfwhm = 'yes'
    while checkfwhm == 'yes':
        print '################ FWHM(median) = ' + str(_fwhm_ave2) + '  '
        print '################ FWHM(mean) = ' + str(_fwhm_ave) + '  '
        if interactive:
            fwhm_ave = raw_input('################ FWHM = [' +
                                 str(_fwhm_ave2) + '] ? ')
        else:
            fwhm_ave = str(_fwhm_ave2)
        try:
            if not fwhm_ave: fwhm_ave = _fwhm_ave2
            else: fwhm_ave = float(fwhm_ave)
            checkfwhm = 'no'
        except:
            print 'WARNING: FWHM not good !!!!'
            checkfwhm = 'yes'

    #fwhm[filter]=fwhm_ave
    iraf.hedit(img, 'qubafwhm', fwhm_ave, add='yes', update='yes', verify='no')
    return fwhm_ave, _magime, stars_pos, _fwhm