def __init__(self, newPrint = Print() ): """Constructor""" self.__write = newPrint self.__resetMembers() return
def __init__(self, newPrint = Print() ): """Class constructor""" self.__write = newPrint self.__particleData = [] self.__numParticles = 0 return
def __init__(self, fileName=None, newPrint=Print()): """Constructor for the GSM Output class""" # Reset all values: self.__write = newPrint # Set values from constructor: if (fileName == None): self.__write.message = "A filename must be passed in to create an output file object." self.__write.print(0, 1) return self.__resetMembers() self.__fileName = fileName doesFileExist = fileExists(self.__fileName) if (not doesFileExist): self.__write.message = "File \"%s\" does not exist in this directory. Cannot obtain data." % ( self.__fileName) self.__write.print(1, 2) return # Read file: self.__fileData = readFile(self.__fileName) self.__fileLen = len(self.__fileData) self.__fileRead = True # Parse file data: self.__parseData() return
def __init__(self, bins, values, newNote=None, newPrint=Print()): """Constructor for class""" self.__write = newPrint self.__resetMembers() self.setNote(newNote) self.__setBinsAndValues(bins, values) return
def __init__(self, xVals, yVals, xErr=None, yErr=None, newPrint=Print()): """constructor for Scatter object""" # Set default values: self.__write = newPrint self.__resetMembers() self.__setXY(xVals, yVals) self.__setXYError(xErr, yErr) return
def __init__(self, particleID, newPrint = Print() ): """Constructor for object""" # Set defaults: self.__write = newPrint self.__resetMembers() # Set particle ID: self.__setParticleID(particleID) self.__resetObjects() self.__constructed = True if ( self.__particleID is None ): self.__constructed = False return
def __init__(self, particleID, newPrint=Print() ): """Constructor for the class""" # Set default values: self.__write = newPrint self.__resetMembers() self.__constructed = True # Set particle name: particleID = particleID.lower().strip() for partIndx in range(0, self.__numParticleIDs, 1): if ( particleID == self.__particleIDs[partIndx] ): self.__particleID = particleID break if ( self.__particleID is None ): self.__write.message = "An invalid particle identifier (\"%s\") was used when creating a particle's energy spectra." self.__write.print(1, 2) self.__constructed = False return
def __init__(self, particleName, newPrint = Print() ): """Constructor for particle PISA data""" self.__write = newPrint self.__particleName = None self.__histData = [] self.__numHistograms = 0 # Verify particle name: validParticle = False for i in range(0, numParticleTypes, 1): if ( particleName == particleTypes[ i ] ): validParticle = True break # If validparticle found, then store, else print warning: if ( validParticle ): self.__particleName = particleName else: self.__write.message = "Invalid particle ID (%s) used for ParticlePISAData construction." % (particleName) self.__write.print(1, 2) return
def __init__(self, type, angle, bins, values, newPrint = Print() ): """Constructor for class""" self.__write = newPrint # Reset Histogram values: self.__angle = 0 self.__type = None self.__dataPoints = [] self.__numDataPoints = 0 self.__binValues = [] self.__numBins = 0 # Set type of histogram data (double differential, energy integrated, angle integrated) validType = False typeIndx = 0 for i in range(0, numHistTypes, 1): if ( type.strip().lower() == histType[ i ] ): validType = True typeIndx = i break if ( validType ): self.__type = histType[ typeIndx ] else: self.__write.message = "Invalid histogram type detected (\"%s\"). Using \"%s\" instead." % (type, histType[ typeIndx ]) self.__write.print(1, 3) self.__type = histType[ typeIndx ] # Set angle for the plot: try: self.__angle = float(angle) except: # Data is either invalid; assume angle = 0 self.__write.message = "Unable to convert angle parameter (%s) to a float. Assuming angle integrated." % ( str(angle) ) self.__write.print(1, 2) self.__angle = 361 # Ensure valid angle value (in range [0, 360) ) if ( (not self.__angle == 361) and (not self.__angle == 362) ): if ( self.__angle < 0 ): self.__angle += 360 elif ( self.__angle >= 360 ): self.__angle -= 360 # Verify that bins and values are lists: validBins = isinstance(bins, list) validValues = isinstance(values, list) if ( (not validBins) or (not validValues) ): self.__write.message = "Invalid bins or values passed in to Histogram object. Parameters must be lists." self.__write.print(0, 1) return # Convert to floats: for i in range(0, len(bins), 1): try: bins[i] = float( bins[i] ) except: self.__write.message = "Unable to convert bin boundary (%s) to float. Assuming value is 0." % ( str(bins[i]) ) self.__write.print(1, 2) bins[i] = 0 for i in range(0, len(values), 1): try: values[i] = float( values[i] ) except: self.__write.message = "Unable to convert bin boundary (%s) to float. Assuming value is 0." % ( str(values[i]) ) self.__write.print(1, 2) values[i] = 0 # Ensure only positive values (for bins, values) # (Check for bins, shift all values up for non-physical values) shiftValue = 0 badBinBound = 0 for i in range(0, len(values), 1): shiftValue = min(shiftValue, values[i]) badBinBound = min(badBinBound, values[i]) shiftValue = abs(shiftValue) if ( shiftValue > 0 ): self.__write.message = "Unphysical lowest bin boundary (%f). Shifting all values up by (%f)." % (badBinBound, shiftValue) self.__write.print(0, 1) for i in range(0, len(values), 1): values[i] += shiftValue # (Do same for bins) if ( bins[0] < 0 ): shiftValue = -bins[0] self.__write.message = "Unphysical lower bin boundary (%f). Shifting all values up by (%f)." % (bins[0], shiftValue) self.__write.print(0, 1) else: shiftValue = 0 for i in range(0, len(bins), 1): bins[i] = shiftValue + bins[i] # Determine how many values to use (only accept minimum of what given data allows) numValues = min( len(values), (len(bins)-1) ) # Set bin values: self.__binValues.append( bins[0] ) # Append bin start: for i in range(1, (numValues+1), 1): # Ensure bin value is larger than last: if ( bins[i] < bins[i-1] ): self.__write.message = "The bin has smaller value than previous (range [%.2f, %.2f)). Setting bin width to 0..." % (bins[i-1], bins[i]) self.__write.print(1, 3) bins[i] = bins[i-1] self.__binValues.append( bins[i] ) self.__numBins = len( self.__binValues ) # Valid particle was found, now set data: for i in range(0, numValues, 1): self.__dataPoints.append( values[i] ) self.__numDataPoints = len( self.__dataPoints ) return
# IMPORTS: import sys import os import os.path as path # Modules: from printClass import Print # VERSION Number: __version__ = "1.0.0" # Module defaults: fileAppends = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z") numFileAppends = len(fileAppends) __filePrint = Print() def fileExists(fileName=None): """Determines if a file exists in the directory""" if (not fileName == None): doesFileExist = path.isfile(fileName) else: doesFileExist = False __filePrint.message = "No filename was passed in during existence check." __filePrint.print(0, 2) return doesFileExist def deleteFile(fileName):
def importData(cls, fileName, newPrint=Print()): """Reads X/Y points and associated error, if present, from a file""" __dataIdentifiers = ("x", "y", "dx", "dy", "skip") __numDataID = len(__dataIdentifiers) __commentFlags = ("#", "!", "//") __numCommentFlags = len(__commentFlags) __parseFlags = (",", ";", ":", " ") __numParseFlags = len(__parseFlags) newScatter = None # Check if file exists and read data: if (not fileModule.fileExists(fileName)): newPrint.message = "Unable to find filename to read the data from." newPrint.print(1, 2) return newScatter # Read data: theFilesData = fileModule.readFile(fileName) # Manipulate data read: for i in range(0, len(theFilesData), 1): # Remove all comments: for j in range(0, __numCommentFlags, 1): if (__commentFlags[j] in theFilesData[i]): lineData = theFilesData[i] lineData = lineData[:len(__commentFlags[j]) - 1] theFilesData[i] = lineData.rstrip() # Remove tabs: theFilesData[i] = theFilesData[i].replace("\t", " ") # Store fileData: fileData = [] for i in range(0, len(theFilesData), 1): # Ignore blank lines: if (theFilesData[i] == ""): continue else: # Store information: fileData.append(theFilesData[i].strip().lower()) # Obtain headers line, remove from data set, and determine how data is parsed: headerLine = fileData[0] del fileData[0] theParseFlag = None for i in range(0, __numParseFlags, 1): if (__parseFlags[i] in headerLine): theParseFlag = __parseFlags[i] break if (theParseFlag == None): newPrint.message = "Unable to determine how to parse data." newPrint.print(1, 2) newPrint.message = " The detected header is as follows: {}".format( headerLine.strip()) newPrint.print(1, 2) theParseFlag = input( "Please enter the string by which to parse file data: ") # Obtain header flags and the appropriate indices: headerFlags = fileModule.parseLine(headerLine, theParseFlag) numHeaderFlags = len(headerFlags) xIndx = None yIndx = None dxIndx = None dyIndx = None skipIndx = [] numSkipIndx = 0 for hdrID in range(0, numHeaderFlags, 1): headerFlags[hdrID] = headerFlags[hdrID].strip( ) # Remove excess spacing to help match: # Look for valid header: validHeader = False for flagID in range(0, __numDataID, 1): if (headerFlags[hdrID].startswith(__dataIdentifiers[flagID])): validHeader = True headerFlags[hdrID] == __dataIdentifiers[flagID] # Set index for X/Y values and associated errors, if present if (headerFlags[hdrID] == __dataIdentifiers[0]): # X flag if (xIndx == None): xIndx = hdrID else: newPrint.message = "X header already exists in file \"%s\". Using first X header for values." % ( fileName) newPrint.print(1, 2) elif (headerFlags[hdrID] == __dataIdentifiers[1]): # Y flag if (yIndx == None): yIndx = hdrID else: newPrint.message = "Y header already exists in file \"%s\". Using first Y header for values." % ( fileName) newPrint.print(1, 2) elif (headerFlags[hdrID] == __dataIdentifiers[2]): # dX flag if (dxIndx == None): dxIndx = hdrID else: newPrint.message = "dX header already exists in file \"%s\". Using first dX header for values." % ( fileName) newPrint.print(1, 2) elif (headerFlags[hdrID] == __dataIdentifiers[3]): # dY flag if (dyIndx == None): dyIndx = hdrID else: newPrint.message = "dY header already exists in file \"%s\". Using first dY header for values." % ( fileName) newPrint.print(1, 2) else: # Skip flag skipIndx.append(hdrID) numSkipIndx += 1 break if (not validHeader): # Treat as a skip header: skipIndx.append(hdrID) numSkipIndx += 1 # Print to user: newPrint.message = "Invalid data header was given (%s). The valid headers are:" % ( headerFlags[hdrID]) newPrint.print(1, 2) for i in range(0, __numDataID, 1): newPrint.message = " %d: %s" % ( (i + 1), __dataIdentifiers[i]) newPrint.print(1, 2) # Ensure X and Y are present at minimum: if (xIndx == None or yIndx == None): newPrint.message = "Both an X and Y data column must be specified for reading data in to a scatter plot." newPrint.print(0, 1) return newScatter # Parse and store data accordingly: xVals = [] yVals = [] if (dxIndx == None): dxVals = None else: dxVals = [] if (dyIndx == None): dyVals = None else: dyVals = [] for lineIndx in range(0, len(fileData), 1): # Parse the line: lineNum = lineIndx + 1 parsedLine = fileModule.parseLine(fileData[lineIndx], theParseFlag) lineLength = len(parsedLine) # Ensure header indices are within the bounds: if (xIndx >= lineLength): newPrint.message = "Unable to determine X value on line %d: Parsed data doesn't have enough indices." % ( lineNum) newPrint.print(0, 2) xVals.append(0.00) else: xVals.append(parsedLine[xIndx]) if (yIndx >= lineLength): newPrint.message = "Unable to determine Y value on line %d: Parsed data doesn't have enough indices." % ( lineNum) newPrint.print(0, 2) yVals.append(0.00) else: yVals.append(parsedLine[yIndx]) if (not dxIndx == None): if (dxIndx >= lineLength): newPrint.message = "Unable to determine dX value on line %d: Parsed data doesn't have enough indices." % ( lineNum) newPrint.print(0, 2) dxVals.append(0.00) else: dxVals.append(parsedLine[dxIndx]) if (not dyIndx == None): if (dyIndx >= lineLength): newPrint.message = "Unable to determine dY value on line %d: Parsed data doesn't have enough indices." % ( lineNum) newPrint.print(0, 2) dyVals.append(0.00) else: dyVals.append(parsedLine[dyIndx]) # X/Y values are now obtained; create object: newScatter = Scatter(xVals, yVals, dxVals, dyVals, newPrint) return newScatter
eprint("") eprint("--------------------------------------------") eprint("This script may accept any number of input") eprint(" files, requiring at least one input file.") eprint("") eprint("Each input file is given its own processor") eprint(" to aid in image generation.") eprint("") eprint("--------------------------------------------") eprint("The provided command line arguments include:") for i in range(0, numArgs): eprint("\t{}: {}".format(i + 1, cmdArgs[i].strip())) # Now perform for each input (use multiple processors): _numProcessors = min(determineNumProcessors(), numArgs) print("The script is being ran using {} processor(s).".format( _numProcessors)) # Create input file class for each provided file: if (_numProcessors > 1): with Pool(processes=_numProcessors) as pool: pool.map(PlotGSMInputFile, cmdArgs) pool.close() pool.join() else: messageControlloer = Print(2) PlotGSMInputFile(cmdArgs[0], messageControlloer) exit()