def read(self, filepattern): dirname = os.path.dirname(filepattern) items = os.listdir(dirname) escaped_pattern = string.replace( re.escape(os.path.basename(filepattern)), "\\*", ".*?") files = [] for item in items: match = re.match(escaped_pattern, item) if match != None: span = match.span() if span[0] == 0 and span[1] == len(item): files.append(os.path.join(dirname, item)) prog = progress.getProgress(os.path.basename(filepattern), progress.DEFINITE) try: # Look at the just the first file to get some info z = 0 rows, npts = self.readfile(files[0], prog, z) nx = len(rows[0]) ny = len(rows) nz = len(files) dx = rows[0][1].position[0] - rows[0][0].position[0] dy = rows[1][0].position[1] - rows[0][0].position[1] dz = 1 pxlsize = primitives.Point(dx, dy, dz) size = rows[-1][-1].position + pxlsize od = orientmapdata.OrientMap(primitives.iPoint(nx, ny, nz), size) count = 0 for row in rows: count += 1 for datum in row: self.addDatum(od, datum, pxlsize, ny, count, npts, prog, nz) # now look at the rest of the files for file in files[1:]: z = z + dz rows, nrowpts = self.readfile(file, prog, z) npts = npts + nrowpts count = 0 for row in rows: count += 1 if len(row) != nx: raise ooferror.ErrUserError( "Orientation map data appears to be incomplete" ) for datum in row: self.addDatum(od, datum, pxlsize, ny, count, npts, prog, nz) finally: prog.finish() return od
def read(self, filename): prog = progress.getProgress(os.path.basename(filename), progress.DEFINITE) rows, npts = self.readfile(filename, prog) try: nx = len(rows[0]) ny = len(rows) count = 0 for row in rows: count += 1 if len(row) != nx: raise ooferror.ErrUserError( "Orientation map data appears to be incomplete") # pixel size dx = rows[0][1].position[0] - rows[0][0].position[0] dy = rows[1][0].position[1] - rows[0][0].position[1] pxlsize = primitives.Point(dx, dy) # If we assume that the points are in the centers of the # pixels, then the actual physical size is one pixel bigger # than the range of the xy values. size = rows[-1][-1].position + pxlsize debug.fmsg("nx=", nx, "ny=", ny, "size=", size, "pxlsize=", pxlsize) if config.dimension() == 2: od = orientmapdata.OrientMap(primitives.iPoint(nx, ny), size) else: od = orientmapdata.OrientMap(primitives.iPoint(nx, ny, 1), size) count = 0 for row in rows: for datum in row: self.addDatum(od, datum, pxlsize, ny, count, npts, prog) finally: prog.finish() return od
def read(self, filename): hklfile = file(filename, "r") ## TODO OPT: Use lineiter = iter(hklfile) instead of iter(lines). ## Then it's not necessary to create an array of lines. lines = hklfile.readlines() lineiter = iter(lines) line = lineiter.next() while not line.startswith('XCells'): line = lineiter.next() xcells = string.atoi(string.split(line)[1]) line = lineiter.next() ycells = string.atoi(string.split(line)[1]) line = lineiter.next() xstep = string.atof(string.split(line)[1]) line = lineiter.next() ystep = string.atof(string.split(line)[1]) line = lineiter.next() od = orientmapdata.OrientMap( primitives.iPoint(xcells, ycells), primitives.Point(xcells * xstep, ycells * ystep)) while not string.split(line)[0] == 'Phase': line = lineiter.next() prog = progress.getProgress(os.path.basename(filename), progress.DEFINITE) try: count = 0 npts = xcells * ycells for line in lineiter: vals = string.split(line) phase = vals[0] x = string.atof(vals[1]) y = string.atof(vals[2]) angles = map(string.atof, vals[5:8]) mad = string.atof(vals[8]) # mean angular deviation ij = primitives.iPoint(int(round(x / xstep)), ycells - 1 - int(round(y / ystep))) try: self.phaselists[phase].append(ij) except KeyError: self.phaselists[phase] = [ij] self.set_angle( od, ij, corientation.COrientBunge(*map(math.radians, angles))) prog.setFraction(float(count) / npts) prog.setMessage("%d/%d orientations" % (count, npts)) count += 1 if pbar.stopped(): return None finally: prog.finish() return od
def _loadOrientationMap(menuitem, microstructure, filename, orientations): # orientations is a list of 3-tuples representing ABG Euler # angles. mscontext = ooflib.common.microstructure.microStructures[microstructure] ms = mscontext.getObject() od = orientmapdata.OrientMap(ms.sizeInPixels(), ms.size()) reader = orientmapdata.OrientMapReader() # just to borrow its set_angle fn count = 0 for pixel in ms.coords(): reader.set_angle(od, pixel, corientation.COrientABG(*orientations[count])) count += 1 orientmapdata.registerOrientMap(microstructure, od) orientmapplugin = mscontext.getObject().getPlugIn('OrientationMap') orientmapplugin.set_data(od, filename) orientmapplugin.timestamp.increment() orientmapdata.orientationmapNotify(ms)
def _read(self, datafile, prog): # readData gets data from the file, but does no processing data = self.readData(datafile, prog) rows = list(getrows(data)) # sorts data # The grid is hexagonal if the first two rows don't start at the same x. if rows[0][0].position[0] != rows[1][0].position[0]: # Throw out every other row. reporter.warn("Converting hexagonal lattice to rectangular" " by discarding alternate rows.") rows = rows[::2] # discard odd numbered rows # Check that all rows are the same length, and flip the # coordinates if requested. nx = len(rows[0]) ny = len(rows) ymax = rows[-1][0].position[1] ymin = rows[0][0].position[1] xmax = rows[0][-1].position[0] xmin = rows[0][0].position[0] count = 0 for row in rows: count += 1 if len(row) != nx: raise ooferror.ErrUserError( "Orientation map data appears to be incomplete.\n" "len(row 0)=%d len(row %d)=%d" % (nx, count, len(row))) for point in row: if self.flip_x: point.position[0] = xmax - point.position[0] else: point.position[0] = point.position[0] - xmin if self.flip_y: point.position[1] = ymax - point.position[1] else: point.position[1] = point.position[1] - ymin # pixel size dx = abs(rows[0][1].position[0] - rows[0][0].position[0]) dy = abs(rows[0][0].position[1] - rows[1][0].position[1]) pxlsize = primitives.Point(dx, dy) width = abs(rows[0][0].position[0] - rows[0][-1].position[0]) height = abs(rows[0][0].position[1] - rows[-1][0].position[1]) # If we assume that the points are in the centers of the # pixels, then the actual physical size is one pixel bigger # than the range of the xy values. size = primitives.Point(width, height) + pxlsize npts = len(rows) * len(rows[0]) od = orientmapdata.OrientMap(primitives.iPoint(nx, ny), size * self.scale_factor) prog.setMessage("%d/%d orientations" % (0, npts)) count = 0 for row in rows: for datum in row: ij = primitives.iPoint( int(round(datum.position[0] / pxlsize[0])), int(round(datum.position[1] / pxlsize[1]))) for groupname in datum.groups: try: self.groupmembers[groupname].append(ij) except KeyError: self.groupmembers[groupname] = [ij] if self.angle_units == 'Degrees': offset = math.radians(self.angle_offset) angleargs = datum.angletuple else: # angle units are Radians offset = self.angle_offset # All Orientation subclasses that take angle args # assume that they're in degrees. They have a # static radians2Degrees method that converts the # angle args from radians to degrees. angleargs = self.angle_type.radians2Degrees( *datum.angletuple) # Create an instance of the Orientation subclass. orient = self.angle_type(*angleargs) if self.angle_offset != 0: orient = orient.rotateXY(self.angle_offset) # Insert this point into the OrientMap object. self.set_angle(od, ij, orient.corient) prog.setMessage("%d/%d orientations" % (count, npts)) prog.setFraction(float(count) / npts) count += 1 if prog.stopped(): return None return od
def _read(self, tslfile, prog): data, hexgrid = self.readData(tslfile, prog) npts = len(data) rows = list(getrows(data)) # sorts data if hexgrid is None: # readData didn't set hexgrid. The grid is hexagonal if # the first two rows don't start at the same x. hexgrid = rows[0][0].position[0] != rows[1][0].position[0] if hexgrid: # Throw out every other row. reporter.warn("Converting hexagonal lattice to rectangular" " by discarding alternate rows.") rows = rows[::2] # discard odd numbered rows nx = len(rows[0]) ny = len(rows) count = 0 for row in rows: count += 1 if len(row) != nx: raise ooferror.ErrUserError( "Orientation map data appears to be incomplete.\n" "len(row 0)=%d len(row %d)=%d" % (nx, count, len(row))) # TSL puts the origin at the top left, so it's using a left # handed coordinate system! If flip_y==True, fix that. Also, # make sure there are no negative x or y values. ymax = rows[-1][0].position[1] ymin = rows[0][0].position[1] xmax = rows[0][-1].position[0] xmin = rows[0][0].position[0] for row in rows: for point in row: if self.flip_x: point.position[0] = xmax - point.position[0] else: point.position[0] = point.position[0] - xmin if self.flip_y: point.position[1] = ymax - point.position[1] else: point.position[1] = point.position[1] - ymin # If flipped, the rows are still ordered top to bottom, but # the coordinates increase bottom to top. # pixel size dx = abs(rows[0][1].position[0] - rows[0][0].position[0]) dy = abs(rows[0][0].position[1] - rows[1][0].position[1]) pxlsize = primitives.Point(dx, dy) width = abs(rows[0][0].position[0] - rows[0][-1].position[0]) height = abs(rows[0][0].position[1] - rows[-1][0].position[1]) # If we assume that the points are in the centers of the # pixels, then the actual physical size is one pixel bigger # than the range of the xy values. size = primitives.Point(width, height) + pxlsize od = orientmapdata.OrientMap(primitives.iPoint(nx, ny), size) prog.setMessage("%d/%d orientations" % (0, npts)) count = 0 for row in rows: for datum in row: ij = primitives.iPoint( int(round(datum.position[0] / pxlsize[0])), int(round(datum.position[1] / pxlsize[1]))) try: self.phaselists[datum.phasename].append(ij) except KeyError: self.phaselists[datum.phasename] = [ij] self.set_angle(od, ij, datum.euler()) prog.setMessage("%d/%d orientations" % (count, npts)) prog.setFraction(float(count) / npts) count += 1 if prog.stopped(): return None prog.finish() return od