-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_json.py
143 lines (131 loc) · 4.63 KB
/
parse_json.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Util functions for pyefie2d
from excitation import Planewave
import numpy as np
import json
from object import Object
from point2D import Point2D
from primitive_objects import Rectangle, Circle
from material import Material
from excitation import Planewave
from frequency import Frequency
def factory_rectangle(dictObj):
""" Create a rectangle object from parsed json file
"""
idObj = dictObj['id']
pMin = Point2D(dictObj['pmin'][0], dictObj['pmin'][1])
pMax = Point2D(dictObj['pmax'][0], dictObj['pmax'][1])
rectangle = Rectangle(idObj, pMin, pMax)
return rectangle
#--------------------------------------------
def factory_circle(dictObj):
""" Create a circle object from parsed json file
"""
idObj = dictObj['id']
center = Point2D(dictObj['center'][0], dictObj['center'][1])
radius = dictObj['radius']
circObj = Circle(idObj, radius, center)
return circObj
#--------------------------------------------
def factory_material(dictObj):
""" Create a material object from parsed json file
"""
idObj = dictObj['id']
epsr = dictObj['epsr']
sigma = dictObj['sigma']
material = Material(idObj, epsr, sigma)
return material
#--------------------------------------------
def factory_frequency(dictObj):
""" Create a frequency object from parsed json file
"""
idObj = dictObj['id']
val = dictObj['val']
freq = Frequency(idObj, val)
return freq
#--------------------------------------------
def factory_planewave(dicObj):
numPlws = dicObj['num_plws']
startPhi = dicObj['start_phi']
endPhi = dicObj['end_phi']
plw = Planewave(numPlws, startPhi, endPhi)
return plw
#--------------------------------------------
def create_object_to_material_map(fileJSON):
""" Given a json file, it creates a map between the object and the material
(as a dictionary)
"""
objToMatMap = {}
# Open the JSON file
with open(fileJSON, 'r') as json_file:
data = json.load(json_file)
# loop over all the geometry entities
for d in data['geometry']:
if d == 'circle' or d == 'rectangle':
for obj in data['geometry'][d]:
objToMatMap.update({obj['id']: obj['id_mat']})
return objToMatMap
#--------------------------------------------
def create_material_to_frequency_map(fileJSON):
""" Given a json file, it creates a map between the material and frequency
(as a dictionary)
"""
matToFreqMap = {}
# Open the JSON file
with open(fileJSON, 'r') as json_file:
data = json.load(json_file)
# loop over all the geometry entities
for obj in data['material']:
matToFreqMap.update({obj['id']: obj['id_freq']})
return matToFreqMap
#--------------------------------------------
def parse_json(fileJSON):
""" General function that parses the json file and creates all the maps
"""
# First loop over the geometry
vecObj = []
vecFreq = []
vecMat = []
vecInc = []
# Open the JSON file
with open(fileJSON, 'r') as json_file:
data = json.load(json_file)
# Resolution
resolution = data['resolution']
# Geometry
for d in data['geometry']:
if d == 'circle':
for obj in data['geometry'][d]:
circ = factory_circle(obj)
vecObj.append(circ)
elif d == 'rectangle':
for obj in data['geometry'][d]:
rec = factory_rectangle(obj)
vecObj.append(rec)
else:
raise ValueError("Wrong geometry entity")
# Frequency
for obj in data['frequency']:
freq = factory_frequency(obj)
vecFreq.append(freq)
# Material
for obj in data['material']:
mat = factory_material(obj)
vecMat.append(mat)
# Excitation
for d in data['excitation']:
if d == 'planewave':
for obj in data['excitation'][d]:
plw = factory_planewave(obj)
vecInc.append(plw)
else:
raise ValueError("Wrong excitation")
# Create the maps
objToMatMap = create_object_to_material_map(fileJSON)
matToFreqMap = create_material_to_frequency_map(fileJSON)
# Return all the info
return resolution, vecObj, vecInc, vecFreq, vecMat, objToMatMap, matToFreqMap
#--------------------------------------------
if __name__ == '__main__':
fileJSON = 'data/PetersonCylinder/PetersonCyl.json'
resolution, vecObj, vecInc, vecFreq, vecMat, objToMatMap, matToFreqMap = parse_json(
fileJSON)