def convert_starlab_string_to_xml_string(self, string): xml_lines = ['<?xml version="1.0"?>\n<System>'] openpar = 0 closepar = 0 for line in string.splitlines(): line = line.rstrip() if line.startswith("("): openpar += 1 line = line.lstrip("(") newline = "<"+line+">" elif line.startswith(")"): closepar +=1 line = line.lstrip(")") newline = "</"+line+">" else: newline = self.convert_storyline(line) xml_lines.append(newline) xml_lines.append("</System>") if closepar!=openpar: raise base.IoException("\nConversion failure, parenthesis mismatch. Return: no output string written\n") return '\n'.join(xml_lines)
def get_item_header(self): magic_number = self.read_magic_number() if magic_number is None: return (None, None, None, None) mustswap = False if magic_number == self.reversed_SingMagic: magic_number == self.SingMagic mustswap = True elif magic_number == self.reversed_PlurMagic: magic_number == self.PlurMagic mustswap = True if not (magic_number == self.SingMagic or magic_number == self.PlurMagic): raise base.IoException("Item does not have a valid header") typecharacter = self.read_string() if not typecharacter == TesItem.typecharacter: tagstring = self.read_string() else: tagstring = '' if magic_number == self.PlurMagic: dim = self.read_array('i') if mustswap: dim.byteswap() dim = dim.tolist() else: dim = [1] return (typecharacter, tagstring, dim, mustswap)
def read_rows(self): values = [[] for x in range(len(self.attribute_names))] number_of_particles = 0 keys = [] self.set = None string_converters = list( map(self._new_converter_from_string_to_dtype, self.attribute_dtypes)) units_with_dtype = list( map(core.unit_with_specific_dtype, self.attribute_types, self.attribute_dtypes)) while not self.cursor.is_at_end() and not self.cursor.line( ).startswith(self.footer_prefix_string): columns = self.split_into_columns(self.cursor.line()) if len(columns) > 0: if self.key_in_column >= 0: if len(columns) != len(self.attribute_names) + 1: raise base.IoException( "Number of values on line '{0}' is {1}, expected {2}" .format(self.cursor.line(), len(columns), len(self.attribute_names))) key = self.convert_string_to_long( columns[self.key_in_column]) keys.append(key) del columns[self.key_in_column] if len(columns) != len(self.attribute_names): raise base.IoException( "Number of values on line '{0}' is {1}, expected {2}". format(self.cursor.line(), len(columns), len(self.attribute_names))) list( map( lambda value_string, list_of_values, conv: list_of_values.append(conv(value_string)), columns, values, string_converters)) number_of_particles += 1 self.cursor.forward() if number_of_particles >= self.maximum_number_of_lines_buffered: quantities = list( map( lambda value, unit, dtype: unit.new_quantity(value) if not unit is None else (numpy.asarray(value, dtype=dtype) if not dtype is None else value), values, units_with_dtype, self.attribute_dtypes)) if self.set is None: self.set = self.new_set(number_of_particles, keys=keys) self.set.set_values_in_store( self.set.get_all_indices_in_store(), self.attribute_names, quantities) else: tmp_set = self.new_set(number_of_particles, keys=keys) tmp_set.set_values_in_store( tmp_set.get_all_indices_in_store(), self.attribute_names, quantities) self.set.add_particles(tmp_set) number_of_particles = 0 keys = [] values = [[] for x in range(len(self.attribute_names))] if number_of_particles > 0: quantities = list( map( lambda value, unit, dtype: unit.new_quantity(value) if not unit is None else (numpy.asarray(value, dtype=dtype) if not dtype is None else value), values, units_with_dtype, self.attribute_dtypes)) if self.set is None: self.set = self.new_set(number_of_particles, keys=keys) self.set.set_values_in_store( self.set.get_all_indices_in_store(), self.attribute_names, quantities) else: tmp_set = self.new_set(number_of_particles, keys=keys) tmp_set.set_values_in_store(tmp_set.get_all_indices_in_store(), self.attribute_names, quantities) self.set.add_particles(tmp_set) elif self.set is None: self.set = self.new_set(0) self.cursor.forward()