Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "archive/tar"
- "fmt"
- "io"
- "log"
- "os"
- "strings"
- "encoding/binary"
- "bytes"
- )
- func bytesToFloat64(b []byte) float64 {
- var x float64
- buf := bytes.NewBuffer(b)
- binary.Read(buf, binary.LittleEndian, &x)
- return x
- }
- func bytesToInt(b []byte) int {
- buf := bytes.NewBuffer(b)
- switch len(b) {
- case 1:
- var x int8
- binary.Read(buf, binary.LittleEndian, &x)
- return int(x)
- case 2:
- var x int16
- binary.Read(buf, binary.LittleEndian, &x)
- return int(x)
- case 4:
- var x int32
- binary.Read(buf, binary.LittleEndian, &x)
- return int(x)
- default:
- var x int64
- binary.Read(buf, binary.LittleEndian, &x)
- return int(x)
- }
- }
- func read_partial_file(reader *tar.Reader) {
- var t []int
- var n []int
- var abxy []int
- var ablay []int
- var x []float64
- var y []float64
- var z []float64
- var ax []float64
- var ay []float64
- var az []float64
- var vx []float64
- var vy []float64
- var vz []float64
- var fx []float64
- var fy []float64
- var fz []float64
- lineBuf := make([]byte, 4)
- reader.Read(lineBuf)
- box_count := bytesToInt(lineBuf[0:4])
- var particles_count int;
- for i := 0; i < box_count; i++ {
- lineBuf := make([]byte, 20)
- reader.Read(lineBuf)
- bxyz2 := bytesToInt(lineBuf[0:4])
- blay := bytesToInt(lineBuf[4:8])
- breal := bytesToInt(lineBuf[8:12])
- msize := bytesToInt(lineBuf[12:16])
- csize := bytesToInt(lineBuf[16:20])
- fmt.Println(i, bxyz2, blay, breal, msize, csize, lineBuf )
- if i == 100 {
- break
- }
- if csize <= 0{
- continue
- }
- lineBuf = make([]byte, (4+4+8+8+8+8+8+8+8+8+8+8+8+8)*csize)
- reader.Read(lineBuf)
- for p := int(0); p < csize; p++ {
- buf_int := bytesToInt(lineBuf[4*p:4*(p+1)])
- t = append(t, buf_int)
- }
- lineBuf = lineBuf[4*csize:]
- for p := int(0); p < csize; p++ {
- buf_int := bytesToInt(lineBuf[4*p:4*(p+1)])
- n = append(n, buf_int)
- }
- lineBuf = lineBuf[4*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- x = append(x, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- y = append(y, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- z = append(z, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- ax = append(ax, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- ay = append(ay, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- az = append(az, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- vx = append(vx, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- vy = append(vy, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- vz = append(vz, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- fx = append(fx, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- fy = append(fy, buf_double)
- }
- lineBuf = lineBuf[8*csize:]
- for p := int(0); p < csize; p++ {
- buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
- fz = append(fz, buf_double)
- }
- for p := int(0); p < csize; p++ {
- abxy = append(abxy, bxyz2)
- }
- for p := int(0); p < csize; p++ {
- ablay = append(ablay, blay)
- }
- particles_count += csize
- }
- fmt.Println(particles_count)
- }
- func main() {
- path := os.Args[1]
- file, err := os.Open(path)
- if err != nil {
- fmt.Println("There is a problem with os.Open")
- os.Exit(-1)
- }
- // Небольшой костыль, связанный с тем, что next делается на 1 раз больше, чем файлов внутри
- tr := tar.NewReader(file)
- for {
- hdr, err := tr.Next()
- if err == io.EOF {
- break // End of archive
- }
- if err != nil {
- log.Fatal(err)
- }
- if strings.HasSuffix(hdr.Name, ".w2") == false {
- continue;
- }
- fmt.Printf("Reading file %s:\n", hdr.Name)
- read_partial_file(tr)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement