##// END OF EJS Templates
sin matplotlib de los modulos de claire
José Chávez -
r1075:eb67833bff79
parent child
Show More
@@ -1,12 +1,12
1 1 #from schainpy.model.data.jrodata import *
2 2 # from schainpy.model.io.jrodataIO import *
3 3 # from schainpy.model.proc.jroprocessing import *
4 4 # from schainpy.model.graphics.jroplot import *
5 5 # from schainpy.model.utils.jroutils import *
6 6 # from schainpy.serializer import *
7 7
8 from graphics import *
8 9 from data import *
9 10 from io import *
10 11 from proc import *
11 from graphics import *
12 from utils import * No newline at end of file
12 from utils import *
@@ -1,468 +1,495
1 1 import numpy
2 2 import datetime
3 3 import sys
4 4 import matplotlib
5 5
6 6 if 'linux' in sys.platform:
7 matplotlib.use("TKAgg")
7 matplotlib.use("TkAgg")
8 8
9 9 if 'darwin' in sys.platform:
10 10 matplotlib.use('TKAgg')
11 #Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
11 # Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
12 12 import matplotlib.pyplot
13 13
14 14 from mpl_toolkits.axes_grid1 import make_axes_locatable
15 15 from matplotlib.ticker import FuncFormatter, LinearLocator
16 16
17 17 ###########################################
18 #Actualizacion de las funciones del driver
18 # Actualizacion de las funciones del driver
19 19 ###########################################
20 20
21 21 # create jro colormap
22 22
23 23 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
24 blu_values = matplotlib.pyplot.get_cmap("seismic_r", 20)(numpy.arange(20))[10:15]
25 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list("jro", numpy.vstack((blu_values, jet_values)))
24 blu_values = matplotlib.pyplot.get_cmap(
25 "seismic_r", 20)(numpy.arange(20))[10:15]
26 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
27 "jro", numpy.vstack((blu_values, jet_values)))
26 28 matplotlib.pyplot.register_cmap(cmap=ncmap)
27 29
28 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi = 80):
30
31 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80):
29 32
30 33 matplotlib.pyplot.ioff()
31 34
32 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(1.0*width/dpi, 1.0*height/dpi))
35 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(
36 1.0 * width / dpi, 1.0 * height / dpi))
33 37 fig.canvas.manager.set_window_title(wintitle)
34 38 # fig.canvas.manager.resize(width, height)
35 39 matplotlib.pyplot.ion()
36 40
37 41 if show:
38 42 matplotlib.pyplot.show()
39 43
40 44 return fig
41 45
46
42 47 def closeFigure(show=False, fig=None):
43 48
44 # matplotlib.pyplot.ioff()
45 # matplotlib.pyplot.pause(0)
49 # matplotlib.pyplot.ioff()
50 # matplotlib.pyplot.pause(0)
46 51
47 52 if show:
48 53 matplotlib.pyplot.show()
49 54
50 55 if fig != None:
51 56 matplotlib.pyplot.close(fig)
52 57 # matplotlib.pyplot.pause(0)
53 58 # matplotlib.pyplot.ion()
54 59
55 60 return
56 61
57 62 matplotlib.pyplot.close("all")
58 63 # matplotlib.pyplot.pause(0)
59 64 # matplotlib.pyplot.ion()
60 65
61 66 return
62 67
68
63 69 def saveFigure(fig, filename):
64 70
65 # matplotlib.pyplot.ioff()
71 # matplotlib.pyplot.ioff()
66 72 fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi)
67 73 # matplotlib.pyplot.ion()
68 74
75
69 76 def clearFigure(fig):
70 77
71 78 fig.clf()
72 79
80
73 81 def setWinTitle(fig, title):
74 82
75 83 fig.canvas.manager.set_window_title(title)
76 84
85
77 86 def setTitle(fig, title):
78 87
79 88 fig.suptitle(title)
80 89
90
81 91 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
82 92
83 93 matplotlib.pyplot.ioff()
84 94 matplotlib.pyplot.figure(fig.number)
85 95 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
86 (xpos, ypos),
87 colspan=colspan,
88 rowspan=rowspan,
89 polar=polar)
96 (xpos, ypos),
97 colspan=colspan,
98 rowspan=rowspan,
99 polar=polar)
90 100
91 101 matplotlib.pyplot.ion()
92 102 return axes
93 103
104
94 105 def setAxesText(ax, text):
95 106
96 107 ax.annotate(text,
97 xy = (.1, .99),
98 xycoords = 'figure fraction',
99 horizontalalignment = 'left',
100 verticalalignment = 'top',
101 fontsize = 10)
108 xy=(.1, .99),
109 xycoords='figure fraction',
110 horizontalalignment='left',
111 verticalalignment='top',
112 fontsize=10)
113
102 114
103 115 def printLabels(ax, xlabel, ylabel, title):
104 116
105 117 ax.set_xlabel(xlabel, size=11)
106 118 ax.set_ylabel(ylabel, size=11)
107 119 ax.set_title(title, size=8)
108 120
121
109 122 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
110 123 ticksize=9, xtick_visible=True, ytick_visible=True,
111 124 nxticks=4, nyticks=10,
112 grid=None,color='blue'):
113
125 grid=None, color='blue'):
114 126 """
115 127
116 128 Input:
117 129 grid : None, 'both', 'x', 'y'
118 130 """
119 131
120 132 matplotlib.pyplot.ioff()
121 133
122 ax.set_xlim([xmin,xmax])
123 ax.set_ylim([ymin,ymax])
134 ax.set_xlim([xmin, xmax])
135 ax.set_ylim([ymin, ymax])
124 136
125 137 printLabels(ax, xlabel, ylabel, title)
126 138
127 139 ######################################################
128 if (xmax-xmin)<=1:
129 xtickspos = numpy.linspace(xmin,xmax,nxticks)
130 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
140 if (xmax - xmin) <= 1:
141 xtickspos = numpy.linspace(xmin, xmax, nxticks)
142 xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos])
131 143 ax.set_xticks(xtickspos)
132 144 else:
133 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
145 xtickspos = numpy.arange(nxticks) * \
146 int((xmax - xmin) / (nxticks)) + int(xmin)
134 147 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
135 148 ax.set_xticks(xtickspos)
136 149
137 150 for tick in ax.get_xticklabels():
138 151 tick.set_visible(xtick_visible)
139 152
140 153 for tick in ax.xaxis.get_major_ticks():
141 154 tick.label.set_fontsize(ticksize)
142 155
143 156 ######################################################
144 157 for tick in ax.get_yticklabels():
145 158 tick.set_visible(ytick_visible)
146 159
147 160 for tick in ax.yaxis.get_major_ticks():
148 161 tick.label.set_fontsize(ticksize)
149 162
150 163 ax.plot(x, y, color=color)
151 164 iplot = ax.lines[-1]
152 165
153 166 ######################################################
154 167 if '0.' in matplotlib.__version__[0:2]:
155 168 print "The matplotlib version has to be updated to 1.1 or newer"
156 169 return iplot
157 170
158 171 if '1.0.' in matplotlib.__version__[0:4]:
159 172 print "The matplotlib version has to be updated to 1.1 or newer"
160 173 return iplot
161 174
162 175 if grid != None:
163 176 ax.grid(b=True, which='major', axis=grid)
164 177
165 178 matplotlib.pyplot.tight_layout()
166 179
167 180 matplotlib.pyplot.ion()
168 181
169 182 return iplot
170 183
184
171 185 def set_linedata(ax, x, y, idline):
172 186
173 ax.lines[idline].set_data(x,y)
187 ax.lines[idline].set_data(x, y)
188
174 189
175 190 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
176 191
177 192 ax = iplot.axes
178 193
179 194 printLabels(ax, xlabel, ylabel, title)
180 195
181 196 set_linedata(ax, x, y, idline=0)
182 197
198
183 199 def addpline(ax, x, y, color, linestyle, lw):
184 200
185 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
201 ax.plot(x, y, color=color, linestyle=linestyle, lw=lw)
186 202
187 203
188 204 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
189 xlabel='', ylabel='', title='', ticksize = 9,
190 colormap='jet',cblabel='', cbsize="5%",
205 xlabel='', ylabel='', title='', ticksize=9,
206 colormap='jet', cblabel='', cbsize="5%",
191 207 XAxisAsTime=False):
192 208
193 209 matplotlib.pyplot.ioff()
194 210
195 211 divider = make_axes_locatable(ax)
196 212 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
197 213 fig = ax.get_figure()
198 214 fig.add_axes(ax_cb)
199 215
200 ax.set_xlim([xmin,xmax])
201 ax.set_ylim([ymin,ymax])
216 ax.set_xlim([xmin, xmax])
217 ax.set_ylim([ymin, ymax])
202 218
203 219 printLabels(ax, xlabel, ylabel, title)
204 220
205 221 z = numpy.ma.masked_invalid(z)
206 cmap=matplotlib.pyplot.get_cmap(colormap)
222 cmap = matplotlib.pyplot.get_cmap(colormap)
207 223 cmap.set_bad('black', 1.)
208 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=cmap)
209 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
224 imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
225 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
210 226 cb.set_label(cblabel)
211 227
212 228 # for tl in ax_cb.get_yticklabels():
213 229 # tl.set_visible(True)
214 230
215 231 for tick in ax.yaxis.get_major_ticks():
216 232 tick.label.set_fontsize(ticksize)
217 233
218 234 for tick in ax.xaxis.get_major_ticks():
219 235 tick.label.set_fontsize(ticksize)
220 236
221 237 for tick in cb.ax.get_yticklabels():
222 238 tick.set_fontsize(ticksize)
223 239
224 240 ax_cb.yaxis.tick_right()
225 241
226 242 if '0.' in matplotlib.__version__[0:2]:
227 243 print "The matplotlib version has to be updated to 1.1 or newer"
228 244 return imesh
229 245
230 246 if '1.0.' in matplotlib.__version__[0:4]:
231 247 print "The matplotlib version has to be updated to 1.1 or newer"
232 248 return imesh
233 249
234 250 matplotlib.pyplot.tight_layout()
235 251
236 252 if XAxisAsTime:
237 253
238 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
254 def func(x, pos): return ('%s') % (
255 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
239 256 ax.xaxis.set_major_formatter(FuncFormatter(func))
240 257 ax.xaxis.set_major_locator(LinearLocator(7))
241 258
242 259 matplotlib.pyplot.ion()
243 260 return imesh
244 261
262
245 263 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
246 264
247 265 z = z.T
248 266 ax = imesh.axes
249 267 printLabels(ax, xlabel, ylabel, title)
250 268 imesh.set_array(z.ravel())
251 269
270
252 271 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
253 272
254 273 printLabels(ax, xlabel, ylabel, title)
255 274
256 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
275 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax,
276 cmap=matplotlib.pyplot.get_cmap(colormap))
277
257 278
258 279 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
259 280
260 281 printLabels(ax, xlabel, ylabel, title)
261 282
262 283 ax.collections.remove(ax.collections[0])
263 284
264 285 z = numpy.ma.masked_invalid(z)
265 286
266 cmap=matplotlib.pyplot.get_cmap(colormap)
287 cmap = matplotlib.pyplot.get_cmap(colormap)
267 288 cmap.set_bad('black', 1.)
268 289
290 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
269 291
270 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=cmap)
271 292
272 293 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
273 ticksize=9, xtick_visible=True, ytick_visible=True,
274 nxticks=4, nyticks=10,
275 grid=None):
276
294 ticksize=9, xtick_visible=True, ytick_visible=True,
295 nxticks=4, nyticks=10,
296 grid=None):
277 297 """
278 298
279 299 Input:
280 300 grid : None, 'both', 'x', 'y'
281 301 """
282 302
283 303 matplotlib.pyplot.ioff()
284 304
285 305 lines = ax.plot(x.T, y)
286 306 leg = ax.legend(lines, legendlabels, loc='upper right')
287 307 leg.get_frame().set_alpha(0.5)
288 ax.set_xlim([xmin,xmax])
289 ax.set_ylim([ymin,ymax])
308 ax.set_xlim([xmin, xmax])
309 ax.set_ylim([ymin, ymax])
290 310 printLabels(ax, xlabel, ylabel, title)
291 311
292 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
312 xtickspos = numpy.arange(nxticks) * \
313 int((xmax - xmin) / (nxticks)) + int(xmin)
293 314 ax.set_xticks(xtickspos)
294 315
295 316 for tick in ax.get_xticklabels():
296 317 tick.set_visible(xtick_visible)
297 318
298 319 for tick in ax.xaxis.get_major_ticks():
299 320 tick.label.set_fontsize(ticksize)
300 321
301 322 for tick in ax.get_yticklabels():
302 323 tick.set_visible(ytick_visible)
303 324
304 325 for tick in ax.yaxis.get_major_ticks():
305 326 tick.label.set_fontsize(ticksize)
306 327
307 328 iplot = ax.lines[-1]
308 329
309 330 if '0.' in matplotlib.__version__[0:2]:
310 331 print "The matplotlib version has to be updated to 1.1 or newer"
311 332 return iplot
312 333
313 334 if '1.0.' in matplotlib.__version__[0:4]:
314 335 print "The matplotlib version has to be updated to 1.1 or newer"
315 336 return iplot
316 337
317 338 if grid != None:
318 339 ax.grid(b=True, which='major', axis=grid)
319 340
320 341 matplotlib.pyplot.tight_layout()
321 342
322 343 matplotlib.pyplot.ion()
323 344
324 345 return iplot
325 346
326 347
327 348 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
328 349
329 350 ax = iplot.axes
330 351
331 352 printLabels(ax, xlabel, ylabel, title)
332 353
333 354 for i in range(len(ax.lines)):
334 355 line = ax.lines[i]
335 line.set_data(x[i,:],y)
356 line.set_data(x[i, :], y)
336 357
337 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
338 ticksize=9, xtick_visible=True, ytick_visible=True,
339 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
340 grid=None, XAxisAsTime=False):
341 358
359 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
360 ticksize=9, xtick_visible=True, ytick_visible=True,
361 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
362 grid=None, XAxisAsTime=False):
342 363 """
343 364
344 365 Input:
345 366 grid : None, 'both', 'x', 'y'
346 367 """
347 368
348 369 matplotlib.pyplot.ioff()
349 370
350 371 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
351 372 lines = ax.plot(x, y.T)
352 373 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
353 374 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
354 375
355 376 leg = ax.legend(lines, legendlabels,
356 377 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
357 378
358 for label in leg.get_texts(): label.set_fontsize(9)
379 for label in leg.get_texts():
380 label.set_fontsize(9)
359 381
360 ax.set_xlim([xmin,xmax])
361 ax.set_ylim([ymin,ymax])
382 ax.set_xlim([xmin, xmax])
383 ax.set_ylim([ymin, ymax])
362 384 printLabels(ax, xlabel, ylabel, title)
363 385
364 386 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
365 387 # ax.set_xticks(xtickspos)
366 388
367 389 for tick in ax.get_xticklabels():
368 390 tick.set_visible(xtick_visible)
369 391
370 392 for tick in ax.xaxis.get_major_ticks():
371 393 tick.label.set_fontsize(ticksize)
372 394
373 395 for tick in ax.get_yticklabels():
374 396 tick.set_visible(ytick_visible)
375 397
376 398 for tick in ax.yaxis.get_major_ticks():
377 399 tick.label.set_fontsize(ticksize)
378 400
379 401 iplot = ax.lines[-1]
380 402
381 403 if '0.' in matplotlib.__version__[0:2]:
382 404 print "The matplotlib version has to be updated to 1.1 or newer"
383 405 return iplot
384 406
385 407 if '1.0.' in matplotlib.__version__[0:4]:
386 408 print "The matplotlib version has to be updated to 1.1 or newer"
387 409 return iplot
388 410
389 411 if grid != None:
390 412 ax.grid(b=True, which='major', axis=grid)
391 413
392 414 matplotlib.pyplot.tight_layout()
393 415
394 416 if XAxisAsTime:
395 417
396 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
418 def func(x, pos): return ('%s') % (
419 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
397 420 ax.xaxis.set_major_formatter(FuncFormatter(func))
398 421 ax.xaxis.set_major_locator(LinearLocator(7))
399 422
400 423 matplotlib.pyplot.ion()
401 424
402 425 return iplot
403 426
427
404 428 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
405 429
406 430 ax = iplot.axes
407 431
408 432 printLabels(ax, xlabel, ylabel, title)
409 433
410 434 for i in range(len(ax.lines)):
411 435 line = ax.lines[i]
412 line.set_data(x,y[i,:])
436 line.set_data(x, y[i, :])
437
413 438
414 439 def createPolar(ax, x, y,
415 xlabel='', ylabel='', title='', ticksize = 9,
416 colormap='jet',cblabel='', cbsize="5%",
417 XAxisAsTime=False):
440 xlabel='', ylabel='', title='', ticksize=9,
441 colormap='jet', cblabel='', cbsize="5%",
442 XAxisAsTime=False):
418 443
419 444 matplotlib.pyplot.ioff()
420 445
421 ax.plot(x,y,'bo', markersize=5)
446 ax.plot(x, y, 'bo', markersize=5)
422 447 # ax.set_rmax(90)
423 ax.set_ylim(0,90)
424 ax.set_yticks(numpy.arange(0,90,20))
448 ax.set_ylim(0, 90)
449 ax.set_yticks(numpy.arange(0, 90, 20))
425 450 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
426 451 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
427 452 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
428 453 ax.yaxis.labelpad = 40
429 454 printLabels(ax, xlabel, ylabel, title)
430 455 iplot = ax.lines[-1]
431 456
432 457 if '0.' in matplotlib.__version__[0:2]:
433 458 print "The matplotlib version has to be updated to 1.1 or newer"
434 459 return iplot
435 460
436 461 if '1.0.' in matplotlib.__version__[0:4]:
437 462 print "The matplotlib version has to be updated to 1.1 or newer"
438 463 return iplot
439 464
440 465 # if grid != None:
441 466 # ax.grid(b=True, which='major', axis=grid)
442 467
443 468 matplotlib.pyplot.tight_layout()
444 469
445 470 matplotlib.pyplot.ion()
446 471
447
448 472 return iplot
449 473
474
450 475 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
451 476
452 477 ax = iplot.axes
453 478
454 479 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
455 480 printLabels(ax, xlabel, ylabel, title)
456 481
457 482 set_linedata(ax, x, y, idline=0)
458 483
484
459 485 def draw(fig):
460 486
461 487 if type(fig) == 'int':
462 488 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
463 489
464 490 fig.canvas.draw()
465 491
492
466 493 def pause(interval=0.000001):
467 494
468 495 matplotlib.pyplot.pause(interval)
@@ -1,321 +1,331
1 import os, sys
1 import os
2 import sys
2 3 import glob
3 4 import fnmatch
4 5 import datetime
5 6 import time
6 7 import re
7 8 import h5py
8 9 import numpy
9 import matplotlib.pyplot as plt
10 10
11 import pylab as plb
12 11 from scipy.optimize import curve_fit
13 from scipy import asarray as ar,exp
12 from scipy import asarray as ar, exp
14 13 from scipy import stats
15 14
16 15 from duplicity.path import Path
17 16 from numpy.ma.core import getdata
18 17
19 18 SPEED_OF_LIGHT = 299792458
20 19 SPEED_OF_LIGHT = 3e8
21 20
22 21 try:
23 22 from gevent import sleep
24 23 except:
25 24 from time import sleep
26 25
27 26 from schainpy.model.data.jrodata import Spectra
28 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
29 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
30 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
31 30 from numpy import imag, shape, NaN
32 31
33 32
34 startFp = open('/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc',"rb")
35
36
37 FILE_HEADER = numpy.dtype([ #HEADER 1024bytes
38 ('Hname',numpy.str_,32), #Original file name
39 ('Htime',numpy.str_,32), #Date and time when the file was created
40 ('Hoper',numpy.str_,64), #Name of operator who created the file
41 ('Hplace',numpy.str_,128), #Place where the measurements was carried out
42 ('Hdescr',numpy.str_,256), #Description of measurements
43 ('Hdummy',numpy.str_,512), #Reserved space
44 #Main chunk
45 ('Msign','<i4'), #Main chunk signature FZKF or NUIG
46 ('MsizeData','<i4'), #Size of data block main chunk
47 #Processing DSP parameters
48 ('PPARsign','<i4'), #PPAR signature
49 ('PPARsize','<i4'), #PPAR size of block
50 ('PPARprf','<i4'), #Pulse repetition frequency
51 ('PPARpdr','<i4'), #Pulse duration
52 ('PPARsft','<i4'), #FFT length
53 ('PPARavc','<i4'), #Number of spectral (in-coherent) averages
54 ('PPARihp','<i4'), #Number of lowest range gate for moment estimation
55 ('PPARchg','<i4'), #Count for gates for moment estimation
56 ('PPARpol','<i4'), #switch on/off polarimetric measurements. Should be 1.
57 #Service DSP parameters
58 ('SPARatt','<i4'), #STC attenuation on the lowest ranges on/off
59 ('SPARtx','<i4'), #OBSOLETE
60 ('SPARaddGain0','<f4'), #OBSOLETE
61 ('SPARaddGain1','<f4'), #OBSOLETE
62 ('SPARwnd','<i4'), #Debug only. It normal mode it is 0.
63 ('SPARpos','<i4'), #Delay between sync pulse and tx pulse for phase corr, ns
64 ('SPARadd','<i4'), #"add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
65 ('SPARlen','<i4'), #Time for measuring txn pulse phase. OBSOLETE
66 ('SPARcal','<i4'), #OBSOLETE
67 ('SPARnos','<i4'), #OBSOLETE
68 ('SPARof0','<i4'), #detection threshold
69 ('SPARof1','<i4'), #OBSOLETE
70 ('SPARswt','<i4'), #2nd moment estimation threshold
71 ('SPARsum','<i4'), #OBSOLETE
72 ('SPARosc','<i4'), #flag Oscillosgram mode
73 ('SPARtst','<i4'), #OBSOLETE
74 ('SPARcor','<i4'), #OBSOLETE
75 ('SPARofs','<i4'), #OBSOLETE
76 ('SPARhsn','<i4'), #Hildebrand div noise detection on noise gate
77 ('SPARhsa','<f4'), #Hildebrand div noise detection on all gates
78 ('SPARcalibPow_M','<f4'), #OBSOLETE
79 ('SPARcalibSNR_M','<f4'), #OBSOLETE
80 ('SPARcalibPow_S','<f4'), #OBSOLETE
81 ('SPARcalibSNR_S','<f4'), #OBSOLETE
82 ('SPARrawGate1','<i4'), #Lowest range gate for spectra saving Raw_Gate1 >=5
83 ('SPARrawGate2','<i4'), #Number of range gates with atmospheric signal
84 ('SPARraw','<i4'), #flag - IQ or spectra saving on/off
85 ('SPARprc','<i4'),]) #flag - Moment estimation switched on/off
86
87
88
89 self.Hname= None
90 self.Htime= None
91 self.Hoper= None
92 self.Hplace= None
93 self.Hdescr= None
94 self.Hdummy= None
95
96 self.Msign=None
97 self.MsizeData=None
98
99 self.PPARsign=None
100 self.PPARsize=None
101 self.PPARprf=None
102 self.PPARpdr=None
103 self.PPARsft=None
104 self.PPARavc=None
105 self.PPARihp=None
106 self.PPARchg=None
107 self.PPARpol=None
108 #Service DSP parameters
109 self.SPARatt=None
110 self.SPARtx=None
111 self.SPARaddGain0=None
112 self.SPARaddGain1=None
113 self.SPARwnd=None
114 self.SPARpos=None
115 self.SPARadd=None
116 self.SPARlen=None
117 self.SPARcal=None
118 self.SPARnos=None
119 self.SPARof0=None
120 self.SPARof1=None
121 self.SPARswt=None
122 self.SPARsum=None
123 self.SPARosc=None
124 self.SPARtst=None
125 self.SPARcor=None
126 self.SPARofs=None
127 self.SPARhsn=None
128 self.SPARhsa=None
129 self.SPARcalibPow_M=None
130 self.SPARcalibSNR_M=None
131 self.SPARcalibPow_S=None
132 self.SPARcalibSNR_S=None
133 self.SPARrawGate1=None
134 self.SPARrawGate2=None
135 self.SPARraw=None
136 self.SPARprc=None
137
138
139
140 header = numpy.fromfile(fp, FILE_HEADER,1)
33 startFp = open(
34 '/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc', "rb")
35
36
37 FILE_HEADER = numpy.dtype([ # HEADER 1024bytes
38 ('Hname', numpy.str_, 32), # Original file name
39 # Date and time when the file was created
40 ('Htime', numpy.str_, 32),
41 # Name of operator who created the file
42 ('Hoper', numpy.str_, 64),
43 # Place where the measurements was carried out
44 ('Hplace', numpy.str_, 128),
45 # Description of measurements
46 ('Hdescr', numpy.str_, 256),
47 ('Hdummy', numpy.str_, 512), # Reserved space
48 # Main chunk
49 ('Msign', '<i4'), # Main chunk signature FZKF or NUIG
50 ('MsizeData', '<i4'), # Size of data block main chunk
51 # Processing DSP parameters
52 ('PPARsign', '<i4'), # PPAR signature
53 ('PPARsize', '<i4'), # PPAR size of block
54 ('PPARprf', '<i4'), # Pulse repetition frequency
55 ('PPARpdr', '<i4'), # Pulse duration
56 ('PPARsft', '<i4'), # FFT length
57 # Number of spectral (in-coherent) averages
58 ('PPARavc', '<i4'),
59 # Number of lowest range gate for moment estimation
60 ('PPARihp', '<i4'),
61 # Count for gates for moment estimation
62 ('PPARchg', '<i4'),
63 # switch on/off polarimetric measurements. Should be 1.
64 ('PPARpol', '<i4'),
65 # Service DSP parameters
66 # STC attenuation on the lowest ranges on/off
67 ('SPARatt', '<i4'),
68 ('SPARtx', '<i4'), # OBSOLETE
69 ('SPARaddGain0', '<f4'), # OBSOLETE
70 ('SPARaddGain1', '<f4'), # OBSOLETE
71 # Debug only. It normal mode it is 0.
72 ('SPARwnd', '<i4'),
73 # Delay between sync pulse and tx pulse for phase corr, ns
74 ('SPARpos', '<i4'),
75 # "add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
76 ('SPARadd', '<i4'),
77 # Time for measuring txn pulse phase. OBSOLETE
78 ('SPARlen', '<i4'),
79 ('SPARcal', '<i4'), # OBSOLETE
80 ('SPARnos', '<i4'), # OBSOLETE
81 ('SPARof0', '<i4'), # detection threshold
82 ('SPARof1', '<i4'), # OBSOLETE
83 ('SPARswt', '<i4'), # 2nd moment estimation threshold
84 ('SPARsum', '<i4'), # OBSOLETE
85 ('SPARosc', '<i4'), # flag Oscillosgram mode
86 ('SPARtst', '<i4'), # OBSOLETE
87 ('SPARcor', '<i4'), # OBSOLETE
88 ('SPARofs', '<i4'), # OBSOLETE
89 # Hildebrand div noise detection on noise gate
90 ('SPARhsn', '<i4'),
91 # Hildebrand div noise detection on all gates
92 ('SPARhsa', '<f4'),
93 ('SPARcalibPow_M', '<f4'), # OBSOLETE
94 ('SPARcalibSNR_M', '<f4'), # OBSOLETE
95 ('SPARcalibPow_S', '<f4'), # OBSOLETE
96 ('SPARcalibSNR_S', '<f4'), # OBSOLETE
97 # Lowest range gate for spectra saving Raw_Gate1 >=5
98 ('SPARrawGate1', '<i4'),
99 # Number of range gates with atmospheric signal
100 ('SPARrawGate2', '<i4'),
101 # flag - IQ or spectra saving on/off
102 ('SPARraw', '<i4'),
103 ('SPARprc', '<i4'), ]) # flag - Moment estimation switched on/off
104
105
106 self.Hname = None
107 self.Htime = None
108 self.Hoper = None
109 self.Hplace = None
110 self.Hdescr = None
111 self.Hdummy = None
112
113 self.Msign = None
114 self.MsizeData = None
115
116 self.PPARsign = None
117 self.PPARsize = None
118 self.PPARprf = None
119 self.PPARpdr = None
120 self.PPARsft = None
121 self.PPARavc = None
122 self.PPARihp = None
123 self.PPARchg = None
124 self.PPARpol = None
125 # Service DSP parameters
126 self.SPARatt = None
127 self.SPARtx = None
128 self.SPARaddGain0 = None
129 self.SPARaddGain1 = None
130 self.SPARwnd = None
131 self.SPARpos = None
132 self.SPARadd = None
133 self.SPARlen = None
134 self.SPARcal = None
135 self.SPARnos = None
136 self.SPARof0 = None
137 self.SPARof1 = None
138 self.SPARswt = None
139 self.SPARsum = None
140 self.SPARosc = None
141 self.SPARtst = None
142 self.SPARcor = None
143 self.SPARofs = None
144 self.SPARhsn = None
145 self.SPARhsa = None
146 self.SPARcalibPow_M = None
147 self.SPARcalibSNR_M = None
148 self.SPARcalibPow_S = None
149 self.SPARcalibSNR_S = None
150 self.SPARrawGate1 = None
151 self.SPARrawGate2 = None
152 self.SPARraw = None
153 self.SPARprc = None
154
155
156 header = numpy.fromfile(fp, FILE_HEADER, 1)
141 157 ''' numpy.fromfile(file, dtype, count, sep='')
142 158 file : file or str
143 159 Open file object or filename.
144 160
145 161 dtype : data-type
146 162 Data type of the returned array. For binary files, it is used to determine
147 163 the size and byte-order of the items in the file.
148 164
149 165 count : int
150 166 Number of items to read. -1 means all items (i.e., the complete file).
151 167
152 168 sep : str
153 169 Separator between items if file is a text file. Empty ("") separator means
154 170 the file should be treated as binary. Spaces (" ") in the separator match zero
155 171 or more whitespace characters. A separator consisting only of spaces must match
156 172 at least one whitespace.
157 173
158 174 '''
159 175
160 Hname= str(header['Hname'][0])
161 Htime= str(header['Htime'][0])
162 Hoper= str(header['Hoper'][0])
163 Hplace= str(header['Hplace'][0])
164 Hdescr= str(header['Hdescr'][0])
165 Hdummy= str(header['Hdummy'][0])
166
167 Msign=header['Msign'][0]
168 MsizeData=header['MsizeData'][0]
169
170 PPARsign=header['PPARsign'][0]
171 PPARsize=header['PPARsize'][0]
172 PPARprf=header['PPARprf'][0]
173 PPARpdr=header['PPARpdr'][0]
174 PPARsft=header['PPARsft'][0]
175 PPARavc=header['PPARavc'][0]
176 PPARihp=header['PPARihp'][0]
177 PPARchg=header['PPARchg'][0]
178 PPARpol=header['PPARpol'][0]
179 #Service DSP parameters
180 SPARatt=header['SPARatt'][0]
181 SPARtx=header['SPARtx'][0]
182 SPARaddGain0=header['SPARaddGain0'][0]
183 SPARaddGain1=header['SPARaddGain1'][0]
184 SPARwnd=header['SPARwnd'][0]
185 SPARpos=header['SPARpos'][0]
186 SPARadd=header['SPARadd'][0]
187 SPARlen=header['SPARlen'][0]
188 SPARcal=header['SPARcal'][0]
189 SPARnos=header['SPARnos'][0]
190 SPARof0=header['SPARof0'][0]
191 SPARof1=header['SPARof1'][0]
192 SPARswt=header['SPARswt'][0]
193 SPARsum=header['SPARsum'][0]
194 SPARosc=header['SPARosc'][0]
195 SPARtst=header['SPARtst'][0]
196 SPARcor=header['SPARcor'][0]
197 SPARofs=header['SPARofs'][0]
198 SPARhsn=header['SPARhsn'][0]
199 SPARhsa=header['SPARhsa'][0]
200 SPARcalibPow_M=header['SPARcalibPow_M'][0]
201 SPARcalibSNR_M=header['SPARcalibSNR_M'][0]
202 SPARcalibPow_S=header['SPARcalibPow_S'][0]
203 SPARcalibSNR_S=header['SPARcalibSNR_S'][0]
204 SPARrawGate1=header['SPARrawGate1'][0]
205 SPARrawGate2=header['SPARrawGate2'][0]
206 SPARraw=header['SPARraw'][0]
207 SPARprc=header['SPARprc'][0]
208
209
210
211 SRVI_STRUCTURE = numpy.dtype([
212 ('frame_cnt','<u4'),#
213 ('time_t','<u4'), #
214 ('tpow','<f4'), #
215 ('npw1','<f4'), #
216 ('npw2','<f4'), #
217 ('cpw1','<f4'), #
218 ('pcw2','<f4'), #
219 ('ps_err','<u4'), #
220 ('te_err','<u4'), #
221 ('rc_err','<u4'), #
222 ('grs1','<u4'), #
223 ('grs2','<u4'), #
224 ('azipos','<f4'), #
225 ('azivel','<f4'), #
226 ('elvpos','<f4'), #
227 ('elvvel','<f4'), #
228 ('northAngle','<f4'), #
229 ('microsec','<u4'), #
230 ('azisetvel','<f4'), #
231 ('elvsetpos','<f4'), #
232 ('RadarConst','<f4'),]) #
233
234 JUMP_STRUCTURE = numpy.dtype([
235 ('jump','<u140'),#
236 ('SizeOfDataBlock1',numpy.str_,32),#
237 ('jump','<i4'),#
238 ('DataBlockTitleSRVI1',numpy.str_,32),#
239 ('SizeOfSRVI1','<i4'),])#
240
241
242
243 #frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
244 #cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
245 #grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
246 #microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0
247
248
176 Hname = str(header['Hname'][0])
177 Htime = str(header['Htime'][0])
178 Hoper = str(header['Hoper'][0])
179 Hplace = str(header['Hplace'][0])
180 Hdescr = str(header['Hdescr'][0])
181 Hdummy = str(header['Hdummy'][0])
182
183 Msign = header['Msign'][0]
184 MsizeData = header['MsizeData'][0]
185
186 PPARsign = header['PPARsign'][0]
187 PPARsize = header['PPARsize'][0]
188 PPARprf = header['PPARprf'][0]
189 PPARpdr = header['PPARpdr'][0]
190 PPARsft = header['PPARsft'][0]
191 PPARavc = header['PPARavc'][0]
192 PPARihp = header['PPARihp'][0]
193 PPARchg = header['PPARchg'][0]
194 PPARpol = header['PPARpol'][0]
195 # Service DSP parameters
196 SPARatt = header['SPARatt'][0]
197 SPARtx = header['SPARtx'][0]
198 SPARaddGain0 = header['SPARaddGain0'][0]
199 SPARaddGain1 = header['SPARaddGain1'][0]
200 SPARwnd = header['SPARwnd'][0]
201 SPARpos = header['SPARpos'][0]
202 SPARadd = header['SPARadd'][0]
203 SPARlen = header['SPARlen'][0]
204 SPARcal = header['SPARcal'][0]
205 SPARnos = header['SPARnos'][0]
206 SPARof0 = header['SPARof0'][0]
207 SPARof1 = header['SPARof1'][0]
208 SPARswt = header['SPARswt'][0]
209 SPARsum = header['SPARsum'][0]
210 SPARosc = header['SPARosc'][0]
211 SPARtst = header['SPARtst'][0]
212 SPARcor = header['SPARcor'][0]
213 SPARofs = header['SPARofs'][0]
214 SPARhsn = header['SPARhsn'][0]
215 SPARhsa = header['SPARhsa'][0]
216 SPARcalibPow_M = header['SPARcalibPow_M'][0]
217 SPARcalibSNR_M = header['SPARcalibSNR_M'][0]
218 SPARcalibPow_S = header['SPARcalibPow_S'][0]
219 SPARcalibSNR_S = header['SPARcalibSNR_S'][0]
220 SPARrawGate1 = header['SPARrawGate1'][0]
221 SPARrawGate2 = header['SPARrawGate2'][0]
222 SPARraw = header['SPARraw'][0]
223 SPARprc = header['SPARprc'][0]
224
225
226 SRVI_STRUCTURE = numpy.dtype([
227 ('frame_cnt', '<u4'),
228 ('time_t', '<u4'), #
229 ('tpow', '<f4'), #
230 ('npw1', '<f4'), #
231 ('npw2', '<f4'), #
232 ('cpw1', '<f4'), #
233 ('pcw2', '<f4'), #
234 ('ps_err', '<u4'), #
235 ('te_err', '<u4'), #
236 ('rc_err', '<u4'), #
237 ('grs1', '<u4'), #
238 ('grs2', '<u4'), #
239 ('azipos', '<f4'), #
240 ('azivel', '<f4'), #
241 ('elvpos', '<f4'), #
242 ('elvvel', '<f4'), #
243 ('northAngle', '<f4'),
244 ('microsec', '<u4'), #
245 ('azisetvel', '<f4'), #
246 ('elvsetpos', '<f4'), #
247 ('RadarConst', '<f4'), ]) #
248
249 JUMP_STRUCTURE = numpy.dtype([
250 ('jump', '<u140'),
251 ('SizeOfDataBlock1', numpy.str_, 32),
252 ('jump', '<i4'),
253 ('DataBlockTitleSRVI1', numpy.str_, 32),
254 ('SizeOfSRVI1', '<i4'), ])
255
256
257 # frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
258 # cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
259 # grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
260 # microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0
261
262
249 263 frame_cnt = frame_cnt
250 264 dwell = time_t
251 265 tpow = tpow
252 266 npw1 = npw1
253 267 npw2 = npw2
254 268 cpw1 = cpw1
255 269 pcw2 = pcw2
256 270 ps_err = ps_err
257 271 te_err = te_err
258 272 rc_err = rc_err
259 273 grs1 = grs1
260 274 grs2 = grs2
261 275 azipos = azipos
262 276 azivel = azivel
263 277 elvpos = elvpos
264 278 elvvel = elvvel
265 279 northAngle = northAngle
266 280 microsec = microsec
267 281 azisetvel = azisetvel
268 282 elvsetpos = elvsetpos
269 283 RadarConst5 = RadarConst
270
271 284
272 285
273 #print fp
274 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
275 #startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
276 #RecCounter=0
277 #Off2StartNxtRec=811248
278 #print 'OffsetStartHeader ',self.OffsetStartHeader,'RecCounter ', self.RecCounter, 'Off2StartNxtRec ' , self.Off2StartNxtRec
286 # print fp
287 # startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
288 # startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
289 # RecCounter=0
290 # Off2StartNxtRec=811248
291 # print 'OffsetStartHeader ',self.OffsetStartHeader,'RecCounter ', self.RecCounter, 'Off2StartNxtRec ' , self.Off2StartNxtRec
279 292 #OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
280 293 #startFp.seek(OffRHeader, os.SEEK_SET)
281 print 'debe ser 48, RecCounter*811248', self.OffsetStartHeader,self.RecCounter,self.Off2StartNxtRec
282 print 'Posicion del bloque: ',OffRHeader
294 print 'debe ser 48, RecCounter*811248', self.OffsetStartHeader, self.RecCounter, self.Off2StartNxtRec
295 print 'Posicion del bloque: ', OffRHeader
283 296
284 header = numpy.fromfile(startFp,SRVI_STRUCTURE,1)
297 header = numpy.fromfile(startFp, SRVI_STRUCTURE, 1)
285 298
286 self.frame_cnt = header['frame_cnt'][0]#
299 self.frame_cnt = header['frame_cnt'][0]
287 300 self.time_t = header['frame_cnt'][0] #
288 301 self.tpow = header['frame_cnt'][0] #
289 302 self.npw1 = header['frame_cnt'][0] #
290 303 self.npw2 = header['frame_cnt'][0] #
291 304 self.cpw1 = header['frame_cnt'][0] #
292 305 self.pcw2 = header['frame_cnt'][0] #
293 306 self.ps_err = header['frame_cnt'][0] #
294 307 self.te_err = header['frame_cnt'][0] #
295 308 self.rc_err = header['frame_cnt'][0] #
296 309 self.grs1 = header['frame_cnt'][0] #
297 310 self.grs2 = header['frame_cnt'][0] #
298 311 self.azipos = header['frame_cnt'][0] #
299 312 self.azivel = header['frame_cnt'][0] #
300 313 self.elvpos = header['frame_cnt'][0] #
301 314 self.elvvel = header['frame_cnt'][0] #
302 315 self.northAngle = header['frame_cnt'][0] #
303 316 self.microsec = header['frame_cnt'][0] #
304 317 self.azisetvel = header['frame_cnt'][0] #
305 318 self.elvsetpos = header['frame_cnt'][0] #
306 self.RadarConst = header['frame_cnt'][0] #
319 self.RadarConst = header['frame_cnt'][0] #
307 320
308 321
309 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
322 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
310 323
311 self.RHsize = 180+20*self.nChannels
312 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
313 #print 'Datasize',self.Datasize
314 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
324 self.RHsize = 180 + 20 * self.nChannels
325 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
326 # print 'Datasize',self.Datasize
327 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
315 328
316 329 print '=============================================='
317 330
318 331 print '=============================================='
319
320
321 No newline at end of file
This diff has been collapsed as it changes many lines, (1270 lines changed) Show them Hide them
@@ -1,1154 +1,1182
1 import os, sys
1 import os
2 import sys
2 3 import glob
3 4 import fnmatch
4 5 import datetime
5 6 import time
6 7 import re
7 8 import h5py
8 9 import numpy
9 import matplotlib.pyplot as plt
10 10
11 11 import pylab as plb
12 12 from scipy.optimize import curve_fit
13 13 from scipy import asarray as ar, exp
14 14 from scipy import stats
15 15
16 16 from numpy.ma.core import getdata
17 17
18 18 SPEED_OF_LIGHT = 299792458
19 19 SPEED_OF_LIGHT = 3e8
20 20
21 21 try:
22 22 from gevent import sleep
23 23 except:
24 24 from time import sleep
25 25
26 26 from schainpy.model.data.jrodata import Spectra
27 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
28 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
30 30 from numpy import imag, shape, NaN
31 31
32 32 from jroIO_base import JRODataReader
33 33
34 34
35 35 class Header(object):
36
36
37 37 def __init__(self):
38 38 raise NotImplementedError
39
40
39
41 40 def read(self):
42
41
43 42 raise NotImplementedError
44
43
45 44 def write(self):
46
45
47 46 raise NotImplementedError
48
47
49 48 def printInfo(self):
50
51 message = "#"*50 + "\n"
49
50 message = "#" * 50 + "\n"
52 51 message += self.__class__.__name__.upper() + "\n"
53 message += "#"*50 + "\n"
54
52 message += "#" * 50 + "\n"
53
55 54 keyList = self.__dict__.keys()
56 55 keyList.sort()
57
56
58 57 for key in keyList:
59 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
60
58 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
59
61 60 if "size" not in keyList:
62 61 attr = getattr(self, "size")
63
64 if attr:
65 message += "%s = %s" %("size", attr) + "\n"
66
67 #print message
68 62
63 if attr:
64 message += "%s = %s" % ("size", attr) + "\n"
69 65
66 # print message
70 67
71 68
69 FILE_STRUCTURE = numpy.dtype([ # HEADER 48bytes
70 ('FileMgcNumber', '<u4'), # 0x23020100
71 # No Of FDT data records in this file (0 or more)
72 ('nFDTdataRecors', '<u4'),
73 ('OffsetStartHeader', '<u4'),
74 ('RadarUnitId', '<u4'),
75 ('SiteName', numpy.str_, 32), # Null terminated
76 ])
72 77
73 FILE_STRUCTURE = numpy.dtype([ #HEADER 48bytes
74 ('FileMgcNumber','<u4'), #0x23020100
75 ('nFDTdataRecors','<u4'), #No Of FDT data records in this file (0 or more)
76 ('OffsetStartHeader','<u4'),
77 ('RadarUnitId','<u4'),
78 ('SiteName',numpy.str_,32), #Null terminated
79 ])
80 78
81 79 class FileHeaderBLTR(Header):
82
80
83 81 def __init__(self):
84
85 self.FileMgcNumber= 0 #0x23020100
86 self.nFDTdataRecors=0 #No Of FDT data records in this file (0 or more)
87 self.RadarUnitId= 0
88 self.OffsetStartHeader=0
89 self.SiteName= ""
82
83 self.FileMgcNumber = 0 # 0x23020100
84 # No Of FDT data records in this file (0 or more)
85 self.nFDTdataRecors = 0
86 self.RadarUnitId = 0
87 self.OffsetStartHeader = 0
88 self.SiteName = ""
90 89 self.size = 48
91
90
92 91 def FHread(self, fp):
93 #try:
94 startFp = open(fp,"rb")
95
96 header = numpy.fromfile(startFp, FILE_STRUCTURE,1)
97
92 # try:
93 startFp = open(fp, "rb")
94
95 header = numpy.fromfile(startFp, FILE_STRUCTURE, 1)
96
98 97 print ' '
99 98 print 'puntero file header', startFp.tell()
100 99 print ' '
101
102
100
103 101 ''' numpy.fromfile(file, dtype, count, sep='')
104 102 file : file or str
105 103 Open file object or filename.
106 104
107 105 dtype : data-type
108 106 Data type of the returned array. For binary files, it is used to determine
109 107 the size and byte-order of the items in the file.
110 108
111 109 count : int
112 110 Number of items to read. -1 means all items (i.e., the complete file).
113 111
114 112 sep : str
115 113 Separator between items if file is a text file. Empty ("") separator means
116 114 the file should be treated as binary. Spaces (" ") in the separator match zero
117 115 or more whitespace characters. A separator consisting only of spaces must match
118 116 at least one whitespace.
119 117
120 118 '''
121 119
120 self.FileMgcNumber = hex(header['FileMgcNumber'][0])
121 # No Of FDT data records in this file (0 or more)
122 self.nFDTdataRecors = int(header['nFDTdataRecors'][0])
123 self.RadarUnitId = int(header['RadarUnitId'][0])
124 self.OffsetStartHeader = int(header['OffsetStartHeader'][0])
125 self.SiteName = str(header['SiteName'][0])
126
127 # print 'Numero de bloques', self.nFDTdataRecors
122 128
123
124 self.FileMgcNumber= hex(header['FileMgcNumber'][0])
125 self.nFDTdataRecors=int(header['nFDTdataRecors'][0]) #No Of FDT data records in this file (0 or more)
126 self.RadarUnitId= int(header['RadarUnitId'][0])
127 self.OffsetStartHeader= int(header['OffsetStartHeader'][0])
128 self.SiteName= str(header['SiteName'][0])
129
130 #print 'Numero de bloques', self.nFDTdataRecors
131
132
133 if self.size <48:
129 if self.size < 48:
134 130 return 0
135
131
136 132 return 1
137
138
133
139 134 def write(self, fp):
140
135
141 136 headerTuple = (self.FileMgcNumber,
142 137 self.nFDTdataRecors,
143 138 self.RadarUnitId,
144 139 self.SiteName,
145 140 self.size)
146
147
141
148 142 header = numpy.array(headerTuple, FILE_STRUCTURE)
149 143 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
150 144 header.tofile(fp)
151 145 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
152 146
153 147 fid : file or str
154 148 An open file object, or a string containing a filename.
155 149
156 150 sep : str
157 151 Separator between array items for text output. If "" (empty), a binary file is written,
158 152 equivalent to file.write(a.tobytes()).
159 153
160 154 format : str
161 155 Format string for text file output. Each entry in the array is formatted to text by
162 156 first converting it to the closest Python type, and then using "format" % item.
163 157
164 158 '''
165
166 return 1
167 159
160 return 1
168 161
169 162
163 RECORD_STRUCTURE = numpy.dtype([ # RECORD HEADER 180+20N bytes
164 ('RecMgcNumber', '<u4'), # 0x23030001
165 ('RecCounter', '<u4'), # Record counter(0,1, ...)
166 # Offset to start of next record form start of this record
167 ('Off2StartNxtRec', '<u4'),
168 # Offset to start of data from start of this record
169 ('Off2StartData', '<u4'),
170 # Epoch time stamp of start of acquisition (seconds)
171 ('nUtime', '<i4'),
172 # Millisecond component of time stamp (0,...,999)
173 ('nMilisec', '<u4'),
174 # Experiment tag name (null terminated)
175 ('ExpTagName', numpy.str_, 32),
176 # Experiment comment (null terminated)
177 ('ExpComment', numpy.str_, 32),
178 # Site latitude (from GPS) in degrees (positive implies North)
179 ('SiteLatDegrees', '<f4'),
180 # Site longitude (from GPS) in degrees (positive implies East)
181 ('SiteLongDegrees', '<f4'),
182 # RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
183 ('RTCgpsStatus', '<u4'),
184 ('TransmitFrec', '<u4'), # Transmit frequency (Hz)
185 ('ReceiveFrec', '<u4'), # Receive frequency
186 # First local oscillator frequency (Hz)
187 ('FirstOsciFrec', '<u4'),
188 # (0="O", 1="E", 2="linear 1", 3="linear2")
189 ('Polarisation', '<u4'),
190 # Receiver filter settings (0,1,2,3)
191 ('ReceiverFiltSett', '<u4'),
192 # Number of modes in use (1 or 2)
193 ('nModesInUse', '<u4'),
194 # Dual Mode index number for these data (0 or 1)
195 ('DualModeIndex', '<u4'),
196 # Dual Mode range correction for these data (m)
197 ('DualModeRange', '<u4'),
198 # Number of digital channels acquired (2*N)
199 ('nDigChannels', '<u4'),
200 # Sampling resolution (meters)
201 ('SampResolution', '<u4'),
202 # Number of range gates sampled
203 ('nHeights', '<u4'),
204 # Start range of sampling (meters)
205 ('StartRangeSamp', '<u4'),
206 ('PRFhz', '<u4'), # PRF (Hz)
207 ('nCohInt', '<u4'), # Integrations
208 # Number of data points transformed
209 ('nProfiles', '<u4'),
210 # Number of receive beams stored in file (1 or N)
211 ('nChannels', '<u4'),
212 ('nIncohInt', '<u4'), # Number of spectral averages
213 # FFT windowing index (0 = no window)
214 ('FFTwindowingInd', '<u4'),
215 # Beam steer angle (azimuth) in degrees (clockwise from true North)
216 ('BeamAngleAzim', '<f4'),
217 # Beam steer angle (zenith) in degrees (0=> vertical)
218 ('BeamAngleZen', '<f4'),
219 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
220 ('AntennaCoord0', '<f4'),
221 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
222 ('AntennaAngl0', '<f4'),
223 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
224 ('AntennaCoord1', '<f4'),
225 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
226 ('AntennaAngl1', '<f4'),
227 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
228 ('AntennaCoord2', '<f4'),
229 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
230 ('AntennaAngl2', '<f4'),
231 # Receiver phase calibration (degrees) - N values
232 ('RecPhaseCalibr0', '<f4'),
233 # Receiver phase calibration (degrees) - N values
234 ('RecPhaseCalibr1', '<f4'),
235 # Receiver phase calibration (degrees) - N values
236 ('RecPhaseCalibr2', '<f4'),
237 # Receiver amplitude calibration (ratio relative to receiver one) - N values
238 ('RecAmpCalibr0', '<f4'),
239 # Receiver amplitude calibration (ratio relative to receiver one) - N values
240 ('RecAmpCalibr1', '<f4'),
241 # Receiver amplitude calibration (ratio relative to receiver one) - N values
242 ('RecAmpCalibr2', '<f4'),
243 # Receiver gains in dB - N values
244 ('ReceiverGaindB0', '<i4'),
245 # Receiver gains in dB - N values
246 ('ReceiverGaindB1', '<i4'),
247 # Receiver gains in dB - N values
248 ('ReceiverGaindB2', '<i4'),
249 ])
170 250
171 251
172 RECORD_STRUCTURE = numpy.dtype([ #RECORD HEADER 180+20N bytes
173 ('RecMgcNumber','<u4'), #0x23030001
174 ('RecCounter','<u4'), #Record counter(0,1, ...)
175 ('Off2StartNxtRec','<u4'), #Offset to start of next record form start of this record
176 ('Off2StartData','<u4'), #Offset to start of data from start of this record
177 ('nUtime','<i4'), #Epoch time stamp of start of acquisition (seconds)
178 ('nMilisec','<u4'), #Millisecond component of time stamp (0,...,999)
179 ('ExpTagName',numpy.str_,32), #Experiment tag name (null terminated)
180 ('ExpComment',numpy.str_,32), #Experiment comment (null terminated)
181 ('SiteLatDegrees','<f4'), #Site latitude (from GPS) in degrees (positive implies North)
182 ('SiteLongDegrees','<f4'), #Site longitude (from GPS) in degrees (positive implies East)
183 ('RTCgpsStatus','<u4'), #RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
184 ('TransmitFrec','<u4'), #Transmit frequency (Hz)
185 ('ReceiveFrec','<u4'), #Receive frequency
186 ('FirstOsciFrec','<u4'), #First local oscillator frequency (Hz)
187 ('Polarisation','<u4'), #(0="O", 1="E", 2="linear 1", 3="linear2")
188 ('ReceiverFiltSett','<u4'), #Receiver filter settings (0,1,2,3)
189 ('nModesInUse','<u4'), #Number of modes in use (1 or 2)
190 ('DualModeIndex','<u4'), #Dual Mode index number for these data (0 or 1)
191 ('DualModeRange','<u4'), #Dual Mode range correction for these data (m)
192 ('nDigChannels','<u4'), #Number of digital channels acquired (2*N)
193 ('SampResolution','<u4'), #Sampling resolution (meters)
194 ('nHeights','<u4'), #Number of range gates sampled
195 ('StartRangeSamp','<u4'), #Start range of sampling (meters)
196 ('PRFhz','<u4'), #PRF (Hz)
197 ('nCohInt','<u4'), #Integrations
198 ('nProfiles','<u4'), #Number of data points transformed
199 ('nChannels','<u4'), #Number of receive beams stored in file (1 or N)
200 ('nIncohInt','<u4'), #Number of spectral averages
201 ('FFTwindowingInd','<u4'), #FFT windowing index (0 = no window)
202 ('BeamAngleAzim','<f4'), #Beam steer angle (azimuth) in degrees (clockwise from true North)
203 ('BeamAngleZen','<f4'), #Beam steer angle (zenith) in degrees (0=> vertical)
204 ('AntennaCoord0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
205 ('AntennaAngl0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
206 ('AntennaCoord1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
207 ('AntennaAngl1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
208 ('AntennaCoord2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
209 ('AntennaAngl2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
210 ('RecPhaseCalibr0','<f4'), #Receiver phase calibration (degrees) - N values
211 ('RecPhaseCalibr1','<f4'), #Receiver phase calibration (degrees) - N values
212 ('RecPhaseCalibr2','<f4'), #Receiver phase calibration (degrees) - N values
213 ('RecAmpCalibr0','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
214 ('RecAmpCalibr1','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
215 ('RecAmpCalibr2','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
216 ('ReceiverGaindB0','<i4'), #Receiver gains in dB - N values
217 ('ReceiverGaindB1','<i4'), #Receiver gains in dB - N values
218 ('ReceiverGaindB2','<i4'), #Receiver gains in dB - N values
219 ])
252 class RecordHeaderBLTR(Header):
220 253
254 def __init__(self, RecMgcNumber=None, RecCounter=0, Off2StartNxtRec=811248,
255 nUtime=0, nMilisec=0, ExpTagName=None,
256 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees=0,
257 RTCgpsStatus=0, TransmitFrec=0, ReceiveFrec=0,
258 FirstOsciFrec=0, Polarisation=0, ReceiverFiltSett=0,
259 nModesInUse=0, DualModeIndex=0, DualModeRange=0,
260 nDigChannels=0, SampResolution=0, nHeights=0,
261 StartRangeSamp=0, PRFhz=0, nCohInt=0,
262 nProfiles=0, nChannels=0, nIncohInt=0,
263 FFTwindowingInd=0, BeamAngleAzim=0, BeamAngleZen=0,
264 AntennaCoord0=0, AntennaCoord1=0, AntennaCoord2=0,
265 RecPhaseCalibr0=0, RecPhaseCalibr1=0, RecPhaseCalibr2=0,
266 RecAmpCalibr0=0, RecAmpCalibr1=0, RecAmpCalibr2=0,
267 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
268 ReceiverGaindB0=0, ReceiverGaindB1=0, ReceiverGaindB2=0, Off2StartData=0, OffsetStartHeader=0):
221 269
222 class RecordHeaderBLTR(Header):
223
224 def __init__(self, RecMgcNumber=None, RecCounter= 0, Off2StartNxtRec= 811248,
225 nUtime= 0, nMilisec= 0, ExpTagName= None,
226 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees= 0,
227 RTCgpsStatus= 0, TransmitFrec= 0, ReceiveFrec= 0,
228 FirstOsciFrec= 0, Polarisation= 0, ReceiverFiltSett= 0,
229 nModesInUse= 0, DualModeIndex= 0, DualModeRange= 0,
230 nDigChannels= 0, SampResolution= 0, nHeights= 0,
231 StartRangeSamp= 0, PRFhz= 0, nCohInt= 0,
232 nProfiles= 0, nChannels= 0, nIncohInt= 0,
233 FFTwindowingInd= 0, BeamAngleAzim= 0, BeamAngleZen= 0,
234 AntennaCoord0= 0, AntennaCoord1= 0, AntennaCoord2= 0,
235 RecPhaseCalibr0= 0, RecPhaseCalibr1= 0, RecPhaseCalibr2= 0,
236 RecAmpCalibr0= 0, RecAmpCalibr1= 0, RecAmpCalibr2= 0,
237 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
238 ReceiverGaindB0= 0, ReceiverGaindB1= 0, ReceiverGaindB2= 0, Off2StartData=0, OffsetStartHeader=0):
239
240 self.RecMgcNumber = RecMgcNumber #0x23030001
241 self.RecCounter = RecCounter
242 self.Off2StartNxtRec = Off2StartNxtRec
270 self.RecMgcNumber = RecMgcNumber # 0x23030001
271 self.RecCounter = RecCounter
272 self.Off2StartNxtRec = Off2StartNxtRec
243 273 self.Off2StartData = Off2StartData
244 self.nUtime = nUtime
245 self.nMilisec = nMilisec
246 self.ExpTagName = ExpTagName
247 self.ExpComment = ExpComment
248 self.SiteLatDegrees = SiteLatDegrees
249 self.SiteLongDegrees = SiteLongDegrees
250 self.RTCgpsStatus = RTCgpsStatus
251 self.TransmitFrec = TransmitFrec
252 self.ReceiveFrec = ReceiveFrec
253 self.FirstOsciFrec = FirstOsciFrec
254 self.Polarisation = Polarisation
255 self.ReceiverFiltSett = ReceiverFiltSett
256 self.nModesInUse = nModesInUse
257 self.DualModeIndex = DualModeIndex
258 self.DualModeRange = DualModeRange
274 self.nUtime = nUtime
275 self.nMilisec = nMilisec
276 self.ExpTagName = ExpTagName
277 self.ExpComment = ExpComment
278 self.SiteLatDegrees = SiteLatDegrees
279 self.SiteLongDegrees = SiteLongDegrees
280 self.RTCgpsStatus = RTCgpsStatus
281 self.TransmitFrec = TransmitFrec
282 self.ReceiveFrec = ReceiveFrec
283 self.FirstOsciFrec = FirstOsciFrec
284 self.Polarisation = Polarisation
285 self.ReceiverFiltSett = ReceiverFiltSett
286 self.nModesInUse = nModesInUse
287 self.DualModeIndex = DualModeIndex
288 self.DualModeRange = DualModeRange
259 289 self.nDigChannels = nDigChannels
260 self.SampResolution = SampResolution
261 self.nHeights = nHeights
262 self.StartRangeSamp = StartRangeSamp
263 self.PRFhz = PRFhz
264 self.nCohInt = nCohInt
265 self.nProfiles = nProfiles
266 self.nChannels = nChannels
267 self.nIncohInt = nIncohInt
268 self.FFTwindowingInd = FFTwindowingInd
269 self.BeamAngleAzim = BeamAngleAzim
270 self.BeamAngleZen = BeamAngleZen
290 self.SampResolution = SampResolution
291 self.nHeights = nHeights
292 self.StartRangeSamp = StartRangeSamp
293 self.PRFhz = PRFhz
294 self.nCohInt = nCohInt
295 self.nProfiles = nProfiles
296 self.nChannels = nChannels
297 self.nIncohInt = nIncohInt
298 self.FFTwindowingInd = FFTwindowingInd
299 self.BeamAngleAzim = BeamAngleAzim
300 self.BeamAngleZen = BeamAngleZen
271 301 self.AntennaCoord0 = AntennaCoord0
272 302 self.AntennaAngl0 = AntennaAngl0
273 303 self.AntennaAngl1 = AntennaAngl1
274 304 self.AntennaAngl2 = AntennaAngl2
275 305 self.AntennaCoord1 = AntennaCoord1
276 self.AntennaCoord2 = AntennaCoord2
306 self.AntennaCoord2 = AntennaCoord2
277 307 self.RecPhaseCalibr0 = RecPhaseCalibr0
278 308 self.RecPhaseCalibr1 = RecPhaseCalibr1
279 self.RecPhaseCalibr2 = RecPhaseCalibr2
309 self.RecPhaseCalibr2 = RecPhaseCalibr2
280 310 self.RecAmpCalibr0 = RecAmpCalibr0
281 311 self.RecAmpCalibr1 = RecAmpCalibr1
282 self.RecAmpCalibr2 = RecAmpCalibr2
312 self.RecAmpCalibr2 = RecAmpCalibr2
283 313 self.ReceiverGaindB0 = ReceiverGaindB0
284 314 self.ReceiverGaindB1 = ReceiverGaindB1
285 self.ReceiverGaindB2 = ReceiverGaindB2
286 self.OffsetStartHeader = 48
287
288
289
315 self.ReceiverGaindB2 = ReceiverGaindB2
316 self.OffsetStartHeader = 48
317
290 318 def RHread(self, fp):
291 #print fp
292 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
293 startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
294 #RecCounter=0
295 #Off2StartNxtRec=811248
296 OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
319 # print fp
320 # startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
321 # The method tell() returns the current position of the file read/write pointer within the file.
322 startFp = open(fp, "rb")
323 # RecCounter=0
324 # Off2StartNxtRec=811248
325 OffRHeader = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
297 326 print ' '
298 327 print 'puntero Record Header', startFp.tell()
299 328 print ' '
300
301
329
302 330 startFp.seek(OffRHeader, os.SEEK_SET)
303
331
304 332 print ' '
305 333 print 'puntero Record Header con seek', startFp.tell()
306 334 print ' '
307
308 #print 'Posicion del bloque: ',OffRHeader
309
310 header = numpy.fromfile(startFp,RECORD_STRUCTURE,1)
311
335
336 # print 'Posicion del bloque: ',OffRHeader
337
338 header = numpy.fromfile(startFp, RECORD_STRUCTURE, 1)
339
312 340 print ' '
313 341 print 'puntero Record Header con seek', startFp.tell()
314 342 print ' '
315
343
316 344 print ' '
317 345 #
318 #print 'puntero Record Header despues de seek', header.tell()
346 # print 'puntero Record Header despues de seek', header.tell()
319 347 print ' '
320
321 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) #0x23030001
322 self.RecCounter = int(header['RecCounter'][0])
323 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
324 self.Off2StartData = int(header['Off2StartData'][0])
325 self.nUtime = header['nUtime'][0]
326 self.nMilisec = header['nMilisec'][0]
327 self.ExpTagName = str(header['ExpTagName'][0])
328 self.ExpComment = str(header['ExpComment'][0])
329 self.SiteLatDegrees = header['SiteLatDegrees'][0]
330 self.SiteLongDegrees = header['SiteLongDegrees'][0]
331 self.RTCgpsStatus = header['RTCgpsStatus'][0]
332 self.TransmitFrec = header['TransmitFrec'][0]
333 self.ReceiveFrec = header['ReceiveFrec'][0]
334 self.FirstOsciFrec = header['FirstOsciFrec'][0]
335 self.Polarisation = header['Polarisation'][0]
336 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
337 self.nModesInUse = header['nModesInUse'][0]
338 self.DualModeIndex = header['DualModeIndex'][0]
339 self.DualModeRange = header['DualModeRange'][0]
348
349 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) # 0x23030001
350 self.RecCounter = int(header['RecCounter'][0])
351 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
352 self.Off2StartData = int(header['Off2StartData'][0])
353 self.nUtime = header['nUtime'][0]
354 self.nMilisec = header['nMilisec'][0]
355 self.ExpTagName = str(header['ExpTagName'][0])
356 self.ExpComment = str(header['ExpComment'][0])
357 self.SiteLatDegrees = header['SiteLatDegrees'][0]
358 self.SiteLongDegrees = header['SiteLongDegrees'][0]
359 self.RTCgpsStatus = header['RTCgpsStatus'][0]
360 self.TransmitFrec = header['TransmitFrec'][0]
361 self.ReceiveFrec = header['ReceiveFrec'][0]
362 self.FirstOsciFrec = header['FirstOsciFrec'][0]
363 self.Polarisation = header['Polarisation'][0]
364 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
365 self.nModesInUse = header['nModesInUse'][0]
366 self.DualModeIndex = header['DualModeIndex'][0]
367 self.DualModeRange = header['DualModeRange'][0]
340 368 self.nDigChannels = header['nDigChannels'][0]
341 self.SampResolution = header['SampResolution'][0]
342 self.nHeights = header['nHeights'][0]
343 self.StartRangeSamp = header['StartRangeSamp'][0]
344 self.PRFhz = header['PRFhz'][0]
345 self.nCohInt = header['nCohInt'][0]
346 self.nProfiles = header['nProfiles'][0]
347 self.nChannels = header['nChannels'][0]
348 self.nIncohInt = header['nIncohInt'][0]
349 self.FFTwindowingInd = header['FFTwindowingInd'][0]
350 self.BeamAngleAzim = header['BeamAngleAzim'][0]
351 self.BeamAngleZen = header['BeamAngleZen'][0]
369 self.SampResolution = header['SampResolution'][0]
370 self.nHeights = header['nHeights'][0]
371 self.StartRangeSamp = header['StartRangeSamp'][0]
372 self.PRFhz = header['PRFhz'][0]
373 self.nCohInt = header['nCohInt'][0]
374 self.nProfiles = header['nProfiles'][0]
375 self.nChannels = header['nChannels'][0]
376 self.nIncohInt = header['nIncohInt'][0]
377 self.FFTwindowingInd = header['FFTwindowingInd'][0]
378 self.BeamAngleAzim = header['BeamAngleAzim'][0]
379 self.BeamAngleZen = header['BeamAngleZen'][0]
352 380 self.AntennaCoord0 = header['AntennaCoord0'][0]
353 381 self.AntennaAngl0 = header['AntennaAngl0'][0]
354 382 self.AntennaCoord1 = header['AntennaCoord1'][0]
355 self.AntennaAngl1 = header['AntennaAngl1'][0]
383 self.AntennaAngl1 = header['AntennaAngl1'][0]
356 384 self.AntennaCoord2 = header['AntennaCoord2'][0]
357 self.AntennaAngl2 = header['AntennaAngl2'][0]
385 self.AntennaAngl2 = header['AntennaAngl2'][0]
358 386 self.RecPhaseCalibr0 = header['RecPhaseCalibr0'][0]
359 387 self.RecPhaseCalibr1 = header['RecPhaseCalibr1'][0]
360 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
388 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
361 389 self.RecAmpCalibr0 = header['RecAmpCalibr0'][0]
362 390 self.RecAmpCalibr1 = header['RecAmpCalibr1'][0]
363 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
391 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
364 392 self.ReceiverGaindB0 = header['ReceiverGaindB0'][0]
365 393 self.ReceiverGaindB1 = header['ReceiverGaindB1'][0]
366 394 self.ReceiverGaindB2 = header['ReceiverGaindB2'][0]
367 395
368 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
369
370 self.RHsize = 180+20*self.nChannels
371 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
372 #print 'Datasize',self.Datasize
373 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
374
396 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
397
398 self.RHsize = 180 + 20 * self.nChannels
399 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
400 # print 'Datasize',self.Datasize
401 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
402
375 403 print '=============================================='
376 print 'RecMgcNumber ',self.RecMgcNumber
377 print 'RecCounter ',self.RecCounter
378 print 'Off2StartNxtRec ',self.Off2StartNxtRec
379 print 'Off2StartData ',self.Off2StartData
380 print 'Range Resolution ',self.SampResolution
381 print 'First Height ',self.StartRangeSamp
382 print 'PRF (Hz) ',self.PRFhz
383 print 'Heights (K) ',self.nHeights
384 print 'Channels (N) ',self.nChannels
385 print 'Profiles (J) ',self.nProfiles
386 print 'iCoh ',self.nCohInt
387 print 'iInCoh ',self.nIncohInt
388 print 'BeamAngleAzim ',self.BeamAngleAzim
389 print 'BeamAngleZen ',self.BeamAngleZen
390
391 #print 'ModoEnUso ',self.DualModeIndex
392 #print 'UtcTime ',self.nUtime
393 #print 'MiliSec ',self.nMilisec
394 #print 'Exp TagName ',self.ExpTagName
395 #print 'Exp Comment ',self.ExpComment
396 #print 'FFT Window Index ',self.FFTwindowingInd
397 #print 'N Dig. Channels ',self.nDigChannels
398 print 'Size de bloque ',self.RHsize
399 print 'DataSize ',self.Datasize
400 print 'BeamAngleAzim ',self.BeamAngleAzim
401 #print 'AntennaCoord0 ',self.AntennaCoord0
402 #print 'AntennaAngl0 ',self.AntennaAngl0
403 #print 'AntennaCoord1 ',self.AntennaCoord1
404 #print 'AntennaAngl1 ',self.AntennaAngl1
405 #print 'AntennaCoord2 ',self.AntennaCoord2
406 #print 'AntennaAngl2 ',self.AntennaAngl2
407 print 'RecPhaseCalibr0 ',self.RecPhaseCalibr0
408 print 'RecPhaseCalibr1 ',self.RecPhaseCalibr1
409 print 'RecPhaseCalibr2 ',self.RecPhaseCalibr2
410 print 'RecAmpCalibr0 ',self.RecAmpCalibr0
411 print 'RecAmpCalibr1 ',self.RecAmpCalibr1
412 print 'RecAmpCalibr2 ',self.RecAmpCalibr2
413 print 'ReceiverGaindB0 ',self.ReceiverGaindB0
414 print 'ReceiverGaindB1 ',self.ReceiverGaindB1
415 print 'ReceiverGaindB2 ',self.ReceiverGaindB2
404 print 'RecMgcNumber ', self.RecMgcNumber
405 print 'RecCounter ', self.RecCounter
406 print 'Off2StartNxtRec ', self.Off2StartNxtRec
407 print 'Off2StartData ', self.Off2StartData
408 print 'Range Resolution ', self.SampResolution
409 print 'First Height ', self.StartRangeSamp
410 print 'PRF (Hz) ', self.PRFhz
411 print 'Heights (K) ', self.nHeights
412 print 'Channels (N) ', self.nChannels
413 print 'Profiles (J) ', self.nProfiles
414 print 'iCoh ', self.nCohInt
415 print 'iInCoh ', self.nIncohInt
416 print 'BeamAngleAzim ', self.BeamAngleAzim
417 print 'BeamAngleZen ', self.BeamAngleZen
418
419 # print 'ModoEnUso ',self.DualModeIndex
420 # print 'UtcTime ',self.nUtime
421 # print 'MiliSec ',self.nMilisec
422 # print 'Exp TagName ',self.ExpTagName
423 # print 'Exp Comment ',self.ExpComment
424 # print 'FFT Window Index ',self.FFTwindowingInd
425 # print 'N Dig. Channels ',self.nDigChannels
426 print 'Size de bloque ', self.RHsize
427 print 'DataSize ', self.Datasize
428 print 'BeamAngleAzim ', self.BeamAngleAzim
429 # print 'AntennaCoord0 ',self.AntennaCoord0
430 # print 'AntennaAngl0 ',self.AntennaAngl0
431 # print 'AntennaCoord1 ',self.AntennaCoord1
432 # print 'AntennaAngl1 ',self.AntennaAngl1
433 # print 'AntennaCoord2 ',self.AntennaCoord2
434 # print 'AntennaAngl2 ',self.AntennaAngl2
435 print 'RecPhaseCalibr0 ', self.RecPhaseCalibr0
436 print 'RecPhaseCalibr1 ', self.RecPhaseCalibr1
437 print 'RecPhaseCalibr2 ', self.RecPhaseCalibr2
438 print 'RecAmpCalibr0 ', self.RecAmpCalibr0
439 print 'RecAmpCalibr1 ', self.RecAmpCalibr1
440 print 'RecAmpCalibr2 ', self.RecAmpCalibr2
441 print 'ReceiverGaindB0 ', self.ReceiverGaindB0
442 print 'ReceiverGaindB1 ', self.ReceiverGaindB1
443 print 'ReceiverGaindB2 ', self.ReceiverGaindB2
416 444 print '=============================================='
417
445
418 446 if OffRHeader > endFp:
419 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp)
447 sys.stderr.write(
448 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp)
420 449 return 0
421
450
422 451 if OffRHeader < endFp:
423 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp)
452 sys.stderr.write(
453 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp)
424 454 return 0
425
455
426 456 return 1
427 457
428
458
429 459 class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader):
430
460
431 461 path = None
432 462 startDate = None
433 463 endDate = None
434 464 startTime = None
435 465 endTime = None
436 466 walk = None
437 467 isConfig = False
438
439
440 fileList= None
441
442 #metadata
443 TimeZone= None
444 Interval= None
445 heightList= None
446
447 #data
448 data= None
449 utctime= None
450
451
452
468
469 fileList = None
470
471 # metadata
472 TimeZone = None
473 Interval = None
474 heightList = None
475
476 # data
477 data = None
478 utctime = None
479
453 480 def __init__(self, **kwargs):
454
455 #Eliminar de la base la herencia
481
482 # Eliminar de la base la herencia
456 483 ProcessingUnit.__init__(self, **kwargs)
457
484
458 485 #self.isConfig = False
459
486
460 487 #self.pts2read_SelfSpectra = 0
461 488 #self.pts2read_CrossSpectra = 0
462 489 #self.pts2read_DCchannels = 0
463 490 #self.datablock = None
464 491 self.utc = None
465 492 self.ext = ".fdt"
466 493 self.optchar = "P"
467 self.fpFile=None
494 self.fpFile = None
468 495 self.fp = None
469 self.BlockCounter=0
496 self.BlockCounter = 0
470 497 self.dtype = None
471 498 self.fileSizeByHeader = None
472 499 self.filenameList = []
473 500 self.fileSelector = 0
474 self.Off2StartNxtRec=0
475 self.RecCounter=0
501 self.Off2StartNxtRec = 0
502 self.RecCounter = 0
476 503 self.flagNoMoreFiles = 0
477 self.data_spc=None
478 self.data_cspc=None
479 self.data_output=None
504 self.data_spc = None
505 self.data_cspc = None
506 self.data_output = None
480 507 self.path = None
481 self.OffsetStartHeader=0
482 self.Off2StartData=0
508 self.OffsetStartHeader = 0
509 self.Off2StartData = 0
483 510 self.ipp = 0
484 self.nFDTdataRecors=0
511 self.nFDTdataRecors = 0
485 512 self.blocksize = 0
486 513 self.dataOut = Spectra()
487 self.profileIndex = 1 #Always
488 self.dataOut.flagNoData=False
514 self.profileIndex = 1 # Always
515 self.dataOut.flagNoData = False
489 516 self.dataOut.nRdPairs = 0
490 517 self.dataOut.pairsList = []
491 self.dataOut.data_spc=None
492 self.dataOut.noise=[]
493 self.dataOut.velocityX=[]
494 self.dataOut.velocityY=[]
495 self.dataOut.velocityV=[]
496
497
518 self.dataOut.data_spc = None
519 self.dataOut.noise = []
520 self.dataOut.velocityX = []
521 self.dataOut.velocityY = []
522 self.dataOut.velocityV = []
498 523
499 524 def Files2Read(self, fp):
500 525 '''
501 526 Function that indicates the number of .fdt files that exist in the folder to be read.
502 527 It also creates an organized list with the names of the files to read.
503 528 '''
504 #self.__checkPath()
505
506 ListaData=os.listdir(fp) #Gets the list of files within the fp address
507 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
508 nFiles=0 #File Counter
509 FileList=[] #A list is created that will contain the .fdt files
510 for IndexFile in ListaData :
511 if '.fdt' in IndexFile:
529 # self.__checkPath()
530
531 # Gets the list of files within the fp address
532 ListaData = os.listdir(fp)
533 # Sort the list of files from least to largest by names
534 ListaData = sorted(ListaData)
535 nFiles = 0 # File Counter
536 FileList = [] # A list is created that will contain the .fdt files
537 for IndexFile in ListaData:
538 if '.fdt' in IndexFile:
512 539 FileList.append(IndexFile)
513 nFiles+=1
514
515 #print 'Files2Read'
516 #print 'Existen '+str(nFiles)+' archivos .fdt'
517
518 self.filenameList=FileList #List of files from least to largest by names
519
520
540 nFiles += 1
541
542 # print 'Files2Read'
543 # print 'Existen '+str(nFiles)+' archivos .fdt'
544
545 self.filenameList = FileList # List of files from least to largest by names
546
521 547 def run(self, **kwargs):
522 548 '''
523 549 This method will be the one that will initiate the data entry, will be called constantly.
524 550 You should first verify that your Setup () is set up and then continue to acquire
525 551 the data to be processed with getData ().
526 552 '''
527 553 if not self.isConfig:
528 554 self.setup(**kwargs)
529 555 self.isConfig = True
530
556
531 557 self.getData()
532 #print 'running'
533
534
558 # print 'running'
559
535 560 def setup(self, path=None,
536 startDate=None,
537 endDate=None,
538 startTime=None,
539 endTime=None,
540 walk=True,
541 timezone='utc',
542 code = None,
543 online=False,
544 ReadMode=None,
545 **kwargs):
546
561 startDate=None,
562 endDate=None,
563 startTime=None,
564 endTime=None,
565 walk=True,
566 timezone='utc',
567 code=None,
568 online=False,
569 ReadMode=None,
570 **kwargs):
571
547 572 self.isConfig = True
548
549 self.path=path
550 self.startDate=startDate
551 self.endDate=endDate
552 self.startTime=startTime
553 self.endTime=endTime
554 self.walk=walk
555 self.ReadMode=int(ReadMode)
556
573
574 self.path = path
575 self.startDate = startDate
576 self.endDate = endDate
577 self.startTime = startTime
578 self.endTime = endTime
579 self.walk = walk
580 self.ReadMode = int(ReadMode)
581
557 582 pass
558
559
583
560 584 def getData(self):
561 585 '''
562 586 Before starting this function, you should check that there is still an unread file,
563 587 If there are still blocks to read or if the data block is empty.
564
588
565 589 You should call the file "read".
566
590
567 591 '''
568
592
569 593 if self.flagNoMoreFiles:
570 594 self.dataOut.flagNoData = True
571 595 print 'NoData se vuelve true'
572 596 return 0
573 597
574 self.fp=self.path
598 self.fp = self.path
575 599 self.Files2Read(self.fp)
576 600 self.readFile(self.fp)
577 601 self.dataOut.data_spc = self.data_spc
578 self.dataOut.data_cspc =self.data_cspc
579 self.dataOut.data_output=self.data_output
580
602 self.dataOut.data_cspc = self.data_cspc
603 self.dataOut.data_output = self.data_output
604
581 605 print 'self.dataOut.data_output', shape(self.dataOut.data_output)
582
583 #self.removeDC()
584 return self.dataOut.data_spc
585
586
587 def readFile(self,fp):
606
607 # self.removeDC()
608 return self.dataOut.data_spc
609
610 def readFile(self, fp):
588 611 '''
589 612 You must indicate if you are reading in Online or Offline mode and load the
590 613 The parameters for this file reading mode.
591
614
592 615 Then you must do 2 actions:
593
616
594 617 1. Get the BLTR FileHeader.
595 618 2. Start reading the first block.
596 619 '''
597
598 #The address of the folder is generated the name of the .fdt file that will be read
599 print "File: ",self.fileSelector+1
600
620
621 # The address of the folder is generated the name of the .fdt file that will be read
622 print "File: ", self.fileSelector + 1
623
601 624 if self.fileSelector < len(self.filenameList):
602
603 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
604 #print self.fpFile
625
626 self.fpFile = str(fp) + '/' + \
627 str(self.filenameList[self.fileSelector])
628 # print self.fpFile
605 629 fheader = FileHeaderBLTR()
606 fheader.FHread(self.fpFile) #Bltr FileHeader Reading
607 self.nFDTdataRecors=fheader.nFDTdataRecors
608
609 self.readBlock() #Block reading
630 fheader.FHread(self.fpFile) # Bltr FileHeader Reading
631 self.nFDTdataRecors = fheader.nFDTdataRecors
632
633 self.readBlock() # Block reading
610 634 else:
611 635 print 'readFile FlagNoData becomes true'
612 self.flagNoMoreFiles=True
636 self.flagNoMoreFiles = True
613 637 self.dataOut.flagNoData = True
614 return 0
615
638 return 0
639
616 640 def getVelRange(self, extrapoints=0):
617 Lambda= SPEED_OF_LIGHT/50000000
618 PRF = self.dataOut.PRF#1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
619 Vmax=-Lambda/(4.*(1./PRF)*self.dataOut.nCohInt*2.)
620 deltafreq = PRF / (self.nProfiles)
621 deltavel = (Vmax*2) / (self.nProfiles)
622 freqrange = deltafreq*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - deltafreq/2
623 velrange = deltavel*(numpy.arange(self.nProfiles)-self.nProfiles/2.)
624 return velrange
625
626 def readBlock(self):
641 Lambda = SPEED_OF_LIGHT / 50000000
642 # 1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
643 PRF = self.dataOut.PRF
644 Vmax = -Lambda / (4. * (1. / PRF) * self.dataOut.nCohInt * 2.)
645 deltafreq = PRF / (self.nProfiles)
646 deltavel = (Vmax * 2) / (self.nProfiles)
647 freqrange = deltafreq * \
648 (numpy.arange(self.nProfiles) - self.nProfiles / 2.) - deltafreq / 2
649 velrange = deltavel * \
650 (numpy.arange(self.nProfiles) - self.nProfiles / 2.)
651 return velrange
652
653 def readBlock(self):
627 654 '''
628 655 It should be checked if the block has data, if it is not passed to the next file.
629
656
630 657 Then the following is done:
631
658
632 659 1. Read the RecordHeader
633 660 2. Fill the buffer with the current block number.
634
661
635 662 '''
636
637 if self.BlockCounter < self.nFDTdataRecors-2:
663
664 if self.BlockCounter < self.nFDTdataRecors - 2:
638 665 print self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
639 if self.ReadMode==1:
640 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1)
641 elif self.ReadMode==0:
666 if self.ReadMode == 1:
667 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter + 1)
668 elif self.ReadMode == 0:
642 669 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter)
643
644 rheader.RHread(self.fpFile) #Bltr FileHeader Reading
645
646 self.OffsetStartHeader=rheader.OffsetStartHeader
647 self.RecCounter=rheader.RecCounter
648 self.Off2StartNxtRec=rheader.Off2StartNxtRec
649 self.Off2StartData=rheader.Off2StartData
650 self.nProfiles=rheader.nProfiles
651 self.nChannels=rheader.nChannels
652 self.nHeights=rheader.nHeights
653 self.frequency=rheader.TransmitFrec
654 self.DualModeIndex=rheader.DualModeIndex
655
656 self.pairsList =[(0,1),(0,2),(1,2)]
670
671 rheader.RHread(self.fpFile) # Bltr FileHeader Reading
672
673 self.OffsetStartHeader = rheader.OffsetStartHeader
674 self.RecCounter = rheader.RecCounter
675 self.Off2StartNxtRec = rheader.Off2StartNxtRec
676 self.Off2StartData = rheader.Off2StartData
677 self.nProfiles = rheader.nProfiles
678 self.nChannels = rheader.nChannels
679 self.nHeights = rheader.nHeights
680 self.frequency = rheader.TransmitFrec
681 self.DualModeIndex = rheader.DualModeIndex
682
683 self.pairsList = [(0, 1), (0, 2), (1, 2)]
657 684 self.dataOut.pairsList = self.pairsList
658
659 self.nRdPairs=len(self.dataOut.pairsList)
685
686 self.nRdPairs = len(self.dataOut.pairsList)
660 687 self.dataOut.nRdPairs = self.nRdPairs
661
662 self.__firstHeigth=rheader.StartRangeSamp
663 self.__deltaHeigth=rheader.SampResolution
664 self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth
688
689 self.__firstHeigth = rheader.StartRangeSamp
690 self.__deltaHeigth = rheader.SampResolution
691 self.dataOut.heightList = self.__firstHeigth + \
692 numpy.array(range(self.nHeights)) * self.__deltaHeigth
665 693 self.dataOut.channelList = range(self.nChannels)
666 self.dataOut.nProfiles=rheader.nProfiles
667 self.dataOut.nIncohInt=rheader.nIncohInt
668 self.dataOut.nCohInt=rheader.nCohInt
669 self.dataOut.ippSeconds= 1/float(rheader.PRFhz)
670 self.dataOut.PRF=rheader.PRFhz
671 self.dataOut.nFFTPoints=rheader.nProfiles
672 self.dataOut.utctime=rheader.nUtime
673 self.dataOut.timeZone=0
674 self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt
675 self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
676
677 self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN
694 self.dataOut.nProfiles = rheader.nProfiles
695 self.dataOut.nIncohInt = rheader.nIncohInt
696 self.dataOut.nCohInt = rheader.nCohInt
697 self.dataOut.ippSeconds = 1 / float(rheader.PRFhz)
698 self.dataOut.PRF = rheader.PRFhz
699 self.dataOut.nFFTPoints = rheader.nProfiles
700 self.dataOut.utctime = rheader.nUtime
701 self.dataOut.timeZone = 0
702 self.dataOut.normFactor = self.dataOut.nProfiles * \
703 self.dataOut.nIncohInt * self.dataOut.nCohInt
704 self.dataOut.outputInterval = self.dataOut.ippSeconds * \
705 self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
706
707 self.data_output = numpy.ones([3, rheader.nHeights]) * numpy.NaN
678 708 print 'self.data_output', shape(self.data_output)
679 self.dataOut.velocityX=[]
680 self.dataOut.velocityY=[]
681 self.dataOut.velocityV=[]
682
709 self.dataOut.velocityX = []
710 self.dataOut.velocityY = []
711 self.dataOut.velocityV = []
712
683 713 '''Block Reading, the Block Data is received and Reshape is used to give it
684 714 shape.
685 '''
686
687 #Procedure to take the pointer to where the date block starts
688 startDATA = open(self.fpFile,"rb")
689 OffDATA= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec+self.Off2StartData
715 '''
716
717 # Procedure to take the pointer to where the date block starts
718 startDATA = open(self.fpFile, "rb")
719 OffDATA = self.OffsetStartHeader + self.RecCounter * \
720 self.Off2StartNxtRec + self.Off2StartData
690 721 startDATA.seek(OffDATA, os.SEEK_SET)
691
722
692 723 def moving_average(x, N=2):
693 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
694
695 def gaus(xSamples,a,x0,sigma):
696 return a*exp(-(xSamples-x0)**2/(2*sigma**2))
697
698 def Find(x,value):
724 return numpy.convolve(x, numpy.ones((N,)) / N)[(N - 1):]
725
726 def gaus(xSamples, a, x0, sigma):
727 return a * exp(-(xSamples - x0)**2 / (2 * sigma**2))
728
729 def Find(x, value):
699 730 for index in range(len(x)):
700 if x[index]==value:
701 return index
702
731 if x[index] == value:
732 return index
733
703 734 def pol2cart(rho, phi):
704 735 x = rho * numpy.cos(phi)
705 736 y = rho * numpy.sin(phi)
706 737 return(x, y)
707
708
709
710
711 if self.DualModeIndex==self.ReadMode:
712
713 self.data_fft = numpy.fromfile( startDATA, [('complex','<c8')],self.nProfiles*self.nChannels*self.nHeights )
714
715 self.data_fft=self.data_fft.astype(numpy.dtype('complex'))
716
717 self.data_block=numpy.reshape(self.data_fft,(self.nHeights, self.nChannels, self.nProfiles ))
718
719 self.data_block = numpy.transpose(self.data_block, (1,2,0))
720
738
739 if self.DualModeIndex == self.ReadMode:
740
741 self.data_fft = numpy.fromfile(
742 startDATA, [('complex', '<c8')], self.nProfiles * self.nChannels * self.nHeights)
743
744 self.data_fft = self.data_fft.astype(numpy.dtype('complex'))
745
746 self.data_block = numpy.reshape(
747 self.data_fft, (self.nHeights, self.nChannels, self.nProfiles))
748
749 self.data_block = numpy.transpose(self.data_block, (1, 2, 0))
750
721 751 copy = self.data_block.copy()
722 spc = copy * numpy.conjugate(copy)
723
724 self.data_spc = numpy.absolute(spc) # valor absoluto o magnitud
725
752 spc = copy * numpy.conjugate(copy)
753
754 self.data_spc = numpy.absolute(
755 spc) # valor absoluto o magnitud
756
726 757 factor = self.dataOut.normFactor
727
728
729 z = self.data_spc.copy()#/factor
758
759 z = self.data_spc.copy() # /factor
730 760 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
731 761 #zdB = 10*numpy.log10(z)
732 762 print ' '
733 763 print 'Z: '
734 print shape(z)
764 print shape(z)
735 765 print ' '
736 766 print ' '
737
738 self.dataOut.data_spc=self.data_spc
739
740 self.noise = self.dataOut.getNoise(ymin_index=80, ymax_index=132)#/factor
767
768 self.dataOut.data_spc = self.data_spc
769
770 self.noise = self.dataOut.getNoise(
771 ymin_index=80, ymax_index=132) # /factor
741 772 #noisedB = 10*numpy.log10(self.noise)
742
743
744 ySamples=numpy.ones([3,self.nProfiles])
745 phase=numpy.ones([3,self.nProfiles])
746 CSPCSamples=numpy.ones([3,self.nProfiles],dtype=numpy.complex_)
747 coherence=numpy.ones([3,self.nProfiles])
748 PhaseSlope=numpy.ones(3)
749 PhaseInter=numpy.ones(3)
750
773
774 ySamples = numpy.ones([3, self.nProfiles])
775 phase = numpy.ones([3, self.nProfiles])
776 CSPCSamples = numpy.ones(
777 [3, self.nProfiles], dtype=numpy.complex_)
778 coherence = numpy.ones([3, self.nProfiles])
779 PhaseSlope = numpy.ones(3)
780 PhaseInter = numpy.ones(3)
781
751 782 '''****** Getting CrossSpectra ******'''
752 cspc=self.data_block.copy()
753 self.data_cspc=self.data_block.copy()
754
755 xFrec=self.getVelRange(1)
756 VelRange=self.getVelRange(1)
757 self.dataOut.VelRange=VelRange
758 #print ' '
759 #print ' '
760 #print 'xFrec',xFrec
761 #print ' '
762 #print ' '
763 #Height=35
764 for i in range(self.nRdPairs):
765
783 cspc = self.data_block.copy()
784 self.data_cspc = self.data_block.copy()
785
786 xFrec = self.getVelRange(1)
787 VelRange = self.getVelRange(1)
788 self.dataOut.VelRange = VelRange
789 # print ' '
790 # print ' '
791 # print 'xFrec',xFrec
792 # print ' '
793 # print ' '
794 # Height=35
795 for i in range(self.nRdPairs):
796
766 797 chan_index0 = self.dataOut.pairsList[i][0]
767 798 chan_index1 = self.dataOut.pairsList[i][1]
768
769 self.data_cspc[i,:,:]=cspc[chan_index0,:,:] * numpy.conjugate(cspc[chan_index1,:,:])
770
771
799
800 self.data_cspc[i, :, :] = cspc[chan_index0, :,
801 :] * numpy.conjugate(cspc[chan_index1, :, :])
802
772 803 '''Getting Eij and Nij'''
773 (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
774 (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
775 (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
776
777 E01=AntennaX0-AntennaX1
778 N01=AntennaY0-AntennaY1
779
780 E02=AntennaX0-AntennaX2
781 N02=AntennaY0-AntennaY2
782
783 E12=AntennaX1-AntennaX2
784 N12=AntennaY1-AntennaY2
785
786 self.ChanDist= numpy.array([[E01, N01],[E02,N02],[E12,N12]])
787
804 (AntennaX0, AntennaY0) = pol2cart(
805 rheader.AntennaCoord0, rheader.AntennaAngl0 * numpy.pi / 180)
806 (AntennaX1, AntennaY1) = pol2cart(
807 rheader.AntennaCoord1, rheader.AntennaAngl1 * numpy.pi / 180)
808 (AntennaX2, AntennaY2) = pol2cart(
809 rheader.AntennaCoord2, rheader.AntennaAngl2 * numpy.pi / 180)
810
811 E01 = AntennaX0 - AntennaX1
812 N01 = AntennaY0 - AntennaY1
813
814 E02 = AntennaX0 - AntennaX2
815 N02 = AntennaY0 - AntennaY2
816
817 E12 = AntennaX1 - AntennaX2
818 N12 = AntennaY1 - AntennaY2
819
820 self.ChanDist = numpy.array(
821 [[E01, N01], [E02, N02], [E12, N12]])
822
788 823 self.dataOut.ChanDist = self.ChanDist
789
790
824
825
791 826 # for Height in range(self.nHeights):
792 #
827 #
793 828 # for i in range(self.nRdPairs):
794 #
829 #
795 830 # '''****** Line of Data SPC ******'''
796 831 # zline=z[i,:,Height]
797 #
832 #
798 833 # '''****** DC is removed ******'''
799 834 # DC=Find(zline,numpy.amax(zline))
800 835 # zline[DC]=(zline[DC-1]+zline[DC+1])/2
801 #
802 #
836 #
837 #
803 838 # '''****** SPC is normalized ******'''
804 839 # FactNorm= zline.copy() / numpy.sum(zline.copy())
805 840 # FactNorm= FactNorm/numpy.sum(FactNorm)
806 #
841 #
807 842 # SmoothSPC=moving_average(FactNorm,N=3)
808 #
843 #
809 844 # xSamples = ar(range(len(SmoothSPC)))
810 845 # ySamples[i] = SmoothSPC-self.noise[i]
811 #
846 #
812 847 # for i in range(self.nRdPairs):
813 #
848 #
814 849 # '''****** Line of Data CSPC ******'''
815 850 # cspcLine=self.data_cspc[i,:,Height].copy()
816 #
817 #
818 #
851 #
852 #
853 #
819 854 # '''****** CSPC is normalized ******'''
820 855 # chan_index0 = self.dataOut.pairsList[i][0]
821 # chan_index1 = self.dataOut.pairsList[i][1]
856 # chan_index1 = self.dataOut.pairsList[i][1]
822 857 # CSPCFactor= numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])
823 #
824 #
858 #
859 #
825 860 # CSPCNorm= cspcLine.copy() / numpy.sqrt(CSPCFactor)
826 #
827 #
861 #
862 #
828 863 # CSPCSamples[i] = CSPCNorm-self.noise[i]
829 864 # coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
830 #
865 #
831 866 # '''****** DC is removed ******'''
832 867 # DC=Find(coherence[i],numpy.amax(coherence[i]))
833 868 # coherence[i][DC]=(coherence[i][DC-1]+coherence[i][DC+1])/2
834 869 # coherence[i]= moving_average(coherence[i],N=2)
835 #
870 #
836 871 # phase[i] = moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
837 #
838 #
872 #
873 #
839 874 # '''****** Getting fij width ******'''
840 #
875 #
841 876 # yMean=[]
842 # yMean2=[]
843 #
877 # yMean2=[]
878 #
844 879 # for j in range(len(ySamples[1])):
845 880 # yMean=numpy.append(yMean,numpy.average([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
846 #
881 #
847 882 # '''******* Getting fitting Gaussian ******'''
848 883 # meanGauss=sum(xSamples*yMean) / len(xSamples)
849 884 # sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
850 885 # #print 'Height',Height,'SNR', meanGauss/sigma**2
851 #
886 #
852 887 # if (abs(meanGauss/sigma**2) > 0.0001) :
853 #
854 # try:
888 #
889 # try:
855 890 # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
856 #
891 #
857 892 # if numpy.amax(popt)>numpy.amax(yMean)*0.3:
858 893 # FitGauss=gaus(xSamples,*popt)
859 #
860 # else:
894 #
895 # else:
861 896 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
862 897 # print 'Verificador: Dentro', Height
863 898 # except RuntimeError:
864 #
899 #
865 900 # try:
866 901 # for j in range(len(ySamples[1])):
867 902 # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]]))
868 903 # popt,pcov = curve_fit(gaus,xSamples,yMean2,p0=[1,meanGauss,sigma])
869 904 # FitGauss=gaus(xSamples,*popt)
870 905 # print 'Verificador: Exepcion1', Height
871 906 # except RuntimeError:
872 #
907 #
873 908 # try:
874 909 # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma])
875 910 # FitGauss=gaus(xSamples,*popt)
876 911 # print 'Verificador: Exepcion2', Height
877 912 # except RuntimeError:
878 913 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
879 914 # print 'Verificador: Exepcion3', Height
880 915 # else:
881 916 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
882 917 # #print 'Verificador: Fuera', Height
883 #
884 #
885 #
918 #
919 #
920 #
886 921 # Maximun=numpy.amax(yMean)
887 922 # eMinus1=Maximun*numpy.exp(-1)
888 #
923 #
889 924 # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
890 925 # HalfWidth= xFrec[HWpos]
891 926 # GCpos=Find(FitGauss, numpy.amax(FitGauss))
892 927 # Vpos=Find(FactNorm, numpy.amax(FactNorm))
893 928 # #Vpos=numpy.sum(FactNorm)/len(FactNorm)
894 929 # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) )))
895 930 # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos
896 931 # '''****** Getting Fij ******'''
897 #
932 #
898 933 # GaussCenter=xFrec[GCpos]
899 934 # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
900 935 # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
901 936 # else:
902 937 # Fij=abs(GaussCenter-HalfWidth)+0.0000001
903 #
938 #
904 939 # '''****** Getting Frecuency range of significant data ******'''
905 #
940 #
906 941 # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
907 #
942 #
908 943 # if Rangpos<GCpos:
909 944 # Range=numpy.array([Rangpos,2*GCpos-Rangpos])
910 945 # else:
911 946 # Range=numpy.array([2*GCpos-Rangpos,Rangpos])
912 #
947 #
913 948 # FrecRange=xFrec[Range[0]:Range[1]]
914 #
949 #
915 950 # #print 'FrecRange', FrecRange
916 951 # '''****** Getting SCPC Slope ******'''
917 #
952 #
918 953 # for i in range(self.nRdPairs):
919 #
954 #
920 955 # if len(FrecRange)>5 and len(FrecRange)<self.nProfiles*0.5:
921 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
922 #
956 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
957 #
923 958 # slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
924 959 # PhaseSlope[i]=slope
925 960 # PhaseInter[i]=intercept
926 961 # else:
927 962 # PhaseSlope[i]=0
928 963 # PhaseInter[i]=0
929 #
964 #
930 965 # # plt.figure(i+15)
931 966 # # plt.title('FASE ( CH%s*CH%s )' %(self.dataOut.pairsList[i][0],self.dataOut.pairsList[i][1]))
932 967 # # plt.xlabel('Frecuencia (KHz)')
933 968 # # plt.ylabel('Magnitud')
934 969 # # #plt.subplot(311+i)
935 970 # # plt.plot(FrecRange,PhaseRange,'b')
936 971 # # plt.plot(FrecRange,FrecRange*PhaseSlope[i]+PhaseInter[i],'r')
937 #
972 #
938 973 # #plt.axis([-0.6, 0.2, -3.2, 3.2])
939 #
940 #
974 #
975 #
941 976 # '''Getting constant C'''
942 977 # cC=(Fij*numpy.pi)**2
943 #
978 #
944 979 # # '''Getting Eij and Nij'''
945 980 # # (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
946 981 # # (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
947 982 # # (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
948 # #
983 # #
949 984 # # E01=AntennaX0-AntennaX1
950 985 # # N01=AntennaY0-AntennaY1
951 # #
986 # #
952 987 # # E02=AntennaX0-AntennaX2
953 988 # # N02=AntennaY0-AntennaY2
954 # #
989 # #
955 990 # # E12=AntennaX1-AntennaX2
956 991 # # N12=AntennaY1-AntennaY2
957 #
992 #
958 993 # '''****** Getting constants F and G ******'''
959 994 # MijEijNij=numpy.array([[E02,N02], [E12,N12]])
960 995 # MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
961 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
996 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
962 997 # MijResults=numpy.array([MijResult0,MijResult1])
963 998 # (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
964 #
999 #
965 1000 # '''****** Getting constants A, B and H ******'''
966 1001 # W01=numpy.amax(coherence[0])
967 1002 # W02=numpy.amax(coherence[1])
968 1003 # W12=numpy.amax(coherence[2])
969 #
1004 #
970 1005 # WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
971 1006 # WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
972 1007 # WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
973 #
1008 #
974 1009 # WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
975 #
976 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
1010 #
1011 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
977 1012 # (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
978 #
1013 #
979 1014 # VxVy=numpy.array([[cA,cH],[cH,cB]])
980 #
1015 #
981 1016 # VxVyResults=numpy.array([-cF,-cG])
982 1017 # (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
983 1018 # Vzon = Vy
984 1019 # Vmer = Vx
985 1020 # Vmag=numpy.sqrt(Vzon**2+Vmer**2)
986 1021 # Vang=numpy.arctan2(Vmer,Vzon)
987 #
1022 #
988 1023 # if abs(Vy)<100 and abs(Vy)> 0.:
989 1024 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag
990 1025 # #print 'Vmag',Vmag
991 1026 # else:
992 1027 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN)
993 #
1028 #
994 1029 # if abs(Vx)<100 and abs(Vx) > 0.:
995 1030 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang
996 # #print 'Vang',Vang
1031 # #print 'Vang',Vang
997 1032 # else:
998 1033 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN)
999 #
1034 #
1000 1035 # if abs(GaussCenter)<2:
1001 1036 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos])
1002 #
1037 #
1003 1038 # else:
1004 1039 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN)
1005 #
1006 #
1040 #
1041 #
1007 1042 # # print '********************************************'
1008 1043 # # print 'HalfWidth ', HalfWidth
1009 1044 # # print 'Maximun ', Maximun
1010 1045 # # print 'eMinus1 ', eMinus1
1011 1046 # # print 'Rangpos ', Rangpos
1012 1047 # # print 'GaussCenter ',GaussCenter
1013 1048 # # print 'E01 ',E01
1014 1049 # # print 'N01 ',N01
1015 1050 # # print 'E02 ',E02
1016 1051 # # print 'N02 ',N02
1017 1052 # # print 'E12 ',E12
1018 1053 # # print 'N12 ',N12
1019 1054 # #print 'self.dataOut.velocityX ', self.dataOut.velocityX
1020 1055 # # print 'Fij ', Fij
1021 1056 # # print 'cC ', cC
1022 1057 # # print 'cF ', cF
1023 1058 # # print 'cG ', cG
1024 1059 # # print 'cA ', cA
1025 1060 # # print 'cB ', cB
1026 1061 # # print 'cH ', cH
1027 1062 # # print 'Vx ', Vx
1028 1063 # # print 'Vy ', Vy
1029 1064 # # print 'Vmag ', Vmag
1030 1065 # # print 'Vang ', Vang*180/numpy.pi
1031 1066 # # print 'PhaseSlope ',PhaseSlope[0]
1032 1067 # # print 'PhaseSlope ',PhaseSlope[1]
1033 1068 # # print 'PhaseSlope ',PhaseSlope[2]
1034 1069 # # print '********************************************'
1035 1070 # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY)
1036 #
1071 #
1037 1072 # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX)
1038 1073 # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY)
1039 1074 # #print 'self.dataOut.velocityV', self.dataOut.velocityV
1040 #
1075 #
1041 1076 # self.data_output[0]=numpy.array(self.dataOut.velocityX)
1042 1077 # self.data_output[1]=numpy.array(self.dataOut.velocityY)
1043 1078 # self.data_output[2]=numpy.array(self.dataOut.velocityV)
1044 #
1079 #
1045 1080 # prin= self.data_output[0][~numpy.isnan(self.data_output[0])]
1046 1081 # print ' '
1047 # print 'VmagAverage',numpy.mean(prin)
1082 # print 'VmagAverage',numpy.mean(prin)
1048 1083 # print ' '
1049 1084 # # plt.figure(5)
1050 1085 # # plt.subplot(211)
1051 1086 # # plt.plot(self.dataOut.velocityX,'yo:')
1052 1087 # # plt.subplot(212)
1053 1088 # # plt.plot(self.dataOut.velocityY,'yo:')
1054 #
1089 #
1055 1090 # # plt.figure(1)
1056 1091 # # # plt.subplot(121)
1057 1092 # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0')
1058 1093 # # # plt.plot(xFrec,ySamples[1],'g',label='Ch1')
1059 1094 # # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1060 1095 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1061 1096 # # # plt.legend()
1062 1097 # # plt.title('DATOS A ALTURA DE 2850 METROS')
1063 # #
1098 # #
1064 1099 # # plt.xlabel('Frecuencia (KHz)')
1065 1100 # # plt.ylabel('Magnitud')
1066 1101 # # # plt.subplot(122)
1067 1102 # # # plt.title('Fit for Time Constant')
1068 1103 # # #plt.plot(xFrec,zline)
1069 1104 # # #plt.plot(xFrec,SmoothSPC,'g')
1070 1105 # # plt.plot(xFrec,FactNorm)
1071 1106 # # plt.axis([-4, 4, 0, 0.15])
1072 1107 # # # plt.xlabel('SelfSpectra KHz')
1073 # #
1108 # #
1074 1109 # # plt.figure(10)
1075 1110 # # # plt.subplot(121)
1076 1111 # # plt.plot(xFrec,ySamples[0],'b',label='Ch0')
1077 1112 # # plt.plot(xFrec,ySamples[1],'y',label='Ch1')
1078 1113 # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1079 1114 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1080 1115 # # plt.legend()
1081 1116 # # plt.title('SELFSPECTRA EN CANALES')
1082 # #
1117 # #
1083 1118 # # plt.xlabel('Frecuencia (KHz)')
1084 1119 # # plt.ylabel('Magnitud')
1085 1120 # # # plt.subplot(122)
1086 1121 # # # plt.title('Fit for Time Constant')
1087 1122 # # #plt.plot(xFrec,zline)
1088 1123 # # #plt.plot(xFrec,SmoothSPC,'g')
1089 1124 # # # plt.plot(xFrec,FactNorm)
1090 1125 # # # plt.axis([-4, 4, 0, 0.15])
1091 1126 # # # plt.xlabel('SelfSpectra KHz')
1092 # #
1127 # #
1093 1128 # # plt.figure(9)
1094 # #
1095 # #
1129 # #
1130 # #
1096 1131 # # plt.title('DATOS SUAVIZADOS')
1097 1132 # # plt.xlabel('Frecuencia (KHz)')
1098 1133 # # plt.ylabel('Magnitud')
1099 1134 # # plt.plot(xFrec,SmoothSPC,'g')
1100 # #
1135 # #
1101 1136 # # #plt.plot(xFrec,FactNorm)
1102 1137 # # plt.axis([-4, 4, 0, 0.15])
1103 1138 # # # plt.xlabel('SelfSpectra KHz')
1104 # # #
1139 # # #
1105 1140 # # plt.figure(2)
1106 1141 # # # #plt.subplot(121)
1107 1142 # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra')
1108 1143 # # plt.plot(xFrec,FitGauss,'yo:',label='Ajuste Gaussiano')
1109 1144 # # # plt.plot(xFrec[Rangpos],FitGauss[Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.1)))],'bo')
1110 1145 # # # #plt.plot(xFrec,phase)
1111 1146 # # # plt.xlabel('Suavizado, promediado KHz')
1112 1147 # # plt.title('SELFSPECTRA PROMEDIADO')
1113 1148 # # # #plt.subplot(122)
1114 1149 # # # #plt.plot(xSamples,zline)
1115 1150 # # plt.xlabel('Frecuencia (KHz)')
1116 1151 # # plt.ylabel('Magnitud')
1117 1152 # # plt.legend()
1118 # # #
1153 # # #
1119 1154 # # # plt.figure(3)
1120 1155 # # # plt.subplot(311)
1121 1156 # # # #plt.plot(xFrec,phase[0])
1122 1157 # # # plt.plot(xFrec,phase[0],'g')
1123 1158 # # # plt.subplot(312)
1124 1159 # # # plt.plot(xFrec,phase[1],'g')
1125 1160 # # # plt.subplot(313)
1126 1161 # # # plt.plot(xFrec,phase[2],'g')
1127 1162 # # # #plt.plot(xFrec,phase[2])
1128 # # #
1163 # # #
1129 1164 # # # plt.figure(4)
1130 # # #
1165 # # #
1131 1166 # # # plt.plot(xSamples,coherence[0],'b')
1132 1167 # # # plt.plot(xSamples,coherence[1],'r')
1133 1168 # # # plt.plot(xSamples,coherence[2],'g')
1134 1169 # # plt.show()
1135 # # #
1170 # # #
1136 1171 # # # plt.clf()
1137 1172 # # # plt.cla()
1138 # # # plt.close()
1139 #
1140 # print ' '
1141
1142
1143
1144 self.BlockCounter+=2
1145
1173 # # # plt.close()
1174 #
1175 # print ' '
1176
1177 self.BlockCounter += 2
1178
1146 1179 else:
1147 self.fileSelector+=1
1148 self.BlockCounter=0
1180 self.fileSelector += 1
1181 self.BlockCounter = 0
1149 1182 print "Next File"
1150
1151
1152
1153
1154
This diff has been collapsed as it changes many lines, (1027 lines changed) Show them Hide them
@@ -1,803 +1,802
1 import os, sys
1 import os
2 import sys
2 3 import glob
3 4 import fnmatch
4 5 import datetime
5 6 import time
6 7 import re
7 8 import h5py
8 9 import numpy
9 import matplotlib.pyplot as plt
10 10
11 import pylab as plb
12 11 from scipy.optimize import curve_fit
13 from scipy import asarray as ar,exp
12 from scipy import asarray as ar, exp
14 13 from scipy import stats
15 14
16 15 from numpy.ma.core import getdata
17 16
18 17 SPEED_OF_LIGHT = 299792458
19 18 SPEED_OF_LIGHT = 3e8
20 19
21 20 try:
22 21 from gevent import sleep
23 22 except:
24 23 from time import sleep
25 24
26 25 from schainpy.model.data.jrodata import Spectra
27 26 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
28 27 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 28 #from schainpy.model.io.jroIO_bltr import BLTRReader
30 29 from numpy import imag, shape, NaN, empty
31 30
32 31
33
34 32 class Header(object):
35
33
36 34 def __init__(self):
37 35 raise NotImplementedError
38
39
36
40 37 def read(self):
41
38
42 39 raise NotImplementedError
43
40
44 41 def write(self):
45
42
46 43 raise NotImplementedError
47
44
48 45 def printInfo(self):
49
50 message = "#"*50 + "\n"
46
47 message = "#" * 50 + "\n"
51 48 message += self.__class__.__name__.upper() + "\n"
52 message += "#"*50 + "\n"
53
49 message += "#" * 50 + "\n"
50
54 51 keyList = self.__dict__.keys()
55 52 keyList.sort()
56
53
57 54 for key in keyList:
58 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
59
55 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
56
60 57 if "size" not in keyList:
61 58 attr = getattr(self, "size")
62
59
63 60 if attr:
64 message += "%s = %s" %("size", attr) + "\n"
65
66 #print message
67
68
69 FILE_HEADER = numpy.dtype([ #HEADER 1024bytes
70 ('Hname','a32'), #Original file name
71 ('Htime',numpy.str_,32), #Date and time when the file was created
72 ('Hoper',numpy.str_,64), #Name of operator who created the file
73 ('Hplace',numpy.str_,128), #Place where the measurements was carried out
74 ('Hdescr',numpy.str_,256), #Description of measurements
75 ('Hdummy',numpy.str_,512), #Reserved space
76 #Main chunk 8bytes
77 ('Msign',numpy.str_,4), #Main chunk signature FZKF or NUIG
78 ('MsizeData','<i4'), #Size of data block main chunk
79 #Processing DSP parameters 36bytes
80 ('PPARsign',numpy.str_,4), #PPAR signature
81 ('PPARsize','<i4'), #PPAR size of block
82 ('PPARprf','<i4'), #Pulse repetition frequency
83 ('PPARpdr','<i4'), #Pulse duration
84 ('PPARsft','<i4'), #FFT length
85 ('PPARavc','<i4'), #Number of spectral (in-coherent) averages
86 ('PPARihp','<i4'), #Number of lowest range gate for moment estimation
87 ('PPARchg','<i4'), #Count for gates for moment estimation
88 ('PPARpol','<i4'), #switch on/off polarimetric measurements. Should be 1.
89 #Service DSP parameters 112bytes
90 ('SPARatt','<i4'), #STC attenuation on the lowest ranges on/off
91 ('SPARtx','<i4'), #OBSOLETE
92 ('SPARaddGain0','<f4'), #OBSOLETE
93 ('SPARaddGain1','<f4'), #OBSOLETE
94 ('SPARwnd','<i4'), #Debug only. It normal mode it is 0.
95 ('SPARpos','<i4'), #Delay between sync pulse and tx pulse for phase corr, ns
96 ('SPARadd','<i4'), #"add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
97 ('SPARlen','<i4'), #Time for measuring txn pulse phase. OBSOLETE
98 ('SPARcal','<i4'), #OBSOLETE
99 ('SPARnos','<i4'), #OBSOLETE
100 ('SPARof0','<i4'), #detection threshold
101 ('SPARof1','<i4'), #OBSOLETE
102 ('SPARswt','<i4'), #2nd moment estimation threshold
103 ('SPARsum','<i4'), #OBSOLETE
104 ('SPARosc','<i4'), #flag Oscillosgram mode
105 ('SPARtst','<i4'), #OBSOLETE
106 ('SPARcor','<i4'), #OBSOLETE
107 ('SPARofs','<i4'), #OBSOLETE
108 ('SPARhsn','<i4'), #Hildebrand div noise detection on noise gate
109 ('SPARhsa','<f4'), #Hildebrand div noise detection on all gates
110 ('SPARcalibPow_M','<f4'), #OBSOLETE
111 ('SPARcalibSNR_M','<f4'), #OBSOLETE
112 ('SPARcalibPow_S','<f4'), #OBSOLETE
113 ('SPARcalibSNR_S','<f4'), #OBSOLETE
114 ('SPARrawGate1','<i4'), #Lowest range gate for spectra saving Raw_Gate1 >=5
115 ('SPARrawGate2','<i4'), #Number of range gates with atmospheric signal
116 ('SPARraw','<i4'), #flag - IQ or spectra saving on/off
117 ('SPARprc','<i4'),]) #flag - Moment estimation switched on/off
118
119
120
61 message += "%s = %s" % ("size", attr) + "\n"
62
63 # print message
64
65
66 FILE_HEADER = numpy.dtype([ # HEADER 1024bytes
67 ('Hname', 'a32'), # Original file name
68 # Date and time when the file was created
69 ('Htime', numpy.str_, 32),
70 # Name of operator who created the file
71 ('Hoper', numpy.str_, 64),
72 # Place where the measurements was carried out
73 ('Hplace', numpy.str_, 128),
74 # Description of measurements
75 ('Hdescr', numpy.str_, 256),
76 ('Hdummy', numpy.str_, 512), # Reserved space
77 # Main chunk 8bytes
78 # Main chunk signature FZKF or NUIG
79 ('Msign', numpy.str_, 4),
80 ('MsizeData', '<i4'), # Size of data block main chunk
81 # Processing DSP parameters 36bytes
82 ('PPARsign', numpy.str_, 4), # PPAR signature
83 ('PPARsize', '<i4'), # PPAR size of block
84 ('PPARprf', '<i4'), # Pulse repetition frequency
85 ('PPARpdr', '<i4'), # Pulse duration
86 ('PPARsft', '<i4'), # FFT length
87 # Number of spectral (in-coherent) averages
88 ('PPARavc', '<i4'),
89 # Number of lowest range gate for moment estimation
90 ('PPARihp', '<i4'),
91 # Count for gates for moment estimation
92 ('PPARchg', '<i4'),
93 # switch on/off polarimetric measurements. Should be 1.
94 ('PPARpol', '<i4'),
95 # Service DSP parameters 112bytes
96 # STC attenuation on the lowest ranges on/off
97 ('SPARatt', '<i4'),
98 ('SPARtx', '<i4'), # OBSOLETE
99 ('SPARaddGain0', '<f4'), # OBSOLETE
100 ('SPARaddGain1', '<f4'), # OBSOLETE
101 # Debug only. It normal mode it is 0.
102 ('SPARwnd', '<i4'),
103 # Delay between sync pulse and tx pulse for phase corr, ns
104 ('SPARpos', '<i4'),
105 # "add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
106 ('SPARadd', '<i4'),
107 # Time for measuring txn pulse phase. OBSOLETE
108 ('SPARlen', '<i4'),
109 ('SPARcal', '<i4'), # OBSOLETE
110 ('SPARnos', '<i4'), # OBSOLETE
111 ('SPARof0', '<i4'), # detection threshold
112 ('SPARof1', '<i4'), # OBSOLETE
113 ('SPARswt', '<i4'), # 2nd moment estimation threshold
114 ('SPARsum', '<i4'), # OBSOLETE
115 ('SPARosc', '<i4'), # flag Oscillosgram mode
116 ('SPARtst', '<i4'), # OBSOLETE
117 ('SPARcor', '<i4'), # OBSOLETE
118 ('SPARofs', '<i4'), # OBSOLETE
119 # Hildebrand div noise detection on noise gate
120 ('SPARhsn', '<i4'),
121 # Hildebrand div noise detection on all gates
122 ('SPARhsa', '<f4'),
123 ('SPARcalibPow_M', '<f4'), # OBSOLETE
124 ('SPARcalibSNR_M', '<f4'), # OBSOLETE
125 ('SPARcalibPow_S', '<f4'), # OBSOLETE
126 ('SPARcalibSNR_S', '<f4'), # OBSOLETE
127 # Lowest range gate for spectra saving Raw_Gate1 >=5
128 ('SPARrawGate1', '<i4'),
129 # Number of range gates with atmospheric signal
130 ('SPARrawGate2', '<i4'),
131 # flag - IQ or spectra saving on/off
132 ('SPARraw', '<i4'),
133 ('SPARprc', '<i4'), ]) # flag - Moment estimation switched on/off
134
135
121 136 class FileHeaderMIRA35c(Header):
122
137
123 138 def __init__(self):
124
125 self.Hname= None
126 self.Htime= None
127 self.Hoper= None
128 self.Hplace= None
129 self.Hdescr= None
130 self.Hdummy= None
131
132 self.Msign=None
133 self.MsizeData=None
134
135 self.PPARsign=None
136 self.PPARsize=None
137 self.PPARprf=None
138 self.PPARpdr=None
139 self.PPARsft=None
140 self.PPARavc=None
141 self.PPARihp=None
142 self.PPARchg=None
143 self.PPARpol=None
144 #Service DSP parameters
145 self.SPARatt=None
146 self.SPARtx=None
147 self.SPARaddGain0=None
148 self.SPARaddGain1=None
149 self.SPARwnd=None
150 self.SPARpos=None
151 self.SPARadd=None
152 self.SPARlen=None
153 self.SPARcal=None
154 self.SPARnos=None
155 self.SPARof0=None
156 self.SPARof1=None
157 self.SPARswt=None
158 self.SPARsum=None
159 self.SPARosc=None
160 self.SPARtst=None
161 self.SPARcor=None
162 self.SPARofs=None
163 self.SPARhsn=None
164 self.SPARhsa=None
165 self.SPARcalibPow_M=None
166 self.SPARcalibSNR_M=None
167 self.SPARcalibPow_S=None
168 self.SPARcalibSNR_S=None
169 self.SPARrawGate1=None
170 self.SPARrawGate2=None
171 self.SPARraw=None
172 self.SPARprc=None
173
174 self.FHsize=1180
175
139
140 self.Hname = None
141 self.Htime = None
142 self.Hoper = None
143 self.Hplace = None
144 self.Hdescr = None
145 self.Hdummy = None
146
147 self.Msign = None
148 self.MsizeData = None
149
150 self.PPARsign = None
151 self.PPARsize = None
152 self.PPARprf = None
153 self.PPARpdr = None
154 self.PPARsft = None
155 self.PPARavc = None
156 self.PPARihp = None
157 self.PPARchg = None
158 self.PPARpol = None
159 # Service DSP parameters
160 self.SPARatt = None
161 self.SPARtx = None
162 self.SPARaddGain0 = None
163 self.SPARaddGain1 = None
164 self.SPARwnd = None
165 self.SPARpos = None
166 self.SPARadd = None
167 self.SPARlen = None
168 self.SPARcal = None
169 self.SPARnos = None
170 self.SPARof0 = None
171 self.SPARof1 = None
172 self.SPARswt = None
173 self.SPARsum = None
174 self.SPARosc = None
175 self.SPARtst = None
176 self.SPARcor = None
177 self.SPARofs = None
178 self.SPARhsn = None
179 self.SPARhsa = None
180 self.SPARcalibPow_M = None
181 self.SPARcalibSNR_M = None
182 self.SPARcalibPow_S = None
183 self.SPARcalibSNR_S = None
184 self.SPARrawGate1 = None
185 self.SPARrawGate2 = None
186 self.SPARraw = None
187 self.SPARprc = None
188
189 self.FHsize = 1180
190
176 191 def FHread(self, fp):
177
178 header = numpy.fromfile(fp, FILE_HEADER,1)
192
193 header = numpy.fromfile(fp, FILE_HEADER, 1)
179 194 ''' numpy.fromfile(file, dtype, count, sep='')
180 195 file : file or str
181 196 Open file object or filename.
182 197
183 198 dtype : data-type
184 199 Data type of the returned array. For binary files, it is used to determine
185 200 the size and byte-order of the items in the file.
186 201
187 202 count : int
188 203 Number of items to read. -1 means all items (i.e., the complete file).
189 204
190 205 sep : str
191 206 Separator between items if file is a text file. Empty ("") separator means
192 207 the file should be treated as binary. Spaces (" ") in the separator match zero
193 208 or more whitespace characters. A separator consisting only of spaces must match
194 209 at least one whitespace.
195 210
196 211 '''
197
198
199 self.Hname= str(header['Hname'][0])
200 self.Htime= str(header['Htime'][0])
201 self.Hoper= str(header['Hoper'][0])
202 self.Hplace= str(header['Hplace'][0])
203 self.Hdescr= str(header['Hdescr'][0])
204 self.Hdummy= str(header['Hdummy'][0])
205 #1024
206
207 self.Msign=str(header['Msign'][0])
208 self.MsizeData=header['MsizeData'][0]
209 #8
210
211 self.PPARsign=str(header['PPARsign'][0])
212 self.PPARsize=header['PPARsize'][0]
213 self.PPARprf=header['PPARprf'][0]
214 self.PPARpdr=header['PPARpdr'][0]
215 self.PPARsft=header['PPARsft'][0]
216 self.PPARavc=header['PPARavc'][0]
217 self.PPARihp=header['PPARihp'][0]
218 self.PPARchg=header['PPARchg'][0]
219 self.PPARpol=header['PPARpol'][0]
220 #Service DSP parameters
221 #36
222
223 self.SPARatt=header['SPARatt'][0]
224 self.SPARtx=header['SPARtx'][0]
225 self.SPARaddGain0=header['SPARaddGain0'][0]
226 self.SPARaddGain1=header['SPARaddGain1'][0]
227 self.SPARwnd=header['SPARwnd'][0]
228 self.SPARpos=header['SPARpos'][0]
229 self.SPARadd=header['SPARadd'][0]
230 self.SPARlen=header['SPARlen'][0]
231 self.SPARcal=header['SPARcal'][0]
232 self.SPARnos=header['SPARnos'][0]
233 self.SPARof0=header['SPARof0'][0]
234 self.SPARof1=header['SPARof1'][0]
235 self.SPARswt=header['SPARswt'][0]
236 self.SPARsum=header['SPARsum'][0]
237 self.SPARosc=header['SPARosc'][0]
238 self.SPARtst=header['SPARtst'][0]
239 self.SPARcor=header['SPARcor'][0]
240 self.SPARofs=header['SPARofs'][0]
241 self.SPARhsn=header['SPARhsn'][0]
242 self.SPARhsa=header['SPARhsa'][0]
243 self.SPARcalibPow_M=header['SPARcalibPow_M'][0]
244 self.SPARcalibSNR_M=header['SPARcalibSNR_M'][0]
245 self.SPARcalibPow_S=header['SPARcalibPow_S'][0]
246 self.SPARcalibSNR_S=header['SPARcalibSNR_S'][0]
247 self.SPARrawGate1=header['SPARrawGate1'][0]
248 self.SPARrawGate2=header['SPARrawGate2'][0]
249 self.SPARraw=header['SPARraw'][0]
250 self.SPARprc=header['SPARprc'][0]
251 #112
252 #1180
253 #print 'Pointer fp header', fp.tell()
254 #print ' '
255 #print 'SPARrawGate'
256 #print self.SPARrawGate2 - self.SPARrawGate1
257
258 #print ' '
259 #print 'Hname'
260 #print self.Hname
261
262 #print ' '
263 #print 'Msign'
264 #print self.Msign
265
212
213 self.Hname = str(header['Hname'][0])
214 self.Htime = str(header['Htime'][0])
215 self.Hoper = str(header['Hoper'][0])
216 self.Hplace = str(header['Hplace'][0])
217 self.Hdescr = str(header['Hdescr'][0])
218 self.Hdummy = str(header['Hdummy'][0])
219 # 1024
220
221 self.Msign = str(header['Msign'][0])
222 self.MsizeData = header['MsizeData'][0]
223 # 8
224
225 self.PPARsign = str(header['PPARsign'][0])
226 self.PPARsize = header['PPARsize'][0]
227 self.PPARprf = header['PPARprf'][0]
228 self.PPARpdr = header['PPARpdr'][0]
229 self.PPARsft = header['PPARsft'][0]
230 self.PPARavc = header['PPARavc'][0]
231 self.PPARihp = header['PPARihp'][0]
232 self.PPARchg = header['PPARchg'][0]
233 self.PPARpol = header['PPARpol'][0]
234 # Service DSP parameters
235 # 36
236
237 self.SPARatt = header['SPARatt'][0]
238 self.SPARtx = header['SPARtx'][0]
239 self.SPARaddGain0 = header['SPARaddGain0'][0]
240 self.SPARaddGain1 = header['SPARaddGain1'][0]
241 self.SPARwnd = header['SPARwnd'][0]
242 self.SPARpos = header['SPARpos'][0]
243 self.SPARadd = header['SPARadd'][0]
244 self.SPARlen = header['SPARlen'][0]
245 self.SPARcal = header['SPARcal'][0]
246 self.SPARnos = header['SPARnos'][0]
247 self.SPARof0 = header['SPARof0'][0]
248 self.SPARof1 = header['SPARof1'][0]
249 self.SPARswt = header['SPARswt'][0]
250 self.SPARsum = header['SPARsum'][0]
251 self.SPARosc = header['SPARosc'][0]
252 self.SPARtst = header['SPARtst'][0]
253 self.SPARcor = header['SPARcor'][0]
254 self.SPARofs = header['SPARofs'][0]
255 self.SPARhsn = header['SPARhsn'][0]
256 self.SPARhsa = header['SPARhsa'][0]
257 self.SPARcalibPow_M = header['SPARcalibPow_M'][0]
258 self.SPARcalibSNR_M = header['SPARcalibSNR_M'][0]
259 self.SPARcalibPow_S = header['SPARcalibPow_S'][0]
260 self.SPARcalibSNR_S = header['SPARcalibSNR_S'][0]
261 self.SPARrawGate1 = header['SPARrawGate1'][0]
262 self.SPARrawGate2 = header['SPARrawGate2'][0]
263 self.SPARraw = header['SPARraw'][0]
264 self.SPARprc = header['SPARprc'][0]
265 # 112
266 # 1180
267 # print 'Pointer fp header', fp.tell()
268 # print ' '
269 # print 'SPARrawGate'
270 # print self.SPARrawGate2 - self.SPARrawGate1
271
272 # print ' '
273 # print 'Hname'
274 # print self.Hname
275
276 # print ' '
277 # print 'Msign'
278 # print self.Msign
279
266 280 def write(self, fp):
267
281
268 282 headerTuple = (self.Hname,
269 283 self.Htime,
270 284 self.Hoper,
271 285 self.Hplace,
272 286 self.Hdescr,
273 287 self.Hdummy)
274
275
288
276 289 header = numpy.array(headerTuple, FILE_HEADER)
277 290 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
278 291 header.tofile(fp)
279 292 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
280 293
281 294 fid : file or str
282 295 An open file object, or a string containing a filename.
283 296
284 297 sep : str
285 298 Separator between array items for text output. If "" (empty), a binary file is written,
286 299 equivalent to file.write(a.tobytes()).
287 300
288 301 format : str
289 302 Format string for text file output. Each entry in the array is formatted to text by
290 303 first converting it to the closest Python type, and then using "format" % item.
291 304
292 305 '''
293
306
294 307 return 1
295 308
296 SRVI_HEADER = numpy.dtype([
297 ('SignatureSRVI1',numpy.str_,4),#
298 ('SizeOfDataBlock1','<i4'),#
299 ('DataBlockTitleSRVI1',numpy.str_,4),#
300 ('SizeOfSRVI1','<i4'),])#
301
302 class SRVIHeader(Header):
309
310 SRVI_HEADER = numpy.dtype([
311 ('SignatureSRVI1', numpy.str_, 4),
312 ('SizeOfDataBlock1', '<i4'),
313 ('DataBlockTitleSRVI1', numpy.str_, 4),
314 ('SizeOfSRVI1', '<i4'), ])
315
316
317 class SRVIHeader(Header):
303 318 def __init__(self, SignatureSRVI1=0, SizeOfDataBlock1=0, DataBlockTitleSRVI1=0, SizeOfSRVI1=0):
304
319
305 320 self.SignatureSRVI1 = SignatureSRVI1
306 321 self.SizeOfDataBlock1 = SizeOfDataBlock1
307 322 self.DataBlockTitleSRVI1 = DataBlockTitleSRVI1
308 self.SizeOfSRVI1 = SizeOfSRVI1
309
310 self.SRVIHsize=16
311
312 def SRVIread(self, fp):
313
314 header = numpy.fromfile(fp, SRVI_HEADER,1)
323 self.SizeOfSRVI1 = SizeOfSRVI1
324
325 self.SRVIHsize = 16
326
327 def SRVIread(self, fp):
328
329 header = numpy.fromfile(fp, SRVI_HEADER, 1)
315 330
316 331 self.SignatureSRVI1 = str(header['SignatureSRVI1'][0])
317 332 self.SizeOfDataBlock1 = header['SizeOfDataBlock1'][0]
318 333 self.DataBlockTitleSRVI1 = str(header['DataBlockTitleSRVI1'][0])
319 334 self.SizeOfSRVI1 = header['SizeOfSRVI1'][0]
320 #16
335 # 16
321 336 print 'Pointer fp SRVIheader', fp.tell()
322
323
324 SRVI_STRUCTURE = numpy.dtype([
325 ('frame_cnt','<u4'),#
326 ('time_t','<u4'), #
327 ('tpow','<f4'), #
328 ('npw1','<f4'), #
329 ('npw2','<f4'), #
330 ('cpw1','<f4'), #
331 ('pcw2','<f4'), #
332 ('ps_err','<u4'), #
333 ('te_err','<u4'), #
334 ('rc_err','<u4'), #
335 ('grs1','<u4'), #
336 ('grs2','<u4'), #
337 ('azipos','<f4'), #
338 ('azivel','<f4'), #
339 ('elvpos','<f4'), #
340 ('elvvel','<f4'), #
341 ('northAngle','<f4'), #
342 ('microsec','<u4'), #
343 ('azisetvel','<f4'), #
344 ('elvsetpos','<f4'), #
345 ('RadarConst','<f4'),]) #
346 337
347 338
339 SRVI_STRUCTURE = numpy.dtype([
340 ('frame_cnt', '<u4'),
341 ('time_t', '<u4'), #
342 ('tpow', '<f4'), #
343 ('npw1', '<f4'), #
344 ('npw2', '<f4'), #
345 ('cpw1', '<f4'), #
346 ('pcw2', '<f4'), #
347 ('ps_err', '<u4'), #
348 ('te_err', '<u4'), #
349 ('rc_err', '<u4'), #
350 ('grs1', '<u4'), #
351 ('grs2', '<u4'), #
352 ('azipos', '<f4'), #
353 ('azivel', '<f4'), #
354 ('elvpos', '<f4'), #
355 ('elvvel', '<f4'), #
356 ('northAngle', '<f4'),
357 ('microsec', '<u4'), #
358 ('azisetvel', '<f4'), #
359 ('elvsetpos', '<f4'), #
360 ('RadarConst', '<f4'), ]) #
348 361
349 362
350 363 class RecordHeader(Header):
351
352
353 def __init__(self, frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
354 cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
355 grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
356 microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0 , RecCounter=0, Off2StartNxtRec=0):
357
358
364
365 def __init__(self, frame_cnt=0, time_t=0, tpow=0, npw1=0, npw2=0,
366 cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
367 grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
368 microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0, RecCounter=0, Off2StartNxtRec=0):
369
359 370 self.frame_cnt = frame_cnt
360 371 self.dwell = time_t
361 372 self.tpow = tpow
362 373 self.npw1 = npw1
363 374 self.npw2 = npw2
364 375 self.cpw1 = cpw1
365 376 self.pcw2 = pcw2
366 377 self.ps_err = ps_err
367 378 self.te_err = te_err
368 379 self.rc_err = rc_err
369 380 self.grs1 = grs1
370 381 self.grs2 = grs2
371 382 self.azipos = azipos
372 383 self.azivel = azivel
373 384 self.elvpos = elvpos
374 385 self.elvvel = elvvel
375 386 self.northAngle = northangle
376 387 self.microsec = microsec
377 388 self.azisetvel = azisetvel
378 389 self.elvsetpos = elvsetpos
379 390 self.RadarConst = RadarConst
380 self.RHsize=84
391 self.RHsize = 84
381 392 self.RecCounter = RecCounter
382 self.Off2StartNxtRec=Off2StartNxtRec
383
393 self.Off2StartNxtRec = Off2StartNxtRec
394
384 395 def RHread(self, fp):
385
386 #startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
387
396
397 # startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
398
388 399 #OffRHeader= 1180 + self.RecCounter*(self.Off2StartNxtRec)
389 400 #startFp.seek(OffRHeader, os.SEEK_SET)
390
391 #print 'Posicion del bloque: ',OffRHeader
392
393 header = numpy.fromfile(fp,SRVI_STRUCTURE,1)
394
395 self.frame_cnt = header['frame_cnt'][0]#
401
402 # print 'Posicion del bloque: ',OffRHeader
403
404 header = numpy.fromfile(fp, SRVI_STRUCTURE, 1)
405
406 self.frame_cnt = header['frame_cnt'][0]
396 407 self.time_t = header['time_t'][0] #
397 408 self.tpow = header['tpow'][0] #
398 409 self.npw1 = header['npw1'][0] #
399 410 self.npw2 = header['npw2'][0] #
400 411 self.cpw1 = header['cpw1'][0] #
401 412 self.pcw2 = header['pcw2'][0] #
402 413 self.ps_err = header['ps_err'][0] #
403 414 self.te_err = header['te_err'][0] #
404 415 self.rc_err = header['rc_err'][0] #
405 416 self.grs1 = header['grs1'][0] #
406 417 self.grs2 = header['grs2'][0] #
407 418 self.azipos = header['azipos'][0] #
408 419 self.azivel = header['azivel'][0] #
409 420 self.elvpos = header['elvpos'][0] #
410 421 self.elvvel = header['elvvel'][0] #
411 422 self.northAngle = header['northAngle'][0] #
412 423 self.microsec = header['microsec'][0] #
413 424 self.azisetvel = header['azisetvel'][0] #
414 425 self.elvsetpos = header['elvsetpos'][0] #
415 426 self.RadarConst = header['RadarConst'][0] #
416 #84
417
418 #print 'Pointer fp RECheader', fp.tell()
419
427 # 84
428
429 # print 'Pointer fp RECheader', fp.tell()
430
420 431 #self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
421
432
422 433 #self.RHsize = 180+20*self.nChannels
423 434 #self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
424 #print 'Datasize',self.Datasize
435 # print 'Datasize',self.Datasize
425 436 #endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
426
437
427 438 print '=============================================='
428
439
429 440 print '=============================================='
430 441
431
432 442 return 1
433
434 class MIRA35CReader (ProcessingUnit,FileHeaderMIRA35c,SRVIHeader,RecordHeader):
435
443
444
445 class MIRA35CReader (ProcessingUnit, FileHeaderMIRA35c, SRVIHeader, RecordHeader):
446
436 447 path = None
437 448 startDate = None
438 449 endDate = None
439 450 startTime = None
440 451 endTime = None
441 452 walk = None
442 453 isConfig = False
443
444
445 fileList= None
446
447 #metadata
448 TimeZone= None
449 Interval= None
450 heightList= None
451
452 #data
453 data= None
454 utctime= None
455
456
457
458 def __init__(self, **kwargs):
459
460 #Eliminar de la base la herencia
454
455 fileList = None
456
457 # metadata
458 TimeZone = None
459 Interval = None
460 heightList = None
461
462 # data
463 data = None
464 utctime = None
465
466 def __init__(self, **kwargs):
467
468 # Eliminar de la base la herencia
461 469 ProcessingUnit.__init__(self, **kwargs)
462 470 self.PointerReader = 0
463 471 self.FileHeaderFlag = False
464 472 self.utc = None
465 473 self.ext = ".zspca"
466 474 self.optchar = "P"
467 self.fpFile=None
475 self.fpFile = None
468 476 self.fp = None
469 self.BlockCounter=0
477 self.BlockCounter = 0
470 478 self.dtype = None
471 479 self.fileSizeByHeader = None
472 480 self.filenameList = []
473 481 self.fileSelector = 0
474 self.Off2StartNxtRec=0
475 self.RecCounter=0
482 self.Off2StartNxtRec = 0
483 self.RecCounter = 0
476 484 self.flagNoMoreFiles = 0
477 self.data_spc=None
478 #self.data_cspc=None
479 self.data_output=None
485 self.data_spc = None
486 # self.data_cspc=None
487 self.data_output = None
480 488 self.path = None
481 self.OffsetStartHeader=0
482 self.Off2StartData=0
489 self.OffsetStartHeader = 0
490 self.Off2StartData = 0
483 491 self.ipp = 0
484 self.nFDTdataRecors=0
492 self.nFDTdataRecors = 0
485 493 self.blocksize = 0
486 494 self.dataOut = Spectra()
487 self.profileIndex = 1 #Always
488 self.dataOut.flagNoData=False
495 self.profileIndex = 1 # Always
496 self.dataOut.flagNoData = False
489 497 self.dataOut.nRdPairs = 0
490 498 self.dataOut.pairsList = []
491 self.dataOut.data_spc=None
492
493 self.dataOut.normFactor=1
499 self.dataOut.data_spc = None
500
501 self.dataOut.normFactor = 1
494 502 self.nextfileflag = True
495 503 self.dataOut.RadarConst = 0
496 504 self.dataOut.HSDV = []
497 505 self.dataOut.NPW = []
498 506 self.dataOut.COFA = []
499 507 self.dataOut.noise = 0
500
501
508
502 509 def Files2Read(self, fp):
503 510 '''
504 511 Function that indicates the number of .fdt files that exist in the folder to be read.
505 512 It also creates an organized list with the names of the files to read.
506 513 '''
507 #self.__checkPath()
508
509 ListaData=os.listdir(fp) #Gets the list of files within the fp address
510 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
511 nFiles=0 #File Counter
512 FileList=[] #A list is created that will contain the .fdt files
513 for IndexFile in ListaData :
514 if '.zspca' in IndexFile and '.gz' not in IndexFile:
514 # self.__checkPath()
515
516 # Gets the list of files within the fp address
517 ListaData = os.listdir(fp)
518 # Sort the list of files from least to largest by names
519 ListaData = sorted(ListaData)
520 nFiles = 0 # File Counter
521 FileList = [] # A list is created that will contain the .fdt files
522 for IndexFile in ListaData:
523 if '.zspca' in IndexFile and '.gz' not in IndexFile:
515 524 FileList.append(IndexFile)
516 nFiles+=1
517
518 #print 'Files2Read'
519 #print 'Existen '+str(nFiles)+' archivos .fdt'
520
521 self.filenameList=FileList #List of files from least to largest by names
522
523
525 nFiles += 1
526
527 # print 'Files2Read'
528 # print 'Existen '+str(nFiles)+' archivos .fdt'
529
530 self.filenameList = FileList # List of files from least to largest by names
531
524 532 def run(self, **kwargs):
525 533 '''
526 534 This method will be the one that will initiate the data entry, will be called constantly.
527 535 You should first verify that your Setup () is set up and then continue to acquire
528 536 the data to be processed with getData ().
529 537 '''
530 538 if not self.isConfig:
531 539 self.setup(**kwargs)
532 540 self.isConfig = True
533
541
534 542 self.getData()
535
536
543
537 544 def setup(self, path=None,
538 startDate=None,
539 endDate=None,
540 startTime=None,
541 endTime=None,
542 walk=True,
543 timezone='utc',
544 code = None,
545 online=False,
546 ReadMode=None, **kwargs):
547
545 startDate=None,
546 endDate=None,
547 startTime=None,
548 endTime=None,
549 walk=True,
550 timezone='utc',
551 code=None,
552 online=False,
553 ReadMode=None, **kwargs):
554
548 555 self.isConfig = True
549
550 self.path=path
551 self.startDate=startDate
552 self.endDate=endDate
553 self.startTime=startTime
554 self.endTime=endTime
555 self.walk=walk
556 #self.ReadMode=int(ReadMode)
557
556
557 self.path = path
558 self.startDate = startDate
559 self.endDate = endDate
560 self.startTime = startTime
561 self.endTime = endTime
562 self.walk = walk
563 # self.ReadMode=int(ReadMode)
564
558 565 pass
559
560
566
561 567 def getData(self):
562 568 '''
563 569 Before starting this function, you should check that there is still an unread file,
564 570 If there are still blocks to read or if the data block is empty.
565
571
566 572 You should call the file "read".
567
573
568 574 '''
569
575
570 576 if self.flagNoMoreFiles:
571 577 self.dataOut.flagNoData = True
572 578 print 'NoData se vuelve true'
573 579 return 0
574
575 self.fp=self.path
580
581 self.fp = self.path
576 582 self.Files2Read(self.fp)
577 583 self.readFile(self.fp)
578
579 self.dataOut.data_spc = self.dataOut_spc#self.data_spc.copy()
584
585 self.dataOut.data_spc = self.dataOut_spc # self.data_spc.copy()
580 586 self.dataOut.RadarConst = self.RadarConst
581 self.dataOut.data_output=self.data_output
587 self.dataOut.data_output = self.data_output
582 588 self.dataOut.noise = self.dataOut.getNoise()
583 #print 'ACAAAAAA', self.dataOut.noise
584 self.dataOut.data_spc = self.dataOut.data_spc+self.dataOut.noise
585 #print 'self.dataOut.noise',self.dataOut.noise
586
587
589 # print 'ACAAAAAA', self.dataOut.noise
590 self.dataOut.data_spc = self.dataOut.data_spc + self.dataOut.noise
591 # print 'self.dataOut.noise',self.dataOut.noise
592
588 593 return self.dataOut.data_spc
589
590
591 def readFile(self,fp):
594
595 def readFile(self, fp):
592 596 '''
593 597 You must indicate if you are reading in Online or Offline mode and load the
594 598 The parameters for this file reading mode.
595
599
596 600 Then you must do 2 actions:
597
601
598 602 1. Get the BLTR FileHeader.
599 603 2. Start reading the first block.
600 604 '''
601
602 #The address of the folder is generated the name of the .fdt file that will be read
603 print "File: ",self.fileSelector+1
604
605
606 # The address of the folder is generated the name of the .fdt file that will be read
607 print "File: ", self.fileSelector + 1
608
605 609 if self.fileSelector < len(self.filenameList):
606
607 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
608
609 if self.nextfileflag==True:
610 self.fp = open(self.fpFile,"rb")
611 self.nextfileflag==False
612
610
611 self.fpFile = str(fp) + '/' + \
612 str(self.filenameList[self.fileSelector])
613
614 if self.nextfileflag == True:
615 self.fp = open(self.fpFile, "rb")
616 self.nextfileflag == False
617
613 618 '''HERE STARTING THE FILE READING'''
614
615
619
616 620 self.fheader = FileHeaderMIRA35c()
617 self.fheader.FHread(self.fp) #Bltr FileHeader Reading
618
621 self.fheader.FHread(self.fp) # Bltr FileHeader Reading
619 622
620 623 self.SPARrawGate1 = self.fheader.SPARrawGate1
621 624 self.SPARrawGate2 = self.fheader.SPARrawGate2
622 625 self.Num_Hei = self.SPARrawGate2 - self.SPARrawGate1
623 626 self.Num_Bins = self.fheader.PPARsft
624 627 self.dataOut.nFFTPoints = self.fheader.PPARsft
625
626
628
627 629 self.Num_inCoh = self.fheader.PPARavc
628 630 self.dataOut.PRF = self.fheader.PPARprf
629 self.dataOut.frequency = 34.85*10**9
630 self.Lambda = SPEED_OF_LIGHT/self.dataOut.frequency
631 self.dataOut.ippSeconds= 1./float(self.dataOut.PRF)
632
633 pulse_width = self.fheader.PPARpdr * 10**-9
631 self.dataOut.frequency = 34.85 * 10**9
632 self.Lambda = SPEED_OF_LIGHT / self.dataOut.frequency
633 self.dataOut.ippSeconds = 1. / float(self.dataOut.PRF)
634
635 pulse_width = self.fheader.PPARpdr * 10**-9
634 636 self.__deltaHeigth = 0.5 * SPEED_OF_LIGHT * pulse_width
635
636 self.data_spc = numpy.zeros((self.Num_Hei, self.Num_Bins,2))#
637
638 self.data_spc = numpy.zeros((self.Num_Hei, self.Num_Bins, 2))
637 639 self.dataOut.HSDV = numpy.zeros((self.Num_Hei, 2))
638
640
639 641 self.Ze = numpy.zeros(self.Num_Hei)
640 self.ETA = numpy.zeros(([2,self.Num_Hei]))
641
642
643
644 self.readBlock() #Block reading
645
642 self.ETA = numpy.zeros(([2, self.Num_Hei]))
643
644 self.readBlock() # Block reading
645
646 646 else:
647 647 print 'readFile FlagNoData becomes true'
648 self.flagNoMoreFiles=True
648 self.flagNoMoreFiles = True
649 649 self.dataOut.flagNoData = True
650 650 self.FileHeaderFlag == True
651 return 0
652
653
654
655 def readBlock(self):
651 return 0
652
653 def readBlock(self):
656 654 '''
657 655 It should be checked if the block has data, if it is not passed to the next file.
658
656
659 657 Then the following is done:
660
658
661 659 1. Read the RecordHeader
662 660 2. Fill the buffer with the current block number.
663
661
664 662 '''
665
663
666 664 if self.PointerReader > 1180:
667 self.fp.seek(self.PointerReader , os.SEEK_SET)
665 self.fp.seek(self.PointerReader, os.SEEK_SET)
668 666 self.FirstPoint = self.PointerReader
669
670 else :
667
668 else:
671 669 self.FirstPoint = 1180
672
673
674
670
675 671 self.srviHeader = SRVIHeader()
676
677 self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI
678
679 self.blocksize = self.srviHeader.SizeOfDataBlock1 # Se obtiene el tamao del bloque
680
672
673 self.srviHeader.SRVIread(self.fp) # Se obtiene la cabecera del SRVI
674
675 self.blocksize = self.srviHeader.SizeOfDataBlock1 # Se obtiene el tamao del bloque
676
681 677 if self.blocksize == 148:
682 678 print 'blocksize == 148 bug'
683 jump = numpy.fromfile(self.fp,[('jump',numpy.str_,140)] ,1)
684
685 self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI
686
679 jump = numpy.fromfile(self.fp, [('jump', numpy.str_, 140)], 1)
680
681 # Se obtiene la cabecera del SRVI
682 self.srviHeader.SRVIread(self.fp)
683
687 684 if not self.srviHeader.SizeOfSRVI1:
688 self.fileSelector+=1
689 self.nextfileflag==True
685 self.fileSelector += 1
686 self.nextfileflag == True
690 687 self.FileHeaderFlag == True
691
688
692 689 self.recordheader = RecordHeader()
693 690 self.recordheader.RHread(self.fp)
694 691 self.RadarConst = self.recordheader.RadarConst
695 692 dwell = self.recordheader.time_t
696 693 npw1 = self.recordheader.npw1
697 694 npw2 = self.recordheader.npw2
698
699
695
700 696 self.dataOut.channelList = range(1)
701 self.dataOut.nIncohInt = self.Num_inCoh
697 self.dataOut.nIncohInt = self.Num_inCoh
702 698 self.dataOut.nProfiles = self.Num_Bins
703 699 self.dataOut.nCohInt = 1
704 700 self.dataOut.windowOfFilter = 1
705 701 self.dataOut.utctime = dwell
706 self.dataOut.timeZone=0
707
702 self.dataOut.timeZone = 0
703
708 704 self.dataOut.outputInterval = self.dataOut.getTimeInterval()
709 self.dataOut.heightList = self.SPARrawGate1*self.__deltaHeigth + numpy.array(range(self.Num_Hei))*self.__deltaHeigth
710
711
712
713 self.HSDVsign = numpy.fromfile( self.fp, [('HSDV',numpy.str_,4)],1)
714 self.SizeHSDV = numpy.fromfile( self.fp, [('SizeHSDV','<i4')],1)
715 self.HSDV_Co = numpy.fromfile( self.fp, [('HSDV_Co','<f4')],self.Num_Hei)
716 self.HSDV_Cx = numpy.fromfile( self.fp, [('HSDV_Cx','<f4')],self.Num_Hei)
717
718 self.COFAsign = numpy.fromfile( self.fp, [('COFA',numpy.str_,4)],1)
719 self.SizeCOFA = numpy.fromfile( self.fp, [('SizeCOFA','<i4')],1)
720 self.COFA_Co = numpy.fromfile( self.fp, [('COFA_Co','<f4')],self.Num_Hei)
721 self.COFA_Cx = numpy.fromfile( self.fp, [('COFA_Cx','<f4')],self.Num_Hei)
722
723 self.ZSPCsign = numpy.fromfile(self.fp, [('ZSPCsign',numpy.str_,4)],1)
724 self.SizeZSPC = numpy.fromfile(self.fp, [('SizeZSPC','<i4')],1)
725
726 self.dataOut.HSDV[0]=self.HSDV_Co[:][0]
727 self.dataOut.HSDV[1]=self.HSDV_Cx[:][0]
728
705 self.dataOut.heightList = self.SPARrawGate1 * self.__deltaHeigth + \
706 numpy.array(range(self.Num_Hei)) * self.__deltaHeigth
707
708 self.HSDVsign = numpy.fromfile(self.fp, [('HSDV', numpy.str_, 4)], 1)
709 self.SizeHSDV = numpy.fromfile(self.fp, [('SizeHSDV', '<i4')], 1)
710 self.HSDV_Co = numpy.fromfile(
711 self.fp, [('HSDV_Co', '<f4')], self.Num_Hei)
712 self.HSDV_Cx = numpy.fromfile(
713 self.fp, [('HSDV_Cx', '<f4')], self.Num_Hei)
714
715 self.COFAsign = numpy.fromfile(self.fp, [('COFA', numpy.str_, 4)], 1)
716 self.SizeCOFA = numpy.fromfile(self.fp, [('SizeCOFA', '<i4')], 1)
717 self.COFA_Co = numpy.fromfile(
718 self.fp, [('COFA_Co', '<f4')], self.Num_Hei)
719 self.COFA_Cx = numpy.fromfile(
720 self.fp, [('COFA_Cx', '<f4')], self.Num_Hei)
721
722 self.ZSPCsign = numpy.fromfile(
723 self.fp, [('ZSPCsign', numpy.str_, 4)], 1)
724 self.SizeZSPC = numpy.fromfile(self.fp, [('SizeZSPC', '<i4')], 1)
725
726 self.dataOut.HSDV[0] = self.HSDV_Co[:][0]
727 self.dataOut.HSDV[1] = self.HSDV_Cx[:][0]
728
729 729 for irg in range(self.Num_Hei):
730 nspc = numpy.fromfile(self.fp, [('nspc','int16')],1)[0][0] # Number of spectral sub pieces containing significant power
731
730 # Number of spectral sub pieces containing significant power
731 nspc = numpy.fromfile(self.fp, [('nspc', 'int16')], 1)[0][0]
732
732 733 for k in range(nspc):
733 binIndex = numpy.fromfile(self.fp, [('binIndex','int16')],1)[0][0] # Index of the spectral bin where the piece is beginning
734 nbins = numpy.fromfile(self.fp, [('nbins','int16')],1)[0][0] # Number of bins of the piece
735
736 #Co_Channel
737 jbin = numpy.fromfile(self.fp, [('jbin','uint16')],nbins)[0][0] # Spectrum piece to be normaliced
738 jmax = numpy.fromfile(self.fp, [('jmax','float32')],1)[0][0] # Maximun piece to be normaliced
739
740
741 self.data_spc[irg,binIndex:binIndex+nbins,0] = self.data_spc[irg,binIndex:binIndex+nbins,0]+jbin/65530.*jmax
742
743 #Cx_Channel
744 jbin = numpy.fromfile(self.fp, [('jbin','uint16')],nbins)[0][0]
745 jmax = numpy.fromfile(self.fp, [('jmax','float32')],1)[0][0]
746
747
748 self.data_spc[irg,binIndex:binIndex+nbins,1] = self.data_spc[irg,binIndex:binIndex+nbins,1]+jbin/65530.*jmax
749
750 for bin in range(self.Num_Bins):
751
752 self.data_spc[:,bin,0] = self.data_spc[:,bin,0] - self.dataOut.HSDV[:,0]
753
754 self.data_spc[:,bin,1] = self.data_spc[:,bin,1] - self.dataOut.HSDV[:,1]
755
756
734 # Index of the spectral bin where the piece is beginning
735 binIndex = numpy.fromfile(
736 self.fp, [('binIndex', 'int16')], 1)[0][0]
737 nbins = numpy.fromfile(self.fp, [('nbins', 'int16')], 1)[
738 0][0] # Number of bins of the piece
739
740 # Co_Channel
741 jbin = numpy.fromfile(self.fp, [('jbin', 'uint16')], nbins)[
742 0][0] # Spectrum piece to be normaliced
743 jmax = numpy.fromfile(self.fp, [('jmax', 'float32')], 1)[
744 0][0] # Maximun piece to be normaliced
745
746 self.data_spc[irg, binIndex:binIndex + nbins, 0] = self.data_spc[irg,
747 binIndex:binIndex + nbins, 0] + jbin / 65530. * jmax
748
749 # Cx_Channel
750 jbin = numpy.fromfile(
751 self.fp, [('jbin', 'uint16')], nbins)[0][0]
752 jmax = numpy.fromfile(self.fp, [('jmax', 'float32')], 1)[0][0]
753
754 self.data_spc[irg, binIndex:binIndex + nbins, 1] = self.data_spc[irg,
755 binIndex:binIndex + nbins, 1] + jbin / 65530. * jmax
756
757 for bin in range(self.Num_Bins):
758
759 self.data_spc[:, bin, 0] = self.data_spc[:,
760 bin, 0] - self.dataOut.HSDV[:, 0]
761
762 self.data_spc[:, bin, 1] = self.data_spc[:,
763 bin, 1] - self.dataOut.HSDV[:, 1]
764
757 765 numpy.set_printoptions(threshold='nan')
758
759 self.data_spc = numpy.where(self.data_spc > 0. , self.data_spc, 0)
760
761 self.dataOut.COFA = numpy.array([self.COFA_Co , self.COFA_Cx])
762
766
767 self.data_spc = numpy.where(self.data_spc > 0., self.data_spc, 0)
768
769 self.dataOut.COFA = numpy.array([self.COFA_Co, self.COFA_Cx])
770
763 771 print ' '
764 print 'SPC',numpy.shape(self.dataOut.data_spc)
765 #print 'SPC',self.dataOut.data_spc
766
772 print 'SPC', numpy.shape(self.dataOut.data_spc)
773 # print 'SPC',self.dataOut.data_spc
774
767 775 noinor1 = 713031680
768 776 noinor2 = 30
769
770 npw1 = 1#0**(npw1/10) * noinor1 * noinor2
771 npw2 = 1#0**(npw2/10) * noinor1 * noinor2
777
778 npw1 = 1 # 0**(npw1/10) * noinor1 * noinor2
779 npw2 = 1 # 0**(npw2/10) * noinor1 * noinor2
772 780 self.dataOut.NPW = numpy.array([npw1, npw2])
773
781
774 782 print ' '
775
776 self.data_spc = numpy.transpose(self.data_spc, (2,1,0))
777 self.data_spc = numpy.fft.fftshift(self.data_spc, axes = 1)
778
783
784 self.data_spc = numpy.transpose(self.data_spc, (2, 1, 0))
785 self.data_spc = numpy.fft.fftshift(self.data_spc, axes=1)
786
779 787 self.data_spc = numpy.fliplr(self.data_spc)
780
781 self.data_spc = numpy.where(self.data_spc > 0. , self.data_spc, 0)
782 self.dataOut_spc= numpy.ones([1, self.Num_Bins , self.Num_Hei])
783 self.dataOut_spc[0,:,:] = self.data_spc[0,:,:]
784 #print 'SHAPE', self.dataOut_spc.shape
785 #For nyquist correction:
786 #fix = 20 # ~3m/s
788
789 self.data_spc = numpy.where(self.data_spc > 0., self.data_spc, 0)
790 self.dataOut_spc = numpy.ones([1, self.Num_Bins, self.Num_Hei])
791 self.dataOut_spc[0, :, :] = self.data_spc[0, :, :]
792 # print 'SHAPE', self.dataOut_spc.shape
793 # For nyquist correction:
794 # fix = 20 # ~3m/s
787 795 #shift = self.Num_Bins/2 + fix
788 796 #self.data_spc = numpy.array([ self.data_spc[: , self.Num_Bins-shift+1: , :] , self.data_spc[: , 0:self.Num_Bins-shift , :]])
789
790
791
797
792 798 '''Block Reading, the Block Data is received and Reshape is used to give it
793 799 shape.
794 800 '''
795
801
796 802 self.PointerReader = self.fp.tell()
797
798
799
800
801
802
803 No newline at end of file
@@ -1,4045 +1,4044
1 1 import numpy
2 2 import math
3 3 from scipy import optimize, interpolate, signal, stats, ndimage
4 4 import scipy
5 5 import re
6 6 import datetime
7 7 import copy
8 8 import sys
9 9 import importlib
10 10 import itertools
11 11 from multiprocessing import Pool, TimeoutError
12 12 from multiprocessing.pool import ThreadPool
13 13 import copy_reg
14 14 import cPickle
15 15 import types
16 16 from functools import partial
17 17 import time
18 18 #from sklearn.cluster import KMeans
19 19
20 import matplotlib.pyplot as plt
21 20
22 21 from scipy.optimize import fmin_l_bfgs_b #optimize with bounds on state papameters
23 22 from jroproc_base import ProcessingUnit, Operation
24 23 from schainpy.model.data.jrodata import Parameters, hildebrand_sekhon
25 24 from scipy import asarray as ar,exp
26 25 from scipy.optimize import curve_fit
27 26
28 27 import warnings
29 28 from numpy import NaN
30 29 from scipy.optimize.optimize import OptimizeWarning
31 30 warnings.filterwarnings('ignore')
32 31
33 32
34 33 SPEED_OF_LIGHT = 299792458
35 34
36 35
37 36 '''solving pickling issue'''
38 37
39 38 def _pickle_method(method):
40 39 func_name = method.im_func.__name__
41 40 obj = method.im_self
42 41 cls = method.im_class
43 42 return _unpickle_method, (func_name, obj, cls)
44 43
45 44 def _unpickle_method(func_name, obj, cls):
46 45 for cls in cls.mro():
47 46 try:
48 47 func = cls.__dict__[func_name]
49 48 except KeyError:
50 49 pass
51 50 else:
52 51 break
53 52 return func.__get__(obj, cls)
54 53
55 54 class ParametersProc(ProcessingUnit):
56 55
57 56 nSeconds = None
58 57
59 58 def __init__(self):
60 59 ProcessingUnit.__init__(self)
61 60
62 61 # self.objectDict = {}
63 62 self.buffer = None
64 63 self.firstdatatime = None
65 64 self.profIndex = 0
66 65 self.dataOut = Parameters()
67 66
68 67 def __updateObjFromInput(self):
69 68
70 69 self.dataOut.inputUnit = self.dataIn.type
71 70
72 71 self.dataOut.timeZone = self.dataIn.timeZone
73 72 self.dataOut.dstFlag = self.dataIn.dstFlag
74 73 self.dataOut.errorCount = self.dataIn.errorCount
75 74 self.dataOut.useLocalTime = self.dataIn.useLocalTime
76 75
77 76 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
78 77 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
79 78 self.dataOut.channelList = self.dataIn.channelList
80 79 self.dataOut.heightList = self.dataIn.heightList
81 80 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
82 81 # self.dataOut.nHeights = self.dataIn.nHeights
83 82 # self.dataOut.nChannels = self.dataIn.nChannels
84 83 self.dataOut.nBaud = self.dataIn.nBaud
85 84 self.dataOut.nCode = self.dataIn.nCode
86 85 self.dataOut.code = self.dataIn.code
87 86 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
88 87 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
89 88 # self.dataOut.utctime = self.firstdatatime
90 89 self.dataOut.utctime = self.dataIn.utctime
91 90 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
92 91 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
93 92 self.dataOut.nCohInt = self.dataIn.nCohInt
94 93 # self.dataOut.nIncohInt = 1
95 94 self.dataOut.ippSeconds = self.dataIn.ippSeconds
96 95 # self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
97 96 self.dataOut.timeInterval1 = self.dataIn.timeInterval
98 97 self.dataOut.heightList = self.dataIn.getHeiRange()
99 98 self.dataOut.frequency = self.dataIn.frequency
100 99 # self.dataOut.noise = self.dataIn.noise
101 100
102 101 def run(self):
103 102
104 103 #---------------------- Voltage Data ---------------------------
105 104
106 105 if self.dataIn.type == "Voltage":
107 106
108 107 self.__updateObjFromInput()
109 108 self.dataOut.data_pre = self.dataIn.data.copy()
110 109 self.dataOut.flagNoData = False
111 110 self.dataOut.utctimeInit = self.dataIn.utctime
112 111 self.dataOut.paramInterval = self.dataIn.nProfiles*self.dataIn.nCohInt*self.dataIn.ippSeconds
113 112 return
114 113
115 114 #---------------------- Spectra Data ---------------------------
116 115
117 116 if self.dataIn.type == "Spectra":
118 117
119 118 self.dataOut.data_pre = (self.dataIn.data_spc, self.dataIn.data_cspc)
120 119 self.dataOut.data_spc = self.dataIn.data_spc
121 120 self.dataOut.data_cspc = self.dataIn.data_cspc
122 121 self.dataOut.nProfiles = self.dataIn.nProfiles
123 122 self.dataOut.nIncohInt = self.dataIn.nIncohInt
124 123 self.dataOut.nFFTPoints = self.dataIn.nFFTPoints
125 124 self.dataOut.ippFactor = self.dataIn.ippFactor
126 125 self.dataOut.abscissaList = self.dataIn.getVelRange(1)
127 126 self.dataOut.spc_noise = self.dataIn.getNoise()
128 127 self.dataOut.spc_range = (self.dataIn.getFreqRange(1)/1000. , self.dataIn.getAcfRange(1) , self.dataIn.getVelRange(1))
129 128 self.dataOut.pairsList = self.dataIn.pairsList
130 129 self.dataOut.groupList = self.dataIn.pairsList
131 130 self.dataOut.flagNoData = False
132 131
133 132 if hasattr(self.dataIn, 'ChanDist'): #Distances of receiver channels
134 133 self.dataOut.ChanDist = self.dataIn.ChanDist
135 134 else: self.dataOut.ChanDist = None
136 135
137 136 if hasattr(self.dataIn, 'VelRange'): #Velocities range
138 137 self.dataOut.VelRange = self.dataIn.VelRange
139 138 else: self.dataOut.VelRange = None
140 139
141 140 if hasattr(self.dataIn, 'RadarConst'): #Radar Constant
142 141 self.dataOut.RadarConst = self.dataIn.RadarConst
143 142
144 143 if hasattr(self.dataIn, 'NPW'): #NPW
145 144 self.dataOut.NPW = self.dataIn.NPW
146 145
147 146 if hasattr(self.dataIn, 'COFA'): #COFA
148 147 self.dataOut.COFA = self.dataIn.COFA
149 148
150 149
151 150
152 151 #---------------------- Correlation Data ---------------------------
153 152
154 153 if self.dataIn.type == "Correlation":
155 154 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.dataIn.splitFunctions()
156 155
157 156 self.dataOut.data_pre = (self.dataIn.data_cf[acf_ind,:], self.dataIn.data_cf[ccf_ind,:,:])
158 157 self.dataOut.normFactor = (self.dataIn.normFactor[acf_ind,:], self.dataIn.normFactor[ccf_ind,:])
159 158 self.dataOut.groupList = (acf_pairs, ccf_pairs)
160 159
161 160 self.dataOut.abscissaList = self.dataIn.lagRange
162 161 self.dataOut.noise = self.dataIn.noise
163 162 self.dataOut.data_SNR = self.dataIn.SNR
164 163 self.dataOut.flagNoData = False
165 164 self.dataOut.nAvg = self.dataIn.nAvg
166 165
167 166 #---------------------- Parameters Data ---------------------------
168 167
169 168 if self.dataIn.type == "Parameters":
170 169 self.dataOut.copy(self.dataIn)
171 170 self.dataOut.flagNoData = False
172 171
173 172 return True
174 173
175 174 self.__updateObjFromInput()
176 175 self.dataOut.utctimeInit = self.dataIn.utctime
177 176 self.dataOut.paramInterval = self.dataIn.timeInterval
178 177
179 178 return
180 179
181 180
182 181 def target(tups):
183 182
184 183 obj, args = tups
185 184 #print 'TARGETTT', obj, args
186 185 return obj.FitGau(args)
187 186
188 187 class GaussianFit(Operation):
189 188
190 189 '''
191 190 Function that fit of one and two generalized gaussians (gg) based
192 191 on the PSD shape across an "power band" identified from a cumsum of
193 192 the measured spectrum - noise.
194 193
195 194 Input:
196 195 self.dataOut.data_pre : SelfSpectra
197 196
198 197 Output:
199 198 self.dataOut.GauSPC : SPC_ch1, SPC_ch2
200 199
201 200 '''
202 201 def __init__(self, **kwargs):
203 202 Operation.__init__(self, **kwargs)
204 203 self.i=0
205 204
206 205
207 206 def run(self, dataOut, num_intg=7, pnoise=1., vel_arr=None, SNRlimit=-9): #num_intg: Incoherent integrations, pnoise: Noise, vel_arr: range of velocities, similar to the ftt points
208 207 """This routine will find a couple of generalized Gaussians to a power spectrum
209 208 input: spc
210 209 output:
211 210 Amplitude0,shift0,width0,p0,Amplitude1,shift1,width1,p1,noise
212 211 """
213 212
214 213 self.spc = dataOut.data_pre[0].copy()
215 214
216 215
217 216 print 'SelfSpectra Shape', numpy.asarray(self.spc).shape
218 217
219 218
220 219 #plt.figure(50)
221 220 #plt.subplot(121)
222 221 #plt.plot(self.spc,'k',label='spc(66)')
223 222 #plt.plot(xFrec,ySamples[1],'g',label='Ch1')
224 223 #plt.plot(xFrec,ySamples[2],'r',label='Ch2')
225 224 #plt.plot(xFrec,FitGauss,'yo:',label='fit')
226 225 #plt.legend()
227 226 #plt.title('DATOS A ALTURA DE 7500 METROS')
228 227 #plt.show()
229 228
230 229 self.Num_Hei = self.spc.shape[2]
231 230 #self.Num_Bin = len(self.spc)
232 231 self.Num_Bin = self.spc.shape[1]
233 232 self.Num_Chn = self.spc.shape[0]
234 233
235 234 Vrange = dataOut.abscissaList
236 235
237 236 #print 'self.spc2', numpy.asarray(self.spc).shape
238 237
239 238 GauSPC = numpy.empty([2,self.Num_Bin,self.Num_Hei])
240 239 SPC_ch1 = numpy.empty([self.Num_Bin,self.Num_Hei])
241 240 SPC_ch2 = numpy.empty([self.Num_Bin,self.Num_Hei])
242 241 SPC_ch1[:] = numpy.NaN
243 242 SPC_ch2[:] = numpy.NaN
244 243
245 244
246 245 start_time = time.time()
247 246
248 247 noise_ = dataOut.spc_noise[0].copy()
249 248
250 249
251 250
252 251 pool = Pool(processes=self.Num_Chn)
253 252 args = [(Vrange, Ch, pnoise, noise_, num_intg, SNRlimit) for Ch in range(self.Num_Chn)]
254 253 objs = [self for __ in range(self.Num_Chn)]
255 254 attrs = zip(objs, args)
256 255 gauSPC = pool.map(target, attrs)
257 256 dataOut.GauSPC = numpy.asarray(gauSPC)
258 257 # ret = []
259 258 # for n in range(self.Num_Chn):
260 259 # self.FitGau(args[n])
261 260 # dataOut.GauSPC = ret
262 261
263 262
264 263
265 264 # for ch in range(self.Num_Chn):
266 265 #
267 266 # for ht in range(self.Num_Hei):
268 267 # #print (numpy.asarray(self.spc).shape)
269 268 # spc = numpy.asarray(self.spc)[ch,:,ht]
270 269 #
271 270 # #############################################
272 271 # # normalizing spc and noise
273 272 # # This part differs from gg1
274 273 # spc_norm_max = max(spc)
275 274 # spc = spc / spc_norm_max
276 275 # pnoise = pnoise / spc_norm_max
277 276 # #############################################
278 277 #
279 278 # if abs(vel_arr[0])<15.0: # this switch is for spectra collected with different length IPP's
280 279 # fatspectra=1.0
281 280 # else:
282 281 # fatspectra=0.5
283 282 #
284 283 # wnoise = noise_ / spc_norm_max
285 284 # #print 'wnoise', noise_, dataOut.spc_noise[0], wnoise
286 285 # #wnoise,stdv,i_max,index =enoise(spc,num_intg) #noise estimate using Hildebrand Sekhon, only wnoise is used
287 286 # #if wnoise>1.1*pnoise: # to be tested later
288 287 # # wnoise=pnoise
289 288 # noisebl=wnoise*0.9; noisebh=wnoise*1.1
290 289 # spc=spc-wnoise
291 290 #
292 291 # minx=numpy.argmin(spc)
293 292 # spcs=numpy.roll(spc,-minx)
294 293 # cum=numpy.cumsum(spcs)
295 294 # tot_noise=wnoise * self.Num_Bin #64;
296 295 # #tot_signal=sum(cum[-5:])/5.; ''' How does this line work? '''
297 296 # #snr=tot_signal/tot_noise
298 297 # #snr=cum[-1]/tot_noise
299 298 #
300 299 # #print 'spc' , spcs[5:8] , 'tot_noise', tot_noise
301 300 #
302 301 # snr = sum(spcs)/tot_noise
303 302 # snrdB=10.*numpy.log10(snr)
304 303 #
305 304 # #if snrdB < -9 :
306 305 # # snrdB = numpy.NaN
307 306 # # continue
308 307 #
309 308 # #print 'snr',snrdB # , sum(spcs) , tot_noise
310 309 #
311 310 #
312 311 # #if snrdB<-18 or numpy.isnan(snrdB) or num_intg<4:
313 312 # # return [None,]*4,[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
314 313 #
315 314 # cummax=max(cum); epsi=0.08*fatspectra # cumsum to narrow down the energy region
316 315 # cumlo=cummax*epsi;
317 316 # cumhi=cummax*(1-epsi)
318 317 # powerindex=numpy.array(numpy.where(numpy.logical_and(cum>cumlo, cum<cumhi))[0])
319 318 #
320 319 # #if len(powerindex)==1:
321 320 # ##return [numpy.mod(powerindex[0]+minx,64),None,None,None,],[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
322 321 # #return [numpy.mod(powerindex[0]+minx, self.Num_Bin ),None,None,None,],[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
323 322 # #elif len(powerindex)<4*fatspectra:
324 323 # #return [None,]*4,[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
325 324 #
326 325 # if len(powerindex) < 1:# case for powerindex 0
327 326 # continue
328 327 # powerlo=powerindex[0]
329 328 # powerhi=powerindex[-1]
330 329 # powerwidth=powerhi-powerlo
331 330 #
332 331 # firstpeak=powerlo+powerwidth/10.# first gaussian energy location
333 332 # secondpeak=powerhi-powerwidth/10.#second gaussian energy location
334 333 # midpeak=(firstpeak+secondpeak)/2.
335 334 # firstamp=spcs[int(firstpeak)]
336 335 # secondamp=spcs[int(secondpeak)]
337 336 # midamp=spcs[int(midpeak)]
338 337 # #x=numpy.spc.shape[1]
339 338 #
340 339 # #x=numpy.arange(64)
341 340 # x=numpy.arange( self.Num_Bin )
342 341 # y_data=spc+wnoise
343 342 #
344 343 # # single gaussian
345 344 # #shift0=numpy.mod(midpeak+minx,64)
346 345 # shift0=numpy.mod(midpeak+minx, self.Num_Bin )
347 346 # width0=powerwidth/4.#Initialization entire power of spectrum divided by 4
348 347 # power0=2.
349 348 # amplitude0=midamp
350 349 # state0=[shift0,width0,amplitude0,power0,wnoise]
351 350 # #bnds=((0,63),(1,powerwidth),(0,None),(0.5,3.),(noisebl,noisebh))
352 351 # bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth),(0,None),(0.5,3.),(noisebl,noisebh))
353 352 # #bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth),(0,None),(0.5,3.),(0.1,0.5))
354 353 # # bnds = range of fft, power width, amplitude, power, noise
355 354 # lsq1=fmin_l_bfgs_b(self.misfit1,state0,args=(y_data,x,num_intg),bounds=bnds,approx_grad=True)
356 355 #
357 356 # chiSq1=lsq1[1];
358 357 # jack1= self.y_jacobian1(x,lsq1[0])
359 358 #
360 359 #
361 360 # try:
362 361 # sigmas1=numpy.sqrt(chiSq1*numpy.diag(numpy.linalg.inv(numpy.dot(jack1.T,jack1))))
363 362 # except:
364 363 # std1=32.; sigmas1=numpy.ones(5)
365 364 # else:
366 365 # std1=sigmas1[0]
367 366 #
368 367 #
369 368 # if fatspectra<1.0 and powerwidth<4:
370 369 # choice=0
371 370 # Amplitude0=lsq1[0][2]
372 371 # shift0=lsq1[0][0]
373 372 # width0=lsq1[0][1]
374 373 # p0=lsq1[0][3]
375 374 # Amplitude1=0.
376 375 # shift1=0.
377 376 # width1=0.
378 377 # p1=0.
379 378 # noise=lsq1[0][4]
380 379 # #return (numpy.array([shift0,width0,Amplitude0,p0]),
381 380 # # numpy.array([shift1,width1,Amplitude1,p1]),noise,snrdB,chiSq1,6.,sigmas1,[None,]*9,choice)
382 381 #
383 382 # # two gaussians
384 383 # #shift0=numpy.mod(firstpeak+minx,64); shift1=numpy.mod(secondpeak+minx,64)
385 384 # shift0=numpy.mod(firstpeak+minx, self.Num_Bin );
386 385 # shift1=numpy.mod(secondpeak+minx, self.Num_Bin )
387 386 # width0=powerwidth/6.;
388 387 # width1=width0
389 388 # power0=2.;
390 389 # power1=power0
391 390 # amplitude0=firstamp;
392 391 # amplitude1=secondamp
393 392 # state0=[shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,wnoise]
394 393 # #bnds=((0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
395 394 # bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth/2.),(0,None),(0.5,3.),( 0,(self.Num_Bin-1)),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
396 395 # #bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth/2.),(0,None),(0.5,3.),( 0,(self.Num_Bin-1)),(1,powerwidth/2.),(0,None),(0.5,3.),(0.1,0.5))
397 396 #
398 397 # lsq2=fmin_l_bfgs_b(self.misfit2,state0,args=(y_data,x,num_intg),bounds=bnds,approx_grad=True)
399 398 #
400 399 #
401 400 # chiSq2=lsq2[1]; jack2=self.y_jacobian2(x,lsq2[0])
402 401 #
403 402 #
404 403 # try:
405 404 # sigmas2=numpy.sqrt(chiSq2*numpy.diag(numpy.linalg.inv(numpy.dot(jack2.T,jack2))))
406 405 # except:
407 406 # std2a=32.; std2b=32.; sigmas2=numpy.ones(9)
408 407 # else:
409 408 # std2a=sigmas2[0]; std2b=sigmas2[4]
410 409 #
411 410 #
412 411 #
413 412 # oneG=(chiSq1<5 and chiSq1/chiSq2<2.0) and (abs(lsq2[0][0]-lsq2[0][4])<(lsq2[0][1]+lsq2[0][5])/3. or abs(lsq2[0][0]-lsq2[0][4])<10)
414 413 #
415 414 # if snrdB>-9: # when SNR is strong pick the peak with least shift (LOS velocity) error
416 415 # if oneG:
417 416 # choice=0
418 417 # else:
419 418 # w1=lsq2[0][1]; w2=lsq2[0][5]
420 419 # a1=lsq2[0][2]; a2=lsq2[0][6]
421 420 # p1=lsq2[0][3]; p2=lsq2[0][7]
422 421 # s1=(2**(1+1./p1))*scipy.special.gamma(1./p1)/p1; s2=(2**(1+1./p2))*scipy.special.gamma(1./p2)/p2;
423 422 # gp1=a1*w1*s1; gp2=a2*w2*s2 # power content of each ggaussian with proper p scaling
424 423 #
425 424 # if gp1>gp2:
426 425 # if a1>0.7*a2:
427 426 # choice=1
428 427 # else:
429 428 # choice=2
430 429 # elif gp2>gp1:
431 430 # if a2>0.7*a1:
432 431 # choice=2
433 432 # else:
434 433 # choice=1
435 434 # else:
436 435 # choice=numpy.argmax([a1,a2])+1
437 436 # #else:
438 437 # #choice=argmin([std2a,std2b])+1
439 438 #
440 439 # else: # with low SNR go to the most energetic peak
441 440 # choice=numpy.argmax([lsq1[0][2]*lsq1[0][1],lsq2[0][2]*lsq2[0][1],lsq2[0][6]*lsq2[0][5]])
442 441 #
443 442 # #print 'choice',choice
444 443 #
445 444 # if choice==0: # pick the single gaussian fit
446 445 # Amplitude0=lsq1[0][2]
447 446 # shift0=lsq1[0][0]
448 447 # width0=lsq1[0][1]
449 448 # p0=lsq1[0][3]
450 449 # Amplitude1=0.
451 450 # shift1=0.
452 451 # width1=0.
453 452 # p1=0.
454 453 # noise=lsq1[0][4]
455 454 # elif choice==1: # take the first one of the 2 gaussians fitted
456 455 # Amplitude0 = lsq2[0][2]
457 456 # shift0 = lsq2[0][0]
458 457 # width0 = lsq2[0][1]
459 458 # p0 = lsq2[0][3]
460 459 # Amplitude1 = lsq2[0][6] # This is 0 in gg1
461 460 # shift1 = lsq2[0][4] # This is 0 in gg1
462 461 # width1 = lsq2[0][5] # This is 0 in gg1
463 462 # p1 = lsq2[0][7] # This is 0 in gg1
464 463 # noise = lsq2[0][8]
465 464 # else: # the second one
466 465 # Amplitude0 = lsq2[0][6]
467 466 # shift0 = lsq2[0][4]
468 467 # width0 = lsq2[0][5]
469 468 # p0 = lsq2[0][7]
470 469 # Amplitude1 = lsq2[0][2] # This is 0 in gg1
471 470 # shift1 = lsq2[0][0] # This is 0 in gg1
472 471 # width1 = lsq2[0][1] # This is 0 in gg1
473 472 # p1 = lsq2[0][3] # This is 0 in gg1
474 473 # noise = lsq2[0][8]
475 474 #
476 475 # #print len(noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0)
477 476 # SPC_ch1[:,ht] = noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0
478 477 # SPC_ch2[:,ht] = noise + Amplitude1*numpy.exp(-0.5*(abs(x-shift1))/width1)**p1
479 478 # #print 'SPC_ch1.shape',SPC_ch1.shape
480 479 # #print 'SPC_ch2.shape',SPC_ch2.shape
481 480 # #dataOut.data_param = SPC_ch1
482 481 # GauSPC[0] = SPC_ch1
483 482 # GauSPC[1] = SPC_ch2
484 483
485 484 # #plt.gcf().clear()
486 485 # plt.figure(50+self.i)
487 486 # self.i=self.i+1
488 487 # #plt.subplot(121)
489 488 # plt.plot(self.spc,'k')#,label='spc(66)')
490 489 # plt.plot(SPC_ch1[ch,ht],'b')#,label='gg1')
491 490 # #plt.plot(SPC_ch2,'r')#,label='gg2')
492 491 # #plt.plot(xFrec,ySamples[1],'g',label='Ch1')
493 492 # #plt.plot(xFrec,ySamples[2],'r',label='Ch2')
494 493 # #plt.plot(xFrec,FitGauss,'yo:',label='fit')
495 494 # plt.legend()
496 495 # plt.title('DATOS A ALTURA DE 7500 METROS')
497 496 # plt.show()
498 497 # print 'shift0', shift0
499 498 # print 'Amplitude0', Amplitude0
500 499 # print 'width0', width0
501 500 # print 'p0', p0
502 501 # print '========================'
503 502 # print 'shift1', shift1
504 503 # print 'Amplitude1', Amplitude1
505 504 # print 'width1', width1
506 505 # print 'p1', p1
507 506 # print 'noise', noise
508 507 # print 's_noise', wnoise
509 508
510 509 print '========================================================'
511 510 print 'total_time: ', time.time()-start_time
512 511
513 512 # re-normalizing spc and noise
514 513 # This part differs from gg1
515 514
516 515
517 516
518 517 ''' Parameters:
519 518 1. Amplitude
520 519 2. Shift
521 520 3. Width
522 521 4. Power
523 522 '''
524 523
525 524
526 525 ###############################################################################
527 526 def FitGau(self, X):
528 527
529 528 Vrange, ch, pnoise, noise_, num_intg, SNRlimit = X
530 529 #print 'VARSSSS', ch, pnoise, noise, num_intg
531 530
532 531 #print 'HEIGHTS', self.Num_Hei
533 532
534 533 GauSPC = []
535 534 SPC_ch1 = numpy.empty([self.Num_Bin,self.Num_Hei])
536 535 SPC_ch2 = numpy.empty([self.Num_Bin,self.Num_Hei])
537 536 SPC_ch1[:] = 0#numpy.NaN
538 537 SPC_ch2[:] = 0#numpy.NaN
539 538
540 539
541 540
542 541 for ht in range(self.Num_Hei):
543 542 #print (numpy.asarray(self.spc).shape)
544 543
545 544 #print 'TTTTT', ch , ht
546 545 #print self.spc.shape
547 546
548 547
549 548 spc = numpy.asarray(self.spc)[ch,:,ht]
550 549
551 550 #############################################
552 551 # normalizing spc and noise
553 552 # This part differs from gg1
554 553 spc_norm_max = max(spc)
555 554 spc = spc / spc_norm_max
556 555 pnoise = pnoise / spc_norm_max
557 556 #############################################
558 557
559 558 fatspectra=1.0
560 559
561 560 wnoise = noise_ / spc_norm_max
562 561 #wnoise,stdv,i_max,index =enoise(spc,num_intg) #noise estimate using Hildebrand Sekhon, only wnoise is used
563 562 #if wnoise>1.1*pnoise: # to be tested later
564 563 # wnoise=pnoise
565 564 noisebl=wnoise*0.9; noisebh=wnoise*1.1
566 565 spc=spc-wnoise
567 566 # print 'wnoise', noise_[0], spc_norm_max, wnoise
568 567 minx=numpy.argmin(spc)
569 568 spcs=numpy.roll(spc,-minx)
570 569 cum=numpy.cumsum(spcs)
571 570 tot_noise=wnoise * self.Num_Bin #64;
572 571 #print 'spc' , spcs[5:8] , 'tot_noise', tot_noise
573 572 #tot_signal=sum(cum[-5:])/5.; ''' How does this line work? '''
574 573 #snr=tot_signal/tot_noise
575 574 #snr=cum[-1]/tot_noise
576 575 snr = sum(spcs)/tot_noise
577 576 snrdB=10.*numpy.log10(snr)
578 577
579 578 if snrdB < SNRlimit :
580 579 snr = numpy.NaN
581 580 SPC_ch1[:,ht] = 0#numpy.NaN
582 581 SPC_ch1[:,ht] = 0#numpy.NaN
583 582 GauSPC = (SPC_ch1,SPC_ch2)
584 583 continue
585 584 #print 'snr',snrdB #, sum(spcs) , tot_noise
586 585
587 586
588 587
589 588 #if snrdB<-18 or numpy.isnan(snrdB) or num_intg<4:
590 589 # return [None,]*4,[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
591 590
592 591 cummax=max(cum); epsi=0.08*fatspectra # cumsum to narrow down the energy region
593 592 cumlo=cummax*epsi;
594 593 cumhi=cummax*(1-epsi)
595 594 powerindex=numpy.array(numpy.where(numpy.logical_and(cum>cumlo, cum<cumhi))[0])
596 595
597 596
598 597 if len(powerindex) < 1:# case for powerindex 0
599 598 continue
600 599 powerlo=powerindex[0]
601 600 powerhi=powerindex[-1]
602 601 powerwidth=powerhi-powerlo
603 602
604 603 firstpeak=powerlo+powerwidth/10.# first gaussian energy location
605 604 secondpeak=powerhi-powerwidth/10.#second gaussian energy location
606 605 midpeak=(firstpeak+secondpeak)/2.
607 606 firstamp=spcs[int(firstpeak)]
608 607 secondamp=spcs[int(secondpeak)]
609 608 midamp=spcs[int(midpeak)]
610 609
611 610 x=numpy.arange( self.Num_Bin )
612 611 y_data=spc+wnoise
613 612
614 613 # single gaussian
615 614 shift0=numpy.mod(midpeak+minx, self.Num_Bin )
616 615 width0=powerwidth/4.#Initialization entire power of spectrum divided by 4
617 616 power0=2.
618 617 amplitude0=midamp
619 618 state0=[shift0,width0,amplitude0,power0,wnoise]
620 619 bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth),(0,None),(0.5,3.),(noisebl,noisebh))
621 620 lsq1=fmin_l_bfgs_b(self.misfit1,state0,args=(y_data,x,num_intg),bounds=bnds,approx_grad=True)
622 621
623 622 chiSq1=lsq1[1];
624 623 jack1= self.y_jacobian1(x,lsq1[0])
625 624
626 625
627 626 try:
628 627 sigmas1=numpy.sqrt(chiSq1*numpy.diag(numpy.linalg.inv(numpy.dot(jack1.T,jack1))))
629 628 except:
630 629 std1=32.; sigmas1=numpy.ones(5)
631 630 else:
632 631 std1=sigmas1[0]
633 632
634 633
635 634 if fatspectra<1.0 and powerwidth<4:
636 635 choice=0
637 636 Amplitude0=lsq1[0][2]
638 637 shift0=lsq1[0][0]
639 638 width0=lsq1[0][1]
640 639 p0=lsq1[0][3]
641 640 Amplitude1=0.
642 641 shift1=0.
643 642 width1=0.
644 643 p1=0.
645 644 noise=lsq1[0][4]
646 645 #return (numpy.array([shift0,width0,Amplitude0,p0]),
647 646 # numpy.array([shift1,width1,Amplitude1,p1]),noise,snrdB,chiSq1,6.,sigmas1,[None,]*9,choice)
648 647
649 648 # two gaussians
650 649 #shift0=numpy.mod(firstpeak+minx,64); shift1=numpy.mod(secondpeak+minx,64)
651 650 shift0=numpy.mod(firstpeak+minx, self.Num_Bin );
652 651 shift1=numpy.mod(secondpeak+minx, self.Num_Bin )
653 652 width0=powerwidth/6.;
654 653 width1=width0
655 654 power0=2.;
656 655 power1=power0
657 656 amplitude0=firstamp;
658 657 amplitude1=secondamp
659 658 state0=[shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,wnoise]
660 659 #bnds=((0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
661 660 bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth/2.),(0,None),(0.5,3.),( 0,(self.Num_Bin-1)),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
662 661 #bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth/2.),(0,None),(0.5,3.),( 0,(self.Num_Bin-1)),(1,powerwidth/2.),(0,None),(0.5,3.),(0.1,0.5))
663 662
664 663 lsq2=fmin_l_bfgs_b(self.misfit2,state0,args=(y_data,x,num_intg),bounds=bnds,approx_grad=True)
665 664
666 665
667 666 chiSq2=lsq2[1]; jack2=self.y_jacobian2(x,lsq2[0])
668 667
669 668
670 669 try:
671 670 sigmas2=numpy.sqrt(chiSq2*numpy.diag(numpy.linalg.inv(numpy.dot(jack2.T,jack2))))
672 671 except:
673 672 std2a=32.; std2b=32.; sigmas2=numpy.ones(9)
674 673 else:
675 674 std2a=sigmas2[0]; std2b=sigmas2[4]
676 675
677 676
678 677
679 678 oneG=(chiSq1<5 and chiSq1/chiSq2<2.0) and (abs(lsq2[0][0]-lsq2[0][4])<(lsq2[0][1]+lsq2[0][5])/3. or abs(lsq2[0][0]-lsq2[0][4])<10)
680 679
681 680 if snrdB>-6: # when SNR is strong pick the peak with least shift (LOS velocity) error
682 681 if oneG:
683 682 choice=0
684 683 else:
685 684 w1=lsq2[0][1]; w2=lsq2[0][5]
686 685 a1=lsq2[0][2]; a2=lsq2[0][6]
687 686 p1=lsq2[0][3]; p2=lsq2[0][7]
688 687 s1=(2**(1+1./p1))*scipy.special.gamma(1./p1)/p1;
689 688 s2=(2**(1+1./p2))*scipy.special.gamma(1./p2)/p2;
690 689 gp1=a1*w1*s1; gp2=a2*w2*s2 # power content of each ggaussian with proper p scaling
691 690
692 691 if gp1>gp2:
693 692 if a1>0.7*a2:
694 693 choice=1
695 694 else:
696 695 choice=2
697 696 elif gp2>gp1:
698 697 if a2>0.7*a1:
699 698 choice=2
700 699 else:
701 700 choice=1
702 701 else:
703 702 choice=numpy.argmax([a1,a2])+1
704 703 #else:
705 704 #choice=argmin([std2a,std2b])+1
706 705
707 706 else: # with low SNR go to the most energetic peak
708 707 choice=numpy.argmax([lsq1[0][2]*lsq1[0][1],lsq2[0][2]*lsq2[0][1],lsq2[0][6]*lsq2[0][5]])
709 708
710 709
711 710 shift0=lsq2[0][0]; vel0=Vrange[0] + shift0*(Vrange[1]-Vrange[0])
712 711 shift1=lsq2[0][4]; vel1=Vrange[0] + shift1*(Vrange[1]-Vrange[0])
713 712
714 713 max_vel = 20
715 714
716 715 #first peak will be 0, second peak will be 1
717 716 if vel0 > 0 and vel0 < max_vel : #first peak is in the correct range
718 717 shift0=lsq2[0][0]
719 718 width0=lsq2[0][1]
720 719 Amplitude0=lsq2[0][2]
721 720 p0=lsq2[0][3]
722 721
723 722 shift1=lsq2[0][4]
724 723 width1=lsq2[0][5]
725 724 Amplitude1=lsq2[0][6]
726 725 p1=lsq2[0][7]
727 726 noise=lsq2[0][8]
728 727 else:
729 728 shift1=lsq2[0][0]
730 729 width1=lsq2[0][1]
731 730 Amplitude1=lsq2[0][2]
732 731 p1=lsq2[0][3]
733 732
734 733 shift0=lsq2[0][4]
735 734 width0=lsq2[0][5]
736 735 Amplitude0=lsq2[0][6]
737 736 p0=lsq2[0][7]
738 737 noise=lsq2[0][8]
739 738
740 739 if Amplitude0<0.1: # in case the peak is noise
741 740 shift0,width0,Amplitude0,p0 = 4*[numpy.NaN]
742 741 if Amplitude1<0.1:
743 742 shift1,width1,Amplitude1,p1 = 4*[numpy.NaN]
744 743
745 744
746 745 # if choice==0: # pick the single gaussian fit
747 746 # Amplitude0=lsq1[0][2]
748 747 # shift0=lsq1[0][0]
749 748 # width0=lsq1[0][1]
750 749 # p0=lsq1[0][3]
751 750 # Amplitude1=0.
752 751 # shift1=0.
753 752 # width1=0.
754 753 # p1=0.
755 754 # noise=lsq1[0][4]
756 755 # elif choice==1: # take the first one of the 2 gaussians fitted
757 756 # Amplitude0 = lsq2[0][2]
758 757 # shift0 = lsq2[0][0]
759 758 # width0 = lsq2[0][1]
760 759 # p0 = lsq2[0][3]
761 760 # Amplitude1 = lsq2[0][6] # This is 0 in gg1
762 761 # shift1 = lsq2[0][4] # This is 0 in gg1
763 762 # width1 = lsq2[0][5] # This is 0 in gg1
764 763 # p1 = lsq2[0][7] # This is 0 in gg1
765 764 # noise = lsq2[0][8]
766 765 # else: # the second one
767 766 # Amplitude0 = lsq2[0][6]
768 767 # shift0 = lsq2[0][4]
769 768 # width0 = lsq2[0][5]
770 769 # p0 = lsq2[0][7]
771 770 # Amplitude1 = lsq2[0][2] # This is 0 in gg1
772 771 # shift1 = lsq2[0][0] # This is 0 in gg1
773 772 # width1 = lsq2[0][1] # This is 0 in gg1
774 773 # p1 = lsq2[0][3] # This is 0 in gg1
775 774 # noise = lsq2[0][8]
776 775
777 776 #print len(noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0)
778 777 SPC_ch1[:,ht] = noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0
779 778 SPC_ch2[:,ht] = noise + Amplitude1*numpy.exp(-0.5*(abs(x-shift1))/width1)**p1
780 779 #print 'SPC_ch1.shape',SPC_ch1.shape
781 780 #print 'SPC_ch2.shape',SPC_ch2.shape
782 781 #dataOut.data_param = SPC_ch1
783 782 GauSPC = (SPC_ch1,SPC_ch2)
784 783 #GauSPC[1] = SPC_ch2
785 784
786 785 # print 'shift0', shift0
787 786 # print 'Amplitude0', Amplitude0
788 787 # print 'width0', width0
789 788 # print 'p0', p0
790 789 # print '========================'
791 790 # print 'shift1', shift1
792 791 # print 'Amplitude1', Amplitude1
793 792 # print 'width1', width1
794 793 # print 'p1', p1
795 794 # print 'noise', noise
796 795 # print 's_noise', wnoise
797 796
798 797 return GauSPC
799 798
800 799
801 800 def y_jacobian1(self,x,state): # This function is for further analysis of generalized Gaussians, it is not too importan for the signal discrimination.
802 801 y_model=self.y_model1(x,state)
803 802 s0,w0,a0,p0,n=state
804 803 e0=((x-s0)/w0)**2;
805 804
806 805 e0u=((x-s0-self.Num_Bin)/w0)**2;
807 806
808 807 e0d=((x-s0+self.Num_Bin)/w0)**2
809 808 m0=numpy.exp(-0.5*e0**(p0/2.));
810 809 m0u=numpy.exp(-0.5*e0u**(p0/2.));
811 810 m0d=numpy.exp(-0.5*e0d**(p0/2.))
812 811 JA=m0+m0u+m0d
813 812 JP=(-1/4.)*a0*m0*e0**(p0/2.)*numpy.log(e0)+(-1/4.)*a0*m0u*e0u**(p0/2.)*numpy.log(e0u)+(-1/4.)*a0*m0d*e0d**(p0/2.)*numpy.log(e0d)
814 813
815 814 JS=(p0/w0/2.)*a0*m0*e0**(p0/2.-1)*((x-s0)/w0)+(p0/w0/2.)*a0*m0u*e0u**(p0/2.-1)*((x-s0- self.Num_Bin )/w0)+(p0/w0/2.)*a0*m0d*e0d**(p0/2.-1)*((x-s0+ self.Num_Bin )/w0)
816 815
817 816 JW=(p0/w0/2.)*a0*m0*e0**(p0/2.-1)*((x-s0)/w0)**2+(p0/w0/2.)*a0*m0u*e0u**(p0/2.-1)*((x-s0- self.Num_Bin )/w0)**2+(p0/w0/2.)*a0*m0d*e0d**(p0/2.-1)*((x-s0+ self.Num_Bin )/w0)**2
818 817 jack1=numpy.sqrt(7)*numpy.array([JS/y_model,JW/y_model,JA/y_model,JP/y_model,1./y_model])
819 818 return jack1.T
820 819
821 820 def y_jacobian2(self,x,state):
822 821 y_model=self.y_model2(x,state)
823 822 s0,w0,a0,p0,s1,w1,a1,p1,n=state
824 823 e0=((x-s0)/w0)**2;
825 824
826 825 e0u=((x-s0- self.Num_Bin )/w0)**2;
827 826
828 827 e0d=((x-s0+ self.Num_Bin )/w0)**2
829 828 e1=((x-s1)/w1)**2;
830 829
831 830 e1u=((x-s1- self.Num_Bin )/w1)**2;
832 831
833 832 e1d=((x-s1+ self.Num_Bin )/w1)**2
834 833 m0=numpy.exp(-0.5*e0**(p0/2.));
835 834 m0u=numpy.exp(-0.5*e0u**(p0/2.));
836 835 m0d=numpy.exp(-0.5*e0d**(p0/2.))
837 836 m1=numpy.exp(-0.5*e1**(p1/2.));
838 837 m1u=numpy.exp(-0.5*e1u**(p1/2.));
839 838 m1d=numpy.exp(-0.5*e1d**(p1/2.))
840 839 JA=m0+m0u+m0d
841 840 JA1=m1+m1u+m1d
842 841 JP=(-1/4.)*a0*m0*e0**(p0/2.)*numpy.log(e0)+(-1/4.)*a0*m0u*e0u**(p0/2.)*numpy.log(e0u)+(-1/4.)*a0*m0d*e0d**(p0/2.)*numpy.log(e0d)
843 842 JP1=(-1/4.)*a1*m1*e1**(p1/2.)*numpy.log(e1)+(-1/4.)*a1*m1u*e1u**(p1/2.)*numpy.log(e1u)+(-1/4.)*a1*m1d*e1d**(p1/2.)*numpy.log(e1d)
844 843
845 844 JS=(p0/w0/2.)*a0*m0*e0**(p0/2.-1)*((x-s0)/w0)+(p0/w0/2.)*a0*m0u*e0u**(p0/2.-1)*((x-s0- self.Num_Bin )/w0)+(p0/w0/2.)*a0*m0d*e0d**(p0/2.-1)*((x-s0+ self.Num_Bin )/w0)
846 845
847 846 JS1=(p1/w1/2.)*a1*m1*e1**(p1/2.-1)*((x-s1)/w1)+(p1/w1/2.)*a1*m1u*e1u**(p1/2.-1)*((x-s1- self.Num_Bin )/w1)+(p1/w1/2.)*a1*m1d*e1d**(p1/2.-1)*((x-s1+ self.Num_Bin )/w1)
848 847
849 848 JW=(p0/w0/2.)*a0*m0*e0**(p0/2.-1)*((x-s0)/w0)**2+(p0/w0/2.)*a0*m0u*e0u**(p0/2.-1)*((x-s0- self.Num_Bin )/w0)**2+(p0/w0/2.)*a0*m0d*e0d**(p0/2.-1)*((x-s0+ self.Num_Bin )/w0)**2
850 849
851 850 JW1=(p1/w1/2.)*a1*m1*e1**(p1/2.-1)*((x-s1)/w1)**2+(p1/w1/2.)*a1*m1u*e1u**(p1/2.-1)*((x-s1- self.Num_Bin )/w1)**2+(p1/w1/2.)*a1*m1d*e1d**(p1/2.-1)*((x-s1+ self.Num_Bin )/w1)**2
852 851 jack2=numpy.sqrt(7)*numpy.array([JS/y_model,JW/y_model,JA/y_model,JP/y_model,JS1/y_model,JW1/y_model,JA1/y_model,JP1/y_model,1./y_model])
853 852 return jack2.T
854 853
855 854 def y_model1(self,x,state):
856 855 shift0,width0,amplitude0,power0,noise=state
857 856 model0=amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
858 857
859 858 model0u=amplitude0*numpy.exp(-0.5*abs((x-shift0- self.Num_Bin )/width0)**power0)
860 859
861 860 model0d=amplitude0*numpy.exp(-0.5*abs((x-shift0+ self.Num_Bin )/width0)**power0)
862 861 return model0+model0u+model0d+noise
863 862
864 863 def y_model2(self,x,state): #Equation for two generalized Gaussians with Nyquist
865 864 shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,noise=state
866 865 model0=amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
867 866
868 867 model0u=amplitude0*numpy.exp(-0.5*abs((x-shift0- self.Num_Bin )/width0)**power0)
869 868
870 869 model0d=amplitude0*numpy.exp(-0.5*abs((x-shift0+ self.Num_Bin )/width0)**power0)
871 870 model1=amplitude1*numpy.exp(-0.5*abs((x-shift1)/width1)**power1)
872 871
873 872 model1u=amplitude1*numpy.exp(-0.5*abs((x-shift1- self.Num_Bin )/width1)**power1)
874 873
875 874 model1d=amplitude1*numpy.exp(-0.5*abs((x-shift1+ self.Num_Bin )/width1)**power1)
876 875 return model0+model0u+model0d+model1+model1u+model1d+noise
877 876
878 877 def misfit1(self,state,y_data,x,num_intg): # This function compares how close real data is with the model data, the close it is, the better it is.
879 878
880 879 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model1(x,state)))**2)#/(64-5.) # /(64-5.) can be commented
881 880
882 881 def misfit2(self,state,y_data,x,num_intg):
883 882 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model2(x,state)))**2)#/(64-9.)
884 883
885 884
886 885 class PrecipitationProc(Operation):
887 886
888 887 '''
889 888 Operator that estimates Reflectivity factor (Z), and estimates rainfall Rate (R)
890 889
891 890 Input:
892 891 self.dataOut.data_pre : SelfSpectra
893 892
894 893 Output:
895 894
896 895 self.dataOut.data_output : Reflectivity factor, rainfall Rate
897 896
898 897
899 898 Parameters affected:
900 899 '''
901 900
902 901
903 902 def run(self, dataOut, radar=None, Pt=None, Gt=None, Gr=None, Lambda=None, aL=None,
904 903 tauW=None, ThetaT=None, ThetaR=None, Km = 0.93, Altitude=None):
905 904
906 905 self.spc = dataOut.data_pre[0].copy()
907 906 self.Num_Hei = self.spc.shape[2]
908 907 self.Num_Bin = self.spc.shape[1]
909 908 self.Num_Chn = self.spc.shape[0]
910 909
911 910 Velrange = dataOut.abscissaList
912 911
913 912 if radar == "MIRA35C" :
914 913
915 914 Ze = self.dBZeMODE2(dataOut)
916 915
917 916 else:
918 917
919 918 self.Pt = Pt
920 919 self.Gt = Gt
921 920 self.Gr = Gr
922 921 self.Lambda = Lambda
923 922 self.aL = aL
924 923 self.tauW = tauW
925 924 self.ThetaT = ThetaT
926 925 self.ThetaR = ThetaR
927 926
928 927 RadarConstant = GetRadarConstant()
929 928 SPCmean = numpy.mean(self.spc,0)
930 929 ETA = numpy.zeros(self.Num_Hei)
931 930 Pr = numpy.sum(SPCmean,0)
932 931
933 932 #for R in range(self.Num_Hei):
934 933 # ETA[R] = RadarConstant * Pr[R] * R**2 #Reflectivity (ETA)
935 934
936 935 D_range = numpy.zeros(self.Num_Hei)
937 936 EqSec = numpy.zeros(self.Num_Hei)
938 937 del_V = numpy.zeros(self.Num_Hei)
939 938
940 939 for R in range(self.Num_Hei):
941 940 ETA[R] = RadarConstant * Pr[R] * R**2 #Reflectivity (ETA)
942 941
943 942 h = R + Altitude #Range from ground to radar pulse altitude
944 943 del_V[R] = 1 + 3.68 * 10**-5 * h + 1.71 * 10**-9 * h**2 #Density change correction for velocity
945 944
946 945 D_range[R] = numpy.log( (9.65 - (Velrange[R]/del_V[R])) / 10.3 ) / -0.6 #Range of Diameter of drops related to velocity
947 946 SIGMA[R] = numpy.pi**5 / Lambda**4 * Km * D_range[R]**6 #Equivalent Section of drops (sigma)
948 947
949 948 N_dist[R] = ETA[R] / SIGMA[R]
950 949
951 950 Ze = (ETA * Lambda**4) / (numpy.pi * Km)
952 951 Z = numpy.sum( N_dist * D_range**6 )
953 952 RR = 6*10**-4*numpy.pi * numpy.sum( D_range**3 * N_dist * Velrange ) #Rainfall rate
954 953
955 954
956 955 RR = (Ze/200)**(1/1.6)
957 956 dBRR = 10*numpy.log10(RR)
958 957
959 958 dBZe = 10*numpy.log10(Ze)
960 959 dataOut.data_output = Ze
961 960 dataOut.data_param = numpy.ones([2,self.Num_Hei])
962 961 dataOut.channelList = [0,1]
963 962 print 'channelList', dataOut.channelList
964 963 dataOut.data_param[0]=dBZe
965 964 dataOut.data_param[1]=dBRR
966 965 print 'RR SHAPE', dBRR.shape
967 966 print 'Ze SHAPE', dBZe.shape
968 967 print 'dataOut.data_param SHAPE', dataOut.data_param.shape
969 968
970 969
971 970 def dBZeMODE2(self, dataOut): # Processing for MIRA35C
972 971
973 972 NPW = dataOut.NPW
974 973 COFA = dataOut.COFA
975 974
976 975 SNR = numpy.array([self.spc[0,:,:] / NPW[0]]) #, self.spc[1,:,:] / NPW[1]])
977 976 RadarConst = dataOut.RadarConst
978 977 #frequency = 34.85*10**9
979 978
980 979 ETA = numpy.zeros(([self.Num_Chn ,self.Num_Hei]))
981 980 data_output = numpy.ones([self.Num_Chn , self.Num_Hei])*numpy.NaN
982 981
983 982 ETA = numpy.sum(SNR,1)
984 983 print 'ETA' , ETA
985 984 ETA = numpy.where(ETA is not 0. , ETA, numpy.NaN)
986 985
987 986 Ze = numpy.ones([self.Num_Chn, self.Num_Hei] )
988 987
989 988 for r in range(self.Num_Hei):
990 989
991 990 Ze[0,r] = ( ETA[0,r] ) * COFA[0,r][0] * RadarConst * ((r/5000.)**2)
992 991 #Ze[1,r] = ( ETA[1,r] ) * COFA[1,r][0] * RadarConst * ((r/5000.)**2)
993 992
994 993 return Ze
995 994
996 995 def GetRadarConstant(self):
997 996
998 997 """
999 998 Constants:
1000 999
1001 1000 Pt: Transmission Power dB
1002 1001 Gt: Transmission Gain dB
1003 1002 Gr: Reception Gain dB
1004 1003 Lambda: Wavelenght m
1005 1004 aL: Attenuation loses dB
1006 1005 tauW: Width of transmission pulse s
1007 1006 ThetaT: Transmission antenna bean angle rad
1008 1007 ThetaR: Reception antenna beam angle rad
1009 1008
1010 1009 """
1011 1010 Numerator = ( (4*numpy.pi)**3 * aL**2 * 16 * numpy.log(2) )
1012 1011 Denominator = ( Pt * Gt * Gr * Lambda**2 * SPEED_OF_LIGHT * TauW * numpy.pi * ThetaT * TheraR)
1013 1012 RadarConstant = Numerator / Denominator
1014 1013
1015 1014 return RadarConstant
1016 1015
1017 1016
1018 1017
1019 1018 class FullSpectralAnalysis(Operation):
1020 1019
1021 1020 """
1022 1021 Function that implements Full Spectral Analisys technique.
1023 1022
1024 1023 Input:
1025 1024 self.dataOut.data_pre : SelfSpectra and CrossSPectra data
1026 1025 self.dataOut.groupList : Pairlist of channels
1027 1026 self.dataOut.ChanDist : Physical distance between receivers
1028 1027
1029 1028
1030 1029 Output:
1031 1030
1032 1031 self.dataOut.data_output : Zonal wind, Meridional wind and Vertical wind
1033 1032
1034 1033
1035 1034 Parameters affected: Winds, height range, SNR
1036 1035
1037 1036 """
1038 1037 def run(self, dataOut, E01=None, E02=None, E12=None, N01=None, N02=None, N12=None, SNRlimit=7):
1039 1038
1040 1039 spc = dataOut.data_pre[0].copy()
1041 1040 cspc = dataOut.data_pre[1].copy()
1042 1041
1043 1042 nChannel = spc.shape[0]
1044 1043 nProfiles = spc.shape[1]
1045 1044 nHeights = spc.shape[2]
1046 1045
1047 1046 pairsList = dataOut.groupList
1048 1047 if dataOut.ChanDist is not None :
1049 1048 ChanDist = dataOut.ChanDist
1050 1049 else:
1051 1050 ChanDist = numpy.array([[E01, N01],[E02,N02],[E12,N12]])
1052 1051
1053 1052 #print 'ChanDist', ChanDist
1054 1053
1055 1054 if dataOut.VelRange is not None:
1056 1055 VelRange= dataOut.VelRange
1057 1056 else:
1058 1057 VelRange= dataOut.abscissaList
1059 1058
1060 1059 ySamples=numpy.ones([nChannel,nProfiles])
1061 1060 phase=numpy.ones([nChannel,nProfiles])
1062 1061 CSPCSamples=numpy.ones([nChannel,nProfiles],dtype=numpy.complex_)
1063 1062 coherence=numpy.ones([nChannel,nProfiles])
1064 1063 PhaseSlope=numpy.ones(nChannel)
1065 1064 PhaseInter=numpy.ones(nChannel)
1066 1065 dataSNR = dataOut.data_SNR
1067 1066
1068 1067
1069 1068
1070 1069 data = dataOut.data_pre
1071 1070 noise = dataOut.noise
1072 1071 print 'noise',noise
1073 1072 #SNRdB = 10*numpy.log10(dataOut.data_SNR)
1074 1073
1075 1074 FirstMoment = numpy.average(dataOut.data_param[:,1,:],0)
1076 1075 #SNRdBMean = []
1077 1076
1078 1077
1079 1078 #for j in range(nHeights):
1080 1079 # FirstMoment = numpy.append(FirstMoment,numpy.mean([dataOut.data_param[0,1,j],dataOut.data_param[1,1,j],dataOut.data_param[2,1,j]]))
1081 1080 # SNRdBMean = numpy.append(SNRdBMean,numpy.mean([SNRdB[0,j],SNRdB[1,j],SNRdB[2,j]]))
1082 1081
1083 1082 data_output=numpy.ones([3,spc.shape[2]])*numpy.NaN
1084 1083
1085 1084 velocityX=[]
1086 1085 velocityY=[]
1087 1086 velocityV=[]
1088 1087
1089 1088 dbSNR = 10*numpy.log10(dataSNR)
1090 1089 dbSNR = numpy.average(dbSNR,0)
1091 1090 for Height in range(nHeights):
1092 1091
1093 1092 [Vzon,Vmer,Vver, GaussCenter]= self.WindEstimation(spc, cspc, pairsList, ChanDist, Height, noise, VelRange, dbSNR[Height], SNRlimit)
1094 1093
1095 1094 if abs(Vzon)<100. and abs(Vzon)> 0.:
1096 1095 velocityX=numpy.append(velocityX, Vzon)#Vmag
1097 1096
1098 1097 else:
1099 1098 print 'Vzon',Vzon
1100 1099 velocityX=numpy.append(velocityX, numpy.NaN)
1101 1100
1102 1101 if abs(Vmer)<100. and abs(Vmer) > 0.:
1103 1102 velocityY=numpy.append(velocityY, Vmer)#Vang
1104 1103
1105 1104 else:
1106 1105 print 'Vmer',Vmer
1107 1106 velocityY=numpy.append(velocityY, numpy.NaN)
1108 1107
1109 1108 if dbSNR[Height] > SNRlimit:
1110 1109 velocityV=numpy.append(velocityV, FirstMoment[Height])
1111 1110 else:
1112 1111 velocityV=numpy.append(velocityV, numpy.NaN)
1113 1112 #FirstMoment[Height]= numpy.NaN
1114 1113 # if SNRdBMean[Height] <12:
1115 1114 # FirstMoment[Height] = numpy.NaN
1116 1115 # velocityX[Height] = numpy.NaN
1117 1116 # velocityY[Height] = numpy.NaN
1118 1117
1119 1118
1120 1119 data_output[0]=numpy.array(velocityX)
1121 1120 data_output[1]=numpy.array(velocityY)
1122 1121 data_output[2]=-velocityV#FirstMoment
1123 1122
1124 1123 print ' '
1125 1124 #print 'FirstMoment'
1126 1125 #print FirstMoment
1127 1126 print 'velocityX',data_output[0]
1128 1127 print ' '
1129 1128 print 'velocityY',data_output[1]
1130 1129 #print numpy.array(velocityY)
1131 1130 print ' '
1132 1131 #print 'SNR'
1133 1132 #print 10*numpy.log10(dataOut.data_SNR)
1134 1133 #print numpy.shape(10*numpy.log10(dataOut.data_SNR))
1135 1134 print ' '
1136 1135
1137 1136
1138 1137 dataOut.data_output=data_output
1139 1138 return
1140 1139
1141 1140
1142 1141 def moving_average(self,x, N=2):
1143 1142 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
1144 1143
1145 1144 def gaus(self,xSamples,a,x0,sigma):
1146 1145 return a*numpy.exp(-(xSamples-x0)**2/(2*sigma**2))
1147 1146
1148 1147 def Find(self,x,value):
1149 1148 for index in range(len(x)):
1150 1149 if x[index]==value:
1151 1150 return index
1152 1151
1153 1152 def WindEstimation(self, spc, cspc, pairsList, ChanDist, Height, noise, VelRange, dbSNR, SNRlimit):
1154 1153
1155 1154 ySamples=numpy.ones([spc.shape[0],spc.shape[1]])
1156 1155 phase=numpy.ones([spc.shape[0],spc.shape[1]])
1157 1156 CSPCSamples=numpy.ones([spc.shape[0],spc.shape[1]],dtype=numpy.complex_)
1158 1157 coherence=numpy.ones([spc.shape[0],spc.shape[1]])
1159 1158 PhaseSlope=numpy.ones(spc.shape[0])
1160 1159 PhaseInter=numpy.ones(spc.shape[0])
1161 1160 xFrec=VelRange
1162 1161
1163 1162 '''Getting Eij and Nij'''
1164 1163
1165 1164 E01=ChanDist[0][0]
1166 1165 N01=ChanDist[0][1]
1167 1166
1168 1167 E02=ChanDist[1][0]
1169 1168 N02=ChanDist[1][1]
1170 1169
1171 1170 E12=ChanDist[2][0]
1172 1171 N12=ChanDist[2][1]
1173 1172
1174 1173 z = spc.copy()
1175 1174 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1176 1175
1177 1176 for i in range(spc.shape[0]):
1178 1177
1179 1178 '''****** Line of Data SPC ******'''
1180 1179 zline=z[i,:,Height]
1181 1180
1182 1181 '''****** SPC is normalized ******'''
1183 1182 FactNorm= (zline.copy()-noise[i]) / numpy.sum(zline.copy())
1184 1183 FactNorm= FactNorm/numpy.sum(FactNorm)
1185 1184
1186 1185 SmoothSPC=self.moving_average(FactNorm,N=3)
1187 1186
1188 1187 xSamples = ar(range(len(SmoothSPC)))
1189 1188 ySamples[i] = SmoothSPC
1190 1189
1191 1190 #dbSNR=10*numpy.log10(dataSNR)
1192 1191 print ' '
1193 1192 print ' '
1194 1193 print ' '
1195 1194
1196 1195 #print 'dataSNR', dbSNR.shape, dbSNR[0,40:120]
1197 1196 print 'SmoothSPC', SmoothSPC.shape, SmoothSPC[0:20]
1198 1197 print 'noise',noise
1199 1198 print 'zline',zline.shape, zline[0:20]
1200 1199 print 'FactNorm',FactNorm.shape, FactNorm[0:20]
1201 1200 print 'FactNorm suma', numpy.sum(FactNorm)
1202 1201
1203 1202 for i in range(spc.shape[0]):
1204 1203
1205 1204 '''****** Line of Data CSPC ******'''
1206 1205 cspcLine=cspc[i,:,Height].copy()
1207 1206
1208 1207 '''****** CSPC is normalized ******'''
1209 1208 chan_index0 = pairsList[i][0]
1210 1209 chan_index1 = pairsList[i][1]
1211 1210 CSPCFactor= abs(numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])) #
1212 1211
1213 1212 CSPCNorm = (cspcLine.copy() -noise[i]) / numpy.sqrt(CSPCFactor)
1214 1213
1215 1214 CSPCSamples[i] = CSPCNorm
1216 1215 coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
1217 1216
1218 1217 coherence[i]= self.moving_average(coherence[i],N=2)
1219 1218
1220 1219 phase[i] = self.moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
1221 1220
1222 1221 print 'cspcLine', cspcLine.shape, cspcLine[0:20]
1223 1222 print 'CSPCFactor', CSPCFactor#, CSPCFactor[0:20]
1224 1223 print numpy.sum(ySamples[chan_index0]), numpy.sum(ySamples[chan_index1]), -noise[i]
1225 1224 print 'CSPCNorm', CSPCNorm.shape, CSPCNorm[0:20]
1226 1225 print 'CSPCNorm suma', numpy.sum(CSPCNorm)
1227 1226 print 'CSPCSamples', CSPCSamples.shape, CSPCSamples[0,0:20]
1228 1227
1229 1228 '''****** Getting fij width ******'''
1230 1229
1231 1230 yMean=[]
1232 1231 yMean2=[]
1233 1232
1234 1233 for j in range(len(ySamples[1])):
1235 1234 yMean=numpy.append(yMean,numpy.mean([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
1236 1235
1237 1236 '''******* Getting fitting Gaussian ******'''
1238 1237 meanGauss=sum(xSamples*yMean) / len(xSamples)
1239 1238 sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
1240 1239
1241 1240 print '****************************'
1242 1241 print 'len(xSamples): ',len(xSamples)
1243 1242 print 'yMean: ', yMean.shape, yMean[0:20]
1244 1243 print 'ySamples', ySamples.shape, ySamples[0,0:20]
1245 1244 print 'xSamples: ',xSamples.shape, xSamples[0:20]
1246 1245
1247 1246 print 'meanGauss',meanGauss
1248 1247 print 'sigma',sigma
1249 1248
1250 1249 #if (abs(meanGauss/sigma**2) > 0.0001) : #0.000000001):
1251 1250 if dbSNR > SNRlimit :
1252 1251 try:
1253 1252 popt,pcov = curve_fit(self.gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
1254 1253
1255 1254 if numpy.amax(popt)>numpy.amax(yMean)*0.3:
1256 1255 FitGauss=self.gaus(xSamples,*popt)
1257 1256
1258 1257 else:
1259 1258 FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
1260 1259 print 'Verificador: Dentro', Height
1261 1260 except :#RuntimeError:
1262 1261 FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
1263 1262
1264 1263
1265 1264 else:
1266 1265 FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
1267 1266
1268 1267 Maximun=numpy.amax(yMean)
1269 1268 eMinus1=Maximun*numpy.exp(-1)#*0.8
1270 1269
1271 1270 HWpos=self.Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
1272 1271 HalfWidth= xFrec[HWpos]
1273 1272 GCpos=self.Find(FitGauss, numpy.amax(FitGauss))
1274 1273 Vpos=self.Find(FactNorm, numpy.amax(FactNorm))
1275 1274
1276 1275 #Vpos=FirstMoment[]
1277 1276
1278 1277 '''****** Getting Fij ******'''
1279 1278
1280 1279 GaussCenter=xFrec[GCpos]
1281 1280 if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
1282 1281 Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
1283 1282 else:
1284 1283 Fij=abs(GaussCenter-HalfWidth)+0.0000001
1285 1284
1286 1285 '''****** Getting Frecuency range of significant data ******'''
1287 1286
1288 1287 Rangpos=self.Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
1289 1288
1290 1289 if Rangpos<GCpos:
1291 1290 Range=numpy.array([Rangpos,2*GCpos-Rangpos])
1292 1291 elif Rangpos< ( len(xFrec)- len(xFrec)*0.1):
1293 1292 Range=numpy.array([2*GCpos-Rangpos,Rangpos])
1294 1293 else:
1295 1294 Range = numpy.array([0,0])
1296 1295
1297 1296 print ' '
1298 1297 print 'GCpos',GCpos, ( len(xFrec)- len(xFrec)*0.1)
1299 1298 print 'Rangpos',Rangpos
1300 1299 print 'RANGE: ', Range
1301 1300 FrecRange=xFrec[Range[0]:Range[1]]
1302 1301
1303 1302 '''****** Getting SCPC Slope ******'''
1304 1303
1305 1304 for i in range(spc.shape[0]):
1306 1305
1307 1306 if len(FrecRange)>5 and len(FrecRange)<spc.shape[1]*0.5:
1308 1307 PhaseRange=self.moving_average(phase[i,Range[0]:Range[1]],N=3)
1309 1308
1310 1309 print 'FrecRange', len(FrecRange) , FrecRange
1311 1310 print 'PhaseRange', len(PhaseRange), PhaseRange
1312 1311 print ' '
1313 1312 if len(FrecRange) == len(PhaseRange):
1314 1313 slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
1315 1314 PhaseSlope[i]=slope
1316 1315 PhaseInter[i]=intercept
1317 1316 else:
1318 1317 PhaseSlope[i]=0
1319 1318 PhaseInter[i]=0
1320 1319 else:
1321 1320 PhaseSlope[i]=0
1322 1321 PhaseInter[i]=0
1323 1322
1324 1323 '''Getting constant C'''
1325 1324 cC=(Fij*numpy.pi)**2
1326 1325
1327 1326 '''****** Getting constants F and G ******'''
1328 1327 MijEijNij=numpy.array([[E02,N02], [E12,N12]])
1329 1328 MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
1330 1329 MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
1331 1330 MijResults=numpy.array([MijResult0,MijResult1])
1332 1331 (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
1333 1332
1334 1333 '''****** Getting constants A, B and H ******'''
1335 1334 W01=numpy.amax(coherence[0])
1336 1335 W02=numpy.amax(coherence[1])
1337 1336 W12=numpy.amax(coherence[2])
1338 1337
1339 1338 WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
1340 1339 WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
1341 1340 WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
1342 1341
1343 1342 WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
1344 1343
1345 1344 WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
1346 1345 (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
1347 1346
1348 1347 VxVy=numpy.array([[cA,cH],[cH,cB]])
1349 1348
1350 1349 VxVyResults=numpy.array([-cF,-cG])
1351 1350 (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
1352 1351
1353 1352 Vzon = Vy
1354 1353 Vmer = Vx
1355 1354 Vmag=numpy.sqrt(Vzon**2+Vmer**2)
1356 1355 Vang=numpy.arctan2(Vmer,Vzon)
1357 1356 Vver=xFrec[Vpos]
1358 1357 print 'vzon y vmer', Vzon, Vmer
1359 1358 return Vzon, Vmer, Vver, GaussCenter
1360 1359
1361 1360 class SpectralMoments(Operation):
1362 1361
1363 1362 '''
1364 1363 Function SpectralMoments()
1365 1364
1366 1365 Calculates moments (power, mean, standard deviation) and SNR of the signal
1367 1366
1368 1367 Type of dataIn: Spectra
1369 1368
1370 1369 Configuration Parameters:
1371 1370
1372 1371 dirCosx : Cosine director in X axis
1373 1372 dirCosy : Cosine director in Y axis
1374 1373
1375 1374 elevation :
1376 1375 azimuth :
1377 1376
1378 1377 Input:
1379 1378 channelList : simple channel list to select e.g. [2,3,7]
1380 1379 self.dataOut.data_pre : Spectral data
1381 1380 self.dataOut.abscissaList : List of frequencies
1382 1381 self.dataOut.noise : Noise level per channel
1383 1382
1384 1383 Affected:
1385 1384 self.dataOut.data_param : Parameters per channel
1386 1385 self.dataOut.data_SNR : SNR per channel
1387 1386
1388 1387 '''
1389 1388
1390 1389 def run(self, dataOut):
1391 1390
1392 1391 #dataOut.data_pre = dataOut.data_pre[0]
1393 1392 data = dataOut.data_pre[0]
1394 1393 absc = dataOut.abscissaList[:-1]
1395 1394 noise = dataOut.noise
1396 1395 nChannel = data.shape[0]
1397 1396 data_param = numpy.zeros((nChannel, 4, data.shape[2]))
1398 1397
1399 1398 for ind in range(nChannel):
1400 1399 data_param[ind,:,:] = self.__calculateMoments( data[ind,:,:] , absc , noise[ind] )
1401 1400
1402 1401 dataOut.data_param = data_param[:,1:,:]
1403 1402 dataOut.data_SNR = data_param[:,0]
1404 1403 dataOut.data_DOP = data_param[:,1]
1405 1404 dataOut.data_MEAN = data_param[:,2]
1406 1405 dataOut.data_STD = data_param[:,3]
1407 1406 return
1408 1407
1409 1408 def __calculateMoments(self, oldspec, oldfreq, n0,
1410 1409 nicoh = None, graph = None, smooth = None, type1 = None, fwindow = None, snrth = None, dc = None, aliasing = None, oldfd = None, wwauto = None):
1411 1410
1412 1411 if (nicoh == None): nicoh = 1
1413 1412 if (graph == None): graph = 0
1414 1413 if (smooth == None): smooth = 0
1415 1414 elif (self.smooth < 3): smooth = 0
1416 1415
1417 1416 if (type1 == None): type1 = 0
1418 1417 if (fwindow == None): fwindow = numpy.zeros(oldfreq.size) + 1
1419 1418 if (snrth == None): snrth = -3
1420 1419 if (dc == None): dc = 0
1421 1420 if (aliasing == None): aliasing = 0
1422 1421 if (oldfd == None): oldfd = 0
1423 1422 if (wwauto == None): wwauto = 0
1424 1423
1425 1424 if (n0 < 1.e-20): n0 = 1.e-20
1426 1425
1427 1426 freq = oldfreq
1428 1427 vec_power = numpy.zeros(oldspec.shape[1])
1429 1428 vec_fd = numpy.zeros(oldspec.shape[1])
1430 1429 vec_w = numpy.zeros(oldspec.shape[1])
1431 1430 vec_snr = numpy.zeros(oldspec.shape[1])
1432 1431
1433 1432 for ind in range(oldspec.shape[1]):
1434 1433
1435 1434 spec = oldspec[:,ind]
1436 1435 aux = spec*fwindow
1437 1436 max_spec = aux.max()
1438 1437 m = list(aux).index(max_spec)
1439 1438
1440 1439 #Smooth
1441 1440 if (smooth == 0): spec2 = spec
1442 1441 else: spec2 = scipy.ndimage.filters.uniform_filter1d(spec,size=smooth)
1443 1442
1444 1443 # Calculo de Momentos
1445 1444 bb = spec2[range(m,spec2.size)]
1446 1445 bb = (bb<n0).nonzero()
1447 1446 bb = bb[0]
1448 1447
1449 1448 ss = spec2[range(0,m + 1)]
1450 1449 ss = (ss<n0).nonzero()
1451 1450 ss = ss[0]
1452 1451
1453 1452 if (bb.size == 0):
1454 1453 bb0 = spec.size - 1 - m
1455 1454 else:
1456 1455 bb0 = bb[0] - 1
1457 1456 if (bb0 < 0):
1458 1457 bb0 = 0
1459 1458
1460 1459 if (ss.size == 0): ss1 = 1
1461 1460 else: ss1 = max(ss) + 1
1462 1461
1463 1462 if (ss1 > m): ss1 = m
1464 1463
1465 1464 valid = numpy.asarray(range(int(m + bb0 - ss1 + 1))) + ss1
1466 1465 power = ((spec2[valid] - n0)*fwindow[valid]).sum()
1467 1466 fd = ((spec2[valid]- n0)*freq[valid]*fwindow[valid]).sum()/power
1468 1467 w = math.sqrt(((spec2[valid] - n0)*fwindow[valid]*(freq[valid]- fd)**2).sum()/power)
1469 1468 snr = (spec2.mean()-n0)/n0
1470 1469
1471 1470 if (snr < 1.e-20) :
1472 1471 snr = 1.e-20
1473 1472
1474 1473 vec_power[ind] = power
1475 1474 vec_fd[ind] = fd
1476 1475 vec_w[ind] = w
1477 1476 vec_snr[ind] = snr
1478 1477
1479 1478 moments = numpy.vstack((vec_snr, vec_power, vec_fd, vec_w))
1480 1479 return moments
1481 1480
1482 1481 #------------------ Get SA Parameters --------------------------
1483 1482
1484 1483 def GetSAParameters(self):
1485 1484 #SA en frecuencia
1486 1485 pairslist = self.dataOut.groupList
1487 1486 num_pairs = len(pairslist)
1488 1487
1489 1488 vel = self.dataOut.abscissaList
1490 1489 spectra = self.dataOut.data_pre
1491 1490 cspectra = self.dataIn.data_cspc
1492 1491 delta_v = vel[1] - vel[0]
1493 1492
1494 1493 #Calculating the power spectrum
1495 1494 spc_pow = numpy.sum(spectra, 3)*delta_v
1496 1495 #Normalizing Spectra
1497 1496 norm_spectra = spectra/spc_pow
1498 1497 #Calculating the norm_spectra at peak
1499 1498 max_spectra = numpy.max(norm_spectra, 3)
1500 1499
1501 1500 #Normalizing Cross Spectra
1502 1501 norm_cspectra = numpy.zeros(cspectra.shape)
1503 1502
1504 1503 for i in range(num_chan):
1505 1504 norm_cspectra[i,:,:] = cspectra[i,:,:]/numpy.sqrt(spc_pow[pairslist[i][0],:]*spc_pow[pairslist[i][1],:])
1506 1505
1507 1506 max_cspectra = numpy.max(norm_cspectra,2)
1508 1507 max_cspectra_index = numpy.argmax(norm_cspectra, 2)
1509 1508
1510 1509 for i in range(num_pairs):
1511 1510 cspc_par[i,:,:] = __calculateMoments(norm_cspectra)
1512 1511 #------------------- Get Lags ----------------------------------
1513 1512
1514 1513 class SALags(Operation):
1515 1514 '''
1516 1515 Function GetMoments()
1517 1516
1518 1517 Input:
1519 1518 self.dataOut.data_pre
1520 1519 self.dataOut.abscissaList
1521 1520 self.dataOut.noise
1522 1521 self.dataOut.normFactor
1523 1522 self.dataOut.data_SNR
1524 1523 self.dataOut.groupList
1525 1524 self.dataOut.nChannels
1526 1525
1527 1526 Affected:
1528 1527 self.dataOut.data_param
1529 1528
1530 1529 '''
1531 1530 def run(self, dataOut):
1532 1531 data_acf = dataOut.data_pre[0]
1533 1532 data_ccf = dataOut.data_pre[1]
1534 1533 normFactor_acf = dataOut.normFactor[0]
1535 1534 normFactor_ccf = dataOut.normFactor[1]
1536 1535 pairs_acf = dataOut.groupList[0]
1537 1536 pairs_ccf = dataOut.groupList[1]
1538 1537
1539 1538 nHeights = dataOut.nHeights
1540 1539 absc = dataOut.abscissaList
1541 1540 noise = dataOut.noise
1542 1541 SNR = dataOut.data_SNR
1543 1542 nChannels = dataOut.nChannels
1544 1543 # pairsList = dataOut.groupList
1545 1544 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairsList, nChannels)
1546 1545
1547 1546 for l in range(len(pairs_acf)):
1548 1547 data_acf[l,:,:] = data_acf[l,:,:]/normFactor_acf[l,:]
1549 1548
1550 1549 for l in range(len(pairs_ccf)):
1551 1550 data_ccf[l,:,:] = data_ccf[l,:,:]/normFactor_ccf[l,:]
1552 1551
1553 1552 dataOut.data_param = numpy.zeros((len(pairs_ccf)*2 + 1, nHeights))
1554 1553 dataOut.data_param[:-1,:] = self.__calculateTaus(data_acf, data_ccf, absc)
1555 1554 dataOut.data_param[-1,:] = self.__calculateLag1Phase(data_acf, absc)
1556 1555 return
1557 1556
1558 1557 # def __getPairsAutoCorr(self, pairsList, nChannels):
1559 1558 #
1560 1559 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1561 1560 #
1562 1561 # for l in range(len(pairsList)):
1563 1562 # firstChannel = pairsList[l][0]
1564 1563 # secondChannel = pairsList[l][1]
1565 1564 #
1566 1565 # #Obteniendo pares de Autocorrelacion
1567 1566 # if firstChannel == secondChannel:
1568 1567 # pairsAutoCorr[firstChannel] = int(l)
1569 1568 #
1570 1569 # pairsAutoCorr = pairsAutoCorr.astype(int)
1571 1570 #
1572 1571 # pairsCrossCorr = range(len(pairsList))
1573 1572 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1574 1573 #
1575 1574 # return pairsAutoCorr, pairsCrossCorr
1576 1575
1577 1576 def __calculateTaus(self, data_acf, data_ccf, lagRange):
1578 1577
1579 1578 lag0 = data_acf.shape[1]/2
1580 1579 #Funcion de Autocorrelacion
1581 1580 mean_acf = stats.nanmean(data_acf, axis = 0)
1582 1581
1583 1582 #Obtencion Indice de TauCross
1584 1583 ind_ccf = data_ccf.argmax(axis = 1)
1585 1584 #Obtencion Indice de TauAuto
1586 1585 ind_acf = numpy.zeros(ind_ccf.shape,dtype = 'int')
1587 1586 ccf_lag0 = data_ccf[:,lag0,:]
1588 1587
1589 1588 for i in range(ccf_lag0.shape[0]):
1590 1589 ind_acf[i,:] = numpy.abs(mean_acf - ccf_lag0[i,:]).argmin(axis = 0)
1591 1590
1592 1591 #Obtencion de TauCross y TauAuto
1593 1592 tau_ccf = lagRange[ind_ccf]
1594 1593 tau_acf = lagRange[ind_acf]
1595 1594
1596 1595 Nan1, Nan2 = numpy.where(tau_ccf == lagRange[0])
1597 1596
1598 1597 tau_ccf[Nan1,Nan2] = numpy.nan
1599 1598 tau_acf[Nan1,Nan2] = numpy.nan
1600 1599 tau = numpy.vstack((tau_ccf,tau_acf))
1601 1600
1602 1601 return tau
1603 1602
1604 1603 def __calculateLag1Phase(self, data, lagTRange):
1605 1604 data1 = stats.nanmean(data, axis = 0)
1606 1605 lag1 = numpy.where(lagTRange == 0)[0][0] + 1
1607 1606
1608 1607 phase = numpy.angle(data1[lag1,:])
1609 1608
1610 1609 return phase
1611 1610
1612 1611 class SpectralFitting(Operation):
1613 1612 '''
1614 1613 Function GetMoments()
1615 1614
1616 1615 Input:
1617 1616 Output:
1618 1617 Variables modified:
1619 1618 '''
1620 1619
1621 1620 def run(self, dataOut, getSNR = True, path=None, file=None, groupList=None):
1622 1621
1623 1622
1624 1623 if path != None:
1625 1624 sys.path.append(path)
1626 1625 self.dataOut.library = importlib.import_module(file)
1627 1626
1628 1627 #To be inserted as a parameter
1629 1628 groupArray = numpy.array(groupList)
1630 1629 # groupArray = numpy.array([[0,1],[2,3]])
1631 1630 self.dataOut.groupList = groupArray
1632 1631
1633 1632 nGroups = groupArray.shape[0]
1634 1633 nChannels = self.dataIn.nChannels
1635 1634 nHeights=self.dataIn.heightList.size
1636 1635
1637 1636 #Parameters Array
1638 1637 self.dataOut.data_param = None
1639 1638
1640 1639 #Set constants
1641 1640 constants = self.dataOut.library.setConstants(self.dataIn)
1642 1641 self.dataOut.constants = constants
1643 1642 M = self.dataIn.normFactor
1644 1643 N = self.dataIn.nFFTPoints
1645 1644 ippSeconds = self.dataIn.ippSeconds
1646 1645 K = self.dataIn.nIncohInt
1647 1646 pairsArray = numpy.array(self.dataIn.pairsList)
1648 1647
1649 1648 #List of possible combinations
1650 1649 listComb = itertools.combinations(numpy.arange(groupArray.shape[1]),2)
1651 1650 indCross = numpy.zeros(len(list(listComb)), dtype = 'int')
1652 1651
1653 1652 if getSNR:
1654 1653 listChannels = groupArray.reshape((groupArray.size))
1655 1654 listChannels.sort()
1656 1655 noise = self.dataIn.getNoise()
1657 1656 self.dataOut.data_SNR = self.__getSNR(self.dataIn.data_spc[listChannels,:,:], noise[listChannels])
1658 1657
1659 1658 for i in range(nGroups):
1660 1659 coord = groupArray[i,:]
1661 1660
1662 1661 #Input data array
1663 1662 data = self.dataIn.data_spc[coord,:,:]/(M*N)
1664 1663 data = data.reshape((data.shape[0]*data.shape[1],data.shape[2]))
1665 1664
1666 1665 #Cross Spectra data array for Covariance Matrixes
1667 1666 ind = 0
1668 1667 for pairs in listComb:
1669 1668 pairsSel = numpy.array([coord[x],coord[y]])
1670 1669 indCross[ind] = int(numpy.where(numpy.all(pairsArray == pairsSel, axis = 1))[0][0])
1671 1670 ind += 1
1672 1671 dataCross = self.dataIn.data_cspc[indCross,:,:]/(M*N)
1673 1672 dataCross = dataCross**2/K
1674 1673
1675 1674 for h in range(nHeights):
1676 1675 # print self.dataOut.heightList[h]
1677 1676
1678 1677 #Input
1679 1678 d = data[:,h]
1680 1679
1681 1680 #Covariance Matrix
1682 1681 D = numpy.diag(d**2/K)
1683 1682 ind = 0
1684 1683 for pairs in listComb:
1685 1684 #Coordinates in Covariance Matrix
1686 1685 x = pairs[0]
1687 1686 y = pairs[1]
1688 1687 #Channel Index
1689 1688 S12 = dataCross[ind,:,h]
1690 1689 D12 = numpy.diag(S12)
1691 1690 #Completing Covariance Matrix with Cross Spectras
1692 1691 D[x*N:(x+1)*N,y*N:(y+1)*N] = D12
1693 1692 D[y*N:(y+1)*N,x*N:(x+1)*N] = D12
1694 1693 ind += 1
1695 1694 Dinv=numpy.linalg.inv(D)
1696 1695 L=numpy.linalg.cholesky(Dinv)
1697 1696 LT=L.T
1698 1697
1699 1698 dp = numpy.dot(LT,d)
1700 1699
1701 1700 #Initial values
1702 1701 data_spc = self.dataIn.data_spc[coord,:,h]
1703 1702
1704 1703 if (h>0)and(error1[3]<5):
1705 1704 p0 = self.dataOut.data_param[i,:,h-1]
1706 1705 else:
1707 1706 p0 = numpy.array(self.dataOut.library.initialValuesFunction(data_spc, constants, i))
1708 1707
1709 1708 try:
1710 1709 #Least Squares
1711 1710 minp,covp,infodict,mesg,ier = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants),full_output=True)
1712 1711 # minp,covp = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants))
1713 1712 #Chi square error
1714 1713 error0 = numpy.sum(infodict['fvec']**2)/(2*N)
1715 1714 #Error with Jacobian
1716 1715 error1 = self.dataOut.library.errorFunction(minp,constants,LT)
1717 1716 except:
1718 1717 minp = p0*numpy.nan
1719 1718 error0 = numpy.nan
1720 1719 error1 = p0*numpy.nan
1721 1720
1722 1721 #Save
1723 1722 if self.dataOut.data_param == None:
1724 1723 self.dataOut.data_param = numpy.zeros((nGroups, p0.size, nHeights))*numpy.nan
1725 1724 self.dataOut.data_error = numpy.zeros((nGroups, p0.size + 1, nHeights))*numpy.nan
1726 1725
1727 1726 self.dataOut.data_error[i,:,h] = numpy.hstack((error0,error1))
1728 1727 self.dataOut.data_param[i,:,h] = minp
1729 1728 return
1730 1729
1731 1730 def __residFunction(self, p, dp, LT, constants):
1732 1731
1733 1732 fm = self.dataOut.library.modelFunction(p, constants)
1734 1733 fmp=numpy.dot(LT,fm)
1735 1734
1736 1735 return dp-fmp
1737 1736
1738 1737 def __getSNR(self, z, noise):
1739 1738
1740 1739 avg = numpy.average(z, axis=1)
1741 1740 SNR = (avg.T-noise)/noise
1742 1741 SNR = SNR.T
1743 1742 return SNR
1744 1743
1745 1744 def __chisq(p,chindex,hindex):
1746 1745 #similar to Resid but calculates CHI**2
1747 1746 [LT,d,fm]=setupLTdfm(p,chindex,hindex)
1748 1747 dp=numpy.dot(LT,d)
1749 1748 fmp=numpy.dot(LT,fm)
1750 1749 chisq=numpy.dot((dp-fmp).T,(dp-fmp))
1751 1750 return chisq
1752 1751
1753 1752 class WindProfiler(Operation):
1754 1753
1755 1754 __isConfig = False
1756 1755
1757 1756 __initime = None
1758 1757 __lastdatatime = None
1759 1758 __integrationtime = None
1760 1759
1761 1760 __buffer = None
1762 1761
1763 1762 __dataReady = False
1764 1763
1765 1764 __firstdata = None
1766 1765
1767 1766 n = None
1768 1767
1769 1768 def __init__(self):
1770 1769 Operation.__init__(self)
1771 1770
1772 1771 def __calculateCosDir(self, elev, azim):
1773 1772 zen = (90 - elev)*numpy.pi/180
1774 1773 azim = azim*numpy.pi/180
1775 1774 cosDirX = numpy.sqrt((1-numpy.cos(zen)**2)/((1+numpy.tan(azim)**2)))
1776 1775 cosDirY = numpy.sqrt(1-numpy.cos(zen)**2-cosDirX**2)
1777 1776
1778 1777 signX = numpy.sign(numpy.cos(azim))
1779 1778 signY = numpy.sign(numpy.sin(azim))
1780 1779
1781 1780 cosDirX = numpy.copysign(cosDirX, signX)
1782 1781 cosDirY = numpy.copysign(cosDirY, signY)
1783 1782 return cosDirX, cosDirY
1784 1783
1785 1784 def __calculateAngles(self, theta_x, theta_y, azimuth):
1786 1785
1787 1786 dir_cosw = numpy.sqrt(1-theta_x**2-theta_y**2)
1788 1787 zenith_arr = numpy.arccos(dir_cosw)
1789 1788 azimuth_arr = numpy.arctan2(theta_x,theta_y) + azimuth*math.pi/180
1790 1789
1791 1790 dir_cosu = numpy.sin(azimuth_arr)*numpy.sin(zenith_arr)
1792 1791 dir_cosv = numpy.cos(azimuth_arr)*numpy.sin(zenith_arr)
1793 1792
1794 1793 return azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw
1795 1794
1796 1795 def __calculateMatA(self, dir_cosu, dir_cosv, dir_cosw, horOnly):
1797 1796
1798 1797 #
1799 1798 if horOnly:
1800 1799 A = numpy.c_[dir_cosu,dir_cosv]
1801 1800 else:
1802 1801 A = numpy.c_[dir_cosu,dir_cosv,dir_cosw]
1803 1802 A = numpy.asmatrix(A)
1804 1803 A1 = numpy.linalg.inv(A.transpose()*A)*A.transpose()
1805 1804
1806 1805 return A1
1807 1806
1808 1807 def __correctValues(self, heiRang, phi, velRadial, SNR):
1809 1808 listPhi = phi.tolist()
1810 1809 maxid = listPhi.index(max(listPhi))
1811 1810 minid = listPhi.index(min(listPhi))
1812 1811
1813 1812 rango = range(len(phi))
1814 1813 # rango = numpy.delete(rango,maxid)
1815 1814
1816 1815 heiRang1 = heiRang*math.cos(phi[maxid])
1817 1816 heiRangAux = heiRang*math.cos(phi[minid])
1818 1817 indOut = (heiRang1 < heiRangAux[0]).nonzero()
1819 1818 heiRang1 = numpy.delete(heiRang1,indOut)
1820 1819
1821 1820 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
1822 1821 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
1823 1822
1824 1823 for i in rango:
1825 1824 x = heiRang*math.cos(phi[i])
1826 1825 y1 = velRadial[i,:]
1827 1826 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
1828 1827
1829 1828 x1 = heiRang1
1830 1829 y11 = f1(x1)
1831 1830
1832 1831 y2 = SNR[i,:]
1833 1832 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
1834 1833 y21 = f2(x1)
1835 1834
1836 1835 velRadial1[i,:] = y11
1837 1836 SNR1[i,:] = y21
1838 1837
1839 1838 return heiRang1, velRadial1, SNR1
1840 1839
1841 1840 def __calculateVelUVW(self, A, velRadial):
1842 1841
1843 1842 #Operacion Matricial
1844 1843 # velUVW = numpy.zeros((velRadial.shape[1],3))
1845 1844 # for ind in range(velRadial.shape[1]):
1846 1845 # velUVW[ind,:] = numpy.dot(A,velRadial[:,ind])
1847 1846 # velUVW = velUVW.transpose()
1848 1847 velUVW = numpy.zeros((A.shape[0],velRadial.shape[1]))
1849 1848 velUVW[:,:] = numpy.dot(A,velRadial)
1850 1849
1851 1850
1852 1851 return velUVW
1853 1852
1854 1853 # def techniqueDBS(self, velRadial0, dirCosx, disrCosy, azimuth, correct, horizontalOnly, heiRang, SNR0):
1855 1854
1856 1855 def techniqueDBS(self, kwargs):
1857 1856 """
1858 1857 Function that implements Doppler Beam Swinging (DBS) technique.
1859 1858
1860 1859 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1861 1860 Direction correction (if necessary), Ranges and SNR
1862 1861
1863 1862 Output: Winds estimation (Zonal, Meridional and Vertical)
1864 1863
1865 1864 Parameters affected: Winds, height range, SNR
1866 1865 """
1867 1866 velRadial0 = kwargs['velRadial']
1868 1867 heiRang = kwargs['heightList']
1869 1868 SNR0 = kwargs['SNR']
1870 1869
1871 1870 if kwargs.has_key('dirCosx') and kwargs.has_key('dirCosy'):
1872 1871 theta_x = numpy.array(kwargs['dirCosx'])
1873 1872 theta_y = numpy.array(kwargs['dirCosy'])
1874 1873 else:
1875 1874 elev = numpy.array(kwargs['elevation'])
1876 1875 azim = numpy.array(kwargs['azimuth'])
1877 1876 theta_x, theta_y = self.__calculateCosDir(elev, azim)
1878 1877 azimuth = kwargs['correctAzimuth']
1879 1878 if kwargs.has_key('horizontalOnly'):
1880 1879 horizontalOnly = kwargs['horizontalOnly']
1881 1880 else: horizontalOnly = False
1882 1881 if kwargs.has_key('correctFactor'):
1883 1882 correctFactor = kwargs['correctFactor']
1884 1883 else: correctFactor = 1
1885 1884 if kwargs.has_key('channelList'):
1886 1885 channelList = kwargs['channelList']
1887 1886 if len(channelList) == 2:
1888 1887 horizontalOnly = True
1889 1888 arrayChannel = numpy.array(channelList)
1890 1889 param = param[arrayChannel,:,:]
1891 1890 theta_x = theta_x[arrayChannel]
1892 1891 theta_y = theta_y[arrayChannel]
1893 1892
1894 1893 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(theta_x, theta_y, azimuth)
1895 1894 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, zenith_arr, correctFactor*velRadial0, SNR0)
1896 1895 A = self.__calculateMatA(dir_cosu, dir_cosv, dir_cosw, horizontalOnly)
1897 1896
1898 1897 #Calculo de Componentes de la velocidad con DBS
1899 1898 winds = self.__calculateVelUVW(A,velRadial1)
1900 1899
1901 1900 return winds, heiRang1, SNR1
1902 1901
1903 1902 def __calculateDistance(self, posx, posy, pairs_ccf, azimuth = None):
1904 1903
1905 1904 nPairs = len(pairs_ccf)
1906 1905 posx = numpy.asarray(posx)
1907 1906 posy = numpy.asarray(posy)
1908 1907
1909 1908 #Rotacion Inversa para alinear con el azimuth
1910 1909 if azimuth!= None:
1911 1910 azimuth = azimuth*math.pi/180
1912 1911 posx1 = posx*math.cos(azimuth) + posy*math.sin(azimuth)
1913 1912 posy1 = -posx*math.sin(azimuth) + posy*math.cos(azimuth)
1914 1913 else:
1915 1914 posx1 = posx
1916 1915 posy1 = posy
1917 1916
1918 1917 #Calculo de Distancias
1919 1918 distx = numpy.zeros(nPairs)
1920 1919 disty = numpy.zeros(nPairs)
1921 1920 dist = numpy.zeros(nPairs)
1922 1921 ang = numpy.zeros(nPairs)
1923 1922
1924 1923 for i in range(nPairs):
1925 1924 distx[i] = posx1[pairs_ccf[i][1]] - posx1[pairs_ccf[i][0]]
1926 1925 disty[i] = posy1[pairs_ccf[i][1]] - posy1[pairs_ccf[i][0]]
1927 1926 dist[i] = numpy.sqrt(distx[i]**2 + disty[i]**2)
1928 1927 ang[i] = numpy.arctan2(disty[i],distx[i])
1929 1928
1930 1929 return distx, disty, dist, ang
1931 1930 #Calculo de Matrices
1932 1931 # nPairs = len(pairs)
1933 1932 # ang1 = numpy.zeros((nPairs, 2, 1))
1934 1933 # dist1 = numpy.zeros((nPairs, 2, 1))
1935 1934 #
1936 1935 # for j in range(nPairs):
1937 1936 # dist1[j,0,0] = dist[pairs[j][0]]
1938 1937 # dist1[j,1,0] = dist[pairs[j][1]]
1939 1938 # ang1[j,0,0] = ang[pairs[j][0]]
1940 1939 # ang1[j,1,0] = ang[pairs[j][1]]
1941 1940 #
1942 1941 # return distx,disty, dist1,ang1
1943 1942
1944 1943
1945 1944 def __calculateVelVer(self, phase, lagTRange, _lambda):
1946 1945
1947 1946 Ts = lagTRange[1] - lagTRange[0]
1948 1947 velW = -_lambda*phase/(4*math.pi*Ts)
1949 1948
1950 1949 return velW
1951 1950
1952 1951 def __calculateVelHorDir(self, dist, tau1, tau2, ang):
1953 1952 nPairs = tau1.shape[0]
1954 1953 nHeights = tau1.shape[1]
1955 1954 vel = numpy.zeros((nPairs,3,nHeights))
1956 1955 dist1 = numpy.reshape(dist, (dist.size,1))
1957 1956
1958 1957 angCos = numpy.cos(ang)
1959 1958 angSin = numpy.sin(ang)
1960 1959
1961 1960 vel0 = dist1*tau1/(2*tau2**2)
1962 1961 vel[:,0,:] = (vel0*angCos).sum(axis = 1)
1963 1962 vel[:,1,:] = (vel0*angSin).sum(axis = 1)
1964 1963
1965 1964 ind = numpy.where(numpy.isinf(vel))
1966 1965 vel[ind] = numpy.nan
1967 1966
1968 1967 return vel
1969 1968
1970 1969 # def __getPairsAutoCorr(self, pairsList, nChannels):
1971 1970 #
1972 1971 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1973 1972 #
1974 1973 # for l in range(len(pairsList)):
1975 1974 # firstChannel = pairsList[l][0]
1976 1975 # secondChannel = pairsList[l][1]
1977 1976 #
1978 1977 # #Obteniendo pares de Autocorrelacion
1979 1978 # if firstChannel == secondChannel:
1980 1979 # pairsAutoCorr[firstChannel] = int(l)
1981 1980 #
1982 1981 # pairsAutoCorr = pairsAutoCorr.astype(int)
1983 1982 #
1984 1983 # pairsCrossCorr = range(len(pairsList))
1985 1984 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1986 1985 #
1987 1986 # return pairsAutoCorr, pairsCrossCorr
1988 1987
1989 1988 # def techniqueSA(self, pairsSelected, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, lagTRange, correctFactor):
1990 1989 def techniqueSA(self, kwargs):
1991 1990
1992 1991 """
1993 1992 Function that implements Spaced Antenna (SA) technique.
1994 1993
1995 1994 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1996 1995 Direction correction (if necessary), Ranges and SNR
1997 1996
1998 1997 Output: Winds estimation (Zonal, Meridional and Vertical)
1999 1998
2000 1999 Parameters affected: Winds
2001 2000 """
2002 2001 position_x = kwargs['positionX']
2003 2002 position_y = kwargs['positionY']
2004 2003 azimuth = kwargs['azimuth']
2005 2004
2006 2005 if kwargs.has_key('correctFactor'):
2007 2006 correctFactor = kwargs['correctFactor']
2008 2007 else:
2009 2008 correctFactor = 1
2010 2009
2011 2010 groupList = kwargs['groupList']
2012 2011 pairs_ccf = groupList[1]
2013 2012 tau = kwargs['tau']
2014 2013 _lambda = kwargs['_lambda']
2015 2014
2016 2015 #Cross Correlation pairs obtained
2017 2016 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairssList, nChannels)
2018 2017 # pairsArray = numpy.array(pairsList)[pairsCrossCorr]
2019 2018 # pairsSelArray = numpy.array(pairsSelected)
2020 2019 # pairs = []
2021 2020 #
2022 2021 # #Wind estimation pairs obtained
2023 2022 # for i in range(pairsSelArray.shape[0]/2):
2024 2023 # ind1 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i], axis = 1))[0][0]
2025 2024 # ind2 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i + 1], axis = 1))[0][0]
2026 2025 # pairs.append((ind1,ind2))
2027 2026
2028 2027 indtau = tau.shape[0]/2
2029 2028 tau1 = tau[:indtau,:]
2030 2029 tau2 = tau[indtau:-1,:]
2031 2030 # tau1 = tau1[pairs,:]
2032 2031 # tau2 = tau2[pairs,:]
2033 2032 phase1 = tau[-1,:]
2034 2033
2035 2034 #---------------------------------------------------------------------
2036 2035 #Metodo Directo
2037 2036 distx, disty, dist, ang = self.__calculateDistance(position_x, position_y, pairs_ccf,azimuth)
2038 2037 winds = self.__calculateVelHorDir(dist, tau1, tau2, ang)
2039 2038 winds = stats.nanmean(winds, axis=0)
2040 2039 #---------------------------------------------------------------------
2041 2040 #Metodo General
2042 2041 # distx, disty, dist = self.calculateDistance(position_x,position_y,pairsCrossCorr, pairsList, azimuth)
2043 2042 # #Calculo Coeficientes de Funcion de Correlacion
2044 2043 # F,G,A,B,H = self.calculateCoef(tau1,tau2,distx,disty,n)
2045 2044 # #Calculo de Velocidades
2046 2045 # winds = self.calculateVelUV(F,G,A,B,H)
2047 2046
2048 2047 #---------------------------------------------------------------------
2049 2048 winds[2,:] = self.__calculateVelVer(phase1, lagTRange, _lambda)
2050 2049 winds = correctFactor*winds
2051 2050 return winds
2052 2051
2053 2052 def __checkTime(self, currentTime, paramInterval, outputInterval):
2054 2053
2055 2054 dataTime = currentTime + paramInterval
2056 2055 deltaTime = dataTime - self.__initime
2057 2056
2058 2057 if deltaTime >= outputInterval or deltaTime < 0:
2059 2058 self.__dataReady = True
2060 2059 return
2061 2060
2062 2061 def techniqueMeteors(self, arrayMeteor, meteorThresh, heightMin, heightMax):
2063 2062 '''
2064 2063 Function that implements winds estimation technique with detected meteors.
2065 2064
2066 2065 Input: Detected meteors, Minimum meteor quantity to wind estimation
2067 2066
2068 2067 Output: Winds estimation (Zonal and Meridional)
2069 2068
2070 2069 Parameters affected: Winds
2071 2070 '''
2072 2071 # print arrayMeteor.shape
2073 2072 #Settings
2074 2073 nInt = (heightMax - heightMin)/2
2075 2074 # print nInt
2076 2075 nInt = int(nInt)
2077 2076 # print nInt
2078 2077 winds = numpy.zeros((2,nInt))*numpy.nan
2079 2078
2080 2079 #Filter errors
2081 2080 error = numpy.where(arrayMeteor[:,-1] == 0)[0]
2082 2081 finalMeteor = arrayMeteor[error,:]
2083 2082
2084 2083 #Meteor Histogram
2085 2084 finalHeights = finalMeteor[:,2]
2086 2085 hist = numpy.histogram(finalHeights, bins = nInt, range = (heightMin,heightMax))
2087 2086 nMeteorsPerI = hist[0]
2088 2087 heightPerI = hist[1]
2089 2088
2090 2089 #Sort of meteors
2091 2090 indSort = finalHeights.argsort()
2092 2091 finalMeteor2 = finalMeteor[indSort,:]
2093 2092
2094 2093 # Calculating winds
2095 2094 ind1 = 0
2096 2095 ind2 = 0
2097 2096
2098 2097 for i in range(nInt):
2099 2098 nMet = nMeteorsPerI[i]
2100 2099 ind1 = ind2
2101 2100 ind2 = ind1 + nMet
2102 2101
2103 2102 meteorAux = finalMeteor2[ind1:ind2,:]
2104 2103
2105 2104 if meteorAux.shape[0] >= meteorThresh:
2106 2105 vel = meteorAux[:, 6]
2107 2106 zen = meteorAux[:, 4]*numpy.pi/180
2108 2107 azim = meteorAux[:, 3]*numpy.pi/180
2109 2108
2110 2109 n = numpy.cos(zen)
2111 2110 # m = (1 - n**2)/(1 - numpy.tan(azim)**2)
2112 2111 # l = m*numpy.tan(azim)
2113 2112 l = numpy.sin(zen)*numpy.sin(azim)
2114 2113 m = numpy.sin(zen)*numpy.cos(azim)
2115 2114
2116 2115 A = numpy.vstack((l, m)).transpose()
2117 2116 A1 = numpy.dot(numpy.linalg.inv( numpy.dot(A.transpose(),A) ),A.transpose())
2118 2117 windsAux = numpy.dot(A1, vel)
2119 2118
2120 2119 winds[0,i] = windsAux[0]
2121 2120 winds[1,i] = windsAux[1]
2122 2121
2123 2122 return winds, heightPerI[:-1]
2124 2123
2125 2124 def techniqueNSM_SA(self, **kwargs):
2126 2125 metArray = kwargs['metArray']
2127 2126 heightList = kwargs['heightList']
2128 2127 timeList = kwargs['timeList']
2129 2128
2130 2129 rx_location = kwargs['rx_location']
2131 2130 groupList = kwargs['groupList']
2132 2131 azimuth = kwargs['azimuth']
2133 2132 dfactor = kwargs['dfactor']
2134 2133 k = kwargs['k']
2135 2134
2136 2135 azimuth1, dist = self.__calculateAzimuth1(rx_location, groupList, azimuth)
2137 2136 d = dist*dfactor
2138 2137 #Phase calculation
2139 2138 metArray1 = self.__getPhaseSlope(metArray, heightList, timeList)
2140 2139
2141 2140 metArray1[:,-2] = metArray1[:,-2]*metArray1[:,2]*1000/(k*d[metArray1[:,1].astype(int)]) #angles into velocities
2142 2141
2143 2142 velEst = numpy.zeros((heightList.size,2))*numpy.nan
2144 2143 azimuth1 = azimuth1*numpy.pi/180
2145 2144
2146 2145 for i in range(heightList.size):
2147 2146 h = heightList[i]
2148 2147 indH = numpy.where((metArray1[:,2] == h)&(numpy.abs(metArray1[:,-2]) < 100))[0]
2149 2148 metHeight = metArray1[indH,:]
2150 2149 if metHeight.shape[0] >= 2:
2151 2150 velAux = numpy.asmatrix(metHeight[:,-2]).T #Radial Velocities
2152 2151 iazim = metHeight[:,1].astype(int)
2153 2152 azimAux = numpy.asmatrix(azimuth1[iazim]).T #Azimuths
2154 2153 A = numpy.hstack((numpy.cos(azimAux),numpy.sin(azimAux)))
2155 2154 A = numpy.asmatrix(A)
2156 2155 A1 = numpy.linalg.pinv(A.transpose()*A)*A.transpose()
2157 2156 velHor = numpy.dot(A1,velAux)
2158 2157
2159 2158 velEst[i,:] = numpy.squeeze(velHor)
2160 2159 return velEst
2161 2160
2162 2161 def __getPhaseSlope(self, metArray, heightList, timeList):
2163 2162 meteorList = []
2164 2163 #utctime sec1 height SNR velRad ph0 ph1 ph2 coh0 coh1 coh2
2165 2164 #Putting back together the meteor matrix
2166 2165 utctime = metArray[:,0]
2167 2166 uniqueTime = numpy.unique(utctime)
2168 2167
2169 2168 phaseDerThresh = 0.5
2170 2169 ippSeconds = timeList[1] - timeList[0]
2171 2170 sec = numpy.where(timeList>1)[0][0]
2172 2171 nPairs = metArray.shape[1] - 6
2173 2172 nHeights = len(heightList)
2174 2173
2175 2174 for t in uniqueTime:
2176 2175 metArray1 = metArray[utctime==t,:]
2177 2176 # phaseDerThresh = numpy.pi/4 #reducir Phase thresh
2178 2177 tmet = metArray1[:,1].astype(int)
2179 2178 hmet = metArray1[:,2].astype(int)
2180 2179
2181 2180 metPhase = numpy.zeros((nPairs, heightList.size, timeList.size - 1))
2182 2181 metPhase[:,:] = numpy.nan
2183 2182 metPhase[:,hmet,tmet] = metArray1[:,6:].T
2184 2183
2185 2184 #Delete short trails
2186 2185 metBool = ~numpy.isnan(metPhase[0,:,:])
2187 2186 heightVect = numpy.sum(metBool, axis = 1)
2188 2187 metBool[heightVect<sec,:] = False
2189 2188 metPhase[:,heightVect<sec,:] = numpy.nan
2190 2189
2191 2190 #Derivative
2192 2191 metDer = numpy.abs(metPhase[:,:,1:] - metPhase[:,:,:-1])
2193 2192 phDerAux = numpy.dstack((numpy.full((nPairs,nHeights,1), False, dtype=bool),metDer > phaseDerThresh))
2194 2193 metPhase[phDerAux] = numpy.nan
2195 2194
2196 2195 #--------------------------METEOR DETECTION -----------------------------------------
2197 2196 indMet = numpy.where(numpy.any(metBool,axis=1))[0]
2198 2197
2199 2198 for p in numpy.arange(nPairs):
2200 2199 phase = metPhase[p,:,:]
2201 2200 phDer = metDer[p,:,:]
2202 2201
2203 2202 for h in indMet:
2204 2203 height = heightList[h]
2205 2204 phase1 = phase[h,:] #82
2206 2205 phDer1 = phDer[h,:]
2207 2206
2208 2207 phase1[~numpy.isnan(phase1)] = numpy.unwrap(phase1[~numpy.isnan(phase1)]) #Unwrap
2209 2208
2210 2209 indValid = numpy.where(~numpy.isnan(phase1))[0]
2211 2210 initMet = indValid[0]
2212 2211 endMet = 0
2213 2212
2214 2213 for i in range(len(indValid)-1):
2215 2214
2216 2215 #Time difference
2217 2216 inow = indValid[i]
2218 2217 inext = indValid[i+1]
2219 2218 idiff = inext - inow
2220 2219 #Phase difference
2221 2220 phDiff = numpy.abs(phase1[inext] - phase1[inow])
2222 2221
2223 2222 if idiff>sec or phDiff>numpy.pi/4 or inext==indValid[-1]: #End of Meteor
2224 2223 sizeTrail = inow - initMet + 1
2225 2224 if sizeTrail>3*sec: #Too short meteors
2226 2225 x = numpy.arange(initMet,inow+1)*ippSeconds
2227 2226 y = phase1[initMet:inow+1]
2228 2227 ynnan = ~numpy.isnan(y)
2229 2228 x = x[ynnan]
2230 2229 y = y[ynnan]
2231 2230 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
2232 2231 ylin = x*slope + intercept
2233 2232 rsq = r_value**2
2234 2233 if rsq > 0.5:
2235 2234 vel = slope#*height*1000/(k*d)
2236 2235 estAux = numpy.array([utctime,p,height, vel, rsq])
2237 2236 meteorList.append(estAux)
2238 2237 initMet = inext
2239 2238 metArray2 = numpy.array(meteorList)
2240 2239
2241 2240 return metArray2
2242 2241
2243 2242 def __calculateAzimuth1(self, rx_location, pairslist, azimuth0):
2244 2243
2245 2244 azimuth1 = numpy.zeros(len(pairslist))
2246 2245 dist = numpy.zeros(len(pairslist))
2247 2246
2248 2247 for i in range(len(rx_location)):
2249 2248 ch0 = pairslist[i][0]
2250 2249 ch1 = pairslist[i][1]
2251 2250
2252 2251 diffX = rx_location[ch0][0] - rx_location[ch1][0]
2253 2252 diffY = rx_location[ch0][1] - rx_location[ch1][1]
2254 2253 azimuth1[i] = numpy.arctan2(diffY,diffX)*180/numpy.pi
2255 2254 dist[i] = numpy.sqrt(diffX**2 + diffY**2)
2256 2255
2257 2256 azimuth1 -= azimuth0
2258 2257 return azimuth1, dist
2259 2258
2260 2259 def techniqueNSM_DBS(self, **kwargs):
2261 2260 metArray = kwargs['metArray']
2262 2261 heightList = kwargs['heightList']
2263 2262 timeList = kwargs['timeList']
2264 2263 azimuth = kwargs['azimuth']
2265 2264 theta_x = numpy.array(kwargs['theta_x'])
2266 2265 theta_y = numpy.array(kwargs['theta_y'])
2267 2266
2268 2267 utctime = metArray[:,0]
2269 2268 cmet = metArray[:,1].astype(int)
2270 2269 hmet = metArray[:,3].astype(int)
2271 2270 SNRmet = metArray[:,4]
2272 2271 vmet = metArray[:,5]
2273 2272 spcmet = metArray[:,6]
2274 2273
2275 2274 nChan = numpy.max(cmet) + 1
2276 2275 nHeights = len(heightList)
2277 2276
2278 2277 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(theta_x, theta_y, azimuth)
2279 2278 hmet = heightList[hmet]
2280 2279 h1met = hmet*numpy.cos(zenith_arr[cmet]) #Corrected heights
2281 2280
2282 2281 velEst = numpy.zeros((heightList.size,2))*numpy.nan
2283 2282
2284 2283 for i in range(nHeights - 1):
2285 2284 hmin = heightList[i]
2286 2285 hmax = heightList[i + 1]
2287 2286
2288 2287 thisH = (h1met>=hmin) & (h1met<hmax) & (cmet!=2) & (SNRmet>8) & (vmet<50) & (spcmet<10)
2289 2288 indthisH = numpy.where(thisH)
2290 2289
2291 2290 if numpy.size(indthisH) > 3:
2292 2291
2293 2292 vel_aux = vmet[thisH]
2294 2293 chan_aux = cmet[thisH]
2295 2294 cosu_aux = dir_cosu[chan_aux]
2296 2295 cosv_aux = dir_cosv[chan_aux]
2297 2296 cosw_aux = dir_cosw[chan_aux]
2298 2297
2299 2298 nch = numpy.size(numpy.unique(chan_aux))
2300 2299 if nch > 1:
2301 2300 A = self.__calculateMatA(cosu_aux, cosv_aux, cosw_aux, True)
2302 2301 velEst[i,:] = numpy.dot(A,vel_aux)
2303 2302
2304 2303 return velEst
2305 2304
2306 2305 def run(self, dataOut, technique, nHours=1, hmin=70, hmax=110, **kwargs):
2307 2306
2308 2307 param = dataOut.data_param
2309 2308 if dataOut.abscissaList != None:
2310 2309 absc = dataOut.abscissaList[:-1]
2311 2310 # noise = dataOut.noise
2312 2311 heightList = dataOut.heightList
2313 2312 SNR = dataOut.data_SNR
2314 2313
2315 2314 if technique == 'DBS':
2316 2315
2317 2316 kwargs['velRadial'] = param[:,1,:] #Radial velocity
2318 2317 kwargs['heightList'] = heightList
2319 2318 kwargs['SNR'] = SNR
2320 2319
2321 2320 dataOut.data_output, dataOut.heightList, dataOut.data_SNR = self.techniqueDBS(kwargs) #DBS Function
2322 2321 dataOut.utctimeInit = dataOut.utctime
2323 2322 dataOut.outputInterval = dataOut.paramInterval
2324 2323
2325 2324 elif technique == 'SA':
2326 2325
2327 2326 #Parameters
2328 2327 # position_x = kwargs['positionX']
2329 2328 # position_y = kwargs['positionY']
2330 2329 # azimuth = kwargs['azimuth']
2331 2330 #
2332 2331 # if kwargs.has_key('crosspairsList'):
2333 2332 # pairs = kwargs['crosspairsList']
2334 2333 # else:
2335 2334 # pairs = None
2336 2335 #
2337 2336 # if kwargs.has_key('correctFactor'):
2338 2337 # correctFactor = kwargs['correctFactor']
2339 2338 # else:
2340 2339 # correctFactor = 1
2341 2340
2342 2341 # tau = dataOut.data_param
2343 2342 # _lambda = dataOut.C/dataOut.frequency
2344 2343 # pairsList = dataOut.groupList
2345 2344 # nChannels = dataOut.nChannels
2346 2345
2347 2346 kwargs['groupList'] = dataOut.groupList
2348 2347 kwargs['tau'] = dataOut.data_param
2349 2348 kwargs['_lambda'] = dataOut.C/dataOut.frequency
2350 2349 # dataOut.data_output = self.techniqueSA(pairs, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, absc, correctFactor)
2351 2350 dataOut.data_output = self.techniqueSA(kwargs)
2352 2351 dataOut.utctimeInit = dataOut.utctime
2353 2352 dataOut.outputInterval = dataOut.timeInterval
2354 2353
2355 2354 elif technique == 'Meteors':
2356 2355 dataOut.flagNoData = True
2357 2356 self.__dataReady = False
2358 2357
2359 2358 if kwargs.has_key('nHours'):
2360 2359 nHours = kwargs['nHours']
2361 2360 else:
2362 2361 nHours = 1
2363 2362
2364 2363 if kwargs.has_key('meteorsPerBin'):
2365 2364 meteorThresh = kwargs['meteorsPerBin']
2366 2365 else:
2367 2366 meteorThresh = 6
2368 2367
2369 2368 if kwargs.has_key('hmin'):
2370 2369 hmin = kwargs['hmin']
2371 2370 else: hmin = 70
2372 2371 if kwargs.has_key('hmax'):
2373 2372 hmax = kwargs['hmax']
2374 2373 else: hmax = 110
2375 2374
2376 2375 dataOut.outputInterval = nHours*3600
2377 2376
2378 2377 if self.__isConfig == False:
2379 2378 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2380 2379 #Get Initial LTC time
2381 2380 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2382 2381 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2383 2382
2384 2383 self.__isConfig = True
2385 2384
2386 2385 if self.__buffer == None:
2387 2386 self.__buffer = dataOut.data_param
2388 2387 self.__firstdata = copy.copy(dataOut)
2389 2388
2390 2389 else:
2391 2390 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2392 2391
2393 2392 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2394 2393
2395 2394 if self.__dataReady:
2396 2395 dataOut.utctimeInit = self.__initime
2397 2396
2398 2397 self.__initime += dataOut.outputInterval #to erase time offset
2399 2398
2400 2399 dataOut.data_output, dataOut.heightList = self.techniqueMeteors(self.__buffer, meteorThresh, hmin, hmax)
2401 2400 dataOut.flagNoData = False
2402 2401 self.__buffer = None
2403 2402
2404 2403 elif technique == 'Meteors1':
2405 2404 dataOut.flagNoData = True
2406 2405 self.__dataReady = False
2407 2406
2408 2407 if kwargs.has_key('nMins'):
2409 2408 nMins = kwargs['nMins']
2410 2409 else: nMins = 20
2411 2410 if kwargs.has_key('rx_location'):
2412 2411 rx_location = kwargs['rx_location']
2413 2412 else: rx_location = [(0,1),(1,1),(1,0)]
2414 2413 if kwargs.has_key('azimuth'):
2415 2414 azimuth = kwargs['azimuth']
2416 2415 else: azimuth = 51.06
2417 2416 if kwargs.has_key('dfactor'):
2418 2417 dfactor = kwargs['dfactor']
2419 2418 if kwargs.has_key('mode'):
2420 2419 mode = kwargs['mode']
2421 2420 if kwargs.has_key('theta_x'):
2422 2421 theta_x = kwargs['theta_x']
2423 2422 if kwargs.has_key('theta_y'):
2424 2423 theta_y = kwargs['theta_y']
2425 2424 else: mode = 'SA'
2426 2425
2427 2426 #Borrar luego esto
2428 2427 if dataOut.groupList == None:
2429 2428 dataOut.groupList = [(0,1),(0,2),(1,2)]
2430 2429 groupList = dataOut.groupList
2431 2430 C = 3e8
2432 2431 freq = 50e6
2433 2432 lamb = C/freq
2434 2433 k = 2*numpy.pi/lamb
2435 2434
2436 2435 timeList = dataOut.abscissaList
2437 2436 heightList = dataOut.heightList
2438 2437
2439 2438 if self.__isConfig == False:
2440 2439 dataOut.outputInterval = nMins*60
2441 2440 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2442 2441 #Get Initial LTC time
2443 2442 initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2444 2443 minuteAux = initime.minute
2445 2444 minuteNew = int(numpy.floor(minuteAux/nMins)*nMins)
2446 2445 self.__initime = (initime.replace(minute = minuteNew, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2447 2446
2448 2447 self.__isConfig = True
2449 2448
2450 2449 if self.__buffer == None:
2451 2450 self.__buffer = dataOut.data_param
2452 2451 self.__firstdata = copy.copy(dataOut)
2453 2452
2454 2453 else:
2455 2454 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2456 2455
2457 2456 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2458 2457
2459 2458 if self.__dataReady:
2460 2459 dataOut.utctimeInit = self.__initime
2461 2460 self.__initime += dataOut.outputInterval #to erase time offset
2462 2461
2463 2462 metArray = self.__buffer
2464 2463 if mode == 'SA':
2465 2464 dataOut.data_output = self.techniqueNSM_SA(rx_location=rx_location, groupList=groupList, azimuth=azimuth, dfactor=dfactor, k=k,metArray=metArray, heightList=heightList,timeList=timeList)
2466 2465 elif mode == 'DBS':
2467 2466 dataOut.data_output = self.techniqueNSM_DBS(metArray=metArray,heightList=heightList,timeList=timeList, azimuth=azimuth, theta_x=theta_x, theta_y=theta_y)
2468 2467 dataOut.data_output = dataOut.data_output.T
2469 2468 dataOut.flagNoData = False
2470 2469 self.__buffer = None
2471 2470
2472 2471 return
2473 2472
2474 2473 class EWDriftsEstimation(Operation):
2475 2474
2476 2475 def __init__(self):
2477 2476 Operation.__init__(self)
2478 2477
2479 2478 def __correctValues(self, heiRang, phi, velRadial, SNR):
2480 2479 listPhi = phi.tolist()
2481 2480 maxid = listPhi.index(max(listPhi))
2482 2481 minid = listPhi.index(min(listPhi))
2483 2482
2484 2483 rango = range(len(phi))
2485 2484 # rango = numpy.delete(rango,maxid)
2486 2485
2487 2486 heiRang1 = heiRang*math.cos(phi[maxid])
2488 2487 heiRangAux = heiRang*math.cos(phi[minid])
2489 2488 indOut = (heiRang1 < heiRangAux[0]).nonzero()
2490 2489 heiRang1 = numpy.delete(heiRang1,indOut)
2491 2490
2492 2491 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
2493 2492 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
2494 2493
2495 2494 for i in rango:
2496 2495 x = heiRang*math.cos(phi[i])
2497 2496 y1 = velRadial[i,:]
2498 2497 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
2499 2498
2500 2499 x1 = heiRang1
2501 2500 y11 = f1(x1)
2502 2501
2503 2502 y2 = SNR[i,:]
2504 2503 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
2505 2504 y21 = f2(x1)
2506 2505
2507 2506 velRadial1[i,:] = y11
2508 2507 SNR1[i,:] = y21
2509 2508
2510 2509 return heiRang1, velRadial1, SNR1
2511 2510
2512 2511 def run(self, dataOut, zenith, zenithCorrection):
2513 2512 heiRang = dataOut.heightList
2514 2513 velRadial = dataOut.data_param[:,3,:]
2515 2514 SNR = dataOut.data_SNR
2516 2515
2517 2516 zenith = numpy.array(zenith)
2518 2517 zenith -= zenithCorrection
2519 2518 zenith *= numpy.pi/180
2520 2519
2521 2520 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, numpy.abs(zenith), velRadial, SNR)
2522 2521
2523 2522 alp = zenith[0]
2524 2523 bet = zenith[1]
2525 2524
2526 2525 w_w = velRadial1[0,:]
2527 2526 w_e = velRadial1[1,:]
2528 2527
2529 2528 w = (w_w*numpy.sin(bet) - w_e*numpy.sin(alp))/(numpy.cos(alp)*numpy.sin(bet) - numpy.cos(bet)*numpy.sin(alp))
2530 2529 u = (w_w*numpy.cos(bet) - w_e*numpy.cos(alp))/(numpy.sin(alp)*numpy.cos(bet) - numpy.sin(bet)*numpy.cos(alp))
2531 2530
2532 2531 winds = numpy.vstack((u,w))
2533 2532
2534 2533 dataOut.heightList = heiRang1
2535 2534 dataOut.data_output = winds
2536 2535 dataOut.data_SNR = SNR1
2537 2536
2538 2537 dataOut.utctimeInit = dataOut.utctime
2539 2538 dataOut.outputInterval = dataOut.timeInterval
2540 2539 return
2541 2540
2542 2541 #--------------- Non Specular Meteor ----------------
2543 2542
2544 2543 class NonSpecularMeteorDetection(Operation):
2545 2544
2546 2545 def run(self, dataOut, mode, SNRthresh=8, phaseDerThresh=0.5, cohThresh=0.8, allData = False):
2547 2546 data_acf = dataOut.data_pre[0]
2548 2547 data_ccf = dataOut.data_pre[1]
2549 2548 pairsList = dataOut.groupList[1]
2550 2549
2551 2550 lamb = dataOut.C/dataOut.frequency
2552 2551 tSamp = dataOut.ippSeconds*dataOut.nCohInt
2553 2552 paramInterval = dataOut.paramInterval
2554 2553
2555 2554 nChannels = data_acf.shape[0]
2556 2555 nLags = data_acf.shape[1]
2557 2556 nProfiles = data_acf.shape[2]
2558 2557 nHeights = dataOut.nHeights
2559 2558 nCohInt = dataOut.nCohInt
2560 2559 sec = numpy.round(nProfiles/dataOut.paramInterval)
2561 2560 heightList = dataOut.heightList
2562 2561 ippSeconds = dataOut.ippSeconds*dataOut.nCohInt*dataOut.nAvg
2563 2562 utctime = dataOut.utctime
2564 2563
2565 2564 dataOut.abscissaList = numpy.arange(0,paramInterval+ippSeconds,ippSeconds)
2566 2565
2567 2566 #------------------------ SNR --------------------------------------
2568 2567 power = data_acf[:,0,:,:].real
2569 2568 noise = numpy.zeros(nChannels)
2570 2569 SNR = numpy.zeros(power.shape)
2571 2570 for i in range(nChannels):
2572 2571 noise[i] = hildebrand_sekhon(power[i,:], nCohInt)
2573 2572 SNR[i] = (power[i]-noise[i])/noise[i]
2574 2573 SNRm = numpy.nanmean(SNR, axis = 0)
2575 2574 SNRdB = 10*numpy.log10(SNR)
2576 2575
2577 2576 if mode == 'SA':
2578 2577 dataOut.groupList = dataOut.groupList[1]
2579 2578 nPairs = data_ccf.shape[0]
2580 2579 #---------------------- Coherence and Phase --------------------------
2581 2580 phase = numpy.zeros(data_ccf[:,0,:,:].shape)
2582 2581 # phase1 = numpy.copy(phase)
2583 2582 coh1 = numpy.zeros(data_ccf[:,0,:,:].shape)
2584 2583
2585 2584 for p in range(nPairs):
2586 2585 ch0 = pairsList[p][0]
2587 2586 ch1 = pairsList[p][1]
2588 2587 ccf = data_ccf[p,0,:,:]/numpy.sqrt(data_acf[ch0,0,:,:]*data_acf[ch1,0,:,:])
2589 2588 phase[p,:,:] = ndimage.median_filter(numpy.angle(ccf), size = (5,1)) #median filter
2590 2589 # phase1[p,:,:] = numpy.angle(ccf) #median filter
2591 2590 coh1[p,:,:] = ndimage.median_filter(numpy.abs(ccf), 5) #median filter
2592 2591 # coh1[p,:,:] = numpy.abs(ccf) #median filter
2593 2592 coh = numpy.nanmax(coh1, axis = 0)
2594 2593 # struc = numpy.ones((5,1))
2595 2594 # coh = ndimage.morphology.grey_dilation(coh, size=(10,1))
2596 2595 #---------------------- Radial Velocity ----------------------------
2597 2596 phaseAux = numpy.mean(numpy.angle(data_acf[:,1,:,:]), axis = 0)
2598 2597 velRad = phaseAux*lamb/(4*numpy.pi*tSamp)
2599 2598
2600 2599 if allData:
2601 2600 boolMetFin = ~numpy.isnan(SNRm)
2602 2601 # coh[:-1,:] = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2603 2602 else:
2604 2603 #------------------------ Meteor mask ---------------------------------
2605 2604 # #SNR mask
2606 2605 # boolMet = (SNRdB>SNRthresh)#|(~numpy.isnan(SNRdB))
2607 2606 #
2608 2607 # #Erase small objects
2609 2608 # boolMet1 = self.__erase_small(boolMet, 2*sec, 5)
2610 2609 #
2611 2610 # auxEEJ = numpy.sum(boolMet1,axis=0)
2612 2611 # indOver = auxEEJ>nProfiles*0.8 #Use this later
2613 2612 # indEEJ = numpy.where(indOver)[0]
2614 2613 # indNEEJ = numpy.where(~indOver)[0]
2615 2614 #
2616 2615 # boolMetFin = boolMet1
2617 2616 #
2618 2617 # if indEEJ.size > 0:
2619 2618 # boolMet1[:,indEEJ] = False #Erase heights with EEJ
2620 2619 #
2621 2620 # boolMet2 = coh > cohThresh
2622 2621 # boolMet2 = self.__erase_small(boolMet2, 2*sec,5)
2623 2622 #
2624 2623 # #Final Meteor mask
2625 2624 # boolMetFin = boolMet1|boolMet2
2626 2625
2627 2626 #Coherence mask
2628 2627 boolMet1 = coh > 0.75
2629 2628 struc = numpy.ones((30,1))
2630 2629 boolMet1 = ndimage.morphology.binary_dilation(boolMet1, structure=struc)
2631 2630
2632 2631 #Derivative mask
2633 2632 derPhase = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2634 2633 boolMet2 = derPhase < 0.2
2635 2634 # boolMet2 = ndimage.morphology.binary_opening(boolMet2)
2636 2635 # boolMet2 = ndimage.morphology.binary_closing(boolMet2, structure = numpy.ones((10,1)))
2637 2636 boolMet2 = ndimage.median_filter(boolMet2,size=5)
2638 2637 boolMet2 = numpy.vstack((boolMet2,numpy.full((1,nHeights), True, dtype=bool)))
2639 2638 # #Final mask
2640 2639 # boolMetFin = boolMet2
2641 2640 boolMetFin = boolMet1&boolMet2
2642 2641 # boolMetFin = ndimage.morphology.binary_dilation(boolMetFin)
2643 2642 #Creating data_param
2644 2643 coordMet = numpy.where(boolMetFin)
2645 2644
2646 2645 tmet = coordMet[0]
2647 2646 hmet = coordMet[1]
2648 2647
2649 2648 data_param = numpy.zeros((tmet.size, 6 + nPairs))
2650 2649 data_param[:,0] = utctime
2651 2650 data_param[:,1] = tmet
2652 2651 data_param[:,2] = hmet
2653 2652 data_param[:,3] = SNRm[tmet,hmet]
2654 2653 data_param[:,4] = velRad[tmet,hmet]
2655 2654 data_param[:,5] = coh[tmet,hmet]
2656 2655 data_param[:,6:] = phase[:,tmet,hmet].T
2657 2656
2658 2657 elif mode == 'DBS':
2659 2658 dataOut.groupList = numpy.arange(nChannels)
2660 2659
2661 2660 #Radial Velocities
2662 2661 phase = numpy.angle(data_acf[:,1,:,:])
2663 2662 # phase = ndimage.median_filter(numpy.angle(data_acf[:,1,:,:]), size = (1,5,1))
2664 2663 velRad = phase*lamb/(4*numpy.pi*tSamp)
2665 2664
2666 2665 #Spectral width
2667 2666 # acf1 = ndimage.median_filter(numpy.abs(data_acf[:,1,:,:]), size = (1,5,1))
2668 2667 # acf2 = ndimage.median_filter(numpy.abs(data_acf[:,2,:,:]), size = (1,5,1))
2669 2668 acf1 = data_acf[:,1,:,:]
2670 2669 acf2 = data_acf[:,2,:,:]
2671 2670
2672 2671 spcWidth = (lamb/(2*numpy.sqrt(6)*numpy.pi*tSamp))*numpy.sqrt(numpy.log(acf1/acf2))
2673 2672 # velRad = ndimage.median_filter(velRad, size = (1,5,1))
2674 2673 if allData:
2675 2674 boolMetFin = ~numpy.isnan(SNRdB)
2676 2675 else:
2677 2676 #SNR
2678 2677 boolMet1 = (SNRdB>SNRthresh) #SNR mask
2679 2678 boolMet1 = ndimage.median_filter(boolMet1, size=(1,5,5))
2680 2679
2681 2680 #Radial velocity
2682 2681 boolMet2 = numpy.abs(velRad) < 20
2683 2682 boolMet2 = ndimage.median_filter(boolMet2, (1,5,5))
2684 2683
2685 2684 #Spectral Width
2686 2685 boolMet3 = spcWidth < 30
2687 2686 boolMet3 = ndimage.median_filter(boolMet3, (1,5,5))
2688 2687 # boolMetFin = self.__erase_small(boolMet1, 10,5)
2689 2688 boolMetFin = boolMet1&boolMet2&boolMet3
2690 2689
2691 2690 #Creating data_param
2692 2691 coordMet = numpy.where(boolMetFin)
2693 2692
2694 2693 cmet = coordMet[0]
2695 2694 tmet = coordMet[1]
2696 2695 hmet = coordMet[2]
2697 2696
2698 2697 data_param = numpy.zeros((tmet.size, 7))
2699 2698 data_param[:,0] = utctime
2700 2699 data_param[:,1] = cmet
2701 2700 data_param[:,2] = tmet
2702 2701 data_param[:,3] = hmet
2703 2702 data_param[:,4] = SNR[cmet,tmet,hmet].T
2704 2703 data_param[:,5] = velRad[cmet,tmet,hmet].T
2705 2704 data_param[:,6] = spcWidth[cmet,tmet,hmet].T
2706 2705
2707 2706 # self.dataOut.data_param = data_int
2708 2707 if len(data_param) == 0:
2709 2708 dataOut.flagNoData = True
2710 2709 else:
2711 2710 dataOut.data_param = data_param
2712 2711
2713 2712 def __erase_small(self, binArray, threshX, threshY):
2714 2713 labarray, numfeat = ndimage.measurements.label(binArray)
2715 2714 binArray1 = numpy.copy(binArray)
2716 2715
2717 2716 for i in range(1,numfeat + 1):
2718 2717 auxBin = (labarray==i)
2719 2718 auxSize = auxBin.sum()
2720 2719
2721 2720 x,y = numpy.where(auxBin)
2722 2721 widthX = x.max() - x.min()
2723 2722 widthY = y.max() - y.min()
2724 2723
2725 2724 #width X: 3 seg -> 12.5*3
2726 2725 #width Y:
2727 2726
2728 2727 if (auxSize < 50) or (widthX < threshX) or (widthY < threshY):
2729 2728 binArray1[auxBin] = False
2730 2729
2731 2730 return binArray1
2732 2731
2733 2732 #--------------- Specular Meteor ----------------
2734 2733
2735 2734 class SMDetection(Operation):
2736 2735 '''
2737 2736 Function DetectMeteors()
2738 2737 Project developed with paper:
2739 2738 HOLDSWORTH ET AL. 2004
2740 2739
2741 2740 Input:
2742 2741 self.dataOut.data_pre
2743 2742
2744 2743 centerReceiverIndex: From the channels, which is the center receiver
2745 2744
2746 2745 hei_ref: Height reference for the Beacon signal extraction
2747 2746 tauindex:
2748 2747 predefinedPhaseShifts: Predefined phase offset for the voltge signals
2749 2748
2750 2749 cohDetection: Whether to user Coherent detection or not
2751 2750 cohDet_timeStep: Coherent Detection calculation time step
2752 2751 cohDet_thresh: Coherent Detection phase threshold to correct phases
2753 2752
2754 2753 noise_timeStep: Noise calculation time step
2755 2754 noise_multiple: Noise multiple to define signal threshold
2756 2755
2757 2756 multDet_timeLimit: Multiple Detection Removal time limit in seconds
2758 2757 multDet_rangeLimit: Multiple Detection Removal range limit in km
2759 2758
2760 2759 phaseThresh: Maximum phase difference between receiver to be consider a meteor
2761 2760 SNRThresh: Minimum SNR threshold of the meteor signal to be consider a meteor
2762 2761
2763 2762 hmin: Minimum Height of the meteor to use it in the further wind estimations
2764 2763 hmax: Maximum Height of the meteor to use it in the further wind estimations
2765 2764 azimuth: Azimuth angle correction
2766 2765
2767 2766 Affected:
2768 2767 self.dataOut.data_param
2769 2768
2770 2769 Rejection Criteria (Errors):
2771 2770 0: No error; analysis OK
2772 2771 1: SNR < SNR threshold
2773 2772 2: angle of arrival (AOA) ambiguously determined
2774 2773 3: AOA estimate not feasible
2775 2774 4: Large difference in AOAs obtained from different antenna baselines
2776 2775 5: echo at start or end of time series
2777 2776 6: echo less than 5 examples long; too short for analysis
2778 2777 7: echo rise exceeds 0.3s
2779 2778 8: echo decay time less than twice rise time
2780 2779 9: large power level before echo
2781 2780 10: large power level after echo
2782 2781 11: poor fit to amplitude for estimation of decay time
2783 2782 12: poor fit to CCF phase variation for estimation of radial drift velocity
2784 2783 13: height unresolvable echo: not valid height within 70 to 110 km
2785 2784 14: height ambiguous echo: more then one possible height within 70 to 110 km
2786 2785 15: radial drift velocity or projected horizontal velocity exceeds 200 m/s
2787 2786 16: oscilatory echo, indicating event most likely not an underdense echo
2788 2787
2789 2788 17: phase difference in meteor Reestimation
2790 2789
2791 2790 Data Storage:
2792 2791 Meteors for Wind Estimation (8):
2793 2792 Utc Time | Range Height
2794 2793 Azimuth Zenith errorCosDir
2795 2794 VelRad errorVelRad
2796 2795 Phase0 Phase1 Phase2 Phase3
2797 2796 TypeError
2798 2797
2799 2798 '''
2800 2799
2801 2800 def run(self, dataOut, hei_ref = None, tauindex = 0,
2802 2801 phaseOffsets = None,
2803 2802 cohDetection = False, cohDet_timeStep = 1, cohDet_thresh = 25,
2804 2803 noise_timeStep = 4, noise_multiple = 4,
2805 2804 multDet_timeLimit = 1, multDet_rangeLimit = 3,
2806 2805 phaseThresh = 20, SNRThresh = 5,
2807 2806 hmin = 50, hmax=150, azimuth = 0,
2808 2807 channelPositions = None) :
2809 2808
2810 2809
2811 2810 #Getting Pairslist
2812 2811 if channelPositions == None:
2813 2812 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
2814 2813 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
2815 2814 meteorOps = SMOperations()
2816 2815 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
2817 2816 heiRang = dataOut.getHeiRange()
2818 2817 #Get Beacon signal - No Beacon signal anymore
2819 2818 # newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex])
2820 2819 #
2821 2820 # if hei_ref != None:
2822 2821 # newheis = numpy.where(self.dataOut.heightList>hei_ref)
2823 2822 #
2824 2823
2825 2824
2826 2825 #****************REMOVING HARDWARE PHASE DIFFERENCES***************
2827 2826 # see if the user put in pre defined phase shifts
2828 2827 voltsPShift = dataOut.data_pre.copy()
2829 2828
2830 2829 # if predefinedPhaseShifts != None:
2831 2830 # hardwarePhaseShifts = numpy.array(predefinedPhaseShifts)*numpy.pi/180
2832 2831 #
2833 2832 # # elif beaconPhaseShifts:
2834 2833 # # #get hardware phase shifts using beacon signal
2835 2834 # # hardwarePhaseShifts = self.__getHardwarePhaseDiff(self.dataOut.data_pre, pairslist, newheis, 10)
2836 2835 # # hardwarePhaseShifts = numpy.insert(hardwarePhaseShifts,centerReceiverIndex,0)
2837 2836 #
2838 2837 # else:
2839 2838 # hardwarePhaseShifts = numpy.zeros(5)
2840 2839 #
2841 2840 # voltsPShift = numpy.zeros((self.dataOut.data_pre.shape[0],self.dataOut.data_pre.shape[1],self.dataOut.data_pre.shape[2]), dtype = 'complex')
2842 2841 # for i in range(self.dataOut.data_pre.shape[0]):
2843 2842 # voltsPShift[i,:,:] = self.__shiftPhase(self.dataOut.data_pre[i,:,:], hardwarePhaseShifts[i])
2844 2843
2845 2844 #******************END OF REMOVING HARDWARE PHASE DIFFERENCES*********
2846 2845
2847 2846 #Remove DC
2848 2847 voltsDC = numpy.mean(voltsPShift,1)
2849 2848 voltsDC = numpy.mean(voltsDC,1)
2850 2849 for i in range(voltsDC.shape[0]):
2851 2850 voltsPShift[i] = voltsPShift[i] - voltsDC[i]
2852 2851
2853 2852 #Don't considerate last heights, theyre used to calculate Hardware Phase Shift
2854 2853 # voltsPShift = voltsPShift[:,:,:newheis[0][0]]
2855 2854
2856 2855 #************ FIND POWER OF DATA W/COH OR NON COH DETECTION (3.4) **********
2857 2856 #Coherent Detection
2858 2857 if cohDetection:
2859 2858 #use coherent detection to get the net power
2860 2859 cohDet_thresh = cohDet_thresh*numpy.pi/180
2861 2860 voltsPShift = self.__coherentDetection(voltsPShift, cohDet_timeStep, dataOut.timeInterval, pairslist0, cohDet_thresh)
2862 2861
2863 2862 #Non-coherent detection!
2864 2863 powerNet = numpy.nansum(numpy.abs(voltsPShift[:,:,:])**2,0)
2865 2864 #********** END OF COH/NON-COH POWER CALCULATION**********************
2866 2865
2867 2866 #********** FIND THE NOISE LEVEL AND POSSIBLE METEORS ****************
2868 2867 #Get noise
2869 2868 noise, noise1 = self.__getNoise(powerNet, noise_timeStep, dataOut.timeInterval)
2870 2869 # noise = self.getNoise1(powerNet, noise_timeStep, self.dataOut.timeInterval)
2871 2870 #Get signal threshold
2872 2871 signalThresh = noise_multiple*noise
2873 2872 #Meteor echoes detection
2874 2873 listMeteors = self.__findMeteors(powerNet, signalThresh)
2875 2874 #******* END OF NOISE LEVEL AND POSSIBLE METEORS CACULATION **********
2876 2875
2877 2876 #************** REMOVE MULTIPLE DETECTIONS (3.5) ***************************
2878 2877 #Parameters
2879 2878 heiRange = dataOut.getHeiRange()
2880 2879 rangeInterval = heiRange[1] - heiRange[0]
2881 2880 rangeLimit = multDet_rangeLimit/rangeInterval
2882 2881 timeLimit = multDet_timeLimit/dataOut.timeInterval
2883 2882 #Multiple detection removals
2884 2883 listMeteors1 = self.__removeMultipleDetections(listMeteors, rangeLimit, timeLimit)
2885 2884 #************ END OF REMOVE MULTIPLE DETECTIONS **********************
2886 2885
2887 2886 #********************* METEOR REESTIMATION (3.7, 3.8, 3.9, 3.10) ********************
2888 2887 #Parameters
2889 2888 phaseThresh = phaseThresh*numpy.pi/180
2890 2889 thresh = [phaseThresh, noise_multiple, SNRThresh]
2891 2890 #Meteor reestimation (Errors N 1, 6, 12, 17)
2892 2891 listMeteors2, listMeteorsPower, listMeteorsVolts = self.__meteorReestimation(listMeteors1, voltsPShift, pairslist0, thresh, noise, dataOut.timeInterval, dataOut.frequency)
2893 2892 # listMeteors2, listMeteorsPower, listMeteorsVolts = self.meteorReestimation3(listMeteors2, listMeteorsPower, listMeteorsVolts, voltsPShift, pairslist, thresh, noise)
2894 2893 #Estimation of decay times (Errors N 7, 8, 11)
2895 2894 listMeteors3 = self.__estimateDecayTime(listMeteors2, listMeteorsPower, dataOut.timeInterval, dataOut.frequency)
2896 2895 #******************* END OF METEOR REESTIMATION *******************
2897 2896
2898 2897 #********************* METEOR PARAMETERS CALCULATION (3.11, 3.12, 3.13) **************************
2899 2898 #Calculating Radial Velocity (Error N 15)
2900 2899 radialStdThresh = 10
2901 2900 listMeteors4 = self.__getRadialVelocity(listMeteors3, listMeteorsVolts, radialStdThresh, pairslist0, dataOut.timeInterval)
2902 2901
2903 2902 if len(listMeteors4) > 0:
2904 2903 #Setting New Array
2905 2904 date = dataOut.utctime
2906 2905 arrayParameters = self.__setNewArrays(listMeteors4, date, heiRang)
2907 2906
2908 2907 #Correcting phase offset
2909 2908 if phaseOffsets != None:
2910 2909 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
2911 2910 arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
2912 2911
2913 2912 #Second Pairslist
2914 2913 pairsList = []
2915 2914 pairx = (0,1)
2916 2915 pairy = (2,3)
2917 2916 pairsList.append(pairx)
2918 2917 pairsList.append(pairy)
2919 2918
2920 2919 jph = numpy.array([0,0,0,0])
2921 2920 h = (hmin,hmax)
2922 2921 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
2923 2922
2924 2923 # #Calculate AOA (Error N 3, 4)
2925 2924 # #JONES ET AL. 1998
2926 2925 # error = arrayParameters[:,-1]
2927 2926 # AOAthresh = numpy.pi/8
2928 2927 # phases = -arrayParameters[:,9:13]
2929 2928 # arrayParameters[:,4:7], arrayParameters[:,-1] = meteorOps.getAOA(phases, pairsList, error, AOAthresh, azimuth)
2930 2929 #
2931 2930 # #Calculate Heights (Error N 13 and 14)
2932 2931 # error = arrayParameters[:,-1]
2933 2932 # Ranges = arrayParameters[:,2]
2934 2933 # zenith = arrayParameters[:,5]
2935 2934 # arrayParameters[:,3], arrayParameters[:,-1] = meteorOps.getHeights(Ranges, zenith, error, hmin, hmax)
2936 2935 # error = arrayParameters[:,-1]
2937 2936 #********************* END OF PARAMETERS CALCULATION **************************
2938 2937
2939 2938 #***************************+ PASS DATA TO NEXT STEP **********************
2940 2939 # arrayFinal = arrayParameters.reshape((1,arrayParameters.shape[0],arrayParameters.shape[1]))
2941 2940 dataOut.data_param = arrayParameters
2942 2941
2943 2942 if arrayParameters == None:
2944 2943 dataOut.flagNoData = True
2945 2944 else:
2946 2945 dataOut.flagNoData = True
2947 2946
2948 2947 return
2949 2948
2950 2949 def __getHardwarePhaseDiff(self, voltage0, pairslist, newheis, n):
2951 2950
2952 2951 minIndex = min(newheis[0])
2953 2952 maxIndex = max(newheis[0])
2954 2953
2955 2954 voltage = voltage0[:,:,minIndex:maxIndex+1]
2956 2955 nLength = voltage.shape[1]/n
2957 2956 nMin = 0
2958 2957 nMax = 0
2959 2958 phaseOffset = numpy.zeros((len(pairslist),n))
2960 2959
2961 2960 for i in range(n):
2962 2961 nMax += nLength
2963 2962 phaseCCF = -numpy.angle(self.__calculateCCF(voltage[:,nMin:nMax,:], pairslist, [0]))
2964 2963 phaseCCF = numpy.mean(phaseCCF, axis = 2)
2965 2964 phaseOffset[:,i] = phaseCCF.transpose()
2966 2965 nMin = nMax
2967 2966 # phaseDiff, phaseArrival = self.estimatePhaseDifference(voltage, pairslist)
2968 2967
2969 2968 #Remove Outliers
2970 2969 factor = 2
2971 2970 wt = phaseOffset - signal.medfilt(phaseOffset,(1,5))
2972 2971 dw = numpy.std(wt,axis = 1)
2973 2972 dw = dw.reshape((dw.size,1))
2974 2973 ind = numpy.where(numpy.logical_or(wt>dw*factor,wt<-dw*factor))
2975 2974 phaseOffset[ind] = numpy.nan
2976 2975 phaseOffset = stats.nanmean(phaseOffset, axis=1)
2977 2976
2978 2977 return phaseOffset
2979 2978
2980 2979 def __shiftPhase(self, data, phaseShift):
2981 2980 #this will shift the phase of a complex number
2982 2981 dataShifted = numpy.abs(data) * numpy.exp((numpy.angle(data)+phaseShift)*1j)
2983 2982 return dataShifted
2984 2983
2985 2984 def __estimatePhaseDifference(self, array, pairslist):
2986 2985 nChannel = array.shape[0]
2987 2986 nHeights = array.shape[2]
2988 2987 numPairs = len(pairslist)
2989 2988 # phaseCCF = numpy.zeros((nChannel, 5, nHeights))
2990 2989 phaseCCF = numpy.angle(self.__calculateCCF(array, pairslist, [-2,-1,0,1,2]))
2991 2990
2992 2991 #Correct phases
2993 2992 derPhaseCCF = phaseCCF[:,1:,:] - phaseCCF[:,0:-1,:]
2994 2993 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
2995 2994
2996 2995 if indDer[0].shape[0] > 0:
2997 2996 for i in range(indDer[0].shape[0]):
2998 2997 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i],indDer[2][i]])
2999 2998 phaseCCF[indDer[0][i],indDer[1][i]+1:,:] += signo*2*numpy.pi
3000 2999
3001 3000 # for j in range(numSides):
3002 3001 # phaseCCFAux = self.calculateCCF(arrayCenter, arraySides[j,:,:], [-2,1,0,1,2])
3003 3002 # phaseCCF[j,:,:] = numpy.angle(phaseCCFAux)
3004 3003 #
3005 3004 #Linear
3006 3005 phaseInt = numpy.zeros((numPairs,1))
3007 3006 angAllCCF = phaseCCF[:,[0,1,3,4],0]
3008 3007 for j in range(numPairs):
3009 3008 fit = stats.linregress([-2,-1,1,2],angAllCCF[j,:])
3010 3009 phaseInt[j] = fit[1]
3011 3010 #Phase Differences
3012 3011 phaseDiff = phaseInt - phaseCCF[:,2,:]
3013 3012 phaseArrival = phaseInt.reshape(phaseInt.size)
3014 3013
3015 3014 #Dealias
3016 3015 phaseArrival = numpy.angle(numpy.exp(1j*phaseArrival))
3017 3016 # indAlias = numpy.where(phaseArrival > numpy.pi)
3018 3017 # phaseArrival[indAlias] -= 2*numpy.pi
3019 3018 # indAlias = numpy.where(phaseArrival < -numpy.pi)
3020 3019 # phaseArrival[indAlias] += 2*numpy.pi
3021 3020
3022 3021 return phaseDiff, phaseArrival
3023 3022
3024 3023 def __coherentDetection(self, volts, timeSegment, timeInterval, pairslist, thresh):
3025 3024 #this function will run the coherent detection used in Holdworth et al. 2004 and return the net power
3026 3025 #find the phase shifts of each channel over 1 second intervals
3027 3026 #only look at ranges below the beacon signal
3028 3027 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
3029 3028 numBlocks = int(volts.shape[1]/numProfPerBlock)
3030 3029 numHeights = volts.shape[2]
3031 3030 nChannel = volts.shape[0]
3032 3031 voltsCohDet = volts.copy()
3033 3032
3034 3033 pairsarray = numpy.array(pairslist)
3035 3034 indSides = pairsarray[:,1]
3036 3035 # indSides = numpy.array(range(nChannel))
3037 3036 # indSides = numpy.delete(indSides, indCenter)
3038 3037 #
3039 3038 # listCenter = numpy.array_split(volts[indCenter,:,:], numBlocks, 0)
3040 3039 listBlocks = numpy.array_split(volts, numBlocks, 1)
3041 3040
3042 3041 startInd = 0
3043 3042 endInd = 0
3044 3043
3045 3044 for i in range(numBlocks):
3046 3045 startInd = endInd
3047 3046 endInd = endInd + listBlocks[i].shape[1]
3048 3047
3049 3048 arrayBlock = listBlocks[i]
3050 3049 # arrayBlockCenter = listCenter[i]
3051 3050
3052 3051 #Estimate the Phase Difference
3053 3052 phaseDiff, aux = self.__estimatePhaseDifference(arrayBlock, pairslist)
3054 3053 #Phase Difference RMS
3055 3054 arrayPhaseRMS = numpy.abs(phaseDiff)
3056 3055 phaseRMSaux = numpy.sum(arrayPhaseRMS < thresh,0)
3057 3056 indPhase = numpy.where(phaseRMSaux==4)
3058 3057 #Shifting
3059 3058 if indPhase[0].shape[0] > 0:
3060 3059 for j in range(indSides.size):
3061 3060 arrayBlock[indSides[j],:,indPhase] = self.__shiftPhase(arrayBlock[indSides[j],:,indPhase], phaseDiff[j,indPhase].transpose())
3062 3061 voltsCohDet[:,startInd:endInd,:] = arrayBlock
3063 3062
3064 3063 return voltsCohDet
3065 3064
3066 3065 def __calculateCCF(self, volts, pairslist ,laglist):
3067 3066
3068 3067 nHeights = volts.shape[2]
3069 3068 nPoints = volts.shape[1]
3070 3069 voltsCCF = numpy.zeros((len(pairslist), len(laglist), nHeights),dtype = 'complex')
3071 3070
3072 3071 for i in range(len(pairslist)):
3073 3072 volts1 = volts[pairslist[i][0]]
3074 3073 volts2 = volts[pairslist[i][1]]
3075 3074
3076 3075 for t in range(len(laglist)):
3077 3076 idxT = laglist[t]
3078 3077 if idxT >= 0:
3079 3078 vStacked = numpy.vstack((volts2[idxT:,:],
3080 3079 numpy.zeros((idxT, nHeights),dtype='complex')))
3081 3080 else:
3082 3081 vStacked = numpy.vstack((numpy.zeros((-idxT, nHeights),dtype='complex'),
3083 3082 volts2[:(nPoints + idxT),:]))
3084 3083 voltsCCF[i,t,:] = numpy.sum((numpy.conjugate(volts1)*vStacked),axis=0)
3085 3084
3086 3085 vStacked = None
3087 3086 return voltsCCF
3088 3087
3089 3088 def __getNoise(self, power, timeSegment, timeInterval):
3090 3089 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
3091 3090 numBlocks = int(power.shape[0]/numProfPerBlock)
3092 3091 numHeights = power.shape[1]
3093 3092
3094 3093 listPower = numpy.array_split(power, numBlocks, 0)
3095 3094 noise = numpy.zeros((power.shape[0], power.shape[1]))
3096 3095 noise1 = numpy.zeros((power.shape[0], power.shape[1]))
3097 3096
3098 3097 startInd = 0
3099 3098 endInd = 0
3100 3099
3101 3100 for i in range(numBlocks): #split por canal
3102 3101 startInd = endInd
3103 3102 endInd = endInd + listPower[i].shape[0]
3104 3103
3105 3104 arrayBlock = listPower[i]
3106 3105 noiseAux = numpy.mean(arrayBlock, 0)
3107 3106 # noiseAux = numpy.median(noiseAux)
3108 3107 # noiseAux = numpy.mean(arrayBlock)
3109 3108 noise[startInd:endInd,:] = noise[startInd:endInd,:] + noiseAux
3110 3109
3111 3110 noiseAux1 = numpy.mean(arrayBlock)
3112 3111 noise1[startInd:endInd,:] = noise1[startInd:endInd,:] + noiseAux1
3113 3112
3114 3113 return noise, noise1
3115 3114
3116 3115 def __findMeteors(self, power, thresh):
3117 3116 nProf = power.shape[0]
3118 3117 nHeights = power.shape[1]
3119 3118 listMeteors = []
3120 3119
3121 3120 for i in range(nHeights):
3122 3121 powerAux = power[:,i]
3123 3122 threshAux = thresh[:,i]
3124 3123
3125 3124 indUPthresh = numpy.where(powerAux > threshAux)[0]
3126 3125 indDNthresh = numpy.where(powerAux <= threshAux)[0]
3127 3126
3128 3127 j = 0
3129 3128
3130 3129 while (j < indUPthresh.size - 2):
3131 3130 if (indUPthresh[j + 2] == indUPthresh[j] + 2):
3132 3131 indDNAux = numpy.where(indDNthresh > indUPthresh[j])
3133 3132 indDNthresh = indDNthresh[indDNAux]
3134 3133
3135 3134 if (indDNthresh.size > 0):
3136 3135 indEnd = indDNthresh[0] - 1
3137 3136 indInit = indUPthresh[j]
3138 3137
3139 3138 meteor = powerAux[indInit:indEnd + 1]
3140 3139 indPeak = meteor.argmax() + indInit
3141 3140 FLA = sum(numpy.conj(meteor)*numpy.hstack((meteor[1:],0)))
3142 3141
3143 3142 listMeteors.append(numpy.array([i,indInit,indPeak,indEnd,FLA])) #CHEQUEAR!!!!!
3144 3143 j = numpy.where(indUPthresh == indEnd)[0] + 1
3145 3144 else: j+=1
3146 3145 else: j+=1
3147 3146
3148 3147 return listMeteors
3149 3148
3150 3149 def __removeMultipleDetections(self,listMeteors, rangeLimit, timeLimit):
3151 3150
3152 3151 arrayMeteors = numpy.asarray(listMeteors)
3153 3152 listMeteors1 = []
3154 3153
3155 3154 while arrayMeteors.shape[0] > 0:
3156 3155 FLAs = arrayMeteors[:,4]
3157 3156 maxFLA = FLAs.argmax()
3158 3157 listMeteors1.append(arrayMeteors[maxFLA,:])
3159 3158
3160 3159 MeteorInitTime = arrayMeteors[maxFLA,1]
3161 3160 MeteorEndTime = arrayMeteors[maxFLA,3]
3162 3161 MeteorHeight = arrayMeteors[maxFLA,0]
3163 3162
3164 3163 #Check neighborhood
3165 3164 maxHeightIndex = MeteorHeight + rangeLimit
3166 3165 minHeightIndex = MeteorHeight - rangeLimit
3167 3166 minTimeIndex = MeteorInitTime - timeLimit
3168 3167 maxTimeIndex = MeteorEndTime + timeLimit
3169 3168
3170 3169 #Check Heights
3171 3170 indHeight = numpy.logical_and(arrayMeteors[:,0] >= minHeightIndex, arrayMeteors[:,0] <= maxHeightIndex)
3172 3171 indTime = numpy.logical_and(arrayMeteors[:,3] >= minTimeIndex, arrayMeteors[:,1] <= maxTimeIndex)
3173 3172 indBoth = numpy.where(numpy.logical_and(indTime,indHeight))
3174 3173
3175 3174 arrayMeteors = numpy.delete(arrayMeteors, indBoth, axis = 0)
3176 3175
3177 3176 return listMeteors1
3178 3177
3179 3178 def __meteorReestimation(self, listMeteors, volts, pairslist, thresh, noise, timeInterval,frequency):
3180 3179 numHeights = volts.shape[2]
3181 3180 nChannel = volts.shape[0]
3182 3181
3183 3182 thresholdPhase = thresh[0]
3184 3183 thresholdNoise = thresh[1]
3185 3184 thresholdDB = float(thresh[2])
3186 3185
3187 3186 thresholdDB1 = 10**(thresholdDB/10)
3188 3187 pairsarray = numpy.array(pairslist)
3189 3188 indSides = pairsarray[:,1]
3190 3189
3191 3190 pairslist1 = list(pairslist)
3192 3191 pairslist1.append((0,1))
3193 3192 pairslist1.append((3,4))
3194 3193
3195 3194 listMeteors1 = []
3196 3195 listPowerSeries = []
3197 3196 listVoltageSeries = []
3198 3197 #volts has the war data
3199 3198
3200 3199 if frequency == 30e6:
3201 3200 timeLag = 45*10**-3
3202 3201 else:
3203 3202 timeLag = 15*10**-3
3204 3203 lag = numpy.ceil(timeLag/timeInterval)
3205 3204
3206 3205 for i in range(len(listMeteors)):
3207 3206
3208 3207 ###################### 3.6 - 3.7 PARAMETERS REESTIMATION #########################
3209 3208 meteorAux = numpy.zeros(16)
3210 3209
3211 3210 #Loading meteor Data (mHeight, mStart, mPeak, mEnd)
3212 3211 mHeight = listMeteors[i][0]
3213 3212 mStart = listMeteors[i][1]
3214 3213 mPeak = listMeteors[i][2]
3215 3214 mEnd = listMeteors[i][3]
3216 3215
3217 3216 #get the volt data between the start and end times of the meteor
3218 3217 meteorVolts = volts[:,mStart:mEnd+1,mHeight]
3219 3218 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3220 3219
3221 3220 #3.6. Phase Difference estimation
3222 3221 phaseDiff, aux = self.__estimatePhaseDifference(meteorVolts, pairslist)
3223 3222
3224 3223 #3.7. Phase difference removal & meteor start, peak and end times reestimated
3225 3224 #meteorVolts0.- all Channels, all Profiles
3226 3225 meteorVolts0 = volts[:,:,mHeight]
3227 3226 meteorThresh = noise[:,mHeight]*thresholdNoise
3228 3227 meteorNoise = noise[:,mHeight]
3229 3228 meteorVolts0[indSides,:] = self.__shiftPhase(meteorVolts0[indSides,:], phaseDiff) #Phase Shifting
3230 3229 powerNet0 = numpy.nansum(numpy.abs(meteorVolts0)**2, axis = 0) #Power
3231 3230
3232 3231 #Times reestimation
3233 3232 mStart1 = numpy.where(powerNet0[:mPeak] < meteorThresh[:mPeak])[0]
3234 3233 if mStart1.size > 0:
3235 3234 mStart1 = mStart1[-1] + 1
3236 3235
3237 3236 else:
3238 3237 mStart1 = mPeak
3239 3238
3240 3239 mEnd1 = numpy.where(powerNet0[mPeak:] < meteorThresh[mPeak:])[0][0] + mPeak - 1
3241 3240 mEndDecayTime1 = numpy.where(powerNet0[mPeak:] < meteorNoise[mPeak:])[0]
3242 3241 if mEndDecayTime1.size == 0:
3243 3242 mEndDecayTime1 = powerNet0.size
3244 3243 else:
3245 3244 mEndDecayTime1 = mEndDecayTime1[0] + mPeak - 1
3246 3245 # mPeak1 = meteorVolts0[mStart1:mEnd1 + 1].argmax()
3247 3246
3248 3247 #meteorVolts1.- all Channels, from start to end
3249 3248 meteorVolts1 = meteorVolts0[:,mStart1:mEnd1 + 1]
3250 3249 meteorVolts2 = meteorVolts0[:,mPeak + lag:mEnd1 + 1]
3251 3250 if meteorVolts2.shape[1] == 0:
3252 3251 meteorVolts2 = meteorVolts0[:,mPeak:mEnd1 + 1]
3253 3252 meteorVolts1 = meteorVolts1.reshape(meteorVolts1.shape[0], meteorVolts1.shape[1], 1)
3254 3253 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1], 1)
3255 3254 ##################### END PARAMETERS REESTIMATION #########################
3256 3255
3257 3256 ##################### 3.8 PHASE DIFFERENCE REESTIMATION ########################
3258 3257 # if mEnd1 - mStart1 > 4: #Error Number 6: echo less than 5 samples long; too short for analysis
3259 3258 if meteorVolts2.shape[1] > 0:
3260 3259 #Phase Difference re-estimation
3261 3260 phaseDiff1, phaseDiffint = self.__estimatePhaseDifference(meteorVolts2, pairslist1) #Phase Difference Estimation
3262 3261 # phaseDiff1, phaseDiffint = self.estimatePhaseDifference(meteorVolts2, pairslist)
3263 3262 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1])
3264 3263 phaseDiff11 = numpy.reshape(phaseDiff1, (phaseDiff1.shape[0],1))
3265 3264 meteorVolts2[indSides,:] = self.__shiftPhase(meteorVolts2[indSides,:], phaseDiff11[0:4]) #Phase Shifting
3266 3265
3267 3266 #Phase Difference RMS
3268 3267 phaseRMS1 = numpy.sqrt(numpy.mean(numpy.square(phaseDiff1)))
3269 3268 powerNet1 = numpy.nansum(numpy.abs(meteorVolts1[:,:])**2,0)
3270 3269 #Data from Meteor
3271 3270 mPeak1 = powerNet1.argmax() + mStart1
3272 3271 mPeakPower1 = powerNet1.max()
3273 3272 noiseAux = sum(noise[mStart1:mEnd1 + 1,mHeight])
3274 3273 mSNR1 = (sum(powerNet1)-noiseAux)/noiseAux
3275 3274 Meteor1 = numpy.array([mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1])
3276 3275 Meteor1 = numpy.hstack((Meteor1,phaseDiffint))
3277 3276 PowerSeries = powerNet0[mStart1:mEndDecayTime1 + 1]
3278 3277 #Vectorize
3279 3278 meteorAux[0:7] = [mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1]
3280 3279 meteorAux[7:11] = phaseDiffint[0:4]
3281 3280
3282 3281 #Rejection Criterions
3283 3282 if phaseRMS1 > thresholdPhase: #Error Number 17: Phase variation
3284 3283 meteorAux[-1] = 17
3285 3284 elif mSNR1 < thresholdDB1: #Error Number 1: SNR < threshold dB
3286 3285 meteorAux[-1] = 1
3287 3286
3288 3287
3289 3288 else:
3290 3289 meteorAux[0:4] = [mHeight, mStart, mPeak, mEnd]
3291 3290 meteorAux[-1] = 6 #Error Number 6: echo less than 5 samples long; too short for analysis
3292 3291 PowerSeries = 0
3293 3292
3294 3293 listMeteors1.append(meteorAux)
3295 3294 listPowerSeries.append(PowerSeries)
3296 3295 listVoltageSeries.append(meteorVolts1)
3297 3296
3298 3297 return listMeteors1, listPowerSeries, listVoltageSeries
3299 3298
3300 3299 def __estimateDecayTime(self, listMeteors, listPower, timeInterval, frequency):
3301 3300
3302 3301 threshError = 10
3303 3302 #Depending if it is 30 or 50 MHz
3304 3303 if frequency == 30e6:
3305 3304 timeLag = 45*10**-3
3306 3305 else:
3307 3306 timeLag = 15*10**-3
3308 3307 lag = numpy.ceil(timeLag/timeInterval)
3309 3308
3310 3309 listMeteors1 = []
3311 3310
3312 3311 for i in range(len(listMeteors)):
3313 3312 meteorPower = listPower[i]
3314 3313 meteorAux = listMeteors[i]
3315 3314
3316 3315 if meteorAux[-1] == 0:
3317 3316
3318 3317 try:
3319 3318 indmax = meteorPower.argmax()
3320 3319 indlag = indmax + lag
3321 3320
3322 3321 y = meteorPower[indlag:]
3323 3322 x = numpy.arange(0, y.size)*timeLag
3324 3323
3325 3324 #first guess
3326 3325 a = y[0]
3327 3326 tau = timeLag
3328 3327 #exponential fit
3329 3328 popt, pcov = optimize.curve_fit(self.__exponential_function, x, y, p0 = [a, tau])
3330 3329 y1 = self.__exponential_function(x, *popt)
3331 3330 #error estimation
3332 3331 error = sum((y - y1)**2)/(numpy.var(y)*(y.size - popt.size))
3333 3332
3334 3333 decayTime = popt[1]
3335 3334 riseTime = indmax*timeInterval
3336 3335 meteorAux[11:13] = [decayTime, error]
3337 3336
3338 3337 #Table items 7, 8 and 11
3339 3338 if (riseTime > 0.3): #Number 7: Echo rise exceeds 0.3s
3340 3339 meteorAux[-1] = 7
3341 3340 elif (decayTime < 2*riseTime) : #Number 8: Echo decay time less than than twice rise time
3342 3341 meteorAux[-1] = 8
3343 3342 if (error > threshError): #Number 11: Poor fit to amplitude for estimation of decay time
3344 3343 meteorAux[-1] = 11
3345 3344
3346 3345
3347 3346 except:
3348 3347 meteorAux[-1] = 11
3349 3348
3350 3349
3351 3350 listMeteors1.append(meteorAux)
3352 3351
3353 3352 return listMeteors1
3354 3353
3355 3354 #Exponential Function
3356 3355
3357 3356 def __exponential_function(self, x, a, tau):
3358 3357 y = a*numpy.exp(-x/tau)
3359 3358 return y
3360 3359
3361 3360 def __getRadialVelocity(self, listMeteors, listVolts, radialStdThresh, pairslist, timeInterval):
3362 3361
3363 3362 pairslist1 = list(pairslist)
3364 3363 pairslist1.append((0,1))
3365 3364 pairslist1.append((3,4))
3366 3365 numPairs = len(pairslist1)
3367 3366 #Time Lag
3368 3367 timeLag = 45*10**-3
3369 3368 c = 3e8
3370 3369 lag = numpy.ceil(timeLag/timeInterval)
3371 3370 freq = 30e6
3372 3371
3373 3372 listMeteors1 = []
3374 3373
3375 3374 for i in range(len(listMeteors)):
3376 3375 meteorAux = listMeteors[i]
3377 3376 if meteorAux[-1] == 0:
3378 3377 mStart = listMeteors[i][1]
3379 3378 mPeak = listMeteors[i][2]
3380 3379 mLag = mPeak - mStart + lag
3381 3380
3382 3381 #get the volt data between the start and end times of the meteor
3383 3382 meteorVolts = listVolts[i]
3384 3383 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3385 3384
3386 3385 #Get CCF
3387 3386 allCCFs = self.__calculateCCF(meteorVolts, pairslist1, [-2,-1,0,1,2])
3388 3387
3389 3388 #Method 2
3390 3389 slopes = numpy.zeros(numPairs)
3391 3390 time = numpy.array([-2,-1,1,2])*timeInterval
3392 3391 angAllCCF = numpy.angle(allCCFs[:,[0,1,3,4],0])
3393 3392
3394 3393 #Correct phases
3395 3394 derPhaseCCF = angAllCCF[:,1:] - angAllCCF[:,0:-1]
3396 3395 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
3397 3396
3398 3397 if indDer[0].shape[0] > 0:
3399 3398 for i in range(indDer[0].shape[0]):
3400 3399 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i]])
3401 3400 angAllCCF[indDer[0][i],indDer[1][i]+1:] += signo*2*numpy.pi
3402 3401
3403 3402 # fit = scipy.stats.linregress(numpy.array([-2,-1,1,2])*timeInterval, numpy.array([phaseLagN2s[i],phaseLagN1s[i],phaseLag1s[i],phaseLag2s[i]]))
3404 3403 for j in range(numPairs):
3405 3404 fit = stats.linregress(time, angAllCCF[j,:])
3406 3405 slopes[j] = fit[0]
3407 3406
3408 3407 #Remove Outlier
3409 3408 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3410 3409 # slopes = numpy.delete(slopes,indOut)
3411 3410 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3412 3411 # slopes = numpy.delete(slopes,indOut)
3413 3412
3414 3413 radialVelocity = -numpy.mean(slopes)*(0.25/numpy.pi)*(c/freq)
3415 3414 radialError = numpy.std(slopes)*(0.25/numpy.pi)*(c/freq)
3416 3415 meteorAux[-2] = radialError
3417 3416 meteorAux[-3] = radialVelocity
3418 3417
3419 3418 #Setting Error
3420 3419 #Number 15: Radial Drift velocity or projected horizontal velocity exceeds 200 m/s
3421 3420 if numpy.abs(radialVelocity) > 200:
3422 3421 meteorAux[-1] = 15
3423 3422 #Number 12: Poor fit to CCF variation for estimation of radial drift velocity
3424 3423 elif radialError > radialStdThresh:
3425 3424 meteorAux[-1] = 12
3426 3425
3427 3426 listMeteors1.append(meteorAux)
3428 3427 return listMeteors1
3429 3428
3430 3429 def __setNewArrays(self, listMeteors, date, heiRang):
3431 3430
3432 3431 #New arrays
3433 3432 arrayMeteors = numpy.array(listMeteors)
3434 3433 arrayParameters = numpy.zeros((len(listMeteors), 13))
3435 3434
3436 3435 #Date inclusion
3437 3436 # date = re.findall(r'\((.*?)\)', date)
3438 3437 # date = date[0].split(',')
3439 3438 # date = map(int, date)
3440 3439 #
3441 3440 # if len(date)<6:
3442 3441 # date.append(0)
3443 3442 #
3444 3443 # date = [date[0]*10000 + date[1]*100 + date[2], date[3]*10000 + date[4]*100 + date[5]]
3445 3444 # arrayDate = numpy.tile(date, (len(listMeteors), 1))
3446 3445 arrayDate = numpy.tile(date, (len(listMeteors)))
3447 3446
3448 3447 #Meteor array
3449 3448 # arrayMeteors[:,0] = heiRang[arrayMeteors[:,0].astype(int)]
3450 3449 # arrayMeteors = numpy.hstack((arrayDate, arrayMeteors))
3451 3450
3452 3451 #Parameters Array
3453 3452 arrayParameters[:,0] = arrayDate #Date
3454 3453 arrayParameters[:,1] = heiRang[arrayMeteors[:,0].astype(int)] #Range
3455 3454 arrayParameters[:,6:8] = arrayMeteors[:,-3:-1] #Radial velocity and its error
3456 3455 arrayParameters[:,8:12] = arrayMeteors[:,7:11] #Phases
3457 3456 arrayParameters[:,-1] = arrayMeteors[:,-1] #Error
3458 3457
3459 3458
3460 3459 return arrayParameters
3461 3460
3462 3461 class CorrectSMPhases(Operation):
3463 3462
3464 3463 def run(self, dataOut, phaseOffsets, hmin = 50, hmax = 150, azimuth = 45, channelPositions = None):
3465 3464
3466 3465 arrayParameters = dataOut.data_param
3467 3466 pairsList = []
3468 3467 pairx = (0,1)
3469 3468 pairy = (2,3)
3470 3469 pairsList.append(pairx)
3471 3470 pairsList.append(pairy)
3472 3471 jph = numpy.zeros(4)
3473 3472
3474 3473 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
3475 3474 # arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
3476 3475 arrayParameters[:,8:12] = numpy.angle(numpy.exp(1j*(arrayParameters[:,8:12] + phaseOffsets)))
3477 3476
3478 3477 meteorOps = SMOperations()
3479 3478 if channelPositions == None:
3480 3479 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3481 3480 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3482 3481
3483 3482 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3484 3483 h = (hmin,hmax)
3485 3484
3486 3485 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
3487 3486
3488 3487 dataOut.data_param = arrayParameters
3489 3488 return
3490 3489
3491 3490 class SMPhaseCalibration(Operation):
3492 3491
3493 3492 __buffer = None
3494 3493
3495 3494 __initime = None
3496 3495
3497 3496 __dataReady = False
3498 3497
3499 3498 __isConfig = False
3500 3499
3501 3500 def __checkTime(self, currentTime, initTime, paramInterval, outputInterval):
3502 3501
3503 3502 dataTime = currentTime + paramInterval
3504 3503 deltaTime = dataTime - initTime
3505 3504
3506 3505 if deltaTime >= outputInterval or deltaTime < 0:
3507 3506 return True
3508 3507
3509 3508 return False
3510 3509
3511 3510 def __getGammas(self, pairs, d, phases):
3512 3511 gammas = numpy.zeros(2)
3513 3512
3514 3513 for i in range(len(pairs)):
3515 3514
3516 3515 pairi = pairs[i]
3517 3516
3518 3517 phip3 = phases[:,pairi[0]]
3519 3518 d3 = d[pairi[0]]
3520 3519 phip2 = phases[:,pairi[1]]
3521 3520 d2 = d[pairi[1]]
3522 3521 #Calculating gamma
3523 3522 # jdcos = alp1/(k*d1)
3524 3523 # jgamma = numpy.angle(numpy.exp(1j*(d0*alp1/d1 - alp0)))
3525 3524 jgamma = -phip2*d3/d2 - phip3
3526 3525 jgamma = numpy.angle(numpy.exp(1j*jgamma))
3527 3526 # jgamma[jgamma>numpy.pi] -= 2*numpy.pi
3528 3527 # jgamma[jgamma<-numpy.pi] += 2*numpy.pi
3529 3528
3530 3529 #Revised distribution
3531 3530 jgammaArray = numpy.hstack((jgamma,jgamma+0.5*numpy.pi,jgamma-0.5*numpy.pi))
3532 3531
3533 3532 #Histogram
3534 3533 nBins = 64
3535 3534 rmin = -0.5*numpy.pi
3536 3535 rmax = 0.5*numpy.pi
3537 3536 phaseHisto = numpy.histogram(jgammaArray, bins=nBins, range=(rmin,rmax))
3538 3537
3539 3538 meteorsY = phaseHisto[0]
3540 3539 phasesX = phaseHisto[1][:-1]
3541 3540 width = phasesX[1] - phasesX[0]
3542 3541 phasesX += width/2
3543 3542
3544 3543 #Gaussian aproximation
3545 3544 bpeak = meteorsY.argmax()
3546 3545 peak = meteorsY.max()
3547 3546 jmin = bpeak - 5
3548 3547 jmax = bpeak + 5 + 1
3549 3548
3550 3549 if jmin<0:
3551 3550 jmin = 0
3552 3551 jmax = 6
3553 3552 elif jmax > meteorsY.size:
3554 3553 jmin = meteorsY.size - 6
3555 3554 jmax = meteorsY.size
3556 3555
3557 3556 x0 = numpy.array([peak,bpeak,50])
3558 3557 coeff = optimize.leastsq(self.__residualFunction, x0, args=(meteorsY[jmin:jmax], phasesX[jmin:jmax]))
3559 3558
3560 3559 #Gammas
3561 3560 gammas[i] = coeff[0][1]
3562 3561
3563 3562 return gammas
3564 3563
3565 3564 def __residualFunction(self, coeffs, y, t):
3566 3565
3567 3566 return y - self.__gauss_function(t, coeffs)
3568 3567
3569 3568 def __gauss_function(self, t, coeffs):
3570 3569
3571 3570 return coeffs[0]*numpy.exp(-0.5*((t - coeffs[1]) / coeffs[2])**2)
3572 3571
3573 3572 def __getPhases(self, azimuth, h, pairsList, d, gammas, meteorsArray):
3574 3573 meteorOps = SMOperations()
3575 3574 nchan = 4
3576 3575 pairx = pairsList[0] #x es 0
3577 3576 pairy = pairsList[1] #y es 1
3578 3577 center_xangle = 0
3579 3578 center_yangle = 0
3580 3579 range_angle = numpy.array([10*numpy.pi,numpy.pi,numpy.pi/2,numpy.pi/4])
3581 3580 ntimes = len(range_angle)
3582 3581
3583 3582 nstepsx = 20
3584 3583 nstepsy = 20
3585 3584
3586 3585 for iz in range(ntimes):
3587 3586 min_xangle = -range_angle[iz]/2 + center_xangle
3588 3587 max_xangle = range_angle[iz]/2 + center_xangle
3589 3588 min_yangle = -range_angle[iz]/2 + center_yangle
3590 3589 max_yangle = range_angle[iz]/2 + center_yangle
3591 3590
3592 3591 inc_x = (max_xangle-min_xangle)/nstepsx
3593 3592 inc_y = (max_yangle-min_yangle)/nstepsy
3594 3593
3595 3594 alpha_y = numpy.arange(nstepsy)*inc_y + min_yangle
3596 3595 alpha_x = numpy.arange(nstepsx)*inc_x + min_xangle
3597 3596 penalty = numpy.zeros((nstepsx,nstepsy))
3598 3597 jph_array = numpy.zeros((nchan,nstepsx,nstepsy))
3599 3598 jph = numpy.zeros(nchan)
3600 3599
3601 3600 # Iterations looking for the offset
3602 3601 for iy in range(int(nstepsy)):
3603 3602 for ix in range(int(nstepsx)):
3604 3603 d3 = d[pairsList[1][0]]
3605 3604 d2 = d[pairsList[1][1]]
3606 3605 d5 = d[pairsList[0][0]]
3607 3606 d4 = d[pairsList[0][1]]
3608 3607
3609 3608 alp2 = alpha_y[iy] #gamma 1
3610 3609 alp4 = alpha_x[ix] #gamma 0
3611 3610
3612 3611 alp3 = -alp2*d3/d2 - gammas[1]
3613 3612 alp5 = -alp4*d5/d4 - gammas[0]
3614 3613 # jph[pairy[1]] = alpha_y[iy]
3615 3614 # jph[pairy[0]] = -gammas[1] - alpha_y[iy]*d[pairy[1]]/d[pairy[0]]
3616 3615
3617 3616 # jph[pairx[1]] = alpha_x[ix]
3618 3617 # jph[pairx[0]] = -gammas[0] - alpha_x[ix]*d[pairx[1]]/d[pairx[0]]
3619 3618 jph[pairsList[0][1]] = alp4
3620 3619 jph[pairsList[0][0]] = alp5
3621 3620 jph[pairsList[1][0]] = alp3
3622 3621 jph[pairsList[1][1]] = alp2
3623 3622 jph_array[:,ix,iy] = jph
3624 3623 # d = [2.0,2.5,2.5,2.0]
3625 3624 #falta chequear si va a leer bien los meteoros
3626 3625 meteorsArray1 = meteorOps.getMeteorParams(meteorsArray, azimuth, h, pairsList, d, jph)
3627 3626 error = meteorsArray1[:,-1]
3628 3627 ind1 = numpy.where(error==0)[0]
3629 3628 penalty[ix,iy] = ind1.size
3630 3629
3631 3630 i,j = numpy.unravel_index(penalty.argmax(), penalty.shape)
3632 3631 phOffset = jph_array[:,i,j]
3633 3632
3634 3633 center_xangle = phOffset[pairx[1]]
3635 3634 center_yangle = phOffset[pairy[1]]
3636 3635
3637 3636 phOffset = numpy.angle(numpy.exp(1j*jph_array[:,i,j]))
3638 3637 phOffset = phOffset*180/numpy.pi
3639 3638 return phOffset
3640 3639
3641 3640
3642 3641 def run(self, dataOut, hmin, hmax, channelPositions=None, nHours = 1):
3643 3642
3644 3643 dataOut.flagNoData = True
3645 3644 self.__dataReady = False
3646 3645 dataOut.outputInterval = nHours*3600
3647 3646
3648 3647 if self.__isConfig == False:
3649 3648 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
3650 3649 #Get Initial LTC time
3651 3650 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
3652 3651 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
3653 3652
3654 3653 self.__isConfig = True
3655 3654
3656 3655 if self.__buffer == None:
3657 3656 self.__buffer = dataOut.data_param.copy()
3658 3657
3659 3658 else:
3660 3659 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
3661 3660
3662 3661 self.__dataReady = self.__checkTime(dataOut.utctime, self.__initime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
3663 3662
3664 3663 if self.__dataReady:
3665 3664 dataOut.utctimeInit = self.__initime
3666 3665 self.__initime += dataOut.outputInterval #to erase time offset
3667 3666
3668 3667 freq = dataOut.frequency
3669 3668 c = dataOut.C #m/s
3670 3669 lamb = c/freq
3671 3670 k = 2*numpy.pi/lamb
3672 3671 azimuth = 0
3673 3672 h = (hmin, hmax)
3674 3673 # pairs = ((0,1),(2,3)) #Estrella
3675 3674 # pairs = ((1,0),(2,3)) #T
3676 3675
3677 3676 if channelPositions is None:
3678 3677 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3679 3678 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3680 3679 meteorOps = SMOperations()
3681 3680 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3682 3681
3683 3682 #Checking correct order of pairs
3684 3683 pairs = []
3685 3684 if distances[1] > distances[0]:
3686 3685 pairs.append((1,0))
3687 3686 else:
3688 3687 pairs.append((0,1))
3689 3688
3690 3689 if distances[3] > distances[2]:
3691 3690 pairs.append((3,2))
3692 3691 else:
3693 3692 pairs.append((2,3))
3694 3693 # distances1 = [-distances[0]*lamb, distances[1]*lamb, -distances[2]*lamb, distances[3]*lamb]
3695 3694
3696 3695 meteorsArray = self.__buffer
3697 3696 error = meteorsArray[:,-1]
3698 3697 boolError = (error==0)|(error==3)|(error==4)|(error==13)|(error==14)
3699 3698 ind1 = numpy.where(boolError)[0]
3700 3699 meteorsArray = meteorsArray[ind1,:]
3701 3700 meteorsArray[:,-1] = 0
3702 3701 phases = meteorsArray[:,8:12]
3703 3702
3704 3703 #Calculate Gammas
3705 3704 gammas = self.__getGammas(pairs, distances, phases)
3706 3705 # gammas = numpy.array([-21.70409463,45.76935864])*numpy.pi/180
3707 3706 #Calculate Phases
3708 3707 phasesOff = self.__getPhases(azimuth, h, pairs, distances, gammas, meteorsArray)
3709 3708 phasesOff = phasesOff.reshape((1,phasesOff.size))
3710 3709 dataOut.data_output = -phasesOff
3711 3710 dataOut.flagNoData = False
3712 3711 self.__buffer = None
3713 3712
3714 3713
3715 3714 return
3716 3715
3717 3716 class SMOperations():
3718 3717
3719 3718 def __init__(self):
3720 3719
3721 3720 return
3722 3721
3723 3722 def getMeteorParams(self, arrayParameters0, azimuth, h, pairsList, distances, jph):
3724 3723
3725 3724 arrayParameters = arrayParameters0.copy()
3726 3725 hmin = h[0]
3727 3726 hmax = h[1]
3728 3727
3729 3728 #Calculate AOA (Error N 3, 4)
3730 3729 #JONES ET AL. 1998
3731 3730 AOAthresh = numpy.pi/8
3732 3731 error = arrayParameters[:,-1]
3733 3732 phases = -arrayParameters[:,8:12] + jph
3734 3733 # phases = numpy.unwrap(phases)
3735 3734 arrayParameters[:,3:6], arrayParameters[:,-1] = self.__getAOA(phases, pairsList, distances, error, AOAthresh, azimuth)
3736 3735
3737 3736 #Calculate Heights (Error N 13 and 14)
3738 3737 error = arrayParameters[:,-1]
3739 3738 Ranges = arrayParameters[:,1]
3740 3739 zenith = arrayParameters[:,4]
3741 3740 arrayParameters[:,2], arrayParameters[:,-1] = self.__getHeights(Ranges, zenith, error, hmin, hmax)
3742 3741
3743 3742 #----------------------- Get Final data ------------------------------------
3744 3743 # error = arrayParameters[:,-1]
3745 3744 # ind1 = numpy.where(error==0)[0]
3746 3745 # arrayParameters = arrayParameters[ind1,:]
3747 3746
3748 3747 return arrayParameters
3749 3748
3750 3749 def __getAOA(self, phases, pairsList, directions, error, AOAthresh, azimuth):
3751 3750
3752 3751 arrayAOA = numpy.zeros((phases.shape[0],3))
3753 3752 cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList,directions)
3754 3753
3755 3754 arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3756 3755 cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3757 3756 arrayAOA[:,2] = cosDirError
3758 3757
3759 3758 azimuthAngle = arrayAOA[:,0]
3760 3759 zenithAngle = arrayAOA[:,1]
3761 3760
3762 3761 #Setting Error
3763 3762 indError = numpy.where(numpy.logical_or(error == 3, error == 4))[0]
3764 3763 error[indError] = 0
3765 3764 #Number 3: AOA not fesible
3766 3765 indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3767 3766 error[indInvalid] = 3
3768 3767 #Number 4: Large difference in AOAs obtained from different antenna baselines
3769 3768 indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3770 3769 error[indInvalid] = 4
3771 3770 return arrayAOA, error
3772 3771
3773 3772 def __getDirectionCosines(self, arrayPhase, pairsList, distances):
3774 3773
3775 3774 #Initializing some variables
3776 3775 ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3777 3776 ang_aux = ang_aux.reshape(1,ang_aux.size)
3778 3777
3779 3778 cosdir = numpy.zeros((arrayPhase.shape[0],2))
3780 3779 cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3781 3780
3782 3781
3783 3782 for i in range(2):
3784 3783 ph0 = arrayPhase[:,pairsList[i][0]]
3785 3784 ph1 = arrayPhase[:,pairsList[i][1]]
3786 3785 d0 = distances[pairsList[i][0]]
3787 3786 d1 = distances[pairsList[i][1]]
3788 3787
3789 3788 ph0_aux = ph0 + ph1
3790 3789 ph0_aux = numpy.angle(numpy.exp(1j*ph0_aux))
3791 3790 # ph0_aux[ph0_aux > numpy.pi] -= 2*numpy.pi
3792 3791 # ph0_aux[ph0_aux < -numpy.pi] += 2*numpy.pi
3793 3792 #First Estimation
3794 3793 cosdir0[:,i] = (ph0_aux)/(2*numpy.pi*(d0 - d1))
3795 3794
3796 3795 #Most-Accurate Second Estimation
3797 3796 phi1_aux = ph0 - ph1
3798 3797 phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3799 3798 #Direction Cosine 1
3800 3799 cosdir1 = (phi1_aux + ang_aux)/(2*numpy.pi*(d0 + d1))
3801 3800
3802 3801 #Searching the correct Direction Cosine
3803 3802 cosdir0_aux = cosdir0[:,i]
3804 3803 cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3805 3804 #Minimum Distance
3806 3805 cosDiff = (cosdir1 - cosdir0_aux)**2
3807 3806 indcos = cosDiff.argmin(axis = 1)
3808 3807 #Saving Value obtained
3809 3808 cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3810 3809
3811 3810 return cosdir0, cosdir
3812 3811
3813 3812 def __calculateAOA(self, cosdir, azimuth):
3814 3813 cosdirX = cosdir[:,0]
3815 3814 cosdirY = cosdir[:,1]
3816 3815
3817 3816 zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
3818 3817 azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth#0 deg north, 90 deg east
3819 3818 angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
3820 3819
3821 3820 return angles
3822 3821
3823 3822 def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
3824 3823
3825 3824 Ramb = 375 #Ramb = c/(2*PRF)
3826 3825 Re = 6371 #Earth Radius
3827 3826 heights = numpy.zeros(Ranges.shape)
3828 3827
3829 3828 R_aux = numpy.array([0,1,2])*Ramb
3830 3829 R_aux = R_aux.reshape(1,R_aux.size)
3831 3830
3832 3831 Ranges = Ranges.reshape(Ranges.size,1)
3833 3832
3834 3833 Ri = Ranges + R_aux
3835 3834 hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
3836 3835
3837 3836 #Check if there is a height between 70 and 110 km
3838 3837 h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
3839 3838 ind_h = numpy.where(h_bool == 1)[0]
3840 3839
3841 3840 hCorr = hi[ind_h, :]
3842 3841 ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
3843 3842
3844 3843 hCorr = hi[ind_hCorr][:len(ind_h)]
3845 3844 heights[ind_h] = hCorr
3846 3845
3847 3846 #Setting Error
3848 3847 #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
3849 3848 #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
3850 3849 indError = numpy.where(numpy.logical_or(error == 13, error == 14))[0]
3851 3850 error[indError] = 0
3852 3851 indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
3853 3852 error[indInvalid2] = 14
3854 3853 indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
3855 3854 error[indInvalid1] = 13
3856 3855
3857 3856 return heights, error
3858 3857
3859 3858 def getPhasePairs(self, channelPositions):
3860 3859 chanPos = numpy.array(channelPositions)
3861 3860 listOper = list(itertools.combinations(range(5),2))
3862 3861
3863 3862 distances = numpy.zeros(4)
3864 3863 axisX = []
3865 3864 axisY = []
3866 3865 distX = numpy.zeros(3)
3867 3866 distY = numpy.zeros(3)
3868 3867 ix = 0
3869 3868 iy = 0
3870 3869
3871 3870 pairX = numpy.zeros((2,2))
3872 3871 pairY = numpy.zeros((2,2))
3873 3872
3874 3873 for i in range(len(listOper)):
3875 3874 pairi = listOper[i]
3876 3875
3877 3876 posDif = numpy.abs(chanPos[pairi[0],:] - chanPos[pairi[1],:])
3878 3877
3879 3878 if posDif[0] == 0:
3880 3879 axisY.append(pairi)
3881 3880 distY[iy] = posDif[1]
3882 3881 iy += 1
3883 3882 elif posDif[1] == 0:
3884 3883 axisX.append(pairi)
3885 3884 distX[ix] = posDif[0]
3886 3885 ix += 1
3887 3886
3888 3887 for i in range(2):
3889 3888 if i==0:
3890 3889 dist0 = distX
3891 3890 axis0 = axisX
3892 3891 else:
3893 3892 dist0 = distY
3894 3893 axis0 = axisY
3895 3894
3896 3895 side = numpy.argsort(dist0)[:-1]
3897 3896 axis0 = numpy.array(axis0)[side,:]
3898 3897 chanC = int(numpy.intersect1d(axis0[0,:], axis0[1,:])[0])
3899 3898 axis1 = numpy.unique(numpy.reshape(axis0,4))
3900 3899 side = axis1[axis1 != chanC]
3901 3900 diff1 = chanPos[chanC,i] - chanPos[side[0],i]
3902 3901 diff2 = chanPos[chanC,i] - chanPos[side[1],i]
3903 3902 if diff1<0:
3904 3903 chan2 = side[0]
3905 3904 d2 = numpy.abs(diff1)
3906 3905 chan1 = side[1]
3907 3906 d1 = numpy.abs(diff2)
3908 3907 else:
3909 3908 chan2 = side[1]
3910 3909 d2 = numpy.abs(diff2)
3911 3910 chan1 = side[0]
3912 3911 d1 = numpy.abs(diff1)
3913 3912
3914 3913 if i==0:
3915 3914 chanCX = chanC
3916 3915 chan1X = chan1
3917 3916 chan2X = chan2
3918 3917 distances[0:2] = numpy.array([d1,d2])
3919 3918 else:
3920 3919 chanCY = chanC
3921 3920 chan1Y = chan1
3922 3921 chan2Y = chan2
3923 3922 distances[2:4] = numpy.array([d1,d2])
3924 3923 # axisXsides = numpy.reshape(axisX[ix,:],4)
3925 3924 #
3926 3925 # channelCentX = int(numpy.intersect1d(pairX[0,:], pairX[1,:])[0])
3927 3926 # channelCentY = int(numpy.intersect1d(pairY[0,:], pairY[1,:])[0])
3928 3927 #
3929 3928 # ind25X = numpy.where(pairX[0,:] != channelCentX)[0][0]
3930 3929 # ind20X = numpy.where(pairX[1,:] != channelCentX)[0][0]
3931 3930 # channel25X = int(pairX[0,ind25X])
3932 3931 # channel20X = int(pairX[1,ind20X])
3933 3932 # ind25Y = numpy.where(pairY[0,:] != channelCentY)[0][0]
3934 3933 # ind20Y = numpy.where(pairY[1,:] != channelCentY)[0][0]
3935 3934 # channel25Y = int(pairY[0,ind25Y])
3936 3935 # channel20Y = int(pairY[1,ind20Y])
3937 3936
3938 3937 # pairslist = [(channelCentX, channel25X),(channelCentX, channel20X),(channelCentY,channel25Y),(channelCentY, channel20Y)]
3939 3938 pairslist = [(chanCX, chan1X),(chanCX, chan2X),(chanCY,chan1Y),(chanCY, chan2Y)]
3940 3939
3941 3940 return pairslist, distances
3942 3941 # def __getAOA(self, phases, pairsList, error, AOAthresh, azimuth):
3943 3942 #
3944 3943 # arrayAOA = numpy.zeros((phases.shape[0],3))
3945 3944 # cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList)
3946 3945 #
3947 3946 # arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3948 3947 # cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3949 3948 # arrayAOA[:,2] = cosDirError
3950 3949 #
3951 3950 # azimuthAngle = arrayAOA[:,0]
3952 3951 # zenithAngle = arrayAOA[:,1]
3953 3952 #
3954 3953 # #Setting Error
3955 3954 # #Number 3: AOA not fesible
3956 3955 # indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3957 3956 # error[indInvalid] = 3
3958 3957 # #Number 4: Large difference in AOAs obtained from different antenna baselines
3959 3958 # indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3960 3959 # error[indInvalid] = 4
3961 3960 # return arrayAOA, error
3962 3961 #
3963 3962 # def __getDirectionCosines(self, arrayPhase, pairsList):
3964 3963 #
3965 3964 # #Initializing some variables
3966 3965 # ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3967 3966 # ang_aux = ang_aux.reshape(1,ang_aux.size)
3968 3967 #
3969 3968 # cosdir = numpy.zeros((arrayPhase.shape[0],2))
3970 3969 # cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3971 3970 #
3972 3971 #
3973 3972 # for i in range(2):
3974 3973 # #First Estimation
3975 3974 # phi0_aux = arrayPhase[:,pairsList[i][0]] + arrayPhase[:,pairsList[i][1]]
3976 3975 # #Dealias
3977 3976 # indcsi = numpy.where(phi0_aux > numpy.pi)
3978 3977 # phi0_aux[indcsi] -= 2*numpy.pi
3979 3978 # indcsi = numpy.where(phi0_aux < -numpy.pi)
3980 3979 # phi0_aux[indcsi] += 2*numpy.pi
3981 3980 # #Direction Cosine 0
3982 3981 # cosdir0[:,i] = -(phi0_aux)/(2*numpy.pi*0.5)
3983 3982 #
3984 3983 # #Most-Accurate Second Estimation
3985 3984 # phi1_aux = arrayPhase[:,pairsList[i][0]] - arrayPhase[:,pairsList[i][1]]
3986 3985 # phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3987 3986 # #Direction Cosine 1
3988 3987 # cosdir1 = -(phi1_aux + ang_aux)/(2*numpy.pi*4.5)
3989 3988 #
3990 3989 # #Searching the correct Direction Cosine
3991 3990 # cosdir0_aux = cosdir0[:,i]
3992 3991 # cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3993 3992 # #Minimum Distance
3994 3993 # cosDiff = (cosdir1 - cosdir0_aux)**2
3995 3994 # indcos = cosDiff.argmin(axis = 1)
3996 3995 # #Saving Value obtained
3997 3996 # cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3998 3997 #
3999 3998 # return cosdir0, cosdir
4000 3999 #
4001 4000 # def __calculateAOA(self, cosdir, azimuth):
4002 4001 # cosdirX = cosdir[:,0]
4003 4002 # cosdirY = cosdir[:,1]
4004 4003 #
4005 4004 # zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
4006 4005 # azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth #0 deg north, 90 deg east
4007 4006 # angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
4008 4007 #
4009 4008 # return angles
4010 4009 #
4011 4010 # def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
4012 4011 #
4013 4012 # Ramb = 375 #Ramb = c/(2*PRF)
4014 4013 # Re = 6371 #Earth Radius
4015 4014 # heights = numpy.zeros(Ranges.shape)
4016 4015 #
4017 4016 # R_aux = numpy.array([0,1,2])*Ramb
4018 4017 # R_aux = R_aux.reshape(1,R_aux.size)
4019 4018 #
4020 4019 # Ranges = Ranges.reshape(Ranges.size,1)
4021 4020 #
4022 4021 # Ri = Ranges + R_aux
4023 4022 # hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
4024 4023 #
4025 4024 # #Check if there is a height between 70 and 110 km
4026 4025 # h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
4027 4026 # ind_h = numpy.where(h_bool == 1)[0]
4028 4027 #
4029 4028 # hCorr = hi[ind_h, :]
4030 4029 # ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
4031 4030 #
4032 4031 # hCorr = hi[ind_hCorr]
4033 4032 # heights[ind_h] = hCorr
4034 4033 #
4035 4034 # #Setting Error
4036 4035 # #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
4037 4036 # #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
4038 4037 #
4039 4038 # indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
4040 4039 # error[indInvalid2] = 14
4041 4040 # indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
4042 4041 # error[indInvalid1] = 13
4043 4042 #
4044 4043 # return heights, error
4045 4044 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now