[1344] | 1 | #-------------------------------------------------------------------------------
|
---|
| 2 | # Name: SourceObject
|
---|
| 3 | # Purpose:
|
---|
| 4 | #
|
---|
| 5 | # Author: Kelvin
|
---|
| 6 | #
|
---|
| 7 | # Created: 02/01/2014
|
---|
| 8 | #-------------------------------------------------------------------------------
|
---|
| 9 |
|
---|
| 10 | """Duchamp Results Class Definition"""
|
---|
| 11 | # This is a class that holds various information from the results file generated by Duchamp
|
---|
| 12 | class DuchampResults():
|
---|
| 13 |
|
---|
| 14 | # Duchamp Results initialisation
|
---|
| 15 | def __init__(self, resultsFileName):
|
---|
| 16 |
|
---|
| 17 | # This is the name of the results file
|
---|
| 18 | self.resultsFileName = resultsFileName
|
---|
| 19 |
|
---|
| 20 | # This is the total number of detections we found in Duchamp
|
---|
| 21 | self.totalDetections = int(self.findParamInformation("# Total number of detections"))
|
---|
| 22 |
|
---|
| 23 | # This is the lower and upper boundaries of the channels flagged by the user
|
---|
| 24 | fc = self.findParamInformation("# Channels flagged by user")
|
---|
| 25 | (self.zf1, self.zf2) = (int(fc.split("-")[0]), int(fc.split("-")[1]))
|
---|
| 26 |
|
---|
| 27 | # This produces an array of SourceObject classes with indices corresponding to their Object ID
|
---|
| 28 | self.getSkyObjects()
|
---|
| 29 |
|
---|
| 30 | # This obtains all the parameters and their values listed in the results file
|
---|
| 31 | self.getParameterNamesAndValues()
|
---|
| 32 |
|
---|
| 33 | # By default, the names of the maskfits and input fits file are empty
|
---|
| 34 | self.maskfitsFileName = ''
|
---|
| 35 | self.fitsFileName = ''
|
---|
| 36 |
|
---|
| 37 | # This produces an array of SourceObject classes with indices corresponding to their Object ID
|
---|
| 38 | def getSkyObjects(self):
|
---|
| 39 |
|
---|
| 40 | # Initialise the SkyObjects read from the results file with indices corresponding to ObjID
|
---|
| 41 | self.SkyObjects = [self.totalDetections]
|
---|
| 42 | for ObjID in range(1, self.totalDetections + 1):
|
---|
| 43 | self.SkyObjects += [SourceObject(self.resultsFileName, ObjID)]
|
---|
| 44 |
|
---|
| 45 | # This finds the parameter value (on the other side of the equal sign) of the parameter name we specified
|
---|
| 46 | def findParamInformation(self, infoName):
|
---|
| 47 |
|
---|
| 48 | # Open the 'duchamp-Results.txt' file.
|
---|
| 49 | resultsFile = open(self.resultsFileName, 'r')
|
---|
| 50 |
|
---|
| 51 | # Go through each line.
|
---|
| 52 | for line in resultsFile:
|
---|
| 53 |
|
---|
| 54 | # Find the line with '=' in it.
|
---|
| 55 | if " = " in line:
|
---|
| 56 |
|
---|
| 57 | # Split the line at '='. There should only be two parts.
|
---|
| 58 | linebits = line.split(" = ")
|
---|
| 59 |
|
---|
| 60 | # If the variable is information we want, then close the file and grab the value of the total number of detections.
|
---|
| 61 | if infoName in linebits[0]:
|
---|
| 62 |
|
---|
| 63 | resultsFile.close()
|
---|
| 64 | return(linebits[1])
|
---|
| 65 |
|
---|
| 66 | # Otherwise, we will assume that there are no detections.
|
---|
| 67 | resultsFile.close()
|
---|
| 68 | return(0)
|
---|
| 69 |
|
---|
| 70 | def getParameterNamesAndValues(self):
|
---|
| 71 |
|
---|
| 72 | # Open the 'duchamp-Results.txt' file.
|
---|
| 73 | resultsFile = open(self.resultsFileName, 'r')
|
---|
| 74 |
|
---|
| 75 | self.parameterNames = []
|
---|
| 76 | self.parameterValues = []
|
---|
| 77 | self.parameters = {}
|
---|
| 78 | # Go through each line.
|
---|
| 79 | for line in resultsFile:
|
---|
| 80 |
|
---|
| 81 | # Find the line with '=' in it.
|
---|
| 82 | if " = " in line and "[" in line and "]" in line:
|
---|
| 83 |
|
---|
| 84 | parameterName = line.split('[')[1].split(']')[0]
|
---|
| 85 | parameterValue = line.split(' = ')[-1].split()[0]
|
---|
| 86 |
|
---|
| 87 | self.parameterNames += [parameterName]
|
---|
| 88 | self.parameterValues += [parameterValue]
|
---|
| 89 | self.parameters[parameterName] = parameterValue
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | # This converts the values of each paramter to its current format, given a list of values in string format.
|
---|
| 93 | def convertValues(values):
|
---|
| 94 |
|
---|
| 95 | # This will store the list of converted values.
|
---|
| 96 | convertedValues = []
|
---|
| 97 |
|
---|
| 98 | # Go through each value.
|
---|
| 99 | for value in values:
|
---|
| 100 |
|
---|
| 101 | # Convert it to either a string, a float, or integer, and add it to the list.
|
---|
| 102 | if ':' in value or 'J' in value or 'E' in value or 'J' in value or value == '-':
|
---|
| 103 | convertedValue = value
|
---|
| 104 | elif '.' in value and ':' not in value:
|
---|
| 105 | convertedValue = float(value)
|
---|
| 106 | else:
|
---|
| 107 |
|
---|
| 108 | try:
|
---|
| 109 | convertedValue = int(value)
|
---|
| 110 | except ValueError:
|
---|
| 111 | convertedValue = value
|
---|
| 112 |
|
---|
| 113 | convertedValues += [convertedValue]
|
---|
| 114 |
|
---|
| 115 | return(convertedValues)
|
---|
| 116 |
|
---|
| 117 | """Source Object Class Definition"""
|
---|
| 118 | # This contains some of the important information for a source we found through Duchamp
|
---|
| 119 | class SourceObject:
|
---|
| 120 |
|
---|
| 121 | # To initialise, we need the name of the results file and the ID of the object we want information from
|
---|
| 122 | def __init__(self, resultsFileName, ObjID):
|
---|
| 123 |
|
---|
| 124 | # This will be a dictionary mapping the parameter names of the source to its parameter values
|
---|
| 125 | self.stats = {}
|
---|
| 126 |
|
---|
| 127 | # Those are the list of parameter names and values in the order they were listed in the file
|
---|
| 128 | self.parameterNames = []
|
---|
| 129 | self.parameterValues = []
|
---|
| 130 |
|
---|
| 131 | # This is the results file
|
---|
| 132 | resultsFile = open(resultsFileName, 'r')
|
---|
| 133 |
|
---|
| 134 | #
|
---|
| 135 | for line in resultsFile:
|
---|
| 136 |
|
---|
| 137 | try:
|
---|
| 138 | if line.split()[1] == "ObjID":
|
---|
| 139 |
|
---|
| 140 | self.parameterNames = line.replace("# ", "").split()
|
---|
| 141 |
|
---|
| 142 | except IndexError:
|
---|
| 143 | pass
|
---|
| 144 |
|
---|
| 145 | if line.split()[0] == str(ObjID):
|
---|
| 146 |
|
---|
| 147 | self.parameterValueString = line.split()
|
---|
| 148 | self.parameterValues = convertValues(self.parameterValueString)
|
---|
| 149 |
|
---|
| 150 | resultsFile.close()
|
---|
| 151 |
|
---|
| 152 | if len(self.parameterNames) != len(self.parameterValues):
|
---|
| 153 | return
|
---|
| 154 |
|
---|
| 155 | for i in range(len(self.parameterNames)):
|
---|
| 156 | self.stats[self.parameterNames[i]] = self.parameterValues[i]
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | # Define a function to create a parameter file with the specified format
|
---|
| 162 | # Parameters:
|
---|
| 163 | # newParamFilePath: This is the path and name of the parameter file to write to
|
---|
| 164 | # parameters : This is the dictionary mapping the parameter names to their values
|
---|
| 165 | # parameterOrder : This is a list of the parameter names in the order they were found in the results file
|
---|
| 166 | def writeParamFile(newParamFilePath, parameters, parameterOrder):
|
---|
| 167 |
|
---|
| 168 | newParamFile = open(newParamFilePath, 'w')
|
---|
| 169 |
|
---|
| 170 | # Write the editted set of parameters into the new file
|
---|
| 171 | for par in parameterOrder:
|
---|
| 172 | newParamFile.write('%s %s\n'%(par, parameters[par]))
|
---|
| 173 |
|
---|
| 174 | newParamFile.close()
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 |
|
---|