forked from openPSTD/openPSTD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpstd_data.py
More file actions
164 lines (151 loc) · 5.84 KB
/
pstd_data.py
File metadata and controls
164 lines (151 loc) · 5.84 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
########################################################################
# #
# This file is part of openPSTD. #
# #
# openPSTD is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# openPSTD is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with openPSTD. If not, see <http://www.gnu.org/licenses/>. #
# #
########################################################################
"""
Utility classes that interpret output data from the openPSTD simulation process
Author: Thomas Krijnen
"""
import io
import os
import json
import array
import struct
import pickle
import pickletools
from array import array
class PickleStream:
def __init__(self, f):
self.w = io.BytesIO()
self.r = f
def read(self,n):
a = self.r.read(n)
self.w.write(a)
if n == 1: return a[0]
if n == 2: return struct.unpack("<H",a)[0]
if n == 4: return struct.unpack("<i",a)[0]
else: return None
def readline(self):
self.w.write(self.r.readline())
def get(self):
while True:
code = chr(self.read(1))
if not code:
raise EOFError
opcode = pickletools.code2op[code]
if opcode.arg is not None:
n = opcode.arg.n
if n > 0:
self.read(n)
elif n == pickletools.UP_TO_NEWLINE:
self.readline()
elif n == pickletools.TAKEN_FROM_ARGUMENT1:
self.read(self.read(1))
elif n == pickletools.TAKEN_FROM_ARGUMENT4:
self.read(self.read(4))
if code == '.':
break
self.w.seek(0)
return pickle.load(self.w)
class Listener:
ob_to_listener = {}
def __init__(self, receiver):
self.array = array('f')
self.fn = os.path.join(receiver.pstd_output_directory, 'rec-%s.bin'%receiver.pstd_output_id)
self.itemsize = struct.calcsize('f')
self.opened = False
self.receiver_object = receiver
self.file = None
self.try_open()
Listener.ob_to_listener[receiver] = self
@classmethod
def getFromObj(cls, arg):
return cls.ob_to_listener.get(arg, None)
@classmethod
def closeAll(cls):
for ob, listener in cls.ob_to_listener.items():
if listener.file:
listener.file.close()
listener.file = None
def try_open(self):
try:
self.file = open(self.fn, 'rb')
self.opened = True
except: pass
return self.opened
def read(self):
if not self.opened:
if not self.try_open():
return False
d = self.file.read()
has_data = len(d) >= self.itemsize
if has_data:
self.array.extend(struct.unpack('%df'%(len(d) // self.itemsize), d))
return has_data
class DomainInfo:
class SubDomainInfo:
def __init__(self, **di):
if di:
self.data = {'id': di['id'],
'nx': di['nx'],
'nz': di['nz'],
'cx': di['center'][0],
'cz': di['center'][1],
'sx': di['size'][0],
'sz': di['size'][1]}
def __getattr__(self, k):
return self.data[k]
@staticmethod
def restore(data):
d = DomainInfo.SubDomainInfo()
d.data = data
return d
def __init__(self, pstd_data=None, ll=None):
self.subdomains = []
if pstd_data and ll:
dx, dz = pstd_data['dx'], pstd_data['dz'],
for domain in pstd_data['domains']:
self.subdomains.append(self.SubDomainInfo(id=domain['id'],
nx=domain['Nx'],
nz=domain['Nz'],
center=(domain['center'][0] + ll.x, domain['center'][1] + ll.y, 0),
size=(domain['size'][0] - dx, domain['size'][1] - dz, 0)))
def __repr__(self):
return json.dumps([d.data for d in self.subdomains])
@staticmethod
def restore(str):
d = DomainInfo()
d.subdomains = [DomainInfo.SubDomainInfo.restore(d) for d in json.loads(str)]
return d
pressure_level_cache = {}
float_size = struct.calcsize('f')
def read_pressure_level_data(dir, id, frame):
tup = (dir, id, frame)
data = pressure_level_cache.get(tup, None)
if data: return data
fn = os.path.join(dir,"%s-%06d.bin"%(id, frame))
if os.path.exists(fn):
f = open(fn, "rb")
f.seek(0, 2)
fsize = f.tell()
n = fsize // float_size
f.seek(0, 0)
data = array('f')
data.fromfile(f, n)
f.close()
pressure_level_cache[tup] = data
return data