def _read_ydata(self, f, data, column_headers): """Read the y data and column headers from spc file.""" n = self._header["fnsub"] subhdr_keys = ( "subflgs", "subexp", "subindx", "subtime", "subnext", "subnois", "subnpts", "subscan", "subwlevel", "subresv", ) if self._header["ftflgs"] & 1: y_width = 2 y_fmt = "h" divisor = 2**16 else: y_width = 4 y_fmt = "i" divisor = 2**32 if n * (y_width * self._pts + 32) > self._filesize - f.tell(): raise Core.StonerLoadError("No good, going to read too much data!") for j in range(n): # We have n sub-scans # Read the subheader and import into the main metadata dictionary as scan#:<subheader item> subhdr = struct.unpack(b"BBHfffIIf4s", f.read(32)) subheader = dict( zip(["scan" + str(j) + ":" + x for x in subhdr_keys], subhdr)) # Now read the y-data exponent = subheader["scan" + str(j) + ":subexp"] if int(exponent) & -128: # Data is unscaled direct floats ydata = np.array( struct.unpack(str2bytes(str(self._pts) + "f"), f.read(self._pts * y_width))) else: # Data is scaled by exponent yvals = struct.unpack(str2bytes(str(self._pts) + y_fmt), f.read(self._pts * y_width)) ydata = np.array(yvals, dtype="float64") * (2** exponent) / divisor data[:, j + 1] = ydata self._header = dict(self._header, **subheader) column_headers.append("Scan" + str(j) + ":" + self._yvars[self._header["fytype"]]) return data
def save(self, filename=None, **kargs): """Overrides the save method to allow KermitPNGFiles to be written out to disc Args: filename (string): Filename to save as (using the same rules as for the load routines) Keyword Arguments: deliminator (string): Record deliniminator (defaults to a comma) Returns: A copy of itself. """ if filename is None: filename = self.filename if filename is None or (isinstance(filename, bool) and not filename): # now go and ask for one filename = self.__file_dialog("w") metadata = PIL.PngImagePlugin.PngInfo() for k in self.metadata: parts = self.metadata.export(k).split("=") key = parts[0] val = str2bytes("=".join(parts[1:])) metadata.add_text(key, val) img = PIL.Image.fromarray(self.data) img.save(filename, "png", pnginfo=metadata) self.filename = filename return self
def _load(self, filename=None, *args, **kargs): """Load an OpenGDA file. Args: filename (string or bool): File to load. If None then the existing filename is used, if False, then a file dialog will be used. Returns: A copy of the itself after loading the data. """ if filename is None or not filename: self.get_filename("r") else: self.filename = filename i = 0 with io.open(self.filename, "r", errors="ignore", encoding="utf-8") as f: for i, line in enumerate(f): line = line.strip() if i == 0 and line != "&SRS": raise Core.StonerLoadError("Not a GDA File from Rasor ?" + str(line)) if "&END" in line: break parts = line.split("=") if len(parts) != 2: continue key = parts[0] value = parts[1].strip() self.metadata[key] = string_to_type(value) column_headers = f.readline().strip().split("\t") self.data = np.genfromtxt([str2bytes(l) for l in f], dtype="float", invalid_raise=False) self.column_headers = column_headers return self
def save(self, filename, **kargs): """Overrides the save method to allow KermitPNGFiles to be written out to disc Args: filename (string): Filename to save as (using the same rules as for the load routines) Keyword Arguments: deliminator (string): Record deliniminator (defaults to a comma) Returns: A copy of itself. """ if filename is None: filename = self.filename if filename is None or (isinstance(filename, bool) and not filename): # now go and ask for one filename = self.__file_dialog("w") metadata = PIL.PngImagePlugin.PngInfo() for k in self.metadata: parts = self.metadata.export(k).split("=") key = parts[0] val = str2bytes("=".join(parts[1:])) metadata.add_text(key, val) img = PIL.Image.fromarray(self.data) img.save(filename, "png", pnginfo=metadata) self.filename = filename return self
def _load(self, filename=None, *args, **kargs): """Load an OpenGDA file. Args: filename (string or bool): File to load. If None then the existing filename is used, if False, then a file dialog will be used. Returns: A copy of the itself after loading the data. """ if filename is None or not filename: self.get_filename("r") else: self.filename = filename i = 0 with io.open(self.filename, "r", errors="ignore", encoding="utf-8") as f: for i, line in enumerate(f): line = line.strip() if i == 0 and line != "&SRS": raise Core.StonerLoadError("Not a GDA File from Rasor ?" + str(line)) if "&END" in line: break parts = line.split("=") if len(parts) != 2: continue key = parts[0] value = parts[1].strip() self.metadata[key] = self.metadata.string_to_type(value) if python_v3: column_headers = f.readline().strip().split("\t") else: column_headers = f.next().strip().split("\t") self.data = np.genfromtxt([str2bytes(l) for l in f], dtype="float", invalid_raise=False) self.column_headers = column_headers return self
def _read_ydata(self, f, data, column_headers): """Read the y data and column headers from spc file.""" n = self._header["fnsub"] subhdr_keys = ( "subflgs", "subexp", "subindx", "subtime", "subnext", "subnois", "subnpts", "subscan", "subwlevel", "subresv", ) if self._header["ftflgs"] & 1: y_width = 2 y_fmt = "h" divisor = 2 ** 16 else: y_width = 4 y_fmt = "i" divisor = 2 ** 32 if n * (y_width * self._pts + 32) > self._filesize - f.tell(): raise Core.StonerLoadError("No good, going to read too much data!") for j in range(n): # We have n sub-scans # Read the subheader and import into the main metadata dictionary as scan#:<subheader item> subhdr = struct.unpack(b"BBHfffIIf4s", f.read(32)) subheader = dict(zip(["scan" + str(j) + ":" + x for x in subhdr_keys], subhdr)) # Now read the y-data exponent = subheader["scan" + str(j) + ":subexp"] if int(exponent) & -128: # Data is unscaled direct floats ydata = np.array(struct.unpack(str2bytes(str(self._pts) + "f"), f.read(self._pts * y_width))) else: # Data is scaled by exponent yvals = struct.unpack(str2bytes(str(self._pts) + y_fmt), f.read(self._pts * y_width)) ydata = np.array(yvals, dtype="float64") * (2 ** exponent) / divisor data[:, j + 1] = ydata self._header = dict(self._header, **subheader) column_headers.append("Scan" + str(j) + ":" + self._yvars[self._header["fytype"]]) return data
def _read_xdata(self, f): """Read the xdata from the spc file.""" self._pts = self._header["fnpts"] if self._header["ftflgs"] & 128: # We need to read some X Data if 4 * self._pts > self._filesize - f.tell(): raise Core.StonerLoadError("Trying to read too much data!") xvals = f.read(4 * self._pts) # I think storing X vals directly implies that each one is 4 bytes.... xdata = np.array(struct.unpack(str2bytes(str(self._pts) + "f"), xvals)) else: # Generate the X Data ourselves first = self._header["ffirst"] last = self._header["flast"] if self._pts > 1e6: # Something not right here ! raise Core.StonerLoadError("More than 1 million points requested. Bugging out now!") xdata = np.linspace(first, last, self._pts) return xdata
def _load(self, filename=None, *args, **kargs): """QD system file loader routine. Args: filename (string or bool): File to load. If None then the existing filename is used, if False, then a file dialog will be used. Returns: A copy of the itself after loading the data. """ if filename is None or not filename: self.get_filename("r") else: self.filename = filename setas = {} i = 0 with io.open(self.filename, "r", encoding="utf-8", errors="ignore") as f: # Read filename linewise for i, line in enumerate(f): line = line.strip() if i == 0 and line != "[Header]": raise Core.StonerLoadError("Not a Quantum Design File !") elif line == "[Header]" or line.startswith(";") or line == "": continue elif "[Data]" in line: break elif "," not in line: raise Core.StonerLoadError("No data in file!") parts = [x.strip() for x in line.split(",")] if parts[1].split(":")[0] == "SEQUENCE FILE": key = parts[1].split(":")[0].title() value = parts[1].split(":")[1] elif parts[0] == "INFO": if parts[1] == "APPNAME": parts[1], parts[2] = parts[2], parts[1] if len(parts) > 2: key = "{}.{}".format(parts[0], parts[2]) else: raise Core.StonerLoadError("No data in file!") key = key.title() value = parts[1] elif parts[0] in ["BYAPP", "FILEOPENTIME"]: key = parts[0].title() value = " ".join(parts[1:]) elif parts[0] == "FIELDGROUP": key = "{}.{}".format(parts[0], parts[1]).title() value = "[{}]".format(",".join(parts[2:])) elif parts[0] == "STARTUPAXIS": axis = parts[1][0].lower() setas[axis] = setas.get(axis, []) + [int(parts[2])] key = "Startupaxis-{}".format(parts[1].strip()) value = parts[2].strip() else: key = parts[0] + "," + parts[1] key = key.title() value = " ".join(parts[2:]) self.metadata[key] = self.metadata.string_to_type(value) else: raise Core.StonerLoadError("No data in file!") if "Byapp" not in self: raise Core.StonerLoadError("Not a Quantum Design File !") if python_v3: column_headers = f.readline().strip().split(",") if "," not in f.readline(): assert False raise Core.StonerLoadError("No data in file!") else: column_headers = f.next().strip().split(",") if "," not in f.next(): raise Core.StonerLoadError("No data in file!") data = np.genfromtxt([str2bytes(l) for l in f], dtype="float", delimiter=",", invalid_raise=False) if data.shape[1] != len(column_headers): # Trap for buggy QD software not giving ewnough columns of data data = np.append(data, np.ones((data.shape[0], len(column_headers) - data.shape[1])) * np.NaN, axis=1) self.data = data self.column_headers = column_headers s = self.setas for k in setas: for ix in setas[k]: s[ix - 1] = k self.setas = s return self
def _load(self, filename=None, *args, **kargs): """QD system file loader routine. Args: filename (string or bool): File to load. If None then the existing filename is used, if False, then a file dialog will be used. Returns: A copy of the itself after loading the data. """ if filename is None or not filename: self.get_filename("r") else: self.filename = filename setas = {} i = 0 with io.open(self.filename, "r", encoding="utf-8", errors="ignore") as f: # Read filename linewise for i, line in enumerate(f): line = line.strip() if i == 0 and line != "[Header]": raise Core.StonerLoadError("Not a Quantum Design File !") elif line == "[Header]" or line.startswith(";") or line == "": continue elif "[Data]" in line: break elif "," not in line: raise Core.StonerLoadError("No data in file!") parts = [x.strip() for x in line.split(",")] if parts[1].split(":")[0] == "SEQUENCE FILE": key = parts[1].split(":")[0].title() value = parts[1].split(":")[1] elif parts[0] == "INFO": if parts[1] == "APPNAME": parts[1], parts[2] = parts[2], parts[1] if len(parts) > 2: key = "{}.{}".format(parts[0], parts[2]) else: raise Core.StonerLoadError("No data in file!") key = key.title() value = parts[1] elif parts[0] in ["BYAPP", "FILEOPENTIME"]: key = parts[0].title() value = " ".join(parts[1:]) elif parts[0] == "FIELDGROUP": key = "{}.{}".format(parts[0], parts[1]).title() value = "[{}]".format(",".join(parts[2:])) elif parts[0] == "STARTUPAXIS": axis = parts[1][0].lower() setas[axis] = setas.get(axis, []) + [int(parts[2])] key = "Startupaxis-{}".format(parts[1].strip()) value = parts[2].strip() else: key = parts[0] + "," + parts[1] key = key.title() value = " ".join(parts[2:]) self.metadata[key] = string_to_type(value) else: raise Core.StonerLoadError("No data in file!") if "Byapp" not in self: raise Core.StonerLoadError("Not a Quantum Design File !") column_headers = f.readline().strip().split(",") data = np.genfromtxt([str2bytes(l) for l in f], dtype="float", delimiter=",", invalid_raise=False) if data.shape[0] == 0: raise Core.StonerLoadError("No data in file!") if data.shape[1] < len( column_headers ): # Trap for buggy QD software not giving ewnough columns of data data = np.append( data, np.ones( (data.shape[0], len(column_headers) - data.shape[1])) * np.NaN, axis=1) elif data.shape[1] > len(column_headers): # too much data data = data[:, :len(column_headers) - data.shape[1]] self.data = data self.column_headers = column_headers s = self.setas for k in setas: for ix in setas[k]: s[ix - 1] = k self.setas = s return self
def save(self, filename=None, compression=zf.ZIP_DEFLATED): """Overrides the save method to allow ZippedFile to be written out to disc (as a mininmalist output) Args: filename (string or zipfile.ZipFile instance): Filename to save as (using the same rules as for the load routines) Returns: A copy of itself. """ if filename is None: filename = self.filename if filename is None or (isinstance(filename, bool) and not filename): # now go and ask for one filename = self.__file_dialog('w') try: if isinstance(filename, string_types): #We;ve got a string filename if test_is_zip( filename ): # We can find an existing zip file somewhere in the filename zipfile, member = test_is_zip(filename) zipfile = zf.ZipFile(zipfile, "a") close_me = True elif path.exists( filename): # The fiule exists but isn't a zip file raise IOError( "{} Should either be a zip file or a new zip file". format(filename)) else: # Path doesn't exist, use extension of file part to find where the zip file should be parts = path.split(filename) for i, part in enumerate(parts): if path.splitext(part)[1].lower() == ".zip": break else: raise IOError( "Can't figure out where the zip file is in {}". format(filename)) zipfile = zf.ZipFile(path.join(*parts[:i + 1]), "w", compression, True) close_me = True member = path.join("/", *parts[i + 1:]) elif isinstance(filename, zf.ZipFile ): #Handle\ zipfile instance, opening if necessary if not filename.fp: filename = zf.ZipFile(filename.filename, 'a') close_me = True else: close_me = False zipfile = filename member = "" if member == "" or member == "/": # Is our file object a bare zip file - if so create a default member name if len(zipfile.namelist()) > 0: member = zipfile.namelist()[-1] self.filename = path.join(filename, member) else: member = "DataFile.txt" self.filename = filename zipfile.writestr(member, str2bytes(str(self))) if close_me: zipfile.close() except Exception: error = format_exc() try: zipfile.close() except Exception: pass raise IOError("Error saving zipfile\n{}".format(error)) return self