// Writes data in OMF Binary 4 format func writeOVF1Binary4(out io.Writer, array *data.Slice) (err error) { data := array.Tensors() gridsize := array.Size() var bytes []byte // OOMMF requires this number to be first to check the format var controlnumber float32 = OVF_CONTROL_NUMBER_4 // Conversion form float32 [4]byte in big-endian // Inlined for performance, terabytes of data will pass here... bytes = (*[4]byte)(unsafe.Pointer(&controlnumber))[:] bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0] // swap endianess _, err = out.Write(bytes) ncomp := array.NComp() for iz := 0; iz < gridsize[Z]; iz++ { for iy := 0; iy < gridsize[Y]; iy++ { for ix := 0; ix < gridsize[X]; ix++ { for c := 0; c < ncomp; c++ { // dirty conversion from float32 to [4]byte bytes = (*[4]byte)(unsafe.Pointer(&data[c][iz][iy][ix]))[:] bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0] out.Write(bytes) } } } } return }
func writeOVF2DataBinary4(out io.Writer, array *data.Slice) { //w.count(w.out.Write((*(*[1<<31 - 1]byte)(unsafe.Pointer(&list[0])))[0 : 4*len(list)])) // (shortcut) data := array.Tensors() size := array.Size() var bytes []byte // OOMMF requires this number to be first to check the format var controlnumber float32 = OVF_CONTROL_NUMBER_4 bytes = (*[4]byte)(unsafe.Pointer(&controlnumber))[:] out.Write(bytes) ncomp := array.NComp() for iz := 0; iz < size[Z]; iz++ { for iy := 0; iy < size[Y]; iy++ { for ix := 0; ix < size[X]; ix++ { for c := 0; c < ncomp; c++ { bytes = (*[4]byte)(unsafe.Pointer(&data[c][iz][iy][ix]))[:] out.Write(bytes) } } } } }
func dumpGnuplot(f *data.Slice, m data.Meta, out io.Writer) { buf := bufio.NewWriter(out) defer buf.Flush() data := f.Tensors() cellsize := m.CellSize // If no cell size is set, use generic cell index. if cellsize == [3]float64{0, 0, 0} { cellsize = [3]float64{1, 1, 1} } ncomp := f.NComp() for iz := range data[0] { z := float64(iz) * cellsize[Z] for iy := range data[0][iz] { y := float64(iy) * cellsize[Y] for ix := range data[0][iz][iy] { x := float64(ix) * cellsize[X] fmt.Fprint(buf, x, DELIM, y, DELIM, z, DELIM) for c := 0; c < ncomp-1; c++ { fmt.Fprint(buf, data[c][iz][iy][ix], DELIM) } fmt.Fprint(buf, data[ncomp-1][iz][iy][ix]) fmt.Fprint(buf, "\n") } fmt.Fprint(buf, "\n") } } }
func readOVF1DataBinary8(in io.Reader, t *data.Slice) { size := t.Size() data := t.Tensors() // OOMMF requires this number to be first to check the format var controlnumber float64 // OVF 1.0 is network byte order (MSB) binary.Read(in, binary.BigEndian, &controlnumber) if controlnumber != OVF_CONTROL_NUMBER_8 { panic("invalid OVF1 control number: " + fmt.Sprint(controlnumber)) } var tmp float64 for iz := 0; iz < size[Z]; iz++ { for iy := 0; iy < size[Y]; iy++ { for ix := 0; ix < size[X]; ix++ { for c := 0; c < 3; c++ { err := binary.Read(in, binary.BigEndian, &tmp) if err != nil { panic(err) } data[c][iz][iy][ix] = float32(tmp) } } } } }
// Writes the data. func (w *writer) writeData(array *data.Slice) { data := array.Tensors() size := array.Size() ncomp := array.NComp() for c := 0; c < ncomp; c++ { for iz := 0; iz < size[2]; iz++ { for iy := 0; iy < size[1]; iy++ { for ix := 0; ix < size[0]; ix++ { w.writeFloat32(data[c][iz][iy][ix]) } } } } }
// comma-separated values func dumpCSV(f *data.Slice, info data.Meta, out io.Writer) { f2 := ", " + *flag_format a := f.Tensors() for _, a := range a { for _, a := range a { for _, a := range a { fmt.Fprintf(out, *flag_format, a[0]) for i := 1; i < len(a); i++ { fmt.Fprintf(out, f2, a[i]) } fmt.Fprintln(out) } fmt.Fprintln(out) } } }
// read data block in text format, for OVF1 and OVF2 func readOVFDataText(in io.Reader, t *data.Slice) { size := t.Size() data := t.Tensors() for iz := 0; iz < size[Z]; iz++ { for iy := 0; iy < size[Y]; iy++ { for ix := 0; ix < size[X]; ix++ { for c := 0; c < t.NComp(); c++ { _, err := fmt.Fscan(in, &data[c][iz][iy][ix]) if err != nil { panic(err) } } } } } }
// write data block in text format, for OVF1 and OVF2 func writeOVFText(out io.Writer, tens *data.Slice) (err error) { data := tens.Tensors() gridsize := tens.Size() ncomp := tens.NComp() // Here we loop over X,Y,Z, not Z,Y,X, because // internal in C-order == external in Fortran-order for iz := 0; iz < gridsize[Z]; iz++ { for iy := 0; iy < gridsize[Y]; iy++ { for ix := 0; ix < gridsize[X]; ix++ { for c := 0; c < ncomp; c++ { _, err = fmt.Fprint(out, data[c][iz][iy][ix], " ") } _, err = fmt.Fprint(out, "\n") } } } return }
func readOVF2DataBinary8(in io.Reader, array *data.Slice) { size := array.Size() data := array.Tensors() // OOMMF requires this number to be first to check the format controlnumber := readFloat64(in) if controlnumber != OVF_CONTROL_NUMBER_8 { panic("invalid OVF2 control number: " + fmt.Sprint(controlnumber)) } ncomp := array.NComp() for iz := 0; iz < size[Z]; iz++ { for iy := 0; iy < size[Y]; iy++ { for ix := 0; ix < size[X]; ix++ { for c := 0; c < ncomp; c++ { data[c][iz][iy][ix] = float32(readFloat64(in)) } } } } }
func dumpJSON(f *data.Slice, info data.Meta, out io.Writer) { w := json.NewEncoder(out) w.Encode(f.Tensors()) }
// does not output to out, just prints to stdout func show(f *data.Slice, info data.Meta, out io.Writer) { fmt.Println(info) util.Fprintf(os.Stdout, *flag_format, f.Tensors()) }
func writeVTKCellData(out io.Writer, q *data.Slice, meta data.Meta, dataformat string) (err error) { N := q.NComp() data := q.Tensors() switch N { case 1: fmt.Fprintf(out, "\t\t\t<PointData Scalars=\"%s\">\n", meta.Name) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", meta.Name, N, dataformat) case 3: fmt.Fprintf(out, "\t\t\t<PointData Vectors=\"%s\">\n", meta.Name) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", meta.Name, N, dataformat) case 6, 9: fmt.Fprintf(out, "\t\t\t<PointData Tensors=\"%s\">\n", meta.Name) fmt.Fprintf(out, "\t\t\t\t<DataArray type=\"Float32\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"%s\">\n\t\t\t\t\t", meta.Name, 9, dataformat) // must be 9! default: log.Fatalf("vtk: cannot handle %v components", N) } gridsize := q.Size() switch dataformat { case "ascii": for k := 0; k < gridsize[2]; k++ { for j := 0; j < gridsize[1]; j++ { for i := 0; i < gridsize[0]; i++ { // if symmetric tensor manage it appart to write the full 9 components if N == 6 { fmt.Fprint(out, data[0][k][j][i], " ") fmt.Fprint(out, data[1][k][j][i], " ") fmt.Fprint(out, data[2][k][j][i], " ") fmt.Fprint(out, data[1][k][j][i], " ") fmt.Fprint(out, data[3][k][j][i], " ") fmt.Fprint(out, data[4][k][j][i], " ") fmt.Fprint(out, data[2][k][j][i], " ") fmt.Fprint(out, data[4][k][j][i], " ") fmt.Fprint(out, data[5][k][j][i], " ") } else { for c := 0; c < N; c++ { fmt.Fprint(out, data[c][k][j][i], " ") } } } } } case "binary": // Inlined for performance, terabytes of data will pass here... buffer := new(bytes.Buffer) for k := 0; k < gridsize[2]; k++ { for j := 0; j < gridsize[1]; j++ { for i := 0; i < gridsize[0]; i++ { // if symmetric tensor manage it appart to write the full 9 components if N == 6 { binary.Write(buffer, binary.LittleEndian, data[0][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[1][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[2][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[1][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[3][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[4][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[2][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[4][k][j][i]) binary.Write(buffer, binary.LittleEndian, data[5][k][j][i]) } else { for c := 0; c < N; c++ { binary.Write(buffer, binary.LittleEndian, data[c][k][j][i]) } } } } } b64len := uint32(len(buffer.Bytes())) bufLen := new(bytes.Buffer) binary.Write(bufLen, binary.LittleEndian, b64len) base64out := base64.NewEncoder(base64.StdEncoding, out) base64out.Write(bufLen.Bytes()) base64out.Write(buffer.Bytes()) base64out.Close() default: panic(fmt.Errorf("vtk: illegal data format " + dataformat + ". Options are: ascii, binary")) } fmt.Fprintln(out, "\n\t\t\t\t</DataArray>") fmt.Fprintln(out, "\t\t\t</PointData>") return }