-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZMX.py
59 lines (46 loc) · 1.56 KB
/
ZMX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#Read/write ZMX files
#Ben Stabler, [email protected], 11/11/12
######################################################################
#load libraries
import sys, os.path, zipfile, struct, array
#write ZMX file
def writeZMX(fileName, zoneNames, mat):
#calculate numZones to remove trailing zeros
numZones = len(zoneNames)
#write header files
z = zipfile.ZipFile(fileName, "w")
z.writestr("_version", str(2))
z.writestr("_description", fileName)
z.writestr("_name", os.path.splitext(os.path.basename(fileName))[0])
z.writestr("_external column numbers", ",".join(zoneNames))
z.writestr("_external row numbers", ",".join(zoneNames))
z.writestr("_columns", str(numZones))
z.writestr("_rows", str(numZones))
#write rows, trim unused zones, big-endian floats
for i in range(1,numZones+1):
fileNameRow = "row_" + str(i)
data = struct.pack(">" + "f"*numZones,*mat[i-1][0:numZones])
z.writestr(fileNameRow, data)
#close connections
z.close()
#read ZMX file
def readZMX(fileName):
#read header files
z = zipfile.ZipFile(fileName, "r")
version = z.read("_version")
description = z.read("_description")
name = z.read("_name")
zoneNames = z.read("_external column numbers")
rowZoneNames = z.read("_external row numbers")
columns = z.read("_columns")
rows = int(z.read("_rows"))
#read rows, big-endian floats
mat = []
for i in range(1,rows+1):
fileNameRow = "row_" + str(i)
data = z.read(fileNameRow)
mat.append(struct.unpack(">" + "f"*rows,data))
#close connections
z.close()
#return matrix data, zone names, matrix name
return(mat, zoneNames, name)