-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
226 lines (180 loc) · 6.73 KB
/
server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
from astropy.io import fits
from astropy import units as u
from astropy import wcs
from astropy.coordinates import SkyCoord
from multiprocessing import Process, Value, Array, Manager
from multiprocessing.pool import ThreadPool
import numpy as np
import sys
import collections
import zerorpc
import logging
import math
import json
import threading
import averager
# import struct
filePrefix = "/data3/artemix-data/FITS/"
class HelloRPC(object):
data = []
header = []
averager = None
thread = None
def degToHMSDMS(self, ra, dec):
logging.debug( "in HMSDMS: %d %d" % (ra, dec))
w = wcs.WCS(self.header)
pixcrd = np.array([[ra, dec, 0, 0]], np.float_)
world = w.wcs_pix2world(pixcrd, 0)
res = SkyCoord(world[0][0], world[0][1], unit="deg").to_string('hmsdms')
return res
def rangeToHMS(self, start, end, step):
start = int(start)
end = int(end)
step = int(step)
logging.debug( "Getting HMS range.")
logging.debug("%d %d %d" % (start, end, step))
res = []
for i in range(start, end, step):
logging.debug( "i: %d" % i)
res.append(self.degToHMSDMS(i, 0).split(" ")[0])
return res
def rangeToDMS(self, start, end, step):
start = int(start)
end = int(end)
step = int(step)
logging.debug( "Getting DMS range.")
logging.debug("%d %d %d" % (start, end, step))
res = []
for i in range(start, end, step):
logging.debug( "i: %d" % i)
res.append(self.degToHMSDMS(0, i).split(" ")[1])
return res
def _startDataLoading(self, fileName):
self.averager = averager.Averager(str(fileName))
def setData(self, fileName):
fileName = filePrefix + fileName
logging.debug( "Setting data: %s" % fileName)
try:
hdu_list = fits.open(fileName)
except IOError as e:
# logging.info("Error while opening file %s: %s" % (fileName, e))
raise Exception("Error while opening file %s: %s" % (fileName, e))
except:
raise Exception("Unknown error occurred.")
self.thread = threading.Thread(target=self._startDataLoading, args=(fileName,))
self.thread.start()
logging.debug( "Thread started")
self.data = hdu_list[0].data[0]
self.header = hdu_list[0].header
self.header.pop("HISTORY", None) # *None* prevents throwing error
self.header.pop("COMMENT", None)
self.header.pop("", None)
try:
logging.debug("Got RESTFRQ %f" % self.header["RESTFRQ"])
except KeyError:
self.header["RESTFRQ"] = self.header["RESTFREQ"]
d = dict(self.header)
return json.dumps(d)
def getDimensions(self):
logging.debug( "Getting dimensions.")
return self.data.shape
def flatten(self, l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in self.flatten(el):
yield sub
else:
yield el
def getSlice(self, sliceNumb, step):
try:
sliceNumb = int(sliceNumb)
except TypeError:
sliceNumb = 0
logging.debug( "Getting slice: %d" % sliceNumb)
res = self.data[sliceNumb, 0:self.data.shape[1]:step, 0:self.data.shape[2]:step].tolist()
return res
def getFreq(self, y=None, x=None, startZ=None, endZ=None):
logging.debug( "Getting frequency.")
if x == None:
x = 0
if y == None:
y = 0
if startZ == None:
startZ = 0
if endZ == None:
endZ = self.data.shape[0]
res = self.data[startZ:endZ, y, x].tolist()
logging.debug("%d %d" % (x, y))
return res
def getFreqAverage(self, startY=None, endY=None, startX=None, endX=None):
logging.debug( "Getting frequency average.")
startZ = 0
endZ = self.data.shape[0]
if startX == None:
startX = 0
if endX == None:
endX = self.data.shape[2]
if startY == None:
startY = 0
if endY == None:
endY = self.data.shape[1]
logging.debug("%d %d %d %d" % (startX, endX, startY, endY))
logging.debug("%s %s %s %s" % (type(startX), type(endX), type(startY), type(endY)))
# numberElements = (endX - startX) * (endY - startY)
res = []
i = startZ
pi180 = math.pi / 180 / 4.86e-6
#logging.info("observer : " + self.header["OBSERVER"])
bmaj = self.header["BMAJ"] * pi180
bmin = self.header["BMIN"] * pi180
convert = math.pi * bmaj * bmin
cdelt = 4 * math.log(2) * math.fabs(self.header["CDELT1"] * pi180) * math.fabs(self.header["CDELT2"] * pi180)
while i < endZ:
res.append(float(np.nansum(self.data[i, startY:endY, startX:endX]) / convert * cdelt))
i += 1
return res
def getAverage(self, step, startZ=None, endZ=None, startY=None, endY=None, startX=None, endX=None):
logging.info("Getting average.")
if startX == None:
startX = 0
if endX == None:
endX = self.data.shape[2]
if startY == None:
startY = 0
if endY == None:
endY = self.data.shape[1]
if startZ == None:
startZ = 0
else:
startZ = int(startZ)
if endZ == None:
endZ = self.data.shape[0]
else:
endZ = int(endZ)
if self.data.shape[0] == 1:
res = self.data.tolist()
logging.debug("Returning")
return res
res = [[]] * endY
# i = startY
cdelt = 3e+5 * math.fabs(self.header["CDELT3"] / self.header["RESTFRQ"])
numThreads = 16
self.thread.join()
logging.debug("startZ= %d, endZ=%d" % (startZ, endZ))
self.averager.setVariables(startX, endX, startY, endY, startZ, endZ, cdelt, numThreads)
res = self.averager.countAverage()
res2 = [list(res[i:i + endX]) for i in range(0, len(res), endX)]
res3 = np.array(res2)
logging.info("End of getting average.")
return res3[0:endY:step, 0:endX:step].tolist()
#
# End of the class
#
#
def main():
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
s = zerorpc.Server(HelloRPC())
s.bind("tcp://*:4242")
s.run()
if __name__ == '__main__': main()