##// END OF EJS Templates
Modulo de lectura de espectros corregido y testeado....
Miguel Valdez -
r25:7eaaf56ff468
parent child
Show More
@@ -1,603 +1,844
1 1 """
2 2 Created on Feb 7, 2012
3 3
4 4 @autor $Author$
5 5 @version $Id$
6 6
7 7 """
8 8
9 9 import numpy
10 10 import plplot
11 11
12 def cmap1_init(colormap="gray"):
13
14 ncolor = None
15 rgb_lvl = None
16
17 # Routine for defining a specific color map 1 in HLS space.
18 # if gray is true, use basic grayscale variation from half-dark to light.
19 # otherwise use false color variation from blue (240 deg) to red (360 deg).
20
21 # Independent variable of control points.
22 i = numpy.array((0., 1.))
23 if colormap=="gray":
24 ncolor = 256
25 # Hue for control points. Doesn't matter since saturation is zero.
26 h = numpy.array((0., 0.))
27 # Lightness ranging from half-dark (for interest) to light.
28 l = numpy.array((0.5, 1.))
29 # Gray scale has zero saturation
30 s = numpy.array((0., 0.))
31
32 # number of cmap1 colours is 256 in this case.
33 plplot.plscmap1n(ncolor)
34 # Interpolate between control points to set up cmap1.
35 plplot.plscmap1l(0, i, h, l, s)
36
37 return None
38
39 if colormap=="br_green":
40 ncolor = 256
41 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
42 h = numpy.array((240., 0.))
43 # Lightness and saturation are constant (values taken from C example).
44 l = numpy.array((0.6, 0.6))
45 s = numpy.array((0.8, 0.8))
46
47 # number of cmap1 colours is 256 in this case.
48 plplot.plscmap1n(ncolor)
49 # Interpolate between control points to set up cmap1.
50 plplot.plscmap1l(0, i, h, l, s)
51
52 return None
53
54 if colormap=="tricolor":
55 ncolor = 3
56 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
57 h = numpy.array((240., 0.))
58 # Lightness and saturation are constant (values taken from C example).
59 l = numpy.array((0.6, 0.6))
60 s = numpy.array((0.8, 0.8))
61
62 # number of cmap1 colours is 256 in this case.
63 plplot.plscmap1n(ncolor)
64 # Interpolate between control points to set up cmap1.
65 plplot.plscmap1l(0, i, h, l, s)
66
67 return None
68
69 if colormap == 'rgb' or colormap == 'rgb666':
70
71 color_sz = 6
72 ncolor = color_sz*color_sz*color_sz
73 pos = numpy.zeros((ncolor))
74 r = numpy.zeros((ncolor))
75 g = numpy.zeros((ncolor))
76 b = numpy.zeros((ncolor))
77 ind = 0
78 for ri in range(color_sz):
79 for gi in range(color_sz):
80 for bi in range(color_sz):
81 r[ind] = ri/(color_sz-1.0)
82 g[ind] = gi/(color_sz-1.0)
83 b[ind] = bi/(color_sz-1.0)
84 pos[ind] = ind/(ncolor-1.0)
85 ind += 1
86 rgb_lvl = [6,6,6] #Levels for RGB colors
87
88 if colormap == 'rgb676':
89 ncolor = 6*7*6
90 pos = numpy.zeros((ncolor))
91 r = numpy.zeros((ncolor))
92 g = numpy.zeros((ncolor))
93 b = numpy.zeros((ncolor))
94 ind = 0
95 for ri in range(8):
96 for gi in range(8):
97 for bi in range(4):
98 r[ind] = ri/(6-1.0)
99 g[ind] = gi/(7-1.0)
100 b[ind] = bi/(6-1.0)
101 pos[ind] = ind/(ncolor-1.0)
102 ind += 1
103 rgb_lvl = [6,7,6] #Levels for RGB colors
104
105 if colormap == 'rgb685':
106 ncolor = 6*8*5
107 pos = numpy.zeros((ncolor))
108 r = numpy.zeros((ncolor))
109 g = numpy.zeros((ncolor))
110 b = numpy.zeros((ncolor))
111 ind = 0
112 for ri in range(8):
113 for gi in range(8):
114 for bi in range(4):
115 r[ind] = ri/(6-1.0)
116 g[ind] = gi/(8-1.0)
117 b[ind] = bi/(5-1.0)
118 pos[ind] = ind/(ncolor-1.0)
119 ind += 1
120 rgb_lvl = [6,8,5] #Levels for RGB colors
121
122 if colormap == 'rgb884':
123 ncolor = 8*8*4
124 pos = numpy.zeros((ncolor))
125 r = numpy.zeros((ncolor))
126 g = numpy.zeros((ncolor))
127 b = numpy.zeros((ncolor))
128 ind = 0
129 for ri in range(8):
130 for gi in range(8):
131 for bi in range(4):
132 r[ind] = ri/(8-1.0)
133 g[ind] = gi/(8-1.0)
134 b[ind] = bi/(4-1.0)
135 pos[ind] = ind/(ncolor-1.0)
136 ind += 1
137 rgb_lvl = [8,8,4] #Levels for RGB colors
138
139 if ncolor == None:
140 raise ValueError, "The colormap selected is not valid"
141
142 plplot.plscmap1n(ncolor)
143 plplot.plscmap1l(1, pos, r, g, b)
144
145 return rgb_lvl
146
12 147 class BaseGraph:
13 148 """
14 149
15 150 """
16 151
17 152
18 153
19 154 def __init__(self):
20 155 """
21 156
22 157 """
23 158 self.hasNotRange = True
24 159
25 160 self.xrange = None
26 161 self.yrange = None
27 162 self.zrange = None
28 163
29 164 self.xlabel = None
30 165 self.ylabel = None
31 166 self.title = None
32 167
33 168 self.legends = None
34 169
35 170 self.__name = None
36 171
37 172 self.__colormap = None
38 173 self.__colbox = None
39 174 self.__colleg = None
40 175
41 176 self.__xpos = None
42 177 self.__ypos = None
43 178
44 179 self.__xopt = None #"bcnst"
45 180 self.__yopt = None #"bcnstv"
46 181
47 182 self.__xlpos = None
48 183 self.__ylpos = None
49 184
50 185 self.__xrangeIsTime = False
51 186
52 187 #Advanced
53 188 self.__xg = None
54 189 self.__yg = None
55 190
56 191 def setName(self, name):
57 192 self.__name = name
58 193
59 194 def setScreenPos(self, xpos, ypos):
60 195 self.__xpos = xpos
61 196 self.__ypos = ypos
62 197
63 198 def setOpt(self, xopt, yopt):
64 199 self.__xopt = xopt
65 200 self.__yopt = yopt
66 201
67 202 def setXAxisAsTime(self):
68 203 self.__xrangeIsTime = True
69 204
70 205
71 206 def setup(self, title=None, xlabel=None, ylabel=None, colormap=None):
72 207 """
73 208 """
74 209 self.title = title
75 210 self.xlabel = xlabel
76 211 self.ylabel = ylabel
77 212 self.__colormap = colormap
78 213
79 214 def plotBox(self, xmin, xmax, ymin, ymax):
80 215 """
81 216
82 217 """
83 218 if self.__xrangeIsTime:
84 219 plplot.pltimefmt("%H:%M")
85 220
86 221 plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
87 222 plplot.plwind(float(xmin),
88 223 float(xmax),
89 224 float(ymin),
90 225 float(ymax)
91 226 )
92 227 plplot.plbox(self.__xopt, 0.0, 0, self.__yopt, 0.0, 0)
93 228 plplot.pllab(self.xlabel, self.ylabel, self.title)
94 229
95 230
96 231 def colorbarPlot(self, xmin=0., xmax=1., ymin=0., ymax=1.):
97 232 data = numpy.arange(256)
98 233 data = numpy.reshape(data, (1,-1))
99 234
100 self.plotBox(xmin, xmax, ymin, ymax)
101 235 plplot.plimage(data,
102 236 float(xmin),
103 237 float(xmax),
104 238 float(ymin),
105 239 float(ymax),
106 240 0.,
107 241 255.,
108 242 float(xmin),
109 243 float(xmax),
110 244 float(ymin),
111 245 float(ymax))
112 246
113 247 def basicXYPlot(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None):
114 248
115 249 if xmin == None: xmin = x[0]
116 250 if xmax == None: xmax = x[-1]
117 251 if ymin == None: ymin = y[0]
118 252 if ymax == None: ymax = y[-1]
119 253
120 254 plplot.plline(x, y)
121 255
122 256 def basicXYwithErrorPlot(self):
123 257 pass
124 258
125 259 def basicLineTimePlot(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None, colline=1):
126 260
127 261 if xmin == None: xmin = x[0]
128 262 if xmax == None: xmax = x[-1]
129 263 if ymin == None: ymin = y[0]
130 264 if ymax == None: ymax = y[-1]
131 265
132 266 plplot.plcol0(colline)
133 267 plplot.plline(x, y)
134 268 plplot.plcol0(1)
135 269
136 270 def basicPcolorPlot(self, data, x, y, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
137 271 """
138 272 """
139 273 if xmin == None: xmin = x[0]
140 274 if xmax == None: xmax = x[-1]
141 275 if ymin == None: ymin = y[0]
142 276 if ymax == None: ymax = y[-1]
143 277 if zmin == None: zmin = numpy.nanmin(data)
144 278 if zmax == None: zmax = numpy.nanmax(data)
145 279
146 280 plplot.plimage(data,
147 281 float(x[0]),
148 282 float(x[-1]),
149 283 float(y[0]),
150 284 float(y[-1]),
151 285 float(zmin),
152 286 float(zmax),
153 287 float(xmin),
154 288 float(xmax),
155 289 float(ymin),
156 290 float(ymax)
157 291 )
158 292
159 293 def __getBoxpltr(self, x, y, deltax=None, deltay=None):
160 294
161 295 if not(len(x)>1 and len(y)>1):
162 296 raise ValueError, "x axis and y axis are empty"
163 297
164 298 if deltax == None: deltax = x[-1] - x[-2]
165 299 if deltay == None: deltay = y[-1] - y[-2]
166 300
167 301 x1 = numpy.append(x, x[-1] + deltax)
168 302 y1 = numpy.append(y, y[-1] + deltay)
169 303
170 304 xg = (numpy.multiply.outer(x1, numpy.ones(len(y1))))
171 305 yg = (numpy.multiply.outer(numpy.ones(len(x1)), y1))
172 306
173 307 self.__xg = xg
174 308 self.__yg = yg
175 309
176 310 def advPcolorPlot(self, data, x, y, zmin=0., zmax=0.):
177 311 """
178 312 """
179 313
180 314 if self.__xg == None and self.__yg == None:
181 315 self.__getBoxpltr(x, y)
182 316
183 317 plplot.plimagefr(data, x[0], x[-1], y[0], y[-1], 0., 0., zmin, zmax, plplot.pltr2, self.__xg, self.__yg)
184 318
185 319
186 320 class LinearPlot():
187 321
188 322 __szchar = 1.0
189 323 __xrange = None
190 324 __yrange = None
191 325
192 326 m_BaseGraph = None
193 327
194 328 def __init__(self):
195 329
196 330
197 331 key = "linearplot"
198 332 self.m_BaseGraph = BaseGraph()
199 333 self.m_BaseGraph.setName(key)
200
334
335 self.__subpage = 0
336
201 337 def setColormap(self, colormap="br_green"):
202 338
203 339 if colormap == None:
204 340 colormap = self.__colormap
205 341
206 342 cmap1_init(colormap)
207 343
208 344 def iniSubpage(self):
209 345
210 346 if plplot.plgdev() == '':
211 347 raise ValueError, "Plot device has not been initialize"
212 348
213 349 plplot.pladv(self.__subpage)
214 350 plplot.plschr(0.0, self.__szchar)
215 351
216 352 self.setColormap()
217 353
218 354 def setScreenPos(self, width='small'):
219 355
220 356 if width == 'small':
221 357 xi = 0.12; yi = 0.14; xw = 0.78; yw = 0.80
222 358
223 359 if width == 'medium':
224 360 xi = 0.07; yi = 0.10; xw = 0.90; yw = 0.60
225 361
226 362 xf = xi + xw
227 363 yf = yi + yw
228 364
229 365 self.m_BaseGraph.setScreenPos([xi, xf], [yi, yf])
230 366
231 367 def setup(self, subpage, title="", xlabel="", ylabel="", XAxisAsTime=False):
232 368 """
233 369 """
234 370
235 371 self.m_BaseGraph.setOpt("bcnts","bcntsv")
236 372 self.m_BaseGraph.setup(title,
237 373 xlabel,
238 374 ylabel
239 375 )
240 376
241 377 self.setScreenPos(width='medium')
242 378
243 379 if XAxisAsTime:
244 380 self.m_BaseGraph.setXAxisAsTime()
245 381
246 382 self.__subpage = subpage
247 383 # def setRanges(self, xrange, yrange, zrange):
248 384 #
249 385 # self.m_BaseGraph.setRanges(xrange, yrange, zrange)
250 386
251 387 def plotData(self, x, y=None, xmin=None, xmax=None, ymin=None, ymax=None, colline=1):
252 388 """
253 389 Inputs:
254 390
255 391 x : Numpy array of dimension 1
256 392 y : Numpy array of dimension 1
257 393
258 394 """
259 395
260 396 try:
261 397 nX = numpy.shape(x)
262 398 except:
263 399 raise ValueError, "x is not a numpy array"
264 400
265 401 if y == None: y = numpy.arange(nX)
266 402
267 403 if xmin == None: xmin = x[0]
268 404 if xmax == None: xmax = x[-1]
269 405 if ymin == None: ymin = y[0]
270 406 if ymax == None: ymax = y[-1]
271 407
272 408 self.m_BaseGraph.plotBox(xmin, xmax, ymin, ymax)
273 409 self.m_BaseGraph.basicLineTimePlot(x, y, xmin, xmax, ymin, ymax, colline)
274 410
275 411 def plotComplexData(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None, colline=1, type='power'):
276 412 """
277 413 Inputs:
278 414
279 415 x : Numpy array of dimension 1
280 416 y : Complex numpy array of dimension 1
281 417
282 418 """
283 419
284 420 try:
285 421 nX = numpy.shape(x)
286 422 except:
287 423 raise ValueError, "x is not a numpy array"
288 424
289 425 try:
290 426 nY = numpy.shape(y)
291 427 except:
292 428 raise ValueError, "y is not a numpy array"
293 429
294 430 if xmin == None: xmin = x[0]
295 431 if xmax == None: xmax = x[-1]
296 432 if ymin == None: ymin = y[0]
297 433 if ymax == None: ymax = y[-1]
298 434
299 435 self.m_BaseGraph.plotBox(xmin, xmax, ymin, ymax)
300 436
301 437 if type.lower() == 'power':
302 438 self.m_BaseGraph.basicLineTimePlot(x, abs(y), xmin, xmax, ymin, ymax, colline)
303 439
304 440 if type.lower() == 'iq':
305 441
306 442 self.m_BaseGraph.basicLineTimePlot(x, y.real, xmin, xmax, ymin, ymax, colline)
307 443 self.m_BaseGraph.basicLineTimePlot(x, y.imag, xmin, xmax, ymin, ymax, colline+1)
308 444
309 445 class ColorPlot():
446
447 def __init__(self):
448
449 self.graphObjDict = {}
450
451 self.__subpage = 0
452 self.__showColorbar = False
453 self.__showPowerProfile = True
454
455 self.__szchar = 0.7
456 self.__xrange = None
457 self.__yrange = None
458 self.__zrange = None
459
460 key = "colorplot"
461 self.m_BaseGraph = BaseGraph()
462 self.m_BaseGraph.setName(key)
463
464 def setup(self, subpage, title="", xlabel="Frequency", ylabel="Range", colormap="jet", showColorbar=False, showPowerProfile=False, XAxisAsTime=False):
465 """
466 """
467
468 self.m_BaseGraph.setOpt("bcnts","bcntsv")
469 self.m_BaseGraph.setup(title,
470 xlabel,
471 ylabel
472 )
473
474 self.__subpage = subpage
475 self.__colormap = colormap
476 self.__showColorbar = showColorbar
477 self.__showPowerProfile = showPowerProfile
478
479 if showColorbar:
480 key = "colorbar"
481
482 cmapObj = BaseGraph()
483 cmapObj.setName(key)
484 cmapObj.setOpt("bc","bcmt")
485 cmapObj.setup(title="dBs",
486 xlabel="",
487 ylabel="",
488 colormap=colormap)
489
490 self.graphObjDict[key] = cmapObj
491
492
493 if showPowerProfile:
494 key = "powerprof"
495
496 powObj = BaseGraph()
497 powObj.setName(key)
498 powObj.setOpt("bcntg","bc")
499 powObj.setup(title="Power Profile",
500 xlabel="dBs",
501 ylabel="")
502
503 self.graphObjDict[key] = powObj
504
505 self.setScreenPos(width='small')
506
507 if XAxisAsTime:
508 self.m_BaseGraph.setXAxisAsTime()
509
510
511
512 def setColormap(self, colormap="br_green"):
513
514 if colormap == None:
515 colormap = self.__colormap
516
517 cmap1_init(colormap)
518
519 def iniSubpage(self):
520
521 if plplot.plgdev() == '':
522 raise ValueError, "Plot device has not been initialize"
523
524 plplot.pladv(self.__subpage)
525 plplot.plschr(0.0, self.__szchar)
526
527 self.setColormap()
528
529 def setScreenPos(self, width='small'):
530
531 if width == 'small':
532 xi = 0.12; yi = 0.12; xw = 0.86; yw = 0.70; xcmapw = 0.05; xpoww = 0.26; deltaxcmap = 0.02; deltaxpow = 0.06
533
534 if width == 'medium':
535 xi = 0.07; yi = 0.10; xw = 0.90; yw = 0.60; xcmapw = 0.05; xpoww = 0.24; deltaxcmap = 0.02; deltaxpow = 0.06
536
537 if self.__showColorbar:
538 xw -= xcmapw + deltaxcmap
539
540 if self.__showPowerProfile:
541 xw -= xpoww + deltaxpow
542
543 xf = xi + xw
544 yf = yi + yw
545 xcmapf = xf
546
547 self.m_BaseGraph.setScreenPos([xi, xf], [yi, yf])
548
549 if self.__showColorbar:
550 xcmapi = xf + deltaxcmap
551 xcmapf = xcmapi + xcmapw
552
553 key = "colorbar"
554 cmapObj = self.graphObjDict[key]
555 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
556
557 if self.__showPowerProfile:
558
559 xpowi = xcmapf + deltaxpow
560 xpowf = xpowi + xpoww
561
562 key = "powerprof"
563 powObj = self.graphObjDict[key]
564 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
565
566
567
568 def plotData(self, data, x=None, y=None, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
569 """
570 Inputs:
571
572 x : Numpy array of dimension 1
573 y : Numpy array of dimension 1
574
575 """
576
577 try:
578 nX, nY = numpy.shape(data)
579 except:
580 raise ValueError, "data is not a numpy array"
581
582 if x == None: x = numpy.arange(nX)
583 if y == None: y = numpy.arange(nY)
584
585 if xmin == None: xmin = x[0]
586 if xmax == None: xmax = x[-1]
587 if ymin == None: ymin = y[0]
588 if ymax == None: ymax = y[-1]
589 if zmin == None: zmin = numpy.nanmin(data)
590 if zmax == None: zmax = numpy.nanmax(data)
591
592 self.m_BaseGraph.plotBox(xmin, xmax, ymin, ymax)
593 self.m_BaseGraph.basicPcolorPlot(data, x, y, xmin, xmax, ymin, ymax, zmin, zmax)
594
595 if self.__showColorbar:
596 key = "colorbar"
597 cmapObj = self.graphObjDict[key]
598 cmapObj.plotBox(0., 1., zmin, zmax)
599 cmapObj.colorbarPlot(0., 1., zmin, zmax)
600
601 if self.__showPowerProfile:
602 power = numpy.average(data, axis=0)
603
604 step = (ymax - ymin)/(nY-1)
605 heis = numpy.arange(ymin, ymax + step, step)
606
607 key = "powerprof"
608 powObj = self.graphObjDict[key]
609
610 plplot.pllsty(2)
611 powObj.plotBox(zmin, zmax, ymin, ymax)
612 plplot.pllsty(1)
613 powObj.basicXYPlot(power, heis)
614
615
616 class ColorPlotX():
310 617
311 618
312 619 graphObjDict = {}
313 620 showColorbar = False
314 621 showPowerProfile = True
315 622
316 623 __szchar = 0.7
317 624 __xrange = None
318 625 __yrange = None
319 626 __zrange = None
320 627
321 628 m_BaseGraph = BaseGraph()
322 629
323 630 def __init__(self):
324 631
325 632 key = "colorplot"
326 633 self.m_BaseGraph.setName(key)
327 634
635 self.__subpage = 0
636
328 637 self.graphObjDict[key] = self.m_BaseGraph
329
638
639 def setColormap(self, colormap="br_green"):
640
641 if colormap == None:
642 colormap = self.__colormap
643
644 cmap1_init(colormap)
645
646 def iniSubpage(self):
647
648 if plplot.plgdev() == '':
649 raise ValueError, "Plot device has not been initialize"
650
651 plplot.pladv(self.__subpage)
652 plplot.plschr(0.0, self.__szchar)
653
654 self.setColormap()
655
330 656 def setup(self, subpage, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False, XAxisAsTime=False):
331 657 """
332 658 """
333 659
334 660 self.m_BaseGraph.setSubpage(subpage)
335 661 self.m_BaseGraph.setSzchar(self.__szchar)
336 662 self.m_BaseGraph.setOpt("bcnts","bcntsv")
337 663 self.m_BaseGraph.setup(title,
338 664 xlabel,
339 665 ylabel,
340 666 colormap)
341 667
342 668 if showColorbar:
343 669 key = "colorbar"
344 670
345 671 cmapObj = BaseGraph()
346 672 cmapObj.setName(key)
347 673 cmapObj.setSubpage(subpage)
348 674 cmapObj.setSzchar(self.__szchar)
349 675 cmapObj.setOpt("bc","bcmt")
350 676 cmapObj.setup(title="dBs",
351 677 xlabel="",
352 678 ylabel="",
353 679 colormap=colormap)
354 680
355 681 self.graphObjDict[key] = cmapObj
356 682
357 683
358 684 if showPowerProfile:
359 685 key = "powerprof"
360 686
361 687 powObj = BaseGraph()
362 688 powObj.setName(key)
363 689 powObj.setSubpage(subpage)
364 690 powObj.setSzchar(self.__szchar)
365 691 plplot.pllsty(2)
366 692 powObj.setOpt("bcntg","bc")
367 693 plplot.pllsty(1)
368 694 powObj.setup(title="Power Profile",
369 695 xlabel="dBs",
370 696 ylabel="")
371 697
372 698 self.graphObjDict[key] = powObj
373 699
374 700 self.showColorbar = showColorbar
375 701 self.showPowerProfile = showPowerProfile
376 702 self.setScreenPos()
377 703
378 704 if XAxisAsTime:
379 705 self.m_BaseGraph.setXAxisAsTime()
380 706 #self.setScreenPos(xi = 0.05, yi = 0.18, xw = 0.92, yw = 0.74, xcmapw = 0.015, xpoww = 0.14, deltaxcmap = 0.01, deltaxpow = 0.02)
381 707
382 708
383 709 def setScreenPos(self, xi = 0.12, yi = 0.14, xw = 0.78, yw = 0.80, xcmapw = 0.05, xpoww = 0.24, deltaxcmap = 0.02, deltaxpow = 0.06):
384 710
385 711 if self.showColorbar:
386 712 xw -= xcmapw + deltaxcmap
387 713
388 714 if self.showPowerProfile:
389 715 xw -= xpoww + deltaxpow
390 716
391 717 xf = xi + xw
392 718 yf = yi + yw
393 719 xcmapf = xf
394 720
395 721 self.m_BaseGraph.setScreenPos([xi, xf], [yi, yf])
396 722
397 723 if self.showColorbar:
398 724 xcmapi = xf + deltaxcmap
399 725 xcmapf = xcmapi + xcmapw
400 726
401 727 key = "colorbar"
402 728 cmapObj = self.graphObjDict[key]
403 729 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
404 730
405 731 if self.showPowerProfile:
406 732
407 733 xpowi = xcmapf + deltaxpow
408 734 xpowf = xpowi + xpoww
409 735
410 736 key = "powerprof"
411 737 powObj = self.graphObjDict[key]
412 738 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
413 739
414 740 def setRanges(self, xrange, yrange, zrange):
415 741
416 742 self.m_BaseGraph.setRanges(xrange, yrange, zrange)
417 743
418 744 keyList = self.graphObjDict.keys()
419 745
420 746 key = "colorbar"
421 747 if key in keyList:
422 748 cmapObj = self.graphObjDict[key]
423 749 cmapObj.setRanges([0., 1.], zrange)
424 750
425 751 key = "powerprof"
426 752 if key in keyList:
427 753 powObj = self.graphObjDict[key]
428 754 powObj.setRanges(zrange, yrange)
429 755
430 756 def plotData(self, data, x=None, y=None, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
431 757 """
432 758 """
433 759
434 760 try:
435 761 nX, nY = numpy.shape(data)
436 762 except:
437 763 raise ValueError, "data is not a numpy array"
438 764
439 765 if x == None: x = numpy.arange(nX)
440 766 if y == None: y = numpy.arange(nY)
441 767
442 768 if xmin == None: xmin = x[0]
443 769 if xmax == None: xmax = x[-1]
444 770 if ymin == None: ymin = y[0]
445 771 if ymax == None: ymax = y[-1]
446 772 if zmin == None: zmin = numpy.nanmin(data)
447 773 if zmax == None: zmax = numpy.nanmax(data)
448 774
449 775 if self.m_BaseGraph.hasNotRange:
450 776 self.setRanges([xmin, xmax], [ymin,ymax], [zmin,zmax])
451 777
452 778 self.m_BaseGraph.initSubpage()
453 779 self.m_BaseGraph.basicPcolorPlot(data, x, y, xmin, xmax, ymin, ymax, self.m_BaseGraph.zrange[0], self.m_BaseGraph.zrange[1])
454 780
455 781 if self.showColorbar:
456 782 key = "colorbar"
457 783 cmapObj = self.graphObjDict[key]
458 784 cmapObj.colorbarPlot()
459 785
460 786 if self.showPowerProfile:
461 787 power = numpy.average(data, axis=1)
462 788
463 789 step = (ymax - ymin)/(nY-1)
464 790 heis = numpy.arange(ymin, ymax + step, step)
465 791
466 792 key = "powerprof"
467 793 powObj = self.graphObjDict[key]
468 794 powObj.basicXYPlot(power, heis)
469 795
470 def cmap1_init(colormap="gray"):
796 if __name__ == '__main__':
471 797
472 ncolor = None
473 rgb_lvl = None
798 import numpy
799 plplot.plsetopt("geometry", "%dx%d" %(350*2, 300*2))
800 plplot.plsdev("xwin")
801 plplot.plscolbg(255,255,255)
802 plplot.plscol0(1,0,0,0)
803 plplot.plspause(False)
804 plplot.plinit()
805 plplot.plssub(2, 2)
474 806
475 # Routine for defining a specific color map 1 in HLS space.
476 # if gray is true, use basic grayscale variation from half-dark to light.
477 # otherwise use false color variation from blue (240 deg) to red (360 deg).
478
479 # Independent variable of control points.
480 i = numpy.array((0., 1.))
481 if colormap=="gray":
482 ncolor = 256
483 # Hue for control points. Doesn't matter since saturation is zero.
484 h = numpy.array((0., 0.))
485 # Lightness ranging from half-dark (for interest) to light.
486 l = numpy.array((0.5, 1.))
487 # Gray scale has zero saturation
488 s = numpy.array((0., 0.))
489
490 # number of cmap1 colours is 256 in this case.
491 plplot.plscmap1n(ncolor)
492 # Interpolate between control points to set up cmap1.
493 plplot.plscmap1l(0, i, h, l, s)
494
495 return None
496
497 if colormap=="br_green":
498 ncolor = 256
499 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
500 h = numpy.array((240., 0.))
501 # Lightness and saturation are constant (values taken from C example).
502 l = numpy.array((0.6, 0.6))
503 s = numpy.array((0.8, 0.8))
504
505 # number of cmap1 colours is 256 in this case.
506 plplot.plscmap1n(ncolor)
507 # Interpolate between control points to set up cmap1.
508 plplot.plscmap1l(0, i, h, l, s)
509
510 return None
807 nx = 64
808 ny = 100
511 809
512 if colormap=="tricolor":
513 ncolor = 3
514 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
515 h = numpy.array((240., 0.))
516 # Lightness and saturation are constant (values taken from C example).
517 l = numpy.array((0.6, 0.6))
518 s = numpy.array((0.8, 0.8))
810 data = numpy.random.uniform(-50,50,(nx,ny))
811
812 baseObj = ColorPlot()
813 specObj = ColorPlot()
814 baseObj1 = ColorPlot()
815 specObj1 = ColorPlot()
816
817 baseObj.setup(1, "Spectrum", "Frequency", "Range", "br_green", True, True)
818 specObj.setup(2, "Spectrum", "Frequency", "Range", "br_green", False, True)
519 819
520 # number of cmap1 colours is 256 in this case.
521 plplot.plscmap1n(ncolor)
522 # Interpolate between control points to set up cmap1.
523 plplot.plscmap1l(0, i, h, l, s)
820 baseObj1.setup(3, "Spectrum", "Frequency", "Range", "br_green", False, True)
821 specObj1.setup(4, "Spectrum", "Frequency", "Range", "br_green", False, True)
524 822
525 return None
823 data = numpy.random.uniform(-50,50,(nx,ny))
526 824
527 if colormap == 'rgb' or colormap == 'rgb666':
528
529 color_sz = 6
530 ncolor = color_sz*color_sz*color_sz
531 pos = numpy.zeros((ncolor))
532 r = numpy.zeros((ncolor))
533 g = numpy.zeros((ncolor))
534 b = numpy.zeros((ncolor))
535 ind = 0
536 for ri in range(color_sz):
537 for gi in range(color_sz):
538 for bi in range(color_sz):
539 r[ind] = ri/(color_sz-1.0)
540 g[ind] = gi/(color_sz-1.0)
541 b[ind] = bi/(color_sz-1.0)
542 pos[ind] = ind/(ncolor-1.0)
543 ind += 1
544 rgb_lvl = [6,6,6] #Levels for RGB colors
545
546 if colormap == 'rgb676':
547 ncolor = 6*7*6
548 pos = numpy.zeros((ncolor))
549 r = numpy.zeros((ncolor))
550 g = numpy.zeros((ncolor))
551 b = numpy.zeros((ncolor))
552 ind = 0
553 for ri in range(8):
554 for gi in range(8):
555 for bi in range(4):
556 r[ind] = ri/(6-1.0)
557 g[ind] = gi/(7-1.0)
558 b[ind] = bi/(6-1.0)
559 pos[ind] = ind/(ncolor-1.0)
560 ind += 1
561 rgb_lvl = [6,7,6] #Levels for RGB colors
825 plplot.plbop()
826 baseObj.iniSubpage()
827 baseObj.plotData(data)
562 828
563 if colormap == 'rgb685':
564 ncolor = 6*8*5
565 pos = numpy.zeros((ncolor))
566 r = numpy.zeros((ncolor))
567 g = numpy.zeros((ncolor))
568 b = numpy.zeros((ncolor))
569 ind = 0
570 for ri in range(8):
571 for gi in range(8):
572 for bi in range(4):
573 r[ind] = ri/(6-1.0)
574 g[ind] = gi/(8-1.0)
575 b[ind] = bi/(5-1.0)
576 pos[ind] = ind/(ncolor-1.0)
577 ind += 1
578 rgb_lvl = [6,8,5] #Levels for RGB colors
579
580 if colormap == 'rgb884':
581 ncolor = 8*8*4
582 pos = numpy.zeros((ncolor))
583 r = numpy.zeros((ncolor))
584 g = numpy.zeros((ncolor))
585 b = numpy.zeros((ncolor))
586 ind = 0
587 for ri in range(8):
588 for gi in range(8):
589 for bi in range(4):
590 r[ind] = ri/(8-1.0)
591 g[ind] = gi/(8-1.0)
592 b[ind] = bi/(4-1.0)
593 pos[ind] = ind/(ncolor-1.0)
594 ind += 1
595 rgb_lvl = [8,8,4] #Levels for RGB colors
829 specObj.iniSubpage()
830 specObj.plotData(data)
596 831
597 if ncolor == None:
598 raise ValueError, "The colormap selected is not valid"
832 baseObj1.iniSubpage()
833 baseObj1.plotData(data)
599 834
600 plplot.plscmap1n(ncolor)
601 plplot.plscmap1l(1, pos, r, g, b)
835 specObj1.iniSubpage()
836 specObj1.plotData(data)
602 837
603 return rgb_lvl No newline at end of file
838 plplot.plflush()
839
840 plplot.plspause(1)
841 plplot.plend()
842 exit(0)
843
844
@@ -1,234 +1,149
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 import os, sys
8 9 import numpy
9 10 import plplot
10 11
11 from BaseGraph import *
12 path = os.path.split(os.getcwd())[0]
13 sys.path.append(path)
12 14
13 class Spectrum:
14
15 graphObjDict = {}
16 showColorbar = False
17 showPowerProfile = True
18
19 __szchar = 0.7
20 __xrange = None
21 __yrange = None
22 __zrange = None
23 baseObj = BaseGraph()
15 from Graphics.BaseGraph import *
16 from Model.Spectra import Spectra
24 17
18 class Spectrum():
25 19
26 def __init__(self):
27
28 key = "spec"
20 def __init__(self, Spectra):
29 21
30 baseObj = BaseGraph()
31 baseObj.setName(key)
32
33 self.graphObjDict[key] = baseObj
34
35
36 def setup(self, subpage, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False):
37 22 """
38 """
39
40 xi = 0.12; xw = 0.78; xf = xi + xw
41 yi = 0.14; yw = 0.80; yf = yi + yw
42
43 xcmapi = xcmapf = 0.; xpowi = xpowf = 0.
44
45 key = "spec"
46 baseObj = self.graphObjDict[key]
47 baseObj.setSubpage(subpage)
48 baseObj.setSzchar(self.__szchar)
49 baseObj.setOpt("bcnts","bcnts")
50 baseObj.setup(title,
51 xlabel,
52 ylabel,
53 colormap)
54
55 if showColorbar:
56 key = "colorbar"
57
58 cmapObj = BaseGraph()
59 cmapObj.setName(key)
60 cmapObj.setSubpage(subpage)
61 cmapObj.setSzchar(self.__szchar)
62 cmapObj.setOpt("bc","bcmt")
63 cmapObj.setup(title="dBs",
64 xlabel="",
65 ylabel="",
66 colormap=colormap)
67
68 self.graphObjDict[key] = cmapObj
69
70 xcmapi = 0.
71 xcmapw = 0.05
72 xw -= xcmapw
73 23
74 if showPowerProfile:
75 key = "powerprof"
24 Inputs:
76 25
77 powObj = BaseGraph()
78 powObj.setName(key)
79 powObj.setSubpage(subpage)
80 powObj.setSzchar(self.__szchar)
81 plplot.pllsty(2)
82 powObj.setOpt("bcntg","bc")
83 plplot.pllsty(1)
84 powObj.setup(title="Power Profile",
85 xlabel="dBs",
86 ylabel="")
87
88 self.graphObjDict[key] = powObj
89
90 xpowi = 0.
91 xpoww = 0.24
92 xw -= xpoww
93
94 xf = xi + xw
95 yf = yi + yw
96 xcmapf = xf
26 type: "power" ->> Potencia
27 "iq" ->> Real + Imaginario
28 """
97 29
98 baseObj.setScreenPos([xi, xf], [yi, yf])
30 self.__isPlotConfig = False
99 31
100 if showColorbar:
101 xcmapi = xf + 0.02
102 xcmapf = xcmapi + xcmapw
103 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
104
105 if showPowerProfile:
106 xpowi = xcmapf + 0.06
107 xpowf = xpowi + xpoww
108 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
109
32 self.__isPlotIni = False
110 33
111 # baseObj.initSubpage()
112 #
113 # if showColorbar:
114 # cmapObj.initPlot()
115 #
116 # if showPowerProfile:
117 # powObj.initPlot()
118
119 self.showColorbar = showColorbar
120 self.showPowerProfile = showPowerProfile
121
122 def setRanges(self, xrange, yrange, zrange):
34 self.__xrange = None
123 35
124 key = "spec"
125 baseObj = self.graphObjDict[key]
126 baseObj.setRanges(xrange, yrange, zrange)
36 self.__yrange = None
127 37
128 keyList = self.graphObjDict.keys()
38 self.nGraphs = 0
129 39
130 key = "colorbar"
131 if key in keyList:
132 cmapObj = self.graphObjDict[key]
133 cmapObj.setRanges([0., 1.], zrange)
40 self.graphObjList = []
41
42 self.m_Spectra = Spectra
134 43
135 key = "powerprof"
136 if key in keyList:
137 powObj = self.graphObjDict[key]
138 powObj.setRanges(zrange, yrange)
139 44
140 def plotData(self, data , xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
45 def __addGraph(self, subpage, title="", xlabel="", ylabel="", showColorbar=False, showPowerProfile=True, XAxisAsTime=False):
46
47 graphObj = ColorPlot()
48 graphObj.setup(subpage,
49 title,
50 xlabel,
51 ylabel,
52 showColorbar=showColorbar,
53 showPowerProfile=showPowerProfile,
54 XAxisAsTime=XAxisAsTime)
55
56 self.graphObjList.append(graphObj)
57
58
59 def setup(self, titleList=None, xlabelList=None, ylabelList=None, showColorbar=False, showPowerProfile=True, XAxisAsTime=False):
141 60
142 key = "spec"
143 baseObj = self.graphObjDict[key]
144 baseObj.initSubpage()
61 nChan = int(self.m_Spectra.m_SystemHeader.numChannels)
145 62
146 if xmin == None:
147 xmin = 0.
63 myTitle = ""
64 myXlabel = ""
65 myYlabel = ""
148 66
149 if xmax == None:
150 xmax = 1.
67 for i in range(nChan):
68 if titleList != None:
69 myTitle = titleList[i]
70 myXlabel = xlabelList[i]
71 myYlabel = ylabelList[i]
72
73 self.__addGraph(i+1,
74 title=myTitle,
75 xlabel=myXlabel,
76 ylabel=myYlabel,
77 showColorbar=showColorbar,
78 showPowerProfile=showPowerProfile,
79 XAxisAsTime=XAxisAsTime)
80
81 self.nGraphs = nChan
82 self.__isPlotConfig = True
151 83
152 if ymin == None:
153 ymin = 0.
84 def iniPlot(self):
154 85
155 if ymax == None:
156 ymax = 1.
86 nx = int(numpy.sqrt(self.nGraphs)+1)
87 #ny = int(self.nGraphs/nx)
157 88
158 if zmin == None:
159 zmin = numpy.nanmin(data)
89 plplot.plsetopt("geometry", "%dx%d" %(400*nx, 300*nx))
90 plplot.plsdev("xcairo")
91 plplot.plscolbg(255,255,255)
92 plplot.plscol0(1,0,0,0)
93 plplot.plinit()
94 plplot.plspause(False)
95 plplot.pladv(0)
96 plplot.plssub(nx, nx)
160 97
161 if zmax == None:
162 zmax = numpy.nanmax(data)
98 self.__isPlotIni = True
163 99
164 if not(baseObj.hasRange):
165 self.setRanges([xmin, xmax], [ymin,ymax], [zmin,zmax])
166
167 baseObj.basicPcolorPlot(data, xmin, xmax, ymin, ymax, baseObj.zrange[0], baseObj.zrange[1])
168
169 if self.showColorbar:
170 key = "colorbar"
171 cmapObj = self.graphObjDict[key]
172 cmapObj.colorbarPlot()
173
174 if self.showPowerProfile:
175 power = numpy.average(data, axis=1)
176
177 step = (ymax - ymin)/(power.shape[0]-1)
178 heis = numpy.arange(ymin, ymax + step, step)
100 def plotData(self, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, titleList=None, xlabelList=None, ylabelList=None, showColorbar=False, showPowerProfile=True, XAxisAsTime=False):
101
102 if not(self.__isPlotConfig):
103 self.setup(titleList,
104 xlabelList,
105 ylabelList,
106 showColorbar,
107 showPowerProfile,
108 XAxisAsTime)
109
110 if not(self.__isPlotIni):
111 self.iniPlot()
112
113 data = numpy.log10(self.m_Spectra.data_spc)
114
115 nX, nY, nChan = numpy.shape(data)
116
117 x = numpy.arange(nX)
118 y = self.m_Spectra.heights
119
120 if xmin == None: xmin = x[0]
121 if xmax == None: xmax = x[-1]
122 if ymin == None: ymin = y[0]
123 if ymax == None: ymax = y[-1]
124 if zmin == None: zmin = numpy.nanmin(abs(data))
125 if zmax == None: zmax = numpy.nanmax(abs(data))
126
127 plplot.plbop()
128 for i in range(self.nGraphs):
129 self.graphObjList[i].iniSubpage()
130 self.graphObjList[i].plotData(data[:,:,i],
131 x,
132 y,
133 xmin=xmin,
134 xmax=xmax,
135 ymin=ymin,
136 ymax=ymax,
137 zmin=zmin,
138 zmax=zmax)
179 139
180 key = "powerprof"
181 powObj = self.graphObjDict[key]
182 powObj.basicXYPlot(power, heis)
183
184 class CrossSpectrum:
185 graphObjDict = {}
186 showColorbar = False
187 showPowerProfile = True
140
141 plplot.plflush()
142 plplot.pleop()
188 143
189 __szchar = 0.7
190 __showPhase = False
191 __xrange = None
192 __yrange = None
193 __zrange = None
194 m_BaseGraph= BaseGraph()
195
196 def __init__(self):
197 pass
198
199 def setup(self, subpage, title, xlabel, ylabel, colormap, showColorbar, showPowerProfile):
200 pass
201
202 def setRanges(self, xrange, yrange, zrange):
203 pass
204
205 def plotData(self, data, xmin, xmax, ymin, ymax, zmin, zmax):
206 pass
144 def end(self):
145 plplot.plend()
146
207 147
208 148 if __name__ == '__main__':
209
210 import numpy
211 plplot.plsetopt("geometry", "%dx%d" %(350*2, 300*2))
212 plplot.plsdev("xcairo")
213 plplot.plscolbg(255,255,255)
214 plplot.plscol0(1,0,0,0)
215 plplot.plinit()
216 plplot.plssub(2, 2)
217
218 nx = 64
219 ny = 100
220
221 data = numpy.random.uniform(-50,50,(nx,ny))
222
223 baseObj = ColorPlot()
224 baseObj.setup(1, "Spectrum", "Frequency", "Range", "br_green", False, False)
225 baseObj.plotData(data)
226
227 data = numpy.random.uniform(-50,50,(nx,ny))
228
229 spec2Obj = ColorPlot()
230 spec2Obj.setup(2, "Spectrum", "Frequency", "Range", "br_green", True, True)
231 spec2Obj.plotData(data)
232
233 plplot.plend()
234 exit(0) No newline at end of file
149 pass No newline at end of file
@@ -1,813 +1,813
1 1 '''
2 2 File: SpectraIO.py
3 3 Created on 20/02/2012
4 4
5 5 @author $Author$
6 6 @version $Id$
7 7 '''
8 8
9 9 import os, sys
10 10 import numpy
11 11 import glob
12 12 import fnmatch
13 13 import time, datetime
14 14
15 15 path = os.path.split(os.getcwd())[0]
16 16 sys.path.append(path)
17 17
18 18 from HeaderIO import *
19 19 from DataIO import DataReader
20 20 from DataIO import DataWriter
21 21
22 22 from Model.Spectra import Spectra
23 23
24 24 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
25 25 """
26 26 Esta funcion determina si un archivo de datos en formato Jicamarca(.r) se encuentra
27 27 o no dentro del rango de fecha especificado.
28 28
29 29 Inputs:
30 30 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
31 31
32 32 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
33 33 segundos contados desde 01/01/1970.
34 34 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
35 35 segundos contados desde 01/01/1970.
36 36
37 37 Return:
38 38 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
39 39 fecha especificado, de lo contrario retorna False.
40 40
41 41 Excepciones:
42 42 Si el archivo no existe o no puede ser abierto
43 43 Si la cabecera no puede ser leida.
44 44
45 45 """
46 46 m_BasicHeader = BasicHeader()
47 47
48 48 try:
49 49 fp = open(filename,'rb')
50 50 except:
51 51 raise IOError, "The file %s can't be opened" %(filename)
52 52
53 53 if not(m_BasicHeader.read(fp)):
54 54 raise IOError, "The file %s has not a valid header" %(filename)
55 55
56 56 fp.close()
57 57
58 58 if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds >= m_BasicHeader.utc)):
59 59 return 0
60 60
61 61 return 1
62 62
63 63
64 64 class SpectraReader(DataReader):
65 65 """
66 66 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
67 67 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
68 68 perfiless*alturas*canales) son almacenados en la variable "buffer".
69 69
70 70 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
71 71 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
72 72 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
73 73 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
74 74
75 75 Example:
76 76
77 77 dpath = "/home/myuser/data"
78 78
79 79 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
80 80
81 81 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
82 82
83 83 readerObj = SpectraReader()
84 84
85 85 readerObj.setup(dpath, startTime, endTime)
86 86
87 87 while(True):
88 88
89 89 readerObj.getData()
90 90
91 91 print readerObj.m_Spectra.data
92 92
93 93 if readerObj.noMoreFiles:
94 94 break
95 95
96 96 """
97 97
98 98 #speed of light
99 99 __c = 3E8
100 100
101 101 def __init__( self, m_Spectra = None ):
102 102 """
103 103 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
104 104
105 105 Input:
106 106 m_Spectra : Objeto de la clase Spectra. Este objeto sera utilizado para
107 107 almacenar un perfil de datos cada vez que se haga un requerimiento
108 108 (getData). El perfil sera obtenido a partir del buffer de datos,
109 109 si el buffer esta vacio se hara un nuevo proceso de lectura de un
110 110 bloque de datos.
111 111 Si este parametro no es pasado se creara uno internamente.
112 112
113 113 Variables afectadas:
114 114 self.m_Spectra
115 115 self.m_BasicHeader
116 116 self.m_SystemHeader
117 117 self.m_RadarControllerHeader
118 118 self.m_ProcessingHeader
119 119
120 120
121 121 Return:
122 122 Void
123 123
124 124 """
125 125 if m_Spectra == None:
126 126 m_Spectra = Spectra()
127 127
128 128 if not( isinstance(m_Spectra, Spectra) ):
129 129 raise ValueError, "in SpectraReader, m_Spectra must be an Spectra class object"
130 130
131 131 self.m_Spectra = m_Spectra
132 132
133 133 self.m_BasicHeader = BasicHeader()
134 134
135 135 self.m_SystemHeader = SystemHeader()
136 136
137 137 self.m_RadarControllerHeader = RadarControllerHeader()
138 138
139 139 self.m_ProcessingHeader = ProcessingHeader()
140 140
141 141 self.__fp = None
142 142
143 143 self.__idFile = None
144 144
145 145 self.__startDateTime = None
146 146
147 147 self.__endDateTime = None
148 148
149 149 self.__dataType = None
150 150
151 151 self.__fileSizeByHeader = 0
152 152
153 153 self.__pathList = []
154 154
155 155 self.filenameList = []
156 156
157 157 self.__lastUTTime = 0
158 158
159 159 self.__maxTimeStep = 30
160 160
161 161 self.__flagIsNewFile = 0
162 162
163 163 self.flagResetProcessing = 0
164 164
165 165 self.flagIsNewBlock = 0
166 166
167 167 self.noMoreFiles = 0
168 168
169 169 self.nReadBlocks = 0
170 170
171 171 self.online = 0
172 172
173 173 self.firstHeaderSize = 0
174 174
175 175 self.basicHeaderSize = 24
176 176
177 177 self.filename = None
178 178
179 179 self.fileSize = None
180 180
181 181 self.__buffer_spc = None
182 182 self.__buffer_cspc = None
183 183 self.__buffer_dc = None
184 184
185 185 self.__buffer_id = 0
186 186
187 187 self.__ippSeconds = 0
188 188
189 189 self.nSelfChannels = 0
190 190
191 191 self.nCrossPairs = 0
192 192
193 193 self.nChannels = 0
194 194
195 195 def __rdSystemHeader( self, fp=None ):
196 196 if fp == None:
197 197 fp = self.__fp
198 198
199 199 self.m_SystemHeader.read( fp )
200 200
201 201 def __rdRadarControllerHeader( self, fp=None ):
202 202 if fp == None:
203 203 fp = self.__fp
204 204
205 205 self.m_RadarControllerHeader.read(fp)
206 206
207 207 def __rdProcessingHeader( self,fp=None ):
208 208 if fp == None:
209 209 fp = self.__fp
210 210
211 211 self.m_ProcessingHeader.read(fp)
212 212
213 213 def __rdBasicHeader( self, fp=None ):
214 214 if fp == None:
215 215 fp = self.__fp
216 216
217 217 self.m_BasicHeader.read(fp)
218 218
219 219 def __readFirstHeader( self ):
220 220 self.__rdBasicHeader()
221 221 self.__rdSystemHeader()
222 222 self.__rdRadarControllerHeader()
223 223 self.__rdProcessingHeader()
224 224 self.firstHeaderSize = self.m_BasicHeader.size
225 225
226 226 data_type = int( numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR) )
227 227 if data_type == 0:
228 228 tmp = numpy.dtype([('real','<i1'),('imag','<i1')])
229 229
230 230 elif data_type == 1:
231 231 tmp = numpy.dtype([('real','<i2'),('imag','<i2')])
232 232
233 233 elif data_type == 2:
234 234 tmp = numpy.dtype([('real','<i4'),('imag','<i4')])
235 235
236 236 elif data_type == 3:
237 237 tmp = numpy.dtype([('real','<i8'),('imag','<i8')])
238 238
239 239 elif data_type == 4:
240 240 tmp = numpy.dtype([('real','<f4'),('imag','<f4')])
241 241
242 242 elif data_type == 5:
243 243 tmp = numpy.dtype([('real','<f8'),('imag','<f8')])
244 244
245 245 else:
246 246 raise ValueError, 'Data type was not defined'
247 247
248 248 xi = self.m_ProcessingHeader.firstHeight
249 249 step = self.m_ProcessingHeader.deltaHeight
250 250 xf = xi + self.m_ProcessingHeader.numHeights*step
251 251
252 252 self.__heights = numpy.arange(xi, xf, step)
253 253 self.__dataType = tmp
254 254 self.__fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1)
255 255 self.__ippSeconds = 2 * 1000 * self.m_RadarControllerHeader.ipp/self.__c
256 256
257 257 def __setNextFileOnline( self ):
258 258 return 1
259 259
260 260 def __setNextFileOffline( self ):
261 261 idFile = self.__idFile
262 262
263 263 while(True):
264 264 idFile += 1
265 265
266 266 if not( idFile < len(self.filenameList) ):
267 267 self.noMoreFiles = 1
268 268 return 0
269 269
270 270 filename = self.filenameList[idFile]
271 271 fileSize = os.path.getsize(filename)
272 272
273 273 try:
274 274 fp = open( filename, 'rb' )
275 275 except:
276 276 raise IOError, "The file %s can't be opened" %filename
277 277
278 278 currentSize = fileSize - fp.tell()
279 279 neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize
280 280
281 281 if (currentSize < neededSize):
282 282 print "Skipping the file %s due to it hasn't enough data" %filename
283 283 continue
284 284
285 285 break
286 286
287 287 self.__flagIsNewFile = 1
288 288 self.__idFile = idFile
289 289 self.filename = filename
290 290 self.fileSize = fileSize
291 291 self.__fp = fp
292 292
293 293 print 'Setting the file: %s'%self.filename
294 294
295 295 return 1
296 296
297 297 def __setNextFile(self):
298 298 if self.online:
299 299 newFile = self.__setNextFileOnline()
300 300 else:
301 301 newFile = self.__setNextFileOffline()
302 302
303 303 if not(newFile):
304 304 return 0
305 305
306 306 self.__readFirstHeader()
307 307
308 308 return 1
309 309
310 310 def __setNewBlock( self ):
311 311 if self.__fp == None:
312 312 return 0
313 313
314 314 if self.__flagIsNewFile:
315 315 return 1
316 316
317 317 currentSize = self.fileSize - self.__fp.tell()
318 318 neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize
319 319
320 320 #If there is enough data setting new data block
321 321 if ( currentSize >= neededSize ):
322 322 self.__rdBasicHeader()
323 323 return 1
324 324
325 325 #Setting new file
326 326 if not( self.__setNextFile() ):
327 327 return 0
328 328
329 329 deltaTime = self.m_BasicHeader.utc - self.__lastUTTime # check this
330 330
331 331 self.flagResetProcessing = 0
332 332
333 333 if deltaTime > self.__maxTimeStep:
334 334 self.flagResetProcessing = 1
335 335 self.ns = 0
336 336
337 337 return 1
338 338
339 339
340 340
341 341 def __readBlock(self):
342 342 """
343 343 __readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
344 344 (self.__fp) y actualiza todos los parametros relacionados al bloque de datos
345 345 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
346 346 es seteado a 0
347 347
348 348 Inputs:
349 349 None
350 350
351 351 Return:
352 352 None
353 353
354 354 Variables afectadas:
355 355
356 356 self.__buffer_id
357 357
358 358 self.__buffer_sspc
359 359
360 360 self.__flagIsNewFile
361 361
362 362 self.flagIsNewBlock
363 363
364 364 self.nReadBlocks
365 365
366 366 """
367 367 Npair_SelfSpectra = 0
368 368 Npair_CrossSpectra = 0
369 369
370 370 for i in range( 0,self.m_ProcessingHeader.totalSpectra*2,2 ):
371 371 if self.m_ProcessingHeader.spectraComb[i] == self.m_ProcessingHeader.spectraComb[i+1]:
372 372 Npair_SelfSpectra = Npair_SelfSpectra + 1
373 373 else:
374 374 Npair_CrossSpectra = Npair_CrossSpectra + 1
375 375
376 376 # self.__buffer_sspc = numpy.concatenate( (data_sspc,data_cspc,data_dcc), axis=0 )
377 377
378 378 self.__buffer_id = 0
379 379 self.__flagIsNewFile = 0
380 380 self.flagIsNewBlock = 1
381 381
382 382 pts2read = self.m_ProcessingHeader.profilesPerBlock*self.m_ProcessingHeader.numHeights
383 383
384 384 spc = numpy.fromfile(self.__fp, self.__dataType[0], int(pts2read*Npair_SelfSpectra))
385 385 cspc = numpy.fromfile(self.__fp, self.__dataType, int(pts2read*Npair_CrossSpectra))
386 dc = numpy.fromfile(self.__fp, self.__dataType, int(pts2read*self.m_SystemHeader.numChannels) )
386 dc = numpy.fromfile(self.__fp, self.__dataType, int(self.m_ProcessingHeader.numHeights*self.m_SystemHeader.numChannels) )
387 387
388 388 spc = spc.reshape((self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, Npair_SelfSpectra))
389 389 cspc = cspc.reshape((self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, Npair_CrossSpectra))
390 dc = dc.reshape((self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, self.m_SystemHeader.numChannels))
390 dc = dc.reshape((self.m_ProcessingHeader.numHeights, self.m_SystemHeader.numChannels))
391 391
392 392 data_spc = spc
393 393 data_cspc = cspc['real'] + cspc['imag']*1j
394 394 data_dc = dc['real'] + dc['imag']*1j
395 395
396 396 self.__buffer_spc = data_spc
397 397 self.__buffer_cspc = data_cspc
398 398 self.__buffer_dc = data_dc
399 399
400 400 self.__flagIsNewFile = 0
401 401
402 402 self.flagIsNewBlock = 1
403 403
404 404 self.nReadBlocks += 1
405 405
406 406
407 407 def __hasNotDataInBuffer(self):
408 408 return 1
409 409
410 410 def __searchFiles(self, path, startDateTime, endDateTime, set=None, expLabel = "", ext = ".pdata"):
411 411 """
412 412 __searchFiles realiza una busqueda de los archivos que coincidan con los parametros
413 413 especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda
414 414 correcta la estructura de directorios debe ser la siguiente:
415 415
416 416 ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext
417 417
418 418 [yyyy]: anio
419 419 [ddd] : dia del anio
420 420 [sss] : set del archivo
421 421
422 422 Inputs:
423 423 path : Directorio de datos donde se realizara la busqueda. Todos los
424 424 ficheros que concidan con el criterio de busqueda seran
425 425 almacenados en una lista y luego retornados.
426 426 startDateTime : Fecha inicial. Rechaza todos los archivos donde
427 427 file end time < startDateTime (objeto datetime.datetime)
428 428
429 429 endDateTime : Fecha final. Rechaza todos los archivos donde
430 430 file start time > endDateTime (obejto datetime.datetime)
431 431
432 432 set : Set del primer archivo a leer. Por defecto None
433 433
434 434 expLabel : Nombre del subdirectorio de datos. Por defecto ""
435 435
436 436 ext : Extension de los archivos a leer. Por defecto .r
437 437
438 438 Return:
439 439
440 440 (pathList, filenameList)
441 441
442 442 pathList : Lista de directorios donde se encontraron archivos dentro
443 443 de los parametros especificados
444 444 filenameList : Lista de archivos (ruta completa) que coincidieron con los
445 445 parametros especificados.
446 446
447 447 Variables afectadas:
448 448
449 449 self.filenameList: Lista de archivos (ruta completa) que la clase utiliza
450 450 como fuente para leer los bloque de datos, si se termina
451 451 de leer todos los bloques de datos de un determinado
452 452 archivo se pasa al siguiente archivo de la lista.
453 453
454 454 Excepciones:
455 455
456 456 """
457 457
458 458 print "Searching files ..."
459 459
460 460 dirList = []
461 461 for thisPath in os.listdir(path):
462 462 if os.path.isdir(os.path.join(path,thisPath)):
463 463 dirList.append(thisPath)
464 464
465 465 pathList = []
466 466
467 467 thisDateTime = startDateTime
468 468
469 469 while(thisDateTime <= endDateTime):
470 470 year = thisDateTime.timetuple().tm_year
471 471 doy = thisDateTime.timetuple().tm_yday
472 472
473 473 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
474 474 if len(match) == 0:
475 475 thisDateTime += datetime.timedelta(1)
476 476 continue
477 477
478 478 pathList.append(os.path.join(path,match[0],expLabel))
479 479 thisDateTime += datetime.timedelta(1)
480 480
481 481 startUtSeconds = time.mktime(startDateTime.timetuple())
482 482 endUtSeconds = time.mktime(endDateTime.timetuple())
483 483
484 484 filenameList = []
485 485 for thisPath in pathList:
486 486 fileList = glob.glob1(thisPath, "*%s" %ext)
487 487 fileList.sort()
488 488 for file in fileList:
489 489 filename = os.path.join(thisPath,file)
490 490 if isThisFileinRange(filename, startUtSeconds, endUtSeconds):
491 491 filenameList.append(filename)
492 492
493 493 self.filenameList = filenameList
494 494
495 495 return pathList, filenameList
496 496
497 497
498 498 def setup( self, path, startDateTime, endDateTime=None, set=None, expLabel = "", ext = ".pdata", online = 0 ):
499 499 """
500 500 setup configura los parametros de lectura de la clase SpectraReader.
501 501
502 502 Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos
503 503 que coincidan con los parametros especificados; esta lista de archivos son almacenados en
504 504 self.filenameList.
505 505
506 506 Input:
507 507 path : Directorios donde se ubican los datos a leer. Dentro de este
508 508 directorio deberia de estar subdirectorios de la forma:
509 509
510 510 path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext]
511 511
512 512 startDateTime : Fecha inicial. Rechaza todos los archivos donde
513 513 file end time < startDatetime (objeto datetime.datetime)
514 514
515 515 endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde
516 516 file end time < startDatetime (objeto datetime.datetime)
517 517
518 518 set : Set del primer archivo a leer. Por defecto None
519 519
520 520 expLabel : Nombre del subdirectorio de datos. Por defecto ""
521 521
522 522 ext : Extension de los archivos a leer. Por defecto .r
523 523
524 524 online :
525 525
526 526 Return:
527 527
528 528 Affected:
529 529
530 530 Excepciones:
531 531
532 532 Example:
533 533
534 534 """
535 535 if online == 0:
536 536 pathList, filenameList = self.__searchFiles(path, startDateTime, endDateTime, set, expLabel, ext)
537 537
538 538 self.__idFile = -1
539 539
540 540 if not(self.__setNextFile()):
541 541 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
542 542 return 0
543 543
544 544 self.startUTCSeconds = time.mktime(startDateTime.timetuple())
545 545 self.endUTCSeconds = time.mktime(endDateTime.timetuple())
546 546
547 547 self.startYear = startDateTime.timetuple().tm_year
548 548 self.endYear = endDateTime.timetuple().tm_year
549 549
550 550 self.startDoy = startDateTime.timetuple().tm_yday
551 551 self.endDoy = endDateTime.timetuple().tm_yday
552 552 #call fillHeaderValues() - to Data Object
553 553
554 554 self.__pathList = pathList
555 555 self.filenameList = filenameList
556 556 self.online = online
557 557
558 558 return 1
559 559
560 560 def readNextBlock(self):
561 561 """
562 562 readNextBlock establece un nuevo bloque de datos a leer y los lee, si es que no existiese
563 563 mas bloques disponibles en el archivo actual salta al siguiente.
564 564
565 565 """
566 566
567 567 if not( self.__setNewBlock() ):
568 568 return 0
569 569
570 570 self.__readBlock()
571 571
572 572 self.__lastUTTime = self.m_BasicHeader.utc
573 573
574 574 return 1
575 575
576 576
577 577 def getData(self):
578 578 """
579 579 getData copia el buffer de lectura a la clase "Spectra",
580 580 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
581 581 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
582 582
583 583 Inputs:
584 584 None
585 585
586 586 Return:
587 587 data : retorna un bloque de datos (nFFTs * alturas * canales) copiados desde el
588 588 buffer. Si no hay mas archivos a leer retorna None.
589 589
590 590 Variables afectadas:
591 591 self.m_Spectra
592 592 self.__buffer_id
593 593
594 594 Excepciones:
595 595
596 596 """
597 597
598 598 self.flagResetProcessing = 0
599 599 self.flagIsNewBlock = 0
600 600
601 601 if self.__hasNotDataInBuffer():
602 602 self.readNextBlock()
603 603
604 604 self.m_Spectra.m_BasicHeader = self.m_BasicHeader.copy()
605 605 self.m_Spectra.m_ProcessingHeader = self.m_ProcessingHeader.copy()
606 606 self.m_Spectra.m_RadarControllerHeader = self.m_RadarControllerHeader.copy()
607 607 self.m_Spectra.m_SystemHeader = self.m_SystemHeader.copy()
608 608 self.m_Spectra.heights = self.__heights
609 609 self.m_Spectra.dataType = self.__dataType
610 610
611 611 if self.noMoreFiles == 1:
612 612 print 'Process finished'
613 613 return 0
614 614
615 615 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
616 616 #print type(self.__buffer_sspc)
617 617
618 618 time = self.m_BasicHeader.utc + self.__buffer_id*self.__ippSeconds
619 619
620 620 self.m_Spectra.m_BasicHeader.utc = time
621 621 self.m_Spectra.data_spc = self.__buffer_spc
622 622 self.m_Spectra.data_cspc = self.__buffer_cspc
623 623 self.m_Spectra.data_dc = self.__buffer_dc
624 624
625 625 #call setData - to Data Object
626 626
627 627 return 1
628 628
629 629
630 630 class SpectraWriter(DataWriter):
631 631
632 632 def __init__(self):
633 633 if m_Spectra == None:
634 634 m_Spectra = Spectra()
635 635
636 636 self.m_Spectra = m_Spectra
637 637
638 638 self.__fp = None
639 639
640 640 self.__blocksCounter = 0
641 641
642 642 self.__setFile = None
643 643
644 644 self.__flagIsNewFile = 0
645 645
646 646 self.__buffer_sspc = 0
647 647
648 648 self.__buffer_id = 0
649 649
650 650 self.__dataType = None
651 651
652 652 self.__ext = None
653 653
654 654 self.nWriteBlocks = 0
655 655
656 656 self.flagIsNewBlock = 0
657 657
658 658 self.noMoreFiles = 0
659 659
660 660 self.filename = None
661 661
662 662 self.m_BasicHeader= BasicHeader()
663 663
664 664 self.m_SystemHeader = SystemHeader()
665 665
666 666 self.m_RadarControllerHeader = RadarControllerHeader()
667 667
668 668 self.m_ProcessingHeader = ProcessingHeader()
669 669
670 670 def __setNextFile(self):
671 671 setFile = self.__setFile
672 672 ext = self.__ext
673 673 path = self.__path
674 674
675 675 setFile += 1
676 676
677 677 if not(self.__blocksCounter >= self.m_ProcessingHeader.dataBlocksPerFile):
678 678 self.__fp.close()
679 679 return 0
680 680
681 681 timeTuple = time.localtime(self.m_Spectra.m_BasicHeader.utc) # utc from m_Spectra
682 682 file = 'D%4.4d%3.3d%3.3d%s' % (timeTuple.tm_year,timeTuple.tm_doy,setFile,ext)
683 683 subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_doy)
684 684 tmp = os.path.join(path,subfolder)
685 685 if not(os.path.exists(tmp)):
686 686 os.mkdir(tmp)
687 687
688 688 filename = os.path.join(path,subfolder,file)
689 689 fp = open(filename,'wb')
690 690
691 691 #guardando atributos
692 692 self.filename = filename
693 693 self.__subfolder = subfolder
694 694 self.__fp = fp
695 695 self.__setFile = setFile
696 696 self.__flagIsNewFile = 1
697 697
698 698 print 'Writing the file: %s'%self.filename
699 699
700 700 return 1
701 701
702 702
703 703
704 704 def __setNewBlock(self):
705 705 if self.__fp == None:
706 706 return 0
707 707
708 708 if self.__flagIsNewFile:
709 709 return 1
710 710
711 711 #Bloques completados?
712 712 if self.__blocksCounter < self.m_ProcessingHeader.profilesPerBlock:
713 713 self.__writeBasicHeader()
714 714 return 1
715 715
716 716 if not(self.__setNextFile()):
717 717 return 0
718 718
719 719 self.__writeFirstHeader()
720 720
721 721 return 1
722 722
723 723 def __writeBlock(self):
724 724
725 725 numpy.save(self.__fp,self.__buffer_sspc)
726 726
727 727 self.__buffer_sspc = numpy.array([],self.__dataType)
728 728
729 729 self.__buffer_id = 0
730 730
731 731 self.__flagIsNewFile = 0
732 732
733 733 self.flagIsNewBlock = 1
734 734
735 735 self.nWriteBlocks += 1
736 736
737 737 self.__blocksCounter += 1
738 738
739 739 def writeNextBlock(self):
740 740 if not(self.__setNewBlock()):
741 741 return 0
742 742
743 743 self.__writeBlock()
744 744
745 745 return 1
746 746
747 747 def __hasAllDataInBuffer(self):
748 748 if self.__buffer_id >= self.m_ProcessingHeader.profilesPerBlock:
749 749 return 1
750 750
751 751 return 0
752 752
753 753 def putData(self):
754 754 self.flagIsNewBlock = 0
755 755
756 756 if self.m_Spectra.noData:
757 757 return None
758 758
759 759 shape = self.m_Spectra.data.shape
760 760 data = numpy.zeros(shape,self.__dataType)
761 761 data['real'] = self.m_Spectra.data.real
762 762 data['imag'] = self.m_Spectra.data.imag
763 763 data = data.reshape((-1))
764 764
765 765 self.__buffer_sspc = numpy.hstack((self.__buffer_sspc,data))
766 766
767 767 self.__buffer_id += 1
768 768
769 769 if __hasAllDataInBuffer():
770 770 self.writeNextBlock()
771 771
772 772
773 773 if self.noMoreFiles:
774 774 print 'Process finished'
775 775 return None
776 776
777 777 return 1
778 778
779 779
780 780 def setup(self,path,set=None,format=None):
781 781
782 782 if set == None:
783 783 set = -1
784 784 else:
785 785 set -= 1
786 786
787 787 if format == 'hdf5':
788 788 ext = '.hdf5'
789 789 print 'call hdf5 library'
790 790 return 0
791 791
792 792 if format == 'rawdata':
793 793 ext = '.r'
794 794
795 795 #call to config_headers
796 796
797 797 self.__setFile = set
798 798
799 799 if not(self.__setNextFile()):
800 800 print "zzzzzzzzzzzz"
801 801 return 0
802 802
803 803 self.__writeFirstHeader() # dentro de esta funcion se debe setear e __dataType
804 804
805 805 self.__buffer_sspc = numpy.array([],self.__dataType)
806 806
807 807
808 808
809 809 def __writeBasicHeader(self):
810 810 pass
811 811
812 812 def __writeFirstHeader(self):
813 813 pass
@@ -1,945 +1,950
1 1 '''
2 2 Created on 23/01/2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 8 import os, sys
9 9 import numpy
10 10 import glob
11 11 import fnmatch
12 12 import time, datetime
13 13
14 14 path = os.path.split(os.getcwd())[0]
15 15 sys.path.append(path)
16 16
17 17 from IO.HeaderIO import *
18 18 from IO.DataIO import DataReader
19 19 from IO.DataIO import DataWriter
20 20
21 21 from Model.Voltage import Voltage
22 22
23 23 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
24 24 """
25 25 Esta funcion determina si un archivo de datos en formato Jicamarca(.r) se encuentra
26 26 o no dentro del rango de fecha especificado.
27 27
28 28 Inputs:
29 29 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
30 30
31 31 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
32 32 segundos contados desde 01/01/1970.
33 33 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
34 34 segundos contados desde 01/01/1970.
35 35
36 36 Return:
37 37 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
38 38 fecha especificado, de lo contrario retorna False.
39 39
40 40 Excepciones:
41 41 Si el archivo no existe o no puede ser abierto
42 42 Si la cabecera no puede ser leida.
43 43
44 44 """
45 45 m_BasicHeader = BasicHeader()
46 46
47 47 try:
48 48 fp = open(filename,'rb')
49 49 except:
50 50 raise IOError, "The file %s can't be opened" %(filename)
51 51
52 52 if not(m_BasicHeader.read(fp)):
53 53 raise IOError, "The file %s has not a valid header" %(filename)
54 54
55 55 fp.close()
56 56
57 57 if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds >= m_BasicHeader.utc)):
58 58 return 0
59 59
60 60 return 1
61 61
62 62 class VoltageReader(DataReader):
63 63 """
64 64 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
65 65 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
66 66 perfiles*alturas*canales) son almacenados en la variable "buffer".
67 67
68 68 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
69 69 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
70 70 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
71 71 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
72 72
73 73 Example:
74 74
75 75 dpath = "/home/myuser/data"
76 76
77 77 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
78 78
79 79 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
80 80
81 81 readerObj = VoltageReader()
82 82
83 83 readerObj.setup(dpath, startTime, endTime)
84 84
85 85 while(True):
86 86
87 readerObj.getData()
87 #to get one profile
88 profile = readerObj.getData()
88 89
89 print readerObj.m_Voltage.data
90 #print the profile
91 print profile
92
93 #If you want to see all datablock
94 print readerObj.datablock
90 95
91 96 if readerObj.noMoreFiles:
92 97 break
93 98
94 99 """
95 100
96 101 #speed of light
97 102 __c = 3E8
98 103
99 104 def __init__(self, m_Voltage = None):
100 105 """
101 106 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
102 107
103 108 Input:
104 109 m_Voltage : Objeto de la clase Voltage. Este objeto sera utilizado para
105 110 almacenar un perfil de datos cada vez que se haga un requerimiento
106 111 (getData). El perfil sera obtenido a partir del buffer de datos,
107 112 si el buffer esta vacio se hara un nuevo proceso de lectura de un
108 113 bloque de datos.
109 114 Si este parametro no es pasado se creara uno internamente.
110 115
111 116 Variables afectadas:
112 117 self.m_Voltage
113 118 self.m_BasicHeader
114 119 self.m_SystemHeader
115 120 self.m_RadarControllerHeader
116 121 self.m_ProcessingHeader
117 122
118 123
119 124 Return:
120 125 Void
121 126
122 127 """
123 128 if m_Voltage == None:
124 129 m_Voltage = Voltage()
125 130
126 131 if not(isinstance(m_Voltage, Voltage)):
127 132 raise ValueError, "in VoltageReader, m_Voltage must be an Voltage class object"
128 133
129 134 self.m_Voltage = m_Voltage
130 135
131 136 self.m_BasicHeader = BasicHeader()
132 137
133 138 self.m_SystemHeader = SystemHeader()
134 139
135 140 self.m_RadarControllerHeader = RadarControllerHeader()
136 141
137 142 self.m_ProcessingHeader = ProcessingHeader()
138 143
139 144 self.__fp = None
140 145
141 146 self.__idFile = None
142 147
143 148 self.__startDateTime = None
144 149
145 150 self.__endDateTime = None
146 151
147 152 self.__dataType = None
148 153
149 154 self.__fileSizeByHeader = 0
150 155
151 156 self.__pathList = []
152 157
153 158 self.filenameList = []
154 159
155 160 self.__lastUTTime = 0
156 161
157 162 self.__maxTimeStep = 30
158 163
159 164 self.__flagIsNewFile = 0
160 165
161 166 self.__ippSeconds = 0
162 167
163 168 self.flagResetProcessing = 0
164 169
165 170 self.flagIsNewBlock = 0
166 171
167 172 self.noMoreFiles = 0
168 173
169 174 self.nReadBlocks = 0
170 175
171 176 self.online = 0
172 177
173 178 self.filename = None
174 179
175 180 self.fileSize = None
176 181
177 182 self.firstHeaderSize = 0
178 183
179 184 self.basicHeaderSize = 24
180 185
181 186 self.idProfile = 0
182 187
183 self.__buffer = None
188 self.datablock = None
184 189
185 self.__buffer_id = 9999
190 self.datablock_id = 9999
186 191
187 192 def __rdSystemHeader(self,fp=None):
188 193 if fp == None:
189 194 fp = self.__fp
190 195
191 196 self.m_SystemHeader.read(fp)
192 197
193 198 def __rdRadarControllerHeader(self,fp=None):
194 199 if fp == None:
195 200 fp = self.__fp
196 201
197 202 self.m_RadarControllerHeader.read(fp)
198 203
199 204 def __rdProcessingHeader(self,fp=None):
200 205 if fp == None:
201 206 fp = self.__fp
202 207
203 208 self.m_ProcessingHeader.read(fp)
204 209
205 210 def __rdBasicHeader(self, fp=None):
206 211
207 212 if fp == None:
208 213 fp = self.__fp
209 214
210 215 self.m_BasicHeader.read(fp)
211 216
212 217 def __readFirstHeader(self):
213 218
214 219 self.__rdBasicHeader()
215 220 self.__rdSystemHeader()
216 221 self.__rdRadarControllerHeader()
217 222 self.__rdProcessingHeader()
218 223 self.firstHeaderSize = self.m_BasicHeader.size
219 224
220 225 data_type=int(numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
221 226 if data_type == 0:
222 227 tmp = numpy.dtype([('real','<i1'),('imag','<i1')])
223 228
224 229 elif data_type == 1:
225 230 tmp = numpy.dtype([('real','<i2'),('imag','<i2')])
226 231
227 232 elif data_type == 2:
228 233 tmp = numpy.dtype([('real','<i4'),('imag','<i4')])
229 234
230 235 elif data_type == 3:
231 236 tmp = numpy.dtype([('real','<i8'),('imag','<i8')])
232 237
233 238 elif data_type == 4:
234 239 tmp = numpy.dtype([('real','<f4'),('imag','<f4')])
235 240
236 241 elif data_type == 5:
237 242 tmp = numpy.dtype([('real','<f8'),('imag','<f8')])
238 243
239 244 else:
240 245 raise ValueError, 'Data type was not defined'
241 246
242 247 xi = self.m_ProcessingHeader.firstHeight
243 248 step = self.m_ProcessingHeader.deltaHeight
244 249 xf = xi + self.m_ProcessingHeader.numHeights*step
245 250
246 251 self.__heights = numpy.arange(xi, xf, step)
247 252 self.__dataType = tmp
248 253 self.__fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1)
249 254 self.__ippSeconds = 2*1000*self.m_RadarControllerHeader.ipp/self.__c
250 255
251 256 def __setNextFileOnline(self):
252 257 return 0
253 258
254 259 def __setNextFileOffline(self):
255 260
256 261 idFile = self.__idFile
257 262 while(True):
258 263
259 264 idFile += 1
260 265
261 266 if not(idFile < len(self.filenameList)):
262 267 self.noMoreFiles = 1
263 268 return 0
264 269
265 270 filename = self.filenameList[idFile]
266 271 fileSize = os.path.getsize(filename)
267 272
268 273 try:
269 274 fp = open(filename,'rb')
270 275 except:
271 276 raise IOError, "The file %s can't be opened" %filename
272 277
273 278 currentSize = fileSize - fp.tell()
274 279 neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize
275 280
276 281 if (currentSize < neededSize):
277 282 print "Skipping the file %s due to it hasn't enough data" %filename
278 283 continue
279 284
280 285 break
281 286
282 287 self.__flagIsNewFile = 1
283 288 self.__idFile = idFile
284 289 self.filename = filename
285 290 self.fileSize = fileSize
286 291 self.__fp = fp
287 292
288 293 print 'Setting the file: %s'%self.filename
289 294
290 295 return 1
291 296
292 297 def __setNextFile(self):
293 298
294 299 if self.online:
295 300 newFile = self.__setNextFileOnline()
296 301 else:
297 302 newFile = self.__setNextFileOffline()
298 303
299 304 if not(newFile):
300 305 return 0
301 306
302 307 self.__readFirstHeader()
303 308
304 309 return 1
305 310
306 311 def __setNewBlock(self):
307 312
308 313 if self.__fp == None:
309 314 return 0
310 315
311 316 if self.__flagIsNewFile:
312 317 return 1
313 318
314 319 currentSize = self.fileSize - self.__fp.tell()
315 320 neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize
316 321
317 322 #If there is enough data setting new data block
318 323 if (currentSize >= neededSize):
319 324 self.__rdBasicHeader()
320 325 return 1
321 326
322 327 #Setting new file
323 328 if not(self.__setNextFile()):
324 329 return 0
325 330
326 331 deltaTime = self.m_BasicHeader.utc - self.__lastUTTime # check this
327 332
328 333 self.flagResetProcessing = 0
329 334
330 335 if deltaTime > self.__maxTimeStep:
331 336 self.flagResetProcessing = 1
332 337 self.nReadBlocks = 0
333 338
334 339 return 1
335 340
336 341 def __readBlock(self):
337 342 """
338 343 __readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
339 344 (self.__fp) y actualiza todos los parametros relacionados al bloque de datos
340 345 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
341 346 es seteado a 0
342 347
343 348
344 349 Inputs:
345 350 None
346 351
347 352 Return:
348 353 None
349 354
350 355 Variables afectadas:
351 356
352 self.__buffer_id
357 self.datablock_id
353 358
354 self.__buffer
359 self.datablock
355 360
356 361 self.__flagIsNewFile
357 362
358 363 self.idProfile
359 364
360 365 self.flagIsNewBlock
361 366
362 367 self.nReadBlocks
363 368
364 369 """
365 370
366 371 pts2read = self.m_ProcessingHeader.profilesPerBlock*self.m_ProcessingHeader.numHeights*self.m_SystemHeader.numChannels
367 372
368 373 junk = numpy.fromfile(self.__fp, self.__dataType, pts2read)
369 374
370 375 junk = junk.reshape((self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, self.m_SystemHeader.numChannels))
371 376
372 377 data = junk['real'] + junk['imag']*1j
373 378
374 self.__buffer_id = 0
379 self.datablock_id = 0
375 380
376 self.__buffer = data
381 self.datablock = data
377 382
378 383 self.__flagIsNewFile = 0
379 384
380 385 self.idProfile = 0
381 386
382 387 self.flagIsNewBlock = 1
383 388
384 389 self.nReadBlocks += 1
385 390
386 391 def __hasNotDataInBuffer(self):
387 if self.__buffer_id >= self.m_ProcessingHeader.profilesPerBlock:
392 if self.datablock_id >= self.m_ProcessingHeader.profilesPerBlock:
388 393 return 1
389 394
390 395 return 0
391 396
392 397 def __searchFiles(self, path, startDateTime, endDateTime, set=None, expLabel = "", ext = ".r"):
393 398 """
394 399 __searchFiles realiza una busqueda de los archivos que coincidan con los parametros
395 400 especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda
396 401 correcta la estructura de directorios debe ser la siguiente:
397 402
398 403 ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext
399 404
400 405 [yyyy]: anio
401 406 [ddd] : dia del anio
402 407 [sss] : set del archivo
403 408
404 409 Inputs:
405 410 path : Directorio de datos donde se realizara la busqueda. Todos los
406 411 ficheros que concidan con el criterio de busqueda seran
407 412 almacenados en una lista y luego retornados.
408 413 startDateTime : Fecha inicial. Rechaza todos los archivos donde
409 414 file end time < startDateTime (obejto datetime.datetime)
410 415
411 416 endDateTime : Fecha final. Rechaza todos los archivos donde
412 417 file start time > endDateTime (obejto datetime.datetime)
413 418
414 419 set : Set del primer archivo a leer. Por defecto None
415 420
416 421 expLabel : Nombre del subdirectorio de datos. Por defecto ""
417 422
418 423 ext : Extension de los archivos a leer. Por defecto .r
419 424
420 425 Return:
421 426
422 427 (pathList, filenameList)
423 428
424 429 pathList : Lista de directorios donde se encontraron archivos dentro
425 430 de los parametros especificados
426 431 filenameList : Lista de archivos (ruta completa) que coincidieron con los
427 432 parametros especificados.
428 433
429 434 Variables afectadas:
430 435
431 436 self.filenameList: Lista de archivos (ruta completa) que la clase utiliza
432 437 como fuente para leer los bloque de datos, si se termina
433 438 de leer todos los bloques de datos de un determinado
434 439 archivo se pasa al siguiente archivo de la lista.
435 440
436 441 Excepciones:
437 442
438 443 """
439 444
440 445 print "Searching files ..."
441 446
442 447 dirList = []
443 448 for thisPath in os.listdir(path):
444 449 if os.path.isdir(os.path.join(path,thisPath)):
445 450 dirList.append(thisPath)
446 451
447 452 pathList = []
448 453
449 454 thisDateTime = startDateTime
450 455
451 456 while(thisDateTime <= endDateTime):
452 457 year = thisDateTime.timetuple().tm_year
453 458 doy = thisDateTime.timetuple().tm_yday
454 459
455 460 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
456 461 if len(match) == 0:
457 462 thisDateTime += datetime.timedelta(1)
458 463 continue
459 464
460 465 pathList.append(os.path.join(path,match[0],expLabel))
461 466 thisDateTime += datetime.timedelta(1)
462 467
463 468 startUtSeconds = time.mktime(startDateTime.timetuple())
464 469 endUtSeconds = time.mktime(endDateTime.timetuple())
465 470
466 471 filenameList = []
467 472 for thisPath in pathList:
468 473 fileList = glob.glob1(thisPath, "*%s" %ext)
469 474 fileList.sort()
470 475 for file in fileList:
471 476 filename = os.path.join(thisPath,file)
472 477 if isThisFileinRange(filename, startUtSeconds, endUtSeconds):
473 478 filenameList.append(filename)
474 479
475 480 self.filenameList = filenameList
476 481
477 482 return pathList, filenameList
478 483
479 484 def setup(self, path, startDateTime, endDateTime=None, set=None, expLabel = "", ext = ".r", online = 0):
480 485 """
481 486 setup configura los parametros de lectura de la clase VoltageReader.
482 487
483 488 Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos
484 489 que coincidan con los parametros especificados; esta lista de archivos son almacenados en
485 490 self.filenameList.
486 491
487 492 Input:
488 493 path : Directorios donde se ubican los datos a leer. Dentro de este
489 494 directorio deberia de estar subdirectorios de la forma:
490 495
491 496 path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext]
492 497
493 498 startDateTime : Fecha inicial. Rechaza todos los archivos donde
494 499 file end time < startDatetime (obejto datetime.datetime)
495 500
496 501 endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde
497 502 file end time < startDatetime (obejto datetime.datetime)
498 503
499 504 set : Set del primer archivo a leer. Por defecto None
500 505
501 506 expLabel : Nombre del subdirectorio de datos. Por defecto ""
502 507
503 508 ext : Extension de los archivos a leer. Por defecto .r
504 509
505 510 online :
506 511
507 512 Return:
508 513
509 514 Affected:
510 515
511 516 Excepciones:
512 517
513 518 Example:
514 519
515 520 """
516 521
517 522 if online == 0:
518 523 pathList, filenameList = self.__searchFiles(path, startDateTime, endDateTime, set, expLabel, ext)
519 524
520 525 self.__idFile = -1
521 526
522 527 if not(self.__setNextFile()):
523 528 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
524 529 return 0
525 530
526 531 self.startUTCSeconds = time.mktime(startDateTime.timetuple())
527 532 self.endUTCSeconds = time.mktime(endDateTime.timetuple())
528 533
529 534 self.startYear = startDateTime.timetuple().tm_year
530 535 self.endYear = endDateTime.timetuple().tm_year
531 536
532 537 self.startDoy = startDateTime.timetuple().tm_yday
533 538 self.endDoy = endDateTime.timetuple().tm_yday
534 539
535 540 self.m_Voltage.m_BasicHeader = self.m_BasicHeader.copy()
536 541 self.m_Voltage.m_ProcessingHeader = self.m_ProcessingHeader.copy()
537 542 self.m_Voltage.m_RadarControllerHeader = self.m_RadarControllerHeader.copy()
538 543 self.m_Voltage.m_SystemHeader = self.m_SystemHeader.copy()
539 544 self.m_Voltage.dataType = self.__dataType
540 545
541 546 self.__pathList = pathList
542 547 self.filenameList = filenameList
543 548 self.online = online
544 549
545 550 return 1
546 551
547 552 def readNextBlock(self):
548 553 """
549 554 readNextBlock establece un nuevo bloque de datos a leer y los lee, si es que no existiese
550 555 mas bloques disponibles en el archivo actual salta al siguiente.
551 556
552 557 """
553 558
554 559 if not(self.__setNewBlock()):
555 560 return 0
556 561
557 562 self.__readBlock()
558 563
559 564 self.__lastUTTime = self.m_BasicHeader.utc
560 565
561 566 return 1
562 567
563 568 def getData(self):
564 569 """
565 570 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
566 571 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
567 572 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
568 573
569 574 Ademas incrementa el contador del buffer en 1.
570 575
571 576 Inputs:
572 577 None
573 578
574 579 Return:
575 580 data : retorna un perfil de voltages (alturas * canales) copiados desde el
576 581 buffer. Si no hay mas archivos a leer retorna None.
577 582
578 583 Variables afectadas:
579 584 self.m_Voltage
580 self.__buffer_id
585 self.datablock_id
581 586 self.idProfile
582 587
583 588 Excepciones:
584 589
585 590 """
586 591 self.flagResetProcessing = 0
587 592 self.flagIsNewBlock = 0
588 593
589 594 if self.__hasNotDataInBuffer():
590 595 self.readNextBlock()
591 596
592 597 self.m_Voltage.m_BasicHeader = self.m_BasicHeader.copy()
593 598 self.m_Voltage.m_ProcessingHeader = self.m_ProcessingHeader.copy()
594 599 self.m_Voltage.m_RadarControllerHeader = self.m_RadarControllerHeader.copy()
595 600 self.m_Voltage.m_SystemHeader = self.m_SystemHeader.copy()
596 601
597 602 self.m_Voltage.heights = self.__heights
598 603 self.m_Voltage.dataType = self.__dataType
599 604
600 605 if self.noMoreFiles == 1:
601 606 print 'Process finished'
602 607 return None
603 608
604 609 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
605 610
606 time = self.m_BasicHeader.utc + self.__buffer_id*self.__ippSeconds
611 time = self.m_BasicHeader.utc + self.datablock_id*self.__ippSeconds
607 612 self.m_Voltage.m_BasicHeader.utc = time
608 self.m_Voltage.data = self.__buffer[self.__buffer_id,:,:]
613 self.m_Voltage.data = self.datablock[self.datablock_id,:,:]
609 614 self.m_Voltage.flagNoData = False
610 615 self.m_Voltage.flagResetProcessing = self.flagResetProcessing
611 616
612 617 self.m_Voltage.idProfile = self.idProfile
613 618
614 619
615 self.__buffer_id += 1
620 self.datablock_id += 1
616 621 self.idProfile += 1
617 622
618 623 #call setData - to Data Object
619 624
620 return 1
625 return self.m_Voltage.data
621 626
622 627 class VoltageWriter(DataWriter):
623 628 __configHeaderFile = 'wrSetHeadet.txt'
624 629
625 630 def __init__(self, m_Voltage = None):
626 631
627 632 if m_Voltage == None:
628 633 m_Voltage = Voltage()
629 634
630 635 self.m_Voltage = m_Voltage
631 636
632 637 self.__path = None
633 638
634 639 self.__fp = None
635 640
636 641 self.__format = None
637 642
638 643 self.__blocksCounter = 0
639 644
640 645 self.__setFile = None
641 646
642 647 self.__flagIsNewFile = 1
643 648
644 self.__buffer = None
649 self.datablock = None
645 650
646 self.__buffer_id = 0
651 self.datablock_id = 0
647 652
648 653 self.__dataType = None
649 654
650 655 self.__ext = None
651 656
652 657 self.__shapeBuffer = None
653 658
654 659 self.nWriteBlocks = 0
655 660
656 661 self.flagIsNewBlock = 0
657 662
658 663 self.noMoreFiles = 0
659 664
660 665 self.filename = None
661 666
662 667 self.m_BasicHeader= BasicHeader()
663 668
664 669 self.m_SystemHeader = SystemHeader()
665 670
666 671 self.m_RadarControllerHeader = RadarControllerHeader()
667 672
668 673 self.m_ProcessingHeader = ProcessingHeader()
669 674
670 675
671 676 def __writeBasicHeader(self, fp=None):
672 677 if fp == None:
673 678 fp = self.__fp
674 679
675 680 self.m_BasicHeader.write(fp)
676 681
677 682 def __wrSystemHeader(self,fp=None):
678 683 if fp == None:
679 684 fp = self.__fp
680 685
681 686 self.m_SystemHeader.write(fp)
682 687
683 688 def __wrRadarControllerHeader(self,fp=None):
684 689 if fp == None:
685 690 fp = self.__fp
686 691
687 692 self.m_RadarControllerHeader.write(fp)
688 693
689 694 def __wrProcessingHeader(self,fp=None):
690 695 if fp == None:
691 696 fp = self.__fp
692 697
693 698 self.m_ProcessingHeader.write(fp)
694 699
695 700 def __writeFirstHeader(self):
696 701 self.__writeBasicHeader()
697 702 self.__wrSystemHeader()
698 703 self.__wrRadarControllerHeader()
699 704 self.__wrProcessingHeader()
700 705 self.__dataType = self.m_Voltage.dataType
701 706
702 707
703 708 def __setNextFile(self):
704 709
705 710 setFile = self.__setFile
706 711 ext = self.__ext
707 712 path = self.__path
708 713
709 714 setFile += 1
710 715
711 716 if self.__fp != None:
712 717 self.__fp.close()
713 718
714 719 timeTuple = time.localtime(self.m_Voltage.m_BasicHeader.utc) # utc from m_Voltage
715 720 file = 'D%4.4d%3.3d%3.3d%s' % (timeTuple.tm_year,timeTuple.tm_yday,setFile,ext)
716 721 subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
717 722 tmp = os.path.join(path,subfolder)
718 723 if not(os.path.exists(tmp)):
719 724 os.mkdir(tmp)
720 725
721 726 filename = os.path.join(path,subfolder,file)
722 727 fp = open(filename,'wb')
723 728
724 729 self.__blocksCounter = 0
725 730
726 731 #guardando atributos
727 732 self.filename = filename
728 733 self.__subfolder = subfolder
729 734 self.__fp = fp
730 735 self.__setFile = setFile
731 736 self.__flagIsNewFile = 1
732 737
733 738 print 'Writing the file: %s'%self.filename
734 739
735 740 self.__writeFirstHeader()
736 741
737 742 return 1
738 743
739 744 def __setNewBlock(self):
740 745
741 746 if self.__fp == None:
742 747 self.__setNextFile()
743 748
744 749 if self.__flagIsNewFile:
745 750 return 1
746 751
747 752 if self.__blocksCounter < self.m_ProcessingHeader.dataBlocksPerFile:
748 753 self.__writeBasicHeader()
749 754 return 1
750 755
751 756 if not(self.__setNextFile()):
752 757 return 0
753 758
754 759 return 1
755 760
756 761 def __writeBlock(self):
757 762
758 763 data = numpy.zeros(self.__shapeBuffer, self.__dataType)
759 764
760 data['real'] = self.__buffer.real
761 data['imag'] = self.__buffer.imag
765 data['real'] = self.datablock.real
766 data['imag'] = self.datablock.imag
762 767
763 768 data = data.reshape((-1))
764 769
765 770 data.tofile(self.__fp)
766 771
767 self.__buffer.fill(0)
772 self.datablock.fill(0)
768 773
769 self.__buffer_id = 0
774 self.datablock_id = 0
770 775
771 776 self.__flagIsNewFile = 0
772 777
773 778 self.flagIsNewBlock = 1
774 779
775 780 self.nWriteBlocks += 1
776 781
777 782 self.__blocksCounter += 1
778 783
779 784
780 785 def writeNextBlock(self):
781 786
782 787 if not(self.__setNewBlock()):
783 788 return 0
784 789
785 790 self.__writeBlock()
786 791
787 792 return 1
788 793
789 794 def __hasAllDataInBuffer(self):
790 if self.__buffer_id >= self.m_ProcessingHeader.profilesPerBlock:
795 if self.datablock_id >= self.m_ProcessingHeader.profilesPerBlock:
791 796 return 1
792 797
793 798 return 0
794 799
795 800 def putData(self):
796 801
797 802 self.flagIsNewBlock = 0
798 803
799 804 if self.m_Voltage.flagNoData:
800 805 return 0
801 806
802 807 if self.m_Voltage.flagResetProcessing:
803 808
804 self.__buffer.fill(0)
809 self.datablock.fill(0)
805 810
806 self.__buffer_id = 0
811 self.datablock_id = 0
807 812 self.__setNextFile()
808 813
809 self.__buffer[self.__buffer_id,:,:] = self.m_Voltage.data
814 self.datablock[self.datablock_id,:,:] = self.m_Voltage.data
810 815
811 self.__buffer_id += 1
816 self.datablock_id += 1
812 817
813 818 if self.__hasAllDataInBuffer():
814 819
815 820 self.__getHeader()
816 821 self.writeNextBlock()
817 822
818 823 if self.noMoreFiles:
819 824 #print 'Process finished'
820 825 return 0
821 826
822 827 return 1
823 828
824 829 def __getHeader(self):
825 830 self.m_BasicHeader = self.m_Voltage.m_BasicHeader.copy()
826 831 self.m_SystemHeader = self.m_Voltage.m_SystemHeader.copy()
827 832 self.m_RadarControllerHeader = self.m_Voltage.m_RadarControllerHeader.copy()
828 833 self.m_ProcessingHeader = self.m_Voltage.m_ProcessingHeader.copy()
829 834 self.__dataType = self.m_Voltage.dataType
830 835
831 836 def __setHeaderByFile(self):
832 837
833 838 format = self.__format
834 839 header = ['Basic','System','RadarController','Processing']
835 840
836 841 fmtFromFile = None
837 842 headerFromFile = None
838 843
839 844
840 845 fileTable = self.__configHeaderFile
841 846
842 847 if os.access(fileTable, os.R_OK):
843 848 import re, string
844 849
845 850 f = open(fileTable,'r')
846 851 lines = f.read()
847 852 f.close()
848 853
849 854 #Delete comments into expConfig
850 855 while 1:
851 856
852 857 startComment = string.find(lines.lower(),'#')
853 858 if startComment == -1:
854 859 break
855 860 endComment = string.find(lines.lower(),'\n',startComment)
856 861 lines = string.replace(lines,lines[startComment:endComment+1],'', 1)
857 862
858 863 while expFromFile == None:
859 864
860 865 currFmt = string.find(lines.lower(),'format="%s"' %(expName))
861 866 nextFmt = string.find(lines.lower(),'format',currFmt+10)
862 867
863 868 if currFmt == -1:
864 869 break
865 870 if nextFmt == -1:
866 871 nextFmt = len(lines)-1
867 872
868 873 fmtTable = lines[currFmt:nextFmt]
869 874 lines = lines[nextFmt:]
870 875
871 876 fmtRead = self.__getValueFromArg(fmtTable,'format')
872 877 if fmtRead != format:
873 878 continue
874 879 fmtFromFile = fmtRead
875 880
876 881 lines2 = fmtTable
877 882
878 883 while headerFromFile == None:
879 884
880 885 currHeader = string.find(lines2.lower(),'header="%s"' %(header))
881 886 nextHeader = string.find(lines2.lower(),'header',currHeader+10)
882 887
883 888 if currHeader == -1:
884 889 break
885 890 if nextHeader == -1:
886 891 nextHeader = len(lines2)-1
887 892
888 893 headerTable = lines2[currHeader:nextHeader]
889 894 lines2 = lines2[nextHeader:]
890 895
891 896 headerRead = self.__getValueFromArg(headerTable,'site')
892 897 if not(headerRead in header):
893 898 continue
894 899 headerFromFile = headerRead
895 900
896 901 if headerRead == 'Basic':
897 902 self.m_BasicHeader.size = self.__getValueFromArg(headerTable,'size',lower=False)
898 903 self.m_BasicHeader.version = self.__getValueFromArg(headerTable,'version',lower=False)
899 904 self.m_BasicHeader.dataBlock = self.__getValueFromArg(headerTable,'dataBlock',lower=False)
900 905 self.m_BasicHeader.utc = self.__getValueFromArg(headerTable,'utc',lower=False)
901 906 self.m_BasicHeader.miliSecond = self.__getValueFromArg(headerTable,'miliSecond',lower=False)
902 907 self.m_BasicHeader.timeZone = self.__getValueFromArg(headerTable,'timeZone',lower=False)
903 908 self.m_BasicHeader.dstFlag = self.__getValueFromArg(headerTable,'dstFlag',lower=False)
904 909 self.m_BasicHeader.errorCount = self.__getValueFromArg(headerTable,'errorCount',lower=False)
905 910
906 911 else:
907 912 print "file access denied:%s"%fileTable
908 913 sys.exit(0)
909 914
910 915 def setup(self, path, set=0, format='rawdata'):
911 916
912 917
913 918 if format == 'hdf5':
914 919 ext = '.hdf5'
915 920 format = 'hdf5'
916 921 print 'call hdf5 library'
917 922 return 0
918 923
919 924 if format == 'rawdata':
920 925 ext = '.r'
921 926 format = 'Jicamarca'
922 927
923 928 #call to config_headers
924 929 #self.__setHeaderByFile()
925 930
926 931 self.__path = path
927 932 self.__setFile = set - 1
928 933 self.__ext = ext
929 934 self.__format = format
930 935
931 936 self.__getHeader()
932 937 self.__shapeBuffer = (self.m_ProcessingHeader.profilesPerBlock,
933 938 self.m_ProcessingHeader.numHeights,
934 939 self.m_SystemHeader.numChannels )
935 940
936 self.__buffer = numpy.zeros(self.__shapeBuffer, numpy.dtype('complex'))
941 self.datablock = numpy.zeros(self.__shapeBuffer, numpy.dtype('complex'))
937 942
938 943 # if not(self.__setNextFile()):
939 944 # return 0
940 945 return 1
941 946
942 947
943 948
944 949
945 950 No newline at end of file
@@ -1,74 +1,75
1 1 '''
2 2 Created on 23/01/2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7 import os, sys
8 8 import time, datetime
9 9
10 10 from Model.Voltage import Voltage
11 11 from IO.VoltageIO import *
12 12 from Graphics.VoltagePlot import Osciloscope
13 13
14 from Model.Spectra import Spectra
15 from IO.SpectraIO import *
16 from Graphics.SpectraPlot import Spectrum
17
14 18 class TestSChain():
15 19
16 20
17 21 def __init__(self):
18 22 self.setValues()
19 23 self.createObjects()
20 24 self.testSChain()
21 25 pass
22 26
23 27 def setValues(self):
24 28
25 29 self.path = '/home/roj-idl71/Data/RAWDATA/DP_Faraday/'
26 30 self.path = '/Users/danielangelsuarezmunoz/Documents/Projects/testWR'
27 31 self.path = '/home/roj-idl71/Data/RAWDATA/IMAGING'
28 self.path = '/home/roj-idl71/tmp/data'
32 # self.path = '/home/roj-idl71/tmp/data'
29 33 #self.path = '/remote/puma/2004_11/DVD/'
30 34
31 35 self.ppath = "/home/roj-idl71/tmp/data"
32 self.startDateTime = datetime.datetime(2004,5,1,17,49,0)
33 self.endDateTime = datetime.datetime(2012,5,1,18,10,0)
36 self.startDateTime = datetime.datetime(2011,1,1,17,49,0)
37 self.endDateTime = datetime.datetime(2011,1,30,18,10,0)
34 38
35 39 def createObjects(self):
36 40
37 self.voltageObj = Voltage()
38 self.readerObj = VoltageReader(self.voltageObj)
39 self.plotObj = Osciloscope(self.voltageObj)
40 self.writerObj = VoltageWriter(self.voltageObj)
41 self.Obj = Spectra()
42 self.readerObj = SpectraReader(self.Obj)
43 self.plotObj = Spectrum(self.Obj)
44 # self.writerObj = SpectraWriter(self.Obj)
41 45
42 if not(self.readerObj.setup(self.path, self.startDateTime, self.endDateTime)):
46 if not(self.readerObj.setup(self.path, self.startDateTime, self.endDateTime, expLabel='')):
43 47 sys.exit(0)
44 48
45 if not(self.writerObj.setup(self.ppath)):
46 sys.exit(0)
49 # if not(self.writerObj.setup(self.ppath)):
50 # sys.exit(0)
47 51
48 52 def testSChain(self):
49 53
50 54 ini = time.time()
51 55 while(True):
52 56 self.readerObj.getData()
53 self.plotObj.plotData(idProfile = 1, type='iq', ymin = -100, ymax = 100)
57 self.plotObj.plotData(showColorbar=False, showPowerProfile=True)
54 58
55 59 # self.writerObj.putData()
56 60
57 61 if self.readerObj.noMoreFiles:
58 62 break
59 63
60 64 if self.readerObj.flagIsNewBlock:
61 65 print 'Block No %04d, Time: %s' %(self.readerObj.nReadBlocks,
62 66 datetime.datetime.fromtimestamp(self.readerObj.m_BasicHeader.utc),)
63 67 fin = time.time()
64 68 print 'Tiempo de un bloque leido y escrito: [%6.5f]' %(fin - ini)
65 ini = time.time()
66
67
68
69
69 ini = time.time()
70 70
71 #time.sleep(0.5)
71 72 self.plotObj.end()
72 73
73 74 if __name__ == '__main__':
74 75 TestSChain() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now