Exemplo n.º 1
0
def writeToFile_CoordsComp2D(data1D, outputDN, which, npix, overwrite=False, suffix=""):
	# Select the right coordinate:
	which = int(which)
	if which == 0:
		outputFN = names.joinPaths(outputDN,names.pixelCoords2DxFN) + str(suffix)
	elif which == 1:
		outputFN = names.joinPaths(outputDN,names.pixelCoords2DyFN) + str(suffix)
	else:
		raise Exception("Error: Tried to access coordinate \"" + str(which) + "\", but only \"0\" and \"1\" are implemented.")
	# Sanity:
	assert (fileNotAlreadyExists(outputFN,overwrite))

	# Extract component, and reshape to 2D:
	#  Reshape it such that x increases with column number and y increases with row number:
	#  To flip these, use: data2Dx=np.transpose(data2Dx) and data2Dy=np.transpose(data2Dy).
	data2Di = reshapeTo2D(data1D[:,which],npix)

	# Write to file in 2D format (matrix shape that holds one component for every pixel):
	outputFile = open(outputFN, "w")
	for i_x in range(0, npix[0]) : # x goes into different rows
		#(note: this is row-column convention, not an x-y-axis convention)
		line = ""
		for i_y in range(0, npix[1]) : # y goes into different columns
			line = line + str(data2Di[i_x,i_y]) + " "
		outputFile.write(line[0:-1] + "\n")
	outputFile.close()
Exemplo n.º 2
0
def restructureInputDir(inputDN):
    input1DDN = names.joinPaths(inputDN, names.input1DDN)
    if (not os.path.exists(input1DDN)):
        os.makedirs(input1DDN)
        expr = myRE.either(
            myRE.either(names.intensity1DFNRE, names.pixelCoordsFNRE),
            names.intensitySortedDNRE)
        RO = re.compile(expr)
        files = myRE.getMatchingItems(os.listdir(inputDN), RO)
        print("(restructureInputDir) Moving files: " + str(files))
        for item in files:
            FN = names.joinPaths(inputDN, item)
            shutil.move(FN, input1DDN)
    input2DDN = names.joinPaths(inputDN, names.input2DDN)
    if (os.path.exists(input2DDN)):
        shutil.rmtree(input2DDN)
    os.makedirs(input2DDN)
Exemplo n.º 3
0
def writeToFile_Coords2D(data, outputDN, span, npix, overwrite=False, suffix=""):
	outputFN = names.joinPaths(outputDN,names.pixelCoords2DFN) + str(suffix)
	# Sanity:
	assert (fileNotAlreadyExists(outputFN,overwrite))
	assert (len(span)==2), "In writeToFile_Coords2D: len(span) must be 2."
	assert (len(npix)==2), "In writeToFile_Coords2D: len(npix) must be 2."

	## Detect whether "data" is in 1D format or in 2D (meshgrid) format:
	shape = np.shape(data)
	#print("data shape = " + str(shape))
	if (len(shape) == 3):
		assert(shape[0] == 2), "In writeToFile_Coords2D: expected a tuple of length two: (A,B), but received shape: " + str(shape) + "."
		assert(np.shape(data[0]) == np.shape(data[1])), "In writeToFile_Coords2D: meshgrid (A,B), must have the same shape, but received shape(meshgrid): " + str(np.shape(data[0])) + " and " + str(np.shape(data[1])) + "."
		assert(npix == np.shape(data[0])), "In writeToFile_Coords2D: meshgrid (A,B) must have the shape of npix, but received shape(meshgrid)=" + str(np.shape(data[0])) + ", npix=" + str(npix) + "."
		# We have a tuple (A,B) with A and B meshgrids.
		dataType=2
		#print("Found 2D meshgrid data set!")
		#print(np.shape(data[0]))
		#print(np.shape(data[1]))
		#print(npix)

	elif (len(shape) == 2):
		assert(shape[1] == 2), "In writeToFile_Coords2D: expected a 1D vector with two coordinates, but it has " + str(shape[1]) + " coordinates."
		assert(npix[0]*npix[1] == shape[0]), "In writeToFile_Coords2D: expected a 1D vector with length equal to the number of pixels (" + str(npix) + " = " + str(npix[0]*npix[1]) + "), but it had length " + str(shape[0]) + "."
		# We have a 1D vector of length (npix[0]*npix[1]) with 2 coordinates.
		dataType=1
		#print("Found 1D data set!")

	else:
		raise Exception("writeToFile_Coords2D received data that it cannot interpret. Expected either:\n" + \
			"  - matrix of shape (number of pixels, 2), giving the (a,b) coordinates in order with the a-direction incrementing first" + \
			"  - tuple of length two holding (A,B), where A and B are meshgrids of shape (npix_a, npix_b)." \
		)

	# Write file header:
	outputFile = open(outputFN, "w")
	a=span[0]
	b=span[1]
	outputFile.write("a= "+str(a/np.linalg.norm(a)) + "\n" )
	outputFile.write("b= "+str(b/np.linalg.norm(b)) + "\n" )
	outputFile.write(str(npix[0]) + " " + str(npix[1]) + "\n" )
	# Write the 2D data to file in 1D format (i.e., on each line the (a,b) coordinate):
	if dataType == 1:
		for elem in data :
			line = ""
			for elemm in elem :
				line = line + str(elemm) + " "
			outputFile.write(line[0:-1] + "\n")
	elif dataType == 2:
		for i_b in range(0,npix[1]) :
			for i_a in range(0,npix[0]) :
				outputFile.write(str(data[0][i_a,i_b]) + " " + str(data[1][i_a,i_b]) + "\n")
	else:
		raise Exception("In writeToFile_Coords2D: programming logic error. This cannot occur.")

	# And finally close the file:
	outputFile.close()
Exemplo n.º 4
0
def writeToFile_Intensity2D(data2D, outputDN, time, index=None, overwrite=False, suffix=""):
	# Generate output filename:
	outputFN = names.joinPaths(outputDN,names.intensity2DFN(time,index)) + str(suffix)
	# Sanity:
	assert (fileNotAlreadyExists(outputFN,overwrite))
	# Write data to file:
	outputFile = open(outputFN, "w")
	for i_x in range(0, len(data2D[:,0]) ) : # x (a-direction) goes into different rows = vertical
		#(note: this is row-column convention, not an x-y-axis convention, such that "first index" = "x")
		line = ""
		for i_y in range(0, len(data2D[0,:])) : # y (b-direction) goes into different columns = horizontal
			line = line + str(data2D[i_x,i_y]) + " "
		outputFile.write(line[0:-1] + "\n")
	outputFile.close()
Exemplo n.º 5
0
########
## Check Validity of Input Parameters
####
# Check for existence of the files
if (not os.path.exists(inputDN)):
    sys.exit("\nERROR: Inputdir '" + inputDN + "' does not exist.\n" + \
       "Terminating program.\n" )
if (not outputIsInput and os.path.exists(outputDN) and not overwrite):
    sys.exit("\nERROR: Outputdir '" + outputDN + "' already exists.\n" + \
       "Terminating program to prevent overwrite. Use the -f option to enforce overwrite.\n" + \
       "BE WARNED: This will remove the existing directory with the same name as the Outputdir!")

# Check whether input has a "1D" directory to store Intensity/PixelCoords:
intInputDN = inputDN
input1DDN = names.joinPaths(inputDN, names.input1DDN)
if (os.path.exists(input1DDN)):
    intInputDN = input1DDN

# Check whether we are trying to write to an already-existing "2D" directory:
input2DDN = names.joinPaths(inputDN, names.input2DDN)
if (outputIsInput and os.path.exists(input2DDN) and not overwrite):
    sys.exit("\nERROR: Outputdir '" + input2DDN + "' already exists.\n" + \
       "Terminating program to prevent overwrite. Use the -f option to enforce overwrite.\n" + \
       "BE WARNED: This will remove the existing directory with the same name as the Outputdir!")

# Check whether input exists
pixelCoordsFN = names.joinPaths(intInputDN, names.pixelCoordsFN)
if (not os.path.exists(pixelCoordsFN)):
    sys.exit("\nERROR: Inputfile for the PixelCoords '" + pixelCoordsFN + "' does not exist.\n" + \
       "Terminating program.\n" )
Exemplo n.º 6
0
 def generatePosFN(self, time):
     return names.joinPaths(self.outputDN, names.particlePositionsFN(time))
Exemplo n.º 7
0
def timeIntegrateOptics(inputDN, outputDN, overwrite=False):
    ### Preamble
    ## Process parameters
    if (outputDN == None or outputDN == ""):
        outputDN = inputDN  # Write to same directory
    checkArgumentValidity(inputDN, outputDN, overwrite=overwrite)
    outputIsInput = os.path.samefile(inputDN, outputDN)

    ## Detect "2D" and "1D" directories, if exists:
    input1DDN = names.joinPaths(inputDN, names.input1DDN)
    input2DDN = names.joinPaths(inputDN, names.input2DDN)
    if (os.path.exists(input2DDN)):
        inputDN = input2DDN
        print("Found \"2D\" directory. Using it as input: " + str(inputDN))
    elif (os.path.exists(input1DDN)):
        inputDN = input1DDN
        print("Found \"1D\" directory. Using it as input: " + str(inputDN))
    else:
        print(
            "Did not find \"2D\" or \"1D\" directory. Using root as input: " +
            str(inputDN))
    if (outputIsInput):
        outputDN = inputDN

    ## In the input directory, there should be sorted directories with the microsteps. Detect them:
    RO = re.compile(names.intensitySortedDNRE)
    timeDNList = myRE.getMatchingItems(os.listdir(inputDN), RO)
    print("Found the following major timesteps: " + str(timeDNList))
    if (len(timeDNList) == 0):
        sys.exit(
            "\nERROR: Did not find any sorted major-timestep time directories."
        )

    ## Prepare output directory
    if (outputIsInput):
        # Can we write to the "blurred" directory?
        outputDN = names.joinPaths(outputDN, names.intensityBlurredDN())
        if (os.path.exists(outputDN)):
            if (overwrite):
                rmtree(outputDN)
            else:
                sys.exit("\nERROR: Output location '" + outputDN + "' already exists.\n" + \
                  "Terminating program to prevent overwrite. Use the -f option to enforce overwrite.\n" + \
                  "BE WARNED: This will remove the existing file with the same name!!")
    elif (os.path.exists(outputDN) and overwrite):
        rmtree(outputDN)
    os.makedirs(outputDN)

    ## Process every timestep
    for timeDN in tqdm(timeDNList):
        time = timeDN
        timeDN = names.joinPaths(inputDN, timeDN)
        # Count #1D intensities:
        num1D = myRE.countMatchingItems(os.listdir(timeDN),
                                        re.compile(names.intensity1DFNRE))
        num2D = myRE.countMatchingItems(os.listdir(timeDN),
                                        re.compile(names.intensity2DFNRE))
        numTot = len(os.listdir(timeDN))
        #print("Found for time " + str(timeDN) + ":\n" + \
        #		"num1D = " + str(num1D) + "; num2D = " + str(num2D) + "; numTot = " + str(numTot))
        if (num1D > 0 and num2D == 0):
            outputFN = names.intensity1DFN(time)
        elif (num2D > 0 and num1D == 0):
            outputFN = names.intensity2DFN(time)
        else:
            sys.exit("\nERROR: Input direction '" + str(timeDN) +
                     "' mixes 1D and 2D formatted intensity files.")
        outputFN = names.joinPaths(outputDN, outputFN)
        print(str(timeDN) + " --> " + str(outputFN))
        # Camera-integrate this timestep:
        integrateOneDir(intensityDN=timeDN,
                        outputName=outputFN,
                        overwrite=overwrite)
Exemplo n.º 8
0
    def run(self):
        ##############
        ## Analyse input directory times;
        ## raise errors before doing any harmful operations
        ####

        ## 0) Detect location of the intensity files
        input1DDN = names.joinPaths(self.inputDN, names.input1DDN)
        input2DDN = names.joinPaths(self.inputDN, names.input2DDN)
        if (os.path.exists(input2DDN)):
            self.inputDN = input2DDN
            self.vprint("Found \"2D\" directory. Using it as input: " +
                        str(self.inputDN))
        elif (os.path.exists(input1DDN)):
            self.inputDN = input1DDN
            self.vprint("Found \"1D\" directory. Using it as input: " +
                        str(self.inputDN))
        else:
            self.vprint(
                "Did not find \"2D\" or \"1D\" directory. Using root as input: "
                + str(self.inputDN))
        if (self.outputIsInput):
            self.outputDN = self.inputDN

        ## 1) Read all intensity filenames with regex
        ## 2) Make a list of all times; sort it numerically
        intFNRO = re.compile(names.intensityFNRE)
        resultFileList = os.listdir(self.inputDN)
        intFiles = myRE.getMatchingItemsAndGroups(resultFileList, intFNRO)
        # intFiles is now an array of tuples of the form: (filename, time)

        # Sort the times
        intFiles.sort(
            key=lambda time: float(time[1]))  # sort by time, incl. exp.not.

        # Show found times to user:
        out = "Found (sorted) times: "
        for FN, time in intFiles:
            out += time + " "
        self.vprint(out)

        if (len(intFiles) == 0):
            sys.exit(
                "Did not find any intensity files in the input directory ('" +
                self.inputDN + "'). Exiting.")
        if (len(intFiles) == 1):
            sys.exit("ERROR:\n" + \
               " Only found one intensity files in the input directory ('"+self.inputDN+"'). Nothing to sort." + "\n" + \
               " In case this directory was already sorted, and you wish to sort-in new files, then " + "\n" + \
               " unsort it by hand before re-sorting it (echo to check first):\n" + \
               self.unsortMsg(" ") + \
               " Exiting.")

        ## 3) Detect dt and dt_us
        deltaTimes = makeDeltaList(intFiles, 1)
        self.vprint("Resulting dts are:  " + str(deltaTimes))
        ## 3a) Compare first and second timename, that is dt_us.
        dt_us = deltaTimes[0]
        ## 3b) Make a list of indices in which the jump is larger than dt_us
        big_start_ilist = [0]
        data_lengths = []
        prev = 0
        for idt in range(len(deltaTimes)):
            #if ( deltaTimes[idt] != dt_us):
            if (deltaTimes[idt] > self.dt_us_tol * dt_us):
                big_start_ilist.append(idt + 1)
                data_lengths.append(idt + 1 - prev)
                prev = idt + 1
        data_lengths.append(idt + 2 - prev)
        del prev
        self.vprint("Big-step starting indices: " + str(big_start_ilist))
        self.vprint("Big-step data lengths:     " + str(data_lengths))

        ## 3c) Apply a check to make sure dt_us and dt are constants. Otherwise raise an error.
        if (len(big_start_ilist) > 1):  # else only one major timestep
            dt1 = myRound(
                myRound(float(intFiles[big_start_ilist[1]][1])) -
                myRound(float(intFiles[0][1])))
            prev = 0
            for i in range(1, len(big_start_ilist)):
                ijump = big_start_ilist[i]
                dt2 = myRound(
                    myRound(float(intFiles[ijump][1])) -
                    myRound(float(intFiles[prev][1])))
                self.vprint("(" + str(i) + ") Found major dt = " + str(dt2))
                if (dt1 != dt2):
                    sys.exit("ERROR\n" + \
                       " Found different major timesteps: dt1="+str(dt1)+", dt2="+str(dt2)+".\n" + \
                       " Terminating without sorting.")
                prev = ijump
            del prev

        ## 4) Make an array of the different sets of microsteps
        sortedFNs = []
        sortedMajorTimes = []
        self.vprint("Sorted filenames: ")
        for i in range(len(big_start_ilist)):
            #print(i)
            sortedMajorTimes.append(intFiles[big_start_ilist[i]][1])
            tupleMaker = ()
            for j in range(big_start_ilist[i],
                           big_start_ilist[i] + data_lengths[i]):
                #print(" " + str(j))
                tupleMaker += (intFiles[j][0], )
            self.vprint(" " + str(tupleMaker))
            sortedFNs.append(tupleMaker)

        self.vprint("Major start times: " + str(sortedMajorTimes))

        ##############
        ## Sort times into output directory
        ####

        # Pre-check the existence of (sorted) major-time directories if outputIsInput
        # and exit if it already exists. Then apparently this directory was already sorted.
        # Cannot sort twice (presently)!
        if (self.outputIsInput):
            for time in sortedMajorTimes:
                dirOut = os.path.join(self.outputDN,
                                      names.intensitySortedDN(time))
                if (os.path.exists(dirOut)):
                    sys.exit("ERROR:\n" + \
                      " Majortime directory ('"+str(dirOut)+"') already exists.\n" + \
                      " Presumably this directory was already sorted?\n" + \
                      " If so, unsort it by hand before re-sorting it (echo to check first):\n" + \
                      self.unsortMsg(" ") + \
                      " Exiting.")

        ###
        ## All checks are done. From now on we may safely apply irreversible operations.
        ###

        #  Move the inputDir to outputDir.
        # Then we can treat self.outputIsInput True and False alike:
        # outputDir is then both the source as the target!
        if (not self.outputIsInput):
            # move/copy (depending on self.preserveInputDir) inputDir to outputDir
            if (os.path.exists(self.outputDN) and self.overwrite):
                shutil.rmtree(self.outputDN)
            self.preserveMove(self.inputDN,
                              self.outputDN,
                              self.preserveInputDir,
                              recursive=True)
        elif (self.preserveInputDir):
            #  preserve inputDir by moving it to a hidden directory of the same name;
            # then copy it to outputDir such that we can treat outputDir as if it was the inputDir
            self.inputDN = os.path.join(os.path.dirname(self.inputDN),
                                        "." + os.path.basename(self.inputDN))
            self.vprint("Preserving input \"" + str(self.outputDN) +
                        "\" to \"" + str(self.inputDN) + "\".")
            if (os.path.exists(self.inputDN)):
                if (self.overwrite):
                    shutil.rmtree(self.inputDN)  # overwrite preserve location
                else:
                    sys.exit("ERROR\n" + \
                       " Trying to preserve input '" + self.outputDN + \
                       "' to '" + self.inputDN + "', but this directory already exists." + "\n" + \
                       " Use -f to overwrite.\n" \
                       " Exiting."
                    )
            shutil.move(self.outputDN, self.inputDN)
            shutil.copytree(self.inputDN, self.outputDN)

        # 5) Make a directory for each main timename
        dirOutList = []
        for time in sortedMajorTimes:
            # Convert to float to str, such that "0.000000" is just "0.0", and "0.000001" is "1e-06".
            dirOut = os.path.join(self.outputDN, str(float(time)))
            os.makedirs(dirOut)
            dirOutList.append(dirOut)

        # 6) Move all intensity files to the appropriate directory:
        for iset in range(len(sortedFNs)):
            dirOut = dirOutList[iset]
            for FN in sortedFNs[iset]:
                self.vprint("Moving: " + str(FN) + " --> " + str(dirOut))
                shutil.move(os.path.join(self.outputDN, FN), dirOut)