The requested changes are too big and content was truncated. Show full diff
|
1 | NO CONTENT: new file 100644 | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: new file 100644 | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: new file 100644 | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: new file 100644 | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: new file 100644 | |
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,119 +1,132 | |||
|
1 | 1 | # Byte-compiled / optimized / DLL files |
|
2 | 2 | __pycache__/ |
|
3 | 3 | *.py[cod] |
|
4 | 4 | *$py.class |
|
5 | 5 | |
|
6 | 6 | # C extensions |
|
7 | 7 | *.so |
|
8 | 8 | |
|
9 | 9 | # Distribution / packaging |
|
10 | 10 | .Python |
|
11 | 11 | env/ |
|
12 | 12 | build/ |
|
13 | 13 | develop-eggs/ |
|
14 | 14 | dist/ |
|
15 | 15 | downloads/ |
|
16 | 16 | eggs/ |
|
17 | 17 | .eggs/ |
|
18 | 18 | lib/ |
|
19 | 19 | lib64/ |
|
20 | 20 | parts/ |
|
21 | 21 | sdist/ |
|
22 | 22 | var/ |
|
23 | 23 | wheels/ |
|
24 | 24 | *.egg-info/ |
|
25 | 25 | .installed.cfg |
|
26 | 26 | *.egg |
|
27 | 27 | |
|
28 | 28 | # PyInstaller |
|
29 | 29 | # Usually these files are written by a python script from a template |
|
30 | 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. |
|
31 | 31 | *.manifest |
|
32 | 32 | *.spec |
|
33 | 33 | |
|
34 | 34 | # Installer logs |
|
35 | 35 | pip-log.txt |
|
36 | 36 | pip-delete-this-directory.txt |
|
37 | 37 | |
|
38 | 38 | # Unit test / coverage reports |
|
39 | 39 | htmlcov/ |
|
40 | 40 | .tox/ |
|
41 | 41 | .coverage |
|
42 | 42 | .coverage.* |
|
43 | 43 | .cache |
|
44 | 44 | nosetests.xml |
|
45 | 45 | coverage.xml |
|
46 | 46 | *,cover |
|
47 | 47 | .hypothesis/ |
|
48 | 48 | |
|
49 | 49 | # Translations |
|
50 | 50 | *.mo |
|
51 | 51 | *.pot |
|
52 | 52 | |
|
53 | 53 | # Django stuff: |
|
54 | 54 | *.log |
|
55 | 55 | local_settings.py |
|
56 | 56 | |
|
57 | 57 | # Flask stuff: |
|
58 | 58 | instance/ |
|
59 | 59 | .webassets-cache |
|
60 | 60 | |
|
61 | 61 | # Scrapy stuff: |
|
62 | 62 | .scrapy |
|
63 | 63 | |
|
64 | 64 | # Sphinx documentation |
|
65 | 65 | docs/_build/ |
|
66 | 66 | |
|
67 | 67 | # PyBuilder |
|
68 | 68 | target/ |
|
69 | 69 | |
|
70 | 70 | # Jupyter Notebook |
|
71 | 71 | .ipynb_checkpoints |
|
72 | 72 | |
|
73 | 73 | # pyenv |
|
74 | 74 | .python-version |
|
75 | 75 | |
|
76 | 76 | # celery beat schedule file |
|
77 | 77 | celerybeat-schedule |
|
78 | 78 | |
|
79 | 79 | # SageMath parsed files |
|
80 | 80 | *.sage.py |
|
81 | 81 | |
|
82 | 82 | # dotenv |
|
83 | 83 | .env |
|
84 | 84 | |
|
85 | 85 | # virtualenv |
|
86 | 86 | .venv |
|
87 | 87 | venv/ |
|
88 | 88 | ENV/ |
|
89 | 89 | |
|
90 | 90 | # Spyder project settings |
|
91 | 91 | .spyderproject |
|
92 | 92 | .spyproject |
|
93 | 93 | |
|
94 | 94 | # Rope project settings |
|
95 | 95 | .ropeproject |
|
96 | 96 | |
|
97 | 97 | # mkdocs documentation |
|
98 | 98 | /site |
|
99 | 99 | |
|
100 | 100 | # eclipse |
|
101 | 101 | .project |
|
102 | 102 | .pydevproject |
|
103 | 103 | <<<<<<< HEAD |
|
104 | <<<<<<< HEAD | |
|
105 | ======= | |
|
106 | >>>>>>> v2.3 | |
|
104 | 107 | |
|
105 | 108 | # vscode |
|
106 | 109 | |
|
107 | 110 | .vscode |
|
108 | 111 | |
|
109 | 112 | schaingui/node_modules/ |
|
110 | 113 | schainpy/scripts/ |
|
114 | <<<<<<< HEAD | |
|
111 | 115 | ======= |
|
112 | 116 | .svn/ |
|
113 | 117 | *.png |
|
114 | 118 | *.pyc |
|
115 | 119 | schainpy/scripts |
|
116 | 120 | .vscode |
|
117 | 121 | schaingui/node_modules |
|
118 | 122 | trash |
|
119 | 123 | >>>>>>> master |
|
124 | ======= | |
|
125 | schaingui/node_modules/ | |
|
126 | .svn/ | |
|
127 | *.png | |
|
128 | *.pyc | |
|
129 | *.xml | |
|
130 | *.log | |
|
131 | trash | |
|
132 | >>>>>>> v2.3 |
@@ -1,188 +1,157 | |||
|
1 | 1 | import click |
|
2 | 2 | import schainpy |
|
3 | 3 | import subprocess |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import glob |
|
7 | 7 | save_stdout = sys.stdout |
|
8 | 8 | sys.stdout = open('trash', 'w') |
|
9 | 9 | from multiprocessing import cpu_count |
|
10 | 10 | from schaincli import templates |
|
11 |
from schainpy import |
|
|
11 | from schainpy.controller import Project | |
|
12 | 12 | from schainpy.model import Operation, ProcessingUnit |
|
13 | 13 | from schainpy.utils import log |
|
14 | 14 | from importlib import import_module |
|
15 | 15 | from pydoc import locate |
|
16 | 16 | from fuzzywuzzy import process |
|
17 | from schainpy.utils import paramsFinder | |
|
17 | 18 | sys.stdout = save_stdout |
|
18 | 19 | |
|
19 | 20 | |
|
20 | 21 | def print_version(ctx, param, value): |
|
21 | 22 | if not value or ctx.resilient_parsing: |
|
22 | 23 | return |
|
23 | 24 | click.echo(schainpy.__version__) |
|
24 | 25 | ctx.exit() |
|
25 | 26 | |
|
26 | 27 | |
|
27 | 28 | cliLogger = log.makelogger('schain cli') |
|
28 | 29 | PREFIX = 'experiment' |
|
29 | 30 | |
|
30 | 31 | |
|
31 | 32 | @click.command() |
|
32 | 33 | @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str) |
|
33 | 34 | @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True)) |
|
34 | 35 | @click.argument('command', default='run', required=True) |
|
35 | 36 | @click.argument('nextcommand', default=None, required=False, type=str) |
|
36 | 37 | def main(command, nextcommand, version, xml): |
|
37 | 38 | """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY \n |
|
38 | 39 | Available commands.\n |
|
39 | 40 | --xml: runs a schain XML generated file\n |
|
40 | 41 | run: runs any python script starting 'experiment_'\n |
|
41 | 42 | generate: generates a template schain script\n |
|
42 | 43 | search: return avilable operations, procs or arguments of the give operation/proc\n""" |
|
43 | 44 | if xml is not None: |
|
44 | 45 | runFromXML(xml) |
|
45 | 46 | elif command == 'generate': |
|
46 | 47 | generate() |
|
47 | 48 | elif command == 'test': |
|
48 | 49 | test() |
|
49 | 50 | elif command == 'run': |
|
50 | 51 | runschain(nextcommand) |
|
51 | 52 | elif command == 'search': |
|
52 | 53 | search(nextcommand) |
|
53 | 54 | else: |
|
54 | 55 | log.error('Command {} is not defined'.format(command)) |
|
55 | 56 | |
|
56 | 57 | def check_module(possible, instance): |
|
57 | 58 | def check(x): |
|
58 | 59 | try: |
|
59 | 60 | instancia = locate('schainpy.model.{}'.format(x)) |
|
60 | 61 | return isinstance(instancia(), instance) |
|
61 | 62 | except Exception as e: |
|
62 | 63 | return False |
|
63 | 64 | clean = clean_modules(possible) |
|
64 | 65 | return [x for x in clean if check(x)] |
|
65 | 66 | |
|
66 | 67 | |
|
67 | 68 | def clean_modules(module): |
|
68 | 69 | noEndsUnder = [x for x in module if not x.endswith('__')] |
|
69 | 70 | noStartUnder = [x for x in noEndsUnder if not x.startswith('__')] |
|
70 | 71 | noFullUpper = [x for x in noStartUnder if not x.isupper()] |
|
71 | 72 | return noFullUpper |
|
72 | 73 | |
|
73 | 74 | |
|
74 | 75 | def search(nextcommand): |
|
75 | 76 | if nextcommand is None: |
|
76 | 77 | log.error('There is no Operation/ProcessingUnit to search') |
|
77 | 78 | elif nextcommand == 'procs': |
|
78 | module = dir(import_module('schainpy.model')) | |
|
79 | procs = check_module(module, ProcessingUnit) | |
|
80 | try: | |
|
81 | procs.remove('ProcessingUnit') | |
|
82 | except Exception as e: | |
|
83 | pass | |
|
79 | procs = paramsFinder.getProcs() | |
|
84 | 80 | log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs))) |
|
85 | 81 | |
|
86 | 82 | elif nextcommand == 'operations': |
|
87 | module = dir(import_module('schainpy.model')) | |
|
88 | noProcs = [x for x in module if not x.endswith('Proc')] | |
|
89 | operations = check_module(noProcs, Operation) | |
|
90 | try: | |
|
91 | operations.remove('Operation') | |
|
92 | except Exception as e: | |
|
93 | pass | |
|
83 | operations = paramsFinder.getOperations() | |
|
94 | 84 | log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations))) |
|
95 | 85 | else: |
|
96 | 86 | try: |
|
97 |
|
|
|
98 | args = module().getAllowedArgs() | |
|
87 | args = paramsFinder.getArgs(nextcommand) | |
|
99 | 88 | log.warning('Use this feature with caution. It may not return all the allowed arguments') |
|
100 | try: | |
|
101 | args.remove('self') | |
|
102 | except Exception as e: | |
|
103 | pass | |
|
104 | try: | |
|
105 | args.remove('dataOut') | |
|
106 | except Exception as e: | |
|
107 | pass | |
|
108 | 89 | if len(args) == 0: |
|
109 | 90 | log.success('{} has no arguments'.format(nextcommand)) |
|
110 | 91 | else: |
|
111 | 92 | log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args))) |
|
112 | 93 | except Exception as e: |
|
113 | 94 | log.error('Module {} does not exists'.format(nextcommand)) |
|
114 |
allModules = |
|
|
115 | module = check_module(allModules, Operation) | |
|
116 | module.extend(check_module(allModules, ProcessingUnit)) | |
|
117 | similar = process.extractOne(nextcommand, module)[0] | |
|
118 | log.success('Searching {} instead'.format(similar)) | |
|
95 | allModules = paramsFinder.getAll() | |
|
96 | similar = process.extractOne(nextcommand, allModules)[0] | |
|
97 | log.success('Showing {} instead'.format(similar)) | |
|
119 | 98 | search(similar) |
|
120 | 99 | |
|
121 | 100 | |
|
122 | 101 | def runschain(nextcommand): |
|
123 | 102 | if nextcommand is None: |
|
124 | 103 | currentfiles = glob.glob('./{}_*.py'.format(PREFIX)) |
|
125 | 104 | numberfiles = len(currentfiles) |
|
126 | 105 | if numberfiles > 1: |
|
127 | 106 | log.error('There is more than one file to run') |
|
128 | 107 | elif numberfiles == 1: |
|
129 | 108 | subprocess.call(['python ' + currentfiles[0]], shell=True) |
|
130 | 109 | else: |
|
131 | 110 | log.error('There is no file to run') |
|
132 | 111 | else: |
|
133 | 112 | try: |
|
134 | 113 | subprocess.call(['python ' + nextcommand], shell=True) |
|
135 | 114 | except Exception as e: |
|
136 | 115 | log.error("I cannot run the file. Does it exists?") |
|
137 | 116 | |
|
138 | 117 | |
|
139 | 118 | def basicInputs(): |
|
140 | 119 | inputs = {} |
|
141 | 120 | inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str) |
|
142 | 121 | inputs['name'] = click.prompt('Name of the project', default="project", type=str) |
|
143 | 122 | inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True)) |
|
144 | 123 | inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str) |
|
145 | 124 | inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str) |
|
146 | 125 | inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str) |
|
147 | 126 | inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str) |
|
148 | 127 | inputs['figpath'] = inputs['path'] + '/figs' |
|
149 | 128 | return inputs |
|
150 | 129 | |
|
151 | 130 | |
|
152 | 131 | def generate(): |
|
153 | 132 | inputs = basicInputs() |
|
154 | 133 | inputs['multiprocess'] = click.confirm('Is this a multiprocess script?') |
|
155 | 134 | if inputs['multiprocess']: |
|
156 | 135 | inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int) |
|
157 | 136 | current = templates.multiprocess.format(**inputs) |
|
158 | 137 | else: |
|
159 | 138 | current = templates.basic.format(**inputs) |
|
160 | 139 | scriptname = '{}_{}.py'.format(PREFIX, inputs['name']) |
|
161 | 140 | script = open(scriptname, 'w') |
|
162 | 141 | try: |
|
163 | 142 | script.write(current) |
|
164 | 143 | log.success('Script {} generated'.format(scriptname)) |
|
165 | 144 | except Exception as e: |
|
166 | 145 | log.error('I cannot create the file. Do you have writing permissions?') |
|
167 | 146 | |
|
168 | 147 | |
|
169 | 148 | def test(): |
|
170 | 149 | log.warning('testing') |
|
171 | 150 | |
|
172 | 151 | |
|
173 | 152 | def runFromXML(filename): |
|
174 |
controller = |
|
|
153 | controller = Project() | |
|
175 | 154 | if not controller.readXml(filename): |
|
176 | 155 | return |
|
177 | ||
|
178 | plotterObj = controller.useExternalPlotter() | |
|
179 | ||
|
180 | 156 | controller.start() |
|
181 | plotterObj.start() | |
|
182 | ||
|
183 | cliLogger("Finishing all processes") | |
|
184 | ||
|
185 | controller.join(5) | |
|
186 | ||
|
187 | cliLogger("End of script") | |
|
188 | 157 | return |
@@ -1,108 +1,109 | |||
|
1 | 1 | ## CHANGELOG: |
|
2 | 2 | |
|
3 | 3 | ### 2.3 |
|
4 | 4 | * Added high order function `multiSchain` for multiprocessing scripts. |
|
5 | 5 | * Added two new Processing Units `PublishData` and `ReceiverData` for receiving and sending dataOut through multiple ways (tcp, ipc, inproc). |
|
6 | 6 | * Added a new graphics Processing Unit `PlotterReceiver`. It is decoupled from normal processing sequence with support for data generated by multiprocessing scripts. |
|
7 | 7 | * Added support for sending realtime graphic to web server. |
|
8 |
* |
|
|
8 | * GUI command `schain` is now `schainGUI`. | |
|
9 | 9 | * Added a CLI tool named `schain`. |
|
10 | 10 | * Scripts templates can be now generated with `schain generate`. |
|
11 | 11 | * Now it is possible to search Processing Units and Operations with `schain search [module]` to get the right name and its allowed parameters. |
|
12 | * `schain xml` to run xml scripts. | |
|
12 | 13 | * Added suggestions when parameters are poorly written. |
|
13 | 14 | * `Controller.start()` now runs in a different process than the process calling it. |
|
14 | 15 | * Added `schainpy.utils.log` for log standarization. |
|
15 | 16 | * Running script on online mode no longer ignores date and hour. Issue #1109. |
|
16 | 17 | * Added support for receving voltage data directly from JARS (tcp, ipc). |
|
17 |
* Updated README for |
|
|
18 | * Updated README for MAC OS GUI installation. | |
|
18 | 19 | * Setup now installs numpy. |
|
19 | 20 | |
|
20 | 21 | ### 2.2.6 |
|
21 | 22 | * Graphics generated by the GUI are now the same as generated by scripts. Issue #1074. |
|
22 | 23 | * Added support for C extensions. |
|
23 | 24 | * Function `hildebrand_sehkon` optimized with a C wrapper. |
|
24 | 25 | * Numpy version updated. |
|
25 | 26 | * Migration to GIT. |
|
26 | 27 | |
|
27 | 28 | ### 2.2.5: |
|
28 | 29 | * splitProfiles and combineProfiles modules were added to VoltageProc and Signal Chain GUI. |
|
29 | 30 | * nProfiles of USRP data (hdf5) is the number of profiles thera are in one second. |
|
30 | 31 | * jroPlotter works directly with data objects instead of dictionaries |
|
31 | 32 | * script "schain" was added to Signal Chain installer |
|
32 | 33 | |
|
33 | 34 | ### 2.2.4.1: |
|
34 | 35 | * jroIO_usrp.py is update to read Sandra's data |
|
35 | 36 | * decimation in Spectra and RTI plots is always enabled. |
|
36 | 37 | * time* window option added to GUI |
|
37 | 38 | |
|
38 | 39 | ### 2.2.4: |
|
39 | 40 | * jroproc_spectra_lags.py added to schainpy |
|
40 | 41 | * Bug fixed in schainGUI: ProcUnit was created with the same id in some cases. |
|
41 | 42 | * Bug fixed in jroHeaderIO: Header size validation. |
|
42 | 43 | |
|
43 | 44 | ### 2.2.3.1: |
|
44 | 45 | * Filtering block by time has been added. |
|
45 | 46 | * Bug fixed plotting RTI, CoherenceMap and others using xmin and xmax parameters. The first day worked |
|
46 | 47 | properly but the next days did not. |
|
47 | 48 | |
|
48 | 49 | ### 2.2.3: |
|
49 | 50 | * Bug fixed in GUI: Error getting(reading) Code value |
|
50 | 51 | * Bug fixed in GUI: Flip option always needs channelList field |
|
51 | 52 | * Bug fixed in jrodata: when one branch modified a value in "dataOut" (example: dataOut.code) this value |
|
52 | 53 | was modified for every branch (because this was a reference). It was modified in data.copy() |
|
53 | 54 | * Bug fixed in jroproc_voltage.profileSelector(): rangeList replaces to profileRangeList. |
|
54 | 55 | |
|
55 | 56 | ### 2.2.2: |
|
56 | 57 | * VoltageProc: ProfileSelector, Reshape, Decoder with nTxs!=1 and getblock=True was tested |
|
57 | 58 | * Rawdata and testRawdata.py added to Signal Chain project |
|
58 | 59 | |
|
59 | 60 | ### 2.2.1: |
|
60 | 61 | * Bugs fixed in GUI |
|
61 | 62 | * Views were improved in GUI |
|
62 | 63 | * Support to MST* ISR experiments |
|
63 | 64 | * Bug fixed getting noise using hyldebrant. (minimum number of points > 20%) |
|
64 | 65 | * handleError added to jroplotter.py |
|
65 | 66 | |
|
66 | 67 | ### 2.2.0: |
|
67 | 68 | * GUI: use of external plotter |
|
68 | 69 | * Compatible with matplotlib 1.5.0 |
|
69 | 70 | |
|
70 | 71 | ### 2.1.5: |
|
71 | 72 | * serializer module added to Signal Chain |
|
72 | 73 | * jroplotter.py added to Signal Chain |
|
73 | 74 | |
|
74 | 75 | ### 2.1.4.2: |
|
75 | 76 | * A new Plotter Class was added |
|
76 | 77 | * Project.start() does not accept filename as a parameter anymore |
|
77 | 78 | |
|
78 | 79 | ### 2.1.4.1: |
|
79 | 80 | * Send notifications when an error different to ValueError is detected |
|
80 | 81 | |
|
81 | 82 | ### 2.1.4: |
|
82 | 83 | * Sending error notifications to signal chain administrator |
|
83 | 84 | * Login to email server added |
|
84 | 85 | |
|
85 | 86 | ### 2.1.3.3: |
|
86 | 87 | * Colored Button Icons were added to GUI |
|
87 | 88 | |
|
88 | 89 | ### 2.1.3.2: |
|
89 | 90 | * GUI: user interaction enhanced |
|
90 | 91 | * controller_api.py: Safe access to ControllerThead |
|
91 | 92 | |
|
92 | 93 | ### 2.1.3.1: |
|
93 | 94 | * GUI: every icon were resized |
|
94 | 95 | * jroproc_voltage.py: Print a message when "Read from code" option is selected and the code is not defined inside data file |
|
95 | 96 | |
|
96 | 97 | ### 2.1.3: |
|
97 | 98 | * jroplot_heispectra.py: SpectraHeisScope was not showing the right channels |
|
98 | 99 | * jroproc_voltage.py: Bug fixed selecting profiles (self.nProfiles took a wrong value), |
|
99 | 100 | Bug fixed selecting heights by block (selecting profiles instead heights) |
|
100 | 101 | * jroproc_voltage.py: New feature added: decoding data by block using FFT. |
|
101 | 102 | * jroIO_heispectra.py: Bug fixed in FitsReader. Using local Fits instance instead schainpy.mode.data.jrodata.Fits. |
|
102 | 103 | * jroIO_heispectra.py: Channel index list does not exist. |
|
103 | 104 | |
|
104 | 105 | ### 2.1.2: |
|
105 | 106 | * jroutils_ftp.py: Bug fixed, Any error sending file stopped the Server Thread |
|
106 | 107 | Server thread opens and closes remote server each time file list is sent |
|
107 | 108 | * jroplot_spectra.py: Noise path was not being created when noise data is saved. |
|
108 | 109 | * jroIO_base.py: startTime can be greater than endTime. Example: SpreadF [18:00 * 07:00] No newline at end of file |
@@ -1,8 +1,7 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Feb 7, 2012 |
|
3 | 3 | |
|
4 | 4 | @author $Author$ |
|
5 | 5 | @version $Id$ |
|
6 | 6 | ''' |
|
7 | ||
|
8 | 7 | __version__ = "2.3" |
@@ -1,1294 +1,1280 | |||
|
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 schainpy |
|
11 | 11 | import schainpy.admin |
|
12 | from schainpy.utils.log import logToFile | |
|
12 | 13 | |
|
13 | 14 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring |
|
14 | 15 | from xml.dom import minidom |
|
15 | 16 | |
|
16 | 17 | from schainpy.model import * |
|
17 | 18 | from time import sleep |
|
18 | 19 | |
|
20 | ||
|
21 | ||
|
19 | 22 | def prettify(elem): |
|
20 | 23 | """Return a pretty-printed XML string for the Element. |
|
21 | 24 | """ |
|
22 | 25 | rough_string = tostring(elem, 'utf-8') |
|
23 | 26 | reparsed = minidom.parseString(rough_string) |
|
24 | 27 | return reparsed.toprettyxml(indent=" ") |
|
25 | 28 | |
|
29 | def multiSchain(child, nProcess=cpu_count(), startDate=None, endDate=None, by_day=False): | |
|
30 | skip = 0 | |
|
31 | cursor = 0 | |
|
32 | nFiles = None | |
|
33 | processes = [] | |
|
34 | dt1 = datetime.datetime.strptime(startDate, '%Y/%m/%d') | |
|
35 | dt2 = datetime.datetime.strptime(endDate, '%Y/%m/%d') | |
|
36 | days = (dt2 - dt1).days | |
|
37 | ||
|
38 | for day in range(days+1): | |
|
39 | skip = 0 | |
|
40 | cursor = 0 | |
|
41 | q = Queue() | |
|
42 | processes = [] | |
|
43 | dt = (dt1 + datetime.timedelta(day)).strftime('%Y/%m/%d') | |
|
44 | firstProcess = Process(target=child, args=(cursor, skip, q, dt)) | |
|
45 | firstProcess.start() | |
|
46 | if by_day: | |
|
47 | continue | |
|
48 | nFiles = q.get() | |
|
49 | if nFiles==0: | |
|
50 | continue | |
|
51 | firstProcess.terminate() | |
|
52 | skip = int(math.ceil(nFiles/nProcess)) | |
|
53 | while True: | |
|
54 | processes.append(Process(target=child, args=(cursor, skip, q, dt))) | |
|
55 | processes[cursor].start() | |
|
56 | if nFiles < cursor*skip: | |
|
57 | break | |
|
58 | cursor += 1 | |
|
59 | ||
|
60 | def beforeExit(exctype, value, trace): | |
|
61 | for process in processes: | |
|
62 | process.terminate() | |
|
63 | process.join() | |
|
64 | print traceback.print_tb(trace) | |
|
65 | ||
|
66 | sys.excepthook = beforeExit | |
|
67 | ||
|
68 | for process in processes: | |
|
69 | process.join() | |
|
70 | process.terminate() | |
|
71 | ||
|
72 | time.sleep(3) | |
|
73 | ||
|
74 | ||
|
26 | 75 | class ParameterConf(): |
|
27 | 76 | |
|
28 | 77 | id = None |
|
29 | 78 | name = None |
|
30 | 79 | value = None |
|
31 | 80 | format = None |
|
32 | 81 | |
|
33 | 82 | __formated_value = None |
|
34 | 83 | |
|
35 | 84 | ELEMENTNAME = 'Parameter' |
|
36 | 85 | |
|
37 | 86 | def __init__(self): |
|
38 | 87 | |
|
39 | 88 | self.format = 'str' |
|
40 | 89 | |
|
41 | 90 | def getElementName(self): |
|
42 | 91 | |
|
43 | 92 | return self.ELEMENTNAME |
|
44 | 93 | |
|
45 | 94 | def getValue(self): |
|
46 | 95 | |
|
47 | 96 | value = self.value |
|
48 | 97 | format = self.format |
|
49 | 98 | |
|
50 | 99 | if self.__formated_value != None: |
|
51 | 100 | |
|
52 | 101 | return self.__formated_value |
|
53 | 102 | |
|
54 | 103 | if format == 'str': |
|
55 | 104 | self.__formated_value = str(value) |
|
56 | 105 | return self.__formated_value |
|
57 | 106 | |
|
58 | 107 | if value == '': |
|
59 | 108 | raise ValueError, "%s: This parameter value is empty" %self.name |
|
60 | 109 | |
|
61 | 110 | if format == 'list': |
|
62 | 111 | strList = value.split(',') |
|
63 | 112 | |
|
64 | 113 | self.__formated_value = strList |
|
65 | 114 | |
|
66 | 115 | return self.__formated_value |
|
67 | 116 | |
|
68 | 117 | if format == 'intlist': |
|
69 | 118 | """ |
|
70 | 119 | Example: |
|
71 | 120 | value = (0,1,2) |
|
72 | 121 | """ |
|
73 | 122 | |
|
74 | 123 | new_value = ast.literal_eval(value) |
|
75 | 124 | |
|
76 | 125 | if type(new_value) not in (tuple, list): |
|
77 | 126 | new_value = [int(new_value)] |
|
78 | 127 | |
|
79 | 128 | self.__formated_value = new_value |
|
80 | 129 | |
|
81 | 130 | return self.__formated_value |
|
82 | 131 | |
|
83 | 132 | if format == 'floatlist': |
|
84 | 133 | """ |
|
85 | 134 | Example: |
|
86 | 135 | value = (0.5, 1.4, 2.7) |
|
87 | 136 | """ |
|
88 | 137 | |
|
89 | 138 | new_value = ast.literal_eval(value) |
|
90 | 139 | |
|
91 | 140 | if type(new_value) not in (tuple, list): |
|
92 | 141 | new_value = [float(new_value)] |
|
93 | 142 | |
|
94 | 143 | self.__formated_value = new_value |
|
95 | 144 | |
|
96 | 145 | return self.__formated_value |
|
97 | 146 | |
|
98 | 147 | if format == 'date': |
|
99 | 148 | strList = value.split('/') |
|
100 | 149 | intList = [int(x) for x in strList] |
|
101 | 150 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
102 | 151 | |
|
103 | 152 | self.__formated_value = date |
|
104 | 153 | |
|
105 | 154 | return self.__formated_value |
|
106 | 155 | |
|
107 | 156 | if format == 'time': |
|
108 | 157 | strList = value.split(':') |
|
109 | 158 | intList = [int(x) for x in strList] |
|
110 | 159 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
111 | 160 | |
|
112 | 161 | self.__formated_value = time |
|
113 | 162 | |
|
114 | 163 | return self.__formated_value |
|
115 | 164 | |
|
116 | 165 | if format == 'pairslist': |
|
117 | 166 | """ |
|
118 | 167 | Example: |
|
119 | 168 | value = (0,1),(1,2) |
|
120 | 169 | """ |
|
121 | 170 | |
|
122 | 171 | new_value = ast.literal_eval(value) |
|
123 | 172 | |
|
124 | 173 | if type(new_value) not in (tuple, list): |
|
125 | 174 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
126 | 175 | |
|
127 | 176 | if type(new_value[0]) not in (tuple, list): |
|
128 | 177 | if len(new_value) != 2: |
|
129 | 178 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
130 | 179 | new_value = [new_value] |
|
131 | 180 | |
|
132 | 181 | for thisPair in new_value: |
|
133 | 182 | if len(thisPair) != 2: |
|
134 | 183 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
135 | 184 | |
|
136 | 185 | self.__formated_value = new_value |
|
137 | 186 | |
|
138 | 187 | return self.__formated_value |
|
139 | 188 | |
|
140 | 189 | if format == 'multilist': |
|
141 | 190 | """ |
|
142 | 191 | Example: |
|
143 | 192 | value = (0,1,2),(3,4,5) |
|
144 | 193 | """ |
|
145 | 194 | multiList = ast.literal_eval(value) |
|
146 | 195 | |
|
147 | 196 | if type(multiList[0]) == int: |
|
148 | 197 | multiList = ast.literal_eval("(" + value + ")") |
|
149 | 198 | |
|
150 | 199 | self.__formated_value = multiList |
|
151 | 200 | |
|
152 | 201 | return self.__formated_value |
|
153 | 202 | |
|
154 | 203 | if format == 'bool': |
|
155 | 204 | value = int(value) |
|
156 | 205 | |
|
157 | 206 | if format == 'int': |
|
158 | 207 | value = float(value) |
|
159 | 208 | |
|
160 | 209 | format_func = eval(format) |
|
161 | 210 | |
|
162 | 211 | self.__formated_value = format_func(value) |
|
163 | 212 | |
|
164 | 213 | return self.__formated_value |
|
165 | 214 | |
|
166 | 215 | def updateId(self, new_id): |
|
167 | 216 | |
|
168 | 217 | self.id = str(new_id) |
|
169 | 218 | |
|
170 | 219 | def setup(self, id, name, value, format='str'): |
|
171 | 220 | |
|
172 | 221 | self.id = str(id) |
|
173 | 222 | self.name = name |
|
174 | 223 | self.value = str(value) |
|
175 | 224 | self.format = str.lower(format) |
|
176 | 225 | |
|
177 | 226 | self.getValue() |
|
178 | 227 | |
|
179 | 228 | return 1 |
|
180 | 229 | |
|
181 | 230 | def update(self, name, value, format='str'): |
|
182 | 231 | |
|
183 | 232 | self.name = name |
|
184 | 233 | self.value = str(value) |
|
185 | 234 | self.format = format |
|
186 | 235 | |
|
187 | 236 | def makeXml(self, opElement): |
|
188 | 237 | |
|
189 | 238 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
190 | 239 | parmElement.set('id', str(self.id)) |
|
191 | 240 | parmElement.set('name', self.name) |
|
192 | 241 | parmElement.set('value', self.value) |
|
193 | 242 | parmElement.set('format', self.format) |
|
194 | 243 | |
|
195 | 244 | def readXml(self, parmElement): |
|
196 | 245 | |
|
197 | 246 | self.id = parmElement.get('id') |
|
198 | 247 | self.name = parmElement.get('name') |
|
199 | 248 | self.value = parmElement.get('value') |
|
200 | 249 | self.format = str.lower(parmElement.get('format')) |
|
201 | 250 | |
|
202 | 251 | #Compatible with old signal chain version |
|
203 | 252 | if self.format == 'int' and self.name == 'idfigure': |
|
204 | 253 | self.name = 'id' |
|
205 | 254 | |
|
206 | 255 | def printattr(self): |
|
207 | 256 | |
|
208 | 257 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
209 | 258 | |
|
210 | class OperationConf(): | |
|
211 | ||
|
259 | class OperationConf(): | |
|
260 | ||
|
212 | 261 | id = None |
|
213 | 262 | name = None |
|
214 | 263 | priority = None |
|
215 | 264 | type = None |
|
216 | 265 | |
|
217 | 266 | parmConfObjList = [] |
|
218 | 267 | |
|
219 | 268 | ELEMENTNAME = 'Operation' |
|
220 | 269 | |
|
221 | 270 | def __init__(self): |
|
222 | 271 | |
|
223 | 272 | self.id = '0' |
|
224 | 273 | self.name = None |
|
225 | 274 | self.priority = None |
|
226 | 275 | self.type = 'self' |
|
227 | 276 | |
|
228 | 277 | |
|
229 | 278 | def __getNewId(self): |
|
230 | 279 | |
|
231 | 280 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
232 | 281 | |
|
233 | 282 | def updateId(self, new_id): |
|
234 | 283 | |
|
235 | 284 | self.id = str(new_id) |
|
236 | 285 | |
|
237 | 286 | n = 1 |
|
238 | 287 | for parmObj in self.parmConfObjList: |
|
239 | 288 | |
|
240 | 289 | idParm = str(int(new_id)*10 + n) |
|
241 | 290 | parmObj.updateId(idParm) |
|
242 | 291 | |
|
243 | 292 | n += 1 |
|
244 | 293 | |
|
245 | 294 | def getElementName(self): |
|
246 | 295 | |
|
247 | 296 | return self.ELEMENTNAME |
|
248 | 297 | |
|
249 | 298 | def getParameterObjList(self): |
|
250 | 299 | |
|
251 | 300 | return self.parmConfObjList |
|
252 | 301 | |
|
253 | 302 | def getParameterObj(self, parameterName): |
|
254 | 303 | |
|
255 | 304 | for parmConfObj in self.parmConfObjList: |
|
256 | 305 | |
|
257 | 306 | if parmConfObj.name != parameterName: |
|
258 | 307 | continue |
|
259 | 308 | |
|
260 | 309 | return parmConfObj |
|
261 | 310 | |
|
262 | 311 | return None |
|
263 | 312 | |
|
264 | 313 | def getParameterObjfromValue(self, parameterValue): |
|
265 | 314 | |
|
266 | 315 | for parmConfObj in self.parmConfObjList: |
|
267 | 316 | |
|
268 | 317 | if parmConfObj.getValue() != parameterValue: |
|
269 | 318 | continue |
|
270 | 319 | |
|
271 | 320 | return parmConfObj.getValue() |
|
272 | 321 | |
|
273 | 322 | return None |
|
274 | 323 | |
|
275 | 324 | def getParameterValue(self, parameterName): |
|
276 | 325 | |
|
277 | 326 | parameterObj = self.getParameterObj(parameterName) |
|
278 | 327 | |
|
279 | 328 | # if not parameterObj: |
|
280 | 329 | # return None |
|
281 | 330 | |
|
282 | 331 | value = parameterObj.getValue() |
|
283 | 332 | |
|
284 | 333 | return value |
|
285 | 334 | |
|
286 | 335 | def setup(self, id, name, priority, type): |
|
287 | 336 | |
|
288 | 337 | self.id = str(id) |
|
289 | 338 | self.name = name |
|
290 | 339 | self.type = type |
|
291 | 340 | self.priority = priority |
|
292 | 341 | |
|
293 | 342 | self.parmConfObjList = [] |
|
294 | 343 | |
|
295 | 344 | def removeParameters(self): |
|
296 | 345 | |
|
297 | 346 | for obj in self.parmConfObjList: |
|
298 | 347 | del obj |
|
299 | 348 | |
|
300 | 349 | self.parmConfObjList = [] |
|
301 | 350 | |
|
302 | 351 | def addParameter(self, name, value, format='str'): |
|
303 | 352 | |
|
304 | 353 | id = self.__getNewId() |
|
305 | 354 | |
|
306 | 355 | parmConfObj = ParameterConf() |
|
307 | 356 | if not parmConfObj.setup(id, name, value, format): |
|
308 | 357 | return None |
|
309 | 358 | |
|
310 | 359 | self.parmConfObjList.append(parmConfObj) |
|
311 | 360 | |
|
312 | 361 | return parmConfObj |
|
313 | 362 | |
|
314 | 363 | def changeParameter(self, name, value, format='str'): |
|
315 | 364 | |
|
316 | 365 | parmConfObj = self.getParameterObj(name) |
|
317 | 366 | parmConfObj.update(name, value, format) |
|
318 | 367 | |
|
319 | 368 | return parmConfObj |
|
320 | 369 | |
|
321 | 370 | def makeXml(self, procUnitElement): |
|
322 | 371 | |
|
323 | 372 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
324 | 373 | opElement.set('id', str(self.id)) |
|
325 | 374 | opElement.set('name', self.name) |
|
326 | 375 | opElement.set('type', self.type) |
|
327 | 376 | opElement.set('priority', str(self.priority)) |
|
328 | 377 | |
|
329 | 378 | for parmConfObj in self.parmConfObjList: |
|
330 | 379 | parmConfObj.makeXml(opElement) |
|
331 | 380 | |
|
332 | 381 | def readXml(self, opElement): |
|
333 | 382 | |
|
334 | 383 | self.id = opElement.get('id') |
|
335 | 384 | self.name = opElement.get('name') |
|
336 | 385 | self.type = opElement.get('type') |
|
337 | 386 | self.priority = opElement.get('priority') |
|
338 | 387 | |
|
339 | 388 | #Compatible with old signal chain version |
|
340 | 389 | #Use of 'run' method instead 'init' |
|
341 | 390 | if self.type == 'self' and self.name == 'init': |
|
342 | 391 | self.name = 'run' |
|
343 | 392 | |
|
344 | 393 | self.parmConfObjList = [] |
|
345 | 394 | |
|
346 | 395 | parmElementList = opElement.iter(ParameterConf().getElementName()) |
|
347 | 396 | |
|
348 | 397 | for parmElement in parmElementList: |
|
349 | 398 | parmConfObj = ParameterConf() |
|
350 | 399 | parmConfObj.readXml(parmElement) |
|
351 | 400 | |
|
352 | 401 | #Compatible with old signal chain version |
|
353 | 402 | #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
354 | 403 | if self.type != 'self' and self.name == 'Plot': |
|
355 | 404 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
356 | 405 | self.name = parmConfObj.value |
|
357 | 406 | continue |
|
358 | 407 | |
|
359 | 408 | self.parmConfObjList.append(parmConfObj) |
|
360 | 409 | |
|
361 | 410 | def printattr(self): |
|
362 | 411 | |
|
363 | 412 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
364 | 413 | self.id, |
|
365 | 414 | self.name, |
|
366 | 415 | self.type, |
|
367 | 416 | self.priority) |
|
368 | 417 | |
|
369 | 418 | for parmConfObj in self.parmConfObjList: |
|
370 | 419 | parmConfObj.printattr() |
|
371 | 420 | |
|
372 | 421 | def createObject(self, plotter_queue=None): |
|
373 | 422 | |
|
374 | 423 | if self.type == 'self': |
|
375 | 424 | raise ValueError, "This operation type cannot be created" |
|
376 | 425 | |
|
377 | 426 | if self.type == 'plotter': |
|
378 | 427 | #Plotter(plotter_name) |
|
379 | 428 | if not plotter_queue: |
|
380 | 429 | raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)" |
|
381 | 430 | |
|
382 | 431 | opObj = Plotter(self.name, plotter_queue) |
|
383 | 432 | |
|
384 | 433 | if self.type == 'external' or self.type == 'other': |
|
385 | 434 | className = eval(self.name) |
|
386 | 435 | opObj = className() |
|
387 | 436 | |
|
388 | 437 | return opObj |
|
389 | 438 | |
|
390 | 439 | class ProcUnitConf(): |
|
391 | 440 | |
|
392 | 441 | id = None |
|
393 | 442 | name = None |
|
394 | 443 | datatype = None |
|
395 | 444 | inputId = None |
|
396 | 445 | parentId = None |
|
397 | 446 | |
|
398 | 447 | opConfObjList = [] |
|
399 | 448 | |
|
400 | 449 | procUnitObj = None |
|
401 | 450 | opObjList = [] |
|
402 | 451 | |
|
403 | 452 | ELEMENTNAME = 'ProcUnit' |
|
404 | 453 | |
|
405 | 454 | def __init__(self): |
|
406 | 455 | |
|
407 | 456 | self.id = None |
|
408 | 457 | self.datatype = None |
|
409 | 458 | self.name = None |
|
410 | 459 | self.inputId = None |
|
411 | 460 | |
|
412 | 461 | self.opConfObjList = [] |
|
413 | 462 | |
|
414 | 463 | self.procUnitObj = None |
|
415 | 464 | self.opObjDict = {} |
|
416 | 465 | |
|
417 | 466 | def __getPriority(self): |
|
418 | 467 | |
|
419 | 468 | return len(self.opConfObjList)+1 |
|
420 | 469 | |
|
421 | 470 | def __getNewId(self): |
|
422 | 471 | |
|
423 | 472 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
424 | 473 | |
|
425 | 474 | def getElementName(self): |
|
426 | 475 | |
|
427 | 476 | return self.ELEMENTNAME |
|
428 | 477 | |
|
429 | 478 | def getId(self): |
|
430 | 479 | |
|
431 | 480 | return self.id |
|
432 | 481 | |
|
433 | 482 | def updateId(self, new_id, parentId=parentId): |
|
434 | 483 | |
|
435 | 484 | |
|
436 | 485 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
437 | 486 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
438 | 487 | |
|
439 | 488 | #If this proc unit has not inputs |
|
440 | 489 | if self.inputId == '0': |
|
441 | 490 | new_inputId = 0 |
|
442 | 491 | |
|
443 | 492 | n = 1 |
|
444 | 493 | for opConfObj in self.opConfObjList: |
|
445 | 494 | |
|
446 | 495 | idOp = str(int(new_id)*10 + n) |
|
447 | 496 | opConfObj.updateId(idOp) |
|
448 | 497 | |
|
449 | 498 | n += 1 |
|
450 | 499 | |
|
451 | 500 | self.parentId = str(parentId) |
|
452 | 501 | self.id = str(new_id) |
|
453 | 502 | self.inputId = str(new_inputId) |
|
454 | 503 | |
|
455 | 504 | |
|
456 | 505 | def getInputId(self): |
|
457 | 506 | |
|
458 | 507 | return self.inputId |
|
459 | 508 | |
|
460 | 509 | def getOperationObjList(self): |
|
461 | 510 | |
|
462 | 511 | return self.opConfObjList |
|
463 | 512 | |
|
464 | 513 | def getOperationObj(self, name=None): |
|
465 | 514 | |
|
466 | 515 | for opConfObj in self.opConfObjList: |
|
467 | 516 | |
|
468 | 517 | if opConfObj.name != name: |
|
469 | 518 | continue |
|
470 | 519 | |
|
471 | 520 | return opConfObj |
|
472 | 521 | |
|
473 | 522 | return None |
|
474 | 523 | |
|
475 | 524 | def getOpObjfromParamValue(self, value=None): |
|
476 | 525 | |
|
477 | 526 | for opConfObj in self.opConfObjList: |
|
478 | 527 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
479 | 528 | continue |
|
480 | 529 | return opConfObj |
|
481 | 530 | return None |
|
482 | 531 | |
|
483 | 532 | def getProcUnitObj(self): |
|
484 | 533 | |
|
485 | 534 | return self.procUnitObj |
|
486 | 535 | |
|
487 | 536 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
488 | 537 | |
|
489 | 538 | #Compatible with old signal chain version |
|
490 | 539 | if datatype==None and name==None: |
|
491 | 540 | raise ValueError, "datatype or name should be defined" |
|
492 | 541 | |
|
493 | 542 | if name==None: |
|
494 | 543 | if 'Proc' in datatype: |
|
495 | 544 | name = datatype |
|
496 | 545 | else: |
|
497 | 546 | name = '%sProc' %(datatype) |
|
498 | 547 | |
|
499 | 548 | if datatype==None: |
|
500 | 549 | datatype = name.replace('Proc','') |
|
501 | 550 | |
|
502 | 551 | self.id = str(id) |
|
503 | 552 | self.name = name |
|
504 | 553 | self.datatype = datatype |
|
505 | 554 | self.inputId = inputId |
|
506 | 555 | self.parentId = parentId |
|
507 | 556 | |
|
508 | 557 | self.opConfObjList = [] |
|
509 | 558 | |
|
510 | 559 | self.addOperation(name='run', optype='self') |
|
511 | 560 | |
|
512 | 561 | def removeOperations(self): |
|
513 | 562 | |
|
514 | 563 | for obj in self.opConfObjList: |
|
515 | 564 | del obj |
|
516 | 565 | |
|
517 | 566 | self.opConfObjList = [] |
|
518 | 567 | self.addOperation(name='run') |
|
519 | 568 | |
|
520 | 569 | def addParameter(self, **kwargs): |
|
521 | 570 | ''' |
|
522 | 571 | Add parameters to "run" operation |
|
523 | 572 | ''' |
|
524 | 573 | opObj = self.opConfObjList[0] |
|
525 | 574 | |
|
526 | 575 | opObj.addParameter(**kwargs) |
|
527 | 576 | |
|
528 | 577 | return opObj |
|
529 | 578 | |
|
530 | 579 | def addOperation(self, name, optype='self'): |
|
531 | 580 | |
|
532 | 581 | id = self.__getNewId() |
|
533 | 582 | priority = self.__getPriority() |
|
534 | 583 | |
|
535 | 584 | opConfObj = OperationConf() |
|
536 | 585 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
537 | 586 | |
|
538 | 587 | self.opConfObjList.append(opConfObj) |
|
539 | 588 | |
|
540 | 589 | return opConfObj |
|
541 | 590 | |
|
542 | 591 | def makeXml(self, projectElement): |
|
543 | 592 | |
|
544 | 593 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
545 | 594 | procUnitElement.set('id', str(self.id)) |
|
546 | 595 | procUnitElement.set('name', self.name) |
|
547 | 596 | procUnitElement.set('datatype', self.datatype) |
|
548 | 597 | procUnitElement.set('inputId', str(self.inputId)) |
|
549 | 598 | |
|
550 | 599 | for opConfObj in self.opConfObjList: |
|
551 | 600 | opConfObj.makeXml(procUnitElement) |
|
552 | 601 | |
|
553 | 602 | def readXml(self, upElement): |
|
554 | 603 | |
|
555 | 604 | self.id = upElement.get('id') |
|
556 | 605 | self.name = upElement.get('name') |
|
557 | 606 | self.datatype = upElement.get('datatype') |
|
558 | 607 | self.inputId = upElement.get('inputId') |
|
559 | 608 | |
|
560 | 609 | if self.ELEMENTNAME == "ReadUnit": |
|
561 | 610 | self.datatype = self.datatype.replace("Reader", "") |
|
562 | 611 | |
|
563 | 612 | if self.ELEMENTNAME == "ProcUnit": |
|
564 | 613 | self.datatype = self.datatype.replace("Proc", "") |
|
565 | 614 | |
|
566 | 615 | if self.inputId == 'None': |
|
567 | 616 | self.inputId = '0' |
|
568 | 617 | |
|
569 | 618 | self.opConfObjList = [] |
|
570 | 619 | |
|
571 | 620 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
572 | 621 | |
|
573 | 622 | for opElement in opElementList: |
|
574 | 623 | opConfObj = OperationConf() |
|
575 | 624 | opConfObj.readXml(opElement) |
|
576 | 625 | self.opConfObjList.append(opConfObj) |
|
577 | 626 | |
|
578 | 627 | def printattr(self): |
|
579 | 628 | |
|
580 | 629 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
581 | 630 | self.id, |
|
582 | 631 | self.name, |
|
583 | 632 | self.datatype, |
|
584 | 633 | self.inputId) |
|
585 | 634 | |
|
586 | 635 | for opConfObj in self.opConfObjList: |
|
587 | 636 | opConfObj.printattr() |
|
588 | 637 | |
|
589 | 638 | def createObjects(self, plotter_queue=None): |
|
590 | 639 | |
|
591 | 640 | className = eval(self.name) |
|
592 | 641 | procUnitObj = className() |
|
593 | 642 | |
|
594 | 643 | for opConfObj in self.opConfObjList: |
|
595 | 644 | |
|
596 | 645 | if opConfObj.type == 'self': |
|
597 | 646 | continue |
|
598 | 647 | |
|
599 | 648 | opObj = opConfObj.createObject(plotter_queue) |
|
600 | 649 | |
|
601 | 650 | self.opObjDict[opConfObj.id] = opObj |
|
602 | 651 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
603 | 652 | |
|
604 | 653 | self.procUnitObj = procUnitObj |
|
605 | 654 | |
|
606 | 655 | return procUnitObj |
|
607 | 656 | |
|
608 | 657 | def run(self): |
|
609 | 658 | |
|
610 | 659 | is_ok = False |
|
611 | 660 | |
|
612 | 661 | for opConfObj in self.opConfObjList: |
|
613 | 662 | |
|
614 | 663 | kwargs = {} |
|
615 | 664 | for parmConfObj in opConfObj.getParameterObjList(): |
|
616 | 665 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
617 | 666 | continue |
|
618 | 667 | |
|
619 | 668 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
620 | 669 | |
|
621 | 670 | #ini = time.time() |
|
622 | 671 | |
|
623 | 672 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
624 | 673 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
625 | 674 | opName = opConfObj.name, |
|
626 |
opId = opConfObj.id |
|
|
627 | **kwargs) | |
|
675 | opId = opConfObj.id) | |
|
628 | 676 | |
|
629 | 677 | # total_time = time.time() - ini |
|
630 | 678 | # |
|
631 | 679 | # if total_time > 0.002: |
|
632 | 680 | # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time) |
|
633 | 681 | |
|
634 | 682 | is_ok = is_ok or sts |
|
635 | 683 | |
|
636 | 684 | return is_ok |
|
637 | 685 | |
|
638 | 686 | def close(self): |
|
639 | 687 | |
|
640 | 688 | for opConfObj in self.opConfObjList: |
|
641 | 689 | if opConfObj.type == 'self': |
|
642 | 690 | continue |
|
643 | 691 | |
|
644 | 692 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
645 | 693 | opObj.close() |
|
646 | 694 | |
|
647 | 695 | self.procUnitObj.close() |
|
648 | 696 | |
|
649 | 697 | return |
|
650 | 698 | |
|
651 | 699 | class ReadUnitConf(ProcUnitConf): |
|
652 | 700 | |
|
653 | 701 | path = None |
|
654 | 702 | startDate = None |
|
655 | 703 | endDate = None |
|
656 | 704 | startTime = None |
|
657 | 705 | endTime = None |
|
658 | 706 | |
|
659 | 707 | ELEMENTNAME = 'ReadUnit' |
|
660 | 708 | |
|
661 | 709 | def __init__(self): |
|
662 | 710 | |
|
663 | 711 | self.id = None |
|
664 | 712 | self.datatype = None |
|
665 | 713 | self.name = None |
|
666 | 714 | self.inputId = None |
|
667 | 715 | |
|
668 | 716 | self.parentId = None |
|
669 | 717 | |
|
670 | 718 | self.opConfObjList = [] |
|
671 | 719 | self.opObjList = [] |
|
672 | 720 | |
|
673 | 721 | def getElementName(self): |
|
674 | 722 | |
|
675 | 723 | return self.ELEMENTNAME |
|
676 | ||
|
677 | def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs): | |
|
678 | 724 | |
|
725 | def setup(self, id, name, datatype, path='', startDate="", endDate="", startTime="", | |
|
726 | endTime="", parentId=None, queue=None, server=None, **kwargs): | |
|
679 | 727 | #Compatible with old signal chain version |
|
680 | 728 | if datatype==None and name==None: |
|
681 | 729 | raise ValueError, "datatype or name should be defined" |
|
682 | 730 | |
|
683 | 731 | if name==None: |
|
684 | 732 | if 'Reader' in datatype: |
|
685 | 733 | name = datatype |
|
686 | 734 | else: |
|
687 | 735 | name = '%sReader' %(datatype) |
|
688 | 736 | |
|
689 | 737 | if datatype==None: |
|
690 | 738 | datatype = name.replace('Reader','') |
|
691 | 739 | |
|
692 | 740 | self.id = id |
|
693 | 741 | self.name = name |
|
694 | 742 | self.datatype = datatype |
|
695 | 743 | |
|
696 | 744 | self.path = os.path.abspath(path) |
|
697 | 745 | self.startDate = startDate |
|
698 | 746 | self.endDate = endDate |
|
699 | 747 | self.startTime = startTime |
|
700 | 748 | self.endTime = endTime |
|
701 | 749 | |
|
702 | 750 | self.inputId = '0' |
|
703 | 751 | self.parentId = parentId |
|
704 | 752 | |
|
705 | 753 | self.addRunOperation(**kwargs) |
|
706 | 754 | |
|
707 | 755 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
708 | 756 | |
|
709 | 757 | #Compatible with old signal chain version |
|
710 | 758 | if datatype==None and name==None: |
|
711 | 759 | raise ValueError, "datatype or name should be defined" |
|
712 | 760 | |
|
713 | 761 | if name==None: |
|
714 | 762 | if 'Reader' in datatype: |
|
715 | 763 | name = datatype |
|
716 | 764 | else: |
|
717 | 765 | name = '%sReader' %(datatype) |
|
718 | 766 | |
|
719 | 767 | if datatype==None: |
|
720 | 768 | datatype = name.replace('Reader','') |
|
721 | 769 | |
|
722 | 770 | self.datatype = datatype |
|
723 | 771 | self.name = name |
|
724 | 772 | self.path = path |
|
725 | 773 | self.startDate = startDate |
|
726 | 774 | self.endDate = endDate |
|
727 | 775 | self.startTime = startTime |
|
728 | 776 | self.endTime = endTime |
|
729 | 777 | |
|
730 | 778 | self.inputId = '0' |
|
731 | 779 | self.parentId = parentId |
|
732 | 780 | |
|
733 | 781 | self.updateRunOperation(**kwargs) |
|
734 | 782 | |
|
735 | 783 | def removeOperations(self): |
|
736 | 784 | |
|
737 | 785 | for obj in self.opConfObjList: |
|
738 | 786 | del obj |
|
739 | 787 | |
|
740 | 788 | self.opConfObjList = [] |
|
741 | 789 | |
|
742 | 790 | def addRunOperation(self, **kwargs): |
|
743 | 791 | |
|
744 | 792 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
745 | 793 | |
|
746 | 794 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
747 | 795 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
748 | 796 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
749 | 797 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
750 | 798 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
751 | 799 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
752 | 800 | |
|
753 | 801 | for key, value in kwargs.items(): |
|
754 | 802 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
755 | 803 | |
|
756 | 804 | return opObj |
|
757 | 805 | |
|
758 | 806 | def updateRunOperation(self, **kwargs): |
|
759 | 807 | |
|
760 | 808 | opObj = self.getOperationObj(name = 'run') |
|
761 | 809 | opObj.removeParameters() |
|
762 | 810 | |
|
763 | 811 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
764 | 812 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
765 | 813 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
766 | 814 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
767 | 815 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
768 | 816 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
769 | 817 | |
|
770 | 818 | for key, value in kwargs.items(): |
|
771 | 819 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
772 | 820 | |
|
773 | 821 | return opObj |
|
774 | 822 | |
|
775 | 823 | # def makeXml(self, projectElement): |
|
776 | 824 | # |
|
777 | 825 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
778 | 826 | # procUnitElement.set('id', str(self.id)) |
|
779 | 827 | # procUnitElement.set('name', self.name) |
|
780 | 828 | # procUnitElement.set('datatype', self.datatype) |
|
781 | 829 | # procUnitElement.set('inputId', str(self.inputId)) |
|
782 | 830 | # |
|
783 | 831 | # for opConfObj in self.opConfObjList: |
|
784 | 832 | # opConfObj.makeXml(procUnitElement) |
|
785 | 833 | |
|
786 | 834 | def readXml(self, upElement): |
|
787 | 835 | |
|
788 | 836 | self.id = upElement.get('id') |
|
789 | 837 | self.name = upElement.get('name') |
|
790 | 838 | self.datatype = upElement.get('datatype') |
|
791 | 839 | self.inputId = upElement.get('inputId') |
|
792 | 840 | |
|
793 | 841 | if self.ELEMENTNAME == "ReadUnit": |
|
794 | 842 | self.datatype = self.datatype.replace("Reader", "") |
|
795 | 843 | |
|
796 | 844 | if self.inputId == 'None': |
|
797 | 845 | self.inputId = '0' |
|
798 | 846 | |
|
799 | 847 | self.opConfObjList = [] |
|
800 | 848 | |
|
801 | 849 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
802 | 850 | |
|
803 | 851 | for opElement in opElementList: |
|
804 | 852 | opConfObj = OperationConf() |
|
805 | 853 | opConfObj.readXml(opElement) |
|
806 | 854 | self.opConfObjList.append(opConfObj) |
|
807 | 855 | |
|
808 | 856 | if opConfObj.name == 'run': |
|
809 | 857 | self.path = opConfObj.getParameterValue('path') |
|
810 | 858 | self.startDate = opConfObj.getParameterValue('startDate') |
|
811 | 859 | self.endDate = opConfObj.getParameterValue('endDate') |
|
812 | 860 | self.startTime = opConfObj.getParameterValue('startTime') |
|
813 | 861 | self.endTime = opConfObj.getParameterValue('endTime') |
|
814 | ||
|
815 | class Project(): | |
|
816 | ||
|
862 | ||
|
863 | class Project(Process): | |
|
817 | 864 | id = None |
|
818 | 865 | name = None |
|
819 | 866 | description = None |
|
820 | 867 | filename = None |
|
821 | 868 | |
|
822 | 869 | procUnitConfObjDict = None |
|
823 | 870 | |
|
824 | 871 | ELEMENTNAME = 'Project' |
|
825 | 872 | |
|
826 | 873 | plotterQueue = None |
|
827 | ||
|
828 | def __init__(self, plotter_queue=None): | |
|
829 | ||
|
874 | ||
|
875 | def __init__(self, plotter_queue=None, logfile=None): | |
|
876 | Process.__init__(self) | |
|
830 | 877 | self.id = None |
|
831 | 878 | self.name = None |
|
832 | 879 | self.description = None |
|
833 | ||
|
880 | if logfile is not None: | |
|
881 | logToFile(logfile) | |
|
834 | 882 | self.plotterQueue = plotter_queue |
|
835 | 883 | |
|
836 | 884 | self.procUnitConfObjDict = {} |
|
837 | 885 | |
|
838 | 886 | def __getNewId(self): |
|
839 | 887 | |
|
840 | 888 | idList = self.procUnitConfObjDict.keys() |
|
841 | 889 | |
|
842 | 890 | id = int(self.id)*10 |
|
843 | 891 | |
|
844 | 892 | while True: |
|
845 | 893 | id += 1 |
|
846 | 894 | |
|
847 | 895 | if str(id) in idList: |
|
848 | 896 | continue |
|
849 | 897 | |
|
850 | 898 | break |
|
851 | 899 | |
|
852 | 900 | return str(id) |
|
853 | 901 | |
|
854 | 902 | def getElementName(self): |
|
855 | 903 | |
|
856 | 904 | return self.ELEMENTNAME |
|
857 | 905 | |
|
858 | 906 | def getId(self): |
|
859 | 907 | |
|
860 | 908 | return self.id |
|
861 | 909 | |
|
862 | 910 | def updateId(self, new_id): |
|
863 | 911 | |
|
864 | 912 | self.id = str(new_id) |
|
865 | 913 | |
|
866 | 914 | keyList = self.procUnitConfObjDict.keys() |
|
867 | 915 | keyList.sort() |
|
868 | 916 | |
|
869 | 917 | n = 1 |
|
870 | 918 | newProcUnitConfObjDict = {} |
|
871 | 919 | |
|
872 | 920 | for procKey in keyList: |
|
873 | 921 | |
|
874 | 922 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
875 | 923 | idProcUnit = str(int(self.id)*10 + n) |
|
876 | 924 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
877 | 925 | |
|
878 | 926 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
879 | 927 | n += 1 |
|
880 | 928 | |
|
881 | 929 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
882 | 930 | |
|
883 | 931 | def setup(self, id, name, description): |
|
884 | 932 | |
|
885 | 933 | self.id = str(id) |
|
886 | 934 | self.name = name |
|
887 | 935 | self.description = description |
|
888 | 936 | |
|
889 | 937 | def update(self, name, description): |
|
890 | 938 | |
|
891 | 939 | self.name = name |
|
892 | 940 | self.description = description |
|
893 | 941 | |
|
894 | 942 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
895 | ||
|
896 | 943 | if id is None: |
|
897 | 944 | idReadUnit = self.__getNewId() |
|
898 | 945 | else: |
|
899 | 946 | idReadUnit = str(id) |
|
900 | 947 | |
|
901 | 948 | readUnitConfObj = ReadUnitConf() |
|
902 | 949 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
903 | 950 | |
|
904 | 951 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
905 | 952 | |
|
906 | 953 | return readUnitConfObj |
|
907 | 954 | |
|
908 | 955 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
909 | 956 | |
|
910 | 957 | idProcUnit = self.__getNewId() |
|
911 | 958 | |
|
912 | 959 | procUnitConfObj = ProcUnitConf() |
|
913 | 960 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
914 | 961 | |
|
915 | 962 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
916 | 963 | |
|
917 | 964 | return procUnitConfObj |
|
918 | 965 | |
|
919 | 966 | def removeProcUnit(self, id): |
|
920 | 967 | |
|
921 | 968 | if id in self.procUnitConfObjDict.keys(): |
|
922 | 969 | self.procUnitConfObjDict.pop(id) |
|
923 | 970 | |
|
924 | 971 | def getReadUnitId(self): |
|
925 | 972 | |
|
926 | 973 | readUnitConfObj = self.getReadUnitObj() |
|
927 | 974 | |
|
928 | 975 | return readUnitConfObj.id |
|
929 | 976 | |
|
930 | 977 | def getReadUnitObj(self): |
|
931 | 978 | |
|
932 | 979 | for obj in self.procUnitConfObjDict.values(): |
|
933 | 980 | if obj.getElementName() == "ReadUnit": |
|
934 | 981 | return obj |
|
935 | 982 | |
|
936 | 983 | return None |
|
937 | 984 | |
|
938 | 985 | def getProcUnitObj(self, id=None, name=None): |
|
939 | 986 | |
|
940 | 987 | if id != None: |
|
941 | 988 | return self.procUnitConfObjDict[id] |
|
942 | 989 | |
|
943 | 990 | if name != None: |
|
944 | 991 | return self.getProcUnitObjByName(name) |
|
945 | 992 | |
|
946 | 993 | return None |
|
947 | 994 | |
|
948 | 995 | def getProcUnitObjByName(self, name): |
|
949 | 996 | |
|
950 | 997 | for obj in self.procUnitConfObjDict.values(): |
|
951 | 998 | if obj.name == name: |
|
952 | 999 | return obj |
|
953 | 1000 | |
|
954 | 1001 | return None |
|
955 | 1002 | |
|
956 | 1003 | def procUnitItems(self): |
|
957 | 1004 | |
|
958 | 1005 | return self.procUnitConfObjDict.items() |
|
959 | 1006 | |
|
960 | 1007 | def makeXml(self): |
|
961 | 1008 | |
|
962 | 1009 | projectElement = Element('Project') |
|
963 | 1010 | projectElement.set('id', str(self.id)) |
|
964 | 1011 | projectElement.set('name', self.name) |
|
965 | 1012 | projectElement.set('description', self.description) |
|
966 | 1013 | |
|
967 | 1014 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
968 | 1015 | procUnitConfObj.makeXml(projectElement) |
|
969 | 1016 | |
|
970 | 1017 | self.projectElement = projectElement |
|
971 | 1018 | |
|
972 | 1019 | def writeXml(self, filename=None): |
|
973 | 1020 | |
|
974 | 1021 | if filename == None: |
|
975 | 1022 | if self.filename: |
|
976 | 1023 | filename = self.filename |
|
977 | 1024 | else: |
|
978 | 1025 | filename = "schain.xml" |
|
979 | 1026 | |
|
980 | 1027 | if not filename: |
|
981 | 1028 | print "filename has not been defined. Use setFilename(filename) for do it." |
|
982 | 1029 | return 0 |
|
983 | 1030 | |
|
984 | 1031 | abs_file = os.path.abspath(filename) |
|
985 | 1032 | |
|
986 | 1033 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
987 | 1034 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
988 | 1035 | return 0 |
|
989 | 1036 | |
|
990 | 1037 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
991 | 1038 | print "File %s already exists and it could not be overwriten" %abs_file |
|
992 | 1039 | return 0 |
|
993 | 1040 | |
|
994 | 1041 | self.makeXml() |
|
995 | 1042 | |
|
996 | 1043 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
997 | 1044 | |
|
998 | 1045 | self.filename = abs_file |
|
999 | 1046 | |
|
1000 | 1047 | return 1 |
|
1001 | 1048 | |
|
1002 | 1049 | def readXml(self, filename = None): |
|
1003 | 1050 | |
|
1004 | 1051 | if not filename: |
|
1005 | 1052 | print "filename is not defined" |
|
1006 | 1053 | return 0 |
|
1007 | 1054 | |
|
1008 | 1055 | abs_file = os.path.abspath(filename) |
|
1009 | 1056 | |
|
1010 | 1057 | if not os.path.isfile(abs_file): |
|
1011 | 1058 | print "%s file does not exist" %abs_file |
|
1012 | 1059 | return 0 |
|
1013 | 1060 | |
|
1014 | 1061 | self.projectElement = None |
|
1015 | 1062 | self.procUnitConfObjDict = {} |
|
1016 | 1063 | |
|
1017 | 1064 | try: |
|
1018 | 1065 | self.projectElement = ElementTree().parse(abs_file) |
|
1019 | 1066 | except: |
|
1020 | 1067 | print "Error reading %s, verify file format" %filename |
|
1021 | 1068 | return 0 |
|
1022 | 1069 | |
|
1023 | 1070 | self.project = self.projectElement.tag |
|
1024 | 1071 | |
|
1025 | 1072 | self.id = self.projectElement.get('id') |
|
1026 | 1073 | self.name = self.projectElement.get('name') |
|
1027 | 1074 | self.description = self.projectElement.get('description') |
|
1028 | 1075 | |
|
1029 | 1076 | readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName()) |
|
1030 | 1077 | |
|
1031 | 1078 | for readUnitElement in readUnitElementList: |
|
1032 | 1079 | readUnitConfObj = ReadUnitConf() |
|
1033 | 1080 | readUnitConfObj.readXml(readUnitElement) |
|
1034 | 1081 | |
|
1035 | 1082 | if readUnitConfObj.parentId == None: |
|
1036 | 1083 | readUnitConfObj.parentId = self.id |
|
1037 | 1084 | |
|
1038 | 1085 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1039 | 1086 | |
|
1040 | 1087 | procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName()) |
|
1041 | 1088 | |
|
1042 | 1089 | for procUnitElement in procUnitElementList: |
|
1043 | 1090 | procUnitConfObj = ProcUnitConf() |
|
1044 | 1091 | procUnitConfObj.readXml(procUnitElement) |
|
1045 | 1092 | |
|
1046 | 1093 | if procUnitConfObj.parentId == None: |
|
1047 | 1094 | procUnitConfObj.parentId = self.id |
|
1048 | 1095 | |
|
1049 | 1096 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1050 | 1097 | |
|
1051 | 1098 | self.filename = abs_file |
|
1052 | 1099 | |
|
1053 | 1100 | return 1 |
|
1054 | 1101 | |
|
1055 | 1102 | def printattr(self): |
|
1056 | 1103 | |
|
1057 | 1104 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1058 | self.name, | |
|
1059 | self.description) | |
|
1060 | ||
|
1105 | self.name, | |
|
1106 | self.description) | |
|
1107 | ||
|
1061 | 1108 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1062 | 1109 | procUnitConfObj.printattr() |
|
1063 | 1110 | |
|
1064 | 1111 | def createObjects(self): |
|
1065 | 1112 | |
|
1066 | 1113 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1067 | 1114 | procUnitConfObj.createObjects(self.plotterQueue) |
|
1068 | 1115 | |
|
1069 | 1116 | def __connect(self, objIN, thisObj): |
|
1070 | 1117 | |
|
1071 | 1118 | thisObj.setInput(objIN.getOutputObj()) |
|
1072 | 1119 | |
|
1073 | 1120 | def connectObjects(self): |
|
1074 | 1121 | |
|
1075 | 1122 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1076 | 1123 | |
|
1077 | 1124 | inputId = thisPUConfObj.getInputId() |
|
1078 | 1125 | |
|
1079 | 1126 | if int(inputId) == 0: |
|
1080 | 1127 | continue |
|
1081 | 1128 | |
|
1082 | 1129 | #Get input object |
|
1083 | 1130 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1084 | 1131 | puObjIN = puConfINObj.getProcUnitObj() |
|
1085 | 1132 | |
|
1086 | 1133 | #Get current object |
|
1087 | 1134 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1088 | 1135 | |
|
1089 | 1136 | self.__connect(puObjIN, thisPUObj) |
|
1090 | 1137 | |
|
1091 | 1138 | def __handleError(self, procUnitConfObj, send_email=True): |
|
1092 | 1139 | |
|
1093 | 1140 | import socket |
|
1094 | 1141 | |
|
1095 | 1142 | err = traceback.format_exception(sys.exc_info()[0], |
|
1096 | 1143 | sys.exc_info()[1], |
|
1097 | 1144 | sys.exc_info()[2]) |
|
1098 | 1145 | |
|
1099 | 1146 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1100 | 1147 | print "***** %s" %err[-1] |
|
1101 | 1148 | |
|
1102 | 1149 | message = "".join(err) |
|
1103 | 1150 | |
|
1104 | 1151 | sys.stderr.write(message) |
|
1105 | 1152 | |
|
1106 | 1153 | if not send_email: |
|
1107 | 1154 | return |
|
1108 | 1155 | |
|
1109 | 1156 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1110 | 1157 | |
|
1111 | 1158 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1112 | 1159 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1113 | 1160 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1114 | 1161 | subtitle += "Configuration file: %s\n" %self.filename |
|
1115 | 1162 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1116 | 1163 | |
|
1117 | 1164 | readUnitConfObj = self.getReadUnitObj() |
|
1118 | 1165 | if readUnitConfObj: |
|
1119 | 1166 | subtitle += "\nInput parameters:\n" |
|
1120 | 1167 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1121 | 1168 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1122 | 1169 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1123 | 1170 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1124 | 1171 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1125 | 1172 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1126 | 1173 | |
|
1127 | 1174 | adminObj = schainpy.admin.SchainNotify() |
|
1128 | 1175 | adminObj.sendAlert(message=message, |
|
1129 | 1176 | subject=subject, |
|
1130 | 1177 | subtitle=subtitle, |
|
1131 | 1178 | filename=self.filename) |
|
1132 | 1179 | |
|
1133 | 1180 | def isPaused(self): |
|
1134 | 1181 | return 0 |
|
1135 | 1182 | |
|
1136 | 1183 | def isStopped(self): |
|
1137 | 1184 | return 0 |
|
1138 | 1185 | |
|
1139 | 1186 | def runController(self): |
|
1140 | 1187 | """ |
|
1141 | 1188 | returns 0 when this process has been stopped, 1 otherwise |
|
1142 | 1189 | """ |
|
1143 | 1190 | |
|
1144 | 1191 | if self.isPaused(): |
|
1145 | 1192 | print "Process suspended" |
|
1146 | 1193 | |
|
1147 | 1194 | while True: |
|
1148 | 1195 | sleep(0.1) |
|
1149 | 1196 | |
|
1150 | 1197 | if not self.isPaused(): |
|
1151 | 1198 | break |
|
1152 | 1199 | |
|
1153 | 1200 | if self.isStopped(): |
|
1154 | 1201 | break |
|
1155 | 1202 | |
|
1156 | 1203 | print "Process reinitialized" |
|
1157 | 1204 | |
|
1158 | 1205 | if self.isStopped(): |
|
1159 | 1206 | print "Process stopped" |
|
1160 | 1207 | return 0 |
|
1161 | 1208 | |
|
1162 | 1209 | return 1 |
|
1163 | 1210 | |
|
1164 | 1211 | def setFilename(self, filename): |
|
1165 | 1212 | |
|
1166 | 1213 | self.filename = filename |
|
1167 | 1214 | |
|
1168 | 1215 | def setPlotterQueue(self, plotter_queue): |
|
1169 | 1216 | |
|
1170 | 1217 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1171 | 1218 | |
|
1172 | 1219 | def getPlotterQueue(self): |
|
1173 | 1220 | |
|
1174 | 1221 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1175 | 1222 | |
|
1176 | 1223 | def useExternalPlotter(self): |
|
1177 | 1224 | |
|
1178 | 1225 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1179 | ||
|
1180 | def run(self): | |
|
1226 | ||
|
1227 | ||
|
1228 | def run(self, filename=None): | |
|
1181 | 1229 | |
|
1230 | # self.writeXml(filename) | |
|
1231 | self.createObjects() | |
|
1232 | self.connectObjects() | |
|
1233 | ||
|
1182 | 1234 | |
|
1183 | 1235 | print "*"*60 |
|
1184 | 1236 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1185 | 1237 | print "*"*60 |
|
1186 | 1238 | |
|
1187 | 1239 | |
|
1188 | 1240 | keyList = self.procUnitConfObjDict.keys() |
|
1189 | 1241 | keyList.sort() |
|
1190 | 1242 | |
|
1191 | 1243 | while(True): |
|
1192 | 1244 | |
|
1193 | 1245 | is_ok = False |
|
1194 | 1246 | |
|
1195 | 1247 | for procKey in keyList: |
|
1196 | 1248 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1197 | 1249 | |
|
1198 | 1250 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1199 | 1251 | |
|
1200 | 1252 | try: |
|
1201 | 1253 | sts = procUnitConfObj.run() |
|
1202 | 1254 | is_ok = is_ok or sts |
|
1203 | 1255 | except KeyboardInterrupt: |
|
1204 | 1256 | is_ok = False |
|
1205 | 1257 | break |
|
1206 | 1258 | except ValueError, e: |
|
1207 | 1259 | sleep(0.5) |
|
1208 | 1260 | self.__handleError(procUnitConfObj, send_email=True) |
|
1209 | 1261 | is_ok = False |
|
1210 | 1262 | break |
|
1211 | 1263 | except: |
|
1212 | 1264 | sleep(0.5) |
|
1213 | 1265 | self.__handleError(procUnitConfObj) |
|
1214 | 1266 | is_ok = False |
|
1215 | 1267 | break |
|
1216 | 1268 | |
|
1217 | 1269 | #If every process unit finished so end process |
|
1218 | 1270 | if not(is_ok): |
|
1219 | 1271 | # print "Every process unit have finished" |
|
1220 | 1272 | break |
|
1221 | 1273 | |
|
1222 | 1274 | if not self.runController(): |
|
1223 | 1275 | break |
|
1224 | 1276 | |
|
1225 | 1277 | #Closing every process |
|
1226 | 1278 | for procKey in keyList: |
|
1227 | 1279 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1228 | 1280 | procUnitConfObj.close() |
|
1229 | ||
|
1230 | print "Process finished" | |
|
1231 | ||
|
1232 | def start(self): | |
|
1233 | ||
|
1234 | self.writeXml() | |
|
1235 | ||
|
1236 | self.createObjects() | |
|
1237 | self.connectObjects() | |
|
1238 | self.run() | |
|
1239 | ||
|
1240 | if __name__ == '__main__': | |
|
1241 | ||
|
1242 | desc = "Segundo Test" | |
|
1243 | filename = "schain.xml" | |
|
1244 | ||
|
1245 | controllerObj = Project() | |
|
1246 | ||
|
1247 | controllerObj.setup(id = '191', name='test01', description=desc) | |
|
1248 | ||
|
1249 | readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage', | |
|
1250 | path='data/rawdata/', | |
|
1251 | startDate='2011/01/01', | |
|
1252 | endDate='2012/12/31', | |
|
1253 | startTime='00:00:00', | |
|
1254 | endTime='23:59:59', | |
|
1255 | online=1, | |
|
1256 | walk=1) | |
|
1257 | ||
|
1258 | procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId()) | |
|
1259 | ||
|
1260 | opObj10 = procUnitConfObj0.addOperation(name='selectChannels') | |
|
1261 | opObj10.addParameter(name='channelList', value='3,4,5', format='intlist') | |
|
1262 | ||
|
1263 | opObj10 = procUnitConfObj0.addOperation(name='selectHeights') | |
|
1264 | opObj10.addParameter(name='minHei', value='90', format='float') | |
|
1265 | opObj10.addParameter(name='maxHei', value='180', format='float') | |
|
1266 | ||
|
1267 | opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external') | |
|
1268 | opObj12.addParameter(name='n', value='10', format='int') | |
|
1269 | ||
|
1270 | procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId()) | |
|
1271 | procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int') | |
|
1272 | # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='') | |
|
1273 | ||
|
1274 | ||
|
1275 | opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') | |
|
1276 | opObj11.addParameter(name='idfigure', value='1', format='int') | |
|
1277 | opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str') | |
|
1278 | opObj11.addParameter(name='zmin', value='40', format='int') | |
|
1279 | opObj11.addParameter(name='zmax', value='90', format='int') | |
|
1280 | opObj11.addParameter(name='showprofile', value='1', format='int') | |
|
1281 | ||
|
1282 | print "Escribiendo el archivo XML" | |
|
1283 | ||
|
1284 | controllerObj.writeXml(filename) | |
|
1285 | ||
|
1286 | print "Leyendo el archivo XML" | |
|
1287 | controllerObj.readXml(filename) | |
|
1288 | #controllerObj.printattr() | |
|
1289 | ||
|
1290 | controllerObj.createObjects() | |
|
1291 | controllerObj.connectObjects() | |
|
1292 | controllerObj.run() | |
|
1293 | ||
|
1294 | 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 | 8 | from data import * |
|
9 | 9 | from io import * |
|
10 | 10 | from proc import * |
|
11 | 11 | from graphics import * |
|
12 |
from utils import * |
|
|
12 | from utils import * No newline at end of file |
@@ -1,876 +1,876 | |||
|
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 | 11 | |
|
12 | 12 | SPEED_OF_LIGHT = 299792458 |
|
13 | 13 | SPEED_OF_LIGHT = 3e8 |
|
14 | 14 | |
|
15 | 15 | BASIC_STRUCTURE = numpy.dtype([ |
|
16 | 16 | ('nSize','<u4'), |
|
17 | 17 | ('nVersion','<u2'), |
|
18 | 18 | ('nDataBlockId','<u4'), |
|
19 | 19 | ('nUtime','<u4'), |
|
20 | 20 | ('nMilsec','<u2'), |
|
21 | 21 | ('nTimezone','<i2'), |
|
22 | 22 | ('nDstflag','<i2'), |
|
23 | 23 | ('nErrorCount','<u4') |
|
24 | 24 | ]) |
|
25 | 25 | |
|
26 | 26 | SYSTEM_STRUCTURE = numpy.dtype([ |
|
27 | 27 | ('nSize','<u4'), |
|
28 | 28 | ('nNumSamples','<u4'), |
|
29 | 29 | ('nNumProfiles','<u4'), |
|
30 | 30 | ('nNumChannels','<u4'), |
|
31 | 31 | ('nADCResolution','<u4'), |
|
32 | 32 | ('nPCDIOBusWidth','<u4'), |
|
33 | 33 | ]) |
|
34 | 34 | |
|
35 | 35 | RADAR_STRUCTURE = numpy.dtype([ |
|
36 | 36 | ('nSize','<u4'), |
|
37 | 37 | ('nExpType','<u4'), |
|
38 | 38 | ('nNTx','<u4'), |
|
39 | 39 | ('fIpp','<f4'), |
|
40 | 40 | ('fTxA','<f4'), |
|
41 | 41 | ('fTxB','<f4'), |
|
42 | 42 | ('nNumWindows','<u4'), |
|
43 | 43 | ('nNumTaus','<u4'), |
|
44 | 44 | ('nCodeType','<u4'), |
|
45 | 45 | ('nLine6Function','<u4'), |
|
46 | 46 | ('nLine5Function','<u4'), |
|
47 | 47 | ('fClock','<f4'), |
|
48 | 48 | ('nPrePulseBefore','<u4'), |
|
49 | 49 | ('nPrePulseAfter','<u4'), |
|
50 | 50 | ('sRangeIPP','<a20'), |
|
51 | 51 | ('sRangeTxA','<a20'), |
|
52 | 52 | ('sRangeTxB','<a20'), |
|
53 | 53 | ]) |
|
54 | 54 | |
|
55 | 55 | SAMPLING_STRUCTURE = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')]) |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | PROCESSING_STRUCTURE = numpy.dtype([ |
|
59 | 59 | ('nSize','<u4'), |
|
60 | 60 | ('nDataType','<u4'), |
|
61 | 61 | ('nSizeOfDataBlock','<u4'), |
|
62 | 62 | ('nProfilesperBlock','<u4'), |
|
63 | 63 | ('nDataBlocksperFile','<u4'), |
|
64 | 64 | ('nNumWindows','<u4'), |
|
65 | 65 | ('nProcessFlags','<u4'), |
|
66 | 66 | ('nCoherentIntegrations','<u4'), |
|
67 | 67 | ('nIncoherentIntegrations','<u4'), |
|
68 | 68 | ('nTotalSpectra','<u4') |
|
69 | 69 | ]) |
|
70 | 70 | |
|
71 | 71 | class Header(object): |
|
72 | 72 | |
|
73 | 73 | def __init__(self): |
|
74 | 74 | raise NotImplementedError |
|
75 | 75 | |
|
76 | 76 | def copy(self): |
|
77 | 77 | return copy.deepcopy(self) |
|
78 | 78 | |
|
79 | 79 | def read(self): |
|
80 | 80 | |
|
81 | 81 | raise NotImplementedError |
|
82 | 82 | |
|
83 | 83 | def write(self): |
|
84 | 84 | |
|
85 | 85 | raise NotImplementedError |
|
86 | 86 | |
|
87 | 87 | def getAllowedArgs(self): |
|
88 | 88 | args = inspect.getargspec(self.__init__).args |
|
89 | 89 | try: |
|
90 | 90 | args.remove('self') |
|
91 | 91 | except: |
|
92 | 92 | pass |
|
93 | 93 | return args |
|
94 | 94 | |
|
95 | 95 | def getAsDict(self): |
|
96 | 96 | args = self.getAllowedArgs() |
|
97 | 97 | asDict = {} |
|
98 | 98 | for x in args: |
|
99 | 99 | asDict[x] = self[x] |
|
100 | 100 | return asDict |
|
101 | 101 | |
|
102 | 102 | def __getitem__(self, name): |
|
103 | 103 | return getattr(self, name) |
|
104 | 104 | |
|
105 | 105 | def printInfo(self): |
|
106 | 106 | |
|
107 | 107 | message = "#"*50 + "\n" |
|
108 | 108 | message += self.__class__.__name__.upper() + "\n" |
|
109 | 109 | message += "#"*50 + "\n" |
|
110 | 110 | |
|
111 | 111 | keyList = self.__dict__.keys() |
|
112 | 112 | keyList.sort() |
|
113 | 113 | |
|
114 | 114 | for key in keyList: |
|
115 | 115 | message += "%s = %s" %(key, self.__dict__[key]) + "\n" |
|
116 | 116 | |
|
117 | 117 | if "size" not in keyList: |
|
118 | 118 | attr = getattr(self, "size") |
|
119 | 119 | |
|
120 | 120 | if attr: |
|
121 | 121 | message += "%s = %s" %("size", attr) + "\n" |
|
122 | 122 | |
|
123 | 123 | print message |
|
124 | 124 | |
|
125 | 125 | class BasicHeader(Header): |
|
126 | 126 | |
|
127 | 127 | size = None |
|
128 | 128 | version = None |
|
129 | 129 | dataBlock = None |
|
130 | 130 | utc = None |
|
131 | 131 | ltc = None |
|
132 | 132 | miliSecond = None |
|
133 | 133 | timeZone = None |
|
134 | 134 | dstFlag = None |
|
135 | 135 | errorCount = None |
|
136 | 136 | datatime = None |
|
137 | 137 | structure = BASIC_STRUCTURE |
|
138 | 138 | __LOCALTIME = None |
|
139 | 139 | |
|
140 | 140 | def __init__(self, useLocalTime=True): |
|
141 | 141 | |
|
142 | 142 | self.size = 24 |
|
143 | 143 | self.version = 0 |
|
144 | 144 | self.dataBlock = 0 |
|
145 | 145 | self.utc = 0 |
|
146 | 146 | self.miliSecond = 0 |
|
147 | 147 | self.timeZone = 0 |
|
148 | 148 | self.dstFlag = 0 |
|
149 | 149 | self.errorCount = 0 |
|
150 | 150 | |
|
151 | 151 | self.useLocalTime = useLocalTime |
|
152 | 152 | |
|
153 | 153 | def read(self, fp): |
|
154 | 154 | |
|
155 | 155 | self.length = 0 |
|
156 | 156 | try: |
|
157 | 157 | if hasattr(fp, 'read'): |
|
158 | 158 | header = numpy.fromfile(fp, BASIC_STRUCTURE,1) |
|
159 | 159 | else: |
|
160 | 160 | header = numpy.fromstring(fp, BASIC_STRUCTURE,1) |
|
161 | 161 | except Exception, e: |
|
162 | 162 | print "BasicHeader: " |
|
163 | 163 | print e |
|
164 | 164 | return 0 |
|
165 | 165 | |
|
166 | 166 | self.size = int(header['nSize'][0]) |
|
167 | 167 | self.version = int(header['nVersion'][0]) |
|
168 | 168 | self.dataBlock = int(header['nDataBlockId'][0]) |
|
169 | 169 | self.utc = int(header['nUtime'][0]) |
|
170 | 170 | self.miliSecond = int(header['nMilsec'][0]) |
|
171 | 171 | self.timeZone = int(header['nTimezone'][0]) |
|
172 | 172 | self.dstFlag = int(header['nDstflag'][0]) |
|
173 | 173 | self.errorCount = int(header['nErrorCount'][0]) |
|
174 | 174 | |
|
175 | 175 | if self.size < 24: |
|
176 | 176 | return 0 |
|
177 | 177 | |
|
178 | 178 | self.length = header.nbytes |
|
179 | 179 | return 1 |
|
180 | 180 | |
|
181 | 181 | def write(self, fp): |
|
182 | 182 | |
|
183 | 183 | headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount) |
|
184 | 184 | header = numpy.array(headerTuple, BASIC_STRUCTURE) |
|
185 | 185 | header.tofile(fp) |
|
186 | 186 | |
|
187 | 187 | return 1 |
|
188 | 188 | |
|
189 | 189 | def get_ltc(self): |
|
190 | 190 | |
|
191 | 191 | return self.utc - self.timeZone*60 |
|
192 | 192 | |
|
193 | 193 | def set_ltc(self, value): |
|
194 | 194 | |
|
195 | 195 | self.utc = value + self.timeZone*60 |
|
196 | 196 | |
|
197 | 197 | def get_datatime(self): |
|
198 | 198 | |
|
199 | 199 | return datetime.datetime.utcfromtimestamp(self.ltc) |
|
200 | 200 | |
|
201 | 201 | ltc = property(get_ltc, set_ltc) |
|
202 | 202 | datatime = property(get_datatime) |
|
203 | 203 | |
|
204 | 204 | class SystemHeader(Header): |
|
205 | 205 | |
|
206 | 206 | size = None |
|
207 | 207 | nSamples = None |
|
208 | 208 | nProfiles = None |
|
209 | 209 | nChannels = None |
|
210 | 210 | adcResolution = None |
|
211 | 211 | pciDioBusWidth = None |
|
212 | 212 | structure = SYSTEM_STRUCTURE |
|
213 | 213 | |
|
214 | 214 | def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0): |
|
215 | 215 | |
|
216 | 216 | self.size = 24 |
|
217 | 217 | self.nSamples = nSamples |
|
218 | 218 | self.nProfiles = nProfiles |
|
219 | 219 | self.nChannels = nChannels |
|
220 | 220 | self.adcResolution = adcResolution |
|
221 | 221 | self.pciDioBusWidth = pciDioBusWidth |
|
222 | 222 | |
|
223 | 223 | def read(self, fp): |
|
224 | 224 | self.length = 0 |
|
225 | 225 | try: |
|
226 | 226 | startFp = fp.tell() |
|
227 | 227 | except Exception, e: |
|
228 | 228 | startFp = None |
|
229 | 229 | pass |
|
230 | 230 | |
|
231 | 231 | try: |
|
232 | 232 | if hasattr(fp, 'read'): |
|
233 | 233 | header = numpy.fromfile(fp, SYSTEM_STRUCTURE,1) |
|
234 | 234 | else: |
|
235 | 235 | header = numpy.fromstring(fp, SYSTEM_STRUCTURE,1) |
|
236 | 236 | except Exception, e: |
|
237 | 237 | print "System Header: " + str(e) |
|
238 | 238 | return 0 |
|
239 | 239 | |
|
240 | 240 | self.size = header['nSize'][0] |
|
241 | 241 | self.nSamples = header['nNumSamples'][0] |
|
242 | 242 | self.nProfiles = header['nNumProfiles'][0] |
|
243 | 243 | self.nChannels = header['nNumChannels'][0] |
|
244 | 244 | self.adcResolution = header['nADCResolution'][0] |
|
245 | 245 | self.pciDioBusWidth = header['nPCDIOBusWidth'][0] |
|
246 | 246 | |
|
247 | 247 | |
|
248 | 248 | if startFp is not None: |
|
249 | 249 | endFp = self.size + startFp |
|
250 | 250 | |
|
251 | 251 | if fp.tell() > endFp: |
|
252 | 252 | sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp.name) |
|
253 | 253 | return 0 |
|
254 | 254 | |
|
255 | 255 | if fp.tell() < endFp: |
|
256 | 256 | sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp.name) |
|
257 | 257 | return 0 |
|
258 | 258 | |
|
259 | 259 | self.length = header.nbytes |
|
260 | 260 | return 1 |
|
261 | 261 | |
|
262 | 262 | def write(self, fp): |
|
263 | 263 | |
|
264 | 264 | headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth) |
|
265 | 265 | header = numpy.array(headerTuple,SYSTEM_STRUCTURE) |
|
266 | 266 | header.tofile(fp) |
|
267 | 267 | |
|
268 | 268 | return 1 |
|
269 | 269 | |
|
270 | 270 | class RadarControllerHeader(Header): |
|
271 | 271 | |
|
272 | 272 | expType = None |
|
273 | 273 | nTx = None |
|
274 | 274 | ipp = None |
|
275 | 275 | txA = None |
|
276 | 276 | txB = None |
|
277 | 277 | nWindows = None |
|
278 | 278 | numTaus = None |
|
279 | 279 | codeType = None |
|
280 | 280 | line6Function = None |
|
281 | 281 | line5Function = None |
|
282 | 282 | fClock = None |
|
283 | 283 | prePulseBefore = None |
|
284 | 284 | prePulseAfter = None |
|
285 | 285 | rangeIpp = None |
|
286 | 286 | rangeTxA = None |
|
287 | 287 | rangeTxB = None |
|
288 | 288 | structure = RADAR_STRUCTURE |
|
289 | 289 | __size = None |
|
290 | 290 | |
|
291 | 291 | def __init__(self, expType=2, nTx=1, |
|
292 | 292 | ipp=None, txA=0, txB=0, |
|
293 | 293 | nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None, |
|
294 | 294 | numTaus=0, line6Function=0, line5Function=0, fClock=None, |
|
295 | 295 | prePulseBefore=0, prePulseAfter=0, |
|
296 | 296 | codeType=0, nCode=0, nBaud=0, code=None, |
|
297 | 297 | flip1=0, flip2=0): |
|
298 | 298 | |
|
299 | 299 | # self.size = 116 |
|
300 | 300 | self.expType = expType |
|
301 | 301 | self.nTx = nTx |
|
302 | 302 | self.ipp = ipp |
|
303 | 303 | self.txA = txA |
|
304 | 304 | self.txB = txB |
|
305 | 305 | self.rangeIpp = ipp |
|
306 | 306 | self.rangeTxA = txA |
|
307 | 307 | self.rangeTxB = txB |
|
308 | 308 | |
|
309 | 309 | self.nWindows = nWindows |
|
310 | 310 | self.numTaus = numTaus |
|
311 | 311 | self.codeType = codeType |
|
312 | 312 | self.line6Function = line6Function |
|
313 | 313 | self.line5Function = line5Function |
|
314 | 314 | self.fClock = fClock |
|
315 | 315 | self.prePulseBefore = prePulseBefore |
|
316 | 316 | self.prePulseAfter = prePulseAfter |
|
317 | 317 | |
|
318 | 318 | self.nHeights = nHeights |
|
319 | 319 | self.firstHeight = firstHeight |
|
320 | 320 | self.deltaHeight = deltaHeight |
|
321 | 321 | self.samplesWin = nHeights |
|
322 | 322 | |
|
323 | 323 | self.nCode = nCode |
|
324 | 324 | self.nBaud = nBaud |
|
325 | 325 | self.code = code |
|
326 | 326 | self.flip1 = flip1 |
|
327 | 327 | self.flip2 = flip2 |
|
328 | 328 | |
|
329 | 329 | self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4 |
|
330 | 330 | # self.dynamic = numpy.array([],numpy.dtype('byte')) |
|
331 | 331 | |
|
332 | 332 | if self.fClock is None and self.deltaHeight is not None: |
|
333 | 333 | self.fClock = 0.15/(deltaHeight*1e-6) #0.15Km / (height * 1u) |
|
334 | 334 | |
|
335 | 335 | def read(self, fp): |
|
336 | 336 | self.length = 0 |
|
337 | 337 | try: |
|
338 | 338 | startFp = fp.tell() |
|
339 | 339 | except Exception, e: |
|
340 | 340 | startFp = None |
|
341 | 341 | pass |
|
342 | 342 | |
|
343 | 343 | try: |
|
344 | 344 | if hasattr(fp, 'read'): |
|
345 | 345 | header = numpy.fromfile(fp, RADAR_STRUCTURE,1) |
|
346 | 346 | else: |
|
347 | 347 | header = numpy.fromstring(fp, RADAR_STRUCTURE,1) |
|
348 | 348 | self.length += header.nbytes |
|
349 | 349 | except Exception, e: |
|
350 | 350 | print "RadarControllerHeader: " + str(e) |
|
351 | 351 | return 0 |
|
352 | 352 | |
|
353 | 353 | size = int(header['nSize'][0]) |
|
354 | 354 | self.expType = int(header['nExpType'][0]) |
|
355 | 355 | self.nTx = int(header['nNTx'][0]) |
|
356 | 356 | self.ipp = float(header['fIpp'][0]) |
|
357 | 357 | self.txA = float(header['fTxA'][0]) |
|
358 | 358 | self.txB = float(header['fTxB'][0]) |
|
359 | 359 | self.nWindows = int(header['nNumWindows'][0]) |
|
360 | 360 | self.numTaus = int(header['nNumTaus'][0]) |
|
361 | 361 | self.codeType = int(header['nCodeType'][0]) |
|
362 | 362 | self.line6Function = int(header['nLine6Function'][0]) |
|
363 | 363 | self.line5Function = int(header['nLine5Function'][0]) |
|
364 | 364 | self.fClock = float(header['fClock'][0]) |
|
365 | 365 | self.prePulseBefore = int(header['nPrePulseBefore'][0]) |
|
366 | 366 | self.prePulseAfter = int(header['nPrePulseAfter'][0]) |
|
367 | 367 | self.rangeIpp = header['sRangeIPP'][0] |
|
368 | 368 | self.rangeTxA = header['sRangeTxA'][0] |
|
369 | 369 | self.rangeTxB = header['sRangeTxB'][0] |
|
370 | 370 | |
|
371 | 371 | try: |
|
372 | 372 | if hasattr(fp, 'read'): |
|
373 | 373 | samplingWindow = numpy.fromfile(fp, SAMPLING_STRUCTURE, self.nWindows) |
|
374 | 374 | else: |
|
375 | 375 | samplingWindow = numpy.fromstring(fp[self.length:], SAMPLING_STRUCTURE, self.nWindows) |
|
376 | 376 | self.length += samplingWindow.nbytes |
|
377 | 377 | except Exception, e: |
|
378 | 378 | print "RadarControllerHeader: " + str(e) |
|
379 | 379 | return 0 |
|
380 | 380 | self.nHeights = int(numpy.sum(samplingWindow['nsa'])) |
|
381 | 381 | self.firstHeight = samplingWindow['h0'] |
|
382 | 382 | self.deltaHeight = samplingWindow['dh'] |
|
383 | 383 | self.samplesWin = samplingWindow['nsa'] |
|
384 | 384 | |
|
385 | 385 | |
|
386 | 386 | |
|
387 | 387 | try: |
|
388 | 388 | if hasattr(fp, 'read'): |
|
389 | 389 | self.Taus = numpy.fromfile(fp, '<f4', self.numTaus) |
|
390 | 390 | else: |
|
391 | 391 | self.Taus = numpy.fromstring(fp[self.length:], '<f4', self.numTaus) |
|
392 | 392 | self.length += self.Taus.nbytes |
|
393 | 393 | except Exception, e: |
|
394 | 394 | print "RadarControllerHeader: " + str(e) |
|
395 | 395 | return 0 |
|
396 | 396 | |
|
397 | 397 | |
|
398 | 398 | |
|
399 | 399 | self.code_size = 0 |
|
400 | 400 | if self.codeType != 0: |
|
401 | 401 | |
|
402 | 402 | try: |
|
403 | 403 | if hasattr(fp, 'read'): |
|
404 | self.nCode = numpy.fromfile(fp, '<u4', 1) | |
|
404 | self.nCode = numpy.fromfile(fp, '<u4', 1)[0] | |
|
405 | 405 | self.length += self.nCode.nbytes |
|
406 | self.nBaud = numpy.fromfile(fp, '<u4', 1) | |
|
406 | self.nBaud = numpy.fromfile(fp, '<u4', 1)[0] | |
|
407 | 407 | self.length += self.nBaud.nbytes |
|
408 | 408 | else: |
|
409 | 409 | self.nCode = numpy.fromstring(fp[self.length:], '<u4', 1)[0] |
|
410 | 410 | self.length += self.nCode.nbytes |
|
411 | 411 | self.nBaud = numpy.fromstring(fp[self.length:], '<u4', 1)[0] |
|
412 | 412 | self.length += self.nBaud.nbytes |
|
413 | 413 | except Exception, e: |
|
414 | 414 | print "RadarControllerHeader: " + str(e) |
|
415 | return 0 | |
|
415 | return 0 | |
|
416 | 416 | code = numpy.empty([self.nCode,self.nBaud],dtype='i1') |
|
417 | 417 | |
|
418 | 418 | for ic in range(self.nCode): |
|
419 | 419 | try: |
|
420 | 420 | if hasattr(fp, 'read'): |
|
421 | 421 | temp = numpy.fromfile(fp,'u4', int(numpy.ceil(self.nBaud/32.))) |
|
422 | 422 | else: |
|
423 | 423 | temp = numpy.fromstring(fp,'u4', int(numpy.ceil(self.nBaud/32.))) |
|
424 | 424 | self.length += temp.nbytes |
|
425 | 425 | except Exception, e: |
|
426 | 426 | print "RadarControllerHeader: " + str(e) |
|
427 | 427 | return 0 |
|
428 | 428 | |
|
429 | 429 | for ib in range(self.nBaud-1,-1,-1): |
|
430 | 430 | code[ic,ib] = temp[ib/32]%2 |
|
431 | 431 | temp[ib/32] = temp[ib/32]/2 |
|
432 | 432 | |
|
433 | 433 | self.code = 2.0*code - 1.0 |
|
434 | 434 | self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4 |
|
435 | 435 | |
|
436 | 436 | # if self.line5Function == RCfunction.FLIP: |
|
437 | 437 | # self.flip1 = numpy.fromfile(fp,'<u4',1) |
|
438 | 438 | # |
|
439 | 439 | # if self.line6Function == RCfunction.FLIP: |
|
440 | 440 | # self.flip2 = numpy.fromfile(fp,'<u4',1) |
|
441 | 441 | if startFp is not None: |
|
442 | 442 | endFp = size + startFp |
|
443 | 443 | |
|
444 | 444 | if fp.tell() != endFp: |
|
445 | 445 | # fp.seek(endFp) |
|
446 | 446 | print "%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" %(fp.name, fp.tell()-startFp, size) |
|
447 | 447 | # return 0 |
|
448 | 448 | |
|
449 | 449 | if fp.tell() > endFp: |
|
450 | 450 | sys.stderr.write("Warning %s: Size value read from Radar Controller header is lower than it has to be\n" %fp.name) |
|
451 | 451 | # return 0 |
|
452 | 452 | |
|
453 | 453 | if fp.tell() < endFp: |
|
454 | 454 | sys.stderr.write("Warning %s: Size value read from Radar Controller header is greater than it has to be\n" %fp.name) |
|
455 | 455 | |
|
456 | 456 | |
|
457 | 457 | return 1 |
|
458 | 458 | |
|
459 | 459 | def write(self, fp): |
|
460 | 460 | |
|
461 | 461 | headerTuple = (self.size, |
|
462 | 462 | self.expType, |
|
463 | 463 | self.nTx, |
|
464 | 464 | self.ipp, |
|
465 | 465 | self.txA, |
|
466 | 466 | self.txB, |
|
467 | 467 | self.nWindows, |
|
468 | 468 | self.numTaus, |
|
469 | 469 | self.codeType, |
|
470 | 470 | self.line6Function, |
|
471 | 471 | self.line5Function, |
|
472 | 472 | self.fClock, |
|
473 | 473 | self.prePulseBefore, |
|
474 | 474 | self.prePulseAfter, |
|
475 | 475 | self.rangeIpp, |
|
476 | 476 | self.rangeTxA, |
|
477 | 477 | self.rangeTxB) |
|
478 | 478 | |
|
479 | 479 | header = numpy.array(headerTuple,RADAR_STRUCTURE) |
|
480 | 480 | header.tofile(fp) |
|
481 | 481 | |
|
482 | 482 | sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin) |
|
483 | 483 | samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE) |
|
484 | 484 | samplingWindow.tofile(fp) |
|
485 | 485 | |
|
486 | 486 | if self.numTaus > 0: |
|
487 | 487 | self.Taus.tofile(fp) |
|
488 | 488 | |
|
489 | 489 | if self.codeType !=0: |
|
490 | 490 | nCode = numpy.array(self.nCode, '<u4') |
|
491 | 491 | nCode.tofile(fp) |
|
492 | 492 | nBaud = numpy.array(self.nBaud, '<u4') |
|
493 | 493 | nBaud.tofile(fp) |
|
494 | 494 | code1 = (self.code + 1.0)/2. |
|
495 | 495 | |
|
496 | 496 | for ic in range(self.nCode): |
|
497 | 497 | tempx = numpy.zeros(numpy.ceil(self.nBaud/32.)) |
|
498 | 498 | start = 0 |
|
499 | 499 | end = 32 |
|
500 | 500 | for i in range(len(tempx)): |
|
501 | 501 | code_selected = code1[ic,start:end] |
|
502 | 502 | for j in range(len(code_selected)-1,-1,-1): |
|
503 | 503 | if code_selected[j] == 1: |
|
504 | 504 | tempx[i] = tempx[i] + 2**(len(code_selected)-1-j) |
|
505 | 505 | start = start + 32 |
|
506 | 506 | end = end + 32 |
|
507 | 507 | |
|
508 | 508 | tempx = tempx.astype('u4') |
|
509 | 509 | tempx.tofile(fp) |
|
510 | 510 | |
|
511 | 511 | # if self.line5Function == RCfunction.FLIP: |
|
512 | 512 | # self.flip1.tofile(fp) |
|
513 | 513 | # |
|
514 | 514 | # if self.line6Function == RCfunction.FLIP: |
|
515 | 515 | # self.flip2.tofile(fp) |
|
516 | 516 | |
|
517 | 517 | return 1 |
|
518 | 518 | |
|
519 | 519 | def get_ippSeconds(self): |
|
520 | 520 | ''' |
|
521 | 521 | ''' |
|
522 | 522 | ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT |
|
523 | 523 | |
|
524 | 524 | return ippSeconds |
|
525 | 525 | |
|
526 | 526 | def set_ippSeconds(self, ippSeconds): |
|
527 | 527 | ''' |
|
528 | 528 | ''' |
|
529 | 529 | |
|
530 | 530 | self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0*1000) |
|
531 | 531 | |
|
532 | 532 | return |
|
533 | 533 | |
|
534 | 534 | def get_size(self): |
|
535 | 535 | |
|
536 | 536 | self.__size = 116 + 12*self.nWindows + 4*self.numTaus |
|
537 | 537 | |
|
538 | 538 | if self.codeType != 0: |
|
539 | 539 | self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.) |
|
540 | 540 | |
|
541 | 541 | return self.__size |
|
542 | 542 | |
|
543 | 543 | def set_size(self, value): |
|
544 | 544 | |
|
545 | 545 | raise IOError, "size is a property and it cannot be set, just read" |
|
546 | 546 | |
|
547 | 547 | return |
|
548 | 548 | |
|
549 | 549 | ippSeconds = property(get_ippSeconds, set_ippSeconds) |
|
550 | 550 | size = property(get_size, set_size) |
|
551 | 551 | |
|
552 | 552 | class ProcessingHeader(Header): |
|
553 | 553 | |
|
554 | 554 | # size = None |
|
555 | 555 | dtype = None |
|
556 | 556 | blockSize = None |
|
557 | 557 | profilesPerBlock = None |
|
558 | 558 | dataBlocksPerFile = None |
|
559 | 559 | nWindows = None |
|
560 | 560 | processFlags = None |
|
561 | 561 | nCohInt = None |
|
562 | 562 | nIncohInt = None |
|
563 | 563 | totalSpectra = None |
|
564 | 564 | structure = PROCESSING_STRUCTURE |
|
565 | 565 | flag_dc = None |
|
566 | 566 | flag_cspc = None |
|
567 | 567 | |
|
568 | 568 | def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0,processFlags=0, nCohInt=0, |
|
569 | 569 | nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0, |
|
570 | 570 | code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False |
|
571 | 571 | ): |
|
572 | 572 | |
|
573 | 573 | # self.size = 0 |
|
574 | 574 | self.dtype = dtype |
|
575 | 575 | self.blockSize = blockSize |
|
576 | 576 | self.profilesPerBlock = 0 |
|
577 | 577 | self.dataBlocksPerFile = 0 |
|
578 | 578 | self.nWindows = 0 |
|
579 | 579 | self.processFlags = 0 |
|
580 | 580 | self.nCohInt = 0 |
|
581 | 581 | self.nIncohInt = 0 |
|
582 | 582 | self.totalSpectra = 0 |
|
583 | 583 | |
|
584 | 584 | self.nHeights = 0 |
|
585 | 585 | self.firstHeight = 0 |
|
586 | 586 | self.deltaHeight = 0 |
|
587 | 587 | self.samplesWin = 0 |
|
588 | 588 | self.spectraComb = 0 |
|
589 | 589 | self.nCode = None |
|
590 | 590 | self.code = None |
|
591 | 591 | self.nBaud = None |
|
592 | 592 | |
|
593 | 593 | self.shif_fft = False |
|
594 | 594 | self.flag_dc = False |
|
595 | 595 | self.flag_cspc = False |
|
596 | 596 | self.flag_decode = False |
|
597 | 597 | self.flag_deflip = False |
|
598 | 598 | self.length = 0 |
|
599 | 599 | |
|
600 | 600 | def read(self, fp): |
|
601 | 601 | self.length = 0 |
|
602 | 602 | try: |
|
603 | 603 | startFp = fp.tell() |
|
604 | 604 | except Exception, e: |
|
605 | 605 | startFp = None |
|
606 | 606 | pass |
|
607 | 607 | |
|
608 | 608 | try: |
|
609 | 609 | if hasattr(fp, 'read'): |
|
610 | 610 | header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1) |
|
611 | 611 | else: |
|
612 | 612 | header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1) |
|
613 | 613 | self.length += header.nbytes |
|
614 | 614 | except Exception, e: |
|
615 | 615 | print "ProcessingHeader: " + str(e) |
|
616 | 616 | return 0 |
|
617 | 617 | |
|
618 | 618 | size = int(header['nSize'][0]) |
|
619 | 619 | self.dtype = int(header['nDataType'][0]) |
|
620 | 620 | self.blockSize = int(header['nSizeOfDataBlock'][0]) |
|
621 | 621 | self.profilesPerBlock = int(header['nProfilesperBlock'][0]) |
|
622 | 622 | self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0]) |
|
623 | 623 | self.nWindows = int(header['nNumWindows'][0]) |
|
624 | 624 | self.processFlags = header['nProcessFlags'] |
|
625 | 625 | self.nCohInt = int(header['nCoherentIntegrations'][0]) |
|
626 | 626 | self.nIncohInt = int(header['nIncoherentIntegrations'][0]) |
|
627 | 627 | self.totalSpectra = int(header['nTotalSpectra'][0]) |
|
628 | 628 | |
|
629 | 629 | try: |
|
630 | 630 | if hasattr(fp, 'read'): |
|
631 | 631 | samplingWindow = numpy.fromfile(fp, SAMPLING_STRUCTURE, self.nWindows) |
|
632 | 632 | else: |
|
633 | 633 | samplingWindow = numpy.fromstring(fp[self.length:], SAMPLING_STRUCTURE, self.nWindows) |
|
634 | 634 | self.length += samplingWindow.nbytes |
|
635 | 635 | except Exception, e: |
|
636 | 636 | print "ProcessingHeader: " + str(e) |
|
637 | 637 | return 0 |
|
638 | 638 | |
|
639 | 639 | self.nHeights = int(numpy.sum(samplingWindow['nsa'])) |
|
640 | 640 | self.firstHeight = float(samplingWindow['h0'][0]) |
|
641 | 641 | self.deltaHeight = float(samplingWindow['dh'][0]) |
|
642 | 642 | self.samplesWin = samplingWindow['nsa'][0] |
|
643 | 643 | |
|
644 | 644 | |
|
645 | 645 | try: |
|
646 | 646 | if hasattr(fp, 'read'): |
|
647 | 647 | self.spectraComb = numpy.fromfile(fp, 'u1', 2*self.totalSpectra) |
|
648 | 648 | else: |
|
649 | 649 | self.spectraComb = numpy.fromstring(fp[self.length:], 'u1', 2*self.totalSpectra) |
|
650 | 650 | self.length += self.spectraComb.nbytes |
|
651 | 651 | except Exception, e: |
|
652 | 652 | print "ProcessingHeader: " + str(e) |
|
653 | 653 | return 0 |
|
654 | 654 | |
|
655 | 655 | if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE): |
|
656 | 656 | self.nCode = int(numpy.fromfile(fp,'<u4',1)) |
|
657 | 657 | self.nBaud = int(numpy.fromfile(fp,'<u4',1)) |
|
658 | 658 | self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nCode,self.nBaud) |
|
659 | 659 | |
|
660 | 660 | if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP): |
|
661 | 661 | exp_name_len = int(numpy.fromfile(fp,'<u4',1)) |
|
662 | 662 | exp_name = numpy.fromfile(fp,'u1',exp_name_len+1) |
|
663 | 663 | |
|
664 | 664 | if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA): |
|
665 | 665 | self.shif_fft = True |
|
666 | 666 | else: |
|
667 | 667 | self.shif_fft = False |
|
668 | 668 | |
|
669 | 669 | if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC): |
|
670 | 670 | self.flag_dc = True |
|
671 | 671 | else: |
|
672 | 672 | self.flag_dc = False |
|
673 | 673 | |
|
674 | 674 | if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA): |
|
675 | 675 | self.flag_decode = True |
|
676 | 676 | else: |
|
677 | 677 | self.flag_decode = False |
|
678 | 678 | |
|
679 | 679 | if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA): |
|
680 | 680 | self.flag_deflip = True |
|
681 | 681 | else: |
|
682 | 682 | self.flag_deflip = False |
|
683 | 683 | |
|
684 | 684 | nChannels = 0 |
|
685 | 685 | nPairs = 0 |
|
686 | 686 | pairList = [] |
|
687 | 687 | |
|
688 | 688 | for i in range( 0, self.totalSpectra*2, 2 ): |
|
689 | 689 | if self.spectraComb[i] == self.spectraComb[i+1]: |
|
690 | 690 | nChannels = nChannels + 1 #par de canales iguales |
|
691 | 691 | else: |
|
692 | 692 | nPairs = nPairs + 1 #par de canales diferentes |
|
693 | 693 | pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) ) |
|
694 | 694 | |
|
695 | 695 | self.flag_cspc = False |
|
696 | 696 | if nPairs > 0: |
|
697 | 697 | self.flag_cspc = True |
|
698 | 698 | |
|
699 | 699 | |
|
700 | 700 | |
|
701 | 701 | if startFp is not None: |
|
702 | 702 | endFp = size + startFp |
|
703 | 703 | if fp.tell() > endFp: |
|
704 | 704 | sys.stderr.write("Warning: Processing header size is lower than it has to be") |
|
705 | 705 | return 0 |
|
706 | 706 | |
|
707 | 707 | if fp.tell() < endFp: |
|
708 | 708 | sys.stderr.write("Warning: Processing header size is greater than it is considered") |
|
709 | 709 | |
|
710 | 710 | return 1 |
|
711 | 711 | |
|
712 | 712 | def write(self, fp): |
|
713 | 713 | #Clear DEFINE_PROCESS_CODE |
|
714 | 714 | self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE) |
|
715 | 715 | |
|
716 | 716 | headerTuple = (self.size, |
|
717 | 717 | self.dtype, |
|
718 | 718 | self.blockSize, |
|
719 | 719 | self.profilesPerBlock, |
|
720 | 720 | self.dataBlocksPerFile, |
|
721 | 721 | self.nWindows, |
|
722 | 722 | self.processFlags, |
|
723 | 723 | self.nCohInt, |
|
724 | 724 | self.nIncohInt, |
|
725 | 725 | self.totalSpectra) |
|
726 | 726 | |
|
727 | 727 | header = numpy.array(headerTuple,PROCESSING_STRUCTURE) |
|
728 | 728 | header.tofile(fp) |
|
729 | 729 | |
|
730 | 730 | if self.nWindows != 0: |
|
731 | 731 | sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin) |
|
732 | 732 | samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE) |
|
733 | 733 | samplingWindow.tofile(fp) |
|
734 | 734 | |
|
735 | 735 | if self.totalSpectra != 0: |
|
736 | 736 | # spectraComb = numpy.array([],numpy.dtype('u1')) |
|
737 | 737 | spectraComb = self.spectraComb |
|
738 | 738 | spectraComb.tofile(fp) |
|
739 | 739 | |
|
740 | 740 | # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE: |
|
741 | 741 | # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba |
|
742 | 742 | # nCode.tofile(fp) |
|
743 | 743 | # |
|
744 | 744 | # nBaud = numpy.array([self.nBaud], numpy.dtype('u4')) |
|
745 | 745 | # nBaud.tofile(fp) |
|
746 | 746 | # |
|
747 | 747 | # code = self.code.reshape(self.nCode*self.nBaud) |
|
748 | 748 | # code = code.astype(numpy.dtype('<f4')) |
|
749 | 749 | # code.tofile(fp) |
|
750 | 750 | |
|
751 | 751 | return 1 |
|
752 | 752 | |
|
753 | 753 | def get_size(self): |
|
754 | 754 | |
|
755 | 755 | self.__size = 40 + 12*self.nWindows + 2*self.totalSpectra |
|
756 | 756 | |
|
757 | 757 | # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE: |
|
758 | 758 | # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.) |
|
759 | 759 | # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud |
|
760 | 760 | |
|
761 | 761 | return self.__size |
|
762 | 762 | |
|
763 | 763 | def set_size(self, value): |
|
764 | 764 | |
|
765 | 765 | raise IOError, "size is a property and it cannot be set, just read" |
|
766 | 766 | |
|
767 | 767 | return |
|
768 | 768 | |
|
769 | 769 | size = property(get_size, set_size) |
|
770 | 770 | |
|
771 | 771 | class RCfunction: |
|
772 | 772 | NONE=0 |
|
773 | 773 | FLIP=1 |
|
774 | 774 | CODE=2 |
|
775 | 775 | SAMPLING=3 |
|
776 | 776 | LIN6DIV256=4 |
|
777 | 777 | SYNCHRO=5 |
|
778 | 778 | |
|
779 | 779 | class nCodeType: |
|
780 | 780 | NONE=0 |
|
781 | 781 | USERDEFINE=1 |
|
782 | 782 | BARKER2=2 |
|
783 | 783 | BARKER3=3 |
|
784 | 784 | BARKER4=4 |
|
785 | 785 | BARKER5=5 |
|
786 | 786 | BARKER7=6 |
|
787 | 787 | BARKER11=7 |
|
788 | 788 | BARKER13=8 |
|
789 | 789 | AC128=9 |
|
790 | 790 | COMPLEMENTARYCODE2=10 |
|
791 | 791 | COMPLEMENTARYCODE4=11 |
|
792 | 792 | COMPLEMENTARYCODE8=12 |
|
793 | 793 | COMPLEMENTARYCODE16=13 |
|
794 | 794 | COMPLEMENTARYCODE32=14 |
|
795 | 795 | COMPLEMENTARYCODE64=15 |
|
796 | 796 | COMPLEMENTARYCODE128=16 |
|
797 | 797 | CODE_BINARY28=17 |
|
798 | 798 | |
|
799 | 799 | class PROCFLAG: |
|
800 | 800 | |
|
801 | 801 | COHERENT_INTEGRATION = numpy.uint32(0x00000001) |
|
802 | 802 | DECODE_DATA = numpy.uint32(0x00000002) |
|
803 | 803 | SPECTRA_CALC = numpy.uint32(0x00000004) |
|
804 | 804 | INCOHERENT_INTEGRATION = numpy.uint32(0x00000008) |
|
805 | 805 | POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010) |
|
806 | 806 | SHIFT_FFT_DATA = numpy.uint32(0x00000020) |
|
807 | 807 | |
|
808 | 808 | DATATYPE_CHAR = numpy.uint32(0x00000040) |
|
809 | 809 | DATATYPE_SHORT = numpy.uint32(0x00000080) |
|
810 | 810 | DATATYPE_LONG = numpy.uint32(0x00000100) |
|
811 | 811 | DATATYPE_INT64 = numpy.uint32(0x00000200) |
|
812 | 812 | DATATYPE_FLOAT = numpy.uint32(0x00000400) |
|
813 | 813 | DATATYPE_DOUBLE = numpy.uint32(0x00000800) |
|
814 | 814 | |
|
815 | 815 | DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000) |
|
816 | 816 | DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000) |
|
817 | 817 | DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000) |
|
818 | 818 | |
|
819 | 819 | SAVE_CHANNELS_DC = numpy.uint32(0x00008000) |
|
820 | 820 | DEFLIP_DATA = numpy.uint32(0x00010000) |
|
821 | 821 | DEFINE_PROCESS_CODE = numpy.uint32(0x00020000) |
|
822 | 822 | |
|
823 | 823 | ACQ_SYS_NATALIA = numpy.uint32(0x00040000) |
|
824 | 824 | ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000) |
|
825 | 825 | ACQ_SYS_ADRXD = numpy.uint32(0x000C0000) |
|
826 | 826 | ACQ_SYS_JULIA = numpy.uint32(0x00100000) |
|
827 | 827 | ACQ_SYS_XXXXXX = numpy.uint32(0x00140000) |
|
828 | 828 | |
|
829 | 829 | EXP_NAME_ESP = numpy.uint32(0x00200000) |
|
830 | 830 | CHANNEL_NAMES_ESP = numpy.uint32(0x00400000) |
|
831 | 831 | |
|
832 | 832 | OPERATION_MASK = numpy.uint32(0x0000003F) |
|
833 | 833 | DATATYPE_MASK = numpy.uint32(0x00000FC0) |
|
834 | 834 | DATAARRANGE_MASK = numpy.uint32(0x00007000) |
|
835 | 835 | ACQ_SYS_MASK = numpy.uint32(0x001C0000) |
|
836 | 836 | |
|
837 | 837 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
838 | 838 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
839 | 839 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
840 | 840 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
841 | 841 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
842 | 842 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
843 | 843 | |
|
844 | 844 | NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
845 | 845 | |
|
846 | 846 | PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR, |
|
847 | 847 | PROCFLAG.DATATYPE_SHORT, |
|
848 | 848 | PROCFLAG.DATATYPE_LONG, |
|
849 | 849 | PROCFLAG.DATATYPE_INT64, |
|
850 | 850 | PROCFLAG.DATATYPE_FLOAT, |
|
851 | 851 | PROCFLAG.DATATYPE_DOUBLE] |
|
852 | 852 | |
|
853 | 853 | DTYPE_WIDTH = [1, 2, 4, 8, 4, 8] |
|
854 | 854 | |
|
855 | 855 | def get_dtype_index(numpy_dtype): |
|
856 | 856 | |
|
857 | 857 | index = None |
|
858 | 858 | |
|
859 | 859 | for i in range(len(NUMPY_DTYPE_LIST)): |
|
860 | 860 | if numpy_dtype == NUMPY_DTYPE_LIST[i]: |
|
861 | 861 | index = i |
|
862 | 862 | break |
|
863 | 863 | |
|
864 | 864 | return index |
|
865 | 865 | |
|
866 | 866 | def get_numpy_dtype(index): |
|
867 | 867 | |
|
868 | 868 | return NUMPY_DTYPE_LIST[index] |
|
869 | 869 | |
|
870 | 870 | def get_procflag_dtype(index): |
|
871 | 871 | |
|
872 | 872 | return PROCFLAG_DTYPE_LIST[index] |
|
873 | 873 | |
|
874 | 874 | def get_dtype_width(index): |
|
875 | 875 | |
|
876 | 876 | return DTYPE_WIDTH[index] No newline at end of file |
@@ -1,657 +1,657 | |||
|
1 | 1 | import os |
|
2 | 2 | import numpy |
|
3 | 3 | import time, datetime |
|
4 | 4 | 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 | 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 | 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 | 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 | 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 | 657 | return x_buffer, y_buffer, z_buffer |
@@ -1,188 +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 | 6 | from figure import Figure, isRealtime |
|
7 | 7 | |
|
8 | 8 | class CorrelationPlot(Figure): |
|
9 | ||
|
10 | 9 | isConfig = None |
|
11 | 10 | __nsubplots = None |
|
12 | 11 | |
|
13 | 12 | WIDTHPROF = None |
|
14 | 13 | HEIGHTPROF = None |
|
15 | 14 | PREFIX = 'corr' |
|
16 | 15 | |
|
17 | 16 | def __init__(self, **kwargs): |
|
18 | 17 | Figure.__init__(self, **kwargs) |
|
19 | 18 | self.isConfig = False |
|
20 | 19 | self.__nsubplots = 1 |
|
21 | 20 | |
|
22 | 21 | self.WIDTH = 280 |
|
23 | 22 | self.HEIGHT = 250 |
|
24 | 23 | self.WIDTHPROF = 120 |
|
25 | 24 | self.HEIGHTPROF = 0 |
|
26 | 25 | self.counter_imagwr = 0 |
|
27 | 26 | |
|
28 | 27 | self.PLOT_CODE = 1 |
|
29 | 28 | self.FTP_WEI = None |
|
30 | 29 | self.EXP_CODE = None |
|
31 | 30 | self.SUB_EXP_CODE = None |
|
32 | 31 | self.PLOT_POS = None |
|
33 | 32 | |
|
34 | 33 | def getSubplots(self): |
|
35 | 34 | |
|
36 | 35 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
37 | 36 | nrow = int(self.nplots*1./ncol + 0.9) |
|
38 | 37 | |
|
39 | 38 | return nrow, ncol |
|
40 | 39 | |
|
41 | 40 | def setup(self, id, nplots, wintitle, showprofile=False, show=True): |
|
42 | 41 | |
|
43 | 42 | showprofile = False |
|
44 | 43 | self.__showprofile = showprofile |
|
45 | 44 | self.nplots = nplots |
|
46 | 45 | |
|
47 | 46 | ncolspan = 1 |
|
48 | 47 | colspan = 1 |
|
49 | 48 | if showprofile: |
|
50 | 49 | ncolspan = 3 |
|
51 | 50 | colspan = 2 |
|
52 | 51 | self.__nsubplots = 2 |
|
53 | 52 | |
|
54 | 53 | self.createFigure(id = id, |
|
55 | 54 | wintitle = wintitle, |
|
56 | 55 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
57 | 56 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
58 | 57 | show=show) |
|
59 | 58 | |
|
60 | 59 | nrow, ncol = self.getSubplots() |
|
61 | 60 | |
|
62 | 61 | counter = 0 |
|
63 | 62 | for y in range(nrow): |
|
64 | 63 | for x in range(ncol): |
|
65 | 64 | |
|
66 | 65 | if counter >= self.nplots: |
|
67 | 66 | break |
|
68 | 67 | |
|
69 | 68 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
70 | 69 | |
|
71 | 70 | if showprofile: |
|
72 | 71 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
73 | 72 | |
|
74 | 73 | counter += 1 |
|
75 | 74 | |
|
76 | 75 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False, |
|
77 | 76 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
78 | 77 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
79 | 78 | server=None, folder=None, username=None, password=None, |
|
80 | 79 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False): |
|
81 | 80 | |
|
82 | 81 | """ |
|
83 | 82 | |
|
84 | 83 | Input: |
|
85 | 84 | dataOut : |
|
86 | 85 | id : |
|
87 | 86 | wintitle : |
|
88 | 87 | channelList : |
|
89 | 88 | showProfile : |
|
90 | 89 | xmin : None, |
|
91 | 90 | xmax : None, |
|
92 | 91 | ymin : None, |
|
93 | 92 | ymax : None, |
|
94 | 93 | zmin : None, |
|
95 | 94 | zmax : None |
|
96 | 95 | """ |
|
97 | 96 | |
|
98 | 97 | if dataOut.flagNoData: |
|
99 | 98 | return None |
|
100 | 99 | |
|
101 | 100 | if realtime: |
|
102 | 101 | if not(isRealtime(utcdatatime = dataOut.utctime)): |
|
103 | 102 | print 'Skipping this plot function' |
|
104 | 103 | return |
|
105 | 104 | |
|
106 | 105 | if channelList == None: |
|
107 | 106 | channelIndexList = dataOut.channelIndexList |
|
108 | 107 | else: |
|
109 | 108 | channelIndexList = [] |
|
110 | 109 | for channel in channelList: |
|
111 | 110 | if channel not in dataOut.channelList: |
|
112 | 111 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
113 | 112 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
114 | 113 | |
|
115 | 114 | factor = dataOut.normFactor |
|
116 | 115 | lenfactor = factor.shape[1] |
|
117 | 116 | x = dataOut.getLagTRange(1) |
|
118 | 117 | y = dataOut.getHeiRange() |
|
119 | 118 | |
|
120 | 119 | z = copy.copy(dataOut.data_corr[:,:,0,:]) |
|
121 | 120 | for i in range(dataOut.data_corr.shape[0]): |
|
122 | 121 | z[i,:,:] = z[i,:,:]/factor[i,:] |
|
123 | 122 | zdB = numpy.abs(z) |
|
124 | 123 | |
|
125 | 124 | avg = numpy.average(z, axis=1) |
|
126 | 125 | # avg = numpy.nanmean(z, axis=1) |
|
127 | 126 | # noise = dataOut.noise/factor |
|
128 | 127 | |
|
129 | 128 | #thisDatetime = dataOut.datatime |
|
130 | 129 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
131 | 130 | title = wintitle + " Correlation" |
|
132 | 131 | xlabel = "Lag T (s)" |
|
133 | 132 | ylabel = "Range (Km)" |
|
134 | 133 | |
|
135 | 134 | if not self.isConfig: |
|
136 | 135 | |
|
137 | 136 | nplots = dataOut.data_corr.shape[0] |
|
138 | 137 | |
|
139 | 138 | self.setup(id=id, |
|
140 | 139 | nplots=nplots, |
|
141 | 140 | wintitle=wintitle, |
|
142 | 141 | showprofile=showprofile, |
|
143 | 142 | show=show) |
|
144 | 143 | |
|
145 | 144 | if xmin == None: xmin = numpy.nanmin(x) |
|
146 | 145 | if xmax == None: xmax = numpy.nanmax(x) |
|
147 | 146 | if ymin == None: ymin = numpy.nanmin(y) |
|
148 | 147 | if ymax == None: ymax = numpy.nanmax(y) |
|
149 | 148 | if zmin == None: zmin = 0 |
|
150 | 149 | if zmax == None: zmax = 1 |
|
151 | 150 | |
|
152 | 151 | self.FTP_WEI = ftp_wei |
|
153 | 152 | self.EXP_CODE = exp_code |
|
154 | 153 | self.SUB_EXP_CODE = sub_exp_code |
|
155 | 154 | self.PLOT_POS = plot_pos |
|
156 | 155 | |
|
157 | 156 | self.isConfig = True |
|
158 | 157 | |
|
159 | 158 | self.setWinTitle(title) |
|
160 | 159 | |
|
161 | 160 | for i in range(self.nplots): |
|
162 | 161 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
163 | 162 | title = "Channel %d and %d: : %s" %(dataOut.pairsList[i][0],dataOut.pairsList[i][1] , str_datetime) |
|
164 | 163 | axes = self.axesList[i*self.__nsubplots] |
|
165 | 164 | axes.pcolor(x, y, zdB[i,:,:], |
|
166 | 165 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
167 | 166 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
168 | 167 | ticksize=9, cblabel='') |
|
169 | 168 | |
|
170 | 169 | # if self.__showprofile: |
|
171 | 170 | # axes = self.axesList[i*self.__nsubplots +1] |
|
172 | 171 | # axes.pline(avgdB[i], y, |
|
173 | 172 | # xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
174 | 173 | # xlabel='dB', ylabel='', title='', |
|
175 | 174 | # ytick_visible=False, |
|
176 | 175 | # grid='x') |
|
177 | 176 | # |
|
178 | 177 | # noiseline = numpy.repeat(noisedB[i], len(y)) |
|
179 | 178 | # axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) |
|
180 | 179 | |
|
181 | 180 | self.draw() |
|
182 | 181 | |
|
183 | 182 | self.save(figpath=figpath, |
|
184 | 183 | figfile=figfile, |
|
185 | 184 | save=save, |
|
186 | 185 | ftp=ftp, |
|
187 | 186 | wr_period=wr_period, |
|
188 | 187 | thisDatetime=thisDatetime) |
@@ -1,775 +1,964 | |||
|
1 | 1 | |
|
2 | 2 | import os |
|
3 | 3 | import zmq |
|
4 | 4 | import time |
|
5 | 5 | import numpy |
|
6 | 6 | import datetime |
|
7 | 7 | import numpy as np |
|
8 | 8 | import matplotlib |
|
9 | import glob | |
|
9 | 10 | matplotlib.use('TkAgg') |
|
10 | 11 | import matplotlib.pyplot as plt |
|
11 | 12 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
12 | 13 | from matplotlib.ticker import FuncFormatter, LinearLocator |
|
13 | 14 | from multiprocessing import Process |
|
14 | 15 | |
|
15 | 16 | from schainpy.model.proc.jroproc_base import Operation |
|
16 | 17 | |
|
17 | 18 | plt.ion() |
|
18 | 19 | |
|
19 | 20 | func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime('%H:%M')) |
|
21 | fromtimestamp = lambda x, mintime : (datetime.datetime.utcfromtimestamp(mintime).replace(hour=(x + 5), minute=0) - d1970).total_seconds() | |
|
22 | ||
|
20 | 23 | |
|
21 | 24 | d1970 = datetime.datetime(1970,1,1) |
|
22 | 25 | |
|
23 | 26 | class PlotData(Operation, Process): |
|
24 | 27 | |
|
25 | 28 | CODE = 'Figure' |
|
26 | 29 | colormap = 'jro' |
|
27 | 30 | CONFLATE = False |
|
28 | 31 | __MAXNUMX = 80 |
|
29 | 32 | __missing = 1E30 |
|
30 | 33 | |
|
31 | 34 | def __init__(self, **kwargs): |
|
32 | 35 | |
|
33 | 36 | Operation.__init__(self, plot=True, **kwargs) |
|
34 | 37 | Process.__init__(self) |
|
35 | 38 | self.kwargs['code'] = self.CODE |
|
36 | 39 | self.mp = False |
|
37 | 40 | self.dataOut = None |
|
38 | 41 | self.isConfig = False |
|
39 | 42 | self.figure = None |
|
40 | 43 | self.axes = [] |
|
41 | 44 | self.localtime = kwargs.pop('localtime', True) |
|
42 | 45 | self.show = kwargs.get('show', True) |
|
43 | 46 | self.save = kwargs.get('save', False) |
|
44 | 47 | self.colormap = kwargs.get('colormap', self.colormap) |
|
45 | 48 | self.colormap_coh = kwargs.get('colormap_coh', 'jet') |
|
46 | 49 | self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r') |
|
47 | 50 | self.showprofile = kwargs.get('showprofile', True) |
|
48 | 51 | self.title = kwargs.get('wintitle', '') |
|
49 | 52 | self.xaxis = kwargs.get('xaxis', 'frequency') |
|
50 | 53 | self.zmin = kwargs.get('zmin', None) |
|
51 | 54 | self.zmax = kwargs.get('zmax', None) |
|
52 | 55 | self.xmin = kwargs.get('xmin', None) |
|
53 | 56 | self.xmax = kwargs.get('xmax', None) |
|
54 | 57 | self.xrange = kwargs.get('xrange', 24) |
|
55 | 58 | self.ymin = kwargs.get('ymin', None) |
|
56 | 59 | self.ymax = kwargs.get('ymax', None) |
|
57 |
self.__MAXNUMY = kwargs.get('decimation', |
|
|
60 | self.__MAXNUMY = kwargs.get('decimation', 5000) | |
|
58 | 61 | self.throttle_value = 5 |
|
59 | 62 | self.times = [] |
|
60 | 63 | #self.interactive = self.kwargs['parent'] |
|
61 | 64 | |
|
65 | ''' | |
|
66 | this new parameter is created to plot data from varius channels at different figures | |
|
67 | 1. crear una lista de figuras donde se puedan plotear las figuras, | |
|
68 | 2. dar las opciones de configuracion a cada figura, estas opciones son iguales para ambas figuras | |
|
69 | 3. probar? | |
|
70 | ''' | |
|
71 | self.ind_plt_ch = kwargs.get('ind_plt_ch', False) | |
|
72 | self.figurelist = None | |
|
73 | ||
|
62 | 74 | |
|
63 | 75 | def fill_gaps(self, x_buffer, y_buffer, z_buffer): |
|
64 | 76 | |
|
65 | 77 | if x_buffer.shape[0] < 2: |
|
66 | 78 | return x_buffer, y_buffer, z_buffer |
|
67 | 79 | |
|
68 | 80 | deltas = x_buffer[1:] - x_buffer[0:-1] |
|
69 | 81 | x_median = np.median(deltas) |
|
70 | 82 | |
|
71 | 83 | index = np.where(deltas > 5*x_median) |
|
72 | 84 | |
|
73 | 85 | if len(index[0]) != 0: |
|
74 | 86 | z_buffer[::, index[0], ::] = self.__missing |
|
75 | 87 | z_buffer = np.ma.masked_inside(z_buffer, |
|
76 | 88 | 0.99*self.__missing, |
|
77 | 89 | 1.01*self.__missing) |
|
78 | 90 | |
|
79 | 91 | return x_buffer, y_buffer, z_buffer |
|
80 | 92 | |
|
81 | 93 | def decimate(self): |
|
82 | 94 | |
|
83 | 95 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 |
|
84 | 96 | dy = int(len(self.y)/self.__MAXNUMY) + 1 |
|
85 | 97 | |
|
86 | 98 | # x = self.x[::dx] |
|
87 | 99 | x = self.x |
|
88 | 100 | y = self.y[::dy] |
|
89 | 101 | z = self.z[::, ::, ::dy] |
|
90 | 102 | |
|
91 | 103 | return x, y, z |
|
92 | 104 | |
|
105 | ''' | |
|
106 | JM: | |
|
107 | elimana las otras imagenes generadas debido a que lso workers no llegan en orden y le pueden | |
|
108 | poner otro tiempo a la figura q no necesariamente es el ultimo. | |
|
109 | Solo se realiza cuando termina la imagen. | |
|
110 | Problemas: | |
|
111 | ||
|
112 | File "/home/ci-81/workspace/schainv2.3/schainpy/model/graphics/jroplot_data.py", line 145, in __plot | |
|
113 | for n, eachfigure in enumerate(self.figurelist): | |
|
114 | TypeError: 'NoneType' object is not iterable | |
|
115 | ||
|
116 | ''' | |
|
117 | def deleteanotherfiles(self): | |
|
118 | figurenames=[] | |
|
119 | if self.figurelist != None: | |
|
120 | for n, eachfigure in enumerate(self.figurelist): | |
|
121 | #add specific name for each channel in channelList | |
|
122 | ghostfigname = os.path.join(self.save, '{}_{}_{}'.format(self.titles[n].replace(' ',''),self.CODE, | |
|
123 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d'))) | |
|
124 | figname = os.path.join(self.save, '{}_{}_{}.png'.format(self.titles[n].replace(' ',''),self.CODE, | |
|
125 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
126 | ||
|
127 | for ghostfigure in glob.glob(ghostfigname+'*'): #ghostfigure will adopt all posible names of figures | |
|
128 | if ghostfigure != figname: | |
|
129 | os.remove(ghostfigure) | |
|
130 | print 'Removing GhostFigures:' , figname | |
|
131 | else : | |
|
132 | '''Erasing ghost images for just on******************''' | |
|
133 | ghostfigname = os.path.join(self.save, '{}_{}'.format(self.CODE,datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d'))) | |
|
134 | figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
135 | for ghostfigure in glob.glob(ghostfigname+'*'): #ghostfigure will adopt all posible names of figures | |
|
136 | if ghostfigure != figname: | |
|
137 | os.remove(ghostfigure) | |
|
138 | print 'Removing GhostFigures:' , figname | |
|
139 | ||
|
93 | 140 | def __plot(self): |
|
94 | 141 | |
|
95 | 142 | print 'plotting...{}'.format(self.CODE) |
|
96 | ||
|
97 | if self.show: | |
|
98 | self.figure.show() | |
|
99 | ||
|
100 | self.plot() | |
|
101 | plt.tight_layout() | |
|
102 | self.figure.canvas.manager.set_window_title('{} {} - {}'.format(self.title, self.CODE.upper(), | |
|
143 | if self.ind_plt_ch is False : #standard | |
|
144 | if self.show: | |
|
145 | self.figure.show() | |
|
146 | self.plot() | |
|
147 | plt.tight_layout() | |
|
148 | self.figure.canvas.manager.set_window_title('{} {} - {}'.format(self.title, self.CODE.upper(), | |
|
103 | 149 | datetime.datetime.fromtimestamp(self.max_time).strftime('%Y/%m/%d'))) |
|
150 | else : | |
|
151 | print 'len(self.figurelist): ',len(self.figurelist) | |
|
152 | for n, eachfigure in enumerate(self.figurelist): | |
|
153 | if self.show: | |
|
154 | eachfigure.show() | |
|
155 | ||
|
156 | self.plot() | |
|
157 | eachfigure.tight_layout() # ajuste de cada subplot | |
|
158 | eachfigure.canvas.manager.set_window_title('{} {} - {}'.format(self.title[n], self.CODE.upper(), | |
|
159 | datetime.datetime.fromtimestamp(self.max_time).strftime('%Y/%m/%d'))) | |
|
160 | ||
|
161 | # if self.save: | |
|
162 | # if self.ind_plt_ch is False : #standard | |
|
163 | # figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE, | |
|
164 | # datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
165 | # print 'Saving figure: {}'.format(figname) | |
|
166 | # self.figure.savefig(figname) | |
|
167 | # else : | |
|
168 | # for n, eachfigure in enumerate(self.figurelist): | |
|
169 | # #add specific name for each channel in channelList | |
|
170 | # figname = os.path.join(self.save, '{}_{}_{}.png'.format(self.titles[n],self.CODE, | |
|
171 | # datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
172 | # | |
|
173 | # print 'Saving figure: {}'.format(figname) | |
|
174 | # eachfigure.savefig(figname) | |
|
175 | ||
|
176 | if self.ind_plt_ch is False : | |
|
177 | self.figure.canvas.draw() | |
|
178 | else : | |
|
179 | for eachfigure in self.figurelist: | |
|
180 | eachfigure.canvas.draw() | |
|
104 | 181 | |
|
105 | 182 | if self.save: |
|
106 | figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE, | |
|
107 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
108 | print 'Saving figure: {}'.format(figname) | |
|
109 | self.figure.savefig(figname) | |
|
183 | if self.ind_plt_ch is False : #standard | |
|
184 | figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE, | |
|
185 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
186 | print 'Saving figure: {}'.format(figname) | |
|
187 | self.figure.savefig(figname) | |
|
188 | else : | |
|
189 | for n, eachfigure in enumerate(self.figurelist): | |
|
190 | #add specific name for each channel in channelList | |
|
191 | figname = os.path.join(self.save, '{}_{}_{}.png'.format(self.titles[n].replace(' ',''),self.CODE, | |
|
192 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) | |
|
193 | ||
|
194 | print 'Saving figure: {}'.format(figname) | |
|
195 | eachfigure.savefig(figname) | |
|
110 | 196 | |
|
111 | self.figure.canvas.draw() | |
|
112 | 197 | |
|
113 | 198 | def plot(self): |
|
114 | 199 | |
|
115 | 200 | print 'plotting...{}'.format(self.CODE.upper()) |
|
116 | 201 | return |
|
117 | 202 | |
|
118 | 203 | def run(self): |
|
119 | 204 | |
|
120 | 205 | print '[Starting] {}'.format(self.name) |
|
121 | 206 | |
|
122 | 207 | context = zmq.Context() |
|
123 | 208 | receiver = context.socket(zmq.SUB) |
|
124 | 209 | receiver.setsockopt(zmq.SUBSCRIBE, '') |
|
125 | 210 | receiver.setsockopt(zmq.CONFLATE, self.CONFLATE) |
|
126 | ||
|
211 | ||
|
127 | 212 | if 'server' in self.kwargs['parent']: |
|
128 | 213 | receiver.connect('ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server'])) |
|
129 | 214 | else: |
|
130 | 215 | receiver.connect("ipc:///tmp/zmq.plots") |
|
131 | 216 | |
|
132 | 217 | seconds_passed = 0 |
|
133 | 218 | |
|
134 | 219 | while True: |
|
135 | 220 | try: |
|
136 | 221 | self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)#flags=zmq.NOBLOCK |
|
137 | 222 | self.started = self.data['STARTED'] |
|
138 | 223 | self.dataOut = self.data['dataOut'] |
|
139 | 224 | |
|
140 | 225 | if (len(self.times) < len(self.data['times']) and not self.started and self.data['ENDED']): |
|
141 | 226 | continue |
|
142 | 227 | |
|
143 | 228 | self.times = self.data['times'] |
|
144 | 229 | self.times.sort() |
|
145 | 230 | self.throttle_value = self.data['throttle'] |
|
146 | 231 | self.min_time = self.times[0] |
|
147 | 232 | self.max_time = self.times[-1] |
|
148 | 233 | |
|
149 | 234 | if self.isConfig is False: |
|
150 | 235 | print 'setting up' |
|
151 | 236 | self.setup() |
|
152 | 237 | self.isConfig = True |
|
153 | 238 | self.__plot() |
|
154 | 239 | |
|
155 | 240 | if self.data['ENDED'] is True: |
|
156 | 241 | print '********GRAPHIC ENDED********' |
|
157 | 242 | self.ended = True |
|
158 | 243 | self.isConfig = False |
|
159 | 244 | self.__plot() |
|
245 | self.deleteanotherfiles() #CLPDG | |
|
160 | 246 | elif seconds_passed >= self.data['throttle']: |
|
161 | 247 | print 'passed', seconds_passed |
|
162 | 248 | self.__plot() |
|
163 | 249 | seconds_passed = 0 |
|
164 | 250 | |
|
165 | 251 | except zmq.Again as e: |
|
166 | 252 | print 'Waiting for data...' |
|
167 | 253 | plt.pause(2) |
|
168 | 254 | seconds_passed += 2 |
|
169 | 255 | |
|
170 | 256 | def close(self): |
|
171 | 257 | if self.dataOut: |
|
172 | 258 | self.__plot() |
|
173 | 259 | |
|
174 | 260 | |
|
175 | 261 | class PlotSpectraData(PlotData): |
|
176 | 262 | |
|
177 | 263 | CODE = 'spc' |
|
178 | 264 | colormap = 'jro' |
|
179 | 265 | CONFLATE = False |
|
180 | 266 | |
|
181 | 267 | def setup(self): |
|
182 | 268 | |
|
183 | 269 | ncolspan = 1 |
|
184 | 270 | colspan = 1 |
|
185 | 271 | self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9) |
|
186 | 272 | self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9) |
|
187 | 273 | self.width = 3.6*self.ncols |
|
188 | 274 | self.height = 3.2*self.nrows |
|
189 | 275 | if self.showprofile: |
|
190 | 276 | ncolspan = 3 |
|
191 | 277 | colspan = 2 |
|
192 | 278 | self.width += 1.2*self.ncols |
|
193 | 279 | |
|
194 | 280 | self.ylabel = 'Range [Km]' |
|
195 | 281 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
196 | 282 | |
|
197 | 283 | if self.figure is None: |
|
198 | 284 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
199 | 285 | edgecolor='k', |
|
200 | 286 | facecolor='w') |
|
201 | 287 | else: |
|
202 | 288 | self.figure.clf() |
|
203 | 289 | |
|
204 | 290 | n = 0 |
|
205 | 291 | for y in range(self.nrows): |
|
206 | 292 | for x in range(self.ncols): |
|
207 | 293 | if n >= self.dataOut.nChannels: |
|
208 | 294 | break |
|
209 | 295 | ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan) |
|
210 | 296 | if self.showprofile: |
|
211 | 297 | ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1) |
|
212 | 298 | |
|
213 | 299 | ax.firsttime = True |
|
214 | 300 | self.axes.append(ax) |
|
215 | 301 | n += 1 |
|
216 | 302 | |
|
217 | 303 | def plot(self): |
|
218 | 304 | |
|
219 | 305 | if self.xaxis == "frequency": |
|
220 | 306 | x = self.dataOut.getFreqRange(1)/1000. |
|
221 | 307 | xlabel = "Frequency (kHz)" |
|
222 | 308 | elif self.xaxis == "time": |
|
223 | 309 | x = self.dataOut.getAcfRange(1) |
|
224 | 310 | xlabel = "Time (ms)" |
|
225 | 311 | else: |
|
226 | 312 | x = self.dataOut.getVelRange(1) |
|
227 | 313 | xlabel = "Velocity (m/s)" |
|
228 | 314 | |
|
229 | 315 | y = self.dataOut.getHeiRange() |
|
230 | 316 | z = self.data[self.CODE] |
|
231 | 317 | |
|
232 | 318 | for n, ax in enumerate(self.axes): |
|
233 | ||
|
234 | 319 | if ax.firsttime: |
|
235 | 320 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
236 | 321 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
237 | 322 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
238 | 323 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
239 | 324 | self.zmin = self.zmin if self.zmin else np.nanmin(z) |
|
240 | 325 | self.zmax = self.zmax if self.zmax else np.nanmax(z) |
|
241 | 326 | ax.plot = ax.pcolormesh(x, y, z[n].T, |
|
242 | 327 | vmin=self.zmin, |
|
243 | 328 | vmax=self.zmax, |
|
244 | 329 | cmap=plt.get_cmap(self.colormap) |
|
245 | 330 | ) |
|
246 | 331 | divider = make_axes_locatable(ax) |
|
247 | 332 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
248 | 333 | self.figure.add_axes(cax) |
|
249 | 334 | plt.colorbar(ax.plot, cax) |
|
250 | 335 | |
|
251 | 336 | ax.set_xlim(self.xmin, self.xmax) |
|
252 | 337 | ax.set_ylim(self.ymin, self.ymax) |
|
253 | 338 | |
|
254 | 339 | ax.set_ylabel(self.ylabel) |
|
255 | 340 | ax.set_xlabel(xlabel) |
|
256 | 341 | |
|
257 | 342 | ax.firsttime = False |
|
258 | 343 | |
|
259 | 344 | if self.showprofile: |
|
260 | 345 | ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0] |
|
261 | 346 | ax.ax_profile.set_xlim(self.zmin, self.zmax) |
|
262 | 347 | ax.ax_profile.set_ylim(self.ymin, self.ymax) |
|
263 | 348 | ax.ax_profile.set_xlabel('dB') |
|
264 | 349 | ax.ax_profile.grid(b=True, axis='x') |
|
265 | 350 | ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y, |
|
266 | 351 | color="k", linestyle="dashed", lw=2)[0] |
|
267 | 352 | [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()] |
|
268 | 353 | else: |
|
269 | 354 | ax.plot.set_array(z[n].T.ravel()) |
|
270 | 355 | if self.showprofile: |
|
271 | 356 | ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y) |
|
272 | 357 | ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y) |
|
273 | 358 | |
|
274 | 359 | ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]), |
|
275 | 360 | size=8) |
|
276 | 361 | self.saveTime = self.max_time |
|
277 | 362 | |
|
278 | 363 | |
|
279 | 364 | class PlotCrossSpectraData(PlotData): |
|
280 | 365 | |
|
281 | 366 | CODE = 'cspc' |
|
282 | 367 | zmin_coh = None |
|
283 | 368 | zmax_coh = None |
|
284 | 369 | zmin_phase = None |
|
285 | 370 | zmax_phase = None |
|
286 | 371 | CONFLATE = False |
|
287 | 372 | |
|
288 | 373 | def setup(self): |
|
289 | 374 | |
|
290 | 375 | ncolspan = 1 |
|
291 | 376 | colspan = 1 |
|
292 | 377 | self.ncols = 2 |
|
293 | 378 | self.nrows = self.dataOut.nPairs |
|
294 | 379 | self.width = 3.6*self.ncols |
|
295 | 380 | self.height = 3.2*self.nrows |
|
296 | 381 | |
|
297 | 382 | self.ylabel = 'Range [Km]' |
|
298 | 383 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
299 | 384 | |
|
300 | 385 | if self.figure is None: |
|
301 | 386 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
302 | 387 | edgecolor='k', |
|
303 | 388 | facecolor='w') |
|
304 | 389 | else: |
|
305 | 390 | self.figure.clf() |
|
306 | 391 | |
|
307 | 392 | for y in range(self.nrows): |
|
308 | 393 | for x in range(self.ncols): |
|
309 | 394 | ax = plt.subplot2grid((self.nrows, self.ncols), (y, x), 1, 1) |
|
310 | 395 | ax.firsttime = True |
|
311 | 396 | self.axes.append(ax) |
|
312 | 397 | |
|
313 | 398 | def plot(self): |
|
314 | 399 | |
|
315 | 400 | if self.xaxis == "frequency": |
|
316 | 401 | x = self.dataOut.getFreqRange(1)/1000. |
|
317 | 402 | xlabel = "Frequency (kHz)" |
|
318 | 403 | elif self.xaxis == "time": |
|
319 | 404 | x = self.dataOut.getAcfRange(1) |
|
320 | 405 | xlabel = "Time (ms)" |
|
321 | 406 | else: |
|
322 | 407 | x = self.dataOut.getVelRange(1) |
|
323 | 408 | xlabel = "Velocity (m/s)" |
|
324 | 409 | |
|
325 | 410 | y = self.dataOut.getHeiRange() |
|
326 | 411 | z_coh = self.data['cspc_coh'] |
|
327 | 412 | z_phase = self.data['cspc_phase'] |
|
328 | 413 | |
|
329 | 414 | for n in range(self.nrows): |
|
330 | 415 | ax = self.axes[2*n] |
|
331 | 416 | ax1 = self.axes[2*n+1] |
|
332 | 417 | if ax.firsttime: |
|
333 | 418 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
334 | 419 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
335 | 420 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
336 | 421 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
337 | 422 | self.zmin_coh = self.zmin_coh if self.zmin_coh else 0.0 |
|
338 | 423 | self.zmax_coh = self.zmax_coh if self.zmax_coh else 1.0 |
|
339 | 424 | self.zmin_phase = self.zmin_phase if self.zmin_phase else -180 |
|
340 | 425 | self.zmax_phase = self.zmax_phase if self.zmax_phase else 180 |
|
341 | 426 | |
|
342 | 427 | ax.plot = ax.pcolormesh(x, y, z_coh[n].T, |
|
343 | 428 | vmin=self.zmin_coh, |
|
344 | 429 | vmax=self.zmax_coh, |
|
345 | 430 | cmap=plt.get_cmap(self.colormap_coh) |
|
346 | 431 | ) |
|
347 | 432 | divider = make_axes_locatable(ax) |
|
348 | 433 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
349 | 434 | self.figure.add_axes(cax) |
|
350 | 435 | plt.colorbar(ax.plot, cax) |
|
351 | 436 | |
|
352 | 437 | ax.set_xlim(self.xmin, self.xmax) |
|
353 | 438 | ax.set_ylim(self.ymin, self.ymax) |
|
354 | 439 | |
|
355 | 440 | ax.set_ylabel(self.ylabel) |
|
356 | 441 | ax.set_xlabel(xlabel) |
|
357 | 442 | ax.firsttime = False |
|
358 | 443 | |
|
359 | 444 | ax1.plot = ax1.pcolormesh(x, y, z_phase[n].T, |
|
360 | 445 | vmin=self.zmin_phase, |
|
361 | 446 | vmax=self.zmax_phase, |
|
362 | 447 | cmap=plt.get_cmap(self.colormap_phase) |
|
363 | 448 | ) |
|
364 | 449 | divider = make_axes_locatable(ax1) |
|
365 | 450 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
366 | 451 | self.figure.add_axes(cax) |
|
367 | 452 | plt.colorbar(ax1.plot, cax) |
|
368 | 453 | |
|
369 | 454 | ax1.set_xlim(self.xmin, self.xmax) |
|
370 | 455 | ax1.set_ylim(self.ymin, self.ymax) |
|
371 | 456 | |
|
372 | 457 | ax1.set_ylabel(self.ylabel) |
|
373 | 458 | ax1.set_xlabel(xlabel) |
|
374 | 459 | ax1.firsttime = False |
|
375 | 460 | else: |
|
376 | 461 | ax.plot.set_array(z_coh[n].T.ravel()) |
|
377 | 462 | ax1.plot.set_array(z_phase[n].T.ravel()) |
|
378 | 463 | |
|
379 | 464 | ax.set_title('Coherence Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8) |
|
380 | 465 | ax1.set_title('Phase Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8) |
|
381 | 466 | self.saveTime = self.max_time |
|
382 | 467 | |
|
383 | 468 | |
|
384 | 469 | class PlotSpectraMeanData(PlotSpectraData): |
|
385 | 470 | |
|
386 | 471 | CODE = 'spc_mean' |
|
387 | 472 | colormap = 'jet' |
|
388 | 473 | |
|
389 | 474 | def plot(self): |
|
390 | 475 | |
|
391 | 476 | if self.xaxis == "frequency": |
|
392 | 477 | x = self.dataOut.getFreqRange(1)/1000. |
|
393 | 478 | xlabel = "Frequency (kHz)" |
|
394 | 479 | elif self.xaxis == "time": |
|
395 | 480 | x = self.dataOut.getAcfRange(1) |
|
396 | 481 | xlabel = "Time (ms)" |
|
397 | 482 | else: |
|
398 | 483 | x = self.dataOut.getVelRange(1) |
|
399 | 484 | xlabel = "Velocity (m/s)" |
|
400 | 485 | |
|
401 | 486 | y = self.dataOut.getHeiRange() |
|
402 | 487 | z = self.data['spc'] |
|
403 | 488 | mean = self.data['mean'][self.max_time] |
|
404 | 489 | |
|
405 | 490 | for n, ax in enumerate(self.axes): |
|
406 | 491 | |
|
407 | 492 | if ax.firsttime: |
|
408 | 493 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
409 | 494 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
410 | 495 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
411 | 496 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
412 | 497 | self.zmin = self.zmin if self.zmin else np.nanmin(z) |
|
413 | 498 | self.zmax = self.zmax if self.zmax else np.nanmax(z) |
|
414 | 499 | ax.plt = ax.pcolormesh(x, y, z[n].T, |
|
415 | 500 | vmin=self.zmin, |
|
416 | 501 | vmax=self.zmax, |
|
417 | 502 | cmap=plt.get_cmap(self.colormap) |
|
418 | 503 | ) |
|
419 | 504 | ax.plt_dop = ax.plot(mean[n], y, |
|
420 | 505 | color='k')[0] |
|
421 | 506 | |
|
422 | 507 | divider = make_axes_locatable(ax) |
|
423 | 508 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
424 | 509 | self.figure.add_axes(cax) |
|
425 | 510 | plt.colorbar(ax.plt, cax) |
|
426 | 511 | |
|
427 | 512 | ax.set_xlim(self.xmin, self.xmax) |
|
428 | 513 | ax.set_ylim(self.ymin, self.ymax) |
|
429 | 514 | |
|
430 | 515 | ax.set_ylabel(self.ylabel) |
|
431 | 516 | ax.set_xlabel(xlabel) |
|
432 | 517 | |
|
433 | 518 | ax.firsttime = False |
|
434 | 519 | |
|
435 | 520 | if self.showprofile: |
|
436 | 521 | ax.plt_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0] |
|
437 | 522 | ax.ax_profile.set_xlim(self.zmin, self.zmax) |
|
438 | 523 | ax.ax_profile.set_ylim(self.ymin, self.ymax) |
|
439 | 524 | ax.ax_profile.set_xlabel('dB') |
|
440 | 525 | ax.ax_profile.grid(b=True, axis='x') |
|
441 | 526 | ax.plt_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y, |
|
442 | 527 | color="k", linestyle="dashed", lw=2)[0] |
|
443 | 528 | [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()] |
|
444 | 529 | else: |
|
445 | 530 | ax.plt.set_array(z[n].T.ravel()) |
|
446 | 531 | ax.plt_dop.set_data(mean[n], y) |
|
447 | 532 | if self.showprofile: |
|
448 | 533 | ax.plt_profile.set_data(self.data['rti'][self.max_time][n], y) |
|
449 | 534 | ax.plt_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y) |
|
450 | 535 | |
|
451 | 536 | ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]), |
|
452 | 537 | size=8) |
|
453 | 538 | self.saveTime = self.max_time |
|
454 | 539 | |
|
455 | 540 | |
|
456 | 541 | class PlotRTIData(PlotData): |
|
457 | 542 | |
|
458 | 543 | CODE = 'rti' |
|
459 | 544 | colormap = 'jro' |
|
460 | 545 | |
|
461 | 546 | def setup(self): |
|
462 | 547 | self.ncols = 1 |
|
463 | 548 | self.nrows = self.dataOut.nChannels |
|
464 | 549 | self.width = 10 |
|
465 | self.height = 2.2*self.nrows if self.nrows<6 else 12 | |
|
550 | #TODO : arreglar la altura de la figura, esta hardcodeada. | |
|
551 | #Se arreglo, testear! | |
|
552 | if self.ind_plt_ch: | |
|
553 | self.height = 3.2#*self.nrows if self.nrows<6 else 12 | |
|
554 | else: | |
|
555 | self.height = 2.2*self.nrows if self.nrows<6 else 12 | |
|
556 | ||
|
557 | ''' | |
|
466 | 558 | if self.nrows==1: |
|
467 | 559 | self.height += 1 |
|
560 | ''' | |
|
468 | 561 | self.ylabel = 'Range [Km]' |
|
469 | 562 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
470 | 563 | |
|
471 | if self.figure is None: | |
|
472 | self.figure = plt.figure(figsize=(self.width, self.height), | |
|
473 | edgecolor='k', | |
|
474 | facecolor='w') | |
|
475 | else: | |
|
476 | self.figure.clf() | |
|
477 | self.axes = [] | |
|
478 | ||
|
479 | for n in range(self.nrows): | |
|
480 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) | |
|
481 | ax.firsttime = True | |
|
482 | self.axes.append(ax) | |
|
483 | ||
|
484 | def plot(self): | |
|
485 | ||
|
486 | self.x = np.array(self.times) | |
|
487 | self.y = self.dataOut.getHeiRange() | |
|
488 | self.z = [] | |
|
489 | ||
|
490 | for ch in range(self.nrows): | |
|
491 | self.z.append([self.data[self.CODE][t][ch] for t in self.times]) | |
|
564 | ''' | |
|
565 | Logica: | |
|
566 | 1) Si la variable ind_plt_ch es True, va a crear mas de 1 figura | |
|
567 | 2) guardamos "Figures" en una lista y "axes" en otra, quizas se deberia guardar el | |
|
568 | axis dentro de "Figures" como un diccionario. | |
|
569 | ''' | |
|
570 | if self.ind_plt_ch is False: #standard mode | |
|
571 | ||
|
572 | if self.figure is None: #solo para la priemra vez | |
|
573 | self.figure = plt.figure(figsize=(self.width, self.height), | |
|
574 | edgecolor='k', | |
|
575 | facecolor='w') | |
|
576 | else: | |
|
577 | self.figure.clf() | |
|
578 | self.axes = [] | |
|
492 | 579 | |
|
493 | self.z = np.array(self.z) | |
|
494 | for n, ax in enumerate(self.axes): | |
|
495 | x, y, z = self.fill_gaps(*self.decimate()) | |
|
496 | xmin = self.min_time | |
|
497 | xmax = xmin+self.xrange*60*60 | |
|
498 | self.zmin = self.zmin if self.zmin else np.min(self.z) | |
|
499 | self.zmax = self.zmax if self.zmax else np.max(self.z) | |
|
500 | if ax.firsttime: | |
|
501 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) | |
|
502 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) | |
|
503 | plot = ax.pcolormesh(x, y, z[n].T, | |
|
504 | vmin=self.zmin, | |
|
505 | vmax=self.zmax, | |
|
506 | cmap=plt.get_cmap(self.colormap) | |
|
507 | ) | |
|
508 | divider = make_axes_locatable(ax) | |
|
509 | cax = divider.new_horizontal(size='2%', pad=0.05) | |
|
510 | self.figure.add_axes(cax) | |
|
511 | plt.colorbar(plot, cax) | |
|
512 | ax.set_ylim(self.ymin, self.ymax) | |
|
513 | 580 | |
|
514 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
|
515 | ax.xaxis.set_major_locator(LinearLocator(6)) | |
|
581 | for n in range(self.nrows): | |
|
582 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) | |
|
583 | #ax = self.figure(n+1) | |
|
584 | ax.firsttime = True | |
|
585 | self.axes.append(ax) | |
|
516 | 586 | |
|
517 | ax.set_ylabel(self.ylabel) | |
|
587 | else : #append one figure foreach channel in channelList | |
|
588 | if self.figurelist == None: | |
|
589 | self.figurelist = [] | |
|
590 | for n in range(self.nrows): | |
|
591 | self.figure = plt.figure(figsize=(self.width, self.height), | |
|
592 | edgecolor='k', | |
|
593 | facecolor='w') | |
|
594 | #add always one subplot | |
|
595 | self.figurelist.append(self.figure) | |
|
596 | ||
|
597 | else : # cada dia nuevo limpia el axes, pero mantiene el figure | |
|
598 | for eachfigure in self.figurelist: | |
|
599 | eachfigure.clf() # eliminaria todas las figuras de la lista? | |
|
600 | self.axes = [] | |
|
601 | ||
|
602 | for eachfigure in self.figurelist: | |
|
603 | ax = eachfigure.add_subplot(1,1,1) #solo 1 axis por figura | |
|
604 | #ax = self.figure(n+1) | |
|
605 | ax.firsttime = True | |
|
606 | #Cada figura tiene un distinto puntero | |
|
607 | self.axes.append(ax) | |
|
608 | #plt.close(eachfigure) | |
|
518 | 609 | |
|
519 | # if self.xmin is None: | |
|
520 | # xmin = self.min_time | |
|
521 | # else: | |
|
522 | # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(), | |
|
523 | # datetime.time(self.xmin, 0, 0))-d1970).total_seconds() | |
|
524 | 610 | |
|
525 | ax.set_xlim(xmin, xmax) | |
|
526 | ax.firsttime = False | |
|
527 | else: | |
|
528 | ax.collections.remove(ax.collections[0]) | |
|
529 | ax.set_xlim(xmin, xmax) | |
|
530 | plot = ax.pcolormesh(x, y, z[n].T, | |
|
531 | vmin=self.zmin, | |
|
532 | vmax=self.zmax, | |
|
533 | cmap=plt.get_cmap(self.colormap) | |
|
534 | ) | |
|
535 | ax.set_title('{} {}'.format(self.titles[n], | |
|
536 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), | |
|
537 | size=8) | |
|
611 | def plot(self): | |
|
538 | 612 | |
|
539 | self.saveTime = self.min_time | |
|
613 | if self.ind_plt_ch is False: #standard mode | |
|
614 | self.x = np.array(self.times) | |
|
615 | self.y = self.dataOut.getHeiRange() | |
|
616 | self.z = [] | |
|
617 | ||
|
618 | for ch in range(self.nrows): | |
|
619 | self.z.append([self.data[self.CODE][t][ch] for t in self.times]) | |
|
620 | ||
|
621 | self.z = np.array(self.z) | |
|
622 | for n, ax in enumerate(self.axes): | |
|
623 | x, y, z = self.fill_gaps(*self.decimate()) | |
|
624 | if self.xmin is None: | |
|
625 | xmin = self.min_time | |
|
626 | else: | |
|
627 | xmin = fromtimestamp(int(self.xmin), self.min_time) | |
|
628 | if self.xmax is None: | |
|
629 | xmax = xmin + self.xrange*60*60 | |
|
630 | else: | |
|
631 | xmax = xmin + (self.xmax - self.xmin) * 60 * 60 | |
|
632 | self.zmin = self.zmin if self.zmin else np.min(self.z) | |
|
633 | self.zmax = self.zmax if self.zmax else np.max(self.z) | |
|
634 | if ax.firsttime: | |
|
635 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) | |
|
636 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) | |
|
637 | plot = ax.pcolormesh(x, y, z[n].T, | |
|
638 | vmin=self.zmin, | |
|
639 | vmax=self.zmax, | |
|
640 | cmap=plt.get_cmap(self.colormap) | |
|
641 | ) | |
|
642 | divider = make_axes_locatable(ax) | |
|
643 | cax = divider.new_horizontal(size='2%', pad=0.05) | |
|
644 | self.figure.add_axes(cax) | |
|
645 | plt.colorbar(plot, cax) | |
|
646 | ax.set_ylim(self.ymin, self.ymax) | |
|
647 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
|
648 | ax.xaxis.set_major_locator(LinearLocator(6)) | |
|
649 | ax.set_ylabel(self.ylabel) | |
|
650 | # if self.xmin is None: | |
|
651 | # xmin = self.min_time | |
|
652 | # else: | |
|
653 | # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(), | |
|
654 | # datetime.time(self.xmin, 0, 0))-d1970).total_seconds() | |
|
655 | ||
|
656 | ax.set_xlim(xmin, xmax) | |
|
657 | ax.firsttime = False | |
|
658 | else: | |
|
659 | ax.collections.remove(ax.collections[0]) | |
|
660 | ax.set_xlim(xmin, xmax) | |
|
661 | plot = ax.pcolormesh(x, y, z[n].T, | |
|
662 | vmin=self.zmin, | |
|
663 | vmax=self.zmax, | |
|
664 | cmap=plt.get_cmap(self.colormap) | |
|
665 | ) | |
|
666 | ax.set_title('{} {}'.format(self.titles[n], | |
|
667 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), | |
|
668 | size=8) | |
|
669 | ||
|
670 | self.saveTime = self.min_time | |
|
671 | else : | |
|
672 | self.x = np.array(self.times) | |
|
673 | self.y = self.dataOut.getHeiRange() | |
|
674 | self.z = [] | |
|
675 | ||
|
676 | for ch in range(self.nrows): | |
|
677 | self.z.append([self.data[self.CODE][t][ch] for t in self.times]) | |
|
678 | ||
|
679 | self.z = np.array(self.z) | |
|
680 | for n, eachfigure in enumerate(self.figurelist): #estaba ax in axes | |
|
681 | ||
|
682 | x, y, z = self.fill_gaps(*self.decimate()) | |
|
683 | xmin = self.min_time | |
|
684 | xmax = xmin+self.xrange*60*60 | |
|
685 | self.zmin = self.zmin if self.zmin else np.min(self.z) | |
|
686 | self.zmax = self.zmax if self.zmax else np.max(self.z) | |
|
687 | if self.axes[n].firsttime: | |
|
688 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) | |
|
689 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) | |
|
690 | plot = self.axes[n].pcolormesh(x, y, z[n].T, | |
|
691 | vmin=self.zmin, | |
|
692 | vmax=self.zmax, | |
|
693 | cmap=plt.get_cmap(self.colormap) | |
|
694 | ) | |
|
695 | divider = make_axes_locatable(self.axes[n]) | |
|
696 | cax = divider.new_horizontal(size='2%', pad=0.05) | |
|
697 | eachfigure.add_axes(cax) | |
|
698 | #self.figure2.add_axes(cax) | |
|
699 | plt.colorbar(plot, cax) | |
|
700 | self.axes[n].set_ylim(self.ymin, self.ymax) | |
|
701 | ||
|
702 | self.axes[n].xaxis.set_major_formatter(FuncFormatter(func)) | |
|
703 | self.axes[n].xaxis.set_major_locator(LinearLocator(6)) | |
|
704 | ||
|
705 | self.axes[n].set_ylabel(self.ylabel) | |
|
706 | ||
|
707 | if self.xmin is None: | |
|
708 | xmin = self.min_time | |
|
709 | else: | |
|
710 | xmin = (datetime.datetime.combine(self.dataOut.datatime.date(), | |
|
711 | datetime.time(self.xmin, 0, 0))-d1970).total_seconds() | |
|
712 | ||
|
713 | self.axes[n].set_xlim(xmin, xmax) | |
|
714 | self.axes[n].firsttime = False | |
|
715 | else: | |
|
716 | self.axes[n].collections.remove(self.axes[n].collections[0]) | |
|
717 | self.axes[n].set_xlim(xmin, xmax) | |
|
718 | plot = self.axes[n].pcolormesh(x, y, z[n].T, | |
|
719 | vmin=self.zmin, | |
|
720 | vmax=self.zmax, | |
|
721 | cmap=plt.get_cmap(self.colormap) | |
|
722 | ) | |
|
723 | self.axes[n].set_title('{} {}'.format(self.titles[n], | |
|
724 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), | |
|
725 | size=8) | |
|
726 | ||
|
727 | self.saveTime = self.min_time | |
|
540 | 728 | |
|
541 | 729 | |
|
542 | 730 | class PlotCOHData(PlotRTIData): |
|
543 | 731 | |
|
544 | 732 | CODE = 'coh' |
|
545 | 733 | |
|
546 | 734 | def setup(self): |
|
547 | 735 | |
|
548 | 736 | self.ncols = 1 |
|
549 | 737 | self.nrows = self.dataOut.nPairs |
|
550 | 738 | self.width = 10 |
|
551 | 739 | self.height = 2.2*self.nrows if self.nrows<6 else 12 |
|
740 | self.ind_plt_ch = False #just for coherence and phase | |
|
552 | 741 | if self.nrows==1: |
|
553 | 742 | self.height += 1 |
|
554 | 743 | self.ylabel = 'Range [Km]' |
|
555 | 744 | self.titles = ['{} Ch{} * Ch{}'.format(self.CODE.upper(), x[0], x[1]) for x in self.dataOut.pairsList] |
|
556 | 745 | |
|
557 | 746 | if self.figure is None: |
|
558 | 747 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
559 | 748 | edgecolor='k', |
|
560 | 749 | facecolor='w') |
|
561 | 750 | else: |
|
562 | 751 | self.figure.clf() |
|
563 | 752 | self.axes = [] |
|
564 | 753 | |
|
565 | 754 | for n in range(self.nrows): |
|
566 | 755 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) |
|
567 | 756 | ax.firsttime = True |
|
568 | 757 | self.axes.append(ax) |
|
569 | 758 | |
|
570 | 759 | |
|
571 | 760 | class PlotNoiseData(PlotData): |
|
572 | 761 | CODE = 'noise' |
|
573 | 762 | |
|
574 | 763 | def setup(self): |
|
575 | 764 | |
|
576 | 765 | self.ncols = 1 |
|
577 | 766 | self.nrows = 1 |
|
578 | 767 | self.width = 10 |
|
579 | 768 | self.height = 3.2 |
|
580 | 769 | self.ylabel = 'Intensity [dB]' |
|
581 | 770 | self.titles = ['Noise'] |
|
582 | 771 | |
|
583 | 772 | if self.figure is None: |
|
584 | 773 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
585 | 774 | edgecolor='k', |
|
586 | 775 | facecolor='w') |
|
587 | 776 | else: |
|
588 | 777 | self.figure.clf() |
|
589 | 778 | self.axes = [] |
|
590 | 779 | |
|
591 | 780 | self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1) |
|
592 | 781 | self.ax.firsttime = True |
|
593 | 782 | |
|
594 | 783 | def plot(self): |
|
595 | 784 | |
|
596 | 785 | x = self.times |
|
597 | 786 | xmin = self.min_time |
|
598 | 787 | xmax = xmin+self.xrange*60*60 |
|
599 | 788 | if self.ax.firsttime: |
|
600 | 789 | for ch in self.dataOut.channelList: |
|
601 | 790 | y = [self.data[self.CODE][t][ch] for t in self.times] |
|
602 | 791 | self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch)) |
|
603 | 792 | self.ax.firsttime = False |
|
604 | 793 | self.ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
605 | 794 | self.ax.xaxis.set_major_locator(LinearLocator(6)) |
|
606 | 795 | self.ax.set_ylabel(self.ylabel) |
|
607 | 796 | plt.legend() |
|
608 | 797 | else: |
|
609 | 798 | for ch in self.dataOut.channelList: |
|
610 | 799 | y = [self.data[self.CODE][t][ch] for t in self.times] |
|
611 | 800 | self.ax.lines[ch].set_data(x, y) |
|
612 | 801 | |
|
613 | 802 | self.ax.set_xlim(xmin, xmax) |
|
614 | 803 | self.ax.set_ylim(min(y)-5, max(y)+5) |
|
615 | 804 | self.saveTime = self.min_time |
|
616 | 805 | |
|
617 | 806 | |
|
618 | 807 | class PlotWindProfilerData(PlotRTIData): |
|
619 | 808 | |
|
620 | 809 | CODE = 'wind' |
|
621 | 810 | colormap = 'seismic' |
|
622 | 811 | |
|
623 | 812 | def setup(self): |
|
624 | 813 | self.ncols = 1 |
|
625 | 814 | self.nrows = self.dataOut.data_output.shape[0] |
|
626 | 815 | self.width = 10 |
|
627 | 816 | self.height = 2.2*self.nrows |
|
628 | 817 | self.ylabel = 'Height [Km]' |
|
629 | 818 | self.titles = ['Zonal Wind' ,'Meridional Wind', 'Vertical Wind'] |
|
630 | 819 | self.clabels = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)'] |
|
631 | 820 | self.windFactor = [1, 1, 100] |
|
632 | 821 | |
|
633 | 822 | if self.figure is None: |
|
634 | 823 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
635 | 824 | edgecolor='k', |
|
636 | 825 | facecolor='w') |
|
637 | 826 | else: |
|
638 | 827 | self.figure.clf() |
|
639 | 828 | self.axes = [] |
|
640 | 829 | |
|
641 | 830 | for n in range(self.nrows): |
|
642 | 831 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) |
|
643 | 832 | ax.firsttime = True |
|
644 | 833 | self.axes.append(ax) |
|
645 | 834 | |
|
646 | 835 | def plot(self): |
|
647 | 836 | |
|
648 | 837 | self.x = np.array(self.times) |
|
649 | 838 | self.y = self.dataOut.heightList |
|
650 | 839 | self.z = [] |
|
651 | 840 | |
|
652 | 841 | for ch in range(self.nrows): |
|
653 | 842 | self.z.append([self.data['output'][t][ch] for t in self.times]) |
|
654 | 843 | |
|
655 | 844 | self.z = np.array(self.z) |
|
656 | 845 | self.z = numpy.ma.masked_invalid(self.z) |
|
657 | 846 | |
|
658 | 847 | cmap=plt.get_cmap(self.colormap) |
|
659 | 848 | cmap.set_bad('black', 1.) |
|
660 | 849 | |
|
661 | 850 | for n, ax in enumerate(self.axes): |
|
662 | 851 | x, y, z = self.fill_gaps(*self.decimate()) |
|
663 | 852 | xmin = self.min_time |
|
664 | 853 | xmax = xmin+self.xrange*60*60 |
|
665 | 854 | if ax.firsttime: |
|
666 | 855 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) |
|
667 | 856 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) |
|
668 | 857 | self.zmax = self.zmax if self.zmax else numpy.nanmax(abs(self.z[:-1, :])) |
|
669 | 858 | self.zmin = self.zmin if self.zmin else -self.zmax |
|
670 | 859 | |
|
671 | 860 | plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n], |
|
672 | 861 | vmin=self.zmin, |
|
673 | 862 | vmax=self.zmax, |
|
674 | 863 | cmap=cmap |
|
675 | 864 | ) |
|
676 | 865 | divider = make_axes_locatable(ax) |
|
677 | 866 | cax = divider.new_horizontal(size='2%', pad=0.05) |
|
678 | 867 | self.figure.add_axes(cax) |
|
679 | 868 | cb = plt.colorbar(plot, cax) |
|
680 | 869 | cb.set_label(self.clabels[n]) |
|
681 | 870 | ax.set_ylim(self.ymin, self.ymax) |
|
682 | 871 | |
|
683 | 872 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
684 | 873 | ax.xaxis.set_major_locator(LinearLocator(6)) |
|
685 | 874 | |
|
686 | 875 | ax.set_ylabel(self.ylabel) |
|
687 | 876 | |
|
688 | 877 | ax.set_xlim(xmin, xmax) |
|
689 | 878 | ax.firsttime = False |
|
690 | 879 | else: |
|
691 | 880 | ax.collections.remove(ax.collections[0]) |
|
692 | 881 | ax.set_xlim(xmin, xmax) |
|
693 | 882 | plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n], |
|
694 | 883 | vmin=self.zmin, |
|
695 | 884 | vmax=self.zmax, |
|
696 | 885 | cmap=plt.get_cmap(self.colormap) |
|
697 | 886 | ) |
|
698 | 887 | ax.set_title('{} {}'.format(self.titles[n], |
|
699 | 888 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), |
|
700 | 889 | size=8) |
|
701 | 890 | |
|
702 | 891 | self.saveTime = self.min_time |
|
703 | 892 | |
|
704 | 893 | |
|
705 | 894 | class PlotSNRData(PlotRTIData): |
|
706 | 895 | CODE = 'snr' |
|
707 | 896 | colormap = 'jet' |
|
708 | 897 | |
|
709 | 898 | class PlotDOPData(PlotRTIData): |
|
710 | 899 | CODE = 'dop' |
|
711 | 900 | colormap = 'jet' |
|
712 | 901 | |
|
713 | 902 | |
|
714 | 903 | class PlotPHASEData(PlotCOHData): |
|
715 | 904 | CODE = 'phase' |
|
716 | 905 | colormap = 'seismic' |
|
717 | 906 | |
|
718 | 907 | |
|
719 | 908 | class PlotSkyMapData(PlotData): |
|
720 | 909 | |
|
721 | 910 | CODE = 'met' |
|
722 | 911 | |
|
723 | 912 | def setup(self): |
|
724 | 913 | |
|
725 | 914 | self.ncols = 1 |
|
726 | 915 | self.nrows = 1 |
|
727 | 916 | self.width = 7.2 |
|
728 | 917 | self.height = 7.2 |
|
729 | 918 | |
|
730 | 919 | self.xlabel = 'Zonal Zenith Angle (deg)' |
|
731 | 920 | self.ylabel = 'Meridional Zenith Angle (deg)' |
|
732 | 921 | |
|
733 | 922 | if self.figure is None: |
|
734 | 923 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
735 | 924 | edgecolor='k', |
|
736 | 925 | facecolor='w') |
|
737 | 926 | else: |
|
738 | 927 | self.figure.clf() |
|
739 | 928 | |
|
740 | 929 | self.ax = plt.subplot2grid((self.nrows, self.ncols), (0, 0), 1, 1, polar=True) |
|
741 | 930 | self.ax.firsttime = True |
|
742 | 931 | |
|
743 | 932 | |
|
744 | 933 | def plot(self): |
|
745 | 934 | |
|
746 | 935 | arrayParameters = np.concatenate([self.data['param'][t] for t in self.times]) |
|
747 | 936 | error = arrayParameters[:,-1] |
|
748 | 937 | indValid = numpy.where(error == 0)[0] |
|
749 | 938 | finalMeteor = arrayParameters[indValid,:] |
|
750 | 939 | finalAzimuth = finalMeteor[:,3] |
|
751 | 940 | finalZenith = finalMeteor[:,4] |
|
752 | 941 | |
|
753 | 942 | x = finalAzimuth*numpy.pi/180 |
|
754 | 943 | y = finalZenith |
|
755 | 944 | |
|
756 | 945 | if self.ax.firsttime: |
|
757 | 946 | self.ax.plot = self.ax.plot(x, y, 'bo', markersize=5)[0] |
|
758 | 947 | self.ax.set_ylim(0,90) |
|
759 | 948 | self.ax.set_yticks(numpy.arange(0,90,20)) |
|
760 | 949 | self.ax.set_xlabel(self.xlabel) |
|
761 | 950 | self.ax.set_ylabel(self.ylabel) |
|
762 | 951 | self.ax.yaxis.labelpad = 40 |
|
763 | 952 | self.ax.firsttime = False |
|
764 | 953 | else: |
|
765 | 954 | self.ax.plot.set_data(x, y) |
|
766 | 955 | |
|
767 | 956 | |
|
768 | 957 | dt1 = datetime.datetime.fromtimestamp(self.min_time).strftime('%y/%m/%d %H:%M:%S') |
|
769 | 958 | dt2 = datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S') |
|
770 | 959 | title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1, |
|
771 | 960 | dt2, |
|
772 | 961 | len(x)) |
|
773 | 962 | self.ax.set_title(title, size=8) |
|
774 | 963 | |
|
775 | 964 | self.saveTime = self.max_time |
@@ -1,1945 +1,1945 | |||
|
1 | 1 | import os |
|
2 | 2 | import datetime |
|
3 | 3 | import numpy |
|
4 | 4 | import inspect |
|
5 | 5 | from figure import Figure, isRealtime, isTimeInHourRange |
|
6 | 6 | from plotting_codes import * |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | class MomentsPlot(Figure): |
|
10 | 10 | |
|
11 | 11 | isConfig = None |
|
12 | 12 | __nsubplots = None |
|
13 | 13 | |
|
14 | 14 | WIDTHPROF = None |
|
15 | 15 | HEIGHTPROF = None |
|
16 | 16 | PREFIX = 'prm' |
|
17 | ||
|
18 | 17 | def __init__(self, **kwargs): |
|
19 | 18 | Figure.__init__(self, **kwargs) |
|
20 | 19 | self.isConfig = False |
|
21 | 20 | self.__nsubplots = 1 |
|
22 | 21 | |
|
23 | 22 | self.WIDTH = 280 |
|
24 | 23 | self.HEIGHT = 250 |
|
25 | 24 | self.WIDTHPROF = 120 |
|
26 | 25 | self.HEIGHTPROF = 0 |
|
27 | 26 | self.counter_imagwr = 0 |
|
28 | 27 | |
|
29 | 28 | self.PLOT_CODE = MOMENTS_CODE |
|
30 | 29 | |
|
31 | 30 | self.FTP_WEI = None |
|
32 | 31 | self.EXP_CODE = None |
|
33 | 32 | self.SUB_EXP_CODE = None |
|
34 | 33 | self.PLOT_POS = None |
|
35 | 34 | |
|
36 | 35 | def getSubplots(self): |
|
37 | 36 | |
|
38 | 37 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
39 | 38 | nrow = int(self.nplots*1./ncol + 0.9) |
|
40 | 39 | |
|
41 | 40 | return nrow, ncol |
|
42 | 41 | |
|
43 | 42 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
44 | 43 | |
|
45 | 44 | self.__showprofile = showprofile |
|
46 | 45 | self.nplots = nplots |
|
47 | 46 | |
|
48 | 47 | ncolspan = 1 |
|
49 | 48 | colspan = 1 |
|
50 | 49 | if showprofile: |
|
51 | 50 | ncolspan = 3 |
|
52 | 51 | colspan = 2 |
|
53 | 52 | self.__nsubplots = 2 |
|
54 | 53 | |
|
55 | 54 | self.createFigure(id = id, |
|
56 | 55 | wintitle = wintitle, |
|
57 | 56 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
58 | 57 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
59 | 58 | show=show) |
|
60 | 59 | |
|
61 | 60 | nrow, ncol = self.getSubplots() |
|
62 | 61 | |
|
63 | 62 | counter = 0 |
|
64 | 63 | for y in range(nrow): |
|
65 | 64 | for x in range(ncol): |
|
66 | 65 | |
|
67 | 66 | if counter >= self.nplots: |
|
68 | 67 | break |
|
69 | 68 | |
|
70 | 69 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
71 | 70 | |
|
72 | 71 | if showprofile: |
|
73 | 72 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
74 | 73 | |
|
75 | 74 | counter += 1 |
|
76 | 75 | |
|
77 | 76 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, |
|
78 | 77 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
79 | 78 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
80 | 79 | server=None, folder=None, username=None, password=None, |
|
81 | 80 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False): |
|
82 | 81 | |
|
83 | 82 | """ |
|
84 | 83 | |
|
85 | 84 | Input: |
|
86 | 85 | dataOut : |
|
87 | 86 | id : |
|
88 | 87 | wintitle : |
|
89 | 88 | channelList : |
|
90 | 89 | showProfile : |
|
91 | 90 | xmin : None, |
|
92 | 91 | xmax : None, |
|
93 | 92 | ymin : None, |
|
94 | 93 | ymax : None, |
|
95 | 94 | zmin : None, |
|
96 | 95 | zmax : None |
|
97 | 96 | """ |
|
98 | 97 | |
|
99 | 98 | if dataOut.flagNoData: |
|
100 | 99 | return None |
|
101 | 100 | |
|
102 | 101 | if realtime: |
|
103 | 102 | if not(isRealtime(utcdatatime = dataOut.utctime)): |
|
104 | 103 | print 'Skipping this plot function' |
|
105 | 104 | return |
|
106 | 105 | |
|
107 | 106 | if channelList == None: |
|
108 | 107 | channelIndexList = dataOut.channelIndexList |
|
109 | 108 | else: |
|
110 | 109 | channelIndexList = [] |
|
111 | 110 | for channel in channelList: |
|
112 | 111 | if channel not in dataOut.channelList: |
|
113 | 112 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
114 | 113 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
115 | 114 | |
|
116 | 115 | factor = dataOut.normFactor |
|
117 | 116 | x = dataOut.abscissaList |
|
118 | 117 | y = dataOut.heightList |
|
119 | 118 | |
|
120 | 119 | z = dataOut.data_pre[channelIndexList,:,:]/factor |
|
121 | 120 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
122 | 121 | avg = numpy.average(z, axis=1) |
|
123 | 122 | noise = dataOut.noise/factor |
|
124 | 123 | |
|
125 | 124 | zdB = 10*numpy.log10(z) |
|
126 | 125 | avgdB = 10*numpy.log10(avg) |
|
127 | 126 | noisedB = 10*numpy.log10(noise) |
|
128 | 127 | |
|
129 | 128 | #thisDatetime = dataOut.datatime |
|
130 | 129 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
131 | 130 | title = wintitle + " Parameters" |
|
132 | 131 | xlabel = "Velocity (m/s)" |
|
133 | 132 | ylabel = "Range (Km)" |
|
134 | 133 | |
|
135 | 134 | update_figfile = False |
|
136 | 135 | |
|
137 | 136 | if not self.isConfig: |
|
138 | 137 | |
|
139 | 138 | nplots = len(channelIndexList) |
|
140 | 139 | |
|
141 | 140 | self.setup(id=id, |
|
142 | 141 | nplots=nplots, |
|
143 | 142 | wintitle=wintitle, |
|
144 | 143 | showprofile=showprofile, |
|
145 | 144 | show=show) |
|
146 | 145 | |
|
147 | 146 | if xmin == None: xmin = numpy.nanmin(x) |
|
148 | 147 | if xmax == None: xmax = numpy.nanmax(x) |
|
149 | 148 | if ymin == None: ymin = numpy.nanmin(y) |
|
150 | 149 | if ymax == None: ymax = numpy.nanmax(y) |
|
151 | 150 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 |
|
152 | 151 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 |
|
153 | 152 | |
|
154 | 153 | self.FTP_WEI = ftp_wei |
|
155 | 154 | self.EXP_CODE = exp_code |
|
156 | 155 | self.SUB_EXP_CODE = sub_exp_code |
|
157 | 156 | self.PLOT_POS = plot_pos |
|
158 | 157 | |
|
159 | 158 | self.isConfig = True |
|
160 | 159 | update_figfile = True |
|
161 | 160 | |
|
162 | 161 | self.setWinTitle(title) |
|
163 | 162 | |
|
164 | 163 | for i in range(self.nplots): |
|
165 | 164 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
166 | 165 | title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i], noisedB[i], str_datetime) |
|
167 | 166 | axes = self.axesList[i*self.__nsubplots] |
|
168 | 167 | axes.pcolor(x, y, zdB[i,:,:], |
|
169 | 168 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
170 | 169 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
171 | 170 | ticksize=9, cblabel='') |
|
172 | 171 | #Mean Line |
|
173 | 172 | mean = dataOut.data_param[i, 1, :] |
|
174 | 173 | axes.addpline(mean, y, idline=0, color="black", linestyle="solid", lw=1) |
|
175 | 174 | |
|
176 | 175 | if self.__showprofile: |
|
177 | 176 | axes = self.axesList[i*self.__nsubplots +1] |
|
178 | 177 | axes.pline(avgdB[i], y, |
|
179 | 178 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
180 | 179 | xlabel='dB', ylabel='', title='', |
|
181 | 180 | ytick_visible=False, |
|
182 | 181 | grid='x') |
|
183 | 182 | |
|
184 | 183 | noiseline = numpy.repeat(noisedB[i], len(y)) |
|
185 | 184 | axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) |
|
186 | 185 | |
|
187 | 186 | self.draw() |
|
188 | 187 | |
|
189 | 188 | self.save(figpath=figpath, |
|
190 | 189 | figfile=figfile, |
|
191 | 190 | save=save, |
|
192 | 191 | ftp=ftp, |
|
193 | 192 | wr_period=wr_period, |
|
194 | 193 | thisDatetime=thisDatetime) |
|
195 | 194 | |
|
196 | 195 | |
|
197 | 196 | |
|
198 | 197 | class SkyMapPlot(Figure): |
|
199 | 198 | |
|
200 | 199 | __isConfig = None |
|
201 | 200 | __nsubplots = None |
|
202 | 201 | |
|
203 | 202 | WIDTHPROF = None |
|
204 | 203 | HEIGHTPROF = None |
|
205 | 204 | PREFIX = 'mmap' |
|
206 | 205 | |
|
207 | 206 | def __init__(self, **kwargs): |
|
208 | 207 | Figure.__init__(self, **kwargs) |
|
209 | 208 | self.isConfig = False |
|
210 | 209 | self.__nsubplots = 1 |
|
211 | 210 | |
|
212 | 211 | # self.WIDTH = 280 |
|
213 | 212 | # self.HEIGHT = 250 |
|
214 | 213 | self.WIDTH = 600 |
|
215 | 214 | self.HEIGHT = 600 |
|
216 | 215 | self.WIDTHPROF = 120 |
|
217 | 216 | self.HEIGHTPROF = 0 |
|
218 | 217 | self.counter_imagwr = 0 |
|
219 | 218 | |
|
220 | 219 | self.PLOT_CODE = MSKYMAP_CODE |
|
221 | 220 | |
|
222 | 221 | self.FTP_WEI = None |
|
223 | 222 | self.EXP_CODE = None |
|
224 | 223 | self.SUB_EXP_CODE = None |
|
225 | 224 | self.PLOT_POS = None |
|
226 | 225 | |
|
227 | 226 | def getSubplots(self): |
|
228 | 227 | |
|
229 | 228 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
230 | 229 | nrow = int(self.nplots*1./ncol + 0.9) |
|
231 | 230 | |
|
232 | 231 | return nrow, ncol |
|
233 | 232 | |
|
234 | 233 | def setup(self, id, nplots, wintitle, showprofile=False, show=True): |
|
235 | 234 | |
|
236 | 235 | self.__showprofile = showprofile |
|
237 | 236 | self.nplots = nplots |
|
238 | 237 | |
|
239 | 238 | ncolspan = 1 |
|
240 | 239 | colspan = 1 |
|
241 | 240 | |
|
242 | 241 | self.createFigure(id = id, |
|
243 | 242 | wintitle = wintitle, |
|
244 | 243 | widthplot = self.WIDTH, #+ self.WIDTHPROF, |
|
245 | 244 | heightplot = self.HEIGHT,# + self.HEIGHTPROF, |
|
246 | 245 | show=show) |
|
247 | 246 | |
|
248 | 247 | nrow, ncol = 1,1 |
|
249 | 248 | counter = 0 |
|
250 | 249 | x = 0 |
|
251 | 250 | y = 0 |
|
252 | 251 | self.addAxes(1, 1, 0, 0, 1, 1, True) |
|
253 | 252 | |
|
254 | 253 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False, |
|
255 | 254 | tmin=0, tmax=24, timerange=None, |
|
256 | 255 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
257 | 256 | server=None, folder=None, username=None, password=None, |
|
258 | 257 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False): |
|
259 | 258 | |
|
260 | 259 | """ |
|
261 | 260 | |
|
262 | 261 | Input: |
|
263 | 262 | dataOut : |
|
264 | 263 | id : |
|
265 | 264 | wintitle : |
|
266 | 265 | channelList : |
|
267 | 266 | showProfile : |
|
268 | 267 | xmin : None, |
|
269 | 268 | xmax : None, |
|
270 | 269 | ymin : None, |
|
271 | 270 | ymax : None, |
|
272 | 271 | zmin : None, |
|
273 | 272 | zmax : None |
|
274 | 273 | """ |
|
275 | 274 | |
|
276 | 275 | arrayParameters = dataOut.data_param |
|
277 | 276 | error = arrayParameters[:,-1] |
|
278 | 277 | indValid = numpy.where(error == 0)[0] |
|
279 | 278 | finalMeteor = arrayParameters[indValid,:] |
|
280 | 279 | finalAzimuth = finalMeteor[:,3] |
|
281 | 280 | finalZenith = finalMeteor[:,4] |
|
282 | 281 | |
|
283 | 282 | x = finalAzimuth*numpy.pi/180 |
|
284 | 283 | y = finalZenith |
|
285 | 284 | x1 = [dataOut.ltctime, dataOut.ltctime] |
|
286 | 285 | |
|
287 | 286 | #thisDatetime = dataOut.datatime |
|
288 | 287 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime) |
|
289 | 288 | title = wintitle + " Parameters" |
|
290 | 289 | xlabel = "Zonal Zenith Angle (deg) " |
|
291 | 290 | ylabel = "Meridional Zenith Angle (deg)" |
|
292 | 291 | update_figfile = False |
|
293 | 292 | |
|
294 | 293 | if not self.isConfig: |
|
295 | 294 | |
|
296 | 295 | nplots = 1 |
|
297 | 296 | |
|
298 | 297 | self.setup(id=id, |
|
299 | 298 | nplots=nplots, |
|
300 | 299 | wintitle=wintitle, |
|
301 | 300 | showprofile=showprofile, |
|
302 | 301 | show=show) |
|
303 | 302 | |
|
304 | 303 | if self.xmin is None and self.xmax is None: |
|
305 | 304 | self.xmin, self.xmax = self.getTimeLim(x1, tmin, tmax, timerange) |
|
306 | 305 | |
|
307 | 306 | if timerange != None: |
|
308 | 307 | self.timerange = timerange |
|
309 | 308 | else: |
|
310 | 309 | self.timerange = self.xmax - self.xmin |
|
311 | 310 | |
|
312 | 311 | self.FTP_WEI = ftp_wei |
|
313 | 312 | self.EXP_CODE = exp_code |
|
314 | 313 | self.SUB_EXP_CODE = sub_exp_code |
|
315 | 314 | self.PLOT_POS = plot_pos |
|
316 | 315 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
317 | 316 | self.firstdate = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
318 | 317 | self.isConfig = True |
|
319 | 318 | update_figfile = True |
|
320 | 319 | |
|
321 | 320 | self.setWinTitle(title) |
|
322 | 321 | |
|
323 | 322 | i = 0 |
|
324 | 323 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
325 | 324 | |
|
326 | 325 | axes = self.axesList[i*self.__nsubplots] |
|
327 | 326 | nevents = axes.x_buffer.shape[0] + x.shape[0] |
|
328 | 327 | title = "Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n" %(self.firstdate,str_datetime,nevents) |
|
329 | 328 | axes.polar(x, y, |
|
330 | 329 | title=title, xlabel=xlabel, ylabel=ylabel, |
|
331 | 330 | ticksize=9, cblabel='') |
|
332 | 331 | |
|
333 | 332 | self.draw() |
|
334 | 333 | |
|
335 | 334 | self.save(figpath=figpath, |
|
336 | 335 | figfile=figfile, |
|
337 | 336 | save=save, |
|
338 | 337 | ftp=ftp, |
|
339 | 338 | wr_period=wr_period, |
|
340 | 339 | thisDatetime=thisDatetime, |
|
341 | 340 | update_figfile=update_figfile) |
|
342 | 341 | |
|
343 | 342 | if dataOut.ltctime >= self.xmax: |
|
344 | 343 | self.isConfigmagwr = wr_period |
|
345 | 344 | self.isConfig = False |
|
346 | 345 | update_figfile = True |
|
347 | 346 | axes.__firsttime = True |
|
348 | 347 | self.xmin += self.timerange |
|
349 | 348 | self.xmax += self.timerange |
|
350 | 349 | |
|
351 | 350 | |
|
352 | 351 | |
|
353 | 352 | |
|
354 | 353 | class WindProfilerPlot(Figure): |
|
355 | 354 | |
|
356 | 355 | __isConfig = None |
|
357 | 356 | __nsubplots = None |
|
358 | 357 | |
|
359 | 358 | WIDTHPROF = None |
|
360 | 359 | HEIGHTPROF = None |
|
361 | 360 | PREFIX = 'wind' |
|
362 | 361 | |
|
363 | 362 | def __init__(self, **kwargs): |
|
364 | 363 | Figure.__init__(self, **kwargs) |
|
365 | 364 | self.timerange = None |
|
366 | 365 | self.isConfig = False |
|
367 | 366 | self.__nsubplots = 1 |
|
368 | 367 | |
|
369 | 368 | self.WIDTH = 800 |
|
370 | 369 | self.HEIGHT = 300 |
|
371 | 370 | self.WIDTHPROF = 120 |
|
372 | 371 | self.HEIGHTPROF = 0 |
|
373 | 372 | self.counter_imagwr = 0 |
|
374 | 373 | |
|
375 | 374 | self.PLOT_CODE = WIND_CODE |
|
376 | 375 | |
|
377 | 376 | self.FTP_WEI = None |
|
378 | 377 | self.EXP_CODE = None |
|
379 | 378 | self.SUB_EXP_CODE = None |
|
380 | 379 | self.PLOT_POS = None |
|
381 | 380 | self.tmin = None |
|
382 | 381 | self.tmax = None |
|
383 | 382 | |
|
384 | 383 | self.xmin = None |
|
385 | 384 | self.xmax = None |
|
386 | 385 | |
|
387 | 386 | self.figfile = None |
|
388 | 387 | |
|
389 | 388 | def getSubplots(self): |
|
390 | 389 | |
|
391 | 390 | ncol = 1 |
|
392 | 391 | nrow = self.nplots |
|
393 | 392 | |
|
394 | 393 | return nrow, ncol |
|
395 | 394 | |
|
396 | 395 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
397 | 396 | |
|
398 | 397 | self.__showprofile = showprofile |
|
399 | 398 | self.nplots = nplots |
|
400 | 399 | |
|
401 | 400 | ncolspan = 1 |
|
402 | 401 | colspan = 1 |
|
403 | 402 | |
|
404 | 403 | self.createFigure(id = id, |
|
405 | 404 | wintitle = wintitle, |
|
406 | 405 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
407 | 406 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
408 | 407 | show=show) |
|
409 | 408 | |
|
410 | 409 | nrow, ncol = self.getSubplots() |
|
411 | 410 | |
|
412 | 411 | counter = 0 |
|
413 | 412 | for y in range(nrow): |
|
414 | 413 | if counter >= self.nplots: |
|
415 | 414 | break |
|
416 | 415 | |
|
417 | 416 | self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1) |
|
418 | 417 | counter += 1 |
|
419 | 418 | |
|
420 | 419 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='False', |
|
421 | 420 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
422 | 421 | zmax_ver = None, zmin_ver = None, SNRmin = None, SNRmax = None, |
|
423 | 422 | timerange=None, SNRthresh = None, |
|
424 | 423 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, |
|
425 | 424 | server=None, folder=None, username=None, password=None, |
|
426 | 425 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
427 | 426 | """ |
|
428 | 427 | |
|
429 | 428 | Input: |
|
430 | 429 | dataOut : |
|
431 | 430 | id : |
|
432 | 431 | wintitle : |
|
433 | 432 | channelList : |
|
434 | 433 | showProfile : |
|
435 | 434 | xmin : None, |
|
436 | 435 | xmax : None, |
|
437 | 436 | ymin : None, |
|
438 | 437 | ymax : None, |
|
439 | 438 | zmin : None, |
|
440 | 439 | zmax : None |
|
441 | 440 | """ |
|
442 | 441 | |
|
443 | 442 | # if timerange is not None: |
|
444 | 443 | # self.timerange = timerange |
|
445 | 444 | # |
|
446 | 445 | # tmin = None |
|
447 | 446 | # tmax = None |
|
448 | 447 | |
|
449 | 448 | |
|
450 | 449 | x = dataOut.getTimeRange1(dataOut.outputInterval) |
|
451 | 450 | y = dataOut.heightList |
|
452 | 451 | z = dataOut.data_output.copy() |
|
453 | 452 | nplots = z.shape[0] #Number of wind dimensions estimated |
|
454 | 453 | nplotsw = nplots |
|
455 | 454 | |
|
456 | 455 | |
|
457 | 456 | #If there is a SNR function defined |
|
458 | 457 | if dataOut.data_SNR is not None: |
|
459 | 458 | nplots += 1 |
|
460 | 459 | SNR = dataOut.data_SNR |
|
461 | 460 | SNRavg = numpy.average(SNR, axis=0) |
|
462 | 461 | |
|
463 | 462 | SNRdB = 10*numpy.log10(SNR) |
|
464 | 463 | SNRavgdB = 10*numpy.log10(SNRavg) |
|
465 | 464 | |
|
466 | 465 | if SNRthresh == None: SNRthresh = -5.0 |
|
467 | 466 | ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0] |
|
468 | 467 | |
|
469 | 468 | for i in range(nplotsw): |
|
470 | 469 | z[i,ind] = numpy.nan |
|
471 | 470 | |
|
472 | 471 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime) |
|
473 | 472 | #thisDatetime = datetime.datetime.now() |
|
474 | 473 | title = wintitle + "Wind" |
|
475 | 474 | xlabel = "" |
|
476 | 475 | ylabel = "Height (km)" |
|
477 | 476 | update_figfile = False |
|
478 | 477 | |
|
479 | 478 | if not self.isConfig: |
|
480 | 479 | |
|
481 | 480 | self.setup(id=id, |
|
482 | 481 | nplots=nplots, |
|
483 | 482 | wintitle=wintitle, |
|
484 | 483 | showprofile=showprofile, |
|
485 | 484 | show=show) |
|
486 | 485 | |
|
487 | 486 | if timerange is not None: |
|
488 | 487 | self.timerange = timerange |
|
489 | 488 | |
|
490 | 489 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
491 | 490 | |
|
492 | 491 | if ymin == None: ymin = numpy.nanmin(y) |
|
493 | 492 | if ymax == None: ymax = numpy.nanmax(y) |
|
494 | 493 | |
|
495 | 494 | if zmax == None: zmax = numpy.nanmax(abs(z[range(2),:])) |
|
496 | 495 | #if numpy.isnan(zmax): zmax = 50 |
|
497 | 496 | if zmin == None: zmin = -zmax |
|
498 | 497 | |
|
499 | 498 | if nplotsw == 3: |
|
500 | 499 | if zmax_ver == None: zmax_ver = numpy.nanmax(abs(z[2,:])) |
|
501 | 500 | if zmin_ver == None: zmin_ver = -zmax_ver |
|
502 | 501 | |
|
503 | 502 | if dataOut.data_SNR is not None: |
|
504 | 503 | if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB) |
|
505 | 504 | if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB) |
|
506 | 505 | |
|
507 | 506 | |
|
508 | 507 | self.FTP_WEI = ftp_wei |
|
509 | 508 | self.EXP_CODE = exp_code |
|
510 | 509 | self.SUB_EXP_CODE = sub_exp_code |
|
511 | 510 | self.PLOT_POS = plot_pos |
|
512 | 511 | |
|
513 | 512 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
514 | 513 | self.isConfig = True |
|
515 | 514 | self.figfile = figfile |
|
516 | 515 | update_figfile = True |
|
517 | 516 | |
|
518 | 517 | self.setWinTitle(title) |
|
519 | 518 | |
|
520 | 519 | if ((self.xmax - x[1]) < (x[1]-x[0])): |
|
521 | 520 | x[1] = self.xmax |
|
522 | 521 | |
|
523 | 522 | strWind = ['Zonal', 'Meridional', 'Vertical'] |
|
524 | 523 | strCb = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)'] |
|
525 | 524 | zmaxVector = [zmax, zmax, zmax_ver] |
|
526 | 525 | zminVector = [zmin, zmin, zmin_ver] |
|
527 | 526 | windFactor = [1,1,100] |
|
528 | 527 | |
|
529 | 528 | for i in range(nplotsw): |
|
530 | 529 | |
|
531 | 530 | title = "%s Wind: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
532 | 531 | axes = self.axesList[i*self.__nsubplots] |
|
533 | 532 | |
|
534 | 533 | z1 = z[i,:].reshape((1,-1))*windFactor[i] |
|
535 | 534 | #z1=numpy.ma.masked_where(z1==0.,z1) |
|
536 | 535 | |
|
537 | 536 | axes.pcolorbuffer(x, y, z1, |
|
538 | 537 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i], |
|
539 | 538 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
540 | 539 | ticksize=9, cblabel=strCb[i], cbsize="1%", colormap="seismic" ) |
|
541 | 540 | |
|
542 | 541 | if dataOut.data_SNR is not None: |
|
543 | 542 | i += 1 |
|
544 | 543 | title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
545 | 544 | axes = self.axesList[i*self.__nsubplots] |
|
546 | 545 | SNRavgdB = SNRavgdB.reshape((1,-1)) |
|
547 | 546 | axes.pcolorbuffer(x, y, SNRavgdB, |
|
548 | 547 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax, |
|
549 | 548 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
550 | 549 | ticksize=9, cblabel='', cbsize="1%", colormap="jet") |
|
551 | 550 | |
|
552 | 551 | self.draw() |
|
553 | 552 | |
|
554 | 553 | self.save(figpath=figpath, |
|
555 | 554 | figfile=figfile, |
|
556 | 555 | save=save, |
|
557 | 556 | ftp=ftp, |
|
558 | 557 | wr_period=wr_period, |
|
559 | 558 | thisDatetime=thisDatetime, |
|
560 | 559 | update_figfile=update_figfile) |
|
561 | 560 | |
|
562 | 561 | if dataOut.ltctime + dataOut.outputInterval >= self.xmax: |
|
563 | 562 | self.counter_imagwr = wr_period |
|
564 | 563 | self.isConfig = False |
|
565 | 564 | update_figfile = True |
|
566 | 565 | |
|
567 | 566 | |
|
568 | 567 | class ParametersPlot(Figure): |
|
569 | 568 | |
|
570 | 569 | __isConfig = None |
|
571 | 570 | __nsubplots = None |
|
572 | 571 | |
|
573 | 572 | WIDTHPROF = None |
|
574 | 573 | HEIGHTPROF = None |
|
575 | 574 | PREFIX = 'param' |
|
576 | 575 | |
|
577 | 576 | nplots = None |
|
578 | 577 | nchan = None |
|
579 | 578 | |
|
580 | 579 | def __init__(self, **kwargs): |
|
581 | 580 | Figure.__init__(self, **kwargs) |
|
582 | 581 | self.timerange = None |
|
583 | 582 | self.isConfig = False |
|
584 | 583 | self.__nsubplots = 1 |
|
585 | 584 | |
|
586 | 585 | self.WIDTH = 800 |
|
587 | 586 | self.HEIGHT = 180 |
|
588 | 587 | self.WIDTHPROF = 120 |
|
589 | 588 | self.HEIGHTPROF = 0 |
|
590 | 589 | self.counter_imagwr = 0 |
|
591 | 590 | |
|
592 | 591 | self.PLOT_CODE = RTI_CODE |
|
593 | 592 | |
|
594 | 593 | self.FTP_WEI = None |
|
595 | 594 | self.EXP_CODE = None |
|
596 | 595 | self.SUB_EXP_CODE = None |
|
597 | 596 | self.PLOT_POS = None |
|
598 | 597 | self.tmin = None |
|
599 | 598 | self.tmax = None |
|
600 | 599 | |
|
601 | 600 | self.xmin = None |
|
602 | 601 | self.xmax = None |
|
603 | 602 | |
|
604 | 603 | self.figfile = None |
|
605 | 604 | |
|
606 | 605 | def getSubplots(self): |
|
607 | 606 | |
|
608 | 607 | ncol = 1 |
|
609 | 608 | nrow = self.nplots |
|
610 | 609 | |
|
611 | 610 | return nrow, ncol |
|
612 | 611 | |
|
613 | 612 | def setup(self, id, nplots, wintitle, show=True): |
|
614 | 613 | |
|
615 | 614 | self.nplots = nplots |
|
616 | 615 | |
|
617 | 616 | ncolspan = 1 |
|
618 | 617 | colspan = 1 |
|
619 | 618 | |
|
620 | 619 | self.createFigure(id = id, |
|
621 | 620 | wintitle = wintitle, |
|
622 | 621 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
623 | 622 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
624 | 623 | show=show) |
|
625 | 624 | |
|
626 | 625 | nrow, ncol = self.getSubplots() |
|
627 | 626 | |
|
628 | 627 | counter = 0 |
|
629 | 628 | for y in range(nrow): |
|
630 | 629 | for x in range(ncol): |
|
631 | 630 | |
|
632 | 631 | if counter >= self.nplots: |
|
633 | 632 | break |
|
634 | 633 | |
|
635 | 634 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
636 | 635 | |
|
637 | 636 | counter += 1 |
|
638 | 637 | |
|
639 | 638 | def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap=True, |
|
640 | 639 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, timerange=None, |
|
641 | 640 | showSNR=False, SNRthresh = -numpy.inf, SNRmin=None, SNRmax=None, |
|
642 | 641 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, |
|
643 | 642 | server=None, folder=None, username=None, password=None, |
|
644 | 643 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
645 | 644 | """ |
|
646 | 645 | |
|
647 | 646 | Input: |
|
648 | 647 | dataOut : |
|
649 | 648 | id : |
|
650 | 649 | wintitle : |
|
651 | 650 | channelList : |
|
652 | 651 | showProfile : |
|
653 | 652 | xmin : None, |
|
654 | 653 | xmax : None, |
|
655 | 654 | ymin : None, |
|
656 | 655 | ymax : None, |
|
657 | 656 | zmin : None, |
|
658 | 657 | zmax : None |
|
659 | 658 | """ |
|
660 | 659 | |
|
661 | 660 | if colormap: |
|
662 | 661 | colormap="jet" |
|
663 | 662 | else: |
|
664 | 663 | colormap="RdBu_r" |
|
665 | 664 | |
|
666 | 665 | if not isTimeInHourRange(dataOut.datatime, xmin, xmax): |
|
667 | 666 | return |
|
668 | 667 | |
|
669 | 668 | if channelList == None: |
|
670 | 669 | channelIndexList = range(dataOut.data_param.shape[0]) |
|
671 | 670 | else: |
|
672 | 671 | channelIndexList = [] |
|
673 | 672 | for channel in channelList: |
|
674 | 673 | if channel not in dataOut.channelList: |
|
675 | 674 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
676 | 675 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
677 | 676 | |
|
678 | 677 | x = dataOut.getTimeRange1(dataOut.paramInterval) |
|
679 | 678 | y = dataOut.getHeiRange() |
|
680 | 679 | |
|
681 | 680 | if dataOut.data_param.ndim == 3: |
|
682 | 681 | z = dataOut.data_param[channelIndexList,paramIndex,:] |
|
683 | 682 | else: |
|
684 | 683 | z = dataOut.data_param[channelIndexList,:] |
|
685 | 684 | |
|
686 | 685 | if showSNR: |
|
687 | 686 | #SNR data |
|
688 | 687 | SNRarray = dataOut.data_SNR[channelIndexList,:] |
|
689 | 688 | SNRdB = 10*numpy.log10(SNRarray) |
|
690 | 689 | ind = numpy.where(SNRdB < SNRthresh) |
|
691 | 690 | z[ind] = numpy.nan |
|
692 | 691 | |
|
693 | 692 | thisDatetime = dataOut.datatime |
|
694 | 693 | # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
695 | 694 | title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
696 | 695 | xlabel = "" |
|
697 | 696 | ylabel = "Range (Km)" |
|
698 | 697 | |
|
699 | 698 | update_figfile = False |
|
700 | 699 | |
|
701 | 700 | if not self.isConfig: |
|
702 | 701 | |
|
703 | 702 | nchan = len(channelIndexList) |
|
704 | 703 | self.nchan = nchan |
|
705 | 704 | self.plotFact = 1 |
|
706 | 705 | nplots = nchan |
|
707 | 706 | |
|
708 | 707 | if showSNR: |
|
709 | 708 | nplots = nchan*2 |
|
710 | 709 | self.plotFact = 2 |
|
711 | 710 | if SNRmin == None: SNRmin = numpy.nanmin(SNRdB) |
|
712 | 711 | if SNRmax == None: SNRmax = numpy.nanmax(SNRdB) |
|
713 | 712 | |
|
714 | 713 | self.setup(id=id, |
|
715 | 714 | nplots=nplots, |
|
716 | 715 | wintitle=wintitle, |
|
717 | 716 | show=show) |
|
718 | 717 | |
|
719 | 718 | if timerange != None: |
|
720 | 719 | self.timerange = timerange |
|
721 | 720 | |
|
722 | 721 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
723 | 722 | |
|
724 | 723 | if ymin == None: ymin = numpy.nanmin(y) |
|
725 | 724 | if ymax == None: ymax = numpy.nanmax(y) |
|
726 | 725 | if zmin == None: zmin = numpy.nanmin(z) |
|
727 | 726 | if zmax == None: zmax = numpy.nanmax(z) |
|
728 | 727 | |
|
729 | 728 | self.FTP_WEI = ftp_wei |
|
730 | 729 | self.EXP_CODE = exp_code |
|
731 | 730 | self.SUB_EXP_CODE = sub_exp_code |
|
732 | 731 | self.PLOT_POS = plot_pos |
|
733 | 732 | |
|
734 | 733 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
735 | 734 | self.isConfig = True |
|
736 | 735 | self.figfile = figfile |
|
737 | 736 | update_figfile = True |
|
738 | 737 | |
|
739 | 738 | self.setWinTitle(title) |
|
740 | 739 | |
|
741 | 740 | for i in range(self.nchan): |
|
742 | 741 | index = channelIndexList[i] |
|
743 | 742 | title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
744 | 743 | axes = self.axesList[i*self.plotFact] |
|
745 | 744 | z1 = z[i,:].reshape((1,-1)) |
|
746 | 745 | axes.pcolorbuffer(x, y, z1, |
|
747 | 746 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
748 | 747 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
749 | 748 | ticksize=9, cblabel='', cbsize="1%",colormap=colormap) |
|
750 | 749 | |
|
751 | 750 | if showSNR: |
|
752 | 751 | title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
753 | 752 | axes = self.axesList[i*self.plotFact + 1] |
|
754 | 753 | SNRdB1 = SNRdB[i,:].reshape((1,-1)) |
|
755 | 754 | axes.pcolorbuffer(x, y, SNRdB1, |
|
756 | 755 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax, |
|
757 | 756 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
758 | 757 | ticksize=9, cblabel='', cbsize="1%",colormap='jet') |
|
759 | 758 | |
|
760 | 759 | |
|
761 | 760 | self.draw() |
|
762 | 761 | |
|
763 | 762 | if dataOut.ltctime >= self.xmax: |
|
764 | 763 | self.counter_imagwr = wr_period |
|
765 | 764 | self.isConfig = False |
|
766 | 765 | update_figfile = True |
|
767 | 766 | |
|
768 | 767 | self.save(figpath=figpath, |
|
769 | 768 | figfile=figfile, |
|
770 | 769 | save=save, |
|
771 | 770 | ftp=ftp, |
|
772 | 771 | wr_period=wr_period, |
|
773 | 772 | thisDatetime=thisDatetime, |
|
774 | 773 | update_figfile=update_figfile) |
|
775 | 774 | |
|
776 | 775 | |
|
777 | 776 | |
|
778 | 777 | class Parameters1Plot(Figure): |
|
779 | 778 | |
|
780 | 779 | __isConfig = None |
|
781 | 780 | __nsubplots = None |
|
782 | 781 | |
|
783 | 782 | WIDTHPROF = None |
|
784 | 783 | HEIGHTPROF = None |
|
785 | 784 | PREFIX = 'prm' |
|
786 | 785 | |
|
787 | 786 | def __init__(self, **kwargs): |
|
788 | 787 | Figure.__init__(self, **kwargs) |
|
789 | 788 | self.timerange = 2*60*60 |
|
790 | 789 | self.isConfig = False |
|
791 | 790 | self.__nsubplots = 1 |
|
792 | 791 | |
|
793 | 792 | self.WIDTH = 800 |
|
794 | 793 | self.HEIGHT = 180 |
|
795 | 794 | self.WIDTHPROF = 120 |
|
796 | 795 | self.HEIGHTPROF = 0 |
|
797 | 796 | self.counter_imagwr = 0 |
|
798 | 797 | |
|
799 | 798 | self.PLOT_CODE = PARMS_CODE |
|
800 | 799 | |
|
801 | 800 | self.FTP_WEI = None |
|
802 | 801 | self.EXP_CODE = None |
|
803 | 802 | self.SUB_EXP_CODE = None |
|
804 | 803 | self.PLOT_POS = None |
|
805 | 804 | self.tmin = None |
|
806 | 805 | self.tmax = None |
|
807 | 806 | |
|
808 | 807 | self.xmin = None |
|
809 | 808 | self.xmax = None |
|
810 | 809 | |
|
811 | 810 | self.figfile = None |
|
812 | 811 | |
|
813 | 812 | def getSubplots(self): |
|
814 | 813 | |
|
815 | 814 | ncol = 1 |
|
816 | 815 | nrow = self.nplots |
|
817 | 816 | |
|
818 | 817 | return nrow, ncol |
|
819 | 818 | |
|
820 | 819 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
821 | 820 | |
|
822 | 821 | self.__showprofile = showprofile |
|
823 | 822 | self.nplots = nplots |
|
824 | 823 | |
|
825 | 824 | ncolspan = 1 |
|
826 | 825 | colspan = 1 |
|
827 | 826 | |
|
828 | 827 | self.createFigure(id = id, |
|
829 | 828 | wintitle = wintitle, |
|
830 | 829 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
831 | 830 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
832 | 831 | show=show) |
|
833 | 832 | |
|
834 | 833 | nrow, ncol = self.getSubplots() |
|
835 | 834 | |
|
836 | 835 | counter = 0 |
|
837 | 836 | for y in range(nrow): |
|
838 | 837 | for x in range(ncol): |
|
839 | 838 | |
|
840 | 839 | if counter >= self.nplots: |
|
841 | 840 | break |
|
842 | 841 | |
|
843 | 842 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
844 | 843 | |
|
845 | 844 | if showprofile: |
|
846 | 845 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
847 | 846 | |
|
848 | 847 | counter += 1 |
|
849 | 848 | |
|
850 | 849 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False, |
|
851 | 850 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,timerange=None, |
|
852 | 851 | parameterIndex = None, onlyPositive = False, |
|
853 | 852 | SNRthresh = -numpy.inf, SNR = True, SNRmin = None, SNRmax = None, onlySNR = False, |
|
854 | 853 | DOP = True, |
|
855 | 854 | zlabel = "", parameterName = "", parameterObject = "data_param", |
|
856 | 855 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, |
|
857 | 856 | server=None, folder=None, username=None, password=None, |
|
858 | 857 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
859 | 858 | #print inspect.getargspec(self.run).args |
|
860 | 859 | """ |
|
861 | 860 | |
|
862 | 861 | Input: |
|
863 | 862 | dataOut : |
|
864 | 863 | id : |
|
865 | 864 | wintitle : |
|
866 | 865 | channelList : |
|
867 | 866 | showProfile : |
|
868 | 867 | xmin : None, |
|
869 | 868 | xmax : None, |
|
870 | 869 | ymin : None, |
|
871 | 870 | ymax : None, |
|
872 | 871 | zmin : None, |
|
873 | 872 | zmax : None |
|
874 | 873 | """ |
|
875 | 874 | |
|
876 | 875 | data_param = getattr(dataOut, parameterObject) |
|
877 | 876 | |
|
878 | 877 | if channelList == None: |
|
879 | 878 | channelIndexList = numpy.arange(data_param.shape[0]) |
|
880 | 879 | else: |
|
881 | 880 | channelIndexList = numpy.array(channelList) |
|
882 | 881 | |
|
883 | 882 | nchan = len(channelIndexList) #Number of channels being plotted |
|
884 | 883 | |
|
885 | 884 | if nchan < 1: |
|
886 | 885 | return |
|
887 | 886 | |
|
888 | 887 | nGraphsByChannel = 0 |
|
889 | 888 | |
|
890 | 889 | if SNR: |
|
891 | 890 | nGraphsByChannel += 1 |
|
892 | 891 | if DOP: |
|
893 | 892 | nGraphsByChannel += 1 |
|
894 | 893 | |
|
895 | 894 | if nGraphsByChannel < 1: |
|
896 | 895 | return |
|
897 | 896 | |
|
898 | 897 | nplots = nGraphsByChannel*nchan |
|
899 | 898 | |
|
900 | 899 | if timerange is not None: |
|
901 | 900 | self.timerange = timerange |
|
902 | 901 | |
|
903 | 902 | #tmin = None |
|
904 | 903 | #tmax = None |
|
905 | 904 | if parameterIndex == None: |
|
906 | 905 | parameterIndex = 1 |
|
907 | 906 | |
|
908 | 907 | x = dataOut.getTimeRange1(dataOut.paramInterval) |
|
909 | 908 | y = dataOut.heightList |
|
910 | 909 | z = data_param[channelIndexList,parameterIndex,:].copy() |
|
911 | 910 | |
|
912 | 911 | zRange = dataOut.abscissaList |
|
913 | 912 | # nChannels = z.shape[0] #Number of wind dimensions estimated |
|
914 | 913 | # thisDatetime = dataOut.datatime |
|
915 | 914 | |
|
916 | 915 | if dataOut.data_SNR is not None: |
|
917 | 916 | SNRarray = dataOut.data_SNR[channelIndexList,:] |
|
918 | 917 | SNRdB = 10*numpy.log10(SNRarray) |
|
919 | 918 | # SNRavgdB = 10*numpy.log10(SNRavg) |
|
920 | 919 | ind = numpy.where(SNRdB < 10**(SNRthresh/10)) |
|
921 | 920 | z[ind] = numpy.nan |
|
922 | 921 | |
|
923 | 922 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
924 | 923 | title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
925 | 924 | xlabel = "" |
|
926 | 925 | ylabel = "Range (Km)" |
|
927 | 926 | |
|
928 | 927 | if (SNR and not onlySNR): nplots = 2*nplots |
|
929 | 928 | |
|
930 | 929 | if onlyPositive: |
|
931 | 930 | colormap = "jet" |
|
932 | 931 | zmin = 0 |
|
933 | 932 | else: colormap = "RdBu_r" |
|
934 | 933 | |
|
935 | 934 | if not self.isConfig: |
|
936 | 935 | |
|
937 | 936 | self.setup(id=id, |
|
938 | 937 | nplots=nplots, |
|
939 | 938 | wintitle=wintitle, |
|
940 | 939 | showprofile=showprofile, |
|
941 | 940 | show=show) |
|
942 | 941 | |
|
943 | 942 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
944 | 943 | |
|
945 | 944 | if ymin == None: ymin = numpy.nanmin(y) |
|
946 | 945 | if ymax == None: ymax = numpy.nanmax(y) |
|
947 | 946 | if zmin == None: zmin = numpy.nanmin(zRange) |
|
948 | 947 | if zmax == None: zmax = numpy.nanmax(zRange) |
|
949 | 948 | |
|
950 | 949 | if SNR: |
|
951 | 950 | if SNRmin == None: SNRmin = numpy.nanmin(SNRdB) |
|
952 | 951 | if SNRmax == None: SNRmax = numpy.nanmax(SNRdB) |
|
953 | 952 | |
|
954 | 953 | self.FTP_WEI = ftp_wei |
|
955 | 954 | self.EXP_CODE = exp_code |
|
956 | 955 | self.SUB_EXP_CODE = sub_exp_code |
|
957 | 956 | self.PLOT_POS = plot_pos |
|
958 | 957 | |
|
959 | 958 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
960 | 959 | self.isConfig = True |
|
961 | 960 | self.figfile = figfile |
|
962 | 961 | |
|
963 | 962 | self.setWinTitle(title) |
|
964 | 963 | |
|
965 | 964 | if ((self.xmax - x[1]) < (x[1]-x[0])): |
|
966 | 965 | x[1] = self.xmax |
|
967 | 966 | |
|
968 | 967 | for i in range(nchan): |
|
969 | 968 | |
|
970 | 969 | if (SNR and not onlySNR): j = 2*i |
|
971 | 970 | else: j = i |
|
972 | 971 | |
|
973 | 972 | j = nGraphsByChannel*i |
|
974 | 973 | |
|
975 | 974 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
976 | 975 | title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith) |
|
977 | 976 | |
|
978 | 977 | if not onlySNR: |
|
979 | 978 | axes = self.axesList[j*self.__nsubplots] |
|
980 | 979 | z1 = z[i,:].reshape((1,-1)) |
|
981 | 980 | axes.pcolorbuffer(x, y, z1, |
|
982 | 981 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
983 | 982 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap, |
|
984 | 983 | ticksize=9, cblabel=zlabel, cbsize="1%") |
|
985 | 984 | |
|
986 | 985 | if DOP: |
|
987 | 986 | title = "%s Channel %d: %s" %(parameterName, channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
988 | 987 | |
|
989 | 988 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
990 | 989 | title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith) |
|
991 | 990 | axes = self.axesList[j] |
|
992 | 991 | z1 = z[i,:].reshape((1,-1)) |
|
993 | 992 | axes.pcolorbuffer(x, y, z1, |
|
994 | 993 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
995 | 994 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap, |
|
996 | 995 | ticksize=9, cblabel=zlabel, cbsize="1%") |
|
997 | 996 | |
|
998 | 997 | if SNR: |
|
999 | 998 | title = "Channel %d Signal Noise Ratio (SNR): %s" %(channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1000 | 999 | axes = self.axesList[(j)*self.__nsubplots] |
|
1001 | 1000 | if not onlySNR: |
|
1002 | 1001 | axes = self.axesList[(j + 1)*self.__nsubplots] |
|
1003 | 1002 | |
|
1004 | 1003 | axes = self.axesList[(j + nGraphsByChannel-1)] |
|
1005 | 1004 | |
|
1006 | 1005 | z1 = SNRdB[i,:].reshape((1,-1)) |
|
1007 | 1006 | axes.pcolorbuffer(x, y, z1, |
|
1008 | 1007 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax, |
|
1009 | 1008 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap="jet", |
|
1010 | 1009 | ticksize=9, cblabel=zlabel, cbsize="1%") |
|
1011 | 1010 | |
|
1012 | 1011 | |
|
1013 | 1012 | |
|
1014 | 1013 | self.draw() |
|
1015 | 1014 | |
|
1016 | 1015 | if x[1] >= self.axesList[0].xmax: |
|
1017 | 1016 | self.counter_imagwr = wr_period |
|
1018 | 1017 | self.isConfig = False |
|
1019 | 1018 | self.figfile = None |
|
1020 | 1019 | |
|
1021 | 1020 | self.save(figpath=figpath, |
|
1022 | 1021 | figfile=figfile, |
|
1023 | 1022 | save=save, |
|
1024 | 1023 | ftp=ftp, |
|
1025 | 1024 | wr_period=wr_period, |
|
1026 | 1025 | thisDatetime=thisDatetime, |
|
1027 | 1026 | update_figfile=False) |
|
1028 | 1027 | |
|
1029 | 1028 | class SpectralFittingPlot(Figure): |
|
1030 | 1029 | |
|
1031 | 1030 | __isConfig = None |
|
1032 | 1031 | __nsubplots = None |
|
1033 | 1032 | |
|
1034 | 1033 | WIDTHPROF = None |
|
1035 | 1034 | HEIGHTPROF = None |
|
1036 | 1035 | PREFIX = 'prm' |
|
1037 | 1036 | |
|
1038 | 1037 | |
|
1039 | 1038 | N = None |
|
1040 | 1039 | ippSeconds = None |
|
1041 | 1040 | |
|
1042 | 1041 | def __init__(self, **kwargs): |
|
1043 | 1042 | Figure.__init__(self, **kwargs) |
|
1044 | 1043 | self.isConfig = False |
|
1045 | 1044 | self.__nsubplots = 1 |
|
1046 | 1045 | |
|
1047 | 1046 | self.PLOT_CODE = SPECFIT_CODE |
|
1048 | 1047 | |
|
1049 | 1048 | self.WIDTH = 450 |
|
1050 | 1049 | self.HEIGHT = 250 |
|
1051 | 1050 | self.WIDTHPROF = 0 |
|
1052 | 1051 | self.HEIGHTPROF = 0 |
|
1053 | 1052 | |
|
1054 | 1053 | def getSubplots(self): |
|
1055 | 1054 | |
|
1056 | 1055 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
1057 | 1056 | nrow = int(self.nplots*1./ncol + 0.9) |
|
1058 | 1057 | |
|
1059 | 1058 | return nrow, ncol |
|
1060 | 1059 | |
|
1061 | 1060 | def setup(self, id, nplots, wintitle, showprofile=False, show=True): |
|
1062 | 1061 | |
|
1063 | 1062 | showprofile = False |
|
1064 | 1063 | self.__showprofile = showprofile |
|
1065 | 1064 | self.nplots = nplots |
|
1066 | 1065 | |
|
1067 | 1066 | ncolspan = 5 |
|
1068 | 1067 | colspan = 4 |
|
1069 | 1068 | if showprofile: |
|
1070 | 1069 | ncolspan = 5 |
|
1071 | 1070 | colspan = 4 |
|
1072 | 1071 | self.__nsubplots = 2 |
|
1073 | 1072 | |
|
1074 | 1073 | self.createFigure(id = id, |
|
1075 | 1074 | wintitle = wintitle, |
|
1076 | 1075 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
1077 | 1076 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
1078 | 1077 | show=show) |
|
1079 | 1078 | |
|
1080 | 1079 | nrow, ncol = self.getSubplots() |
|
1081 | 1080 | |
|
1082 | 1081 | counter = 0 |
|
1083 | 1082 | for y in range(nrow): |
|
1084 | 1083 | for x in range(ncol): |
|
1085 | 1084 | |
|
1086 | 1085 | if counter >= self.nplots: |
|
1087 | 1086 | break |
|
1088 | 1087 | |
|
1089 | 1088 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
1090 | 1089 | |
|
1091 | 1090 | if showprofile: |
|
1092 | 1091 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
1093 | 1092 | |
|
1094 | 1093 | counter += 1 |
|
1095 | 1094 | |
|
1096 | 1095 | def run(self, dataOut, id, cutHeight=None, fit=False, wintitle="", channelList=None, showprofile=True, |
|
1097 | 1096 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
1098 | 1097 | save=False, figpath='./', figfile=None, show=True): |
|
1099 | 1098 | |
|
1100 | 1099 | """ |
|
1101 | 1100 | |
|
1102 | 1101 | Input: |
|
1103 | 1102 | dataOut : |
|
1104 | 1103 | id : |
|
1105 | 1104 | wintitle : |
|
1106 | 1105 | channelList : |
|
1107 | 1106 | showProfile : |
|
1108 | 1107 | xmin : None, |
|
1109 | 1108 | xmax : None, |
|
1110 | 1109 | zmin : None, |
|
1111 | 1110 | zmax : None |
|
1112 | 1111 | """ |
|
1113 | 1112 | |
|
1114 | 1113 | if cutHeight==None: |
|
1115 | 1114 | h=270 |
|
1116 | 1115 | heightindex = numpy.abs(cutHeight - dataOut.heightList).argmin() |
|
1117 | 1116 | cutHeight = dataOut.heightList[heightindex] |
|
1118 | 1117 | |
|
1119 | 1118 | factor = dataOut.normFactor |
|
1120 | 1119 | x = dataOut.abscissaList[:-1] |
|
1121 | 1120 | #y = dataOut.getHeiRange() |
|
1122 | 1121 | |
|
1123 | 1122 | z = dataOut.data_pre[:,:,heightindex]/factor |
|
1124 | 1123 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
1125 | 1124 | avg = numpy.average(z, axis=1) |
|
1126 | 1125 | listChannels = z.shape[0] |
|
1127 | 1126 | |
|
1128 | 1127 | #Reconstruct Function |
|
1129 | 1128 | if fit==True: |
|
1130 | 1129 | groupArray = dataOut.groupList |
|
1131 | 1130 | listChannels = groupArray.reshape((groupArray.size)) |
|
1132 | 1131 | listChannels.sort() |
|
1133 | 1132 | spcFitLine = numpy.zeros(z.shape) |
|
1134 | 1133 | constants = dataOut.constants |
|
1135 | 1134 | |
|
1136 | 1135 | nGroups = groupArray.shape[0] |
|
1137 | 1136 | nChannels = groupArray.shape[1] |
|
1138 | 1137 | nProfiles = z.shape[1] |
|
1139 | 1138 | |
|
1140 | 1139 | for f in range(nGroups): |
|
1141 | 1140 | groupChann = groupArray[f,:] |
|
1142 | 1141 | p = dataOut.data_param[f,:,heightindex] |
|
1143 | 1142 | # p = numpy.array([ 89.343967,0.14036615,0.17086219,18.89835291,1.58388365,1.55099167]) |
|
1144 | 1143 | fitLineAux = dataOut.library.modelFunction(p, constants)*nProfiles |
|
1145 | 1144 | fitLineAux = fitLineAux.reshape((nChannels,nProfiles)) |
|
1146 | 1145 | spcFitLine[groupChann,:] = fitLineAux |
|
1147 | 1146 | # spcFitLine = spcFitLine/factor |
|
1148 | 1147 | |
|
1149 | 1148 | z = z[listChannels,:] |
|
1150 | 1149 | spcFitLine = spcFitLine[listChannels,:] |
|
1151 | 1150 | spcFitLinedB = 10*numpy.log10(spcFitLine) |
|
1152 | 1151 | |
|
1153 | 1152 | zdB = 10*numpy.log10(z) |
|
1154 | 1153 | #thisDatetime = dataOut.datatime |
|
1155 | 1154 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
1156 | 1155 | title = wintitle + " Doppler Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
1157 | 1156 | xlabel = "Velocity (m/s)" |
|
1158 | 1157 | ylabel = "Spectrum" |
|
1159 | 1158 | |
|
1160 | 1159 | if not self.isConfig: |
|
1161 | 1160 | |
|
1162 | 1161 | nplots = listChannels.size |
|
1163 | 1162 | |
|
1164 | 1163 | self.setup(id=id, |
|
1165 | 1164 | nplots=nplots, |
|
1166 | 1165 | wintitle=wintitle, |
|
1167 | 1166 | showprofile=showprofile, |
|
1168 | 1167 | show=show) |
|
1169 | 1168 | |
|
1170 | 1169 | if xmin == None: xmin = numpy.nanmin(x) |
|
1171 | 1170 | if xmax == None: xmax = numpy.nanmax(x) |
|
1172 | 1171 | if ymin == None: ymin = numpy.nanmin(zdB) |
|
1173 | 1172 | if ymax == None: ymax = numpy.nanmax(zdB)+2 |
|
1174 | 1173 | |
|
1175 | 1174 | self.isConfig = True |
|
1176 | 1175 | |
|
1177 | 1176 | self.setWinTitle(title) |
|
1178 | 1177 | for i in range(self.nplots): |
|
1179 | 1178 | # title = "Channel %d: %4.2fdB" %(dataOut.channelList[i]+1, noisedB[i]) |
|
1180 | 1179 | title = "Height %4.1f km\nChannel %d:" %(cutHeight, listChannels[i]) |
|
1181 | 1180 | axes = self.axesList[i*self.__nsubplots] |
|
1182 | 1181 | if fit == False: |
|
1183 | 1182 | axes.pline(x, zdB[i,:], |
|
1184 | 1183 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
1185 | 1184 | xlabel=xlabel, ylabel=ylabel, title=title |
|
1186 | 1185 | ) |
|
1187 | 1186 | if fit == True: |
|
1188 | 1187 | fitline=spcFitLinedB[i,:] |
|
1189 | 1188 | y=numpy.vstack([zdB[i,:],fitline] ) |
|
1190 | 1189 | legendlabels=['Data','Fitting'] |
|
1191 | 1190 | axes.pmultilineyaxis(x, y, |
|
1192 | 1191 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
1193 | 1192 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
1194 | 1193 | legendlabels=legendlabels, marker=None, |
|
1195 | 1194 | linestyle='solid', grid='both') |
|
1196 | 1195 | |
|
1197 | 1196 | self.draw() |
|
1198 | 1197 | |
|
1199 | 1198 | self.save(figpath=figpath, |
|
1200 | 1199 | figfile=figfile, |
|
1201 | 1200 | save=save, |
|
1202 | 1201 | ftp=ftp, |
|
1203 | 1202 | wr_period=wr_period, |
|
1204 | 1203 | thisDatetime=thisDatetime) |
|
1205 | 1204 | |
|
1206 | 1205 | |
|
1207 | 1206 | class EWDriftsPlot(Figure): |
|
1208 | 1207 | |
|
1209 | 1208 | __isConfig = None |
|
1210 | 1209 | __nsubplots = None |
|
1211 | 1210 | |
|
1212 | 1211 | WIDTHPROF = None |
|
1213 | 1212 | HEIGHTPROF = None |
|
1214 | 1213 | PREFIX = 'drift' |
|
1215 | 1214 | |
|
1216 | 1215 | def __init__(self, **kwargs): |
|
1217 | 1216 | Figure.__init__(self, **kwargs) |
|
1218 | 1217 | self.timerange = 2*60*60 |
|
1219 | 1218 | self.isConfig = False |
|
1220 | 1219 | self.__nsubplots = 1 |
|
1221 | 1220 | |
|
1222 | 1221 | self.WIDTH = 800 |
|
1223 | 1222 | self.HEIGHT = 150 |
|
1224 | 1223 | self.WIDTHPROF = 120 |
|
1225 | 1224 | self.HEIGHTPROF = 0 |
|
1226 | 1225 | self.counter_imagwr = 0 |
|
1227 | 1226 | |
|
1228 | 1227 | self.PLOT_CODE = EWDRIFT_CODE |
|
1229 | 1228 | |
|
1230 | 1229 | self.FTP_WEI = None |
|
1231 | 1230 | self.EXP_CODE = None |
|
1232 | 1231 | self.SUB_EXP_CODE = None |
|
1233 | 1232 | self.PLOT_POS = None |
|
1234 | 1233 | self.tmin = None |
|
1235 | 1234 | self.tmax = None |
|
1236 | 1235 | |
|
1237 | 1236 | self.xmin = None |
|
1238 | 1237 | self.xmax = None |
|
1239 | 1238 | |
|
1240 | 1239 | self.figfile = None |
|
1241 | 1240 | |
|
1242 | 1241 | def getSubplots(self): |
|
1243 | 1242 | |
|
1244 | 1243 | ncol = 1 |
|
1245 | 1244 | nrow = self.nplots |
|
1246 | 1245 | |
|
1247 | 1246 | return nrow, ncol |
|
1248 | 1247 | |
|
1249 | 1248 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
1250 | 1249 | |
|
1251 | 1250 | self.__showprofile = showprofile |
|
1252 | 1251 | self.nplots = nplots |
|
1253 | 1252 | |
|
1254 | 1253 | ncolspan = 1 |
|
1255 | 1254 | colspan = 1 |
|
1256 | 1255 | |
|
1257 | 1256 | self.createFigure(id = id, |
|
1258 | 1257 | wintitle = wintitle, |
|
1259 | 1258 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
1260 | 1259 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
1261 | 1260 | show=show) |
|
1262 | 1261 | |
|
1263 | 1262 | nrow, ncol = self.getSubplots() |
|
1264 | 1263 | |
|
1265 | 1264 | counter = 0 |
|
1266 | 1265 | for y in range(nrow): |
|
1267 | 1266 | if counter >= self.nplots: |
|
1268 | 1267 | break |
|
1269 | 1268 | |
|
1270 | 1269 | self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1) |
|
1271 | 1270 | counter += 1 |
|
1272 | 1271 | |
|
1273 | 1272 | def run(self, dataOut, id, wintitle="", channelList=None, |
|
1274 | 1273 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
1275 | 1274 | zmaxVertical = None, zminVertical = None, zmaxZonal = None, zminZonal = None, |
|
1276 | 1275 | timerange=None, SNRthresh = -numpy.inf, SNRmin = None, SNRmax = None, SNR_1 = False, |
|
1277 | 1276 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, |
|
1278 | 1277 | server=None, folder=None, username=None, password=None, |
|
1279 | 1278 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
1280 | 1279 | """ |
|
1281 | 1280 | |
|
1282 | 1281 | Input: |
|
1283 | 1282 | dataOut : |
|
1284 | 1283 | id : |
|
1285 | 1284 | wintitle : |
|
1286 | 1285 | channelList : |
|
1287 | 1286 | showProfile : |
|
1288 | 1287 | xmin : None, |
|
1289 | 1288 | xmax : None, |
|
1290 | 1289 | ymin : None, |
|
1291 | 1290 | ymax : None, |
|
1292 | 1291 | zmin : None, |
|
1293 | 1292 | zmax : None |
|
1294 | 1293 | """ |
|
1295 | 1294 | |
|
1296 | 1295 | if timerange is not None: |
|
1297 | 1296 | self.timerange = timerange |
|
1298 | 1297 | |
|
1299 | 1298 | tmin = None |
|
1300 | 1299 | tmax = None |
|
1301 | 1300 | |
|
1302 | 1301 | x = dataOut.getTimeRange1(dataOut.outputInterval) |
|
1303 | 1302 | # y = dataOut.heightList |
|
1304 | 1303 | y = dataOut.heightList |
|
1305 | 1304 | |
|
1306 | 1305 | z = dataOut.data_output |
|
1307 | 1306 | nplots = z.shape[0] #Number of wind dimensions estimated |
|
1308 | 1307 | nplotsw = nplots |
|
1309 | 1308 | |
|
1310 | 1309 | #If there is a SNR function defined |
|
1311 | 1310 | if dataOut.data_SNR is not None: |
|
1312 | 1311 | nplots += 1 |
|
1313 | 1312 | SNR = dataOut.data_SNR |
|
1314 | 1313 | |
|
1315 | 1314 | if SNR_1: |
|
1316 | 1315 | SNR += 1 |
|
1317 | 1316 | |
|
1318 | 1317 | SNRavg = numpy.average(SNR, axis=0) |
|
1319 | 1318 | |
|
1320 | 1319 | SNRdB = 10*numpy.log10(SNR) |
|
1321 | 1320 | SNRavgdB = 10*numpy.log10(SNRavg) |
|
1322 | 1321 | |
|
1323 | 1322 | ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0] |
|
1324 | 1323 | |
|
1325 | 1324 | for i in range(nplotsw): |
|
1326 | 1325 | z[i,ind] = numpy.nan |
|
1327 | 1326 | |
|
1328 | 1327 | |
|
1329 | 1328 | showprofile = False |
|
1330 | 1329 | # thisDatetime = dataOut.datatime |
|
1331 | 1330 | thisDatetime = datetime.datetime.utcfromtimestamp(x[1]) |
|
1332 | 1331 | title = wintitle + " EW Drifts" |
|
1333 | 1332 | xlabel = "" |
|
1334 | 1333 | ylabel = "Height (Km)" |
|
1335 | 1334 | |
|
1336 | 1335 | if not self.isConfig: |
|
1337 | 1336 | |
|
1338 | 1337 | self.setup(id=id, |
|
1339 | 1338 | nplots=nplots, |
|
1340 | 1339 | wintitle=wintitle, |
|
1341 | 1340 | showprofile=showprofile, |
|
1342 | 1341 | show=show) |
|
1343 | 1342 | |
|
1344 | 1343 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
1345 | 1344 | |
|
1346 | 1345 | if ymin == None: ymin = numpy.nanmin(y) |
|
1347 | 1346 | if ymax == None: ymax = numpy.nanmax(y) |
|
1348 | 1347 | |
|
1349 | 1348 | if zmaxZonal == None: zmaxZonal = numpy.nanmax(abs(z[0,:])) |
|
1350 | 1349 | if zminZonal == None: zminZonal = -zmaxZonal |
|
1351 | 1350 | if zmaxVertical == None: zmaxVertical = numpy.nanmax(abs(z[1,:])) |
|
1352 | 1351 | if zminVertical == None: zminVertical = -zmaxVertical |
|
1353 | 1352 | |
|
1354 | 1353 | if dataOut.data_SNR is not None: |
|
1355 | 1354 | if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB) |
|
1356 | 1355 | if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB) |
|
1357 | 1356 | |
|
1358 | 1357 | self.FTP_WEI = ftp_wei |
|
1359 | 1358 | self.EXP_CODE = exp_code |
|
1360 | 1359 | self.SUB_EXP_CODE = sub_exp_code |
|
1361 | 1360 | self.PLOT_POS = plot_pos |
|
1362 | 1361 | |
|
1363 | 1362 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1364 | 1363 | self.isConfig = True |
|
1365 | 1364 | |
|
1366 | 1365 | |
|
1367 | 1366 | self.setWinTitle(title) |
|
1368 | 1367 | |
|
1369 | 1368 | if ((self.xmax - x[1]) < (x[1]-x[0])): |
|
1370 | 1369 | x[1] = self.xmax |
|
1371 | 1370 | |
|
1372 | 1371 | strWind = ['Zonal','Vertical'] |
|
1373 | 1372 | strCb = 'Velocity (m/s)' |
|
1374 | 1373 | zmaxVector = [zmaxZonal, zmaxVertical] |
|
1375 | 1374 | zminVector = [zminZonal, zminVertical] |
|
1376 | 1375 | |
|
1377 | 1376 | for i in range(nplotsw): |
|
1378 | 1377 | |
|
1379 | 1378 | title = "%s Drifts: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1380 | 1379 | axes = self.axesList[i*self.__nsubplots] |
|
1381 | 1380 | |
|
1382 | 1381 | z1 = z[i,:].reshape((1,-1)) |
|
1383 | 1382 | |
|
1384 | 1383 | axes.pcolorbuffer(x, y, z1, |
|
1385 | 1384 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i], |
|
1386 | 1385 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
1387 | 1386 | ticksize=9, cblabel=strCb, cbsize="1%", colormap="RdBu_r") |
|
1388 | 1387 | |
|
1389 | 1388 | if dataOut.data_SNR is not None: |
|
1390 | 1389 | i += 1 |
|
1391 | 1390 | if SNR_1: |
|
1392 | 1391 | title = "Signal Noise Ratio + 1 (SNR+1): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1393 | 1392 | else: |
|
1394 | 1393 | title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1395 | 1394 | axes = self.axesList[i*self.__nsubplots] |
|
1396 | 1395 | SNRavgdB = SNRavgdB.reshape((1,-1)) |
|
1397 | 1396 | |
|
1398 | 1397 | axes.pcolorbuffer(x, y, SNRavgdB, |
|
1399 | 1398 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax, |
|
1400 | 1399 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
1401 | 1400 | ticksize=9, cblabel='', cbsize="1%", colormap="jet") |
|
1402 | 1401 | |
|
1403 | 1402 | self.draw() |
|
1404 | 1403 | |
|
1405 | 1404 | if x[1] >= self.axesList[0].xmax: |
|
1406 | 1405 | self.counter_imagwr = wr_period |
|
1407 | 1406 | self.isConfig = False |
|
1408 | 1407 | self.figfile = None |
|
1409 | 1408 | |
|
1410 | 1409 | |
|
1411 | 1410 | |
|
1412 | 1411 | |
|
1413 | 1412 | class PhasePlot(Figure): |
|
1414 | 1413 | |
|
1415 | 1414 | __isConfig = None |
|
1416 | 1415 | __nsubplots = None |
|
1417 | 1416 | |
|
1418 | 1417 | PREFIX = 'mphase' |
|
1419 | 1418 | |
|
1419 | ||
|
1420 | 1420 | def __init__(self, **kwargs): |
|
1421 | 1421 | Figure.__init__(self, **kwargs) |
|
1422 | 1422 | self.timerange = 24*60*60 |
|
1423 | 1423 | self.isConfig = False |
|
1424 | 1424 | self.__nsubplots = 1 |
|
1425 | 1425 | self.counter_imagwr = 0 |
|
1426 | 1426 | self.WIDTH = 600 |
|
1427 | 1427 | self.HEIGHT = 300 |
|
1428 | 1428 | self.WIDTHPROF = 120 |
|
1429 | 1429 | self.HEIGHTPROF = 0 |
|
1430 | 1430 | self.xdata = None |
|
1431 | 1431 | self.ydata = None |
|
1432 | 1432 | |
|
1433 | 1433 | self.PLOT_CODE = MPHASE_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 | |
|
1440 | 1440 | |
|
1441 | 1441 | self.filename_phase = None |
|
1442 | 1442 | |
|
1443 | 1443 | self.figfile = None |
|
1444 | 1444 | |
|
1445 | 1445 | def getSubplots(self): |
|
1446 | 1446 | |
|
1447 | 1447 | ncol = 1 |
|
1448 | 1448 | nrow = 1 |
|
1449 | 1449 | |
|
1450 | 1450 | return nrow, ncol |
|
1451 | 1451 | |
|
1452 | 1452 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
1453 | 1453 | |
|
1454 | 1454 | self.__showprofile = showprofile |
|
1455 | 1455 | self.nplots = nplots |
|
1456 | 1456 | |
|
1457 | 1457 | ncolspan = 7 |
|
1458 | 1458 | colspan = 6 |
|
1459 | 1459 | self.__nsubplots = 2 |
|
1460 | 1460 | |
|
1461 | 1461 | self.createFigure(id = id, |
|
1462 | 1462 | wintitle = wintitle, |
|
1463 | 1463 | widthplot = self.WIDTH+self.WIDTHPROF, |
|
1464 | 1464 | heightplot = self.HEIGHT+self.HEIGHTPROF, |
|
1465 | 1465 | show=show) |
|
1466 | 1466 | |
|
1467 | 1467 | nrow, ncol = self.getSubplots() |
|
1468 | 1468 | |
|
1469 | 1469 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) |
|
1470 | 1470 | |
|
1471 | 1471 | |
|
1472 | 1472 | def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', |
|
1473 | 1473 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
1474 | 1474 | timerange=None, |
|
1475 | 1475 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
1476 | 1476 | server=None, folder=None, username=None, password=None, |
|
1477 | 1477 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
1478 | 1478 | |
|
1479 | 1479 | |
|
1480 | 1480 | tmin = None |
|
1481 | 1481 | tmax = None |
|
1482 | 1482 | x = dataOut.getTimeRange1(dataOut.outputInterval) |
|
1483 | 1483 | y = dataOut.getHeiRange() |
|
1484 | 1484 | |
|
1485 | 1485 | |
|
1486 | 1486 | #thisDatetime = dataOut.datatime |
|
1487 | 1487 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime) |
|
1488 | 1488 | title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
1489 | 1489 | xlabel = "Local Time" |
|
1490 | 1490 | ylabel = "Phase" |
|
1491 | 1491 | |
|
1492 | 1492 | |
|
1493 | 1493 | #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList))) |
|
1494 | 1494 | phase_beacon = dataOut.data_output |
|
1495 | 1495 | update_figfile = False |
|
1496 | 1496 | |
|
1497 | 1497 | if not self.isConfig: |
|
1498 | 1498 | |
|
1499 | 1499 | self.nplots = phase_beacon.size |
|
1500 | 1500 | |
|
1501 | 1501 | self.setup(id=id, |
|
1502 | 1502 | nplots=self.nplots, |
|
1503 | 1503 | wintitle=wintitle, |
|
1504 | 1504 | showprofile=showprofile, |
|
1505 | 1505 | show=show) |
|
1506 | 1506 | |
|
1507 | 1507 | if timerange is not None: |
|
1508 | 1508 | self.timerange = timerange |
|
1509 | 1509 | |
|
1510 | 1510 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
1511 | 1511 | |
|
1512 | 1512 | if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0 |
|
1513 | 1513 | if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0 |
|
1514 | 1514 | |
|
1515 | 1515 | self.FTP_WEI = ftp_wei |
|
1516 | 1516 | self.EXP_CODE = exp_code |
|
1517 | 1517 | self.SUB_EXP_CODE = sub_exp_code |
|
1518 | 1518 | self.PLOT_POS = plot_pos |
|
1519 | 1519 | |
|
1520 | 1520 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1521 | 1521 | self.isConfig = True |
|
1522 | 1522 | self.figfile = figfile |
|
1523 | 1523 | self.xdata = numpy.array([]) |
|
1524 | 1524 | self.ydata = numpy.array([]) |
|
1525 | 1525 | |
|
1526 | 1526 | #open file beacon phase |
|
1527 | 1527 | path = '%s%03d' %(self.PREFIX, self.id) |
|
1528 | 1528 | beacon_file = os.path.join(path,'%s.txt'%self.name) |
|
1529 | 1529 | self.filename_phase = os.path.join(figpath,beacon_file) |
|
1530 | 1530 | update_figfile = True |
|
1531 | 1531 | |
|
1532 | 1532 | |
|
1533 | 1533 | #store data beacon phase |
|
1534 | 1534 | #self.save_data(self.filename_phase, phase_beacon, thisDatetime) |
|
1535 | 1535 | |
|
1536 | 1536 | self.setWinTitle(title) |
|
1537 | 1537 | |
|
1538 | 1538 | |
|
1539 | 1539 | title = "Phase Offset %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1540 | 1540 | |
|
1541 | 1541 | legendlabels = ["phase %d"%(chan) for chan in numpy.arange(self.nplots)] |
|
1542 | 1542 | |
|
1543 | 1543 | axes = self.axesList[0] |
|
1544 | 1544 | |
|
1545 | 1545 | self.xdata = numpy.hstack((self.xdata, x[0:1])) |
|
1546 | 1546 | |
|
1547 | 1547 | if len(self.ydata)==0: |
|
1548 | 1548 | self.ydata = phase_beacon.reshape(-1,1) |
|
1549 | 1549 | else: |
|
1550 | 1550 | self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1))) |
|
1551 | 1551 | |
|
1552 | 1552 | |
|
1553 | 1553 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, |
|
1554 | 1554 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, |
|
1555 | 1555 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", |
|
1556 | 1556 | XAxisAsTime=True, grid='both' |
|
1557 | 1557 | ) |
|
1558 | 1558 | |
|
1559 | 1559 | self.draw() |
|
1560 | 1560 | |
|
1561 | 1561 | self.save(figpath=figpath, |
|
1562 | 1562 | figfile=figfile, |
|
1563 | 1563 | save=save, |
|
1564 | 1564 | ftp=ftp, |
|
1565 | 1565 | wr_period=wr_period, |
|
1566 | 1566 | thisDatetime=thisDatetime, |
|
1567 | 1567 | update_figfile=update_figfile) |
|
1568 | 1568 | |
|
1569 | 1569 | if dataOut.ltctime + dataOut.outputInterval >= self.xmax: |
|
1570 | 1570 | self.counter_imagwr = wr_period |
|
1571 | 1571 | self.isConfig = False |
|
1572 | 1572 | update_figfile = True |
|
1573 | 1573 | |
|
1574 | 1574 | |
|
1575 | 1575 | |
|
1576 | 1576 | class NSMeteorDetection1Plot(Figure): |
|
1577 | 1577 | |
|
1578 | 1578 | isConfig = None |
|
1579 | 1579 | __nsubplots = None |
|
1580 | 1580 | |
|
1581 | 1581 | WIDTHPROF = None |
|
1582 | 1582 | HEIGHTPROF = None |
|
1583 | 1583 | PREFIX = 'nsm' |
|
1584 | 1584 | |
|
1585 | 1585 | zminList = None |
|
1586 | 1586 | zmaxList = None |
|
1587 | 1587 | cmapList = None |
|
1588 | 1588 | titleList = None |
|
1589 | 1589 | nPairs = None |
|
1590 | 1590 | nChannels = None |
|
1591 | 1591 | nParam = None |
|
1592 | 1592 | |
|
1593 | 1593 | def __init__(self, **kwargs): |
|
1594 | 1594 | Figure.__init__(self, **kwargs) |
|
1595 | 1595 | self.isConfig = False |
|
1596 | 1596 | self.__nsubplots = 1 |
|
1597 | 1597 | |
|
1598 | 1598 | self.WIDTH = 750 |
|
1599 | 1599 | self.HEIGHT = 250 |
|
1600 | 1600 | self.WIDTHPROF = 120 |
|
1601 | 1601 | self.HEIGHTPROF = 0 |
|
1602 | 1602 | self.counter_imagwr = 0 |
|
1603 | 1603 | |
|
1604 | 1604 | self.PLOT_CODE = SPEC_CODE |
|
1605 | 1605 | |
|
1606 | 1606 | self.FTP_WEI = None |
|
1607 | 1607 | self.EXP_CODE = None |
|
1608 | 1608 | self.SUB_EXP_CODE = None |
|
1609 | 1609 | self.PLOT_POS = None |
|
1610 | 1610 | |
|
1611 | 1611 | self.__xfilter_ena = False |
|
1612 | 1612 | self.__yfilter_ena = False |
|
1613 | 1613 | |
|
1614 | 1614 | def getSubplots(self): |
|
1615 | 1615 | |
|
1616 | 1616 | ncol = 3 |
|
1617 | 1617 | nrow = int(numpy.ceil(self.nplots/3.0)) |
|
1618 | 1618 | |
|
1619 | 1619 | return nrow, ncol |
|
1620 | 1620 | |
|
1621 | 1621 | def setup(self, id, nplots, wintitle, show=True): |
|
1622 | 1622 | |
|
1623 | 1623 | self.nplots = nplots |
|
1624 | 1624 | |
|
1625 | 1625 | ncolspan = 1 |
|
1626 | 1626 | colspan = 1 |
|
1627 | 1627 | |
|
1628 | 1628 | self.createFigure(id = id, |
|
1629 | 1629 | wintitle = wintitle, |
|
1630 | 1630 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
1631 | 1631 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
1632 | 1632 | show=show) |
|
1633 | 1633 | |
|
1634 | 1634 | nrow, ncol = self.getSubplots() |
|
1635 | 1635 | |
|
1636 | 1636 | counter = 0 |
|
1637 | 1637 | for y in range(nrow): |
|
1638 | 1638 | for x in range(ncol): |
|
1639 | 1639 | |
|
1640 | 1640 | if counter >= self.nplots: |
|
1641 | 1641 | break |
|
1642 | 1642 | |
|
1643 | 1643 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
1644 | 1644 | |
|
1645 | 1645 | counter += 1 |
|
1646 | 1646 | |
|
1647 | 1647 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, |
|
1648 | 1648 | xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None, |
|
1649 | 1649 | vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA', |
|
1650 | 1650 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
1651 | 1651 | server=None, folder=None, username=None, password=None, |
|
1652 | 1652 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False, |
|
1653 | 1653 | xaxis="frequency"): |
|
1654 | 1654 | |
|
1655 | 1655 | """ |
|
1656 | 1656 | |
|
1657 | 1657 | Input: |
|
1658 | 1658 | dataOut : |
|
1659 | 1659 | id : |
|
1660 | 1660 | wintitle : |
|
1661 | 1661 | channelList : |
|
1662 | 1662 | showProfile : |
|
1663 | 1663 | xmin : None, |
|
1664 | 1664 | xmax : None, |
|
1665 | 1665 | ymin : None, |
|
1666 | 1666 | ymax : None, |
|
1667 | 1667 | zmin : None, |
|
1668 | 1668 | zmax : None |
|
1669 | 1669 | """ |
|
1670 | 1670 | #SEPARAR EN DOS PLOTS |
|
1671 | 1671 | nParam = dataOut.data_param.shape[1] - 3 |
|
1672 | 1672 | |
|
1673 | 1673 | utctime = dataOut.data_param[0,0] |
|
1674 | 1674 | tmet = dataOut.data_param[:,1].astype(int) |
|
1675 | 1675 | hmet = dataOut.data_param[:,2].astype(int) |
|
1676 | 1676 | |
|
1677 | 1677 | x = dataOut.abscissaList |
|
1678 | 1678 | y = dataOut.heightList |
|
1679 | 1679 | |
|
1680 | 1680 | z = numpy.zeros((nParam, y.size, x.size - 1)) |
|
1681 | 1681 | z[:,:] = numpy.nan |
|
1682 | 1682 | z[:,hmet,tmet] = dataOut.data_param[:,3:].T |
|
1683 | 1683 | z[0,:,:] = 10*numpy.log10(z[0,:,:]) |
|
1684 | 1684 | |
|
1685 | 1685 | xlabel = "Time (s)" |
|
1686 | 1686 | ylabel = "Range (km)" |
|
1687 | 1687 | |
|
1688 | 1688 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime) |
|
1689 | 1689 | |
|
1690 | 1690 | if not self.isConfig: |
|
1691 | 1691 | |
|
1692 | 1692 | nplots = nParam |
|
1693 | 1693 | |
|
1694 | 1694 | self.setup(id=id, |
|
1695 | 1695 | nplots=nplots, |
|
1696 | 1696 | wintitle=wintitle, |
|
1697 | 1697 | show=show) |
|
1698 | 1698 | |
|
1699 | 1699 | if xmin is None: xmin = numpy.nanmin(x) |
|
1700 | 1700 | if xmax is None: xmax = numpy.nanmax(x) |
|
1701 | 1701 | if ymin is None: ymin = numpy.nanmin(y) |
|
1702 | 1702 | if ymax is None: ymax = numpy.nanmax(y) |
|
1703 | 1703 | if SNRmin is None: SNRmin = numpy.nanmin(z[0,:]) |
|
1704 | 1704 | if SNRmax is None: SNRmax = numpy.nanmax(z[0,:]) |
|
1705 | 1705 | if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:])) |
|
1706 | 1706 | if vmin is None: vmin = -vmax |
|
1707 | 1707 | if wmin is None: wmin = 0 |
|
1708 | 1708 | if wmax is None: wmax = 50 |
|
1709 | 1709 | |
|
1710 | 1710 | pairsList = dataOut.groupList |
|
1711 | 1711 | self.nPairs = len(dataOut.groupList) |
|
1712 | 1712 | |
|
1713 | 1713 | zminList = [SNRmin, vmin, cmin] + [pmin]*self.nPairs |
|
1714 | 1714 | zmaxList = [SNRmax, vmax, cmax] + [pmax]*self.nPairs |
|
1715 | 1715 | titleList = ["SNR","Radial Velocity","Coherence"] |
|
1716 | 1716 | cmapList = ["jet","RdBu_r","jet"] |
|
1717 | 1717 | |
|
1718 | 1718 | for i in range(self.nPairs): |
|
1719 | 1719 | strAux1 = "Phase Difference "+ str(pairsList[i][0]) + str(pairsList[i][1]) |
|
1720 | 1720 | titleList = titleList + [strAux1] |
|
1721 | 1721 | cmapList = cmapList + ["RdBu_r"] |
|
1722 | 1722 | |
|
1723 | 1723 | self.zminList = zminList |
|
1724 | 1724 | self.zmaxList = zmaxList |
|
1725 | 1725 | self.cmapList = cmapList |
|
1726 | 1726 | self.titleList = titleList |
|
1727 | 1727 | |
|
1728 | 1728 | self.FTP_WEI = ftp_wei |
|
1729 | 1729 | self.EXP_CODE = exp_code |
|
1730 | 1730 | self.SUB_EXP_CODE = sub_exp_code |
|
1731 | 1731 | self.PLOT_POS = plot_pos |
|
1732 | 1732 | |
|
1733 | 1733 | self.isConfig = True |
|
1734 | 1734 | |
|
1735 | 1735 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
1736 | 1736 | |
|
1737 | 1737 | for i in range(nParam): |
|
1738 | 1738 | title = self.titleList[i] + ": " +str_datetime |
|
1739 | 1739 | axes = self.axesList[i] |
|
1740 | 1740 | axes.pcolor(x, y, z[i,:].T, |
|
1741 | 1741 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i], |
|
1742 | 1742 | xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='') |
|
1743 | 1743 | self.draw() |
|
1744 | 1744 | |
|
1745 | 1745 | if figfile == None: |
|
1746 | 1746 | str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1747 | 1747 | name = str_datetime |
|
1748 | 1748 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
1749 | 1749 | name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith) |
|
1750 | 1750 | figfile = self.getFilename(name) |
|
1751 | 1751 | |
|
1752 | 1752 | self.save(figpath=figpath, |
|
1753 | 1753 | figfile=figfile, |
|
1754 | 1754 | save=save, |
|
1755 | 1755 | ftp=ftp, |
|
1756 | 1756 | wr_period=wr_period, |
|
1757 | 1757 | thisDatetime=thisDatetime) |
|
1758 | 1758 | |
|
1759 | 1759 | |
|
1760 | 1760 | class NSMeteorDetection2Plot(Figure): |
|
1761 | 1761 | |
|
1762 | 1762 | isConfig = None |
|
1763 | 1763 | __nsubplots = None |
|
1764 | 1764 | |
|
1765 | 1765 | WIDTHPROF = None |
|
1766 | 1766 | HEIGHTPROF = None |
|
1767 | 1767 | PREFIX = 'nsm' |
|
1768 | 1768 | |
|
1769 | 1769 | zminList = None |
|
1770 | 1770 | zmaxList = None |
|
1771 | 1771 | cmapList = None |
|
1772 | 1772 | titleList = None |
|
1773 | 1773 | nPairs = None |
|
1774 | 1774 | nChannels = None |
|
1775 | 1775 | nParam = None |
|
1776 | 1776 | |
|
1777 | 1777 | def __init__(self, **kwargs): |
|
1778 | 1778 | Figure.__init__(self, **kwargs) |
|
1779 | 1779 | self.isConfig = False |
|
1780 | 1780 | self.__nsubplots = 1 |
|
1781 | 1781 | |
|
1782 | 1782 | self.WIDTH = 750 |
|
1783 | 1783 | self.HEIGHT = 250 |
|
1784 | 1784 | self.WIDTHPROF = 120 |
|
1785 | 1785 | self.HEIGHTPROF = 0 |
|
1786 | 1786 | self.counter_imagwr = 0 |
|
1787 | 1787 | |
|
1788 | 1788 | self.PLOT_CODE = SPEC_CODE |
|
1789 | 1789 | |
|
1790 | 1790 | self.FTP_WEI = None |
|
1791 | 1791 | self.EXP_CODE = None |
|
1792 | 1792 | self.SUB_EXP_CODE = None |
|
1793 | 1793 | self.PLOT_POS = None |
|
1794 | 1794 | |
|
1795 | 1795 | self.__xfilter_ena = False |
|
1796 | 1796 | self.__yfilter_ena = False |
|
1797 | 1797 | |
|
1798 | 1798 | def getSubplots(self): |
|
1799 | 1799 | |
|
1800 | 1800 | ncol = 3 |
|
1801 | 1801 | nrow = int(numpy.ceil(self.nplots/3.0)) |
|
1802 | 1802 | |
|
1803 | 1803 | return nrow, ncol |
|
1804 | 1804 | |
|
1805 | 1805 | def setup(self, id, nplots, wintitle, show=True): |
|
1806 | 1806 | |
|
1807 | 1807 | self.nplots = nplots |
|
1808 | 1808 | |
|
1809 | 1809 | ncolspan = 1 |
|
1810 | 1810 | colspan = 1 |
|
1811 | 1811 | |
|
1812 | 1812 | self.createFigure(id = id, |
|
1813 | 1813 | wintitle = wintitle, |
|
1814 | 1814 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
1815 | 1815 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
1816 | 1816 | show=show) |
|
1817 | 1817 | |
|
1818 | 1818 | nrow, ncol = self.getSubplots() |
|
1819 | 1819 | |
|
1820 | 1820 | counter = 0 |
|
1821 | 1821 | for y in range(nrow): |
|
1822 | 1822 | for x in range(ncol): |
|
1823 | 1823 | |
|
1824 | 1824 | if counter >= self.nplots: |
|
1825 | 1825 | break |
|
1826 | 1826 | |
|
1827 | 1827 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
1828 | 1828 | |
|
1829 | 1829 | counter += 1 |
|
1830 | 1830 | |
|
1831 | 1831 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, |
|
1832 | 1832 | xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None, |
|
1833 | 1833 | vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA', |
|
1834 | 1834 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
1835 | 1835 | server=None, folder=None, username=None, password=None, |
|
1836 | 1836 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False, |
|
1837 | 1837 | xaxis="frequency"): |
|
1838 | 1838 | |
|
1839 | 1839 | """ |
|
1840 | 1840 | |
|
1841 | 1841 | Input: |
|
1842 | 1842 | dataOut : |
|
1843 | 1843 | id : |
|
1844 | 1844 | wintitle : |
|
1845 | 1845 | channelList : |
|
1846 | 1846 | showProfile : |
|
1847 | 1847 | xmin : None, |
|
1848 | 1848 | xmax : None, |
|
1849 | 1849 | ymin : None, |
|
1850 | 1850 | ymax : None, |
|
1851 | 1851 | zmin : None, |
|
1852 | 1852 | zmax : None |
|
1853 | 1853 | """ |
|
1854 | 1854 | #Rebuild matrix |
|
1855 | 1855 | utctime = dataOut.data_param[0,0] |
|
1856 | 1856 | cmet = dataOut.data_param[:,1].astype(int) |
|
1857 | 1857 | tmet = dataOut.data_param[:,2].astype(int) |
|
1858 | 1858 | hmet = dataOut.data_param[:,3].astype(int) |
|
1859 | 1859 | |
|
1860 | 1860 | nParam = 3 |
|
1861 | 1861 | nChan = len(dataOut.groupList) |
|
1862 | 1862 | x = dataOut.abscissaList |
|
1863 | 1863 | y = dataOut.heightList |
|
1864 | 1864 | |
|
1865 | 1865 | z = numpy.full((nChan, nParam, y.size, x.size - 1),numpy.nan) |
|
1866 | 1866 | z[cmet,:,hmet,tmet] = dataOut.data_param[:,4:] |
|
1867 | 1867 | z[:,0,:,:] = 10*numpy.log10(z[:,0,:,:]) #logarithmic scale |
|
1868 | 1868 | z = numpy.reshape(z, (nChan*nParam, y.size, x.size-1)) |
|
1869 | 1869 | |
|
1870 | 1870 | xlabel = "Time (s)" |
|
1871 | 1871 | ylabel = "Range (km)" |
|
1872 | 1872 | |
|
1873 | 1873 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime) |
|
1874 | 1874 | |
|
1875 | 1875 | if not self.isConfig: |
|
1876 | 1876 | |
|
1877 | 1877 | nplots = nParam*nChan |
|
1878 | 1878 | |
|
1879 | 1879 | self.setup(id=id, |
|
1880 | 1880 | nplots=nplots, |
|
1881 | 1881 | wintitle=wintitle, |
|
1882 | 1882 | show=show) |
|
1883 | 1883 | |
|
1884 | 1884 | if xmin is None: xmin = numpy.nanmin(x) |
|
1885 | 1885 | if xmax is None: xmax = numpy.nanmax(x) |
|
1886 | 1886 | if ymin is None: ymin = numpy.nanmin(y) |
|
1887 | 1887 | if ymax is None: ymax = numpy.nanmax(y) |
|
1888 | 1888 | if SNRmin is None: SNRmin = numpy.nanmin(z[0,:]) |
|
1889 | 1889 | if SNRmax is None: SNRmax = numpy.nanmax(z[0,:]) |
|
1890 | 1890 | if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:])) |
|
1891 | 1891 | if vmin is None: vmin = -vmax |
|
1892 | 1892 | if wmin is None: wmin = 0 |
|
1893 | 1893 | if wmax is None: wmax = 50 |
|
1894 | 1894 | |
|
1895 | 1895 | self.nChannels = nChan |
|
1896 | 1896 | |
|
1897 | 1897 | zminList = [] |
|
1898 | 1898 | zmaxList = [] |
|
1899 | 1899 | titleList = [] |
|
1900 | 1900 | cmapList = [] |
|
1901 | 1901 | for i in range(self.nChannels): |
|
1902 | 1902 | strAux1 = "SNR Channel "+ str(i) |
|
1903 | 1903 | strAux2 = "Radial Velocity Channel "+ str(i) |
|
1904 | 1904 | strAux3 = "Spectral Width Channel "+ str(i) |
|
1905 | 1905 | |
|
1906 | 1906 | titleList = titleList + [strAux1,strAux2,strAux3] |
|
1907 | 1907 | cmapList = cmapList + ["jet","RdBu_r","jet"] |
|
1908 | 1908 | zminList = zminList + [SNRmin,vmin,wmin] |
|
1909 | 1909 | zmaxList = zmaxList + [SNRmax,vmax,wmax] |
|
1910 | 1910 | |
|
1911 | 1911 | self.zminList = zminList |
|
1912 | 1912 | self.zmaxList = zmaxList |
|
1913 | 1913 | self.cmapList = cmapList |
|
1914 | 1914 | self.titleList = titleList |
|
1915 | 1915 | |
|
1916 | 1916 | self.FTP_WEI = ftp_wei |
|
1917 | 1917 | self.EXP_CODE = exp_code |
|
1918 | 1918 | self.SUB_EXP_CODE = sub_exp_code |
|
1919 | 1919 | self.PLOT_POS = plot_pos |
|
1920 | 1920 | |
|
1921 | 1921 | self.isConfig = True |
|
1922 | 1922 | |
|
1923 | 1923 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
1924 | 1924 | |
|
1925 | 1925 | for i in range(self.nplots): |
|
1926 | 1926 | title = self.titleList[i] + ": " +str_datetime |
|
1927 | 1927 | axes = self.axesList[i] |
|
1928 | 1928 | axes.pcolor(x, y, z[i,:].T, |
|
1929 | 1929 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i], |
|
1930 | 1930 | xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='') |
|
1931 | 1931 | self.draw() |
|
1932 | 1932 | |
|
1933 | 1933 | if figfile == None: |
|
1934 | 1934 | str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1935 | 1935 | name = str_datetime |
|
1936 | 1936 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
1937 | 1937 | name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith) |
|
1938 | 1938 | figfile = self.getFilename(name) |
|
1939 | 1939 | |
|
1940 | 1940 | self.save(figpath=figpath, |
|
1941 | 1941 | figfile=figfile, |
|
1942 | 1942 | save=save, |
|
1943 | 1943 | ftp=ftp, |
|
1944 | 1944 | wr_period=wr_period, |
|
1945 | 1945 | thisDatetime=thisDatetime) |
@@ -1,1534 +1,1535 | |||
|
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 | 10 | from figure import Figure, isRealtime, isTimeInHourRange |
|
11 | 11 | from plotting_codes import * |
|
12 | 12 | |
|
13 | ||
|
13 | 14 | class SpectraPlot(Figure): |
|
14 | 15 | |
|
15 | 16 | isConfig = None |
|
16 | 17 | __nsubplots = None |
|
17 | 18 | |
|
18 | 19 | WIDTHPROF = None |
|
19 | 20 | HEIGHTPROF = None |
|
20 | 21 | PREFIX = 'spc' |
|
21 | 22 | |
|
22 | 23 | def __init__(self, **kwargs): |
|
23 | 24 | Figure.__init__(self, **kwargs) |
|
24 | 25 | self.isConfig = False |
|
25 | 26 | self.__nsubplots = 1 |
|
26 | 27 | |
|
27 | 28 | self.WIDTH = 1000 |
|
28 | 29 | self.HEIGHT = 1000 |
|
29 | 30 | self.WIDTHPROF = 120 |
|
30 | 31 | self.HEIGHTPROF = 0 |
|
31 | 32 | self.counter_imagwr = 0 |
|
32 | 33 | |
|
33 | 34 | self.PLOT_CODE = SPEC_CODE |
|
34 | 35 | |
|
35 | 36 | self.FTP_WEI = None |
|
36 | 37 | self.EXP_CODE = None |
|
37 | 38 | self.SUB_EXP_CODE = None |
|
38 | 39 | self.PLOT_POS = None |
|
39 | 40 | |
|
40 | 41 | self.__xfilter_ena = False |
|
41 | 42 | self.__yfilter_ena = False |
|
42 | 43 | |
|
43 | 44 | def getSubplots(self): |
|
44 | 45 | |
|
45 | 46 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
46 | 47 | nrow = int(self.nplots*1./ncol + 0.9) |
|
47 | 48 | |
|
48 | 49 | return nrow, ncol |
|
49 | 50 | |
|
50 | 51 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
51 | 52 | |
|
52 | 53 | self.__showprofile = showprofile |
|
53 | 54 | self.nplots = nplots |
|
54 | 55 | |
|
55 | 56 | ncolspan = 1 |
|
56 | 57 | colspan = 1 |
|
57 | 58 | if showprofile: |
|
58 | 59 | ncolspan = 3 |
|
59 | 60 | colspan = 2 |
|
60 | 61 | self.__nsubplots = 2 |
|
61 | 62 | |
|
62 | 63 | self.createFigure(id = id, |
|
63 | 64 | wintitle = wintitle, |
|
64 | 65 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
65 | 66 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
66 | 67 | show=show) |
|
67 | 68 | |
|
68 | 69 | nrow, ncol = self.getSubplots() |
|
69 | 70 | |
|
70 | 71 | counter = 0 |
|
71 | 72 | for y in range(nrow): |
|
72 | 73 | for x in range(ncol): |
|
73 | 74 | |
|
74 | 75 | if counter >= self.nplots: |
|
75 | 76 | break |
|
76 | 77 | |
|
77 | 78 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
78 | 79 | |
|
79 | 80 | if showprofile: |
|
80 | 81 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
81 | 82 | |
|
82 | 83 | counter += 1 |
|
83 | 84 | |
|
84 | 85 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, |
|
85 | 86 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
86 | 87 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
87 | 88 | server=None, folder=None, username=None, password=None, |
|
88 | 89 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False, |
|
89 | 90 | xaxis="velocity", **kwargs): |
|
90 | 91 | |
|
91 | 92 | """ |
|
92 | 93 | |
|
93 | 94 | Input: |
|
94 | 95 | dataOut : |
|
95 | 96 | id : |
|
96 | 97 | wintitle : |
|
97 | 98 | channelList : |
|
98 | 99 | showProfile : |
|
99 | 100 | xmin : None, |
|
100 | 101 | xmax : None, |
|
101 | 102 | ymin : None, |
|
102 | 103 | ymax : None, |
|
103 | 104 | zmin : None, |
|
104 | 105 | zmax : None |
|
105 | 106 | """ |
|
106 | 107 | |
|
107 | 108 | colormap = kwargs.get('colormap','jet') |
|
108 | 109 | |
|
109 | 110 | if realtime: |
|
110 | 111 | if not(isRealtime(utcdatatime = dataOut.utctime)): |
|
111 | 112 | print 'Skipping this plot function' |
|
112 | 113 | return |
|
113 | 114 | |
|
114 | 115 | if channelList == None: |
|
115 | 116 | channelIndexList = dataOut.channelIndexList |
|
116 | 117 | else: |
|
117 | 118 | channelIndexList = [] |
|
118 | 119 | for channel in channelList: |
|
119 | 120 | if channel not in dataOut.channelList: |
|
120 | 121 | raise ValueError, "Channel %d is not in dataOut.channelList" %channel |
|
121 | 122 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
122 | 123 | |
|
123 | 124 | factor = dataOut.normFactor |
|
124 | 125 | |
|
125 | 126 | if xaxis == "frequency": |
|
126 | 127 | x = dataOut.getFreqRange(1)/1000. |
|
127 | 128 | xlabel = "Frequency (kHz)" |
|
128 | 129 | |
|
129 | 130 | elif xaxis == "time": |
|
130 | 131 | x = dataOut.getAcfRange(1) |
|
131 | 132 | xlabel = "Time (ms)" |
|
132 | 133 | |
|
133 | 134 | else: |
|
134 | 135 | x = dataOut.getVelRange(1) |
|
135 | 136 | xlabel = "Velocity (m/s)" |
|
136 | 137 | |
|
137 | 138 | ylabel = "Range (Km)" |
|
138 | 139 | |
|
139 | 140 | y = dataOut.getHeiRange() |
|
140 | 141 | |
|
141 | 142 | z = dataOut.data_spc/factor |
|
142 | 143 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
143 | 144 | zdB = 10*numpy.log10(z) |
|
144 | 145 | |
|
145 | 146 | avg = numpy.average(z, axis=1) |
|
146 | 147 | avgdB = 10*numpy.log10(avg) |
|
147 | 148 | |
|
148 | 149 | noise = dataOut.getNoise()/factor |
|
149 | 150 | noisedB = 10*numpy.log10(noise) |
|
150 | 151 | |
|
151 | 152 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
152 | 153 | title = wintitle + " Spectra" |
|
153 | 154 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
154 | 155 | title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith) |
|
155 | 156 | |
|
156 | 157 | if not self.isConfig: |
|
157 | 158 | |
|
158 | 159 | nplots = len(channelIndexList) |
|
159 | 160 | |
|
160 | 161 | self.setup(id=id, |
|
161 | 162 | nplots=nplots, |
|
162 | 163 | wintitle=wintitle, |
|
163 | 164 | showprofile=showprofile, |
|
164 | 165 | show=show) |
|
165 | 166 | |
|
166 | 167 | if xmin == None: xmin = numpy.nanmin(x) |
|
167 | 168 | if xmax == None: xmax = numpy.nanmax(x) |
|
168 | 169 | if ymin == None: ymin = numpy.nanmin(y) |
|
169 | 170 | if ymax == None: ymax = numpy.nanmax(y) |
|
170 | 171 | if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3 |
|
171 | 172 | if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3 |
|
172 | 173 | |
|
173 | 174 | self.FTP_WEI = ftp_wei |
|
174 | 175 | self.EXP_CODE = exp_code |
|
175 | 176 | self.SUB_EXP_CODE = sub_exp_code |
|
176 | 177 | self.PLOT_POS = plot_pos |
|
177 | 178 | |
|
178 | 179 | self.isConfig = True |
|
179 | 180 | |
|
180 | 181 | self.setWinTitle(title) |
|
181 | 182 | |
|
182 | 183 | for i in range(self.nplots): |
|
183 | 184 | index = channelIndexList[i] |
|
184 | 185 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
185 | 186 | title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime) |
|
186 | 187 | if len(dataOut.beam.codeList) != 0: |
|
187 | 188 | 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 | 189 | |
|
189 | 190 | axes = self.axesList[i*self.__nsubplots] |
|
190 | 191 | axes.pcolor(x, y, zdB[index,:,:], |
|
191 | 192 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
192 | 193 | xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap, |
|
193 | 194 | ticksize=9, cblabel='') |
|
194 | 195 | |
|
195 | 196 | if self.__showprofile: |
|
196 | 197 | axes = self.axesList[i*self.__nsubplots +1] |
|
197 | 198 | axes.pline(avgdB[index,:], y, |
|
198 | 199 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
199 | 200 | xlabel='dB', ylabel='', title='', |
|
200 | 201 | ytick_visible=False, |
|
201 | 202 | grid='x') |
|
202 | 203 | |
|
203 | 204 | noiseline = numpy.repeat(noisedB[index], len(y)) |
|
204 | 205 | axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) |
|
205 | 206 | |
|
206 | 207 | self.draw() |
|
207 | 208 | |
|
208 | 209 | if figfile == None: |
|
209 | 210 | str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
210 | 211 | name = str_datetime |
|
211 | 212 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
212 | 213 | name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith) |
|
213 | 214 | figfile = self.getFilename(name) |
|
214 | 215 | |
|
215 | 216 | self.save(figpath=figpath, |
|
216 | 217 | figfile=figfile, |
|
217 | 218 | save=save, |
|
218 | 219 | ftp=ftp, |
|
219 | 220 | wr_period=wr_period, |
|
220 | 221 | thisDatetime=thisDatetime) |
|
221 | 222 | |
|
222 | 223 | class CrossSpectraPlot(Figure): |
|
223 | 224 | |
|
224 | 225 | isConfig = None |
|
225 | 226 | __nsubplots = None |
|
226 | 227 | |
|
227 | 228 | WIDTH = None |
|
228 | 229 | HEIGHT = None |
|
229 | 230 | WIDTHPROF = None |
|
230 | 231 | HEIGHTPROF = None |
|
231 | 232 | PREFIX = 'cspc' |
|
232 | 233 | |
|
233 | 234 | def __init__(self, **kwargs): |
|
234 | 235 | Figure.__init__(self, **kwargs) |
|
235 | 236 | self.isConfig = False |
|
236 | 237 | self.__nsubplots = 4 |
|
237 | 238 | self.counter_imagwr = 0 |
|
238 | 239 | self.WIDTH = 250 |
|
239 | 240 | self.HEIGHT = 250 |
|
240 | 241 | self.WIDTHPROF = 0 |
|
241 | 242 | self.HEIGHTPROF = 0 |
|
242 | 243 | |
|
243 | 244 | self.PLOT_CODE = CROSS_CODE |
|
244 | 245 | self.FTP_WEI = None |
|
245 | 246 | self.EXP_CODE = None |
|
246 | 247 | self.SUB_EXP_CODE = None |
|
247 | 248 | self.PLOT_POS = None |
|
248 | 249 | |
|
249 | 250 | def getSubplots(self): |
|
250 | 251 | |
|
251 | 252 | ncol = 4 |
|
252 | 253 | nrow = self.nplots |
|
253 | 254 | |
|
254 | 255 | return nrow, ncol |
|
255 | 256 | |
|
256 | 257 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
257 | 258 | |
|
258 | 259 | self.__showprofile = showprofile |
|
259 | 260 | self.nplots = nplots |
|
260 | 261 | |
|
261 | 262 | ncolspan = 1 |
|
262 | 263 | colspan = 1 |
|
263 | 264 | |
|
264 | 265 | self.createFigure(id = id, |
|
265 | 266 | wintitle = wintitle, |
|
266 | 267 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
267 | 268 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
268 | 269 | show=True) |
|
269 | 270 | |
|
270 | 271 | nrow, ncol = self.getSubplots() |
|
271 | 272 | |
|
272 | 273 | counter = 0 |
|
273 | 274 | for y in range(nrow): |
|
274 | 275 | for x in range(ncol): |
|
275 | 276 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
276 | 277 | |
|
277 | 278 | counter += 1 |
|
278 | 279 | |
|
279 | 280 | def run(self, dataOut, id, wintitle="", pairsList=None, |
|
280 | 281 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
281 | 282 | coh_min=None, coh_max=None, phase_min=None, phase_max=None, |
|
282 | 283 | save=False, figpath='./', figfile=None, ftp=False, wr_period=1, |
|
283 | 284 | power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True, |
|
284 | 285 | server=None, folder=None, username=None, password=None, |
|
285 | 286 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, |
|
286 | 287 | xaxis='frequency'): |
|
287 | 288 | |
|
288 | 289 | """ |
|
289 | 290 | |
|
290 | 291 | Input: |
|
291 | 292 | dataOut : |
|
292 | 293 | id : |
|
293 | 294 | wintitle : |
|
294 | 295 | channelList : |
|
295 | 296 | showProfile : |
|
296 | 297 | xmin : None, |
|
297 | 298 | xmax : None, |
|
298 | 299 | ymin : None, |
|
299 | 300 | ymax : None, |
|
300 | 301 | zmin : None, |
|
301 | 302 | zmax : None |
|
302 | 303 | """ |
|
303 | 304 | |
|
304 | 305 | if pairsList == None: |
|
305 | 306 | pairsIndexList = dataOut.pairsIndexList |
|
306 | 307 | else: |
|
307 | 308 | pairsIndexList = [] |
|
308 | 309 | for pair in pairsList: |
|
309 | 310 | if pair not in dataOut.pairsList: |
|
310 | 311 | raise ValueError, "Pair %s is not in dataOut.pairsList" %str(pair) |
|
311 | 312 | pairsIndexList.append(dataOut.pairsList.index(pair)) |
|
312 | 313 | |
|
313 | 314 | if not pairsIndexList: |
|
314 | 315 | return |
|
315 | 316 | |
|
316 | 317 | if len(pairsIndexList) > 4: |
|
317 | 318 | pairsIndexList = pairsIndexList[0:4] |
|
318 | 319 | |
|
319 | 320 | factor = dataOut.normFactor |
|
320 | 321 | x = dataOut.getVelRange(1) |
|
321 | 322 | y = dataOut.getHeiRange() |
|
322 | 323 | z = dataOut.data_spc[:,:,:]/factor |
|
323 | 324 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
324 | 325 | |
|
325 | 326 | noise = dataOut.noise/factor |
|
326 | 327 | |
|
327 | 328 | zdB = 10*numpy.log10(z) |
|
328 | 329 | noisedB = 10*numpy.log10(noise) |
|
329 | 330 | |
|
330 | 331 | if coh_min == None: |
|
331 | 332 | coh_min = 0.0 |
|
332 | 333 | if coh_max == None: |
|
333 | 334 | coh_max = 1.0 |
|
334 | 335 | |
|
335 | 336 | if phase_min == None: |
|
336 | 337 | phase_min = -180 |
|
337 | 338 | if phase_max == None: |
|
338 | 339 | phase_max = 180 |
|
339 | 340 | |
|
340 | 341 | #thisDatetime = dataOut.datatime |
|
341 | 342 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
342 | 343 | title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
343 | 344 | # xlabel = "Velocity (m/s)" |
|
344 | 345 | ylabel = "Range (Km)" |
|
345 | 346 | |
|
346 | 347 | if xaxis == "frequency": |
|
347 | 348 | x = dataOut.getFreqRange(1)/1000. |
|
348 | 349 | xlabel = "Frequency (kHz)" |
|
349 | 350 | |
|
350 | 351 | elif xaxis == "time": |
|
351 | 352 | x = dataOut.getAcfRange(1) |
|
352 | 353 | xlabel = "Time (ms)" |
|
353 | 354 | |
|
354 | 355 | else: |
|
355 | 356 | x = dataOut.getVelRange(1) |
|
356 | 357 | xlabel = "Velocity (m/s)" |
|
357 | 358 | |
|
358 | 359 | if not self.isConfig: |
|
359 | 360 | |
|
360 | 361 | nplots = len(pairsIndexList) |
|
361 | 362 | |
|
362 | 363 | self.setup(id=id, |
|
363 | 364 | nplots=nplots, |
|
364 | 365 | wintitle=wintitle, |
|
365 | 366 | showprofile=False, |
|
366 | 367 | show=show) |
|
367 | 368 | |
|
368 | 369 | avg = numpy.abs(numpy.average(z, axis=1)) |
|
369 | 370 | avgdB = 10*numpy.log10(avg) |
|
370 | 371 | |
|
371 | 372 | if xmin == None: xmin = numpy.nanmin(x) |
|
372 | 373 | if xmax == None: xmax = numpy.nanmax(x) |
|
373 | 374 | if ymin == None: ymin = numpy.nanmin(y) |
|
374 | 375 | if ymax == None: ymax = numpy.nanmax(y) |
|
375 | 376 | if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3 |
|
376 | 377 | if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3 |
|
377 | 378 | |
|
378 | 379 | self.FTP_WEI = ftp_wei |
|
379 | 380 | self.EXP_CODE = exp_code |
|
380 | 381 | self.SUB_EXP_CODE = sub_exp_code |
|
381 | 382 | self.PLOT_POS = plot_pos |
|
382 | 383 | |
|
383 | 384 | self.isConfig = True |
|
384 | 385 | |
|
385 | 386 | self.setWinTitle(title) |
|
386 | 387 | |
|
387 | 388 | for i in range(self.nplots): |
|
388 | 389 | pair = dataOut.pairsList[pairsIndexList[i]] |
|
389 | 390 | |
|
390 | 391 | chan_index0 = dataOut.channelList.index(pair[0]) |
|
391 | 392 | chan_index1 = dataOut.channelList.index(pair[1]) |
|
392 | 393 | |
|
393 | 394 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) |
|
394 | 395 | title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime) |
|
395 | 396 | zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor) |
|
396 | 397 | axes0 = self.axesList[i*self.__nsubplots] |
|
397 | 398 | axes0.pcolor(x, y, zdB, |
|
398 | 399 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
399 | 400 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
400 | 401 | ticksize=9, colormap=power_cmap, cblabel='') |
|
401 | 402 | |
|
402 | 403 | title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime) |
|
403 | 404 | zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor) |
|
404 | 405 | axes0 = self.axesList[i*self.__nsubplots+1] |
|
405 | 406 | axes0.pcolor(x, y, zdB, |
|
406 | 407 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
407 | 408 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
408 | 409 | ticksize=9, colormap=power_cmap, cblabel='') |
|
409 | 410 | |
|
410 | 411 | coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:]) |
|
411 | 412 | coherence = numpy.abs(coherenceComplex) |
|
412 | 413 | # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi |
|
413 | 414 | phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi |
|
414 | 415 | |
|
415 | 416 | title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1]) |
|
416 | 417 | axes0 = self.axesList[i*self.__nsubplots+2] |
|
417 | 418 | axes0.pcolor(x, y, coherence, |
|
418 | 419 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max, |
|
419 | 420 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
420 | 421 | ticksize=9, colormap=coherence_cmap, cblabel='') |
|
421 | 422 | |
|
422 | 423 | title = "Phase Ch%d * Ch%d" %(pair[0], pair[1]) |
|
423 | 424 | axes0 = self.axesList[i*self.__nsubplots+3] |
|
424 | 425 | axes0.pcolor(x, y, phase, |
|
425 | 426 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max, |
|
426 | 427 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
427 | 428 | ticksize=9, colormap=phase_cmap, cblabel='') |
|
428 | 429 | |
|
429 | 430 | |
|
430 | 431 | |
|
431 | 432 | self.draw() |
|
432 | 433 | |
|
433 | 434 | self.save(figpath=figpath, |
|
434 | 435 | figfile=figfile, |
|
435 | 436 | save=save, |
|
436 | 437 | ftp=ftp, |
|
437 | 438 | wr_period=wr_period, |
|
438 | 439 | thisDatetime=thisDatetime) |
|
439 | 440 | |
|
440 | 441 | |
|
441 | 442 | class RTIPlot(Figure): |
|
442 | 443 | |
|
443 | 444 | __isConfig = None |
|
444 | 445 | __nsubplots = None |
|
445 | 446 | |
|
446 | 447 | WIDTHPROF = None |
|
447 | 448 | HEIGHTPROF = None |
|
448 | 449 | PREFIX = 'rti' |
|
449 | 450 | |
|
450 | 451 | def __init__(self, **kwargs): |
|
451 | 452 | |
|
452 | 453 | Figure.__init__(self, **kwargs) |
|
453 | 454 | self.timerange = None |
|
454 | 455 | self.isConfig = False |
|
455 | 456 | self.__nsubplots = 1 |
|
456 | 457 | |
|
457 | 458 | self.WIDTH = 800 |
|
458 | 459 | self.HEIGHT = 180 |
|
459 | 460 | self.WIDTHPROF = 120 |
|
460 | 461 | self.HEIGHTPROF = 0 |
|
461 | 462 | self.counter_imagwr = 0 |
|
462 | 463 | |
|
463 | 464 | self.PLOT_CODE = RTI_CODE |
|
464 | 465 | |
|
465 | 466 | self.FTP_WEI = None |
|
466 | 467 | self.EXP_CODE = None |
|
467 | 468 | self.SUB_EXP_CODE = None |
|
468 | 469 | self.PLOT_POS = None |
|
469 | 470 | self.tmin = None |
|
470 | 471 | self.tmax = None |
|
471 | 472 | |
|
472 | 473 | self.xmin = None |
|
473 | 474 | self.xmax = None |
|
474 | 475 | |
|
475 | 476 | self.figfile = None |
|
476 | 477 | |
|
477 | 478 | def getSubplots(self): |
|
478 | 479 | |
|
479 | 480 | ncol = 1 |
|
480 | 481 | nrow = self.nplots |
|
481 | 482 | |
|
482 | 483 | return nrow, ncol |
|
483 | 484 | |
|
484 | 485 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
485 | 486 | |
|
486 | 487 | self.__showprofile = showprofile |
|
487 | 488 | self.nplots = nplots |
|
488 | 489 | |
|
489 | 490 | ncolspan = 1 |
|
490 | 491 | colspan = 1 |
|
491 | 492 | if showprofile: |
|
492 | 493 | ncolspan = 7 |
|
493 | 494 | colspan = 6 |
|
494 | 495 | self.__nsubplots = 2 |
|
495 | 496 | |
|
496 | 497 | self.createFigure(id = id, |
|
497 | 498 | wintitle = wintitle, |
|
498 | 499 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
499 | 500 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
500 | 501 | show=show) |
|
501 | 502 | |
|
502 | 503 | nrow, ncol = self.getSubplots() |
|
503 | 504 | |
|
504 | 505 | counter = 0 |
|
505 | 506 | for y in range(nrow): |
|
506 | 507 | for x in range(ncol): |
|
507 | 508 | |
|
508 | 509 | if counter >= self.nplots: |
|
509 | 510 | break |
|
510 | 511 | |
|
511 | 512 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
512 | 513 | |
|
513 | 514 | if showprofile: |
|
514 | 515 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
515 | 516 | |
|
516 | 517 | counter += 1 |
|
517 | 518 | |
|
518 | 519 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', |
|
519 | 520 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
520 | 521 | timerange=None, |
|
521 | 522 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, |
|
522 | 523 | server=None, folder=None, username=None, password=None, |
|
523 | 524 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, **kwargs): |
|
524 | 525 | |
|
525 | 526 | """ |
|
526 | 527 | |
|
527 | 528 | Input: |
|
528 | 529 | dataOut : |
|
529 | 530 | id : |
|
530 | 531 | wintitle : |
|
531 | 532 | channelList : |
|
532 | 533 | showProfile : |
|
533 | 534 | xmin : None, |
|
534 | 535 | xmax : None, |
|
535 | 536 | ymin : None, |
|
536 | 537 | ymax : None, |
|
537 | 538 | zmin : None, |
|
538 | 539 | zmax : None |
|
539 | 540 | """ |
|
540 | 541 | |
|
541 | 542 | colormap = kwargs.get('colormap', 'jet') |
|
542 | 543 | if not isTimeInHourRange(dataOut.datatime, xmin, xmax): |
|
543 | 544 | return |
|
544 | 545 | |
|
545 | 546 | if channelList == None: |
|
546 | 547 | channelIndexList = dataOut.channelIndexList |
|
547 | 548 | else: |
|
548 | 549 | channelIndexList = [] |
|
549 | 550 | for channel in channelList: |
|
550 | 551 | if channel not in dataOut.channelList: |
|
551 | 552 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
552 | 553 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
553 | 554 | |
|
554 | 555 | if hasattr(dataOut, 'normFactor'): |
|
555 | 556 | factor = dataOut.normFactor |
|
556 | 557 | else: |
|
557 | 558 | factor = 1 |
|
558 | 559 | |
|
559 | 560 | # factor = dataOut.normFactor |
|
560 | 561 | x = dataOut.getTimeRange() |
|
561 | 562 | y = dataOut.getHeiRange() |
|
562 | 563 | |
|
563 | 564 | # z = dataOut.data_spc/factor |
|
564 | 565 | # z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
565 | 566 | # avg = numpy.average(z, axis=1) |
|
566 | 567 | # avgdB = 10.*numpy.log10(avg) |
|
567 | 568 | avgdB = dataOut.getPower() |
|
568 | 569 | |
|
569 | 570 | thisDatetime = dataOut.datatime |
|
570 | 571 | # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
571 | 572 | title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
572 | 573 | xlabel = "" |
|
573 | 574 | ylabel = "Range (Km)" |
|
574 | 575 | |
|
575 | 576 | update_figfile = False |
|
576 | 577 | |
|
577 | 578 | if dataOut.ltctime >= self.xmax: |
|
578 | 579 | self.counter_imagwr = wr_period |
|
579 | 580 | self.isConfig = False |
|
580 | 581 | update_figfile = True |
|
581 | 582 | |
|
582 | 583 | if not self.isConfig: |
|
583 | 584 | |
|
584 | 585 | nplots = len(channelIndexList) |
|
585 | 586 | |
|
586 | 587 | self.setup(id=id, |
|
587 | 588 | nplots=nplots, |
|
588 | 589 | wintitle=wintitle, |
|
589 | 590 | showprofile=showprofile, |
|
590 | 591 | show=show) |
|
591 | 592 | |
|
592 | 593 | if timerange != None: |
|
593 | 594 | self.timerange = timerange |
|
594 | 595 | |
|
595 | 596 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
596 | 597 | |
|
597 | 598 | noise = dataOut.noise/factor |
|
598 | 599 | noisedB = 10*numpy.log10(noise) |
|
599 | 600 | |
|
600 | 601 | if ymin == None: ymin = numpy.nanmin(y) |
|
601 | 602 | if ymax == None: ymax = numpy.nanmax(y) |
|
602 | 603 | if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3 |
|
603 | 604 | if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3 |
|
604 | 605 | |
|
605 | 606 | self.FTP_WEI = ftp_wei |
|
606 | 607 | self.EXP_CODE = exp_code |
|
607 | 608 | self.SUB_EXP_CODE = sub_exp_code |
|
608 | 609 | self.PLOT_POS = plot_pos |
|
609 | 610 | |
|
610 | 611 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
611 | 612 | self.isConfig = True |
|
612 | 613 | self.figfile = figfile |
|
613 | 614 | update_figfile = True |
|
614 | 615 | |
|
615 | 616 | self.setWinTitle(title) |
|
616 | 617 | |
|
617 | 618 | for i in range(self.nplots): |
|
618 | 619 | index = channelIndexList[i] |
|
619 | 620 | title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
620 | 621 | if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)): |
|
621 | 622 | title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith) |
|
622 | 623 | axes = self.axesList[i*self.__nsubplots] |
|
623 | 624 | zdB = avgdB[index].reshape((1,-1)) |
|
624 | 625 | axes.pcolorbuffer(x, y, zdB, |
|
625 | 626 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
626 | 627 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
627 | 628 | ticksize=9, cblabel='', cbsize="1%", colormap=colormap) |
|
628 | 629 | |
|
629 | 630 | if self.__showprofile: |
|
630 | 631 | axes = self.axesList[i*self.__nsubplots +1] |
|
631 | 632 | axes.pline(avgdB[index], y, |
|
632 | 633 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
633 | 634 | xlabel='dB', ylabel='', title='', |
|
634 | 635 | ytick_visible=False, |
|
635 | 636 | grid='x') |
|
636 | 637 | |
|
637 | 638 | self.draw() |
|
638 | 639 | |
|
639 | 640 | self.save(figpath=figpath, |
|
640 | 641 | figfile=figfile, |
|
641 | 642 | save=save, |
|
642 | 643 | ftp=ftp, |
|
643 | 644 | wr_period=wr_period, |
|
644 | 645 | thisDatetime=thisDatetime, |
|
645 | 646 | update_figfile=update_figfile) |
|
646 | 647 | |
|
647 | 648 | class CoherenceMap(Figure): |
|
648 | 649 | isConfig = None |
|
649 | 650 | __nsubplots = None |
|
650 | 651 | |
|
651 | 652 | WIDTHPROF = None |
|
652 | 653 | HEIGHTPROF = None |
|
653 | 654 | PREFIX = 'cmap' |
|
654 | 655 | |
|
655 | 656 | def __init__(self, **kwargs): |
|
656 | 657 | Figure.__init__(self, **kwargs) |
|
657 | 658 | self.timerange = 2*60*60 |
|
658 | 659 | self.isConfig = False |
|
659 | 660 | self.__nsubplots = 1 |
|
660 | 661 | |
|
661 | 662 | self.WIDTH = 800 |
|
662 | 663 | self.HEIGHT = 180 |
|
663 | 664 | self.WIDTHPROF = 120 |
|
664 | 665 | self.HEIGHTPROF = 0 |
|
665 | 666 | self.counter_imagwr = 0 |
|
666 | 667 | |
|
667 | 668 | self.PLOT_CODE = COH_CODE |
|
668 | 669 | |
|
669 | 670 | self.FTP_WEI = None |
|
670 | 671 | self.EXP_CODE = None |
|
671 | 672 | self.SUB_EXP_CODE = None |
|
672 | 673 | self.PLOT_POS = None |
|
673 | 674 | self.counter_imagwr = 0 |
|
674 | 675 | |
|
675 | 676 | self.xmin = None |
|
676 | 677 | self.xmax = None |
|
677 | 678 | |
|
678 | 679 | def getSubplots(self): |
|
679 | 680 | ncol = 1 |
|
680 | 681 | nrow = self.nplots*2 |
|
681 | 682 | |
|
682 | 683 | return nrow, ncol |
|
683 | 684 | |
|
684 | 685 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
685 | 686 | self.__showprofile = showprofile |
|
686 | 687 | self.nplots = nplots |
|
687 | 688 | |
|
688 | 689 | ncolspan = 1 |
|
689 | 690 | colspan = 1 |
|
690 | 691 | if showprofile: |
|
691 | 692 | ncolspan = 7 |
|
692 | 693 | colspan = 6 |
|
693 | 694 | self.__nsubplots = 2 |
|
694 | 695 | |
|
695 | 696 | self.createFigure(id = id, |
|
696 | 697 | wintitle = wintitle, |
|
697 | 698 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
698 | 699 | heightplot = self.HEIGHT + self.HEIGHTPROF, |
|
699 | 700 | show=True) |
|
700 | 701 | |
|
701 | 702 | nrow, ncol = self.getSubplots() |
|
702 | 703 | |
|
703 | 704 | for y in range(nrow): |
|
704 | 705 | for x in range(ncol): |
|
705 | 706 | |
|
706 | 707 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
707 | 708 | |
|
708 | 709 | if showprofile: |
|
709 | 710 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
710 | 711 | |
|
711 | 712 | def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', |
|
712 | 713 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
713 | 714 | timerange=None, phase_min=None, phase_max=None, |
|
714 | 715 | save=False, figpath='./', figfile=None, ftp=False, wr_period=1, |
|
715 | 716 | coherence_cmap='jet', phase_cmap='RdBu_r', show=True, |
|
716 | 717 | server=None, folder=None, username=None, password=None, |
|
717 | 718 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
718 | 719 | |
|
719 | 720 | if not isTimeInHourRange(dataOut.datatime, xmin, xmax): |
|
720 | 721 | return |
|
721 | 722 | |
|
722 | 723 | if pairsList == None: |
|
723 | 724 | pairsIndexList = dataOut.pairsIndexList |
|
724 | 725 | else: |
|
725 | 726 | pairsIndexList = [] |
|
726 | 727 | for pair in pairsList: |
|
727 | 728 | if pair not in dataOut.pairsList: |
|
728 | 729 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) |
|
729 | 730 | pairsIndexList.append(dataOut.pairsList.index(pair)) |
|
730 | 731 | |
|
731 | 732 | if pairsIndexList == []: |
|
732 | 733 | return |
|
733 | 734 | |
|
734 | 735 | if len(pairsIndexList) > 4: |
|
735 | 736 | pairsIndexList = pairsIndexList[0:4] |
|
736 | 737 | |
|
737 | 738 | if phase_min == None: |
|
738 | 739 | phase_min = -180 |
|
739 | 740 | if phase_max == None: |
|
740 | 741 | phase_max = 180 |
|
741 | 742 | |
|
742 | 743 | x = dataOut.getTimeRange() |
|
743 | 744 | y = dataOut.getHeiRange() |
|
744 | 745 | |
|
745 | 746 | thisDatetime = dataOut.datatime |
|
746 | 747 | |
|
747 | 748 | title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
748 | 749 | xlabel = "" |
|
749 | 750 | ylabel = "Range (Km)" |
|
750 | 751 | update_figfile = False |
|
751 | 752 | |
|
752 | 753 | if not self.isConfig: |
|
753 | 754 | nplots = len(pairsIndexList) |
|
754 | 755 | self.setup(id=id, |
|
755 | 756 | nplots=nplots, |
|
756 | 757 | wintitle=wintitle, |
|
757 | 758 | showprofile=showprofile, |
|
758 | 759 | show=show) |
|
759 | 760 | |
|
760 | 761 | if timerange != None: |
|
761 | 762 | self.timerange = timerange |
|
762 | 763 | |
|
763 | 764 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
764 | 765 | |
|
765 | 766 | if ymin == None: ymin = numpy.nanmin(y) |
|
766 | 767 | if ymax == None: ymax = numpy.nanmax(y) |
|
767 | 768 | if zmin == None: zmin = 0. |
|
768 | 769 | if zmax == None: zmax = 1. |
|
769 | 770 | |
|
770 | 771 | self.FTP_WEI = ftp_wei |
|
771 | 772 | self.EXP_CODE = exp_code |
|
772 | 773 | self.SUB_EXP_CODE = sub_exp_code |
|
773 | 774 | self.PLOT_POS = plot_pos |
|
774 | 775 | |
|
775 | 776 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
776 | 777 | |
|
777 | 778 | self.isConfig = True |
|
778 | 779 | update_figfile = True |
|
779 | 780 | |
|
780 | 781 | self.setWinTitle(title) |
|
781 | 782 | |
|
782 | 783 | for i in range(self.nplots): |
|
783 | 784 | |
|
784 | 785 | pair = dataOut.pairsList[pairsIndexList[i]] |
|
785 | 786 | |
|
786 | 787 | ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0) |
|
787 | 788 | powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0) |
|
788 | 789 | powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0) |
|
789 | 790 | |
|
790 | 791 | |
|
791 | 792 | avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) |
|
792 | 793 | coherence = numpy.abs(avgcoherenceComplex) |
|
793 | 794 | |
|
794 | 795 | z = coherence.reshape((1,-1)) |
|
795 | 796 | |
|
796 | 797 | counter = 0 |
|
797 | 798 | |
|
798 | 799 | title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
799 | 800 | axes = self.axesList[i*self.__nsubplots*2] |
|
800 | 801 | axes.pcolorbuffer(x, y, z, |
|
801 | 802 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
802 | 803 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
803 | 804 | ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%") |
|
804 | 805 | |
|
805 | 806 | if self.__showprofile: |
|
806 | 807 | counter += 1 |
|
807 | 808 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
808 | 809 | axes.pline(coherence, y, |
|
809 | 810 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
810 | 811 | xlabel='', ylabel='', title='', ticksize=7, |
|
811 | 812 | ytick_visible=False, nxticks=5, |
|
812 | 813 | grid='x') |
|
813 | 814 | |
|
814 | 815 | counter += 1 |
|
815 | 816 | |
|
816 | 817 | phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi |
|
817 | 818 | |
|
818 | 819 | z = phase.reshape((1,-1)) |
|
819 | 820 | |
|
820 | 821 | title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
821 | 822 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
822 | 823 | axes.pcolorbuffer(x, y, z, |
|
823 | 824 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max, |
|
824 | 825 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
825 | 826 | ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%") |
|
826 | 827 | |
|
827 | 828 | if self.__showprofile: |
|
828 | 829 | counter += 1 |
|
829 | 830 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
830 | 831 | axes.pline(phase, y, |
|
831 | 832 | xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax, |
|
832 | 833 | xlabel='', ylabel='', title='', ticksize=7, |
|
833 | 834 | ytick_visible=False, nxticks=4, |
|
834 | 835 | grid='x') |
|
835 | 836 | |
|
836 | 837 | self.draw() |
|
837 | 838 | |
|
838 | 839 | if dataOut.ltctime >= self.xmax: |
|
839 | 840 | self.counter_imagwr = wr_period |
|
840 | 841 | self.isConfig = False |
|
841 | 842 | update_figfile = True |
|
842 | 843 | |
|
843 | 844 | self.save(figpath=figpath, |
|
844 | 845 | figfile=figfile, |
|
845 | 846 | save=save, |
|
846 | 847 | ftp=ftp, |
|
847 | 848 | wr_period=wr_period, |
|
848 | 849 | thisDatetime=thisDatetime, |
|
849 | 850 | update_figfile=update_figfile) |
|
850 | 851 | |
|
851 | 852 | class PowerProfilePlot(Figure): |
|
852 | 853 | |
|
853 | 854 | isConfig = None |
|
854 | 855 | __nsubplots = None |
|
855 | 856 | |
|
856 | 857 | WIDTHPROF = None |
|
857 | 858 | HEIGHTPROF = None |
|
858 | 859 | PREFIX = 'spcprofile' |
|
859 | 860 | |
|
860 | 861 | def __init__(self, **kwargs): |
|
861 | 862 | Figure.__init__(self, **kwargs) |
|
862 | 863 | self.isConfig = False |
|
863 | 864 | self.__nsubplots = 1 |
|
864 | 865 | |
|
865 | 866 | self.PLOT_CODE = POWER_CODE |
|
866 | 867 | |
|
867 | 868 | self.WIDTH = 300 |
|
868 | 869 | self.HEIGHT = 500 |
|
869 | 870 | self.counter_imagwr = 0 |
|
870 | 871 | |
|
871 | 872 | def getSubplots(self): |
|
872 | 873 | ncol = 1 |
|
873 | 874 | nrow = 1 |
|
874 | 875 | |
|
875 | 876 | return nrow, ncol |
|
876 | 877 | |
|
877 | 878 | def setup(self, id, nplots, wintitle, show): |
|
878 | 879 | |
|
879 | 880 | self.nplots = nplots |
|
880 | 881 | |
|
881 | 882 | ncolspan = 1 |
|
882 | 883 | colspan = 1 |
|
883 | 884 | |
|
884 | 885 | self.createFigure(id = id, |
|
885 | 886 | wintitle = wintitle, |
|
886 | 887 | widthplot = self.WIDTH, |
|
887 | 888 | heightplot = self.HEIGHT, |
|
888 | 889 | show=show) |
|
889 | 890 | |
|
890 | 891 | nrow, ncol = self.getSubplots() |
|
891 | 892 | |
|
892 | 893 | counter = 0 |
|
893 | 894 | for y in range(nrow): |
|
894 | 895 | for x in range(ncol): |
|
895 | 896 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
896 | 897 | |
|
897 | 898 | def run(self, dataOut, id, wintitle="", channelList=None, |
|
898 | 899 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
899 | 900 | save=False, figpath='./', figfile=None, show=True, |
|
900 | 901 | ftp=False, wr_period=1, server=None, |
|
901 | 902 | folder=None, username=None, password=None): |
|
902 | 903 | |
|
903 | 904 | |
|
904 | 905 | if channelList == None: |
|
905 | 906 | channelIndexList = dataOut.channelIndexList |
|
906 | 907 | channelList = dataOut.channelList |
|
907 | 908 | else: |
|
908 | 909 | channelIndexList = [] |
|
909 | 910 | for channel in channelList: |
|
910 | 911 | if channel not in dataOut.channelList: |
|
911 | 912 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
912 | 913 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
913 | 914 | |
|
914 | 915 | factor = dataOut.normFactor |
|
915 | 916 | |
|
916 | 917 | y = dataOut.getHeiRange() |
|
917 | 918 | |
|
918 | 919 | #for voltage |
|
919 | 920 | if dataOut.type == 'Voltage': |
|
920 | 921 | x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) |
|
921 | 922 | x = x.real |
|
922 | 923 | x = numpy.where(numpy.isfinite(x), x, numpy.NAN) |
|
923 | 924 | |
|
924 | 925 | #for spectra |
|
925 | 926 | if dataOut.type == 'Spectra': |
|
926 | 927 | x = dataOut.data_spc[channelIndexList,:,:]/factor |
|
927 | 928 | x = numpy.where(numpy.isfinite(x), x, numpy.NAN) |
|
928 | 929 | x = numpy.average(x, axis=1) |
|
929 | 930 | |
|
930 | 931 | |
|
931 | 932 | xdB = 10*numpy.log10(x) |
|
932 | 933 | |
|
933 | 934 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
934 | 935 | title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
935 | 936 | xlabel = "dB" |
|
936 | 937 | ylabel = "Range (Km)" |
|
937 | 938 | |
|
938 | 939 | if not self.isConfig: |
|
939 | 940 | |
|
940 | 941 | nplots = 1 |
|
941 | 942 | |
|
942 | 943 | self.setup(id=id, |
|
943 | 944 | nplots=nplots, |
|
944 | 945 | wintitle=wintitle, |
|
945 | 946 | show=show) |
|
946 | 947 | |
|
947 | 948 | if ymin == None: ymin = numpy.nanmin(y) |
|
948 | 949 | if ymax == None: ymax = numpy.nanmax(y) |
|
949 | 950 | if xmin == None: xmin = numpy.nanmin(xdB)*0.9 |
|
950 | 951 | if xmax == None: xmax = numpy.nanmax(xdB)*1.1 |
|
951 | 952 | |
|
952 | 953 | self.isConfig = True |
|
953 | 954 | |
|
954 | 955 | self.setWinTitle(title) |
|
955 | 956 | |
|
956 | 957 | title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
957 | 958 | axes = self.axesList[0] |
|
958 | 959 | |
|
959 | 960 | legendlabels = ["channel %d"%x for x in channelList] |
|
960 | 961 | axes.pmultiline(xdB, y, |
|
961 | 962 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
962 | 963 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, |
|
963 | 964 | ytick_visible=True, nxticks=5, |
|
964 | 965 | grid='x') |
|
965 | 966 | |
|
966 | 967 | self.draw() |
|
967 | 968 | |
|
968 | 969 | self.save(figpath=figpath, |
|
969 | 970 | figfile=figfile, |
|
970 | 971 | save=save, |
|
971 | 972 | ftp=ftp, |
|
972 | 973 | wr_period=wr_period, |
|
973 | 974 | thisDatetime=thisDatetime) |
|
974 | 975 | |
|
975 | 976 | class SpectraCutPlot(Figure): |
|
976 | 977 | |
|
977 | 978 | isConfig = None |
|
978 | 979 | __nsubplots = None |
|
979 | 980 | |
|
980 | 981 | WIDTHPROF = None |
|
981 | 982 | HEIGHTPROF = None |
|
982 | 983 | PREFIX = 'spc_cut' |
|
983 | 984 | |
|
984 | 985 | def __init__(self, **kwargs): |
|
985 | 986 | Figure.__init__(self, **kwargs) |
|
986 | 987 | self.isConfig = False |
|
987 | 988 | self.__nsubplots = 1 |
|
988 | 989 | |
|
989 | 990 | self.PLOT_CODE = POWER_CODE |
|
990 | 991 | |
|
991 | 992 | self.WIDTH = 700 |
|
992 | 993 | self.HEIGHT = 500 |
|
993 | 994 | self.counter_imagwr = 0 |
|
994 | 995 | |
|
995 | 996 | def getSubplots(self): |
|
996 | 997 | ncol = 1 |
|
997 | 998 | nrow = 1 |
|
998 | 999 | |
|
999 | 1000 | return nrow, ncol |
|
1000 | 1001 | |
|
1001 | 1002 | def setup(self, id, nplots, wintitle, show): |
|
1002 | 1003 | |
|
1003 | 1004 | self.nplots = nplots |
|
1004 | 1005 | |
|
1005 | 1006 | ncolspan = 1 |
|
1006 | 1007 | colspan = 1 |
|
1007 | 1008 | |
|
1008 | 1009 | self.createFigure(id = id, |
|
1009 | 1010 | wintitle = wintitle, |
|
1010 | 1011 | widthplot = self.WIDTH, |
|
1011 | 1012 | heightplot = self.HEIGHT, |
|
1012 | 1013 | show=show) |
|
1013 | 1014 | |
|
1014 | 1015 | nrow, ncol = self.getSubplots() |
|
1015 | 1016 | |
|
1016 | 1017 | counter = 0 |
|
1017 | 1018 | for y in range(nrow): |
|
1018 | 1019 | for x in range(ncol): |
|
1019 | 1020 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
1020 | 1021 | |
|
1021 | 1022 | def run(self, dataOut, id, wintitle="", channelList=None, |
|
1022 | 1023 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
1023 | 1024 | save=False, figpath='./', figfile=None, show=True, |
|
1024 | 1025 | ftp=False, wr_period=1, server=None, |
|
1025 | 1026 | folder=None, username=None, password=None, |
|
1026 | 1027 | xaxis="frequency"): |
|
1027 | 1028 | |
|
1028 | 1029 | |
|
1029 | 1030 | if channelList == None: |
|
1030 | 1031 | channelIndexList = dataOut.channelIndexList |
|
1031 | 1032 | channelList = dataOut.channelList |
|
1032 | 1033 | else: |
|
1033 | 1034 | channelIndexList = [] |
|
1034 | 1035 | for channel in channelList: |
|
1035 | 1036 | if channel not in dataOut.channelList: |
|
1036 | 1037 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
1037 | 1038 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
1038 | 1039 | |
|
1039 | 1040 | factor = dataOut.normFactor |
|
1040 | 1041 | |
|
1041 | 1042 | y = dataOut.getHeiRange() |
|
1042 | 1043 | |
|
1043 | 1044 | z = dataOut.data_spc/factor |
|
1044 | 1045 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
1045 | 1046 | |
|
1046 | 1047 | hei_index = numpy.arange(25)*3 + 20 |
|
1047 | 1048 | |
|
1048 | 1049 | if xaxis == "frequency": |
|
1049 | 1050 | x = dataOut.getFreqRange()/1000. |
|
1050 | 1051 | zdB = 10*numpy.log10(z[0,:,hei_index]) |
|
1051 | 1052 | xlabel = "Frequency (kHz)" |
|
1052 | 1053 | ylabel = "Power (dB)" |
|
1053 | 1054 | |
|
1054 | 1055 | elif xaxis == "time": |
|
1055 | 1056 | x = dataOut.getAcfRange() |
|
1056 | 1057 | zdB = z[0,:,hei_index] |
|
1057 | 1058 | xlabel = "Time (ms)" |
|
1058 | 1059 | ylabel = "ACF" |
|
1059 | 1060 | |
|
1060 | 1061 | else: |
|
1061 | 1062 | x = dataOut.getVelRange() |
|
1062 | 1063 | zdB = 10*numpy.log10(z[0,:,hei_index]) |
|
1063 | 1064 | xlabel = "Velocity (m/s)" |
|
1064 | 1065 | ylabel = "Power (dB)" |
|
1065 | 1066 | |
|
1066 | 1067 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0]) |
|
1067 | 1068 | title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
1068 | 1069 | |
|
1069 | 1070 | if not self.isConfig: |
|
1070 | 1071 | |
|
1071 | 1072 | nplots = 1 |
|
1072 | 1073 | |
|
1073 | 1074 | self.setup(id=id, |
|
1074 | 1075 | nplots=nplots, |
|
1075 | 1076 | wintitle=wintitle, |
|
1076 | 1077 | show=show) |
|
1077 | 1078 | |
|
1078 | 1079 | if xmin == None: xmin = numpy.nanmin(x)*0.9 |
|
1079 | 1080 | if xmax == None: xmax = numpy.nanmax(x)*1.1 |
|
1080 | 1081 | if ymin == None: ymin = numpy.nanmin(zdB) |
|
1081 | 1082 | if ymax == None: ymax = numpy.nanmax(zdB) |
|
1082 | 1083 | |
|
1083 | 1084 | self.isConfig = True |
|
1084 | 1085 | |
|
1085 | 1086 | self.setWinTitle(title) |
|
1086 | 1087 | |
|
1087 | 1088 | title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
1088 | 1089 | axes = self.axesList[0] |
|
1089 | 1090 | |
|
1090 | 1091 | legendlabels = ["Range = %dKm" %y[i] for i in hei_index] |
|
1091 | 1092 | |
|
1092 | 1093 | axes.pmultilineyaxis( x, zdB, |
|
1093 | 1094 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
1094 | 1095 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, |
|
1095 | 1096 | ytick_visible=True, nxticks=5, |
|
1096 | 1097 | grid='x') |
|
1097 | 1098 | |
|
1098 | 1099 | self.draw() |
|
1099 | 1100 | |
|
1100 | 1101 | self.save(figpath=figpath, |
|
1101 | 1102 | figfile=figfile, |
|
1102 | 1103 | save=save, |
|
1103 | 1104 | ftp=ftp, |
|
1104 | 1105 | wr_period=wr_period, |
|
1105 | 1106 | thisDatetime=thisDatetime) |
|
1106 | 1107 | |
|
1107 | 1108 | class Noise(Figure): |
|
1108 | 1109 | |
|
1109 | 1110 | isConfig = None |
|
1110 | 1111 | __nsubplots = None |
|
1111 | 1112 | |
|
1112 | 1113 | PREFIX = 'noise' |
|
1113 | 1114 | |
|
1114 | 1115 | def __init__(self, **kwargs): |
|
1115 | 1116 | Figure.__init__(self, **kwargs) |
|
1116 | 1117 | self.timerange = 24*60*60 |
|
1117 | 1118 | self.isConfig = False |
|
1118 | 1119 | self.__nsubplots = 1 |
|
1119 | 1120 | self.counter_imagwr = 0 |
|
1120 | 1121 | self.WIDTH = 800 |
|
1121 | 1122 | self.HEIGHT = 400 |
|
1122 | 1123 | self.WIDTHPROF = 120 |
|
1123 | 1124 | self.HEIGHTPROF = 0 |
|
1124 | 1125 | self.xdata = None |
|
1125 | 1126 | self.ydata = None |
|
1126 | 1127 | |
|
1127 | 1128 | self.PLOT_CODE = NOISE_CODE |
|
1128 | 1129 | |
|
1129 | 1130 | self.FTP_WEI = None |
|
1130 | 1131 | self.EXP_CODE = None |
|
1131 | 1132 | self.SUB_EXP_CODE = None |
|
1132 | 1133 | self.PLOT_POS = None |
|
1133 | 1134 | self.figfile = None |
|
1134 | 1135 | |
|
1135 | 1136 | self.xmin = None |
|
1136 | 1137 | self.xmax = None |
|
1137 | 1138 | |
|
1138 | 1139 | def getSubplots(self): |
|
1139 | 1140 | |
|
1140 | 1141 | ncol = 1 |
|
1141 | 1142 | nrow = 1 |
|
1142 | 1143 | |
|
1143 | 1144 | return nrow, ncol |
|
1144 | 1145 | |
|
1145 | 1146 | def openfile(self, filename): |
|
1146 | 1147 | dirname = os.path.dirname(filename) |
|
1147 | 1148 | |
|
1148 | 1149 | if not os.path.exists(dirname): |
|
1149 | 1150 | os.mkdir(dirname) |
|
1150 | 1151 | |
|
1151 | 1152 | f = open(filename,'w+') |
|
1152 | 1153 | f.write('\n\n') |
|
1153 | 1154 | f.write('JICAMARCA RADIO OBSERVATORY - Noise \n') |
|
1154 | 1155 | f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' ) |
|
1155 | 1156 | f.close() |
|
1156 | 1157 | |
|
1157 | 1158 | def save_data(self, filename_phase, data, data_datetime): |
|
1158 | 1159 | |
|
1159 | 1160 | f=open(filename_phase,'a') |
|
1160 | 1161 | |
|
1161 | 1162 | timetuple_data = data_datetime.timetuple() |
|
1162 | 1163 | day = str(timetuple_data.tm_mday) |
|
1163 | 1164 | month = str(timetuple_data.tm_mon) |
|
1164 | 1165 | year = str(timetuple_data.tm_year) |
|
1165 | 1166 | hour = str(timetuple_data.tm_hour) |
|
1166 | 1167 | minute = str(timetuple_data.tm_min) |
|
1167 | 1168 | second = str(timetuple_data.tm_sec) |
|
1168 | 1169 | |
|
1169 | 1170 | data_msg = '' |
|
1170 | 1171 | for i in range(len(data)): |
|
1171 | 1172 | data_msg += str(data[i]) + ' ' |
|
1172 | 1173 | |
|
1173 | 1174 | f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n') |
|
1174 | 1175 | f.close() |
|
1175 | 1176 | |
|
1176 | 1177 | |
|
1177 | 1178 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
1178 | 1179 | |
|
1179 | 1180 | self.__showprofile = showprofile |
|
1180 | 1181 | self.nplots = nplots |
|
1181 | 1182 | |
|
1182 | 1183 | ncolspan = 7 |
|
1183 | 1184 | colspan = 6 |
|
1184 | 1185 | self.__nsubplots = 2 |
|
1185 | 1186 | |
|
1186 | 1187 | self.createFigure(id = id, |
|
1187 | 1188 | wintitle = wintitle, |
|
1188 | 1189 | widthplot = self.WIDTH+self.WIDTHPROF, |
|
1189 | 1190 | heightplot = self.HEIGHT+self.HEIGHTPROF, |
|
1190 | 1191 | show=show) |
|
1191 | 1192 | |
|
1192 | 1193 | nrow, ncol = self.getSubplots() |
|
1193 | 1194 | |
|
1194 | 1195 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) |
|
1195 | 1196 | |
|
1196 | 1197 | |
|
1197 | 1198 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', |
|
1198 | 1199 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
1199 | 1200 | timerange=None, |
|
1200 | 1201 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
1201 | 1202 | server=None, folder=None, username=None, password=None, |
|
1202 | 1203 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
1203 | 1204 | |
|
1204 | 1205 | if not isTimeInHourRange(dataOut.datatime, xmin, xmax): |
|
1205 | 1206 | return |
|
1206 | 1207 | |
|
1207 | 1208 | if channelList == None: |
|
1208 | 1209 | channelIndexList = dataOut.channelIndexList |
|
1209 | 1210 | channelList = dataOut.channelList |
|
1210 | 1211 | else: |
|
1211 | 1212 | channelIndexList = [] |
|
1212 | 1213 | for channel in channelList: |
|
1213 | 1214 | if channel not in dataOut.channelList: |
|
1214 | 1215 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
1215 | 1216 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
1216 | 1217 | |
|
1217 | 1218 | x = dataOut.getTimeRange() |
|
1218 | 1219 | #y = dataOut.getHeiRange() |
|
1219 | 1220 | factor = dataOut.normFactor |
|
1220 | 1221 | noise = dataOut.noise[channelIndexList]/factor |
|
1221 | 1222 | noisedB = 10*numpy.log10(noise) |
|
1222 | 1223 | |
|
1223 | 1224 | thisDatetime = dataOut.datatime |
|
1224 | 1225 | |
|
1225 | 1226 | title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
1226 | 1227 | xlabel = "" |
|
1227 | 1228 | ylabel = "Intensity (dB)" |
|
1228 | 1229 | update_figfile = False |
|
1229 | 1230 | |
|
1230 | 1231 | if not self.isConfig: |
|
1231 | 1232 | |
|
1232 | 1233 | nplots = 1 |
|
1233 | 1234 | |
|
1234 | 1235 | self.setup(id=id, |
|
1235 | 1236 | nplots=nplots, |
|
1236 | 1237 | wintitle=wintitle, |
|
1237 | 1238 | showprofile=showprofile, |
|
1238 | 1239 | show=show) |
|
1239 | 1240 | |
|
1240 | 1241 | if timerange != None: |
|
1241 | 1242 | self.timerange = timerange |
|
1242 | 1243 | |
|
1243 | 1244 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
1244 | 1245 | |
|
1245 | 1246 | if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0 |
|
1246 | 1247 | if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0 |
|
1247 | 1248 | |
|
1248 | 1249 | self.FTP_WEI = ftp_wei |
|
1249 | 1250 | self.EXP_CODE = exp_code |
|
1250 | 1251 | self.SUB_EXP_CODE = sub_exp_code |
|
1251 | 1252 | self.PLOT_POS = plot_pos |
|
1252 | 1253 | |
|
1253 | 1254 | |
|
1254 | 1255 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1255 | 1256 | self.isConfig = True |
|
1256 | 1257 | self.figfile = figfile |
|
1257 | 1258 | self.xdata = numpy.array([]) |
|
1258 | 1259 | self.ydata = numpy.array([]) |
|
1259 | 1260 | |
|
1260 | 1261 | update_figfile = True |
|
1261 | 1262 | |
|
1262 | 1263 | #open file beacon phase |
|
1263 | 1264 | path = '%s%03d' %(self.PREFIX, self.id) |
|
1264 | 1265 | noise_file = os.path.join(path,'%s.txt'%self.name) |
|
1265 | 1266 | self.filename_noise = os.path.join(figpath,noise_file) |
|
1266 | 1267 | |
|
1267 | 1268 | self.setWinTitle(title) |
|
1268 | 1269 | |
|
1269 | 1270 | title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1270 | 1271 | |
|
1271 | 1272 | legendlabels = ["channel %d"%(idchannel) for idchannel in channelList] |
|
1272 | 1273 | axes = self.axesList[0] |
|
1273 | 1274 | |
|
1274 | 1275 | self.xdata = numpy.hstack((self.xdata, x[0:1])) |
|
1275 | 1276 | |
|
1276 | 1277 | if len(self.ydata)==0: |
|
1277 | 1278 | self.ydata = noisedB.reshape(-1,1) |
|
1278 | 1279 | else: |
|
1279 | 1280 | self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1))) |
|
1280 | 1281 | |
|
1281 | 1282 | |
|
1282 | 1283 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, |
|
1283 | 1284 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, |
|
1284 | 1285 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", |
|
1285 | 1286 | XAxisAsTime=True, grid='both' |
|
1286 | 1287 | ) |
|
1287 | 1288 | |
|
1288 | 1289 | self.draw() |
|
1289 | 1290 | |
|
1290 | 1291 | if dataOut.ltctime >= self.xmax: |
|
1291 | 1292 | self.counter_imagwr = wr_period |
|
1292 | 1293 | self.isConfig = False |
|
1293 | 1294 | update_figfile = True |
|
1294 | 1295 | |
|
1295 | 1296 | self.save(figpath=figpath, |
|
1296 | 1297 | figfile=figfile, |
|
1297 | 1298 | save=save, |
|
1298 | 1299 | ftp=ftp, |
|
1299 | 1300 | wr_period=wr_period, |
|
1300 | 1301 | thisDatetime=thisDatetime, |
|
1301 | 1302 | update_figfile=update_figfile) |
|
1302 | 1303 | |
|
1303 | 1304 | #store data beacon phase |
|
1304 | 1305 | if save: |
|
1305 | 1306 | self.save_data(self.filename_noise, noisedB, thisDatetime) |
|
1306 | 1307 | |
|
1307 | 1308 | class BeaconPhase(Figure): |
|
1308 | 1309 | |
|
1309 | 1310 | __isConfig = None |
|
1310 | 1311 | __nsubplots = None |
|
1311 | 1312 | |
|
1312 | 1313 | PREFIX = 'beacon_phase' |
|
1313 | 1314 | |
|
1314 | 1315 | def __init__(self, **kwargs): |
|
1315 | 1316 | Figure.__init__(self, **kwargs) |
|
1316 | 1317 | self.timerange = 24*60*60 |
|
1317 | 1318 | self.isConfig = False |
|
1318 | 1319 | self.__nsubplots = 1 |
|
1319 | 1320 | self.counter_imagwr = 0 |
|
1320 | 1321 | self.WIDTH = 800 |
|
1321 | 1322 | self.HEIGHT = 400 |
|
1322 | 1323 | self.WIDTHPROF = 120 |
|
1323 | 1324 | self.HEIGHTPROF = 0 |
|
1324 | 1325 | self.xdata = None |
|
1325 | 1326 | self.ydata = None |
|
1326 | 1327 | |
|
1327 | 1328 | self.PLOT_CODE = BEACON_CODE |
|
1328 | 1329 | |
|
1329 | 1330 | self.FTP_WEI = None |
|
1330 | 1331 | self.EXP_CODE = None |
|
1331 | 1332 | self.SUB_EXP_CODE = None |
|
1332 | 1333 | self.PLOT_POS = None |
|
1333 | 1334 | |
|
1334 | 1335 | self.filename_phase = None |
|
1335 | 1336 | |
|
1336 | 1337 | self.figfile = None |
|
1337 | 1338 | |
|
1338 | 1339 | self.xmin = None |
|
1339 | 1340 | self.xmax = None |
|
1340 | 1341 | |
|
1341 | 1342 | def getSubplots(self): |
|
1342 | 1343 | |
|
1343 | 1344 | ncol = 1 |
|
1344 | 1345 | nrow = 1 |
|
1345 | 1346 | |
|
1346 | 1347 | return nrow, ncol |
|
1347 | 1348 | |
|
1348 | 1349 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): |
|
1349 | 1350 | |
|
1350 | 1351 | self.__showprofile = showprofile |
|
1351 | 1352 | self.nplots = nplots |
|
1352 | 1353 | |
|
1353 | 1354 | ncolspan = 7 |
|
1354 | 1355 | colspan = 6 |
|
1355 | 1356 | self.__nsubplots = 2 |
|
1356 | 1357 | |
|
1357 | 1358 | self.createFigure(id = id, |
|
1358 | 1359 | wintitle = wintitle, |
|
1359 | 1360 | widthplot = self.WIDTH+self.WIDTHPROF, |
|
1360 | 1361 | heightplot = self.HEIGHT+self.HEIGHTPROF, |
|
1361 | 1362 | show=show) |
|
1362 | 1363 | |
|
1363 | 1364 | nrow, ncol = self.getSubplots() |
|
1364 | 1365 | |
|
1365 | 1366 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) |
|
1366 | 1367 | |
|
1367 | 1368 | def save_phase(self, filename_phase): |
|
1368 | 1369 | f = open(filename_phase,'w+') |
|
1369 | 1370 | f.write('\n\n') |
|
1370 | 1371 | f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n') |
|
1371 | 1372 | f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' ) |
|
1372 | 1373 | f.close() |
|
1373 | 1374 | |
|
1374 | 1375 | def save_data(self, filename_phase, data, data_datetime): |
|
1375 | 1376 | f=open(filename_phase,'a') |
|
1376 | 1377 | timetuple_data = data_datetime.timetuple() |
|
1377 | 1378 | day = str(timetuple_data.tm_mday) |
|
1378 | 1379 | month = str(timetuple_data.tm_mon) |
|
1379 | 1380 | year = str(timetuple_data.tm_year) |
|
1380 | 1381 | hour = str(timetuple_data.tm_hour) |
|
1381 | 1382 | minute = str(timetuple_data.tm_min) |
|
1382 | 1383 | second = str(timetuple_data.tm_sec) |
|
1383 | 1384 | f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n') |
|
1384 | 1385 | f.close() |
|
1385 | 1386 | |
|
1386 | 1387 | |
|
1387 | 1388 | def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', |
|
1388 | 1389 | xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None, |
|
1389 | 1390 | timerange=None, |
|
1390 | 1391 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, |
|
1391 | 1392 | server=None, folder=None, username=None, password=None, |
|
1392 | 1393 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): |
|
1393 | 1394 | |
|
1394 | 1395 | if not isTimeInHourRange(dataOut.datatime, xmin, xmax): |
|
1395 | 1396 | return |
|
1396 | 1397 | |
|
1397 | 1398 | if pairsList == None: |
|
1398 | 1399 | pairsIndexList = dataOut.pairsIndexList[:10] |
|
1399 | 1400 | else: |
|
1400 | 1401 | pairsIndexList = [] |
|
1401 | 1402 | for pair in pairsList: |
|
1402 | 1403 | if pair not in dataOut.pairsList: |
|
1403 | 1404 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) |
|
1404 | 1405 | pairsIndexList.append(dataOut.pairsList.index(pair)) |
|
1405 | 1406 | |
|
1406 | 1407 | if pairsIndexList == []: |
|
1407 | 1408 | return |
|
1408 | 1409 | |
|
1409 | 1410 | # if len(pairsIndexList) > 4: |
|
1410 | 1411 | # pairsIndexList = pairsIndexList[0:4] |
|
1411 | 1412 | |
|
1412 | 1413 | hmin_index = None |
|
1413 | 1414 | hmax_index = None |
|
1414 | 1415 | |
|
1415 | 1416 | if hmin != None and hmax != None: |
|
1416 | 1417 | indexes = numpy.arange(dataOut.nHeights) |
|
1417 | 1418 | hmin_list = indexes[dataOut.heightList >= hmin] |
|
1418 | 1419 | hmax_list = indexes[dataOut.heightList <= hmax] |
|
1419 | 1420 | |
|
1420 | 1421 | if hmin_list.any(): |
|
1421 | 1422 | hmin_index = hmin_list[0] |
|
1422 | 1423 | |
|
1423 | 1424 | if hmax_list.any(): |
|
1424 | 1425 | hmax_index = hmax_list[-1]+1 |
|
1425 | 1426 | |
|
1426 | 1427 | x = dataOut.getTimeRange() |
|
1427 | 1428 | #y = dataOut.getHeiRange() |
|
1428 | 1429 | |
|
1429 | 1430 | |
|
1430 | 1431 | thisDatetime = dataOut.datatime |
|
1431 | 1432 | |
|
1432 | 1433 | title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
1433 | 1434 | xlabel = "Local Time" |
|
1434 | 1435 | ylabel = "Phase (degrees)" |
|
1435 | 1436 | |
|
1436 | 1437 | update_figfile = False |
|
1437 | 1438 | |
|
1438 | 1439 | nplots = len(pairsIndexList) |
|
1439 | 1440 | #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList))) |
|
1440 | 1441 | phase_beacon = numpy.zeros(len(pairsIndexList)) |
|
1441 | 1442 | for i in range(nplots): |
|
1442 | 1443 | pair = dataOut.pairsList[pairsIndexList[i]] |
|
1443 | 1444 | ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0) |
|
1444 | 1445 | powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0) |
|
1445 | 1446 | powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0) |
|
1446 | 1447 | avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) |
|
1447 | 1448 | phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi |
|
1448 | 1449 | |
|
1449 | 1450 | #print "Phase %d%d" %(pair[0], pair[1]) |
|
1450 | 1451 | #print phase[dataOut.beacon_heiIndexList] |
|
1451 | 1452 | |
|
1452 | 1453 | if dataOut.beacon_heiIndexList: |
|
1453 | 1454 | phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList]) |
|
1454 | 1455 | else: |
|
1455 | 1456 | phase_beacon[i] = numpy.average(phase) |
|
1456 | 1457 | |
|
1457 | 1458 | if not self.isConfig: |
|
1458 | 1459 | |
|
1459 | 1460 | nplots = len(pairsIndexList) |
|
1460 | 1461 | |
|
1461 | 1462 | self.setup(id=id, |
|
1462 | 1463 | nplots=nplots, |
|
1463 | 1464 | wintitle=wintitle, |
|
1464 | 1465 | showprofile=showprofile, |
|
1465 | 1466 | show=show) |
|
1466 | 1467 | |
|
1467 | 1468 | if timerange != None: |
|
1468 | 1469 | self.timerange = timerange |
|
1469 | 1470 | |
|
1470 | 1471 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) |
|
1471 | 1472 | |
|
1472 | 1473 | if ymin == None: ymin = 0 |
|
1473 | 1474 | if ymax == None: ymax = 360 |
|
1474 | 1475 | |
|
1475 | 1476 | self.FTP_WEI = ftp_wei |
|
1476 | 1477 | self.EXP_CODE = exp_code |
|
1477 | 1478 | self.SUB_EXP_CODE = sub_exp_code |
|
1478 | 1479 | self.PLOT_POS = plot_pos |
|
1479 | 1480 | |
|
1480 | 1481 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
1481 | 1482 | self.isConfig = True |
|
1482 | 1483 | self.figfile = figfile |
|
1483 | 1484 | self.xdata = numpy.array([]) |
|
1484 | 1485 | self.ydata = numpy.array([]) |
|
1485 | 1486 | |
|
1486 | 1487 | update_figfile = True |
|
1487 | 1488 | |
|
1488 | 1489 | #open file beacon phase |
|
1489 | 1490 | path = '%s%03d' %(self.PREFIX, self.id) |
|
1490 | 1491 | beacon_file = os.path.join(path,'%s.txt'%self.name) |
|
1491 | 1492 | self.filename_phase = os.path.join(figpath,beacon_file) |
|
1492 | 1493 | #self.save_phase(self.filename_phase) |
|
1493 | 1494 | |
|
1494 | 1495 | |
|
1495 | 1496 | #store data beacon phase |
|
1496 | 1497 | #self.save_data(self.filename_phase, phase_beacon, thisDatetime) |
|
1497 | 1498 | |
|
1498 | 1499 | self.setWinTitle(title) |
|
1499 | 1500 | |
|
1500 | 1501 | |
|
1501 | 1502 | title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) |
|
1502 | 1503 | |
|
1503 | 1504 | legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList] |
|
1504 | 1505 | |
|
1505 | 1506 | axes = self.axesList[0] |
|
1506 | 1507 | |
|
1507 | 1508 | self.xdata = numpy.hstack((self.xdata, x[0:1])) |
|
1508 | 1509 | |
|
1509 | 1510 | if len(self.ydata)==0: |
|
1510 | 1511 | self.ydata = phase_beacon.reshape(-1,1) |
|
1511 | 1512 | else: |
|
1512 | 1513 | self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1))) |
|
1513 | 1514 | |
|
1514 | 1515 | |
|
1515 | 1516 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, |
|
1516 | 1517 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, |
|
1517 | 1518 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", |
|
1518 | 1519 | XAxisAsTime=True, grid='both' |
|
1519 | 1520 | ) |
|
1520 | 1521 | |
|
1521 | 1522 | self.draw() |
|
1522 | 1523 | |
|
1523 | 1524 | if dataOut.ltctime >= self.xmax: |
|
1524 | 1525 | self.counter_imagwr = wr_period |
|
1525 | 1526 | self.isConfig = False |
|
1526 | 1527 | update_figfile = True |
|
1527 | 1528 | |
|
1528 | 1529 | self.save(figpath=figpath, |
|
1529 | 1530 | figfile=figfile, |
|
1530 | 1531 | save=save, |
|
1531 | 1532 | ftp=ftp, |
|
1532 | 1533 | wr_period=wr_period, |
|
1533 | 1534 | thisDatetime=thisDatetime, |
|
1534 | 1535 | update_figfile=update_figfile) |
@@ -1,1779 +1,1855 | |||
|
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, datetime |
|
14 | 14 | import traceback |
|
15 | 15 | import zmq |
|
16 | 16 | |
|
17 | 17 | try: |
|
18 | 18 | from gevent import sleep |
|
19 | 19 | except: |
|
20 | 20 | from time import sleep |
|
21 | 21 | |
|
22 | 22 | from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader |
|
23 | 23 | from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width |
|
24 | 24 | |
|
25 | 25 | LOCALTIME = True |
|
26 | 26 | |
|
27 | 27 | def isNumber(cad): |
|
28 | 28 | """ |
|
29 | 29 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
30 | 30 | |
|
31 | 31 | Excepciones: |
|
32 | 32 | Si un determinado string no puede ser convertido a numero |
|
33 | 33 | Input: |
|
34 | 34 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
35 | 35 | |
|
36 | 36 | Return: |
|
37 | 37 | True : si el string es uno numerico |
|
38 | 38 | False : no es un string numerico |
|
39 | 39 | """ |
|
40 | 40 | try: |
|
41 | 41 | float( cad ) |
|
42 | 42 | return True |
|
43 | 43 | except: |
|
44 | 44 | return False |
|
45 | 45 | |
|
46 | 46 | def isFileInEpoch(filename, startUTSeconds, endUTSeconds): |
|
47 | 47 | """ |
|
48 | 48 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
49 | 49 | |
|
50 | 50 | Inputs: |
|
51 | 51 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
52 | 52 | |
|
53 | 53 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
54 | 54 | segundos contados desde 01/01/1970. |
|
55 | 55 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
56 | 56 | segundos contados desde 01/01/1970. |
|
57 | 57 | |
|
58 | 58 | Return: |
|
59 | 59 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
60 | 60 | fecha especificado, de lo contrario retorna False. |
|
61 | 61 | |
|
62 | 62 | Excepciones: |
|
63 | 63 | Si el archivo no existe o no puede ser abierto |
|
64 | 64 | Si la cabecera no puede ser leida. |
|
65 | 65 | |
|
66 | 66 | """ |
|
67 | 67 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
68 | 68 | |
|
69 | 69 | try: |
|
70 | 70 | fp = open(filename,'rb') |
|
71 | 71 | except IOError: |
|
72 | 72 | print "The file %s can't be opened" %(filename) |
|
73 | 73 | return 0 |
|
74 | 74 | |
|
75 | 75 | sts = basicHeaderObj.read(fp) |
|
76 | 76 | fp.close() |
|
77 | 77 | |
|
78 | 78 | if not(sts): |
|
79 | 79 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
80 | 80 | return 0 |
|
81 | 81 | |
|
82 | 82 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): |
|
83 | 83 | return 0 |
|
84 | 84 | |
|
85 | 85 | return 1 |
|
86 | 86 | |
|
87 | 87 | def isTimeInRange(thisTime, startTime, endTime): |
|
88 | 88 | |
|
89 | 89 | if endTime >= startTime: |
|
90 | 90 | if (thisTime < startTime) or (thisTime > endTime): |
|
91 | 91 | return 0 |
|
92 | 92 | |
|
93 | 93 | return 1 |
|
94 | 94 | else: |
|
95 | 95 | if (thisTime < startTime) and (thisTime > endTime): |
|
96 | 96 | return 0 |
|
97 | 97 | |
|
98 | 98 | return 1 |
|
99 | 99 | |
|
100 | 100 | def isFileInTimeRange(filename, startDate, endDate, startTime, endTime): |
|
101 | 101 | """ |
|
102 | 102 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
103 | 103 | |
|
104 | 104 | Inputs: |
|
105 | 105 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
106 | 106 | |
|
107 | 107 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
108 | 108 | |
|
109 | 109 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
110 | 110 | |
|
111 | 111 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
112 | 112 | |
|
113 | 113 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
114 | 114 | |
|
115 | 115 | Return: |
|
116 | 116 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
117 | 117 | fecha especificado, de lo contrario retorna False. |
|
118 | 118 | |
|
119 | 119 | Excepciones: |
|
120 | 120 | Si el archivo no existe o no puede ser abierto |
|
121 | 121 | Si la cabecera no puede ser leida. |
|
122 | 122 | |
|
123 | 123 | """ |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | try: |
|
127 | 127 | fp = open(filename,'rb') |
|
128 | 128 | except IOError: |
|
129 | 129 | print "The file %s can't be opened" %(filename) |
|
130 | 130 | return None |
|
131 | 131 | |
|
132 | 132 | firstBasicHeaderObj = BasicHeader(LOCALTIME) |
|
133 | 133 | systemHeaderObj = SystemHeader() |
|
134 | 134 | radarControllerHeaderObj = RadarControllerHeader() |
|
135 | 135 | processingHeaderObj = ProcessingHeader() |
|
136 | 136 | |
|
137 | 137 | lastBasicHeaderObj = BasicHeader(LOCALTIME) |
|
138 | 138 | |
|
139 | 139 | sts = firstBasicHeaderObj.read(fp) |
|
140 | 140 | |
|
141 | 141 | if not(sts): |
|
142 | 142 | print "[Reading] Skipping the file %s because it has not a valid header" %(filename) |
|
143 | 143 | return None |
|
144 | 144 | |
|
145 | 145 | if not systemHeaderObj.read(fp): |
|
146 | 146 | return None |
|
147 | 147 | |
|
148 | 148 | if not radarControllerHeaderObj.read(fp): |
|
149 | 149 | return None |
|
150 | 150 | |
|
151 | 151 | if not processingHeaderObj.read(fp): |
|
152 | 152 | return None |
|
153 | 153 | |
|
154 | 154 | filesize = os.path.getsize(filename) |
|
155 | 155 | |
|
156 | 156 | offset = processingHeaderObj.blockSize + 24 #header size |
|
157 | 157 | |
|
158 | 158 | if filesize <= offset: |
|
159 | 159 | print "[Reading] %s: This file has not enough data" %filename |
|
160 | 160 | return None |
|
161 | 161 | |
|
162 | 162 | fp.seek(-offset, 2) |
|
163 | 163 | |
|
164 | 164 | sts = lastBasicHeaderObj.read(fp) |
|
165 | 165 | |
|
166 | 166 | fp.close() |
|
167 | 167 | |
|
168 | 168 | thisDatetime = lastBasicHeaderObj.datatime |
|
169 | 169 | thisTime_last_block = thisDatetime.time() |
|
170 | 170 | |
|
171 | 171 | thisDatetime = firstBasicHeaderObj.datatime |
|
172 | 172 | thisDate = thisDatetime.date() |
|
173 | 173 | thisTime_first_block = thisDatetime.time() |
|
174 | 174 | |
|
175 | 175 | #General case |
|
176 | 176 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o |
|
177 | 177 | #-----------o----------------------------o----------- |
|
178 | 178 | # startTime endTime |
|
179 | 179 | |
|
180 | 180 | if endTime >= startTime: |
|
181 | 181 | if (thisTime_last_block < startTime) or (thisTime_first_block > endTime): |
|
182 | 182 | return None |
|
183 | 183 | |
|
184 | 184 | return thisDatetime |
|
185 | 185 | |
|
186 | 186 | #If endTime < startTime then endTime belongs to the next day |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | #<<<<<<<<<<<o o>>>>>>>>>>> |
|
190 | 190 | #-----------o----------------------------o----------- |
|
191 | 191 | # endTime startTime |
|
192 | 192 | |
|
193 | 193 | if (thisDate == startDate) and (thisTime_last_block < startTime): |
|
194 | 194 | return None |
|
195 | 195 | |
|
196 | 196 | if (thisDate == endDate) and (thisTime_first_block > endTime): |
|
197 | 197 | return None |
|
198 | 198 | |
|
199 | 199 | if (thisTime_last_block < startTime) and (thisTime_first_block > endTime): |
|
200 | 200 | return None |
|
201 | 201 | |
|
202 | 202 | return thisDatetime |
|
203 | 203 | |
|
204 | 204 | def isFolderInDateRange(folder, startDate=None, endDate=None): |
|
205 | 205 | """ |
|
206 | 206 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
207 | 207 | |
|
208 | 208 | Inputs: |
|
209 | 209 | folder : nombre completo del directorio. |
|
210 | 210 | Su formato deberia ser "/path_root/?YYYYDDD" |
|
211 | 211 | |
|
212 | 212 | siendo: |
|
213 | 213 | YYYY : Anio (ejemplo 2015) |
|
214 | 214 | DDD : Dia del anio (ejemplo 305) |
|
215 | 215 | |
|
216 | 216 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
217 | 217 | |
|
218 | 218 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
219 | 219 | |
|
220 | 220 | Return: |
|
221 | 221 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
222 | 222 | fecha especificado, de lo contrario retorna False. |
|
223 | 223 | Excepciones: |
|
224 | 224 | Si el directorio no tiene el formato adecuado |
|
225 | 225 | """ |
|
226 | 226 | |
|
227 | 227 | basename = os.path.basename(folder) |
|
228 | 228 | |
|
229 | 229 | if not isRadarFolder(basename): |
|
230 | 230 | print "The folder %s has not the rigth format" %folder |
|
231 | 231 | return 0 |
|
232 | 232 | |
|
233 | 233 | if startDate and endDate: |
|
234 | 234 | thisDate = getDateFromRadarFolder(basename) |
|
235 | 235 | |
|
236 | 236 | if thisDate < startDate: |
|
237 | 237 | return 0 |
|
238 | 238 | |
|
239 | 239 | if thisDate > endDate: |
|
240 | 240 | return 0 |
|
241 | 241 | |
|
242 | 242 | return 1 |
|
243 | 243 | |
|
244 | 244 | def isFileInDateRange(filename, startDate=None, endDate=None): |
|
245 | 245 | """ |
|
246 | 246 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
247 | 247 | |
|
248 | 248 | Inputs: |
|
249 | 249 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
250 | 250 | |
|
251 | 251 | Su formato deberia ser "?YYYYDDDsss" |
|
252 | 252 | |
|
253 | 253 | siendo: |
|
254 | 254 | YYYY : Anio (ejemplo 2015) |
|
255 | 255 | DDD : Dia del anio (ejemplo 305) |
|
256 | 256 | sss : set |
|
257 | 257 | |
|
258 | 258 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
259 | 259 | |
|
260 | 260 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
261 | 261 | |
|
262 | 262 | Return: |
|
263 | 263 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
264 | 264 | fecha especificado, de lo contrario retorna False. |
|
265 | 265 | Excepciones: |
|
266 | 266 | Si el archivo no tiene el formato adecuado |
|
267 | 267 | """ |
|
268 | 268 | |
|
269 | 269 | basename = os.path.basename(filename) |
|
270 | 270 | |
|
271 | 271 | if not isRadarFile(basename): |
|
272 | 272 | print "The filename %s has not the rigth format" %filename |
|
273 | 273 | return 0 |
|
274 | 274 | |
|
275 | 275 | if startDate and endDate: |
|
276 | 276 | thisDate = getDateFromRadarFile(basename) |
|
277 | 277 | |
|
278 | 278 | if thisDate < startDate: |
|
279 | 279 | return 0 |
|
280 | 280 | |
|
281 | 281 | if thisDate > endDate: |
|
282 | 282 | return 0 |
|
283 | 283 | |
|
284 | 284 | return 1 |
|
285 | 285 | |
|
286 | 286 | def getFileFromSet(path, ext, set): |
|
287 | 287 | validFilelist = [] |
|
288 | 288 | fileList = os.listdir(path) |
|
289 | 289 | |
|
290 | 290 | # 0 1234 567 89A BCDE |
|
291 | 291 | # H YYYY DDD SSS .ext |
|
292 | 292 | |
|
293 | 293 | for thisFile in fileList: |
|
294 | 294 | try: |
|
295 | 295 | year = int(thisFile[1:5]) |
|
296 | 296 | doy = int(thisFile[5:8]) |
|
297 | 297 | except: |
|
298 | 298 | continue |
|
299 | 299 | |
|
300 | 300 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
301 | 301 | continue |
|
302 | 302 | |
|
303 | 303 | validFilelist.append(thisFile) |
|
304 | 304 | |
|
305 | 305 | myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set)) |
|
306 | 306 | |
|
307 | 307 | if len(myfile)!= 0: |
|
308 | 308 | return myfile[0] |
|
309 | 309 | else: |
|
310 | 310 | filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower()) |
|
311 | 311 | print 'the filename %s does not exist'%filename |
|
312 | 312 | print '...going to the last file: ' |
|
313 | 313 | |
|
314 | 314 | if validFilelist: |
|
315 | 315 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
316 | 316 | return validFilelist[-1] |
|
317 | 317 | |
|
318 | 318 | return None |
|
319 | 319 | |
|
320 | 320 | def getlastFileFromPath(path, ext): |
|
321 | 321 | """ |
|
322 | 322 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
323 | 323 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
324 | 324 | |
|
325 | 325 | Input: |
|
326 | 326 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
327 | 327 | ext : extension de los files contenidos en una carpeta |
|
328 | 328 | |
|
329 | 329 | Return: |
|
330 | 330 | El ultimo file de una determinada carpeta, no se considera el path. |
|
331 | 331 | """ |
|
332 | 332 | validFilelist = [] |
|
333 | 333 | fileList = os.listdir(path) |
|
334 | 334 | |
|
335 | 335 | # 0 1234 567 89A BCDE |
|
336 | 336 | # H YYYY DDD SSS .ext |
|
337 | 337 | |
|
338 | 338 | for thisFile in fileList: |
|
339 | 339 | |
|
340 | 340 | year = thisFile[1:5] |
|
341 | 341 | if not isNumber(year): |
|
342 | 342 | continue |
|
343 | 343 | |
|
344 | 344 | doy = thisFile[5:8] |
|
345 | 345 | if not isNumber(doy): |
|
346 | 346 | continue |
|
347 | 347 | |
|
348 | 348 | year = int(year) |
|
349 | 349 | doy = int(doy) |
|
350 | 350 | |
|
351 | 351 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
352 | 352 | continue |
|
353 | 353 | |
|
354 | 354 | validFilelist.append(thisFile) |
|
355 | 355 | |
|
356 | 356 | if validFilelist: |
|
357 | 357 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
358 | 358 | return validFilelist[-1] |
|
359 | 359 | |
|
360 | 360 | return None |
|
361 | 361 | |
|
362 | 362 | def checkForRealPath(path, foldercounter, year, doy, set, ext): |
|
363 | 363 | """ |
|
364 | 364 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
365 | 365 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
366 | 366 | el path exacto de un determinado file. |
|
367 | 367 | |
|
368 | 368 | Example : |
|
369 | 369 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
370 | 370 | |
|
371 | 371 | Entonces la funcion prueba con las siguientes combinaciones |
|
372 | 372 | .../.../y2009307367.ext |
|
373 | 373 | .../.../Y2009307367.ext |
|
374 | 374 | .../.../x2009307/y2009307367.ext |
|
375 | 375 | .../.../x2009307/Y2009307367.ext |
|
376 | 376 | .../.../X2009307/y2009307367.ext |
|
377 | 377 | .../.../X2009307/Y2009307367.ext |
|
378 | 378 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
379 | 379 | |
|
380 | 380 | Return: |
|
381 | 381 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
382 | 382 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
383 | 383 | para el filename |
|
384 | 384 | """ |
|
385 | 385 | fullfilename = None |
|
386 | 386 | find_flag = False |
|
387 | 387 | filename = None |
|
388 | 388 | |
|
389 | 389 | prefixDirList = [None,'d','D'] |
|
390 | 390 | if ext.lower() == ".r": #voltage |
|
391 | 391 | prefixFileList = ['d','D'] |
|
392 | 392 | elif ext.lower() == ".pdata": #spectra |
|
393 | 393 | prefixFileList = ['p','P'] |
|
394 | 394 | else: |
|
395 | 395 | return None, filename |
|
396 | 396 | |
|
397 | 397 | #barrido por las combinaciones posibles |
|
398 | 398 | for prefixDir in prefixDirList: |
|
399 | 399 | thispath = path |
|
400 | 400 | if prefixDir != None: |
|
401 | 401 | #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
402 | 402 | if foldercounter == 0: |
|
403 | 403 | thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy )) |
|
404 | 404 | else: |
|
405 | 405 | thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter)) |
|
406 | 406 | for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D" |
|
407 | 407 | filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext |
|
408 | 408 | fullfilename = os.path.join( thispath, filename ) #formo el path completo |
|
409 | 409 | |
|
410 | 410 | if os.path.exists( fullfilename ): #verifico que exista |
|
411 | 411 | find_flag = True |
|
412 | 412 | break |
|
413 | 413 | if find_flag: |
|
414 | 414 | break |
|
415 | 415 | |
|
416 | 416 | if not(find_flag): |
|
417 | 417 | return None, filename |
|
418 | 418 | |
|
419 | 419 | return fullfilename, filename |
|
420 | 420 | |
|
421 | 421 | def isRadarFolder(folder): |
|
422 | 422 | try: |
|
423 | 423 | year = int(folder[1:5]) |
|
424 | 424 | doy = int(folder[5:8]) |
|
425 | 425 | except: |
|
426 | 426 | return 0 |
|
427 | 427 | |
|
428 | 428 | return 1 |
|
429 | 429 | |
|
430 | 430 | def isRadarFile(file): |
|
431 | 431 | try: |
|
432 | 432 | year = int(file[1:5]) |
|
433 | 433 | doy = int(file[5:8]) |
|
434 | 434 | set = int(file[8:11]) |
|
435 | 435 | except: |
|
436 | 436 | return 0 |
|
437 | 437 | |
|
438 | 438 | return 1 |
|
439 | 439 | |
|
440 | 440 | def getDateFromRadarFile(file): |
|
441 | 441 | try: |
|
442 | 442 | year = int(file[1:5]) |
|
443 | 443 | doy = int(file[5:8]) |
|
444 | 444 | set = int(file[8:11]) |
|
445 | 445 | except: |
|
446 | 446 | return None |
|
447 | 447 | |
|
448 | 448 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1) |
|
449 | 449 | return thisDate |
|
450 | 450 | |
|
451 | 451 | def getDateFromRadarFolder(folder): |
|
452 | 452 | try: |
|
453 | 453 | year = int(folder[1:5]) |
|
454 | 454 | doy = int(folder[5:8]) |
|
455 | 455 | except: |
|
456 | 456 | return None |
|
457 | 457 | |
|
458 | 458 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1) |
|
459 | 459 | return thisDate |
|
460 | 460 | |
|
461 | 461 | class JRODataIO: |
|
462 | 462 | |
|
463 | 463 | c = 3E8 |
|
464 | 464 | |
|
465 | 465 | isConfig = False |
|
466 | 466 | |
|
467 | 467 | basicHeaderObj = None |
|
468 | 468 | |
|
469 | 469 | systemHeaderObj = None |
|
470 | 470 | |
|
471 | 471 | radarControllerHeaderObj = None |
|
472 | 472 | |
|
473 | 473 | processingHeaderObj = None |
|
474 | 474 | |
|
475 | 475 | dtype = None |
|
476 | 476 | |
|
477 | 477 | pathList = [] |
|
478 | 478 | |
|
479 | 479 | filenameList = [] |
|
480 | 480 | |
|
481 | 481 | filename = None |
|
482 | 482 | |
|
483 | 483 | ext = None |
|
484 | 484 | |
|
485 | 485 | flagIsNewFile = 1 |
|
486 | 486 | |
|
487 | 487 | flagDiscontinuousBlock = 0 |
|
488 | 488 | |
|
489 | 489 | flagIsNewBlock = 0 |
|
490 | 490 | |
|
491 | 491 | fp = None |
|
492 | 492 | |
|
493 | 493 | firstHeaderSize = 0 |
|
494 | 494 | |
|
495 | 495 | basicHeaderSize = 24 |
|
496 | 496 | |
|
497 | 497 | versionFile = 1103 |
|
498 | 498 | |
|
499 | 499 | fileSize = None |
|
500 | 500 | |
|
501 | 501 | # ippSeconds = None |
|
502 | 502 | |
|
503 | 503 | fileSizeByHeader = None |
|
504 | 504 | |
|
505 | 505 | fileIndex = None |
|
506 | 506 | |
|
507 | 507 | profileIndex = None |
|
508 | 508 | |
|
509 | 509 | blockIndex = None |
|
510 | 510 | |
|
511 | 511 | nTotalBlocks = None |
|
512 | 512 | |
|
513 | 513 | maxTimeStep = 30 |
|
514 | 514 | |
|
515 | 515 | lastUTTime = None |
|
516 | 516 | |
|
517 | 517 | datablock = None |
|
518 | 518 | |
|
519 | 519 | dataOut = None |
|
520 | 520 | |
|
521 | 521 | blocksize = None |
|
522 | 522 | |
|
523 | 523 | getByBlock = False |
|
524 | 524 | |
|
525 | 525 | def __init__(self): |
|
526 | 526 | |
|
527 | 527 | raise NotImplementedError |
|
528 | 528 | |
|
529 | 529 | def run(self): |
|
530 | 530 | |
|
531 | 531 | raise NotImplementedError |
|
532 | 532 | |
|
533 | 533 | def getDtypeWidth(self): |
|
534 | 534 | |
|
535 | 535 | dtype_index = get_dtype_index(self.dtype) |
|
536 | 536 | dtype_width = get_dtype_width(dtype_index) |
|
537 | 537 | |
|
538 | 538 | return dtype_width |
|
539 | 539 | |
|
540 | 540 | def getAllowedArgs(self): |
|
541 | 541 | return inspect.getargspec(self.run).args |
|
542 | 542 | |
|
543 | 543 | class JRODataReader(JRODataIO): |
|
544 | ||
|
545 | ||
|
544 | ||
|
545 | firstTime = True | |
|
546 | 546 | online = 0 |
|
547 | 547 | |
|
548 | 548 | realtime = 0 |
|
549 | 549 | |
|
550 | 550 | nReadBlocks = 0 |
|
551 | 551 | |
|
552 | 552 | delay = 10 #number of seconds waiting a new file |
|
553 | 553 | |
|
554 | 554 | nTries = 3 #quantity tries |
|
555 | 555 | |
|
556 | 556 | nFiles = 3 #number of files for searching |
|
557 | 557 | |
|
558 | 558 | path = None |
|
559 | 559 | |
|
560 | 560 | foldercounter = 0 |
|
561 | 561 | |
|
562 | 562 | flagNoMoreFiles = 0 |
|
563 | 563 | |
|
564 | 564 | datetimeList = [] |
|
565 | 565 | |
|
566 | 566 | __isFirstTimeOnline = 1 |
|
567 | 567 | |
|
568 | 568 | __printInfo = True |
|
569 | 569 | |
|
570 | 570 | profileIndex = None |
|
571 | 571 | |
|
572 | 572 | nTxs = 1 |
|
573 | 573 | |
|
574 | 574 | txIndex = None |
|
575 | 575 | |
|
576 | 576 | #Added-------------------- |
|
577 | 577 | |
|
578 | 578 | selBlocksize = None |
|
579 | 579 | |
|
580 | 580 | selBlocktime = None |
|
581 | ||
|
582 | ||
|
581 | ||
|
582 | onlineWithDate = False | |
|
583 | 583 | def __init__(self): |
|
584 | 584 | |
|
585 | 585 | """ |
|
586 | 586 | This class is used to find data files |
|
587 | 587 | |
|
588 | 588 | Example: |
|
589 | 589 | reader = JRODataReader() |
|
590 | 590 | fileList = reader.findDataFiles() |
|
591 | 591 | |
|
592 | 592 | """ |
|
593 | 593 | pass |
|
594 | 594 | |
|
595 | 595 | |
|
596 | 596 | def createObjByDefault(self): |
|
597 | 597 | """ |
|
598 | 598 | |
|
599 | 599 | """ |
|
600 | 600 | raise NotImplementedError |
|
601 | 601 | |
|
602 | 602 | def getBlockDimension(self): |
|
603 | 603 | |
|
604 | 604 | raise NotImplementedError |
|
605 | 605 | |
|
606 | 606 | def __searchFilesOffLine(self, |
|
607 | 607 | path, |
|
608 | 608 | startDate=None, |
|
609 | 609 | endDate=None, |
|
610 | 610 | startTime=datetime.time(0,0,0), |
|
611 | 611 | endTime=datetime.time(23,59,59), |
|
612 | 612 | set=None, |
|
613 | 613 | expLabel='', |
|
614 | 614 | ext='.r', |
|
615 | 615 | queue=None, |
|
616 | 616 | cursor=None, |
|
617 | 617 | skip=None, |
|
618 | 618 | walk=True): |
|
619 | ||
|
620 | 619 | self.filenameList = [] |
|
621 | 620 | self.datetimeList = [] |
|
622 | 621 | |
|
623 | 622 | pathList = [] |
|
624 | 623 | |
|
625 | 624 | dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) |
|
626 | 625 | |
|
627 | 626 | if dateList == []: |
|
628 | 627 | # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path) |
|
629 | 628 | return None, None |
|
630 | 629 | |
|
631 | 630 | if len(dateList) > 1: |
|
632 | 631 | print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList)) |
|
633 | 632 | else: |
|
634 | 633 | print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0]) |
|
635 | 634 | |
|
636 | 635 | filenameList = [] |
|
637 | 636 | datetimeList = [] |
|
638 | 637 | |
|
639 | 638 | for thisPath in pathList: |
|
640 | 639 | # thisPath = pathList[pathDict[file]] |
|
641 | 640 | |
|
642 | 641 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
643 | 642 | fileList.sort() |
|
644 | 643 | |
|
645 | 644 | skippedFileList = [] |
|
646 | 645 | |
|
647 | 646 | if cursor is not None and skip is not None: |
|
648 | 647 | # if cursor*skip > len(fileList): |
|
649 | 648 | if skip == 0: |
|
650 | 649 | if queue is not None: |
|
651 | 650 | queue.put(len(fileList)) |
|
652 | 651 | skippedFileList = [] |
|
653 | 652 | else: |
|
654 | 653 | skippedFileList = fileList[cursor*skip: cursor*skip + skip] |
|
655 | 654 | |
|
656 | 655 | else: |
|
657 | 656 | skippedFileList = fileList |
|
658 | 657 | |
|
659 | 658 | for file in skippedFileList: |
|
660 | 659 | |
|
661 | 660 | filename = os.path.join(thisPath,file) |
|
662 | 661 | |
|
663 | 662 | if not isFileInDateRange(filename, startDate, endDate): |
|
664 | 663 | continue |
|
665 | 664 | |
|
666 | 665 | thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime) |
|
667 | 666 | |
|
668 | 667 | if not(thisDatetime): |
|
669 | 668 | continue |
|
670 | 669 | |
|
671 | 670 | filenameList.append(filename) |
|
672 | 671 | datetimeList.append(thisDatetime) |
|
673 | 672 | |
|
674 | 673 | if not(filenameList): |
|
675 | 674 | print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path) |
|
676 | 675 | return None, None |
|
677 | 676 | |
|
678 | 677 | print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime) |
|
679 | 678 | |
|
680 | 679 | |
|
681 | 680 | for i in range(len(filenameList)): |
|
682 | 681 | print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) |
|
683 | 682 | |
|
684 | 683 | self.filenameList = filenameList |
|
685 | self.datetimeList = datetimeList | |
|
686 | ||
|
684 | self.datetimeList = datetimeList | |
|
687 | 685 | return pathList, filenameList |
|
688 | 686 | |
|
689 |
def __searchFilesOnLine(self, path, expLabel |
|
|
690 | ||
|
687 | def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None, startDate=None, startTime=None): | |
|
688 | ||
|
691 | 689 |
|
|
692 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y | |
|
693 | devuelve el archivo encontrado ademas de otros datos. | |
|
694 | ||
|
695 | Input: | |
|
696 | path : carpeta donde estan contenidos los files que contiene data | |
|
697 | ||
|
698 | expLabel : Nombre del subexperimento (subfolder) | |
|
699 | ||
|
700 |
ext |
|
|
701 | ||
|
702 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) | |
|
703 | ||
|
704 | Return: | |
|
705 | directory : eL directorio donde esta el file encontrado | |
|
706 | filename : el ultimo file de una determinada carpeta | |
|
707 | year : el anho | |
|
708 | doy : el numero de dia del anho | |
|
709 | set : el set del archivo | |
|
710 | ||
|
711 | ||
|
690 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y | |
|
691 | devuelve el archivo encontrado ademas de otros datos. | |
|
692 | ||
|
693 | Input: | |
|
694 | path : carpeta donde estan contenidos los files que contiene data | |
|
695 | ||
|
696 | expLabel : Nombre del subexperimento (subfolder) | |
|
697 | ||
|
698 | ext : extension de los files | |
|
699 | ||
|
700 | walk : Si es habilitado no realiza busquedas dentro de los subdirectorios (doypath) | |
|
701 | ||
|
702 | Return: | |
|
703 | directory : eL directorio donde esta el file encontrado | |
|
704 | filename : el ultimo file de una determinada carpeta | |
|
705 | year : el anho | |
|
706 | doy : el numero de dia del anho | |
|
707 | set : el set del archivo | |
|
708 | ||
|
709 | ||
|
712 | 710 | """ |
|
711 | pathList = None | |
|
712 | filenameList = None | |
|
713 | 713 | if not os.path.isdir(path): |
|
714 | 714 | return None, None, None, None, None, None |
|
715 | 715 | |
|
716 | 716 | dirList = [] |
|
717 | 717 | |
|
718 | 718 | if not walk: |
|
719 | 719 | fullpath = path |
|
720 | 720 | foldercounter = 0 |
|
721 | 721 | else: |
|
722 | #Filtra solo los directorios | |
|
722 | # Filtra solo los directorios | |
|
723 | 723 | for thisPath in os.listdir(path): |
|
724 | 724 | if not os.path.isdir(os.path.join(path,thisPath)): |
|
725 | 725 | continue |
|
726 | 726 | if not isRadarFolder(thisPath): |
|
727 | 727 | continue |
|
728 | 728 | |
|
729 | 729 | dirList.append(thisPath) |
|
730 | 730 | |
|
731 | 731 | if not(dirList): |
|
732 | 732 | return None, None, None, None, None, None |
|
733 | 733 | |
|
734 | 734 | dirList = sorted( dirList, key=str.lower ) |
|
735 | 735 | |
|
736 | 736 | doypath = dirList[-1] |
|
737 | 737 | foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0 |
|
738 | 738 | fullpath = os.path.join(path, doypath, expLabel) |
|
739 | 739 | |
|
740 | 740 | |
|
741 | 741 | print "[Reading] %s folder was found: " %(fullpath ) |
|
742 | 742 | |
|
743 | 743 | if set == None: |
|
744 | 744 | filename = getlastFileFromPath(fullpath, ext) |
|
745 | 745 | else: |
|
746 | 746 | filename = getFileFromSet(fullpath, ext, set) |
|
747 | 747 | |
|
748 | 748 | if not(filename): |
|
749 | 749 | return None, None, None, None, None, None |
|
750 | 750 | |
|
751 | 751 | print "[Reading] %s file was found" %(filename) |
|
752 | 752 | |
|
753 | 753 | if not(self.__verifyFile(os.path.join(fullpath, filename))): |
|
754 | 754 | return None, None, None, None, None, None |
|
755 | 755 | |
|
756 | 756 | year = int( filename[1:5] ) |
|
757 | 757 | doy = int( filename[5:8] ) |
|
758 | set = int( filename[8:11] ) | |
|
758 | set = int( filename[8:11] ) | |
|
759 | 759 | |
|
760 | 760 | return fullpath, foldercounter, filename, year, doy, set |
|
761 | 761 | |
|
762 | 762 | def __setNextFileOffline(self): |
|
763 | 763 | |
|
764 | 764 | idFile = self.fileIndex |
|
765 | 765 | |
|
766 | 766 | while (True): |
|
767 | 767 | idFile += 1 |
|
768 | 768 | if not(idFile < len(self.filenameList)): |
|
769 | 769 | self.flagNoMoreFiles = 1 |
|
770 |
|
|
|
770 | # print "[Reading] No more Files" | |
|
771 | 771 | return 0 |
|
772 | 772 | |
|
773 | 773 | filename = self.filenameList[idFile] |
|
774 | 774 | |
|
775 | 775 | if not(self.__verifyFile(filename)): |
|
776 | 776 | continue |
|
777 | 777 | |
|
778 | 778 | fileSize = os.path.getsize(filename) |
|
779 | 779 | fp = open(filename,'rb') |
|
780 | 780 | break |
|
781 | 781 | |
|
782 | 782 | self.flagIsNewFile = 1 |
|
783 | 783 | self.fileIndex = idFile |
|
784 | 784 | self.filename = filename |
|
785 | 785 | self.fileSize = fileSize |
|
786 | 786 | self.fp = fp |
|
787 | 787 | |
|
788 |
# |
|
|
788 | #print "[Reading] Setting the file: %s"%self.filename | |
|
789 | 789 | |
|
790 | 790 | return 1 |
|
791 | 791 | |
|
792 | 792 | def __setNextFileOnline(self): |
|
793 | 793 | """ |
|
794 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si | |
|
795 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files | |
|
796 | siguientes. | |
|
797 | ||
|
798 | Affected: | |
|
799 | self.flagIsNewFile | |
|
800 | self.filename | |
|
801 | self.fileSize | |
|
802 | self.fp | |
|
803 | self.set | |
|
804 | self.flagNoMoreFiles | |
|
805 | ||
|
806 | Return: | |
|
807 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado | |
|
808 | 1 : si el file fue abierto con exito y esta listo a ser leido | |
|
809 | ||
|
810 | Excepciones: | |
|
811 | Si un determinado file no puede ser abierto | |
|
812 | """ | |
|
794 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si | |
|
795 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files | |
|
796 | siguientes. | |
|
797 | ||
|
798 | Affected: | |
|
799 | self.flagIsNewFile | |
|
800 | self.filename | |
|
801 | self.fileSize | |
|
802 | self.fp | |
|
803 | self.set | |
|
804 | self.flagNoMoreFiles | |
|
805 | ||
|
806 | Return: | |
|
807 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado | |
|
808 | 1 : si el file fue abierto con exito y esta listo a ser leido | |
|
809 | ||
|
810 | Excepciones: | |
|
811 | Si un determinado file no puede ser abierto | |
|
812 | """ | |
|
813 | ||
|
813 | 814 | nFiles = 0 |
|
814 | 815 | fileOk_flag = False |
|
815 | 816 | firstTime_flag = True |
|
816 | 817 | |
|
817 | 818 | self.set += 1 |
|
818 | 819 | |
|
819 | 820 | if self.set > 999: |
|
820 | 821 | self.set = 0 |
|
821 | 822 | self.foldercounter += 1 |
|
822 | 823 | |
|
823 | 824 | #busca el 1er file disponible |
|
824 | 825 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) |
|
825 | 826 | if fullfilename: |
|
826 | 827 | if self.__verifyFile(fullfilename, False): |
|
827 | 828 | fileOk_flag = True |
|
828 | 829 | |
|
829 | 830 | #si no encuentra un file entonces espera y vuelve a buscar |
|
830 | 831 | if not(fileOk_flag): |
|
831 | 832 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles |
|
832 | 833 | |
|
833 | 834 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces |
|
834 | 835 | tries = self.nTries |
|
835 | 836 | else: |
|
836 | 837 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez |
|
837 | 838 | |
|
838 | 839 | for nTries in range( tries ): |
|
839 | 840 | if firstTime_flag: |
|
840 | 841 | print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) |
|
841 | 842 | sleep( self.delay ) |
|
842 | 843 | else: |
|
843 | 844 | print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) |
|
844 | 845 | |
|
845 | 846 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) |
|
846 | 847 | if fullfilename: |
|
847 | 848 | if self.__verifyFile(fullfilename): |
|
848 | 849 | fileOk_flag = True |
|
849 | 850 | break |
|
850 | 851 | |
|
851 | 852 | if fileOk_flag: |
|
852 | 853 | break |
|
853 | 854 | |
|
854 | 855 | firstTime_flag = False |
|
855 | 856 | |
|
856 | 857 | print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename |
|
857 | 858 | self.set += 1 |
|
858 | 859 | |
|
859 | 860 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
860 | 861 | self.set = 0 |
|
861 | 862 | self.doy += 1 |
|
862 | 863 | self.foldercounter = 0 |
|
863 | 864 | |
|
864 | 865 | if fileOk_flag: |
|
865 | 866 | self.fileSize = os.path.getsize( fullfilename ) |
|
866 | 867 | self.filename = fullfilename |
|
867 | 868 | self.flagIsNewFile = 1 |
|
868 | 869 | if self.fp != None: self.fp.close() |
|
869 | 870 | self.fp = open(fullfilename, 'rb') |
|
870 | 871 | self.flagNoMoreFiles = 0 |
|
871 | 872 | # print '[Reading] Setting the file: %s' % fullfilename |
|
872 | 873 | else: |
|
873 | 874 | self.fileSize = 0 |
|
874 | 875 | self.filename = None |
|
875 | 876 | self.flagIsNewFile = 0 |
|
876 | 877 | self.fp = None |
|
877 | 878 | self.flagNoMoreFiles = 1 |
|
878 | 879 | # print '[Reading] No more files to read' |
|
879 | 880 | |
|
880 | 881 | return fileOk_flag |
|
881 | 882 | |
|
882 | 883 | def setNextFile(self): |
|
883 | 884 | if self.fp != None: |
|
884 | 885 | self.fp.close() |
|
885 | ||
|
886 | 886 | if self.online: |
|
887 | 887 | newFile = self.__setNextFileOnline() |
|
888 | 888 | else: |
|
889 | 889 | newFile = self.__setNextFileOffline() |
|
890 | ||
|
891 | 890 | if not(newFile): |
|
891 | if self.onlineWithDate is True: | |
|
892 | self.onlineWithDate=False | |
|
893 | self.online = True | |
|
894 | self.firstTime = False | |
|
895 | self.setup( | |
|
896 | path=self.path, | |
|
897 | startDate=self.startDate, | |
|
898 | endDate=self.endDate, | |
|
899 | startTime=self.startTime , | |
|
900 | endTime=self.endTime, | |
|
901 | set=self.set, | |
|
902 | expLabel=self.expLabel, | |
|
903 | ext=self.ext, | |
|
904 | online=self.online, | |
|
905 | delay=self.delay, | |
|
906 | walk=self.walk, | |
|
907 | getblock=self.getblock, | |
|
908 | nTxs=self.nTxs, | |
|
909 | realtime=self.realtime, | |
|
910 | blocksize=self.blocksize, | |
|
911 | blocktime=self.blocktime | |
|
912 | ) | |
|
913 | return 1 | |
|
892 | 914 | print '[Reading] No more files to read' |
|
893 | 915 | return 0 |
|
894 | 916 | |
|
895 | 917 | if self.verbose: |
|
896 | 918 | print '[Reading] Setting the file: %s' % self.filename |
|
897 | 919 | |
|
898 | 920 | self.__readFirstHeader() |
|
899 | 921 | self.nReadBlocks = 0 |
|
900 | 922 | return 1 |
|
901 | 923 | |
|
902 | 924 | def __waitNewBlock(self): |
|
903 | 925 | """ |
|
904 | 926 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. |
|
905 | 927 | |
|
906 | 928 | Si el modo de lectura es OffLine siempre retorn 0 |
|
907 | 929 | """ |
|
908 | 930 | if not self.online: |
|
909 | 931 | return 0 |
|
910 | 932 | |
|
911 | 933 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): |
|
912 | 934 | return 0 |
|
913 | 935 | |
|
914 | 936 | currentPointer = self.fp.tell() |
|
915 | 937 | |
|
916 | 938 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
917 | 939 | |
|
918 | 940 | for nTries in range( self.nTries ): |
|
919 | 941 | |
|
920 | 942 | self.fp.close() |
|
921 | 943 | self.fp = open( self.filename, 'rb' ) |
|
922 | 944 | self.fp.seek( currentPointer ) |
|
923 | 945 | |
|
924 | 946 | self.fileSize = os.path.getsize( self.filename ) |
|
925 | 947 | currentSize = self.fileSize - currentPointer |
|
926 | 948 | |
|
927 | 949 | if ( currentSize >= neededSize ): |
|
928 | 950 | self.basicHeaderObj.read(self.fp) |
|
929 | 951 | return 1 |
|
930 | 952 | |
|
931 | 953 | if self.fileSize == self.fileSizeByHeader: |
|
932 | 954 | # self.flagEoF = True |
|
933 | 955 | return 0 |
|
934 | 956 | |
|
935 | 957 | print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
936 | 958 | sleep( self.delay ) |
|
937 | 959 | |
|
938 | 960 | |
|
939 | 961 | return 0 |
|
940 | 962 | |
|
941 | 963 | def waitDataBlock(self,pointer_location): |
|
942 | 964 | |
|
943 | 965 | currentPointer = pointer_location |
|
944 | 966 | |
|
945 | 967 | neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize |
|
946 | 968 | |
|
947 | 969 | for nTries in range( self.nTries ): |
|
948 | 970 | self.fp.close() |
|
949 | 971 | self.fp = open( self.filename, 'rb' ) |
|
950 | 972 | self.fp.seek( currentPointer ) |
|
951 | 973 | |
|
952 | 974 | self.fileSize = os.path.getsize( self.filename ) |
|
953 | 975 | currentSize = self.fileSize - currentPointer |
|
954 | 976 | |
|
955 | 977 | if ( currentSize >= neededSize ): |
|
956 | 978 | return 1 |
|
957 | 979 | |
|
958 | 980 | print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
959 | 981 | sleep( self.delay ) |
|
960 | 982 | |
|
961 | 983 | return 0 |
|
962 | 984 | |
|
963 | 985 | def __jumpToLastBlock(self): |
|
964 | 986 | |
|
965 | 987 | if not(self.__isFirstTimeOnline): |
|
966 | 988 | return |
|
967 | 989 | |
|
968 | 990 | csize = self.fileSize - self.fp.tell() |
|
969 | 991 | blocksize = self.processingHeaderObj.blockSize |
|
970 | 992 | |
|
971 | 993 | #salta el primer bloque de datos |
|
972 | 994 | if csize > self.processingHeaderObj.blockSize: |
|
973 | 995 | self.fp.seek(self.fp.tell() + blocksize) |
|
974 | 996 | else: |
|
975 | 997 | return |
|
976 | 998 | |
|
977 | 999 | csize = self.fileSize - self.fp.tell() |
|
978 | 1000 | neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
979 | 1001 | while True: |
|
980 | 1002 | |
|
981 | 1003 | if self.fp.tell()<self.fileSize: |
|
982 | 1004 | self.fp.seek(self.fp.tell() + neededsize) |
|
983 | 1005 | else: |
|
984 | 1006 | self.fp.seek(self.fp.tell() - neededsize) |
|
985 | 1007 | break |
|
986 | 1008 | |
|
987 | 1009 | # csize = self.fileSize - self.fp.tell() |
|
988 | 1010 | # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
989 | 1011 | # factor = int(csize/neededsize) |
|
990 | 1012 | # if factor > 0: |
|
991 | 1013 | # self.fp.seek(self.fp.tell() + factor*neededsize) |
|
992 | 1014 | |
|
993 | 1015 | self.flagIsNewFile = 0 |
|
994 | 1016 | self.__isFirstTimeOnline = 0 |
|
995 | 1017 | |
|
996 | 1018 | def __setNewBlock(self): |
|
997 | 1019 | #if self.server is None: |
|
998 | 1020 | if self.fp == None: |
|
999 | 1021 | return 0 |
|
1000 | 1022 | |
|
1001 | 1023 | # if self.online: |
|
1002 | 1024 | # self.__jumpToLastBlock() |
|
1003 | 1025 | |
|
1004 | 1026 | if self.flagIsNewFile: |
|
1005 | 1027 | self.lastUTTime = self.basicHeaderObj.utc |
|
1006 | 1028 | return 1 |
|
1007 | 1029 | |
|
1008 | 1030 | if self.realtime: |
|
1009 | 1031 | self.flagDiscontinuousBlock = 1 |
|
1010 | 1032 | if not(self.setNextFile()): |
|
1011 | 1033 | return 0 |
|
1012 | 1034 | else: |
|
1013 | return 1 | |
|
1035 | return 1 | |
|
1014 | 1036 | #if self.server is None: |
|
1015 | 1037 | currentSize = self.fileSize - self.fp.tell() |
|
1016 | 1038 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
1017 | 1039 | if (currentSize >= neededSize): |
|
1018 | 1040 | self.basicHeaderObj.read(self.fp) |
|
1019 | 1041 | self.lastUTTime = self.basicHeaderObj.utc |
|
1020 | 1042 | return 1 |
|
1021 | 1043 | # else: |
|
1022 | 1044 | # self.basicHeaderObj.read(self.zHeader) |
|
1023 | 1045 | # self.lastUTTime = self.basicHeaderObj.utc |
|
1024 | 1046 | # return 1 |
|
1025 | 1047 | if self.__waitNewBlock(): |
|
1026 | 1048 | self.lastUTTime = self.basicHeaderObj.utc |
|
1027 | 1049 | return 1 |
|
1028 | 1050 | #if self.server is None: |
|
1029 | 1051 | if not(self.setNextFile()): |
|
1030 | 1052 | return 0 |
|
1031 | 1053 | |
|
1032 | 1054 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime # |
|
1033 | 1055 | self.lastUTTime = self.basicHeaderObj.utc |
|
1034 | 1056 | |
|
1035 | 1057 | self.flagDiscontinuousBlock = 0 |
|
1036 | 1058 | |
|
1037 | 1059 | if deltaTime > self.maxTimeStep: |
|
1038 | 1060 | self.flagDiscontinuousBlock = 1 |
|
1039 | 1061 | |
|
1040 | 1062 | return 1 |
|
1041 | 1063 | |
|
1042 | 1064 | def readNextBlock(self): |
|
1043 | 1065 | |
|
1044 | 1066 | #Skip block out of startTime and endTime |
|
1045 | while True: | |
|
1067 | while True: | |
|
1046 | 1068 | if not(self.__setNewBlock()): |
|
1047 | 1069 | print 'returning' |
|
1048 | 1070 | return 0 |
|
1049 | 1071 | if not(self.readBlock()): |
|
1050 | 1072 | return 0 |
|
1051 | ||
|
1052 | 1073 | self.getBasicHeader() |
|
1053 | ||
|
1054 | 1074 | if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime): |
|
1055 | 1075 | |
|
1056 | 1076 | print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks, |
|
1057 | 1077 | self.processingHeaderObj.dataBlocksPerFile, |
|
1058 | 1078 | self.dataOut.datatime.ctime()) |
|
1059 | 1079 | continue |
|
1060 | 1080 | |
|
1061 | 1081 | break |
|
1062 | 1082 | |
|
1063 | 1083 | if self.verbose: |
|
1064 | 1084 | print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks, |
|
1065 |
|
|
|
1066 |
|
|
|
1085 | self.processingHeaderObj.dataBlocksPerFile, | |
|
1086 | self.dataOut.datatime.ctime()) | |
|
1067 | 1087 | return 1 |
|
1068 | 1088 | |
|
1069 | 1089 | def __readFirstHeader(self): |
|
1070 | 1090 | |
|
1071 | 1091 | self.basicHeaderObj.read(self.fp) |
|
1072 | 1092 | self.systemHeaderObj.read(self.fp) |
|
1073 | 1093 | self.radarControllerHeaderObj.read(self.fp) |
|
1074 | 1094 | self.processingHeaderObj.read(self.fp) |
|
1075 | 1095 | |
|
1076 | 1096 | self.firstHeaderSize = self.basicHeaderObj.size |
|
1077 | 1097 | |
|
1078 | 1098 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
1079 | 1099 | if datatype == 0: |
|
1080 | 1100 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
1081 | 1101 | elif datatype == 1: |
|
1082 | 1102 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
1083 | 1103 | elif datatype == 2: |
|
1084 | 1104 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
1085 | 1105 | elif datatype == 3: |
|
1086 | 1106 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
1087 | 1107 | elif datatype == 4: |
|
1088 | 1108 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
1089 | 1109 | elif datatype == 5: |
|
1090 | 1110 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
1091 | 1111 | else: |
|
1092 | 1112 | raise ValueError, 'Data type was not defined' |
|
1093 | 1113 | |
|
1094 | 1114 | self.dtype = datatype_str |
|
1095 | 1115 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c |
|
1096 | 1116 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1) |
|
1097 | 1117 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) |
|
1098 | 1118 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) |
|
1099 | 1119 | self.getBlockDimension() |
|
1100 | 1120 | |
|
1101 | 1121 | def __verifyFile(self, filename, msgFlag=True): |
|
1102 | 1122 | |
|
1103 | 1123 | msg = None |
|
1104 | 1124 | |
|
1105 | 1125 | try: |
|
1106 | 1126 | fp = open(filename, 'rb') |
|
1107 | 1127 | except IOError: |
|
1108 | 1128 | |
|
1109 | 1129 | if msgFlag: |
|
1110 | 1130 | print "[Reading] File %s can't be opened" % (filename) |
|
1111 | 1131 | |
|
1112 | 1132 | return False |
|
1113 | 1133 | |
|
1114 | 1134 | currentPosition = fp.tell() |
|
1115 | 1135 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize |
|
1116 | 1136 | |
|
1117 | 1137 | if neededSize == 0: |
|
1118 | 1138 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
1119 | 1139 | systemHeaderObj = SystemHeader() |
|
1120 | 1140 | radarControllerHeaderObj = RadarControllerHeader() |
|
1121 | 1141 | processingHeaderObj = ProcessingHeader() |
|
1122 | 1142 | |
|
1123 | 1143 | if not( basicHeaderObj.read(fp) ): |
|
1124 | 1144 | fp.close() |
|
1125 | 1145 | return False |
|
1126 | 1146 | |
|
1127 | 1147 | if not( systemHeaderObj.read(fp) ): |
|
1128 | 1148 | fp.close() |
|
1129 | 1149 | return False |
|
1130 | 1150 | |
|
1131 | 1151 | if not( radarControllerHeaderObj.read(fp) ): |
|
1132 | 1152 | fp.close() |
|
1133 | 1153 | return False |
|
1134 | 1154 | |
|
1135 | 1155 | if not( processingHeaderObj.read(fp) ): |
|
1136 | 1156 | fp.close() |
|
1137 | 1157 | return False |
|
1138 | 1158 | |
|
1139 | 1159 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size |
|
1140 | 1160 | else: |
|
1141 | 1161 | msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename |
|
1142 | 1162 | |
|
1143 | 1163 | fp.close() |
|
1144 | 1164 | |
|
1145 | 1165 | fileSize = os.path.getsize(filename) |
|
1146 | 1166 | currentSize = fileSize - currentPosition |
|
1147 | 1167 | |
|
1148 | 1168 | if currentSize < neededSize: |
|
1149 | 1169 | if msgFlag and (msg != None): |
|
1150 | 1170 | print msg |
|
1151 | 1171 | return False |
|
1152 | 1172 | |
|
1153 | 1173 | return True |
|
1154 | 1174 | |
|
1155 | 1175 | def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False): |
|
1156 | 1176 | |
|
1157 | 1177 | path_empty = True |
|
1158 | 1178 | |
|
1159 | 1179 | dateList = [] |
|
1160 | 1180 | pathList = [] |
|
1161 | 1181 | |
|
1162 | 1182 | multi_path = path.split(',') |
|
1163 | 1183 | |
|
1164 | 1184 | if not walk: |
|
1165 | 1185 | |
|
1166 | 1186 | for single_path in multi_path: |
|
1167 | 1187 | |
|
1168 | 1188 | if not os.path.isdir(single_path): |
|
1169 | 1189 | continue |
|
1170 | 1190 | |
|
1171 | 1191 | fileList = glob.glob1(single_path, "*"+ext) |
|
1172 | 1192 | |
|
1173 | 1193 | if not fileList: |
|
1174 | 1194 | continue |
|
1175 | 1195 | |
|
1176 | 1196 | path_empty = False |
|
1177 | 1197 | |
|
1178 | 1198 | fileList.sort() |
|
1179 | 1199 | |
|
1180 | 1200 | for thisFile in fileList: |
|
1181 | 1201 | |
|
1182 | 1202 | if not os.path.isfile(os.path.join(single_path, thisFile)): |
|
1183 | 1203 | continue |
|
1184 | 1204 | |
|
1185 | 1205 | if not isRadarFile(thisFile): |
|
1186 | 1206 | continue |
|
1187 | 1207 | |
|
1188 | 1208 | if not isFileInDateRange(thisFile, startDate, endDate): |
|
1189 | 1209 | continue |
|
1190 | 1210 | |
|
1191 | 1211 | thisDate = getDateFromRadarFile(thisFile) |
|
1192 | 1212 | |
|
1193 | 1213 | if thisDate in dateList: |
|
1194 | 1214 | continue |
|
1195 | 1215 | |
|
1196 | 1216 | dateList.append(thisDate) |
|
1197 | 1217 | pathList.append(single_path) |
|
1198 | 1218 | |
|
1199 | 1219 | else: |
|
1200 | 1220 | for single_path in multi_path: |
|
1201 | 1221 | |
|
1202 | 1222 | if not os.path.isdir(single_path): |
|
1203 | 1223 | continue |
|
1204 | 1224 | |
|
1205 | 1225 | dirList = [] |
|
1206 | 1226 | |
|
1207 | 1227 | for thisPath in os.listdir(single_path): |
|
1208 | 1228 | |
|
1209 | 1229 | if not os.path.isdir(os.path.join(single_path,thisPath)): |
|
1210 | 1230 | continue |
|
1211 | 1231 | |
|
1212 | 1232 | if not isRadarFolder(thisPath): |
|
1213 | 1233 | continue |
|
1214 | 1234 | |
|
1215 | 1235 | if not isFolderInDateRange(thisPath, startDate, endDate): |
|
1216 | 1236 | continue |
|
1217 | 1237 | |
|
1218 | 1238 | dirList.append(thisPath) |
|
1219 | 1239 | |
|
1220 | 1240 | if not dirList: |
|
1221 | 1241 | continue |
|
1222 | 1242 | |
|
1223 | 1243 | dirList.sort() |
|
1224 | 1244 | |
|
1225 | 1245 | for thisDir in dirList: |
|
1226 | 1246 | |
|
1227 | 1247 | datapath = os.path.join(single_path, thisDir, expLabel) |
|
1228 | 1248 | fileList = glob.glob1(datapath, "*"+ext) |
|
1229 | 1249 | |
|
1230 | 1250 | if not fileList: |
|
1231 | 1251 | continue |
|
1232 | 1252 | |
|
1233 | 1253 | path_empty = False |
|
1234 | 1254 | |
|
1235 | 1255 | thisDate = getDateFromRadarFolder(thisDir) |
|
1236 | 1256 | |
|
1237 | 1257 | pathList.append(datapath) |
|
1238 | 1258 | dateList.append(thisDate) |
|
1239 | 1259 | |
|
1240 | 1260 | dateList.sort() |
|
1241 | 1261 | |
|
1242 | 1262 | if walk: |
|
1243 | 1263 | pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel) |
|
1244 | 1264 | else: |
|
1245 | 1265 | pattern_path = multi_path[0] |
|
1246 | 1266 | |
|
1247 | 1267 | if path_empty: |
|
1248 | 1268 | print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate) |
|
1249 | 1269 | else: |
|
1250 | 1270 | if not dateList: |
|
1251 | 1271 | print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path) |
|
1252 | 1272 | |
|
1253 | 1273 | if include_path: |
|
1254 | 1274 | return dateList, pathList |
|
1255 | 1275 | |
|
1256 | 1276 | return dateList |
|
1257 | 1277 | |
|
1258 | 1278 | def setup(self, |
|
1259 | 1279 | path=None, |
|
1260 | startDate=None, | |
|
1261 | endDate=None, | |
|
1262 | startTime=datetime.time(0,0,0), | |
|
1263 | endTime=datetime.time(23,59,59), | |
|
1264 | set=None, | |
|
1265 | expLabel = "", | |
|
1266 | ext = None, | |
|
1280 | startDate=None, | |
|
1281 | endDate=None, | |
|
1282 | startTime=datetime.time(0,0,0), | |
|
1283 | endTime=datetime.time(23,59,59), | |
|
1284 | set=None, | |
|
1285 | expLabel = "", | |
|
1286 | ext = None, | |
|
1267 | 1287 | online = False, |
|
1268 | 1288 | delay = 60, |
|
1269 | 1289 | walk = True, |
|
1270 | 1290 | getblock = False, |
|
1271 | 1291 | nTxs = 1, |
|
1272 | 1292 | realtime=False, |
|
1273 | 1293 | blocksize=None, |
|
1274 | 1294 | blocktime=None, |
|
1275 | queue=None, | |
|
1276 | skip=None, | |
|
1277 | cursor=None, | |
|
1278 | warnings=True, | |
|
1279 | 1295 | verbose=True, |
|
1280 |
|
|
|
1281 | if server is not None: | |
|
1282 | if 'tcp://' in server: | |
|
1283 | address = server | |
|
1284 | else: | |
|
1285 | address = 'ipc:///tmp/%s' % server | |
|
1286 | self.server = address | |
|
1287 | self.context = zmq.Context() | |
|
1288 | self.receiver = self.context.socket(zmq.PULL) | |
|
1289 | self.receiver.connect(self.server) | |
|
1290 | time.sleep(0.5) | |
|
1291 | print '[Starting] ReceiverData from {}'.format(self.server) | |
|
1292 | else: | |
|
1293 | self.server = None | |
|
1294 | if path == None: | |
|
1295 | raise ValueError, "[Reading] The path is not valid" | |
|
1296 | ||
|
1297 | if ext == None: | |
|
1298 | ext = self.ext | |
|
1299 | ||
|
1300 | if online: | |
|
1301 | print "[Reading] Searching files in online mode..." | |
|
1296 | **kwargs): | |
|
1302 | 1297 | |
|
1303 | for nTries in range( self.nTries ): | |
|
1304 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set) | |
|
1305 | ||
|
1306 | if fullpath: | |
|
1307 | break | |
|
1308 | ||
|
1309 | print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) | |
|
1310 | sleep( self.delay ) | |
|
1311 | ||
|
1312 | if not(fullpath): | |
|
1313 | print "[Reading] There 'isn't any valid file in %s" % path | |
|
1314 | return | |
|
1298 | if path == None: | |
|
1299 | raise ValueError, "[Reading] The path is not valid" | |
|
1300 | ||
|
1315 | 1301 | |
|
1316 | self.year = year | |
|
1317 |
|
|
|
1318 | self.set = set - 1 | |
|
1319 | self.path = path | |
|
1320 | self.foldercounter = foldercounter | |
|
1321 | last_set = None | |
|
1322 | else: | |
|
1323 | print "[Reading] Searching files in offline mode ..." | |
|
1324 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
|
1302 | if ext == None: | |
|
1303 | ext = self.ext | |
|
1304 | ||
|
1305 | self.verbose=verbose | |
|
1306 | self.path = path | |
|
1307 | self.startDate = startDate | |
|
1308 | self.endDate = endDate | |
|
1309 | self.startTime = startTime | |
|
1310 | self.endTime = endTime | |
|
1311 | self.set = set | |
|
1312 | self.expLabel = expLabel | |
|
1313 | self.ext = ext | |
|
1314 | self.online = online | |
|
1315 | self.delay = delay | |
|
1316 | self.walk = walk | |
|
1317 | self.getblock = getblock | |
|
1318 | self.nTxs = nTxs | |
|
1319 | self.realtime = realtime | |
|
1320 | self.blocksize = blocksize | |
|
1321 | self.blocktime = blocktime | |
|
1322 | ||
|
1323 | ||
|
1324 | if self.firstTime is True: | |
|
1325 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
|
1325 | 1326 | startTime=startTime, endTime=endTime, |
|
1326 | 1327 | set=set, expLabel=expLabel, ext=ext, |
|
1327 |
walk=walk |
|
|
1328 | skip=skip, queue=queue) | |
|
1329 | ||
|
1330 | if not(pathList): | |
|
1331 | # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path, | |
|
1332 | # datetime.datetime.combine(startDate,startTime).ctime(), | |
|
1333 | # datetime.datetime.combine(endDate,endTime).ctime()) | |
|
1334 | ||
|
1335 | # sys.exit(-1) | |
|
1328 | walk=walk) | |
|
1329 | if filenameList is not None: filenameList = filenameList[:-1] | |
|
1336 | 1330 | |
|
1331 | if pathList is not None and filenameList is not None and online: | |
|
1332 | self.onlineWithDate = True | |
|
1333 | online = False | |
|
1337 | 1334 | self.fileIndex = -1 |
|
1338 |
self.pathList = |
|
|
1339 |
self.filenameList = |
|
|
1340 | return | |
|
1335 | self.pathList = pathList | |
|
1336 | self.filenameList = filenameList | |
|
1337 | file_name = os.path.basename(filenameList[-1]) | |
|
1338 | basename, ext = os.path.splitext(file_name) | |
|
1339 | last_set = int(basename[-3:]) | |
|
1340 | ||
|
1341 | if online: | |
|
1342 | print "[Reading] Searching files in online mode..." | |
|
1343 | ||
|
1344 | for nTries in range(self.nTries): | |
|
1345 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, | |
|
1346 | expLabel=expLabel, | |
|
1347 | ext=ext, | |
|
1348 | walk=walk, | |
|
1349 | startDate=startDate, | |
|
1350 | startTime=startTime, | |
|
1351 | set=set) | |
|
1352 | ||
|
1353 | if fullpath: | |
|
1354 | break | |
|
1355 | print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) | |
|
1356 | sleep( self.delay ) | |
|
1357 | ||
|
1358 | if not(fullpath): | |
|
1359 | print "[Reading] There 'isn't any valid file in %s" % path | |
|
1360 | return | |
|
1361 | ||
|
1362 | self.year = year | |
|
1363 | self.doy = doy | |
|
1364 | self.set = set - 1 | |
|
1365 | self.path = path | |
|
1366 | self.foldercounter = foldercounter | |
|
1367 | last_set = None | |
|
1368 | else: | |
|
1369 | print "[Reading] Searching files in offline mode ..." | |
|
1370 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
|
1371 | startTime=startTime, endTime=endTime, | |
|
1372 | set=set, expLabel=expLabel, ext=ext, | |
|
1373 | walk=walk) | |
|
1374 | ||
|
1375 | if not(pathList): | |
|
1376 | # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path, | |
|
1377 | # datetime.datetime.combine(startDate,startTime).ctime(), | |
|
1378 | # datetime.datetime.combine(endDate,endTime).ctime()) | |
|
1379 | ||
|
1380 | # sys.exit(-1) | |
|
1381 | ||
|
1382 | self.fileIndex = -1 | |
|
1383 | self.pathList = [] | |
|
1384 | self.filenameList = [] | |
|
1385 | return | |
|
1386 | ||
|
1387 | self.fileIndex = -1 | |
|
1388 | self.pathList = pathList | |
|
1389 | self.filenameList = filenameList | |
|
1390 | file_name = os.path.basename(filenameList[-1]) | |
|
1391 | basename, ext = os.path.splitext(file_name) | |
|
1392 | last_set = int(basename[-3:]) | |
|
1341 | 1393 | |
|
1394 | ||
|
1395 | self.online = online | |
|
1396 | self.realtime = realtime | |
|
1397 | self.delay = delay | |
|
1398 | ext = ext.lower() | |
|
1399 | self.ext = ext | |
|
1400 | self.getByBlock = getblock | |
|
1401 | self.nTxs = nTxs | |
|
1402 | self.startTime = startTime | |
|
1403 | self.endTime = endTime | |
|
1404 | ||
|
1405 | ||
|
1406 | #Added----------------- | |
|
1407 | self.selBlocksize = blocksize | |
|
1408 | self.selBlocktime = blocktime | |
|
1409 | ||
|
1410 | ||
|
1411 | if not(self.setNextFile()): | |
|
1412 | if (startDate!=None) and (endDate!=None): | |
|
1413 | print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) | |
|
1414 | elif startDate != None: | |
|
1415 | print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) | |
|
1416 | else: | |
|
1417 | print "[Reading] No files" | |
|
1418 | ||
|
1342 | 1419 | self.fileIndex = -1 |
|
1343 | 1420 | self.pathList = [] |
|
1344 | 1421 | self.filenameList = [] |
|
1345 | 1422 | return |
|
1346 | 1423 | |
|
1347 | 1424 | # self.getBasicHeader() |
|
1348 | ||
|
1425 | ||
|
1349 | 1426 | if last_set != None: |
|
1350 | 1427 | self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock |
|
1351 | 1428 | return |
|
1352 | 1429 | |
|
1353 | 1430 | def getBasicHeader(self): |
|
1354 | 1431 | |
|
1355 | 1432 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds |
|
1356 | 1433 | |
|
1357 | 1434 | self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock |
|
1358 | 1435 | |
|
1359 | 1436 | self.dataOut.timeZone = self.basicHeaderObj.timeZone |
|
1360 | 1437 | |
|
1361 | 1438 | self.dataOut.dstFlag = self.basicHeaderObj.dstFlag |
|
1362 | 1439 | |
|
1363 | 1440 | self.dataOut.errorCount = self.basicHeaderObj.errorCount |
|
1364 | 1441 | |
|
1365 | 1442 | self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime |
|
1366 | 1443 | |
|
1367 | 1444 | self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs |
|
1368 | ||
|
1369 | # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs | |
|
1370 | ||
|
1371 | ||
|
1445 | ||
|
1446 | # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs | |
|
1447 | ||
|
1448 | ||
|
1372 | 1449 | def getFirstHeader(self): |
|
1373 | 1450 | |
|
1374 | 1451 | raise NotImplementedError |
|
1375 | 1452 | |
|
1376 | 1453 | def getData(self): |
|
1377 | 1454 | |
|
1378 | 1455 | raise NotImplementedError |
|
1379 | 1456 | |
|
1380 | 1457 | def hasNotDataInBuffer(self): |
|
1381 | 1458 | |
|
1382 | 1459 | raise NotImplementedError |
|
1383 | 1460 | |
|
1384 | 1461 | def readBlock(self): |
|
1385 | 1462 | |
|
1386 | 1463 | raise NotImplementedError |
|
1387 | 1464 | |
|
1388 | 1465 | def isEndProcess(self): |
|
1389 | 1466 | |
|
1390 | 1467 | return self.flagNoMoreFiles |
|
1391 | 1468 | |
|
1392 | 1469 | def printReadBlocks(self): |
|
1393 | 1470 | |
|
1394 | 1471 | print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks |
|
1395 | 1472 | |
|
1396 | 1473 | def printTotalBlocks(self): |
|
1397 | 1474 | |
|
1398 | 1475 | print "[Reading] Number of read blocks %04d" %self.nTotalBlocks |
|
1399 | 1476 | |
|
1400 | 1477 | def printNumberOfBlock(self): |
|
1401 | 1478 | |
|
1402 | 1479 | if self.flagIsNewBlock: |
|
1403 | 1480 | print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks, |
|
1404 | 1481 | self.processingHeaderObj.dataBlocksPerFile, |
|
1405 | 1482 | self.dataOut.datatime.ctime()) |
|
1406 | 1483 | |
|
1407 | 1484 | def printInfo(self): |
|
1408 | 1485 | |
|
1409 | 1486 | if self.__printInfo == False: |
|
1410 | 1487 | return |
|
1411 | 1488 | |
|
1412 | 1489 | self.basicHeaderObj.printInfo() |
|
1413 | 1490 | self.systemHeaderObj.printInfo() |
|
1414 | 1491 | self.radarControllerHeaderObj.printInfo() |
|
1415 | 1492 | self.processingHeaderObj.printInfo() |
|
1416 | 1493 | |
|
1417 | 1494 | self.__printInfo = False |
|
1418 | 1495 | |
|
1419 | ||
|
1420 | 1496 | def run(self, |
|
1421 |
|
|
|
1422 |
|
|
|
1423 |
|
|
|
1424 |
|
|
|
1425 |
|
|
|
1426 |
|
|
|
1427 |
|
|
|
1428 |
|
|
|
1429 |
|
|
|
1430 |
|
|
|
1431 |
|
|
|
1432 |
|
|
|
1433 |
|
|
|
1434 |
|
|
|
1435 |
|
|
|
1436 |
|
|
|
1437 |
|
|
|
1438 |
|
|
|
1439 |
|
|
|
1440 |
|
|
|
1441 |
|
|
|
1442 |
|
|
|
1497 | path=None, | |
|
1498 | startDate=None, | |
|
1499 | endDate=None, | |
|
1500 | startTime=datetime.time(0,0,0), | |
|
1501 | endTime=datetime.time(23,59,59), | |
|
1502 | set=None, | |
|
1503 | expLabel = "", | |
|
1504 | ext = None, | |
|
1505 | online = False, | |
|
1506 | delay = 60, | |
|
1507 | walk = True, | |
|
1508 | getblock = False, | |
|
1509 | nTxs = 1, | |
|
1510 | realtime=False, | |
|
1511 | blocksize=None, | |
|
1512 | blocktime=None, | |
|
1513 | queue=None, | |
|
1514 | skip=None, | |
|
1515 | cursor=None, | |
|
1516 | warnings=True, | |
|
1517 | server=None, | |
|
1518 | verbose=True, **kwargs): | |
|
1443 | 1519 | |
|
1444 | 1520 | if not(self.isConfig): |
|
1445 | 1521 | # self.dataOut = dataOut |
|
1446 | 1522 | self.setup( path=path, |
|
1447 | 1523 | startDate=startDate, |
|
1448 | 1524 | endDate=endDate, |
|
1449 | 1525 | startTime=startTime, |
|
1450 | 1526 | endTime=endTime, |
|
1451 | 1527 | set=set, |
|
1452 | 1528 | expLabel=expLabel, |
|
1453 | 1529 | ext=ext, |
|
1454 | 1530 | online=online, |
|
1455 | 1531 | delay=delay, |
|
1456 | 1532 | walk=walk, |
|
1457 | 1533 | getblock=getblock, |
|
1458 | 1534 | nTxs=nTxs, |
|
1459 | 1535 | realtime=realtime, |
|
1460 | 1536 | blocksize=blocksize, |
|
1461 | 1537 | blocktime=blocktime, |
|
1462 | 1538 | queue=queue, |
|
1463 | 1539 | skip=skip, |
|
1464 | 1540 | cursor=cursor, |
|
1465 | 1541 | warnings=warnings, |
|
1466 | 1542 | server=server, |
|
1467 | verbose=verbose) | |
|
1543 | verbose=verbose, **kwargs) | |
|
1468 | 1544 | self.isConfig = True |
|
1469 | 1545 | if server is None: |
|
1470 | 1546 | self.getData() |
|
1471 | 1547 | else: |
|
1472 | 1548 | self.getFromServer() |
|
1473 | 1549 | |
|
1474 | 1550 | class JRODataWriter(JRODataIO): |
|
1475 | 1551 | |
|
1476 | 1552 | """ |
|
1477 | 1553 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
1478 | 1554 | de los datos siempre se realiza por bloques. |
|
1479 | 1555 | """ |
|
1480 | 1556 | |
|
1481 | 1557 | blockIndex = 0 |
|
1482 | 1558 | |
|
1483 | 1559 | path = None |
|
1484 | 1560 | |
|
1485 | 1561 | setFile = None |
|
1486 | 1562 | |
|
1487 | 1563 | profilesPerBlock = None |
|
1488 | 1564 | |
|
1489 | 1565 | blocksPerFile = None |
|
1490 | 1566 | |
|
1491 | 1567 | nWriteBlocks = 0 |
|
1492 | 1568 | |
|
1493 | 1569 | fileDate = None |
|
1494 | 1570 | |
|
1495 | 1571 | def __init__(self, dataOut=None): |
|
1496 | 1572 | raise NotImplementedError |
|
1497 | 1573 | |
|
1498 | 1574 | |
|
1499 | 1575 | def hasAllDataInBuffer(self): |
|
1500 | 1576 | raise NotImplementedError |
|
1501 | 1577 | |
|
1502 | 1578 | |
|
1503 | 1579 | def setBlockDimension(self): |
|
1504 | 1580 | raise NotImplementedError |
|
1505 | 1581 | |
|
1506 | 1582 | |
|
1507 | 1583 | def writeBlock(self): |
|
1508 | 1584 | raise NotImplementedError |
|
1509 | 1585 | |
|
1510 | 1586 | |
|
1511 | 1587 | def putData(self): |
|
1512 | 1588 | raise NotImplementedError |
|
1513 | 1589 | |
|
1514 | 1590 | |
|
1515 | 1591 | def getProcessFlags(self): |
|
1516 | 1592 | |
|
1517 | 1593 | processFlags = 0 |
|
1518 | 1594 | |
|
1519 | 1595 | dtype_index = get_dtype_index(self.dtype) |
|
1520 | 1596 | procflag_dtype = get_procflag_dtype(dtype_index) |
|
1521 | 1597 | |
|
1522 | 1598 | processFlags += procflag_dtype |
|
1523 | 1599 | |
|
1524 | 1600 | if self.dataOut.flagDecodeData: |
|
1525 | 1601 | processFlags += PROCFLAG.DECODE_DATA |
|
1526 | 1602 | |
|
1527 | 1603 | if self.dataOut.flagDeflipData: |
|
1528 | 1604 | processFlags += PROCFLAG.DEFLIP_DATA |
|
1529 | 1605 | |
|
1530 | 1606 | if self.dataOut.code is not None: |
|
1531 | 1607 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE |
|
1532 | 1608 | |
|
1533 | 1609 | if self.dataOut.nCohInt > 1: |
|
1534 | 1610 | processFlags += PROCFLAG.COHERENT_INTEGRATION |
|
1535 | 1611 | |
|
1536 | 1612 | if self.dataOut.type == "Spectra": |
|
1537 | 1613 | if self.dataOut.nIncohInt > 1: |
|
1538 | 1614 | processFlags += PROCFLAG.INCOHERENT_INTEGRATION |
|
1539 | 1615 | |
|
1540 | 1616 | if self.dataOut.data_dc is not None: |
|
1541 | 1617 | processFlags += PROCFLAG.SAVE_CHANNELS_DC |
|
1542 | 1618 | |
|
1543 | 1619 | if self.dataOut.flagShiftFFT: |
|
1544 | 1620 | processFlags += PROCFLAG.SHIFT_FFT_DATA |
|
1545 | 1621 | |
|
1546 | 1622 | return processFlags |
|
1547 | 1623 | |
|
1548 | 1624 | def setBasicHeader(self): |
|
1549 | 1625 | |
|
1550 | 1626 | self.basicHeaderObj.size = self.basicHeaderSize #bytes |
|
1551 | 1627 | self.basicHeaderObj.version = self.versionFile |
|
1552 | 1628 | self.basicHeaderObj.dataBlock = self.nTotalBlocks |
|
1553 | 1629 | |
|
1554 | 1630 | utc = numpy.floor(self.dataOut.utctime) |
|
1555 | 1631 | milisecond = (self.dataOut.utctime - utc)* 1000.0 |
|
1556 | 1632 | |
|
1557 | 1633 | self.basicHeaderObj.utc = utc |
|
1558 | 1634 | self.basicHeaderObj.miliSecond = milisecond |
|
1559 | 1635 | self.basicHeaderObj.timeZone = self.dataOut.timeZone |
|
1560 | 1636 | self.basicHeaderObj.dstFlag = self.dataOut.dstFlag |
|
1561 | 1637 | self.basicHeaderObj.errorCount = self.dataOut.errorCount |
|
1562 | 1638 | |
|
1563 | 1639 | def setFirstHeader(self): |
|
1564 | 1640 | """ |
|
1565 | 1641 | Obtiene una copia del First Header |
|
1566 | 1642 | |
|
1567 | 1643 | Affected: |
|
1568 | 1644 | |
|
1569 | 1645 | self.basicHeaderObj |
|
1570 | 1646 | self.systemHeaderObj |
|
1571 | 1647 | self.radarControllerHeaderObj |
|
1572 | 1648 | self.processingHeaderObj self. |
|
1573 | 1649 | |
|
1574 | 1650 | Return: |
|
1575 | 1651 | None |
|
1576 | 1652 | """ |
|
1577 | 1653 | |
|
1578 | 1654 | raise NotImplementedError |
|
1579 | 1655 | |
|
1580 | 1656 | def __writeFirstHeader(self): |
|
1581 | 1657 | """ |
|
1582 | 1658 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1583 | 1659 | |
|
1584 | 1660 | Affected: |
|
1585 | 1661 | __dataType |
|
1586 | 1662 | |
|
1587 | 1663 | Return: |
|
1588 | 1664 | None |
|
1589 | 1665 | """ |
|
1590 | ||
|
1591 | # CALCULAR PARAMETROS | |
|
1592 | ||
|
1666 | ||
|
1667 | # CALCULAR PARAMETROS | |
|
1668 | ||
|
1593 | 1669 | sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size |
|
1594 | 1670 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader |
|
1595 | 1671 | |
|
1596 | 1672 | self.basicHeaderObj.write(self.fp) |
|
1597 | 1673 | self.systemHeaderObj.write(self.fp) |
|
1598 | 1674 | self.radarControllerHeaderObj.write(self.fp) |
|
1599 | 1675 | self.processingHeaderObj.write(self.fp) |
|
1600 | 1676 | |
|
1601 | 1677 | def __setNewBlock(self): |
|
1602 | 1678 | """ |
|
1603 | 1679 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1604 | 1680 | |
|
1605 | 1681 | Return: |
|
1606 | 1682 | 0 : si no pudo escribir nada |
|
1607 | 1683 | 1 : Si escribio el Basic el First Header |
|
1608 | 1684 | """ |
|
1609 | 1685 | if self.fp == None: |
|
1610 | 1686 | self.setNextFile() |
|
1611 | 1687 | |
|
1612 | 1688 | if self.flagIsNewFile: |
|
1613 | 1689 | return 1 |
|
1614 | 1690 | |
|
1615 | 1691 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: |
|
1616 | 1692 | self.basicHeaderObj.write(self.fp) |
|
1617 | 1693 | return 1 |
|
1618 | 1694 | |
|
1619 | 1695 | if not( self.setNextFile() ): |
|
1620 | 1696 | return 0 |
|
1621 | 1697 | |
|
1622 | 1698 | return 1 |
|
1623 | 1699 | |
|
1624 | 1700 | |
|
1625 | 1701 | def writeNextBlock(self): |
|
1626 | 1702 | """ |
|
1627 | 1703 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1628 | 1704 | |
|
1629 | 1705 | Return: |
|
1630 | 1706 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1631 | 1707 | 1 : Si no pudo escribir el bloque de datos |
|
1632 | 1708 | """ |
|
1633 | 1709 | if not( self.__setNewBlock() ): |
|
1634 | 1710 | return 0 |
|
1635 | 1711 | |
|
1636 | 1712 | self.writeBlock() |
|
1637 | 1713 | |
|
1638 | 1714 | print "[Writing] Block No. %d/%d" %(self.blockIndex, |
|
1639 | 1715 | self.processingHeaderObj.dataBlocksPerFile) |
|
1640 | 1716 | |
|
1641 | 1717 | return 1 |
|
1642 | 1718 | |
|
1643 | 1719 | def setNextFile(self): |
|
1644 | 1720 | """ |
|
1645 | 1721 | Determina el siguiente file que sera escrito |
|
1646 | 1722 | |
|
1647 | 1723 | Affected: |
|
1648 | 1724 | self.filename |
|
1649 | 1725 | self.subfolder |
|
1650 | 1726 | self.fp |
|
1651 | 1727 | self.setFile |
|
1652 | 1728 | self.flagIsNewFile |
|
1653 | 1729 | |
|
1654 | 1730 | Return: |
|
1655 | 1731 | 0 : Si el archivo no puede ser escrito |
|
1656 | 1732 | 1 : Si el archivo esta listo para ser escrito |
|
1657 | 1733 | """ |
|
1658 | 1734 | ext = self.ext |
|
1659 | 1735 | path = self.path |
|
1660 | 1736 | |
|
1661 | 1737 | if self.fp != None: |
|
1662 | 1738 | self.fp.close() |
|
1663 | 1739 | |
|
1664 | 1740 | timeTuple = time.localtime( self.dataOut.utctime) |
|
1665 | 1741 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1666 | 1742 | |
|
1667 | 1743 | fullpath = os.path.join( path, subfolder ) |
|
1668 | 1744 | setFile = self.setFile |
|
1669 | 1745 | |
|
1670 | 1746 | if not( os.path.exists(fullpath) ): |
|
1671 | 1747 | os.mkdir(fullpath) |
|
1672 | 1748 | setFile = -1 #inicializo mi contador de seteo |
|
1673 | 1749 | else: |
|
1674 | 1750 | filesList = os.listdir( fullpath ) |
|
1675 | 1751 | if len( filesList ) > 0: |
|
1676 | 1752 | filesList = sorted( filesList, key=str.lower ) |
|
1677 | 1753 | filen = filesList[-1] |
|
1678 | 1754 | # el filename debera tener el siguiente formato |
|
1679 | 1755 | # 0 1234 567 89A BCDE (hex) |
|
1680 | 1756 | # x YYYY DDD SSS .ext |
|
1681 | 1757 | if isNumber( filen[8:11] ): |
|
1682 | 1758 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1683 | 1759 | else: |
|
1684 | 1760 | setFile = -1 |
|
1685 | 1761 | else: |
|
1686 | 1762 | setFile = -1 #inicializo mi contador de seteo |
|
1687 | 1763 | |
|
1688 | 1764 | setFile += 1 |
|
1689 | 1765 | |
|
1690 | 1766 | #If this is a new day it resets some values |
|
1691 | 1767 | if self.dataOut.datatime.date() > self.fileDate: |
|
1692 | 1768 | setFile = 0 |
|
1693 | 1769 | self.nTotalBlocks = 0 |
|
1694 | 1770 | |
|
1695 | 1771 | filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext ) |
|
1696 | 1772 | |
|
1697 | 1773 | filename = os.path.join( path, subfolder, filen ) |
|
1698 | 1774 | |
|
1699 | 1775 | fp = open( filename,'wb' ) |
|
1700 | 1776 | |
|
1701 | 1777 | self.blockIndex = 0 |
|
1702 | 1778 | |
|
1703 | 1779 | #guardando atributos |
|
1704 | 1780 | self.filename = filename |
|
1705 | 1781 | self.subfolder = subfolder |
|
1706 | 1782 | self.fp = fp |
|
1707 | 1783 | self.setFile = setFile |
|
1708 | 1784 | self.flagIsNewFile = 1 |
|
1709 | 1785 | self.fileDate = self.dataOut.datatime.date() |
|
1710 | 1786 | |
|
1711 | 1787 | self.setFirstHeader() |
|
1712 | 1788 | |
|
1713 | 1789 | print '[Writing] Opening file: %s'%self.filename |
|
1714 | 1790 | |
|
1715 | 1791 | self.__writeFirstHeader() |
|
1716 | 1792 | |
|
1717 | 1793 | return 1 |
|
1718 | 1794 | |
|
1719 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4): | |
|
1795 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, verbose=True): | |
|
1720 | 1796 | """ |
|
1721 | 1797 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1722 | 1798 | |
|
1723 | 1799 | Inputs: |
|
1724 | 1800 | path : directory where data will be saved |
|
1725 | 1801 | profilesPerBlock : number of profiles per block |
|
1726 | 1802 | set : initial file set |
|
1727 | 1803 | datatype : An integer number that defines data type: |
|
1728 | 1804 | 0 : int8 (1 byte) |
|
1729 | 1805 | 1 : int16 (2 bytes) |
|
1730 | 1806 | 2 : int32 (4 bytes) |
|
1731 | 1807 | 3 : int64 (8 bytes) |
|
1732 | 1808 | 4 : float32 (4 bytes) |
|
1733 | 1809 | 5 : double64 (8 bytes) |
|
1734 | 1810 | |
|
1735 | 1811 | Return: |
|
1736 | 1812 | 0 : Si no realizo un buen seteo |
|
1737 | 1813 | 1 : Si realizo un buen seteo |
|
1738 | 1814 | """ |
|
1739 | 1815 | |
|
1740 | 1816 | if ext == None: |
|
1741 | 1817 | ext = self.ext |
|
1742 | 1818 | |
|
1743 | 1819 | self.ext = ext.lower() |
|
1744 | 1820 | |
|
1745 | 1821 | self.path = path |
|
1746 | 1822 | |
|
1747 | 1823 | if set is None: |
|
1748 | 1824 | self.setFile = -1 |
|
1749 | 1825 | else: |
|
1750 | 1826 | self.setFile = set - 1 |
|
1751 | 1827 | |
|
1752 | 1828 | self.blocksPerFile = blocksPerFile |
|
1753 | 1829 | |
|
1754 | 1830 | self.profilesPerBlock = profilesPerBlock |
|
1755 | 1831 | |
|
1756 | 1832 | self.dataOut = dataOut |
|
1757 | 1833 | self.fileDate = self.dataOut.datatime.date() |
|
1758 | 1834 | #By default |
|
1759 | 1835 | self.dtype = self.dataOut.dtype |
|
1760 | 1836 | |
|
1761 | 1837 | if datatype is not None: |
|
1762 | 1838 | self.dtype = get_numpy_dtype(datatype) |
|
1763 | 1839 | |
|
1764 | 1840 | if not(self.setNextFile()): |
|
1765 | 1841 | print "[Writing] There isn't a next file" |
|
1766 | 1842 | return 0 |
|
1767 | 1843 | |
|
1768 | 1844 | self.setBlockDimension() |
|
1769 | 1845 | |
|
1770 | 1846 | return 1 |
|
1771 | 1847 | |
|
1772 | 1848 | def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs): |
|
1773 | 1849 | |
|
1774 | 1850 | if not(self.isConfig): |
|
1775 | 1851 | |
|
1776 | 1852 | self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs) |
|
1777 | 1853 | self.isConfig = True |
|
1778 | 1854 | |
|
1779 | 1855 | self.putData() |
@@ -1,849 +1,848 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 3, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import os, sys |
|
8 | 8 | import time, datetime |
|
9 | 9 | import numpy |
|
10 | 10 | import fnmatch |
|
11 | 11 | import glob |
|
12 | 12 | from time import sleep |
|
13 | 13 | |
|
14 | 14 | try: |
|
15 | 15 | import pyfits |
|
16 | 16 | except ImportError, e: |
|
17 | 17 | print "Fits data cannot be used. Install pyfits module" |
|
18 | 18 | |
|
19 | 19 | from xml.etree.ElementTree import ElementTree |
|
20 | 20 | |
|
21 | 21 | from jroIO_base import isRadarFolder, isNumber |
|
22 | 22 | from schainpy.model.data.jrodata import Fits |
|
23 | 23 | from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit |
|
24 | 24 | |
|
25 | 25 | class PyFits(object): |
|
26 | 26 | name=None |
|
27 | 27 | format=None |
|
28 | 28 | array =None |
|
29 | 29 | data =None |
|
30 | 30 | thdulist=None |
|
31 | 31 | prihdr=None |
|
32 | 32 | hdu=None |
|
33 | 33 | |
|
34 | 34 | def __init__(self): |
|
35 | 35 | |
|
36 | 36 | pass |
|
37 | 37 | |
|
38 | 38 | def setColF(self,name,format,array): |
|
39 | 39 | self.name=name |
|
40 | 40 | self.format=format |
|
41 | 41 | self.array=array |
|
42 | 42 | a1=numpy.array([self.array],dtype=numpy.float32) |
|
43 | 43 | self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1) |
|
44 | 44 | return self.col1 |
|
45 | 45 | |
|
46 | 46 | # def setColP(self,name,format,data): |
|
47 | 47 | # self.name=name |
|
48 | 48 | # self.format=format |
|
49 | 49 | # self.data=data |
|
50 | 50 | # a2=numpy.array([self.data],dtype=numpy.float32) |
|
51 | 51 | # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) |
|
52 | 52 | # return self.col2 |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | def writeData(self,name,format,data): |
|
56 | 56 | self.name=name |
|
57 | 57 | self.format=format |
|
58 | 58 | self.data=data |
|
59 | 59 | a2=numpy.array([self.data],dtype=numpy.float32) |
|
60 | 60 | self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) |
|
61 | 61 | return self.col2 |
|
62 | 62 | |
|
63 | 63 | def cFImage(self,idblock,year,month,day,hour,minute,second): |
|
64 | 64 | self.hdu= pyfits.PrimaryHDU(idblock) |
|
65 | 65 | self.hdu.header.set("Year",year) |
|
66 | 66 | self.hdu.header.set("Month",month) |
|
67 | 67 | self.hdu.header.set("Day",day) |
|
68 | 68 | self.hdu.header.set("Hour",hour) |
|
69 | 69 | self.hdu.header.set("Minute",minute) |
|
70 | 70 | self.hdu.header.set("Second",second) |
|
71 | 71 | return self.hdu |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | def Ctable(self,colList): |
|
75 | 75 | self.cols=pyfits.ColDefs(colList) |
|
76 | 76 | self.tbhdu = pyfits.new_table(self.cols) |
|
77 | 77 | return self.tbhdu |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def CFile(self,hdu,tbhdu): |
|
81 | 81 | self.thdulist=pyfits.HDUList([hdu,tbhdu]) |
|
82 | 82 | |
|
83 | 83 | def wFile(self,filename): |
|
84 | 84 | if os.path.isfile(filename): |
|
85 | 85 | os.remove(filename) |
|
86 | 86 | self.thdulist.writeto(filename) |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | class ParameterConf: |
|
90 | 90 | ELEMENTNAME = 'Parameter' |
|
91 | 91 | def __init__(self): |
|
92 | 92 | self.name = '' |
|
93 | 93 | self.value = '' |
|
94 | 94 | |
|
95 | 95 | def readXml(self, parmElement): |
|
96 | 96 | self.name = parmElement.get('name') |
|
97 | 97 | self.value = parmElement.get('value') |
|
98 | 98 | |
|
99 | 99 | def getElementName(self): |
|
100 | 100 | return self.ELEMENTNAME |
|
101 | 101 | |
|
102 | 102 | class Metadata(object): |
|
103 | 103 | |
|
104 | 104 | def __init__(self, filename): |
|
105 | 105 | self.parmConfObjList = [] |
|
106 | 106 | self.readXml(filename) |
|
107 | 107 | |
|
108 | 108 | def readXml(self, filename): |
|
109 | 109 | self.projectElement = None |
|
110 | 110 | self.procUnitConfObjDict = {} |
|
111 | 111 | self.projectElement = ElementTree().parse(filename) |
|
112 | 112 | self.project = self.projectElement.tag |
|
113 | 113 | |
|
114 | 114 | parmElementList = self.projectElement.getiterator(ParameterConf().getElementName()) |
|
115 | 115 | |
|
116 | 116 | for parmElement in parmElementList: |
|
117 | 117 | parmConfObj = ParameterConf() |
|
118 | 118 | parmConfObj.readXml(parmElement) |
|
119 | 119 | self.parmConfObjList.append(parmConfObj) |
|
120 | 120 | |
|
121 | 121 | class FitsWriter(Operation): |
|
122 | ||
|
123 | 122 | def __init__(self, **kwargs): |
|
124 | 123 | Operation.__init__(self, **kwargs) |
|
125 | 124 | self.isConfig = False |
|
126 | 125 | self.dataBlocksPerFile = None |
|
127 | 126 | self.blockIndex = 0 |
|
128 | 127 | self.flagIsNewFile = 1 |
|
129 | 128 | self.fitsObj = None |
|
130 | 129 | self.optchar = 'P' |
|
131 | 130 | self.ext = '.fits' |
|
132 | 131 | self.setFile = 0 |
|
133 | 132 | |
|
134 | 133 | def setFitsHeader(self, dataOut, metadatafile=None): |
|
135 | 134 | |
|
136 | 135 | header_data = pyfits.PrimaryHDU() |
|
137 | 136 | |
|
138 | 137 | header_data.header['EXPNAME'] = "RADAR DATA" |
|
139 | 138 | header_data.header['DATATYPE'] = "SPECTRA" |
|
140 | 139 | header_data.header['COMMENT'] = "" |
|
141 | 140 | |
|
142 | 141 | if metadatafile: |
|
143 | 142 | |
|
144 | 143 | metadata4fits = Metadata(metadatafile) |
|
145 | 144 | |
|
146 | 145 | for parameter in metadata4fits.parmConfObjList: |
|
147 | 146 | parm_name = parameter.name |
|
148 | 147 | parm_value = parameter.value |
|
149 | 148 | |
|
150 | 149 | header_data.header[parm_name] = parm_value |
|
151 | 150 | |
|
152 | 151 | header_data.header['DATETIME'] = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple()) |
|
153 | 152 | header_data.header['CHANNELLIST'] = str(dataOut.channelList) |
|
154 | 153 | header_data.header['NCHANNELS'] = dataOut.nChannels |
|
155 | 154 | #header_data.header['HEIGHTS'] = dataOut.heightList |
|
156 | 155 | header_data.header['NHEIGHTS'] = dataOut.nHeights |
|
157 | 156 | |
|
158 | 157 | header_data.header['IPPSECONDS'] = dataOut.ippSeconds |
|
159 | 158 | header_data.header['NCOHINT'] = dataOut.nCohInt |
|
160 | 159 | header_data.header['NINCOHINT'] = dataOut.nIncohInt |
|
161 | 160 | header_data.header['TIMEZONE'] = dataOut.timeZone |
|
162 | 161 | header_data.header['NBLOCK'] = self.blockIndex |
|
163 | 162 | |
|
164 | 163 | header_data.writeto(self.filename) |
|
165 | 164 | |
|
166 | 165 | self.addExtension(dataOut.heightList,'HEIGHTLIST') |
|
167 | 166 | |
|
168 | 167 | |
|
169 | 168 | def setup(self, dataOut, path, dataBlocksPerFile=100, metadatafile=None): |
|
170 | 169 | |
|
171 | 170 | self.path = path |
|
172 | 171 | self.dataOut = dataOut |
|
173 | 172 | self.metadatafile = metadatafile |
|
174 | 173 | self.dataBlocksPerFile = dataBlocksPerFile |
|
175 | 174 | |
|
176 | 175 | def open(self): |
|
177 | 176 | self.fitsObj = pyfits.open(self.filename, mode='update') |
|
178 | 177 | |
|
179 | 178 | |
|
180 | 179 | def addExtension(self, data, tagname): |
|
181 | 180 | self.open() |
|
182 | 181 | extension = pyfits.ImageHDU(data=data, name=tagname) |
|
183 | 182 | #extension.header['TAG'] = tagname |
|
184 | 183 | self.fitsObj.append(extension) |
|
185 | 184 | self.write() |
|
186 | 185 | |
|
187 | 186 | def addData(self, data): |
|
188 | 187 | self.open() |
|
189 | 188 | extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATATYPE']) |
|
190 | 189 | extension.header['UTCTIME'] = self.dataOut.utctime |
|
191 | 190 | self.fitsObj.append(extension) |
|
192 | 191 | self.blockIndex += 1 |
|
193 | 192 | self.fitsObj[0].header['NBLOCK'] = self.blockIndex |
|
194 | 193 | |
|
195 | 194 | self.write() |
|
196 | 195 | |
|
197 | 196 | def write(self): |
|
198 | 197 | |
|
199 | 198 | self.fitsObj.flush(verbose=True) |
|
200 | 199 | self.fitsObj.close() |
|
201 | 200 | |
|
202 | 201 | |
|
203 | 202 | def setNextFile(self): |
|
204 | 203 | |
|
205 | 204 | ext = self.ext |
|
206 | 205 | path = self.path |
|
207 | 206 | |
|
208 | 207 | timeTuple = time.localtime( self.dataOut.utctime) |
|
209 | 208 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
210 | 209 | |
|
211 | 210 | fullpath = os.path.join( path, subfolder ) |
|
212 | 211 | if not( os.path.exists(fullpath) ): |
|
213 | 212 | os.mkdir(fullpath) |
|
214 | 213 | self.setFile = -1 #inicializo mi contador de seteo |
|
215 | 214 | else: |
|
216 | 215 | filesList = os.listdir( fullpath ) |
|
217 | 216 | if len( filesList ) > 0: |
|
218 | 217 | filesList = sorted( filesList, key=str.lower ) |
|
219 | 218 | filen = filesList[-1] |
|
220 | 219 | |
|
221 | 220 | if isNumber( filen[8:11] ): |
|
222 | 221 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
223 | 222 | else: |
|
224 | 223 | self.setFile = -1 |
|
225 | 224 | else: |
|
226 | 225 | self.setFile = -1 #inicializo mi contador de seteo |
|
227 | 226 | |
|
228 | 227 | setFile = self.setFile |
|
229 | 228 | setFile += 1 |
|
230 | 229 | |
|
231 | 230 | thisFile = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, |
|
232 | 231 | timeTuple.tm_year, |
|
233 | 232 | timeTuple.tm_yday, |
|
234 | 233 | setFile, |
|
235 | 234 | ext ) |
|
236 | 235 | |
|
237 | 236 | filename = os.path.join( path, subfolder, thisFile ) |
|
238 | 237 | |
|
239 | 238 | self.blockIndex = 0 |
|
240 | 239 | self.filename = filename |
|
241 | 240 | self.setFile = setFile |
|
242 | 241 | self.flagIsNewFile = 1 |
|
243 | 242 | |
|
244 | 243 | print 'Writing the file: %s'%self.filename |
|
245 | 244 | |
|
246 | 245 | self.setFitsHeader(self.dataOut, self.metadatafile) |
|
247 | 246 | |
|
248 | 247 | return 1 |
|
249 | 248 | |
|
250 | 249 | def writeBlock(self): |
|
251 | 250 | self.addData(self.dataOut.data_spc) |
|
252 | 251 | self.flagIsNewFile = 0 |
|
253 | 252 | |
|
254 | 253 | |
|
255 | 254 | def __setNewBlock(self): |
|
256 | 255 | |
|
257 | 256 | if self.flagIsNewFile: |
|
258 | 257 | return 1 |
|
259 | 258 | |
|
260 | 259 | if self.blockIndex < self.dataBlocksPerFile: |
|
261 | 260 | return 1 |
|
262 | 261 | |
|
263 | 262 | if not( self.setNextFile() ): |
|
264 | 263 | return 0 |
|
265 | 264 | |
|
266 | 265 | return 1 |
|
267 | 266 | |
|
268 | 267 | def writeNextBlock(self): |
|
269 | 268 | if not( self.__setNewBlock() ): |
|
270 | 269 | return 0 |
|
271 | 270 | self.writeBlock() |
|
272 | 271 | return 1 |
|
273 | 272 | |
|
274 | 273 | def putData(self): |
|
275 | 274 | if self.flagIsNewFile: |
|
276 | 275 | self.setNextFile() |
|
277 | 276 | self.writeNextBlock() |
|
278 | 277 | |
|
279 | def run(self, dataOut, **kwargs): | |
|
278 | def run(self, dataOut, path, dataBlocksPerFile=100, metadatafile=None, **kwargs): | |
|
280 | 279 | if not(self.isConfig): |
|
281 | self.setup(dataOut, **kwargs) | |
|
280 | self.setup(dataOut, path, dataBlocksPerFile=dataBlocksPerFile, metadatafile=metadatafile, **kwargs) | |
|
282 | 281 | self.isConfig = True |
|
283 | 282 | self.putData() |
|
284 | 283 | |
|
285 | 284 | |
|
286 | 285 | class FitsReader(ProcessingUnit): |
|
287 | 286 | |
|
288 | 287 | # __TIMEZONE = time.timezone |
|
289 | 288 | |
|
290 | 289 | expName = None |
|
291 | 290 | datetimestr = None |
|
292 | 291 | utc = None |
|
293 | 292 | nChannels = None |
|
294 | 293 | nSamples = None |
|
295 | 294 | dataBlocksPerFile = None |
|
296 | 295 | comments = None |
|
297 | 296 | lastUTTime = None |
|
298 | 297 | header_dict = None |
|
299 | 298 | data = None |
|
300 | 299 | data_header_dict = None |
|
301 | 300 | |
|
302 | 301 | def __init__(self, **kwargs): |
|
303 | 302 | ProcessingUnit.__init__(self, **kwargs) |
|
304 | 303 | self.isConfig = False |
|
305 | 304 | self.ext = '.fits' |
|
306 | 305 | self.setFile = 0 |
|
307 | 306 | self.flagNoMoreFiles = 0 |
|
308 | 307 | self.flagIsNewFile = 1 |
|
309 | 308 | self.flagDiscontinuousBlock = None |
|
310 | 309 | self.fileIndex = None |
|
311 | 310 | self.filename = None |
|
312 | 311 | self.fileSize = None |
|
313 | 312 | self.fitsObj = None |
|
314 | 313 | self.timeZone = None |
|
315 | 314 | self.nReadBlocks = 0 |
|
316 | 315 | self.nTotalBlocks = 0 |
|
317 | 316 | self.dataOut = self.createObjByDefault() |
|
318 | 317 | self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup() |
|
319 | 318 | self.blockIndex = 1 |
|
320 | 319 | |
|
321 | 320 | def createObjByDefault(self): |
|
322 | 321 | |
|
323 | 322 | dataObj = Fits() |
|
324 | 323 | |
|
325 | 324 | return dataObj |
|
326 | 325 | |
|
327 | 326 | def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False): |
|
328 | 327 | try: |
|
329 | 328 | fitsObj = pyfits.open(filename,'readonly') |
|
330 | 329 | except: |
|
331 | 330 | print "File %s can't be opened" %(filename) |
|
332 | 331 | return None |
|
333 | 332 | |
|
334 | 333 | header = fitsObj[0].header |
|
335 | 334 | struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S") |
|
336 | 335 | utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS |
|
337 | 336 | |
|
338 | 337 | ltc = utc |
|
339 | 338 | if useLocalTime: |
|
340 | 339 | ltc -= time.timezone |
|
341 | 340 | thisDatetime = datetime.datetime.utcfromtimestamp(ltc) |
|
342 | 341 | thisTime = thisDatetime.time() |
|
343 | 342 | |
|
344 | 343 | if not ((startTime <= thisTime) and (endTime > thisTime)): |
|
345 | 344 | return None |
|
346 | 345 | |
|
347 | 346 | return thisDatetime |
|
348 | 347 | |
|
349 | 348 | def __setNextFileOnline(self): |
|
350 | 349 | raise NotImplementedError |
|
351 | 350 | |
|
352 | 351 | def __setNextFileOffline(self): |
|
353 | 352 | idFile = self.fileIndex |
|
354 | 353 | |
|
355 | 354 | while (True): |
|
356 | 355 | idFile += 1 |
|
357 | 356 | if not(idFile < len(self.filenameList)): |
|
358 | 357 | self.flagNoMoreFiles = 1 |
|
359 | 358 | print "No more Files" |
|
360 | 359 | return 0 |
|
361 | 360 | |
|
362 | 361 | filename = self.filenameList[idFile] |
|
363 | 362 | |
|
364 | 363 | # if not(self.__verifyFile(filename)): |
|
365 | 364 | # continue |
|
366 | 365 | |
|
367 | 366 | fileSize = os.path.getsize(filename) |
|
368 | 367 | fitsObj = pyfits.open(filename,'readonly') |
|
369 | 368 | break |
|
370 | 369 | |
|
371 | 370 | self.flagIsNewFile = 1 |
|
372 | 371 | self.fileIndex = idFile |
|
373 | 372 | self.filename = filename |
|
374 | 373 | self.fileSize = fileSize |
|
375 | 374 | self.fitsObj = fitsObj |
|
376 | 375 | self.blockIndex = 0 |
|
377 | 376 | print "Setting the file: %s"%self.filename |
|
378 | 377 | |
|
379 | 378 | return 1 |
|
380 | 379 | |
|
381 | 380 | def __setValuesFromHeader(self): |
|
382 | 381 | |
|
383 | 382 | self.dataOut.header = self.header_dict |
|
384 | 383 | self.dataOut.expName = self.expName |
|
385 | 384 | |
|
386 | 385 | self.dataOut.timeZone = self.timeZone |
|
387 | 386 | self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile |
|
388 | 387 | self.dataOut.comments = self.comments |
|
389 | 388 | # self.dataOut.timeInterval = self.timeInterval |
|
390 | 389 | self.dataOut.channelList = self.channelList |
|
391 | 390 | self.dataOut.heightList = self.heightList |
|
392 | 391 | |
|
393 | 392 | self.dataOut.nCohInt = self.nCohInt |
|
394 | 393 | self.dataOut.nIncohInt = self.nIncohInt |
|
395 | 394 | |
|
396 | 395 | self.dataOut.ippSeconds = self.ippSeconds |
|
397 | 396 | |
|
398 | 397 | def readHeader(self): |
|
399 | 398 | headerObj = self.fitsObj[0] |
|
400 | 399 | |
|
401 | 400 | self.header_dict = headerObj.header |
|
402 | 401 | if 'EXPNAME' in headerObj.header.keys(): |
|
403 | 402 | self.expName = headerObj.header['EXPNAME'] |
|
404 | 403 | |
|
405 | 404 | if 'DATATYPE' in headerObj.header.keys(): |
|
406 | 405 | self.dataType = headerObj.header['DATATYPE'] |
|
407 | 406 | |
|
408 | 407 | self.datetimestr = headerObj.header['DATETIME'] |
|
409 | 408 | channelList = headerObj.header['CHANNELLIST'] |
|
410 | 409 | channelList = channelList.split('[') |
|
411 | 410 | channelList = channelList[1].split(']') |
|
412 | 411 | channelList = channelList[0].split(',') |
|
413 | 412 | channelList = [int(ch) for ch in channelList] |
|
414 | 413 | self.channelList = channelList |
|
415 | 414 | self.nChannels = headerObj.header['NCHANNELS'] |
|
416 | 415 | self.nHeights = headerObj.header['NHEIGHTS'] |
|
417 | 416 | self.ippSeconds = headerObj.header['IPPSECONDS'] |
|
418 | 417 | self.nCohInt = headerObj.header['NCOHINT'] |
|
419 | 418 | self.nIncohInt = headerObj.header['NINCOHINT'] |
|
420 | 419 | self.dataBlocksPerFile = headerObj.header['NBLOCK'] |
|
421 | 420 | self.timeZone = headerObj.header['TIMEZONE'] |
|
422 | 421 | |
|
423 | 422 | # self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt |
|
424 | 423 | |
|
425 | 424 | if 'COMMENT' in headerObj.header.keys(): |
|
426 | 425 | self.comments = headerObj.header['COMMENT'] |
|
427 | 426 | |
|
428 | 427 | self.readHeightList() |
|
429 | 428 | |
|
430 | 429 | def readHeightList(self): |
|
431 | 430 | self.blockIndex = self.blockIndex + 1 |
|
432 | 431 | obj = self.fitsObj[self.blockIndex] |
|
433 | 432 | self.heightList = obj.data |
|
434 | 433 | self.blockIndex = self.blockIndex + 1 |
|
435 | 434 | |
|
436 | 435 | def readExtension(self): |
|
437 | 436 | obj = self.fitsObj[self.blockIndex] |
|
438 | 437 | self.heightList = obj.data |
|
439 | 438 | self.blockIndex = self.blockIndex + 1 |
|
440 | 439 | |
|
441 | 440 | def setNextFile(self): |
|
442 | 441 | |
|
443 | 442 | if self.online: |
|
444 | 443 | newFile = self.__setNextFileOnline() |
|
445 | 444 | else: |
|
446 | 445 | newFile = self.__setNextFileOffline() |
|
447 | 446 | |
|
448 | 447 | if not(newFile): |
|
449 | 448 | return 0 |
|
450 | 449 | |
|
451 | 450 | self.readHeader() |
|
452 | 451 | self.__setValuesFromHeader() |
|
453 | 452 | self.nReadBlocks = 0 |
|
454 | 453 | # self.blockIndex = 1 |
|
455 | 454 | return 1 |
|
456 | 455 | |
|
457 | 456 | def __searchFilesOffLine(self, |
|
458 | 457 | path, |
|
459 | 458 | startDate, |
|
460 | 459 | endDate, |
|
461 | 460 | startTime=datetime.time(0,0,0), |
|
462 | 461 | endTime=datetime.time(23,59,59), |
|
463 | 462 | set=None, |
|
464 | 463 | expLabel='', |
|
465 | 464 | ext='.fits', |
|
466 | 465 | walk=True): |
|
467 | 466 | |
|
468 | 467 | pathList = [] |
|
469 | 468 | |
|
470 | 469 | if not walk: |
|
471 | 470 | pathList.append(path) |
|
472 | 471 | |
|
473 | 472 | else: |
|
474 | 473 | dirList = [] |
|
475 | 474 | for thisPath in os.listdir(path): |
|
476 | 475 | if not os.path.isdir(os.path.join(path,thisPath)): |
|
477 | 476 | continue |
|
478 | 477 | if not isRadarFolder(thisPath): |
|
479 | 478 | continue |
|
480 | 479 | |
|
481 | 480 | dirList.append(thisPath) |
|
482 | 481 | |
|
483 | 482 | if not(dirList): |
|
484 | 483 | return None, None |
|
485 | 484 | |
|
486 | 485 | thisDate = startDate |
|
487 | 486 | |
|
488 | 487 | while(thisDate <= endDate): |
|
489 | 488 | year = thisDate.timetuple().tm_year |
|
490 | 489 | doy = thisDate.timetuple().tm_yday |
|
491 | 490 | |
|
492 | 491 | matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') |
|
493 | 492 | if len(matchlist) == 0: |
|
494 | 493 | thisDate += datetime.timedelta(1) |
|
495 | 494 | continue |
|
496 | 495 | for match in matchlist: |
|
497 | 496 | pathList.append(os.path.join(path,match,expLabel)) |
|
498 | 497 | |
|
499 | 498 | thisDate += datetime.timedelta(1) |
|
500 | 499 | |
|
501 | 500 | if pathList == []: |
|
502 | 501 | print "Any folder was found for the date range: %s-%s" %(startDate, endDate) |
|
503 | 502 | return None, None |
|
504 | 503 | |
|
505 | 504 | print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) |
|
506 | 505 | |
|
507 | 506 | filenameList = [] |
|
508 | 507 | datetimeList = [] |
|
509 | 508 | |
|
510 | 509 | for i in range(len(pathList)): |
|
511 | 510 | |
|
512 | 511 | thisPath = pathList[i] |
|
513 | 512 | |
|
514 | 513 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
515 | 514 | fileList.sort() |
|
516 | 515 | |
|
517 | 516 | for thisFile in fileList: |
|
518 | 517 | |
|
519 | 518 | filename = os.path.join(thisPath,thisFile) |
|
520 | 519 | thisDatetime = self.isFileinThisTime(filename, startTime, endTime) |
|
521 | 520 | |
|
522 | 521 | if not(thisDatetime): |
|
523 | 522 | continue |
|
524 | 523 | |
|
525 | 524 | filenameList.append(filename) |
|
526 | 525 | datetimeList.append(thisDatetime) |
|
527 | 526 | |
|
528 | 527 | if not(filenameList): |
|
529 | 528 | print "Any file was found for the time range %s - %s" %(startTime, endTime) |
|
530 | 529 | return None, None |
|
531 | 530 | |
|
532 | 531 | print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) |
|
533 | 532 | |
|
534 | 533 | |
|
535 | 534 | for i in range(len(filenameList)): |
|
536 | 535 | print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) |
|
537 | 536 | |
|
538 | 537 | self.filenameList = filenameList |
|
539 | 538 | self.datetimeList = datetimeList |
|
540 | 539 | |
|
541 | 540 | return pathList, filenameList |
|
542 | 541 | |
|
543 | 542 | def setup(self, path=None, |
|
544 | 543 | startDate=None, |
|
545 | 544 | endDate=None, |
|
546 | 545 | startTime=datetime.time(0,0,0), |
|
547 | 546 | endTime=datetime.time(23,59,59), |
|
548 | 547 | set=0, |
|
549 | 548 | expLabel = "", |
|
550 | 549 | ext = None, |
|
551 | 550 | online = False, |
|
552 | 551 | delay = 60, |
|
553 | 552 | walk = True): |
|
554 | 553 | |
|
555 | 554 | if path == None: |
|
556 | 555 | raise ValueError, "The path is not valid" |
|
557 | 556 | |
|
558 | 557 | if ext == None: |
|
559 | 558 | ext = self.ext |
|
560 | 559 | |
|
561 | 560 | if not(online): |
|
562 | 561 | print "Searching files in offline mode ..." |
|
563 | 562 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
564 | 563 | startTime=startTime, endTime=endTime, |
|
565 | 564 | set=set, expLabel=expLabel, ext=ext, |
|
566 | 565 | walk=walk) |
|
567 | 566 | |
|
568 | 567 | if not(pathList): |
|
569 | 568 | print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, |
|
570 | 569 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
571 | 570 | datetime.datetime.combine(endDate,endTime).ctime()) |
|
572 | 571 | |
|
573 | 572 | sys.exit(-1) |
|
574 | 573 | |
|
575 | 574 | self.fileIndex = -1 |
|
576 | 575 | self.pathList = pathList |
|
577 | 576 | self.filenameList = filenameList |
|
578 | 577 | |
|
579 | 578 | self.online = online |
|
580 | 579 | self.delay = delay |
|
581 | 580 | ext = ext.lower() |
|
582 | 581 | self.ext = ext |
|
583 | 582 | |
|
584 | 583 | if not(self.setNextFile()): |
|
585 | 584 | if (startDate!=None) and (endDate!=None): |
|
586 | 585 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
587 | 586 | elif startDate != None: |
|
588 | 587 | print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) |
|
589 | 588 | else: |
|
590 | 589 | print "No files" |
|
591 | 590 | |
|
592 | 591 | sys.exit(-1) |
|
593 | 592 | |
|
594 | 593 | |
|
595 | 594 | |
|
596 | 595 | def readBlock(self): |
|
597 | 596 | dataObj = self.fitsObj[self.blockIndex] |
|
598 | 597 | |
|
599 | 598 | self.data = dataObj.data |
|
600 | 599 | self.data_header_dict = dataObj.header |
|
601 | 600 | self.utc = self.data_header_dict['UTCTIME'] |
|
602 | 601 | |
|
603 | 602 | self.flagIsNewFile = 0 |
|
604 | 603 | self.blockIndex += 1 |
|
605 | 604 | self.nTotalBlocks += 1 |
|
606 | 605 | self.nReadBlocks += 1 |
|
607 | 606 | |
|
608 | 607 | return 1 |
|
609 | 608 | |
|
610 | 609 | def __jumpToLastBlock(self): |
|
611 | 610 | raise NotImplementedError |
|
612 | 611 | |
|
613 | 612 | def __waitNewBlock(self): |
|
614 | 613 | """ |
|
615 | 614 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. |
|
616 | 615 | |
|
617 | 616 | Si el modo de lectura es OffLine siempre retorn 0 |
|
618 | 617 | """ |
|
619 | 618 | if not self.online: |
|
620 | 619 | return 0 |
|
621 | 620 | |
|
622 | 621 | if (self.nReadBlocks >= self.dataBlocksPerFile): |
|
623 | 622 | return 0 |
|
624 | 623 | |
|
625 | 624 | currentPointer = self.fp.tell() |
|
626 | 625 | |
|
627 | 626 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
628 | 627 | |
|
629 | 628 | for nTries in range( self.nTries ): |
|
630 | 629 | |
|
631 | 630 | self.fp.close() |
|
632 | 631 | self.fp = open( self.filename, 'rb' ) |
|
633 | 632 | self.fp.seek( currentPointer ) |
|
634 | 633 | |
|
635 | 634 | self.fileSize = os.path.getsize( self.filename ) |
|
636 | 635 | currentSize = self.fileSize - currentPointer |
|
637 | 636 | |
|
638 | 637 | if ( currentSize >= neededSize ): |
|
639 | 638 | self.__rdBasicHeader() |
|
640 | 639 | return 1 |
|
641 | 640 | |
|
642 | 641 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
643 | 642 | sleep( self.delay ) |
|
644 | 643 | |
|
645 | 644 | |
|
646 | 645 | return 0 |
|
647 | 646 | |
|
648 | 647 | def __setNewBlock(self): |
|
649 | 648 | |
|
650 | 649 | if self.online: |
|
651 | 650 | self.__jumpToLastBlock() |
|
652 | 651 | |
|
653 | 652 | if self.flagIsNewFile: |
|
654 | 653 | return 1 |
|
655 | 654 | |
|
656 | 655 | self.lastUTTime = self.utc |
|
657 | 656 | |
|
658 | 657 | if self.online: |
|
659 | 658 | if self.__waitNewBlock(): |
|
660 | 659 | return 1 |
|
661 | 660 | |
|
662 | 661 | if self.nReadBlocks < self.dataBlocksPerFile: |
|
663 | 662 | return 1 |
|
664 | 663 | |
|
665 | 664 | if not(self.setNextFile()): |
|
666 | 665 | return 0 |
|
667 | 666 | |
|
668 | 667 | deltaTime = self.utc - self.lastUTTime |
|
669 | 668 | |
|
670 | 669 | self.flagDiscontinuousBlock = 0 |
|
671 | 670 | |
|
672 | 671 | if deltaTime > self.maxTimeStep: |
|
673 | 672 | self.flagDiscontinuousBlock = 1 |
|
674 | 673 | |
|
675 | 674 | return 1 |
|
676 | 675 | |
|
677 | 676 | |
|
678 | 677 | def readNextBlock(self): |
|
679 | 678 | if not(self.__setNewBlock()): |
|
680 | 679 | return 0 |
|
681 | 680 | |
|
682 | 681 | if not(self.readBlock()): |
|
683 | 682 | return 0 |
|
684 | 683 | |
|
685 | 684 | return 1 |
|
686 | 685 | |
|
687 | 686 | def printInfo(self): |
|
688 | 687 | |
|
689 | 688 | pass |
|
690 | 689 | |
|
691 | 690 | def getData(self): |
|
692 | 691 | |
|
693 | 692 | if self.flagNoMoreFiles: |
|
694 | 693 | self.dataOut.flagNoData = True |
|
695 | 694 | print 'Process finished' |
|
696 | 695 | return 0 |
|
697 | 696 | |
|
698 | 697 | self.flagDiscontinuousBlock = 0 |
|
699 | 698 | self.flagIsNewBlock = 0 |
|
700 | 699 | |
|
701 | 700 | if not(self.readNextBlock()): |
|
702 | 701 | return 0 |
|
703 | 702 | |
|
704 | 703 | if self.data is None: |
|
705 | 704 | self.dataOut.flagNoData = True |
|
706 | 705 | return 0 |
|
707 | 706 | |
|
708 | 707 | self.dataOut.data = self.data |
|
709 | 708 | self.dataOut.data_header = self.data_header_dict |
|
710 | 709 | self.dataOut.utctime = self.utc |
|
711 | 710 | |
|
712 | 711 | # self.dataOut.header = self.header_dict |
|
713 | 712 | # self.dataOut.expName = self.expName |
|
714 | 713 | # self.dataOut.nChannels = self.nChannels |
|
715 | 714 | # self.dataOut.timeZone = self.timeZone |
|
716 | 715 | # self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile |
|
717 | 716 | # self.dataOut.comments = self.comments |
|
718 | 717 | # # self.dataOut.timeInterval = self.timeInterval |
|
719 | 718 | # self.dataOut.channelList = self.channelList |
|
720 | 719 | # self.dataOut.heightList = self.heightList |
|
721 | 720 | self.dataOut.flagNoData = False |
|
722 | 721 | |
|
723 | 722 | return self.dataOut.data |
|
724 | 723 | |
|
725 | 724 | def run(self, **kwargs): |
|
726 | 725 | |
|
727 | 726 | if not(self.isConfig): |
|
728 | 727 | self.setup(**kwargs) |
|
729 | 728 | self.isConfig = True |
|
730 | 729 | |
|
731 | 730 | self.getData() |
|
732 | 731 | |
|
733 | 732 | class SpectraHeisWriter(Operation): |
|
734 | 733 | # set = None |
|
735 | 734 | setFile = None |
|
736 | 735 | idblock = None |
|
737 | 736 | doypath = None |
|
738 | 737 | subfolder = None |
|
739 | 738 | |
|
740 | 739 | def __init__(self, **kwargs): |
|
741 | 740 | Operation.__init__(self, **kwargs) |
|
742 | 741 | self.wrObj = PyFits() |
|
743 | 742 | # self.dataOut = dataOut |
|
744 | 743 | self.nTotalBlocks=0 |
|
745 | 744 | # self.set = None |
|
746 | 745 | self.setFile = None |
|
747 | 746 | self.idblock = 0 |
|
748 | 747 | self.wrpath = None |
|
749 | 748 | self.doypath = None |
|
750 | 749 | self.subfolder = None |
|
751 | 750 | self.isConfig = False |
|
752 | 751 | |
|
753 | 752 | def isNumber(str): |
|
754 | 753 | """ |
|
755 | 754 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
756 | 755 | |
|
757 | 756 | Excepciones: |
|
758 | 757 | Si un determinado string no puede ser convertido a numero |
|
759 | 758 | Input: |
|
760 | 759 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
761 | 760 | |
|
762 | 761 | Return: |
|
763 | 762 | True : si el string es uno numerico |
|
764 | 763 | False : no es un string numerico |
|
765 | 764 | """ |
|
766 | 765 | try: |
|
767 | 766 | float( str ) |
|
768 | 767 | return True |
|
769 | 768 | except: |
|
770 | 769 | return False |
|
771 | 770 | |
|
772 | 771 | def setup(self, dataOut, wrpath): |
|
773 | 772 | |
|
774 | 773 | if not(os.path.exists(wrpath)): |
|
775 | 774 | os.mkdir(wrpath) |
|
776 | 775 | |
|
777 | 776 | self.wrpath = wrpath |
|
778 | 777 | # self.setFile = 0 |
|
779 | 778 | self.dataOut = dataOut |
|
780 | 779 | |
|
781 | 780 | def putData(self): |
|
782 | 781 | name= time.localtime( self.dataOut.utctime) |
|
783 | 782 | ext=".fits" |
|
784 | 783 | |
|
785 | 784 | if self.doypath == None: |
|
786 | 785 | self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple())) |
|
787 | 786 | self.doypath = os.path.join( self.wrpath, self.subfolder ) |
|
788 | 787 | os.mkdir(self.doypath) |
|
789 | 788 | |
|
790 | 789 | if self.setFile == None: |
|
791 | 790 | # self.set = self.dataOut.set |
|
792 | 791 | self.setFile = 0 |
|
793 | 792 | # if self.set != self.dataOut.set: |
|
794 | 793 | ## self.set = self.dataOut.set |
|
795 | 794 | # self.setFile = 0 |
|
796 | 795 | |
|
797 | 796 | #make the filename |
|
798 | 797 | thisFile = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext) |
|
799 | 798 | |
|
800 | 799 | filename = os.path.join(self.wrpath,self.subfolder, thisFile) |
|
801 | 800 | |
|
802 | 801 | idblock = numpy.array([self.idblock],dtype="int64") |
|
803 | 802 | header=self.wrObj.cFImage(idblock=idblock, |
|
804 | 803 | year=time.gmtime(self.dataOut.utctime).tm_year, |
|
805 | 804 | month=time.gmtime(self.dataOut.utctime).tm_mon, |
|
806 | 805 | day=time.gmtime(self.dataOut.utctime).tm_mday, |
|
807 | 806 | hour=time.gmtime(self.dataOut.utctime).tm_hour, |
|
808 | 807 | minute=time.gmtime(self.dataOut.utctime).tm_min, |
|
809 | 808 | second=time.gmtime(self.dataOut.utctime).tm_sec) |
|
810 | 809 | |
|
811 | 810 | c=3E8 |
|
812 | 811 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
813 | 812 | freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000)) |
|
814 | 813 | |
|
815 | 814 | colList = [] |
|
816 | 815 | |
|
817 | 816 | colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq) |
|
818 | 817 | |
|
819 | 818 | colList.append(colFreq) |
|
820 | 819 | |
|
821 | 820 | nchannel=self.dataOut.nChannels |
|
822 | 821 | |
|
823 | 822 | for i in range(nchannel): |
|
824 | 823 | col = self.wrObj.writeData(name="PCh"+str(i+1), |
|
825 | 824 | format=str(self.dataOut.nFFTPoints)+'E', |
|
826 | 825 | data=10*numpy.log10(self.dataOut.data_spc[i,:])) |
|
827 | 826 | |
|
828 | 827 | colList.append(col) |
|
829 | 828 | |
|
830 | 829 | data=self.wrObj.Ctable(colList=colList) |
|
831 | 830 | |
|
832 | 831 | self.wrObj.CFile(header,data) |
|
833 | 832 | |
|
834 | 833 | self.wrObj.wFile(filename) |
|
835 | 834 | |
|
836 | 835 | #update the setFile |
|
837 | 836 | self.setFile += 1 |
|
838 | 837 | self.idblock += 1 |
|
839 | 838 | |
|
840 | 839 | return 1 |
|
841 | 840 | |
|
842 | 841 | def run(self, dataOut, **kwargs): |
|
843 | 842 | |
|
844 | 843 | if not(self.isConfig): |
|
845 | 844 | |
|
846 | 845 | self.setup(dataOut, **kwargs) |
|
847 | 846 | self.isConfig = True |
|
848 | 847 | |
|
849 | 848 | self.putData() |
@@ -1,1090 +1,1095 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import time |
|
3 | 3 | import os |
|
4 | 4 | import h5py |
|
5 | 5 | import re |
|
6 | 6 | import datetime |
|
7 | 7 | |
|
8 | 8 | from schainpy.model.data.jrodata import * |
|
9 | 9 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation |
|
10 | 10 | # from jroIO_base import * |
|
11 | 11 | from schainpy.model.io.jroIO_base import * |
|
12 | 12 | import schainpy |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | class ParamReader(ProcessingUnit): |
|
16 | 16 | ''' |
|
17 | 17 | Reads HDF5 format files |
|
18 | 18 | |
|
19 | 19 | path |
|
20 | 20 | |
|
21 | 21 | startDate |
|
22 | 22 | |
|
23 | 23 | endDate |
|
24 | 24 | |
|
25 | 25 | startTime |
|
26 | 26 | |
|
27 | 27 | endTime |
|
28 | 28 | ''' |
|
29 | 29 | |
|
30 | 30 | ext = ".hdf5" |
|
31 | 31 | |
|
32 | 32 | optchar = "D" |
|
33 | 33 | |
|
34 | 34 | timezone = None |
|
35 | 35 | |
|
36 | 36 | startTime = None |
|
37 | 37 | |
|
38 | 38 | endTime = None |
|
39 | 39 | |
|
40 | 40 | fileIndex = None |
|
41 | 41 | |
|
42 | 42 | utcList = None #To select data in the utctime list |
|
43 | 43 | |
|
44 | 44 | blockList = None #List to blocks to be read from the file |
|
45 | 45 | |
|
46 | 46 | blocksPerFile = None #Number of blocks to be read |
|
47 | 47 | |
|
48 | 48 | blockIndex = None |
|
49 | 49 | |
|
50 | 50 | path = None |
|
51 | 51 | |
|
52 | 52 | #List of Files |
|
53 | 53 | |
|
54 | 54 | filenameList = None |
|
55 | 55 | |
|
56 | 56 | datetimeList = None |
|
57 | 57 | |
|
58 | 58 | #Hdf5 File |
|
59 | 59 | |
|
60 | 60 | listMetaname = None |
|
61 | 61 | |
|
62 | 62 | listMeta = None |
|
63 | 63 | |
|
64 | 64 | listDataname = None |
|
65 | 65 | |
|
66 | 66 | listData = None |
|
67 | 67 | |
|
68 | 68 | listShapes = None |
|
69 | 69 | |
|
70 | 70 | fp = None |
|
71 | 71 | |
|
72 | 72 | #dataOut reconstruction |
|
73 | 73 | |
|
74 | 74 | dataOut = None |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | def __init__(self, **kwargs): |
|
78 | 78 | ProcessingUnit.__init__(self, **kwargs) |
|
79 | 79 | self.dataOut = Parameters() |
|
80 | 80 | return |
|
81 | 81 | |
|
82 | 82 | def setup(self, **kwargs): |
|
83 | 83 | |
|
84 | 84 | path = kwargs['path'] |
|
85 | 85 | startDate = kwargs['startDate'] |
|
86 | 86 | endDate = kwargs['endDate'] |
|
87 | 87 | startTime = kwargs['startTime'] |
|
88 | 88 | endTime = kwargs['endTime'] |
|
89 | 89 | walk = kwargs['walk'] |
|
90 | 90 | if kwargs.has_key('ext'): |
|
91 | 91 | ext = kwargs['ext'] |
|
92 | 92 | else: |
|
93 | 93 | ext = '.hdf5' |
|
94 | 94 | if kwargs.has_key('timezone'): |
|
95 | 95 | self.timezone = kwargs['timezone'] |
|
96 | 96 | else: |
|
97 | 97 | self.timezone = 'lt' |
|
98 | 98 | |
|
99 | 99 | print "[Reading] Searching files in offline mode ..." |
|
100 | 100 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
101 | 101 | startTime=startTime, endTime=endTime, |
|
102 | 102 | ext=ext, walk=walk) |
|
103 | 103 | |
|
104 | 104 | if not(filenameList): |
|
105 | 105 | print "There is no files into the folder: %s"%(path) |
|
106 | 106 | sys.exit(-1) |
|
107 | 107 | |
|
108 | 108 | self.fileIndex = -1 |
|
109 | 109 | self.startTime = startTime |
|
110 | 110 | self.endTime = endTime |
|
111 | 111 | |
|
112 | 112 | self.__readMetadata() |
|
113 | 113 | |
|
114 | 114 | self.__setNextFileOffline() |
|
115 | 115 | |
|
116 | 116 | return |
|
117 | 117 | |
|
118 | 118 | def __searchFilesOffLine(self, |
|
119 | 119 | path, |
|
120 | 120 | startDate=None, |
|
121 | 121 | endDate=None, |
|
122 | 122 | startTime=datetime.time(0,0,0), |
|
123 | 123 | endTime=datetime.time(23,59,59), |
|
124 | 124 | ext='.hdf5', |
|
125 | 125 | walk=True): |
|
126 | 126 | |
|
127 | 127 | expLabel = '' |
|
128 | 128 | self.filenameList = [] |
|
129 | 129 | self.datetimeList = [] |
|
130 | 130 | |
|
131 | 131 | pathList = [] |
|
132 | 132 | |
|
133 | 133 | JRODataObj = JRODataReader() |
|
134 | 134 | dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) |
|
135 | 135 | |
|
136 | 136 | if dateList == []: |
|
137 | 137 | print "[Reading] No *%s files in %s from %s to %s)"%(ext, path, |
|
138 | 138 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
139 | 139 | datetime.datetime.combine(endDate,endTime).ctime()) |
|
140 | 140 | |
|
141 | 141 | return None, None |
|
142 | 142 | |
|
143 | 143 | if len(dateList) > 1: |
|
144 | 144 | print "[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate) |
|
145 | 145 | else: |
|
146 | 146 | print "[Reading] data was found for the date %s" %(dateList[0]) |
|
147 | 147 | |
|
148 | 148 | filenameList = [] |
|
149 | 149 | datetimeList = [] |
|
150 | 150 | |
|
151 | 151 | #---------------------------------------------------------------------------------- |
|
152 | 152 | |
|
153 | 153 | for thisPath in pathList: |
|
154 | 154 | # thisPath = pathList[pathDict[file]] |
|
155 | 155 | |
|
156 | 156 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
157 | 157 | fileList.sort() |
|
158 | 158 | |
|
159 | 159 | for file in fileList: |
|
160 | 160 | |
|
161 | 161 | filename = os.path.join(thisPath,file) |
|
162 | 162 | |
|
163 | 163 | if not isFileInDateRange(filename, startDate, endDate): |
|
164 | 164 | continue |
|
165 | 165 | |
|
166 | 166 | thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime) |
|
167 | 167 | |
|
168 | 168 | if not(thisDatetime): |
|
169 | 169 | continue |
|
170 | 170 | |
|
171 | 171 | filenameList.append(filename) |
|
172 | 172 | datetimeList.append(thisDatetime) |
|
173 | 173 | |
|
174 | 174 | if not(filenameList): |
|
175 | 175 | print "[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
176 | 176 | return None, None |
|
177 | 177 | |
|
178 | 178 | print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime) |
|
179 | 179 | |
|
180 | 180 | |
|
181 | 181 | for i in range(len(filenameList)): |
|
182 | 182 | print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) |
|
183 | 183 | |
|
184 | 184 | self.filenameList = filenameList |
|
185 | 185 | self.datetimeList = datetimeList |
|
186 | 186 | |
|
187 | 187 | return pathList, filenameList |
|
188 | 188 | |
|
189 | 189 | def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime): |
|
190 | 190 | |
|
191 | 191 | """ |
|
192 | 192 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
193 | 193 | |
|
194 | 194 | Inputs: |
|
195 | 195 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
196 | 196 | |
|
197 | 197 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
198 | 198 | |
|
199 | 199 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
200 | 200 | |
|
201 | 201 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
202 | 202 | |
|
203 | 203 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
204 | 204 | |
|
205 | 205 | Return: |
|
206 | 206 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
207 | 207 | fecha especificado, de lo contrario retorna False. |
|
208 | 208 | |
|
209 | 209 | Excepciones: |
|
210 | 210 | Si el archivo no existe o no puede ser abierto |
|
211 | 211 | Si la cabecera no puede ser leida. |
|
212 | 212 | |
|
213 | 213 | """ |
|
214 | 214 | |
|
215 | 215 | try: |
|
216 | 216 | fp = h5py.File(filename,'r') |
|
217 | 217 | grp1 = fp['Data'] |
|
218 | 218 | |
|
219 | 219 | except IOError: |
|
220 | 220 | traceback.print_exc() |
|
221 | 221 | raise IOError, "The file %s can't be opened" %(filename) |
|
222 | 222 | #chino rata |
|
223 | 223 | #In case has utctime attribute |
|
224 | 224 | grp2 = grp1['utctime'] |
|
225 | 225 | # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time |
|
226 | 226 | thisUtcTime = grp2.value[0] |
|
227 | 227 | |
|
228 | 228 | fp.close() |
|
229 | 229 | |
|
230 | 230 | if self.timezone == 'lt': |
|
231 | 231 | thisUtcTime -= 5*3600 |
|
232 | 232 | |
|
233 | 233 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) |
|
234 | 234 | # thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0]) |
|
235 | 235 | thisDate = thisDatetime.date() |
|
236 | 236 | thisTime = thisDatetime.time() |
|
237 | 237 | |
|
238 | 238 | startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
239 | 239 | endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
240 | 240 | |
|
241 | 241 | #General case |
|
242 | 242 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o |
|
243 | 243 | #-----------o----------------------------o----------- |
|
244 | 244 | # startTime endTime |
|
245 | 245 | |
|
246 | 246 | if endTime >= startTime: |
|
247 | 247 | thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime) |
|
248 | 248 | if numpy.any(thisUtcLog): #If there is one block between the hours mentioned |
|
249 | 249 | return thisDatetime |
|
250 | 250 | return None |
|
251 | 251 | |
|
252 | 252 | #If endTime < startTime then endTime belongs to the next day |
|
253 | 253 | #<<<<<<<<<<<o o>>>>>>>>>>> |
|
254 | 254 | #-----------o----------------------------o----------- |
|
255 | 255 | # endTime startTime |
|
256 | 256 | |
|
257 | 257 | if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime): |
|
258 | 258 | return None |
|
259 | 259 | |
|
260 | 260 | if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime): |
|
261 | 261 | return None |
|
262 | 262 | |
|
263 | 263 | if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime): |
|
264 | 264 | return None |
|
265 | 265 | |
|
266 | 266 | return thisDatetime |
|
267 | 267 | |
|
268 | 268 | def __setNextFileOffline(self): |
|
269 | 269 | |
|
270 | 270 | self.fileIndex += 1 |
|
271 | 271 | idFile = self.fileIndex |
|
272 | 272 | |
|
273 | 273 | if not(idFile < len(self.filenameList)): |
|
274 | 274 | print "No more Files" |
|
275 | 275 | return 0 |
|
276 | 276 | |
|
277 | 277 | filename = self.filenameList[idFile] |
|
278 | 278 | |
|
279 | 279 | filePointer = h5py.File(filename,'r') |
|
280 | 280 | |
|
281 | 281 | self.filename = filename |
|
282 | 282 | |
|
283 | 283 | self.fp = filePointer |
|
284 | 284 | |
|
285 | 285 | print "Setting the file: %s"%self.filename |
|
286 | 286 | |
|
287 | 287 | # self.__readMetadata() |
|
288 | 288 | self.__setBlockList() |
|
289 | 289 | self.__readData() |
|
290 | 290 | # self.nRecords = self.fp['Data'].attrs['blocksPerFile'] |
|
291 | 291 | # self.nRecords = self.fp['Data'].attrs['nRecords'] |
|
292 | 292 | self.blockIndex = 0 |
|
293 | 293 | return 1 |
|
294 | 294 | |
|
295 | 295 | def __setBlockList(self): |
|
296 | 296 | ''' |
|
297 | 297 | Selects the data within the times defined |
|
298 | 298 | |
|
299 | 299 | self.fp |
|
300 | 300 | self.startTime |
|
301 | 301 | self.endTime |
|
302 | 302 | |
|
303 | 303 | self.blockList |
|
304 | 304 | self.blocksPerFile |
|
305 | 305 | |
|
306 | 306 | ''' |
|
307 | 307 | fp = self.fp |
|
308 | 308 | startTime = self.startTime |
|
309 | 309 | endTime = self.endTime |
|
310 | 310 | |
|
311 | 311 | grp = fp['Data'] |
|
312 | 312 | thisUtcTime = grp['utctime'].value.astype(numpy.float)[0] |
|
313 | 313 | |
|
314 | 314 | #ERROOOOR |
|
315 | 315 | if self.timezone == 'lt': |
|
316 | 316 | thisUtcTime -= 5*3600 |
|
317 | 317 | |
|
318 | 318 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) |
|
319 | 319 | |
|
320 | 320 | thisDate = thisDatetime.date() |
|
321 | 321 | thisTime = thisDatetime.time() |
|
322 | 322 | |
|
323 | 323 | startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
324 | 324 | endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
325 | 325 | |
|
326 | 326 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] |
|
327 | 327 | |
|
328 | 328 | self.blockList = ind |
|
329 | 329 | self.blocksPerFile = len(ind) |
|
330 | 330 | |
|
331 | 331 | return |
|
332 | 332 | |
|
333 | 333 | def __readMetadata(self): |
|
334 | 334 | ''' |
|
335 | 335 | Reads Metadata |
|
336 | 336 | |
|
337 | 337 | self.pathMeta |
|
338 | 338 | |
|
339 | 339 | self.listShapes |
|
340 | 340 | self.listMetaname |
|
341 | 341 | self.listMeta |
|
342 | 342 | |
|
343 | 343 | ''' |
|
344 | 344 | |
|
345 | 345 | # grp = self.fp['Data'] |
|
346 | 346 | # pathMeta = os.path.join(self.path, grp.attrs['metadata']) |
|
347 | 347 | # |
|
348 | 348 | # if pathMeta == self.pathMeta: |
|
349 | 349 | # return |
|
350 | 350 | # else: |
|
351 | 351 | # self.pathMeta = pathMeta |
|
352 | 352 | # |
|
353 | 353 | # filePointer = h5py.File(self.pathMeta,'r') |
|
354 | 354 | # groupPointer = filePointer['Metadata'] |
|
355 | 355 | |
|
356 | 356 | filename = self.filenameList[0] |
|
357 | 357 | |
|
358 | 358 | fp = h5py.File(filename,'r') |
|
359 | 359 | |
|
360 | 360 | gp = fp['Metadata'] |
|
361 | 361 | |
|
362 | 362 | listMetaname = [] |
|
363 | 363 | listMetadata = [] |
|
364 | 364 | for item in gp.items(): |
|
365 | 365 | name = item[0] |
|
366 | 366 | |
|
367 | 367 | if name=='array dimensions': |
|
368 | 368 | table = gp[name][:] |
|
369 | 369 | listShapes = {} |
|
370 | 370 | for shapes in table: |
|
371 | 371 | listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]]) |
|
372 | 372 | else: |
|
373 | 373 | data = gp[name].value |
|
374 | 374 | listMetaname.append(name) |
|
375 | 375 | listMetadata.append(data) |
|
376 | 376 | |
|
377 | 377 | # if name=='type': |
|
378 | 378 | # self.__initDataOut(data) |
|
379 | 379 | |
|
380 | 380 | self.listShapes = listShapes |
|
381 | 381 | self.listMetaname = listMetaname |
|
382 | 382 | self.listMeta = listMetadata |
|
383 | 383 | |
|
384 | 384 | fp.close() |
|
385 | 385 | return |
|
386 | 386 | |
|
387 | 387 | def __readData(self): |
|
388 | 388 | grp = self.fp['Data'] |
|
389 | 389 | listdataname = [] |
|
390 | 390 | listdata = [] |
|
391 | 391 | |
|
392 | 392 | for item in grp.items(): |
|
393 | 393 | name = item[0] |
|
394 | 394 | listdataname.append(name) |
|
395 | 395 | |
|
396 | 396 | array = self.__setDataArray(grp[name],self.listShapes[name]) |
|
397 | 397 | listdata.append(array) |
|
398 | 398 | |
|
399 | 399 | self.listDataname = listdataname |
|
400 | 400 | self.listData = listdata |
|
401 | 401 | return |
|
402 | 402 | |
|
403 | 403 | def __setDataArray(self, dataset, shapes): |
|
404 | 404 | |
|
405 | 405 | nDims = shapes[0] |
|
406 | 406 | |
|
407 | 407 | nDim2 = shapes[1] #Dimension 0 |
|
408 | 408 | |
|
409 | 409 | nDim1 = shapes[2] #Dimension 1, number of Points or Parameters |
|
410 | 410 | |
|
411 | 411 | nDim0 = shapes[3] #Dimension 2, number of samples or ranges |
|
412 | 412 | |
|
413 | 413 | mode = shapes[4] #Mode of storing |
|
414 | 414 | |
|
415 | 415 | blockList = self.blockList |
|
416 | 416 | |
|
417 | 417 | blocksPerFile = self.blocksPerFile |
|
418 | 418 | |
|
419 | 419 | #Depending on what mode the data was stored |
|
420 | 420 | if mode == 0: #Divided in channels |
|
421 | 421 | arrayData = dataset.value.astype(numpy.float)[0][blockList] |
|
422 | 422 | if mode == 1: #Divided in parameter |
|
423 | 423 | strds = 'table' |
|
424 | 424 | nDatas = nDim1 |
|
425 | 425 | newShapes = (blocksPerFile,nDim2,nDim0) |
|
426 | 426 | elif mode==2: #Concatenated in a table |
|
427 | 427 | strds = 'table0' |
|
428 | 428 | arrayData = dataset[strds].value |
|
429 | 429 | #Selecting part of the dataset |
|
430 | 430 | utctime = arrayData[:,0] |
|
431 | 431 | u, indices = numpy.unique(utctime, return_index=True) |
|
432 | 432 | |
|
433 | 433 | if blockList.size != indices.size: |
|
434 | 434 | indMin = indices[blockList[0]] |
|
435 | 435 | if blockList[1] + 1 >= indices.size: |
|
436 | 436 | arrayData = arrayData[indMin:,:] |
|
437 | 437 | else: |
|
438 | 438 | indMax = indices[blockList[1] + 1] |
|
439 | 439 | arrayData = arrayData[indMin:indMax,:] |
|
440 | 440 | return arrayData |
|
441 | 441 | |
|
442 | 442 | # One dimension |
|
443 | 443 | if nDims == 0: |
|
444 | 444 | arrayData = dataset.value.astype(numpy.float)[0][blockList] |
|
445 | 445 | |
|
446 | 446 | # Two dimensions |
|
447 | 447 | elif nDims == 2: |
|
448 | 448 | arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0)) |
|
449 | 449 | newShapes = (blocksPerFile,nDim0) |
|
450 | 450 | nDatas = nDim1 |
|
451 | 451 | |
|
452 | 452 | for i in range(nDatas): |
|
453 | 453 | data = dataset[strds + str(i)].value |
|
454 | 454 | arrayData[:,i,:] = data[blockList,:] |
|
455 | 455 | |
|
456 | 456 | # Three dimensions |
|
457 | 457 | else: |
|
458 | 458 | arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0)) |
|
459 | 459 | for i in range(nDatas): |
|
460 | 460 | |
|
461 | 461 | data = dataset[strds + str(i)].value |
|
462 | 462 | |
|
463 | 463 | for b in range(blockList.size): |
|
464 | 464 | arrayData[b,:,i,:] = data[:,:,blockList[b]] |
|
465 | 465 | |
|
466 | 466 | return arrayData |
|
467 | 467 | |
|
468 | 468 | def __setDataOut(self): |
|
469 | 469 | listMeta = self.listMeta |
|
470 | 470 | listMetaname = self.listMetaname |
|
471 | 471 | listDataname = self.listDataname |
|
472 | 472 | listData = self.listData |
|
473 | 473 | listShapes = self.listShapes |
|
474 | 474 | |
|
475 | 475 | blockIndex = self.blockIndex |
|
476 | 476 | # blockList = self.blockList |
|
477 | 477 | |
|
478 | 478 | for i in range(len(listMeta)): |
|
479 | 479 | setattr(self.dataOut,listMetaname[i],listMeta[i]) |
|
480 | 480 | |
|
481 | 481 | for j in range(len(listData)): |
|
482 | 482 | nShapes = listShapes[listDataname[j]][0] |
|
483 | 483 | mode = listShapes[listDataname[j]][4] |
|
484 | 484 | if nShapes == 1: |
|
485 | 485 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) |
|
486 | 486 | elif nShapes > 1: |
|
487 | 487 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:]) |
|
488 | 488 | elif mode==0: |
|
489 | 489 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) |
|
490 | 490 | #Mode Meteors |
|
491 | 491 | elif mode ==2: |
|
492 | 492 | selectedData = self.__selectDataMode2(listData[j], blockIndex) |
|
493 | 493 | setattr(self.dataOut, listDataname[j], selectedData) |
|
494 | 494 | return |
|
495 | 495 | |
|
496 | 496 | def __selectDataMode2(self, data, blockIndex): |
|
497 | 497 | utctime = data[:,0] |
|
498 | 498 | aux, indices = numpy.unique(utctime, return_inverse=True) |
|
499 | 499 | selInd = numpy.where(indices == blockIndex)[0] |
|
500 | 500 | selData = data[selInd,:] |
|
501 | 501 | |
|
502 | 502 | return selData |
|
503 | 503 | |
|
504 | 504 | def getData(self): |
|
505 | 505 | |
|
506 | 506 | # if self.flagNoMoreFiles: |
|
507 | 507 | # self.dataOut.flagNoData = True |
|
508 | 508 | # print 'Process finished' |
|
509 | 509 | # return 0 |
|
510 | 510 | # |
|
511 | 511 | if self.blockIndex==self.blocksPerFile: |
|
512 | 512 | if not( self.__setNextFileOffline() ): |
|
513 | 513 | self.dataOut.flagNoData = True |
|
514 | 514 | return 0 |
|
515 | 515 | |
|
516 | 516 | # if self.datablock == None: # setear esta condicion cuando no hayan datos por leers |
|
517 | 517 | # self.dataOut.flagNoData = True |
|
518 | 518 | # return 0 |
|
519 | 519 | # self.__readData() |
|
520 | 520 | self.__setDataOut() |
|
521 | 521 | self.dataOut.flagNoData = False |
|
522 | 522 | |
|
523 | 523 | self.blockIndex += 1 |
|
524 | 524 | |
|
525 | 525 | return |
|
526 | 526 | |
|
527 | 527 | def run(self, **kwargs): |
|
528 | 528 | |
|
529 | 529 | if not(self.isConfig): |
|
530 | 530 | self.setup(**kwargs) |
|
531 | 531 | # self.setObjProperties() |
|
532 | 532 | self.isConfig = True |
|
533 | 533 | |
|
534 | 534 | self.getData() |
|
535 | 535 | |
|
536 | 536 | return |
|
537 | 537 | |
|
538 | 538 | class ParamWriter(Operation): |
|
539 | 539 | ''' |
|
540 | 540 | HDF5 Writer, stores parameters data in HDF5 format files |
|
541 | 541 | |
|
542 | 542 | path: path where the files will be stored |
|
543 | 543 | |
|
544 | 544 | blocksPerFile: number of blocks that will be saved in per HDF5 format file |
|
545 | 545 | |
|
546 | 546 | mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors) |
|
547 | 547 | |
|
548 | 548 | metadataList: list of attributes that will be stored as metadata |
|
549 | 549 | |
|
550 | 550 | dataList: list of attributes that will be stores as data |
|
551 | 551 | |
|
552 | 552 | ''' |
|
553 | 553 | |
|
554 | 554 | |
|
555 | 555 | ext = ".hdf5" |
|
556 | 556 | |
|
557 | 557 | optchar = "D" |
|
558 | 558 | |
|
559 | 559 | metaoptchar = "M" |
|
560 | 560 | |
|
561 | 561 | metaFile = None |
|
562 | 562 | |
|
563 | 563 | filename = None |
|
564 | 564 | |
|
565 | 565 | path = None |
|
566 | 566 | |
|
567 | 567 | setFile = None |
|
568 | 568 | |
|
569 | 569 | fp = None |
|
570 | 570 | |
|
571 | 571 | grp = None |
|
572 | 572 | |
|
573 | 573 | ds = None |
|
574 | 574 | |
|
575 | 575 | firsttime = True |
|
576 | 576 | |
|
577 | 577 | #Configurations |
|
578 | 578 | |
|
579 | 579 | blocksPerFile = None |
|
580 | 580 | |
|
581 | 581 | blockIndex = None |
|
582 | 582 | |
|
583 | 583 | dataOut = None |
|
584 | 584 | |
|
585 | 585 | #Data Arrays |
|
586 | 586 | |
|
587 | 587 | dataList = None |
|
588 | 588 | |
|
589 | 589 | metadataList = None |
|
590 | 590 | |
|
591 | 591 | # arrayDim = None |
|
592 | 592 | |
|
593 | 593 | dsList = None #List of dictionaries with dataset properties |
|
594 | 594 | |
|
595 | 595 | tableDim = None |
|
596 | 596 | |
|
597 | 597 | # dtype = [('arrayName', 'S20'),('nChannels', 'i'), ('nPoints', 'i'), ('nSamples', 'i'),('mode', 'b')] |
|
598 | 598 | |
|
599 | 599 | dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')] |
|
600 | 600 | |
|
601 | 601 | currentDay = None |
|
602 | 602 | |
|
603 | 603 | lastTime = None |
|
604 | 604 | |
|
605 | 605 | def __init__(self, **kwargs): |
|
606 | 606 | Operation.__init__(self, **kwargs) |
|
607 | 607 | self.isConfig = False |
|
608 | 608 | return |
|
609 | 609 | |
|
610 | def setup(self, dataOut, **kwargs): | |
|
611 | ||
|
612 | self.path = kwargs['path'] | |
|
613 | ||
|
614 | if kwargs.has_key('blocksPerFile'): | |
|
615 | self.blocksPerFile = kwargs['blocksPerFile'] | |
|
616 | else: | |
|
617 | self.blocksPerFile = 10 | |
|
618 | ||
|
619 | self.metadataList = kwargs['metadataList'] | |
|
620 | self.dataList = kwargs['dataList'] | |
|
610 | def setup(self, dataOut, path=None, blocksPerFile=10, metadataList=None, dataList=None, mode=None, **kwargs): | |
|
611 | self.path = path | |
|
612 | self.blocksPerFile = blocksPerFile | |
|
613 | self.metadataList = metadataList | |
|
614 | self.dataList = dataList | |
|
621 | 615 | self.dataOut = dataOut |
|
622 | ||
|
623 | if kwargs.has_key('mode'): | |
|
624 | mode = kwargs['mode'] | |
|
625 | ||
|
626 | if type(mode) == int: | |
|
627 | mode = numpy.zeros(len(self.dataList)) + mode | |
|
628 | else: | |
|
629 | mode = numpy.ones(len(self.dataList)) | |
|
630 | ||
|
631 | 616 | self.mode = mode |
|
617 | ||
|
618 | if self.mode is not None: | |
|
619 | self.mode = numpy.zeros(len(self.dataList)) + mode | |
|
620 | else: | |
|
621 | self.mode = numpy.ones(len(self.dataList)) | |
|
632 | 622 | |
|
633 | 623 | arrayDim = numpy.zeros((len(self.dataList),5)) |
|
634 | 624 | |
|
635 | 625 | #Table dimensions |
|
636 | 626 | dtype0 = self.dtype |
|
637 | 627 | tableList = [] |
|
638 | 628 | |
|
639 | 629 | #Dictionary and list of tables |
|
640 | 630 | dsList = [] |
|
641 | 631 | |
|
642 | 632 | for i in range(len(self.dataList)): |
|
643 | 633 | dsDict = {} |
|
644 | 634 | dataAux = getattr(self.dataOut, self.dataList[i]) |
|
645 | 635 | dsDict['variable'] = self.dataList[i] |
|
646 | 636 | #--------------------- Conditionals ------------------------ |
|
647 | 637 | #There is no data |
|
648 | 638 | if dataAux is None: |
|
649 | 639 | return 0 |
|
650 | 640 | |
|
651 | 641 | #Not array, just a number |
|
652 | 642 | #Mode 0 |
|
653 | 643 | if type(dataAux)==float or type(dataAux)==int: |
|
654 | 644 | dsDict['mode'] = 0 |
|
655 | 645 | dsDict['nDim'] = 0 |
|
656 | 646 | arrayDim[i,0] = 0 |
|
657 | 647 | dsList.append(dsDict) |
|
658 | 648 | |
|
659 | 649 | #Mode 2: meteors |
|
660 | 650 | elif mode[i] == 2: |
|
661 | 651 | # dsDict['nDim'] = 0 |
|
662 | 652 | dsDict['dsName'] = 'table0' |
|
663 | 653 | dsDict['mode'] = 2 # Mode meteors |
|
664 | 654 | dsDict['shape'] = dataAux.shape[-1] |
|
665 | 655 | dsDict['nDim'] = 0 |
|
666 | 656 | dsDict['dsNumber'] = 1 |
|
667 | 657 | |
|
668 | 658 | arrayDim[i,3] = dataAux.shape[-1] |
|
669 | 659 | arrayDim[i,4] = mode[i] #Mode the data was stored |
|
670 | 660 | |
|
671 | 661 | dsList.append(dsDict) |
|
672 | 662 | |
|
673 | 663 | #Mode 1 |
|
674 | 664 | else: |
|
675 | 665 | arrayDim0 = dataAux.shape #Data dimensions |
|
676 | 666 | arrayDim[i,0] = len(arrayDim0) #Number of array dimensions |
|
677 | 667 | arrayDim[i,4] = mode[i] #Mode the data was stored |
|
678 | 668 | |
|
679 | 669 | strtable = 'table' |
|
680 | 670 | dsDict['mode'] = 1 # Mode parameters |
|
681 | 671 | |
|
682 | 672 | # Three-dimension arrays |
|
683 | 673 | if len(arrayDim0) == 3: |
|
684 | 674 | arrayDim[i,1:-1] = numpy.array(arrayDim0) |
|
685 | 675 | nTables = int(arrayDim[i,2]) |
|
686 | 676 | dsDict['dsNumber'] = nTables |
|
687 | 677 | dsDict['shape'] = arrayDim[i,2:4] |
|
688 | 678 | dsDict['nDim'] = 3 |
|
689 | 679 | |
|
690 | 680 | for j in range(nTables): |
|
691 | 681 | dsDict = dsDict.copy() |
|
692 | 682 | dsDict['dsName'] = strtable + str(j) |
|
693 | 683 | dsList.append(dsDict) |
|
694 | 684 | |
|
695 | 685 | # Two-dimension arrays |
|
696 | 686 | elif len(arrayDim0) == 2: |
|
697 | 687 | arrayDim[i,2:-1] = numpy.array(arrayDim0) |
|
698 | 688 | nTables = int(arrayDim[i,2]) |
|
699 | 689 | dsDict['dsNumber'] = nTables |
|
700 | 690 | dsDict['shape'] = arrayDim[i,3] |
|
701 | 691 | dsDict['nDim'] = 2 |
|
702 | 692 | |
|
703 | 693 | for j in range(nTables): |
|
704 | 694 | dsDict = dsDict.copy() |
|
705 | 695 | dsDict['dsName'] = strtable + str(j) |
|
706 | 696 | dsList.append(dsDict) |
|
707 | 697 | |
|
708 | 698 | # One-dimension arrays |
|
709 | 699 | elif len(arrayDim0) == 1: |
|
710 | 700 | arrayDim[i,3] = arrayDim0[0] |
|
711 | 701 | dsDict['shape'] = arrayDim0[0] |
|
712 | 702 | dsDict['dsNumber'] = 1 |
|
713 | 703 | dsDict['dsName'] = strtable + str(0) |
|
714 | 704 | dsDict['nDim'] = 1 |
|
715 | 705 | dsList.append(dsDict) |
|
716 | 706 | |
|
717 | 707 | table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0) |
|
718 | 708 | tableList.append(table) |
|
719 | 709 | |
|
720 | 710 | # self.arrayDim = arrayDim |
|
721 | 711 | self.dsList = dsList |
|
722 | 712 | self.tableDim = numpy.array(tableList, dtype = dtype0) |
|
723 | 713 | self.blockIndex = 0 |
|
724 | 714 | |
|
725 | 715 | timeTuple = time.localtime(dataOut.utctime) |
|
726 | 716 | self.currentDay = timeTuple.tm_yday |
|
727 | 717 | return 1 |
|
728 | 718 | |
|
729 | 719 | def putMetadata(self): |
|
730 | 720 | |
|
731 | 721 | fp = self.createMetadataFile() |
|
732 | 722 | self.writeMetadata(fp) |
|
733 | 723 | fp.close() |
|
734 | 724 | return |
|
735 | 725 | |
|
736 | 726 | def createMetadataFile(self): |
|
737 | 727 | ext = self.ext |
|
738 | 728 | path = self.path |
|
739 | 729 | setFile = self.setFile |
|
740 | 730 | |
|
741 | 731 | timeTuple = time.localtime(self.dataOut.utctime) |
|
742 | 732 | |
|
743 | 733 | subfolder = '' |
|
744 | 734 | fullpath = os.path.join( path, subfolder ) |
|
745 | 735 | |
|
746 | 736 | if not( os.path.exists(fullpath) ): |
|
747 | 737 | os.mkdir(fullpath) |
|
748 | 738 | setFile = -1 #inicializo mi contador de seteo |
|
749 | 739 | |
|
750 | 740 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
751 | 741 | fullpath = os.path.join( path, subfolder ) |
|
752 | 742 | |
|
753 | 743 | if not( os.path.exists(fullpath) ): |
|
754 | 744 | os.mkdir(fullpath) |
|
755 | 745 | setFile = -1 #inicializo mi contador de seteo |
|
756 | 746 | |
|
757 | 747 | else: |
|
758 | 748 | filesList = os.listdir( fullpath ) |
|
759 | 749 | filesList = sorted( filesList, key=str.lower ) |
|
760 | 750 | if len( filesList ) > 0: |
|
761 | 751 | filesList = [k for k in filesList if 'M' in k] |
|
762 | 752 | filen = filesList[-1] |
|
763 | 753 | # el filename debera tener el siguiente formato |
|
764 | 754 | # 0 1234 567 89A BCDE (hex) |
|
765 | 755 | # x YYYY DDD SSS .ext |
|
766 | 756 | if isNumber( filen[8:11] ): |
|
767 | 757 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
768 | 758 | else: |
|
769 | 759 | setFile = -1 |
|
770 | 760 | else: |
|
771 | 761 | setFile = -1 #inicializo mi contador de seteo |
|
772 | 762 | |
|
773 | setFile += 1 | |
|
774 | ||
|
775 |
file = '%s%4.4d%3.3d% |
|
|
776 | timeTuple.tm_year, | |
|
777 | timeTuple.tm_yday, | |
|
778 | setFile, | |
|
779 | ext ) | |
|
763 | if self.setType is None: | |
|
764 | setFile += 1 | |
|
765 | file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar, | |
|
766 | timeTuple.tm_year, | |
|
767 | timeTuple.tm_yday, | |
|
768 | setFile, | |
|
769 | ext ) | |
|
770 | else: | |
|
771 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
|
772 | file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar, | |
|
773 | timeTuple.tm_year, | |
|
774 | timeTuple.tm_yday, | |
|
775 | setFile, | |
|
776 | ext ) | |
|
780 | 777 | |
|
781 | 778 | filename = os.path.join( path, subfolder, file ) |
|
782 | 779 | self.metaFile = file |
|
783 | 780 | #Setting HDF5 File |
|
784 | 781 | fp = h5py.File(filename,'w') |
|
785 | 782 | |
|
786 | 783 | return fp |
|
787 | 784 | |
|
788 | 785 | def writeMetadata(self, fp): |
|
789 | 786 | |
|
790 | 787 | grp = fp.create_group("Metadata") |
|
791 | 788 | grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype) |
|
792 | 789 | |
|
793 | 790 | for i in range(len(self.metadataList)): |
|
794 | 791 | grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i])) |
|
795 | 792 | return |
|
796 | 793 | |
|
797 | 794 | def timeFlag(self): |
|
798 | 795 | currentTime = self.dataOut.utctime |
|
799 | 796 | |
|
800 | 797 | if self.lastTime is None: |
|
801 | 798 | self.lastTime = currentTime |
|
802 | 799 | |
|
803 | 800 | #Day |
|
804 | 801 | timeTuple = time.localtime(currentTime) |
|
805 | 802 | dataDay = timeTuple.tm_yday |
|
806 | 803 | |
|
807 | 804 | #Time |
|
808 | 805 | timeDiff = currentTime - self.lastTime |
|
809 | 806 | |
|
810 | 807 | #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora |
|
811 | 808 | if dataDay != self.currentDay: |
|
812 | 809 | self.currentDay = dataDay |
|
813 | 810 | return True |
|
814 | 811 | elif timeDiff > 3*60*60: |
|
815 | 812 | self.lastTime = currentTime |
|
816 | 813 | return True |
|
817 | 814 | else: |
|
818 | 815 | self.lastTime = currentTime |
|
819 | 816 | return False |
|
820 | 817 | |
|
821 | 818 | def setNextFile(self): |
|
822 | 819 | |
|
823 | 820 | ext = self.ext |
|
824 | 821 | path = self.path |
|
825 | 822 | setFile = self.setFile |
|
826 | 823 | mode = self.mode |
|
827 | 824 | |
|
828 | 825 | timeTuple = time.localtime(self.dataOut.utctime) |
|
829 | 826 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
830 | 827 | |
|
831 | 828 | fullpath = os.path.join( path, subfolder ) |
|
832 | 829 | |
|
833 | 830 | if os.path.exists(fullpath): |
|
834 | 831 | filesList = os.listdir( fullpath ) |
|
835 | 832 | filesList = [k for k in filesList if 'D' in k] |
|
836 | 833 | if len( filesList ) > 0: |
|
837 | 834 | filesList = sorted( filesList, key=str.lower ) |
|
838 | 835 | filen = filesList[-1] |
|
839 | 836 | # el filename debera tener el siguiente formato |
|
840 | 837 | # 0 1234 567 89A BCDE (hex) |
|
841 | 838 | # x YYYY DDD SSS .ext |
|
842 | 839 | if isNumber( filen[8:11] ): |
|
843 | 840 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
844 | 841 | else: |
|
845 | 842 | setFile = -1 |
|
846 | 843 | else: |
|
847 | 844 | setFile = -1 #inicializo mi contador de seteo |
|
848 | 845 | else: |
|
849 | 846 | os.makedirs(fullpath) |
|
850 | 847 | setFile = -1 #inicializo mi contador de seteo |
|
851 | 848 | |
|
852 | setFile += 1 | |
|
853 | ||
|
854 |
file = '%s%4.4d%3.3d% |
|
|
855 | timeTuple.tm_year, | |
|
856 | timeTuple.tm_yday, | |
|
857 | setFile, | |
|
858 | ext ) | |
|
849 | if self.setType is None: | |
|
850 | setFile += 1 | |
|
851 | file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar, | |
|
852 | timeTuple.tm_year, | |
|
853 | timeTuple.tm_yday, | |
|
854 | setFile, | |
|
855 | ext ) | |
|
856 | else: | |
|
857 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
|
858 | file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar, | |
|
859 | timeTuple.tm_year, | |
|
860 | timeTuple.tm_yday, | |
|
861 | setFile, | |
|
862 | ext ) | |
|
859 | 863 | |
|
860 | 864 | filename = os.path.join( path, subfolder, file ) |
|
861 | 865 | |
|
862 | 866 | #Setting HDF5 File |
|
863 | 867 | fp = h5py.File(filename,'w') |
|
864 | 868 | #write metadata |
|
865 | 869 | self.writeMetadata(fp) |
|
866 | 870 | #Write data |
|
867 | 871 | grp = fp.create_group("Data") |
|
868 | 872 | # grp.attrs['metadata'] = self.metaFile |
|
869 | 873 | |
|
870 | 874 | # grp.attrs['blocksPerFile'] = 0 |
|
871 | 875 | ds = [] |
|
872 | 876 | data = [] |
|
873 | 877 | dsList = self.dsList |
|
874 | 878 | i = 0 |
|
875 | 879 | while i < len(dsList): |
|
876 | 880 | dsInfo = dsList[i] |
|
877 | 881 | #One-dimension data |
|
878 | 882 | if dsInfo['mode'] == 0: |
|
879 | 883 | # ds0 = grp.create_dataset(self.dataList[i], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype='S20') |
|
880 | 884 | ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64) |
|
881 | 885 | ds.append(ds0) |
|
882 | 886 | data.append([]) |
|
883 | 887 | i += 1 |
|
884 | 888 | continue |
|
885 | 889 | # nDimsForDs.append(nDims[i]) |
|
886 | 890 | |
|
887 | 891 | elif dsInfo['mode'] == 2: |
|
888 | 892 | grp0 = grp.create_group(dsInfo['variable']) |
|
889 | 893 | ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True) |
|
890 | 894 | ds.append(ds0) |
|
891 | 895 | data.append([]) |
|
892 | 896 | i += 1 |
|
893 | 897 | continue |
|
894 | 898 | |
|
895 | 899 | elif dsInfo['mode'] == 1: |
|
896 | 900 | grp0 = grp.create_group(dsInfo['variable']) |
|
897 | 901 | |
|
898 | 902 | for j in range(dsInfo['dsNumber']): |
|
899 | 903 | dsInfo = dsList[i] |
|
900 | 904 | tableName = dsInfo['dsName'] |
|
901 | 905 | shape = int(dsInfo['shape']) |
|
902 | 906 | |
|
903 | 907 | if dsInfo['nDim'] == 3: |
|
904 | 908 | ds0 = grp0.create_dataset(tableName, (shape[0],shape[1],1) , data = numpy.zeros((shape[0],shape[1],1)), maxshape = (None,shape[1],None), chunks=True) |
|
905 | 909 | else: |
|
906 | 910 | ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True) |
|
907 | 911 | |
|
908 | 912 | ds.append(ds0) |
|
909 | 913 | data.append([]) |
|
910 | 914 | i += 1 |
|
911 | 915 | # nDimsForDs.append(nDims[i]) |
|
912 | 916 | |
|
913 | 917 | fp.flush() |
|
914 | 918 | fp.close() |
|
915 | 919 | |
|
916 | 920 | # self.nDatas = nDatas |
|
917 | 921 | # self.nDims = nDims |
|
918 | 922 | # self.nDimsForDs = nDimsForDs |
|
919 | 923 | #Saving variables |
|
920 | 924 | print 'Writing the file: %s'%filename |
|
921 | 925 | self.filename = filename |
|
922 | 926 | # self.fp = fp |
|
923 | 927 | # self.grp = grp |
|
924 | 928 | # self.grp.attrs.modify('nRecords', 1) |
|
925 | 929 | self.ds = ds |
|
926 | 930 | self.data = data |
|
927 | 931 | # self.setFile = setFile |
|
928 | 932 | self.firsttime = True |
|
929 | 933 | self.blockIndex = 0 |
|
930 | 934 | return |
|
931 | 935 | |
|
932 | 936 | def putData(self): |
|
933 | 937 | |
|
934 | 938 | if self.blockIndex == self.blocksPerFile or self.timeFlag(): |
|
935 | 939 | self.setNextFile() |
|
936 | 940 | |
|
937 | 941 | # if not self.firsttime: |
|
938 | 942 | self.readBlock() |
|
939 | 943 | self.setBlock() #Prepare data to be written |
|
940 | 944 | self.writeBlock() #Write data |
|
941 | 945 | |
|
942 | 946 | return |
|
943 | 947 | |
|
944 | 948 | def readBlock(self): |
|
945 | 949 | |
|
946 | 950 | ''' |
|
947 | 951 | data Array configured |
|
948 | 952 | |
|
949 | 953 | |
|
950 | 954 | self.data |
|
951 | 955 | ''' |
|
952 | 956 | dsList = self.dsList |
|
953 | 957 | ds = self.ds |
|
954 | 958 | #Setting HDF5 File |
|
955 | 959 | fp = h5py.File(self.filename,'r+') |
|
956 | 960 | grp = fp["Data"] |
|
957 | 961 | ind = 0 |
|
958 | 962 | |
|
959 | 963 | # grp.attrs['blocksPerFile'] = 0 |
|
960 | 964 | while ind < len(dsList): |
|
961 | 965 | dsInfo = dsList[ind] |
|
962 | 966 | |
|
963 | 967 | if dsInfo['mode'] == 0: |
|
964 | 968 | ds0 = grp[dsInfo['variable']] |
|
965 | 969 | ds[ind] = ds0 |
|
966 | 970 | ind += 1 |
|
967 | 971 | else: |
|
968 | 972 | |
|
969 | 973 | grp0 = grp[dsInfo['variable']] |
|
970 | 974 | |
|
971 | 975 | for j in range(dsInfo['dsNumber']): |
|
972 | 976 | dsInfo = dsList[ind] |
|
973 | 977 | ds0 = grp0[dsInfo['dsName']] |
|
974 | 978 | ds[ind] = ds0 |
|
975 | 979 | ind += 1 |
|
976 | 980 | |
|
977 | 981 | self.fp = fp |
|
978 | 982 | self.grp = grp |
|
979 | 983 | self.ds = ds |
|
980 | 984 | |
|
981 | 985 | return |
|
982 | 986 | |
|
983 | 987 | def setBlock(self): |
|
984 | 988 | ''' |
|
985 | 989 | data Array configured |
|
986 | 990 | |
|
987 | 991 | |
|
988 | 992 | self.data |
|
989 | 993 | ''' |
|
990 | 994 | #Creating Arrays |
|
991 | 995 | dsList = self.dsList |
|
992 | 996 | data = self.data |
|
993 | 997 | ind = 0 |
|
994 | 998 | |
|
995 | 999 | while ind < len(dsList): |
|
996 | 1000 | dsInfo = dsList[ind] |
|
997 | 1001 | dataAux = getattr(self.dataOut, dsInfo['variable']) |
|
998 | 1002 | |
|
999 | 1003 | mode = dsInfo['mode'] |
|
1000 | 1004 | nDim = dsInfo['nDim'] |
|
1001 | 1005 | |
|
1002 | 1006 | if mode == 0 or mode == 2 or nDim == 1: |
|
1003 | 1007 | data[ind] = dataAux |
|
1004 | 1008 | ind += 1 |
|
1005 | 1009 | # elif nDim == 1: |
|
1006 | 1010 | # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1)) |
|
1007 | 1011 | # ind += 1 |
|
1008 | 1012 | elif nDim == 2: |
|
1009 | 1013 | for j in range(dsInfo['dsNumber']): |
|
1010 | 1014 | data[ind] = dataAux[j,:] |
|
1011 | 1015 | ind += 1 |
|
1012 | 1016 | elif nDim == 3: |
|
1013 | 1017 | for j in range(dsInfo['dsNumber']): |
|
1014 | 1018 | data[ind] = dataAux[:,j,:] |
|
1015 | 1019 | ind += 1 |
|
1016 | 1020 | |
|
1017 | 1021 | self.data = data |
|
1018 | 1022 | return |
|
1019 | 1023 | |
|
1020 | 1024 | def writeBlock(self): |
|
1021 | 1025 | ''' |
|
1022 | 1026 | Saves the block in the HDF5 file |
|
1023 | 1027 | ''' |
|
1024 | 1028 | dsList = self.dsList |
|
1025 | 1029 | |
|
1026 | 1030 | for i in range(len(self.ds)): |
|
1027 | 1031 | dsInfo = dsList[i] |
|
1028 | 1032 | nDim = dsInfo['nDim'] |
|
1029 | 1033 | mode = dsInfo['mode'] |
|
1030 | 1034 | |
|
1031 | 1035 | # First time |
|
1032 | 1036 | if self.firsttime: |
|
1033 | 1037 | # self.ds[i].resize(self.data[i].shape) |
|
1034 | 1038 | # self.ds[i][self.blockIndex,:] = self.data[i] |
|
1035 | 1039 | if type(self.data[i]) == numpy.ndarray: |
|
1036 | 1040 | |
|
1037 | 1041 | if nDim == 3: |
|
1038 | 1042 | self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1)) |
|
1039 | 1043 | self.ds[i].resize(self.data[i].shape) |
|
1040 | 1044 | if mode == 2: |
|
1041 | 1045 | self.ds[i].resize(self.data[i].shape) |
|
1042 | 1046 | self.ds[i][:] = self.data[i] |
|
1043 | 1047 | else: |
|
1044 | 1048 | |
|
1045 | 1049 | # From second time |
|
1046 | 1050 | # Meteors! |
|
1047 | 1051 | if mode == 2: |
|
1048 | 1052 | dataShape = self.data[i].shape |
|
1049 | 1053 | dsShape = self.ds[i].shape |
|
1050 | 1054 | self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1])) |
|
1051 | 1055 | self.ds[i][dsShape[0]:,:] = self.data[i] |
|
1052 | 1056 | # No dimension |
|
1053 | 1057 | elif mode == 0: |
|
1054 | 1058 | self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1)) |
|
1055 | 1059 | self.ds[i][0,-1] = self.data[i] |
|
1056 | 1060 | # One dimension |
|
1057 | 1061 | elif nDim == 1: |
|
1058 | 1062 | self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1])) |
|
1059 | 1063 | self.ds[i][-1,:] = self.data[i] |
|
1060 | 1064 | # Two dimension |
|
1061 | 1065 | elif nDim == 2: |
|
1062 | 1066 | self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1])) |
|
1063 | 1067 | self.ds[i][self.blockIndex,:] = self.data[i] |
|
1064 | 1068 | # Three dimensions |
|
1065 | 1069 | elif nDim == 3: |
|
1066 | 1070 | self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1)) |
|
1067 | 1071 | self.ds[i][:,:,-1] = self.data[i] |
|
1068 | 1072 | |
|
1069 | 1073 | self.firsttime = False |
|
1070 | 1074 | self.blockIndex += 1 |
|
1071 | 1075 | |
|
1072 | 1076 | #Close to save changes |
|
1073 | 1077 | self.fp.flush() |
|
1074 | 1078 | self.fp.close() |
|
1075 | 1079 | return |
|
1076 | 1080 | |
|
1077 | def run(self, dataOut, **kwargs): | |
|
1081 | def run(self, dataOut, path=None, blocksPerFile=10, metadataList=None, dataList=None, mode=None, **kwargs): | |
|
1078 | 1082 | |
|
1079 | 1083 | if not(self.isConfig): |
|
1080 |
flagdata = self.setup(dataOut, |
|
|
1084 | flagdata = self.setup(dataOut, path=path, blocksPerFile=blocksPerFile, | |
|
1085 | metadataList=metadataList, dataList=dataList, mode=mode, **kwargs) | |
|
1081 | 1086 | |
|
1082 | 1087 | if not(flagdata): |
|
1083 | 1088 | return |
|
1084 | 1089 | |
|
1085 | 1090 | self.isConfig = True |
|
1086 | 1091 | # self.putMetadata() |
|
1087 | 1092 | self.setNextFile() |
|
1088 | 1093 | |
|
1089 | 1094 | self.putData() |
|
1090 | 1095 | return |
@@ -1,637 +1,738 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 2, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import numpy |
|
8 | 8 | |
|
9 | 9 | from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter |
|
10 | 10 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation |
|
11 | 11 | from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader |
|
12 | 12 | from schainpy.model.data.jrodata import Voltage |
|
13 | 13 | # from _sha import blocksize |
|
14 | 14 | |
|
15 | 15 | class VoltageReader(JRODataReader, ProcessingUnit): |
|
16 | 16 | """ |
|
17 | 17 | Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura |
|
18 | 18 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: |
|
19 | 19 | perfiles*alturas*canales) son almacenados en la variable "buffer". |
|
20 | 20 | |
|
21 | 21 | perfiles * alturas * canales |
|
22 | 22 | |
|
23 | 23 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
24 | 24 | RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la |
|
25 | 25 | cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de |
|
26 | 26 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
27 | 27 | |
|
28 | 28 | Example: |
|
29 | 29 | |
|
30 | 30 | dpath = "/home/myuser/data" |
|
31 | 31 | |
|
32 | 32 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
33 | 33 | |
|
34 | 34 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
35 | 35 | |
|
36 | 36 | readerObj = VoltageReader() |
|
37 | 37 | |
|
38 | 38 | readerObj.setup(dpath, startTime, endTime) |
|
39 | 39 | |
|
40 | 40 | while(True): |
|
41 | 41 | |
|
42 | 42 | #to get one profile |
|
43 | 43 | profile = readerObj.getData() |
|
44 | 44 | |
|
45 | 45 | #print the profile |
|
46 | 46 | print profile |
|
47 | 47 | |
|
48 | 48 | #If you want to see all datablock |
|
49 | 49 | print readerObj.datablock |
|
50 | 50 | |
|
51 | 51 | if readerObj.flagNoMoreFiles: |
|
52 | 52 | break |
|
53 | 53 | |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 | 56 | ext = ".r" |
|
57 | 57 | |
|
58 | 58 | optchar = "D" |
|
59 | 59 | dataOut = None |
|
60 | 60 | |
|
61 | 61 | def __init__(self): |
|
62 | 62 | """ |
|
63 | 63 | Inicializador de la clase VoltageReader para la lectura de datos de voltage. |
|
64 | 64 | |
|
65 | 65 | Input: |
|
66 | 66 | dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para |
|
67 | 67 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
68 | 68 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
69 | 69 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
70 | 70 | bloque de datos. |
|
71 | 71 | Si este parametro no es pasado se creara uno internamente. |
|
72 | 72 | |
|
73 | 73 | Variables afectadas: |
|
74 | 74 | self.dataOut |
|
75 | 75 | |
|
76 | 76 | Return: |
|
77 | 77 | None |
|
78 | 78 | """ |
|
79 | 79 | |
|
80 | 80 | ProcessingUnit.__init__(self) |
|
81 | 81 | |
|
82 | 82 | self.isConfig = False |
|
83 | 83 | |
|
84 | 84 | self.datablock = None |
|
85 | 85 | |
|
86 | 86 | self.utc = 0 |
|
87 | 87 | |
|
88 | 88 | self.ext = ".r" |
|
89 | 89 | |
|
90 | 90 | self.optchar = "D" |
|
91 | 91 | |
|
92 | 92 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
93 | 93 | |
|
94 | 94 | self.systemHeaderObj = SystemHeader() |
|
95 | 95 | |
|
96 | 96 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
97 | 97 | |
|
98 | 98 | self.processingHeaderObj = ProcessingHeader() |
|
99 | 99 | |
|
100 | 100 | self.online = 0 |
|
101 | 101 | |
|
102 | 102 | self.fp = None |
|
103 | 103 | |
|
104 | 104 | self.idFile = None |
|
105 | 105 | |
|
106 | 106 | self.dtype = None |
|
107 | 107 | |
|
108 | 108 | self.fileSizeByHeader = None |
|
109 | 109 | |
|
110 | 110 | self.filenameList = [] |
|
111 | 111 | |
|
112 | 112 | self.filename = None |
|
113 | 113 | |
|
114 | 114 | self.fileSize = None |
|
115 | 115 | |
|
116 | 116 | self.firstHeaderSize = 0 |
|
117 | 117 | |
|
118 | 118 | self.basicHeaderSize = 24 |
|
119 | 119 | |
|
120 | 120 | self.pathList = [] |
|
121 | 121 | |
|
122 | 122 | self.filenameList = [] |
|
123 | 123 | |
|
124 | 124 | self.lastUTTime = 0 |
|
125 | 125 | |
|
126 | 126 | self.maxTimeStep = 30 |
|
127 | 127 | |
|
128 | 128 | self.flagNoMoreFiles = 0 |
|
129 | 129 | |
|
130 | 130 | self.set = 0 |
|
131 | 131 | |
|
132 | 132 | self.path = None |
|
133 | 133 | |
|
134 | 134 | self.profileIndex = 2**32-1 |
|
135 | 135 | |
|
136 | 136 | self.delay = 3 #seconds |
|
137 | 137 | |
|
138 | 138 | self.nTries = 3 #quantity tries |
|
139 | 139 | |
|
140 | 140 | self.nFiles = 3 #number of files for searching |
|
141 | 141 | |
|
142 | 142 | self.nReadBlocks = 0 |
|
143 | 143 | |
|
144 | 144 | self.flagIsNewFile = 1 |
|
145 | 145 | |
|
146 | 146 | self.__isFirstTimeOnline = 1 |
|
147 | 147 | |
|
148 | 148 | # self.ippSeconds = 0 |
|
149 | 149 | |
|
150 | 150 | self.flagDiscontinuousBlock = 0 |
|
151 | 151 | |
|
152 | 152 | self.flagIsNewBlock = 0 |
|
153 | 153 | |
|
154 | 154 | self.nTotalBlocks = 0 |
|
155 | 155 | |
|
156 | 156 | self.blocksize = 0 |
|
157 | 157 | |
|
158 | 158 | self.dataOut = self.createObjByDefault() |
|
159 | 159 | |
|
160 | 160 | self.nTxs = 1 |
|
161 | 161 | |
|
162 | 162 | self.txIndex = 0 |
|
163 | 163 | |
|
164 | 164 | def createObjByDefault(self): |
|
165 | 165 | |
|
166 | 166 | dataObj = Voltage() |
|
167 | 167 | |
|
168 | 168 | return dataObj |
|
169 | 169 | |
|
170 | 170 | def __hasNotDataInBuffer(self): |
|
171 | 171 | |
|
172 | 172 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock*self.nTxs: |
|
173 | 173 | return 1 |
|
174 | 174 | |
|
175 | 175 | return 0 |
|
176 | 176 | |
|
177 | 177 | |
|
178 | 178 | def getBlockDimension(self): |
|
179 | 179 | """ |
|
180 | Obtiene la cantidad de puntos a leer por cada bloque de datos | |
|
181 | ||
|
182 | Affected: | |
|
183 | self.blocksize | |
|
180 | Obtiene la cantidad de puntos a leer por cada bloque de datos | |
|
181 | ||
|
182 | Affected: | |
|
183 | self.blocksize | |
|
184 | 184 | |
|
185 | Return: | |
|
186 | None | |
|
185 | Return: | |
|
186 | None | |
|
187 | 187 | """ |
|
188 | 188 | pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels |
|
189 | 189 | self.blocksize = pts2read |
|
190 | 190 | |
|
191 | 191 | |
|
192 | 192 | def readBlock(self): |
|
193 | 193 | """ |
|
194 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo | |
|
195 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos | |
|
196 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer | |
|
197 | es seteado a 0 | |
|
198 | ||
|
199 | Inputs: | |
|
200 | None | |
|
194 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo | |
|
195 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos | |
|
196 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer | |
|
197 | es seteado a 0 | |
|
201 | 198 | |
|
202 | Return: | |
|
203 | None | |
|
204 | ||
|
205 |
|
|
|
206 | self.profileIndex | |
|
207 | self.datablock | |
|
208 | self.flagIsNewFile | |
|
209 | self.flagIsNewBlock | |
|
210 | self.nTotalBlocks | |
|
199 | Inputs: | |
|
200 | None | |
|
201 | ||
|
202 | Return: | |
|
203 | None | |
|
211 | 204 | |
|
212 | Exceptions: | |
|
213 | Si un bloque leido no es un bloque valido | |
|
205 | Affected: | |
|
206 | self.profileIndex | |
|
207 | self.datablock | |
|
208 | self.flagIsNewFile | |
|
209 | self.flagIsNewBlock | |
|
210 | self.nTotalBlocks | |
|
211 | ||
|
212 | Exceptions: | |
|
213 | Si un bloque leido no es un bloque valido | |
|
214 | 214 | """ |
|
215 | ||
|
216 | # if self.server is not None: | |
|
217 | # self.zBlock = self.receiver.recv() | |
|
218 | # self.zHeader = self.zBlock[:24] | |
|
219 | # self.zDataBlock = self.zBlock[24:] | |
|
220 | # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')])) | |
|
221 | # self.processingHeaderObj.profilesPerBlock = 240 | |
|
222 | # self.processingHeaderObj.nHeights = 248 | |
|
223 | # self.systemHeaderObj.nChannels | |
|
224 | # else: | |
|
215 | 225 | current_pointer_location = self.fp.tell() |
|
216 | 226 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) |
|
217 | 227 | |
|
218 | 228 | try: |
|
219 | 229 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) |
|
220 | 230 | except: |
|
221 | 231 | #print "The read block (%3d) has not enough data" %self.nReadBlocks |
|
222 | 232 | |
|
223 | 233 | if self.waitDataBlock(pointer_location=current_pointer_location): |
|
224 | 234 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) |
|
225 | 235 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) |
|
226 | # return 0 | |
|
236 | # return 0 | |
|
227 | 237 | |
|
228 | 238 | #Dimensions : nChannels, nProfiles, nSamples |
|
229 | 239 | |
|
230 | 240 | junk = numpy.transpose(junk, (2,0,1)) |
|
231 | 241 | self.datablock = junk['real'] + junk['imag']*1j |
|
232 | 242 | |
|
233 | 243 | self.profileIndex = 0 |
|
234 | 244 | |
|
235 | 245 | self.flagIsNewFile = 0 |
|
236 | 246 | self.flagIsNewBlock = 1 |
|
237 | 247 | |
|
238 | 248 | self.nTotalBlocks += 1 |
|
239 | 249 | self.nReadBlocks += 1 |
|
240 | 250 | |
|
241 | 251 | return 1 |
|
242 | 252 | |
|
243 | 253 | def getFirstHeader(self): |
|
244 | 254 | |
|
245 | 255 | self.getBasicHeader() |
|
246 | 256 | |
|
247 | 257 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() |
|
248 | 258 | |
|
249 | 259 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() |
|
250 | 260 | |
|
251 | 261 | if self.nTxs > 1: |
|
252 | 262 | self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs |
|
253 | 263 | |
|
254 | 264 | #Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj. |
|
255 | ||
|
256 | # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt | |
|
257 | # | |
|
258 | # if self.radarControllerHeaderObj.code is not None: | |
|
259 |
|
|
|
260 | # self.dataOut.nCode = self.radarControllerHeaderObj.nCode | |
|
261 |
|
|
|
262 | # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud | |
|
263 |
|
|
|
264 | # self.dataOut.code = self.radarControllerHeaderObj.code | |
|
265 | ||
|
265 | ||
|
266 | # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt | |
|
267 | # | |
|
268 | # if self.radarControllerHeaderObj.code is not None: | |
|
269 | # | |
|
270 | # self.dataOut.nCode = self.radarControllerHeaderObj.nCode | |
|
271 | # | |
|
272 | # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud | |
|
273 | # | |
|
274 | # self.dataOut.code = self.radarControllerHeaderObj.code | |
|
275 | ||
|
266 | 276 | self.dataOut.dtype = self.dtype |
|
267 | 277 | |
|
268 | 278 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock |
|
269 | 279 | |
|
270 | 280 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight |
|
271 | 281 | |
|
272 | 282 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) |
|
273 | 283 | |
|
274 | 284 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt |
|
275 | 285 | |
|
276 | 286 | self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada |
|
277 | 287 | |
|
278 | 288 | self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data no esta sin flip |
|
279 | 289 | |
|
280 | 290 | self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft |
|
281 | 291 | |
|
282 | 292 | def reshapeData(self): |
|
283 | 293 | |
|
284 | 294 | if self.nTxs < 0: |
|
285 | 295 | return |
|
286 | 296 | |
|
287 | 297 | if self.nTxs == 1: |
|
288 | 298 | return |
|
289 | 299 | |
|
290 | 300 | if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1./self.nTxs) != 0: |
|
291 | 301 | raise ValueError, "1./nTxs (=%f), should be a multiple of nProfiles (=%d)" %(1./self.nTxs, self.processingHeaderObj.profilesPerBlock) |
|
292 | 302 | |
|
293 | 303 | if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0: |
|
294 | 304 | raise ValueError, "nTxs (=%d), should be a multiple of nHeights (=%d)" %(self.nTxs, self.processingHeaderObj.nHeights) |
|
295 | 305 | |
|
296 | 306 | self.datablock = self.datablock.reshape((self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock*self.nTxs, self.processingHeaderObj.nHeights/self.nTxs)) |
|
297 | 307 | |
|
298 | 308 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs |
|
299 | 309 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights/self.nTxs) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight |
|
300 | 310 | self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs |
|
301 | ||
|
311 | ||
|
302 | 312 | return |
|
303 | ||
|
313 | ||
|
314 | def readFirstHeaderFromServer(self): | |
|
315 | ||
|
316 | self.getFirstHeader() | |
|
317 | ||
|
318 | self.firstHeaderSize = self.basicHeaderObj.size | |
|
319 | ||
|
320 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) | |
|
321 | if datatype == 0: | |
|
322 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) | |
|
323 | elif datatype == 1: | |
|
324 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) | |
|
325 | elif datatype == 2: | |
|
326 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) | |
|
327 | elif datatype == 3: | |
|
328 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) | |
|
329 | elif datatype == 4: | |
|
330 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) | |
|
331 | elif datatype == 5: | |
|
332 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) | |
|
333 | else: | |
|
334 | raise ValueError, 'Data type was not defined' | |
|
335 | ||
|
336 | self.dtype = datatype_str | |
|
337 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c | |
|
338 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1) | |
|
339 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) | |
|
340 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) | |
|
341 | self.getBlockDimension() | |
|
342 | ||
|
343 | ||
|
344 | def getFromServer(self): | |
|
345 | self.flagDiscontinuousBlock = 0 | |
|
346 | self.profileIndex = 0 | |
|
347 | self.flagIsNewBlock = 1 | |
|
348 | self.dataOut.flagNoData = False | |
|
349 | self.nTotalBlocks += 1 | |
|
350 | self.nReadBlocks += 1 | |
|
351 | self.blockPointer = 0 | |
|
352 | ||
|
353 | block = self.receiver.recv() | |
|
354 | ||
|
355 | self.basicHeaderObj.read(block[self.blockPointer:]) | |
|
356 | self.blockPointer += self.basicHeaderObj.length | |
|
357 | self.systemHeaderObj.read(block[self.blockPointer:]) | |
|
358 | self.blockPointer += self.systemHeaderObj.length | |
|
359 | self.radarControllerHeaderObj.read(block[self.blockPointer:]) | |
|
360 | self.blockPointer += self.radarControllerHeaderObj.length | |
|
361 | self.processingHeaderObj.read(block[self.blockPointer:]) | |
|
362 | self.blockPointer += self.processingHeaderObj.length | |
|
363 | self.readFirstHeaderFromServer() | |
|
364 | ||
|
365 | timestamp = self.basicHeaderObj.get_datatime() | |
|
366 | print '[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp) | |
|
367 | current_pointer_location = self.blockPointer | |
|
368 | junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize ) | |
|
369 | ||
|
370 | try: | |
|
371 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) | |
|
372 | except: | |
|
373 | #print "The read block (%3d) has not enough data" %self.nReadBlocks | |
|
374 | if self.waitDataBlock(pointer_location=current_pointer_location): | |
|
375 | junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize ) | |
|
376 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) | |
|
377 | # return 0 | |
|
378 | ||
|
379 | #Dimensions : nChannels, nProfiles, nSamples | |
|
380 | ||
|
381 | junk = numpy.transpose(junk, (2,0,1)) | |
|
382 | self.datablock = junk['real'] + junk['imag'] * 1j | |
|
383 | self.profileIndex = 0 | |
|
384 | if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles | |
|
385 | if self.selBlocktime != None: | |
|
386 | if self.dataOut.nCohInt is not None: | |
|
387 | nCohInt = self.dataOut.nCohInt | |
|
388 | else: | |
|
389 | nCohInt = 1 | |
|
390 | self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles))) | |
|
391 | self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:] | |
|
392 | datasize = self.dataOut.data.shape[1] | |
|
393 | if datasize < self.selBlocksize: | |
|
394 | buffer = numpy.zeros((self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype = 'complex') | |
|
395 | buffer[:,:datasize,:] = self.dataOut.data | |
|
396 | self.dataOut.data = buffer | |
|
397 | self.profileIndex = blockIndex | |
|
398 | ||
|
399 | self.dataOut.flagDataAsBlock = True | |
|
400 | self.flagIsNewBlock = 1 | |
|
401 | self.dataOut.realtime = self.online | |
|
402 | ||
|
403 | return self.dataOut.data | |
|
404 | ||
|
304 | 405 | def getData(self): |
|
305 | 406 | """ |
|
306 | getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut | |
|
307 | del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos | |
|
308 | en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando | |
|
309 | "readNextBlock" | |
|
310 | ||
|
311 | Ademas incrementa el contador del buffer "self.profileIndex" en 1. | |
|
312 | ||
|
313 | Return: | |
|
314 | ||
|
315 | Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex | |
|
316 | es igual al total de perfiles leidos desde el archivo. | |
|
317 | ||
|
318 | Si self.getByBlock == False: | |
|
407 | getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut | |
|
408 | del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos | |
|
409 | en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando | |
|
410 | "readNextBlock" | |
|
319 | 411 | |
|
320 | self.dataOut.data = buffer[:, thisProfile, :] | |
|
321 | ||
|
322 | shape = [nChannels, nHeis] | |
|
323 | ||
|
324 | Si self.getByBlock == True: | |
|
412 | Ademas incrementa el contador del buffer "self.profileIndex" en 1. | |
|
325 | 413 | |
|
326 | self.dataOut.data = buffer[:, :, :] | |
|
327 | ||
|
328 | shape = [nChannels, nProfiles, nHeis] | |
|
414 | Return: | |
|
329 | 415 | |
|
330 | Variables afectadas: | |
|
331 | self.dataOut | |
|
332 | self.profileIndex | |
|
416 | Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex | |
|
417 | es igual al total de perfiles leidos desde el archivo. | |
|
333 | 418 | |
|
334 | Affected: | |
|
335 |
|
|
|
336 | self.profileIndex | |
|
337 | self.flagDiscontinuousBlock | |
|
338 | self.flagIsNewBlock | |
|
419 | Si self.getByBlock == False: | |
|
420 | ||
|
421 | self.dataOut.data = buffer[:, thisProfile, :] | |
|
422 | ||
|
423 | shape = [nChannels, nHeis] | |
|
424 | ||
|
425 | Si self.getByBlock == True: | |
|
426 | ||
|
427 | self.dataOut.data = buffer[:, :, :] | |
|
428 | ||
|
429 | shape = [nChannels, nProfiles, nHeis] | |
|
430 | ||
|
431 | Variables afectadas: | |
|
432 | self.dataOut | |
|
433 | self.profileIndex | |
|
434 | ||
|
435 | Affected: | |
|
436 | self.dataOut | |
|
437 | self.profileIndex | |
|
438 | self.flagDiscontinuousBlock | |
|
439 | self.flagIsNewBlock | |
|
339 | 440 | """ |
|
340 | 441 | |
|
341 | 442 | if self.flagNoMoreFiles: |
|
342 | 443 | self.dataOut.flagNoData = True |
|
343 | 444 | print 'Process finished' |
|
344 | 445 | return 0 |
|
345 | 446 | |
|
346 | 447 | self.flagDiscontinuousBlock = 0 |
|
347 | 448 | self.flagIsNewBlock = 0 |
|
348 | 449 | |
|
349 | 450 | if self.__hasNotDataInBuffer(): |
|
350 | 451 | |
|
351 | 452 | if not( self.readNextBlock() ): |
|
352 | 453 | return 0 |
|
353 | 454 | |
|
354 | 455 | self.getFirstHeader() |
|
355 | 456 | |
|
356 | 457 | self.reshapeData() |
|
357 | 458 | |
|
358 | 459 | if self.datablock is None: |
|
359 | 460 | self.dataOut.flagNoData = True |
|
360 | 461 | return 0 |
|
361 | 462 | |
|
362 | 463 | if not self.getByBlock: |
|
363 | 464 | |
|
364 | 465 | """ |
|
365 | Return profile by profile | |
|
366 | ||
|
367 | If nTxs > 1 then one profile is divided by nTxs and number of total | |
|
368 | blocks is increased by nTxs (nProfiles *= nTxs) | |
|
466 | Return profile by profile | |
|
467 | ||
|
468 | If nTxs > 1 then one profile is divided by nTxs and number of total | |
|
469 | blocks is increased by nTxs (nProfiles *= nTxs) | |
|
369 | 470 | """ |
|
370 | 471 | self.dataOut.flagDataAsBlock = False |
|
371 | 472 | self.dataOut.data = self.datablock[:,self.profileIndex,:] |
|
372 | 473 | self.dataOut.profileIndex = self.profileIndex |
|
373 | 474 | |
|
374 | 475 | self.profileIndex += 1 |
|
375 | 476 | |
|
376 | # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles: | |
|
377 | # """ | |
|
378 | # Return all block | |
|
379 | # """ | |
|
380 | # self.dataOut.flagDataAsBlock = True | |
|
381 | # self.dataOut.data = self.datablock | |
|
382 | # self.dataOut.profileIndex = self.dataOut.nProfiles - 1 | |
|
383 | # | |
|
384 | # self.profileIndex = self.dataOut.nProfiles | |
|
477 | # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles: | |
|
478 | # """ | |
|
479 | # Return all block | |
|
480 | # """ | |
|
481 | # self.dataOut.flagDataAsBlock = True | |
|
482 | # self.dataOut.data = self.datablock | |
|
483 | # self.dataOut.profileIndex = self.dataOut.nProfiles - 1 | |
|
484 | # | |
|
485 | # self.profileIndex = self.dataOut.nProfiles | |
|
385 | 486 | |
|
386 | 487 | else: |
|
387 | 488 | """ |
|
388 | Return a block | |
|
489 | Return a block | |
|
389 | 490 | """ |
|
390 | 491 | if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles |
|
391 | 492 | if self.selBlocktime != None: |
|
392 | 493 | if self.dataOut.nCohInt is not None: |
|
393 | 494 | nCohInt = self.dataOut.nCohInt |
|
394 | 495 | else: |
|
395 | 496 | nCohInt = 1 |
|
396 | 497 | self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles))) |
|
397 | 498 | |
|
398 | 499 | self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:] |
|
399 | 500 | self.profileIndex += self.selBlocksize |
|
400 | 501 | datasize = self.dataOut.data.shape[1] |
|
401 | 502 | |
|
402 | 503 | if datasize < self.selBlocksize: |
|
403 | 504 | buffer = numpy.zeros((self.dataOut.data.shape[0],self.selBlocksize,self.dataOut.data.shape[2]), dtype = 'complex') |
|
404 | 505 | buffer[:,:datasize,:] = self.dataOut.data |
|
405 | 506 | |
|
406 | 507 | while datasize < self.selBlocksize: #Not enough profiles to fill the block |
|
407 | 508 | if not( self.readNextBlock() ): |
|
408 | 509 | return 0 |
|
409 | 510 | self.getFirstHeader() |
|
410 | 511 | self.reshapeData() |
|
411 | 512 | if self.datablock is None: |
|
412 | 513 | self.dataOut.flagNoData = True |
|
413 | 514 | return 0 |
|
414 | 515 | #stack data |
|
415 | 516 | blockIndex = self.selBlocksize - datasize |
|
416 | 517 | datablock1 = self.datablock[:,:blockIndex,:] |
|
417 | 518 | |
|
418 | 519 | buffer[:,datasize:datasize+datablock1.shape[1],:] = datablock1 |
|
419 | 520 | datasize += datablock1.shape[1] |
|
420 | 521 | |
|
421 | 522 | self.dataOut.data = buffer |
|
422 | 523 | self.profileIndex = blockIndex |
|
423 | 524 | |
|
424 | 525 | self.dataOut.flagDataAsBlock = True |
|
425 | 526 | self.dataOut.nProfiles = self.dataOut.data.shape[1] |
|
426 | 527 | |
|
427 | 528 | self.dataOut.flagNoData = False |
|
428 | 529 | |
|
429 | 530 | self.getBasicHeader() |
|
430 | 531 | |
|
431 | 532 | self.dataOut.realtime = self.online |
|
432 | 533 | |
|
433 | 534 | return self.dataOut.data |
|
434 | 535 | |
|
435 | 536 | class VoltageWriter(JRODataWriter, Operation): |
|
436 | 537 | """ |
|
437 | 538 | Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura |
|
438 | 539 | de los datos siempre se realiza por bloques. |
|
439 | 540 | """ |
|
440 | 541 | |
|
441 | 542 | ext = ".r" |
|
442 | 543 | |
|
443 | 544 | optchar = "D" |
|
444 | 545 | |
|
445 | 546 | shapeBuffer = None |
|
446 | 547 | |
|
447 | 548 | |
|
448 | 549 | def __init__(self): |
|
449 | 550 | """ |
|
450 | 551 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. |
|
451 | 552 | |
|
452 | 553 | Affected: |
|
453 | 554 | self.dataOut |
|
454 | 555 | |
|
455 | 556 | Return: None |
|
456 | 557 | """ |
|
457 | 558 | Operation.__init__(self) |
|
458 | 559 | |
|
459 | 560 | self.nTotalBlocks = 0 |
|
460 | 561 | |
|
461 | 562 | self.profileIndex = 0 |
|
462 | 563 | |
|
463 | 564 | self.isConfig = False |
|
464 | 565 | |
|
465 | 566 | self.fp = None |
|
466 | 567 | |
|
467 | 568 | self.flagIsNewFile = 1 |
|
468 | 569 | |
|
469 | 570 | self.blockIndex = 0 |
|
470 | 571 | |
|
471 | 572 | self.flagIsNewBlock = 0 |
|
472 | 573 | |
|
473 | 574 | self.setFile = None |
|
474 | 575 | |
|
475 | 576 | self.dtype = None |
|
476 | 577 | |
|
477 | 578 | self.path = None |
|
478 | 579 | |
|
479 | 580 | self.filename = None |
|
480 | 581 | |
|
481 | 582 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
482 | 583 | |
|
483 | 584 | self.systemHeaderObj = SystemHeader() |
|
484 | 585 | |
|
485 | 586 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
486 | 587 | |
|
487 | 588 | self.processingHeaderObj = ProcessingHeader() |
|
488 | 589 | |
|
489 | 590 | def hasAllDataInBuffer(self): |
|
490 | 591 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: |
|
491 | 592 | return 1 |
|
492 | 593 | return 0 |
|
493 | 594 | |
|
494 | 595 | |
|
495 | 596 | def setBlockDimension(self): |
|
496 | 597 | """ |
|
497 | 598 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
498 | 599 | |
|
499 | 600 | Affected: |
|
500 | 601 | self.shape_spc_Buffer |
|
501 | 602 | self.shape_cspc_Buffer |
|
502 | 603 | self.shape_dc_Buffer |
|
503 | 604 | |
|
504 | 605 | Return: None |
|
505 | 606 | """ |
|
506 | 607 | self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock, |
|
507 | 608 | self.processingHeaderObj.nHeights, |
|
508 | 609 | self.systemHeaderObj.nChannels) |
|
509 | 610 | |
|
510 | 611 | self.datablock = numpy.zeros((self.systemHeaderObj.nChannels, |
|
511 | 612 | self.processingHeaderObj.profilesPerBlock, |
|
512 | 613 | self.processingHeaderObj.nHeights), |
|
513 | 614 | dtype=numpy.dtype('complex64')) |
|
514 | 615 | |
|
515 | 616 | def writeBlock(self): |
|
516 | 617 | """ |
|
517 | 618 | Escribe el buffer en el file designado |
|
518 | 619 | |
|
519 | 620 | Affected: |
|
520 | 621 | self.profileIndex |
|
521 | 622 | self.flagIsNewFile |
|
522 | 623 | self.flagIsNewBlock |
|
523 | 624 | self.nTotalBlocks |
|
524 | 625 | self.blockIndex |
|
525 | 626 | |
|
526 | 627 | Return: None |
|
527 | 628 | """ |
|
528 | 629 | data = numpy.zeros( self.shapeBuffer, self.dtype ) |
|
529 | 630 | |
|
530 | 631 | junk = numpy.transpose(self.datablock, (1,2,0)) |
|
531 | 632 | |
|
532 | 633 | data['real'] = junk.real |
|
533 | 634 | data['imag'] = junk.imag |
|
534 | 635 | |
|
535 | 636 | data = data.reshape( (-1) ) |
|
536 | 637 | |
|
537 | 638 | data.tofile( self.fp ) |
|
538 | 639 | |
|
539 | 640 | self.datablock.fill(0) |
|
540 | 641 | |
|
541 | 642 | self.profileIndex = 0 |
|
542 | 643 | self.flagIsNewFile = 0 |
|
543 | 644 | self.flagIsNewBlock = 1 |
|
544 | 645 | |
|
545 | 646 | self.blockIndex += 1 |
|
546 | 647 | self.nTotalBlocks += 1 |
|
547 | 648 | |
|
548 | 649 | # print "[Writing] Block = %04d" %self.blockIndex |
|
549 | 650 | |
|
550 | 651 | def putData(self): |
|
551 | 652 | """ |
|
552 | 653 | Setea un bloque de datos y luego los escribe en un file |
|
553 | 654 | |
|
554 | 655 | Affected: |
|
555 | 656 | self.flagIsNewBlock |
|
556 | 657 | self.profileIndex |
|
557 | 658 | |
|
558 | 659 | Return: |
|
559 | 660 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
560 | 661 | 1 : Si se escribio la data de un bloque en un file |
|
561 | 662 | """ |
|
562 | 663 | if self.dataOut.flagNoData: |
|
563 | 664 | return 0 |
|
564 | 665 | |
|
565 | 666 | self.flagIsNewBlock = 0 |
|
566 | 667 | |
|
567 | 668 | if self.dataOut.flagDiscontinuousBlock: |
|
568 | 669 | self.datablock.fill(0) |
|
569 | 670 | self.profileIndex = 0 |
|
570 | 671 | self.setNextFile() |
|
571 | 672 | |
|
572 | 673 | if self.profileIndex == 0: |
|
573 | 674 | self.setBasicHeader() |
|
574 | 675 | |
|
575 | 676 | self.datablock[:,self.profileIndex,:] = self.dataOut.data |
|
576 | 677 | |
|
577 | 678 | self.profileIndex += 1 |
|
578 | 679 | |
|
579 | 680 | if self.hasAllDataInBuffer(): |
|
580 | 681 | #if self.flagIsNewFile: |
|
581 | 682 | self.writeNextBlock() |
|
582 | 683 | # self.setFirstHeader() |
|
583 | 684 | |
|
584 | 685 | return 1 |
|
585 | 686 | |
|
586 | 687 | def __getBlockSize(self): |
|
587 | 688 | ''' |
|
588 | 689 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage |
|
589 | 690 | ''' |
|
590 | 691 | |
|
591 | 692 | dtype_width = self.getDtypeWidth() |
|
592 | 693 | |
|
593 | 694 | blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * dtype_width * 2) |
|
594 | 695 | |
|
595 | 696 | return blocksize |
|
596 | 697 | |
|
597 | 698 | def setFirstHeader(self): |
|
598 | 699 | |
|
599 | 700 | """ |
|
600 | 701 | Obtiene una copia del First Header |
|
601 | 702 | |
|
602 | 703 | Affected: |
|
603 | 704 | self.systemHeaderObj |
|
604 | 705 | self.radarControllerHeaderObj |
|
605 | 706 | self.dtype |
|
606 | 707 | |
|
607 | 708 | Return: |
|
608 | 709 | None |
|
609 | 710 | """ |
|
610 | 711 | |
|
611 | 712 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() |
|
612 | 713 | self.systemHeaderObj.nChannels = self.dataOut.nChannels |
|
613 | 714 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() |
|
614 | 715 | |
|
615 | 716 | self.processingHeaderObj.dtype = 0 # Voltage |
|
616 | 717 | self.processingHeaderObj.blockSize = self.__getBlockSize() |
|
617 | 718 | self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock |
|
618 | 719 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile |
|
619 | 720 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows |
|
620 | 721 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt |
|
621 | 722 | self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage |
|
622 | 723 | self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage |
|
623 | 724 | |
|
624 | 725 | if self.dataOut.code is not None: |
|
625 | 726 | self.processingHeaderObj.code = self.dataOut.code |
|
626 | 727 | self.processingHeaderObj.nCode = self.dataOut.nCode |
|
627 | 728 | self.processingHeaderObj.nBaud = self.dataOut.nBaud |
|
628 | 729 | |
|
629 | 730 | if self.processingHeaderObj.nWindows != 0: |
|
630 | 731 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] |
|
631 | 732 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
632 | 733 | self.processingHeaderObj.nHeights = self.dataOut.nHeights |
|
633 | 734 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights |
|
634 | 735 | |
|
635 | 736 | self.processingHeaderObj.processFlags = self.getProcessFlags() |
|
636 | 737 | |
|
637 | 738 | self.setBasicHeader() No newline at end of file |
@@ -1,58 +1,139 | |||
|
1 | #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION | |
|
2 | #define NUM_CPY_THREADS 8 | |
|
1 | 3 | #include <Python.h> |
|
2 | 4 | #include <numpy/arrayobject.h> |
|
3 | 5 | #include <math.h> |
|
6 | #include <complex.h> | |
|
7 | #include <time.h> | |
|
4 | 8 | |
|
9 | // void printArr(int *array); | |
|
5 | 10 | static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args); |
|
11 | static PyObject *correlateByBlock(PyObject *self, PyObject *args); | |
|
12 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ | |
|
13 | #define PyMODINIT_FUNC void | |
|
14 | #endif | |
|
6 | 15 | |
|
7 | 16 | static PyMethodDef extensionsMethods[] = { |
|
8 | { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with" }, | |
|
9 | { NULL, NULL, 0, NULL } | |
|
17 | { "correlateByBlock", (PyCFunction)correlateByBlock, METH_VARARGS, "get correlation by block" }, | |
|
18 | { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with hildebrand_sekhon" }, | |
|
19 | { NULL, NULL, 0, NULL } | |
|
10 | 20 | }; |
|
11 | 21 | |
|
12 | 22 | PyMODINIT_FUNC initcSchain() { |
|
13 | 23 | Py_InitModule("cSchain", extensionsMethods); |
|
14 | 24 | import_array(); |
|
15 | 25 | } |
|
16 | 26 | |
|
27 | static PyObject *correlateByBlock(PyObject *self, PyObject *args) { | |
|
28 | ||
|
29 | // int *x = (int*) malloc(4000000 * 216 * sizeof(int));; | |
|
30 | // int a = 5; | |
|
31 | // x = &a; | |
|
32 | // int b = 6; | |
|
33 | // x = &b; | |
|
34 | // printf("Antes de imprimir x \n"); | |
|
35 | // printf("%d \n", x[0]); | |
|
36 | ||
|
37 | PyObject *data_obj1, *data_obj2; | |
|
38 | PyArrayObject *data_array1, *data_array2, *correlateRow, *out, *dataRow, *codeRow; //, , | |
|
39 | int mode; | |
|
40 | ||
|
41 | if (!PyArg_ParseTuple(args, "OOi", &data_obj1, &data_obj2, &mode)) return NULL; | |
|
42 | ||
|
43 | data_array1 = (PyArrayObject *) PyArray_FROM_OTF(data_obj1, NPY_COMPLEX128, NPY_ARRAY_DEFAULT); | |
|
44 | data_array2 = (PyArrayObject *) PyArray_FROM_OTF(data_obj2, NPY_FLOAT64, NPY_ARRAY_DEFAULT); | |
|
45 | ||
|
46 | npy_intp dims[1]; | |
|
47 | dims[0] = 200; | |
|
48 | npy_intp dims_code[1]; | |
|
49 | dims_code[0] = 16; | |
|
50 | ||
|
51 | double complex * dataRaw; | |
|
52 | double * codeRaw; | |
|
53 | dataRaw = (double complex*) PyArray_DATA(data_array1); | |
|
54 | codeRaw = (double *) PyArray_DATA(data_array2); | |
|
55 | double complex ** outC = malloc(40000*200*sizeof(double complex)); | |
|
56 | int i; | |
|
57 | ||
|
58 | clock_t start = clock(); | |
|
59 | for(i=0; i<40000; i++){ | |
|
60 | // codeRow = PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i); | |
|
61 | // dataRow = PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i); | |
|
62 | // Py_INCREF(codeRow); | |
|
63 | // Py_INCREF(dataRow); | |
|
64 | // PyArray_ENABLEFLAGS(codeRow, NPY_ARRAY_OWNDATA); | |
|
65 | // PyArray_ENABLEFLAGS(dataRow, NPY_ARRAY_OWNDATA); | |
|
66 | correlateRow = (PyArrayObject *) PyArray_Correlate2(PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i), PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i), (npy_intp) 2); | |
|
67 | //Py_INCREF(correlateRow); | |
|
68 | // PyArray_ENABLEFLAGS(correlateRow, NPY_ARRAY_OWNDATA); | |
|
69 | memcpy(outC + 200*i, (double complex*) PyArray_DATA(correlateRow), 200 * sizeof(double complex)); | |
|
70 | ||
|
71 | Py_DECREF(correlateRow); | |
|
72 | // Py_DECREF(codeRow); | |
|
73 | // Py_DECREF(dataRow); | |
|
74 | } | |
|
75 | clock_t end = clock(); | |
|
76 | float seconds = (float)(end - start) / CLOCKS_PER_SEC; | |
|
77 | printf("%f", seconds); | |
|
78 | // | |
|
79 | npy_intp dimsret[2]; | |
|
80 | dimsret[0] = 40000; | |
|
81 | dimsret[1] = 200; | |
|
82 | out = PyArray_SimpleNewFromData(2, dimsret, NPY_COMPLEX128, outC); | |
|
83 | PyArray_ENABLEFLAGS(out, NPY_ARRAY_OWNDATA); | |
|
84 | //Py_INCREF(out); | |
|
85 | Py_DECREF(data_array1); | |
|
86 | Py_DECREF(data_array2); | |
|
87 | // PyArray_DebugPrint(out); | |
|
88 | // Py_DECREF(data_obj2); | |
|
89 | // Py_DECREF(data_obj1); | |
|
90 | // Py_DECREF(codeRow); | |
|
91 | // Py_DECREF(dataRow); | |
|
92 | // free(dataRaw); | |
|
93 | // free(codeRaw); | |
|
94 | ||
|
95 | return PyArray_Return(out); | |
|
96 | } | |
|
97 | ||
|
17 | 98 | static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) { |
|
18 | /* Do your stuff here. */ | |
|
19 | 99 | double navg; |
|
20 | 100 | PyObject *data_obj, *data_array; |
|
21 | 101 | |
|
22 | 102 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL; |
|
23 |
data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_ |
|
|
103 | data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_ARRAY_DEFAULT); | |
|
24 | 104 | if (data_array == NULL) { |
|
25 | 105 | Py_XDECREF(data_array); |
|
26 | 106 | Py_XDECREF(data_obj); |
|
27 | 107 | return NULL; |
|
28 | 108 | } |
|
29 | 109 | double *sortdata = (double*)PyArray_DATA(data_array); |
|
30 | 110 | int lenOfData = (int)PyArray_SIZE(data_array) ; |
|
31 | 111 | double nums_min = lenOfData*0.2; |
|
32 | 112 | if (nums_min <= 5) nums_min = 5; |
|
33 | 113 | double sump = 0; |
|
34 | 114 | double sumq = 0; |
|
35 | 115 | int j = 0; |
|
36 | 116 | int cont = 1; |
|
37 | 117 | double rtest = 0; |
|
38 | 118 | while ((cont == 1) && (j < lenOfData)) { |
|
39 | 119 | sump = sump + sortdata[j]; |
|
40 | 120 | sumq = sumq + pow(sortdata[j], 2); |
|
41 | 121 | if (j > nums_min) { |
|
42 | 122 | rtest = (double)j/(j-1) + 1/navg; |
|
43 | 123 | if ((sumq*j) > (rtest*pow(sump, 2))) { |
|
44 | 124 | j = j - 1; |
|
45 | 125 | sump = sump - sortdata[j]; |
|
46 | 126 | sumq = sumq - pow(sortdata[j],2); |
|
47 | 127 | cont = 0; |
|
48 | 128 | } |
|
49 | 129 | } |
|
50 | 130 | j = j + 1; |
|
51 | 131 | } |
|
52 | 132 | |
|
53 | 133 | double lnoise = sump / j; |
|
54 | 134 | |
|
55 | 135 | Py_DECREF(data_array); |
|
56 | 136 | |
|
57 | 137 | return Py_BuildValue("d", lnoise); |
|
58 | 138 | } |
|
139 |
@@ -1,349 +1,348 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: murco $ |
|
4 | 4 | $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $ |
|
5 | 5 | ''' |
|
6 | 6 | import inspect |
|
7 | 7 | from fuzzywuzzy import process |
|
8 | 8 | |
|
9 | 9 | def checkKwargs(method, kwargs): |
|
10 | 10 | currentKwargs = kwargs |
|
11 | 11 | choices = inspect.getargspec(method).args |
|
12 | 12 | try: |
|
13 | 13 | choices.remove('self') |
|
14 | 14 | except Exception as e: |
|
15 | 15 | pass |
|
16 | 16 | |
|
17 | 17 | try: |
|
18 | 18 | choices.remove('dataOut') |
|
19 | 19 | except Exception as e: |
|
20 | 20 | pass |
|
21 | 21 | |
|
22 | 22 | for kwarg in kwargs: |
|
23 | 23 | fuzz = process.extractOne(kwarg, choices) |
|
24 | 24 | if fuzz is None: |
|
25 | 25 | continue |
|
26 | 26 | if fuzz[1] < 100: |
|
27 | 27 | raise Exception('\x1b[0;32;40mDid you mean {} instead of {} in {}? \x1b[0m'. |
|
28 | 28 | format(fuzz[0], kwarg, method.__self__.__class__.__name__)) |
|
29 | 29 | |
|
30 | 30 | class ProcessingUnit(object): |
|
31 | 31 | |
|
32 | 32 | """ |
|
33 | 33 | Esta es la clase base para el procesamiento de datos. |
|
34 | 34 | |
|
35 | 35 | Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser: |
|
36 | 36 | - Metodos internos (callMethod) |
|
37 | 37 | - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos |
|
38 | 38 | tienen que ser agreagados con el metodo "add". |
|
39 | 39 | |
|
40 | 40 | """ |
|
41 | 41 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
42 | 42 | dataIn = None |
|
43 | 43 | dataInList = [] |
|
44 | 44 | |
|
45 | 45 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
46 | 46 | dataOut = None |
|
47 | 47 | |
|
48 | 48 | operations2RunDict = None |
|
49 | 49 | |
|
50 | 50 | isConfig = False |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def __init__(self, *args, **kwargs): |
|
54 | 54 | |
|
55 | 55 | self.dataIn = None |
|
56 | 56 | self.dataInList = [] |
|
57 | 57 | |
|
58 | 58 | self.dataOut = None |
|
59 | 59 | |
|
60 | 60 | self.operations2RunDict = {} |
|
61 | 61 | self.operationKwargs = {} |
|
62 | 62 | |
|
63 | 63 | self.isConfig = False |
|
64 | 64 | |
|
65 | 65 | self.args = args |
|
66 | 66 | self.kwargs = kwargs |
|
67 | 67 | checkKwargs(self.run, kwargs) |
|
68 | 68 | |
|
69 | 69 | def getAllowedArgs(self): |
|
70 | 70 | return inspect.getargspec(self.run).args |
|
71 | 71 | |
|
72 | 72 | def addOperationKwargs(self, objId, **kwargs): |
|
73 | 73 | ''' |
|
74 | 74 | ''' |
|
75 | 75 | |
|
76 | 76 | self.operationKwargs[objId] = kwargs |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def addOperation(self, opObj, objId): |
|
80 | 80 | |
|
81 | 81 | """ |
|
82 | 82 | Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el |
|
83 | 83 | identificador asociado a este objeto. |
|
84 | 84 | |
|
85 | 85 | Input: |
|
86 | 86 | |
|
87 | 87 | object : objeto de la clase "Operation" |
|
88 | 88 | |
|
89 | 89 | Return: |
|
90 | 90 | |
|
91 | 91 | objId : identificador del objeto, necesario para ejecutar la operacion |
|
92 | 92 | """ |
|
93 | 93 | |
|
94 | 94 | self.operations2RunDict[objId] = opObj |
|
95 | 95 | |
|
96 | 96 | return objId |
|
97 | 97 | |
|
98 | 98 | def getOperationObj(self, objId): |
|
99 | 99 | |
|
100 | 100 | if objId not in self.operations2RunDict.keys(): |
|
101 | 101 | return None |
|
102 | 102 | |
|
103 | 103 | return self.operations2RunDict[objId] |
|
104 | 104 | |
|
105 | 105 | def operation(self, **kwargs): |
|
106 | 106 | |
|
107 | 107 | """ |
|
108 | 108 | Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los |
|
109 | 109 | atributos del objeto dataOut |
|
110 | 110 | |
|
111 | 111 | Input: |
|
112 | 112 | |
|
113 | 113 | **kwargs : Diccionario de argumentos de la funcion a ejecutar |
|
114 | 114 | """ |
|
115 | 115 | |
|
116 | 116 | raise NotImplementedError |
|
117 | 117 | |
|
118 | 118 | def callMethod(self, name, opId): |
|
119 | 119 | |
|
120 | 120 | """ |
|
121 | 121 | Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase. |
|
122 | 122 | |
|
123 | 123 | Input: |
|
124 | 124 | name : nombre del metodo a ejecutar |
|
125 | 125 | |
|
126 | 126 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
127 | 127 | |
|
128 | 128 | """ |
|
129 | 129 | |
|
130 | 130 | #Checking the inputs |
|
131 | 131 | if name == 'run': |
|
132 | 132 | |
|
133 | 133 | if not self.checkInputs(): |
|
134 | 134 | self.dataOut.flagNoData = True |
|
135 | 135 | return False |
|
136 | 136 | else: |
|
137 | 137 | #Si no es un metodo RUN la entrada es la misma dataOut (interna) |
|
138 | 138 | if self.dataOut is not None and self.dataOut.isEmpty(): |
|
139 | 139 | return False |
|
140 | 140 | |
|
141 | 141 | #Getting the pointer to method |
|
142 | 142 | methodToCall = getattr(self, name) |
|
143 | 143 | |
|
144 | 144 | #Executing the self method |
|
145 | 145 | |
|
146 | 146 | if hasattr(self, 'mp'): |
|
147 | 147 | if name=='run': |
|
148 | 148 | if self.mp is False: |
|
149 | 149 | self.mp = True |
|
150 | 150 | self.start() |
|
151 | 151 | else: |
|
152 | 152 | self.operationKwargs[opId]['parent'] = self.kwargs |
|
153 | 153 | methodToCall(**self.operationKwargs[opId]) |
|
154 | 154 | else: |
|
155 | 155 | if name=='run': |
|
156 | 156 | methodToCall(**self.kwargs) |
|
157 | 157 | else: |
|
158 | 158 | methodToCall(**self.operationKwargs[opId]) |
|
159 | 159 | |
|
160 | 160 | if self.dataOut is None: |
|
161 | 161 | return False |
|
162 | 162 | |
|
163 | 163 | if self.dataOut.isEmpty(): |
|
164 | 164 | return False |
|
165 | 165 | |
|
166 | 166 | return True |
|
167 | 167 | |
|
168 | 168 | def callObject(self, objId): |
|
169 | 169 | |
|
170 | 170 | """ |
|
171 | 171 | Ejecuta la operacion asociada al identificador del objeto "objId" |
|
172 | 172 | |
|
173 | 173 | Input: |
|
174 | 174 | |
|
175 | 175 | objId : identificador del objeto a ejecutar |
|
176 | 176 | |
|
177 | 177 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
178 | 178 | |
|
179 | 179 | Return: |
|
180 | 180 | |
|
181 | 181 | None |
|
182 | 182 | """ |
|
183 | 183 | |
|
184 | 184 | if self.dataOut is not None and self.dataOut.isEmpty(): |
|
185 | 185 | return False |
|
186 | 186 | |
|
187 | 187 | externalProcObj = self.operations2RunDict[objId] |
|
188 | 188 | |
|
189 | 189 | if hasattr(externalProcObj, 'mp'): |
|
190 | 190 | if externalProcObj.mp is False: |
|
191 | 191 | externalProcObj.kwargs['parent'] = self.kwargs |
|
192 | 192 | self.operationKwargs[objId] = externalProcObj.kwargs |
|
193 | 193 | externalProcObj.mp = True |
|
194 | 194 | externalProcObj.start() |
|
195 | 195 | else: |
|
196 | 196 | externalProcObj.run(self.dataOut, **externalProcObj.kwargs) |
|
197 | 197 | self.operationKwargs[objId] = externalProcObj.kwargs |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | return True |
|
201 | 201 | |
|
202 | 202 | def call(self, opType, opName=None, opId=None): |
|
203 | ||
|
204 | 203 | """ |
|
205 | 204 | Return True si ejecuta la operacion interna nombrada "opName" o la operacion externa |
|
206 | 205 | identificada con el id "opId"; con los argumentos "**kwargs". |
|
207 | 206 | |
|
208 | 207 | False si la operacion no se ha ejecutado. |
|
209 | 208 | |
|
210 | 209 | Input: |
|
211 | 210 | |
|
212 | 211 | opType : Puede ser "self" o "external" |
|
213 | 212 | |
|
214 | 213 | Depende del tipo de operacion para llamar a:callMethod or callObject: |
|
215 | 214 | |
|
216 | 215 | 1. If opType = "self": Llama a un metodo propio de esta clase: |
|
217 | 216 | |
|
218 | 217 | name_method = getattr(self, name) |
|
219 | 218 | name_method(**kwargs) |
|
220 | 219 | |
|
221 | 220 | |
|
222 | 221 | 2. If opType = "other" o"external": Llama al metodo "run()" de una instancia de la |
|
223 | 222 | clase "Operation" o de un derivado de ella: |
|
224 | 223 | |
|
225 | 224 | instanceName = self.operationList[opId] |
|
226 | 225 | instanceName.run(**kwargs) |
|
227 | 226 | |
|
228 | 227 | opName : Si la operacion es interna (opType = 'self'), entonces el "opName" sera |
|
229 | 228 | usada para llamar a un metodo interno de la clase Processing |
|
230 | 229 | |
|
231 | 230 | opId : Si la operacion es externa (opType = 'other' o 'external), entonces el |
|
232 | 231 | "opId" sera usada para llamar al metodo "run" de la clase Operation |
|
233 | 232 | registrada anteriormente con ese Id |
|
234 | 233 | |
|
235 | 234 | Exception: |
|
236 | 235 | Este objeto de tipo Operation debe de haber sido agregado antes con el metodo: |
|
237 | 236 | "addOperation" e identificado con el valor "opId" = el id de la operacion. |
|
238 | 237 | De lo contrario retornara un error del tipo ValueError |
|
239 | 238 | |
|
240 | 239 | """ |
|
241 | 240 | |
|
242 | 241 | if opType == 'self': |
|
243 | 242 | |
|
244 | 243 | if not opName: |
|
245 | 244 | raise ValueError, "opName parameter should be defined" |
|
246 | 245 | |
|
247 | 246 | sts = self.callMethod(opName, opId) |
|
248 | 247 | |
|
249 | 248 | elif opType == 'other' or opType == 'external' or opType == 'plotter': |
|
250 | 249 | |
|
251 | 250 | if not opId: |
|
252 | 251 | raise ValueError, "opId parameter should be defined" |
|
253 | 252 | |
|
254 | 253 | if opId not in self.operations2RunDict.keys(): |
|
255 | 254 | raise ValueError, "Any operation with id=%s has been added" %str(opId) |
|
256 | 255 | |
|
257 | 256 | sts = self.callObject(opId) |
|
258 | 257 | |
|
259 | 258 | else: |
|
260 | 259 | raise ValueError, "opType should be 'self', 'external' or 'plotter'; and not '%s'" %opType |
|
261 | 260 | |
|
262 | 261 | return sts |
|
263 | 262 | |
|
264 | 263 | def setInput(self, dataIn): |
|
265 | 264 | |
|
266 | 265 | self.dataIn = dataIn |
|
267 | 266 | self.dataInList.append(dataIn) |
|
268 | 267 | |
|
269 | 268 | def getOutputObj(self): |
|
270 | 269 | |
|
271 | 270 | return self.dataOut |
|
272 | 271 | |
|
273 | 272 | def checkInputs(self): |
|
274 | 273 | |
|
275 | 274 | for thisDataIn in self.dataInList: |
|
276 | 275 | |
|
277 | 276 | if thisDataIn.isEmpty(): |
|
278 | 277 | return False |
|
279 | 278 | |
|
280 | 279 | return True |
|
281 | 280 | |
|
282 | 281 | def setup(self): |
|
283 | 282 | |
|
284 | 283 | raise NotImplementedError |
|
285 | 284 | |
|
286 | 285 | def run(self): |
|
287 | 286 | |
|
288 | 287 | raise NotImplementedError |
|
289 | 288 | |
|
290 | 289 | def close(self): |
|
291 | 290 | #Close every thread, queue or any other object here is it is neccesary. |
|
292 | 291 | return |
|
293 | 292 | |
|
294 | 293 | class Operation(object): |
|
295 | 294 | |
|
296 | 295 | """ |
|
297 | 296 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit |
|
298 | 297 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de |
|
299 | 298 | acumulacion dentro de esta clase |
|
300 | 299 | |
|
301 | 300 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) |
|
302 | 301 | |
|
303 | 302 | """ |
|
304 | 303 | |
|
305 | 304 | __buffer = None |
|
306 | 305 | isConfig = False |
|
307 | 306 | |
|
308 | 307 | def __init__(self, **kwargs): |
|
309 | 308 | |
|
310 | 309 | self.__buffer = None |
|
311 | 310 | self.isConfig = False |
|
312 | 311 | self.kwargs = kwargs |
|
313 | 312 | checkKwargs(self.run, kwargs) |
|
314 | 313 | |
|
315 | 314 | def getAllowedArgs(self): |
|
316 | 315 | return inspect.getargspec(self.run).args |
|
317 | 316 | |
|
318 | 317 | def setup(self): |
|
319 | 318 | |
|
320 | 319 | self.isConfig = True |
|
321 | 320 | |
|
322 | 321 | raise NotImplementedError |
|
323 | 322 | |
|
324 | 323 | def run(self, dataIn, **kwargs): |
|
325 | 324 | |
|
326 | 325 | """ |
|
327 | 326 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los |
|
328 | 327 | atributos del objeto dataIn. |
|
329 | 328 | |
|
330 | 329 | Input: |
|
331 | 330 | |
|
332 | 331 | dataIn : objeto del tipo JROData |
|
333 | 332 | |
|
334 | 333 | Return: |
|
335 | 334 | |
|
336 | 335 | None |
|
337 | 336 | |
|
338 | 337 | Affected: |
|
339 | 338 | __buffer : buffer de recepcion de datos. |
|
340 | 339 | |
|
341 | 340 | """ |
|
342 | 341 | if not self.isConfig: |
|
343 | 342 | self.setup(**kwargs) |
|
344 | 343 | |
|
345 | 344 | raise NotImplementedError |
|
346 | 345 | |
|
347 | 346 | def close(self): |
|
348 | 347 | |
|
349 | 348 | pass |
@@ -1,178 +1,178 | |||
|
1 | 1 | import numpy |
|
2 | 2 | |
|
3 | 3 | from jroproc_base import ProcessingUnit, Operation |
|
4 | 4 | from schainpy.model.data.jrodata import Correlation, hildebrand_sekhon |
|
5 | 5 | |
|
6 | 6 | class CorrelationProc(ProcessingUnit): |
|
7 | 7 | |
|
8 | 8 | pairsList = None |
|
9 | 9 | |
|
10 | 10 | data_cf = None |
|
11 | 11 | |
|
12 | 12 | def __init__(self, **kwargs): |
|
13 | 13 | |
|
14 | 14 | ProcessingUnit.__init__(self, **kwargs) |
|
15 | 15 | |
|
16 | 16 | self.objectDict = {} |
|
17 | 17 | self.buffer = None |
|
18 | 18 | self.firstdatatime = None |
|
19 | 19 | self.profIndex = 0 |
|
20 | 20 | self.dataOut = Correlation() |
|
21 | 21 | |
|
22 | 22 | def __updateObjFromVoltage(self): |
|
23 | 23 | |
|
24 | 24 | self.dataOut.timeZone = self.dataIn.timeZone |
|
25 | 25 | self.dataOut.dstFlag = self.dataIn.dstFlag |
|
26 | 26 | self.dataOut.errorCount = self.dataIn.errorCount |
|
27 | 27 | self.dataOut.useLocalTime = self.dataIn.useLocalTime |
|
28 | 28 | |
|
29 | 29 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() |
|
30 | 30 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() |
|
31 | 31 | self.dataOut.channelList = self.dataIn.channelList |
|
32 | 32 | self.dataOut.heightList = self.dataIn.heightList |
|
33 | 33 | self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
34 | 34 | # self.dataOut.nHeights = self.dataIn.nHeights |
|
35 | 35 | # self.dataOut.nChannels = self.dataIn.nChannels |
|
36 | 36 | self.dataOut.nBaud = self.dataIn.nBaud |
|
37 | 37 | self.dataOut.nCode = self.dataIn.nCode |
|
38 | 38 | self.dataOut.code = self.dataIn.code |
|
39 | 39 | # self.dataOut.nProfiles = self.dataOut.nFFTPoints |
|
40 | 40 | self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock |
|
41 | 41 | self.dataOut.utctime = self.firstdatatime |
|
42 | 42 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada |
|
43 | 43 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip |
|
44 | 44 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
45 | 45 | # self.dataOut.nIncohInt = 1 |
|
46 | 46 | self.dataOut.ippSeconds = self.dataIn.ippSeconds |
|
47 | 47 | self.dataOut.nProfiles = self.dataIn.nProfiles |
|
48 | 48 | self.dataOut.utctime = self.dataIn.utctime |
|
49 | 49 | # self.dataOut.windowOfFilter = self.dataIn.windowOfFilter |
|
50 | 50 | |
|
51 | 51 | # self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nPoints |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def removeDC(self, jspectra): |
|
55 | 55 | |
|
56 | 56 | nChannel = jspectra.shape[0] |
|
57 | 57 | |
|
58 | 58 | for i in range(nChannel): |
|
59 | 59 | jspectra_tmp = jspectra[i,:,:] |
|
60 | 60 | jspectra_DC = numpy.mean(jspectra_tmp,axis = 0) |
|
61 | 61 | |
|
62 | 62 | jspectra_tmp = jspectra_tmp - jspectra_DC |
|
63 | 63 | jspectra[i,:,:] = jspectra_tmp |
|
64 | 64 | |
|
65 | 65 | return jspectra |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | def removeNoise(self, mode = 2): |
|
69 | 69 | indR = numpy.where(self.dataOut.lagR == 0)[0][0] |
|
70 | 70 | indT = numpy.where(self.dataOut.lagT == 0)[0][0] |
|
71 | 71 | |
|
72 | 72 | jspectra = self.dataOut.data_corr[:,:,indR,:] |
|
73 | 73 | |
|
74 | 74 | num_chan = jspectra.shape[0] |
|
75 | 75 | num_hei = jspectra.shape[2] |
|
76 | 76 | |
|
77 | 77 | freq_dc = indT |
|
78 | 78 | ind_vel = numpy.array([-2,-1,1,2]) + freq_dc |
|
79 | 79 | |
|
80 | 80 | NPot = self.dataOut.getNoise(mode) |
|
81 | 81 | jspectra[:,freq_dc,:] = jspectra[:,freq_dc,:] - NPot |
|
82 | 82 | SPot = jspectra[:,freq_dc,:] |
|
83 | 83 | pairsAutoCorr = self.dataOut.getPairsAutoCorr() |
|
84 | 84 | # self.dataOut.signalPotency = SPot |
|
85 | 85 | self.dataOut.noise = NPot |
|
86 | 86 | self.dataOut.SNR = (SPot/NPot)[pairsAutoCorr] |
|
87 | 87 | self.dataOut.data_corr[:,:,indR,:] = jspectra |
|
88 | 88 | |
|
89 | 89 | return 1 |
|
90 | 90 | |
|
91 | 91 | def run(self, lags=None, mode = 'time', pairsList=None, fullBuffer=False, nAvg = 1, removeDC = False, splitCF=False): |
|
92 | 92 | |
|
93 | 93 | self.dataOut.flagNoData = True |
|
94 | 94 | |
|
95 | 95 | if self.dataIn.type == "Correlation": |
|
96 | 96 | |
|
97 | 97 | self.dataOut.copy(self.dataIn) |
|
98 | 98 | |
|
99 | 99 | return |
|
100 | 100 | |
|
101 | 101 | if self.dataIn.type == "Voltage": |
|
102 | 102 | |
|
103 | 103 | nChannels = self.dataIn.nChannels |
|
104 | 104 | nProfiles = self.dataIn.nProfiles |
|
105 | 105 | nHeights = self.dataIn.nHeights |
|
106 | 106 | data_pre = self.dataIn.data |
|
107 | 107 | |
|
108 | 108 | #--------------- Remover DC ------------ |
|
109 | 109 | if removeDC: |
|
110 | 110 | data_pre = self.removeDC(data_pre) |
|
111 | 111 | |
|
112 | 112 | #--------------------------------------------- |
|
113 | 113 | # pairsList = list(ccfList) |
|
114 | 114 | # for i in acfList: |
|
115 | 115 | # pairsList.append((i,i)) |
|
116 | 116 | # |
|
117 | 117 | # ccf_pairs = numpy.arange(len(ccfList)) |
|
118 | 118 | # acf_pairs = numpy.arange(len(ccfList),len(pairsList)) |
|
119 | 119 | self.__updateObjFromVoltage() |
|
120 | 120 | #---------------------------------------------------------------------- |
|
121 | 121 | #Creating temporal buffers |
|
122 | 122 | if fullBuffer: |
|
123 | 123 | tmp = numpy.zeros((len(pairsList), len(lags), nProfiles, nHeights), dtype = 'complex')*numpy.nan |
|
124 | 124 | elif mode == 'time': |
|
125 | 125 | if lags == None: |
|
126 | 126 | lags = numpy.arange(-nProfiles+1, nProfiles) |
|
127 | 127 | tmp = numpy.zeros((len(pairsList), len(lags), nHeights),dtype='complex') |
|
128 | 128 | elif mode == 'height': |
|
129 | 129 | if lags == None: |
|
130 | 130 | lags = numpy.arange(-nHeights+1, nHeights) |
|
131 | 131 | tmp = numpy.zeros(len(pairsList), (len(lags), nProfiles),dtype='complex') |
|
132 | 132 | |
|
133 | 133 | #For loop |
|
134 | 134 | for l in range(len(pairsList)): |
|
135 | 135 | |
|
136 | 136 | ch0 = pairsList[l][0] |
|
137 | 137 | ch1 = pairsList[l][1] |
|
138 | 138 | |
|
139 | 139 | for i in range(len(lags)): |
|
140 | 140 | idx = lags[i] |
|
141 | 141 | |
|
142 | 142 | if idx >= 0: |
|
143 | 143 | if mode == 'time': |
|
144 | 144 | ccf0 = data_pre[ch0,:nProfiles-idx,:]*numpy.conj(data_pre[ch1,idx:,:]) #time |
|
145 | 145 | else: |
|
146 | 146 | ccf0 = data_pre[ch0,:,nHeights-idx]*numpy.conj(data_pre[ch1,:,idx:]) #heights |
|
147 | 147 | else: |
|
148 | 148 | if mode == 'time': |
|
149 | 149 | ccf0 = data_pre[ch0,-idx:,:]*numpy.conj(data_pre[ch1,:nProfiles+idx,:]) #time |
|
150 | 150 | else: |
|
151 | 151 | ccf0 = data_pre[ch0,:,-idx:]*numpy.conj(data_pre[ch1,:,:nHeights+idx]) #heights |
|
152 | 152 | |
|
153 | 153 | if fullBuffer: |
|
154 | 154 | tmp[l,i,:ccf0.shape[0],:] = ccf0 |
|
155 | 155 | else: |
|
156 | 156 | tmp[l,i,:] = numpy.sum(ccf0, axis=0) |
|
157 | 157 | |
|
158 | 158 | #----------------------------------------------------------------- |
|
159 | 159 | if fullBuffer: |
|
160 | 160 | tmp = numpy.sum(numpy.reshape(tmp,(tmp.shape[0],tmp.shape[1],tmp.shape[2]/nAvg,nAvg,tmp.shape[3])),axis=3) |
|
161 | 161 | self.dataOut.nAvg = nAvg |
|
162 | 162 | |
|
163 | 163 | self.dataOut.data_cf = tmp |
|
164 | 164 | self.dataOut.mode = mode |
|
165 | 165 | self.dataOut.nLags = len(lags) |
|
166 | 166 | self.dataOut.pairsList = pairsList |
|
167 | 167 | self.dataOut.nPairs = len(pairsList) |
|
168 | 168 | |
|
169 | 169 | #Se Calcula los factores de Normalizacion |
|
170 | 170 | if mode == 'time': |
|
171 | 171 | delta = self.dataIn.ippSeconds*self.dataIn.nCohInt |
|
172 | 172 | else: |
|
173 | 173 | delta = self.dataIn.heightList[1] - self.dataIn.heightList[0] |
|
174 | 174 | self.dataOut.lagRange = numpy.array(lags)*delta |
|
175 | 175 | # self.dataOut.nCohInt = self.dataIn.nCohInt*nAvg |
|
176 | 176 | self.dataOut.flagNoData = False |
|
177 | a = self.dataOut.normFactor | |
|
177 | # a = self.dataOut.normFactor | |
|
178 | 178 | return |
@@ -1,345 +1,344 | |||
|
1 | 1 | import numpy |
|
2 | 2 | |
|
3 | 3 | from jroproc_base import ProcessingUnit, Operation |
|
4 | 4 | from schainpy.model.data.jrodata import SpectraHeis |
|
5 | 5 | |
|
6 | 6 | class SpectraHeisProc(ProcessingUnit): |
|
7 | 7 | |
|
8 | 8 | def __init__(self, **kwargs): |
|
9 | 9 | |
|
10 | 10 | ProcessingUnit.__init__(self, **kwargs) |
|
11 | 11 | |
|
12 | 12 | # self.buffer = None |
|
13 | 13 | # self.firstdatatime = None |
|
14 | 14 | # self.profIndex = 0 |
|
15 | 15 | self.dataOut = SpectraHeis() |
|
16 | 16 | |
|
17 | 17 | def __updateObjFromVoltage(self): |
|
18 | 18 | |
|
19 | 19 | self.dataOut.timeZone = self.dataIn.timeZone |
|
20 | 20 | self.dataOut.dstFlag = self.dataIn.dstFlag |
|
21 | 21 | self.dataOut.errorCount = self.dataIn.errorCount |
|
22 | 22 | self.dataOut.useLocalTime = self.dataIn.useLocalTime |
|
23 | 23 | |
|
24 | 24 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()# |
|
25 | 25 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()# |
|
26 | 26 | self.dataOut.channelList = self.dataIn.channelList |
|
27 | 27 | self.dataOut.heightList = self.dataIn.heightList |
|
28 | 28 | # self.dataOut.dtype = self.dataIn.dtype |
|
29 | 29 | self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
30 | 30 | # self.dataOut.nHeights = self.dataIn.nHeights |
|
31 | 31 | # self.dataOut.nChannels = self.dataIn.nChannels |
|
32 | 32 | self.dataOut.nBaud = self.dataIn.nBaud |
|
33 | 33 | self.dataOut.nCode = self.dataIn.nCode |
|
34 | 34 | self.dataOut.code = self.dataIn.code |
|
35 | 35 | # self.dataOut.nProfiles = 1 |
|
36 | 36 | self.dataOut.ippFactor = 1 |
|
37 | 37 | self.dataOut.noise_estimation = None |
|
38 | 38 | # self.dataOut.nProfiles = self.dataOut.nFFTPoints |
|
39 | 39 | self.dataOut.nFFTPoints = self.dataIn.nHeights |
|
40 | 40 | # self.dataOut.channelIndexList = self.dataIn.channelIndexList |
|
41 | 41 | # self.dataOut.flagNoData = self.dataIn.flagNoData |
|
42 | 42 | self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock |
|
43 | 43 | self.dataOut.utctime = self.dataIn.utctime |
|
44 | 44 | # self.dataOut.utctime = self.firstdatatime |
|
45 | 45 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada |
|
46 | 46 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip |
|
47 | 47 | # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT |
|
48 | 48 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
49 | 49 | self.dataOut.nIncohInt = 1 |
|
50 | 50 | # self.dataOut.ippSeconds= self.dataIn.ippSeconds |
|
51 | 51 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter |
|
52 | 52 | |
|
53 | 53 | # self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nIncohInt |
|
54 | 54 | # self.dataOut.set=self.dataIn.set |
|
55 | 55 | # self.dataOut.deltaHeight=self.dataIn.deltaHeight |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def __updateObjFromFits(self): |
|
59 | 59 | |
|
60 | 60 | self.dataOut.utctime = self.dataIn.utctime |
|
61 | 61 | # self.dataOut.channelIndexList = self.dataIn.channelIndexList |
|
62 | 62 | |
|
63 | 63 | self.dataOut.channelList = self.dataIn.channelList |
|
64 | 64 | self.dataOut.heightList = self.dataIn.heightList |
|
65 | 65 | self.dataOut.data_spc = self.dataIn.data |
|
66 | 66 | self.dataOut.ippSeconds = self.dataIn.ippSeconds |
|
67 | 67 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
68 | 68 | self.dataOut.nIncohInt = self.dataIn.nIncohInt |
|
69 | 69 | # self.dataOut.timeInterval = self.dataIn.timeInterval |
|
70 | 70 | self.dataOut.timeZone = self.dataIn.timeZone |
|
71 | 71 | self.dataOut.useLocalTime = True |
|
72 | 72 | # self.dataOut. |
|
73 | 73 | # self.dataOut. |
|
74 | 74 | |
|
75 | 75 | def __getFft(self): |
|
76 | 76 | |
|
77 | 77 | fft_volt = numpy.fft.fft(self.dataIn.data, axis=1) |
|
78 | 78 | fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,)) |
|
79 | 79 | spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))/(self.dataOut.nFFTPoints) |
|
80 | 80 | self.dataOut.data_spc = spc |
|
81 | 81 | |
|
82 | 82 | def run(self): |
|
83 | 83 | |
|
84 | 84 | self.dataOut.flagNoData = True |
|
85 | 85 | |
|
86 | 86 | if self.dataIn.type == "Fits": |
|
87 | 87 | self.__updateObjFromFits() |
|
88 | 88 | self.dataOut.flagNoData = False |
|
89 | 89 | return |
|
90 | 90 | |
|
91 | 91 | if self.dataIn.type == "SpectraHeis": |
|
92 | 92 | self.dataOut.copy(self.dataIn) |
|
93 | 93 | return |
|
94 | 94 | |
|
95 | 95 | if self.dataIn.type == "Voltage": |
|
96 | 96 | self.__updateObjFromVoltage() |
|
97 | 97 | self.__getFft() |
|
98 | 98 | self.dataOut.flagNoData = False |
|
99 | 99 | |
|
100 | 100 | return |
|
101 | 101 | |
|
102 | 102 | raise ValueError, "The type object %s is not valid"%(self.dataIn.type) |
|
103 | 103 | |
|
104 | 104 | |
|
105 | 105 | def selectChannels(self, channelList): |
|
106 | 106 | |
|
107 | 107 | channelIndexList = [] |
|
108 | 108 | |
|
109 | 109 | for channel in channelList: |
|
110 | 110 | index = self.dataOut.channelList.index(channel) |
|
111 | 111 | channelIndexList.append(index) |
|
112 | 112 | |
|
113 | 113 | self.selectChannelsByIndex(channelIndexList) |
|
114 | 114 | |
|
115 | 115 | def selectChannelsByIndex(self, channelIndexList): |
|
116 | 116 | """ |
|
117 | 117 | Selecciona un bloque de datos en base a canales segun el channelIndexList |
|
118 | 118 | |
|
119 | 119 | Input: |
|
120 | 120 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] |
|
121 | 121 | |
|
122 | 122 | Affected: |
|
123 | 123 | self.dataOut.data |
|
124 | 124 | self.dataOut.channelIndexList |
|
125 | 125 | self.dataOut.nChannels |
|
126 | 126 | self.dataOut.m_ProcessingHeader.totalSpectra |
|
127 | 127 | self.dataOut.systemHeaderObj.numChannels |
|
128 | 128 | self.dataOut.m_ProcessingHeader.blockSize |
|
129 | 129 | |
|
130 | 130 | Return: |
|
131 | 131 | None |
|
132 | 132 | """ |
|
133 | 133 | |
|
134 | 134 | for channelIndex in channelIndexList: |
|
135 | 135 | if channelIndex not in self.dataOut.channelIndexList: |
|
136 | 136 | print channelIndexList |
|
137 | 137 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex |
|
138 | 138 | |
|
139 | 139 | # nChannels = len(channelIndexList) |
|
140 | 140 | |
|
141 | 141 | data_spc = self.dataOut.data_spc[channelIndexList,:] |
|
142 | 142 | |
|
143 | 143 | self.dataOut.data_spc = data_spc |
|
144 | 144 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] |
|
145 | 145 | |
|
146 | 146 | return 1 |
|
147 | 147 | |
|
148 | 148 | class IncohInt4SpectraHeis(Operation): |
|
149 | 149 | |
|
150 | 150 | isConfig = False |
|
151 | 151 | |
|
152 | 152 | __profIndex = 0 |
|
153 | 153 | __withOverapping = False |
|
154 | 154 | |
|
155 | 155 | __byTime = False |
|
156 | 156 | __initime = None |
|
157 | 157 | __lastdatatime = None |
|
158 | 158 | __integrationtime = None |
|
159 | 159 | |
|
160 | 160 | __buffer = None |
|
161 | 161 | |
|
162 | 162 | __dataReady = False |
|
163 | 163 | |
|
164 | 164 | n = None |
|
165 | 165 | |
|
166 | ||
|
167 | 166 | def __init__(self, **kwargs): |
|
168 | 167 | |
|
169 | 168 | Operation.__init__(self, **kwargs) |
|
170 | 169 | # self.isConfig = False |
|
171 | 170 | |
|
172 | 171 | def setup(self, n=None, timeInterval=None, overlapping=False): |
|
173 | 172 | """ |
|
174 | 173 | Set the parameters of the integration class. |
|
175 | 174 | |
|
176 | 175 | Inputs: |
|
177 | 176 | |
|
178 | 177 | n : Number of coherent integrations |
|
179 | 178 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
180 | 179 | overlapping : |
|
181 | 180 | |
|
182 | 181 | """ |
|
183 | 182 | |
|
184 | 183 | self.__initime = None |
|
185 | 184 | self.__lastdatatime = 0 |
|
186 | 185 | self.__buffer = None |
|
187 | 186 | self.__dataReady = False |
|
188 | 187 | |
|
189 | 188 | |
|
190 | 189 | if n == None and timeInterval == None: |
|
191 | 190 | raise ValueError, "n or timeInterval should be specified ..." |
|
192 | 191 | |
|
193 | 192 | if n != None: |
|
194 | 193 | self.n = n |
|
195 | 194 | self.__byTime = False |
|
196 | 195 | else: |
|
197 | 196 | self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line |
|
198 | 197 | self.n = 9999 |
|
199 | 198 | self.__byTime = True |
|
200 | 199 | |
|
201 | 200 | if overlapping: |
|
202 | 201 | self.__withOverapping = True |
|
203 | 202 | self.__buffer = None |
|
204 | 203 | else: |
|
205 | 204 | self.__withOverapping = False |
|
206 | 205 | self.__buffer = 0 |
|
207 | 206 | |
|
208 | 207 | self.__profIndex = 0 |
|
209 | 208 | |
|
210 | 209 | def putData(self, data): |
|
211 | 210 | |
|
212 | 211 | """ |
|
213 | 212 | Add a profile to the __buffer and increase in one the __profileIndex |
|
214 | 213 | |
|
215 | 214 | """ |
|
216 | 215 | |
|
217 | 216 | if not self.__withOverapping: |
|
218 | 217 | self.__buffer += data.copy() |
|
219 | 218 | self.__profIndex += 1 |
|
220 | 219 | return |
|
221 | 220 | |
|
222 | 221 | #Overlapping data |
|
223 | 222 | nChannels, nHeis = data.shape |
|
224 | 223 | data = numpy.reshape(data, (1, nChannels, nHeis)) |
|
225 | 224 | |
|
226 | 225 | #If the buffer is empty then it takes the data value |
|
227 | 226 | if self.__buffer is None: |
|
228 | 227 | self.__buffer = data |
|
229 | 228 | self.__profIndex += 1 |
|
230 | 229 | return |
|
231 | 230 | |
|
232 | 231 | #If the buffer length is lower than n then stakcing the data value |
|
233 | 232 | if self.__profIndex < self.n: |
|
234 | 233 | self.__buffer = numpy.vstack((self.__buffer, data)) |
|
235 | 234 | self.__profIndex += 1 |
|
236 | 235 | return |
|
237 | 236 | |
|
238 | 237 | #If the buffer length is equal to n then replacing the last buffer value with the data value |
|
239 | 238 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) |
|
240 | 239 | self.__buffer[self.n-1] = data |
|
241 | 240 | self.__profIndex = self.n |
|
242 | 241 | return |
|
243 | 242 | |
|
244 | 243 | |
|
245 | 244 | def pushData(self): |
|
246 | 245 | """ |
|
247 | 246 | Return the sum of the last profiles and the profiles used in the sum. |
|
248 | 247 | |
|
249 | 248 | Affected: |
|
250 | 249 | |
|
251 | 250 | self.__profileIndex |
|
252 | 251 | |
|
253 | 252 | """ |
|
254 | 253 | |
|
255 | 254 | if not self.__withOverapping: |
|
256 | 255 | data = self.__buffer |
|
257 | 256 | n = self.__profIndex |
|
258 | 257 | |
|
259 | 258 | self.__buffer = 0 |
|
260 | 259 | self.__profIndex = 0 |
|
261 | 260 | |
|
262 | 261 | return data, n |
|
263 | 262 | |
|
264 | 263 | #Integration with Overlapping |
|
265 | 264 | data = numpy.sum(self.__buffer, axis=0) |
|
266 | 265 | n = self.__profIndex |
|
267 | 266 | |
|
268 | 267 | return data, n |
|
269 | 268 | |
|
270 | 269 | def byProfiles(self, data): |
|
271 | 270 | |
|
272 | 271 | self.__dataReady = False |
|
273 | 272 | avgdata = None |
|
274 | 273 | # n = None |
|
275 | 274 | |
|
276 | 275 | self.putData(data) |
|
277 | 276 | |
|
278 | 277 | if self.__profIndex == self.n: |
|
279 | 278 | |
|
280 | 279 | avgdata, n = self.pushData() |
|
281 | 280 | self.__dataReady = True |
|
282 | 281 | |
|
283 | 282 | return avgdata |
|
284 | 283 | |
|
285 | 284 | def byTime(self, data, datatime): |
|
286 | 285 | |
|
287 | 286 | self.__dataReady = False |
|
288 | 287 | avgdata = None |
|
289 | 288 | n = None |
|
290 | 289 | |
|
291 | 290 | self.putData(data) |
|
292 | 291 | |
|
293 | 292 | if (datatime - self.__initime) >= self.__integrationtime: |
|
294 | 293 | avgdata, n = self.pushData() |
|
295 | 294 | self.n = n |
|
296 | 295 | self.__dataReady = True |
|
297 | 296 | |
|
298 | 297 | return avgdata |
|
299 | 298 | |
|
300 | 299 | def integrate(self, data, datatime=None): |
|
301 | 300 | |
|
302 | 301 | if self.__initime == None: |
|
303 | 302 | self.__initime = datatime |
|
304 | 303 | |
|
305 | 304 | if self.__byTime: |
|
306 | 305 | avgdata = self.byTime(data, datatime) |
|
307 | 306 | else: |
|
308 | 307 | avgdata = self.byProfiles(data) |
|
309 | 308 | |
|
310 | 309 | |
|
311 | 310 | self.__lastdatatime = datatime |
|
312 | 311 | |
|
313 | 312 | if avgdata is None: |
|
314 | 313 | return None, None |
|
315 | 314 | |
|
316 | 315 | avgdatatime = self.__initime |
|
317 | 316 | |
|
318 | 317 | deltatime = datatime -self.__lastdatatime |
|
319 | 318 | |
|
320 | 319 | if not self.__withOverapping: |
|
321 | 320 | self.__initime = datatime |
|
322 | 321 | else: |
|
323 | 322 | self.__initime += deltatime |
|
324 | 323 | |
|
325 | 324 | return avgdata, avgdatatime |
|
326 | 325 | |
|
327 | def run(self, dataOut, **kwargs): | |
|
326 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False, **kwargs): | |
|
328 | 327 | |
|
329 | 328 | if not self.isConfig: |
|
330 | self.setup(**kwargs) | |
|
329 | self.setup(n=n, timeInterval=timeInterval, overlapping=overlapping) | |
|
331 | 330 | self.isConfig = True |
|
332 | 331 | |
|
333 | 332 | avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime) |
|
334 | 333 | |
|
335 | 334 | # dataOut.timeInterval *= n |
|
336 | 335 | dataOut.flagNoData = True |
|
337 | 336 | |
|
338 | 337 | if self.__dataReady: |
|
339 | 338 | dataOut.data_spc = avgdata |
|
340 | 339 | dataOut.nIncohInt *= self.n |
|
341 | 340 | # dataOut.nCohInt *= self.n |
|
342 | 341 | dataOut.utctime = avgdatatime |
|
343 | 342 | # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt |
|
344 | 343 | # dataOut.timeInterval = self.__timeInterval*self.n |
|
345 | 344 | dataOut.flagNoData = False |
|
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: file renamed from schainpy/trash to schainpy/utils/trash |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
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