This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasetparser.py
executable file
·114 lines (100 loc) · 3.76 KB
/
datasetparser.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""Reads all the headers in a folder and creates a vola index.
@author Jonathan Byrne
@copyright 2018 Intel Ltd (see LICENSE file).
"""
from __future__ import print_function
import argparse
import glob
import os
import struct
import json
def main():
"""Read the headers, calc the centroids and output."""
parser = argparse.ArgumentParser()
parser.add_argument("pathname",
help="the path containing volume files", type=str)
args = parser.parse_args()
dirname = args.pathname.rstrip('/')
dataset = os.path.basename(dirname)
volaname = os.path.join(dirname, dataset) + ".vola"
vol = os.path.join(dirname, "*.vol")
infofile = os.path.join(dirname, "info.json")
print("Processing folder:", dirname, " output:", volaname)
files = []
tminx, tminy, tminz = float('inf'), float('inf'), float('inf')
tmaxx, tmaxy, tmaxz = float('-inf'), float('-inf'), float('-inf')
filenames = glob.glob(vol)
hdr = {}
for filename in filenames:
with open(filename, "rb") as f:
hdr['headersize'] = struct.unpack('I', f.read(4))[0]
hdr['version'] = struct.unpack('H', f.read(2))[0]
hdr['mode'] = struct.unpack('B', f.read(1))[0]
hdr['depth'] = struct.unpack('B', f.read(1))[0]
hdr['nbits'] = struct.unpack('I', f.read(4))[0]
hdr['crs'] = struct.unpack('I', f.read(4))[0]
hdr['lat'] = struct.unpack('d', f.read(8))[0]
hdr['lon'] = struct.unpack('d', f.read(8))[0]
minx = struct.unpack('d', f.read(8))[0]
miny = struct.unpack('d', f.read(8))[0]
minz = struct.unpack('d', f.read(8))[0]
maxx = struct.unpack('d', f.read(8))[0]
maxy = struct.unpack('d', f.read(8))[0]
maxz = struct.unpack('d', f.read(8))[0]
if minx < tminx:
tminx = minx
if miny < tminy:
tminy = miny
if minz < tminz:
tminz = minz
if maxx > tmaxx:
tmaxx = maxx
if maxy > tmaxy:
tmaxy = maxy
if maxz > tmaxz:
tmaxz = maxz
bbox = [minx, miny, minz, maxx, maxy, maxz]
sides = [maxx - minx, maxy - miny, maxz - minz]
centroid = ((minx + maxx) / 2, (miny + maxy) / 2, (minz + maxz) / 2)
files.append({
'filename': filename,
'bbox': bbox,
'centroid': centroid,
'sides': sides,
'crs': hdr['crs'],
'lat': hdr['lat'],
'lon': hdr['lon']
})
if not os.path.isfile(infofile):
print("Missing attribution info file!! Attribution is required")
exit()
else:
with open(infofile) as data_file:
infodata = json.load(data_file)
if len(infodata['license']) < 5:
print("No license information!! License is required")
exit()
vola = {}
print("Depth:", hdr['depth'])
vola['dataset'] = infodata['dataset']
vola['info'] = infodata['info']
vola['url'] = infodata['url']
vola['author'] = infodata['author']
vola['authorurl'] = infodata['authorurl']
vola['license'] = infodata['license']
vola['licenseurl'] = infodata['licenseurl']
vola['files'] = files
vola['depth'] = hdr['depth']
vola['nbits'] = hdr['nbits']
vola['crs'] = hdr['crs']
vola['mode'] = hdr['mode']
vola['bbox'] = [tminx, tminy, tminz, tmaxx, tmaxy, tmaxz]
vola['sides'] = [tmaxx - tminx, tmaxy - tminy, tmaxz - tminz]
vola['centroid'] = ((tminx + tmaxx) / 2, (tminy + tmaxy) / 2,
(tminz + tmaxz) / 2)
volafile = open(volaname, 'w')
volafile.write(json.dumps(vola, sort_keys=True, indent=2))
volafile.close()
if __name__ == '__main__':
main()