def main(templatename, sourcename, outname, band="None", cube="None"):

	print "##################################"
        print "HOTPANTS SINGLE RUN PYTHON WRAPPER"
        print "##################################"
        print ""
        
	if band == "None":
		print "Please specify band"
		sys.exit(0)

	imTemplate = imFits()
	imTemplate._Name = templatename
	imTemplate._Band = band

        imSource = imFits()
	imSource._Name = sourcename
	imSource._Band = band

#(self, templateFits, outname, _tu="None", _tuk="None", _tl="None", _tg="None", _iu="None", _iuk="None", _il="None", _ig="None", _ir="None", _nsx="None", _nsy="None", _ng="None", band="r", verbose=False)

	if cube == "None":
		_nsx = "None"
		_nsy = "None"
		_ng = "None"
	else:
		_nsx = cube[1]
		_nsy = cube[2]
		_ng = cube[3:]
	print cube

	imOut = imSource.subtractTemplate(imTemplate, outname, band=band, _nsx=_nsx, _nsy=_nsy, _ng=_ng)

	return imOut
def make_object(imageName, ra, dec, magnitude):

	# Inform the user of their choices
	print "User choices:"
	print "------------"
	print "Input image: %s" % (imageName)
	print "RA: %s" % (ra)
	print "Dec.: %s" % (dec)
	print "Magnitude: %s" % (magnitude)

	# Load instance of image object
	scienceImage = imFits()
	scienceImage._Name = imageName

	# Generate an object instance and find out the pixel co-ordinates
	generatedObject = imObject()
	generatedObject._parentimage = scienceImage._Name
	generatedObject._ra = float(ra)
	generatedObject._dec = float(dec)
	generatedObject._appMag = float(magnitude)
	generatedObject.findLogicalPosition(write=False)
	generatedObject.printInfo()

	# Write the user input to file
	cooFile = open("mkobject.coo", "w")
	cooFile.write("%f %f %f" % (generatedObject._pixelx, generatedObject._pixely, generatedObject._appMag))
	cooFile.close()

	# Make the object
	scienceImage.makeObject()
Example #3
0
def main(templatename, sourcename, outname, wcsregister=False):

        print "WCSREMAP Python Wrapper"
        print "-----------------------"
        print ""
        
	imTemplate = imFits()
        imSource = imFits()

        imTemplate._Name = templatename
        imSource._Name = sourcename
	
	print "### WARNING ###"
	print "PYREMAP IS CHANGING THE TEMPLATE IMAGE TO MATCH THE WCS CO-ORDINATES OF THE SCIENCE IMAGE"
	print "i.e. IT IS NOT CHANGING THE SCIENCE IMAGE"
	print "###         ###"

        imOut = imTemplate.reMap(remapFits=imSource, outname=outname, wcsregister=wcsregister)

	return imOut
Example #4
0
def main(inputfits, outputfits, region, verbose):

	print "-------------"
	print "PYRAF CUTTING"
	print "-------------"

	inFits = imFits()
	inFits._Name = inputfits
	inFits._logger['Info'] = []

	copyFits = inFits.trimMyself(outname=outputfits, region=region, verbose=verbose)

	return copyFits
def big_pants(directory, allpars=False, verbose=False, band=False, OB=False):

	print "#########################"
	print "MASS HOTPANTS SUBTRACTION"
	print "#########################"


	# Make folder list
	print "Given directory: %s" % directory
	if OB:
		print "Given OB: %s" % OB
		OBList = [OB]
	else:
		OBList = glob.glob("%s/OB*" % directory)
	Missing_list = []
	Skipped_list = []
	if not band:
		bandList = ["g", "r", "i", "z", "J", "H", "K"]
	else:
		bandList = band

	
	# Run over OBs
	for OB in OBList:
		print "#####################"
		for band in bandList:

			if allpars:
				gridcube = parameters(verbose=False)
			else:
				print "Using default input grid parameter."
				gridcube = [0, 20, 20, 3, 6, 0.59999999999999998, 4, 1.5, 2, 3]

			for cube in gridcube:

				print "OB: %s" % (OB)
				print "band: %s" % (band)
				print "cube id: %d" % (cube[0])
				print "cube input: %s" % (cube[1:])
	
				bandpath = "%s/%s" % (OB,band)

				templatefile = "%s/template_cut_%s.fits" % (bandpath, band)
				inputfile = "%s/remcut_%s.fits" % (bandpath, band)
				subpath = "%s/sub" % (bandpath)
				cubeoutpath = "%s/cube_%s" % (subpath,cube[0])
				outfile = "%s/diff_%s.fits" % (cubeoutpath, band)

				flag = False
				try:
					os.mkdir(subpath)
					print "Master subtraction folder made: %s" % (subpath)
				except:
					print "Master subtraction folder already exists (%s)" % (subpath)
				try:
					os.mkdir(cubeoutpath)
					print "Cube folder sucessfully made."
				except:
					print "Cube folder already exists."

				#if not os.path.exists(templatefile):
				#	print "Template in band: %s, does not exist, skipping." % band
				#	flag = True
				#	Missing_list.append([OB, band])
				if not os.path.exists(bandpath):
					print "This band has no folder, skipping."
					Missing_list.append([OB, band])
					flag = True
				if not os.path.exists(inputfile):
					print "This has no input fits image, skipping."
	                                Missing_list.append([OB, band])
					flag = True
				if os.path.exists(outfile):
					print "There is already an output file, skipping (delete it if you wish to be done again)"
					Skipped_list.append([OB, band])
					flag = True

				if not flag:
		
					imTemplate = imFits()
					imTemplate._Name = templatefile
					imTemplate._Band = band
				
					imSource = imFits()
					imSource._Name = inputfile
					imSource._Band = band

					try:
						imSubtraction = pypants(imTemplate._Name, imSource._Name, outfile, band=imSource._Band, cube=cube)
						if not os.path.exists(outfile):
							print "Hotpants failed"
						else:
							print "Hotpants worked"
					except:
						print "Hotpants failed"
			
	                                cubelog = "%s/cube.log" % (cubeoutpath)
	                                cubefile = open(cubelog, "w")
        	                        cubefile.write("%s" % (cube))
                	                cubefile.close()	

				print "########################"
				print "\n\n"

		print "Total Missing: %d/%d" % (len(Missing_list), len(OBList)*len(bandList))
		if len(Missing_list)>0:
			print ""
			print "Missing List:"
			print "-------------"
			for missing in Missing_list:
				print "OB: %s, band: %s" % (missing[0], missing[1])
			print ""
		print "Total Skipped: %d/%d" % (len(Skipped_list), len(OBList)*len(bandList))
def main(directory, bandList, objectOfInt, OBList=None):

	# Script outline
	#
	#	 A. 1. Find all the OB directories of interest
	#	    2. Find all the bands of interest and cube subtractions
	#
	#	 B. 1. In a given band, load an image to be used as the reference and find the Nearest Neighbour (NN) in WCS
	#	    2. Run through each cube image and calculate the statistics of the NN
	#	    3. Once the best image is found, copy it into a master image	


	# A.

        # Make folder list
        print "Given directory: %s" % directory
        if not OBList:
		OBList = glob.glob("%s/OB*" % directory)
        Missing_list = []
        Skipped_list = []
#        bandList = ["g", "r", "i", "z", "J", "H", "K"]

	# Specify the object of interest, e.g. GRB 110918A
        objectOfInterest = imObject()
        
        if objectOfInt:
		print "Using object file, %s" % objectOfInt
                ooifile = open(objectOfInt, "r")
                ooilines = ooifile.readlines()[0].replace("\n", "").split(" ")
                ooifile.close()

                #info = ooilines.split(" ")
                objectOfInterest._Name = ooilines[0]
                objectOfInterest._ra = float(ooilines[1])
                objectOfInterest._dec = float(ooilines[2])
                
        else:
                objectOfInterest._Name = "GRB 110918A"
                objectOfInterest._ra = float(32.53894)
                objectOfInterest._dec = float(-27.10548)

	# Run over bands
	for band in bandList:
		# We want to pick only 1 reference star for each band, so that it is consistent
		# Select this by a "chosen" flag
		chosenflag = False
		# Run over OBs
		for OB in OBList:

			# B. 
			# Specify an object image from the first OB
			print "Loading image."
			ReferenceImage = imFits()
			ReferenceImage._Name = "%s/%s/remcut_%s.fits" % (OB, band, band)

                        # get FWHM and STDEV
                        print "Acquiring background standard deviation and median FWHM."
			ReferenceImage.getBackgroundSTDEV()
			
			fwhmfile = "%s/%s/fwhm.dat" % (OB, band)
			try:
				fwhmf = open(fwhmfile, "r")
				fwhm = float(fwhmf.readlines()[0].replace("\n",""))
				print "FWHM taken from file"
				ReferenceImage._MEDFWHM = fwhm
			except:
				print "Calculating FWHM using SEXtractor"
				ReferenceImage.getMyMedianFWHM()
				

			if not chosenflag:
	                        # Make a temporary object of interest
	                        tempInterest = imObject()
	                        tempInterest.copyObjectProperties(objectOfInterest)

	                        # Make a temporary object of interest
	                        tempInterest = imObject()
	                        tempInterest.copyObjectProperties(objectOfInterest)
				tempInterest._parentimage = ReferenceImage._Name
				tempInterest.findLogicalPosition(write=True)
				#tempInterest.printInfo()
			
				# get FWHM and STDEV
				print "Acquiring background standard deviation and median FWHM."

				# Find the NearestNeighbour
				print "Acquiring stars."
				ReferenceImage.findStars()
				#ReferenceImage.printInfo()
			
				print "Acquiring nearest neighbour to object of interest"
				ClosestNeighbour = ReferenceImage.findNearestNeighbour(tempInterest, rcirc=100, write=True)
				#ClosestNeighbour[0].printInfo()
				ClosestNeighbour[0]._Name = "Closest star"
				ClosestNeighbour[0]._parentimage = ReferenceImage._Name
				# Get the WCS co-ordinates for future image usage
				ClosestNeighbour[0].findWorldPosition(write=True)
				print "Found:"
				ClosestNeighbour[0].printInfo()

				# set chosen flag
				chosenflag = True

			cubeList = glob.glob("%s/%s/sub/cube*" % (OB, band))
			
			# Run over each cube
                        # measure the STDDEV and MEAN of each image utilising IRAF.IMSTAT
                        #
                        #       Keep it in the array [listOfCubeStats]
			ListOfCubeStats = []
		

			for cube in cubeList:

				# load image
				print "Cube: %s" % (cube)
				cubeSubtractedImage = imFits()
				cubeSubtractedImage._Name = "%s/diff_%s.fits" % (cube, band)

				# Get sky STDDEV and FWHM (seeing)
				cubeSubtractedImage.skySTDEV = ReferenceImage._skySTDEV
				cubeSubtractedImage._MEDFWHM = ReferenceImage._MEDFWHM
	
				# Look at a box around object of interest
				tempClosestNeighbour = imObject()
				tempClosestNeighbour.copyObjectProperties(ClosestNeighbour[0])
				tempClosestNeighbour._parentimage = cubeSubtractedImage._Name
				tempClosestNeighbour.findLogicalPosition(write=True)
				cubeSubtractedImage.getObjectStatistics(tempClosestNeighbour)
				#tempClosestNeighbour.printInfo()
				
				# Collect the cube output
				ListOfCubeStats.append(tempClosestNeighbour)
				
			# Find the best cube
			# 	1. Set fake STDDEV and MEAN
			#	2. loop through to find the best
			bestSTDEV, bestMEAN, bestOBJECT = 10000000, 10000000, "None"
			for CubeStats in ListOfCubeStats:

				# Test print
				print CubeStats._STDDEV, CubeStats._MEAN

				if abs(CubeStats._STDDEV) < bestSTDEV: #and abs(CubeStats._MEAN) < bestMEAN:
					bestSTDEV = CubeStats._STDDEV
					bestMEAN = CubeStats._MEAN
					bestOBJECT = CubeStats

			#print "The best object is:"
			#bestOBJECT.printInfo()

			# Copy it to a "best" directory
			bestoutput = "%s/%s/sub/best/" % (OB, band)
			if glob.glob(bestoutput+"*"):
				print "File exists"
				shutil.rmtree(bestoutput)
				print "Deleted"

			shutil.copytree(bestOBJECT._parentimage.replace("diff_%s.fits" % band, ""), bestoutput)
			#print glob.glob(bestoutput+"*")			
			print "Finished OB,band: %s, %s" % (OB,band)
			print "\n\n\n"
def main(directory, band, objectfile, fap=False, fdan=False, fan=False, cube="best"):

	# Open and parse objectfile
	objectOfInterest = imObject()
	if objectfile:
		objintfile = open(objectfile, "r")

		obji = objintfile.readlines()[0].replace("\n", "").split(" ")
		objectOfInterest._Name = obji[0]
		objectOfInterest._ra = float(obji[1])
		objectOfInterest._dec = float(obji[2])
		objintfile.close()
	else:
		print __doc__
		sys.exit(0)

	#objectOfFakeness = imObject()
	#objectOfFakeness._Name =
	#objectOfFakeness._ra =
	#objectOfFakeness._dec =

        # Script outline
        # 1. Initial image
        #
        #       Acquire the FWHM and background STDDEV from the original uncut image
        #
        # 2. Normal Image
        #
        #       Load the image and take photometry at the given position
        #
        # 3. Noise Image
        #
        #       Do the same as on the normal image
        #
        # 4. Save the output in a file

	# Normal image
	InitialImage = imFits()
	InitialImage._Band = band
        InitialImage._Name = "%s/%s/remcut_%s.fits" % (directory, InitialImage._Band, InitialImage._Band)
	InitialImage.getBackgroundSTDEV()

        fwhmfile = "%s/%s/fwhm.dat" % (directory, band)
        try:
		fwhmf = open(fwhmfile, "r")
		fwhm = float(fwhmf.readlines()[0].replace("\n",""))
		print "FWHM taken from file"
		InitialImage._MEDFWHM = fwhm
	except:
		print "FWHM calculated by SExtractor"
		InitialImage.getMyMedianFWHM()

	# SubtractedImage
	SubtractedImage = imFits()
	SubtractedImage._Band = band
	SubtractedImage._Name = "%s/%s/sub/%s/diff_%s.fits" % (directory, SubtractedImage._Band, cube, SubtractedImage._Band)
	SubtractedImage._skySTDEV = InitialImage._skySTDEV
	SubtractedImage._MEDFWHM = InitialImage._MEDFWHM

	SubObject = imObject()
	SubObject._parentimage = SubtractedImage._Name
	SubObject.copyObjectProperties(objectOfInterest)
	SubObject.findLogicalPosition(write=True)

	# Photometry
	#
	# Normal image
	#
	fwhm = InitialImage._MEDFWHM
	if fap and fdan and fan:
		print "User defined apertures"
		print ""
		print "fap fdan fan"
		print "%f %f %f" % (fap, fdan, fan)
		fap = fap
		fdan = fdan
		fan = fan
	else:
		fap = fwhm
		fdan = 2*fwhm
		fan = 2.2*fwhm
		
	SubObject.setAps(fap=fap, fdan=fdan, fan=fan, scale=1)
	SubObject.getAps(write=True)
	SubtractedImage.runApperturePhotometryOnObject(SubObject)
	#
	# Noise image 
	#
	NoiseImage = imFits()
	NoiseImage._Band = band
	NoiseImage._Name = "%s/%s/sub/%s/diff_%s_noise.fits" % (directory, NoiseImage._Band, cube, NoiseImage._Band)

	# Noise Squared
	NoiseSquaredImage = imFits()
	NoiseSquaredImage._Band = band
	NoiseSquaredImage._Name = NoiseImage.squareMyself()
	#NoiseSquaredImage._skySTDEV = InitialImage._skySTDEV
	NoiseSquaredImage.getBackgroundSTDEV()
	NoiseSquaredImage._MEDFWHM = InitialImage._MEDFWHM

	NoiseObject = imObject()
	NoiseObject._parentimage = NoiseSquaredImage._Name
	NoiseObject.copyObjectProperties(objectOfInterest)
	NoiseObject.setAps(fap=fap, fdan=fdan, fan=fan, scale=1)
	NoiseObject.findLogicalPosition(write=True)
	
	NoiseSquaredImage.runApperturePhotometryOnObject(NoiseObject)
	subError = NoiseSquaredImage.calculateError(SubObject, NoiseObject)

	InitialImage.loadHeader()
	#timeError = InitialImage.getHeader("EXPTIME")
	
	# Set the subtracted error from the noise image and not the error from the subtracted image
	SubObject._appMagErr = subError

	SubObject.printInfo()
	
	return SubObject._midMJD, SubObject._midMJDErr, SubObject._appMag, SubObject._appMagErr, NoiseObject._appMag, SubObject._appMagErr, SubObject, NoiseObject
def main(directory, bandList):

	# Script outline
	#
	#	 A. 1. Find all the OB directories of interest
	#	    2. Find all the bands of interest and cube subtractions
	#
	#	 B. 1. In a given band, load an image to be used as the reference and find the Nearest Neighbour (NN) in WCS
	#	    2. Run through each cube image and calculate the statistics of the NN
	#	    3. Once the best image is found, copy it into a master image	


	# A.

        # Make folder list
        print "Given directory: %s" % directory
        OBList = glob.glob("%s/OB*" % directory)
        Missing_list = []
        Skipped_list = []
#        bandList = ["g", "r", "i", "z", "J", "H", "K"]

	# Specify the object of interest, e.g. GRB 110918A
	objectOfInterest = imObject()
	objectOfInterest._Name = "GRB 110918A"
	objectOfInterest._ra = float(32.53894)
	objectOfInterest._dec = float(-27.10548)

	# Run over bands
	for band in bandList:
		# We want to pick only 1 reference star for each band, so that it is consistent
		# Select this by a "chosen" flag
		chosenflag = False
		# Run over OBs
		for OB in OBList:

			# B. 
			# Specify an object image from the first OB
			print "Loading image."
			ReferenceImage = imFits()
			ReferenceImage._Name = "%s/%s/remcut_%s.fits" % (OB, band, band)

                        # get FWHM and STDEV
                        print "Acquiring background standard deviation and median FWHM."
			ReferenceImage.getBackgroundSTDEV()
			ReferenceImage.getMyMedianFWHM()

			if not chosenflag:
	                        # Make a temporary object of interest
	                        tempInterest = imObject()
	                        tempInterest.copyObjectProperties(objectOfInterest)

	                        # Make a temporary object of interest
	                        tempInterest = imObject()
	                        tempInterest.copyObjectProperties(objectOfInterest)
				tempInterest._parentimage = ReferenceImage._Name
				tempInterest.findLogicalPosition(write=True)
				#tempInterest.printInfo()
			
				# get FWHM and STDEV
				print "Acquiring background standard deviation and median FWHM."

				# Find the NearestNeighbour
				print "Acquiring stars."
				ReferenceImage.findStars()
				#ReferenceImage.printInfo()
			
				print "Acquiring nearest neighbour to object of interest"
				ClosestNeighbour = ReferenceImage.findNearestNeighbour(tempInterest, rcirc=50, write=True)
				#ClosestNeighbour[0].printInfo()
				ClosestNeighbour[0]._Name = "Closest star"
				ClosestNeighbour[0]._parentimage = ReferenceImage._Name
				# Get the WCS co-ordinates for future image usage
				ClosestNeighbour[0].findWorldPosition(write=True)
				print "Found:"
				ClosestNeighbour[0].printInfo()

				# set chosen flag
				chosenflag = True

			cubeList = glob.glob("%s/%s/sub/cube*" % (OB, band))
			
			# Run over each cube
                        # measure the STDDEV and MEAN of each image utilising IRAF.IMSTAT
                        #
                        #       Keep it in the array [listOfCubeStats]
			ListOfCubeStats = []
		

			for cube in cubeList:

				# load image
				print "Cube: %s" % (cube)
				cubeSubtractedImage = imFits()
				cubeSubtractedImage._Name = "%s/diff_%s.fits" % (cube, band)

				# Get sky STDDEV and FWHM (seeing)
				cubeSubtractedImage.skySTDEV = ReferenceImage._skySTDEV
				cubeSubtractedImage._MEDFWHM = ReferenceImage._MEDFWHM
	
				# Look at a box around object of interest
				tempClosestNeighbour = imObject()
				tempClosestNeighbour.copyObjectProperties(ClosestNeighbour[0])
				tempClosestNeighbour._parentimage = cubeSubtractedImage._Name
				tempClosestNeighbour.findLogicalPosition(write=True)
				cubeSubtractedImage.getObjectStatistics(tempClosestNeighbour, multiple=4)
				#tempClosestNeighbour.printInfo()
				
				# Collect the cube output
				ListOfCubeStats.append(tempClosestNeighbour)
				
			# Find the best cube
			# 	1. Set fake STDDEV and MEAN
			#	2. loop through to find the best 3 MEANS and then the bestSTDEV
			bestSTDEV, bestMEAN, bestMEDIAN, bestOBJECT, bestPercentageDiff = 10000000, 10000000, 10000000, False, 10000000


			# Best STDDEV
			for CubeStats in ListOfCubeStats:
				if abs(CubeStats._STDDEV) < bestSTDEV:
					bestSTDEV = abs(CubeStats._STDDEV)

			# Best MEDIAN
			for CubeStats in ListOfCubeStats:
				if abs(CubeStats._MEDIAN) < bestMEDIAN:
					bestMEDIAN = abs(CubeStats._MEDIAN)

			# Best MEAN
			for CubeStats in ListOfCubeStats:
				if abs(CubeStats._MEAN) < bestMEAN:
					bestMEAN = abs(CubeStats._MEAN)

			print "Minimum STDDEV: %f" % bestSTDEV
			print "Minimum MEDIAN: %f" % bestMEDIAN
			print "Minimum MEAN: %f\n" % bestMEAN
			bar = 1.0
			print "STDDEV MEAN MEDIAN"
			for CubeStats in ListOfCubeStats:

				# Test print
				print CubeStats._STDDEV, CubeStats._MEAN, CubeStats._MEDIAN, CubeStats._parentimage

				percentageDiff = abs(1 - ( (CubeStats._MEDIAN/bestMEDIAN) / (bestMEAN/CubeStats._MEAN) )) #/ ( abs(CubeStats._MEDIAN  ) ) 
				if percentageDiff < bestPercentageDiff:
				#if abs(CubeStats._MEDIAN) < (bestMEDIAN+0.1) and abs(CubeStats._MEAN) < bestMEAN+0.1:
					#bestMEDIAN = abs(CubeStats._MEDIAN)
					#bestSTDEV = abs(CubeStats._STDDEV)
					#bestMEAN = abs(CubeStats._MEAN)
					bestOBJECT = CubeStats
					bestPercentageDiff = percentageDiff

			if not bestOBJECT:
				print "No Object found to fit criteria."
				return 0

			print "The best object is:"
			print bestOBJECT._STDDEV, bestOBJECT._MEAN, bestOBJECT._MEDIAN
			#bestOBJECT.printInfo()

			# Copy it to a "best" directory
			bestoutput = "%s/%s/sub/best/" % (OB, band)
			if glob.glob(bestoutput+"*"):
				print "File exists"
				shutil.rmtree(bestoutput)
				print "Deleted"
			shutil.copytree(bestOBJECT._parentimage.replace("diff_%s.fits" % band, ""), bestoutput)
			#print glob.glob(bestoutput+"*")			
			print "Finished OB,band: %s, %s" % (OB,band)
			print "\n\n\n"