##// END OF EJS Templates
Python 2to3, Spectra (all operations) working
George Yong -
r1167:1f521b07c958
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,7 +1,7
1 1 '''
2 Created on Feb 7, 2012
2 Created on Jul 3, 2018
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 __version__ = '2.3'
7 __version__ = '3.0'
@@ -1,503 +1,503
1 1 """
2 2 The admin module contains all administrative classes relating to the schain python api.
3 3
4 4 The main role of this module is to send some reports. It contains a
5 5 notification class and a standard error handing class.
6 6
7 7 $Id: admin.py 3966 2015-12-01 14:32:29Z miguel.urco $
8 8 """
9 9 import os
10 10 import sys
11 11 import time
12 12 import traceback
13 13 import smtplib
14 import ConfigParser
15 import StringIO
14 import configparser
15 import io
16 16 from threading import Thread
17 17 from multiprocessing import Process
18 18 from email.mime.text import MIMEText
19 19 from email.mime.application import MIMEApplication
20 20 from email.mime.multipart import MIMEMultipart
21 21
22 22 import schainpy
23 23 from schainpy.utils import log
24 24 from schainpy.model.graphics.jroplot_data import popup
25 25
26 26 def get_path():
27 27 '''
28 28 Return schainpy path
29 29 '''
30 30
31 31 try:
32 32 root = __file__
33 33 if os.path.islink(root):
34 34 root = os.path.realpath(root)
35 35
36 36 return os.path.dirname(os.path.abspath(root))
37 37 except:
38 38 log.error('I am sorry, but something is wrong... __file__ not found')
39 39
40 40 class Alarm(Process):
41 41 '''
42 42 modes:
43 43 0 - All
44 44 1 - Send email
45 45 2 - Popup message
46 46 3 - Sound alarm
47 47 4 - Send to alarm system TODO
48 48 '''
49 49
50 50 def __init__(self, modes=[], **kwargs):
51 51 Process.__init__(self)
52 52 self.modes = modes
53 53 self.kwargs = kwargs
54 54
55 55 @staticmethod
56 56 def play_sound():
57 57 sound = os.path.join(get_path(), 'alarm1.oga')
58 58 if os.path.exists(sound):
59 59 for __ in range(2):
60 60 os.system('paplay {}'.format(sound))
61 61 time.sleep(0.5)
62 62 else:
63 63 log.warning('Unable to play alarm, sound file not found', 'ADMIN')
64 64
65 65 @staticmethod
66 66 def send_email(**kwargs):
67 67 notifier = SchainNotify()
68 print kwargs
68 print(kwargs)
69 69 notifier.notify(**kwargs)
70 70
71 71 @staticmethod
72 72 def show_popup(message):
73 73 if isinstance(message, list):
74 74 message = message[-1]
75 75 popup(message)
76 76
77 77 @staticmethod
78 78 def send_alarm():
79 79 pass
80 80
81 81 @staticmethod
82 82 def get_kwargs(kwargs, keys):
83 83 ret = {}
84 84 for key in keys:
85 85 ret[key] = kwargs[key]
86 86 return ret
87 87
88 88 def run(self):
89 89 tasks = {
90 90 1 : self.send_email,
91 91 2 : self.show_popup,
92 92 3 : self.play_sound,
93 93 4 : self.send_alarm,
94 94 }
95 95
96 96 tasks_args = {
97 97 1: ['email', 'message', 'subject', 'subtitle', 'filename'],
98 98 2: ['message'],
99 99 3: [],
100 100 4: [],
101 101 }
102 102 procs = []
103 103 for mode in self.modes:
104 104 if 0 in self.modes:
105 105 for x in tasks:
106 106 t = Thread(target=tasks[x], kwargs=self.get_kwargs(self.kwargs, tasks_args[x]))
107 107 t.start()
108 108 procs.append(t)
109 109 break
110 110 else:
111 111 t = Thread(target=tasks[mode], kwargs=self.get_kwargs(self.kwargs, tasks_args[mode]))
112 112 t.start()
113 113 procs.append(t)
114 114 for t in procs:
115 115 t.join()
116 116
117 117
118 118 class SchainConfigure():
119 119
120 120 __DEFAULT_ADMINISTRATOR_EMAIL = "juan.espinoza@jro.igp.gob.pe"
121 121 __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe"
122 122 __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe"
123 123 __DEFAULT_SENDER_PASS = ""
124 124
125 125 __SCHAIN_ADMINISTRATOR_EMAIL = "CONTACT"
126 126 __SCHAIN_EMAIL_SERVER = "MAILSERVER"
127 127 __SCHAIN_SENDER_EMAIL = "MAILSERVER_ACCOUNT"
128 128 __SCHAIN_SENDER_PASS = "MAILSERVER_PASSWORD"
129 129
130 130 def __init__(self, initFile = None):
131 131
132 132 # Set configuration file
133 133 if (initFile == None):
134 134 self.__confFilePath = "/etc/schain.conf"
135 135 else:
136 136 self.__confFilePath = initFile
137 137
138 138 # open configuration file
139 139 try:
140 140 self.__confFile = open(self.__confFilePath, "r")
141 141 except IOError:
142 142 # can't read from file - use all hard-coded values
143 143 self.__initFromHardCode()
144 144 return
145 145
146 146 # create Parser using standard module ConfigParser
147 self.__parser = ConfigParser.ConfigParser()
147 self.__parser = configparser.ConfigParser()
148 148
149 149 # read conf file into a StringIO with "[madrigal]\n" section heading prepended
150 strConfFile = StringIO.StringIO("[schain]\n" + self.__confFile.read())
150 strConfFile = io.StringIO("[schain]\n" + self.__confFile.read())
151 151
152 152 # parse StringIO configuration file
153 153 self.__parser.readfp(strConfFile)
154 154
155 155 # read information from configuration file
156 156 self.__readConfFile()
157 157
158 158 # close conf file
159 159 self.__confFile.close()
160 160
161 161
162 162 def __initFromHardCode(self):
163 163
164 164 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
165 165 self.__sender_pass = self.__DEFAULT_SENDER_PASS
166 166 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
167 167 self.__email_server = self.__DEFAULT_EMAIL_SERVER
168 168
169 169 def __readConfFile(self):
170 170 """__readConfFile is a private helper function that reads information from the parsed config file.
171 171
172 172 Inputs: None
173 173
174 174 Returns: Void.
175 175
176 176 Affects: Initializes class member variables that are found in the config file.
177 177
178 178 Exceptions: MadrigalError thrown if any key not found.
179 179 """
180 180
181 181 # get the sender email
182 182 try:
183 183 self.__sender_email = self.__parser.get("schain", self.__SCHAIN_SENDER_EMAIL)
184 184 except:
185 185 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
186 186
187 187 # get the sender password
188 188 try:
189 189 self.__sender_pass = self.__parser.get("schain", self.__SCHAIN_SENDER_PASS)
190 190 except:
191 191 self.__sender_pass = self.__DEFAULT_SENDER_PASS
192 192
193 193 # get the administrator email
194 194 try:
195 195 self.__admin_email = self.__parser.get("schain", self.__SCHAIN_ADMINISTRATOR_EMAIL)
196 196 except:
197 197 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
198 198
199 199 # get the server email
200 200 try:
201 201 self.__email_server = self.__parser.get("schain", self.__SCHAIN_EMAIL_SERVER)
202 202 except:
203 203 self.__email_server = self.__DEFAULT_EMAIL_SERVER
204 204
205 205 def getEmailServer(self):
206 206
207 207 return self.__email_server
208 208
209 209 def getSenderEmail(self):
210 210
211 211 return self.__sender_email
212 212
213 213 def getSenderPass(self):
214 214
215 215 return self.__sender_pass
216 216
217 217 def getAdminEmail(self):
218 218
219 219 return self.__admin_email
220 220
221 221 class SchainNotify:
222 222 """SchainNotify is an object used to send messages to an administrator about a Schain software.
223 223
224 224 This object provides functions needed to send messages to an administrator about a Schain , for now
225 225 only sendAlert, which sends an email to the site administrator found is ADMIN_EMAIL
226 226
227 227 Usage example:
228 228
229 229 import schainpy.admin
230 230
231 231 try:
232 232
233 233 adminObj = schainpy.admin.SchainNotify()
234 234 adminObj.sendAlert('This is important!', 'Important Message')
235 235
236 236 except schainpy.admin.SchainError, e:
237 237
238 238 print e.getExceptionStr()
239 239
240 240
241 241 Non-standard Python modules used:
242 242 None
243 243
244 244 Exceptions thrown: None - Note that SchainNotify tries every trick it knows to avoid
245 245 throwing exceptions, since this is the class that will generally be called when there is a problem.
246 246
247 247 Change history:
248 248
249 249 Written by "Miguel Urco":mailto:miguel.urco@jro.igp.gob.pe Dec. 1, 2015
250 250 """
251 251
252 252 #constants
253 253
254 254 def __init__(self):
255 255 """__init__ initializes SchainNotify by getting some basic information from SchainDB and SchainSite.
256 256
257 257 Note that SchainNotify tries every trick it knows to avoid throwing exceptions, since
258 258 this is the class that will generally be called when there is a problem.
259 259
260 260 Inputs: Existing SchainDB object, by default = None.
261 261
262 262 Returns: void
263 263
264 264 Affects: Initializes self.__binDir.
265 265
266 266 Exceptions: None.
267 267 """
268 268
269 269 # note that the main configuration file is unavailable
270 270 # the best that can be done is send an email to root using localhost mailserver
271 271 confObj = SchainConfigure()
272 272
273 273 self.__emailFromAddress = confObj.getSenderEmail()
274 274 self.__emailPass = confObj.getSenderPass()
275 275 self.__emailToAddress = confObj.getAdminEmail()
276 276 self.__emailServer = confObj.getEmailServer()
277 277
278 278 def sendEmail(self, email_from, email_to, subject='Error running ...', message="", subtitle="", filename="", html_format=True):
279 279
280 280 if not email_to:
281 281 return 0
282 282
283 283 if not self.__emailServer:
284 284 return 0
285 285
286 286 log.success('Sending email to {}...'.format(email_to), 'System')
287 287
288 288 msg = MIMEMultipart()
289 289 msg['Subject'] = subject
290 290 msg['From'] = "(Python SChain API): " + email_from
291 291 msg['Reply-to'] = email_from
292 292 msg['To'] = email_to
293 293
294 294 # That is what u see if dont have an email reader:
295 295 msg.preamble = 'SChainPy'
296 296
297 297 if html_format:
298 298 message = "<h1> %s </h1>" %subject + "<h3>" + subtitle.replace("\n", "</h3><h3>\n") + "</h3>" + message.replace("\n", "<br>\n")
299 299 message = "<html>\n" + message + '</html>'
300 300
301 301 # This is the textual part:
302 302 part = MIMEText(message, "html")
303 303 else:
304 304 message = subject + "\n" + subtitle + "\n" + message
305 305 part = MIMEText(message)
306 306
307 307 msg.attach(part)
308 308
309 309 if filename and os.path.isfile(filename):
310 310 # This is the binary part(The Attachment):
311 311 part = MIMEApplication(open(filename,"rb").read())
312 312 part.add_header('Content-Disposition',
313 313 'attachment',
314 314 filename=os.path.basename(filename))
315 315 msg.attach(part)
316 316
317 317 # Create an instance in SMTP server
318 318 try:
319 319 smtp = smtplib.SMTP(self.__emailServer)
320 320 except:
321 321 log.error('Could not connect to server {}'.format(self.__emailServer), 'System')
322 322 return 0
323 323
324 324 # Start the server:
325 325 # smtp.ehlo()
326 326 if self.__emailPass:
327 327 smtp.login(self.__emailFromAddress, self.__emailPass)
328 328
329 329 # Send the email
330 330 try:
331 331 smtp.sendmail(msg['From'], msg['To'], msg.as_string())
332 332 except:
333 333 log.error('Could not send the email to {}'.format(msg['To']), 'System')
334 334 smtp.quit()
335 335 return 0
336 336
337 337 smtp.quit()
338 338
339 339 log.success('Email sent ', 'System')
340 340
341 341 return 1
342 342
343 343 def sendAlert(self, message, subject = "", subtitle="", filename=""):
344 344 """sendAlert sends an email with the given message and optional title.
345 345
346 346 Inputs: message (string), and optional title (string)
347 347
348 348 Returns: void
349 349
350 350 Affects: none
351 351
352 352 Exceptions: None.
353 353 """
354 354
355 355 if not self.__emailToAddress:
356 356 return 0
357 357
358 print "***** Sending alert to %s *****" %self.__emailToAddress
358 print("***** Sending alert to %s *****" %self.__emailToAddress)
359 359 # set up message
360 360
361 361 sent=self.sendEmail(email_from=self.__emailFromAddress,
362 362 email_to=self.__emailToAddress,
363 363 subject=subject,
364 364 message=message,
365 365 subtitle=subtitle,
366 366 filename=filename)
367 367
368 368 if not sent:
369 369 return 0
370 370
371 371 return 1
372 372
373 373 def notify(self, email, message, subject = "", subtitle="", filename=""):
374 374 """notify sends an email with the given message and title to email.
375 375
376 376 Inputs: email (string), message (string), and subject (string)
377 377
378 378 Returns: void
379 379
380 380 Affects: none
381 381
382 382 Exceptions: None.
383 383 """
384 384
385 385 if email is None:
386 386 email = self.__emailToAddress
387 387
388 388 self.sendEmail(
389 389 email_from=self.__emailFromAddress,
390 390 email_to=email,
391 391 subject=subject,
392 392 message=message,
393 393 subtitle=subtitle,
394 394 filename=filename
395 395 )
396 396
397 397
398 398 class SchainError(Exception):
399 399 """SchainError is an exception class that is thrown for all known errors using Schain Py lib.
400 400
401 401 Usage example:
402 402
403 403 import sys, traceback
404 404 import schainpy.admin
405 405
406 406 try:
407 407
408 408 test = open('ImportantFile.txt', 'r')
409 409
410 410 except:
411 411
412 412 raise schainpy.admin.SchainError('ImportantFile.txt not opened!',
413 413 traceback.format_exception(sys.exc_info()[0],
414 414 sys.exc_info()[1],
415 415 sys.exc_info()[2]))
416 416 """
417 417
418 418
419 419 def __init__(self, strInterpretation, exceptionList=None):
420 420 """ __init__ gathers the interpretation string along with all information from sys.exc_info().
421 421
422 422 Inputs:
423 423 strIntepretation - A string representing the programmer's interpretation of
424 424 why the exception occurred
425 425
426 426 exceptionList - a list of strings completely describing the exception.
427 427 Generated by traceback.format_exception(sys.exc_info()[0],
428 428 sys.exc_info()[1],
429 429 sys.exc_info()[2])
430 430
431 431 Returns: Void.
432 432
433 433 Affects: Initializes class member variables _strInterp, _strExcList.
434 434
435 435 Exceptions: None.
436 436 """
437 437
438 438 if not exceptionList:
439 439 exceptionList = traceback.format_exception(sys.exc_info()[0],
440 440 sys.exc_info()[1],
441 441 sys.exc_info()[2])
442 442
443 443 self._strInterp = strInterpretation
444 444 self._strExcList = exceptionList
445 445
446 446
447 447 def getExceptionStr(self):
448 448 """ getExceptionStr returns a formatted string ready for printing completely describing the exception.
449 449
450 450 Inputs: None
451 451
452 452 Returns: A formatted string ready for printing completely describing the exception.
453 453
454 454 Affects: None
455 455
456 456 Exceptions: None.
457 457 """
458 458 excStr = ''
459 459 excStr = excStr + self._strInterp + '\n\n'
460 460
461 461 if self._strExcList != None:
462 462 for item in self._strExcList:
463 463 excStr = excStr + str(item) + '\n'
464 464
465 465 return excStr
466 466
467 467 def __str__(self):
468 468
469 469 return(self.getExceptionStr())
470 470
471 471
472 472 def getExceptionHtml(self):
473 473 """ getExceptionHtml returns an Html formatted string completely describing the exception.
474 474
475 475 Inputs: None
476 476
477 477 Returns: A formatted string ready for printing completely describing the exception.
478 478
479 479 Affects: None
480 480
481 481 Exceptions: None.
482 482 """
483 483
484 484 excStr = '<BR>The following Schain Python exception has occurred:\n<BR>'
485 485 excStr = excStr + self._strInterp + '\n<BR>\n'
486 486
487 487 if self._strExcList != None:
488 488 for item in self._strExcList:
489 489 excStr = excStr + str(item) + '\n<BR>'
490 490
491 491 return excStr
492 492
493 493 class SchainWarning(Exception):
494 494 pass
495 495
496 496
497 497 if __name__ == '__main__':
498 498
499 499 test = SchainNotify()
500 500
501 501 test.sendAlert('This is a message from the python module SchainNotify', 'Test from SchainNotify')
502 502
503 print 'Hopefully message sent - check.'
503 print('Hopefully message sent - check.') No newline at end of file
@@ -1,1342 +1,1342
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5
6 6 import sys
7 7 import ast
8 8 import datetime
9 9 import traceback
10 10 import math
11 11 import time
12 12 from multiprocessing import Process, cpu_count
13 13
14 14 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
15 15 from xml.dom import minidom
16 16
17 17 import schainpy
18 18 from schainpy.admin import Alarm, SchainWarning
19 19 from schainpy.model import *
20 20 from schainpy.utils import log
21 21
22 22 DTYPES = {
23 23 'Voltage': '.r',
24 24 'Spectra': '.pdata'
25 25 }
26 26
27 27
28 28 def MPProject(project, n=cpu_count()):
29 29 '''
30 30 Project wrapper to run schain in n processes
31 31 '''
32 32
33 33 rconf = project.getReadUnitObj()
34 34 op = rconf.getOperationObj('run')
35 35 dt1 = op.getParameterValue('startDate')
36 36 dt2 = op.getParameterValue('endDate')
37 37 tm1 = op.getParameterValue('startTime')
38 38 tm2 = op.getParameterValue('endTime')
39 39 days = (dt2 - dt1).days
40 40
41 41 for day in range(days + 1):
42 42 skip = 0
43 43 cursor = 0
44 44 processes = []
45 45 dt = dt1 + datetime.timedelta(day)
46 46 dt_str = dt.strftime('%Y/%m/%d')
47 47 reader = JRODataReader()
48 48 paths, files = reader.searchFilesOffLine(path=rconf.path,
49 49 startDate=dt,
50 50 endDate=dt,
51 51 startTime=tm1,
52 52 endTime=tm2,
53 53 ext=DTYPES[rconf.datatype])
54 54 nFiles = len(files)
55 55 if nFiles == 0:
56 56 continue
57 57 skip = int(math.ceil(nFiles / n))
58 58 while nFiles > cursor * skip:
59 59 rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor,
60 60 skip=skip)
61 61 p = project.clone()
62 62 p.start()
63 63 processes.append(p)
64 64 cursor += 1
65 65
66 66 def beforeExit(exctype, value, trace):
67 67 for process in processes:
68 68 process.terminate()
69 69 process.join()
70 print traceback.print_tb(trace)
70 print(traceback.print_tb(trace))
71 71
72 72 sys.excepthook = beforeExit
73 73
74 74 for process in processes:
75 75 process.join()
76 76 process.terminate()
77 77
78 78 time.sleep(3)
79 79
80 80
81 81 class ParameterConf():
82 82
83 83 id = None
84 84 name = None
85 85 value = None
86 86 format = None
87 87
88 88 __formated_value = None
89 89
90 90 ELEMENTNAME = 'Parameter'
91 91
92 92 def __init__(self):
93 93
94 94 self.format = 'str'
95 95
96 96 def getElementName(self):
97 97
98 98 return self.ELEMENTNAME
99 99
100 100 def getValue(self):
101 101
102 102 value = self.value
103 103 format = self.format
104 104
105 105 if self.__formated_value != None:
106 106
107 107 return self.__formated_value
108 108
109 109 if format == 'obj':
110 110 return value
111 111
112 112 if format == 'str':
113 113 self.__formated_value = str(value)
114 114 return self.__formated_value
115 115
116 116 if value == '':
117 raise ValueError, '%s: This parameter value is empty' % self.name
117 raise ValueError('%s: This parameter value is empty' % self.name)
118 118
119 119 if format == 'list':
120 120 strList = value.split(',')
121 121
122 122 self.__formated_value = strList
123 123
124 124 return self.__formated_value
125 125
126 126 if format == 'intlist':
127 127 '''
128 128 Example:
129 129 value = (0,1,2)
130 130 '''
131 131
132 132 new_value = ast.literal_eval(value)
133 133
134 134 if type(new_value) not in (tuple, list):
135 135 new_value = [int(new_value)]
136 136
137 137 self.__formated_value = new_value
138 138
139 139 return self.__formated_value
140 140
141 141 if format == 'floatlist':
142 142 '''
143 143 Example:
144 144 value = (0.5, 1.4, 2.7)
145 145 '''
146 146
147 147 new_value = ast.literal_eval(value)
148 148
149 149 if type(new_value) not in (tuple, list):
150 150 new_value = [float(new_value)]
151 151
152 152 self.__formated_value = new_value
153 153
154 154 return self.__formated_value
155 155
156 156 if format == 'date':
157 157 strList = value.split('/')
158 158 intList = [int(x) for x in strList]
159 159 date = datetime.date(intList[0], intList[1], intList[2])
160 160
161 161 self.__formated_value = date
162 162
163 163 return self.__formated_value
164 164
165 165 if format == 'time':
166 166 strList = value.split(':')
167 167 intList = [int(x) for x in strList]
168 168 time = datetime.time(intList[0], intList[1], intList[2])
169 169
170 170 self.__formated_value = time
171 171
172 172 return self.__formated_value
173 173
174 174 if format == 'pairslist':
175 175 '''
176 176 Example:
177 177 value = (0,1),(1,2)
178 178 '''
179 179
180 180 new_value = ast.literal_eval(value)
181 181
182 182 if type(new_value) not in (tuple, list):
183 raise ValueError, '%s has to be a tuple or list of pairs' % value
183 raise ValueError('%s has to be a tuple or list of pairs' % value)
184 184
185 185 if type(new_value[0]) not in (tuple, list):
186 186 if len(new_value) != 2:
187 raise ValueError, '%s has to be a tuple or list of pairs' % value
187 raise ValueError('%s has to be a tuple or list of pairs' % value)
188 188 new_value = [new_value]
189 189
190 190 for thisPair in new_value:
191 191 if len(thisPair) != 2:
192 raise ValueError, '%s has to be a tuple or list of pairs' % value
192 raise ValueError('%s has to be a tuple or list of pairs' % value)
193 193
194 194 self.__formated_value = new_value
195 195
196 196 return self.__formated_value
197 197
198 198 if format == 'multilist':
199 199 '''
200 200 Example:
201 201 value = (0,1,2),(3,4,5)
202 202 '''
203 203 multiList = ast.literal_eval(value)
204 204
205 205 if type(multiList[0]) == int:
206 206 multiList = ast.literal_eval('(' + value + ')')
207 207
208 208 self.__formated_value = multiList
209 209
210 210 return self.__formated_value
211 211
212 212 if format == 'bool':
213 213 value = int(value)
214 214
215 215 if format == 'int':
216 216 value = float(value)
217 217
218 218 format_func = eval(format)
219 219
220 220 self.__formated_value = format_func(value)
221 221
222 222 return self.__formated_value
223 223
224 224 def updateId(self, new_id):
225 225
226 226 self.id = str(new_id)
227 227
228 228 def setup(self, id, name, value, format='str'):
229 229 self.id = str(id)
230 230 self.name = name
231 231 if format == 'obj':
232 232 self.value = value
233 233 else:
234 234 self.value = str(value)
235 235 self.format = str.lower(format)
236 236
237 237 self.getValue()
238 238
239 239 return 1
240 240
241 241 def update(self, name, value, format='str'):
242 242
243 243 self.name = name
244 244 self.value = str(value)
245 245 self.format = format
246 246
247 247 def makeXml(self, opElement):
248 248 if self.name not in ('queue',):
249 249 parmElement = SubElement(opElement, self.ELEMENTNAME)
250 250 parmElement.set('id', str(self.id))
251 251 parmElement.set('name', self.name)
252 252 parmElement.set('value', self.value)
253 253 parmElement.set('format', self.format)
254 254
255 255 def readXml(self, parmElement):
256 256
257 257 self.id = parmElement.get('id')
258 258 self.name = parmElement.get('name')
259 259 self.value = parmElement.get('value')
260 260 self.format = str.lower(parmElement.get('format'))
261 261
262 262 # Compatible with old signal chain version
263 263 if self.format == 'int' and self.name == 'idfigure':
264 264 self.name = 'id'
265 265
266 266 def printattr(self):
267 267
268 print 'Parameter[%s]: name = %s, value = %s, format = %s' % (self.id, self.name, self.value, self.format)
268 print('Parameter[%s]: name = %s, value = %s, format = %s' % (self.id, self.name, self.value, self.format))
269 269
270 270
271 271 class OperationConf():
272 272
273 273 id = None
274 274 name = None
275 275 priority = None
276 276 type = None
277 277
278 278 parmConfObjList = []
279 279
280 280 ELEMENTNAME = 'Operation'
281 281
282 282 def __init__(self):
283 283
284 284 self.id = '0'
285 285 self.name = None
286 286 self.priority = None
287 287 self.type = 'self'
288 288
289 289 def __getNewId(self):
290 290
291 291 return int(self.id) * 10 + len(self.parmConfObjList) + 1
292 292
293 293 def updateId(self, new_id):
294 294
295 295 self.id = str(new_id)
296 296
297 297 n = 1
298 298 for parmObj in self.parmConfObjList:
299 299
300 300 idParm = str(int(new_id) * 10 + n)
301 301 parmObj.updateId(idParm)
302 302
303 303 n += 1
304 304
305 305 def getElementName(self):
306 306
307 307 return self.ELEMENTNAME
308 308
309 309 def getParameterObjList(self):
310 310
311 311 return self.parmConfObjList
312 312
313 313 def getParameterObj(self, parameterName):
314 314
315 315 for parmConfObj in self.parmConfObjList:
316 316
317 317 if parmConfObj.name != parameterName:
318 318 continue
319 319
320 320 return parmConfObj
321 321
322 322 return None
323 323
324 324 def getParameterObjfromValue(self, parameterValue):
325 325
326 326 for parmConfObj in self.parmConfObjList:
327 327
328 328 if parmConfObj.getValue() != parameterValue:
329 329 continue
330 330
331 331 return parmConfObj.getValue()
332 332
333 333 return None
334 334
335 335 def getParameterValue(self, parameterName):
336 336
337 337 parameterObj = self.getParameterObj(parameterName)
338 338
339 339 # if not parameterObj:
340 340 # return None
341 341
342 342 value = parameterObj.getValue()
343 343
344 344 return value
345 345
346 346 def getKwargs(self):
347 347
348 348 kwargs = {}
349 349
350 350 for parmConfObj in self.parmConfObjList:
351 351 if self.name == 'run' and parmConfObj.name == 'datatype':
352 352 continue
353 353
354 354 kwargs[parmConfObj.name] = parmConfObj.getValue()
355 355
356 356 return kwargs
357 357
358 358 def setup(self, id, name, priority, type):
359 359
360 360 self.id = str(id)
361 361 self.name = name
362 362 self.type = type
363 363 self.priority = priority
364 364
365 365 self.parmConfObjList = []
366 366
367 367 def removeParameters(self):
368 368
369 369 for obj in self.parmConfObjList:
370 370 del obj
371 371
372 372 self.parmConfObjList = []
373 373
374 374 def addParameter(self, name, value, format='str'):
375 375
376 376 if value is None:
377 377 return None
378 378 id = self.__getNewId()
379 379
380 380 parmConfObj = ParameterConf()
381 381 if not parmConfObj.setup(id, name, value, format):
382 382 return None
383 383
384 384 self.parmConfObjList.append(parmConfObj)
385 385
386 386 return parmConfObj
387 387
388 388 def changeParameter(self, name, value, format='str'):
389 389
390 390 parmConfObj = self.getParameterObj(name)
391 391 parmConfObj.update(name, value, format)
392 392
393 393 return parmConfObj
394 394
395 395 def makeXml(self, procUnitElement):
396 396
397 397 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
398 398 opElement.set('id', str(self.id))
399 399 opElement.set('name', self.name)
400 400 opElement.set('type', self.type)
401 401 opElement.set('priority', str(self.priority))
402 402
403 403 for parmConfObj in self.parmConfObjList:
404 404 parmConfObj.makeXml(opElement)
405 405
406 406 def readXml(self, opElement):
407 407
408 408 self.id = opElement.get('id')
409 409 self.name = opElement.get('name')
410 410 self.type = opElement.get('type')
411 411 self.priority = opElement.get('priority')
412 412
413 413 # Compatible with old signal chain version
414 414 # Use of 'run' method instead 'init'
415 415 if self.type == 'self' and self.name == 'init':
416 416 self.name = 'run'
417 417
418 418 self.parmConfObjList = []
419 419
420 420 parmElementList = opElement.iter(ParameterConf().getElementName())
421 421
422 422 for parmElement in parmElementList:
423 423 parmConfObj = ParameterConf()
424 424 parmConfObj.readXml(parmElement)
425 425
426 426 # Compatible with old signal chain version
427 427 # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
428 428 if self.type != 'self' and self.name == 'Plot':
429 429 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
430 430 self.name = parmConfObj.value
431 431 continue
432 432
433 433 self.parmConfObjList.append(parmConfObj)
434 434
435 435 def printattr(self):
436 436
437 print '%s[%s]: name = %s, type = %s, priority = %s' % (self.ELEMENTNAME,
437 print('%s[%s]: name = %s, type = %s, priority = %s' % (self.ELEMENTNAME,
438 438 self.id,
439 439 self.name,
440 440 self.type,
441 self.priority)
441 self.priority))
442 442
443 443 for parmConfObj in self.parmConfObjList:
444 444 parmConfObj.printattr()
445 445
446 446 def createObject(self, plotter_queue=None):
447 447
448 448 if self.type == 'self':
449 raise ValueError, 'This operation type cannot be created'
449 raise ValueError('This operation type cannot be created')
450 450
451 451 if self.type == 'plotter':
452 452 if not plotter_queue:
453 raise ValueError, 'plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)'
453 raise ValueError('plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)')
454 454
455 455 opObj = Plotter(self.name, plotter_queue)
456 456
457 457 if self.type == 'external' or self.type == 'other':
458 458
459 459 className = eval(self.name)
460 460 kwargs = self.getKwargs()
461 461
462 462 opObj = className(**kwargs)
463 463
464 464 return opObj
465 465
466 466
467 467 class ProcUnitConf():
468 468
469 469 id = None
470 470 name = None
471 471 datatype = None
472 472 inputId = None
473 473 parentId = None
474 474
475 475 opConfObjList = []
476 476
477 477 procUnitObj = None
478 478 opObjList = []
479 479
480 480 ELEMENTNAME = 'ProcUnit'
481 481
482 482 def __init__(self):
483 483
484 484 self.id = None
485 485 self.datatype = None
486 486 self.name = None
487 487 self.inputId = None
488 488
489 489 self.opConfObjList = []
490 490
491 491 self.procUnitObj = None
492 492 self.opObjDict = {}
493 493
494 494 def __getPriority(self):
495 495
496 496 return len(self.opConfObjList) + 1
497 497
498 498 def __getNewId(self):
499 499
500 500 return int(self.id) * 10 + len(self.opConfObjList) + 1
501 501
502 502 def getElementName(self):
503 503
504 504 return self.ELEMENTNAME
505 505
506 506 def getId(self):
507 507
508 508 return self.id
509 509
510 510 def updateId(self, new_id, parentId=parentId):
511 511
512 512 new_id = int(parentId) * 10 + (int(self.id) % 10)
513 513 new_inputId = int(parentId) * 10 + (int(self.inputId) % 10)
514 514
515 515 # If this proc unit has not inputs
516 516 if self.inputId == '0':
517 517 new_inputId = 0
518 518
519 519 n = 1
520 520 for opConfObj in self.opConfObjList:
521 521
522 522 idOp = str(int(new_id) * 10 + n)
523 523 opConfObj.updateId(idOp)
524 524
525 525 n += 1
526 526
527 527 self.parentId = str(parentId)
528 528 self.id = str(new_id)
529 529 self.inputId = str(new_inputId)
530 530
531 531 def getInputId(self):
532 532
533 533 return self.inputId
534 534
535 535 def getOperationObjList(self):
536 536
537 537 return self.opConfObjList
538 538
539 539 def getOperationObj(self, name=None):
540 540
541 541 for opConfObj in self.opConfObjList:
542 542
543 543 if opConfObj.name != name:
544 544 continue
545 545
546 546 return opConfObj
547 547
548 548 return None
549 549
550 550 def getOpObjfromParamValue(self, value=None):
551 551
552 552 for opConfObj in self.opConfObjList:
553 553 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
554 554 continue
555 555 return opConfObj
556 556 return None
557 557
558 558 def getProcUnitObj(self):
559 559
560 560 return self.procUnitObj
561 561
562 562 def setup(self, id, name, datatype, inputId, parentId=None):
563 563
564 564 # Compatible with old signal chain version
565 565 if datatype == None and name == None:
566 raise ValueError, 'datatype or name should be defined'
566 raise ValueError('datatype or name should be defined')
567 567
568 568 if name == None:
569 569 if 'Proc' in datatype:
570 570 name = datatype
571 571 else:
572 572 name = '%sProc' % (datatype)
573 573
574 574 if datatype == None:
575 575 datatype = name.replace('Proc', '')
576 576
577 577 self.id = str(id)
578 578 self.name = name
579 579 self.datatype = datatype
580 580 self.inputId = inputId
581 581 self.parentId = parentId
582 582
583 583 self.opConfObjList = []
584 584
585 585 self.addOperation(name='run', optype='self')
586 586
587 587 def removeOperations(self):
588 588
589 589 for obj in self.opConfObjList:
590 590 del obj
591 591
592 592 self.opConfObjList = []
593 593 self.addOperation(name='run')
594 594
595 595 def addParameter(self, **kwargs):
596 596 '''
597 597 Add parameters to 'run' operation
598 598 '''
599 599 opObj = self.opConfObjList[0]
600 600
601 601 opObj.addParameter(**kwargs)
602 602
603 603 return opObj
604 604
605 605 def addOperation(self, name, optype='self'):
606 606
607 607 id = self.__getNewId()
608 608 priority = self.__getPriority()
609 609
610 610 opConfObj = OperationConf()
611 611 opConfObj.setup(id, name=name, priority=priority, type=optype)
612 612
613 613 self.opConfObjList.append(opConfObj)
614 614
615 615 return opConfObj
616 616
617 617 def makeXml(self, projectElement):
618 618
619 619 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
620 620 procUnitElement.set('id', str(self.id))
621 621 procUnitElement.set('name', self.name)
622 622 procUnitElement.set('datatype', self.datatype)
623 623 procUnitElement.set('inputId', str(self.inputId))
624 624
625 625 for opConfObj in self.opConfObjList:
626 626 opConfObj.makeXml(procUnitElement)
627 627
628 628 def readXml(self, upElement):
629 629
630 630 self.id = upElement.get('id')
631 631 self.name = upElement.get('name')
632 632 self.datatype = upElement.get('datatype')
633 633 self.inputId = upElement.get('inputId')
634 634
635 635 if self.ELEMENTNAME == 'ReadUnit':
636 636 self.datatype = self.datatype.replace('Reader', '')
637 637
638 638 if self.ELEMENTNAME == 'ProcUnit':
639 639 self.datatype = self.datatype.replace('Proc', '')
640 640
641 641 if self.inputId == 'None':
642 642 self.inputId = '0'
643 643
644 644 self.opConfObjList = []
645 645
646 646 opElementList = upElement.iter(OperationConf().getElementName())
647 647
648 648 for opElement in opElementList:
649 649 opConfObj = OperationConf()
650 650 opConfObj.readXml(opElement)
651 651 self.opConfObjList.append(opConfObj)
652 652
653 653 def printattr(self):
654 654
655 print '%s[%s]: name = %s, datatype = %s, inputId = %s' % (self.ELEMENTNAME,
655 print('%s[%s]: name = %s, datatype = %s, inputId = %s' % (self.ELEMENTNAME,
656 656 self.id,
657 657 self.name,
658 658 self.datatype,
659 self.inputId)
659 self.inputId))
660 660
661 661 for opConfObj in self.opConfObjList:
662 662 opConfObj.printattr()
663 663
664 664 def getKwargs(self):
665 665
666 666 opObj = self.opConfObjList[0]
667 667 kwargs = opObj.getKwargs()
668 668
669 669 return kwargs
670 670
671 671 def createObjects(self, plotter_queue=None):
672 672
673 673 className = eval(self.name)
674 674 kwargs = self.getKwargs()
675 675 procUnitObj = className(**kwargs)
676 676
677 677 for opConfObj in self.opConfObjList:
678 678
679 679 if opConfObj.type == 'self' and self.name == 'run':
680 680 continue
681 681 elif opConfObj.type == 'self':
682 682 procUnitObj.addOperationKwargs(
683 683 opConfObj.id, **opConfObj.getKwargs())
684 684 continue
685 685
686 686 opObj = opConfObj.createObject(plotter_queue)
687 687
688 688 self.opObjDict[opConfObj.id] = opObj
689 689
690 690 procUnitObj.addOperation(opObj, opConfObj.id)
691 691
692 692 self.procUnitObj = procUnitObj
693 693
694 694 return procUnitObj
695 695
696 696 def run(self):
697 697
698 698 is_ok = False
699 699
700 700 for opConfObj in self.opConfObjList:
701 701
702 702 kwargs = {}
703 703 for parmConfObj in opConfObj.getParameterObjList():
704 704 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
705 705 continue
706 706
707 707 kwargs[parmConfObj.name] = parmConfObj.getValue()
708 708
709 709 sts = self.procUnitObj.call(opType=opConfObj.type,
710 710 opName=opConfObj.name,
711 711 opId=opConfObj.id)
712 712
713 713 is_ok = is_ok or sts
714 714
715 715 return is_ok
716 716
717 717 def close(self):
718 718
719 719 for opConfObj in self.opConfObjList:
720 720 if opConfObj.type == 'self':
721 721 continue
722 722
723 723 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
724 724 opObj.close()
725 725
726 726 self.procUnitObj.close()
727 727
728 728 return
729 729
730 730
731 731 class ReadUnitConf(ProcUnitConf):
732 732
733 733 path = None
734 734 startDate = None
735 735 endDate = None
736 736 startTime = None
737 737 endTime = None
738 738
739 739 ELEMENTNAME = 'ReadUnit'
740 740
741 741 def __init__(self):
742 742
743 743 self.id = None
744 744 self.datatype = None
745 745 self.name = None
746 746 self.inputId = None
747 747
748 748 self.parentId = None
749 749
750 750 self.opConfObjList = []
751 751 self.opObjList = []
752 752
753 753 def getElementName(self):
754 754
755 755 return self.ELEMENTNAME
756 756
757 757 def setup(self, id, name, datatype, path='', startDate='', endDate='',
758 758 startTime='', endTime='', parentId=None, server=None, **kwargs):
759 759
760 760 # Compatible with old signal chain version
761 761 if datatype == None and name == None:
762 raise ValueError, 'datatype or name should be defined'
762 raise ValueError('datatype or name should be defined')
763 763 if name == None:
764 764 if 'Reader' in datatype:
765 765 name = datatype
766 766 datatype = name.replace('Reader','')
767 767 else:
768 768 name = '{}Reader'.format(datatype)
769 769 if datatype == None:
770 770 if 'Reader' in name:
771 771 datatype = name.replace('Reader','')
772 772 else:
773 773 datatype = name
774 774 name = '{}Reader'.format(name)
775 775
776 776 self.id = id
777 777 self.name = name
778 778 self.datatype = datatype
779 779 if path != '':
780 780 self.path = os.path.abspath(path)
781 781 self.startDate = startDate
782 782 self.endDate = endDate
783 783 self.startTime = startTime
784 784 self.endTime = endTime
785 785 self.inputId = '0'
786 786 self.parentId = parentId
787 787 self.server = server
788 788 self.addRunOperation(**kwargs)
789 789
790 790 def update(self, **kwargs):
791 791
792 792 if 'datatype' in kwargs:
793 793 datatype = kwargs.pop('datatype')
794 794 if 'Reader' in datatype:
795 795 self.name = datatype
796 796 else:
797 797 self.name = '%sReader' % (datatype)
798 798 self.datatype = self.name.replace('Reader', '')
799 799
800 800 attrs = ('path', 'startDate', 'endDate',
801 801 'startTime', 'endTime', 'parentId')
802 802
803 803 for attr in attrs:
804 804 if attr in kwargs:
805 805 setattr(self, attr, kwargs.pop(attr))
806 806
807 807 self.inputId = '0'
808 808 self.updateRunOperation(**kwargs)
809 809
810 810 def removeOperations(self):
811 811
812 812 for obj in self.opConfObjList:
813 813 del obj
814 814
815 815 self.opConfObjList = []
816 816
817 817 def addRunOperation(self, **kwargs):
818 818
819 819 opObj = self.addOperation(name='run', optype='self')
820 820
821 821 if self.server is None:
822 822 opObj.addParameter(
823 823 name='datatype', value=self.datatype, format='str')
824 824 opObj.addParameter(name='path', value=self.path, format='str')
825 825 opObj.addParameter(
826 826 name='startDate', value=self.startDate, format='date')
827 827 opObj.addParameter(
828 828 name='endDate', value=self.endDate, format='date')
829 829 opObj.addParameter(
830 830 name='startTime', value=self.startTime, format='time')
831 831 opObj.addParameter(
832 832 name='endTime', value=self.endTime, format='time')
833 833
834 for key, value in kwargs.items():
834 for key, value in list(kwargs.items()):
835 835 opObj.addParameter(name=key, value=value,
836 836 format=type(value).__name__)
837 837 else:
838 838 opObj.addParameter(name='server', value=self.server, format='str')
839 839
840 840 return opObj
841 841
842 842 def updateRunOperation(self, **kwargs):
843 843
844 844 opObj = self.getOperationObj(name='run')
845 845 opObj.removeParameters()
846 846
847 847 opObj.addParameter(name='datatype', value=self.datatype, format='str')
848 848 opObj.addParameter(name='path', value=self.path, format='str')
849 849 opObj.addParameter(
850 850 name='startDate', value=self.startDate, format='date')
851 851 opObj.addParameter(name='endDate', value=self.endDate, format='date')
852 852 opObj.addParameter(
853 853 name='startTime', value=self.startTime, format='time')
854 854 opObj.addParameter(name='endTime', value=self.endTime, format='time')
855 855
856 for key, value in kwargs.items():
856 for key, value in list(kwargs.items()):
857 857 opObj.addParameter(name=key, value=value,
858 858 format=type(value).__name__)
859 859
860 860 return opObj
861 861
862 862 def readXml(self, upElement):
863 863
864 864 self.id = upElement.get('id')
865 865 self.name = upElement.get('name')
866 866 self.datatype = upElement.get('datatype')
867 867 self.inputId = upElement.get('inputId')
868 868
869 869 if self.ELEMENTNAME == 'ReadUnit':
870 870 self.datatype = self.datatype.replace('Reader', '')
871 871
872 872 if self.inputId == 'None':
873 873 self.inputId = '0'
874 874
875 875 self.opConfObjList = []
876 876
877 877 opElementList = upElement.iter(OperationConf().getElementName())
878 878
879 879 for opElement in opElementList:
880 880 opConfObj = OperationConf()
881 881 opConfObj.readXml(opElement)
882 882 self.opConfObjList.append(opConfObj)
883 883
884 884 if opConfObj.name == 'run':
885 885 self.path = opConfObj.getParameterValue('path')
886 886 self.startDate = opConfObj.getParameterValue('startDate')
887 887 self.endDate = opConfObj.getParameterValue('endDate')
888 888 self.startTime = opConfObj.getParameterValue('startTime')
889 889 self.endTime = opConfObj.getParameterValue('endTime')
890 890
891 891
892 892 class Project(Process):
893 893
894 894 id = None
895 895 # name = None
896 896 description = None
897 897 filename = None
898 898
899 899 procUnitConfObjDict = None
900 900
901 901 ELEMENTNAME = 'Project'
902 902
903 903 plotterQueue = None
904 904
905 905 def __init__(self, plotter_queue=None):
906 906
907 907 Process.__init__(self)
908 908 self.id = None
909 909 self.description = None
910 910 self.email = None
911 911 self.alarm = None
912 912 self.plotterQueue = plotter_queue
913 913 self.procUnitConfObjDict = {}
914 914
915 915 def __getNewId(self):
916 916
917 idList = self.procUnitConfObjDict.keys()
917 idList = list(self.procUnitConfObjDict.keys())
918 918
919 919 id = int(self.id) * 10
920 920
921 921 while True:
922 922 id += 1
923 923
924 924 if str(id) in idList:
925 925 continue
926 926
927 927 break
928 928
929 929 return str(id)
930 930
931 931 def getElementName(self):
932 932
933 933 return self.ELEMENTNAME
934 934
935 935 def getId(self):
936 936
937 937 return self.id
938 938
939 939 def updateId(self, new_id):
940 940
941 941 self.id = str(new_id)
942 942
943 keyList = self.procUnitConfObjDict.keys()
943 keyList = list(self.procUnitConfObjDict.keys())
944 944 keyList.sort()
945 945
946 946 n = 1
947 947 newProcUnitConfObjDict = {}
948 948
949 949 for procKey in keyList:
950 950
951 951 procUnitConfObj = self.procUnitConfObjDict[procKey]
952 952 idProcUnit = str(int(self.id) * 10 + n)
953 953 procUnitConfObj.updateId(idProcUnit, parentId=self.id)
954 954 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
955 955 n += 1
956 956
957 957 self.procUnitConfObjDict = newProcUnitConfObjDict
958 958
959 959 def setup(self, id, name='', description='', email=None, alarm=[]):
960 960
961 print
962 print '*' * 60
963 print ' Starting SIGNAL CHAIN PROCESSING v%s ' % schainpy.__version__
964 print '*' * 60
965 print
961 print()
962 print('*' * 60)
963 print(' Starting SIGNAL CHAIN PROCESSING v%s ' % schainpy.__version__)
964 print('*' * 60)
965 print()
966 966 self.id = str(id)
967 967 self.description = description
968 968 self.email = email
969 969 self.alarm = alarm
970 970
971 971 def update(self, **kwargs):
972 972
973 for key, value in kwargs.items():
973 for key, value in list(kwargs.items()):
974 974 setattr(self, key, value)
975 975
976 976 def clone(self):
977 977
978 978 p = Project()
979 979 p.procUnitConfObjDict = self.procUnitConfObjDict
980 980 return p
981 981
982 982 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
983 983
984 984 if id is None:
985 985 idReadUnit = self.__getNewId()
986 986 else:
987 987 idReadUnit = str(id)
988 988
989 989 readUnitConfObj = ReadUnitConf()
990 990 readUnitConfObj.setup(idReadUnit, name, datatype,
991 991 parentId=self.id, **kwargs)
992 992
993 993 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
994 994
995 995 return readUnitConfObj
996 996
997 997 def addProcUnit(self, inputId='0', datatype=None, name=None):
998 998
999 999 idProcUnit = self.__getNewId()
1000 1000
1001 1001 procUnitConfObj = ProcUnitConf()
1002 1002 procUnitConfObj.setup(idProcUnit, name, datatype,
1003 1003 inputId, parentId=self.id)
1004 1004
1005 1005 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1006 1006
1007 1007 return procUnitConfObj
1008 1008
1009 1009 def removeProcUnit(self, id):
1010 1010
1011 if id in self.procUnitConfObjDict.keys():
1011 if id in list(self.procUnitConfObjDict.keys()):
1012 1012 self.procUnitConfObjDict.pop(id)
1013 1013
1014 1014 def getReadUnitId(self):
1015 1015
1016 1016 readUnitConfObj = self.getReadUnitObj()
1017 1017
1018 1018 return readUnitConfObj.id
1019 1019
1020 1020 def getReadUnitObj(self):
1021 1021
1022 for obj in self.procUnitConfObjDict.values():
1022 for obj in list(self.procUnitConfObjDict.values()):
1023 1023 if obj.getElementName() == 'ReadUnit':
1024 1024 return obj
1025 1025
1026 1026 return None
1027 1027
1028 1028 def getProcUnitObj(self, id=None, name=None):
1029 1029
1030 1030 if id != None:
1031 1031 return self.procUnitConfObjDict[id]
1032 1032
1033 1033 if name != None:
1034 1034 return self.getProcUnitObjByName(name)
1035 1035
1036 1036 return None
1037 1037
1038 1038 def getProcUnitObjByName(self, name):
1039 1039
1040 for obj in self.procUnitConfObjDict.values():
1040 for obj in list(self.procUnitConfObjDict.values()):
1041 1041 if obj.name == name:
1042 1042 return obj
1043 1043
1044 1044 return None
1045 1045
1046 1046 def procUnitItems(self):
1047 1047
1048 return self.procUnitConfObjDict.items()
1048 return list(self.procUnitConfObjDict.items())
1049 1049
1050 1050 def makeXml(self):
1051 1051
1052 1052 projectElement = Element('Project')
1053 1053 projectElement.set('id', str(self.id))
1054 1054 projectElement.set('name', self.name)
1055 1055 projectElement.set('description', self.description)
1056 1056
1057 for procUnitConfObj in self.procUnitConfObjDict.values():
1057 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1058 1058 procUnitConfObj.makeXml(projectElement)
1059 1059
1060 1060 self.projectElement = projectElement
1061 1061
1062 1062 def writeXml(self, filename=None):
1063 1063
1064 1064 if filename == None:
1065 1065 if self.filename:
1066 1066 filename = self.filename
1067 1067 else:
1068 1068 filename = 'schain.xml'
1069 1069
1070 1070 if not filename:
1071 print 'filename has not been defined. Use setFilename(filename) for do it.'
1071 print('filename has not been defined. Use setFilename(filename) for do it.')
1072 1072 return 0
1073 1073
1074 1074 abs_file = os.path.abspath(filename)
1075 1075
1076 1076 if not os.access(os.path.dirname(abs_file), os.W_OK):
1077 print 'No write permission on %s' % os.path.dirname(abs_file)
1077 print('No write permission on %s' % os.path.dirname(abs_file))
1078 1078 return 0
1079 1079
1080 1080 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1081 print 'File %s already exists and it could not be overwriten' % abs_file
1081 print('File %s already exists and it could not be overwriten' % abs_file)
1082 1082 return 0
1083 1083
1084 1084 self.makeXml()
1085 1085
1086 1086 ElementTree(self.projectElement).write(abs_file, method='xml')
1087 1087
1088 1088 self.filename = abs_file
1089 1089
1090 1090 return 1
1091 1091
1092 1092 def readXml(self, filename=None):
1093 1093
1094 1094 if not filename:
1095 print 'filename is not defined'
1095 print('filename is not defined')
1096 1096 return 0
1097 1097
1098 1098 abs_file = os.path.abspath(filename)
1099 1099
1100 1100 if not os.path.isfile(abs_file):
1101 print '%s file does not exist' % abs_file
1101 print('%s file does not exist' % abs_file)
1102 1102 return 0
1103 1103
1104 1104 self.projectElement = None
1105 1105 self.procUnitConfObjDict = {}
1106 1106
1107 1107 try:
1108 1108 self.projectElement = ElementTree().parse(abs_file)
1109 1109 except:
1110 print 'Error reading %s, verify file format' % filename
1110 print('Error reading %s, verify file format' % filename)
1111 1111 return 0
1112 1112
1113 1113 self.project = self.projectElement.tag
1114 1114
1115 1115 self.id = self.projectElement.get('id')
1116 1116 self.name = self.projectElement.get('name')
1117 1117 self.description = self.projectElement.get('description')
1118 1118
1119 1119 readUnitElementList = self.projectElement.iter(
1120 1120 ReadUnitConf().getElementName())
1121 1121
1122 1122 for readUnitElement in readUnitElementList:
1123 1123 readUnitConfObj = ReadUnitConf()
1124 1124 readUnitConfObj.readXml(readUnitElement)
1125 1125
1126 1126 if readUnitConfObj.parentId == None:
1127 1127 readUnitConfObj.parentId = self.id
1128 1128
1129 1129 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1130 1130
1131 1131 procUnitElementList = self.projectElement.iter(
1132 1132 ProcUnitConf().getElementName())
1133 1133
1134 1134 for procUnitElement in procUnitElementList:
1135 1135 procUnitConfObj = ProcUnitConf()
1136 1136 procUnitConfObj.readXml(procUnitElement)
1137 1137
1138 1138 if procUnitConfObj.parentId == None:
1139 1139 procUnitConfObj.parentId = self.id
1140 1140
1141 1141 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1142 1142
1143 1143 self.filename = abs_file
1144 1144
1145 1145 return 1
1146 1146
1147 1147 def printattr(self):
1148 1148
1149 print 'Project[%s]: name = %s, description = %s' % (self.id,
1149 print('Project[%s]: name = %s, description = %s' % (self.id,
1150 1150 self.name,
1151 self.description)
1151 self.description))
1152 1152
1153 for procUnitConfObj in self.procUnitConfObjDict.values():
1153 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1154 1154 procUnitConfObj.printattr()
1155 1155
1156 1156 def createObjects(self):
1157 1157
1158 for procUnitConfObj in self.procUnitConfObjDict.values():
1158 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1159 1159 procUnitConfObj.createObjects(self.plotterQueue)
1160 1160
1161 1161 def __connect(self, objIN, thisObj):
1162 1162
1163 1163 thisObj.setInput(objIN.getOutputObj())
1164 1164
1165 1165 def connectObjects(self):
1166 1166
1167 for thisPUConfObj in self.procUnitConfObjDict.values():
1167 for thisPUConfObj in list(self.procUnitConfObjDict.values()):
1168 1168
1169 1169 inputId = thisPUConfObj.getInputId()
1170 1170
1171 1171 if int(inputId) == 0:
1172 1172 continue
1173 1173
1174 1174 # Get input object
1175 1175 puConfINObj = self.procUnitConfObjDict[inputId]
1176 1176 puObjIN = puConfINObj.getProcUnitObj()
1177 1177
1178 1178 # Get current object
1179 1179 thisPUObj = thisPUConfObj.getProcUnitObj()
1180 1180
1181 1181 self.__connect(puObjIN, thisPUObj)
1182 1182
1183 1183 def __handleError(self, procUnitConfObj, modes=None, stdout=True):
1184 1184
1185 1185 import socket
1186 1186
1187 1187 if modes is None:
1188 1188 modes = self.alarm
1189 1189
1190 1190 if not self.alarm:
1191 1191 modes = []
1192 1192
1193 1193 err = traceback.format_exception(sys.exc_info()[0],
1194 1194 sys.exc_info()[1],
1195 1195 sys.exc_info()[2])
1196 1196
1197 1197 log.error('{}'.format(err[-1]), procUnitConfObj.name)
1198 1198
1199 1199 message = ''.join(err)
1200 1200
1201 1201 if stdout:
1202 1202 sys.stderr.write(message)
1203 1203
1204 1204 subject = 'SChain v%s: Error running %s\n' % (
1205 1205 schainpy.__version__, procUnitConfObj.name)
1206 1206
1207 1207 subtitle = '%s: %s\n' % (
1208 1208 procUnitConfObj.getElementName(), procUnitConfObj.name)
1209 1209 subtitle += 'Hostname: %s\n' % socket.gethostbyname(
1210 1210 socket.gethostname())
1211 1211 subtitle += 'Working directory: %s\n' % os.path.abspath('./')
1212 1212 subtitle += 'Configuration file: %s\n' % self.filename
1213 1213 subtitle += 'Time: %s\n' % str(datetime.datetime.now())
1214 1214
1215 1215 readUnitConfObj = self.getReadUnitObj()
1216 1216 if readUnitConfObj:
1217 1217 subtitle += '\nInput parameters:\n'
1218 1218 subtitle += '[Data path = %s]\n' % readUnitConfObj.path
1219 1219 subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype
1220 1220 subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate
1221 1221 subtitle += '[End date = %s]\n' % readUnitConfObj.endDate
1222 1222 subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime
1223 1223 subtitle += '[End time = %s]\n' % readUnitConfObj.endTime
1224 1224
1225 1225 a = Alarm(
1226 1226 modes=modes,
1227 1227 email=self.email,
1228 1228 message=message,
1229 1229 subject=subject,
1230 1230 subtitle=subtitle,
1231 1231 filename=self.filename
1232 1232 )
1233 1233
1234 1234 return a
1235 1235
1236 1236 def isPaused(self):
1237 1237 return 0
1238 1238
1239 1239 def isStopped(self):
1240 1240 return 0
1241 1241
1242 1242 def runController(self):
1243 1243 '''
1244 1244 returns 0 when this process has been stopped, 1 otherwise
1245 1245 '''
1246 1246
1247 1247 if self.isPaused():
1248 print 'Process suspended'
1248 print('Process suspended')
1249 1249
1250 1250 while True:
1251 1251 time.sleep(0.1)
1252 1252
1253 1253 if not self.isPaused():
1254 1254 break
1255 1255
1256 1256 if self.isStopped():
1257 1257 break
1258 1258
1259 print 'Process reinitialized'
1259 print('Process reinitialized')
1260 1260
1261 1261 if self.isStopped():
1262 print 'Process stopped'
1262 print('Process stopped')
1263 1263 return 0
1264 1264
1265 1265 return 1
1266 1266
1267 1267 def setFilename(self, filename):
1268 1268
1269 1269 self.filename = filename
1270 1270
1271 1271 def setPlotterQueue(self, plotter_queue):
1272 1272
1273 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1273 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1274 1274
1275 1275 def getPlotterQueue(self):
1276 1276
1277 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1277 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1278 1278
1279 1279 def useExternalPlotter(self):
1280 1280
1281 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1281 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1282 1282
1283 1283 def run(self):
1284 1284
1285 1285 log.success('Starting {}'.format(self.name), tag='')
1286 1286 self.start_time = time.time()
1287 1287 self.createObjects()
1288 1288 self.connectObjects()
1289 1289
1290 keyList = self.procUnitConfObjDict.keys()
1290 keyList = list(self.procUnitConfObjDict.keys())
1291 1291 keyList.sort()
1292 1292
1293 1293 err = None
1294 1294
1295 1295 while(True):
1296 1296
1297 1297 is_ok = False
1298 1298
1299 1299 for procKey in keyList:
1300 1300
1301 1301 procUnitConfObj = self.procUnitConfObjDict[procKey]
1302 1302
1303 1303 try:
1304 1304 sts = procUnitConfObj.run()
1305 1305 is_ok = is_ok or sts
1306 1306 except SchainWarning:
1307 1307 err = self.__handleError(procUnitConfObj, modes=[2, 3], stdout=False)
1308 1308 is_ok = False
1309 1309 break
1310 1310 except KeyboardInterrupt:
1311 1311 is_ok = False
1312 1312 break
1313 except ValueError, e:
1313 except ValueError as e:
1314 1314 time.sleep(0.5)
1315 1315 err = self.__handleError(procUnitConfObj)
1316 1316 is_ok = False
1317 1317 break
1318 1318 except:
1319 1319 time.sleep(0.5)
1320 1320 err = self.__handleError(procUnitConfObj)
1321 1321 is_ok = False
1322 1322 break
1323 1323
1324 1324 # If every process unit finished so end process
1325 1325 if not(is_ok):
1326 1326 break
1327 1327
1328 1328 if not self.runController():
1329 1329 break
1330 1330
1331 1331 # Closing every process
1332 1332 for procKey in keyList:
1333 1333 procUnitConfObj = self.procUnitConfObjDict[procKey]
1334 1334 procUnitConfObj.close()
1335 1335
1336 1336 if err is not None:
1337 1337 err.start()
1338 1338 # err.join()
1339 1339
1340 1340 log.success('{} finished (time: {}s)'.format(
1341 1341 self.name,
1342 time.time()-self.start_time))
1342 time.time()-self.start_time)) No newline at end of file
@@ -1,179 +1,179
1 1 import threading
2 from Queue import Queue
2 from queue import Queue
3 3
4 4 from schainpy.controller import Project
5 5 from schainpy.model.graphics.jroplotter import PlotManager
6 6
7 7 class ControllerThread(threading.Thread, Project):
8 8
9 9 def __init__(self, plotter_queue=None):
10 10
11 11 threading.Thread.__init__(self)
12 12 Project.__init__(self, plotter_queue)
13 13
14 14 self.setDaemon(True)
15 15
16 16 self.lock = threading.Lock()
17 17 self.control = { 'stop':False, 'pause':False }
18 18
19 19 def __del__(self):
20 20
21 21 self.control['stop'] = True
22 22
23 23 def stop(self):
24 24
25 25 self.lock.acquire()
26 26
27 27 self.control['stop'] = True
28 28
29 29 self.lock.release()
30 30
31 31 def pause(self):
32 32
33 33 self.lock.acquire()
34 34
35 35 self.control['pause'] = not(self.control['pause'])
36 36 paused = self.control['pause']
37 37
38 38 self.lock.release()
39 39
40 40 return paused
41 41
42 42 def isPaused(self):
43 43
44 44 self.lock.acquire()
45 45 paused = self.control['pause']
46 46 self.lock.release()
47 47
48 48 return paused
49 49
50 50 def isStopped(self):
51 51
52 52 self.lock.acquire()
53 53 stopped = self.control['stop']
54 54 self.lock.release()
55 55
56 56 return stopped
57 57
58 58 def run(self):
59 59 self.control['stop'] = False
60 60 self.control['pause'] = False
61 61
62 62 self.writeXml()
63 63
64 64 self.createObjects()
65 65 self.connectObjects()
66 66 Project.run(self)
67 67
68 68 def isRunning(self):
69 69
70 70 return self.is_alive()
71 71
72 72 def isFinished(self):
73 73
74 74 return not self.is_alive()
75 75
76 76 def setPlotters(self):
77 77
78 78 plotterList = PlotManager.plotterList
79 79
80 for thisPUConfObj in self.procUnitConfObjDict.values():
80 for thisPUConfObj in list(self.procUnitConfObjDict.values()):
81 81
82 82 inputId = thisPUConfObj.getInputId()
83 83
84 84 if int(inputId) == 0:
85 85 continue
86 86
87 87 for thisOpObj in thisPUConfObj.getOperationObjList():
88 88
89 89 if thisOpObj.type == "self":
90 90 continue
91 91
92 92 if thisOpObj.name in plotterList:
93 93 thisOpObj.type = "other"
94 94
95 95 def setPlotterQueue(self, plotter_queue):
96 96
97 97 self.plotterQueue = plotter_queue
98 98
99 99 def getPlotterQueue(self):
100 100
101 101 return self.plotterQueue
102 102
103 103 def useExternalPlotter(self):
104 104
105 105 self.plotterQueue = Queue(10)
106 106 self.setPlotters()
107 107
108 108 plotManagerObj = PlotManager(self.plotterQueue)
109 109 plotManagerObj.setController(self)
110 110
111 111 return plotManagerObj
112 112
113 113 # from PyQt4 import QtCore
114 114 # from PyQt4.QtCore import SIGNAL
115 115 #
116 116 # class ControllerQThread(QtCore.QThread, Project):
117 117 #
118 118 # def __init__(self, filename):
119 119 #
120 120 # QtCore.QThread.__init__(self)
121 121 # Project.__init__(self)
122 122 #
123 123 # self.filename = filename
124 124 #
125 125 # self.lock = threading.Lock()
126 126 # self.control = {'stop':False, 'pause':False}
127 127 #
128 128 # def __del__(self):
129 129 #
130 130 # self.control['stop'] = True
131 131 # self.wait()
132 132 #
133 133 # def stop(self):
134 134 #
135 135 # self.lock.acquire()
136 136 #
137 137 # self.control['stop'] = True
138 138 #
139 139 # self.lock.release()
140 140 #
141 141 # def pause(self):
142 142 #
143 143 # self.lock.acquire()
144 144 #
145 145 # self.control['pause'] = not(self.control['pause'])
146 146 # paused = self.control['pause']
147 147 #
148 148 # self.lock.release()
149 149 #
150 150 # return paused
151 151 #
152 152 # def isPaused(self):
153 153 #
154 154 # self.lock.acquire()
155 155 # paused = self.control['pause']
156 156 # self.lock.release()
157 157 #
158 158 # return paused
159 159 #
160 160 # def isStopped(self):
161 161 #
162 162 # self.lock.acquire()
163 163 # stopped = self.control['stop']
164 164 # self.lock.release()
165 165 #
166 166 # return stopped
167 167 #
168 168 # def run(self):
169 169 #
170 170 # self.control['stop'] = False
171 171 # self.control['pause'] = False
172 172 #
173 173 # self.readXml(self.filename)
174 174 # self.createObjects()
175 175 # self.connectObjects()
176 176 # self.emit( SIGNAL( "jobStarted( PyQt_PyObject )" ), 1)
177 177 # Project.run(self)
178 178 # self.emit( SIGNAL( "jobFinished( PyQt_PyObject )" ), 1)
179 #
179 # No newline at end of file
@@ -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 *
9 from data import *
10 from io import *
11 from proc import *
12 from utils import *
8 from .graphics import *
9 from .data import *
10 from .io import *
11 from .proc import *
12 from .utils import *
@@ -1,404 +1,404
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 5 '''
6 6 import sys
7 7 import numpy
8 8 import copy
9 9 import datetime
10 from __builtin__ import None
10
11 11
12 12 SPEED_OF_LIGHT = 299792458
13 13 SPEED_OF_LIGHT = 3e8
14 14
15 15 FILE_STRUCTURE = numpy.dtype([ #HEADER 48bytes
16 16 ('FileMgcNumber','<u4'), #0x23020100
17 17 ('nFDTdataRecors','<u4'), #No Of FDT data records in this file (0 or more)
18 18 ('RadarUnitId','<u4'),
19 19 ('SiteName','<s32'), #Null terminated
20 20 ])
21 21
22 22 RECORD_STRUCTURE = numpy.dtype([ #RECORD HEADER 180+20N bytes
23 23 ('RecMgcNumber','<u4'), #0x23030001
24 24 ('RecCounter','<u4'), #Record counter(0,1, ...)
25 25 ('Off2StartNxtRec','<u4'), #Offset to start of next record form start of this record
26 26 ('Off2StartData','<u4'), #Offset to start of data from start of this record
27 27 ('EpTimeStamp','<i4'), #Epoch time stamp of start of acquisition (seconds)
28 28 ('msCompTimeStamp','<u4'), #Millisecond component of time stamp (0,...,999)
29 29 ('ExpTagName','<s32'), #Experiment tag name (null terminated)
30 30 ('ExpComment','<s32'), #Experiment comment (null terminated)
31 31 ('SiteLatDegrees','<f4'), #Site latitude (from GPS) in degrees (positive implies North)
32 32 ('SiteLongDegrees','<f4'), #Site longitude (from GPS) in degrees (positive implies East)
33 33 ('RTCgpsStatus','<u4'), #RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
34 34 ('TransmitFrec','<u4'), #Transmit frequency (Hz)
35 35 ('ReceiveFrec','<u4'), #Receive frequency
36 36 ('FirstOsciFrec','<u4'), #First local oscillator frequency (Hz)
37 37 ('Polarisation','<u4'), #(0="O", 1="E", 2="linear 1", 3="linear2")
38 38 ('ReceiverFiltSett','<u4'), #Receiver filter settings (0,1,2,3)
39 39 ('nModesInUse','<u4'), #Number of modes in use (1 or 2)
40 40 ('DualModeIndex','<u4'), #Dual Mode index number for these data (0 or 1)
41 41 ('DualModeRange','<u4'), #Dual Mode range correction for these data (m)
42 42 ('nDigChannels','<u4'), #Number of digital channels acquired (2*N)
43 43 ('SampResolution','<u4'), #Sampling resolution (meters)
44 44 ('nRangeGatesSamp','<u4'), #Number of range gates sampled
45 45 ('StartRangeSamp','<u4'), #Start range of sampling (meters)
46 46 ('PRFhz','<u4'), #PRF (Hz)
47 47 ('Integrations','<u4'), #Integrations
48 48 ('nDataPointsTrsf','<u4'), #Number of data points transformed
49 49 ('nReceiveBeams','<u4'), #Number of receive beams stored in file (1 or N)
50 50 ('nSpectAverages','<u4'), #Number of spectral averages
51 51 ('FFTwindowingInd','<u4'), #FFT windowing index (0 = no window)
52 52 ('BeamAngleAzim','<f4'), #Beam steer angle (azimuth) in degrees (clockwise from true North)
53 53 ('BeamAngleZen','<f4'), #Beam steer angle (zenith) in degrees (0=> vertical)
54 54 ('AntennaCoord','<f24'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
55 55 ('RecPhaseCalibr','<f12'), #Receiver phase calibration (degrees) - N values
56 56 ('RecAmpCalibr','<f12'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
57 57 ('ReceiverGaindB','<u12'), #Receiver gains in dB - N values
58 58 ])
59 59
60 60
61 61 class Header(object):
62 62
63 63 def __init__(self):
64 64 raise NotImplementedError
65 65
66 66
67 67 def read(self):
68 68
69 69 raise NotImplementedError
70 70
71 71 def write(self):
72 72
73 73 raise NotImplementedError
74 74
75 75 def printInfo(self):
76 76
77 77 message = "#"*50 + "\n"
78 78 message += self.__class__.__name__.upper() + "\n"
79 79 message += "#"*50 + "\n"
80 80
81 keyList = self.__dict__.keys()
81 keyList = list(self.__dict__.keys())
82 82 keyList.sort()
83 83
84 84 for key in keyList:
85 85 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
86 86
87 87 if "size" not in keyList:
88 88 attr = getattr(self, "size")
89 89
90 90 if attr:
91 91 message += "%s = %s" %("size", attr) + "\n"
92 92
93 print message
93 print(message)
94 94
95 95 class FileHeader(Header):
96 96
97 97 FileMgcNumber= None
98 98 nFDTdataRecors=None #No Of FDT data records in this file (0 or more)
99 99 RadarUnitId= None
100 100 SiteName= None
101 101
102 102 #__LOCALTIME = None
103 103
104 104 def __init__(self, useLocalTime=True):
105 105
106 106 self.FileMgcNumber= 0 #0x23020100
107 107 self.nFDTdataRecors=0 #No Of FDT data records in this file (0 or more)
108 108 self.RadarUnitId= 0
109 109 self.SiteName= ""
110 110 self.size = 48
111 111
112 112 #self.useLocalTime = useLocalTime
113 113
114 114 def read(self, fp):
115 115
116 116 try:
117 117 header = numpy.fromfile(fp, FILE_STRUCTURE,1)
118 118 ''' numpy.fromfile(file, dtype, count, sep='')
119 119 file : file or str
120 120 Open file object or filename.
121 121
122 122 dtype : data-type
123 123 Data type of the returned array. For binary files, it is used to determine
124 124 the size and byte-order of the items in the file.
125 125
126 126 count : int
127 127 Number of items to read. -1 means all items (i.e., the complete file).
128 128
129 129 sep : str
130 130 Separator between items if file is a text file. Empty (“”) separator means
131 131 the file should be treated as binary. Spaces (” ”) in the separator match zero
132 132 or more whitespace characters. A separator consisting only of spaces must match
133 133 at least one whitespace.
134 134
135 135 '''
136 136
137 except Exception, e:
138 print "FileHeader: "
139 print eBasicHeader
137 except Exception as e:
138 print("FileHeader: ")
139 print(eBasicHeader)
140 140 return 0
141 141
142 142 self.FileMgcNumber= byte(header['FileMgcNumber'][0])
143 143 self.nFDTdataRecors=int(header['nFDTdataRecors'][0]) #No Of FDT data records in this file (0 or more)
144 144 self.RadarUnitId= int(header['RadarUnitId'][0])
145 145 self.SiteName= char(header['SiteName'][0])
146 146
147 147
148 148 if self.size <48:
149 149 return 0
150 150
151 151 return 1
152 152
153 153 def write(self, fp):
154 154
155 155 headerTuple = (self.FileMgcNumber,
156 156 self.nFDTdataRecors,
157 157 self.RadarUnitId,
158 158 self.SiteName,
159 159 self.size)
160 160
161 161
162 162 header = numpy.array(headerTuple, FILE_STRUCTURE)
163 163 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
164 164 header.tofile(fp)
165 165 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
166 166
167 167 fid : file or str
168 168 An open file object, or a string containing a filename.
169 169
170 170 sep : str
171 171 Separator between array items for text output. If “” (empty), a binary file is written,
172 172 equivalent to file.write(a.tobytes()).
173 173
174 174 format : str
175 175 Format string for text file output. Each entry in the array is formatted to text by
176 176 first converting it to the closest Python type, and then using “format” % item.
177 177
178 178 '''
179 179
180 180 return 1
181 181
182 182
183 183 class RecordHeader(Header):
184 184
185 185 RecMgcNumber=None #0x23030001
186 186 RecCounter= None
187 187 Off2StartNxtRec= None
188 188 EpTimeStamp= None
189 189 msCompTimeStamp= None
190 190 ExpTagName= None
191 191 ExpComment=None
192 192 SiteLatDegrees=None
193 193 SiteLongDegrees= None
194 194 RTCgpsStatus= None
195 195 TransmitFrec= None
196 196 ReceiveFrec= None
197 197 FirstOsciFrec= None
198 198 Polarisation= None
199 199 ReceiverFiltSett= None
200 200 nModesInUse= None
201 201 DualModeIndex= None
202 202 DualModeRange= None
203 203 nDigChannels= None
204 204 SampResolution= None
205 205 nRangeGatesSamp= None
206 206 StartRangeSamp= None
207 207 PRFhz= None
208 208 Integrations= None
209 209 nDataPointsTrsf= None
210 210 nReceiveBeams= None
211 211 nSpectAverages= None
212 212 FFTwindowingInd= None
213 213 BeamAngleAzim= None
214 214 BeamAngleZen= None
215 215 AntennaCoord= None
216 216 RecPhaseCalibr= None
217 217 RecAmpCalibr= None
218 218 ReceiverGaindB= None
219 219
220 220 '''size = None
221 221 nSamples = None
222 222 nProfiles = None
223 223 nChannels = None
224 224 adcResolution = None
225 225 pciDioBusWidth = None'''
226 226
227 227 def __init__(self, RecMgcNumber=None, RecCounter= 0, Off2StartNxtRec= 0,
228 228 EpTimeStamp= 0, msCompTimeStamp= 0, ExpTagName= None,
229 229 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees= 0,
230 230 RTCgpsStatus= 0, TransmitFrec= 0, ReceiveFrec= 0,
231 231 FirstOsciFrec= 0, Polarisation= 0, ReceiverFiltSett= 0,
232 232 nModesInUse= 0, DualModeIndex= 0, DualModeRange= 0,
233 233 nDigChannels= 0, SampResolution= 0, nRangeGatesSamp= 0,
234 234 StartRangeSamp= 0, PRFhz= 0, Integrations= 0,
235 235 nDataPointsTrsf= 0, nReceiveBeams= 0, nSpectAverages= 0,
236 236 FFTwindowingInd= 0, BeamAngleAzim= 0, BeamAngleZen= 0,
237 237 AntennaCoord= 0, RecPhaseCalibr= 0, RecAmpCalibr= 0,
238 238 ReceiverGaindB= 0):
239 239
240 240 self.RecMgcNumber = RecMgcNumber #0x23030001
241 241 self.RecCounter = RecCounter
242 242 self.Off2StartNxtRec = Off2StartNxtRec
243 243 self.EpTimeStamp = EpTimeStamp
244 244 self.msCompTimeStamp = msCompTimeStamp
245 245 self.ExpTagName = ExpTagName
246 246 self.ExpComment = ExpComment
247 247 self.SiteLatDegrees = SiteLatDegrees
248 248 self.SiteLongDegrees = SiteLongDegrees
249 249 self.RTCgpsStatus = RTCgpsStatus
250 250 self.TransmitFrec = TransmitFrec
251 251 self.ReceiveFrec = ReceiveFrec
252 252 self.FirstOsciFrec = FirstOsciFrec
253 253 self.Polarisation = Polarisation
254 254 self.ReceiverFiltSett = ReceiverFiltSett
255 255 self.nModesInUse = nModesInUse
256 256 self.DualModeIndex = DualModeIndex
257 257 self.DualModeRange = DualModeRange
258 258 self.nDigChannels = nDigChannels
259 259 self.SampResolution = SampResolution
260 260 self.nRangeGatesSamp = nRangeGatesSamp
261 261 self.StartRangeSamp = StartRangeSamp
262 262 self.PRFhz = PRFhz
263 263 self.Integrations = Integrations
264 264 self.nDataPointsTrsf = nDataPointsTrsf
265 265 self.nReceiveBeams = nReceiveBeams
266 266 self.nSpectAverages = nSpectAverages
267 267 self.FFTwindowingInd = FFTwindowingInd
268 268 self.BeamAngleAzim = BeamAngleAzim
269 269 self.BeamAngleZen = BeamAngleZen
270 270 self.AntennaCoord = AntennaCoord
271 271 self.RecPhaseCalibr = RecPhaseCalibr
272 272 self.RecAmpCalibr = RecAmpCalibr
273 273 self.ReceiverGaindB = ReceiverGaindB
274 274
275 275
276 276 def read(self, fp):
277 277
278 278 startFp = fp.tell() #The method tell() returns the current position of the file read/write pointer within the file.
279 279
280 280 try:
281 281 header = numpy.fromfile(fp,RECORD_STRUCTURE,1)
282 except Exception, e:
283 print "System Header: " + e
282 except Exception as e:
283 print("System Header: " + e)
284 284 return 0
285 285
286 286 self.RecMgcNumber = header['RecMgcNumber'][0] #0x23030001
287 287 self.RecCounter = header['RecCounter'][0]
288 288 self.Off2StartNxtRec = header['Off2StartNxtRec'][0]
289 289 self.EpTimeStamp = header['EpTimeStamp'][0]
290 290 self.msCompTimeStamp = header['msCompTimeStamp'][0]
291 291 self.ExpTagName = header['ExpTagName'][0]
292 292 self.ExpComment = header['ExpComment'][0]
293 293 self.SiteLatDegrees = header['SiteLatDegrees'][0]
294 294 self.SiteLongDegrees = header['SiteLongDegrees'][0]
295 295 self.RTCgpsStatus = header['RTCgpsStatus'][0]
296 296 self.TransmitFrec = header['TransmitFrec'][0]
297 297 self.ReceiveFrec = header['ReceiveFrec'][0]
298 298 self.FirstOsciFrec = header['FirstOsciFrec'][0]
299 299 self.Polarisation = header['Polarisation'][0]
300 300 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
301 301 self.nModesInUse = header['nModesInUse'][0]
302 302 self.DualModeIndex = header['DualModeIndex'][0]
303 303 self.DualModeRange = header['DualModeRange'][0]
304 304 self.nDigChannels = header['nDigChannels'][0]
305 305 self.SampResolution = header['SampResolution'][0]
306 306 self.nRangeGatesSamp = header['nRangeGatesSamp'][0]
307 307 self.StartRangeSamp = header['StartRangeSamp'][0]
308 308 self.PRFhz = header['PRFhz'][0]
309 309 self.Integrations = header['Integrations'][0]
310 310 self.nDataPointsTrsf = header['nDataPointsTrsf'][0]
311 311 self.nReceiveBeams = header['nReceiveBeams'][0]
312 312 self.nSpectAverages = header['nSpectAverages'][0]
313 313 self.FFTwindowingInd = header['FFTwindowingInd'][0]
314 314 self.BeamAngleAzim = header['BeamAngleAzim'][0]
315 315 self.BeamAngleZen = header['BeamAngleZen'][0]
316 316 self.AntennaCoord = header['AntennaCoord'][0]
317 317 self.RecPhaseCalibr = header['RecPhaseCalibr'][0]
318 318 self.RecAmpCalibr = header['RecAmpCalibr'][0]
319 319 self.ReceiverGaindB = header['ReceiverGaindB'][0]
320 320
321 321 Self.size = 180+20*3
322 322
323 323 endFp = self.size + startFp
324 324
325 325 if fp.tell() > endFp:
326 326 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp.name)
327 327 return 0
328 328
329 329 if fp.tell() < endFp:
330 330 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp.name)
331 331 return 0
332 332
333 333 return 1
334 334
335 335 def write(self, fp):
336 336
337 337 headerTuple = (self.RecMgcNumber,
338 338 self.RecCounter,
339 339 self.Off2StartNxtRec,
340 340 self.EpTimeStamp,
341 341 self.msCompTimeStamp,
342 342 self.ExpTagName,
343 343 self.ExpComment,
344 344 self.SiteLatDegrees,
345 345 self.SiteLongDegrees,
346 346 self.RTCgpsStatus,
347 347 self.TransmitFrec,
348 348 self.ReceiveFrec,
349 349 self.FirstOsciFrec,
350 350 self.Polarisation,
351 351 self.ReceiverFiltSett,
352 352 self.nModesInUse,
353 353 self.DualModeIndex,
354 354 self.DualModeRange,
355 355 self.nDigChannels,
356 356 self.SampResolution,
357 357 self.nRangeGatesSamp,
358 358 self.StartRangeSamp,
359 359 self.PRFhz,
360 360 self.Integrations,
361 361 self.nDataPointsTrsf,
362 362 self.nReceiveBeams,
363 363 self.nSpectAverages,
364 364 self.FFTwindowingInd,
365 365 self.BeamAngleAzim,
366 366 self.BeamAngleZen,
367 367 self.AntennaCoord,
368 368 self.RecPhaseCalibr,
369 369 self.RecAmpCalibr,
370 370 self.ReceiverGaindB)
371 371
372 372 # self.size,self.nSamples,
373 373 # self.nProfiles,
374 374 # self.nChannels,
375 375 # self.adcResolution,
376 376 # self.pciDioBusWidth
377 377
378 378 header = numpy.array(headerTuple,RECORD_STRUCTURE)
379 379 header.tofile(fp)
380 380
381 381 return 1
382 382
383 383
384 384 def get_dtype_index(numpy_dtype):
385 385
386 386 index = None
387 387
388 388 for i in range(len(NUMPY_DTYPE_LIST)):
389 389 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
390 390 index = i
391 391 break
392 392
393 393 return index
394 394
395 395 def get_numpy_dtype(index):
396 396
397 397 #dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
398 398
399 399 return NUMPY_DTYPE_LIST[index]
400 400
401 401
402 402 def get_dtype_width(index):
403 403
404 404 return DTYPE_WIDTH[index] No newline at end of file
@@ -1,3 +1,3
1 from jrodata import *
2 from jroheaderIO import *
3 from jroamisr import * No newline at end of file
1 from .jrodata import *
2 from .jroheaderIO import *
3 from .jroamisr import * No newline at end of file
@@ -1,90 +1,90
1 1 import numpy
2 2 import copy
3 3
4 4 class Beam:
5 5 def __init__(self):
6 6 self.codeList = []
7 7 self.azimuthList = []
8 8 self.zenithList = []
9 9
10 10
11 11 class AMISR:
12 12 def __init__(self):
13 13 self.flagNoData = True
14 14 self.data = None
15 15 self.utctime = None
16 16 self.type = "AMISR"
17 17
18 18 #propiedades para compatibilidad con Voltages
19 19 self.timeZone = 0#timezone like jroheader, difference in minutes between UTC and localtime
20 20 self.dstFlag = 0#self.dataIn.dstFlag
21 21 self.errorCount = 0#self.dataIn.errorCount
22 22 self.useLocalTime = True#self.dataIn.useLocalTime
23 23
24 24 self.radarControllerHeaderObj = None#self.dataIn.radarControllerHeaderObj.copy()
25 25 self.systemHeaderObj = None#self.dataIn.systemHeaderObj.copy()
26 26 self.channelList = [0]#self.dataIn.channelList esto solo aplica para el caso de AMISR
27 27 self.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
28 28
29 29 self.flagDiscontinuousBlock = None#self.dataIn.flagDiscontinuousBlock
30 30 #self.utctime = #self.firstdatatime
31 31 self.flagDecodeData = None#self.dataIn.flagDecodeData #asumo q la data esta decodificada
32 32 self.flagDeflipData = None#self.dataIn.flagDeflipData #asumo q la data esta sin flip
33 33
34 34 self.nCohInt = 1#self.dataIn.nCohInt
35 35 self.nIncohInt = 1
36 36 self.ippSeconds = None#self.dataIn.ippSeconds, segun el filename/Setup/Tufile
37 37 self.windowOfFilter = None#self.dataIn.windowOfFilter
38 38
39 39 self.timeInterval = None#self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
40 40 self.frequency = None#self.dataIn.frequency
41 41 self.realtime = 0#self.dataIn.realtime
42 42
43 43 #actualizar en la lectura de datos
44 44 self.heightList = None#self.dataIn.heightList
45 45 self.nProfiles = None#Number of samples or nFFTPoints
46 46 self.nRecords = None
47 47 self.nBeams = None
48 48 self.nBaud = None#self.dataIn.nBaud
49 49 self.nCode = None#self.dataIn.nCode
50 50 self.code = None#self.dataIn.code
51 51
52 52 #consideracion para los Beams
53 53 self.beamCodeDict = None
54 54 self.beamRangeDict = None
55 55 self.beamcode = None
56 56 self.azimuth = None
57 57 self.zenith = None
58 58 self.gain = None
59 59
60 60 self.npulseByFrame = None
61 61
62 62 self.profileIndex = None
63 63
64 64 self.beam = Beam()
65 65
66 66 def copy(self, inputObj=None):
67 67
68 68 if inputObj is None:
69 69 return copy.deepcopy(self)
70 70
71 for key in inputObj.__dict__.keys():
71 for key in list(inputObj.__dict__.keys()):
72 72 self.__dict__[key] = inputObj.__dict__[key]
73 73
74 74 def getNHeights(self):
75 75
76 76 return len(self.heightList)
77 77
78 78
79 79 def isEmpty(self):
80 80
81 81 return self.flagNoData
82 82
83 83 def getTimeInterval(self):
84 84
85 85 timeInterval = self.ippSeconds * self.nCohInt
86 86
87 87 return timeInterval
88 88
89 89 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
90 90 nHeights = property(getNHeights, "I'm the 'nHeights' property.") No newline at end of file
@@ -1,1251 +1,1251
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
5 5 '''
6 6
7 7 import copy
8 8 import numpy
9 9 import datetime
10 10
11 from jroheaderIO import SystemHeader, RadarControllerHeader
12 from schainpy import cSchain
11 from .jroheaderIO import SystemHeader, RadarControllerHeader
12 # from schainpy import cSchain
13 13
14 14
15 15 def getNumpyDtype(dataTypeCode):
16 16
17 17 if dataTypeCode == 0:
18 18 numpyDtype = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
19 19 elif dataTypeCode == 1:
20 20 numpyDtype = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
21 21 elif dataTypeCode == 2:
22 22 numpyDtype = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
23 23 elif dataTypeCode == 3:
24 24 numpyDtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
25 25 elif dataTypeCode == 4:
26 26 numpyDtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
27 27 elif dataTypeCode == 5:
28 28 numpyDtype = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
29 29 else:
30 raise ValueError, 'dataTypeCode was not defined'
30 raise ValueError('dataTypeCode was not defined')
31 31
32 32 return numpyDtype
33 33
34 34
35 35 def getDataTypeCode(numpyDtype):
36 36
37 37 if numpyDtype == numpy.dtype([('real', '<i1'), ('imag', '<i1')]):
38 38 datatype = 0
39 39 elif numpyDtype == numpy.dtype([('real', '<i2'), ('imag', '<i2')]):
40 40 datatype = 1
41 41 elif numpyDtype == numpy.dtype([('real', '<i4'), ('imag', '<i4')]):
42 42 datatype = 2
43 43 elif numpyDtype == numpy.dtype([('real', '<i8'), ('imag', '<i8')]):
44 44 datatype = 3
45 45 elif numpyDtype == numpy.dtype([('real', '<f4'), ('imag', '<f4')]):
46 46 datatype = 4
47 47 elif numpyDtype == numpy.dtype([('real', '<f8'), ('imag', '<f8')]):
48 48 datatype = 5
49 49 else:
50 50 datatype = None
51 51
52 52 return datatype
53 53
54 54
55 55 def hildebrand_sekhon(data, navg):
56 56 """
57 57 This method is for the objective determination of the noise level in Doppler spectra. This
58 58 implementation technique is based on the fact that the standard deviation of the spectral
59 59 densities is equal to the mean spectral density for white Gaussian noise
60 60
61 61 Inputs:
62 62 Data : heights
63 63 navg : numbers of averages
64 64
65 65 Return:
66 66 -1 : any error
67 67 anoise : noise's level
68 68 """
69 69
70 70 sortdata = numpy.sort(data, axis=None)
71 # lenOfData = len(sortdata)
72 # nums_min = lenOfData*0.2
73 #
74 # if nums_min <= 5:
75 # nums_min = 5
76 #
77 # sump = 0.
78 #
79 # sumq = 0.
80 #
81 # j = 0
82 #
83 # cont = 1
84 #
85 # while((cont==1)and(j<lenOfData)):
86 #
87 # sump += sortdata[j]
88 #
89 # sumq += sortdata[j]**2
90 #
91 # if j > nums_min:
92 # rtest = float(j)/(j-1) + 1.0/navg
93 # if ((sumq*j) > (rtest*sump**2)):
94 # j = j - 1
95 # sump = sump - sortdata[j]
96 # sumq = sumq - sortdata[j]**2
97 # cont = 0
98 #
99 # j += 1
100 #
101 # lnoise = sump /j
102 #
103 # return lnoise
71 lenOfData = len(sortdata)
72 nums_min = lenOfData*0.2
73
74 if nums_min <= 5:
75 nums_min = 5
76
77 sump = 0.
78
79 sumq = 0.
80
81 j = 0
82
83 cont = 1
84
85 while((cont==1)and(j<lenOfData)):
86
87 sump += sortdata[j]
88
89 sumq += sortdata[j]**2
90
91 if j > nums_min:
92 rtest = float(j)/(j-1) + 1.0/navg
93 if ((sumq*j) > (rtest*sump**2)):
94 j = j - 1
95 sump = sump - sortdata[j]
96 sumq = sumq - sortdata[j]**2
97 cont = 0
98
99 j += 1
100
101 lnoise = sump /j
102
103 return lnoise
104 104
105 return cSchain.hildebrand_sekhon(sortdata, navg)
105 # return cSchain.hildebrand_sekhon(sortdata, navg)
106 106
107 107
108 108 class Beam:
109 109
110 110 def __init__(self):
111 111 self.codeList = []
112 112 self.azimuthList = []
113 113 self.zenithList = []
114 114
115 115
116 116 class GenericData(object):
117 117
118 118 flagNoData = True
119 119
120 120 def copy(self, inputObj=None):
121 121
122 122 if inputObj == None:
123 123 return copy.deepcopy(self)
124 124
125 for key in inputObj.__dict__.keys():
125 for key in list(inputObj.__dict__.keys()):
126 126
127 127 attribute = inputObj.__dict__[key]
128 128
129 129 # If this attribute is a tuple or list
130 130 if type(inputObj.__dict__[key]) in (tuple, list):
131 131 self.__dict__[key] = attribute[:]
132 132 continue
133 133
134 134 # If this attribute is another object or instance
135 135 if hasattr(attribute, '__dict__'):
136 136 self.__dict__[key] = attribute.copy()
137 137 continue
138 138
139 139 self.__dict__[key] = inputObj.__dict__[key]
140 140
141 141 def deepcopy(self):
142 142
143 143 return copy.deepcopy(self)
144 144
145 145 def isEmpty(self):
146 146
147 147 return self.flagNoData
148 148
149 149
150 150 class JROData(GenericData):
151 151
152 152 # m_BasicHeader = BasicHeader()
153 153 # m_ProcessingHeader = ProcessingHeader()
154 154
155 155 systemHeaderObj = SystemHeader()
156 156
157 157 radarControllerHeaderObj = RadarControllerHeader()
158 158
159 159 # data = None
160 160
161 161 type = None
162 162
163 163 datatype = None # dtype but in string
164 164
165 165 # dtype = None
166 166
167 167 # nChannels = None
168 168
169 169 # nHeights = None
170 170
171 171 nProfiles = None
172 172
173 173 heightList = None
174 174
175 175 channelList = None
176 176
177 177 flagDiscontinuousBlock = False
178 178
179 179 useLocalTime = False
180 180
181 181 utctime = None
182 182
183 183 timeZone = None
184 184
185 185 dstFlag = None
186 186
187 187 errorCount = None
188 188
189 189 blocksize = None
190 190
191 191 # nCode = None
192 192 #
193 193 # nBaud = None
194 194 #
195 195 # code = None
196 196
197 197 flagDecodeData = False # asumo q la data no esta decodificada
198 198
199 199 flagDeflipData = False # asumo q la data no esta sin flip
200 200
201 201 flagShiftFFT = False
202 202
203 203 # ippSeconds = None
204 204
205 205 # timeInterval = None
206 206
207 207 nCohInt = None
208 208
209 209 # noise = None
210 210
211 211 windowOfFilter = 1
212 212
213 213 # Speed of ligth
214 214 C = 3e8
215 215
216 216 frequency = 49.92e6
217 217
218 218 realtime = False
219 219
220 220 beacon_heiIndexList = None
221 221
222 222 last_block = None
223 223
224 224 blocknow = None
225 225
226 226 azimuth = None
227 227
228 228 zenith = None
229 229
230 230 beam = Beam()
231 231
232 232 profileIndex = None
233 233
234 234 def getNoise(self):
235 235
236 236 raise NotImplementedError
237 237
238 238 def getNChannels(self):
239 239
240 240 return len(self.channelList)
241 241
242 242 def getChannelIndexList(self):
243 243
244 return range(self.nChannels)
244 return list(range(self.nChannels))
245 245
246 246 def getNHeights(self):
247 247
248 248 return len(self.heightList)
249 249
250 250 def getHeiRange(self, extrapoints=0):
251 251
252 252 heis = self.heightList
253 253 # deltah = self.heightList[1] - self.heightList[0]
254 254 #
255 255 # heis.append(self.heightList[-1])
256 256
257 257 return heis
258 258
259 259 def getDeltaH(self):
260 260
261 261 delta = self.heightList[1] - self.heightList[0]
262 262
263 263 return delta
264 264
265 265 def getltctime(self):
266 266
267 267 if self.useLocalTime:
268 268 return self.utctime - self.timeZone * 60
269 269
270 270 return self.utctime
271 271
272 272 def getDatatime(self):
273 273
274 274 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
275 275 return datatimeValue
276 276
277 277 def getTimeRange(self):
278 278
279 279 datatime = []
280 280
281 281 datatime.append(self.ltctime)
282 282 datatime.append(self.ltctime + self.timeInterval + 1)
283 283
284 284 datatime = numpy.array(datatime)
285 285
286 286 return datatime
287 287
288 288 def getFmaxTimeResponse(self):
289 289
290 290 period = (10**-6) * self.getDeltaH() / (0.15)
291 291
292 292 PRF = 1. / (period * self.nCohInt)
293 293
294 294 fmax = PRF
295 295
296 296 return fmax
297 297
298 298 def getFmax(self):
299 299 PRF = 1. / (self.ippSeconds * self.nCohInt)
300 300
301 301 fmax = PRF
302 302 return fmax
303 303
304 304 def getVmax(self):
305 305
306 306 _lambda = self.C / self.frequency
307 307
308 308 vmax = self.getFmax() * _lambda / 2
309 309
310 310 return vmax
311 311
312 312 def get_ippSeconds(self):
313 313 '''
314 314 '''
315 315 return self.radarControllerHeaderObj.ippSeconds
316 316
317 317 def set_ippSeconds(self, ippSeconds):
318 318 '''
319 319 '''
320 320
321 321 self.radarControllerHeaderObj.ippSeconds = ippSeconds
322 322
323 323 return
324 324
325 325 def get_dtype(self):
326 326 '''
327 327 '''
328 328 return getNumpyDtype(self.datatype)
329 329
330 330 def set_dtype(self, numpyDtype):
331 331 '''
332 332 '''
333 333
334 334 self.datatype = getDataTypeCode(numpyDtype)
335 335
336 336 def get_code(self):
337 337 '''
338 338 '''
339 339 return self.radarControllerHeaderObj.code
340 340
341 341 def set_code(self, code):
342 342 '''
343 343 '''
344 344 self.radarControllerHeaderObj.code = code
345 345
346 346 return
347 347
348 348 def get_ncode(self):
349 349 '''
350 350 '''
351 351 return self.radarControllerHeaderObj.nCode
352 352
353 353 def set_ncode(self, nCode):
354 354 '''
355 355 '''
356 356 self.radarControllerHeaderObj.nCode = nCode
357 357
358 358 return
359 359
360 360 def get_nbaud(self):
361 361 '''
362 362 '''
363 363 return self.radarControllerHeaderObj.nBaud
364 364
365 365 def set_nbaud(self, nBaud):
366 366 '''
367 367 '''
368 368 self.radarControllerHeaderObj.nBaud = nBaud
369 369
370 370 return
371 371
372 372 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
373 373 channelIndexList = property(
374 374 getChannelIndexList, "I'm the 'channelIndexList' property.")
375 375 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
376 376 #noise = property(getNoise, "I'm the 'nHeights' property.")
377 377 datatime = property(getDatatime, "I'm the 'datatime' property")
378 378 ltctime = property(getltctime, "I'm the 'ltctime' property")
379 379 ippSeconds = property(get_ippSeconds, set_ippSeconds)
380 380 dtype = property(get_dtype, set_dtype)
381 381 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
382 382 code = property(get_code, set_code)
383 383 nCode = property(get_ncode, set_ncode)
384 384 nBaud = property(get_nbaud, set_nbaud)
385 385
386 386
387 387 class Voltage(JROData):
388 388
389 389 # data es un numpy array de 2 dmensiones (canales, alturas)
390 390 data = None
391 391
392 392 def __init__(self):
393 393 '''
394 394 Constructor
395 395 '''
396 396
397 397 self.useLocalTime = True
398 398
399 399 self.radarControllerHeaderObj = RadarControllerHeader()
400 400
401 401 self.systemHeaderObj = SystemHeader()
402 402
403 403 self.type = "Voltage"
404 404
405 405 self.data = None
406 406
407 407 # self.dtype = None
408 408
409 409 # self.nChannels = 0
410 410
411 411 # self.nHeights = 0
412 412
413 413 self.nProfiles = None
414 414
415 415 self.heightList = None
416 416
417 417 self.channelList = None
418 418
419 419 # self.channelIndexList = None
420 420
421 421 self.flagNoData = True
422 422
423 423 self.flagDiscontinuousBlock = False
424 424
425 425 self.utctime = None
426 426
427 427 self.timeZone = None
428 428
429 429 self.dstFlag = None
430 430
431 431 self.errorCount = None
432 432
433 433 self.nCohInt = None
434 434
435 435 self.blocksize = None
436 436
437 437 self.flagDecodeData = False # asumo q la data no esta decodificada
438 438
439 439 self.flagDeflipData = False # asumo q la data no esta sin flip
440 440
441 441 self.flagShiftFFT = False
442 442
443 443 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
444 444
445 445 self.profileIndex = 0
446 446
447 447 def getNoisebyHildebrand(self, channel=None):
448 448 """
449 449 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
450 450
451 451 Return:
452 452 noiselevel
453 453 """
454 454
455 455 if channel != None:
456 456 data = self.data[channel]
457 457 nChannels = 1
458 458 else:
459 459 data = self.data
460 460 nChannels = self.nChannels
461 461
462 462 noise = numpy.zeros(nChannels)
463 463 power = data * numpy.conjugate(data)
464 464
465 465 for thisChannel in range(nChannels):
466 466 if nChannels == 1:
467 467 daux = power[:].real
468 468 else:
469 469 daux = power[thisChannel, :].real
470 470 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
471 471
472 472 return noise
473 473
474 474 def getNoise(self, type=1, channel=None):
475 475
476 476 if type == 1:
477 477 noise = self.getNoisebyHildebrand(channel)
478 478
479 479 return noise
480 480
481 481 def getPower(self, channel=None):
482 482
483 483 if channel != None:
484 484 data = self.data[channel]
485 485 else:
486 486 data = self.data
487 487
488 488 power = data * numpy.conjugate(data)
489 489 powerdB = 10 * numpy.log10(power.real)
490 490 powerdB = numpy.squeeze(powerdB)
491 491
492 492 return powerdB
493 493
494 494 def getTimeInterval(self):
495 495
496 496 timeInterval = self.ippSeconds * self.nCohInt
497 497
498 498 return timeInterval
499 499
500 500 noise = property(getNoise, "I'm the 'nHeights' property.")
501 501 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
502 502
503 503
504 504 class Spectra(JROData):
505 505
506 506 # data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
507 507 data_spc = None
508 508
509 509 # data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
510 510 data_cspc = None
511 511
512 512 # data dc es un numpy array de 2 dmensiones (canales, alturas)
513 513 data_dc = None
514 514
515 515 # data power
516 516 data_pwr = None
517 517
518 518 nFFTPoints = None
519 519
520 520 # nPairs = None
521 521
522 522 pairsList = None
523 523
524 524 nIncohInt = None
525 525
526 526 wavelength = None # Necesario para cacular el rango de velocidad desde la frecuencia
527 527
528 528 nCohInt = None # se requiere para determinar el valor de timeInterval
529 529
530 530 ippFactor = None
531 531
532 532 profileIndex = 0
533 533
534 534 plotting = "spectra"
535 535
536 536 def __init__(self):
537 537 '''
538 538 Constructor
539 539 '''
540 540
541 541 self.useLocalTime = True
542 542
543 543 self.radarControllerHeaderObj = RadarControllerHeader()
544 544
545 545 self.systemHeaderObj = SystemHeader()
546 546
547 547 self.type = "Spectra"
548 548
549 549 # self.data = None
550 550
551 551 # self.dtype = None
552 552
553 553 # self.nChannels = 0
554 554
555 555 # self.nHeights = 0
556 556
557 557 self.nProfiles = None
558 558
559 559 self.heightList = None
560 560
561 561 self.channelList = None
562 562
563 563 # self.channelIndexList = None
564 564
565 565 self.pairsList = None
566 566
567 567 self.flagNoData = True
568 568
569 569 self.flagDiscontinuousBlock = False
570 570
571 571 self.utctime = None
572 572
573 573 self.nCohInt = None
574 574
575 575 self.nIncohInt = None
576 576
577 577 self.blocksize = None
578 578
579 579 self.nFFTPoints = None
580 580
581 581 self.wavelength = None
582 582
583 583 self.flagDecodeData = False # asumo q la data no esta decodificada
584 584
585 585 self.flagDeflipData = False # asumo q la data no esta sin flip
586 586
587 587 self.flagShiftFFT = False
588 588
589 589 self.ippFactor = 1
590 590
591 591 #self.noise = None
592 592
593 593 self.beacon_heiIndexList = []
594 594
595 595 self.noise_estimation = None
596 596
597 597 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
598 598 """
599 599 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
600 600
601 601 Return:
602 602 noiselevel
603 603 """
604 604
605 605 noise = numpy.zeros(self.nChannels)
606 606
607 607 for channel in range(self.nChannels):
608 608 daux = self.data_spc[channel,
609 609 xmin_index:xmax_index, ymin_index:ymax_index]
610 610 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
611 611
612 612 return noise
613 613
614 614 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
615 615
616 616 if self.noise_estimation is not None:
617 617 # this was estimated by getNoise Operation defined in jroproc_spectra.py
618 618 return self.noise_estimation
619 619 else:
620 620 noise = self.getNoisebyHildebrand(
621 621 xmin_index, xmax_index, ymin_index, ymax_index)
622 622 return noise
623 623
624 624 def getFreqRangeTimeResponse(self, extrapoints=0):
625 625
626 626 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints * self.ippFactor)
627 627 freqrange = deltafreq * \
628 628 (numpy.arange(self.nFFTPoints + extrapoints) -
629 629 self.nFFTPoints / 2.) - deltafreq / 2
630 630
631 631 return freqrange
632 632
633 633 def getAcfRange(self, extrapoints=0):
634 634
635 635 deltafreq = 10. / (self.getFmax() / (self.nFFTPoints * self.ippFactor))
636 636 freqrange = deltafreq * \
637 637 (numpy.arange(self.nFFTPoints + extrapoints) -
638 638 self.nFFTPoints / 2.) - deltafreq / 2
639 639
640 640 return freqrange
641 641
642 642 def getFreqRange(self, extrapoints=0):
643 643
644 644 deltafreq = self.getFmax() / (self.nFFTPoints * self.ippFactor)
645 645 freqrange = deltafreq * \
646 646 (numpy.arange(self.nFFTPoints + extrapoints) -
647 647 self.nFFTPoints / 2.) - deltafreq / 2
648 648
649 649 return freqrange
650 650
651 651 def getVelRange(self, extrapoints=0):
652 652
653 653 deltav = self.getVmax() / (self.nFFTPoints * self.ippFactor)
654 654 velrange = deltav * (numpy.arange(self.nFFTPoints +
655 655 extrapoints) - self.nFFTPoints / 2.) # - deltav/2
656 656
657 657 return velrange
658 658
659 659 def getNPairs(self):
660 660
661 661 return len(self.pairsList)
662 662
663 663 def getPairsIndexList(self):
664 664
665 return range(self.nPairs)
665 return list(range(self.nPairs))
666 666
667 667 def getNormFactor(self):
668 668
669 669 pwcode = 1
670 670
671 671 if self.flagDecodeData:
672 672 pwcode = numpy.sum(self.code[0]**2)
673 673 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
674 674 normFactor = self.nProfiles * self.nIncohInt * \
675 675 self.nCohInt * pwcode * self.windowOfFilter
676 676
677 677 return normFactor
678 678
679 679 def getFlagCspc(self):
680 680
681 681 if self.data_cspc is None:
682 682 return True
683 683
684 684 return False
685 685
686 686 def getFlagDc(self):
687 687
688 688 if self.data_dc is None:
689 689 return True
690 690
691 691 return False
692 692
693 693 def getTimeInterval(self):
694 694
695 695 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles * self.ippFactor
696 696
697 697 return timeInterval
698 698
699 699 def getPower(self):
700 700
701 701 factor = self.normFactor
702 702 z = self.data_spc / factor
703 703 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
704 704 avg = numpy.average(z, axis=1)
705 705
706 706 return 10 * numpy.log10(avg)
707 707
708 708 def getCoherence(self, pairsList=None, phase=False):
709 709
710 710 z = []
711 711 if pairsList is None:
712 712 pairsIndexList = self.pairsIndexList
713 713 else:
714 714 pairsIndexList = []
715 715 for pair in pairsList:
716 716 if pair not in self.pairsList:
717 raise ValueError, "Pair %s is not in dataOut.pairsList" % (
718 pair)
717 raise ValueError("Pair %s is not in dataOut.pairsList" % (
718 pair))
719 719 pairsIndexList.append(self.pairsList.index(pair))
720 720 for i in range(len(pairsIndexList)):
721 721 pair = self.pairsList[pairsIndexList[i]]
722 722 ccf = numpy.average(
723 723 self.data_cspc[pairsIndexList[i], :, :], axis=0)
724 724 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
725 725 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
726 726 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
727 727 if phase:
728 728 data = numpy.arctan2(avgcoherenceComplex.imag,
729 729 avgcoherenceComplex.real) * 180 / numpy.pi
730 730 else:
731 731 data = numpy.abs(avgcoherenceComplex)
732 732
733 733 z.append(data)
734 734
735 735 return numpy.array(z)
736 736
737 737 def setValue(self, value):
738 738
739 print "This property should not be initialized"
739 print("This property should not be initialized")
740 740
741 741 return
742 742
743 743 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
744 744 pairsIndexList = property(
745 745 getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
746 746 normFactor = property(getNormFactor, setValue,
747 747 "I'm the 'getNormFactor' property.")
748 748 flag_cspc = property(getFlagCspc, setValue)
749 749 flag_dc = property(getFlagDc, setValue)
750 750 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
751 751 timeInterval = property(getTimeInterval, setValue,
752 752 "I'm the 'timeInterval' property")
753 753
754 754
755 755 class SpectraHeis(Spectra):
756 756
757 757 data_spc = None
758 758
759 759 data_cspc = None
760 760
761 761 data_dc = None
762 762
763 763 nFFTPoints = None
764 764
765 765 # nPairs = None
766 766
767 767 pairsList = None
768 768
769 769 nCohInt = None
770 770
771 771 nIncohInt = None
772 772
773 773 def __init__(self):
774 774
775 775 self.radarControllerHeaderObj = RadarControllerHeader()
776 776
777 777 self.systemHeaderObj = SystemHeader()
778 778
779 779 self.type = "SpectraHeis"
780 780
781 781 # self.dtype = None
782 782
783 783 # self.nChannels = 0
784 784
785 785 # self.nHeights = 0
786 786
787 787 self.nProfiles = None
788 788
789 789 self.heightList = None
790 790
791 791 self.channelList = None
792 792
793 793 # self.channelIndexList = None
794 794
795 795 self.flagNoData = True
796 796
797 797 self.flagDiscontinuousBlock = False
798 798
799 799 # self.nPairs = 0
800 800
801 801 self.utctime = None
802 802
803 803 self.blocksize = None
804 804
805 805 self.profileIndex = 0
806 806
807 807 self.nCohInt = 1
808 808
809 809 self.nIncohInt = 1
810 810
811 811 def getNormFactor(self):
812 812 pwcode = 1
813 813 if self.flagDecodeData:
814 814 pwcode = numpy.sum(self.code[0]**2)
815 815
816 816 normFactor = self.nIncohInt * self.nCohInt * pwcode
817 817
818 818 return normFactor
819 819
820 820 def getTimeInterval(self):
821 821
822 822 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
823 823
824 824 return timeInterval
825 825
826 826 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
827 827 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
828 828
829 829
830 830 class Fits(JROData):
831 831
832 832 heightList = None
833 833
834 834 channelList = None
835 835
836 836 flagNoData = True
837 837
838 838 flagDiscontinuousBlock = False
839 839
840 840 useLocalTime = False
841 841
842 842 utctime = None
843 843
844 844 timeZone = None
845 845
846 846 # ippSeconds = None
847 847
848 848 # timeInterval = None
849 849
850 850 nCohInt = None
851 851
852 852 nIncohInt = None
853 853
854 854 noise = None
855 855
856 856 windowOfFilter = 1
857 857
858 858 # Speed of ligth
859 859 C = 3e8
860 860
861 861 frequency = 49.92e6
862 862
863 863 realtime = False
864 864
865 865 def __init__(self):
866 866
867 867 self.type = "Fits"
868 868
869 869 self.nProfiles = None
870 870
871 871 self.heightList = None
872 872
873 873 self.channelList = None
874 874
875 875 # self.channelIndexList = None
876 876
877 877 self.flagNoData = True
878 878
879 879 self.utctime = None
880 880
881 881 self.nCohInt = 1
882 882
883 883 self.nIncohInt = 1
884 884
885 885 self.useLocalTime = True
886 886
887 887 self.profileIndex = 0
888 888
889 889 # self.utctime = None
890 890 # self.timeZone = None
891 891 # self.ltctime = None
892 892 # self.timeInterval = None
893 893 # self.header = None
894 894 # self.data_header = None
895 895 # self.data = None
896 896 # self.datatime = None
897 897 # self.flagNoData = False
898 898 # self.expName = ''
899 899 # self.nChannels = None
900 900 # self.nSamples = None
901 901 # self.dataBlocksPerFile = None
902 902 # self.comments = ''
903 903 #
904 904
905 905 def getltctime(self):
906 906
907 907 if self.useLocalTime:
908 908 return self.utctime - self.timeZone * 60
909 909
910 910 return self.utctime
911 911
912 912 def getDatatime(self):
913 913
914 914 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
915 915 return datatime
916 916
917 917 def getTimeRange(self):
918 918
919 919 datatime = []
920 920
921 921 datatime.append(self.ltctime)
922 922 datatime.append(self.ltctime + self.timeInterval)
923 923
924 924 datatime = numpy.array(datatime)
925 925
926 926 return datatime
927 927
928 928 def getHeiRange(self):
929 929
930 930 heis = self.heightList
931 931
932 932 return heis
933 933
934 934 def getNHeights(self):
935 935
936 936 return len(self.heightList)
937 937
938 938 def getNChannels(self):
939 939
940 940 return len(self.channelList)
941 941
942 942 def getChannelIndexList(self):
943 943
944 return range(self.nChannels)
944 return list(range(self.nChannels))
945 945
946 946 def getNoise(self, type=1):
947 947
948 948 #noise = numpy.zeros(self.nChannels)
949 949
950 950 if type == 1:
951 951 noise = self.getNoisebyHildebrand()
952 952
953 953 if type == 2:
954 954 noise = self.getNoisebySort()
955 955
956 956 if type == 3:
957 957 noise = self.getNoisebyWindow()
958 958
959 959 return noise
960 960
961 961 def getTimeInterval(self):
962 962
963 963 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
964 964
965 965 return timeInterval
966 966
967 967 datatime = property(getDatatime, "I'm the 'datatime' property")
968 968 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
969 969 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
970 970 channelIndexList = property(
971 971 getChannelIndexList, "I'm the 'channelIndexList' property.")
972 972 noise = property(getNoise, "I'm the 'nHeights' property.")
973 973
974 974 ltctime = property(getltctime, "I'm the 'ltctime' property")
975 975 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
976 976
977 977
978 978 class Correlation(JROData):
979 979
980 980 noise = None
981 981
982 982 SNR = None
983 983
984 984 #--------------------------------------------------
985 985
986 986 mode = None
987 987
988 988 split = False
989 989
990 990 data_cf = None
991 991
992 992 lags = None
993 993
994 994 lagRange = None
995 995
996 996 pairsList = None
997 997
998 998 normFactor = None
999 999
1000 1000 #--------------------------------------------------
1001 1001
1002 1002 # calculateVelocity = None
1003 1003
1004 1004 nLags = None
1005 1005
1006 1006 nPairs = None
1007 1007
1008 1008 nAvg = None
1009 1009
1010 1010 def __init__(self):
1011 1011 '''
1012 1012 Constructor
1013 1013 '''
1014 1014 self.radarControllerHeaderObj = RadarControllerHeader()
1015 1015
1016 1016 self.systemHeaderObj = SystemHeader()
1017 1017
1018 1018 self.type = "Correlation"
1019 1019
1020 1020 self.data = None
1021 1021
1022 1022 self.dtype = None
1023 1023
1024 1024 self.nProfiles = None
1025 1025
1026 1026 self.heightList = None
1027 1027
1028 1028 self.channelList = None
1029 1029
1030 1030 self.flagNoData = True
1031 1031
1032 1032 self.flagDiscontinuousBlock = False
1033 1033
1034 1034 self.utctime = None
1035 1035
1036 1036 self.timeZone = None
1037 1037
1038 1038 self.dstFlag = None
1039 1039
1040 1040 self.errorCount = None
1041 1041
1042 1042 self.blocksize = None
1043 1043
1044 1044 self.flagDecodeData = False # asumo q la data no esta decodificada
1045 1045
1046 1046 self.flagDeflipData = False # asumo q la data no esta sin flip
1047 1047
1048 1048 self.pairsList = None
1049 1049
1050 1050 self.nPoints = None
1051 1051
1052 1052 def getPairsList(self):
1053 1053
1054 1054 return self.pairsList
1055 1055
1056 1056 def getNoise(self, mode=2):
1057 1057
1058 1058 indR = numpy.where(self.lagR == 0)[0][0]
1059 1059 indT = numpy.where(self.lagT == 0)[0][0]
1060 1060
1061 1061 jspectra0 = self.data_corr[:, :, indR, :]
1062 1062 jspectra = copy.copy(jspectra0)
1063 1063
1064 1064 num_chan = jspectra.shape[0]
1065 1065 num_hei = jspectra.shape[2]
1066 1066
1067 1067 freq_dc = jspectra.shape[1] / 2
1068 1068 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
1069 1069
1070 1070 if ind_vel[0] < 0:
1071 ind_vel[range(0, 1)] = ind_vel[range(0, 1)] + self.num_prof
1071 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
1072 1072
1073 1073 if mode == 1:
1074 1074 jspectra[:, freq_dc, :] = (
1075 1075 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
1076 1076
1077 1077 if mode == 2:
1078 1078
1079 1079 vel = numpy.array([-2, -1, 1, 2])
1080 1080 xx = numpy.zeros([4, 4])
1081 1081
1082 1082 for fil in range(4):
1083 xx[fil, :] = vel[fil]**numpy.asarray(range(4))
1083 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
1084 1084
1085 1085 xx_inv = numpy.linalg.inv(xx)
1086 1086 xx_aux = xx_inv[0, :]
1087 1087
1088 1088 for ich in range(num_chan):
1089 1089 yy = jspectra[ich, ind_vel, :]
1090 1090 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
1091 1091
1092 1092 junkid = jspectra[ich, freq_dc, :] <= 0
1093 1093 cjunkid = sum(junkid)
1094 1094
1095 1095 if cjunkid.any():
1096 1096 jspectra[ich, freq_dc, junkid.nonzero()] = (
1097 1097 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
1098 1098
1099 1099 noise = jspectra0[:, freq_dc, :] - jspectra[:, freq_dc, :]
1100 1100
1101 1101 return noise
1102 1102
1103 1103 def getTimeInterval(self):
1104 1104
1105 1105 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
1106 1106
1107 1107 return timeInterval
1108 1108
1109 1109 def splitFunctions(self):
1110 1110
1111 1111 pairsList = self.pairsList
1112 1112 ccf_pairs = []
1113 1113 acf_pairs = []
1114 1114 ccf_ind = []
1115 1115 acf_ind = []
1116 1116 for l in range(len(pairsList)):
1117 1117 chan0 = pairsList[l][0]
1118 1118 chan1 = pairsList[l][1]
1119 1119
1120 1120 # Obteniendo pares de Autocorrelacion
1121 1121 if chan0 == chan1:
1122 1122 acf_pairs.append(chan0)
1123 1123 acf_ind.append(l)
1124 1124 else:
1125 1125 ccf_pairs.append(pairsList[l])
1126 1126 ccf_ind.append(l)
1127 1127
1128 1128 data_acf = self.data_cf[acf_ind]
1129 1129 data_ccf = self.data_cf[ccf_ind]
1130 1130
1131 1131 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1132 1132
1133 1133 def getNormFactor(self):
1134 1134 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1135 1135 acf_pairs = numpy.array(acf_pairs)
1136 1136 normFactor = numpy.zeros((self.nPairs, self.nHeights))
1137 1137
1138 1138 for p in range(self.nPairs):
1139 1139 pair = self.pairsList[p]
1140 1140
1141 1141 ch0 = pair[0]
1142 1142 ch1 = pair[1]
1143 1143
1144 1144 ch0_max = numpy.max(data_acf[acf_pairs == ch0, :, :], axis=1)
1145 1145 ch1_max = numpy.max(data_acf[acf_pairs == ch1, :, :], axis=1)
1146 1146 normFactor[p, :] = numpy.sqrt(ch0_max * ch1_max)
1147 1147
1148 1148 return normFactor
1149 1149
1150 1150 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1151 1151 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1152 1152
1153 1153
1154 1154 class Parameters(Spectra):
1155 1155
1156 1156 experimentInfo = None # Information about the experiment
1157 1157
1158 1158 # Information from previous data
1159 1159
1160 1160 inputUnit = None # Type of data to be processed
1161 1161
1162 1162 operation = None # Type of operation to parametrize
1163 1163
1164 1164 # normFactor = None #Normalization Factor
1165 1165
1166 1166 groupList = None # List of Pairs, Groups, etc
1167 1167
1168 1168 # Parameters
1169 1169
1170 1170 data_param = None # Parameters obtained
1171 1171
1172 1172 data_pre = None # Data Pre Parametrization
1173 1173
1174 1174 data_SNR = None # Signal to Noise Ratio
1175 1175
1176 1176 # heightRange = None #Heights
1177 1177
1178 1178 abscissaList = None # Abscissa, can be velocities, lags or time
1179 1179
1180 1180 # noise = None #Noise Potency
1181 1181
1182 1182 utctimeInit = None # Initial UTC time
1183 1183
1184 1184 paramInterval = None # Time interval to calculate Parameters in seconds
1185 1185
1186 1186 useLocalTime = True
1187 1187
1188 1188 # Fitting
1189 1189
1190 1190 data_error = None # Error of the estimation
1191 1191
1192 1192 constants = None
1193 1193
1194 1194 library = None
1195 1195
1196 1196 # Output signal
1197 1197
1198 1198 outputInterval = None # Time interval to calculate output signal in seconds
1199 1199
1200 1200 data_output = None # Out signal
1201 1201
1202 1202 nAvg = None
1203 1203
1204 1204 noise_estimation = None
1205 1205
1206 1206 GauSPC = None # Fit gaussian SPC
1207 1207
1208 1208 def __init__(self):
1209 1209 '''
1210 1210 Constructor
1211 1211 '''
1212 1212 self.radarControllerHeaderObj = RadarControllerHeader()
1213 1213
1214 1214 self.systemHeaderObj = SystemHeader()
1215 1215
1216 1216 self.type = "Parameters"
1217 1217
1218 1218 def getTimeRange1(self, interval):
1219 1219
1220 1220 datatime = []
1221 1221
1222 1222 if self.useLocalTime:
1223 1223 time1 = self.utctimeInit - self.timeZone * 60
1224 1224 else:
1225 1225 time1 = self.utctimeInit
1226 1226
1227 1227 datatime.append(time1)
1228 1228 datatime.append(time1 + interval)
1229 1229 datatime = numpy.array(datatime)
1230 1230
1231 1231 return datatime
1232 1232
1233 1233 def getTimeInterval(self):
1234 1234
1235 1235 if hasattr(self, 'timeInterval1'):
1236 1236 return self.timeInterval1
1237 1237 else:
1238 1238 return self.paramInterval
1239 1239
1240 1240 def setValue(self, value):
1241 1241
1242 print "This property should not be initialized"
1242 print("This property should not be initialized")
1243 1243
1244 1244 return
1245 1245
1246 1246 def getNoise(self):
1247 1247
1248 1248 return self.spc_noise
1249 1249
1250 1250 timeInterval = property(getTimeInterval)
1251 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
1251 noise = property(getNoise, setValue, "I'm the 'Noise' property.") No newline at end of file
@@ -1,905 +1,907
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 5 '''
6 6 import sys
7 7 import numpy
8 8 import copy
9 9 import datetime
10 10 import inspect
11 from schainpy.utils import log
11 12
12 13 SPEED_OF_LIGHT = 299792458
13 14 SPEED_OF_LIGHT = 3e8
14 15
15 16 BASIC_STRUCTURE = numpy.dtype([
16 17 ('nSize', '<u4'),
17 18 ('nVersion', '<u2'),
18 19 ('nDataBlockId', '<u4'),
19 20 ('nUtime', '<u4'),
20 21 ('nMilsec', '<u2'),
21 22 ('nTimezone', '<i2'),
22 23 ('nDstflag', '<i2'),
23 24 ('nErrorCount', '<u4')
24 25 ])
25 26
26 27 SYSTEM_STRUCTURE = numpy.dtype([
27 28 ('nSize', '<u4'),
28 29 ('nNumSamples', '<u4'),
29 30 ('nNumProfiles', '<u4'),
30 31 ('nNumChannels', '<u4'),
31 32 ('nADCResolution', '<u4'),
32 33 ('nPCDIOBusWidth', '<u4'),
33 34 ])
34 35
35 36 RADAR_STRUCTURE = numpy.dtype([
36 37 ('nSize', '<u4'),
37 38 ('nExpType', '<u4'),
38 39 ('nNTx', '<u4'),
39 40 ('fIpp', '<f4'),
40 41 ('fTxA', '<f4'),
41 42 ('fTxB', '<f4'),
42 43 ('nNumWindows', '<u4'),
43 44 ('nNumTaus', '<u4'),
44 45 ('nCodeType', '<u4'),
45 46 ('nLine6Function', '<u4'),
46 47 ('nLine5Function', '<u4'),
47 48 ('fClock', '<f4'),
48 49 ('nPrePulseBefore', '<u4'),
49 50 ('nPrePulseAfter', '<u4'),
50 51 ('sRangeIPP', '<a20'),
51 52 ('sRangeTxA', '<a20'),
52 53 ('sRangeTxB', '<a20'),
53 54 ])
54 55
55 56 SAMPLING_STRUCTURE = numpy.dtype(
56 57 [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')])
57 58
58 59
59 60 PROCESSING_STRUCTURE = numpy.dtype([
60 61 ('nSize', '<u4'),
61 62 ('nDataType', '<u4'),
62 63 ('nSizeOfDataBlock', '<u4'),
63 64 ('nProfilesperBlock', '<u4'),
64 65 ('nDataBlocksperFile', '<u4'),
65 66 ('nNumWindows', '<u4'),
66 67 ('nProcessFlags', '<u4'),
67 68 ('nCoherentIntegrations', '<u4'),
68 69 ('nIncoherentIntegrations', '<u4'),
69 70 ('nTotalSpectra', '<u4')
70 71 ])
71 72
72 73
73 74 class Header(object):
74 75
75 76 def __init__(self):
76 77 raise NotImplementedError
77 78
78 79 def copy(self):
79 80 return copy.deepcopy(self)
80 81
81 82 def read(self):
82 83
83 84 raise NotImplementedError
84 85
85 86 def write(self):
86 87
87 88 raise NotImplementedError
88 89
89 90 def getAllowedArgs(self):
90 91 args = inspect.getargspec(self.__init__).args
91 92 try:
92 93 args.remove('self')
93 94 except:
94 95 pass
95 96 return args
96 97
97 98 def getAsDict(self):
98 99 args = self.getAllowedArgs()
99 100 asDict = {}
100 101 for x in args:
101 102 asDict[x] = self[x]
102 103 return asDict
103 104
104 105 def __getitem__(self, name):
105 106 return getattr(self, name)
106 107
107 108 def printInfo(self):
108 109
109 110 message = "#" * 50 + "\n"
110 111 message += self.__class__.__name__.upper() + "\n"
111 112 message += "#" * 50 + "\n"
112 113
113 keyList = self.__dict__.keys()
114 keyList = list(self.__dict__.keys())
114 115 keyList.sort()
115 116
116 117 for key in keyList:
117 118 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
118 119
119 120 if "size" not in keyList:
120 121 attr = getattr(self, "size")
121 122
122 123 if attr:
123 124 message += "%s = %s" % ("size", attr) + "\n"
124 125
125 print message
126 print(message)
126 127
127 128
128 129 class BasicHeader(Header):
129 130
130 131 size = None
131 132 version = None
132 133 dataBlock = None
133 134 utc = None
134 135 ltc = None
135 136 miliSecond = None
136 137 timeZone = None
137 138 dstFlag = None
138 139 errorCount = None
139 140 datatime = None
140 141 structure = BASIC_STRUCTURE
141 142 __LOCALTIME = None
142 143
143 144 def __init__(self, useLocalTime=True):
144 145
145 146 self.size = 24
146 147 self.version = 0
147 148 self.dataBlock = 0
148 149 self.utc = 0
149 150 self.miliSecond = 0
150 151 self.timeZone = 0
151 152 self.dstFlag = 0
152 153 self.errorCount = 0
153 154
154 155 self.useLocalTime = useLocalTime
155 156
156 157 def read(self, fp):
157 158
158 159 self.length = 0
159 160 try:
160 161 if hasattr(fp, 'read'):
161 162 header = numpy.fromfile(fp, BASIC_STRUCTURE, 1)
162 163 else:
163 164 header = numpy.fromstring(fp, BASIC_STRUCTURE, 1)
164 except Exception, e:
165 print "BasicHeader: "
166 print e
165 except Exception as e:
166 print("BasicHeader: ")
167 print(e)
167 168 return 0
168 169
169 170 self.size = int(header['nSize'][0])
170 171 self.version = int(header['nVersion'][0])
171 172 self.dataBlock = int(header['nDataBlockId'][0])
172 173 self.utc = int(header['nUtime'][0])
173 174 self.miliSecond = int(header['nMilsec'][0])
174 175 self.timeZone = int(header['nTimezone'][0])
175 176 self.dstFlag = int(header['nDstflag'][0])
176 177 self.errorCount = int(header['nErrorCount'][0])
177 178
178 179 if self.size < 24:
179 180 return 0
180 181
181 182 self.length = header.nbytes
182 183 return 1
183 184
184 185 def write(self, fp):
185 186
186 187 headerTuple = (self.size, self.version, self.dataBlock, self.utc,
187 188 self.miliSecond, self.timeZone, self.dstFlag, self.errorCount)
188 189 header = numpy.array(headerTuple, BASIC_STRUCTURE)
189 190 header.tofile(fp)
190 191
191 192 return 1
192 193
193 194 def get_ltc(self):
194 195
195 196 return self.utc - self.timeZone * 60
196 197
197 198 def set_ltc(self, value):
198 199
199 200 self.utc = value + self.timeZone * 60
200 201
201 202 def get_datatime(self):
202 203
203 204 return datetime.datetime.utcfromtimestamp(self.ltc)
204 205
205 206 ltc = property(get_ltc, set_ltc)
206 207 datatime = property(get_datatime)
207 208
208 209
209 210 class SystemHeader(Header):
210 211
211 212 size = None
212 213 nSamples = None
213 214 nProfiles = None
214 215 nChannels = None
215 216 adcResolution = None
216 217 pciDioBusWidth = None
217 218 structure = SYSTEM_STRUCTURE
218 219
219 220 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0):
220 221
221 222 self.size = 24
222 223 self.nSamples = nSamples
223 224 self.nProfiles = nProfiles
224 225 self.nChannels = nChannels
225 226 self.adcResolution = adcResolution
226 227 self.pciDioBusWidth = pciDioBusWidth
227 228
228 229 def read(self, fp):
229 230 self.length = 0
230 231 try:
231 232 startFp = fp.tell()
232 except Exception, e:
233 except Exception as e:
233 234 startFp = None
234 235 pass
235 236
236 237 try:
237 238 if hasattr(fp, 'read'):
238 239 header = numpy.fromfile(fp, SYSTEM_STRUCTURE, 1)
239 240 else:
240 241 header = numpy.fromstring(fp, SYSTEM_STRUCTURE, 1)
241 except Exception, e:
242 print "System Header: " + str(e)
242 except Exception as e:
243 print("System Header: " + str(e))
243 244 return 0
244 245
245 246 self.size = header['nSize'][0]
246 247 self.nSamples = header['nNumSamples'][0]
247 248 self.nProfiles = header['nNumProfiles'][0]
248 249 self.nChannels = header['nNumChannels'][0]
249 250 self.adcResolution = header['nADCResolution'][0]
250 251 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
251 252
252 253 if startFp is not None:
253 254 endFp = self.size + startFp
254 255
255 256 if fp.tell() > endFp:
256 257 sys.stderr.write(
257 258 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp.name)
258 259 return 0
259 260
260 261 if fp.tell() < endFp:
261 262 sys.stderr.write(
262 263 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp.name)
263 264 return 0
264 265
265 266 self.length = header.nbytes
266 267 return 1
267 268
268 269 def write(self, fp):
269 270
270 271 headerTuple = (self.size, self.nSamples, self.nProfiles,
271 272 self.nChannels, self.adcResolution, self.pciDioBusWidth)
272 273 header = numpy.array(headerTuple, SYSTEM_STRUCTURE)
273 274 header.tofile(fp)
274 275
275 276 return 1
276 277
277 278
278 279 class RadarControllerHeader(Header):
279 280
280 281 expType = None
281 282 nTx = None
282 283 ipp = None
283 284 txA = None
284 285 txB = None
285 286 nWindows = None
286 287 numTaus = None
287 288 codeType = None
288 289 line6Function = None
289 290 line5Function = None
290 291 fClock = None
291 292 prePulseBefore = None
292 293 prePulseAfter = None
293 294 rangeIpp = None
294 295 rangeTxA = None
295 296 rangeTxB = None
296 297 structure = RADAR_STRUCTURE
297 298 __size = None
298 299
299 300 def __init__(self, expType=2, nTx=1,
300 301 ipp=None, txA=0, txB=0,
301 302 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
302 303 numTaus=0, line6Function=0, line5Function=0, fClock=None,
303 304 prePulseBefore=0, prePulseAfter=0,
304 305 codeType=0, nCode=0, nBaud=0, code=None,
305 306 flip1=0, flip2=0):
306 307
307 308 # self.size = 116
308 309 self.expType = expType
309 310 self.nTx = nTx
310 311 self.ipp = ipp
311 312 self.txA = txA
312 313 self.txB = txB
313 314 self.rangeIpp = ipp
314 315 self.rangeTxA = txA
315 316 self.rangeTxB = txB
316 317
317 318 self.nWindows = nWindows
318 319 self.numTaus = numTaus
319 320 self.codeType = codeType
320 321 self.line6Function = line6Function
321 322 self.line5Function = line5Function
322 323 self.fClock = fClock
323 324 self.prePulseBefore = prePulseBefore
324 325 self.prePulseAfter = prePulseAfter
325 326
326 327 self.nHeights = nHeights
327 328 self.firstHeight = firstHeight
328 329 self.deltaHeight = deltaHeight
329 330 self.samplesWin = nHeights
330 331
331 332 self.nCode = nCode
332 333 self.nBaud = nBaud
333 334 self.code = code
334 335 self.flip1 = flip1
335 336 self.flip2 = flip2
336 337
337 338 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
338 339 # self.dynamic = numpy.array([],numpy.dtype('byte'))
339 340
340 341 if self.fClock is None and self.deltaHeight is not None:
341 342 self.fClock = 0.15 / (deltaHeight * 1e-6) # 0.15Km / (height * 1u)
342 343
343 344 def read(self, fp):
344 345 self.length = 0
345 346 try:
346 347 startFp = fp.tell()
347 except Exception, e:
348 except Exception as e:
348 349 startFp = None
349 350 pass
350 351
351 352 try:
352 353 if hasattr(fp, 'read'):
353 354 header = numpy.fromfile(fp, RADAR_STRUCTURE, 1)
354 355 else:
355 356 header = numpy.fromstring(fp, RADAR_STRUCTURE, 1)
356 357 self.length += header.nbytes
357 except Exception, e:
358 print "RadarControllerHeader: " + str(e)
358 except Exception as e:
359 print("RadarControllerHeader: " + str(e))
359 360 return 0
360 361
361 362 size = int(header['nSize'][0])
362 363 self.expType = int(header['nExpType'][0])
363 364 self.nTx = int(header['nNTx'][0])
364 365 self.ipp = float(header['fIpp'][0])
365 366 self.txA = float(header['fTxA'][0])
366 367 self.txB = float(header['fTxB'][0])
367 368 self.nWindows = int(header['nNumWindows'][0])
368 369 self.numTaus = int(header['nNumTaus'][0])
369 370 self.codeType = int(header['nCodeType'][0])
370 371 self.line6Function = int(header['nLine6Function'][0])
371 372 self.line5Function = int(header['nLine5Function'][0])
372 373 self.fClock = float(header['fClock'][0])
373 374 self.prePulseBefore = int(header['nPrePulseBefore'][0])
374 375 self.prePulseAfter = int(header['nPrePulseAfter'][0])
375 376 self.rangeIpp = header['sRangeIPP'][0]
376 377 self.rangeTxA = header['sRangeTxA'][0]
377 378 self.rangeTxB = header['sRangeTxB'][0]
378 379
379 380 try:
380 381 if hasattr(fp, 'read'):
381 382 samplingWindow = numpy.fromfile(
382 383 fp, SAMPLING_STRUCTURE, self.nWindows)
383 384 else:
384 385 samplingWindow = numpy.fromstring(
385 386 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
386 387 self.length += samplingWindow.nbytes
387 except Exception, e:
388 print "RadarControllerHeader: " + str(e)
388 except Exception as e:
389 print("RadarControllerHeader: " + str(e))
389 390 return 0
390 391 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
391 392 self.firstHeight = samplingWindow['h0']
392 393 self.deltaHeight = samplingWindow['dh']
393 394 self.samplesWin = samplingWindow['nsa']
394 395
395 396 try:
396 397 if hasattr(fp, 'read'):
397 398 self.Taus = numpy.fromfile(fp, '<f4', self.numTaus)
398 399 else:
399 400 self.Taus = numpy.fromstring(
400 401 fp[self.length:], '<f4', self.numTaus)
401 402 self.length += self.Taus.nbytes
402 except Exception, e:
403 print "RadarControllerHeader: " + str(e)
403 except Exception as e:
404 print("RadarControllerHeader: " + str(e))
404 405 return 0
405 406
406 407 self.code_size = 0
407 408 if self.codeType != 0:
408 409
409 410 try:
410 411 if hasattr(fp, 'read'):
411 412 self.nCode = numpy.fromfile(fp, '<u4', 1)[0]
412 413 self.length += self.nCode.nbytes
413 414 self.nBaud = numpy.fromfile(fp, '<u4', 1)[0]
414 415 self.length += self.nBaud.nbytes
415 416 else:
416 417 self.nCode = numpy.fromstring(
417 418 fp[self.length:], '<u4', 1)[0]
418 419 self.length += self.nCode.nbytes
419 420 self.nBaud = numpy.fromstring(
420 421 fp[self.length:], '<u4', 1)[0]
421 422 self.length += self.nBaud.nbytes
422 except Exception, e:
423 print "RadarControllerHeader: " + str(e)
423 except Exception as e:
424 print("RadarControllerHeader: " + str(e))
424 425 return 0
425 426 code = numpy.empty([self.nCode, self.nBaud], dtype='i1')
426 427
427 428 for ic in range(self.nCode):
428 429 try:
429 430 if hasattr(fp, 'read'):
430 431 temp = numpy.fromfile(fp, 'u4', int(
431 432 numpy.ceil(self.nBaud / 32.)))
432 433 else:
433 434 temp = numpy.fromstring(
434 435 fp, 'u4', int(numpy.ceil(self.nBaud / 32.)))
435 436 self.length += temp.nbytes
436 except Exception, e:
437 print "RadarControllerHeader: " + str(e)
437 except Exception as e:
438 print("RadarControllerHeader: " + str(e))
438 439 return 0
439 440
440 441 for ib in range(self.nBaud - 1, -1, -1):
441 code[ic, ib] = temp[ib / 32] % 2
442 temp[ib / 32] = temp[ib / 32] / 2
442 log.error(ib / 32)
443 code[ic, ib] = temp[int(ib / 32)] % 2
444 temp[int(ib / 32)] = temp[int(ib / 32)] / 2
443 445
444 446 self.code = 2.0 * code - 1.0
445 447 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
446 448
447 449 # if self.line5Function == RCfunction.FLIP:
448 450 # self.flip1 = numpy.fromfile(fp,'<u4',1)
449 451 #
450 452 # if self.line6Function == RCfunction.FLIP:
451 453 # self.flip2 = numpy.fromfile(fp,'<u4',1)
452 454 if startFp is not None:
453 455 endFp = size + startFp
454 456
455 457 if fp.tell() != endFp:
456 458 # fp.seek(endFp)
457 print "%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" % (fp.name, fp.tell() - startFp, size)
459 print("%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" % (fp.name, fp.tell() - startFp, size))
458 460 # return 0
459 461
460 462 if fp.tell() > endFp:
461 463 sys.stderr.write(
462 464 "Warning %s: Size value read from Radar Controller header is lower than it has to be\n" % fp.name)
463 465 # return 0
464 466
465 467 if fp.tell() < endFp:
466 468 sys.stderr.write(
467 469 "Warning %s: Size value read from Radar Controller header is greater than it has to be\n" % fp.name)
468 470
469 471 return 1
470 472
471 473 def write(self, fp):
472 474
473 475 headerTuple = (self.size,
474 476 self.expType,
475 477 self.nTx,
476 478 self.ipp,
477 479 self.txA,
478 480 self.txB,
479 481 self.nWindows,
480 482 self.numTaus,
481 483 self.codeType,
482 484 self.line6Function,
483 485 self.line5Function,
484 486 self.fClock,
485 487 self.prePulseBefore,
486 488 self.prePulseAfter,
487 489 self.rangeIpp,
488 490 self.rangeTxA,
489 491 self.rangeTxB)
490 492
491 493 header = numpy.array(headerTuple, RADAR_STRUCTURE)
492 494 header.tofile(fp)
493 495
494 496 sampleWindowTuple = (
495 497 self.firstHeight, self.deltaHeight, self.samplesWin)
496 498 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
497 499 samplingWindow.tofile(fp)
498 500
499 501 if self.numTaus > 0:
500 502 self.Taus.tofile(fp)
501 503
502 504 if self.codeType != 0:
503 505 nCode = numpy.array(self.nCode, '<u4')
504 506 nCode.tofile(fp)
505 507 nBaud = numpy.array(self.nBaud, '<u4')
506 508 nBaud.tofile(fp)
507 509 code1 = (self.code + 1.0) / 2.
508 510
509 511 for ic in range(self.nCode):
510 512 tempx = numpy.zeros(int(numpy.ceil(self.nBaud / 32.)))
511 513 start = 0
512 514 end = 32
513 515 for i in range(len(tempx)):
514 516 code_selected = code1[ic, start:end]
515 517 for j in range(len(code_selected) - 1, -1, -1):
516 518 if code_selected[j] == 1:
517 519 tempx[i] = tempx[i] + \
518 520 2**(len(code_selected) - 1 - j)
519 521 start = start + 32
520 522 end = end + 32
521 523
522 524 tempx = tempx.astype('u4')
523 525 tempx.tofile(fp)
524 526
525 527 # if self.line5Function == RCfunction.FLIP:
526 528 # self.flip1.tofile(fp)
527 529 #
528 530 # if self.line6Function == RCfunction.FLIP:
529 531 # self.flip2.tofile(fp)
530 532
531 533 return 1
532 534
533 535 def get_ippSeconds(self):
534 536 '''
535 537 '''
536 538 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
537 539
538 540 return ippSeconds
539 541
540 542 def set_ippSeconds(self, ippSeconds):
541 543 '''
542 544 '''
543 545
544 546 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0 * 1000)
545 547
546 548 return
547 549
548 550 def get_size(self):
549 551
550 552 self.__size = 116 + 12 * self.nWindows + 4 * self.numTaus
551 553
552 554 if self.codeType != 0:
553 555 self.__size += 4 + 4 + 4 * self.nCode * \
554 556 numpy.ceil(self.nBaud / 32.)
555 557
556 558 return self.__size
557 559
558 560 def set_size(self, value):
559 561
560 raise IOError, "size is a property and it cannot be set, just read"
562 raise IOError("size is a property and it cannot be set, just read")
561 563
562 564 return
563 565
564 566 ippSeconds = property(get_ippSeconds, set_ippSeconds)
565 567 size = property(get_size, set_size)
566 568
567 569
568 570 class ProcessingHeader(Header):
569 571
570 572 # size = None
571 573 dtype = None
572 574 blockSize = None
573 575 profilesPerBlock = None
574 576 dataBlocksPerFile = None
575 577 nWindows = None
576 578 processFlags = None
577 579 nCohInt = None
578 580 nIncohInt = None
579 581 totalSpectra = None
580 582 structure = PROCESSING_STRUCTURE
581 583 flag_dc = None
582 584 flag_cspc = None
583 585
584 586 def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
585 587 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
586 588 code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False
587 589 ):
588 590
589 591 # self.size = 0
590 592 self.dtype = dtype
591 593 self.blockSize = blockSize
592 594 self.profilesPerBlock = 0
593 595 self.dataBlocksPerFile = 0
594 596 self.nWindows = 0
595 597 self.processFlags = 0
596 598 self.nCohInt = 0
597 599 self.nIncohInt = 0
598 600 self.totalSpectra = 0
599 601
600 602 self.nHeights = 0
601 603 self.firstHeight = 0
602 604 self.deltaHeight = 0
603 605 self.samplesWin = 0
604 606 self.spectraComb = 0
605 607 self.nCode = None
606 608 self.code = None
607 609 self.nBaud = None
608 610
609 611 self.shif_fft = False
610 612 self.flag_dc = False
611 613 self.flag_cspc = False
612 614 self.flag_decode = False
613 615 self.flag_deflip = False
614 616 self.length = 0
615 617
616 618 def read(self, fp):
617 619 self.length = 0
618 620 try:
619 621 startFp = fp.tell()
620 except Exception, e:
622 except Exception as e:
621 623 startFp = None
622 624 pass
623 625
624 626 try:
625 627 if hasattr(fp, 'read'):
626 628 header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1)
627 629 else:
628 630 header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1)
629 631 self.length += header.nbytes
630 except Exception, e:
631 print "ProcessingHeader: " + str(e)
632 except Exception as e:
633 print("ProcessingHeader: " + str(e))
632 634 return 0
633 635
634 636 size = int(header['nSize'][0])
635 637 self.dtype = int(header['nDataType'][0])
636 638 self.blockSize = int(header['nSizeOfDataBlock'][0])
637 639 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
638 640 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
639 641 self.nWindows = int(header['nNumWindows'][0])
640 642 self.processFlags = header['nProcessFlags']
641 643 self.nCohInt = int(header['nCoherentIntegrations'][0])
642 644 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
643 645 self.totalSpectra = int(header['nTotalSpectra'][0])
644 646
645 647 try:
646 648 if hasattr(fp, 'read'):
647 649 samplingWindow = numpy.fromfile(
648 650 fp, SAMPLING_STRUCTURE, self.nWindows)
649 651 else:
650 652 samplingWindow = numpy.fromstring(
651 653 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
652 654 self.length += samplingWindow.nbytes
653 except Exception, e:
654 print "ProcessingHeader: " + str(e)
655 except Exception as e:
656 print("ProcessingHeader: " + str(e))
655 657 return 0
656 658
657 659 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
658 660 self.firstHeight = float(samplingWindow['h0'][0])
659 661 self.deltaHeight = float(samplingWindow['dh'][0])
660 662 self.samplesWin = samplingWindow['nsa'][0]
661 663
662 664 try:
663 665 if hasattr(fp, 'read'):
664 666 self.spectraComb = numpy.fromfile(
665 667 fp, 'u1', 2 * self.totalSpectra)
666 668 else:
667 669 self.spectraComb = numpy.fromstring(
668 670 fp[self.length:], 'u1', 2 * self.totalSpectra)
669 671 self.length += self.spectraComb.nbytes
670 except Exception, e:
671 print "ProcessingHeader: " + str(e)
672 except Exception as e:
673 print("ProcessingHeader: " + str(e))
672 674 return 0
673 675
674 676 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
675 677 self.nCode = int(numpy.fromfile(fp, '<u4', 1))
676 678 self.nBaud = int(numpy.fromfile(fp, '<u4', 1))
677 679 self.code = numpy.fromfile(
678 680 fp, '<f4', self.nCode * self.nBaud).reshape(self.nCode, self.nBaud)
679 681
680 682 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
681 683 exp_name_len = int(numpy.fromfile(fp, '<u4', 1))
682 684 exp_name = numpy.fromfile(fp, 'u1', exp_name_len + 1)
683 685
684 686 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
685 687 self.shif_fft = True
686 688 else:
687 689 self.shif_fft = False
688 690
689 691 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
690 692 self.flag_dc = True
691 693 else:
692 694 self.flag_dc = False
693 695
694 696 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
695 697 self.flag_decode = True
696 698 else:
697 699 self.flag_decode = False
698 700
699 701 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
700 702 self.flag_deflip = True
701 703 else:
702 704 self.flag_deflip = False
703 705
704 706 nChannels = 0
705 707 nPairs = 0
706 708 pairList = []
707 709
708 710 for i in range(0, self.totalSpectra * 2, 2):
709 711 if self.spectraComb[i] == self.spectraComb[i + 1]:
710 712 nChannels = nChannels + 1 # par de canales iguales
711 713 else:
712 714 nPairs = nPairs + 1 # par de canales diferentes
713 715 pairList.append((self.spectraComb[i], self.spectraComb[i + 1]))
714 716
715 717 self.flag_cspc = False
716 718 if nPairs > 0:
717 719 self.flag_cspc = True
718 720
719 721 if startFp is not None:
720 722 endFp = size + startFp
721 723 if fp.tell() > endFp:
722 724 sys.stderr.write(
723 725 "Warning: Processing header size is lower than it has to be")
724 726 return 0
725 727
726 728 if fp.tell() < endFp:
727 729 sys.stderr.write(
728 730 "Warning: Processing header size is greater than it is considered")
729 731
730 732 return 1
731 733
732 734 def write(self, fp):
733 735 # Clear DEFINE_PROCESS_CODE
734 736 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
735 737
736 738 headerTuple = (self.size,
737 739 self.dtype,
738 740 self.blockSize,
739 741 self.profilesPerBlock,
740 742 self.dataBlocksPerFile,
741 743 self.nWindows,
742 744 self.processFlags,
743 745 self.nCohInt,
744 746 self.nIncohInt,
745 747 self.totalSpectra)
746 748
747 749 header = numpy.array(headerTuple, PROCESSING_STRUCTURE)
748 750 header.tofile(fp)
749 751
750 752 if self.nWindows != 0:
751 753 sampleWindowTuple = (
752 754 self.firstHeight, self.deltaHeight, self.samplesWin)
753 755 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
754 756 samplingWindow.tofile(fp)
755 757
756 758 if self.totalSpectra != 0:
757 759 # spectraComb = numpy.array([],numpy.dtype('u1'))
758 760 spectraComb = self.spectraComb
759 761 spectraComb.tofile(fp)
760 762
761 763 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
762 764 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
763 765 # nCode.tofile(fp)
764 766 #
765 767 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
766 768 # nBaud.tofile(fp)
767 769 #
768 770 # code = self.code.reshape(self.nCode*self.nBaud)
769 771 # code = code.astype(numpy.dtype('<f4'))
770 772 # code.tofile(fp)
771 773
772 774 return 1
773 775
774 776 def get_size(self):
775 777
776 778 self.__size = 40 + 12 * self.nWindows + 2 * self.totalSpectra
777 779
778 780 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
779 781 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
780 782 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
781 783
782 784 return self.__size
783 785
784 786 def set_size(self, value):
785 787
786 raise IOError, "size is a property and it cannot be set, just read"
788 raise IOError("size is a property and it cannot be set, just read")
787 789
788 790 return
789 791
790 792 size = property(get_size, set_size)
791 793
792 794
793 795 class RCfunction:
794 796 NONE = 0
795 797 FLIP = 1
796 798 CODE = 2
797 799 SAMPLING = 3
798 800 LIN6DIV256 = 4
799 801 SYNCHRO = 5
800 802
801 803
802 804 class nCodeType:
803 805 NONE = 0
804 806 USERDEFINE = 1
805 807 BARKER2 = 2
806 808 BARKER3 = 3
807 809 BARKER4 = 4
808 810 BARKER5 = 5
809 811 BARKER7 = 6
810 812 BARKER11 = 7
811 813 BARKER13 = 8
812 814 AC128 = 9
813 815 COMPLEMENTARYCODE2 = 10
814 816 COMPLEMENTARYCODE4 = 11
815 817 COMPLEMENTARYCODE8 = 12
816 818 COMPLEMENTARYCODE16 = 13
817 819 COMPLEMENTARYCODE32 = 14
818 820 COMPLEMENTARYCODE64 = 15
819 821 COMPLEMENTARYCODE128 = 16
820 822 CODE_BINARY28 = 17
821 823
822 824
823 825 class PROCFLAG:
824 826
825 827 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
826 828 DECODE_DATA = numpy.uint32(0x00000002)
827 829 SPECTRA_CALC = numpy.uint32(0x00000004)
828 830 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
829 831 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
830 832 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
831 833
832 834 DATATYPE_CHAR = numpy.uint32(0x00000040)
833 835 DATATYPE_SHORT = numpy.uint32(0x00000080)
834 836 DATATYPE_LONG = numpy.uint32(0x00000100)
835 837 DATATYPE_INT64 = numpy.uint32(0x00000200)
836 838 DATATYPE_FLOAT = numpy.uint32(0x00000400)
837 839 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
838 840
839 841 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
840 842 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
841 843 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
842 844
843 845 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
844 846 DEFLIP_DATA = numpy.uint32(0x00010000)
845 847 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
846 848
847 849 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
848 850 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
849 851 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
850 852 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
851 853 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
852 854
853 855 EXP_NAME_ESP = numpy.uint32(0x00200000)
854 856 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
855 857
856 858 OPERATION_MASK = numpy.uint32(0x0000003F)
857 859 DATATYPE_MASK = numpy.uint32(0x00000FC0)
858 860 DATAARRANGE_MASK = numpy.uint32(0x00007000)
859 861 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
860 862
861 863
862 864 dtype0 = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
863 865 dtype1 = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
864 866 dtype2 = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
865 867 dtype3 = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
866 868 dtype4 = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
867 869 dtype5 = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
868 870
869 871 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
870 872
871 873 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
872 874 PROCFLAG.DATATYPE_SHORT,
873 875 PROCFLAG.DATATYPE_LONG,
874 876 PROCFLAG.DATATYPE_INT64,
875 877 PROCFLAG.DATATYPE_FLOAT,
876 878 PROCFLAG.DATATYPE_DOUBLE]
877 879
878 880 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
879 881
880 882
881 883 def get_dtype_index(numpy_dtype):
882 884
883 885 index = None
884 886
885 887 for i in range(len(NUMPY_DTYPE_LIST)):
886 888 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
887 889 index = i
888 890 break
889 891
890 892 return index
891 893
892 894
893 895 def get_numpy_dtype(index):
894 896
895 897 return NUMPY_DTYPE_LIST[index]
896 898
897 899
898 900 def get_procflag_dtype(index):
899 901
900 902 return PROCFLAG_DTYPE_LIST[index]
901 903
902 904
903 905 def get_dtype_width(index):
904 906
905 return DTYPE_WIDTH[index]
907 return DTYPE_WIDTH[index] No newline at end of file
@@ -1,7 +1,7
1 from jroplot_voltage import *
2 from jroplot_spectra import *
3 from jroplot_heispectra import *
4 from jroplot_correlation import *
5 from jroplot_parameters import *
6 from jroplot_data import *
7 from jroplotter import *
1 from .jroplot_voltage import *
2 from .jroplot_spectra import *
3 from .jroplot_heispectra import *
4 from .jroplot_correlation import *
5 from .jroplot_parameters import *
6 from .jroplot_data import *
7 from .jroplotter import *
@@ -1,657 +1,657
1 1 import os
2 2 import numpy
3 3 import time, datetime
4 import mpldriver
4 from schainpy.model.graphics import mpldriver
5 5
6 6 from schainpy.model.proc.jroproc_base import Operation
7 7
8 8 def isTimeInHourRange(datatime, xmin, xmax):
9 9
10 10 if xmin == None or xmax == None:
11 11 return 1
12 12 hour = datatime.hour + datatime.minute/60.0
13 13
14 14 if xmin < (xmax % 24):
15 15
16 16 if hour >= xmin and hour <= xmax:
17 17 return 1
18 18 else:
19 19 return 0
20 20
21 21 else:
22 22
23 23 if hour >= xmin or hour <= (xmax % 24):
24 24 return 1
25 25 else:
26 26 return 0
27 27
28 28 return 0
29 29
30 30 def isRealtime(utcdatatime):
31 31
32 32 utcnow = time.mktime(time.localtime())
33 33 delta = abs(utcnow - utcdatatime) # abs
34 34 if delta >= 30.:
35 35 return False
36 36 return True
37 37
38 38 class Figure(Operation):
39 39
40 40 __driver = mpldriver
41 41 fig = None
42 42
43 43 id = None
44 44 wintitle = None
45 45 width = None
46 46 height = None
47 47 nplots = None
48 48 timerange = None
49 49
50 50 axesObjList = []
51 51
52 52 WIDTH = 300
53 53 HEIGHT = 200
54 54 PREFIX = 'fig'
55 55
56 56 xmin = None
57 57 xmax = None
58 58
59 59 counter_imagwr = 0
60 60
61 61 figfile = None
62 62
63 63 created = False
64 64 parameters = {}
65 65 def __init__(self, **kwargs):
66 66
67 67 Operation.__init__(self, **kwargs)
68 68
69 69 def __del__(self):
70 70
71 71 self.__driver.closeFigure()
72 72
73 73 def getFilename(self, name, ext='.png'):
74 74
75 75 path = '%s%03d' %(self.PREFIX, self.id)
76 76 filename = '%s_%s%s' %(self.PREFIX, name, ext)
77 77 return os.path.join(path, filename)
78 78
79 79 def getAxesObjList(self):
80 80
81 81 return self.axesObjList
82 82
83 83 def getSubplots(self):
84 84
85 85 raise NotImplementedError
86 86
87 87 def getScreenDim(self, widthplot, heightplot):
88 88
89 89 nrow, ncol = self.getSubplots()
90 90
91 91 widthscreen = widthplot*ncol
92 92 heightscreen = heightplot*nrow
93 93
94 94 return widthscreen, heightscreen
95 95
96 96 def getTimeLim(self, x, xmin=None, xmax=None, timerange=None):
97 97
98 98 # if self.xmin != None and self.xmax != None:
99 99 # if timerange == None:
100 100 # timerange = self.xmax - self.xmin
101 101 # xmin = self.xmin + timerange
102 102 # xmax = self.xmax + timerange
103 103 #
104 104 # return xmin, xmax
105 105
106 106 if timerange == None and (xmin==None or xmax==None):
107 107 timerange = 14400 #seconds
108 108
109 109 if timerange != None:
110 110 txmin = x[0] #- x[0] % min(timerange/10, 10*60)
111 111 else:
112 112 txmin = x[0] #- x[0] % 10*60
113 113
114 114 thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
115 115 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
116 116
117 117 if timerange != None:
118 118 xmin = (thisdatetime - thisdate).seconds/(60*60.)
119 119 xmax = xmin + timerange/(60*60.)
120 120
121 121 d1970 = datetime.datetime(1970,1,1)
122 122
123 123 mindt = thisdate + datetime.timedelta(hours=xmin) #- datetime.timedelta(seconds=time.timezone)
124 124 xmin_sec = (mindt - d1970).total_seconds() #time.mktime(mindt.timetuple()) - time.timezone
125 125
126 126 maxdt = thisdate + datetime.timedelta(hours=xmax) #- datetime.timedelta(seconds=time.timezone)
127 127 xmax_sec = (maxdt - d1970).total_seconds() #time.mktime(maxdt.timetuple()) - time.timezone
128 128
129 129 return xmin_sec, xmax_sec
130 130
131 131 def init(self, id, nplots, wintitle):
132 132
133 raise NotImplementedError, "This method has been replaced by createFigure"
133 raise NotImplementedError("This method has been replaced by createFigure")
134 134
135 135 def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True):
136 136
137 137 """
138 138 Crea la figura de acuerdo al driver y parametros seleccionados seleccionados.
139 139 Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH
140 140 y self.HEIGHT y el numero de subplots (nrow, ncol)
141 141
142 142 Input:
143 143 id : Los parametros necesarios son
144 144 wintitle :
145 145
146 146 """
147 147
148 148 if widthplot == None:
149 149 widthplot = self.WIDTH
150 150
151 151 if heightplot == None:
152 152 heightplot = self.HEIGHT
153 153
154 154 self.id = id
155 155
156 156 self.wintitle = wintitle
157 157
158 158 self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot)
159 159
160 160 # if self.created:
161 161 # self.__driver.closeFigure(self.fig)
162 162
163 163 if not self.created:
164 164 self.fig = self.__driver.createFigure(id=self.id,
165 165 wintitle=self.wintitle,
166 166 width=self.widthscreen,
167 167 height=self.heightscreen,
168 168 show=show)
169 169 else:
170 170 self.__driver.clearFigure(self.fig)
171 171
172 172 self.axesObjList = []
173 173 self.counter_imagwr = 0
174 174
175 175 self.created = True
176 176
177 177 def setDriver(self, driver=mpldriver):
178 178
179 179 self.__driver = driver
180 180
181 181 def setTitle(self, title):
182 182
183 183 self.__driver.setTitle(self.fig, title)
184 184
185 185 def setWinTitle(self, title):
186 186
187 187 self.__driver.setWinTitle(self.fig, title=title)
188 188
189 189 def setTextFromAxes(self, text):
190 190
191 raise NotImplementedError, "This method has been replaced with Axes.setText"
191 raise NotImplementedError("This method has been replaced with Axes.setText")
192 192
193 193 def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan):
194 194
195 raise NotImplementedError, "This method has been replaced with Axes.addAxes"
195 raise NotImplementedError("This method has been replaced with Axes.addAxes")
196 196
197 197 def addAxes(self, *args):
198 198 """
199 199
200 200 Input:
201 201 *args : Los parametros necesarios son
202 202 nrow, ncol, xpos, ypos, colspan, rowspan
203 203 """
204 204
205 205 axesObj = Axes(self.fig, *args)
206 206 self.axesObjList.append(axesObj)
207 207
208 208 def saveFigure(self, figpath, figfile, *args):
209 209
210 210 filename = os.path.join(figpath, figfile)
211 211
212 212 fullpath = os.path.split(filename)[0]
213 213
214 214 if not os.path.exists(fullpath):
215 215 subpath = os.path.split(fullpath)[0]
216 216
217 217 if not os.path.exists(subpath):
218 218 os.mkdir(subpath)
219 219
220 220 os.mkdir(fullpath)
221 221
222 222 self.__driver.saveFigure(self.fig, filename, *args)
223 223
224 224 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
225 225
226 226 self.counter_imagwr += 1
227 227 if self.counter_imagwr < wr_period:
228 228 return
229 229
230 230 self.counter_imagwr = 0
231 231
232 232 if save:
233 233
234 234 if not figfile:
235 235
236 236 if not thisDatetime:
237 raise ValueError, "Saving figure: figfile or thisDatetime should be defined"
237 raise ValueError("Saving figure: figfile or thisDatetime should be defined")
238 238 return
239 239
240 240 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
241 241 figfile = self.getFilename(name = str_datetime)
242 242
243 243 if self.figfile == None:
244 244 self.figfile = figfile
245 245
246 246 if update_figfile:
247 247 self.figfile = figfile
248 248
249 249 # store png plot to local folder
250 250 self.saveFigure(figpath, self.figfile)
251 251
252 252
253 253 if not ftp:
254 254 return
255 255
256 256 if not thisDatetime:
257 257 return
258 258
259 259 # store png plot to FTP server according to RT-Web format
260 260 ftp_filename = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
261 261 # ftp_filename = os.path.join(figpath, name)
262 262 self.saveFigure(figpath, ftp_filename)
263 263
264 264 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
265 265 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
266 266 DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday
267 267 FTP_WEI = '%2.2d'%FTP_WEI
268 268 EXP_CODE = '%3.3d'%EXP_CODE
269 269 SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE
270 270 PLOT_CODE = '%2.2d'%PLOT_CODE
271 271 PLOT_POS = '%2.2d'%PLOT_POS
272 272 name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS
273 273 return name
274 274
275 275 def draw(self):
276 276
277 277 self.__driver.draw(self.fig)
278 278
279 279 def run(self):
280 280
281 281 raise NotImplementedError
282 282
283 283 def close(self, show=False):
284 284
285 285 self.__driver.closeFigure(show=show, fig=self.fig)
286 286
287 287 axesList = property(getAxesObjList)
288 288
289 289
290 290 class Axes:
291 291
292 292 __driver = mpldriver
293 293 fig = None
294 294 ax = None
295 295 plot = None
296 296 __missing = 1E30
297 297 __firsttime = None
298 298
299 299 __showprofile = False
300 300
301 301 xmin = None
302 302 xmax = None
303 303 ymin = None
304 304 ymax = None
305 305 zmin = None
306 306 zmax = None
307 307
308 308 x_buffer = None
309 309 z_buffer = None
310 310
311 311 decimationx = None
312 312 decimationy = None
313 313
314 314 __MAXNUMX = 200
315 315 __MAXNUMY = 400
316 316
317 317 __MAXNUMTIME = 500
318 318
319 319 def __init__(self, *args):
320 320
321 321 """
322 322
323 323 Input:
324 324 *args : Los parametros necesarios son
325 325 fig, nrow, ncol, xpos, ypos, colspan, rowspan
326 326 """
327 327
328 328 ax = self.__driver.createAxes(*args)
329 329 self.fig = args[0]
330 330 self.ax = ax
331 331 self.plot = None
332 332
333 333 self.__firsttime = True
334 334 self.idlineList = []
335 335
336 336 self.x_buffer = numpy.array([])
337 337 self.z_buffer = numpy.array([])
338 338
339 339 def setText(self, text):
340 340
341 341 self.__driver.setAxesText(self.ax, text)
342 342
343 343 def setXAxisAsTime(self):
344 344 pass
345 345
346 346 def pline(self, x, y,
347 347 xmin=None, xmax=None,
348 348 ymin=None, ymax=None,
349 349 xlabel='', ylabel='',
350 350 title='',
351 351 **kwargs):
352 352
353 353 """
354 354
355 355 Input:
356 356 x :
357 357 y :
358 358 xmin :
359 359 xmax :
360 360 ymin :
361 361 ymax :
362 362 xlabel :
363 363 ylabel :
364 364 title :
365 365 **kwargs : Los parametros aceptados son
366 366
367 367 ticksize
368 368 ytick_visible
369 369 """
370 370
371 371 if self.__firsttime:
372 372
373 373 if xmin == None: xmin = numpy.nanmin(x)
374 374 if xmax == None: xmax = numpy.nanmax(x)
375 375 if ymin == None: ymin = numpy.nanmin(y)
376 376 if ymax == None: ymax = numpy.nanmax(y)
377 377
378 378 self.plot = self.__driver.createPline(self.ax, x, y,
379 379 xmin, xmax,
380 380 ymin, ymax,
381 381 xlabel=xlabel,
382 382 ylabel=ylabel,
383 383 title=title,
384 384 **kwargs)
385 385
386 386 self.idlineList.append(0)
387 387 self.__firsttime = False
388 388 return
389 389
390 390 self.__driver.pline(self.plot, x, y, xlabel=xlabel,
391 391 ylabel=ylabel,
392 392 title=title)
393 393
394 394 # self.__driver.pause()
395 395
396 396 def addpline(self, x, y, idline, **kwargs):
397 397 lines = self.ax.lines
398 398
399 399 if idline in self.idlineList:
400 400 self.__driver.set_linedata(self.ax, x, y, idline)
401 401
402 402 if idline not in(self.idlineList):
403 403 self.__driver.addpline(self.ax, x, y, **kwargs)
404 404 self.idlineList.append(idline)
405 405
406 406 return
407 407
408 408 def pmultiline(self, x, y,
409 409 xmin=None, xmax=None,
410 410 ymin=None, ymax=None,
411 411 xlabel='', ylabel='',
412 412 title='',
413 413 **kwargs):
414 414
415 415 if self.__firsttime:
416 416
417 417 if xmin == None: xmin = numpy.nanmin(x)
418 418 if xmax == None: xmax = numpy.nanmax(x)
419 419 if ymin == None: ymin = numpy.nanmin(y)
420 420 if ymax == None: ymax = numpy.nanmax(y)
421 421
422 422 self.plot = self.__driver.createPmultiline(self.ax, x, y,
423 423 xmin, xmax,
424 424 ymin, ymax,
425 425 xlabel=xlabel,
426 426 ylabel=ylabel,
427 427 title=title,
428 428 **kwargs)
429 429 self.__firsttime = False
430 430 return
431 431
432 432 self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel,
433 433 ylabel=ylabel,
434 434 title=title)
435 435
436 436 # self.__driver.pause()
437 437
438 438 def pmultilineyaxis(self, x, y,
439 439 xmin=None, xmax=None,
440 440 ymin=None, ymax=None,
441 441 xlabel='', ylabel='',
442 442 title='',
443 443 **kwargs):
444 444
445 445 if self.__firsttime:
446 446
447 447 if xmin == None: xmin = numpy.nanmin(x)
448 448 if xmax == None: xmax = numpy.nanmax(x)
449 449 if ymin == None: ymin = numpy.nanmin(y)
450 450 if ymax == None: ymax = numpy.nanmax(y)
451 451
452 452 self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y,
453 453 xmin, xmax,
454 454 ymin, ymax,
455 455 xlabel=xlabel,
456 456 ylabel=ylabel,
457 457 title=title,
458 458 **kwargs)
459 459 if self.xmin == None: self.xmin = xmin
460 460 if self.xmax == None: self.xmax = xmax
461 461 if self.ymin == None: self.ymin = ymin
462 462 if self.ymax == None: self.ymax = ymax
463 463
464 464 self.__firsttime = False
465 465 return
466 466
467 467 self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel,
468 468 ylabel=ylabel,
469 469 title=title)
470 470
471 471 # self.__driver.pause()
472 472
473 473 def pcolor(self, x, y, z,
474 474 xmin=None, xmax=None,
475 475 ymin=None, ymax=None,
476 476 zmin=None, zmax=None,
477 477 xlabel='', ylabel='',
478 478 title='', colormap='jet',
479 479 **kwargs):
480 480
481 481 """
482 482 Input:
483 483 x :
484 484 y :
485 485 x :
486 486 xmin :
487 487 xmax :
488 488 ymin :
489 489 ymax :
490 490 zmin :
491 491 zmax :
492 492 xlabel :
493 493 ylabel :
494 494 title :
495 495 **kwargs : Los parametros aceptados son
496 496 ticksize=9,
497 497 cblabel=''
498 498 """
499 499
500 500 #Decimating data
501 501 xlen = len(x)
502 502 ylen = len(y)
503 503
504 504 decimationx = int(xlen/self.__MAXNUMX) + 1
505 505 decimationy = int(ylen/self.__MAXNUMY) + 1
506 506
507 507
508 508 x_buffer = x#[::decimationx]
509 509 y_buffer = y#[::decimationy]
510 510 z_buffer = z#[::decimationx, ::decimationy]
511 511 #===================================================
512 512
513 513 if self.__firsttime:
514 514
515 515 if xmin == None: xmin = numpy.nanmin(x)
516 516 if xmax == None: xmax = numpy.nanmax(x)
517 517 if ymin == None: ymin = numpy.nanmin(y)
518 518 if ymax == None: ymax = numpy.nanmax(y)
519 519 if zmin == None: zmin = numpy.nanmin(z)
520 520 if zmax == None: zmax = numpy.nanmax(z)
521 521
522 522
523 523 self.plot = self.__driver.createPcolor(self.ax, x_buffer,
524 524 y_buffer,
525 525 z_buffer,
526 526 xmin, xmax,
527 527 ymin, ymax,
528 528 zmin, zmax,
529 529 xlabel=xlabel,
530 530 ylabel=ylabel,
531 531 title=title,
532 532 colormap=colormap,
533 533 **kwargs)
534 534
535 535 if self.xmin == None: self.xmin = xmin
536 536 if self.xmax == None: self.xmax = xmax
537 537 if self.ymin == None: self.ymin = ymin
538 538 if self.ymax == None: self.ymax = ymax
539 539 if self.zmin == None: self.zmin = zmin
540 540 if self.zmax == None: self.zmax = zmax
541 541
542 542 self.__firsttime = False
543 543 return
544 544
545 545 self.__driver.pcolor(self.plot,
546 546 z_buffer,
547 547 xlabel=xlabel,
548 548 ylabel=ylabel,
549 549 title=title)
550 550
551 551 # self.__driver.pause()
552 552
553 553 def pcolorbuffer(self, x, y, z,
554 554 xmin=None, xmax=None,
555 555 ymin=None, ymax=None,
556 556 zmin=None, zmax=None,
557 557 xlabel='', ylabel='',
558 558 title='', rti = True, colormap='jet',
559 559 maxNumX = None, maxNumY = None,
560 560 **kwargs):
561 561
562 562 if maxNumX == None:
563 563 maxNumX = self.__MAXNUMTIME
564 564
565 565 if maxNumY == None:
566 566 maxNumY = self.__MAXNUMY
567 567
568 568 if self.__firsttime:
569 569 self.z_buffer = z
570 570 self.x_buffer = numpy.hstack((self.x_buffer, x))
571 571
572 572 if xmin == None: xmin = numpy.nanmin(x)
573 573 if xmax == None: xmax = numpy.nanmax(x)
574 574 if ymin == None: ymin = numpy.nanmin(y)
575 575 if ymax == None: ymax = numpy.nanmax(y)
576 576 if zmin == None: zmin = numpy.nanmin(z)
577 577 if zmax == None: zmax = numpy.nanmax(z)
578 578
579 579 self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z,
580 580 xmin, xmax,
581 581 ymin, ymax,
582 582 zmin, zmax,
583 583 xlabel=xlabel,
584 584 ylabel=ylabel,
585 585 title=title,
586 586 colormap=colormap,
587 587 **kwargs)
588 588
589 589 if self.xmin == None: self.xmin = xmin
590 590 if self.xmax == None: self.xmax = xmax
591 591 if self.ymin == None: self.ymin = ymin
592 592 if self.ymax == None: self.ymax = ymax
593 593 if self.zmin == None: self.zmin = zmin
594 594 if self.zmax == None: self.zmax = zmax
595 595
596 596 self.__firsttime = False
597 597 return
598 598
599 599 self.x_buffer = numpy.hstack((self.x_buffer[:-1], x[0], x[-1]))
600 600 self.z_buffer = numpy.hstack((self.z_buffer, z))
601 601 z_buffer = self.z_buffer.reshape(-1,len(y))
602 602
603 603 #Decimating data
604 604 xlen = len(self.x_buffer)
605 605 ylen = len(y)
606 606
607 607 decimationx = int(xlen/maxNumX) + 1
608 608 decimationy = int(ylen/maxNumY) + 1
609 609
610 610 x_buffer = self.x_buffer#[::decimationx]
611 611 y_buffer = y#[::decimationy]
612 612 z_buffer = z_buffer#[::decimationx, ::decimationy]
613 613 #===================================================
614 614
615 615 x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer)
616 616
617 617 self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax,
618 618 xlabel=xlabel,
619 619 ylabel=ylabel,
620 620 title=title,
621 621 colormap=colormap)
622 622
623 623 # self.__driver.pause()
624 624
625 625 def polar(self, x, y,
626 626 title='', xlabel='',ylabel='',**kwargs):
627 627
628 628 if self.__firsttime:
629 629 self.plot = self.__driver.createPolar(self.ax, x, y, title = title, xlabel = xlabel, ylabel = ylabel)
630 630 self.__firsttime = False
631 631 self.x_buffer = x
632 632 self.y_buffer = y
633 633 return
634 634
635 635 self.x_buffer = numpy.hstack((self.x_buffer,x))
636 636 self.y_buffer = numpy.hstack((self.y_buffer,y))
637 637 self.__driver.polar(self.plot, self.x_buffer, self.y_buffer, xlabel=xlabel,
638 638 ylabel=ylabel,
639 639 title=title)
640 640
641 641 # self.__driver.pause()
642 642
643 643 def __fillGaps(self, x_buffer, y_buffer, z_buffer):
644 644
645 645 if x_buffer.shape[0] < 2:
646 646 return x_buffer, y_buffer, z_buffer
647 647
648 648 deltas = x_buffer[1:] - x_buffer[0:-1]
649 649 x_median = numpy.median(deltas)
650 650
651 651 index = numpy.where(deltas > 5*x_median)
652 652
653 653 if len(index[0]) != 0:
654 654 z_buffer[index[0],::] = self.__missing
655 655 z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing)
656 656
657 return x_buffer, y_buffer, z_buffer
657 return x_buffer, y_buffer, z_buffer No newline at end of file
@@ -1,187 +1,187
1 1 import os
2 2 import datetime
3 3 import numpy
4 4 import copy
5 5 from schainpy.model import *
6 from figure import Figure, isRealtime
6 from .figure import Figure, isRealtime
7 7
8 8 class CorrelationPlot(Figure):
9 9 isConfig = None
10 10 __nsubplots = None
11 11
12 12 WIDTHPROF = None
13 13 HEIGHTPROF = None
14 14 PREFIX = 'corr'
15 15
16 16 def __init__(self, **kwargs):
17 17 Figure.__init__(self, **kwargs)
18 18 self.isConfig = False
19 19 self.__nsubplots = 1
20 20
21 21 self.WIDTH = 280
22 22 self.HEIGHT = 250
23 23 self.WIDTHPROF = 120
24 24 self.HEIGHTPROF = 0
25 25 self.counter_imagwr = 0
26 26
27 27 self.PLOT_CODE = 1
28 28 self.FTP_WEI = None
29 29 self.EXP_CODE = None
30 30 self.SUB_EXP_CODE = None
31 31 self.PLOT_POS = None
32 32
33 33 def getSubplots(self):
34 34
35 35 ncol = int(numpy.sqrt(self.nplots)+0.9)
36 36 nrow = int(self.nplots*1./ncol + 0.9)
37 37
38 38 return nrow, ncol
39 39
40 40 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
41 41
42 42 showprofile = False
43 43 self.__showprofile = showprofile
44 44 self.nplots = nplots
45 45
46 46 ncolspan = 1
47 47 colspan = 1
48 48 if showprofile:
49 49 ncolspan = 3
50 50 colspan = 2
51 51 self.__nsubplots = 2
52 52
53 53 self.createFigure(id = id,
54 54 wintitle = wintitle,
55 55 widthplot = self.WIDTH + self.WIDTHPROF,
56 56 heightplot = self.HEIGHT + self.HEIGHTPROF,
57 57 show=show)
58 58
59 59 nrow, ncol = self.getSubplots()
60 60
61 61 counter = 0
62 62 for y in range(nrow):
63 63 for x in range(ncol):
64 64
65 65 if counter >= self.nplots:
66 66 break
67 67
68 68 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
69 69
70 70 if showprofile:
71 71 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
72 72
73 73 counter += 1
74 74
75 75 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
76 76 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
77 77 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
78 78 server=None, folder=None, username=None, password=None,
79 79 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
80 80
81 81 """
82 82
83 83 Input:
84 84 dataOut :
85 85 id :
86 86 wintitle :
87 87 channelList :
88 88 showProfile :
89 89 xmin : None,
90 90 xmax : None,
91 91 ymin : None,
92 92 ymax : None,
93 93 zmin : None,
94 94 zmax : None
95 95 """
96 96
97 97 if dataOut.flagNoData:
98 98 return None
99 99
100 100 if realtime:
101 101 if not(isRealtime(utcdatatime = dataOut.utctime)):
102 print 'Skipping this plot function'
102 print('Skipping this plot function')
103 103 return
104 104
105 105 if channelList == None:
106 106 channelIndexList = dataOut.channelIndexList
107 107 else:
108 108 channelIndexList = []
109 109 for channel in channelList:
110 110 if channel not in dataOut.channelList:
111 raise ValueError, "Channel %d is not in dataOut.channelList"
111 raise ValueError("Channel %d is not in dataOut.channelList")
112 112 channelIndexList.append(dataOut.channelList.index(channel))
113 113
114 114 factor = dataOut.normFactor
115 115 lenfactor = factor.shape[1]
116 116 x = dataOut.getLagTRange(1)
117 117 y = dataOut.getHeiRange()
118 118
119 119 z = copy.copy(dataOut.data_corr[:,:,0,:])
120 120 for i in range(dataOut.data_corr.shape[0]):
121 121 z[i,:,:] = z[i,:,:]/factor[i,:]
122 122 zdB = numpy.abs(z)
123 123
124 124 avg = numpy.average(z, axis=1)
125 125 # avg = numpy.nanmean(z, axis=1)
126 126 # noise = dataOut.noise/factor
127 127
128 128 #thisDatetime = dataOut.datatime
129 129 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
130 130 title = wintitle + " Correlation"
131 131 xlabel = "Lag T (s)"
132 132 ylabel = "Range (Km)"
133 133
134 134 if not self.isConfig:
135 135
136 136 nplots = dataOut.data_corr.shape[0]
137 137
138 138 self.setup(id=id,
139 139 nplots=nplots,
140 140 wintitle=wintitle,
141 141 showprofile=showprofile,
142 142 show=show)
143 143
144 144 if xmin == None: xmin = numpy.nanmin(x)
145 145 if xmax == None: xmax = numpy.nanmax(x)
146 146 if ymin == None: ymin = numpy.nanmin(y)
147 147 if ymax == None: ymax = numpy.nanmax(y)
148 148 if zmin == None: zmin = 0
149 149 if zmax == None: zmax = 1
150 150
151 151 self.FTP_WEI = ftp_wei
152 152 self.EXP_CODE = exp_code
153 153 self.SUB_EXP_CODE = sub_exp_code
154 154 self.PLOT_POS = plot_pos
155 155
156 156 self.isConfig = True
157 157
158 158 self.setWinTitle(title)
159 159
160 160 for i in range(self.nplots):
161 161 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
162 162 title = "Channel %d and %d: : %s" %(dataOut.pairsList[i][0],dataOut.pairsList[i][1] , str_datetime)
163 163 axes = self.axesList[i*self.__nsubplots]
164 164 axes.pcolor(x, y, zdB[i,:,:],
165 165 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
166 166 xlabel=xlabel, ylabel=ylabel, title=title,
167 167 ticksize=9, cblabel='')
168 168
169 169 # if self.__showprofile:
170 170 # axes = self.axesList[i*self.__nsubplots +1]
171 171 # axes.pline(avgdB[i], y,
172 172 # xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
173 173 # xlabel='dB', ylabel='', title='',
174 174 # ytick_visible=False,
175 175 # grid='x')
176 176 #
177 177 # noiseline = numpy.repeat(noisedB[i], len(y))
178 178 # axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
179 179
180 180 self.draw()
181 181
182 182 self.save(figpath=figpath,
183 183 figfile=figfile,
184 184 save=save,
185 185 ftp=ftp,
186 186 wr_period=wr_period,
187 thisDatetime=thisDatetime)
187 thisDatetime=thisDatetime) No newline at end of file
@@ -1,1148 +1,1148
1 1
2 2 import os
3 3 import time
4 4 import glob
5 5 import datetime
6 6 from multiprocessing import Process
7 7
8 8 import zmq
9 9 import numpy
10 10 import matplotlib
11 11 import matplotlib.pyplot as plt
12 12 from matplotlib.patches import Polygon
13 13 from mpl_toolkits.axes_grid1 import make_axes_locatable
14 14 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
15 15
16 16 from schainpy.model.proc.jroproc_base import Operation
17 17 from schainpy.utils import log
18 18
19 19 jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90]
20 20 blu_values = matplotlib.pyplot.get_cmap(
21 21 'seismic_r', 20)(numpy.arange(20))[10:15]
22 22 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
23 23 'jro', numpy.vstack((blu_values, jet_values)))
24 24 matplotlib.pyplot.register_cmap(cmap=ncmap)
25 25
26 26 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis', 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')]
27 27
28 28 EARTH_RADIUS = 6.3710e3
29 29
30 30 def ll2xy(lat1, lon1, lat2, lon2):
31 31
32 32 p = 0.017453292519943295
33 33 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
34 34 r = 12742 * numpy.arcsin(numpy.sqrt(a))
35 35 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)*numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
36 36 theta = -theta + numpy.pi/2
37 37 return r*numpy.cos(theta), r*numpy.sin(theta)
38 38
39 39 def km2deg(km):
40 40 '''
41 41 Convert distance in km to degrees
42 42 '''
43 43
44 44 return numpy.rad2deg(km/EARTH_RADIUS)
45 45
46 46 def figpause(interval):
47 47 backend = plt.rcParams['backend']
48 48 if backend in matplotlib.rcsetup.interactive_bk:
49 49 figManager = matplotlib._pylab_helpers.Gcf.get_active()
50 50 if figManager is not None:
51 51 canvas = figManager.canvas
52 52 if canvas.figure.stale:
53 53 canvas.draw()
54 54 try:
55 55 canvas.start_event_loop(interval)
56 56 except:
57 57 pass
58 58 return
59 59
60 60 def popup(message):
61 61 '''
62 62 '''
63 63
64 64 fig = plt.figure(figsize=(12, 8), facecolor='r')
65 65 text = '\n'.join([s.strip() for s in message.split(':')])
66 66 fig.text(0.01, 0.5, text, ha='left', va='center', size='20', weight='heavy', color='w')
67 67 fig.show()
68 68 figpause(1000)
69 69
70 70
71 71 class PlotData(Operation, Process):
72 72 '''
73 73 Base class for Schain plotting operations
74 74 '''
75 75
76 76 CODE = 'Figure'
77 77 colormap = 'jro'
78 78 bgcolor = 'white'
79 79 CONFLATE = False
80 80 __missing = 1E30
81 81
82 82 __attrs__ = ['show', 'save', 'xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax',
83 83 'zlimits', 'xlabel', 'ylabel', 'xaxis','cb_label', 'title',
84 84 'colorbar', 'bgcolor', 'width', 'height', 'localtime', 'oneFigure',
85 85 'showprofile', 'decimation', 'ftp']
86 86
87 87 def __init__(self, **kwargs):
88 88
89 89 Operation.__init__(self, plot=True, **kwargs)
90 90 Process.__init__(self)
91 91
92 92 self.kwargs['code'] = self.CODE
93 93 self.mp = False
94 94 self.data = None
95 95 self.isConfig = False
96 96 self.figures = []
97 97 self.axes = []
98 98 self.cb_axes = []
99 99 self.localtime = kwargs.pop('localtime', True)
100 100 self.show = kwargs.get('show', True)
101 101 self.save = kwargs.get('save', False)
102 102 self.ftp = kwargs.get('ftp', False)
103 103 self.colormap = kwargs.get('colormap', self.colormap)
104 104 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
105 105 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
106 106 self.colormaps = kwargs.get('colormaps', None)
107 107 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
108 108 self.showprofile = kwargs.get('showprofile', False)
109 109 self.title = kwargs.get('wintitle', self.CODE.upper())
110 110 self.cb_label = kwargs.get('cb_label', None)
111 111 self.cb_labels = kwargs.get('cb_labels', None)
112 112 self.labels = kwargs.get('labels', None)
113 113 self.xaxis = kwargs.get('xaxis', 'frequency')
114 114 self.zmin = kwargs.get('zmin', None)
115 115 self.zmax = kwargs.get('zmax', None)
116 116 self.zlimits = kwargs.get('zlimits', None)
117 117 self.xmin = kwargs.get('xmin', None)
118 118 self.xmax = kwargs.get('xmax', None)
119 119 self.xrange = kwargs.get('xrange', 24)
120 120 self.xscale = kwargs.get('xscale', None)
121 121 self.ymin = kwargs.get('ymin', None)
122 122 self.ymax = kwargs.get('ymax', None)
123 123 self.yscale = kwargs.get('yscale', None)
124 124 self.xlabel = kwargs.get('xlabel', None)
125 125 self.decimation = kwargs.get('decimation', None)
126 126 self.showSNR = kwargs.get('showSNR', False)
127 127 self.oneFigure = kwargs.get('oneFigure', True)
128 128 self.width = kwargs.get('width', None)
129 129 self.height = kwargs.get('height', None)
130 130 self.colorbar = kwargs.get('colorbar', True)
131 131 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
132 132 self.channels = kwargs.get('channels', None)
133 133 self.titles = kwargs.get('titles', [])
134 134 self.polar = False
135 135 self.grid = kwargs.get('grid', False)
136 136
137 137 def __fmtTime(self, x, pos):
138 138 '''
139 139 '''
140 140
141 141 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
142 142
143 143 def __setup(self):
144 144 '''
145 145 Common setup for all figures, here figures and axes are created
146 146 '''
147 147
148 148 if self.CODE not in self.data:
149 149 raise ValueError(log.error('Missing data for {}'.format(self.CODE),
150 150 self.name))
151 151
152 152 self.setup()
153 153
154 154 self.time_label = 'LT' if self.localtime else 'UTC'
155 155 if self.data.localtime:
156 156 self.getDateTime = datetime.datetime.fromtimestamp
157 157 else:
158 158 self.getDateTime = datetime.datetime.utcfromtimestamp
159 159
160 160 if self.width is None:
161 161 self.width = 8
162 162
163 163 self.figures = []
164 164 self.axes = []
165 165 self.cb_axes = []
166 166 self.pf_axes = []
167 167 self.cmaps = []
168 168
169 169 size = '15%' if self.ncols == 1 else '30%'
170 170 pad = '4%' if self.ncols == 1 else '8%'
171 171
172 172 if self.oneFigure:
173 173 if self.height is None:
174 174 self.height = 1.4 * self.nrows + 1
175 175 fig = plt.figure(figsize=(self.width, self.height),
176 176 edgecolor='k',
177 177 facecolor='w')
178 178 self.figures.append(fig)
179 179 for n in range(self.nplots):
180 180 ax = fig.add_subplot(self.nrows, self.ncols,
181 181 n + 1, polar=self.polar)
182 182 ax.tick_params(labelsize=8)
183 183 ax.firsttime = True
184 184 ax.index = 0
185 185 ax.press = None
186 186 self.axes.append(ax)
187 187 if self.showprofile:
188 188 cax = self.__add_axes(ax, size=size, pad=pad)
189 189 cax.tick_params(labelsize=8)
190 190 self.pf_axes.append(cax)
191 191 else:
192 192 if self.height is None:
193 193 self.height = 3
194 194 for n in range(self.nplots):
195 195 fig = plt.figure(figsize=(self.width, self.height),
196 196 edgecolor='k',
197 197 facecolor='w')
198 198 ax = fig.add_subplot(1, 1, 1, polar=self.polar)
199 199 ax.tick_params(labelsize=8)
200 200 ax.firsttime = True
201 201 ax.index = 0
202 202 ax.press = None
203 203 self.figures.append(fig)
204 204 self.axes.append(ax)
205 205 if self.showprofile:
206 206 cax = self.__add_axes(ax, size=size, pad=pad)
207 207 cax.tick_params(labelsize=8)
208 208 self.pf_axes.append(cax)
209 209
210 210 for n in range(self.nrows):
211 211 if self.colormaps is not None:
212 212 cmap = plt.get_cmap(self.colormaps[n])
213 213 else:
214 214 cmap = plt.get_cmap(self.colormap)
215 215 cmap.set_bad(self.bgcolor, 1.)
216 216 self.cmaps.append(cmap)
217 217
218 218 for fig in self.figures:
219 219 fig.canvas.mpl_connect('key_press_event', self.OnKeyPress)
220 220 fig.canvas.mpl_connect('scroll_event', self.OnBtnScroll)
221 221 fig.canvas.mpl_connect('button_press_event', self.onBtnPress)
222 222 fig.canvas.mpl_connect('motion_notify_event', self.onMotion)
223 223 fig.canvas.mpl_connect('button_release_event', self.onBtnRelease)
224 224 if self.show:
225 225 fig.show()
226 226
227 227 def OnKeyPress(self, event):
228 228 '''
229 229 Event for pressing keys (up, down) change colormap
230 230 '''
231 231 ax = event.inaxes
232 232 if ax in self.axes:
233 233 if event.key == 'down':
234 234 ax.index += 1
235 235 elif event.key == 'up':
236 236 ax.index -= 1
237 237 if ax.index < 0:
238 238 ax.index = len(CMAPS) - 1
239 239 elif ax.index == len(CMAPS):
240 240 ax.index = 0
241 241 cmap = CMAPS[ax.index]
242 242 ax.cbar.set_cmap(cmap)
243 243 ax.cbar.draw_all()
244 244 ax.plt.set_cmap(cmap)
245 245 ax.cbar.patch.figure.canvas.draw()
246 246 self.colormap = cmap.name
247 247
248 248 def OnBtnScroll(self, event):
249 249 '''
250 250 Event for scrolling, scale figure
251 251 '''
252 252 cb_ax = event.inaxes
253 253 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
254 254 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
255 255 pt = ax.cbar.ax.bbox.get_points()[:, 1]
256 256 nrm = ax.cbar.norm
257 257 vmin, vmax, p0, p1, pS = (
258 258 nrm.vmin, nrm.vmax, pt[0], pt[1], event.y)
259 259 scale = 2 if event.step == 1 else 0.5
260 260 point = vmin + (vmax - vmin) / (p1 - p0) * (pS - p0)
261 261 ax.cbar.norm.vmin = point - scale * (point - vmin)
262 262 ax.cbar.norm.vmax = point - scale * (point - vmax)
263 263 ax.plt.set_norm(ax.cbar.norm)
264 264 ax.cbar.draw_all()
265 265 ax.cbar.patch.figure.canvas.draw()
266 266
267 267 def onBtnPress(self, event):
268 268 '''
269 269 Event for mouse button press
270 270 '''
271 271 cb_ax = event.inaxes
272 272 if cb_ax is None:
273 273 return
274 274
275 275 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
276 276 cb_ax.press = event.x, event.y
277 277 else:
278 278 cb_ax.press = None
279 279
280 280 def onMotion(self, event):
281 281 '''
282 282 Event for move inside colorbar
283 283 '''
284 284 cb_ax = event.inaxes
285 285 if cb_ax is None:
286 286 return
287 287 if cb_ax not in [ax.cbar.ax for ax in self.axes if ax.cbar]:
288 288 return
289 289 if cb_ax.press is None:
290 290 return
291 291
292 292 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
293 293 xprev, yprev = cb_ax.press
294 294 dx = event.x - xprev
295 295 dy = event.y - yprev
296 296 cb_ax.press = event.x, event.y
297 297 scale = ax.cbar.norm.vmax - ax.cbar.norm.vmin
298 298 perc = 0.03
299 299
300 300 if event.button == 1:
301 301 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
302 302 ax.cbar.norm.vmax -= (perc * scale) * numpy.sign(dy)
303 303 elif event.button == 3:
304 304 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
305 305 ax.cbar.norm.vmax += (perc * scale) * numpy.sign(dy)
306 306
307 307 ax.cbar.draw_all()
308 308 ax.plt.set_norm(ax.cbar.norm)
309 309 ax.cbar.patch.figure.canvas.draw()
310 310
311 311 def onBtnRelease(self, event):
312 312 '''
313 313 Event for mouse button release
314 314 '''
315 315 cb_ax = event.inaxes
316 316 if cb_ax is not None:
317 317 cb_ax.press = None
318 318
319 319 def __add_axes(self, ax, size='30%', pad='8%'):
320 320 '''
321 321 Add new axes to the given figure
322 322 '''
323 323 divider = make_axes_locatable(ax)
324 324 nax = divider.new_horizontal(size=size, pad=pad)
325 325 ax.figure.add_axes(nax)
326 326 return nax
327 327
328 328 self.setup()
329 329
330 330 def setup(self):
331 331 '''
332 332 This method should be implemented in the child class, the following
333 333 attributes should be set:
334 334
335 335 self.nrows: number of rows
336 336 self.ncols: number of cols
337 337 self.nplots: number of plots (channels or pairs)
338 338 self.ylabel: label for Y axes
339 339 self.titles: list of axes title
340 340
341 341 '''
342 raise(NotImplementedError, 'Implement this method in child class')
342 raise NotImplementedError
343 343
344 344 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
345 345 '''
346 346 Create a masked array for missing data
347 347 '''
348 348 if x_buffer.shape[0] < 2:
349 349 return x_buffer, y_buffer, z_buffer
350 350
351 351 deltas = x_buffer[1:] - x_buffer[0:-1]
352 352 x_median = numpy.median(deltas)
353 353
354 354 index = numpy.where(deltas > 5 * x_median)
355 355
356 356 if len(index[0]) != 0:
357 357 z_buffer[::, index[0], ::] = self.__missing
358 358 z_buffer = numpy.ma.masked_inside(z_buffer,
359 359 0.99 * self.__missing,
360 360 1.01 * self.__missing)
361 361
362 362 return x_buffer, y_buffer, z_buffer
363 363
364 364 def decimate(self):
365 365
366 366 # dx = int(len(self.x)/self.__MAXNUMX) + 1
367 367 dy = int(len(self.y) / self.decimation) + 1
368 368
369 369 # x = self.x[::dx]
370 370 x = self.x
371 371 y = self.y[::dy]
372 372 z = self.z[::, ::, ::dy]
373 373
374 374 return x, y, z
375 375
376 376 def format(self):
377 377 '''
378 378 Set min and max values, labels, ticks and titles
379 379 '''
380 380
381 381 if self.xmin is None:
382 382 xmin = self.min_time
383 383 else:
384 384 if self.xaxis is 'time':
385 385 dt = self.getDateTime(self.min_time)
386 386 xmin = (dt.replace(hour=int(self.xmin), minute=0, second=0) -
387 387 datetime.datetime(1970, 1, 1)).total_seconds()
388 388 if self.data.localtime:
389 389 xmin += time.timezone
390 390 else:
391 391 xmin = self.xmin
392 392
393 393 if self.xmax is None:
394 394 xmax = xmin + self.xrange * 60 * 60
395 395 else:
396 396 if self.xaxis is 'time':
397 397 dt = self.getDateTime(self.max_time)
398 398 xmax = (dt.replace(hour=int(self.xmax), minute=59, second=59) -
399 399 datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=1)).total_seconds()
400 400 if self.data.localtime:
401 401 xmax += time.timezone
402 402 else:
403 403 xmax = self.xmax
404 404
405 405 ymin = self.ymin if self.ymin else numpy.nanmin(self.y)
406 406 ymax = self.ymax if self.ymax else numpy.nanmax(self.y)
407 407
408 408 Y = numpy.array([1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000])
409 409 i = 1 if numpy.where(abs(ymax-ymin) <= Y)[0][0] < 0 else numpy.where(abs(ymax-ymin) <= Y)[0][0]
410 410 ystep = Y[i] / 10.
411 411
412 412 for n, ax in enumerate(self.axes):
413 413 if ax.firsttime:
414 414 ax.set_facecolor(self.bgcolor)
415 415 ax.yaxis.set_major_locator(MultipleLocator(ystep))
416 416 ax.xaxis.set_major_locator(MultipleLocator(ystep))
417 417 if self.xscale:
418 418 ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.xscale)))
419 419 if self.xscale:
420 420 ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.yscale)))
421 421 if self.xaxis is 'time':
422 422 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
423 423 ax.xaxis.set_major_locator(LinearLocator(9))
424 424 if self.xlabel is not None:
425 425 ax.set_xlabel(self.xlabel)
426 426 ax.set_ylabel(self.ylabel)
427 427 ax.firsttime = False
428 428 if self.showprofile:
429 429 self.pf_axes[n].set_ylim(ymin, ymax)
430 430 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
431 431 self.pf_axes[n].set_xlabel('dB')
432 432 self.pf_axes[n].grid(b=True, axis='x')
433 433 [tick.set_visible(False)
434 434 for tick in self.pf_axes[n].get_yticklabels()]
435 435 if self.colorbar:
436 436 ax.cbar = plt.colorbar(
437 437 ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10)
438 438 ax.cbar.ax.tick_params(labelsize=8)
439 439 ax.cbar.ax.press = None
440 440 if self.cb_label:
441 441 ax.cbar.set_label(self.cb_label, size=8)
442 442 elif self.cb_labels:
443 443 ax.cbar.set_label(self.cb_labels[n], size=8)
444 444 else:
445 445 ax.cbar = None
446 446 if self.grid:
447 447 ax.grid(True)
448 448
449 449 if not self.polar:
450 450 ax.set_xlim(xmin, xmax)
451 451 ax.set_ylim(ymin, ymax)
452 452 ax.set_title('{} {} {}'.format(
453 453 self.titles[n],
454 454 self.getDateTime(self.max_time).strftime('%Y-%m-%dT%H:%M:%S'),
455 455 self.time_label),
456 456 size=8)
457 457 else:
458 458 ax.set_title('{}'.format(self.titles[n]), size=8)
459 459 ax.set_ylim(0, 90)
460 460 ax.set_yticks(numpy.arange(0, 90, 20))
461 461 ax.yaxis.labelpad = 40
462 462
463 463 def __plot(self):
464 464 '''
465 465 '''
466 466 log.log('Plotting', self.name)
467 467
468 468 try:
469 469 self.plot()
470 470 self.format()
471 471 except Exception as e:
472 472 log.warning('{} Plot could not be updated... check data'.format(self.CODE), self.name)
473 473 log.error(str(e), '')
474 474 return
475 475
476 476 for n, fig in enumerate(self.figures):
477 477 if self.nrows == 0 or self.nplots == 0:
478 478 log.warning('No data', self.name)
479 479 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
480 480 fig.canvas.manager.set_window_title(self.CODE)
481 481 continue
482 482
483 483 fig.tight_layout()
484 484 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
485 485 self.getDateTime(self.max_time).strftime('%Y/%m/%d')))
486 486 fig.canvas.draw()
487 487
488 488 if self.save and (self.data.ended or not self.data.buffering):
489 489
490 490 if self.save_labels:
491 491 labels = self.save_labels
492 492 else:
493 labels = range(self.nrows)
493 labels = list(range(self.nrows))
494 494
495 495 if self.oneFigure:
496 496 label = ''
497 497 else:
498 498 label = '-{}'.format(labels[n])
499 499 figname = os.path.join(
500 500 self.save,
501 501 self.CODE,
502 502 '{}{}_{}.png'.format(
503 503 self.CODE,
504 504 label,
505 505 self.getDateTime(self.saveTime).strftime(
506 506 '%Y%m%d_%H%M%S'),
507 507 )
508 508 )
509 509 log.log('Saving figure: {}'.format(figname), self.name)
510 510 if not os.path.isdir(os.path.dirname(figname)):
511 511 os.makedirs(os.path.dirname(figname))
512 512 fig.savefig(figname)
513 513
514 514 def plot(self):
515 515 '''
516 516 '''
517 raise(NotImplementedError, 'Implement this method in child class')
517 raise NotImplementedError
518 518
519 519 def run(self):
520 520
521 521 log.log('Starting', self.name)
522 522
523 523 context = zmq.Context()
524 524 receiver = context.socket(zmq.SUB)
525 525 receiver.setsockopt(zmq.SUBSCRIBE, '')
526 526 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
527 527
528 528 if 'server' in self.kwargs['parent']:
529 529 receiver.connect(
530 530 'ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
531 531 else:
532 532 receiver.connect("ipc:///tmp/zmq.plots")
533 533
534 534 while True:
535 535 try:
536 536 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
537 537 if self.data.localtime and self.localtime:
538 538 self.times = self.data.times
539 539 elif self.data.localtime and not self.localtime:
540 540 self.times = self.data.times + time.timezone
541 541 elif not self.data.localtime and self.localtime:
542 542 self.times = self.data.times - time.timezone
543 543 else:
544 544 self.times = self.data.times
545 545
546 546 self.min_time = self.times[0]
547 547 self.max_time = self.times[-1]
548 548
549 549 if self.isConfig is False:
550 550 self.__setup()
551 551 self.isConfig = True
552 552
553 553 self.__plot()
554 554
555 555 except zmq.Again as e:
556 556 if self.data and self.data.ended:
557 557 break
558 558 log.log('Waiting for data...')
559 559 if self.data:
560 560 figpause(self.data.throttle)
561 561 else:
562 562 time.sleep(2)
563 563
564 564 def close(self):
565 565 if self.data:
566 566 self.__plot()
567 567
568 568
569 569 class PlotSpectraData(PlotData):
570 570 '''
571 571 Plot for Spectra data
572 572 '''
573 573
574 574 CODE = 'spc'
575 575 colormap = 'jro'
576 576
577 577 def setup(self):
578 578 self.nplots = len(self.data.channels)
579 579 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
580 580 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
581 581 self.width = 3.4 * self.ncols
582 582 self.height = 3 * self.nrows
583 583 self.cb_label = 'dB'
584 584 if self.showprofile:
585 585 self.width += 0.8 * self.ncols
586 586
587 587 self.ylabel = 'Range [km]'
588 588
589 589 def plot(self):
590 590 if self.xaxis == "frequency":
591 591 x = self.data.xrange[0]
592 592 self.xlabel = "Frequency (kHz)"
593 593 elif self.xaxis == "time":
594 594 x = self.data.xrange[1]
595 595 self.xlabel = "Time (ms)"
596 596 else:
597 597 x = self.data.xrange[2]
598 598 self.xlabel = "Velocity (m/s)"
599 599
600 600 if self.CODE == 'spc_mean':
601 601 x = self.data.xrange[2]
602 602 self.xlabel = "Velocity (m/s)"
603 603
604 604 self.titles = []
605 605
606 606 y = self.data.heights
607 607 self.y = y
608 608 z = self.data['spc']
609 609
610 610 for n, ax in enumerate(self.axes):
611 611 noise = self.data['noise'][n][-1]
612 612 if self.CODE == 'spc_mean':
613 613 mean = self.data['mean'][n][-1]
614 614 if ax.firsttime:
615 615 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
616 616 self.xmin = self.xmin if self.xmin else -self.xmax
617 617 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
618 618 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
619 619 ax.plt = ax.pcolormesh(x, y, z[n].T,
620 620 vmin=self.zmin,
621 621 vmax=self.zmax,
622 622 cmap=plt.get_cmap(self.colormap)
623 623 )
624 624
625 625 if self.showprofile:
626 626 ax.plt_profile = self.pf_axes[n].plot(
627 627 self.data['rti'][n][-1], y)[0]
628 628 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
629 629 color="k", linestyle="dashed", lw=1)[0]
630 630 if self.CODE == 'spc_mean':
631 631 ax.plt_mean = ax.plot(mean, y, color='k')[0]
632 632 else:
633 633 ax.plt.set_array(z[n].T.ravel())
634 634 if self.showprofile:
635 635 ax.plt_profile.set_data(self.data['rti'][n][-1], y)
636 636 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
637 637 if self.CODE == 'spc_mean':
638 638 ax.plt_mean.set_data(mean, y)
639 639
640 640 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
641 641 self.saveTime = self.max_time
642 642
643 643
644 644 class PlotCrossSpectraData(PlotData):
645 645
646 646 CODE = 'cspc'
647 647 zmin_coh = None
648 648 zmax_coh = None
649 649 zmin_phase = None
650 650 zmax_phase = None
651 651
652 652 def setup(self):
653 653
654 654 self.ncols = 4
655 655 self.nrows = len(self.data.pairs)
656 656 self.nplots = self.nrows * 4
657 657 self.width = 3.4 * self.ncols
658 658 self.height = 3 * self.nrows
659 659 self.ylabel = 'Range [km]'
660 660 self.showprofile = False
661 661
662 662 def plot(self):
663 663
664 664 if self.xaxis == "frequency":
665 665 x = self.data.xrange[0]
666 666 self.xlabel = "Frequency (kHz)"
667 667 elif self.xaxis == "time":
668 668 x = self.data.xrange[1]
669 669 self.xlabel = "Time (ms)"
670 670 else:
671 671 x = self.data.xrange[2]
672 672 self.xlabel = "Velocity (m/s)"
673 673
674 674 self.titles = []
675 675
676 676 y = self.data.heights
677 677 self.y = y
678 678 spc = self.data['spc']
679 679 cspc = self.data['cspc']
680 680
681 681 for n in range(self.nrows):
682 682 noise = self.data['noise'][n][-1]
683 683 pair = self.data.pairs[n]
684 684 ax = self.axes[4 * n]
685 685 ax3 = self.axes[4 * n + 3]
686 686 if ax.firsttime:
687 687 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
688 688 self.xmin = self.xmin if self.xmin else -self.xmax
689 689 self.zmin = self.zmin if self.zmin else numpy.nanmin(spc)
690 690 self.zmax = self.zmax if self.zmax else numpy.nanmax(spc)
691 691 ax.plt = ax.pcolormesh(x, y, spc[pair[0]].T,
692 692 vmin=self.zmin,
693 693 vmax=self.zmax,
694 694 cmap=plt.get_cmap(self.colormap)
695 695 )
696 696 else:
697 697 ax.plt.set_array(spc[pair[0]].T.ravel())
698 698 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
699 699
700 700 ax = self.axes[4 * n + 1]
701 701 if ax.firsttime:
702 702 ax.plt = ax.pcolormesh(x, y, spc[pair[1]].T,
703 703 vmin=self.zmin,
704 704 vmax=self.zmax,
705 705 cmap=plt.get_cmap(self.colormap)
706 706 )
707 707 else:
708 708 ax.plt.set_array(spc[pair[1]].T.ravel())
709 709 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
710 710
711 711 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
712 712 coh = numpy.abs(out)
713 713 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
714 714
715 715 ax = self.axes[4 * n + 2]
716 716 if ax.firsttime:
717 717 ax.plt = ax.pcolormesh(x, y, coh.T,
718 718 vmin=0,
719 719 vmax=1,
720 720 cmap=plt.get_cmap(self.colormap_coh)
721 721 )
722 722 else:
723 723 ax.plt.set_array(coh.T.ravel())
724 724 self.titles.append(
725 725 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
726 726
727 727 ax = self.axes[4 * n + 3]
728 728 if ax.firsttime:
729 729 ax.plt = ax.pcolormesh(x, y, phase.T,
730 730 vmin=-180,
731 731 vmax=180,
732 732 cmap=plt.get_cmap(self.colormap_phase)
733 733 )
734 734 else:
735 735 ax.plt.set_array(phase.T.ravel())
736 736 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
737 737
738 738 self.saveTime = self.max_time
739 739
740 740
741 741 class PlotSpectraMeanData(PlotSpectraData):
742 742 '''
743 743 Plot for Spectra and Mean
744 744 '''
745 745 CODE = 'spc_mean'
746 746 colormap = 'jro'
747 747
748 748
749 749 class PlotRTIData(PlotData):
750 750 '''
751 751 Plot for RTI data
752 752 '''
753 753
754 754 CODE = 'rti'
755 755 colormap = 'jro'
756 756
757 757 def setup(self):
758 758 self.xaxis = 'time'
759 759 self.ncols = 1
760 760 self.nrows = len(self.data.channels)
761 761 self.nplots = len(self.data.channels)
762 762 self.ylabel = 'Range [km]'
763 763 self.cb_label = 'dB'
764 764 self.titles = ['{} Channel {}'.format(
765 765 self.CODE.upper(), x) for x in range(self.nrows)]
766 766
767 767 def plot(self):
768 768 self.x = self.times
769 769 self.y = self.data.heights
770 770 self.z = self.data[self.CODE]
771 771 self.z = numpy.ma.masked_invalid(self.z)
772 772
773 773 if self.decimation is None:
774 774 x, y, z = self.fill_gaps(self.x, self.y, self.z)
775 775 else:
776 776 x, y, z = self.fill_gaps(*self.decimate())
777 777
778 778 for n, ax in enumerate(self.axes):
779 779 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
780 780 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
781 781 if ax.firsttime:
782 782 ax.plt = ax.pcolormesh(x, y, z[n].T,
783 783 vmin=self.zmin,
784 784 vmax=self.zmax,
785 785 cmap=plt.get_cmap(self.colormap)
786 786 )
787 787 if self.showprofile:
788 788 ax.plot_profile = self.pf_axes[n].plot(
789 789 self.data['rti'][n][-1], self.y)[0]
790 790 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y,
791 791 color="k", linestyle="dashed", lw=1)[0]
792 792 else:
793 793 ax.collections.remove(ax.collections[0])
794 794 ax.plt = ax.pcolormesh(x, y, z[n].T,
795 795 vmin=self.zmin,
796 796 vmax=self.zmax,
797 797 cmap=plt.get_cmap(self.colormap)
798 798 )
799 799 if self.showprofile:
800 800 ax.plot_profile.set_data(self.data['rti'][n][-1], self.y)
801 801 ax.plot_noise.set_data(numpy.repeat(
802 802 self.data['noise'][n][-1], len(self.y)), self.y)
803 803
804 804 self.saveTime = self.min_time
805 805
806 806
807 807 class PlotCOHData(PlotRTIData):
808 808 '''
809 809 Plot for Coherence data
810 810 '''
811 811
812 812 CODE = 'coh'
813 813
814 814 def setup(self):
815 815 self.xaxis = 'time'
816 816 self.ncols = 1
817 817 self.nrows = len(self.data.pairs)
818 818 self.nplots = len(self.data.pairs)
819 819 self.ylabel = 'Range [km]'
820 820 if self.CODE == 'coh':
821 821 self.cb_label = ''
822 822 self.titles = [
823 823 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
824 824 else:
825 825 self.cb_label = 'Degrees'
826 826 self.titles = [
827 827 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
828 828
829 829
830 830 class PlotPHASEData(PlotCOHData):
831 831 '''
832 832 Plot for Phase map data
833 833 '''
834 834
835 835 CODE = 'phase'
836 836 colormap = 'seismic'
837 837
838 838
839 839 class PlotNoiseData(PlotData):
840 840 '''
841 841 Plot for noise
842 842 '''
843 843
844 844 CODE = 'noise'
845 845
846 846 def setup(self):
847 847 self.xaxis = 'time'
848 848 self.ncols = 1
849 849 self.nrows = 1
850 850 self.nplots = 1
851 851 self.ylabel = 'Intensity [dB]'
852 852 self.titles = ['Noise']
853 853 self.colorbar = False
854 854
855 855 def plot(self):
856 856
857 857 x = self.times
858 858 xmin = self.min_time
859 859 xmax = xmin + self.xrange * 60 * 60
860 860 Y = self.data[self.CODE]
861 861
862 862 if self.axes[0].firsttime:
863 863 for ch in self.data.channels:
864 864 y = Y[ch]
865 865 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
866 866 plt.legend()
867 867 else:
868 868 for ch in self.data.channels:
869 869 y = Y[ch]
870 870 self.axes[0].lines[ch].set_data(x, y)
871 871
872 872 self.ymin = numpy.nanmin(Y) - 5
873 873 self.ymax = numpy.nanmax(Y) + 5
874 874 self.saveTime = self.min_time
875 875
876 876
877 877 class PlotSNRData(PlotRTIData):
878 878 '''
879 879 Plot for SNR Data
880 880 '''
881 881
882 882 CODE = 'snr'
883 883 colormap = 'jet'
884 884
885 885
886 886 class PlotDOPData(PlotRTIData):
887 887 '''
888 888 Plot for DOPPLER Data
889 889 '''
890 890
891 891 CODE = 'dop'
892 892 colormap = 'jet'
893 893
894 894
895 895 class PlotSkyMapData(PlotData):
896 896 '''
897 897 Plot for meteors detection data
898 898 '''
899 899
900 900 CODE = 'param'
901 901
902 902 def setup(self):
903 903
904 904 self.ncols = 1
905 905 self.nrows = 1
906 906 self.width = 7.2
907 907 self.height = 7.2
908 908 self.nplots = 1
909 909 self.xlabel = 'Zonal Zenith Angle (deg)'
910 910 self.ylabel = 'Meridional Zenith Angle (deg)'
911 911 self.polar = True
912 912 self.ymin = -180
913 913 self.ymax = 180
914 914 self.colorbar = False
915 915
916 916 def plot(self):
917 917
918 918 arrayParameters = numpy.concatenate(self.data['param'])
919 919 error = arrayParameters[:, -1]
920 920 indValid = numpy.where(error == 0)[0]
921 921 finalMeteor = arrayParameters[indValid, :]
922 922 finalAzimuth = finalMeteor[:, 3]
923 923 finalZenith = finalMeteor[:, 4]
924 924
925 925 x = finalAzimuth * numpy.pi / 180
926 926 y = finalZenith
927 927
928 928 ax = self.axes[0]
929 929
930 930 if ax.firsttime:
931 931 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
932 932 else:
933 933 ax.plot.set_data(x, y)
934 934
935 935 dt1 = self.getDateTime(self.min_time).strftime('%y/%m/%d %H:%M:%S')
936 936 dt2 = self.getDateTime(self.max_time).strftime('%y/%m/%d %H:%M:%S')
937 937 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
938 938 dt2,
939 939 len(x))
940 940 self.titles[0] = title
941 941 self.saveTime = self.max_time
942 942
943 943
944 944 class PlotParamData(PlotRTIData):
945 945 '''
946 946 Plot for data_param object
947 947 '''
948 948
949 949 CODE = 'param'
950 950 colormap = 'seismic'
951 951
952 952 def setup(self):
953 953 self.xaxis = 'time'
954 954 self.ncols = 1
955 955 self.nrows = self.data.shape(self.CODE)[0]
956 956 self.nplots = self.nrows
957 957 if self.showSNR:
958 958 self.nrows += 1
959 959 self.nplots += 1
960 960
961 961 self.ylabel = 'Height [km]'
962 962 if not self.titles:
963 963 self.titles = self.data.parameters \
964 if self.data.parameters else ['Param {}'.format(x) for x in xrange(self.nrows)]
964 if self.data.parameters else ['Param {}'.format(x) for x in range(self.nrows)]
965 965 if self.showSNR:
966 966 self.titles.append('SNR')
967 967
968 968 def plot(self):
969 969 self.data.normalize_heights()
970 970 self.x = self.times
971 971 self.y = self.data.heights
972 972 if self.showSNR:
973 973 self.z = numpy.concatenate(
974 974 (self.data[self.CODE], self.data['snr'])
975 975 )
976 976 else:
977 977 self.z = self.data[self.CODE]
978 978
979 979 self.z = numpy.ma.masked_invalid(self.z)
980 980
981 981 if self.decimation is None:
982 982 x, y, z = self.fill_gaps(self.x, self.y, self.z)
983 983 else:
984 984 x, y, z = self.fill_gaps(*self.decimate())
985 985
986 986 for n, ax in enumerate(self.axes):
987 987
988 988 self.zmax = self.zmax if self.zmax is not None else numpy.max(
989 989 self.z[n])
990 990 self.zmin = self.zmin if self.zmin is not None else numpy.min(
991 991 self.z[n])
992 992
993 993 if ax.firsttime:
994 994 if self.zlimits is not None:
995 995 self.zmin, self.zmax = self.zlimits[n]
996 996
997 997 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
998 998 vmin=self.zmin,
999 999 vmax=self.zmax,
1000 1000 cmap=self.cmaps[n]
1001 1001 )
1002 1002 else:
1003 1003 if self.zlimits is not None:
1004 1004 self.zmin, self.zmax = self.zlimits[n]
1005 1005 ax.collections.remove(ax.collections[0])
1006 1006 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
1007 1007 vmin=self.zmin,
1008 1008 vmax=self.zmax,
1009 1009 cmap=self.cmaps[n]
1010 1010 )
1011 1011
1012 1012 self.saveTime = self.min_time
1013 1013
1014 1014
1015 1015 class PlotOutputData(PlotParamData):
1016 1016 '''
1017 1017 Plot data_output object
1018 1018 '''
1019 1019
1020 1020 CODE = 'output'
1021 1021 colormap = 'seismic'
1022 1022
1023 1023
1024 1024 class PlotPolarMapData(PlotData):
1025 1025 '''
1026 1026 Plot for meteors detection data
1027 1027 '''
1028 1028
1029 1029 CODE = 'param'
1030 1030 colormap = 'seismic'
1031 1031
1032 1032 def setup(self):
1033 1033 self.ncols = 1
1034 1034 self.nrows = 1
1035 1035 self.width = 9
1036 1036 self.height = 8
1037 1037 self.mode = self.data.meta['mode']
1038 1038 if self.channels is not None:
1039 1039 self.nplots = len(self.channels)
1040 1040 self.nrows = len(self.channels)
1041 1041 else:
1042 1042 self.nplots = self.data.shape(self.CODE)[0]
1043 1043 self.nrows = self.nplots
1044 self.channels = range(self.nplots)
1044 self.channels = list(range(self.nplots))
1045 1045 if self.mode == 'E':
1046 1046 self.xlabel = 'Longitude'
1047 1047 self.ylabel = 'Latitude'
1048 1048 else:
1049 1049 self.xlabel = 'Range (km)'
1050 1050 self.ylabel = 'Height (km)'
1051 1051 self.bgcolor = 'white'
1052 1052 self.cb_labels = self.data.meta['units']
1053 1053 self.lat = self.data.meta['latitude']
1054 1054 self.lon = self.data.meta['longitude']
1055 1055 self.xmin, self.xmax = float(km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
1056 1056 self.ymin, self.ymax = float(km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
1057 1057 # self.polar = True
1058 1058
1059 1059 def plot(self):
1060 1060
1061 1061 for n, ax in enumerate(self.axes):
1062 1062 data = self.data['param'][self.channels[n]]
1063 1063
1064 1064 zeniths = numpy.linspace(0, self.data.meta['max_range'], data.shape[1])
1065 1065 if self.mode == 'E':
1066 1066 azimuths = -numpy.radians(self.data.heights)+numpy.pi/2
1067 1067 r, theta = numpy.meshgrid(zeniths, azimuths)
1068 1068 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
1069 1069 x = km2deg(x) + self.lon
1070 1070 y = km2deg(y) + self.lat
1071 1071 else:
1072 1072 azimuths = numpy.radians(self.data.heights)
1073 1073 r, theta = numpy.meshgrid(zeniths, azimuths)
1074 1074 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
1075 1075 self.y = zeniths
1076 1076
1077 1077 if ax.firsttime:
1078 1078 if self.zlimits is not None:
1079 1079 self.zmin, self.zmax = self.zlimits[n]
1080 1080 ax.plt = ax.pcolormesh(#r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1081 1081 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1082 1082 vmin=self.zmin,
1083 1083 vmax=self.zmax,
1084 1084 cmap=self.cmaps[n])
1085 1085 else:
1086 1086 if self.zlimits is not None:
1087 1087 self.zmin, self.zmax = self.zlimits[n]
1088 1088 ax.collections.remove(ax.collections[0])
1089 1089 ax.plt = ax.pcolormesh(# r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1090 1090 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1091 1091 vmin=self.zmin,
1092 1092 vmax=self.zmax,
1093 1093 cmap=self.cmaps[n])
1094 1094
1095 1095 if self.mode == 'A':
1096 1096 continue
1097 1097
1098 1098 # plot district names
1099 1099 f = open('/data/workspace/schain_scripts/distrito.csv')
1100 1100 for line in f:
1101 1101 label, lon, lat = [s.strip() for s in line.split(',') if s]
1102 1102 lat = float(lat)
1103 1103 lon = float(lon)
1104 1104 # ax.plot(lon, lat, '.b', ms=2)
1105 1105 ax.text(lon, lat, label.decode('utf8'), ha='center', va='bottom', size='8', color='black')
1106 1106
1107 1107 # plot limites
1108 1108 limites =[]
1109 1109 tmp = []
1110 1110 for line in open('/data/workspace/schain_scripts/lima.csv'):
1111 1111 if '#' in line:
1112 1112 if tmp:
1113 1113 limites.append(tmp)
1114 1114 tmp = []
1115 1115 continue
1116 1116 values = line.strip().split(',')
1117 1117 tmp.append((float(values[0]), float(values[1])))
1118 1118 for points in limites:
1119 1119 ax.add_patch(Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
1120 1120
1121 1121 # plot Cuencas
1122 1122 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
1123 1123 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
1124 1124 values = [line.strip().split(',') for line in f]
1125 1125 points = [(float(s[0]), float(s[1])) for s in values]
1126 1126 ax.add_patch(Polygon(points, ec='b', fc='none'))
1127 1127
1128 1128 # plot grid
1129 1129 for r in (15, 30, 45, 60):
1130 1130 ax.add_artist(plt.Circle((self.lon, self.lat), km2deg(r), color='0.6', fill=False, lw=0.2))
1131 1131 ax.text(
1132 1132 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
1133 1133 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
1134 1134 '{}km'.format(r),
1135 1135 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
1136 1136
1137 1137 if self.mode == 'E':
1138 1138 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
1139 1139 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
1140 1140 else:
1141 1141 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
1142 1142 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
1143 1143
1144 1144 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
1145 1145 self.titles = ['{} {}'.format(self.data.parameters[x], title) for x in self.channels]
1146 1146 self.saveTime = self.max_time
1147 1147
1148
1148 No newline at end of file
@@ -1,329 +1,329
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 from figure import Figure, isRealtime
11 from plotting_codes import *
10 from .figure import Figure, isRealtime
11 from .plotting_codes import *
12 12
13 13 class SpectraHeisScope(Figure):
14 14
15 15
16 16 isConfig = None
17 17 __nsubplots = None
18 18
19 19 WIDTHPROF = None
20 20 HEIGHTPROF = None
21 21 PREFIX = 'spc'
22 22
23 23 def __init__(self, **kwargs):
24 24
25 25 Figure.__init__(self, **kwargs)
26 26 self.isConfig = False
27 27 self.__nsubplots = 1
28 28
29 29 self.WIDTH = 230
30 30 self.HEIGHT = 250
31 31 self.WIDTHPROF = 120
32 32 self.HEIGHTPROF = 0
33 33 self.counter_imagwr = 0
34 34
35 35 self.PLOT_CODE = SPEC_CODE
36 36
37 37 def getSubplots(self):
38 38
39 39 ncol = int(numpy.sqrt(self.nplots)+0.9)
40 40 nrow = int(self.nplots*1./ncol + 0.9)
41 41
42 42 return nrow, ncol
43 43
44 44 def setup(self, id, nplots, wintitle, show):
45 45
46 46 showprofile = False
47 47 self.__showprofile = showprofile
48 48 self.nplots = nplots
49 49
50 50 ncolspan = 1
51 51 colspan = 1
52 52 if showprofile:
53 53 ncolspan = 3
54 54 colspan = 2
55 55 self.__nsubplots = 2
56 56
57 57 self.createFigure(id = id,
58 58 wintitle = wintitle,
59 59 widthplot = self.WIDTH + self.WIDTHPROF,
60 60 heightplot = self.HEIGHT + self.HEIGHTPROF,
61 61 show = show)
62 62
63 63 nrow, ncol = self.getSubplots()
64 64
65 65 counter = 0
66 66 for y in range(nrow):
67 67 for x in range(ncol):
68 68
69 69 if counter >= self.nplots:
70 70 break
71 71
72 72 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
73 73
74 74 if showprofile:
75 75 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
76 76
77 77 counter += 1
78 78
79 79
80 80 def run(self, dataOut, id, wintitle="", channelList=None,
81 81 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
82 82 figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
83 83 server=None, folder=None, username=None, password=None,
84 84 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
85 85
86 86 """
87 87
88 88 Input:
89 89 dataOut :
90 90 id :
91 91 wintitle :
92 92 channelList :
93 93 xmin : None,
94 94 xmax : None,
95 95 ymin : None,
96 96 ymax : None,
97 97 """
98 98
99 99 if dataOut.realtime:
100 100 if not(isRealtime(utcdatatime = dataOut.utctime)):
101 print 'Skipping this plot function'
101 print('Skipping this plot function')
102 102 return
103 103
104 104 if channelList == None:
105 105 channelIndexList = dataOut.channelIndexList
106 106 else:
107 107 channelIndexList = []
108 108 for channel in channelList:
109 109 if channel not in dataOut.channelList:
110 raise ValueError, "Channel %d is not in dataOut.channelList"
110 raise ValueError("Channel %d is not in dataOut.channelList")
111 111 channelIndexList.append(dataOut.channelList.index(channel))
112 112
113 113 # x = dataOut.heightList
114 114 c = 3E8
115 115 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
116 116 #deberia cambiar para el caso de 1Mhz y 100KHz
117 117 x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000))
118 118 #para 1Mhz descomentar la siguiente linea
119 119 #x= x/(10000.0)
120 120 # y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
121 121 # y = y.real
122 122 factor = dataOut.normFactor
123 123 data = dataOut.data_spc / factor
124 124 datadB = 10.*numpy.log10(data)
125 125 y = datadB
126 126
127 127 #thisDatetime = dataOut.datatime
128 128 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
129 129 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
130 130 xlabel = ""
131 131 #para 1Mhz descomentar la siguiente linea
132 132 #xlabel = "Frequency x 10000"
133 133 ylabel = "Intensity (dB)"
134 134
135 135 if not self.isConfig:
136 136 nplots = len(channelIndexList)
137 137
138 138 self.setup(id=id,
139 139 nplots=nplots,
140 140 wintitle=wintitle,
141 141 show=show)
142 142
143 143 if xmin == None: xmin = numpy.nanmin(x)
144 144 if xmax == None: xmax = numpy.nanmax(x)
145 145 if ymin == None: ymin = numpy.nanmin(y)
146 146 if ymax == None: ymax = numpy.nanmax(y)
147 147
148 148 self.FTP_WEI = ftp_wei
149 149 self.EXP_CODE = exp_code
150 150 self.SUB_EXP_CODE = sub_exp_code
151 151 self.PLOT_POS = plot_pos
152 152
153 153 self.isConfig = True
154 154
155 155 self.setWinTitle(title)
156 156
157 157 for i in range(len(self.axesList)):
158 158 ychannel = y[i,:]
159 159 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
160 160 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[channelIndexList[i]], numpy.max(ychannel), str_datetime)
161 161 axes = self.axesList[i]
162 162 axes.pline(x, ychannel,
163 163 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
164 164 xlabel=xlabel, ylabel=ylabel, title=title, grid='both')
165 165
166 166
167 167 self.draw()
168 168
169 169 self.save(figpath=figpath,
170 170 figfile=figfile,
171 171 save=save,
172 172 ftp=ftp,
173 173 wr_period=wr_period,
174 174 thisDatetime=thisDatetime)
175 175
176 176 class RTIfromSpectraHeis(Figure):
177 177
178 178 isConfig = None
179 179 __nsubplots = None
180 180
181 181 PREFIX = 'rtinoise'
182 182
183 183 def __init__(self, **kwargs):
184 184 Figure.__init__(self, **kwargs)
185 185 self.timerange = 24*60*60
186 186 self.isConfig = False
187 187 self.__nsubplots = 1
188 188
189 189 self.WIDTH = 820
190 190 self.HEIGHT = 200
191 191 self.WIDTHPROF = 120
192 192 self.HEIGHTPROF = 0
193 193 self.counter_imagwr = 0
194 194 self.xdata = None
195 195 self.ydata = None
196 196 self.figfile = None
197 197
198 198 self.PLOT_CODE = RTI_CODE
199 199
200 200 def getSubplots(self):
201 201
202 202 ncol = 1
203 203 nrow = 1
204 204
205 205 return nrow, ncol
206 206
207 207 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
208 208
209 209 self.__showprofile = showprofile
210 210 self.nplots = nplots
211 211
212 212 ncolspan = 7
213 213 colspan = 6
214 214 self.__nsubplots = 2
215 215
216 216 self.createFigure(id = id,
217 217 wintitle = wintitle,
218 218 widthplot = self.WIDTH+self.WIDTHPROF,
219 219 heightplot = self.HEIGHT+self.HEIGHTPROF,
220 220 show = show)
221 221
222 222 nrow, ncol = self.getSubplots()
223 223
224 224 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
225 225
226 226
227 227 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
228 228 xmin=None, xmax=None, ymin=None, ymax=None,
229 229 timerange=None,
230 230 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
231 231 server=None, folder=None, username=None, password=None,
232 232 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
233 233
234 234 if channelList == None:
235 235 channelIndexList = dataOut.channelIndexList
236 236 channelList = dataOut.channelList
237 237 else:
238 238 channelIndexList = []
239 239 for channel in channelList:
240 240 if channel not in dataOut.channelList:
241 raise ValueError, "Channel %d is not in dataOut.channelList"
241 raise ValueError("Channel %d is not in dataOut.channelList")
242 242 channelIndexList.append(dataOut.channelList.index(channel))
243 243
244 244 if timerange != None:
245 245 self.timerange = timerange
246 246
247 247 x = dataOut.getTimeRange()
248 248 y = dataOut.getHeiRange()
249 249
250 250 factor = dataOut.normFactor
251 251 data = dataOut.data_spc / factor
252 252 data = numpy.average(data,axis=1)
253 253 datadB = 10*numpy.log10(data)
254 254
255 255 # factor = dataOut.normFactor
256 256 # noise = dataOut.getNoise()/factor
257 257 # noisedB = 10*numpy.log10(noise)
258 258
259 259 #thisDatetime = dataOut.datatime
260 260 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
261 261 title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y"))
262 262 xlabel = "Local Time"
263 263 ylabel = "Intensity (dB)"
264 264
265 265 if not self.isConfig:
266 266
267 267 nplots = 1
268 268
269 269 self.setup(id=id,
270 270 nplots=nplots,
271 271 wintitle=wintitle,
272 272 showprofile=showprofile,
273 273 show=show)
274 274
275 275 self.tmin, self.tmax = self.getTimeLim(x, xmin, xmax)
276 276
277 277 if ymin == None: ymin = numpy.nanmin(datadB)
278 278 if ymax == None: ymax = numpy.nanmax(datadB)
279 279
280 280 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
281 281 self.isConfig = True
282 282 self.figfile = figfile
283 283 self.xdata = numpy.array([])
284 284 self.ydata = numpy.array([])
285 285
286 286 self.FTP_WEI = ftp_wei
287 287 self.EXP_CODE = exp_code
288 288 self.SUB_EXP_CODE = sub_exp_code
289 289 self.PLOT_POS = plot_pos
290 290
291 291 self.setWinTitle(title)
292 292
293 293
294 294 # title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y"))
295 295 title = "RTI - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
296 296
297 297 legendlabels = ["channel %d"%idchannel for idchannel in channelList]
298 298 axes = self.axesList[0]
299 299
300 300 self.xdata = numpy.hstack((self.xdata, x[0:1]))
301 301
302 302 if len(self.ydata)==0:
303 303 self.ydata = datadB[channelIndexList].reshape(-1,1)
304 304 else:
305 305 self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1)))
306 306
307 307
308 308 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
309 309 xmin=self.tmin, xmax=self.tmax, ymin=ymin, ymax=ymax,
310 310 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both',
311 311 XAxisAsTime=True
312 312 )
313 313
314 314 self.draw()
315 315
316 316 update_figfile = False
317 317
318 318 if dataOut.ltctime >= self.tmax:
319 319 self.counter_imagwr = wr_period
320 320 self.isConfig = False
321 321 update_figfile = True
322 322
323 323 self.save(figpath=figpath,
324 324 figfile=figfile,
325 325 save=save,
326 326 ftp=ftp,
327 327 wr_period=wr_period,
328 328 thisDatetime=thisDatetime,
329 update_figfile=update_figfile)
329 update_figfile=update_figfile) No newline at end of file
@@ -1,2151 +1,2151
1 1 import os
2 2 import datetime
3 3 import numpy
4 4 import inspect
5 from figure import Figure, isRealtime, isTimeInHourRange
6 from plotting_codes import *
5 from .figure import Figure, isRealtime, isTimeInHourRange
6 from .plotting_codes import *
7 7
8 8
9 9 class FitGauPlot(Figure):
10 10
11 11 isConfig = None
12 12 __nsubplots = None
13 13
14 14 WIDTHPROF = None
15 15 HEIGHTPROF = None
16 16 PREFIX = 'fitgau'
17 17
18 18 def __init__(self, **kwargs):
19 19 Figure.__init__(self, **kwargs)
20 20 self.isConfig = False
21 21 self.__nsubplots = 1
22 22
23 23 self.WIDTH = 250
24 24 self.HEIGHT = 250
25 25 self.WIDTHPROF = 120
26 26 self.HEIGHTPROF = 0
27 27 self.counter_imagwr = 0
28 28
29 29 self.PLOT_CODE = SPEC_CODE
30 30
31 31 self.FTP_WEI = None
32 32 self.EXP_CODE = None
33 33 self.SUB_EXP_CODE = None
34 34 self.PLOT_POS = None
35 35
36 36 self.__xfilter_ena = False
37 37 self.__yfilter_ena = False
38 38
39 39 def getSubplots(self):
40 40
41 41 ncol = int(numpy.sqrt(self.nplots)+0.9)
42 42 nrow = int(self.nplots*1./ncol + 0.9)
43 43
44 44 return nrow, ncol
45 45
46 46 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
47 47
48 48 self.__showprofile = showprofile
49 49 self.nplots = nplots
50 50
51 51 ncolspan = 1
52 52 colspan = 1
53 53 if showprofile:
54 54 ncolspan = 3
55 55 colspan = 2
56 56 self.__nsubplots = 2
57 57
58 58 self.createFigure(id = id,
59 59 wintitle = wintitle,
60 60 widthplot = self.WIDTH + self.WIDTHPROF,
61 61 heightplot = self.HEIGHT + self.HEIGHTPROF,
62 62 show=show)
63 63
64 64 nrow, ncol = self.getSubplots()
65 65
66 66 counter = 0
67 67 for y in range(nrow):
68 68 for x in range(ncol):
69 69
70 70 if counter >= self.nplots:
71 71 break
72 72
73 73 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
74 74
75 75 if showprofile:
76 76 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
77 77
78 78 counter += 1
79 79
80 80 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
81 81 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
82 82 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
83 83 server=None, folder=None, username=None, password=None,
84 84 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
85 85 xaxis="frequency", colormap='jet', normFactor=None , GauSelector = 1):
86 86
87 87 """
88 88
89 89 Input:
90 90 dataOut :
91 91 id :
92 92 wintitle :
93 93 channelList :
94 94 showProfile :
95 95 xmin : None,
96 96 xmax : None,
97 97 ymin : None,
98 98 ymax : None,
99 99 zmin : None,
100 100 zmax : None
101 101 """
102 102 if realtime:
103 103 if not(isRealtime(utcdatatime = dataOut.utctime)):
104 print 'Skipping this plot function'
104 print('Skipping this plot function')
105 105 return
106 106
107 107 if channelList == None:
108 108 channelIndexList = dataOut.channelIndexList
109 109 else:
110 110 channelIndexList = []
111 111 for channel in channelList:
112 112 if channel not in dataOut.channelList:
113 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
113 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
114 114 channelIndexList.append(dataOut.channelList.index(channel))
115 115
116 116 # if normFactor is None:
117 117 # factor = dataOut.normFactor
118 118 # else:
119 119 # factor = normFactor
120 120 if xaxis == "frequency":
121 121 x = dataOut.spc_range[0]
122 122 xlabel = "Frequency (kHz)"
123 123
124 124 elif xaxis == "time":
125 125 x = dataOut.spc_range[1]
126 126 xlabel = "Time (ms)"
127 127
128 128 else:
129 129 x = dataOut.spc_range[2]
130 130 xlabel = "Velocity (m/s)"
131 131
132 132 ylabel = "Range (Km)"
133 133
134 134 y = dataOut.getHeiRange()
135 135
136 136 z = dataOut.GauSPC[:,GauSelector,:,:] #GauSelector] #dataOut.data_spc/factor
137 print 'GausSPC', z[0,32,10:40]
137 print('GausSPC', z[0,32,10:40])
138 138 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
139 139 zdB = 10*numpy.log10(z)
140 140
141 141 avg = numpy.average(z, axis=1)
142 142 avgdB = 10*numpy.log10(avg)
143 143
144 144 noise = dataOut.spc_noise
145 145 noisedB = 10*numpy.log10(noise)
146 146
147 147 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
148 148 title = wintitle + " Spectra"
149 149 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
150 150 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
151 151
152 152 if not self.isConfig:
153 153
154 154 nplots = len(channelIndexList)
155 155
156 156 self.setup(id=id,
157 157 nplots=nplots,
158 158 wintitle=wintitle,
159 159 showprofile=showprofile,
160 160 show=show)
161 161
162 162 if xmin == None: xmin = numpy.nanmin(x)
163 163 if xmax == None: xmax = numpy.nanmax(x)
164 164 if ymin == None: ymin = numpy.nanmin(y)
165 165 if ymax == None: ymax = numpy.nanmax(y)
166 166 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
167 167 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
168 168
169 169 self.FTP_WEI = ftp_wei
170 170 self.EXP_CODE = exp_code
171 171 self.SUB_EXP_CODE = sub_exp_code
172 172 self.PLOT_POS = plot_pos
173 173
174 174 self.isConfig = True
175 175
176 176 self.setWinTitle(title)
177 177
178 178 for i in range(self.nplots):
179 179 index = channelIndexList[i]
180 180 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
181 181 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
182 182 if len(dataOut.beam.codeList) != 0:
183 183 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
184 184
185 185 axes = self.axesList[i*self.__nsubplots]
186 186 axes.pcolor(x, y, zdB[index,:,:],
187 187 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
188 188 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
189 189 ticksize=9, cblabel='')
190 190
191 191 if self.__showprofile:
192 192 axes = self.axesList[i*self.__nsubplots +1]
193 193 axes.pline(avgdB[index,:], y,
194 194 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
195 195 xlabel='dB', ylabel='', title='',
196 196 ytick_visible=False,
197 197 grid='x')
198 198
199 199 noiseline = numpy.repeat(noisedB[index], len(y))
200 200 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
201 201
202 202 self.draw()
203 203
204 204 if figfile == None:
205 205 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
206 206 name = str_datetime
207 207 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
208 208 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
209 209 figfile = self.getFilename(name)
210 210
211 211 self.save(figpath=figpath,
212 212 figfile=figfile,
213 213 save=save,
214 214 ftp=ftp,
215 215 wr_period=wr_period,
216 216 thisDatetime=thisDatetime)
217 217
218 218
219 219
220 220 class MomentsPlot(Figure):
221 221
222 222 isConfig = None
223 223 __nsubplots = None
224 224
225 225 WIDTHPROF = None
226 226 HEIGHTPROF = None
227 227 PREFIX = 'prm'
228 228 def __init__(self, **kwargs):
229 229 Figure.__init__(self, **kwargs)
230 230 self.isConfig = False
231 231 self.__nsubplots = 1
232 232
233 233 self.WIDTH = 280
234 234 self.HEIGHT = 250
235 235 self.WIDTHPROF = 120
236 236 self.HEIGHTPROF = 0
237 237 self.counter_imagwr = 0
238 238
239 239 self.PLOT_CODE = MOMENTS_CODE
240 240
241 241 self.FTP_WEI = None
242 242 self.EXP_CODE = None
243 243 self.SUB_EXP_CODE = None
244 244 self.PLOT_POS = None
245 245
246 246 def getSubplots(self):
247 247
248 248 ncol = int(numpy.sqrt(self.nplots)+0.9)
249 249 nrow = int(self.nplots*1./ncol + 0.9)
250 250
251 251 return nrow, ncol
252 252
253 253 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
254 254
255 255 self.__showprofile = showprofile
256 256 self.nplots = nplots
257 257
258 258 ncolspan = 1
259 259 colspan = 1
260 260 if showprofile:
261 261 ncolspan = 3
262 262 colspan = 2
263 263 self.__nsubplots = 2
264 264
265 265 self.createFigure(id = id,
266 266 wintitle = wintitle,
267 267 widthplot = self.WIDTH + self.WIDTHPROF,
268 268 heightplot = self.HEIGHT + self.HEIGHTPROF,
269 269 show=show)
270 270
271 271 nrow, ncol = self.getSubplots()
272 272
273 273 counter = 0
274 274 for y in range(nrow):
275 275 for x in range(ncol):
276 276
277 277 if counter >= self.nplots:
278 278 break
279 279
280 280 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
281 281
282 282 if showprofile:
283 283 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
284 284
285 285 counter += 1
286 286
287 287 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
288 288 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
289 289 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
290 290 server=None, folder=None, username=None, password=None,
291 291 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
292 292
293 293 """
294 294
295 295 Input:
296 296 dataOut :
297 297 id :
298 298 wintitle :
299 299 channelList :
300 300 showProfile :
301 301 xmin : None,
302 302 xmax : None,
303 303 ymin : None,
304 304 ymax : None,
305 305 zmin : None,
306 306 zmax : None
307 307 """
308 308
309 309 if dataOut.flagNoData:
310 310 return None
311 311
312 312 if realtime:
313 313 if not(isRealtime(utcdatatime = dataOut.utctime)):
314 print 'Skipping this plot function'
314 print('Skipping this plot function')
315 315 return
316 316
317 317 if channelList == None:
318 318 channelIndexList = dataOut.channelIndexList
319 319 else:
320 320 channelIndexList = []
321 321 for channel in channelList:
322 322 if channel not in dataOut.channelList:
323 raise ValueError, "Channel %d is not in dataOut.channelList"
323 raise ValueError("Channel %d is not in dataOut.channelList")
324 324 channelIndexList.append(dataOut.channelList.index(channel))
325 325
326 326 factor = dataOut.normFactor
327 327 x = dataOut.abscissaList
328 328 y = dataOut.heightList
329 329
330 330 z = dataOut.data_pre[channelIndexList,:,:]/factor
331 331 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
332 332 avg = numpy.average(z, axis=1)
333 333 noise = dataOut.noise/factor
334 334
335 335 zdB = 10*numpy.log10(z)
336 336 avgdB = 10*numpy.log10(avg)
337 337 noisedB = 10*numpy.log10(noise)
338 338
339 339 #thisDatetime = dataOut.datatime
340 340 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
341 341 title = wintitle + " Parameters"
342 342 xlabel = "Velocity (m/s)"
343 343 ylabel = "Range (Km)"
344 344
345 345 update_figfile = False
346 346
347 347 if not self.isConfig:
348 348
349 349 nplots = len(channelIndexList)
350 350
351 351 self.setup(id=id,
352 352 nplots=nplots,
353 353 wintitle=wintitle,
354 354 showprofile=showprofile,
355 355 show=show)
356 356
357 357 if xmin == None: xmin = numpy.nanmin(x)
358 358 if xmax == None: xmax = numpy.nanmax(x)
359 359 if ymin == None: ymin = numpy.nanmin(y)
360 360 if ymax == None: ymax = numpy.nanmax(y)
361 361 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
362 362 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
363 363
364 364 self.FTP_WEI = ftp_wei
365 365 self.EXP_CODE = exp_code
366 366 self.SUB_EXP_CODE = sub_exp_code
367 367 self.PLOT_POS = plot_pos
368 368
369 369 self.isConfig = True
370 370 update_figfile = True
371 371
372 372 self.setWinTitle(title)
373 373
374 374 for i in range(self.nplots):
375 375 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
376 376 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i], noisedB[i], str_datetime)
377 377 axes = self.axesList[i*self.__nsubplots]
378 378 axes.pcolor(x, y, zdB[i,:,:],
379 379 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
380 380 xlabel=xlabel, ylabel=ylabel, title=title,
381 381 ticksize=9, cblabel='')
382 382 #Mean Line
383 383 mean = dataOut.data_param[i, 1, :]
384 384 axes.addpline(mean, y, idline=0, color="black", linestyle="solid", lw=1)
385 385
386 386 if self.__showprofile:
387 387 axes = self.axesList[i*self.__nsubplots +1]
388 388 axes.pline(avgdB[i], y,
389 389 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
390 390 xlabel='dB', ylabel='', title='',
391 391 ytick_visible=False,
392 392 grid='x')
393 393
394 394 noiseline = numpy.repeat(noisedB[i], len(y))
395 395 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
396 396
397 397 self.draw()
398 398
399 399 self.save(figpath=figpath,
400 400 figfile=figfile,
401 401 save=save,
402 402 ftp=ftp,
403 403 wr_period=wr_period,
404 404 thisDatetime=thisDatetime)
405 405
406 406
407 407
408 408 class SkyMapPlot(Figure):
409 409
410 410 __isConfig = None
411 411 __nsubplots = None
412 412
413 413 WIDTHPROF = None
414 414 HEIGHTPROF = None
415 415 PREFIX = 'mmap'
416 416
417 417 def __init__(self, **kwargs):
418 418 Figure.__init__(self, **kwargs)
419 419 self.isConfig = False
420 420 self.__nsubplots = 1
421 421
422 422 # self.WIDTH = 280
423 423 # self.HEIGHT = 250
424 424 self.WIDTH = 600
425 425 self.HEIGHT = 600
426 426 self.WIDTHPROF = 120
427 427 self.HEIGHTPROF = 0
428 428 self.counter_imagwr = 0
429 429
430 430 self.PLOT_CODE = MSKYMAP_CODE
431 431
432 432 self.FTP_WEI = None
433 433 self.EXP_CODE = None
434 434 self.SUB_EXP_CODE = None
435 435 self.PLOT_POS = None
436 436
437 437 def getSubplots(self):
438 438
439 439 ncol = int(numpy.sqrt(self.nplots)+0.9)
440 440 nrow = int(self.nplots*1./ncol + 0.9)
441 441
442 442 return nrow, ncol
443 443
444 444 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
445 445
446 446 self.__showprofile = showprofile
447 447 self.nplots = nplots
448 448
449 449 ncolspan = 1
450 450 colspan = 1
451 451
452 452 self.createFigure(id = id,
453 453 wintitle = wintitle,
454 454 widthplot = self.WIDTH, #+ self.WIDTHPROF,
455 455 heightplot = self.HEIGHT,# + self.HEIGHTPROF,
456 456 show=show)
457 457
458 458 nrow, ncol = 1,1
459 459 counter = 0
460 460 x = 0
461 461 y = 0
462 462 self.addAxes(1, 1, 0, 0, 1, 1, True)
463 463
464 464 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
465 465 tmin=0, tmax=24, timerange=None,
466 466 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
467 467 server=None, folder=None, username=None, password=None,
468 468 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
469 469
470 470 """
471 471
472 472 Input:
473 473 dataOut :
474 474 id :
475 475 wintitle :
476 476 channelList :
477 477 showProfile :
478 478 xmin : None,
479 479 xmax : None,
480 480 ymin : None,
481 481 ymax : None,
482 482 zmin : None,
483 483 zmax : None
484 484 """
485 485
486 486 arrayParameters = dataOut.data_param
487 487 error = arrayParameters[:,-1]
488 488 indValid = numpy.where(error == 0)[0]
489 489 finalMeteor = arrayParameters[indValid,:]
490 490 finalAzimuth = finalMeteor[:,3]
491 491 finalZenith = finalMeteor[:,4]
492 492
493 493 x = finalAzimuth*numpy.pi/180
494 494 y = finalZenith
495 495 x1 = [dataOut.ltctime, dataOut.ltctime]
496 496
497 497 #thisDatetime = dataOut.datatime
498 498 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
499 499 title = wintitle + " Parameters"
500 500 xlabel = "Zonal Zenith Angle (deg) "
501 501 ylabel = "Meridional Zenith Angle (deg)"
502 502 update_figfile = False
503 503
504 504 if not self.isConfig:
505 505
506 506 nplots = 1
507 507
508 508 self.setup(id=id,
509 509 nplots=nplots,
510 510 wintitle=wintitle,
511 511 showprofile=showprofile,
512 512 show=show)
513 513
514 514 if self.xmin is None and self.xmax is None:
515 515 self.xmin, self.xmax = self.getTimeLim(x1, tmin, tmax, timerange)
516 516
517 517 if timerange != None:
518 518 self.timerange = timerange
519 519 else:
520 520 self.timerange = self.xmax - self.xmin
521 521
522 522 self.FTP_WEI = ftp_wei
523 523 self.EXP_CODE = exp_code
524 524 self.SUB_EXP_CODE = sub_exp_code
525 525 self.PLOT_POS = plot_pos
526 526 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
527 527 self.firstdate = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
528 528 self.isConfig = True
529 529 update_figfile = True
530 530
531 531 self.setWinTitle(title)
532 532
533 533 i = 0
534 534 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
535 535
536 536 axes = self.axesList[i*self.__nsubplots]
537 537 nevents = axes.x_buffer.shape[0] + x.shape[0]
538 538 title = "Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n" %(self.firstdate,str_datetime,nevents)
539 539 axes.polar(x, y,
540 540 title=title, xlabel=xlabel, ylabel=ylabel,
541 541 ticksize=9, cblabel='')
542 542
543 543 self.draw()
544 544
545 545 self.save(figpath=figpath,
546 546 figfile=figfile,
547 547 save=save,
548 548 ftp=ftp,
549 549 wr_period=wr_period,
550 550 thisDatetime=thisDatetime,
551 551 update_figfile=update_figfile)
552 552
553 553 if dataOut.ltctime >= self.xmax:
554 554 self.isConfigmagwr = wr_period
555 555 self.isConfig = False
556 556 update_figfile = True
557 557 axes.__firsttime = True
558 558 self.xmin += self.timerange
559 559 self.xmax += self.timerange
560 560
561 561
562 562
563 563
564 564 class WindProfilerPlot(Figure):
565 565
566 566 __isConfig = None
567 567 __nsubplots = None
568 568
569 569 WIDTHPROF = None
570 570 HEIGHTPROF = None
571 571 PREFIX = 'wind'
572 572
573 573 def __init__(self, **kwargs):
574 574 Figure.__init__(self, **kwargs)
575 575 self.timerange = None
576 576 self.isConfig = False
577 577 self.__nsubplots = 1
578 578
579 579 self.WIDTH = 800
580 580 self.HEIGHT = 300
581 581 self.WIDTHPROF = 120
582 582 self.HEIGHTPROF = 0
583 583 self.counter_imagwr = 0
584 584
585 585 self.PLOT_CODE = WIND_CODE
586 586
587 587 self.FTP_WEI = None
588 588 self.EXP_CODE = None
589 589 self.SUB_EXP_CODE = None
590 590 self.PLOT_POS = None
591 591 self.tmin = None
592 592 self.tmax = None
593 593
594 594 self.xmin = None
595 595 self.xmax = None
596 596
597 597 self.figfile = None
598 598
599 599 def getSubplots(self):
600 600
601 601 ncol = 1
602 602 nrow = self.nplots
603 603
604 604 return nrow, ncol
605 605
606 606 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
607 607
608 608 self.__showprofile = showprofile
609 609 self.nplots = nplots
610 610
611 611 ncolspan = 1
612 612 colspan = 1
613 613
614 614 self.createFigure(id = id,
615 615 wintitle = wintitle,
616 616 widthplot = self.WIDTH + self.WIDTHPROF,
617 617 heightplot = self.HEIGHT + self.HEIGHTPROF,
618 618 show=show)
619 619
620 620 nrow, ncol = self.getSubplots()
621 621
622 622 counter = 0
623 623 for y in range(nrow):
624 624 if counter >= self.nplots:
625 625 break
626 626
627 627 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
628 628 counter += 1
629 629
630 630 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='False',
631 631 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
632 632 zmax_ver = None, zmin_ver = None, SNRmin = None, SNRmax = None,
633 633 timerange=None, SNRthresh = None,
634 634 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
635 635 server=None, folder=None, username=None, password=None,
636 636 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
637 637 """
638 638
639 639 Input:
640 640 dataOut :
641 641 id :
642 642 wintitle :
643 643 channelList :
644 644 showProfile :
645 645 xmin : None,
646 646 xmax : None,
647 647 ymin : None,
648 648 ymax : None,
649 649 zmin : None,
650 650 zmax : None
651 651 """
652 652
653 653 # if timerange is not None:
654 654 # self.timerange = timerange
655 655 #
656 656 # tmin = None
657 657 # tmax = None
658 658
659 659 x = dataOut.getTimeRange1(dataOut.paramInterval)
660 660 y = dataOut.heightList
661 661 z = dataOut.data_output.copy()
662 662 nplots = z.shape[0] #Number of wind dimensions estimated
663 663 nplotsw = nplots
664 664
665 665
666 666 #If there is a SNR function defined
667 667 if dataOut.data_SNR is not None:
668 668 nplots += 1
669 669 SNR = dataOut.data_SNR
670 670 SNRavg = numpy.average(SNR, axis=0)
671 671
672 672 SNRdB = 10*numpy.log10(SNR)
673 673 SNRavgdB = 10*numpy.log10(SNRavg)
674 674
675 675 if SNRthresh == None: SNRthresh = -5.0
676 676 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
677 677
678 678 for i in range(nplotsw):
679 679 z[i,ind] = numpy.nan
680 680
681 681 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
682 682 #thisDatetime = datetime.datetime.now()
683 683 title = wintitle + "Wind"
684 684 xlabel = ""
685 685 ylabel = "Height (km)"
686 686 update_figfile = False
687 687
688 688 if not self.isConfig:
689 689
690 690 self.setup(id=id,
691 691 nplots=nplots,
692 692 wintitle=wintitle,
693 693 showprofile=showprofile,
694 694 show=show)
695 695
696 696 if timerange is not None:
697 697 self.timerange = timerange
698 698
699 699 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
700 700
701 701 if ymin == None: ymin = numpy.nanmin(y)
702 702 if ymax == None: ymax = numpy.nanmax(y)
703 703
704 if zmax == None: zmax = numpy.nanmax(abs(z[range(2),:]))
704 if zmax == None: zmax = numpy.nanmax(abs(z[list(range(2)),:]))
705 705 #if numpy.isnan(zmax): zmax = 50
706 706 if zmin == None: zmin = -zmax
707 707
708 708 if nplotsw == 3:
709 709 if zmax_ver == None: zmax_ver = numpy.nanmax(abs(z[2,:]))
710 710 if zmin_ver == None: zmin_ver = -zmax_ver
711 711
712 712 if dataOut.data_SNR is not None:
713 713 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
714 714 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
715 715
716 716
717 717 self.FTP_WEI = ftp_wei
718 718 self.EXP_CODE = exp_code
719 719 self.SUB_EXP_CODE = sub_exp_code
720 720 self.PLOT_POS = plot_pos
721 721
722 722 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
723 723 self.isConfig = True
724 724 self.figfile = figfile
725 725 update_figfile = True
726 726
727 727 self.setWinTitle(title)
728 728
729 729 if ((self.xmax - x[1]) < (x[1]-x[0])):
730 730 x[1] = self.xmax
731 731
732 732 strWind = ['Zonal', 'Meridional', 'Vertical']
733 733 strCb = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
734 734 zmaxVector = [zmax, zmax, zmax_ver]
735 735 zminVector = [zmin, zmin, zmin_ver]
736 736 windFactor = [1,1,100]
737 737
738 738 for i in range(nplotsw):
739 739
740 740 title = "%s Wind: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
741 741 axes = self.axesList[i*self.__nsubplots]
742 742
743 743 z1 = z[i,:].reshape((1,-1))*windFactor[i]
744 744 #z1=numpy.ma.masked_where(z1==0.,z1)
745 745
746 746 axes.pcolorbuffer(x, y, z1,
747 747 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
748 748 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
749 749 ticksize=9, cblabel=strCb[i], cbsize="1%", colormap="seismic" )
750 750
751 751 if dataOut.data_SNR is not None:
752 752 i += 1
753 753 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
754 754 axes = self.axesList[i*self.__nsubplots]
755 755 SNRavgdB = SNRavgdB.reshape((1,-1))
756 756 axes.pcolorbuffer(x, y, SNRavgdB,
757 757 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
758 758 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
759 759 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
760 760
761 761 self.draw()
762 762
763 763 self.save(figpath=figpath,
764 764 figfile=figfile,
765 765 save=save,
766 766 ftp=ftp,
767 767 wr_period=wr_period,
768 768 thisDatetime=thisDatetime,
769 769 update_figfile=update_figfile)
770 770
771 771 if dataOut.ltctime + dataOut.paramInterval >= self.xmax:
772 772 self.counter_imagwr = wr_period
773 773 self.isConfig = False
774 774 update_figfile = True
775 775
776 776
777 777 class ParametersPlot(Figure):
778 778
779 779 __isConfig = None
780 780 __nsubplots = None
781 781
782 782 WIDTHPROF = None
783 783 HEIGHTPROF = None
784 784 PREFIX = 'param'
785 785
786 786 nplots = None
787 787 nchan = None
788 788
789 789 def __init__(self, **kwargs):
790 790 Figure.__init__(self, **kwargs)
791 791 self.timerange = None
792 792 self.isConfig = False
793 793 self.__nsubplots = 1
794 794
795 795 self.WIDTH = 800
796 796 self.HEIGHT = 180
797 797 self.WIDTHPROF = 120
798 798 self.HEIGHTPROF = 0
799 799 self.counter_imagwr = 0
800 800
801 801 self.PLOT_CODE = RTI_CODE
802 802
803 803 self.FTP_WEI = None
804 804 self.EXP_CODE = None
805 805 self.SUB_EXP_CODE = None
806 806 self.PLOT_POS = None
807 807 self.tmin = None
808 808 self.tmax = None
809 809
810 810 self.xmin = None
811 811 self.xmax = None
812 812
813 813 self.figfile = None
814 814
815 815 def getSubplots(self):
816 816
817 817 ncol = 1
818 818 nrow = self.nplots
819 819
820 820 return nrow, ncol
821 821
822 822 def setup(self, id, nplots, wintitle, show=True):
823 823
824 824 self.nplots = nplots
825 825
826 826 ncolspan = 1
827 827 colspan = 1
828 828
829 829 self.createFigure(id = id,
830 830 wintitle = wintitle,
831 831 widthplot = self.WIDTH + self.WIDTHPROF,
832 832 heightplot = self.HEIGHT + self.HEIGHTPROF,
833 833 show=show)
834 834
835 835 nrow, ncol = self.getSubplots()
836 836
837 837 counter = 0
838 838 for y in range(nrow):
839 839 for x in range(ncol):
840 840
841 841 if counter >= self.nplots:
842 842 break
843 843
844 844 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
845 845
846 846 counter += 1
847 847
848 848 def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap="jet",
849 849 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, timerange=None,
850 850 showSNR=False, SNRthresh = -numpy.inf, SNRmin=None, SNRmax=None,
851 851 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
852 852 server=None, folder=None, username=None, password=None,
853 853 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, HEIGHT=None):
854 854 """
855 855
856 856 Input:
857 857 dataOut :
858 858 id :
859 859 wintitle :
860 860 channelList :
861 861 showProfile :
862 862 xmin : None,
863 863 xmax : None,
864 864 ymin : None,
865 865 ymax : None,
866 866 zmin : None,
867 867 zmax : None
868 868 """
869 869
870 870 if HEIGHT is not None:
871 871 self.HEIGHT = HEIGHT
872 872
873 873
874 874 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
875 875 return
876 876
877 877 if channelList == None:
878 channelIndexList = range(dataOut.data_param.shape[0])
878 channelIndexList = list(range(dataOut.data_param.shape[0]))
879 879 else:
880 880 channelIndexList = []
881 881 for channel in channelList:
882 882 if channel not in dataOut.channelList:
883 raise ValueError, "Channel %d is not in dataOut.channelList"
883 raise ValueError("Channel %d is not in dataOut.channelList")
884 884 channelIndexList.append(dataOut.channelList.index(channel))
885 885
886 886 x = dataOut.getTimeRange1(dataOut.paramInterval)
887 887 y = dataOut.getHeiRange()
888 888
889 889 if dataOut.data_param.ndim == 3:
890 890 z = dataOut.data_param[channelIndexList,paramIndex,:]
891 891 else:
892 892 z = dataOut.data_param[channelIndexList,:]
893 893
894 894 if showSNR:
895 895 #SNR data
896 896 SNRarray = dataOut.data_SNR[channelIndexList,:]
897 897 SNRdB = 10*numpy.log10(SNRarray)
898 898 ind = numpy.where(SNRdB < SNRthresh)
899 899 z[ind] = numpy.nan
900 900
901 901 thisDatetime = dataOut.datatime
902 902 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
903 903 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
904 904 xlabel = ""
905 905 ylabel = "Range (Km)"
906 906
907 907 update_figfile = False
908 908
909 909 if not self.isConfig:
910 910
911 911 nchan = len(channelIndexList)
912 912 self.nchan = nchan
913 913 self.plotFact = 1
914 914 nplots = nchan
915 915
916 916 if showSNR:
917 917 nplots = nchan*2
918 918 self.plotFact = 2
919 919 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
920 920 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
921 921
922 922 self.setup(id=id,
923 923 nplots=nplots,
924 924 wintitle=wintitle,
925 925 show=show)
926 926
927 927 if timerange != None:
928 928 self.timerange = timerange
929 929
930 930 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
931 931
932 932 if ymin == None: ymin = numpy.nanmin(y)
933 933 if ymax == None: ymax = numpy.nanmax(y)
934 934 if zmin == None: zmin = numpy.nanmin(z)
935 935 if zmax == None: zmax = numpy.nanmax(z)
936 936
937 937 self.FTP_WEI = ftp_wei
938 938 self.EXP_CODE = exp_code
939 939 self.SUB_EXP_CODE = sub_exp_code
940 940 self.PLOT_POS = plot_pos
941 941
942 942 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
943 943 self.isConfig = True
944 944 self.figfile = figfile
945 945 update_figfile = True
946 946
947 947 self.setWinTitle(title)
948 948
949 949 for i in range(self.nchan):
950 950 index = channelIndexList[i]
951 951 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
952 952 axes = self.axesList[i*self.plotFact]
953 953 z1 = z[i,:].reshape((1,-1))
954 954 axes.pcolorbuffer(x, y, z1,
955 955 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
956 956 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
957 957 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
958 958
959 959 if showSNR:
960 960 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
961 961 axes = self.axesList[i*self.plotFact + 1]
962 962 SNRdB1 = SNRdB[i,:].reshape((1,-1))
963 963 axes.pcolorbuffer(x, y, SNRdB1,
964 964 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
965 965 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
966 966 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
967 967
968 968
969 969 self.draw()
970 970
971 971 if dataOut.ltctime >= self.xmax:
972 972 self.counter_imagwr = wr_period
973 973 self.isConfig = False
974 974 update_figfile = True
975 975
976 976 self.save(figpath=figpath,
977 977 figfile=figfile,
978 978 save=save,
979 979 ftp=ftp,
980 980 wr_period=wr_period,
981 981 thisDatetime=thisDatetime,
982 982 update_figfile=update_figfile)
983 983
984 984
985 985
986 986 class Parameters1Plot(Figure):
987 987
988 988 __isConfig = None
989 989 __nsubplots = None
990 990
991 991 WIDTHPROF = None
992 992 HEIGHTPROF = None
993 993 PREFIX = 'prm'
994 994
995 995 def __init__(self, **kwargs):
996 996 Figure.__init__(self, **kwargs)
997 997 self.timerange = 2*60*60
998 998 self.isConfig = False
999 999 self.__nsubplots = 1
1000 1000
1001 1001 self.WIDTH = 800
1002 1002 self.HEIGHT = 180
1003 1003 self.WIDTHPROF = 120
1004 1004 self.HEIGHTPROF = 0
1005 1005 self.counter_imagwr = 0
1006 1006
1007 1007 self.PLOT_CODE = PARMS_CODE
1008 1008
1009 1009 self.FTP_WEI = None
1010 1010 self.EXP_CODE = None
1011 1011 self.SUB_EXP_CODE = None
1012 1012 self.PLOT_POS = None
1013 1013 self.tmin = None
1014 1014 self.tmax = None
1015 1015
1016 1016 self.xmin = None
1017 1017 self.xmax = None
1018 1018
1019 1019 self.figfile = None
1020 1020
1021 1021 def getSubplots(self):
1022 1022
1023 1023 ncol = 1
1024 1024 nrow = self.nplots
1025 1025
1026 1026 return nrow, ncol
1027 1027
1028 1028 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1029 1029
1030 1030 self.__showprofile = showprofile
1031 1031 self.nplots = nplots
1032 1032
1033 1033 ncolspan = 1
1034 1034 colspan = 1
1035 1035
1036 1036 self.createFigure(id = id,
1037 1037 wintitle = wintitle,
1038 1038 widthplot = self.WIDTH + self.WIDTHPROF,
1039 1039 heightplot = self.HEIGHT + self.HEIGHTPROF,
1040 1040 show=show)
1041 1041
1042 1042 nrow, ncol = self.getSubplots()
1043 1043
1044 1044 counter = 0
1045 1045 for y in range(nrow):
1046 1046 for x in range(ncol):
1047 1047
1048 1048 if counter >= self.nplots:
1049 1049 break
1050 1050
1051 1051 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1052 1052
1053 1053 if showprofile:
1054 1054 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1055 1055
1056 1056 counter += 1
1057 1057
1058 1058 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
1059 1059 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,timerange=None,
1060 1060 parameterIndex = None, onlyPositive = False,
1061 1061 SNRthresh = -numpy.inf, SNR = True, SNRmin = None, SNRmax = None, onlySNR = False,
1062 1062 DOP = True,
1063 1063 zlabel = "", parameterName = "", parameterObject = "data_param",
1064 1064 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1065 1065 server=None, folder=None, username=None, password=None,
1066 1066 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1067 1067 #print inspect.getargspec(self.run).args
1068 1068 """
1069 1069
1070 1070 Input:
1071 1071 dataOut :
1072 1072 id :
1073 1073 wintitle :
1074 1074 channelList :
1075 1075 showProfile :
1076 1076 xmin : None,
1077 1077 xmax : None,
1078 1078 ymin : None,
1079 1079 ymax : None,
1080 1080 zmin : None,
1081 1081 zmax : None
1082 1082 """
1083 1083
1084 1084 data_param = getattr(dataOut, parameterObject)
1085 1085
1086 1086 if channelList == None:
1087 1087 channelIndexList = numpy.arange(data_param.shape[0])
1088 1088 else:
1089 1089 channelIndexList = numpy.array(channelList)
1090 1090
1091 1091 nchan = len(channelIndexList) #Number of channels being plotted
1092 1092
1093 1093 if nchan < 1:
1094 1094 return
1095 1095
1096 1096 nGraphsByChannel = 0
1097 1097
1098 1098 if SNR:
1099 1099 nGraphsByChannel += 1
1100 1100 if DOP:
1101 1101 nGraphsByChannel += 1
1102 1102
1103 1103 if nGraphsByChannel < 1:
1104 1104 return
1105 1105
1106 1106 nplots = nGraphsByChannel*nchan
1107 1107
1108 1108 if timerange is not None:
1109 1109 self.timerange = timerange
1110 1110
1111 1111 #tmin = None
1112 1112 #tmax = None
1113 1113 if parameterIndex == None:
1114 1114 parameterIndex = 1
1115 1115
1116 1116 x = dataOut.getTimeRange1(dataOut.paramInterval)
1117 1117 y = dataOut.heightList
1118 1118
1119 1119 if dataOut.data_param.ndim == 3:
1120 1120 z = dataOut.data_param[channelIndexList,parameterIndex,:]
1121 1121 else:
1122 1122 z = dataOut.data_param[channelIndexList,:]
1123 1123
1124 1124 if dataOut.data_SNR is not None:
1125 1125 if dataOut.data_SNR.ndim == 2:
1126 1126 SNRavg = numpy.average(dataOut.data_SNR, axis=0)
1127 1127 else:
1128 1128 SNRavg = dataOut.data_SNR
1129 1129 SNRdB = 10*numpy.log10(SNRavg)
1130 1130
1131 1131 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1132 1132 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
1133 1133 xlabel = ""
1134 1134 ylabel = "Range (Km)"
1135 1135
1136 1136 if onlyPositive:
1137 1137 colormap = "jet"
1138 1138 zmin = 0
1139 1139 else: colormap = "RdBu_r"
1140 1140
1141 1141 if not self.isConfig:
1142 1142
1143 1143 self.setup(id=id,
1144 1144 nplots=nplots,
1145 1145 wintitle=wintitle,
1146 1146 showprofile=showprofile,
1147 1147 show=show)
1148 1148
1149 1149 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1150 1150
1151 1151 if ymin == None: ymin = numpy.nanmin(y)
1152 1152 if ymax == None: ymax = numpy.nanmax(y)
1153 1153 if zmin == None: zmin = numpy.nanmin(z)
1154 1154 if zmax == None: zmax = numpy.nanmax(z)
1155 1155
1156 1156 if SNR:
1157 1157 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
1158 1158 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
1159 1159
1160 1160 self.FTP_WEI = ftp_wei
1161 1161 self.EXP_CODE = exp_code
1162 1162 self.SUB_EXP_CODE = sub_exp_code
1163 1163 self.PLOT_POS = plot_pos
1164 1164
1165 1165 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1166 1166 self.isConfig = True
1167 1167 self.figfile = figfile
1168 1168
1169 1169 self.setWinTitle(title)
1170 1170
1171 1171 if ((self.xmax - x[1]) < (x[1]-x[0])):
1172 1172 x[1] = self.xmax
1173 1173
1174 1174 for i in range(nchan):
1175 1175
1176 1176 if (SNR and not onlySNR): j = 2*i
1177 1177 else: j = i
1178 1178
1179 1179 j = nGraphsByChannel*i
1180 1180
1181 1181 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1182 1182 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1183 1183
1184 1184 if not onlySNR:
1185 1185 axes = self.axesList[j*self.__nsubplots]
1186 1186 z1 = z[i,:].reshape((1,-1))
1187 1187 axes.pcolorbuffer(x, y, z1,
1188 1188 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1189 1189 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1190 1190 ticksize=9, cblabel=zlabel, cbsize="1%")
1191 1191
1192 1192 if DOP:
1193 1193 title = "%s Channel %d: %s" %(parameterName, channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1194 1194
1195 1195 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1196 1196 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1197 1197 axes = self.axesList[j]
1198 1198 z1 = z[i,:].reshape((1,-1))
1199 1199 axes.pcolorbuffer(x, y, z1,
1200 1200 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1201 1201 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1202 1202 ticksize=9, cblabel=zlabel, cbsize="1%")
1203 1203
1204 1204 if SNR:
1205 1205 title = "Channel %d Signal Noise Ratio (SNR): %s" %(channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1206 1206 axes = self.axesList[(j)*self.__nsubplots]
1207 1207 if not onlySNR:
1208 1208 axes = self.axesList[(j + 1)*self.__nsubplots]
1209 1209
1210 1210 axes = self.axesList[(j + nGraphsByChannel-1)]
1211 1211 z1 = SNRdB.reshape((1,-1))
1212 1212 axes.pcolorbuffer(x, y, z1,
1213 1213 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1214 1214 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap="jet",
1215 1215 ticksize=9, cblabel=zlabel, cbsize="1%")
1216 1216
1217 1217
1218 1218
1219 1219 self.draw()
1220 1220
1221 1221 if x[1] >= self.axesList[0].xmax:
1222 1222 self.counter_imagwr = wr_period
1223 1223 self.isConfig = False
1224 1224 self.figfile = None
1225 1225
1226 1226 self.save(figpath=figpath,
1227 1227 figfile=figfile,
1228 1228 save=save,
1229 1229 ftp=ftp,
1230 1230 wr_period=wr_period,
1231 1231 thisDatetime=thisDatetime,
1232 1232 update_figfile=False)
1233 1233
1234 1234 class SpectralFittingPlot(Figure):
1235 1235
1236 1236 __isConfig = None
1237 1237 __nsubplots = None
1238 1238
1239 1239 WIDTHPROF = None
1240 1240 HEIGHTPROF = None
1241 1241 PREFIX = 'prm'
1242 1242
1243 1243
1244 1244 N = None
1245 1245 ippSeconds = None
1246 1246
1247 1247 def __init__(self, **kwargs):
1248 1248 Figure.__init__(self, **kwargs)
1249 1249 self.isConfig = False
1250 1250 self.__nsubplots = 1
1251 1251
1252 1252 self.PLOT_CODE = SPECFIT_CODE
1253 1253
1254 1254 self.WIDTH = 450
1255 1255 self.HEIGHT = 250
1256 1256 self.WIDTHPROF = 0
1257 1257 self.HEIGHTPROF = 0
1258 1258
1259 1259 def getSubplots(self):
1260 1260
1261 1261 ncol = int(numpy.sqrt(self.nplots)+0.9)
1262 1262 nrow = int(self.nplots*1./ncol + 0.9)
1263 1263
1264 1264 return nrow, ncol
1265 1265
1266 1266 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
1267 1267
1268 1268 showprofile = False
1269 1269 self.__showprofile = showprofile
1270 1270 self.nplots = nplots
1271 1271
1272 1272 ncolspan = 5
1273 1273 colspan = 4
1274 1274 if showprofile:
1275 1275 ncolspan = 5
1276 1276 colspan = 4
1277 1277 self.__nsubplots = 2
1278 1278
1279 1279 self.createFigure(id = id,
1280 1280 wintitle = wintitle,
1281 1281 widthplot = self.WIDTH + self.WIDTHPROF,
1282 1282 heightplot = self.HEIGHT + self.HEIGHTPROF,
1283 1283 show=show)
1284 1284
1285 1285 nrow, ncol = self.getSubplots()
1286 1286
1287 1287 counter = 0
1288 1288 for y in range(nrow):
1289 1289 for x in range(ncol):
1290 1290
1291 1291 if counter >= self.nplots:
1292 1292 break
1293 1293
1294 1294 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1295 1295
1296 1296 if showprofile:
1297 1297 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1298 1298
1299 1299 counter += 1
1300 1300
1301 1301 def run(self, dataOut, id, cutHeight=None, fit=False, wintitle="", channelList=None, showprofile=True,
1302 1302 xmin=None, xmax=None, ymin=None, ymax=None,
1303 1303 save=False, figpath='./', figfile=None, show=True):
1304 1304
1305 1305 """
1306 1306
1307 1307 Input:
1308 1308 dataOut :
1309 1309 id :
1310 1310 wintitle :
1311 1311 channelList :
1312 1312 showProfile :
1313 1313 xmin : None,
1314 1314 xmax : None,
1315 1315 zmin : None,
1316 1316 zmax : None
1317 1317 """
1318 1318
1319 1319 if cutHeight==None:
1320 1320 h=270
1321 1321 heightindex = numpy.abs(cutHeight - dataOut.heightList).argmin()
1322 1322 cutHeight = dataOut.heightList[heightindex]
1323 1323
1324 1324 factor = dataOut.normFactor
1325 1325 x = dataOut.abscissaList[:-1]
1326 1326 #y = dataOut.getHeiRange()
1327 1327
1328 1328 z = dataOut.data_pre[:,:,heightindex]/factor
1329 1329 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1330 1330 avg = numpy.average(z, axis=1)
1331 1331 listChannels = z.shape[0]
1332 1332
1333 1333 #Reconstruct Function
1334 1334 if fit==True:
1335 1335 groupArray = dataOut.groupList
1336 1336 listChannels = groupArray.reshape((groupArray.size))
1337 1337 listChannels.sort()
1338 1338 spcFitLine = numpy.zeros(z.shape)
1339 1339 constants = dataOut.constants
1340 1340
1341 1341 nGroups = groupArray.shape[0]
1342 1342 nChannels = groupArray.shape[1]
1343 1343 nProfiles = z.shape[1]
1344 1344
1345 1345 for f in range(nGroups):
1346 1346 groupChann = groupArray[f,:]
1347 1347 p = dataOut.data_param[f,:,heightindex]
1348 1348 # p = numpy.array([ 89.343967,0.14036615,0.17086219,18.89835291,1.58388365,1.55099167])
1349 1349 fitLineAux = dataOut.library.modelFunction(p, constants)*nProfiles
1350 1350 fitLineAux = fitLineAux.reshape((nChannels,nProfiles))
1351 1351 spcFitLine[groupChann,:] = fitLineAux
1352 1352 # spcFitLine = spcFitLine/factor
1353 1353
1354 1354 z = z[listChannels,:]
1355 1355 spcFitLine = spcFitLine[listChannels,:]
1356 1356 spcFitLinedB = 10*numpy.log10(spcFitLine)
1357 1357
1358 1358 zdB = 10*numpy.log10(z)
1359 1359 #thisDatetime = dataOut.datatime
1360 1360 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1361 1361 title = wintitle + " Doppler Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1362 1362 xlabel = "Velocity (m/s)"
1363 1363 ylabel = "Spectrum"
1364 1364
1365 1365 if not self.isConfig:
1366 1366
1367 1367 nplots = listChannels.size
1368 1368
1369 1369 self.setup(id=id,
1370 1370 nplots=nplots,
1371 1371 wintitle=wintitle,
1372 1372 showprofile=showprofile,
1373 1373 show=show)
1374 1374
1375 1375 if xmin == None: xmin = numpy.nanmin(x)
1376 1376 if xmax == None: xmax = numpy.nanmax(x)
1377 1377 if ymin == None: ymin = numpy.nanmin(zdB)
1378 1378 if ymax == None: ymax = numpy.nanmax(zdB)+2
1379 1379
1380 1380 self.isConfig = True
1381 1381
1382 1382 self.setWinTitle(title)
1383 1383 for i in range(self.nplots):
1384 1384 # title = "Channel %d: %4.2fdB" %(dataOut.channelList[i]+1, noisedB[i])
1385 1385 title = "Height %4.1f km\nChannel %d:" %(cutHeight, listChannels[i])
1386 1386 axes = self.axesList[i*self.__nsubplots]
1387 1387 if fit == False:
1388 1388 axes.pline(x, zdB[i,:],
1389 1389 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1390 1390 xlabel=xlabel, ylabel=ylabel, title=title
1391 1391 )
1392 1392 if fit == True:
1393 1393 fitline=spcFitLinedB[i,:]
1394 1394 y=numpy.vstack([zdB[i,:],fitline] )
1395 1395 legendlabels=['Data','Fitting']
1396 1396 axes.pmultilineyaxis(x, y,
1397 1397 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1398 1398 xlabel=xlabel, ylabel=ylabel, title=title,
1399 1399 legendlabels=legendlabels, marker=None,
1400 1400 linestyle='solid', grid='both')
1401 1401
1402 1402 self.draw()
1403 1403
1404 1404 self.save(figpath=figpath,
1405 1405 figfile=figfile,
1406 1406 save=save,
1407 1407 ftp=ftp,
1408 1408 wr_period=wr_period,
1409 1409 thisDatetime=thisDatetime)
1410 1410
1411 1411
1412 1412 class EWDriftsPlot(Figure):
1413 1413
1414 1414 __isConfig = None
1415 1415 __nsubplots = None
1416 1416
1417 1417 WIDTHPROF = None
1418 1418 HEIGHTPROF = None
1419 1419 PREFIX = 'drift'
1420 1420
1421 1421 def __init__(self, **kwargs):
1422 1422 Figure.__init__(self, **kwargs)
1423 1423 self.timerange = 2*60*60
1424 1424 self.isConfig = False
1425 1425 self.__nsubplots = 1
1426 1426
1427 1427 self.WIDTH = 800
1428 1428 self.HEIGHT = 150
1429 1429 self.WIDTHPROF = 120
1430 1430 self.HEIGHTPROF = 0
1431 1431 self.counter_imagwr = 0
1432 1432
1433 1433 self.PLOT_CODE = EWDRIFT_CODE
1434 1434
1435 1435 self.FTP_WEI = None
1436 1436 self.EXP_CODE = None
1437 1437 self.SUB_EXP_CODE = None
1438 1438 self.PLOT_POS = None
1439 1439 self.tmin = None
1440 1440 self.tmax = None
1441 1441
1442 1442 self.xmin = None
1443 1443 self.xmax = None
1444 1444
1445 1445 self.figfile = None
1446 1446
1447 1447 def getSubplots(self):
1448 1448
1449 1449 ncol = 1
1450 1450 nrow = self.nplots
1451 1451
1452 1452 return nrow, ncol
1453 1453
1454 1454 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1455 1455
1456 1456 self.__showprofile = showprofile
1457 1457 self.nplots = nplots
1458 1458
1459 1459 ncolspan = 1
1460 1460 colspan = 1
1461 1461
1462 1462 self.createFigure(id = id,
1463 1463 wintitle = wintitle,
1464 1464 widthplot = self.WIDTH + self.WIDTHPROF,
1465 1465 heightplot = self.HEIGHT + self.HEIGHTPROF,
1466 1466 show=show)
1467 1467
1468 1468 nrow, ncol = self.getSubplots()
1469 1469
1470 1470 counter = 0
1471 1471 for y in range(nrow):
1472 1472 if counter >= self.nplots:
1473 1473 break
1474 1474
1475 1475 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
1476 1476 counter += 1
1477 1477
1478 1478 def run(self, dataOut, id, wintitle="", channelList=None,
1479 1479 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
1480 1480 zmaxVertical = None, zminVertical = None, zmaxZonal = None, zminZonal = None,
1481 1481 timerange=None, SNRthresh = -numpy.inf, SNRmin = None, SNRmax = None, SNR_1 = False,
1482 1482 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1483 1483 server=None, folder=None, username=None, password=None,
1484 1484 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1485 1485 """
1486 1486
1487 1487 Input:
1488 1488 dataOut :
1489 1489 id :
1490 1490 wintitle :
1491 1491 channelList :
1492 1492 showProfile :
1493 1493 xmin : None,
1494 1494 xmax : None,
1495 1495 ymin : None,
1496 1496 ymax : None,
1497 1497 zmin : None,
1498 1498 zmax : None
1499 1499 """
1500 1500
1501 1501 if timerange is not None:
1502 1502 self.timerange = timerange
1503 1503
1504 1504 tmin = None
1505 1505 tmax = None
1506 1506
1507 1507 x = dataOut.getTimeRange1(dataOut.outputInterval)
1508 1508 # y = dataOut.heightList
1509 1509 y = dataOut.heightList
1510 1510
1511 1511 z = dataOut.data_output
1512 1512 nplots = z.shape[0] #Number of wind dimensions estimated
1513 1513 nplotsw = nplots
1514 1514
1515 1515 #If there is a SNR function defined
1516 1516 if dataOut.data_SNR is not None:
1517 1517 nplots += 1
1518 1518 SNR = dataOut.data_SNR
1519 1519
1520 1520 if SNR_1:
1521 1521 SNR += 1
1522 1522
1523 1523 SNRavg = numpy.average(SNR, axis=0)
1524 1524
1525 1525 SNRdB = 10*numpy.log10(SNR)
1526 1526 SNRavgdB = 10*numpy.log10(SNRavg)
1527 1527
1528 1528 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
1529 1529
1530 1530 for i in range(nplotsw):
1531 1531 z[i,ind] = numpy.nan
1532 1532
1533 1533
1534 1534 showprofile = False
1535 1535 # thisDatetime = dataOut.datatime
1536 1536 thisDatetime = datetime.datetime.utcfromtimestamp(x[1])
1537 1537 title = wintitle + " EW Drifts"
1538 1538 xlabel = ""
1539 1539 ylabel = "Height (Km)"
1540 1540
1541 1541 if not self.isConfig:
1542 1542
1543 1543 self.setup(id=id,
1544 1544 nplots=nplots,
1545 1545 wintitle=wintitle,
1546 1546 showprofile=showprofile,
1547 1547 show=show)
1548 1548
1549 1549 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1550 1550
1551 1551 if ymin == None: ymin = numpy.nanmin(y)
1552 1552 if ymax == None: ymax = numpy.nanmax(y)
1553 1553
1554 1554 if zmaxZonal == None: zmaxZonal = numpy.nanmax(abs(z[0,:]))
1555 1555 if zminZonal == None: zminZonal = -zmaxZonal
1556 1556 if zmaxVertical == None: zmaxVertical = numpy.nanmax(abs(z[1,:]))
1557 1557 if zminVertical == None: zminVertical = -zmaxVertical
1558 1558
1559 1559 if dataOut.data_SNR is not None:
1560 1560 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
1561 1561 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
1562 1562
1563 1563 self.FTP_WEI = ftp_wei
1564 1564 self.EXP_CODE = exp_code
1565 1565 self.SUB_EXP_CODE = sub_exp_code
1566 1566 self.PLOT_POS = plot_pos
1567 1567
1568 1568 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1569 1569 self.isConfig = True
1570 1570
1571 1571
1572 1572 self.setWinTitle(title)
1573 1573
1574 1574 if ((self.xmax - x[1]) < (x[1]-x[0])):
1575 1575 x[1] = self.xmax
1576 1576
1577 1577 strWind = ['Zonal','Vertical']
1578 1578 strCb = 'Velocity (m/s)'
1579 1579 zmaxVector = [zmaxZonal, zmaxVertical]
1580 1580 zminVector = [zminZonal, zminVertical]
1581 1581
1582 1582 for i in range(nplotsw):
1583 1583
1584 1584 title = "%s Drifts: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1585 1585 axes = self.axesList[i*self.__nsubplots]
1586 1586
1587 1587 z1 = z[i,:].reshape((1,-1))
1588 1588
1589 1589 axes.pcolorbuffer(x, y, z1,
1590 1590 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
1591 1591 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1592 1592 ticksize=9, cblabel=strCb, cbsize="1%", colormap="RdBu_r")
1593 1593
1594 1594 if dataOut.data_SNR is not None:
1595 1595 i += 1
1596 1596 if SNR_1:
1597 1597 title = "Signal Noise Ratio + 1 (SNR+1): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1598 1598 else:
1599 1599 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1600 1600 axes = self.axesList[i*self.__nsubplots]
1601 1601 SNRavgdB = SNRavgdB.reshape((1,-1))
1602 1602
1603 1603 axes.pcolorbuffer(x, y, SNRavgdB,
1604 1604 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1605 1605 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1606 1606 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
1607 1607
1608 1608 self.draw()
1609 1609
1610 1610 if x[1] >= self.axesList[0].xmax:
1611 1611 self.counter_imagwr = wr_period
1612 1612 self.isConfig = False
1613 1613 self.figfile = None
1614 1614
1615 1615
1616 1616
1617 1617
1618 1618 class PhasePlot(Figure):
1619 1619
1620 1620 __isConfig = None
1621 1621 __nsubplots = None
1622 1622
1623 1623 PREFIX = 'mphase'
1624 1624
1625 1625
1626 1626 def __init__(self, **kwargs):
1627 1627 Figure.__init__(self, **kwargs)
1628 1628 self.timerange = 24*60*60
1629 1629 self.isConfig = False
1630 1630 self.__nsubplots = 1
1631 1631 self.counter_imagwr = 0
1632 1632 self.WIDTH = 600
1633 1633 self.HEIGHT = 300
1634 1634 self.WIDTHPROF = 120
1635 1635 self.HEIGHTPROF = 0
1636 1636 self.xdata = None
1637 1637 self.ydata = None
1638 1638
1639 1639 self.PLOT_CODE = MPHASE_CODE
1640 1640
1641 1641 self.FTP_WEI = None
1642 1642 self.EXP_CODE = None
1643 1643 self.SUB_EXP_CODE = None
1644 1644 self.PLOT_POS = None
1645 1645
1646 1646
1647 1647 self.filename_phase = None
1648 1648
1649 1649 self.figfile = None
1650 1650
1651 1651 def getSubplots(self):
1652 1652
1653 1653 ncol = 1
1654 1654 nrow = 1
1655 1655
1656 1656 return nrow, ncol
1657 1657
1658 1658 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1659 1659
1660 1660 self.__showprofile = showprofile
1661 1661 self.nplots = nplots
1662 1662
1663 1663 ncolspan = 7
1664 1664 colspan = 6
1665 1665 self.__nsubplots = 2
1666 1666
1667 1667 self.createFigure(id = id,
1668 1668 wintitle = wintitle,
1669 1669 widthplot = self.WIDTH+self.WIDTHPROF,
1670 1670 heightplot = self.HEIGHT+self.HEIGHTPROF,
1671 1671 show=show)
1672 1672
1673 1673 nrow, ncol = self.getSubplots()
1674 1674
1675 1675 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1676 1676
1677 1677
1678 1678 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1679 1679 xmin=None, xmax=None, ymin=None, ymax=None,
1680 1680 timerange=None,
1681 1681 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1682 1682 server=None, folder=None, username=None, password=None,
1683 1683 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1684 1684
1685 1685
1686 1686 tmin = None
1687 1687 tmax = None
1688 1688 x = dataOut.getTimeRange1(dataOut.outputInterval)
1689 1689 y = dataOut.getHeiRange()
1690 1690
1691 1691
1692 1692 #thisDatetime = dataOut.datatime
1693 1693 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1694 1694 title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1695 1695 xlabel = "Local Time"
1696 1696 ylabel = "Phase"
1697 1697
1698 1698
1699 1699 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1700 1700 phase_beacon = dataOut.data_output
1701 1701 update_figfile = False
1702 1702
1703 1703 if not self.isConfig:
1704 1704
1705 1705 self.nplots = phase_beacon.size
1706 1706
1707 1707 self.setup(id=id,
1708 1708 nplots=self.nplots,
1709 1709 wintitle=wintitle,
1710 1710 showprofile=showprofile,
1711 1711 show=show)
1712 1712
1713 1713 if timerange is not None:
1714 1714 self.timerange = timerange
1715 1715
1716 1716 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1717 1717
1718 1718 if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0
1719 1719 if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0
1720 1720
1721 1721 self.FTP_WEI = ftp_wei
1722 1722 self.EXP_CODE = exp_code
1723 1723 self.SUB_EXP_CODE = sub_exp_code
1724 1724 self.PLOT_POS = plot_pos
1725 1725
1726 1726 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1727 1727 self.isConfig = True
1728 1728 self.figfile = figfile
1729 1729 self.xdata = numpy.array([])
1730 1730 self.ydata = numpy.array([])
1731 1731
1732 1732 #open file beacon phase
1733 1733 path = '%s%03d' %(self.PREFIX, self.id)
1734 1734 beacon_file = os.path.join(path,'%s.txt'%self.name)
1735 1735 self.filename_phase = os.path.join(figpath,beacon_file)
1736 1736 update_figfile = True
1737 1737
1738 1738
1739 1739 #store data beacon phase
1740 1740 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1741 1741
1742 1742 self.setWinTitle(title)
1743 1743
1744 1744
1745 1745 title = "Phase Offset %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1746 1746
1747 1747 legendlabels = ["phase %d"%(chan) for chan in numpy.arange(self.nplots)]
1748 1748
1749 1749 axes = self.axesList[0]
1750 1750
1751 1751 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1752 1752
1753 1753 if len(self.ydata)==0:
1754 1754 self.ydata = phase_beacon.reshape(-1,1)
1755 1755 else:
1756 1756 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1757 1757
1758 1758
1759 1759 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1760 1760 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1761 1761 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1762 1762 XAxisAsTime=True, grid='both'
1763 1763 )
1764 1764
1765 1765 self.draw()
1766 1766
1767 1767 self.save(figpath=figpath,
1768 1768 figfile=figfile,
1769 1769 save=save,
1770 1770 ftp=ftp,
1771 1771 wr_period=wr_period,
1772 1772 thisDatetime=thisDatetime,
1773 1773 update_figfile=update_figfile)
1774 1774
1775 1775 if dataOut.ltctime + dataOut.outputInterval >= self.xmax:
1776 1776 self.counter_imagwr = wr_period
1777 1777 self.isConfig = False
1778 1778 update_figfile = True
1779 1779
1780 1780
1781 1781
1782 1782 class NSMeteorDetection1Plot(Figure):
1783 1783
1784 1784 isConfig = None
1785 1785 __nsubplots = None
1786 1786
1787 1787 WIDTHPROF = None
1788 1788 HEIGHTPROF = None
1789 1789 PREFIX = 'nsm'
1790 1790
1791 1791 zminList = None
1792 1792 zmaxList = None
1793 1793 cmapList = None
1794 1794 titleList = None
1795 1795 nPairs = None
1796 1796 nChannels = None
1797 1797 nParam = None
1798 1798
1799 1799 def __init__(self, **kwargs):
1800 1800 Figure.__init__(self, **kwargs)
1801 1801 self.isConfig = False
1802 1802 self.__nsubplots = 1
1803 1803
1804 1804 self.WIDTH = 750
1805 1805 self.HEIGHT = 250
1806 1806 self.WIDTHPROF = 120
1807 1807 self.HEIGHTPROF = 0
1808 1808 self.counter_imagwr = 0
1809 1809
1810 1810 self.PLOT_CODE = SPEC_CODE
1811 1811
1812 1812 self.FTP_WEI = None
1813 1813 self.EXP_CODE = None
1814 1814 self.SUB_EXP_CODE = None
1815 1815 self.PLOT_POS = None
1816 1816
1817 1817 self.__xfilter_ena = False
1818 1818 self.__yfilter_ena = False
1819 1819
1820 1820 def getSubplots(self):
1821 1821
1822 1822 ncol = 3
1823 1823 nrow = int(numpy.ceil(self.nplots/3.0))
1824 1824
1825 1825 return nrow, ncol
1826 1826
1827 1827 def setup(self, id, nplots, wintitle, show=True):
1828 1828
1829 1829 self.nplots = nplots
1830 1830
1831 1831 ncolspan = 1
1832 1832 colspan = 1
1833 1833
1834 1834 self.createFigure(id = id,
1835 1835 wintitle = wintitle,
1836 1836 widthplot = self.WIDTH + self.WIDTHPROF,
1837 1837 heightplot = self.HEIGHT + self.HEIGHTPROF,
1838 1838 show=show)
1839 1839
1840 1840 nrow, ncol = self.getSubplots()
1841 1841
1842 1842 counter = 0
1843 1843 for y in range(nrow):
1844 1844 for x in range(ncol):
1845 1845
1846 1846 if counter >= self.nplots:
1847 1847 break
1848 1848
1849 1849 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1850 1850
1851 1851 counter += 1
1852 1852
1853 1853 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
1854 1854 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
1855 1855 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
1856 1856 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1857 1857 server=None, folder=None, username=None, password=None,
1858 1858 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
1859 1859 xaxis="frequency"):
1860 1860
1861 1861 """
1862 1862
1863 1863 Input:
1864 1864 dataOut :
1865 1865 id :
1866 1866 wintitle :
1867 1867 channelList :
1868 1868 showProfile :
1869 1869 xmin : None,
1870 1870 xmax : None,
1871 1871 ymin : None,
1872 1872 ymax : None,
1873 1873 zmin : None,
1874 1874 zmax : None
1875 1875 """
1876 1876 #SEPARAR EN DOS PLOTS
1877 1877 nParam = dataOut.data_param.shape[1] - 3
1878 1878
1879 1879 utctime = dataOut.data_param[0,0]
1880 1880 tmet = dataOut.data_param[:,1].astype(int)
1881 1881 hmet = dataOut.data_param[:,2].astype(int)
1882 1882
1883 1883 x = dataOut.abscissaList
1884 1884 y = dataOut.heightList
1885 1885
1886 1886 z = numpy.zeros((nParam, y.size, x.size - 1))
1887 1887 z[:,:] = numpy.nan
1888 1888 z[:,hmet,tmet] = dataOut.data_param[:,3:].T
1889 1889 z[0,:,:] = 10*numpy.log10(z[0,:,:])
1890 1890
1891 1891 xlabel = "Time (s)"
1892 1892 ylabel = "Range (km)"
1893 1893
1894 1894 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1895 1895
1896 1896 if not self.isConfig:
1897 1897
1898 1898 nplots = nParam
1899 1899
1900 1900 self.setup(id=id,
1901 1901 nplots=nplots,
1902 1902 wintitle=wintitle,
1903 1903 show=show)
1904 1904
1905 1905 if xmin is None: xmin = numpy.nanmin(x)
1906 1906 if xmax is None: xmax = numpy.nanmax(x)
1907 1907 if ymin is None: ymin = numpy.nanmin(y)
1908 1908 if ymax is None: ymax = numpy.nanmax(y)
1909 1909 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
1910 1910 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
1911 1911 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
1912 1912 if vmin is None: vmin = -vmax
1913 1913 if wmin is None: wmin = 0
1914 1914 if wmax is None: wmax = 50
1915 1915
1916 1916 pairsList = dataOut.groupList
1917 1917 self.nPairs = len(dataOut.groupList)
1918 1918
1919 1919 zminList = [SNRmin, vmin, cmin] + [pmin]*self.nPairs
1920 1920 zmaxList = [SNRmax, vmax, cmax] + [pmax]*self.nPairs
1921 1921 titleList = ["SNR","Radial Velocity","Coherence"]
1922 1922 cmapList = ["jet","RdBu_r","jet"]
1923 1923
1924 1924 for i in range(self.nPairs):
1925 1925 strAux1 = "Phase Difference "+ str(pairsList[i][0]) + str(pairsList[i][1])
1926 1926 titleList = titleList + [strAux1]
1927 1927 cmapList = cmapList + ["RdBu_r"]
1928 1928
1929 1929 self.zminList = zminList
1930 1930 self.zmaxList = zmaxList
1931 1931 self.cmapList = cmapList
1932 1932 self.titleList = titleList
1933 1933
1934 1934 self.FTP_WEI = ftp_wei
1935 1935 self.EXP_CODE = exp_code
1936 1936 self.SUB_EXP_CODE = sub_exp_code
1937 1937 self.PLOT_POS = plot_pos
1938 1938
1939 1939 self.isConfig = True
1940 1940
1941 1941 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
1942 1942
1943 1943 for i in range(nParam):
1944 1944 title = self.titleList[i] + ": " +str_datetime
1945 1945 axes = self.axesList[i]
1946 1946 axes.pcolor(x, y, z[i,:].T,
1947 1947 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
1948 1948 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
1949 1949 self.draw()
1950 1950
1951 1951 if figfile == None:
1952 1952 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1953 1953 name = str_datetime
1954 1954 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1955 1955 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
1956 1956 figfile = self.getFilename(name)
1957 1957
1958 1958 self.save(figpath=figpath,
1959 1959 figfile=figfile,
1960 1960 save=save,
1961 1961 ftp=ftp,
1962 1962 wr_period=wr_period,
1963 1963 thisDatetime=thisDatetime)
1964 1964
1965 1965
1966 1966 class NSMeteorDetection2Plot(Figure):
1967 1967
1968 1968 isConfig = None
1969 1969 __nsubplots = None
1970 1970
1971 1971 WIDTHPROF = None
1972 1972 HEIGHTPROF = None
1973 1973 PREFIX = 'nsm'
1974 1974
1975 1975 zminList = None
1976 1976 zmaxList = None
1977 1977 cmapList = None
1978 1978 titleList = None
1979 1979 nPairs = None
1980 1980 nChannels = None
1981 1981 nParam = None
1982 1982
1983 1983 def __init__(self, **kwargs):
1984 1984 Figure.__init__(self, **kwargs)
1985 1985 self.isConfig = False
1986 1986 self.__nsubplots = 1
1987 1987
1988 1988 self.WIDTH = 750
1989 1989 self.HEIGHT = 250
1990 1990 self.WIDTHPROF = 120
1991 1991 self.HEIGHTPROF = 0
1992 1992 self.counter_imagwr = 0
1993 1993
1994 1994 self.PLOT_CODE = SPEC_CODE
1995 1995
1996 1996 self.FTP_WEI = None
1997 1997 self.EXP_CODE = None
1998 1998 self.SUB_EXP_CODE = None
1999 1999 self.PLOT_POS = None
2000 2000
2001 2001 self.__xfilter_ena = False
2002 2002 self.__yfilter_ena = False
2003 2003
2004 2004 def getSubplots(self):
2005 2005
2006 2006 ncol = 3
2007 2007 nrow = int(numpy.ceil(self.nplots/3.0))
2008 2008
2009 2009 return nrow, ncol
2010 2010
2011 2011 def setup(self, id, nplots, wintitle, show=True):
2012 2012
2013 2013 self.nplots = nplots
2014 2014
2015 2015 ncolspan = 1
2016 2016 colspan = 1
2017 2017
2018 2018 self.createFigure(id = id,
2019 2019 wintitle = wintitle,
2020 2020 widthplot = self.WIDTH + self.WIDTHPROF,
2021 2021 heightplot = self.HEIGHT + self.HEIGHTPROF,
2022 2022 show=show)
2023 2023
2024 2024 nrow, ncol = self.getSubplots()
2025 2025
2026 2026 counter = 0
2027 2027 for y in range(nrow):
2028 2028 for x in range(ncol):
2029 2029
2030 2030 if counter >= self.nplots:
2031 2031 break
2032 2032
2033 2033 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
2034 2034
2035 2035 counter += 1
2036 2036
2037 2037 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
2038 2038 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
2039 2039 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
2040 2040 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
2041 2041 server=None, folder=None, username=None, password=None,
2042 2042 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
2043 2043 xaxis="frequency"):
2044 2044
2045 2045 """
2046 2046
2047 2047 Input:
2048 2048 dataOut :
2049 2049 id :
2050 2050 wintitle :
2051 2051 channelList :
2052 2052 showProfile :
2053 2053 xmin : None,
2054 2054 xmax : None,
2055 2055 ymin : None,
2056 2056 ymax : None,
2057 2057 zmin : None,
2058 2058 zmax : None
2059 2059 """
2060 2060 #Rebuild matrix
2061 2061 utctime = dataOut.data_param[0,0]
2062 2062 cmet = dataOut.data_param[:,1].astype(int)
2063 2063 tmet = dataOut.data_param[:,2].astype(int)
2064 2064 hmet = dataOut.data_param[:,3].astype(int)
2065 2065
2066 2066 nParam = 3
2067 2067 nChan = len(dataOut.groupList)
2068 2068 x = dataOut.abscissaList
2069 2069 y = dataOut.heightList
2070 2070
2071 2071 z = numpy.full((nChan, nParam, y.size, x.size - 1),numpy.nan)
2072 2072 z[cmet,:,hmet,tmet] = dataOut.data_param[:,4:]
2073 2073 z[:,0,:,:] = 10*numpy.log10(z[:,0,:,:]) #logarithmic scale
2074 2074 z = numpy.reshape(z, (nChan*nParam, y.size, x.size-1))
2075 2075
2076 2076 xlabel = "Time (s)"
2077 2077 ylabel = "Range (km)"
2078 2078
2079 2079 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
2080 2080
2081 2081 if not self.isConfig:
2082 2082
2083 2083 nplots = nParam*nChan
2084 2084
2085 2085 self.setup(id=id,
2086 2086 nplots=nplots,
2087 2087 wintitle=wintitle,
2088 2088 show=show)
2089 2089
2090 2090 if xmin is None: xmin = numpy.nanmin(x)
2091 2091 if xmax is None: xmax = numpy.nanmax(x)
2092 2092 if ymin is None: ymin = numpy.nanmin(y)
2093 2093 if ymax is None: ymax = numpy.nanmax(y)
2094 2094 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
2095 2095 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
2096 2096 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
2097 2097 if vmin is None: vmin = -vmax
2098 2098 if wmin is None: wmin = 0
2099 2099 if wmax is None: wmax = 50
2100 2100
2101 2101 self.nChannels = nChan
2102 2102
2103 2103 zminList = []
2104 2104 zmaxList = []
2105 2105 titleList = []
2106 2106 cmapList = []
2107 2107 for i in range(self.nChannels):
2108 2108 strAux1 = "SNR Channel "+ str(i)
2109 2109 strAux2 = "Radial Velocity Channel "+ str(i)
2110 2110 strAux3 = "Spectral Width Channel "+ str(i)
2111 2111
2112 2112 titleList = titleList + [strAux1,strAux2,strAux3]
2113 2113 cmapList = cmapList + ["jet","RdBu_r","jet"]
2114 2114 zminList = zminList + [SNRmin,vmin,wmin]
2115 2115 zmaxList = zmaxList + [SNRmax,vmax,wmax]
2116 2116
2117 2117 self.zminList = zminList
2118 2118 self.zmaxList = zmaxList
2119 2119 self.cmapList = cmapList
2120 2120 self.titleList = titleList
2121 2121
2122 2122 self.FTP_WEI = ftp_wei
2123 2123 self.EXP_CODE = exp_code
2124 2124 self.SUB_EXP_CODE = sub_exp_code
2125 2125 self.PLOT_POS = plot_pos
2126 2126
2127 2127 self.isConfig = True
2128 2128
2129 2129 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
2130 2130
2131 2131 for i in range(self.nplots):
2132 2132 title = self.titleList[i] + ": " +str_datetime
2133 2133 axes = self.axesList[i]
2134 2134 axes.pcolor(x, y, z[i,:].T,
2135 2135 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
2136 2136 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
2137 2137 self.draw()
2138 2138
2139 2139 if figfile == None:
2140 2140 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
2141 2141 name = str_datetime
2142 2142 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
2143 2143 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
2144 2144 figfile = self.getFilename(name)
2145 2145
2146 2146 self.save(figpath=figpath,
2147 2147 figfile=figfile,
2148 2148 save=save,
2149 2149 ftp=ftp,
2150 2150 wr_period=wr_period,
2151 thisDatetime=thisDatetime)
2151 thisDatetime=thisDatetime) No newline at end of file
@@ -1,1542 +1,1542
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 from figure import Figure, isRealtime, isTimeInHourRange
11 from plotting_codes import *
10 from .figure import Figure, isRealtime, isTimeInHourRange
11 from .plotting_codes import *
12 12
13 13
14 14 class SpectraPlot(Figure):
15 15
16 16 isConfig = None
17 17 __nsubplots = None
18 18
19 19 WIDTHPROF = None
20 20 HEIGHTPROF = None
21 21 PREFIX = 'spc'
22 22
23 23 def __init__(self, **kwargs):
24 24 Figure.__init__(self, **kwargs)
25 25 self.isConfig = False
26 26 self.__nsubplots = 1
27 27
28 28 self.WIDTH = 250
29 29 self.HEIGHT = 250
30 30 self.WIDTHPROF = 120
31 31 self.HEIGHTPROF = 0
32 32 self.counter_imagwr = 0
33 33
34 34 self.PLOT_CODE = SPEC_CODE
35 35
36 36 self.FTP_WEI = None
37 37 self.EXP_CODE = None
38 38 self.SUB_EXP_CODE = None
39 39 self.PLOT_POS = None
40 40
41 41 self.__xfilter_ena = False
42 42 self.__yfilter_ena = False
43 43
44 44 def getSubplots(self):
45 45
46 46 ncol = int(numpy.sqrt(self.nplots)+0.9)
47 47 nrow = int(self.nplots*1./ncol + 0.9)
48 48
49 49 return nrow, ncol
50 50
51 51 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
52 52
53 53 self.__showprofile = showprofile
54 54 self.nplots = nplots
55 55
56 56 ncolspan = 1
57 57 colspan = 1
58 58 if showprofile:
59 59 ncolspan = 3
60 60 colspan = 2
61 61 self.__nsubplots = 2
62 62
63 63 self.createFigure(id = id,
64 64 wintitle = wintitle,
65 65 widthplot = self.WIDTH + self.WIDTHPROF,
66 66 heightplot = self.HEIGHT + self.HEIGHTPROF,
67 67 show=show)
68 68
69 69 nrow, ncol = self.getSubplots()
70 70
71 71 counter = 0
72 72 for y in range(nrow):
73 73 for x in range(ncol):
74 74
75 75 if counter >= self.nplots:
76 76 break
77 77
78 78 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
79 79
80 80 if showprofile:
81 81 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
82 82
83 83 counter += 1
84 84
85 85 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
86 86 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
87 87 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
88 88 server=None, folder=None, username=None, password=None,
89 89 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
90 90 xaxis="frequency", colormap='jet', normFactor=None):
91 91
92 92 """
93 93
94 94 Input:
95 95 dataOut :
96 96 id :
97 97 wintitle :
98 98 channelList :
99 99 showProfile :
100 100 xmin : None,
101 101 xmax : None,
102 102 ymin : None,
103 103 ymax : None,
104 104 zmin : None,
105 105 zmax : None
106 106 """
107 107 if realtime:
108 108 if not(isRealtime(utcdatatime = dataOut.utctime)):
109 print 'Skipping this plot function'
109 print('Skipping this plot function')
110 110 return
111 111
112 112 if channelList == None:
113 113 channelIndexList = dataOut.channelIndexList
114 114 else:
115 115 channelIndexList = []
116 116 for channel in channelList:
117 117 if channel not in dataOut.channelList:
118 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
118 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
119 119 channelIndexList.append(dataOut.channelList.index(channel))
120 120
121 121 if normFactor is None:
122 122 factor = dataOut.normFactor
123 123 else:
124 124 factor = normFactor
125 125 if xaxis == "frequency":
126 126 x = dataOut.getFreqRange(1)/1000.
127 127 xlabel = "Frequency (kHz)"
128 128
129 129 elif xaxis == "time":
130 130 x = dataOut.getAcfRange(1)
131 131 xlabel = "Time (ms)"
132 132
133 133 else:
134 134 x = dataOut.getVelRange(1)
135 135 xlabel = "Velocity (m/s)"
136 136
137 137 ylabel = "Range (Km)"
138 138
139 139 y = dataOut.getHeiRange()
140 140
141 141 z = dataOut.data_spc/factor
142 142 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
143 143 zdB = 10*numpy.log10(z)
144 144
145 145 avg = numpy.average(z, axis=1)
146 146 avgdB = 10*numpy.log10(avg)
147 147
148 148 noise = dataOut.getNoise()/factor
149 149 noisedB = 10*numpy.log10(noise)
150 150
151 151 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
152 152 title = wintitle + " Spectra"
153 153 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
154 154 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
155 155
156 156 if not self.isConfig:
157 157
158 158 nplots = len(channelIndexList)
159 159
160 160 self.setup(id=id,
161 161 nplots=nplots,
162 162 wintitle=wintitle,
163 163 showprofile=showprofile,
164 164 show=show)
165 165
166 166 if xmin == None: xmin = numpy.nanmin(x)
167 167 if xmax == None: xmax = numpy.nanmax(x)
168 168 if ymin == None: ymin = numpy.nanmin(y)
169 169 if ymax == None: ymax = numpy.nanmax(y)
170 170 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
171 171 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
172 172
173 173 self.FTP_WEI = ftp_wei
174 174 self.EXP_CODE = exp_code
175 175 self.SUB_EXP_CODE = sub_exp_code
176 176 self.PLOT_POS = plot_pos
177 177
178 178 self.isConfig = True
179 179
180 180 self.setWinTitle(title)
181 181
182 182 for i in range(self.nplots):
183 183 index = channelIndexList[i]
184 184 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
185 185 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
186 186 if len(dataOut.beam.codeList) != 0:
187 187 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
188 188
189 189 axes = self.axesList[i*self.__nsubplots]
190 190 axes.pcolor(x, y, zdB[index,:,:],
191 191 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
192 192 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
193 193 ticksize=9, cblabel='')
194 194
195 195 if self.__showprofile:
196 196 axes = self.axesList[i*self.__nsubplots +1]
197 197 axes.pline(avgdB[index,:], y,
198 198 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
199 199 xlabel='dB', ylabel='', title='',
200 200 ytick_visible=False,
201 201 grid='x')
202 202
203 203 noiseline = numpy.repeat(noisedB[index], len(y))
204 204 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
205 205
206 206 self.draw()
207 207
208 208 if figfile == None:
209 209 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
210 210 name = str_datetime
211 211 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
212 212 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
213 213 figfile = self.getFilename(name)
214 214
215 215 self.save(figpath=figpath,
216 216 figfile=figfile,
217 217 save=save,
218 218 ftp=ftp,
219 219 wr_period=wr_period,
220 220 thisDatetime=thisDatetime)
221 221
222 222 class CrossSpectraPlot(Figure):
223 223
224 224 isConfig = None
225 225 __nsubplots = None
226 226
227 227 WIDTH = None
228 228 HEIGHT = None
229 229 WIDTHPROF = None
230 230 HEIGHTPROF = None
231 231 PREFIX = 'cspc'
232 232
233 233 def __init__(self, **kwargs):
234 234 Figure.__init__(self, **kwargs)
235 235 self.isConfig = False
236 236 self.__nsubplots = 4
237 237 self.counter_imagwr = 0
238 238 self.WIDTH = 250
239 239 self.HEIGHT = 250
240 240 self.WIDTHPROF = 0
241 241 self.HEIGHTPROF = 0
242 242
243 243 self.PLOT_CODE = CROSS_CODE
244 244 self.FTP_WEI = None
245 245 self.EXP_CODE = None
246 246 self.SUB_EXP_CODE = None
247 247 self.PLOT_POS = None
248 248
249 249 def getSubplots(self):
250 250
251 251 ncol = 4
252 252 nrow = self.nplots
253 253
254 254 return nrow, ncol
255 255
256 256 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
257 257
258 258 self.__showprofile = showprofile
259 259 self.nplots = nplots
260 260
261 261 ncolspan = 1
262 262 colspan = 1
263 263
264 264 self.createFigure(id = id,
265 265 wintitle = wintitle,
266 266 widthplot = self.WIDTH + self.WIDTHPROF,
267 267 heightplot = self.HEIGHT + self.HEIGHTPROF,
268 268 show=True)
269 269
270 270 nrow, ncol = self.getSubplots()
271 271
272 272 counter = 0
273 273 for y in range(nrow):
274 274 for x in range(ncol):
275 275 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
276 276
277 277 counter += 1
278 278
279 279 def run(self, dataOut, id, wintitle="", pairsList=None,
280 280 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
281 281 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
282 282 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
283 283 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
284 284 server=None, folder=None, username=None, password=None,
285 285 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
286 286 xaxis='frequency'):
287 287
288 288 """
289 289
290 290 Input:
291 291 dataOut :
292 292 id :
293 293 wintitle :
294 294 channelList :
295 295 showProfile :
296 296 xmin : None,
297 297 xmax : None,
298 298 ymin : None,
299 299 ymax : None,
300 300 zmin : None,
301 301 zmax : None
302 302 """
303 303
304 304 if pairsList == None:
305 305 pairsIndexList = dataOut.pairsIndexList
306 306 else:
307 307 pairsIndexList = []
308 308 for pair in pairsList:
309 309 if pair not in dataOut.pairsList:
310 raise ValueError, "Pair %s is not in dataOut.pairsList" %str(pair)
310 raise ValueError("Pair %s is not in dataOut.pairsList" %str(pair))
311 311 pairsIndexList.append(dataOut.pairsList.index(pair))
312 312
313 313 if not pairsIndexList:
314 314 return
315 315
316 316 if len(pairsIndexList) > 4:
317 317 pairsIndexList = pairsIndexList[0:4]
318 318
319 319 if normFactor is None:
320 320 factor = dataOut.normFactor
321 321 else:
322 322 factor = normFactor
323 323 x = dataOut.getVelRange(1)
324 324 y = dataOut.getHeiRange()
325 325 z = dataOut.data_spc[:,:,:]/factor
326 326 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
327 327
328 328 noise = dataOut.noise/factor
329 329
330 330 zdB = 10*numpy.log10(z)
331 331 noisedB = 10*numpy.log10(noise)
332 332
333 333 if coh_min == None:
334 334 coh_min = 0.0
335 335 if coh_max == None:
336 336 coh_max = 1.0
337 337
338 338 if phase_min == None:
339 339 phase_min = -180
340 340 if phase_max == None:
341 341 phase_max = 180
342 342
343 343 #thisDatetime = dataOut.datatime
344 344 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
345 345 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
346 346 # xlabel = "Velocity (m/s)"
347 347 ylabel = "Range (Km)"
348 348
349 349 if xaxis == "frequency":
350 350 x = dataOut.getFreqRange(1)/1000.
351 351 xlabel = "Frequency (kHz)"
352 352
353 353 elif xaxis == "time":
354 354 x = dataOut.getAcfRange(1)
355 355 xlabel = "Time (ms)"
356 356
357 357 else:
358 358 x = dataOut.getVelRange(1)
359 359 xlabel = "Velocity (m/s)"
360 360
361 361 if not self.isConfig:
362 362
363 363 nplots = len(pairsIndexList)
364 364
365 365 self.setup(id=id,
366 366 nplots=nplots,
367 367 wintitle=wintitle,
368 368 showprofile=False,
369 369 show=show)
370 370
371 371 avg = numpy.abs(numpy.average(z, axis=1))
372 372 avgdB = 10*numpy.log10(avg)
373 373
374 374 if xmin == None: xmin = numpy.nanmin(x)
375 375 if xmax == None: xmax = numpy.nanmax(x)
376 376 if ymin == None: ymin = numpy.nanmin(y)
377 377 if ymax == None: ymax = numpy.nanmax(y)
378 378 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
379 379 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
380 380
381 381 self.FTP_WEI = ftp_wei
382 382 self.EXP_CODE = exp_code
383 383 self.SUB_EXP_CODE = sub_exp_code
384 384 self.PLOT_POS = plot_pos
385 385
386 386 self.isConfig = True
387 387
388 388 self.setWinTitle(title)
389 389
390 390 for i in range(self.nplots):
391 391 pair = dataOut.pairsList[pairsIndexList[i]]
392 392
393 393 chan_index0 = dataOut.channelList.index(pair[0])
394 394 chan_index1 = dataOut.channelList.index(pair[1])
395 395
396 396 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
397 397 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
398 398 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
399 399 axes0 = self.axesList[i*self.__nsubplots]
400 400 axes0.pcolor(x, y, zdB,
401 401 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
402 402 xlabel=xlabel, ylabel=ylabel, title=title,
403 403 ticksize=9, colormap=power_cmap, cblabel='')
404 404
405 405 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
406 406 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
407 407 axes0 = self.axesList[i*self.__nsubplots+1]
408 408 axes0.pcolor(x, y, zdB,
409 409 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
410 410 xlabel=xlabel, ylabel=ylabel, title=title,
411 411 ticksize=9, colormap=power_cmap, cblabel='')
412 412
413 413 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
414 414 coherence = numpy.abs(coherenceComplex)
415 415 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
416 416 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
417 417
418 418 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
419 419 axes0 = self.axesList[i*self.__nsubplots+2]
420 420 axes0.pcolor(x, y, coherence,
421 421 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
422 422 xlabel=xlabel, ylabel=ylabel, title=title,
423 423 ticksize=9, colormap=coherence_cmap, cblabel='')
424 424
425 425 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
426 426 axes0 = self.axesList[i*self.__nsubplots+3]
427 427 axes0.pcolor(x, y, phase,
428 428 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
429 429 xlabel=xlabel, ylabel=ylabel, title=title,
430 430 ticksize=9, colormap=phase_cmap, cblabel='')
431 431
432 432
433 433
434 434 self.draw()
435 435
436 436 self.save(figpath=figpath,
437 437 figfile=figfile,
438 438 save=save,
439 439 ftp=ftp,
440 440 wr_period=wr_period,
441 441 thisDatetime=thisDatetime)
442 442
443 443
444 444 class RTIPlot(Figure):
445 445
446 446 __isConfig = None
447 447 __nsubplots = None
448 448
449 449 WIDTHPROF = None
450 450 HEIGHTPROF = None
451 451 PREFIX = 'rti'
452 452
453 453 def __init__(self, **kwargs):
454 454
455 455 Figure.__init__(self, **kwargs)
456 456 self.timerange = None
457 457 self.isConfig = False
458 458 self.__nsubplots = 1
459 459
460 460 self.WIDTH = 800
461 461 self.HEIGHT = 180
462 462 self.WIDTHPROF = 120
463 463 self.HEIGHTPROF = 0
464 464 self.counter_imagwr = 0
465 465
466 466 self.PLOT_CODE = RTI_CODE
467 467
468 468 self.FTP_WEI = None
469 469 self.EXP_CODE = None
470 470 self.SUB_EXP_CODE = None
471 471 self.PLOT_POS = None
472 472 self.tmin = None
473 473 self.tmax = None
474 474
475 475 self.xmin = None
476 476 self.xmax = None
477 477
478 478 self.figfile = None
479 479
480 480 def getSubplots(self):
481 481
482 482 ncol = 1
483 483 nrow = self.nplots
484 484
485 485 return nrow, ncol
486 486
487 487 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
488 488
489 489 self.__showprofile = showprofile
490 490 self.nplots = nplots
491 491
492 492 ncolspan = 1
493 493 colspan = 1
494 494 if showprofile:
495 495 ncolspan = 7
496 496 colspan = 6
497 497 self.__nsubplots = 2
498 498
499 499 self.createFigure(id = id,
500 500 wintitle = wintitle,
501 501 widthplot = self.WIDTH + self.WIDTHPROF,
502 502 heightplot = self.HEIGHT + self.HEIGHTPROF,
503 503 show=show)
504 504
505 505 nrow, ncol = self.getSubplots()
506 506
507 507 counter = 0
508 508 for y in range(nrow):
509 509 for x in range(ncol):
510 510
511 511 if counter >= self.nplots:
512 512 break
513 513
514 514 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
515 515
516 516 if showprofile:
517 517 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
518 518
519 519 counter += 1
520 520
521 521 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
522 522 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
523 523 timerange=None, colormap='jet',
524 524 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
525 525 server=None, folder=None, username=None, password=None,
526 526 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
527 527
528 528 """
529 529
530 530 Input:
531 531 dataOut :
532 532 id :
533 533 wintitle :
534 534 channelList :
535 535 showProfile :
536 536 xmin : None,
537 537 xmax : None,
538 538 ymin : None,
539 539 ymax : None,
540 540 zmin : None,
541 541 zmax : None
542 542 """
543 543
544 544 #colormap = kwargs.get('colormap', 'jet')
545 545 if HEIGHT is not None:
546 546 self.HEIGHT = HEIGHT
547 547
548 548 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
549 549 return
550 550
551 551 if channelList == None:
552 552 channelIndexList = dataOut.channelIndexList
553 553 else:
554 554 channelIndexList = []
555 555 for channel in channelList:
556 556 if channel not in dataOut.channelList:
557 raise ValueError, "Channel %d is not in dataOut.channelList"
557 raise ValueError("Channel %d is not in dataOut.channelList")
558 558 channelIndexList.append(dataOut.channelList.index(channel))
559 559
560 560 if normFactor is None:
561 561 factor = dataOut.normFactor
562 562 else:
563 563 factor = normFactor
564 564
565 565 # factor = dataOut.normFactor
566 566 x = dataOut.getTimeRange()
567 567 y = dataOut.getHeiRange()
568 568
569 569 z = dataOut.data_spc/factor
570 570 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
571 571 avg = numpy.average(z, axis=1)
572 572 avgdB = 10.*numpy.log10(avg)
573 573 # avgdB = dataOut.getPower()
574 574
575 575
576 576 thisDatetime = dataOut.datatime
577 577 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
578 578 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
579 579 xlabel = ""
580 580 ylabel = "Range (Km)"
581 581
582 582 update_figfile = False
583 583
584 if dataOut.ltctime >= self.xmax:
584 if self.xmax is not None and dataOut.ltctime >= self.xmax: #yong
585 585 self.counter_imagwr = wr_period
586 586 self.isConfig = False
587 587 update_figfile = True
588 588
589 589 if not self.isConfig:
590 590
591 591 nplots = len(channelIndexList)
592 592
593 593 self.setup(id=id,
594 594 nplots=nplots,
595 595 wintitle=wintitle,
596 596 showprofile=showprofile,
597 597 show=show)
598 598
599 599 if timerange != None:
600 600 self.timerange = timerange
601 601
602 602 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
603 603
604 604 noise = dataOut.noise/factor
605 605 noisedB = 10*numpy.log10(noise)
606 606
607 607 if ymin == None: ymin = numpy.nanmin(y)
608 608 if ymax == None: ymax = numpy.nanmax(y)
609 609 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
610 610 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
611 611
612 612 self.FTP_WEI = ftp_wei
613 613 self.EXP_CODE = exp_code
614 614 self.SUB_EXP_CODE = sub_exp_code
615 615 self.PLOT_POS = plot_pos
616 616
617 617 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
618 618 self.isConfig = True
619 619 self.figfile = figfile
620 620 update_figfile = True
621 621
622 622 self.setWinTitle(title)
623 623
624 624 for i in range(self.nplots):
625 625 index = channelIndexList[i]
626 626 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
627 627 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
628 628 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
629 629 axes = self.axesList[i*self.__nsubplots]
630 630 zdB = avgdB[index].reshape((1,-1))
631 631 axes.pcolorbuffer(x, y, zdB,
632 632 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
633 633 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
634 634 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
635 635
636 636 if self.__showprofile:
637 637 axes = self.axesList[i*self.__nsubplots +1]
638 638 axes.pline(avgdB[index], y,
639 639 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
640 640 xlabel='dB', ylabel='', title='',
641 641 ytick_visible=False,
642 642 grid='x')
643 643
644 644 self.draw()
645 645
646 646 self.save(figpath=figpath,
647 647 figfile=figfile,
648 648 save=save,
649 649 ftp=ftp,
650 650 wr_period=wr_period,
651 651 thisDatetime=thisDatetime,
652 652 update_figfile=update_figfile)
653 653
654 654 class CoherenceMap(Figure):
655 655 isConfig = None
656 656 __nsubplots = None
657 657
658 658 WIDTHPROF = None
659 659 HEIGHTPROF = None
660 660 PREFIX = 'cmap'
661 661
662 662 def __init__(self, **kwargs):
663 663 Figure.__init__(self, **kwargs)
664 664 self.timerange = 2*60*60
665 665 self.isConfig = False
666 666 self.__nsubplots = 1
667 667
668 668 self.WIDTH = 800
669 669 self.HEIGHT = 180
670 670 self.WIDTHPROF = 120
671 671 self.HEIGHTPROF = 0
672 672 self.counter_imagwr = 0
673 673
674 674 self.PLOT_CODE = COH_CODE
675 675
676 676 self.FTP_WEI = None
677 677 self.EXP_CODE = None
678 678 self.SUB_EXP_CODE = None
679 679 self.PLOT_POS = None
680 680 self.counter_imagwr = 0
681 681
682 682 self.xmin = None
683 683 self.xmax = None
684 684
685 685 def getSubplots(self):
686 686 ncol = 1
687 687 nrow = self.nplots*2
688 688
689 689 return nrow, ncol
690 690
691 691 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
692 692 self.__showprofile = showprofile
693 693 self.nplots = nplots
694 694
695 695 ncolspan = 1
696 696 colspan = 1
697 697 if showprofile:
698 698 ncolspan = 7
699 699 colspan = 6
700 700 self.__nsubplots = 2
701 701
702 702 self.createFigure(id = id,
703 703 wintitle = wintitle,
704 704 widthplot = self.WIDTH + self.WIDTHPROF,
705 705 heightplot = self.HEIGHT + self.HEIGHTPROF,
706 706 show=True)
707 707
708 708 nrow, ncol = self.getSubplots()
709 709
710 710 for y in range(nrow):
711 711 for x in range(ncol):
712 712
713 713 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
714 714
715 715 if showprofile:
716 716 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
717 717
718 718 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
719 719 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
720 720 timerange=None, phase_min=None, phase_max=None,
721 721 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
722 722 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
723 723 server=None, folder=None, username=None, password=None,
724 724 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
725 725
726 726 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
727 727 return
728 728
729 729 if pairsList == None:
730 730 pairsIndexList = dataOut.pairsIndexList
731 731 else:
732 732 pairsIndexList = []
733 733 for pair in pairsList:
734 734 if pair not in dataOut.pairsList:
735 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
735 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
736 736 pairsIndexList.append(dataOut.pairsList.index(pair))
737 737
738 738 if pairsIndexList == []:
739 739 return
740 740
741 741 if len(pairsIndexList) > 4:
742 742 pairsIndexList = pairsIndexList[0:4]
743 743
744 744 if phase_min == None:
745 745 phase_min = -180
746 746 if phase_max == None:
747 747 phase_max = 180
748 748
749 749 x = dataOut.getTimeRange()
750 750 y = dataOut.getHeiRange()
751 751
752 752 thisDatetime = dataOut.datatime
753 753
754 754 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
755 755 xlabel = ""
756 756 ylabel = "Range (Km)"
757 757 update_figfile = False
758 758
759 759 if not self.isConfig:
760 760 nplots = len(pairsIndexList)
761 761 self.setup(id=id,
762 762 nplots=nplots,
763 763 wintitle=wintitle,
764 764 showprofile=showprofile,
765 765 show=show)
766 766
767 767 if timerange != None:
768 768 self.timerange = timerange
769 769
770 770 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
771 771
772 772 if ymin == None: ymin = numpy.nanmin(y)
773 773 if ymax == None: ymax = numpy.nanmax(y)
774 774 if zmin == None: zmin = 0.
775 775 if zmax == None: zmax = 1.
776 776
777 777 self.FTP_WEI = ftp_wei
778 778 self.EXP_CODE = exp_code
779 779 self.SUB_EXP_CODE = sub_exp_code
780 780 self.PLOT_POS = plot_pos
781 781
782 782 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
783 783
784 784 self.isConfig = True
785 785 update_figfile = True
786 786
787 787 self.setWinTitle(title)
788 788
789 789 for i in range(self.nplots):
790 790
791 791 pair = dataOut.pairsList[pairsIndexList[i]]
792 792
793 793 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
794 794 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
795 795 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
796 796
797 797
798 798 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
799 799 coherence = numpy.abs(avgcoherenceComplex)
800 800
801 801 z = coherence.reshape((1,-1))
802 802
803 803 counter = 0
804 804
805 805 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
806 806 axes = self.axesList[i*self.__nsubplots*2]
807 807 axes.pcolorbuffer(x, y, z,
808 808 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
809 809 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
810 810 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
811 811
812 812 if self.__showprofile:
813 813 counter += 1
814 814 axes = self.axesList[i*self.__nsubplots*2 + counter]
815 815 axes.pline(coherence, y,
816 816 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
817 817 xlabel='', ylabel='', title='', ticksize=7,
818 818 ytick_visible=False, nxticks=5,
819 819 grid='x')
820 820
821 821 counter += 1
822 822
823 823 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
824 824
825 825 z = phase.reshape((1,-1))
826 826
827 827 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
828 828 axes = self.axesList[i*self.__nsubplots*2 + counter]
829 829 axes.pcolorbuffer(x, y, z,
830 830 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
831 831 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
832 832 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
833 833
834 834 if self.__showprofile:
835 835 counter += 1
836 836 axes = self.axesList[i*self.__nsubplots*2 + counter]
837 837 axes.pline(phase, y,
838 838 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
839 839 xlabel='', ylabel='', title='', ticksize=7,
840 840 ytick_visible=False, nxticks=4,
841 841 grid='x')
842 842
843 843 self.draw()
844 844
845 845 if dataOut.ltctime >= self.xmax:
846 846 self.counter_imagwr = wr_period
847 847 self.isConfig = False
848 848 update_figfile = True
849 849
850 850 self.save(figpath=figpath,
851 851 figfile=figfile,
852 852 save=save,
853 853 ftp=ftp,
854 854 wr_period=wr_period,
855 855 thisDatetime=thisDatetime,
856 856 update_figfile=update_figfile)
857 857
858 858 class PowerProfilePlot(Figure):
859 859
860 860 isConfig = None
861 861 __nsubplots = None
862 862
863 863 WIDTHPROF = None
864 864 HEIGHTPROF = None
865 865 PREFIX = 'spcprofile'
866 866
867 867 def __init__(self, **kwargs):
868 868 Figure.__init__(self, **kwargs)
869 869 self.isConfig = False
870 870 self.__nsubplots = 1
871 871
872 872 self.PLOT_CODE = POWER_CODE
873 873
874 874 self.WIDTH = 300
875 875 self.HEIGHT = 500
876 876 self.counter_imagwr = 0
877 877
878 878 def getSubplots(self):
879 879 ncol = 1
880 880 nrow = 1
881 881
882 882 return nrow, ncol
883 883
884 884 def setup(self, id, nplots, wintitle, show):
885 885
886 886 self.nplots = nplots
887 887
888 888 ncolspan = 1
889 889 colspan = 1
890 890
891 891 self.createFigure(id = id,
892 892 wintitle = wintitle,
893 893 widthplot = self.WIDTH,
894 894 heightplot = self.HEIGHT,
895 895 show=show)
896 896
897 897 nrow, ncol = self.getSubplots()
898 898
899 899 counter = 0
900 900 for y in range(nrow):
901 901 for x in range(ncol):
902 902 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
903 903
904 904 def run(self, dataOut, id, wintitle="", channelList=None,
905 905 xmin=None, xmax=None, ymin=None, ymax=None,
906 906 save=False, figpath='./', figfile=None, show=True,
907 907 ftp=False, wr_period=1, server=None,
908 908 folder=None, username=None, password=None):
909 909
910 910
911 911 if channelList == None:
912 912 channelIndexList = dataOut.channelIndexList
913 913 channelList = dataOut.channelList
914 914 else:
915 915 channelIndexList = []
916 916 for channel in channelList:
917 917 if channel not in dataOut.channelList:
918 raise ValueError, "Channel %d is not in dataOut.channelList"
918 raise ValueError("Channel %d is not in dataOut.channelList")
919 919 channelIndexList.append(dataOut.channelList.index(channel))
920 920
921 921 factor = dataOut.normFactor
922 922
923 923 y = dataOut.getHeiRange()
924 924
925 925 #for voltage
926 926 if dataOut.type == 'Voltage':
927 927 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
928 928 x = x.real
929 929 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
930 930
931 931 #for spectra
932 932 if dataOut.type == 'Spectra':
933 933 x = dataOut.data_spc[channelIndexList,:,:]/factor
934 934 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
935 935 x = numpy.average(x, axis=1)
936 936
937 937
938 938 xdB = 10*numpy.log10(x)
939 939
940 940 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
941 941 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
942 942 xlabel = "dB"
943 943 ylabel = "Range (Km)"
944 944
945 945 if not self.isConfig:
946 946
947 947 nplots = 1
948 948
949 949 self.setup(id=id,
950 950 nplots=nplots,
951 951 wintitle=wintitle,
952 952 show=show)
953 953
954 954 if ymin == None: ymin = numpy.nanmin(y)
955 955 if ymax == None: ymax = numpy.nanmax(y)
956 956 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
957 957 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
958 958
959 959 self.isConfig = True
960 960
961 961 self.setWinTitle(title)
962 962
963 963 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
964 964 axes = self.axesList[0]
965 965
966 966 legendlabels = ["channel %d"%x for x in channelList]
967 967 axes.pmultiline(xdB, y,
968 968 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
969 969 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
970 970 ytick_visible=True, nxticks=5,
971 971 grid='x')
972 972
973 973 self.draw()
974 974
975 975 self.save(figpath=figpath,
976 976 figfile=figfile,
977 977 save=save,
978 978 ftp=ftp,
979 979 wr_period=wr_period,
980 980 thisDatetime=thisDatetime)
981 981
982 982 class SpectraCutPlot(Figure):
983 983
984 984 isConfig = None
985 985 __nsubplots = None
986 986
987 987 WIDTHPROF = None
988 988 HEIGHTPROF = None
989 989 PREFIX = 'spc_cut'
990 990
991 991 def __init__(self, **kwargs):
992 992 Figure.__init__(self, **kwargs)
993 993 self.isConfig = False
994 994 self.__nsubplots = 1
995 995
996 996 self.PLOT_CODE = POWER_CODE
997 997
998 998 self.WIDTH = 700
999 999 self.HEIGHT = 500
1000 1000 self.counter_imagwr = 0
1001 1001
1002 1002 def getSubplots(self):
1003 1003 ncol = 1
1004 1004 nrow = 1
1005 1005
1006 1006 return nrow, ncol
1007 1007
1008 1008 def setup(self, id, nplots, wintitle, show):
1009 1009
1010 1010 self.nplots = nplots
1011 1011
1012 1012 ncolspan = 1
1013 1013 colspan = 1
1014 1014
1015 1015 self.createFigure(id = id,
1016 1016 wintitle = wintitle,
1017 1017 widthplot = self.WIDTH,
1018 1018 heightplot = self.HEIGHT,
1019 1019 show=show)
1020 1020
1021 1021 nrow, ncol = self.getSubplots()
1022 1022
1023 1023 counter = 0
1024 1024 for y in range(nrow):
1025 1025 for x in range(ncol):
1026 1026 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1027 1027
1028 1028 def run(self, dataOut, id, wintitle="", channelList=None,
1029 1029 xmin=None, xmax=None, ymin=None, ymax=None,
1030 1030 save=False, figpath='./', figfile=None, show=True,
1031 1031 ftp=False, wr_period=1, server=None,
1032 1032 folder=None, username=None, password=None,
1033 1033 xaxis="frequency"):
1034 1034
1035 1035
1036 1036 if channelList == None:
1037 1037 channelIndexList = dataOut.channelIndexList
1038 1038 channelList = dataOut.channelList
1039 1039 else:
1040 1040 channelIndexList = []
1041 1041 for channel in channelList:
1042 1042 if channel not in dataOut.channelList:
1043 raise ValueError, "Channel %d is not in dataOut.channelList"
1043 raise ValueError("Channel %d is not in dataOut.channelList")
1044 1044 channelIndexList.append(dataOut.channelList.index(channel))
1045 1045
1046 1046 factor = dataOut.normFactor
1047 1047
1048 1048 y = dataOut.getHeiRange()
1049 1049
1050 1050 z = dataOut.data_spc/factor
1051 1051 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1052 1052
1053 1053 hei_index = numpy.arange(25)*3 + 20
1054 1054
1055 1055 if xaxis == "frequency":
1056 1056 x = dataOut.getFreqRange()/1000.
1057 1057 zdB = 10*numpy.log10(z[0,:,hei_index])
1058 1058 xlabel = "Frequency (kHz)"
1059 1059 ylabel = "Power (dB)"
1060 1060
1061 1061 elif xaxis == "time":
1062 1062 x = dataOut.getAcfRange()
1063 1063 zdB = z[0,:,hei_index]
1064 1064 xlabel = "Time (ms)"
1065 1065 ylabel = "ACF"
1066 1066
1067 1067 else:
1068 1068 x = dataOut.getVelRange()
1069 1069 zdB = 10*numpy.log10(z[0,:,hei_index])
1070 1070 xlabel = "Velocity (m/s)"
1071 1071 ylabel = "Power (dB)"
1072 1072
1073 1073 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1074 1074 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1075 1075
1076 1076 if not self.isConfig:
1077 1077
1078 1078 nplots = 1
1079 1079
1080 1080 self.setup(id=id,
1081 1081 nplots=nplots,
1082 1082 wintitle=wintitle,
1083 1083 show=show)
1084 1084
1085 1085 if xmin == None: xmin = numpy.nanmin(x)*0.9
1086 1086 if xmax == None: xmax = numpy.nanmax(x)*1.1
1087 1087 if ymin == None: ymin = numpy.nanmin(zdB)
1088 1088 if ymax == None: ymax = numpy.nanmax(zdB)
1089 1089
1090 1090 self.isConfig = True
1091 1091
1092 1092 self.setWinTitle(title)
1093 1093
1094 1094 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1095 1095 axes = self.axesList[0]
1096 1096
1097 1097 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1098 1098
1099 1099 axes.pmultilineyaxis( x, zdB,
1100 1100 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1101 1101 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1102 1102 ytick_visible=True, nxticks=5,
1103 1103 grid='x')
1104 1104
1105 1105 self.draw()
1106 1106
1107 1107 self.save(figpath=figpath,
1108 1108 figfile=figfile,
1109 1109 save=save,
1110 1110 ftp=ftp,
1111 1111 wr_period=wr_period,
1112 1112 thisDatetime=thisDatetime)
1113 1113
1114 1114 class Noise(Figure):
1115 1115
1116 1116 isConfig = None
1117 1117 __nsubplots = None
1118 1118
1119 1119 PREFIX = 'noise'
1120 1120
1121 1121
1122 1122 def __init__(self, **kwargs):
1123 1123 Figure.__init__(self, **kwargs)
1124 1124 self.timerange = 24*60*60
1125 1125 self.isConfig = False
1126 1126 self.__nsubplots = 1
1127 1127 self.counter_imagwr = 0
1128 1128 self.WIDTH = 800
1129 1129 self.HEIGHT = 400
1130 1130 self.WIDTHPROF = 120
1131 1131 self.HEIGHTPROF = 0
1132 1132 self.xdata = None
1133 1133 self.ydata = None
1134 1134
1135 1135 self.PLOT_CODE = NOISE_CODE
1136 1136
1137 1137 self.FTP_WEI = None
1138 1138 self.EXP_CODE = None
1139 1139 self.SUB_EXP_CODE = None
1140 1140 self.PLOT_POS = None
1141 1141 self.figfile = None
1142 1142
1143 1143 self.xmin = None
1144 1144 self.xmax = None
1145 1145
1146 1146 def getSubplots(self):
1147 1147
1148 1148 ncol = 1
1149 1149 nrow = 1
1150 1150
1151 1151 return nrow, ncol
1152 1152
1153 1153 def openfile(self, filename):
1154 1154 dirname = os.path.dirname(filename)
1155 1155
1156 1156 if not os.path.exists(dirname):
1157 1157 os.mkdir(dirname)
1158 1158
1159 1159 f = open(filename,'w+')
1160 1160 f.write('\n\n')
1161 1161 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1162 1162 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1163 1163 f.close()
1164 1164
1165 1165 def save_data(self, filename_phase, data, data_datetime):
1166 1166
1167 1167 f=open(filename_phase,'a')
1168 1168
1169 1169 timetuple_data = data_datetime.timetuple()
1170 1170 day = str(timetuple_data.tm_mday)
1171 1171 month = str(timetuple_data.tm_mon)
1172 1172 year = str(timetuple_data.tm_year)
1173 1173 hour = str(timetuple_data.tm_hour)
1174 1174 minute = str(timetuple_data.tm_min)
1175 1175 second = str(timetuple_data.tm_sec)
1176 1176
1177 1177 data_msg = ''
1178 1178 for i in range(len(data)):
1179 1179 data_msg += str(data[i]) + ' '
1180 1180
1181 1181 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1182 1182 f.close()
1183 1183
1184 1184
1185 1185 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1186 1186
1187 1187 self.__showprofile = showprofile
1188 1188 self.nplots = nplots
1189 1189
1190 1190 ncolspan = 7
1191 1191 colspan = 6
1192 1192 self.__nsubplots = 2
1193 1193
1194 1194 self.createFigure(id = id,
1195 1195 wintitle = wintitle,
1196 1196 widthplot = self.WIDTH+self.WIDTHPROF,
1197 1197 heightplot = self.HEIGHT+self.HEIGHTPROF,
1198 1198 show=show)
1199 1199
1200 1200 nrow, ncol = self.getSubplots()
1201 1201
1202 1202 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1203 1203
1204 1204
1205 1205 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1206 1206 xmin=None, xmax=None, ymin=None, ymax=None,
1207 1207 timerange=None,
1208 1208 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1209 1209 server=None, folder=None, username=None, password=None,
1210 1210 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1211 1211
1212 1212 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1213 1213 return
1214 1214
1215 1215 if channelList == None:
1216 1216 channelIndexList = dataOut.channelIndexList
1217 1217 channelList = dataOut.channelList
1218 1218 else:
1219 1219 channelIndexList = []
1220 1220 for channel in channelList:
1221 1221 if channel not in dataOut.channelList:
1222 raise ValueError, "Channel %d is not in dataOut.channelList"
1222 raise ValueError("Channel %d is not in dataOut.channelList")
1223 1223 channelIndexList.append(dataOut.channelList.index(channel))
1224 1224
1225 1225 x = dataOut.getTimeRange()
1226 1226 #y = dataOut.getHeiRange()
1227 1227 factor = dataOut.normFactor
1228 1228 noise = dataOut.noise[channelIndexList]/factor
1229 1229 noisedB = 10*numpy.log10(noise)
1230 1230
1231 1231 thisDatetime = dataOut.datatime
1232 1232
1233 1233 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1234 1234 xlabel = ""
1235 1235 ylabel = "Intensity (dB)"
1236 1236 update_figfile = False
1237 1237
1238 1238 if not self.isConfig:
1239 1239
1240 1240 nplots = 1
1241 1241
1242 1242 self.setup(id=id,
1243 1243 nplots=nplots,
1244 1244 wintitle=wintitle,
1245 1245 showprofile=showprofile,
1246 1246 show=show)
1247 1247
1248 1248 if timerange != None:
1249 1249 self.timerange = timerange
1250 1250
1251 1251 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1252 1252
1253 1253 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1254 1254 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1255 1255
1256 1256 self.FTP_WEI = ftp_wei
1257 1257 self.EXP_CODE = exp_code
1258 1258 self.SUB_EXP_CODE = sub_exp_code
1259 1259 self.PLOT_POS = plot_pos
1260 1260
1261 1261
1262 1262 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1263 1263 self.isConfig = True
1264 1264 self.figfile = figfile
1265 1265 self.xdata = numpy.array([])
1266 1266 self.ydata = numpy.array([])
1267 1267
1268 1268 update_figfile = True
1269 1269
1270 1270 #open file beacon phase
1271 1271 path = '%s%03d' %(self.PREFIX, self.id)
1272 1272 noise_file = os.path.join(path,'%s.txt'%self.name)
1273 1273 self.filename_noise = os.path.join(figpath,noise_file)
1274 1274
1275 1275 self.setWinTitle(title)
1276 1276
1277 1277 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1278 1278
1279 1279 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1280 1280 axes = self.axesList[0]
1281 1281
1282 1282 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1283 1283
1284 1284 if len(self.ydata)==0:
1285 1285 self.ydata = noisedB.reshape(-1,1)
1286 1286 else:
1287 1287 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1288 1288
1289 1289
1290 1290 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1291 1291 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1292 1292 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1293 1293 XAxisAsTime=True, grid='both'
1294 1294 )
1295 1295
1296 1296 self.draw()
1297 1297
1298 1298 if dataOut.ltctime >= self.xmax:
1299 1299 self.counter_imagwr = wr_period
1300 1300 self.isConfig = False
1301 1301 update_figfile = True
1302 1302
1303 1303 self.save(figpath=figpath,
1304 1304 figfile=figfile,
1305 1305 save=save,
1306 1306 ftp=ftp,
1307 1307 wr_period=wr_period,
1308 1308 thisDatetime=thisDatetime,
1309 1309 update_figfile=update_figfile)
1310 1310
1311 1311 #store data beacon phase
1312 1312 if save:
1313 1313 self.save_data(self.filename_noise, noisedB, thisDatetime)
1314 1314
1315 1315 class BeaconPhase(Figure):
1316 1316
1317 1317 __isConfig = None
1318 1318 __nsubplots = None
1319 1319
1320 1320 PREFIX = 'beacon_phase'
1321 1321
1322 1322 def __init__(self, **kwargs):
1323 1323 Figure.__init__(self, **kwargs)
1324 1324 self.timerange = 24*60*60
1325 1325 self.isConfig = False
1326 1326 self.__nsubplots = 1
1327 1327 self.counter_imagwr = 0
1328 1328 self.WIDTH = 800
1329 1329 self.HEIGHT = 400
1330 1330 self.WIDTHPROF = 120
1331 1331 self.HEIGHTPROF = 0
1332 1332 self.xdata = None
1333 1333 self.ydata = None
1334 1334
1335 1335 self.PLOT_CODE = BEACON_CODE
1336 1336
1337 1337 self.FTP_WEI = None
1338 1338 self.EXP_CODE = None
1339 1339 self.SUB_EXP_CODE = None
1340 1340 self.PLOT_POS = None
1341 1341
1342 1342 self.filename_phase = None
1343 1343
1344 1344 self.figfile = None
1345 1345
1346 1346 self.xmin = None
1347 1347 self.xmax = None
1348 1348
1349 1349 def getSubplots(self):
1350 1350
1351 1351 ncol = 1
1352 1352 nrow = 1
1353 1353
1354 1354 return nrow, ncol
1355 1355
1356 1356 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1357 1357
1358 1358 self.__showprofile = showprofile
1359 1359 self.nplots = nplots
1360 1360
1361 1361 ncolspan = 7
1362 1362 colspan = 6
1363 1363 self.__nsubplots = 2
1364 1364
1365 1365 self.createFigure(id = id,
1366 1366 wintitle = wintitle,
1367 1367 widthplot = self.WIDTH+self.WIDTHPROF,
1368 1368 heightplot = self.HEIGHT+self.HEIGHTPROF,
1369 1369 show=show)
1370 1370
1371 1371 nrow, ncol = self.getSubplots()
1372 1372
1373 1373 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1374 1374
1375 1375 def save_phase(self, filename_phase):
1376 1376 f = open(filename_phase,'w+')
1377 1377 f.write('\n\n')
1378 1378 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1379 1379 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1380 1380 f.close()
1381 1381
1382 1382 def save_data(self, filename_phase, data, data_datetime):
1383 1383 f=open(filename_phase,'a')
1384 1384 timetuple_data = data_datetime.timetuple()
1385 1385 day = str(timetuple_data.tm_mday)
1386 1386 month = str(timetuple_data.tm_mon)
1387 1387 year = str(timetuple_data.tm_year)
1388 1388 hour = str(timetuple_data.tm_hour)
1389 1389 minute = str(timetuple_data.tm_min)
1390 1390 second = str(timetuple_data.tm_sec)
1391 1391 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1392 1392 f.close()
1393 1393
1394 1394
1395 1395 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1396 1396 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1397 1397 timerange=None,
1398 1398 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1399 1399 server=None, folder=None, username=None, password=None,
1400 1400 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1401 1401
1402 1402 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1403 1403 return
1404 1404
1405 1405 if pairsList == None:
1406 1406 pairsIndexList = dataOut.pairsIndexList[:10]
1407 1407 else:
1408 1408 pairsIndexList = []
1409 1409 for pair in pairsList:
1410 1410 if pair not in dataOut.pairsList:
1411 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
1411 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
1412 1412 pairsIndexList.append(dataOut.pairsList.index(pair))
1413 1413
1414 1414 if pairsIndexList == []:
1415 1415 return
1416 1416
1417 1417 # if len(pairsIndexList) > 4:
1418 1418 # pairsIndexList = pairsIndexList[0:4]
1419 1419
1420 1420 hmin_index = None
1421 1421 hmax_index = None
1422 1422
1423 1423 if hmin != None and hmax != None:
1424 1424 indexes = numpy.arange(dataOut.nHeights)
1425 1425 hmin_list = indexes[dataOut.heightList >= hmin]
1426 1426 hmax_list = indexes[dataOut.heightList <= hmax]
1427 1427
1428 1428 if hmin_list.any():
1429 1429 hmin_index = hmin_list[0]
1430 1430
1431 1431 if hmax_list.any():
1432 1432 hmax_index = hmax_list[-1]+1
1433 1433
1434 1434 x = dataOut.getTimeRange()
1435 1435 #y = dataOut.getHeiRange()
1436 1436
1437 1437
1438 1438 thisDatetime = dataOut.datatime
1439 1439
1440 1440 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1441 1441 xlabel = "Local Time"
1442 1442 ylabel = "Phase (degrees)"
1443 1443
1444 1444 update_figfile = False
1445 1445
1446 1446 nplots = len(pairsIndexList)
1447 1447 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1448 1448 phase_beacon = numpy.zeros(len(pairsIndexList))
1449 1449 for i in range(nplots):
1450 1450 pair = dataOut.pairsList[pairsIndexList[i]]
1451 1451 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1452 1452 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1453 1453 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1454 1454 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1455 1455 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1456 1456
1457 1457 #print "Phase %d%d" %(pair[0], pair[1])
1458 1458 #print phase[dataOut.beacon_heiIndexList]
1459 1459
1460 1460 if dataOut.beacon_heiIndexList:
1461 1461 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1462 1462 else:
1463 1463 phase_beacon[i] = numpy.average(phase)
1464 1464
1465 1465 if not self.isConfig:
1466 1466
1467 1467 nplots = len(pairsIndexList)
1468 1468
1469 1469 self.setup(id=id,
1470 1470 nplots=nplots,
1471 1471 wintitle=wintitle,
1472 1472 showprofile=showprofile,
1473 1473 show=show)
1474 1474
1475 1475 if timerange != None:
1476 1476 self.timerange = timerange
1477 1477
1478 1478 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1479 1479
1480 1480 if ymin == None: ymin = 0
1481 1481 if ymax == None: ymax = 360
1482 1482
1483 1483 self.FTP_WEI = ftp_wei
1484 1484 self.EXP_CODE = exp_code
1485 1485 self.SUB_EXP_CODE = sub_exp_code
1486 1486 self.PLOT_POS = plot_pos
1487 1487
1488 1488 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1489 1489 self.isConfig = True
1490 1490 self.figfile = figfile
1491 1491 self.xdata = numpy.array([])
1492 1492 self.ydata = numpy.array([])
1493 1493
1494 1494 update_figfile = True
1495 1495
1496 1496 #open file beacon phase
1497 1497 path = '%s%03d' %(self.PREFIX, self.id)
1498 1498 beacon_file = os.path.join(path,'%s.txt'%self.name)
1499 1499 self.filename_phase = os.path.join(figpath,beacon_file)
1500 1500 #self.save_phase(self.filename_phase)
1501 1501
1502 1502
1503 1503 #store data beacon phase
1504 1504 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1505 1505
1506 1506 self.setWinTitle(title)
1507 1507
1508 1508
1509 1509 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1510 1510
1511 1511 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1512 1512
1513 1513 axes = self.axesList[0]
1514 1514
1515 1515 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1516 1516
1517 1517 if len(self.ydata)==0:
1518 1518 self.ydata = phase_beacon.reshape(-1,1)
1519 1519 else:
1520 1520 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1521 1521
1522 1522
1523 1523 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1524 1524 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1525 1525 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1526 1526 XAxisAsTime=True, grid='both'
1527 1527 )
1528 1528
1529 1529 self.draw()
1530 1530
1531 1531 if dataOut.ltctime >= self.xmax:
1532 1532 self.counter_imagwr = wr_period
1533 1533 self.isConfig = False
1534 1534 update_figfile = True
1535 1535
1536 1536 self.save(figpath=figpath,
1537 1537 figfile=figfile,
1538 1538 save=save,
1539 1539 ftp=ftp,
1540 1540 wr_period=wr_period,
1541 1541 thisDatetime=thisDatetime,
1542 update_figfile=update_figfile)
1542 update_figfile=update_figfile) No newline at end of file
@@ -1,225 +1,225
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 from figure import Figure
10 from .figure import Figure
11 11
12 12 class Scope(Figure):
13 13
14 14 isConfig = None
15 15
16 16 def __init__(self, **kwargs):
17 17 Figure.__init__(self, **kwargs)
18 18 self.isConfig = False
19 19 self.WIDTH = 300
20 20 self.HEIGHT = 200
21 21 self.counter_imagwr = 0
22 22
23 23 def getSubplots(self):
24 24
25 25 nrow = self.nplots
26 26 ncol = 3
27 27 return nrow, ncol
28 28
29 29 def setup(self, id, nplots, wintitle, show):
30 30
31 31 self.nplots = nplots
32 32
33 33 self.createFigure(id=id,
34 34 wintitle=wintitle,
35 35 show=show)
36 36
37 37 nrow,ncol = self.getSubplots()
38 38 colspan = 3
39 39 rowspan = 1
40 40
41 41 for i in range(nplots):
42 42 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
43 43
44 44 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
45 45 yreal = y[channelIndexList,:].real
46 46 yimag = y[channelIndexList,:].imag
47 47
48 48 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
49 49 xlabel = "Range (Km)"
50 50 ylabel = "Intensity - IQ"
51 51
52 52 if not self.isConfig:
53 53 nplots = len(channelIndexList)
54 54
55 55 self.setup(id=id,
56 56 nplots=nplots,
57 57 wintitle='',
58 58 show=show)
59 59
60 60 if xmin == None: xmin = numpy.nanmin(x)
61 61 if xmax == None: xmax = numpy.nanmax(x)
62 62 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
63 63 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
64 64
65 65 self.isConfig = True
66 66
67 67 self.setWinTitle(title)
68 68
69 69 for i in range(len(self.axesList)):
70 70 title = "Channel %d" %(i)
71 71 axes = self.axesList[i]
72 72
73 73 axes.pline(x, yreal[i,:],
74 74 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
75 75 xlabel=xlabel, ylabel=ylabel, title=title)
76 76
77 77 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
78 78
79 79 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
80 80 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
81 81 yreal = y.real
82 82
83 83 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
84 84 xlabel = "Range (Km)"
85 85 ylabel = "Intensity"
86 86
87 87 if not self.isConfig:
88 88 nplots = len(channelIndexList)
89 89
90 90 self.setup(id=id,
91 91 nplots=nplots,
92 92 wintitle='',
93 93 show=show)
94 94
95 95 if xmin == None: xmin = numpy.nanmin(x)
96 96 if xmax == None: xmax = numpy.nanmax(x)
97 97 if ymin == None: ymin = numpy.nanmin(yreal)
98 98 if ymax == None: ymax = numpy.nanmax(yreal)
99 99
100 100 self.isConfig = True
101 101
102 102 self.setWinTitle(title)
103 103
104 104 for i in range(len(self.axesList)):
105 105 title = "Channel %d" %(i)
106 106 axes = self.axesList[i]
107 107 ychannel = yreal[i,:]
108 108 axes.pline(x, ychannel,
109 109 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
110 110 xlabel=xlabel, ylabel=ylabel, title=title)
111 111
112 112
113 113 def run(self, dataOut, id, wintitle="", channelList=None,
114 114 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
115 115 figpath='./', figfile=None, show=True, wr_period=1,
116 116 ftp=False, server=None, folder=None, username=None, password=None, type='power', **kwargs):
117 117
118 118 """
119 119
120 120 Input:
121 121 dataOut :
122 122 id :
123 123 wintitle :
124 124 channelList :
125 125 xmin : None,
126 126 xmax : None,
127 127 ymin : None,
128 128 ymax : None,
129 129 """
130 130
131 131 if channelList == None:
132 132 channelIndexList = dataOut.channelIndexList
133 133 else:
134 134 channelIndexList = []
135 135 for channel in channelList:
136 136 if channel not in dataOut.channelList:
137 raise ValueError, "Channel %d is not in dataOut.channelList"
137 raise ValueError("Channel %d is not in dataOut.channelList")
138 138 channelIndexList.append(dataOut.channelList.index(channel))
139 139
140 140 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
141 141
142 142 if dataOut.flagDataAsBlock:
143 143
144 144 for i in range(dataOut.nProfiles):
145 145
146 146 wintitle1 = wintitle + " [Profile = %d] " %i
147 147
148 148 if type == "power":
149 149 self.plot_power(dataOut.heightList,
150 150 dataOut.data[:,i,:],
151 151 id,
152 152 channelIndexList,
153 153 thisDatetime,
154 154 wintitle1,
155 155 show,
156 156 xmin,
157 157 xmax,
158 158 ymin,
159 159 ymax)
160 160
161 161 if type == "iq":
162 162 self.plot_iq(dataOut.heightList,
163 163 dataOut.data[:,i,:],
164 164 id,
165 165 channelIndexList,
166 166 thisDatetime,
167 167 wintitle1,
168 168 show,
169 169 xmin,
170 170 xmax,
171 171 ymin,
172 172 ymax)
173 173
174 174 self.draw()
175 175
176 176 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
177 177 figfile = self.getFilename(name = str_datetime) + "_" + str(i)
178 178
179 179 self.save(figpath=figpath,
180 180 figfile=figfile,
181 181 save=save,
182 182 ftp=ftp,
183 183 wr_period=wr_period,
184 184 thisDatetime=thisDatetime)
185 185
186 186 else:
187 187 wintitle += " [Profile = %d] " %dataOut.profileIndex
188 188
189 189 if type == "power":
190 190 self.plot_power(dataOut.heightList,
191 191 dataOut.data,
192 192 id,
193 193 channelIndexList,
194 194 thisDatetime,
195 195 wintitle,
196 196 show,
197 197 xmin,
198 198 xmax,
199 199 ymin,
200 200 ymax)
201 201
202 202 if type == "iq":
203 203 self.plot_iq(dataOut.heightList,
204 204 dataOut.data,
205 205 id,
206 206 channelIndexList,
207 207 thisDatetime,
208 208 wintitle,
209 209 show,
210 210 xmin,
211 211 xmax,
212 212 ymin,
213 213 ymax)
214 214
215 215 self.draw()
216 216
217 217 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
218 218 figfile = self.getFilename(name = str_datetime)
219 219
220 220 self.save(figpath=figpath,
221 221 figfile=figfile,
222 222 save=save,
223 223 ftp=ftp,
224 224 wr_period=wr_period,
225 thisDatetime=thisDatetime)
225 thisDatetime=thisDatetime) No newline at end of file
@@ -1,240 +1,240
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os, sys
7 7 import datetime
8 8 import numpy
9 9 import traceback
10 10
11 11 from time import sleep
12 12 from threading import Lock
13 13 # from threading import Thread
14 14
15 15 import schainpy
16 16 import schainpy.admin
17 17
18 18 from schainpy.model.proc.jroproc_base import Operation
19 19 from schainpy.model.serializer.data import obj2Dict, dict2Obj
20 from jroplot_correlation import *
21 from jroplot_heispectra import *
22 from jroplot_parameters import *
23 from jroplot_spectra import *
24 from jroplot_voltage import *
20 from .jroplot_correlation import *
21 from .jroplot_heispectra import *
22 from .jroplot_parameters import *
23 from .jroplot_spectra import *
24 from .jroplot_voltage import *
25 25
26 26
27 27 class Plotter(Operation):
28 28
29 29 isConfig = None
30 30 name = None
31 31 __queue = None
32 32
33 33 def __init__(self, plotter_name, plotter_queue=None, **kwargs):
34 34
35 35 Operation.__init__(self, **kwargs)
36 36
37 37 self.isConfig = False
38 38 self.name = plotter_name
39 39 self.__queue = plotter_queue
40 40
41 41 def getSubplots(self):
42 42
43 43 nrow = self.nplots
44 44 ncol = 1
45 45 return nrow, ncol
46 46
47 47 def setup(self, **kwargs):
48 48
49 print "Initializing ..."
49 print("Initializing ...")
50 50
51 51
52 52 def run(self, dataOut, id=None, **kwargs):
53 53
54 54 """
55 55
56 56 Input:
57 57 dataOut :
58 58 id :
59 59 """
60 60
61 61 packDict = {}
62 62
63 63 packDict['id'] = id
64 64 packDict['name'] = self.name
65 65 packDict['kwargs'] = kwargs
66 66
67 67 # packDict['data'] = obj2Dict(dataOut)
68 68 packDict['data'] = dataOut
69 69
70 70 self.__queue.put(packDict)
71 71
72 72 # class PlotManager(Thread):
73 73 class PlotManager():
74 74
75 75 __err = False
76 76 __stop = False
77 77 __realtime = False
78 78
79 79 controllerThreadObj = None
80 80
81 81 plotterList = ['Scope',
82 82 'SpectraPlot', 'RTIPlot',
83 83 'SpectraCutPlot',
84 84 'CrossSpectraPlot', 'CoherenceMap',
85 85 'PowerProfilePlot', 'Noise', 'BeaconPhase',
86 86 'CorrelationPlot',
87 87 'SpectraHeisScope', 'RTIfromSpectraHeis']
88 88
89 89 def __init__(self, plotter_queue):
90 90
91 91 # Thread.__init__(self)
92 92 # self.setDaemon(True)
93 93
94 94 self.__queue = plotter_queue
95 95 self.__lock = Lock()
96 96
97 97 self.plotInstanceDict = {}
98 98
99 99 self.__err = False
100 100 self.__stop = False
101 101 self.__realtime = False
102 102
103 103 def __handleError(self, name="", send_email=False):
104 104
105 105 err = traceback.format_exception(sys.exc_info()[0],
106 106 sys.exc_info()[1],
107 107 sys.exc_info()[2])
108 108
109 print "***** Error occurred in PlotManager *****"
110 print "***** [%s]: %s" %(name, err[-1])
109 print("***** Error occurred in PlotManager *****")
110 print("***** [%s]: %s" %(name, err[-1]))
111 111
112 112 message = "\nError ocurred in %s:\n" %name
113 113 message += "".join(err)
114 114
115 115 sys.stderr.write(message)
116 116
117 117 if not send_email:
118 118 return
119 119
120 120 import socket
121 121
122 122 subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, name)
123 123
124 124 subtitle = "%s:\n" %(name)
125 125 subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname())
126 126 subtitle += "Working directory: %s\n" %os.path.abspath("./")
127 127 # subtitle += "Configuration file: %s\n" %self.filename
128 128 subtitle += "Time: %s\n" %str(datetime.datetime.now())
129 129
130 130 adminObj = schainpy.admin.SchainNotify()
131 131 adminObj.sendAlert(message=message,
132 132 subject=subject,
133 133 subtitle=subtitle)
134 134
135 135 def run(self):
136 136
137 137 if self.__queue.empty():
138 138 return
139 139
140 140 if self.__err:
141 141 serial_data = self.__queue.get()
142 142 self.__queue.task_done()
143 143 return
144 144
145 145 self.__lock.acquire()
146 146
147 147 # if self.__queue.full():
148 148 # for i in range(int(self.__queue.qsize()/2)):
149 149 # serial_data = self.__queue.get()
150 150 # self.__queue.task_done()
151 151
152 152 n = int(self.__queue.qsize()/3 + 1)
153 153
154 154 for i in range(n):
155 155
156 156 if self.__queue.empty():
157 157 break
158 158
159 159 serial_data = self.__queue.get()
160 160 self.__queue.task_done()
161 161
162 162 plot_id = serial_data['id']
163 163 plot_name = serial_data['name']
164 164 kwargs = serial_data['kwargs']
165 165 # dataDict = serial_data['data']
166 166 #
167 167 # dataPlot = dict2Obj(dataDict)
168 168
169 169 dataPlot = serial_data['data']
170 170
171 if plot_id not in self.plotInstanceDict.keys():
171 if plot_id not in list(self.plotInstanceDict.keys()):
172 172 className = eval(plot_name)
173 173 self.plotInstanceDict[plot_id] = className(**kwargs)
174 174
175 175 plotter = self.plotInstanceDict[plot_id]
176 176 try:
177 177 plotter.run(dataPlot, plot_id, **kwargs)
178 178 except:
179 179 self.__err = True
180 180 self.__handleError(plot_name, send_email=True)
181 181 break
182 182
183 183 self.__lock.release()
184 184
185 185 def isEmpty(self):
186 186
187 187 return self.__queue.empty()
188 188
189 189 def stop(self):
190 190
191 191 self.__lock.acquire()
192 192
193 193 self.__stop = True
194 194
195 195 self.__lock.release()
196 196
197 197 def close(self):
198 198
199 199 self.__lock.acquire()
200 200
201 for plot_id in self.plotInstanceDict.keys():
201 for plot_id in list(self.plotInstanceDict.keys()):
202 202 plotter = self.plotInstanceDict[plot_id]
203 203 plotter.close()
204 204
205 205 self.__lock.release()
206 206
207 207 def setController(self, controllerThreadObj):
208 208
209 209 self.controllerThreadObj = controllerThreadObj
210 210
211 211 def start(self):
212 212
213 213 if not self.controllerThreadObj.isRunning():
214 raise RuntimeError, "controllerThreadObj has not been initialized. Use controllerThreadObj.start() before call this method"
214 raise RuntimeError("controllerThreadObj has not been initialized. Use controllerThreadObj.start() before call this method")
215 215
216 216 self.join()
217 217
218 218 def join(self):
219 219
220 220 #Execute plotter while controller is running
221 221 while self.controllerThreadObj.isRunning():
222 222 self.run()
223 223
224 224 self.controllerThreadObj.stop()
225 225
226 226 #Wait until plotter queue is empty
227 227 while not self.isEmpty():
228 228 self.run()
229 229
230 230 self.close()
231 231
232 232 def isErrorDetected(self):
233 233
234 234 self.__lock.acquire()
235 235
236 236 err = self.__err
237 237
238 238 self.__lock.release()
239 239
240 return err
240 return err No newline at end of file
@@ -1,501 +1,501
1 1 import os
2 2 import sys
3 3 import datetime
4 4 import numpy
5 5 import matplotlib
6 6
7 7 if 'BACKEND' in os.environ:
8 8 matplotlib.use(os.environ['BACKEND'])
9 9 elif 'linux' in sys.platform:
10 10 matplotlib.use("TkAgg")
11 11 elif 'darwin' in sys.platform:
12 12 matplotlib.use('TkAgg')
13 13 else:
14 14 from schainpy.utils import log
15 15 log.warning('Using default Backend="Agg"', 'INFO')
16 16 matplotlib.use('Agg')
17 17 # Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
18 18 import matplotlib.pyplot
19 19
20 20 from mpl_toolkits.axes_grid1 import make_axes_locatable
21 21 from matplotlib.ticker import FuncFormatter, LinearLocator
22 22
23 23 ###########################################
24 24 # Actualizacion de las funciones del driver
25 25 ###########################################
26 26
27 27 # create jro colormap
28 28
29 29 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
30 30 blu_values = matplotlib.pyplot.get_cmap(
31 31 "seismic_r", 20)(numpy.arange(20))[10:15]
32 32 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
33 33 "jro", numpy.vstack((blu_values, jet_values)))
34 34 matplotlib.pyplot.register_cmap(cmap=ncmap)
35 35
36 36
37 37 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80):
38 38
39 39 matplotlib.pyplot.ioff()
40 40
41 41 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(
42 42 1.0 * width / dpi, 1.0 * height / dpi))
43 43 fig.canvas.manager.set_window_title(wintitle)
44 44 # fig.canvas.manager.resize(width, height)
45 45 matplotlib.pyplot.ion()
46 46
47 47 if show:
48 48 matplotlib.pyplot.show()
49 49
50 50 return fig
51 51
52 52
53 53 def closeFigure(show=False, fig=None):
54 54
55 55 # matplotlib.pyplot.ioff()
56 56 # matplotlib.pyplot.pause(0)
57 57
58 58 if show:
59 59 matplotlib.pyplot.show()
60 60
61 61 if fig != None:
62 62 matplotlib.pyplot.close(fig)
63 63 # matplotlib.pyplot.pause(0)
64 64 # matplotlib.pyplot.ion()
65 65
66 66 return
67 67
68 68 matplotlib.pyplot.close("all")
69 69 # matplotlib.pyplot.pause(0)
70 70 # matplotlib.pyplot.ion()
71 71
72 72 return
73 73
74 74
75 75 def saveFigure(fig, filename):
76 76
77 77 # matplotlib.pyplot.ioff()
78 78 fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi)
79 79 # matplotlib.pyplot.ion()
80 80
81 81
82 82 def clearFigure(fig):
83 83
84 84 fig.clf()
85 85
86 86
87 87 def setWinTitle(fig, title):
88 88
89 89 fig.canvas.manager.set_window_title(title)
90 90
91 91
92 92 def setTitle(fig, title):
93 93
94 94 fig.suptitle(title)
95 95
96 96
97 97 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
98 98
99 99 matplotlib.pyplot.ioff()
100 100 matplotlib.pyplot.figure(fig.number)
101 101 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
102 102 (xpos, ypos),
103 103 colspan=colspan,
104 104 rowspan=rowspan,
105 105 polar=polar)
106 106
107 107 matplotlib.pyplot.ion()
108 108 return axes
109 109
110 110
111 111 def setAxesText(ax, text):
112 112
113 113 ax.annotate(text,
114 114 xy=(.1, .99),
115 115 xycoords='figure fraction',
116 116 horizontalalignment='left',
117 117 verticalalignment='top',
118 118 fontsize=10)
119 119
120 120
121 121 def printLabels(ax, xlabel, ylabel, title):
122 122
123 123 ax.set_xlabel(xlabel, size=11)
124 124 ax.set_ylabel(ylabel, size=11)
125 125 ax.set_title(title, size=8)
126 126
127 127
128 128 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
129 129 ticksize=9, xtick_visible=True, ytick_visible=True,
130 130 nxticks=4, nyticks=10,
131 131 grid=None, color='blue'):
132 132 """
133 133
134 134 Input:
135 135 grid : None, 'both', 'x', 'y'
136 136 """
137 137
138 138 matplotlib.pyplot.ioff()
139 139
140 140 ax.set_xlim([xmin, xmax])
141 141 ax.set_ylim([ymin, ymax])
142 142
143 143 printLabels(ax, xlabel, ylabel, title)
144 144
145 145 ######################################################
146 146 if (xmax - xmin) <= 1:
147 147 xtickspos = numpy.linspace(xmin, xmax, nxticks)
148 148 xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos])
149 149 ax.set_xticks(xtickspos)
150 150 else:
151 151 xtickspos = numpy.arange(nxticks) * \
152 152 int((xmax - xmin) / (nxticks)) + int(xmin)
153 153 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
154 154 ax.set_xticks(xtickspos)
155 155
156 156 for tick in ax.get_xticklabels():
157 157 tick.set_visible(xtick_visible)
158 158
159 159 for tick in ax.xaxis.get_major_ticks():
160 160 tick.label.set_fontsize(ticksize)
161 161
162 162 ######################################################
163 163 for tick in ax.get_yticklabels():
164 164 tick.set_visible(ytick_visible)
165 165
166 166 for tick in ax.yaxis.get_major_ticks():
167 167 tick.label.set_fontsize(ticksize)
168 168
169 169 ax.plot(x, y, color=color)
170 170 iplot = ax.lines[-1]
171 171
172 172 ######################################################
173 173 if '0.' in matplotlib.__version__[0:2]:
174 print "The matplotlib version has to be updated to 1.1 or newer"
174 print("The matplotlib version has to be updated to 1.1 or newer")
175 175 return iplot
176 176
177 177 if '1.0.' in matplotlib.__version__[0:4]:
178 print "The matplotlib version has to be updated to 1.1 or newer"
178 print("The matplotlib version has to be updated to 1.1 or newer")
179 179 return iplot
180 180
181 181 if grid != None:
182 182 ax.grid(b=True, which='major', axis=grid)
183 183
184 184 matplotlib.pyplot.tight_layout()
185 185
186 186 matplotlib.pyplot.ion()
187 187
188 188 return iplot
189 189
190 190
191 191 def set_linedata(ax, x, y, idline):
192 192
193 193 ax.lines[idline].set_data(x, y)
194 194
195 195
196 196 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
197 197
198 198 ax = iplot.axes
199 199
200 200 printLabels(ax, xlabel, ylabel, title)
201 201
202 202 set_linedata(ax, x, y, idline=0)
203 203
204 204
205 205 def addpline(ax, x, y, color, linestyle, lw):
206 206
207 207 ax.plot(x, y, color=color, linestyle=linestyle, lw=lw)
208 208
209 209
210 210 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
211 211 xlabel='', ylabel='', title='', ticksize=9,
212 212 colormap='jet', cblabel='', cbsize="5%",
213 213 XAxisAsTime=False):
214 214
215 215 matplotlib.pyplot.ioff()
216 216
217 217 divider = make_axes_locatable(ax)
218 218 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
219 219 fig = ax.get_figure()
220 220 fig.add_axes(ax_cb)
221 221
222 222 ax.set_xlim([xmin, xmax])
223 223 ax.set_ylim([ymin, ymax])
224 224
225 225 printLabels(ax, xlabel, ylabel, title)
226 226
227 227 z = numpy.ma.masked_invalid(z)
228 228 cmap = matplotlib.pyplot.get_cmap(colormap)
229 229 cmap.set_bad('black', 1.)
230 230 imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
231 231 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
232 232 cb.set_label(cblabel)
233 233
234 234 # for tl in ax_cb.get_yticklabels():
235 235 # tl.set_visible(True)
236 236
237 237 for tick in ax.yaxis.get_major_ticks():
238 238 tick.label.set_fontsize(ticksize)
239 239
240 240 for tick in ax.xaxis.get_major_ticks():
241 241 tick.label.set_fontsize(ticksize)
242 242
243 243 for tick in cb.ax.get_yticklabels():
244 244 tick.set_fontsize(ticksize)
245 245
246 246 ax_cb.yaxis.tick_right()
247 247
248 248 if '0.' in matplotlib.__version__[0:2]:
249 print "The matplotlib version has to be updated to 1.1 or newer"
249 print("The matplotlib version has to be updated to 1.1 or newer")
250 250 return imesh
251 251
252 252 if '1.0.' in matplotlib.__version__[0:4]:
253 print "The matplotlib version has to be updated to 1.1 or newer"
253 print("The matplotlib version has to be updated to 1.1 or newer")
254 254 return imesh
255 255
256 256 matplotlib.pyplot.tight_layout()
257 257
258 258 if XAxisAsTime:
259 259
260 260 def func(x, pos): return ('%s') % (
261 261 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
262 262 ax.xaxis.set_major_formatter(FuncFormatter(func))
263 263 ax.xaxis.set_major_locator(LinearLocator(7))
264 264
265 265 matplotlib.pyplot.ion()
266 266 return imesh
267 267
268 268
269 269 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
270 270
271 271 z = z.T
272 272 ax = imesh.axes
273 273 printLabels(ax, xlabel, ylabel, title)
274 274 imesh.set_array(z.ravel())
275 275
276 276
277 277 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
278 278
279 279 printLabels(ax, xlabel, ylabel, title)
280 280
281 281 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax,
282 282 cmap=matplotlib.pyplot.get_cmap(colormap))
283 283
284 284
285 285 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
286 286
287 287 printLabels(ax, xlabel, ylabel, title)
288 288
289 289 ax.collections.remove(ax.collections[0])
290 290
291 291 z = numpy.ma.masked_invalid(z)
292 292
293 293 cmap = matplotlib.pyplot.get_cmap(colormap)
294 294 cmap.set_bad('black', 1.)
295 295
296 296 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
297 297
298 298
299 299 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
300 300 ticksize=9, xtick_visible=True, ytick_visible=True,
301 301 nxticks=4, nyticks=10,
302 302 grid=None):
303 303 """
304 304
305 305 Input:
306 306 grid : None, 'both', 'x', 'y'
307 307 """
308 308
309 309 matplotlib.pyplot.ioff()
310 310
311 311 lines = ax.plot(x.T, y)
312 312 leg = ax.legend(lines, legendlabels, loc='upper right')
313 313 leg.get_frame().set_alpha(0.5)
314 314 ax.set_xlim([xmin, xmax])
315 315 ax.set_ylim([ymin, ymax])
316 316 printLabels(ax, xlabel, ylabel, title)
317 317
318 318 xtickspos = numpy.arange(nxticks) * \
319 319 int((xmax - xmin) / (nxticks)) + int(xmin)
320 320 ax.set_xticks(xtickspos)
321 321
322 322 for tick in ax.get_xticklabels():
323 323 tick.set_visible(xtick_visible)
324 324
325 325 for tick in ax.xaxis.get_major_ticks():
326 326 tick.label.set_fontsize(ticksize)
327 327
328 328 for tick in ax.get_yticklabels():
329 329 tick.set_visible(ytick_visible)
330 330
331 331 for tick in ax.yaxis.get_major_ticks():
332 332 tick.label.set_fontsize(ticksize)
333 333
334 334 iplot = ax.lines[-1]
335 335
336 336 if '0.' in matplotlib.__version__[0:2]:
337 print "The matplotlib version has to be updated to 1.1 or newer"
337 print("The matplotlib version has to be updated to 1.1 or newer")
338 338 return iplot
339 339
340 340 if '1.0.' in matplotlib.__version__[0:4]:
341 print "The matplotlib version has to be updated to 1.1 or newer"
341 print("The matplotlib version has to be updated to 1.1 or newer")
342 342 return iplot
343 343
344 344 if grid != None:
345 345 ax.grid(b=True, which='major', axis=grid)
346 346
347 347 matplotlib.pyplot.tight_layout()
348 348
349 349 matplotlib.pyplot.ion()
350 350
351 351 return iplot
352 352
353 353
354 354 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
355 355
356 356 ax = iplot.axes
357 357
358 358 printLabels(ax, xlabel, ylabel, title)
359 359
360 360 for i in range(len(ax.lines)):
361 361 line = ax.lines[i]
362 362 line.set_data(x[i, :], y)
363 363
364 364
365 365 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
366 366 ticksize=9, xtick_visible=True, ytick_visible=True,
367 367 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
368 368 grid=None, XAxisAsTime=False):
369 369 """
370 370
371 371 Input:
372 372 grid : None, 'both', 'x', 'y'
373 373 """
374 374
375 375 matplotlib.pyplot.ioff()
376 376
377 377 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
378 378 lines = ax.plot(x, y.T)
379 379 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
380 380 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
381 381
382 382 leg = ax.legend(lines, legendlabels,
383 383 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
384 384
385 385 for label in leg.get_texts():
386 386 label.set_fontsize(9)
387 387
388 388 ax.set_xlim([xmin, xmax])
389 389 ax.set_ylim([ymin, ymax])
390 390 printLabels(ax, xlabel, ylabel, title)
391 391
392 392 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
393 393 # ax.set_xticks(xtickspos)
394 394
395 395 for tick in ax.get_xticklabels():
396 396 tick.set_visible(xtick_visible)
397 397
398 398 for tick in ax.xaxis.get_major_ticks():
399 399 tick.label.set_fontsize(ticksize)
400 400
401 401 for tick in ax.get_yticklabels():
402 402 tick.set_visible(ytick_visible)
403 403
404 404 for tick in ax.yaxis.get_major_ticks():
405 405 tick.label.set_fontsize(ticksize)
406 406
407 407 iplot = ax.lines[-1]
408 408
409 409 if '0.' in matplotlib.__version__[0:2]:
410 print "The matplotlib version has to be updated to 1.1 or newer"
410 print("The matplotlib version has to be updated to 1.1 or newer")
411 411 return iplot
412 412
413 413 if '1.0.' in matplotlib.__version__[0:4]:
414 print "The matplotlib version has to be updated to 1.1 or newer"
414 print("The matplotlib version has to be updated to 1.1 or newer")
415 415 return iplot
416 416
417 417 if grid != None:
418 418 ax.grid(b=True, which='major', axis=grid)
419 419
420 420 matplotlib.pyplot.tight_layout()
421 421
422 422 if XAxisAsTime:
423 423
424 424 def func(x, pos): return ('%s') % (
425 425 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
426 426 ax.xaxis.set_major_formatter(FuncFormatter(func))
427 427 ax.xaxis.set_major_locator(LinearLocator(7))
428 428
429 429 matplotlib.pyplot.ion()
430 430
431 431 return iplot
432 432
433 433
434 434 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
435 435
436 436 ax = iplot.axes
437 437
438 438 printLabels(ax, xlabel, ylabel, title)
439 439
440 440 for i in range(len(ax.lines)):
441 441 line = ax.lines[i]
442 442 line.set_data(x, y[i, :])
443 443
444 444
445 445 def createPolar(ax, x, y,
446 446 xlabel='', ylabel='', title='', ticksize=9,
447 447 colormap='jet', cblabel='', cbsize="5%",
448 448 XAxisAsTime=False):
449 449
450 450 matplotlib.pyplot.ioff()
451 451
452 452 ax.plot(x, y, 'bo', markersize=5)
453 453 # ax.set_rmax(90)
454 454 ax.set_ylim(0, 90)
455 455 ax.set_yticks(numpy.arange(0, 90, 20))
456 456 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
457 457 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
458 458 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
459 459 ax.yaxis.labelpad = 40
460 460 printLabels(ax, xlabel, ylabel, title)
461 461 iplot = ax.lines[-1]
462 462
463 463 if '0.' in matplotlib.__version__[0:2]:
464 print "The matplotlib version has to be updated to 1.1 or newer"
464 print("The matplotlib version has to be updated to 1.1 or newer")
465 465 return iplot
466 466
467 467 if '1.0.' in matplotlib.__version__[0:4]:
468 print "The matplotlib version has to be updated to 1.1 or newer"
468 print("The matplotlib version has to be updated to 1.1 or newer")
469 469 return iplot
470 470
471 471 # if grid != None:
472 472 # ax.grid(b=True, which='major', axis=grid)
473 473
474 474 matplotlib.pyplot.tight_layout()
475 475
476 476 matplotlib.pyplot.ion()
477 477
478 478 return iplot
479 479
480 480
481 481 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
482 482
483 483 ax = iplot.axes
484 484
485 485 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
486 486 printLabels(ax, xlabel, ylabel, title)
487 487
488 488 set_linedata(ax, x, y, idline=0)
489 489
490 490
491 491 def draw(fig):
492 492
493 493 if type(fig) == 'int':
494 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
494 raise ValueError("Error drawing: Fig parameter should be a matplotlib figure object figure")
495 495
496 496 fig.canvas.draw()
497 497
498 498
499 499 def pause(interval=0.000001):
500 500
501 matplotlib.pyplot.pause(interval)
501 matplotlib.pyplot.pause(interval) No newline at end of file
@@ -1,331 +1,331
1 1 import os
2 2 import sys
3 3 import glob
4 4 import fnmatch
5 5 import datetime
6 6 import time
7 7 import re
8 8 import h5py
9 9 import numpy
10 10
11 11 from scipy.optimize import curve_fit
12 12 from scipy import asarray as ar, exp
13 13 from scipy import stats
14 14
15 15 from duplicity.path import Path
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
33 33 startFp = open(
34 34 '/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc', "rb")
35 35
36 36
37 37 FILE_HEADER = numpy.dtype([ # HEADER 1024bytes
38 38 ('Hname', numpy.str_, 32), # Original file name
39 39 # Date and time when the file was created
40 40 ('Htime', numpy.str_, 32),
41 41 # Name of operator who created the file
42 42 ('Hoper', numpy.str_, 64),
43 43 # Place where the measurements was carried out
44 44 ('Hplace', numpy.str_, 128),
45 45 # Description of measurements
46 46 ('Hdescr', numpy.str_, 256),
47 47 ('Hdummy', numpy.str_, 512), # Reserved space
48 48 # Main chunk
49 49 ('Msign', '<i4'), # Main chunk signature FZKF or NUIG
50 50 ('MsizeData', '<i4'), # Size of data block main chunk
51 51 # Processing DSP parameters
52 52 ('PPARsign', '<i4'), # PPAR signature
53 53 ('PPARsize', '<i4'), # PPAR size of block
54 54 ('PPARprf', '<i4'), # Pulse repetition frequency
55 55 ('PPARpdr', '<i4'), # Pulse duration
56 56 ('PPARsft', '<i4'), # FFT length
57 57 # Number of spectral (in-coherent) averages
58 58 ('PPARavc', '<i4'),
59 59 # Number of lowest range gate for moment estimation
60 60 ('PPARihp', '<i4'),
61 61 # Count for gates for moment estimation
62 62 ('PPARchg', '<i4'),
63 63 # switch on/off polarimetric measurements. Should be 1.
64 64 ('PPARpol', '<i4'),
65 65 # Service DSP parameters
66 66 # STC attenuation on the lowest ranges on/off
67 67 ('SPARatt', '<i4'),
68 68 ('SPARtx', '<i4'), # OBSOLETE
69 69 ('SPARaddGain0', '<f4'), # OBSOLETE
70 70 ('SPARaddGain1', '<f4'), # OBSOLETE
71 71 # Debug only. It normal mode it is 0.
72 72 ('SPARwnd', '<i4'),
73 73 # Delay between sync pulse and tx pulse for phase corr, ns
74 74 ('SPARpos', '<i4'),
75 75 # "add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
76 76 ('SPARadd', '<i4'),
77 77 # Time for measuring txn pulse phase. OBSOLETE
78 78 ('SPARlen', '<i4'),
79 79 ('SPARcal', '<i4'), # OBSOLETE
80 80 ('SPARnos', '<i4'), # OBSOLETE
81 81 ('SPARof0', '<i4'), # detection threshold
82 82 ('SPARof1', '<i4'), # OBSOLETE
83 83 ('SPARswt', '<i4'), # 2nd moment estimation threshold
84 84 ('SPARsum', '<i4'), # OBSOLETE
85 85 ('SPARosc', '<i4'), # flag Oscillosgram mode
86 86 ('SPARtst', '<i4'), # OBSOLETE
87 87 ('SPARcor', '<i4'), # OBSOLETE
88 88 ('SPARofs', '<i4'), # OBSOLETE
89 89 # Hildebrand div noise detection on noise gate
90 90 ('SPARhsn', '<i4'),
91 91 # Hildebrand div noise detection on all gates
92 92 ('SPARhsa', '<f4'),
93 93 ('SPARcalibPow_M', '<f4'), # OBSOLETE
94 94 ('SPARcalibSNR_M', '<f4'), # OBSOLETE
95 95 ('SPARcalibPow_S', '<f4'), # OBSOLETE
96 96 ('SPARcalibSNR_S', '<f4'), # OBSOLETE
97 97 # Lowest range gate for spectra saving Raw_Gate1 >=5
98 98 ('SPARrawGate1', '<i4'),
99 99 # Number of range gates with atmospheric signal
100 100 ('SPARrawGate2', '<i4'),
101 101 # flag - IQ or spectra saving on/off
102 102 ('SPARraw', '<i4'),
103 103 ('SPARprc', '<i4'), ]) # flag - Moment estimation switched on/off
104 104
105 105
106 106 self.Hname = None
107 107 self.Htime = None
108 108 self.Hoper = None
109 109 self.Hplace = None
110 110 self.Hdescr = None
111 111 self.Hdummy = None
112 112
113 113 self.Msign = None
114 114 self.MsizeData = None
115 115
116 116 self.PPARsign = None
117 117 self.PPARsize = None
118 118 self.PPARprf = None
119 119 self.PPARpdr = None
120 120 self.PPARsft = None
121 121 self.PPARavc = None
122 122 self.PPARihp = None
123 123 self.PPARchg = None
124 124 self.PPARpol = None
125 125 # Service DSP parameters
126 126 self.SPARatt = None
127 127 self.SPARtx = None
128 128 self.SPARaddGain0 = None
129 129 self.SPARaddGain1 = None
130 130 self.SPARwnd = None
131 131 self.SPARpos = None
132 132 self.SPARadd = None
133 133 self.SPARlen = None
134 134 self.SPARcal = None
135 135 self.SPARnos = None
136 136 self.SPARof0 = None
137 137 self.SPARof1 = None
138 138 self.SPARswt = None
139 139 self.SPARsum = None
140 140 self.SPARosc = None
141 141 self.SPARtst = None
142 142 self.SPARcor = None
143 143 self.SPARofs = None
144 144 self.SPARhsn = None
145 145 self.SPARhsa = None
146 146 self.SPARcalibPow_M = None
147 147 self.SPARcalibSNR_M = None
148 148 self.SPARcalibPow_S = None
149 149 self.SPARcalibSNR_S = None
150 150 self.SPARrawGate1 = None
151 151 self.SPARrawGate2 = None
152 152 self.SPARraw = None
153 153 self.SPARprc = None
154 154
155 155
156 156 header = numpy.fromfile(fp, FILE_HEADER, 1)
157 157 ''' numpy.fromfile(file, dtype, count, sep='')
158 158 file : file or str
159 159 Open file object or filename.
160 160
161 161 dtype : data-type
162 162 Data type of the returned array. For binary files, it is used to determine
163 163 the size and byte-order of the items in the file.
164 164
165 165 count : int
166 166 Number of items to read. -1 means all items (i.e., the complete file).
167 167
168 168 sep : str
169 169 Separator between items if file is a text file. Empty ("") separator means
170 170 the file should be treated as binary. Spaces (" ") in the separator match zero
171 171 or more whitespace characters. A separator consisting only of spaces must match
172 172 at least one whitespace.
173 173
174 174 '''
175 175
176 176 Hname = str(header['Hname'][0])
177 177 Htime = str(header['Htime'][0])
178 178 Hoper = str(header['Hoper'][0])
179 179 Hplace = str(header['Hplace'][0])
180 180 Hdescr = str(header['Hdescr'][0])
181 181 Hdummy = str(header['Hdummy'][0])
182 182
183 183 Msign = header['Msign'][0]
184 184 MsizeData = header['MsizeData'][0]
185 185
186 186 PPARsign = header['PPARsign'][0]
187 187 PPARsize = header['PPARsize'][0]
188 188 PPARprf = header['PPARprf'][0]
189 189 PPARpdr = header['PPARpdr'][0]
190 190 PPARsft = header['PPARsft'][0]
191 191 PPARavc = header['PPARavc'][0]
192 192 PPARihp = header['PPARihp'][0]
193 193 PPARchg = header['PPARchg'][0]
194 194 PPARpol = header['PPARpol'][0]
195 195 # Service DSP parameters
196 196 SPARatt = header['SPARatt'][0]
197 197 SPARtx = header['SPARtx'][0]
198 198 SPARaddGain0 = header['SPARaddGain0'][0]
199 199 SPARaddGain1 = header['SPARaddGain1'][0]
200 200 SPARwnd = header['SPARwnd'][0]
201 201 SPARpos = header['SPARpos'][0]
202 202 SPARadd = header['SPARadd'][0]
203 203 SPARlen = header['SPARlen'][0]
204 204 SPARcal = header['SPARcal'][0]
205 205 SPARnos = header['SPARnos'][0]
206 206 SPARof0 = header['SPARof0'][0]
207 207 SPARof1 = header['SPARof1'][0]
208 208 SPARswt = header['SPARswt'][0]
209 209 SPARsum = header['SPARsum'][0]
210 210 SPARosc = header['SPARosc'][0]
211 211 SPARtst = header['SPARtst'][0]
212 212 SPARcor = header['SPARcor'][0]
213 213 SPARofs = header['SPARofs'][0]
214 214 SPARhsn = header['SPARhsn'][0]
215 215 SPARhsa = header['SPARhsa'][0]
216 216 SPARcalibPow_M = header['SPARcalibPow_M'][0]
217 217 SPARcalibSNR_M = header['SPARcalibSNR_M'][0]
218 218 SPARcalibPow_S = header['SPARcalibPow_S'][0]
219 219 SPARcalibSNR_S = header['SPARcalibSNR_S'][0]
220 220 SPARrawGate1 = header['SPARrawGate1'][0]
221 221 SPARrawGate2 = header['SPARrawGate2'][0]
222 222 SPARraw = header['SPARraw'][0]
223 223 SPARprc = header['SPARprc'][0]
224 224
225 225
226 226 SRVI_STRUCTURE = numpy.dtype([
227 227 ('frame_cnt', '<u4'),
228 228 ('time_t', '<u4'), #
229 229 ('tpow', '<f4'), #
230 230 ('npw1', '<f4'), #
231 231 ('npw2', '<f4'), #
232 232 ('cpw1', '<f4'), #
233 233 ('pcw2', '<f4'), #
234 234 ('ps_err', '<u4'), #
235 235 ('te_err', '<u4'), #
236 236 ('rc_err', '<u4'), #
237 237 ('grs1', '<u4'), #
238 238 ('grs2', '<u4'), #
239 239 ('azipos', '<f4'), #
240 240 ('azivel', '<f4'), #
241 241 ('elvpos', '<f4'), #
242 242 ('elvvel', '<f4'), #
243 243 ('northAngle', '<f4'),
244 244 ('microsec', '<u4'), #
245 245 ('azisetvel', '<f4'), #
246 246 ('elvsetpos', '<f4'), #
247 247 ('RadarConst', '<f4'), ]) #
248 248
249 249 JUMP_STRUCTURE = numpy.dtype([
250 250 ('jump', '<u140'),
251 251 ('SizeOfDataBlock1', numpy.str_, 32),
252 252 ('jump', '<i4'),
253 253 ('DataBlockTitleSRVI1', numpy.str_, 32),
254 254 ('SizeOfSRVI1', '<i4'), ])
255 255
256 256
257 257 # frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
258 258 # cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
259 259 # grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
260 260 # microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0
261 261
262 262
263 263 frame_cnt = frame_cnt
264 264 dwell = time_t
265 265 tpow = tpow
266 266 npw1 = npw1
267 267 npw2 = npw2
268 268 cpw1 = cpw1
269 269 pcw2 = pcw2
270 270 ps_err = ps_err
271 271 te_err = te_err
272 272 rc_err = rc_err
273 273 grs1 = grs1
274 274 grs2 = grs2
275 275 azipos = azipos
276 276 azivel = azivel
277 277 elvpos = elvpos
278 278 elvvel = elvvel
279 279 northAngle = northAngle
280 280 microsec = microsec
281 281 azisetvel = azisetvel
282 282 elvsetpos = elvsetpos
283 283 RadarConst5 = RadarConst
284 284
285 285
286 286 # print fp
287 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 288 # startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
289 289 # RecCounter=0
290 290 # Off2StartNxtRec=811248
291 291 # print 'OffsetStartHeader ',self.OffsetStartHeader,'RecCounter ', self.RecCounter, 'Off2StartNxtRec ' , self.Off2StartNxtRec
292 292 #OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
293 293 #startFp.seek(OffRHeader, os.SEEK_SET)
294 print 'debe ser 48, RecCounter*811248', self.OffsetStartHeader, self.RecCounter, self.Off2StartNxtRec
295 print 'Posicion del bloque: ', OffRHeader
294 print('debe ser 48, RecCounter*811248', self.OffsetStartHeader, self.RecCounter, self.Off2StartNxtRec)
295 print('Posicion del bloque: ', OffRHeader)
296 296
297 297 header = numpy.fromfile(startFp, SRVI_STRUCTURE, 1)
298 298
299 299 self.frame_cnt = header['frame_cnt'][0]
300 300 self.time_t = header['frame_cnt'][0] #
301 301 self.tpow = header['frame_cnt'][0] #
302 302 self.npw1 = header['frame_cnt'][0] #
303 303 self.npw2 = header['frame_cnt'][0] #
304 304 self.cpw1 = header['frame_cnt'][0] #
305 305 self.pcw2 = header['frame_cnt'][0] #
306 306 self.ps_err = header['frame_cnt'][0] #
307 307 self.te_err = header['frame_cnt'][0] #
308 308 self.rc_err = header['frame_cnt'][0] #
309 309 self.grs1 = header['frame_cnt'][0] #
310 310 self.grs2 = header['frame_cnt'][0] #
311 311 self.azipos = header['frame_cnt'][0] #
312 312 self.azivel = header['frame_cnt'][0] #
313 313 self.elvpos = header['frame_cnt'][0] #
314 314 self.elvvel = header['frame_cnt'][0] #
315 315 self.northAngle = header['frame_cnt'][0] #
316 316 self.microsec = header['frame_cnt'][0] #
317 317 self.azisetvel = header['frame_cnt'][0] #
318 318 self.elvsetpos = header['frame_cnt'][0] #
319 319 self.RadarConst = header['frame_cnt'][0] #
320 320
321 321
322 322 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
323 323
324 324 self.RHsize = 180 + 20 * self.nChannels
325 325 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
326 326 # print 'Datasize',self.Datasize
327 327 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
328 328
329 print '=============================================='
329 print('==============================================')
330 330
331 print '=============================================='
331 print('==============================================') No newline at end of file
@@ -1,23 +1,23
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 5 '''
6 6
7 from jroIO_voltage import *
8 from jroIO_spectra import *
9 from jroIO_heispectra import *
10 from jroIO_usrp import *
11 from jroIO_digitalRF import *
12 from jroIO_kamisr import *
13 from jroIO_param import *
14 from jroIO_hf import *
7 from .jroIO_voltage import *
8 from .jroIO_spectra import *
9 from .jroIO_heispectra import *
10 from .jroIO_usrp import *
11 from .jroIO_digitalRF import *
12 from .jroIO_kamisr import *
13 from .jroIO_param import *
14 from .jroIO_hf import *
15 15
16 from jroIO_madrigal import *
16 from .jroIO_madrigal import *
17 17
18 from bltrIO_param import *
19 from jroIO_bltr import *
20 from jroIO_mira35c import *
21 from julIO_param import *
18 from .bltrIO_param import *
19 from .jroIO_bltr import *
20 from .jroIO_mira35c import *
21 from .julIO_param import *
22 22
23 from pxIO_param import * No newline at end of file
23 from .pxIO_param import * No newline at end of file
@@ -1,369 +1,369
1 1 '''
2 2 Created on Nov 9, 2016
3 3
4 4 @author: roj- LouVD
5 5 '''
6 6
7 7
8 8 import os
9 9 import sys
10 10 import time
11 11 import glob
12 12 import datetime
13 13
14 14 import numpy
15 15
16 16 from schainpy.model.proc.jroproc_base import ProcessingUnit
17 17 from schainpy.model.data.jrodata import Parameters
18 18 from schainpy.model.io.jroIO_base import JRODataReader, isNumber
19 19 from schainpy.utils import log
20 20
21 21 FILE_HEADER_STRUCTURE = numpy.dtype([
22 22 ('FMN', '<u4'),
23 23 ('nrec', '<u4'),
24 24 ('fr_offset', '<u4'),
25 25 ('id', '<u4'),
26 26 ('site', 'u1', (32,))
27 27 ])
28 28
29 29 REC_HEADER_STRUCTURE = numpy.dtype([
30 30 ('rmn', '<u4'),
31 31 ('rcounter', '<u4'),
32 32 ('nr_offset', '<u4'),
33 33 ('tr_offset', '<u4'),
34 34 ('time', '<u4'),
35 35 ('time_msec', '<u4'),
36 36 ('tag', 'u1', (32,)),
37 37 ('comments', 'u1', (32,)),
38 38 ('lat', '<f4'),
39 39 ('lon', '<f4'),
40 40 ('gps_status', '<u4'),
41 41 ('freq', '<u4'),
42 42 ('freq0', '<u4'),
43 43 ('nchan', '<u4'),
44 44 ('delta_r', '<u4'),
45 45 ('nranges', '<u4'),
46 46 ('r0', '<u4'),
47 47 ('prf', '<u4'),
48 48 ('ncoh', '<u4'),
49 49 ('npoints', '<u4'),
50 50 ('polarization', '<i4'),
51 51 ('rx_filter', '<u4'),
52 52 ('nmodes', '<u4'),
53 53 ('dmode_index', '<u4'),
54 54 ('dmode_rngcorr', '<u4'),
55 55 ('nrxs', '<u4'),
56 56 ('acf_length', '<u4'),
57 57 ('acf_lags', '<u4'),
58 58 ('sea_to_atmos', '<f4'),
59 59 ('sea_notch', '<u4'),
60 60 ('lh_sea', '<u4'),
61 61 ('hh_sea', '<u4'),
62 62 ('nbins_sea', '<u4'),
63 63 ('min_snr', '<f4'),
64 64 ('min_cc', '<f4'),
65 65 ('max_time_diff', '<f4')
66 66 ])
67 67
68 68 DATA_STRUCTURE = numpy.dtype([
69 69 ('range', '<u4'),
70 70 ('status', '<u4'),
71 71 ('zonal', '<f4'),
72 72 ('meridional', '<f4'),
73 73 ('vertical', '<f4'),
74 74 ('zonal_a', '<f4'),
75 75 ('meridional_a', '<f4'),
76 76 ('corrected_fading', '<f4'), # seconds
77 77 ('uncorrected_fading', '<f4'), # seconds
78 78 ('time_diff', '<f4'),
79 79 ('major_axis', '<f4'),
80 80 ('axial_ratio', '<f4'),
81 81 ('orientation', '<f4'),
82 82 ('sea_power', '<u4'),
83 83 ('sea_algorithm', '<u4')
84 84 ])
85 85
86 86
87 87 class BLTRParamReader(JRODataReader, ProcessingUnit):
88 88 '''
89 89 Boundary Layer and Tropospheric Radar (BLTR) reader, Wind velocities and SNR from *.sswma files
90 90 '''
91 91
92 92 ext = '.sswma'
93 93
94 94 def __init__(self, **kwargs):
95 95
96 96 ProcessingUnit.__init__(self, **kwargs)
97 97
98 98 self.dataOut = Parameters()
99 99 self.counter_records = 0
100 100 self.flagNoMoreFiles = 0
101 101 self.isConfig = False
102 102 self.filename = None
103 103
104 104 def setup(self,
105 105 path=None,
106 106 startDate=None,
107 107 endDate=None,
108 108 ext=None,
109 109 startTime=datetime.time(0, 0, 0),
110 110 endTime=datetime.time(23, 59, 59),
111 111 timezone=0,
112 112 status_value=0,
113 113 **kwargs):
114 114
115 115 self.path = path
116 116 self.startDate = startDate
117 117 self.endDate = endDate
118 118 self.startTime = startTime
119 119 self.endTime = endTime
120 120 self.status_value = status_value
121 121 self.datatime = datetime.datetime(1900,1,1)
122 122
123 123 if self.path is None:
124 raise ValueError, "The path is not valid"
124 raise ValueError("The path is not valid")
125 125
126 126 if ext is None:
127 127 ext = self.ext
128 128
129 129 self.search_files(self.path, startDate, endDate, ext)
130 130 self.timezone = timezone
131 131 self.fileIndex = 0
132 132
133 133 if not self.fileList:
134 raise Warning, "There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % (
135 path)
134 raise Warning("There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % (
135 path))
136 136
137 137 self.setNextFile()
138 138
139 139 def search_files(self, path, startDate, endDate, ext):
140 140 '''
141 141 Searching for BLTR rawdata file in path
142 142 Creating a list of file to proces included in [startDate,endDate]
143 143
144 144 Input:
145 145 path - Path to find BLTR rawdata files
146 146 startDate - Select file from this date
147 147 enDate - Select file until this date
148 148 ext - Extension of the file to read
149 149 '''
150 150
151 151 log.success('Searching files in {} '.format(path), 'BLTRParamReader')
152 152 foldercounter = 0
153 153 fileList0 = glob.glob1(path, "*%s" % ext)
154 154 fileList0.sort()
155 155
156 156 self.fileList = []
157 157 self.dateFileList = []
158 158
159 159 for thisFile in fileList0:
160 160 year = thisFile[-14:-10]
161 161 if not isNumber(year):
162 162 continue
163 163
164 164 month = thisFile[-10:-8]
165 165 if not isNumber(month):
166 166 continue
167 167
168 168 day = thisFile[-8:-6]
169 169 if not isNumber(day):
170 170 continue
171 171
172 172 year, month, day = int(year), int(month), int(day)
173 173 dateFile = datetime.date(year, month, day)
174 174
175 175 if (startDate > dateFile) or (endDate < dateFile):
176 176 continue
177 177
178 178 self.fileList.append(thisFile)
179 179 self.dateFileList.append(dateFile)
180 180
181 181 return
182 182
183 183 def setNextFile(self):
184 184
185 185 file_id = self.fileIndex
186 186
187 187 if file_id == len(self.fileList):
188 188 log.success('No more files in the folder', 'BLTRParamReader')
189 189 self.flagNoMoreFiles = 1
190 190 return 0
191 191
192 192 log.success('Opening {}'.format(self.fileList[file_id]), 'BLTRParamReader')
193 193 filename = os.path.join(self.path, self.fileList[file_id])
194 194
195 195 dirname, name = os.path.split(filename)
196 196 # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya
197 197 self.siteFile = name.split('.')[0]
198 198 if self.filename is not None:
199 199 self.fp.close()
200 200 self.filename = filename
201 201 self.fp = open(self.filename, 'rb')
202 202 self.header_file = numpy.fromfile(self.fp, FILE_HEADER_STRUCTURE, 1)
203 203 self.nrecords = self.header_file['nrec'][0]
204 204 self.sizeOfFile = os.path.getsize(self.filename)
205 205 self.counter_records = 0
206 206 self.flagIsNewFile = 0
207 207 self.fileIndex += 1
208 208
209 209 return 1
210 210
211 211 def readNextBlock(self):
212 212
213 213 while True:
214 214 if self.counter_records == self.nrecords:
215 215 self.flagIsNewFile = 1
216 216 if not self.setNextFile():
217 217 return 0
218 218
219 219 self.readBlock()
220 220
221 221 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
222 222 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
223 223 log.warning(
224 224 'Reading Record No. {}/{} -> {} [Skipping]'.format(
225 225 self.counter_records,
226 226 self.nrecords,
227 227 self.datatime.ctime()),
228 228 'BLTRParamReader')
229 229 continue
230 230 break
231 231
232 232 log.log('Reading Record No. {}/{} -> {}'.format(
233 233 self.counter_records,
234 234 self.nrecords,
235 235 self.datatime.ctime()), 'BLTRParamReader')
236 236
237 237 return 1
238 238
239 239 def readBlock(self):
240 240
241 241 pointer = self.fp.tell()
242 242 header_rec = numpy.fromfile(self.fp, REC_HEADER_STRUCTURE, 1)
243 243 self.nchannels = header_rec['nchan'][0] / 2
244 244 self.kchan = header_rec['nrxs'][0]
245 245 self.nmodes = header_rec['nmodes'][0]
246 246 self.nranges = header_rec['nranges'][0]
247 247 self.fp.seek(pointer)
248 248 self.height = numpy.empty((self.nmodes, self.nranges))
249 249 self.snr = numpy.empty((self.nmodes, self.nchannels, self.nranges))
250 250 self.buffer = numpy.empty((self.nmodes, 3, self.nranges))
251 251 self.flagDiscontinuousBlock = 0
252 252
253 253 for mode in range(self.nmodes):
254 254 self.readHeader()
255 255 data = self.readData()
256 256 self.height[mode] = (data[0] - self.correction) / 1000.
257 257 self.buffer[mode] = data[1]
258 258 self.snr[mode] = data[2]
259 259
260 260 self.counter_records = self.counter_records + self.nmodes
261 261
262 262 return
263 263
264 264 def readHeader(self):
265 265 '''
266 266 RecordHeader of BLTR rawdata file
267 267 '''
268 268
269 269 header_structure = numpy.dtype(
270 270 REC_HEADER_STRUCTURE.descr + [
271 271 ('antenna_coord', 'f4', (2, self.nchannels)),
272 272 ('rx_gains', 'u4', (self.nchannels,)),
273 273 ('rx_analysis', 'u4', (self.nchannels,))
274 274 ]
275 275 )
276 276
277 277 self.header_rec = numpy.fromfile(self.fp, header_structure, 1)
278 278 self.lat = self.header_rec['lat'][0]
279 279 self.lon = self.header_rec['lon'][0]
280 280 self.delta = self.header_rec['delta_r'][0]
281 281 self.correction = self.header_rec['dmode_rngcorr'][0]
282 282 self.imode = self.header_rec['dmode_index'][0]
283 283 self.antenna = self.header_rec['antenna_coord']
284 284 self.rx_gains = self.header_rec['rx_gains']
285 285 self.time = self.header_rec['time'][0]
286 286 dt = datetime.datetime.utcfromtimestamp(self.time)
287 287 if dt.date()>self.datatime.date():
288 288 self.flagDiscontinuousBlock = 1
289 289 self.datatime = dt
290 290
291 291 def readData(self):
292 292 '''
293 293 Reading and filtering data block record of BLTR rawdata file, filtering is according to status_value.
294 294
295 295 Input:
296 296 status_value - Array data is set to NAN for values that are not equal to status_value
297 297
298 298 '''
299 299
300 300 data_structure = numpy.dtype(
301 301 DATA_STRUCTURE.descr + [
302 302 ('rx_saturation', 'u4', (self.nchannels,)),
303 303 ('chan_offset', 'u4', (2 * self.nchannels,)),
304 304 ('rx_amp', 'u4', (self.nchannels,)),
305 305 ('rx_snr', 'f4', (self.nchannels,)),
306 306 ('cross_snr', 'f4', (self.kchan,)),
307 307 ('sea_power_relative', 'f4', (self.kchan,))]
308 308 )
309 309
310 310 data = numpy.fromfile(self.fp, data_structure, self.nranges)
311 311
312 312 height = data['range']
313 313 winds = numpy.array(
314 314 (data['zonal'], data['meridional'], data['vertical']))
315 315 snr = data['rx_snr'].T
316 316
317 317 winds[numpy.where(winds == -9999.)] = numpy.nan
318 318 winds[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
319 319 snr[numpy.where(snr == -9999.)] = numpy.nan
320 320 snr[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
321 321 snr = numpy.power(10, snr / 10)
322 322
323 323 return height, winds, snr
324 324
325 325 def set_output(self):
326 326 '''
327 327 Storing data from databuffer to dataOut object
328 328 '''
329 329
330 330 self.dataOut.data_SNR = self.snr
331 331 self.dataOut.height = self.height
332 332 self.dataOut.data = self.buffer
333 333 self.dataOut.utctimeInit = self.time
334 334 self.dataOut.utctime = self.dataOut.utctimeInit
335 335 self.dataOut.useLocalTime = False
336 336 self.dataOut.paramInterval = 157
337 337 self.dataOut.timezone = self.timezone
338 338 self.dataOut.site = self.siteFile
339 339 self.dataOut.nrecords = self.nrecords / self.nmodes
340 340 self.dataOut.sizeOfFile = self.sizeOfFile
341 341 self.dataOut.lat = self.lat
342 342 self.dataOut.lon = self.lon
343 self.dataOut.channelList = range(self.nchannels)
343 self.dataOut.channelList = list(range(self.nchannels))
344 344 self.dataOut.kchan = self.kchan
345 345 self.dataOut.delta = self.delta
346 346 self.dataOut.correction = self.correction
347 347 self.dataOut.nmodes = self.nmodes
348 348 self.dataOut.imode = self.imode
349 349 self.dataOut.antenna = self.antenna
350 350 self.dataOut.rx_gains = self.rx_gains
351 351 self.dataOut.flagNoData = False
352 352 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
353 353
354 354 def getData(self):
355 355 '''
356 356 Storing data from databuffer to dataOut object
357 357 '''
358 358 if self.flagNoMoreFiles:
359 359 self.dataOut.flagNoData = True
360 360 log.success('No file left to process', 'BLTRParamReader')
361 361 return 0
362 362
363 363 if not self.readNextBlock():
364 364 self.dataOut.flagNoData = True
365 365 return 0
366 366
367 367 self.set_output()
368 368
369 return 1
369 return 1 No newline at end of file
@@ -1,692 +1,692
1 1 '''
2 2 @author: Daniel Suarez
3 3 '''
4 4
5 5 import os
6 6 import sys
7 7 import glob
8 8 import fnmatch
9 9 import datetime
10 10 import time
11 11 import re
12 12 import h5py
13 13 import numpy
14 14
15 15 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
16 16 from schainpy.model.data.jroamisr import AMISR
17 17
18 18 try:
19 19 from gevent import sleep
20 20 except:
21 21 from time import sleep
22 22
23 23 class RadacHeader():
24 24 def __init__(self, fp):
25 25 header = 'Raw11/Data/RadacHeader'
26 26 self.beamCodeByPulse = fp.get(header+'/BeamCode')
27 27 self.beamCode = fp.get('Raw11/Data/Beamcodes')
28 28 self.code = fp.get(header+'/Code')
29 29 self.frameCount = fp.get(header+'/FrameCount')
30 30 self.modeGroup = fp.get(header+'/ModeGroup')
31 31 self.nsamplesPulse = fp.get(header+'/NSamplesPulse')
32 32 self.pulseCount = fp.get(header+'/PulseCount')
33 33 self.radacTime = fp.get(header+'/RadacTime')
34 34 self.timeCount = fp.get(header+'/TimeCount')
35 35 self.timeStatus = fp.get(header+'/TimeStatus')
36 36
37 37 self.nrecords = self.pulseCount.shape[0] #nblocks
38 38 self.npulses = self.pulseCount.shape[1] #nprofile
39 39 self.nsamples = self.nsamplesPulse[0,0] #ngates
40 40 self.nbeams = self.beamCode.shape[1]
41 41
42 42
43 43 def getIndexRangeToPulse(self, idrecord=0):
44 44 #indexToZero = numpy.where(self.pulseCount.value[idrecord,:]==0)
45 45 #startPulseCountId = indexToZero[0][0]
46 46 #endPulseCountId = startPulseCountId - 1
47 47 #range1 = numpy.arange(startPulseCountId,self.npulses,1)
48 48 #range2 = numpy.arange(0,startPulseCountId,1)
49 49 #return range1, range2
50 50 zero = 0
51 51 npulse = max(self.pulseCount[0,:]+1)-1
52 52 looking_index = numpy.where(self.pulseCount.value[idrecord,:]==npulse)[0]
53 53 getLastIndex = looking_index[-1]
54 54 index_data = numpy.arange(0,getLastIndex+1,1)
55 55 index_buffer = numpy.arange(getLastIndex+1,self.npulses,1)
56 56 return index_data, index_buffer
57 57
58 58 class AMISRReader(ProcessingUnit):
59 59
60 60 path = None
61 61 startDate = None
62 62 endDate = None
63 63 startTime = None
64 64 endTime = None
65 65 walk = None
66 66 isConfig = False
67 67
68 68 def __init__(self):
69 69 self.set = None
70 70 self.subset = None
71 71 self.extension_file = '.h5'
72 72 self.dtc_str = 'dtc'
73 73 self.dtc_id = 0
74 74 self.status = True
75 75 self.isConfig = False
76 76 self.dirnameList = []
77 77 self.filenameList = []
78 78 self.fileIndex = None
79 79 self.flagNoMoreFiles = False
80 80 self.flagIsNewFile = 0
81 81 self.filename = ''
82 82 self.amisrFilePointer = None
83 83 self.radacHeaderObj = None
84 84 self.dataOut = self.__createObjByDefault()
85 85 self.datablock = None
86 86 self.rest_datablock = None
87 87 self.range = None
88 88 self.idrecord_count = 0
89 89 self.profileIndex = 0
90 90 self.index_amisr_sample = None
91 91 self.index_amisr_buffer = None
92 92 self.beamCodeByFrame = None
93 93 self.radacTimeByFrame = None
94 94 #atributos originales tal y como esta en el archivo de datos
95 95 self.beamCodesFromFile = None
96 96 self.radacTimeFromFile = None
97 97 self.rangeFromFile = None
98 98 self.dataByFrame = None
99 99 self.dataset = None
100 100
101 101 self.beamCodeDict = {}
102 102 self.beamRangeDict = {}
103 103
104 104 #experiment cgf file
105 105 self.npulsesint_fromfile = None
106 106 self.recordsperfile_fromfile = None
107 107 self.nbeamcodes_fromfile = None
108 108 self.ngates_fromfile = None
109 109 self.ippSeconds_fromfile = None
110 110 self.frequency_h5file = None
111 111
112 112
113 113 self.__firstFile = True
114 114 self.buffer_radactime = None
115 115
116 116 self.index4_schain_datablock = None
117 117 self.index4_buffer = None
118 118 self.schain_datablock = None
119 119 self.buffer = None
120 120 self.linear_pulseCount = None
121 121 self.npulseByFrame = None
122 122 self.profileIndex_offset = None
123 123 self.timezone = 'ut'
124 124
125 125 self.__waitForNewFile = 20
126 126 self.__filename_online = None
127 127
128 128 def __createObjByDefault(self):
129 129
130 130 dataObj = AMISR()
131 131
132 132 return dataObj
133 133
134 134 def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''):
135 135 self.path = path
136 136 self.startDate = startDate
137 137 self.endDate = endDate
138 138 self.startTime = startTime
139 139 self.endTime = endTime
140 140 self.walk = walk
141 141
142 142 def __checkPath(self):
143 143 if os.path.exists(self.path):
144 144 self.status = 1
145 145 else:
146 146 self.status = 0
147 print 'Path:%s does not exists'%self.path
147 print('Path:%s does not exists'%self.path)
148 148
149 149 return
150 150
151 151 def __selDates(self, amisr_dirname_format):
152 152 try:
153 153 year = int(amisr_dirname_format[0:4])
154 154 month = int(amisr_dirname_format[4:6])
155 155 dom = int(amisr_dirname_format[6:8])
156 156 thisDate = datetime.date(year,month,dom)
157 157
158 158 if (thisDate>=self.startDate and thisDate <= self.endDate):
159 159 return amisr_dirname_format
160 160 except:
161 161 return None
162 162
163 163 def __findDataForDates(self,online=False):
164 164
165 165
166 166
167 167 if not(self.status):
168 168 return None
169 169
170 170 pat = '\d+.\d+'
171 171 dirnameList = [re.search(pat,x) for x in os.listdir(self.path)]
172 dirnameList = filter(lambda x:x!=None,dirnameList)
172 dirnameList = [x for x in dirnameList if x!=None]
173 173 dirnameList = [x.string for x in dirnameList]
174 174 if not(online):
175 175 dirnameList = [self.__selDates(x) for x in dirnameList]
176 dirnameList = filter(lambda x:x!=None,dirnameList)
176 dirnameList = [x for x in dirnameList if x!=None]
177 177 if len(dirnameList)>0:
178 178 self.status = 1
179 179 self.dirnameList = dirnameList
180 180 self.dirnameList.sort()
181 181 else:
182 182 self.status = 0
183 183 return None
184 184
185 185 def __getTimeFromData(self):
186 186 startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime)
187 187 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
188 188
189 print 'Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader)
190 print '........................................'
189 print('Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader))
190 print('........................................')
191 191 filter_filenameList = []
192 192 self.filenameList.sort()
193 193 for i in range(len(self.filenameList)-1):
194 194 filename = self.filenameList[i]
195 195 fp = h5py.File(filename,'r')
196 196 time_str = fp.get('Time/RadacTimeString')
197 197
198 198 startDateTimeStr_File = time_str[0][0].split('.')[0]
199 199 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
200 200 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
201 201
202 202 endDateTimeStr_File = time_str[-1][-1].split('.')[0]
203 203 junk = time.strptime(endDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
204 204 endDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
205 205
206 206 fp.close()
207 207
208 208 if self.timezone == 'lt':
209 209 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
210 210 endDateTime_File = endDateTime_File - datetime.timedelta(minutes = 300)
211 211
212 212 if (endDateTime_File>=startDateTime_Reader and endDateTime_File<endDateTime_Reader):
213 213 #self.filenameList.remove(filename)
214 214 filter_filenameList.append(filename)
215 215
216 216 filter_filenameList.sort()
217 217 self.filenameList = filter_filenameList
218 218 return 1
219 219
220 220 def __filterByGlob1(self, dirName):
221 221 filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file)
222 222 filterDict = {}
223 223 filterDict.setdefault(dirName)
224 224 filterDict[dirName] = filter_files
225 225 return filterDict
226 226
227 227 def __getFilenameList(self, fileListInKeys, dirList):
228 228 for value in fileListInKeys:
229 dirName = value.keys()[0]
229 dirName = list(value.keys())[0]
230 230 for file in value[dirName]:
231 231 filename = os.path.join(dirName, file)
232 232 self.filenameList.append(filename)
233 233
234 234
235 235 def __selectDataForTimes(self, online=False):
236 236 #aun no esta implementado el filtro for tiempo
237 237 if not(self.status):
238 238 return None
239 239
240 240 dirList = [os.path.join(self.path,x) for x in self.dirnameList]
241 241
242 242 fileListInKeys = [self.__filterByGlob1(x) for x in dirList]
243 243
244 244 self.__getFilenameList(fileListInKeys, dirList)
245 245 if not(online):
246 246 #filtro por tiempo
247 247 if not(self.all):
248 248 self.__getTimeFromData()
249 249
250 250 if len(self.filenameList)>0:
251 251 self.status = 1
252 252 self.filenameList.sort()
253 253 else:
254 254 self.status = 0
255 255 return None
256 256
257 257 else:
258 258 #get the last file - 1
259 259 self.filenameList = [self.filenameList[-2]]
260 260
261 261 new_dirnameList = []
262 262 for dirname in self.dirnameList:
263 263 junk = numpy.array([dirname in x for x in self.filenameList])
264 264 junk_sum = junk.sum()
265 265 if junk_sum > 0:
266 266 new_dirnameList.append(dirname)
267 267 self.dirnameList = new_dirnameList
268 268 return 1
269 269
270 270 def searchFilesOnLine(self,
271 271 path,
272 272 walk=True):
273 273
274 274 startDate = datetime.datetime.utcnow().date()
275 275 endDate = datetime.datetime.utcnow().date()
276 276
277 277 self.__setParameters(path=path, startDate=startDate, endDate=endDate, walk=walk)
278 278
279 279 self.__checkPath()
280 280
281 281 self.__findDataForDates(online=True)
282 282
283 283 self.dirnameList = [self.dirnameList[-1]]
284 284
285 285 self.__selectDataForTimes(online=True)
286 286
287 287 return
288 288
289 289
290 290 def searchFilesOffLine(self,
291 291 path,
292 292 startDate,
293 293 endDate,
294 294 startTime=datetime.time(0,0,0),
295 295 endTime=datetime.time(23,59,59),
296 296 walk=True):
297 297
298 298 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
299 299
300 300 self.__checkPath()
301 301
302 302 self.__findDataForDates()
303 303
304 304 self.__selectDataForTimes()
305 305
306 306 for i in range(len(self.filenameList)):
307 print "%s" %(self.filenameList[i])
307 print("%s" %(self.filenameList[i]))
308 308
309 309 return
310 310
311 311 def __setNextFileOffline(self):
312 312 idFile = self.fileIndex
313 313
314 314 while (True):
315 315 idFile += 1
316 316 if not(idFile < len(self.filenameList)):
317 317 self.flagNoMoreFiles = 1
318 print "No more Files"
318 print("No more Files")
319 319 return 0
320 320
321 321 filename = self.filenameList[idFile]
322 322
323 323 amisrFilePointer = h5py.File(filename,'r')
324 324
325 325 break
326 326
327 327 self.flagIsNewFile = 1
328 328 self.fileIndex = idFile
329 329 self.filename = filename
330 330
331 331 self.amisrFilePointer = amisrFilePointer
332 332
333 print "Setting the file: %s"%self.filename
333 print("Setting the file: %s"%self.filename)
334 334
335 335 return 1
336 336
337 337
338 338 def __setNextFileOnline(self):
339 339 filename = self.filenameList[0]
340 340 if self.__filename_online != None:
341 341 self.__selectDataForTimes(online=True)
342 342 filename = self.filenameList[0]
343 343 while self.__filename_online == filename:
344 print 'waiting %d seconds to get a new file...'%(self.__waitForNewFile)
344 print('waiting %d seconds to get a new file...'%(self.__waitForNewFile))
345 345 sleep(self.__waitForNewFile)
346 346 self.__selectDataForTimes(online=True)
347 347 filename = self.filenameList[0]
348 348
349 349 self.__filename_online = filename
350 350
351 351 self.amisrFilePointer = h5py.File(filename,'r')
352 352 self.flagIsNewFile = 1
353 353 self.filename = filename
354 print "Setting the file: %s"%self.filename
354 print("Setting the file: %s"%self.filename)
355 355 return 1
356 356
357 357
358 358 def __readHeader(self):
359 359 self.radacHeaderObj = RadacHeader(self.amisrFilePointer)
360 360
361 361 #update values from experiment cfg file
362 362 if self.radacHeaderObj.nrecords == self.recordsperfile_fromfile:
363 363 self.radacHeaderObj.nrecords = self.recordsperfile_fromfile
364 364 self.radacHeaderObj.nbeams = self.nbeamcodes_fromfile
365 365 self.radacHeaderObj.npulses = self.npulsesint_fromfile
366 366 self.radacHeaderObj.nsamples = self.ngates_fromfile
367 367
368 368 #looking index list for data
369 369 start_index = self.radacHeaderObj.pulseCount[0,:][0]
370 370 end_index = self.radacHeaderObj.npulses
371 range4data = range(start_index, end_index)
371 range4data = list(range(start_index, end_index))
372 372 self.index4_schain_datablock = numpy.array(range4data)
373 373
374 374 buffer_start_index = 0
375 375 buffer_end_index = self.radacHeaderObj.pulseCount[0,:][0]
376 range4buffer = range(buffer_start_index, buffer_end_index)
376 range4buffer = list(range(buffer_start_index, buffer_end_index))
377 377 self.index4_buffer = numpy.array(range4buffer)
378 378
379 379 self.linear_pulseCount = numpy.array(range4data + range4buffer)
380 380 self.npulseByFrame = max(self.radacHeaderObj.pulseCount[0,:]+1)
381 381
382 382 #get tuning frequency
383 383 frequency_h5file_dataset = self.amisrFilePointer.get('Rx'+'/TuningFrequency')
384 384 self.frequency_h5file = frequency_h5file_dataset[0,0]
385 385
386 386 self.flagIsNewFile = 1
387 387
388 388 def __getBeamCode(self):
389 389 self.beamCodeDict = {}
390 390 self.beamRangeDict = {}
391 391
392 392 beamCodeMap = self.amisrFilePointer.get('Setup/BeamcodeMap')
393 393
394 394 for i in range(len(self.radacHeaderObj.beamCode[0,:])):
395 395 self.beamCodeDict.setdefault(i)
396 396 self.beamRangeDict.setdefault(i)
397 397 beamcodeValue = self.radacHeaderObj.beamCode[0,i]
398 398 beamcodeIndex = numpy.where(beamCodeMap[:,0] == beamcodeValue)[0][0]
399 399 x = beamCodeMap[beamcodeIndex][1]
400 400 y = beamCodeMap[beamcodeIndex][2]
401 401 z = beamCodeMap[beamcodeIndex][3]
402 402 self.beamCodeDict[i] = [beamcodeValue, x, y, z]
403 403
404 404 just4record0 = self.radacHeaderObj.beamCodeByPulse[0,:]
405 405
406 for i in range(len(self.beamCodeDict.values())):
407 xx = numpy.where(just4record0==self.beamCodeDict.values()[i][0])
406 for i in range(len(list(self.beamCodeDict.values()))):
407 xx = numpy.where(just4record0==list(self.beamCodeDict.values())[i][0])
408 408 indexPulseByBeam = self.linear_pulseCount[xx[0]]
409 409 self.beamRangeDict[i] = indexPulseByBeam
410 410
411 411 def __getExpParameters(self):
412 412 if not(self.status):
413 413 return None
414 414
415 415 experimentCfgPath = os.path.join(self.path, self.dirnameList[0], 'Setup')
416 416
417 417 expFinder = glob.glob1(experimentCfgPath,'*.exp')
418 418 if len(expFinder)== 0:
419 419 self.status = 0
420 420 return None
421 421
422 422 experimentFilename = os.path.join(experimentCfgPath,expFinder[0])
423 423
424 424 f = open(experimentFilename)
425 425 lines = f.readlines()
426 426 f.close()
427 427
428 428 parmsList = ['npulsesint*','recordsperfile*','nbeamcodes*','ngates*']
429 429 filterList = [fnmatch.filter(lines, x) for x in parmsList]
430 430
431 431
432 432 values = [re.sub(r'\D',"",x[0]) for x in filterList]
433 433
434 434 self.npulsesint_fromfile = int(values[0])
435 435 self.recordsperfile_fromfile = int(values[1])
436 436 self.nbeamcodes_fromfile = int(values[2])
437 437 self.ngates_fromfile = int(values[3])
438 438
439 439 tufileFinder = fnmatch.filter(lines, 'tufile=*')
440 440 tufile = tufileFinder[0].split('=')[1].split('\n')[0]
441 441 tufile = tufile.split('\r')[0]
442 442 tufilename = os.path.join(experimentCfgPath,tufile)
443 443
444 444 f = open(tufilename)
445 445 lines = f.readlines()
446 446 f.close()
447 447 self.ippSeconds_fromfile = float(lines[1].split()[2])/1E6
448 448
449 449
450 450 self.status = 1
451 451
452 452 def __setIdsAndArrays(self):
453 453 self.dataByFrame = self.__setDataByFrame()
454 454 self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[0, :]
455 455 self.readRanges()
456 456 self.index_amisr_sample, self.index_amisr_buffer = self.radacHeaderObj.getIndexRangeToPulse(0)
457 457 self.radacTimeByFrame = numpy.zeros(self.radacHeaderObj.npulses)
458 458 if len(self.index_amisr_buffer) > 0:
459 459 self.buffer_radactime = numpy.zeros_like(self.radacTimeByFrame)
460 460
461 461
462 462 def __setNextFile(self,online=False):
463 463
464 464 if not(online):
465 465 newFile = self.__setNextFileOffline()
466 466 else:
467 467 newFile = self.__setNextFileOnline()
468 468
469 469 if not(newFile):
470 470 return 0
471 471
472 472 self.__readHeader()
473 473
474 474 if self.__firstFile:
475 475 self.__setIdsAndArrays()
476 476 self.__firstFile = False
477 477
478 478 self.__getBeamCode()
479 479 self.readDataBlock()
480 480
481 481
482 482 def setup(self,path=None,
483 483 startDate=None,
484 484 endDate=None,
485 485 startTime=datetime.time(0,0,0),
486 486 endTime=datetime.time(23,59,59),
487 487 walk=True,
488 488 timezone='ut',
489 489 all=0,
490 490 online=False):
491 491
492 492 self.timezone = timezone
493 493 self.all = all
494 494 self.online = online
495 495 if not(online):
496 496 #Busqueda de archivos offline
497 497 self.searchFilesOffLine(path, startDate, endDate, startTime, endTime, walk)
498 498 else:
499 499 self.searchFilesOnLine(path, walk)
500 500
501 501 if not(self.filenameList):
502 print "There is no files into the folder: %s"%(path)
502 print("There is no files into the folder: %s"%(path))
503 503
504 504 sys.exit(-1)
505 505
506 506 self.__getExpParameters()
507 507
508 508 self.fileIndex = -1
509 509
510 510 self.__setNextFile(online)
511 511
512 512 # first_beamcode = self.radacHeaderObj.beamCodeByPulse[0,0]
513 513 # index = numpy.where(self.radacHeaderObj.beamCodeByPulse[0,:]!=first_beamcode)[0][0]
514 514 self.profileIndex_offset = self.radacHeaderObj.pulseCount[0,:][0]
515 515 self.profileIndex = self.profileIndex_offset
516 516
517 517 def readRanges(self):
518 518 dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Range')
519 519
520 520 self.rangeFromFile = numpy.reshape(dataset.value,(-1))
521 521 return self.rangeFromFile
522 522
523 523
524 524 def readRadacTime(self,idrecord, range1, range2):
525 525 self.radacTimeFromFile = self.radacHeaderObj.radacTime.value
526 526
527 527 radacTimeByFrame = numpy.zeros((self.radacHeaderObj.npulses))
528 528 #radacTimeByFrame = dataset[idrecord - 1,range1]
529 529 #radacTimeByFrame = dataset[idrecord,range2]
530 530
531 531 return radacTimeByFrame
532 532
533 533 def readBeamCode(self, idrecord, range1, range2):
534 534 dataset = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode')
535 535 beamcodeByFrame = numpy.zeros((self.radacHeaderObj.npulses))
536 536 self.beamCodesFromFile = dataset.value
537 537
538 538 #beamcodeByFrame[range1] = dataset[idrecord - 1, range1]
539 539 #beamcodeByFrame[range2] = dataset[idrecord, range2]
540 540 beamcodeByFrame[range1] = dataset[idrecord, range1]
541 541 beamcodeByFrame[range2] = dataset[idrecord, range2]
542 542
543 543 return beamcodeByFrame
544 544
545 545
546 546 def __setDataByFrame(self):
547 547 ndata = 2 # porque es complejo
548 548 dataByFrame = numpy.zeros((self.radacHeaderObj.npulses, self.radacHeaderObj.nsamples, ndata))
549 549 return dataByFrame
550 550
551 551 def __readDataSet(self):
552 552 dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Data')
553 553 return dataset
554 554
555 555 def __setDataBlock(self,):
556 556 real = self.dataByFrame[:,:,0] #asumo que 0 es real
557 557 imag = self.dataByFrame[:,:,1] #asumo que 1 es imaginario
558 558 datablock = real + imag*1j #armo el complejo
559 559 return datablock
560 560
561 561 def readSamples_version1(self,idrecord):
562 562 #estas tres primeras lineas solo se deben ejecutar una vez
563 563 if self.flagIsNewFile:
564 564 #reading dataset
565 565 self.dataset = self.__readDataSet()
566 566 self.flagIsNewFile = 0
567 567
568 568 if idrecord == 0:
569 569 self.dataByFrame[self.index4_schain_datablock, : ,:] = self.dataset[0, self.index_amisr_sample,:,:]
570 570 self.radacTimeByFrame[self.index4_schain_datablock] = self.radacHeaderObj.radacTime[0, self.index_amisr_sample]
571 571 datablock = self.__setDataBlock()
572 572 if len(self.index_amisr_buffer) > 0:
573 573 self.buffer = self.dataset[0, self.index_amisr_buffer,:,:]
574 574 self.buffer_radactime = self.radacHeaderObj.radacTime[0, self.index_amisr_buffer]
575 575
576 576 return datablock
577 577 if len(self.index_amisr_buffer) > 0:
578 578 self.dataByFrame[self.index4_buffer,:,:] = self.buffer.copy()
579 579 self.radacTimeByFrame[self.index4_buffer] = self.buffer_radactime.copy()
580 580 self.dataByFrame[self.index4_schain_datablock,:,:] = self.dataset[idrecord, self.index_amisr_sample,:,:]
581 581 self.radacTimeByFrame[self.index4_schain_datablock] = self.radacHeaderObj.radacTime[idrecord, self.index_amisr_sample]
582 582 datablock = self.__setDataBlock()
583 583 if len(self.index_amisr_buffer) > 0:
584 584 self.buffer = self.dataset[idrecord, self.index_amisr_buffer, :, :]
585 585 self.buffer_radactime = self.radacHeaderObj.radacTime[idrecord, self.index_amisr_buffer]
586 586
587 587 return datablock
588 588
589 589
590 590 def readSamples(self,idrecord):
591 591 if self.flagIsNewFile:
592 592 self.dataByFrame = self.__setDataByFrame()
593 593 self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[idrecord, :]
594 594
595 595 #reading ranges
596 596 self.readRanges()
597 597 #reading dataset
598 598 self.dataset = self.__readDataSet()
599 599
600 600 self.flagIsNewFile = 0
601 601 self.radacTimeByFrame = self.radacHeaderObj.radacTime.value[idrecord, :]
602 602 self.dataByFrame = self.dataset[idrecord, :, :, :]
603 603 datablock = self.__setDataBlock()
604 604 return datablock
605 605
606 606
607 607 def readDataBlock(self):
608 608
609 609 self.datablock = self.readSamples_version1(self.idrecord_count)
610 610 #self.datablock = self.readSamples(self.idrecord_count)
611 611 #print 'record:', self.idrecord_count
612 612
613 613 self.idrecord_count += 1
614 614 self.profileIndex = 0
615 615
616 616 if self.idrecord_count >= self.radacHeaderObj.nrecords:
617 617 self.idrecord_count = 0
618 618 self.flagIsNewFile = 1
619 619
620 620 def readNextBlock(self):
621 621
622 622 self.readDataBlock()
623 623
624 624 if self.flagIsNewFile:
625 625 self.__setNextFile(self.online)
626 626 pass
627 627
628 628 def __hasNotDataInBuffer(self):
629 629 #self.radacHeaderObj.npulses debe ser otra variable para considerar el numero de pulsos a tomar en el primer y ultimo record
630 630 if self.profileIndex >= self.radacHeaderObj.npulses:
631 631 return 1
632 632 return 0
633 633
634 634 def printUTC(self):
635 print self.dataOut.utctime
636 print ''
635 print(self.dataOut.utctime)
636 print('')
637 637
638 638 def setObjProperties(self):
639 639
640 640 self.dataOut.heightList = self.rangeFromFile/1000.0 #km
641 641 self.dataOut.nProfiles = self.radacHeaderObj.npulses
642 642 self.dataOut.nRecords = self.radacHeaderObj.nrecords
643 643 self.dataOut.nBeams = self.radacHeaderObj.nbeams
644 644 self.dataOut.ippSeconds = self.ippSeconds_fromfile
645 645 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
646 646 self.dataOut.frequency = self.frequency_h5file
647 647 self.dataOut.npulseByFrame = self.npulseByFrame
648 648 self.dataOut.nBaud = None
649 649 self.dataOut.nCode = None
650 650 self.dataOut.code = None
651 651
652 652 self.dataOut.beamCodeDict = self.beamCodeDict
653 653 self.dataOut.beamRangeDict = self.beamRangeDict
654 654
655 655 if self.timezone == 'lt':
656 656 self.dataOut.timeZone = time.timezone / 60. #get the timezone in minutes
657 657 else:
658 658 self.dataOut.timeZone = 0 #by default time is UTC
659 659
660 660 def getData(self):
661 661
662 662 if self.flagNoMoreFiles:
663 663 self.dataOut.flagNoData = True
664 print 'Process finished'
664 print('Process finished')
665 665 return 0
666 666
667 667 if self.__hasNotDataInBuffer():
668 668 self.readNextBlock()
669 669
670 670
671 671 if self.datablock is None: # setear esta condicion cuando no hayan datos por leers
672 672 self.dataOut.flagNoData = True
673 673 return 0
674 674
675 675 self.dataOut.data = numpy.reshape(self.datablock[self.profileIndex,:],(1,-1))
676 676
677 677 self.dataOut.utctime = self.radacTimeByFrame[self.profileIndex]
678 678 self.dataOut.profileIndex = self.profileIndex
679 679 self.dataOut.flagNoData = False
680 680
681 681 self.profileIndex += 1
682 682
683 683 return self.dataOut.data
684 684
685 685
686 686 def run(self, **kwargs):
687 687 if not(self.isConfig):
688 688 self.setup(**kwargs)
689 689 self.setObjProperties()
690 690 self.isConfig = True
691 691
692 self.getData()
692 self.getData() No newline at end of file
@@ -1,1826 +1,1826
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import sys
8 8 import glob
9 9 import time
10 10 import numpy
11 11 import fnmatch
12 12 import inspect
13 13 import time
14 14 import datetime
15 15 import traceback
16 16 import zmq
17 17
18 18 try:
19 19 from gevent import sleep
20 20 except:
21 21 from time import sleep
22 22
23 23 import schainpy.admin
24 24 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
25 25 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
26 26 from schainpy.utils import log
27 27 import schainpy.admin
28 28
29 29 LOCALTIME = True
30 30
31 31
32 32 def isNumber(cad):
33 33 """
34 34 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
35 35
36 36 Excepciones:
37 37 Si un determinado string no puede ser convertido a numero
38 38 Input:
39 39 str, string al cual se le analiza para determinar si convertible a un numero o no
40 40
41 41 Return:
42 42 True : si el string es uno numerico
43 43 False : no es un string numerico
44 44 """
45 45 try:
46 46 float(cad)
47 47 return True
48 48 except:
49 49 return False
50 50
51 51
52 52 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
53 53 """
54 54 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
55 55
56 56 Inputs:
57 57 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
58 58
59 59 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
60 60 segundos contados desde 01/01/1970.
61 61 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
62 62 segundos contados desde 01/01/1970.
63 63
64 64 Return:
65 65 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
66 66 fecha especificado, de lo contrario retorna False.
67 67
68 68 Excepciones:
69 69 Si el archivo no existe o no puede ser abierto
70 70 Si la cabecera no puede ser leida.
71 71
72 72 """
73 73 basicHeaderObj = BasicHeader(LOCALTIME)
74 74
75 75 try:
76 76 fp = open(filename, 'rb')
77 77 except IOError:
78 print "The file %s can't be opened" % (filename)
78 print("The file %s can't be opened" % (filename))
79 79 return 0
80 80
81 81 sts = basicHeaderObj.read(fp)
82 82 fp.close()
83 83
84 84 if not(sts):
85 print "Skipping the file %s because it has not a valid header" % (filename)
85 print("Skipping the file %s because it has not a valid header" % (filename))
86 86 return 0
87 87
88 88 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
89 89 return 0
90 90
91 91 return 1
92 92
93 93
94 94 def isTimeInRange(thisTime, startTime, endTime):
95 95 if endTime >= startTime:
96 96 if (thisTime < startTime) or (thisTime > endTime):
97 97 return 0
98 98 return 1
99 99 else:
100 100 if (thisTime < startTime) and (thisTime > endTime):
101 101 return 0
102 102 return 1
103 103
104 104
105 105 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
106 106 """
107 107 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
108 108
109 109 Inputs:
110 110 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
111 111
112 112 startDate : fecha inicial del rango seleccionado en formato datetime.date
113 113
114 114 endDate : fecha final del rango seleccionado en formato datetime.date
115 115
116 116 startTime : tiempo inicial del rango seleccionado en formato datetime.time
117 117
118 118 endTime : tiempo final del rango seleccionado en formato datetime.time
119 119
120 120 Return:
121 121 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
122 122 fecha especificado, de lo contrario retorna False.
123 123
124 124 Excepciones:
125 125 Si el archivo no existe o no puede ser abierto
126 126 Si la cabecera no puede ser leida.
127 127
128 128 """
129 129
130 130 try:
131 131 fp = open(filename, 'rb')
132 132 except IOError:
133 print "The file %s can't be opened" % (filename)
133 print("The file %s can't be opened" % (filename))
134 134 return None
135 135
136 136 firstBasicHeaderObj = BasicHeader(LOCALTIME)
137 137 systemHeaderObj = SystemHeader()
138 138 radarControllerHeaderObj = RadarControllerHeader()
139 139 processingHeaderObj = ProcessingHeader()
140 140
141 141 lastBasicHeaderObj = BasicHeader(LOCALTIME)
142 142
143 143 sts = firstBasicHeaderObj.read(fp)
144 144
145 145 if not(sts):
146 print "[Reading] Skipping the file %s because it has not a valid header" % (filename)
146 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
147 147 return None
148 148
149 149 if not systemHeaderObj.read(fp):
150 150 return None
151 151
152 152 if not radarControllerHeaderObj.read(fp):
153 153 return None
154 154
155 155 if not processingHeaderObj.read(fp):
156 156 return None
157 157
158 158 filesize = os.path.getsize(filename)
159 159
160 160 offset = processingHeaderObj.blockSize + 24 # header size
161 161
162 162 if filesize <= offset:
163 print "[Reading] %s: This file has not enough data" % filename
163 print("[Reading] %s: This file has not enough data" % filename)
164 164 return None
165 165
166 166 fp.seek(-offset, 2)
167 167
168 168 sts = lastBasicHeaderObj.read(fp)
169 169
170 170 fp.close()
171 171
172 172 thisDatetime = lastBasicHeaderObj.datatime
173 173 thisTime_last_block = thisDatetime.time()
174 174
175 175 thisDatetime = firstBasicHeaderObj.datatime
176 176 thisDate = thisDatetime.date()
177 177 thisTime_first_block = thisDatetime.time()
178 178
179 179 # General case
180 180 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
181 181 #-----------o----------------------------o-----------
182 182 # startTime endTime
183 183
184 184 if endTime >= startTime:
185 185 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
186 186 return None
187 187
188 188 return thisDatetime
189 189
190 190 # If endTime < startTime then endTime belongs to the next day
191 191
192 192 #<<<<<<<<<<<o o>>>>>>>>>>>
193 193 #-----------o----------------------------o-----------
194 194 # endTime startTime
195 195
196 196 if (thisDate == startDate) and (thisTime_last_block < startTime):
197 197 return None
198 198
199 199 if (thisDate == endDate) and (thisTime_first_block > endTime):
200 200 return None
201 201
202 202 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
203 203 return None
204 204
205 205 return thisDatetime
206 206
207 207
208 208 def isFolderInDateRange(folder, startDate=None, endDate=None):
209 209 """
210 210 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
211 211
212 212 Inputs:
213 213 folder : nombre completo del directorio.
214 214 Su formato deberia ser "/path_root/?YYYYDDD"
215 215
216 216 siendo:
217 217 YYYY : Anio (ejemplo 2015)
218 218 DDD : Dia del anio (ejemplo 305)
219 219
220 220 startDate : fecha inicial del rango seleccionado en formato datetime.date
221 221
222 222 endDate : fecha final del rango seleccionado en formato datetime.date
223 223
224 224 Return:
225 225 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
226 226 fecha especificado, de lo contrario retorna False.
227 227 Excepciones:
228 228 Si el directorio no tiene el formato adecuado
229 229 """
230 230
231 231 basename = os.path.basename(folder)
232 232
233 233 if not isRadarFolder(basename):
234 print "The folder %s has not the rigth format" % folder
234 print("The folder %s has not the rigth format" % folder)
235 235 return 0
236 236
237 237 if startDate and endDate:
238 238 thisDate = getDateFromRadarFolder(basename)
239 239
240 240 if thisDate < startDate:
241 241 return 0
242 242
243 243 if thisDate > endDate:
244 244 return 0
245 245
246 246 return 1
247 247
248 248
249 249 def isFileInDateRange(filename, startDate=None, endDate=None):
250 250 """
251 251 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
252 252
253 253 Inputs:
254 254 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
255 255
256 256 Su formato deberia ser "?YYYYDDDsss"
257 257
258 258 siendo:
259 259 YYYY : Anio (ejemplo 2015)
260 260 DDD : Dia del anio (ejemplo 305)
261 261 sss : set
262 262
263 263 startDate : fecha inicial del rango seleccionado en formato datetime.date
264 264
265 265 endDate : fecha final del rango seleccionado en formato datetime.date
266 266
267 267 Return:
268 268 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
269 269 fecha especificado, de lo contrario retorna False.
270 270 Excepciones:
271 271 Si el archivo no tiene el formato adecuado
272 272 """
273 273
274 274 basename = os.path.basename(filename)
275 275
276 276 if not isRadarFile(basename):
277 print "The filename %s has not the rigth format" % filename
277 print("The filename %s has not the rigth format" % filename)
278 278 return 0
279 279
280 280 if startDate and endDate:
281 281 thisDate = getDateFromRadarFile(basename)
282 282
283 283 if thisDate < startDate:
284 284 return 0
285 285
286 286 if thisDate > endDate:
287 287 return 0
288 288
289 289 return 1
290 290
291 291
292 292 def getFileFromSet(path, ext, set):
293 293 validFilelist = []
294 294 fileList = os.listdir(path)
295 295
296 296 # 0 1234 567 89A BCDE
297 297 # H YYYY DDD SSS .ext
298 298
299 299 for thisFile in fileList:
300 300 try:
301 301 year = int(thisFile[1:5])
302 302 doy = int(thisFile[5:8])
303 303 except:
304 304 continue
305 305
306 306 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
307 307 continue
308 308
309 309 validFilelist.append(thisFile)
310 310
311 311 myfile = fnmatch.filter(
312 312 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
313 313
314 314 if len(myfile) != 0:
315 315 return myfile[0]
316 316 else:
317 317 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
318 print 'the filename %s does not exist' % filename
319 print '...going to the last file: '
318 print('the filename %s does not exist' % filename)
319 print('...going to the last file: ')
320 320
321 321 if validFilelist:
322 322 validFilelist = sorted(validFilelist, key=str.lower)
323 323 return validFilelist[-1]
324 324
325 325 return None
326 326
327 327
328 328 def getlastFileFromPath(path, ext):
329 329 """
330 330 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
331 331 al final de la depuracion devuelve el ultimo file de la lista que quedo.
332 332
333 333 Input:
334 334 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
335 335 ext : extension de los files contenidos en una carpeta
336 336
337 337 Return:
338 338 El ultimo file de una determinada carpeta, no se considera el path.
339 339 """
340 340 validFilelist = []
341 341 fileList = os.listdir(path)
342 342
343 343 # 0 1234 567 89A BCDE
344 344 # H YYYY DDD SSS .ext
345 345
346 346 for thisFile in fileList:
347 347
348 348 year = thisFile[1:5]
349 349 if not isNumber(year):
350 350 continue
351 351
352 352 doy = thisFile[5:8]
353 353 if not isNumber(doy):
354 354 continue
355 355
356 356 year = int(year)
357 357 doy = int(doy)
358 358
359 359 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
360 360 continue
361 361
362 362 validFilelist.append(thisFile)
363 363
364 364 if validFilelist:
365 365 validFilelist = sorted(validFilelist, key=str.lower)
366 366 return validFilelist[-1]
367 367
368 368 return None
369 369
370 370
371 371 def checkForRealPath(path, foldercounter, year, doy, set, ext):
372 372 """
373 373 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
374 374 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
375 375 el path exacto de un determinado file.
376 376
377 377 Example :
378 378 nombre correcto del file es .../.../D2009307/P2009307367.ext
379 379
380 380 Entonces la funcion prueba con las siguientes combinaciones
381 381 .../.../y2009307367.ext
382 382 .../.../Y2009307367.ext
383 383 .../.../x2009307/y2009307367.ext
384 384 .../.../x2009307/Y2009307367.ext
385 385 .../.../X2009307/y2009307367.ext
386 386 .../.../X2009307/Y2009307367.ext
387 387 siendo para este caso, la ultima combinacion de letras, identica al file buscado
388 388
389 389 Return:
390 390 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
391 391 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
392 392 para el filename
393 393 """
394 394 fullfilename = None
395 395 find_flag = False
396 396 filename = None
397 397
398 398 prefixDirList = [None, 'd', 'D']
399 399 if ext.lower() == ".r": # voltage
400 400 prefixFileList = ['d', 'D']
401 401 elif ext.lower() == ".pdata": # spectra
402 402 prefixFileList = ['p', 'P']
403 403 else:
404 404 return None, filename
405 405
406 406 # barrido por las combinaciones posibles
407 407 for prefixDir in prefixDirList:
408 408 thispath = path
409 409 if prefixDir != None:
410 410 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
411 411 if foldercounter == 0:
412 412 thispath = os.path.join(path, "%s%04d%03d" %
413 413 (prefixDir, year, doy))
414 414 else:
415 415 thispath = os.path.join(path, "%s%04d%03d_%02d" % (
416 416 prefixDir, year, doy, foldercounter))
417 417 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
418 418 # formo el nombre del file xYYYYDDDSSS.ext
419 419 filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext)
420 420 fullfilename = os.path.join(
421 421 thispath, filename) # formo el path completo
422 422
423 423 if os.path.exists(fullfilename): # verifico que exista
424 424 find_flag = True
425 425 break
426 426 if find_flag:
427 427 break
428 428
429 429 if not(find_flag):
430 430 return None, filename
431 431
432 432 return fullfilename, filename
433 433
434 434
435 435 def isRadarFolder(folder):
436 436 try:
437 437 year = int(folder[1:5])
438 438 doy = int(folder[5:8])
439 439 except:
440 440 return 0
441 441
442 442 return 1
443 443
444 444
445 445 def isRadarFile(file):
446 446 try:
447 447 year = int(file[1:5])
448 448 doy = int(file[5:8])
449 449 set = int(file[8:11])
450 450 except:
451 451 return 0
452 452
453 453 return 1
454 454
455 455
456 456 def getDateFromRadarFile(file):
457 457 try:
458 458 year = int(file[1:5])
459 459 doy = int(file[5:8])
460 460 set = int(file[8:11])
461 461 except:
462 462 return None
463 463
464 464 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
465 465 return thisDate
466 466
467 467
468 468 def getDateFromRadarFolder(folder):
469 469 try:
470 470 year = int(folder[1:5])
471 471 doy = int(folder[5:8])
472 472 except:
473 473 return None
474 474
475 475 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
476 476 return thisDate
477 477
478 478
479 479 class JRODataIO:
480 480
481 481 c = 3E8
482 482
483 483 isConfig = False
484 484
485 485 basicHeaderObj = None
486 486
487 487 systemHeaderObj = None
488 488
489 489 radarControllerHeaderObj = None
490 490
491 491 processingHeaderObj = None
492 492
493 493 dtype = None
494 494
495 495 pathList = []
496 496
497 497 filenameList = []
498 498
499 499 filename = None
500 500
501 501 ext = None
502 502
503 503 flagIsNewFile = 1
504 504
505 505 flagDiscontinuousBlock = 0
506 506
507 507 flagIsNewBlock = 0
508 508
509 509 fp = None
510 510
511 511 firstHeaderSize = 0
512 512
513 513 basicHeaderSize = 24
514 514
515 515 versionFile = 1103
516 516
517 517 fileSize = None
518 518
519 519 # ippSeconds = None
520 520
521 521 fileSizeByHeader = None
522 522
523 523 fileIndex = None
524 524
525 525 profileIndex = None
526 526
527 527 blockIndex = None
528 528
529 529 nTotalBlocks = None
530 530
531 531 maxTimeStep = 30
532 532
533 533 lastUTTime = None
534 534
535 535 datablock = None
536 536
537 537 dataOut = None
538 538
539 539 blocksize = None
540 540
541 541 getByBlock = False
542 542
543 543 def __init__(self):
544 544
545 545 raise NotImplementedError
546 546
547 547 def run(self):
548 548
549 549 raise NotImplementedError
550 550
551 551 def getDtypeWidth(self):
552 552
553 553 dtype_index = get_dtype_index(self.dtype)
554 554 dtype_width = get_dtype_width(dtype_index)
555 555
556 556 return dtype_width
557 557
558 558 def getAllowedArgs(self):
559 559 if hasattr(self, '__attrs__'):
560 560 return self.__attrs__
561 561 else:
562 562 return inspect.getargspec(self.run).args
563 563
564 564
565 565 class JRODataReader(JRODataIO):
566 566
567 567 online = 0
568 568
569 569 realtime = 0
570 570
571 571 nReadBlocks = 0
572 572
573 573 delay = 10 # number of seconds waiting a new file
574 574
575 575 nTries = 3 # quantity tries
576 576
577 577 nFiles = 3 # number of files for searching
578 578
579 579 path = None
580 580
581 581 foldercounter = 0
582 582
583 583 flagNoMoreFiles = 0
584 584
585 585 datetimeList = []
586 586
587 587 __isFirstTimeOnline = 1
588 588
589 589 __printInfo = True
590 590
591 591 profileIndex = None
592 592
593 593 nTxs = 1
594 594
595 595 txIndex = None
596 596
597 597 # Added--------------------
598 598
599 599 selBlocksize = None
600 600
601 601 selBlocktime = None
602 602
603 603 def __init__(self):
604 604 """
605 605 This class is used to find data files
606 606
607 607 Example:
608 608 reader = JRODataReader()
609 609 fileList = reader.findDataFiles()
610 610
611 611 """
612 612 pass
613 613
614 614 def createObjByDefault(self):
615 615 """
616 616
617 617 """
618 618 raise NotImplementedError
619 619
620 620 def getBlockDimension(self):
621 621
622 622 raise NotImplementedError
623 623
624 624 def searchFilesOffLine(self,
625 625 path,
626 626 startDate=None,
627 627 endDate=None,
628 628 startTime=datetime.time(0, 0, 0),
629 629 endTime=datetime.time(23, 59, 59),
630 630 set=None,
631 631 expLabel='',
632 632 ext='.r',
633 633 cursor=None,
634 634 skip=None,
635 635 walk=True):
636 636
637 637 self.filenameList = []
638 638 self.datetimeList = []
639 639
640 640 pathList = []
641 641
642 642 dateList, pathList = self.findDatafiles(
643 643 path, startDate, endDate, expLabel, ext, walk, include_path=True)
644 644
645 645 if dateList == []:
646 646 return [], []
647 647
648 648 if len(dateList) > 1:
649 print "[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList))
649 print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList)))
650 650 else:
651 print "[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0])
651 print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0]))
652 652
653 653 filenameList = []
654 654 datetimeList = []
655 655
656 656 for thisPath in pathList:
657 657
658 658 fileList = glob.glob1(thisPath, "*%s" % ext)
659 659 fileList.sort()
660 660
661 661 for file in fileList:
662 662
663 663 filename = os.path.join(thisPath, file)
664 664
665 665 if not isFileInDateRange(filename, startDate, endDate):
666 666 continue
667 667
668 668 thisDatetime = isFileInTimeRange(
669 669 filename, startDate, endDate, startTime, endTime)
670 670
671 671 if not(thisDatetime):
672 672 continue
673 673
674 674 filenameList.append(filename)
675 675 datetimeList.append(thisDatetime)
676 676
677 677 if cursor is not None and skip is not None:
678 678 filenameList = filenameList[cursor * skip:cursor * skip + skip]
679 679 datetimeList = datetimeList[cursor * skip:cursor * skip + skip]
680 680
681 681 if not(filenameList):
682 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path)
682 print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path))
683 683 return [], []
684 684
685 print "[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime)
685 print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime))
686 686
687 687 # for i in range(len(filenameList)):
688 688 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
689 689
690 690 self.filenameList = filenameList
691 691 self.datetimeList = datetimeList
692 692
693 693 return pathList, filenameList
694 694
695 695 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None):
696 696 """
697 697 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
698 698 devuelve el archivo encontrado ademas de otros datos.
699 699
700 700 Input:
701 701 path : carpeta donde estan contenidos los files que contiene data
702 702
703 703 expLabel : Nombre del subexperimento (subfolder)
704 704
705 705 ext : extension de los files
706 706
707 707 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
708 708
709 709 Return:
710 710 directory : eL directorio donde esta el file encontrado
711 711 filename : el ultimo file de una determinada carpeta
712 712 year : el anho
713 713 doy : el numero de dia del anho
714 714 set : el set del archivo
715 715
716 716
717 717 """
718 718 if not os.path.isdir(path):
719 719 return None, None, None, None, None, None
720 720
721 721 dirList = []
722 722
723 723 if not walk:
724 724 fullpath = path
725 725 foldercounter = 0
726 726 else:
727 727 # Filtra solo los directorios
728 728 for thisPath in os.listdir(path):
729 729 if not os.path.isdir(os.path.join(path, thisPath)):
730 730 continue
731 731 if not isRadarFolder(thisPath):
732 732 continue
733 733
734 734 dirList.append(thisPath)
735 735
736 736 if not(dirList):
737 737 return None, None, None, None, None, None
738 738
739 739 dirList = sorted(dirList, key=str.lower)
740 740
741 741 doypath = dirList[-1]
742 742 foldercounter = int(doypath.split('_')[1]) if len(
743 743 doypath.split('_')) > 1 else 0
744 744 fullpath = os.path.join(path, doypath, expLabel)
745 745
746 print "[Reading] %s folder was found: " % (fullpath)
746 print("[Reading] %s folder was found: " % (fullpath))
747 747
748 748 if set == None:
749 749 filename = getlastFileFromPath(fullpath, ext)
750 750 else:
751 751 filename = getFileFromSet(fullpath, ext, set)
752 752
753 753 if not(filename):
754 754 return None, None, None, None, None, None
755 755
756 print "[Reading] %s file was found" % (filename)
756 print("[Reading] %s file was found" % (filename))
757 757
758 758 if not(self.__verifyFile(os.path.join(fullpath, filename))):
759 759 return None, None, None, None, None, None
760 760
761 761 year = int(filename[1:5])
762 762 doy = int(filename[5:8])
763 763 set = int(filename[8:11])
764 764
765 765 return fullpath, foldercounter, filename, year, doy, set
766 766
767 767 def __setNextFileOffline(self):
768 768
769 769 idFile = self.fileIndex
770 770
771 771 while (True):
772 772 idFile += 1
773 773 if not(idFile < len(self.filenameList)):
774 774 self.flagNoMoreFiles = 1
775 775 # print "[Reading] No more Files"
776 776 return 0
777 777
778 778 filename = self.filenameList[idFile]
779 779
780 780 if not(self.__verifyFile(filename)):
781 781 continue
782 782
783 783 fileSize = os.path.getsize(filename)
784 784 fp = open(filename, 'rb')
785 785 break
786 786
787 787 self.flagIsNewFile = 1
788 788 self.fileIndex = idFile
789 789 self.filename = filename
790 790 self.fileSize = fileSize
791 791 self.fp = fp
792 792
793 793 # print "[Reading] Setting the file: %s"%self.filename
794 794
795 795 return 1
796 796
797 797 def __setNextFileOnline(self):
798 798 """
799 799 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
800 800 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
801 801 siguientes.
802 802
803 803 Affected:
804 804 self.flagIsNewFile
805 805 self.filename
806 806 self.fileSize
807 807 self.fp
808 808 self.set
809 809 self.flagNoMoreFiles
810 810
811 811 Return:
812 812 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
813 813 1 : si el file fue abierto con exito y esta listo a ser leido
814 814
815 815 Excepciones:
816 816 Si un determinado file no puede ser abierto
817 817 """
818 818 nFiles = 0
819 819 fileOk_flag = False
820 820 firstTime_flag = True
821 821
822 822 self.set += 1
823 823
824 824 if self.set > 999:
825 825 self.set = 0
826 826 self.foldercounter += 1
827 827
828 828 # busca el 1er file disponible
829 829 fullfilename, filename = checkForRealPath(
830 830 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
831 831 if fullfilename:
832 832 if self.__verifyFile(fullfilename, False):
833 833 fileOk_flag = True
834 834
835 835 # si no encuentra un file entonces espera y vuelve a buscar
836 836 if not(fileOk_flag):
837 837 # busco en los siguientes self.nFiles+1 files posibles
838 838 for nFiles in range(self.nFiles + 1):
839 839
840 840 if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces
841 841 tries = self.nTries
842 842 else:
843 843 tries = 1 # si no es la 1era vez entonces solo lo hace una vez
844 844
845 845 for nTries in range(tries):
846 846 if firstTime_flag:
847 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1)
847 print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1))
848 848 sleep(self.delay)
849 849 else:
850 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
850 print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext))
851 851
852 852 fullfilename, filename = checkForRealPath(
853 853 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
854 854 if fullfilename:
855 855 if self.__verifyFile(fullfilename):
856 856 fileOk_flag = True
857 857 break
858 858
859 859 if fileOk_flag:
860 860 break
861 861
862 862 firstTime_flag = False
863 863
864 864 log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename))
865 865 self.set += 1
866 866
867 867 # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
868 868 if nFiles == (self.nFiles - 1):
869 869 self.set = 0
870 870 self.doy += 1
871 871 self.foldercounter = 0
872 872
873 873 if fileOk_flag:
874 874 self.fileSize = os.path.getsize(fullfilename)
875 875 self.filename = fullfilename
876 876 self.flagIsNewFile = 1
877 877 if self.fp != None:
878 878 self.fp.close()
879 879 self.fp = open(fullfilename, 'rb')
880 880 self.flagNoMoreFiles = 0
881 881 # print '[Reading] Setting the file: %s' % fullfilename
882 882 else:
883 883 self.fileSize = 0
884 884 self.filename = None
885 885 self.flagIsNewFile = 0
886 886 self.fp = None
887 887 self.flagNoMoreFiles = 1
888 888
889 889 return fileOk_flag
890 890
891 891 def setNextFile(self):
892 892 if self.fp != None:
893 893 self.fp.close()
894 894
895 895 if self.online:
896 896 newFile = self.__setNextFileOnline()
897 897 else:
898 898 newFile = self.__setNextFileOffline()
899 899
900 900 if not(newFile):
901 901 raise schainpy.admin.SchainWarning('No more files to read')
902 902 return 0
903 903
904 904 if self.verbose:
905 print '[Reading] Setting the file: %s' % self.filename
905 print('[Reading] Setting the file: %s' % self.filename)
906 906
907 907 self.__readFirstHeader()
908 908 self.nReadBlocks = 0
909 909 return 1
910 910
911 911 def __waitNewBlock(self):
912 912 """
913 913 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
914 914
915 915 Si el modo de lectura es OffLine siempre retorn 0
916 916 """
917 917 if not self.online:
918 918 return 0
919 919
920 920 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
921 921 return 0
922 922
923 923 currentPointer = self.fp.tell()
924 924
925 925 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
926 926
927 927 for nTries in range(self.nTries):
928 928
929 929 self.fp.close()
930 930 self.fp = open(self.filename, 'rb')
931 931 self.fp.seek(currentPointer)
932 932
933 933 self.fileSize = os.path.getsize(self.filename)
934 934 currentSize = self.fileSize - currentPointer
935 935
936 936 if (currentSize >= neededSize):
937 937 self.basicHeaderObj.read(self.fp)
938 938 return 1
939 939
940 940 if self.fileSize == self.fileSizeByHeader:
941 941 # self.flagEoF = True
942 942 return 0
943 943
944 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)
944 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
945 945 sleep(self.delay)
946 946
947 947 return 0
948 948
949 949 def waitDataBlock(self, pointer_location):
950 950
951 951 currentPointer = pointer_location
952 952
953 953 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
954 954
955 955 for nTries in range(self.nTries):
956 956 self.fp.close()
957 957 self.fp = open(self.filename, 'rb')
958 958 self.fp.seek(currentPointer)
959 959
960 960 self.fileSize = os.path.getsize(self.filename)
961 961 currentSize = self.fileSize - currentPointer
962 962
963 963 if (currentSize >= neededSize):
964 964 return 1
965 965
966 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)
966 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
967 967 sleep(self.delay)
968 968
969 969 return 0
970 970
971 971 def __jumpToLastBlock(self):
972 972
973 973 if not(self.__isFirstTimeOnline):
974 974 return
975 975
976 976 csize = self.fileSize - self.fp.tell()
977 977 blocksize = self.processingHeaderObj.blockSize
978 978
979 979 # salta el primer bloque de datos
980 980 if csize > self.processingHeaderObj.blockSize:
981 981 self.fp.seek(self.fp.tell() + blocksize)
982 982 else:
983 983 return
984 984
985 985 csize = self.fileSize - self.fp.tell()
986 986 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
987 987 while True:
988 988
989 989 if self.fp.tell() < self.fileSize:
990 990 self.fp.seek(self.fp.tell() + neededsize)
991 991 else:
992 992 self.fp.seek(self.fp.tell() - neededsize)
993 993 break
994 994
995 995 # csize = self.fileSize - self.fp.tell()
996 996 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
997 997 # factor = int(csize/neededsize)
998 998 # if factor > 0:
999 999 # self.fp.seek(self.fp.tell() + factor*neededsize)
1000 1000
1001 1001 self.flagIsNewFile = 0
1002 1002 self.__isFirstTimeOnline = 0
1003 1003
1004 1004 def __setNewBlock(self):
1005 1005 # if self.server is None:
1006 1006 if self.fp == None:
1007 1007 return 0
1008 1008
1009 1009 # if self.online:
1010 1010 # self.__jumpToLastBlock()
1011 1011
1012 1012 if self.flagIsNewFile:
1013 1013 self.lastUTTime = self.basicHeaderObj.utc
1014 1014 return 1
1015 1015
1016 1016 if self.realtime:
1017 1017 self.flagDiscontinuousBlock = 1
1018 1018 if not(self.setNextFile()):
1019 1019 return 0
1020 1020 else:
1021 1021 return 1
1022 1022 # if self.server is None:
1023 1023 currentSize = self.fileSize - self.fp.tell()
1024 1024 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1025 1025 if (currentSize >= neededSize):
1026 1026 self.basicHeaderObj.read(self.fp)
1027 1027 self.lastUTTime = self.basicHeaderObj.utc
1028 1028 return 1
1029 1029 # else:
1030 1030 # self.basicHeaderObj.read(self.zHeader)
1031 1031 # self.lastUTTime = self.basicHeaderObj.utc
1032 1032 # return 1
1033 1033 if self.__waitNewBlock():
1034 1034 self.lastUTTime = self.basicHeaderObj.utc
1035 1035 return 1
1036 1036 # if self.server is None:
1037 1037 if not(self.setNextFile()):
1038 1038 return 0
1039 1039
1040 1040 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
1041 1041 self.lastUTTime = self.basicHeaderObj.utc
1042 1042
1043 1043 self.flagDiscontinuousBlock = 0
1044 1044
1045 1045 if deltaTime > self.maxTimeStep:
1046 1046 self.flagDiscontinuousBlock = 1
1047 1047
1048 1048 return 1
1049 1049
1050 1050 def readNextBlock(self):
1051 1051
1052 1052 # Skip block out of startTime and endTime
1053 1053 while True:
1054 1054 if not(self.__setNewBlock()):
1055 raise(schainpy.admin.SchainWarning('No more files'))
1055 raise schainpy
1056 1056 return 0
1057 1057
1058 1058 if not(self.readBlock()):
1059 1059 return 0
1060 1060
1061 1061 self.getBasicHeader()
1062 1062 if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
1063 print "[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1063 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1064 1064 self.processingHeaderObj.dataBlocksPerFile,
1065 self.dataOut.datatime.ctime())
1065 self.dataOut.datatime.ctime()))
1066 1066 continue
1067 1067
1068 1068 break
1069 1069
1070 1070 if self.verbose:
1071 print "[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1071 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1072 1072 self.processingHeaderObj.dataBlocksPerFile,
1073 self.dataOut.datatime.ctime())
1073 self.dataOut.datatime.ctime()))
1074 1074 return 1
1075 1075
1076 1076 def __readFirstHeader(self):
1077 1077
1078 1078 self.basicHeaderObj.read(self.fp)
1079 1079 self.systemHeaderObj.read(self.fp)
1080 1080 self.radarControllerHeaderObj.read(self.fp)
1081 1081 self.processingHeaderObj.read(self.fp)
1082 1082
1083 1083 self.firstHeaderSize = self.basicHeaderObj.size
1084 1084
1085 1085 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
1086 1086 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
1087 1087 if datatype == 0:
1088 1088 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
1089 1089 elif datatype == 1:
1090 1090 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
1091 1091 elif datatype == 2:
1092 1092 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
1093 1093 elif datatype == 3:
1094 1094 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
1095 1095 elif datatype == 4:
1096 1096 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
1097 1097 elif datatype == 5:
1098 1098 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
1099 1099 else:
1100 raise ValueError, 'Data type was not defined'
1100 raise ValueError('Data type was not defined')
1101 1101
1102 1102 self.dtype = datatype_str
1103 1103 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1104 1104 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
1105 1105 self.firstHeaderSize + self.basicHeaderSize * \
1106 1106 (self.processingHeaderObj.dataBlocksPerFile - 1)
1107 1107 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1108 1108 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1109 1109 self.getBlockDimension()
1110 1110
1111 1111 def __verifyFile(self, filename, msgFlag=True):
1112 1112
1113 1113 msg = None
1114 1114
1115 1115 try:
1116 1116 fp = open(filename, 'rb')
1117 1117 except IOError:
1118 1118
1119 1119 if msgFlag:
1120 print "[Reading] File %s can't be opened" % (filename)
1120 print("[Reading] File %s can't be opened" % (filename))
1121 1121
1122 1122 return False
1123 1123
1124 1124 currentPosition = fp.tell()
1125 1125 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1126 1126
1127 1127 if neededSize == 0:
1128 1128 basicHeaderObj = BasicHeader(LOCALTIME)
1129 1129 systemHeaderObj = SystemHeader()
1130 1130 radarControllerHeaderObj = RadarControllerHeader()
1131 1131 processingHeaderObj = ProcessingHeader()
1132 1132
1133 1133 if not(basicHeaderObj.read(fp)):
1134 1134 fp.close()
1135 1135 return False
1136 1136
1137 1137 if not(systemHeaderObj.read(fp)):
1138 1138 fp.close()
1139 1139 return False
1140 1140
1141 1141 if not(radarControllerHeaderObj.read(fp)):
1142 1142 fp.close()
1143 1143 return False
1144 1144
1145 1145 if not(processingHeaderObj.read(fp)):
1146 1146 fp.close()
1147 1147 return False
1148 1148
1149 1149 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1150 1150 else:
1151 1151 msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename
1152 1152
1153 1153 fp.close()
1154 1154
1155 1155 fileSize = os.path.getsize(filename)
1156 1156 currentSize = fileSize - currentPosition
1157 1157
1158 1158 if currentSize < neededSize:
1159 1159 if msgFlag and (msg != None):
1160 print msg
1160 print(msg)
1161 1161 return False
1162 1162
1163 1163 return True
1164 1164
1165 1165 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1166 1166
1167 1167 path_empty = True
1168 1168
1169 1169 dateList = []
1170 1170 pathList = []
1171 1171
1172 1172 multi_path = path.split(',')
1173 1173
1174 1174 if not walk:
1175 1175
1176 1176 for single_path in multi_path:
1177 1177
1178 1178 if not os.path.isdir(single_path):
1179 1179 continue
1180 1180
1181 1181 fileList = glob.glob1(single_path, "*" + ext)
1182 1182
1183 1183 if not fileList:
1184 1184 continue
1185 1185
1186 1186 path_empty = False
1187 1187
1188 1188 fileList.sort()
1189 1189
1190 1190 for thisFile in fileList:
1191 1191
1192 1192 if not os.path.isfile(os.path.join(single_path, thisFile)):
1193 1193 continue
1194 1194
1195 1195 if not isRadarFile(thisFile):
1196 1196 continue
1197 1197
1198 1198 if not isFileInDateRange(thisFile, startDate, endDate):
1199 1199 continue
1200 1200
1201 1201 thisDate = getDateFromRadarFile(thisFile)
1202 1202
1203 1203 if thisDate in dateList:
1204 1204 continue
1205 1205
1206 1206 dateList.append(thisDate)
1207 1207 pathList.append(single_path)
1208 1208
1209 1209 else:
1210 1210 for single_path in multi_path:
1211 1211
1212 1212 if not os.path.isdir(single_path):
1213 1213 continue
1214 1214
1215 1215 dirList = []
1216 1216
1217 1217 for thisPath in os.listdir(single_path):
1218 1218
1219 1219 if not os.path.isdir(os.path.join(single_path, thisPath)):
1220 1220 continue
1221 1221
1222 1222 if not isRadarFolder(thisPath):
1223 1223 continue
1224 1224
1225 1225 if not isFolderInDateRange(thisPath, startDate, endDate):
1226 1226 continue
1227 1227
1228 1228 dirList.append(thisPath)
1229 1229
1230 1230 if not dirList:
1231 1231 continue
1232 1232
1233 1233 dirList.sort()
1234 1234
1235 1235 for thisDir in dirList:
1236 1236
1237 1237 datapath = os.path.join(single_path, thisDir, expLabel)
1238 1238 fileList = glob.glob1(datapath, "*" + ext)
1239 1239
1240 1240 if not fileList:
1241 1241 continue
1242 1242
1243 1243 path_empty = False
1244 1244
1245 1245 thisDate = getDateFromRadarFolder(thisDir)
1246 1246
1247 1247 pathList.append(datapath)
1248 1248 dateList.append(thisDate)
1249 1249
1250 1250 dateList.sort()
1251 1251
1252 1252 if walk:
1253 1253 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1254 1254 else:
1255 1255 pattern_path = multi_path[0]
1256 1256
1257 1257 if path_empty:
1258 print "[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate)
1258 print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1259 1259 else:
1260 1260 if not dateList:
1261 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path)
1261 print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1262 1262
1263 1263 if include_path:
1264 1264 return dateList, pathList
1265 1265
1266 1266 return dateList
1267 1267
1268 1268 def setup(self,
1269 1269 path=None,
1270 1270 startDate=None,
1271 1271 endDate=None,
1272 1272 startTime=datetime.time(0, 0, 0),
1273 1273 endTime=datetime.time(23, 59, 59),
1274 1274 set=None,
1275 1275 expLabel="",
1276 1276 ext=None,
1277 1277 online=False,
1278 1278 delay=60,
1279 1279 walk=True,
1280 1280 getblock=False,
1281 1281 nTxs=1,
1282 1282 realtime=False,
1283 1283 blocksize=None,
1284 1284 blocktime=None,
1285 1285 skip=None,
1286 1286 cursor=None,
1287 1287 warnings=True,
1288 1288 verbose=True,
1289 1289 server=None,
1290 1290 format=None,
1291 1291 oneDDict=None,
1292 1292 twoDDict=None,
1293 1293 ind2DList=None):
1294 1294 if server is not None:
1295 1295 if 'tcp://' in server:
1296 1296 address = server
1297 1297 else:
1298 1298 address = 'ipc:///tmp/%s' % server
1299 1299 self.server = address
1300 1300 self.context = zmq.Context()
1301 1301 self.receiver = self.context.socket(zmq.PULL)
1302 1302 self.receiver.connect(self.server)
1303 1303 time.sleep(0.5)
1304 print '[Starting] ReceiverData from {}'.format(self.server)
1304 print('[Starting] ReceiverData from {}'.format(self.server))
1305 1305 else:
1306 1306 self.server = None
1307 1307 if path == None:
1308 raise ValueError, "[Reading] The path is not valid"
1308 raise ValueError("[Reading] The path is not valid")
1309 1309
1310 1310 if ext == None:
1311 1311 ext = self.ext
1312 1312
1313 1313 if online:
1314 print "[Reading] Searching files in online mode..."
1314 print("[Reading] Searching files in online mode...")
1315 1315
1316 1316 for nTries in range(self.nTries):
1317 1317 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(
1318 1318 path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1319 1319
1320 1320 if fullpath:
1321 1321 break
1322 1322
1323 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (delay, path, nTries + 1)
1323 print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (delay, path, nTries + 1))
1324 1324 sleep(delay)
1325 1325
1326 1326 if not(fullpath):
1327 1327 raise schainpy.admin.SchainWarning('There isn\'t any valid file in {}'.format(path))
1328 1328 return
1329 1329
1330 1330 self.year = year
1331 1331 self.doy = doy
1332 1332 self.set = set - 1
1333 1333 self.path = path
1334 1334 self.foldercounter = foldercounter
1335 1335 last_set = None
1336 1336 else:
1337 print "[Reading] Searching files in offline mode ..."
1337 print("[Reading] Searching files in offline mode ...")
1338 1338 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1339 1339 startTime=startTime, endTime=endTime,
1340 1340 set=set, expLabel=expLabel, ext=ext,
1341 1341 walk=walk, cursor=cursor,
1342 1342 skip=skip)
1343 1343
1344 1344 if not(pathList):
1345 1345 self.fileIndex = -1
1346 1346 self.pathList = []
1347 1347 self.filenameList = []
1348 1348 return
1349 1349
1350 1350 self.fileIndex = -1
1351 1351 self.pathList = pathList
1352 1352 self.filenameList = filenameList
1353 1353 file_name = os.path.basename(filenameList[-1])
1354 1354 basename, ext = os.path.splitext(file_name)
1355 1355 last_set = int(basename[-3:])
1356 1356
1357 1357 self.online = online
1358 1358 self.realtime = realtime
1359 1359 self.delay = delay
1360 1360 ext = ext.lower()
1361 1361 self.ext = ext
1362 1362 self.getByBlock = getblock
1363 1363 self.nTxs = nTxs
1364 1364 self.startTime = startTime
1365 1365 self.endTime = endTime
1366 1366 self.endDate = endDate
1367 1367 self.startDate = startDate
1368 1368 # Added-----------------
1369 1369 self.selBlocksize = blocksize
1370 1370 self.selBlocktime = blocktime
1371 1371
1372 1372 # Verbose-----------
1373 1373 self.verbose = verbose
1374 1374 self.warnings = warnings
1375 1375
1376 1376 if not(self.setNextFile()):
1377 1377 if (startDate != None) and (endDate != None):
1378 print "[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime())
1378 print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime()))
1379 1379 elif startDate != None:
1380 print "[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime())
1380 print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime()))
1381 1381 else:
1382 print "[Reading] No files"
1382 print("[Reading] No files")
1383 1383
1384 1384 self.fileIndex = -1
1385 1385 self.pathList = []
1386 1386 self.filenameList = []
1387 1387 return
1388 1388
1389 1389 # self.getBasicHeader()
1390 1390
1391 1391 if last_set != None:
1392 1392 self.dataOut.last_block = last_set * \
1393 1393 self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1394 1394 return
1395 1395
1396 1396 def getBasicHeader(self):
1397 1397
1398 1398 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1399 1399 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1400 1400
1401 1401 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1402 1402
1403 1403 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1404 1404
1405 1405 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1406 1406
1407 1407 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1408 1408
1409 1409 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1410 1410
1411 1411 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1412 1412
1413 1413 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1414 1414
1415 1415 def getFirstHeader(self):
1416 1416
1417 1417 raise NotImplementedError
1418 1418
1419 1419 def getData(self):
1420 1420
1421 1421 raise NotImplementedError
1422 1422
1423 1423 def hasNotDataInBuffer(self):
1424 1424
1425 1425 raise NotImplementedError
1426 1426
1427 1427 def readBlock(self):
1428 1428
1429 1429 raise NotImplementedError
1430 1430
1431 1431 def isEndProcess(self):
1432 1432
1433 1433 return self.flagNoMoreFiles
1434 1434
1435 1435 def printReadBlocks(self):
1436 1436
1437 print "[Reading] Number of read blocks per file %04d" % self.nReadBlocks
1437 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1438 1438
1439 1439 def printTotalBlocks(self):
1440 1440
1441 print "[Reading] Number of read blocks %04d" % self.nTotalBlocks
1441 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1442 1442
1443 1443 def printNumberOfBlock(self):
1444 1444 'SPAM!'
1445 1445
1446 1446 # if self.flagIsNewBlock:
1447 1447 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1448 1448 # self.processingHeaderObj.dataBlocksPerFile,
1449 1449 # self.dataOut.datatime.ctime())
1450 1450
1451 1451 def printInfo(self):
1452 1452
1453 1453 if self.__printInfo == False:
1454 1454 return
1455 1455
1456 1456 self.basicHeaderObj.printInfo()
1457 1457 self.systemHeaderObj.printInfo()
1458 1458 self.radarControllerHeaderObj.printInfo()
1459 1459 self.processingHeaderObj.printInfo()
1460 1460
1461 1461 self.__printInfo = False
1462 1462
1463 1463 def run(self,
1464 1464 path=None,
1465 1465 startDate=None,
1466 1466 endDate=None,
1467 1467 startTime=datetime.time(0, 0, 0),
1468 1468 endTime=datetime.time(23, 59, 59),
1469 1469 set=None,
1470 1470 expLabel="",
1471 1471 ext=None,
1472 1472 online=False,
1473 1473 delay=60,
1474 1474 walk=True,
1475 1475 getblock=False,
1476 1476 nTxs=1,
1477 1477 realtime=False,
1478 1478 blocksize=None,
1479 1479 blocktime=None,
1480 1480 skip=None,
1481 1481 cursor=None,
1482 1482 warnings=True,
1483 1483 server=None,
1484 1484 verbose=True,
1485 1485 format=None,
1486 1486 oneDDict=None,
1487 1487 twoDDict=None,
1488 1488 ind2DList=None, **kwargs):
1489 1489
1490 1490 if not(self.isConfig):
1491 1491 self.setup(path=path,
1492 1492 startDate=startDate,
1493 1493 endDate=endDate,
1494 1494 startTime=startTime,
1495 1495 endTime=endTime,
1496 1496 set=set,
1497 1497 expLabel=expLabel,
1498 1498 ext=ext,
1499 1499 online=online,
1500 1500 delay=delay,
1501 1501 walk=walk,
1502 1502 getblock=getblock,
1503 1503 nTxs=nTxs,
1504 1504 realtime=realtime,
1505 1505 blocksize=blocksize,
1506 1506 blocktime=blocktime,
1507 1507 skip=skip,
1508 1508 cursor=cursor,
1509 1509 warnings=warnings,
1510 1510 server=server,
1511 1511 verbose=verbose,
1512 1512 format=format,
1513 1513 oneDDict=oneDDict,
1514 1514 twoDDict=twoDDict,
1515 1515 ind2DList=ind2DList)
1516 1516 self.isConfig = True
1517 1517 if server is None:
1518 1518 self.getData()
1519 1519 else:
1520 1520 self.getFromServer()
1521 1521
1522 1522
1523 1523 class JRODataWriter(JRODataIO):
1524 1524
1525 1525 """
1526 1526 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1527 1527 de los datos siempre se realiza por bloques.
1528 1528 """
1529 1529
1530 1530 blockIndex = 0
1531 1531
1532 1532 path = None
1533 1533
1534 1534 setFile = None
1535 1535
1536 1536 profilesPerBlock = None
1537 1537
1538 1538 blocksPerFile = None
1539 1539
1540 1540 nWriteBlocks = 0
1541 1541
1542 1542 fileDate = None
1543 1543
1544 1544 def __init__(self, dataOut=None):
1545 1545 raise NotImplementedError
1546 1546
1547 1547 def hasAllDataInBuffer(self):
1548 1548 raise NotImplementedError
1549 1549
1550 1550 def setBlockDimension(self):
1551 1551 raise NotImplementedError
1552 1552
1553 1553 def writeBlock(self):
1554 1554 raise NotImplementedError
1555 1555
1556 1556 def putData(self):
1557 1557 raise NotImplementedError
1558 1558
1559 1559 def getProcessFlags(self):
1560 1560
1561 1561 processFlags = 0
1562 1562
1563 1563 dtype_index = get_dtype_index(self.dtype)
1564 1564 procflag_dtype = get_procflag_dtype(dtype_index)
1565 1565
1566 1566 processFlags += procflag_dtype
1567 1567
1568 1568 if self.dataOut.flagDecodeData:
1569 1569 processFlags += PROCFLAG.DECODE_DATA
1570 1570
1571 1571 if self.dataOut.flagDeflipData:
1572 1572 processFlags += PROCFLAG.DEFLIP_DATA
1573 1573
1574 1574 if self.dataOut.code is not None:
1575 1575 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1576 1576
1577 1577 if self.dataOut.nCohInt > 1:
1578 1578 processFlags += PROCFLAG.COHERENT_INTEGRATION
1579 1579
1580 1580 if self.dataOut.type == "Spectra":
1581 1581 if self.dataOut.nIncohInt > 1:
1582 1582 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1583 1583
1584 1584 if self.dataOut.data_dc is not None:
1585 1585 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1586 1586
1587 1587 if self.dataOut.flagShiftFFT:
1588 1588 processFlags += PROCFLAG.SHIFT_FFT_DATA
1589 1589
1590 1590 return processFlags
1591 1591
1592 1592 def setBasicHeader(self):
1593 1593
1594 1594 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1595 1595 self.basicHeaderObj.version = self.versionFile
1596 1596 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1597 1597
1598 1598 utc = numpy.floor(self.dataOut.utctime)
1599 1599 milisecond = (self.dataOut.utctime - utc) * 1000.0
1600 1600
1601 1601 self.basicHeaderObj.utc = utc
1602 1602 self.basicHeaderObj.miliSecond = milisecond
1603 1603 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1604 1604 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1605 1605 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1606 1606
1607 1607 def setFirstHeader(self):
1608 1608 """
1609 1609 Obtiene una copia del First Header
1610 1610
1611 1611 Affected:
1612 1612
1613 1613 self.basicHeaderObj
1614 1614 self.systemHeaderObj
1615 1615 self.radarControllerHeaderObj
1616 1616 self.processingHeaderObj self.
1617 1617
1618 1618 Return:
1619 1619 None
1620 1620 """
1621 1621
1622 1622 raise NotImplementedError
1623 1623
1624 1624 def __writeFirstHeader(self):
1625 1625 """
1626 1626 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1627 1627
1628 1628 Affected:
1629 1629 __dataType
1630 1630
1631 1631 Return:
1632 1632 None
1633 1633 """
1634 1634
1635 1635 # CALCULAR PARAMETROS
1636 1636
1637 1637 sizeLongHeader = self.systemHeaderObj.size + \
1638 1638 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1639 1639 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1640 1640
1641 1641 self.basicHeaderObj.write(self.fp)
1642 1642 self.systemHeaderObj.write(self.fp)
1643 1643 self.radarControllerHeaderObj.write(self.fp)
1644 1644 self.processingHeaderObj.write(self.fp)
1645 1645
1646 1646 def __setNewBlock(self):
1647 1647 """
1648 1648 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1649 1649
1650 1650 Return:
1651 1651 0 : si no pudo escribir nada
1652 1652 1 : Si escribio el Basic el First Header
1653 1653 """
1654 1654 if self.fp == None:
1655 1655 self.setNextFile()
1656 1656
1657 1657 if self.flagIsNewFile:
1658 1658 return 1
1659 1659
1660 1660 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1661 1661 self.basicHeaderObj.write(self.fp)
1662 1662 return 1
1663 1663
1664 1664 if not(self.setNextFile()):
1665 1665 return 0
1666 1666
1667 1667 return 1
1668 1668
1669 1669 def writeNextBlock(self):
1670 1670 """
1671 1671 Selecciona el bloque siguiente de datos y los escribe en un file
1672 1672
1673 1673 Return:
1674 1674 0 : Si no hizo pudo escribir el bloque de datos
1675 1675 1 : Si no pudo escribir el bloque de datos
1676 1676 """
1677 1677 if not(self.__setNewBlock()):
1678 1678 return 0
1679 1679
1680 1680 self.writeBlock()
1681 1681
1682 print "[Writing] Block No. %d/%d" % (self.blockIndex,
1683 self.processingHeaderObj.dataBlocksPerFile)
1682 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1683 self.processingHeaderObj.dataBlocksPerFile))
1684 1684
1685 1685 return 1
1686 1686
1687 1687 def setNextFile(self):
1688 1688 """
1689 1689 Determina el siguiente file que sera escrito
1690 1690
1691 1691 Affected:
1692 1692 self.filename
1693 1693 self.subfolder
1694 1694 self.fp
1695 1695 self.setFile
1696 1696 self.flagIsNewFile
1697 1697
1698 1698 Return:
1699 1699 0 : Si el archivo no puede ser escrito
1700 1700 1 : Si el archivo esta listo para ser escrito
1701 1701 """
1702 1702 ext = self.ext
1703 1703 path = self.path
1704 1704
1705 1705 if self.fp != None:
1706 1706 self.fp.close()
1707 1707
1708 1708 timeTuple = time.localtime(self.dataOut.utctime)
1709 1709 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1710 1710
1711 1711 fullpath = os.path.join(path, subfolder)
1712 1712 setFile = self.setFile
1713 1713
1714 1714 if not(os.path.exists(fullpath)):
1715 1715 os.mkdir(fullpath)
1716 1716 setFile = -1 # inicializo mi contador de seteo
1717 1717 else:
1718 1718 filesList = os.listdir(fullpath)
1719 1719 if len(filesList) > 0:
1720 1720 filesList = sorted(filesList, key=str.lower)
1721 1721 filen = filesList[-1]
1722 1722 # el filename debera tener el siguiente formato
1723 1723 # 0 1234 567 89A BCDE (hex)
1724 1724 # x YYYY DDD SSS .ext
1725 1725 if isNumber(filen[8:11]):
1726 1726 # inicializo mi contador de seteo al seteo del ultimo file
1727 1727 setFile = int(filen[8:11])
1728 1728 else:
1729 1729 setFile = -1
1730 1730 else:
1731 1731 setFile = -1 # inicializo mi contador de seteo
1732 1732
1733 1733 setFile += 1
1734 1734
1735 1735 # If this is a new day it resets some values
1736 1736 if self.dataOut.datatime.date() > self.fileDate:
1737 1737 setFile = 0
1738 1738 self.nTotalBlocks = 0
1739 1739
1740 1740 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1741 1741 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1742 1742
1743 1743 filename = os.path.join(path, subfolder, filen)
1744 1744
1745 1745 fp = open(filename, 'wb')
1746 1746
1747 1747 self.blockIndex = 0
1748 1748
1749 1749 # guardando atributos
1750 1750 self.filename = filename
1751 1751 self.subfolder = subfolder
1752 1752 self.fp = fp
1753 1753 self.setFile = setFile
1754 1754 self.flagIsNewFile = 1
1755 1755 self.fileDate = self.dataOut.datatime.date()
1756 1756
1757 1757 self.setFirstHeader()
1758 1758
1759 print '[Writing] Opening file: %s' % self.filename
1759 print('[Writing] Opening file: %s' % self.filename)
1760 1760
1761 1761 self.__writeFirstHeader()
1762 1762
1763 1763 return 1
1764 1764
1765 1765 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1766 1766 """
1767 1767 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1768 1768
1769 1769 Inputs:
1770 1770 path : directory where data will be saved
1771 1771 profilesPerBlock : number of profiles per block
1772 1772 set : initial file set
1773 1773 datatype : An integer number that defines data type:
1774 1774 0 : int8 (1 byte)
1775 1775 1 : int16 (2 bytes)
1776 1776 2 : int32 (4 bytes)
1777 1777 3 : int64 (8 bytes)
1778 1778 4 : float32 (4 bytes)
1779 1779 5 : double64 (8 bytes)
1780 1780
1781 1781 Return:
1782 1782 0 : Si no realizo un buen seteo
1783 1783 1 : Si realizo un buen seteo
1784 1784 """
1785 1785
1786 1786 if ext == None:
1787 1787 ext = self.ext
1788 1788
1789 1789 self.ext = ext.lower()
1790 1790
1791 1791 self.path = path
1792 1792
1793 1793 if set is None:
1794 1794 self.setFile = -1
1795 1795 else:
1796 1796 self.setFile = set - 1
1797 1797
1798 1798 self.blocksPerFile = blocksPerFile
1799 1799
1800 1800 self.profilesPerBlock = profilesPerBlock
1801 1801
1802 1802 self.dataOut = dataOut
1803 1803 self.fileDate = self.dataOut.datatime.date()
1804 1804 # By default
1805 1805 self.dtype = self.dataOut.dtype
1806 1806
1807 1807 if datatype is not None:
1808 1808 self.dtype = get_numpy_dtype(datatype)
1809 1809
1810 1810 if not(self.setNextFile()):
1811 print "[Writing] There isn't a next file"
1811 print("[Writing] There isn't a next file")
1812 1812 return 0
1813 1813
1814 1814 self.setBlockDimension()
1815 1815
1816 1816 return 1
1817 1817
1818 1818 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1819 1819
1820 1820 if not(self.isConfig):
1821 1821
1822 1822 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1823 1823 set=set, ext=ext, datatype=datatype, **kwargs)
1824 1824 self.isConfig = True
1825 1825
1826 self.putData()
1826 self.putData() No newline at end of file
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now