Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "archive/tar"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "strings"
  10. "encoding/binary"
  11. "bytes"
  12.  
  13. )
  14.  
  15. func bytesToFloat64(b []byte) float64 {
  16. var x float64
  17. buf := bytes.NewBuffer(b)
  18. binary.Read(buf, binary.LittleEndian, &x)
  19. return x
  20. }
  21.  
  22. func bytesToInt(b []byte) int {
  23. buf := bytes.NewBuffer(b)
  24.  
  25. switch len(b) {
  26. case 1:
  27. var x int8
  28. binary.Read(buf, binary.LittleEndian, &x)
  29. return int(x)
  30. case 2:
  31. var x int16
  32. binary.Read(buf, binary.LittleEndian, &x)
  33. return int(x)
  34. case 4:
  35. var x int32
  36. binary.Read(buf, binary.LittleEndian, &x)
  37. return int(x)
  38. default:
  39. var x int64
  40. binary.Read(buf, binary.LittleEndian, &x)
  41. return int(x)
  42. }
  43. }
  44.  
  45.  
  46.  
  47. func read_partial_file(reader *tar.Reader) {
  48.  
  49. var t []int
  50. var n []int
  51. var abxy []int
  52. var ablay []int
  53.  
  54. var x []float64
  55. var y []float64
  56. var z []float64
  57.  
  58. var ax []float64
  59. var ay []float64
  60. var az []float64
  61.  
  62. var vx []float64
  63. var vy []float64
  64. var vz []float64
  65.  
  66. var fx []float64
  67. var fy []float64
  68. var fz []float64
  69.  
  70. lineBuf := make([]byte, 4)
  71. reader.Read(lineBuf)
  72.  
  73. box_count := bytesToInt(lineBuf[0:4])
  74. var particles_count int;
  75.  
  76. for i := 0; i < box_count; i++ {
  77.  
  78. lineBuf := make([]byte, 20)
  79. reader.Read(lineBuf)
  80.  
  81. bxyz2 := bytesToInt(lineBuf[0:4])
  82. blay := bytesToInt(lineBuf[4:8])
  83. breal := bytesToInt(lineBuf[8:12])
  84. msize := bytesToInt(lineBuf[12:16])
  85. csize := bytesToInt(lineBuf[16:20])
  86. fmt.Println(i, bxyz2, blay, breal, msize, csize, lineBuf )
  87. if i == 100 {
  88. break
  89. }
  90. if csize <= 0{
  91. continue
  92. }
  93. lineBuf = make([]byte, (4+4+8+8+8+8+8+8+8+8+8+8+8+8)*csize)
  94.  
  95. reader.Read(lineBuf)
  96.  
  97. for p := int(0); p < csize; p++ {
  98. buf_int := bytesToInt(lineBuf[4*p:4*(p+1)])
  99. t = append(t, buf_int)
  100.  
  101. }
  102. lineBuf = lineBuf[4*csize:]
  103. for p := int(0); p < csize; p++ {
  104. buf_int := bytesToInt(lineBuf[4*p:4*(p+1)])
  105. n = append(n, buf_int)
  106.  
  107. }
  108. lineBuf = lineBuf[4*csize:]
  109. for p := int(0); p < csize; p++ {
  110. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  111. x = append(x, buf_double)
  112.  
  113. }
  114. lineBuf = lineBuf[8*csize:]
  115. for p := int(0); p < csize; p++ {
  116. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  117. y = append(y, buf_double)
  118.  
  119. }
  120. lineBuf = lineBuf[8*csize:]
  121. for p := int(0); p < csize; p++ {
  122. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  123. z = append(z, buf_double)
  124.  
  125. }
  126. lineBuf = lineBuf[8*csize:]
  127. for p := int(0); p < csize; p++ {
  128. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  129. ax = append(ax, buf_double)
  130.  
  131. }
  132. lineBuf = lineBuf[8*csize:]
  133. for p := int(0); p < csize; p++ {
  134. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  135. ay = append(ay, buf_double)
  136.  
  137. }
  138. lineBuf = lineBuf[8*csize:]
  139. for p := int(0); p < csize; p++ {
  140. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  141. az = append(az, buf_double)
  142.  
  143. }
  144. lineBuf = lineBuf[8*csize:]
  145. for p := int(0); p < csize; p++ {
  146. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  147. vx = append(vx, buf_double)
  148.  
  149. }
  150. lineBuf = lineBuf[8*csize:]
  151. for p := int(0); p < csize; p++ {
  152. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  153. vy = append(vy, buf_double)
  154.  
  155. }
  156. lineBuf = lineBuf[8*csize:]
  157. for p := int(0); p < csize; p++ {
  158. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  159. vz = append(vz, buf_double)
  160.  
  161. }
  162. lineBuf = lineBuf[8*csize:]
  163. for p := int(0); p < csize; p++ {
  164. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  165. fx = append(fx, buf_double)
  166.  
  167. }
  168. lineBuf = lineBuf[8*csize:]
  169. for p := int(0); p < csize; p++ {
  170. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  171. fy = append(fy, buf_double)
  172.  
  173. }
  174. lineBuf = lineBuf[8*csize:]
  175. for p := int(0); p < csize; p++ {
  176. buf_double := bytesToFloat64(lineBuf[8*p:8*(p+1)])
  177. fz = append(fz, buf_double)
  178.  
  179. }
  180. for p := int(0); p < csize; p++ {
  181. abxy = append(abxy, bxyz2)
  182.  
  183. }
  184. for p := int(0); p < csize; p++ {
  185. ablay = append(ablay, blay)
  186.  
  187. }
  188.  
  189. particles_count += csize
  190.  
  191. }
  192. fmt.Println(particles_count)
  193.  
  194. }
  195.  
  196. func main() {
  197. path := os.Args[1]
  198.  
  199. file, err := os.Open(path)
  200.  
  201.  
  202. if err != nil {
  203. fmt.Println("There is a problem with os.Open")
  204. os.Exit(-1)
  205. }
  206.  
  207.  
  208. // Небольшой костыль, связанный с тем, что next делается на 1 раз больше, чем файлов внутри
  209. tr := tar.NewReader(file)
  210.  
  211. for {
  212. hdr, err := tr.Next()
  213. if err == io.EOF {
  214. break // End of archive
  215. }
  216. if err != nil {
  217. log.Fatal(err)
  218. }
  219.  
  220. if strings.HasSuffix(hdr.Name, ".w2") == false {
  221. continue;
  222. }
  223.  
  224. fmt.Printf("Reading file %s:\n", hdr.Name)
  225. read_partial_file(tr)
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement