def _read_header_binary(self): data = self.infile.read(4) size_little, = unpack(b'<i', data) size_big, = unpack(b'>i', data) if size_big in [12, 8]: self._endian = b'>' size = size_big elif size_little in [8, 12]: self._endian = b'<' size = size_little else: self._rewind() self.show(100) raise RuntimeError('unknown endian') self.n += 4 data = self.infile.read(size) self.n += size so4 = size // 4 # size over 4 if so4 == 3: (npoints, nelements, nresults) = unpack(self._endian + b('iii'), data) self.log.info("npoints=%s nelements=%s nresults=%s" % (npoints, nelements, nresults)) elif so4 == 2: (npoints, nelements) = unpack(self._endian + b('ii'), data) nresults = 0 self.log.info("npoints=%s nelements=%s" % (npoints, nelements)) else: self._rewind() self.show(100) raise RuntimeError('in the wrong spot...endian...size/4=%s' % so4) self.infile.read(8) # end of first block, start of second block return (npoints, nelements, nresults)
def _write_regions(self, outfile, regions, is_binary): """writes the regions""" if is_binary: fmt = self._endian + b('i') four = pack(fmt, 4) outfile.write(four) nregions = len(regions) fmt = self._endian + b('%ii' % nregions) ints = pack(fmt, *regions) outfile.write(ints) outfile.write(four) else: fmt = b'%i' np.savetxt(outfile, regions, fmt)
def _write_points(self, outfile, points, is_binary, float_fmt='%6.6f'): """writes the points""" if is_binary: four = pack(self._endian + b('i'), 4) outfile.write(four) npoints = points.shape[0] fmt = self._endian + b('%if' % (npoints * 3)) floats = pack(fmt, *np.ravel(points)) outfile.write(floats) outfile.write(four) else: if isinstance(float_fmt, bytes_type): fmt_ascii = float_fmt else: fmt_ascii = float_fmt.encode('latin1') np.savetxt(outfile, points, fmt_ascii)
def _read_points_binary(self, npoints): """reads the xyz points""" size = npoints * 12 # 12=3*4 all the points data = self.infile.read(size) dtype = np.dtype(self._endian + b('f4')) points = np.frombuffer(data, dtype=dtype).reshape((npoints, 3)).copy() self.infile.read(8) # end of second block, start of third block return points
def _write_header(self, outfile, points, elements, is_loads, is_binary=False): """ writes the cart3d header Without results --------------- npoints nelements With results ------------ npoints nelements nresults """ npoints = points.shape[0] nelements = elements.shape[0] if is_binary: if is_loads: fmt = self._endian + b('iiiii') msg = pack(fmt, 3 * 4, npoints, nelements, 6, 4) else: fmt = self._endian + b('iiii') msg = pack(fmt, 2 * 4, npoints, nelements, 4) int_fmt = None else: # this is ASCII data if is_loads: msg = b("%i %i 6\n" % (npoints, nelements)) else: msg = b("%i %i\n" % (npoints, nelements)) # take the max value, string it, and length it # so 123,456 is length 6 int_fmt = b('%%%si' % len(str(nelements))) outfile.write(msg) return int_fmt
def _write_elements(self, outfile, elements, is_binary, int_fmt='%6i'): """writes the triangles""" min_e = elements.min() assert min_e == 0, 'min(elements)=%s' % min_e if is_binary: fmt = self._endian + b('i') four = pack(fmt, 4) outfile.write(four) nelements = elements.shape[0] fmt = self._endian + b('%ii' % (nelements * 3)) ints = pack(fmt, *np.ravel(elements + 1)) outfile.write(ints) outfile.write(four) else: if isinstance(int_fmt, bytes_type): fmt_ascii = int_fmt else: fmt_ascii = int_fmt.encode('latin1') np.savetxt(outfile, elements + 1, fmt_ascii)
def _write_elements(self, outfile, elements, is_binary, int_fmt='%6i'): """writes the triangles""" min_e = elements.min() assert min_e == 1, 'min(elements)=%s' % min_e if is_binary: fmt = self._endian + b('i') four = pack(fmt, 4) outfile.write(four) nelements = elements.shape[0] fmt = self._endian + b('%ii' % (nelements * 3)) ints = pack(fmt, *np.ravel(elements)) outfile.write(ints) outfile.write(four) else: if isinstance(int_fmt, bytes_type): fmt_ascii = int_fmt else: fmt_ascii = int_fmt.encode('latin1') np.savetxt(outfile, elements, fmt_ascii)
def _read_elements_binary(self, nelements): """reads the triangles""" size = nelements * 12 # 12=3*4 all the elements data = self.infile.read(size) dtype = np.dtype(self._endian + b('i4')) elements = np.fromstring(data, dtype=dtype).reshape((nelements, 3)) self.infile.read( 8) # end of third (element) block, start of regions (fourth) block assert elements.min() == 1, elements.min() return elements - 1
def _write_header(self, outfile, points, elements, is_loads, is_binary=False): """ writes the cart3d header Without results --------------- npoints nelements With results ------------ npoints nelements nresults """ npoints = points.shape[0] nelements = elements.shape[0] if is_binary: if is_loads: fmt = self._endian + b('iiiii') msg = pack(fmt, 3*4, npoints, nelements, 6, 4) else: fmt = self._endian + b('iiii') msg = pack(fmt, 2*4, npoints, nelements, 4) int_fmt = None else: # this is ASCII data if is_loads: msg = b("%i %i 6\n" % (npoints, nelements)) else: msg = b("%i %i\n" % (npoints, nelements)) # take the max value, string it, and length it # so 123,456 is length 6 int_fmt = b('%%%si' % len(str(nelements))) outfile.write(msg) return int_fmt