#------------------------------------------------------------------------------- # Name: SourceObject # Purpose: # # Author: Kelvin # # Created: 02/01/2014 #------------------------------------------------------------------------------- """Duchamp Results Class Definition""" # This is a class that holds various information from the results file generated by Duchamp class DuchampResults(): # Duchamp Results initialisation def __init__(self, resultsFileName): # This is the name of the results file self.resultsFileName = resultsFileName # This is the total number of detections we found in Duchamp self.totalDetections = int(self.findParamInformation("# Total number of detections")) # This is the lower and upper boundaries of the channels flagged by the user fc = self.findParamInformation("# Channels flagged by user") (self.zf1, self.zf2) = (int(fc.split("-")[0]), int(fc.split("-")[1])) # This produces an array of SourceObject classes with indices corresponding to their Object ID self.getSkyObjects() # This obtains all the parameters and their values listed in the results file self.getParameterNamesAndValues() # By default, the names of the maskfits and input fits file are empty self.maskfitsFileName = '' self.fitsFileName = '' # This produces an array of SourceObject classes with indices corresponding to their Object ID def getSkyObjects(self): # Initialise the SkyObjects read from the results file with indices corresponding to ObjID self.SkyObjects = [self.totalDetections] for ObjID in range(1, self.totalDetections + 1): self.SkyObjects += [SourceObject(self.resultsFileName, ObjID)] # This finds the parameter value (on the other side of the equal sign) of the parameter name we specified def findParamInformation(self, infoName): # Open the 'duchamp-Results.txt' file. resultsFile = open(self.resultsFileName, 'r') # Go through each line. for line in resultsFile: # Find the line with '=' in it. if " = " in line: # Split the line at '='. There should only be two parts. linebits = line.split(" = ") # If the variable is information we want, then close the file and grab the value of the total number of detections. if infoName in linebits[0]: resultsFile.close() return(linebits[1]) # Otherwise, we will assume that there are no detections. resultsFile.close() return(0) def getParameterNamesAndValues(self): # Open the 'duchamp-Results.txt' file. resultsFile = open(self.resultsFileName, 'r') self.parameterNames = [] self.parameterValues = [] self.parameters = {} # Go through each line. for line in resultsFile: # Find the line with '=' in it. if " = " in line and "[" in line and "]" in line: parameterName = line.split('[')[1].split(']')[0] parameterValue = line.split(' = ')[-1].split()[0] self.parameterNames += [parameterName] self.parameterValues += [parameterValue] self.parameters[parameterName] = parameterValue # This converts the values of each paramter to its current format, given a list of values in string format. def convertValues(values): # This will store the list of converted values. convertedValues = [] # Go through each value. for value in values: # Convert it to either a string, a float, or integer, and add it to the list. if ':' in value or 'J' in value or 'E' in value or 'J' in value or value == '-': convertedValue = value elif '.' in value and ':' not in value: convertedValue = float(value) else: try: convertedValue = int(value) except ValueError: convertedValue = value convertedValues += [convertedValue] return(convertedValues) """Source Object Class Definition""" # This contains some of the important information for a source we found through Duchamp class SourceObject: # To initialise, we need the name of the results file and the ID of the object we want information from def __init__(self, resultsFileName, ObjID): # This will be a dictionary mapping the parameter names of the source to its parameter values self.stats = {} # Those are the list of parameter names and values in the order they were listed in the file self.parameterNames = [] self.parameterValues = [] # This is the results file resultsFile = open(resultsFileName, 'r') # for line in resultsFile: try: if line.split()[1] == "ObjID": self.parameterNames = line.replace("# ", "").split() except IndexError: pass if line.split()[0] == str(ObjID): self.parameterValueString = line.split() self.parameterValues = convertValues(self.parameterValueString) resultsFile.close() if len(self.parameterNames) != len(self.parameterValues): return for i in range(len(self.parameterNames)): self.stats[self.parameterNames[i]] = self.parameterValues[i] # Define a function to create a parameter file with the specified format # Parameters: # newParamFilePath: This is the path and name of the parameter file to write to # parameters : This is the dictionary mapping the parameter names to their values # parameterOrder : This is a list of the parameter names in the order they were found in the results file def writeParamFile(newParamFilePath, parameters, parameterOrder): newParamFile = open(newParamFilePath, 'w') # Write the editted set of parameters into the new file for par in parameterOrder: newParamFile.write('%s %s\n'%(par, parameters[par])) newParamFile.close()