def sortDir(dirName, numberPrefix="t"): # Checks if (dirName == "" or dirName == None): sys.exit("Input directory must be specified.") # Obtain files files = os.listdir(dirName) numMaxFiles = len(files) myPrint.Printer.vprint("Applying regex to files: " + str(files) + ".") # Analyse files regex = r'.*' + str(numberPrefix) + myRE.group(myRE.floatRE) + r'.*' regex = re.compile(regex) files = myRE.getMatchingItemsAndGroups(files, regex) numMatchFiles = len(files) myPrint.Printer.vprint("Found " + str(numMatchFiles) + "/" + str(numMaxFiles) + " files that may be sorted numerically:") myPrint.Printer.vprint(files) # Sort files files = sorted(files, key=lambda item: float(item[1])) myPrint.Printer.vprint("Sorted:") myPrint.Printer.vprint(files) return myRE.untupleList(files)
def readFromFile_CoordsAB_header(FN, forceRead=False): # Sanity check filename: if not forceRead: doRegexMatch(FN,names.pixelCoords2DFNRE) assert (fileExists(FN)) # Read header from coordinates file: coordsFile = open(FN) coordsFile_row1 = coordsFile.readline() # "a= [%f %f %f]" line coordsFile_row2 = coordsFile.readline() # "b= [%f %f %f]" line coordsFile_row3 = coordsFile.readline() # "%i %i" line, yielding (Na, Nb) coordsFile.close() # Analyse the read Strings: row1_RE = "a= \[" + myRE.withinSpaces(myRE.floatREx3) + "\]" row1_RO = myRE.compile(row1_RE) span_a_str = myRE.getMatchingGroups(coordsFile_row1, row1_RO) row2_RE = "b= \[" + myRE.withinSpaces(myRE.floatREx3) + "\]" row2_RO = myRE.compile(row2_RE) span_b_str = myRE.getMatchingGroups(coordsFile_row2, row2_RO) row3_RO = myRE.compile(myRE.group(myRE.intRE) + " " + myRE.group(myRE.intRE)) npix_str = myRE.getMatchingGroups(coordsFile_row3, row3_RO) #print(span_a) #print(span_b) #print(npix) # Convert str tuple to ints/floats tuple: npix=() for item in npix_str: npix += (int(item),) span_a=() for item in span_a_str: span_a += (float(item),) span_b=() for item in span_b_str: span_b += (float(item),) # Done! return (npix, (span_a,span_b))
## Create output directory if (os.path.exists(outputDir) and overwrite): rmtree(outputDir) os.makedirs(outputDir) os.makedirs(logDir) # subdir of outputdir if (debug): print("Output directory '" + outputDir + "' was created.") # Save the template in the output directory for future reference copyfile(opticsInputTemplateFN, logDir + "/input.tmplt") # TODO: Do not hardcode the name "interpolConfig.dat" in Mie_MSFF/src/class_ScatterActuator.f90 copyfile(opticsInputInterpolConfig, logDir + "/interpolConfig.dat") ## Define regex for the particlePositions file #floatRE=r"[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?" #intRE=r"[0-9]+" myRegex = ".*_t" + \ myRE.group( myRE.group(myRE.intRE)+"_" , False) + "?" + \ myRE.group(myRE.floatRE) #myRegex = ".*_t(?:("+myRE.intRE+")_)?("+myRE.floatRE+")" #myRegex = "particlePositions_t(?:("+intRE+")_)?("+floatRE+")" #myRegex = "particlePositions_t("+intRE+")_("+floatRE+")" #myRegex = "particlePositions_t([0-9]+(?:\.[0-9]+)?)\.txt" partPosFNRO = re.compile(myRegex) ## Create a list of valid particlePositions files partPosList = os.listdir(partPosDir) num_total = len(partPosList) partPosList = myRE.getMatchingItems(partPosList, partPosFNRO) num_valid = len(partPosList) print("Number of valid ParticlePositions files found in \"" + \ partPosDir + "\" (rel. to total number) = " + \ str(num_valid) + "/" + str(num_total))
def natural_keys(text): try: return atof( myRE.getMatchingGroups(text, myRE.compile(r"[^\d]*" + myRE.group(myRE.floatRE)))[0] ) except: return float("inf")