##// END OF EJS Templates
Miguel Valdez -
r803:3f5bf501c4e1
parent child
Show More
@@ -1,73 +1,74
1 VERSIONS:
1 VERSIONS:
2
2
3 2.1.2:
3 2.1.2:
4 -jroutils_ftp.py: Bug fixed, Any error sending file stopped the Server Thread
4 -jroutils_ftp.py: Bug fixed, Any error sending file stopped the Server Thread
5 Server thread opens and closes remote server each time file list is sent
5 Server thread opens and closes remote server each time file list is sent
6 -jroplot_spectra.py: Noise path was not being created when noise data is saved.
6 -jroplot_spectra.py: Noise path was not being created when noise data is saved.
7 -jroIO_base.py: startTime can be greater than endTime. Example: SpreadF [18:00 - 07:00]
7 -jroIO_base.py: startTime can be greater than endTime. Example: SpreadF [18:00 - 07:00]
8
8
9 2.1.3:
9 2.1.3:
10 -jroplot_heispectra.py: SpectraHeisScope was not showing the right channels
10 -jroplot_heispectra.py: SpectraHeisScope was not showing the right channels
11 -jroproc_voltage.py: Bug fixed selecting profiles (self.nProfiles took a wrong value),
11 -jroproc_voltage.py: Bug fixed selecting profiles (self.nProfiles took a wrong value),
12 Bug fixed selecting heights by block (selecting profiles instead heights)
12 Bug fixed selecting heights by block (selecting profiles instead heights)
13 -jroproc_voltage.py: New feature added: decoding data by block using FFT.
13 -jroproc_voltage.py: New feature added: decoding data by block using FFT.
14 -jroIO_heispectra.py: Bug fixed in FitsReader. Using local Fits instance instead schainpy.mode.data.jrodata.Fits.
14 -jroIO_heispectra.py: Bug fixed in FitsReader. Using local Fits instance instead schainpy.mode.data.jrodata.Fits.
15 -jroIO_heispectra.py: Channel index list does not exist.
15 -jroIO_heispectra.py: Channel index list does not exist.
16
16
17 2.1.3.1:
17 2.1.3.1:
18 -GUI: every icon were resized
18 -GUI: every icon were resized
19 -jroproc_voltage.py: Print a message when "Read from code" option is selected and the code is not defined inside data file
19 -jroproc_voltage.py: Print a message when "Read from code" option is selected and the code is not defined inside data file
20
20
21 2.1.3.2:
21 2.1.3.2:
22 -GUI: user interaction enhanced
22 -GUI: user interaction enhanced
23 -controller_api.py: Safe access to ControllerThead
23 -controller_api.py: Safe access to ControllerThead
24
24
25 2.1.3.3:
25 2.1.3.3:
26 -Colored Button Icons were added to GUI
26 -Colored Button Icons were added to GUI
27
27
28 2.1.4:
28 2.1.4:
29 -Sending error notifications to signal chain administrator
29 -Sending error notifications to signal chain administrator
30 -Login to email server added
30 -Login to email server added
31
31
32 2.1.4.1:
32 2.1.4.1:
33 -Send notifications when an error different to ValueError is detected
33 -Send notifications when an error different to ValueError is detected
34
34
35 2.1.4.2:
35 2.1.4.2:
36 -A new Plotter Class was added
36 -A new Plotter Class was added
37 -Project.start() does not accept filename as a parameter anymore
37 -Project.start() does not accept filename as a parameter anymore
38
38
39 2.1.5:
39 2.1.5:
40 -serializer module added to Signal Chain
40 -serializer module added to Signal Chain
41 -jroplotter.py added to Signal Chain
41 -jroplotter.py added to Signal Chain
42
42
43 2.2.0:
43 2.2.0:
44 -GUI: use of external plotter
44 -GUI: use of external plotter
45 -Compatible with matplotlib 1.5.0
45 -Compatible with matplotlib 1.5.0
46
46
47 2.2.1:
47 2.2.1:
48 -Bugs fixed in GUI
48 -Bugs fixed in GUI
49 -Views were improved in GUI
49 -Views were improved in GUI
50 -Support to MST-ISR experiments
50 -Support to MST-ISR experiments
51 -Bug fixed getting noise using hyldebrant. (minimum number of points > 20%)
51 -Bug fixed getting noise using hyldebrant. (minimum number of points > 20%)
52 -handleError added to jroplotter.py
52 -handleError added to jroplotter.py
53
53
54 2.2.2:
54 2.2.2:
55 -VoltageProc: ProfileSelector, Reshape, Decoder with nTxs!=1 and getblock=True was tested
55 -VoltageProc: ProfileSelector, Reshape, Decoder with nTxs!=1 and getblock=True was tested
56 -Rawdata and testRawdata.py added to Signal Chain project
56 -Rawdata and testRawdata.py added to Signal Chain project
57
57
58 2.2.3:
58 2.2.3:
59 -Bug fixed in GUI: Error getting(reading) Code value
59 -Bug fixed in GUI: Error getting(reading) Code value
60 -Bug fixed in GUI: Flip option always needs channelList field
60 -Bug fixed in GUI: Flip option always needs channelList field
61 -Bug fixed in jrodata: when one branch modified a value in "dataOut" (example: dataOut.code) this value
61 -Bug fixed in jrodata: when one branch modified a value in "dataOut" (example: dataOut.code) this value
62 was modified for every branch (because this was a reference). It was modified in data.copy()
62 was modified for every branch (because this was a reference). It was modified in data.copy()
63 -Bug fixed in jroproc_voltage.profileSelector(): rangeList replaces to profileRangeList.
63 -Bug fixed in jroproc_voltage.profileSelector(): rangeList replaces to profileRangeList.
64
64
65
65
66 2.2.3.1:
66 2.2.3.1:
67 -Filtering block by time has been added.
67 -Filtering block by time has been added.
68 -Bug fixed plotting RTI, CoherenceMap and others using xmin and xmax parameters. The first day worked
68 -Bug fixed plotting RTI, CoherenceMap and others using xmin and xmax parameters. The first day worked
69 properly but the next days did not.
69 properly but the next days did not.
70
70
71 2.2.4:
71 2.2.4:
72 -jroproc_spectra_lags.py added to schainpy
72 -jroproc_spectra_lags.py added to schainpy
73 -Bug fixed in schainGUI: project created the same procUnit in some cases.
73 -Bug fixed in schainGUI: ProcUnit was created with the same id in some cases.
74 -Bug fixed in jroHeaderIO: Header size validation.
@@ -1,7 +1,7
1 '''
1 '''
2 Created on Feb 7, 2012
2 Created on Feb 7, 2012
3
3
4 @author $Author$
4 @author $Author$
5 @version $Id$
5 @version $Id$
6 '''
6 '''
7 __version__ = "2.2.3.3" No newline at end of file
7 __version__ = "2.2.4" No newline at end of file
@@ -1,758 +1,762
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 '''
5 '''
6 import sys
6 import sys
7 import numpy
7 import numpy
8 import copy
8 import copy
9 import datetime
9 import datetime
10
10
11 SPEED_OF_LIGHT = 299792458
11 SPEED_OF_LIGHT = 299792458
12 SPEED_OF_LIGHT = 3e8
12 SPEED_OF_LIGHT = 3e8
13
13
14 BASIC_STRUCTURE = numpy.dtype([
14 BASIC_STRUCTURE = numpy.dtype([
15 ('nSize','<u4'),
15 ('nSize','<u4'),
16 ('nVersion','<u2'),
16 ('nVersion','<u2'),
17 ('nDataBlockId','<u4'),
17 ('nDataBlockId','<u4'),
18 ('nUtime','<u4'),
18 ('nUtime','<u4'),
19 ('nMilsec','<u2'),
19 ('nMilsec','<u2'),
20 ('nTimezone','<i2'),
20 ('nTimezone','<i2'),
21 ('nDstflag','<i2'),
21 ('nDstflag','<i2'),
22 ('nErrorCount','<u4')
22 ('nErrorCount','<u4')
23 ])
23 ])
24
24
25 SYSTEM_STRUCTURE = numpy.dtype([
25 SYSTEM_STRUCTURE = numpy.dtype([
26 ('nSize','<u4'),
26 ('nSize','<u4'),
27 ('nNumSamples','<u4'),
27 ('nNumSamples','<u4'),
28 ('nNumProfiles','<u4'),
28 ('nNumProfiles','<u4'),
29 ('nNumChannels','<u4'),
29 ('nNumChannels','<u4'),
30 ('nADCResolution','<u4'),
30 ('nADCResolution','<u4'),
31 ('nPCDIOBusWidth','<u4'),
31 ('nPCDIOBusWidth','<u4'),
32 ])
32 ])
33
33
34 RADAR_STRUCTURE = numpy.dtype([
34 RADAR_STRUCTURE = numpy.dtype([
35 ('nSize','<u4'),
35 ('nSize','<u4'),
36 ('nExpType','<u4'),
36 ('nExpType','<u4'),
37 ('nNTx','<u4'),
37 ('nNTx','<u4'),
38 ('fIpp','<f4'),
38 ('fIpp','<f4'),
39 ('fTxA','<f4'),
39 ('fTxA','<f4'),
40 ('fTxB','<f4'),
40 ('fTxB','<f4'),
41 ('nNumWindows','<u4'),
41 ('nNumWindows','<u4'),
42 ('nNumTaus','<u4'),
42 ('nNumTaus','<u4'),
43 ('nCodeType','<u4'),
43 ('nCodeType','<u4'),
44 ('nLine6Function','<u4'),
44 ('nLine6Function','<u4'),
45 ('nLine5Function','<u4'),
45 ('nLine5Function','<u4'),
46 ('fClock','<f4'),
46 ('fClock','<f4'),
47 ('nPrePulseBefore','<u4'),
47 ('nPrePulseBefore','<u4'),
48 ('nPrePulseAfter','<u4'),
48 ('nPrePulseAfter','<u4'),
49 ('sRangeIPP','<a20'),
49 ('sRangeIPP','<a20'),
50 ('sRangeTxA','<a20'),
50 ('sRangeTxA','<a20'),
51 ('sRangeTxB','<a20'),
51 ('sRangeTxB','<a20'),
52 ])
52 ])
53
53
54 SAMPLING_STRUCTURE = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
54 SAMPLING_STRUCTURE = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
55
55
56
56
57 PROCESSING_STRUCTURE = numpy.dtype([
57 PROCESSING_STRUCTURE = numpy.dtype([
58 ('nSize','<u4'),
58 ('nSize','<u4'),
59 ('nDataType','<u4'),
59 ('nDataType','<u4'),
60 ('nSizeOfDataBlock','<u4'),
60 ('nSizeOfDataBlock','<u4'),
61 ('nProfilesperBlock','<u4'),
61 ('nProfilesperBlock','<u4'),
62 ('nDataBlocksperFile','<u4'),
62 ('nDataBlocksperFile','<u4'),
63 ('nNumWindows','<u4'),
63 ('nNumWindows','<u4'),
64 ('nProcessFlags','<u4'),
64 ('nProcessFlags','<u4'),
65 ('nCoherentIntegrations','<u4'),
65 ('nCoherentIntegrations','<u4'),
66 ('nIncoherentIntegrations','<u4'),
66 ('nIncoherentIntegrations','<u4'),
67 ('nTotalSpectra','<u4')
67 ('nTotalSpectra','<u4')
68 ])
68 ])
69
69
70 class Header(object):
70 class Header(object):
71
71
72 def __init__(self):
72 def __init__(self):
73 raise NotImplementedError
73 raise NotImplementedError
74
74
75 def copy(self):
75 def copy(self):
76 return copy.deepcopy(self)
76 return copy.deepcopy(self)
77
77
78 def read(self):
78 def read(self):
79
79
80 raise NotImplementedError
80 raise NotImplementedError
81
81
82 def write(self):
82 def write(self):
83
83
84 raise NotImplementedError
84 raise NotImplementedError
85
85
86 def printInfo(self):
86 def printInfo(self):
87
87
88 message = "#"*50 + "\n"
88 message = "#"*50 + "\n"
89 message += self.__class__.__name__.upper() + "\n"
89 message += self.__class__.__name__.upper() + "\n"
90 message += "#"*50 + "\n"
90 message += "#"*50 + "\n"
91
91
92 keyList = self.__dict__.keys()
92 keyList = self.__dict__.keys()
93 keyList.sort()
93 keyList.sort()
94
94
95 for key in keyList:
95 for key in keyList:
96 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
96 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
97
97
98 if "size" not in keyList:
98 if "size" not in keyList:
99 attr = getattr(self, "size")
99 attr = getattr(self, "size")
100
100
101 if attr:
101 if attr:
102 message += "%s = %s" %("size", attr) + "\n"
102 message += "%s = %s" %("size", attr) + "\n"
103
103
104 print message
104 print message
105
105
106 class BasicHeader(Header):
106 class BasicHeader(Header):
107
107
108 size = None
108 size = None
109 version = None
109 version = None
110 dataBlock = None
110 dataBlock = None
111 utc = None
111 utc = None
112 ltc = None
112 ltc = None
113 miliSecond = None
113 miliSecond = None
114 timeZone = None
114 timeZone = None
115 dstFlag = None
115 dstFlag = None
116 errorCount = None
116 errorCount = None
117 datatime = None
117 datatime = None
118
118
119 __LOCALTIME = None
119 __LOCALTIME = None
120
120
121 def __init__(self, useLocalTime=True):
121 def __init__(self, useLocalTime=True):
122
122
123 self.size = 24
123 self.size = 24
124 self.version = 0
124 self.version = 0
125 self.dataBlock = 0
125 self.dataBlock = 0
126 self.utc = 0
126 self.utc = 0
127 self.miliSecond = 0
127 self.miliSecond = 0
128 self.timeZone = 0
128 self.timeZone = 0
129 self.dstFlag = 0
129 self.dstFlag = 0
130 self.errorCount = 0
130 self.errorCount = 0
131
131
132 self.useLocalTime = useLocalTime
132 self.useLocalTime = useLocalTime
133
133
134 def read(self, fp):
134 def read(self, fp):
135
135
136 try:
136 try:
137 header = numpy.fromfile(fp, BASIC_STRUCTURE,1)
137 header = numpy.fromfile(fp, BASIC_STRUCTURE,1)
138
138
139 except Exception, e:
139 except Exception, e:
140 print "BasicHeader: "
140 print "BasicHeader: "
141 print e
141 print e
142 return 0
142 return 0
143
143
144 self.size = int(header['nSize'][0])
144 self.size = int(header['nSize'][0])
145 self.version = int(header['nVersion'][0])
145 self.version = int(header['nVersion'][0])
146 self.dataBlock = int(header['nDataBlockId'][0])
146 self.dataBlock = int(header['nDataBlockId'][0])
147 self.utc = int(header['nUtime'][0])
147 self.utc = int(header['nUtime'][0])
148 self.miliSecond = int(header['nMilsec'][0])
148 self.miliSecond = int(header['nMilsec'][0])
149 self.timeZone = int(header['nTimezone'][0])
149 self.timeZone = int(header['nTimezone'][0])
150 self.dstFlag = int(header['nDstflag'][0])
150 self.dstFlag = int(header['nDstflag'][0])
151 self.errorCount = int(header['nErrorCount'][0])
151 self.errorCount = int(header['nErrorCount'][0])
152
153 if self.size < 24:
154 return 0
152
155
153 return 1
156 return 1
154
157
155 def write(self, fp):
158 def write(self, fp):
156
159
157 headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount)
160 headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount)
158 header = numpy.array(headerTuple, BASIC_STRUCTURE)
161 header = numpy.array(headerTuple, BASIC_STRUCTURE)
159 header.tofile(fp)
162 header.tofile(fp)
160
163
161 return 1
164 return 1
162
165
163 def get_ltc(self):
166 def get_ltc(self):
164
167
165 return self.utc - self.timeZone*60
168 return self.utc - self.timeZone*60
166
169
167 def set_ltc(self, value):
170 def set_ltc(self, value):
168
171
169 self.utc = value + self.timeZone*60
172 self.utc = value + self.timeZone*60
170
173
171 def get_datatime(self):
174 def get_datatime(self):
172
175
173 return datetime.datetime.utcfromtimestamp(self.ltc)
176 return datetime.datetime.utcfromtimestamp(self.ltc)
174
177
175 ltc = property(get_ltc, set_ltc)
178 ltc = property(get_ltc, set_ltc)
176 datatime = property(get_datatime)
179 datatime = property(get_datatime)
177
180
178 class SystemHeader(Header):
181 class SystemHeader(Header):
179
182
180 size = None
183 size = None
181 nSamples = None
184 nSamples = None
182 nProfiles = None
185 nProfiles = None
183 nChannels = None
186 nChannels = None
184 adcResolution = None
187 adcResolution = None
185 pciDioBusWidth = None
188 pciDioBusWidth = None
186
189
187 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWith=0):
190 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWith=0):
188
191
189 self.size = 24
192 self.size = 24
190 self.nSamples = nSamples
193 self.nSamples = nSamples
191 self.nProfiles = nProfiles
194 self.nProfiles = nProfiles
192 self.nChannels = nChannels
195 self.nChannels = nChannels
193 self.adcResolution = adcResolution
196 self.adcResolution = adcResolution
194 self.pciDioBusWidth = pciDioBusWith
197 self.pciDioBusWidth = pciDioBusWith
195
198
196 def read(self, fp):
199 def read(self, fp):
197
200
198 startFp = fp.tell()
201 startFp = fp.tell()
199
202
200 try:
203 try:
201 header = numpy.fromfile(fp,SYSTEM_STRUCTURE,1)
204 header = numpy.fromfile(fp,SYSTEM_STRUCTURE,1)
202 except Exception, e:
205 except Exception, e:
203 print "System Header: " + e
206 print "System Header: " + e
204 return 0
207 return 0
205
208
206 self.size = header['nSize'][0]
209 self.size = header['nSize'][0]
207 self.nSamples = header['nNumSamples'][0]
210 self.nSamples = header['nNumSamples'][0]
208 self.nProfiles = header['nNumProfiles'][0]
211 self.nProfiles = header['nNumProfiles'][0]
209 self.nChannels = header['nNumChannels'][0]
212 self.nChannels = header['nNumChannels'][0]
210 self.adcResolution = header['nADCResolution'][0]
213 self.adcResolution = header['nADCResolution'][0]
211 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
214 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
212
215
213 endFp = self.size + startFp
216 endFp = self.size + startFp
214
217
215 if fp.tell() > endFp:
218 if fp.tell() > endFp:
216 sys.stderr.write("Warning: System header size is lower than it has to be")
219 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp.name)
217 return 0
220 return 0
218
221
219 if fp.tell() < endFp:
222 if fp.tell() < endFp:
220 sys.stderr.write("Warning: System header size is greater than it is considered")
223 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp.name)
221
224 return 0
225
222 return 1
226 return 1
223
227
224 def write(self, fp):
228 def write(self, fp):
225
229
226 headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth)
230 headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth)
227 header = numpy.array(headerTuple,SYSTEM_STRUCTURE)
231 header = numpy.array(headerTuple,SYSTEM_STRUCTURE)
228 header.tofile(fp)
232 header.tofile(fp)
229
233
230 return 1
234 return 1
231
235
232 class RadarControllerHeader(Header):
236 class RadarControllerHeader(Header):
233
237
234 expType = None
238 expType = None
235 nTx = None
239 nTx = None
236 ipp = None
240 ipp = None
237 txA = None
241 txA = None
238 txB = None
242 txB = None
239 nWindows = None
243 nWindows = None
240 numTaus = None
244 numTaus = None
241 codeType = None
245 codeType = None
242 line6Function = None
246 line6Function = None
243 line5Function = None
247 line5Function = None
244 fClock = None
248 fClock = None
245 prePulseBefore = None
249 prePulseBefore = None
246 prePulserAfter = None
250 prePulserAfter = None
247 rangeIpp = None
251 rangeIpp = None
248 rangeTxA = None
252 rangeTxA = None
249 rangeTxB = None
253 rangeTxB = None
250
254
251 __size = None
255 __size = None
252
256
253 def __init__(self, expType=2, nTx=1,
257 def __init__(self, expType=2, nTx=1,
254 ippKm=None, txA=0, txB=0,
258 ippKm=None, txA=0, txB=0,
255 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
259 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
256 numTaus=0, line6Function=0, line5Function=0, fClock=None,
260 numTaus=0, line6Function=0, line5Function=0, fClock=None,
257 prePulseBefore=0, prePulseAfter=0,
261 prePulseBefore=0, prePulseAfter=0,
258 codeType=0, nCode=0, nBaud=0, code=None,
262 codeType=0, nCode=0, nBaud=0, code=None,
259 flip1=0, flip2=0):
263 flip1=0, flip2=0):
260
264
261 # self.size = 116
265 # self.size = 116
262 self.expType = expType
266 self.expType = expType
263 self.nTx = nTx
267 self.nTx = nTx
264 self.ipp = ippKm
268 self.ipp = ippKm
265 self.txA = txA
269 self.txA = txA
266 self.txB = txB
270 self.txB = txB
267 self.rangeIpp = ippKm
271 self.rangeIpp = ippKm
268 self.rangeTxA = txA
272 self.rangeTxA = txA
269 self.rangeTxB = txB
273 self.rangeTxB = txB
270
274
271 self.nWindows = nWindows
275 self.nWindows = nWindows
272 self.numTaus = numTaus
276 self.numTaus = numTaus
273 self.codeType = codeType
277 self.codeType = codeType
274 self.line6Function = line6Function
278 self.line6Function = line6Function
275 self.line5Function = line5Function
279 self.line5Function = line5Function
276 self.fClock = fClock
280 self.fClock = fClock
277 self.prePulseBefore = prePulseBefore
281 self.prePulseBefore = prePulseBefore
278 self.prePulserAfter = prePulseAfter
282 self.prePulserAfter = prePulseAfter
279
283
280 self.nHeights = nHeights
284 self.nHeights = nHeights
281 self.firstHeight = firstHeight
285 self.firstHeight = firstHeight
282 self.deltaHeight = deltaHeight
286 self.deltaHeight = deltaHeight
283 self.samplesWin = nHeights
287 self.samplesWin = nHeights
284
288
285 self.nCode = nCode
289 self.nCode = nCode
286 self.nBaud = nBaud
290 self.nBaud = nBaud
287 self.code = code
291 self.code = code
288 self.flip1 = flip1
292 self.flip1 = flip1
289 self.flip2 = flip2
293 self.flip2 = flip2
290
294
291 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
295 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
292 # self.dynamic = numpy.array([],numpy.dtype('byte'))
296 # self.dynamic = numpy.array([],numpy.dtype('byte'))
293
297
294 if self.fClock is None and self.deltaHeight is not None:
298 if self.fClock is None and self.deltaHeight is not None:
295 self.fClock = 0.15/(deltaHeight*1e-6) #0.15Km / (height * 1u)
299 self.fClock = 0.15/(deltaHeight*1e-6) #0.15Km / (height * 1u)
296
300
297 def read(self, fp):
301 def read(self, fp):
298
302
299
303
300 startFp = fp.tell()
304 startFp = fp.tell()
301 try:
305 try:
302 header = numpy.fromfile(fp,RADAR_STRUCTURE,1)
306 header = numpy.fromfile(fp,RADAR_STRUCTURE,1)
303 except Exception, e:
307 except Exception, e:
304 print "RadarControllerHeader: " + e
308 print "RadarControllerHeader: " + e
305 return 0
309 return 0
306
310
307 size = int(header['nSize'][0])
311 size = int(header['nSize'][0])
308 self.expType = int(header['nExpType'][0])
312 self.expType = int(header['nExpType'][0])
309 self.nTx = int(header['nNTx'][0])
313 self.nTx = int(header['nNTx'][0])
310 self.ipp = float(header['fIpp'][0])
314 self.ipp = float(header['fIpp'][0])
311 self.txA = float(header['fTxA'][0])
315 self.txA = float(header['fTxA'][0])
312 self.txB = float(header['fTxB'][0])
316 self.txB = float(header['fTxB'][0])
313 self.nWindows = int(header['nNumWindows'][0])
317 self.nWindows = int(header['nNumWindows'][0])
314 self.numTaus = int(header['nNumTaus'][0])
318 self.numTaus = int(header['nNumTaus'][0])
315 self.codeType = int(header['nCodeType'][0])
319 self.codeType = int(header['nCodeType'][0])
316 self.line6Function = int(header['nLine6Function'][0])
320 self.line6Function = int(header['nLine6Function'][0])
317 self.line5Function = int(header['nLine5Function'][0])
321 self.line5Function = int(header['nLine5Function'][0])
318 self.fClock = float(header['fClock'][0])
322 self.fClock = float(header['fClock'][0])
319 self.prePulseBefore = int(header['nPrePulseBefore'][0])
323 self.prePulseBefore = int(header['nPrePulseBefore'][0])
320 self.prePulserAfter = int(header['nPrePulseAfter'][0])
324 self.prePulserAfter = int(header['nPrePulseAfter'][0])
321 self.rangeIpp = header['sRangeIPP'][0]
325 self.rangeIpp = header['sRangeIPP'][0]
322 self.rangeTxA = header['sRangeTxA'][0]
326 self.rangeTxA = header['sRangeTxA'][0]
323 self.rangeTxB = header['sRangeTxB'][0]
327 self.rangeTxB = header['sRangeTxB'][0]
324
328
325 samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows)
329 samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows)
326
330
327 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
331 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
328 self.firstHeight = samplingWindow['h0']
332 self.firstHeight = samplingWindow['h0']
329 self.deltaHeight = samplingWindow['dh']
333 self.deltaHeight = samplingWindow['dh']
330 self.samplesWin = samplingWindow['nsa']
334 self.samplesWin = samplingWindow['nsa']
331
335
332 self.Taus = numpy.fromfile(fp,'<f4',self.numTaus)
336 self.Taus = numpy.fromfile(fp,'<f4',self.numTaus)
333
337
334 self.code_size = 0
338 self.code_size = 0
335 if self.codeType != 0:
339 if self.codeType != 0:
336 self.nCode = int(numpy.fromfile(fp,'<u4',1))
340 self.nCode = int(numpy.fromfile(fp,'<u4',1))
337 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
341 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
338
342
339 code = numpy.empty([self.nCode,self.nBaud],dtype='i1')
343 code = numpy.empty([self.nCode,self.nBaud],dtype='i1')
340 for ic in range(self.nCode):
344 for ic in range(self.nCode):
341 temp = numpy.fromfile(fp,'u4',int(numpy.ceil(self.nBaud/32.)))
345 temp = numpy.fromfile(fp,'u4',int(numpy.ceil(self.nBaud/32.)))
342 for ib in range(self.nBaud-1,-1,-1):
346 for ib in range(self.nBaud-1,-1,-1):
343 code[ic,ib] = temp[ib/32]%2
347 code[ic,ib] = temp[ib/32]%2
344 temp[ib/32] = temp[ib/32]/2
348 temp[ib/32] = temp[ib/32]/2
345
349
346 self.code = 2.0*code - 1.0
350 self.code = 2.0*code - 1.0
347 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
351 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
348
352
349 # if self.line5Function == RCfunction.FLIP:
353 # if self.line5Function == RCfunction.FLIP:
350 # self.flip1 = numpy.fromfile(fp,'<u4',1)
354 # self.flip1 = numpy.fromfile(fp,'<u4',1)
351 #
355 #
352 # if self.line6Function == RCfunction.FLIP:
356 # if self.line6Function == RCfunction.FLIP:
353 # self.flip2 = numpy.fromfile(fp,'<u4',1)
357 # self.flip2 = numpy.fromfile(fp,'<u4',1)
354
358
355 endFp = size + startFp
359 endFp = size + startFp
356
360
357 if fp.tell() != endFp:
361 if fp.tell() != endFp:
358 # fp.seek(endFp)
362 # fp.seek(endFp)
359 print "Radar Controller Header is not consistent read[%d] != header[%d]" %(fp.tell()-startFp,endFp)
363 print "%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" %(fp.name, fp.tell()-startFp, size)
360 # return 0
364 # return 0
361
365
362 if fp.tell() > endFp:
366 if fp.tell() > endFp:
363 sys.stderr.write("Warning: Radar Controller header size is lower than it has to be")
367 sys.stderr.write("Warning %s: Size value read from Radar Controller header is lower than it has to be\n" %fp.name)
364 # return 0
368 # return 0
365
369
366 if fp.tell() < endFp:
370 if fp.tell() < endFp:
367 sys.stderr.write("Warning: Radar Controller header size is greater than it is considered")
371 sys.stderr.write("Warning %s: Size value read from Radar Controller header is greater than it has to be\n" %fp.name)
368
372
369
373
370 return 1
374 return 1
371
375
372 def write(self, fp):
376 def write(self, fp):
373
377
374 headerTuple = (self.size,
378 headerTuple = (self.size,
375 self.expType,
379 self.expType,
376 self.nTx,
380 self.nTx,
377 self.ipp,
381 self.ipp,
378 self.txA,
382 self.txA,
379 self.txB,
383 self.txB,
380 self.nWindows,
384 self.nWindows,
381 self.numTaus,
385 self.numTaus,
382 self.codeType,
386 self.codeType,
383 self.line6Function,
387 self.line6Function,
384 self.line5Function,
388 self.line5Function,
385 self.fClock,
389 self.fClock,
386 self.prePulseBefore,
390 self.prePulseBefore,
387 self.prePulserAfter,
391 self.prePulserAfter,
388 self.rangeIpp,
392 self.rangeIpp,
389 self.rangeTxA,
393 self.rangeTxA,
390 self.rangeTxB)
394 self.rangeTxB)
391
395
392 header = numpy.array(headerTuple,RADAR_STRUCTURE)
396 header = numpy.array(headerTuple,RADAR_STRUCTURE)
393 header.tofile(fp)
397 header.tofile(fp)
394
398
395 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
399 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
396 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
400 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
397 samplingWindow.tofile(fp)
401 samplingWindow.tofile(fp)
398
402
399 if self.numTaus > 0:
403 if self.numTaus > 0:
400 self.Taus.tofile(fp)
404 self.Taus.tofile(fp)
401
405
402 if self.codeType !=0:
406 if self.codeType !=0:
403 nCode = numpy.array(self.nCode, '<u4')
407 nCode = numpy.array(self.nCode, '<u4')
404 nCode.tofile(fp)
408 nCode.tofile(fp)
405 nBaud = numpy.array(self.nBaud, '<u4')
409 nBaud = numpy.array(self.nBaud, '<u4')
406 nBaud.tofile(fp)
410 nBaud.tofile(fp)
407 code1 = (self.code + 1.0)/2.
411 code1 = (self.code + 1.0)/2.
408
412
409 for ic in range(self.nCode):
413 for ic in range(self.nCode):
410 tempx = numpy.zeros(numpy.ceil(self.nBaud/32.))
414 tempx = numpy.zeros(numpy.ceil(self.nBaud/32.))
411 start = 0
415 start = 0
412 end = 32
416 end = 32
413 for i in range(len(tempx)):
417 for i in range(len(tempx)):
414 code_selected = code1[ic,start:end]
418 code_selected = code1[ic,start:end]
415 for j in range(len(code_selected)-1,-1,-1):
419 for j in range(len(code_selected)-1,-1,-1):
416 if code_selected[j] == 1:
420 if code_selected[j] == 1:
417 tempx[i] = tempx[i] + 2**(len(code_selected)-1-j)
421 tempx[i] = tempx[i] + 2**(len(code_selected)-1-j)
418 start = start + 32
422 start = start + 32
419 end = end + 32
423 end = end + 32
420
424
421 tempx = tempx.astype('u4')
425 tempx = tempx.astype('u4')
422 tempx.tofile(fp)
426 tempx.tofile(fp)
423
427
424 # if self.line5Function == RCfunction.FLIP:
428 # if self.line5Function == RCfunction.FLIP:
425 # self.flip1.tofile(fp)
429 # self.flip1.tofile(fp)
426 #
430 #
427 # if self.line6Function == RCfunction.FLIP:
431 # if self.line6Function == RCfunction.FLIP:
428 # self.flip2.tofile(fp)
432 # self.flip2.tofile(fp)
429
433
430 return 1
434 return 1
431
435
432 def get_ippSeconds(self):
436 def get_ippSeconds(self):
433 '''
437 '''
434 '''
438 '''
435 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
439 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
436
440
437 return ippSeconds
441 return ippSeconds
438
442
439 def set_ippSeconds(self, ippSeconds):
443 def set_ippSeconds(self, ippSeconds):
440 '''
444 '''
441 '''
445 '''
442
446
443 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0*1000)
447 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0*1000)
444
448
445 return
449 return
446
450
447 def get_size(self):
451 def get_size(self):
448
452
449 self.__size = 116 + 12*self.nWindows + 4*self.numTaus
453 self.__size = 116 + 12*self.nWindows + 4*self.numTaus
450
454
451 if self.codeType != 0:
455 if self.codeType != 0:
452 self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
456 self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
453
457
454 return self.__size
458 return self.__size
455
459
456 def set_size(self, value):
460 def set_size(self, value):
457
461
458 raise IOError, "size is a property and it cannot be set, just read"
462 raise IOError, "size is a property and it cannot be set, just read"
459
463
460 return
464 return
461
465
462 ippSeconds = property(get_ippSeconds, set_ippSeconds)
466 ippSeconds = property(get_ippSeconds, set_ippSeconds)
463 size = property(get_size, set_size)
467 size = property(get_size, set_size)
464
468
465 class ProcessingHeader(Header):
469 class ProcessingHeader(Header):
466
470
467 # size = None
471 # size = None
468 dtype = None
472 dtype = None
469 blockSize = None
473 blockSize = None
470 profilesPerBlock = None
474 profilesPerBlock = None
471 dataBlocksPerFile = None
475 dataBlocksPerFile = None
472 nWindows = None
476 nWindows = None
473 processFlags = None
477 processFlags = None
474 nCohInt = None
478 nCohInt = None
475 nIncohInt = None
479 nIncohInt = None
476 totalSpectra = None
480 totalSpectra = None
477
481
478 flag_dc = None
482 flag_dc = None
479 flag_cspc = None
483 flag_cspc = None
480
484
481 def __init__(self):
485 def __init__(self):
482
486
483 # self.size = 0
487 # self.size = 0
484 self.dtype = 0
488 self.dtype = 0
485 self.blockSize = 0
489 self.blockSize = 0
486 self.profilesPerBlock = 0
490 self.profilesPerBlock = 0
487 self.dataBlocksPerFile = 0
491 self.dataBlocksPerFile = 0
488 self.nWindows = 0
492 self.nWindows = 0
489 self.processFlags = 0
493 self.processFlags = 0
490 self.nCohInt = 0
494 self.nCohInt = 0
491 self.nIncohInt = 0
495 self.nIncohInt = 0
492 self.totalSpectra = 0
496 self.totalSpectra = 0
493
497
494 self.nHeights = 0
498 self.nHeights = 0
495 self.firstHeight = 0
499 self.firstHeight = 0
496 self.deltaHeight = 0
500 self.deltaHeight = 0
497 self.samplesWin = 0
501 self.samplesWin = 0
498 self.spectraComb = 0
502 self.spectraComb = 0
499 self.nCode = None
503 self.nCode = None
500 self.code = None
504 self.code = None
501 self.nBaud = None
505 self.nBaud = None
502
506
503 self.shif_fft = False
507 self.shif_fft = False
504 self.flag_dc = False
508 self.flag_dc = False
505 self.flag_cspc = False
509 self.flag_cspc = False
506 self.flag_decode = False
510 self.flag_decode = False
507 self.flag_deflip = False
511 self.flag_deflip = False
508
512
509 def read(self, fp):
513 def read(self, fp):
510
514
511 startFp = fp.tell()
515 startFp = fp.tell()
512
516
513 try:
517 try:
514 header = numpy.fromfile(fp,PROCESSING_STRUCTURE,1)
518 header = numpy.fromfile(fp,PROCESSING_STRUCTURE,1)
515 except Exception, e:
519 except Exception, e:
516 print "ProcessingHeader: " + e
520 print "ProcessingHeader: " + e
517 return 0
521 return 0
518
522
519 size = int(header['nSize'][0])
523 size = int(header['nSize'][0])
520 self.dtype = int(header['nDataType'][0])
524 self.dtype = int(header['nDataType'][0])
521 self.blockSize = int(header['nSizeOfDataBlock'][0])
525 self.blockSize = int(header['nSizeOfDataBlock'][0])
522 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
526 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
523 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
527 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
524 self.nWindows = int(header['nNumWindows'][0])
528 self.nWindows = int(header['nNumWindows'][0])
525 self.processFlags = header['nProcessFlags']
529 self.processFlags = header['nProcessFlags']
526 self.nCohInt = int(header['nCoherentIntegrations'][0])
530 self.nCohInt = int(header['nCoherentIntegrations'][0])
527 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
531 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
528 self.totalSpectra = int(header['nTotalSpectra'][0])
532 self.totalSpectra = int(header['nTotalSpectra'][0])
529
533
530 samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows)
534 samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows)
531
535
532 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
536 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
533 self.firstHeight = float(samplingWindow['h0'][0])
537 self.firstHeight = float(samplingWindow['h0'][0])
534 self.deltaHeight = float(samplingWindow['dh'][0])
538 self.deltaHeight = float(samplingWindow['dh'][0])
535 self.samplesWin = samplingWindow['nsa'][0]
539 self.samplesWin = samplingWindow['nsa'][0]
536
540
537 self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra)
541 self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra)
538
542
539 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
543 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
540 self.nCode = int(numpy.fromfile(fp,'<u4',1))
544 self.nCode = int(numpy.fromfile(fp,'<u4',1))
541 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
545 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
542 self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nCode,self.nBaud)
546 self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nCode,self.nBaud)
543
547
544 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
548 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
545 exp_name_len = int(numpy.fromfile(fp,'<u4',1))
549 exp_name_len = int(numpy.fromfile(fp,'<u4',1))
546 exp_name = numpy.fromfile(fp,'u1',exp_name_len+1)
550 exp_name = numpy.fromfile(fp,'u1',exp_name_len+1)
547
551
548 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
552 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
549 self.shif_fft = True
553 self.shif_fft = True
550 else:
554 else:
551 self.shif_fft = False
555 self.shif_fft = False
552
556
553 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
557 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
554 self.flag_dc = True
558 self.flag_dc = True
555 else:
559 else:
556 self.flag_dc = False
560 self.flag_dc = False
557
561
558 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
562 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
559 self.flag_decode = True
563 self.flag_decode = True
560 else:
564 else:
561 self.flag_decode = False
565 self.flag_decode = False
562
566
563 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
567 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
564 self.flag_deflip = True
568 self.flag_deflip = True
565 else:
569 else:
566 self.flag_deflip = False
570 self.flag_deflip = False
567
571
568 nChannels = 0
572 nChannels = 0
569 nPairs = 0
573 nPairs = 0
570 pairList = []
574 pairList = []
571
575
572 for i in range( 0, self.totalSpectra*2, 2 ):
576 for i in range( 0, self.totalSpectra*2, 2 ):
573 if self.spectraComb[i] == self.spectraComb[i+1]:
577 if self.spectraComb[i] == self.spectraComb[i+1]:
574 nChannels = nChannels + 1 #par de canales iguales
578 nChannels = nChannels + 1 #par de canales iguales
575 else:
579 else:
576 nPairs = nPairs + 1 #par de canales diferentes
580 nPairs = nPairs + 1 #par de canales diferentes
577 pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) )
581 pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) )
578
582
579 self.flag_cspc = False
583 self.flag_cspc = False
580 if nPairs > 0:
584 if nPairs > 0:
581 self.flag_cspc = True
585 self.flag_cspc = True
582
586
583 endFp = size + startFp
587 endFp = size + startFp
584
588
585 if fp.tell() > endFp:
589 if fp.tell() > endFp:
586 sys.stderr.write("Warning: Processing header size is lower than it has to be")
590 sys.stderr.write("Warning: Processing header size is lower than it has to be")
587 return 0
591 return 0
588
592
589 if fp.tell() < endFp:
593 if fp.tell() < endFp:
590 sys.stderr.write("Warning: Processing header size is greater than it is considered")
594 sys.stderr.write("Warning: Processing header size is greater than it is considered")
591
595
592 return 1
596 return 1
593
597
594 def write(self, fp):
598 def write(self, fp):
595 #Clear DEFINE_PROCESS_CODE
599 #Clear DEFINE_PROCESS_CODE
596 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
600 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
597
601
598 headerTuple = (self.size,
602 headerTuple = (self.size,
599 self.dtype,
603 self.dtype,
600 self.blockSize,
604 self.blockSize,
601 self.profilesPerBlock,
605 self.profilesPerBlock,
602 self.dataBlocksPerFile,
606 self.dataBlocksPerFile,
603 self.nWindows,
607 self.nWindows,
604 self.processFlags,
608 self.processFlags,
605 self.nCohInt,
609 self.nCohInt,
606 self.nIncohInt,
610 self.nIncohInt,
607 self.totalSpectra)
611 self.totalSpectra)
608
612
609 header = numpy.array(headerTuple,PROCESSING_STRUCTURE)
613 header = numpy.array(headerTuple,PROCESSING_STRUCTURE)
610 header.tofile(fp)
614 header.tofile(fp)
611
615
612 if self.nWindows != 0:
616 if self.nWindows != 0:
613 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
617 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
614 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
618 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
615 samplingWindow.tofile(fp)
619 samplingWindow.tofile(fp)
616
620
617 if self.totalSpectra != 0:
621 if self.totalSpectra != 0:
618 # spectraComb = numpy.array([],numpy.dtype('u1'))
622 # spectraComb = numpy.array([],numpy.dtype('u1'))
619 spectraComb = self.spectraComb
623 spectraComb = self.spectraComb
620 spectraComb.tofile(fp)
624 spectraComb.tofile(fp)
621
625
622 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
626 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
623 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
627 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
624 # nCode.tofile(fp)
628 # nCode.tofile(fp)
625 #
629 #
626 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
630 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
627 # nBaud.tofile(fp)
631 # nBaud.tofile(fp)
628 #
632 #
629 # code = self.code.reshape(self.nCode*self.nBaud)
633 # code = self.code.reshape(self.nCode*self.nBaud)
630 # code = code.astype(numpy.dtype('<f4'))
634 # code = code.astype(numpy.dtype('<f4'))
631 # code.tofile(fp)
635 # code.tofile(fp)
632
636
633 return 1
637 return 1
634
638
635 def get_size(self):
639 def get_size(self):
636
640
637 self.__size = 40 + 12*self.nWindows + 2*self.totalSpectra
641 self.__size = 40 + 12*self.nWindows + 2*self.totalSpectra
638
642
639 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
643 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
640 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
644 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
641 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
645 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
642
646
643 return self.__size
647 return self.__size
644
648
645 def set_size(self, value):
649 def set_size(self, value):
646
650
647 raise IOError, "size is a property and it cannot be set, just read"
651 raise IOError, "size is a property and it cannot be set, just read"
648
652
649 return
653 return
650
654
651 size = property(get_size, set_size)
655 size = property(get_size, set_size)
652
656
653 class RCfunction:
657 class RCfunction:
654 NONE=0
658 NONE=0
655 FLIP=1
659 FLIP=1
656 CODE=2
660 CODE=2
657 SAMPLING=3
661 SAMPLING=3
658 LIN6DIV256=4
662 LIN6DIV256=4
659 SYNCHRO=5
663 SYNCHRO=5
660
664
661 class nCodeType:
665 class nCodeType:
662 NONE=0
666 NONE=0
663 USERDEFINE=1
667 USERDEFINE=1
664 BARKER2=2
668 BARKER2=2
665 BARKER3=3
669 BARKER3=3
666 BARKER4=4
670 BARKER4=4
667 BARKER5=5
671 BARKER5=5
668 BARKER7=6
672 BARKER7=6
669 BARKER11=7
673 BARKER11=7
670 BARKER13=8
674 BARKER13=8
671 AC128=9
675 AC128=9
672 COMPLEMENTARYCODE2=10
676 COMPLEMENTARYCODE2=10
673 COMPLEMENTARYCODE4=11
677 COMPLEMENTARYCODE4=11
674 COMPLEMENTARYCODE8=12
678 COMPLEMENTARYCODE8=12
675 COMPLEMENTARYCODE16=13
679 COMPLEMENTARYCODE16=13
676 COMPLEMENTARYCODE32=14
680 COMPLEMENTARYCODE32=14
677 COMPLEMENTARYCODE64=15
681 COMPLEMENTARYCODE64=15
678 COMPLEMENTARYCODE128=16
682 COMPLEMENTARYCODE128=16
679 CODE_BINARY28=17
683 CODE_BINARY28=17
680
684
681 class PROCFLAG:
685 class PROCFLAG:
682
686
683 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
687 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
684 DECODE_DATA = numpy.uint32(0x00000002)
688 DECODE_DATA = numpy.uint32(0x00000002)
685 SPECTRA_CALC = numpy.uint32(0x00000004)
689 SPECTRA_CALC = numpy.uint32(0x00000004)
686 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
690 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
687 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
691 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
688 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
692 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
689
693
690 DATATYPE_CHAR = numpy.uint32(0x00000040)
694 DATATYPE_CHAR = numpy.uint32(0x00000040)
691 DATATYPE_SHORT = numpy.uint32(0x00000080)
695 DATATYPE_SHORT = numpy.uint32(0x00000080)
692 DATATYPE_LONG = numpy.uint32(0x00000100)
696 DATATYPE_LONG = numpy.uint32(0x00000100)
693 DATATYPE_INT64 = numpy.uint32(0x00000200)
697 DATATYPE_INT64 = numpy.uint32(0x00000200)
694 DATATYPE_FLOAT = numpy.uint32(0x00000400)
698 DATATYPE_FLOAT = numpy.uint32(0x00000400)
695 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
699 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
696
700
697 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
701 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
698 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
702 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
699 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
703 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
700
704
701 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
705 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
702 DEFLIP_DATA = numpy.uint32(0x00010000)
706 DEFLIP_DATA = numpy.uint32(0x00010000)
703 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
707 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
704
708
705 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
709 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
706 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
710 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
707 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
711 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
708 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
712 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
709 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
713 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
710
714
711 EXP_NAME_ESP = numpy.uint32(0x00200000)
715 EXP_NAME_ESP = numpy.uint32(0x00200000)
712 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
716 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
713
717
714 OPERATION_MASK = numpy.uint32(0x0000003F)
718 OPERATION_MASK = numpy.uint32(0x0000003F)
715 DATATYPE_MASK = numpy.uint32(0x00000FC0)
719 DATATYPE_MASK = numpy.uint32(0x00000FC0)
716 DATAARRANGE_MASK = numpy.uint32(0x00007000)
720 DATAARRANGE_MASK = numpy.uint32(0x00007000)
717 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
721 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
718
722
719 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
723 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
720 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
724 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
721 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
725 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
722 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
726 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
723 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
727 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
724 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
728 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
725
729
726 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
730 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
727
731
728 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
732 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
729 PROCFLAG.DATATYPE_SHORT,
733 PROCFLAG.DATATYPE_SHORT,
730 PROCFLAG.DATATYPE_LONG,
734 PROCFLAG.DATATYPE_LONG,
731 PROCFLAG.DATATYPE_INT64,
735 PROCFLAG.DATATYPE_INT64,
732 PROCFLAG.DATATYPE_FLOAT,
736 PROCFLAG.DATATYPE_FLOAT,
733 PROCFLAG.DATATYPE_DOUBLE]
737 PROCFLAG.DATATYPE_DOUBLE]
734
738
735 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
739 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
736
740
737 def get_dtype_index(numpy_dtype):
741 def get_dtype_index(numpy_dtype):
738
742
739 index = None
743 index = None
740
744
741 for i in range(len(NUMPY_DTYPE_LIST)):
745 for i in range(len(NUMPY_DTYPE_LIST)):
742 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
746 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
743 index = i
747 index = i
744 break
748 break
745
749
746 return index
750 return index
747
751
748 def get_numpy_dtype(index):
752 def get_numpy_dtype(index):
749
753
750 return NUMPY_DTYPE_LIST[index]
754 return NUMPY_DTYPE_LIST[index]
751
755
752 def get_procflag_dtype(index):
756 def get_procflag_dtype(index):
753
757
754 return PROCFLAG_DTYPE_LIST[index]
758 return PROCFLAG_DTYPE_LIST[index]
755
759
756 def get_dtype_width(index):
760 def get_dtype_width(index):
757
761
758 return DTYPE_WIDTH[index] No newline at end of file
762 return DTYPE_WIDTH[index]
@@ -1,1704 +1,1709
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import time, datetime
12 import time, datetime
13 #import h5py
13 #import h5py
14 import traceback
14 import traceback
15
15
16 try:
16 try:
17 from gevent import sleep
17 from gevent import sleep
18 except:
18 except:
19 from time import sleep
19 from time import sleep
20
20
21 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
21 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
22 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
23
23
24 LOCALTIME = True
24 LOCALTIME = True
25
25
26 def isNumber(cad):
26 def isNumber(cad):
27 """
27 """
28 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
28 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
29
29
30 Excepciones:
30 Excepciones:
31 Si un determinado string no puede ser convertido a numero
31 Si un determinado string no puede ser convertido a numero
32 Input:
32 Input:
33 str, string al cual se le analiza para determinar si convertible a un numero o no
33 str, string al cual se le analiza para determinar si convertible a un numero o no
34
34
35 Return:
35 Return:
36 True : si el string es uno numerico
36 True : si el string es uno numerico
37 False : no es un string numerico
37 False : no es un string numerico
38 """
38 """
39 try:
39 try:
40 float( cad )
40 float( cad )
41 return True
41 return True
42 except:
42 except:
43 return False
43 return False
44
44
45 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
45 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
46 """
46 """
47 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
47 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
48
48
49 Inputs:
49 Inputs:
50 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
50 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
51
51
52 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
52 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
53 segundos contados desde 01/01/1970.
53 segundos contados desde 01/01/1970.
54 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
54 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
55 segundos contados desde 01/01/1970.
55 segundos contados desde 01/01/1970.
56
56
57 Return:
57 Return:
58 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
58 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
59 fecha especificado, de lo contrario retorna False.
59 fecha especificado, de lo contrario retorna False.
60
60
61 Excepciones:
61 Excepciones:
62 Si el archivo no existe o no puede ser abierto
62 Si el archivo no existe o no puede ser abierto
63 Si la cabecera no puede ser leida.
63 Si la cabecera no puede ser leida.
64
64
65 """
65 """
66 basicHeaderObj = BasicHeader(LOCALTIME)
66 basicHeaderObj = BasicHeader(LOCALTIME)
67
67
68 try:
68 try:
69 fp = open(filename,'rb')
69 fp = open(filename,'rb')
70 except IOError:
70 except IOError:
71 print "The file %s can't be opened" %(filename)
71 print "The file %s can't be opened" %(filename)
72 return 0
72 return 0
73
73
74 sts = basicHeaderObj.read(fp)
74 sts = basicHeaderObj.read(fp)
75 fp.close()
75 fp.close()
76
76
77 if not(sts):
77 if not(sts):
78 print "Skipping the file %s because it has not a valid header" %(filename)
78 print "Skipping the file %s because it has not a valid header" %(filename)
79 return 0
79 return 0
80
80
81 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
81 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
82 return 0
82 return 0
83
83
84 return 1
84 return 1
85
85
86 def isTimeInRange(thisTime, startTime, endTime):
86 def isTimeInRange(thisTime, startTime, endTime):
87
87
88 if endTime >= startTime:
88 if endTime >= startTime:
89 if (thisTime < startTime) or (thisTime > endTime):
89 if (thisTime < startTime) or (thisTime > endTime):
90 return 0
90 return 0
91
91
92 return 1
92 return 1
93 else:
93 else:
94 if (thisTime < startTime) and (thisTime > endTime):
94 if (thisTime < startTime) and (thisTime > endTime):
95 return 0
95 return 0
96
96
97 return 1
97 return 1
98
98
99 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
99 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
100 """
100 """
101 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
101 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
102
102
103 Inputs:
103 Inputs:
104 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
104 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
105
105
106 startDate : fecha inicial del rango seleccionado en formato datetime.date
106 startDate : fecha inicial del rango seleccionado en formato datetime.date
107
107
108 endDate : fecha final del rango seleccionado en formato datetime.date
108 endDate : fecha final del rango seleccionado en formato datetime.date
109
109
110 startTime : tiempo inicial del rango seleccionado en formato datetime.time
110 startTime : tiempo inicial del rango seleccionado en formato datetime.time
111
111
112 endTime : tiempo final del rango seleccionado en formato datetime.time
112 endTime : tiempo final del rango seleccionado en formato datetime.time
113
113
114 Return:
114 Return:
115 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
115 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
116 fecha especificado, de lo contrario retorna False.
116 fecha especificado, de lo contrario retorna False.
117
117
118 Excepciones:
118 Excepciones:
119 Si el archivo no existe o no puede ser abierto
119 Si el archivo no existe o no puede ser abierto
120 Si la cabecera no puede ser leida.
120 Si la cabecera no puede ser leida.
121
121
122 """
122 """
123
123
124
124
125 try:
125 try:
126 fp = open(filename,'rb')
126 fp = open(filename,'rb')
127 except IOError:
127 except IOError:
128 print "The file %s can't be opened" %(filename)
128 print "The file %s can't be opened" %(filename)
129 return None
129 return None
130
130
131 firstBasicHeaderObj = BasicHeader(LOCALTIME)
131 firstBasicHeaderObj = BasicHeader(LOCALTIME)
132 systemHeaderObj = SystemHeader()
132 systemHeaderObj = SystemHeader()
133 radarControllerHeaderObj = RadarControllerHeader()
133 radarControllerHeaderObj = RadarControllerHeader()
134 processingHeaderObj = ProcessingHeader()
134 processingHeaderObj = ProcessingHeader()
135
135
136 lastBasicHeaderObj = BasicHeader(LOCALTIME)
136 lastBasicHeaderObj = BasicHeader(LOCALTIME)
137
137
138 sts = firstBasicHeaderObj.read(fp)
138 sts = firstBasicHeaderObj.read(fp)
139
139
140 if not(sts):
140 if not(sts):
141 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
141 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
142 return None
142 return None
143
143
144 sts = systemHeaderObj.read(fp)
144 if not systemHeaderObj.read(fp):
145 sts = radarControllerHeaderObj.read(fp)
145 return None
146 sts = processingHeaderObj.read(fp)
146
147 if not radarControllerHeaderObj.read(fp):
148 return None
149
150 if not processingHeaderObj.read(fp):
151 return None
147
152
148 filesize = os.path.getsize(filename)
153 filesize = os.path.getsize(filename)
149
154
150 offset = processingHeaderObj.blockSize + 24 #header size
155 offset = processingHeaderObj.blockSize + 24 #header size
151
156
152 if filesize <= offset:
157 if filesize <= offset:
153 print "[Reading] %s: This file has not enough data" %filename
158 print "[Reading] %s: This file has not enough data" %filename
154 return None
159 return None
155
160
156 fp.seek(-offset, 2)
161 fp.seek(-offset, 2)
157
162
158 sts = lastBasicHeaderObj.read(fp)
163 sts = lastBasicHeaderObj.read(fp)
159
164
160 fp.close()
165 fp.close()
161
166
162 thisDatetime = lastBasicHeaderObj.datatime
167 thisDatetime = lastBasicHeaderObj.datatime
163 thisTime_last_block = thisDatetime.time()
168 thisTime_last_block = thisDatetime.time()
164
169
165 thisDatetime = firstBasicHeaderObj.datatime
170 thisDatetime = firstBasicHeaderObj.datatime
166 thisDate = thisDatetime.date()
171 thisDate = thisDatetime.date()
167 thisTime_first_block = thisDatetime.time()
172 thisTime_first_block = thisDatetime.time()
168
173
169 #General case
174 #General case
170 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
175 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
171 #-----------o----------------------------o-----------
176 #-----------o----------------------------o-----------
172 # startTime endTime
177 # startTime endTime
173
178
174 if endTime >= startTime:
179 if endTime >= startTime:
175 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
180 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
176 return None
181 return None
177
182
178 return thisDatetime
183 return thisDatetime
179
184
180 #If endTime < startTime then endTime belongs to the next day
185 #If endTime < startTime then endTime belongs to the next day
181
186
182
187
183 #<<<<<<<<<<<o o>>>>>>>>>>>
188 #<<<<<<<<<<<o o>>>>>>>>>>>
184 #-----------o----------------------------o-----------
189 #-----------o----------------------------o-----------
185 # endTime startTime
190 # endTime startTime
186
191
187 if (thisDate == startDate) and (thisTime_last_block < startTime):
192 if (thisDate == startDate) and (thisTime_last_block < startTime):
188 return None
193 return None
189
194
190 if (thisDate == endDate) and (thisTime_first_block > endTime):
195 if (thisDate == endDate) and (thisTime_first_block > endTime):
191 return None
196 return None
192
197
193 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
198 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
194 return None
199 return None
195
200
196 return thisDatetime
201 return thisDatetime
197
202
198 def isFolderInDateRange(folder, startDate=None, endDate=None):
203 def isFolderInDateRange(folder, startDate=None, endDate=None):
199 """
204 """
200 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
205 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
201
206
202 Inputs:
207 Inputs:
203 folder : nombre completo del directorio.
208 folder : nombre completo del directorio.
204 Su formato deberia ser "/path_root/?YYYYDDD"
209 Su formato deberia ser "/path_root/?YYYYDDD"
205
210
206 siendo:
211 siendo:
207 YYYY : Anio (ejemplo 2015)
212 YYYY : Anio (ejemplo 2015)
208 DDD : Dia del anio (ejemplo 305)
213 DDD : Dia del anio (ejemplo 305)
209
214
210 startDate : fecha inicial del rango seleccionado en formato datetime.date
215 startDate : fecha inicial del rango seleccionado en formato datetime.date
211
216
212 endDate : fecha final del rango seleccionado en formato datetime.date
217 endDate : fecha final del rango seleccionado en formato datetime.date
213
218
214 Return:
219 Return:
215 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
220 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
216 fecha especificado, de lo contrario retorna False.
221 fecha especificado, de lo contrario retorna False.
217 Excepciones:
222 Excepciones:
218 Si el directorio no tiene el formato adecuado
223 Si el directorio no tiene el formato adecuado
219 """
224 """
220
225
221 basename = os.path.basename(folder)
226 basename = os.path.basename(folder)
222
227
223 if not isRadarFolder(basename):
228 if not isRadarFolder(basename):
224 print "The folder %s has not the rigth format" %folder
229 print "The folder %s has not the rigth format" %folder
225 return 0
230 return 0
226
231
227 if startDate and endDate:
232 if startDate and endDate:
228 thisDate = getDateFromRadarFolder(basename)
233 thisDate = getDateFromRadarFolder(basename)
229
234
230 if thisDate < startDate:
235 if thisDate < startDate:
231 return 0
236 return 0
232
237
233 if thisDate > endDate:
238 if thisDate > endDate:
234 return 0
239 return 0
235
240
236 return 1
241 return 1
237
242
238 def isFileInDateRange(filename, startDate=None, endDate=None):
243 def isFileInDateRange(filename, startDate=None, endDate=None):
239 """
244 """
240 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
245 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
241
246
242 Inputs:
247 Inputs:
243 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
248 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
244
249
245 Su formato deberia ser "?YYYYDDDsss"
250 Su formato deberia ser "?YYYYDDDsss"
246
251
247 siendo:
252 siendo:
248 YYYY : Anio (ejemplo 2015)
253 YYYY : Anio (ejemplo 2015)
249 DDD : Dia del anio (ejemplo 305)
254 DDD : Dia del anio (ejemplo 305)
250 sss : set
255 sss : set
251
256
252 startDate : fecha inicial del rango seleccionado en formato datetime.date
257 startDate : fecha inicial del rango seleccionado en formato datetime.date
253
258
254 endDate : fecha final del rango seleccionado en formato datetime.date
259 endDate : fecha final del rango seleccionado en formato datetime.date
255
260
256 Return:
261 Return:
257 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
262 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
258 fecha especificado, de lo contrario retorna False.
263 fecha especificado, de lo contrario retorna False.
259 Excepciones:
264 Excepciones:
260 Si el archivo no tiene el formato adecuado
265 Si el archivo no tiene el formato adecuado
261 """
266 """
262
267
263 basename = os.path.basename(filename)
268 basename = os.path.basename(filename)
264
269
265 if not isRadarFile(basename):
270 if not isRadarFile(basename):
266 print "The filename %s has not the rigth format" %filename
271 print "The filename %s has not the rigth format" %filename
267 return 0
272 return 0
268
273
269 if startDate and endDate:
274 if startDate and endDate:
270 thisDate = getDateFromRadarFile(basename)
275 thisDate = getDateFromRadarFile(basename)
271
276
272 if thisDate < startDate:
277 if thisDate < startDate:
273 return 0
278 return 0
274
279
275 if thisDate > endDate:
280 if thisDate > endDate:
276 return 0
281 return 0
277
282
278 return 1
283 return 1
279
284
280 def getFileFromSet(path, ext, set):
285 def getFileFromSet(path, ext, set):
281 validFilelist = []
286 validFilelist = []
282 fileList = os.listdir(path)
287 fileList = os.listdir(path)
283
288
284 # 0 1234 567 89A BCDE
289 # 0 1234 567 89A BCDE
285 # H YYYY DDD SSS .ext
290 # H YYYY DDD SSS .ext
286
291
287 for thisFile in fileList:
292 for thisFile in fileList:
288 try:
293 try:
289 year = int(thisFile[1:5])
294 year = int(thisFile[1:5])
290 doy = int(thisFile[5:8])
295 doy = int(thisFile[5:8])
291 except:
296 except:
292 continue
297 continue
293
298
294 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
299 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
295 continue
300 continue
296
301
297 validFilelist.append(thisFile)
302 validFilelist.append(thisFile)
298
303
299 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
304 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
300
305
301 if len(myfile)!= 0:
306 if len(myfile)!= 0:
302 return myfile[0]
307 return myfile[0]
303 else:
308 else:
304 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
309 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
305 print 'the filename %s does not exist'%filename
310 print 'the filename %s does not exist'%filename
306 print '...going to the last file: '
311 print '...going to the last file: '
307
312
308 if validFilelist:
313 if validFilelist:
309 validFilelist = sorted( validFilelist, key=str.lower )
314 validFilelist = sorted( validFilelist, key=str.lower )
310 return validFilelist[-1]
315 return validFilelist[-1]
311
316
312 return None
317 return None
313
318
314 def getlastFileFromPath(path, ext):
319 def getlastFileFromPath(path, ext):
315 """
320 """
316 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
321 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
317 al final de la depuracion devuelve el ultimo file de la lista que quedo.
322 al final de la depuracion devuelve el ultimo file de la lista que quedo.
318
323
319 Input:
324 Input:
320 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
325 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
321 ext : extension de los files contenidos en una carpeta
326 ext : extension de los files contenidos en una carpeta
322
327
323 Return:
328 Return:
324 El ultimo file de una determinada carpeta, no se considera el path.
329 El ultimo file de una determinada carpeta, no se considera el path.
325 """
330 """
326 validFilelist = []
331 validFilelist = []
327 fileList = os.listdir(path)
332 fileList = os.listdir(path)
328
333
329 # 0 1234 567 89A BCDE
334 # 0 1234 567 89A BCDE
330 # H YYYY DDD SSS .ext
335 # H YYYY DDD SSS .ext
331
336
332 for thisFile in fileList:
337 for thisFile in fileList:
333
338
334 year = thisFile[1:5]
339 year = thisFile[1:5]
335 if not isNumber(year):
340 if not isNumber(year):
336 continue
341 continue
337
342
338 doy = thisFile[5:8]
343 doy = thisFile[5:8]
339 if not isNumber(doy):
344 if not isNumber(doy):
340 continue
345 continue
341
346
342 year = int(year)
347 year = int(year)
343 doy = int(doy)
348 doy = int(doy)
344
349
345 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
350 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
346 continue
351 continue
347
352
348 validFilelist.append(thisFile)
353 validFilelist.append(thisFile)
349
354
350 if validFilelist:
355 if validFilelist:
351 validFilelist = sorted( validFilelist, key=str.lower )
356 validFilelist = sorted( validFilelist, key=str.lower )
352 return validFilelist[-1]
357 return validFilelist[-1]
353
358
354 return None
359 return None
355
360
356 def checkForRealPath(path, foldercounter, year, doy, set, ext):
361 def checkForRealPath(path, foldercounter, year, doy, set, ext):
357 """
362 """
358 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
363 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
359 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
364 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
360 el path exacto de un determinado file.
365 el path exacto de un determinado file.
361
366
362 Example :
367 Example :
363 nombre correcto del file es .../.../D2009307/P2009307367.ext
368 nombre correcto del file es .../.../D2009307/P2009307367.ext
364
369
365 Entonces la funcion prueba con las siguientes combinaciones
370 Entonces la funcion prueba con las siguientes combinaciones
366 .../.../y2009307367.ext
371 .../.../y2009307367.ext
367 .../.../Y2009307367.ext
372 .../.../Y2009307367.ext
368 .../.../x2009307/y2009307367.ext
373 .../.../x2009307/y2009307367.ext
369 .../.../x2009307/Y2009307367.ext
374 .../.../x2009307/Y2009307367.ext
370 .../.../X2009307/y2009307367.ext
375 .../.../X2009307/y2009307367.ext
371 .../.../X2009307/Y2009307367.ext
376 .../.../X2009307/Y2009307367.ext
372 siendo para este caso, la ultima combinacion de letras, identica al file buscado
377 siendo para este caso, la ultima combinacion de letras, identica al file buscado
373
378
374 Return:
379 Return:
375 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
380 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
376 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
381 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
377 para el filename
382 para el filename
378 """
383 """
379 fullfilename = None
384 fullfilename = None
380 find_flag = False
385 find_flag = False
381 filename = None
386 filename = None
382
387
383 prefixDirList = [None,'d','D']
388 prefixDirList = [None,'d','D']
384 if ext.lower() == ".r": #voltage
389 if ext.lower() == ".r": #voltage
385 prefixFileList = ['d','D']
390 prefixFileList = ['d','D']
386 elif ext.lower() == ".pdata": #spectra
391 elif ext.lower() == ".pdata": #spectra
387 prefixFileList = ['p','P']
392 prefixFileList = ['p','P']
388 else:
393 else:
389 return None, filename
394 return None, filename
390
395
391 #barrido por las combinaciones posibles
396 #barrido por las combinaciones posibles
392 for prefixDir in prefixDirList:
397 for prefixDir in prefixDirList:
393 thispath = path
398 thispath = path
394 if prefixDir != None:
399 if prefixDir != None:
395 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
400 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
396 if foldercounter == 0:
401 if foldercounter == 0:
397 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
402 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
398 else:
403 else:
399 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
404 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
400 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
405 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
401 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
406 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
402 fullfilename = os.path.join( thispath, filename ) #formo el path completo
407 fullfilename = os.path.join( thispath, filename ) #formo el path completo
403
408
404 if os.path.exists( fullfilename ): #verifico que exista
409 if os.path.exists( fullfilename ): #verifico que exista
405 find_flag = True
410 find_flag = True
406 break
411 break
407 if find_flag:
412 if find_flag:
408 break
413 break
409
414
410 if not(find_flag):
415 if not(find_flag):
411 return None, filename
416 return None, filename
412
417
413 return fullfilename, filename
418 return fullfilename, filename
414
419
415 def isRadarFolder(folder):
420 def isRadarFolder(folder):
416 try:
421 try:
417 year = int(folder[1:5])
422 year = int(folder[1:5])
418 doy = int(folder[5:8])
423 doy = int(folder[5:8])
419 except:
424 except:
420 return 0
425 return 0
421
426
422 return 1
427 return 1
423
428
424 def isRadarFile(file):
429 def isRadarFile(file):
425 try:
430 try:
426 year = int(file[1:5])
431 year = int(file[1:5])
427 doy = int(file[5:8])
432 doy = int(file[5:8])
428 set = int(file[8:11])
433 set = int(file[8:11])
429 except:
434 except:
430 return 0
435 return 0
431
436
432 return 1
437 return 1
433
438
434 def getDateFromRadarFile(file):
439 def getDateFromRadarFile(file):
435 try:
440 try:
436 year = int(file[1:5])
441 year = int(file[1:5])
437 doy = int(file[5:8])
442 doy = int(file[5:8])
438 set = int(file[8:11])
443 set = int(file[8:11])
439 except:
444 except:
440 return None
445 return None
441
446
442 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
447 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
443 return thisDate
448 return thisDate
444
449
445 def getDateFromRadarFolder(folder):
450 def getDateFromRadarFolder(folder):
446 try:
451 try:
447 year = int(folder[1:5])
452 year = int(folder[1:5])
448 doy = int(folder[5:8])
453 doy = int(folder[5:8])
449 except:
454 except:
450 return None
455 return None
451
456
452 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
457 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
453 return thisDate
458 return thisDate
454
459
455 class JRODataIO:
460 class JRODataIO:
456
461
457 c = 3E8
462 c = 3E8
458
463
459 isConfig = False
464 isConfig = False
460
465
461 basicHeaderObj = None
466 basicHeaderObj = None
462
467
463 systemHeaderObj = None
468 systemHeaderObj = None
464
469
465 radarControllerHeaderObj = None
470 radarControllerHeaderObj = None
466
471
467 processingHeaderObj = None
472 processingHeaderObj = None
468
473
469 dtype = None
474 dtype = None
470
475
471 pathList = []
476 pathList = []
472
477
473 filenameList = []
478 filenameList = []
474
479
475 filename = None
480 filename = None
476
481
477 ext = None
482 ext = None
478
483
479 flagIsNewFile = 1
484 flagIsNewFile = 1
480
485
481 flagDiscontinuousBlock = 0
486 flagDiscontinuousBlock = 0
482
487
483 flagIsNewBlock = 0
488 flagIsNewBlock = 0
484
489
485 fp = None
490 fp = None
486
491
487 firstHeaderSize = 0
492 firstHeaderSize = 0
488
493
489 basicHeaderSize = 24
494 basicHeaderSize = 24
490
495
491 versionFile = 1103
496 versionFile = 1103
492
497
493 fileSize = None
498 fileSize = None
494
499
495 # ippSeconds = None
500 # ippSeconds = None
496
501
497 fileSizeByHeader = None
502 fileSizeByHeader = None
498
503
499 fileIndex = None
504 fileIndex = None
500
505
501 profileIndex = None
506 profileIndex = None
502
507
503 blockIndex = None
508 blockIndex = None
504
509
505 nTotalBlocks = None
510 nTotalBlocks = None
506
511
507 maxTimeStep = 30
512 maxTimeStep = 30
508
513
509 lastUTTime = None
514 lastUTTime = None
510
515
511 datablock = None
516 datablock = None
512
517
513 dataOut = None
518 dataOut = None
514
519
515 blocksize = None
520 blocksize = None
516
521
517 getByBlock = False
522 getByBlock = False
518
523
519 def __init__(self):
524 def __init__(self):
520
525
521 raise NotImplementedError
526 raise NotImplementedError
522
527
523 def run(self):
528 def run(self):
524
529
525 raise NotImplementedError
530 raise NotImplementedError
526
531
527 def getDtypeWidth(self):
532 def getDtypeWidth(self):
528
533
529 dtype_index = get_dtype_index(self.dtype)
534 dtype_index = get_dtype_index(self.dtype)
530 dtype_width = get_dtype_width(dtype_index)
535 dtype_width = get_dtype_width(dtype_index)
531
536
532 return dtype_width
537 return dtype_width
533
538
534 class JRODataReader(JRODataIO):
539 class JRODataReader(JRODataIO):
535
540
536
541
537 online = 0
542 online = 0
538
543
539 realtime = 0
544 realtime = 0
540
545
541 nReadBlocks = 0
546 nReadBlocks = 0
542
547
543 delay = 10 #number of seconds waiting a new file
548 delay = 10 #number of seconds waiting a new file
544
549
545 nTries = 3 #quantity tries
550 nTries = 3 #quantity tries
546
551
547 nFiles = 3 #number of files for searching
552 nFiles = 3 #number of files for searching
548
553
549 path = None
554 path = None
550
555
551 foldercounter = 0
556 foldercounter = 0
552
557
553 flagNoMoreFiles = 0
558 flagNoMoreFiles = 0
554
559
555 datetimeList = []
560 datetimeList = []
556
561
557 __isFirstTimeOnline = 1
562 __isFirstTimeOnline = 1
558
563
559 __printInfo = True
564 __printInfo = True
560
565
561 profileIndex = None
566 profileIndex = None
562
567
563 nTxs = 1
568 nTxs = 1
564
569
565 txIndex = None
570 txIndex = None
566
571
567 def __init__(self):
572 def __init__(self):
568
573
569 """
574 """
570 This class is used to find data files
575 This class is used to find data files
571
576
572 Example:
577 Example:
573 reader = JRODataReader()
578 reader = JRODataReader()
574 fileList = reader.findDataFiles()
579 fileList = reader.findDataFiles()
575
580
576 """
581 """
577 pass
582 pass
578
583
579
584
580 def createObjByDefault(self):
585 def createObjByDefault(self):
581 """
586 """
582
587
583 """
588 """
584 raise NotImplementedError
589 raise NotImplementedError
585
590
586 def getBlockDimension(self):
591 def getBlockDimension(self):
587
592
588 raise NotImplementedError
593 raise NotImplementedError
589
594
590 def __searchFilesOffLine(self,
595 def __searchFilesOffLine(self,
591 path,
596 path,
592 startDate=None,
597 startDate=None,
593 endDate=None,
598 endDate=None,
594 startTime=datetime.time(0,0,0),
599 startTime=datetime.time(0,0,0),
595 endTime=datetime.time(23,59,59),
600 endTime=datetime.time(23,59,59),
596 set=None,
601 set=None,
597 expLabel='',
602 expLabel='',
598 ext='.r',
603 ext='.r',
599 walk=True):
604 walk=True):
600
605
601 self.filenameList = []
606 self.filenameList = []
602 self.datetimeList = []
607 self.datetimeList = []
603
608
604 pathList = []
609 pathList = []
605
610
606 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
611 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
607
612
608 if dateList == []:
613 if dateList == []:
609 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
614 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
610 return None, None
615 return None, None
611
616
612 if len(dateList) > 1:
617 if len(dateList) > 1:
613 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
618 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
614 else:
619 else:
615 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
620 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
616
621
617 filenameList = []
622 filenameList = []
618 datetimeList = []
623 datetimeList = []
619
624
620 for thisPath in pathList:
625 for thisPath in pathList:
621 # thisPath = pathList[pathDict[file]]
626 # thisPath = pathList[pathDict[file]]
622
627
623 fileList = glob.glob1(thisPath, "*%s" %ext)
628 fileList = glob.glob1(thisPath, "*%s" %ext)
624 fileList.sort()
629 fileList.sort()
625
630
626 for file in fileList:
631 for file in fileList:
627
632
628 filename = os.path.join(thisPath,file)
633 filename = os.path.join(thisPath,file)
629
634
630 if not isFileInDateRange(filename, startDate, endDate):
635 if not isFileInDateRange(filename, startDate, endDate):
631 continue
636 continue
632
637
633 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
638 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
634
639
635 if not(thisDatetime):
640 if not(thisDatetime):
636 continue
641 continue
637
642
638 filenameList.append(filename)
643 filenameList.append(filename)
639 datetimeList.append(thisDatetime)
644 datetimeList.append(thisDatetime)
640
645
641 if not(filenameList):
646 if not(filenameList):
642 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
647 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
643 return None, None
648 return None, None
644
649
645 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
650 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
646 print
651 print
647
652
648 for i in range(len(filenameList)):
653 for i in range(len(filenameList)):
649 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
654 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
650
655
651 self.filenameList = filenameList
656 self.filenameList = filenameList
652 self.datetimeList = datetimeList
657 self.datetimeList = datetimeList
653
658
654 return pathList, filenameList
659 return pathList, filenameList
655
660
656 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
661 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
657
662
658 """
663 """
659 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
664 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
660 devuelve el archivo encontrado ademas de otros datos.
665 devuelve el archivo encontrado ademas de otros datos.
661
666
662 Input:
667 Input:
663 path : carpeta donde estan contenidos los files que contiene data
668 path : carpeta donde estan contenidos los files que contiene data
664
669
665 expLabel : Nombre del subexperimento (subfolder)
670 expLabel : Nombre del subexperimento (subfolder)
666
671
667 ext : extension de los files
672 ext : extension de los files
668
673
669 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
674 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
670
675
671 Return:
676 Return:
672 directory : eL directorio donde esta el file encontrado
677 directory : eL directorio donde esta el file encontrado
673 filename : el ultimo file de una determinada carpeta
678 filename : el ultimo file de una determinada carpeta
674 year : el anho
679 year : el anho
675 doy : el numero de dia del anho
680 doy : el numero de dia del anho
676 set : el set del archivo
681 set : el set del archivo
677
682
678
683
679 """
684 """
680 if not os.path.isdir(path):
685 if not os.path.isdir(path):
681 return None, None, None, None, None, None
686 return None, None, None, None, None, None
682
687
683 dirList = []
688 dirList = []
684
689
685 if not walk:
690 if not walk:
686 fullpath = path
691 fullpath = path
687 foldercounter = 0
692 foldercounter = 0
688 else:
693 else:
689 #Filtra solo los directorios
694 #Filtra solo los directorios
690 for thisPath in os.listdir(path):
695 for thisPath in os.listdir(path):
691 if not os.path.isdir(os.path.join(path,thisPath)):
696 if not os.path.isdir(os.path.join(path,thisPath)):
692 continue
697 continue
693 if not isRadarFolder(thisPath):
698 if not isRadarFolder(thisPath):
694 continue
699 continue
695
700
696 dirList.append(thisPath)
701 dirList.append(thisPath)
697
702
698 if not(dirList):
703 if not(dirList):
699 return None, None, None, None, None, None
704 return None, None, None, None, None, None
700
705
701 dirList = sorted( dirList, key=str.lower )
706 dirList = sorted( dirList, key=str.lower )
702
707
703 doypath = dirList[-1]
708 doypath = dirList[-1]
704 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
709 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
705 fullpath = os.path.join(path, doypath, expLabel)
710 fullpath = os.path.join(path, doypath, expLabel)
706
711
707
712
708 print "[Reading] %s folder was found: " %(fullpath )
713 print "[Reading] %s folder was found: " %(fullpath )
709
714
710 if set == None:
715 if set == None:
711 filename = getlastFileFromPath(fullpath, ext)
716 filename = getlastFileFromPath(fullpath, ext)
712 else:
717 else:
713 filename = getFileFromSet(fullpath, ext, set)
718 filename = getFileFromSet(fullpath, ext, set)
714
719
715 if not(filename):
720 if not(filename):
716 return None, None, None, None, None, None
721 return None, None, None, None, None, None
717
722
718 print "[Reading] %s file was found" %(filename)
723 print "[Reading] %s file was found" %(filename)
719
724
720 if not(self.__verifyFile(os.path.join(fullpath, filename))):
725 if not(self.__verifyFile(os.path.join(fullpath, filename))):
721 return None, None, None, None, None, None
726 return None, None, None, None, None, None
722
727
723 year = int( filename[1:5] )
728 year = int( filename[1:5] )
724 doy = int( filename[5:8] )
729 doy = int( filename[5:8] )
725 set = int( filename[8:11] )
730 set = int( filename[8:11] )
726
731
727 return fullpath, foldercounter, filename, year, doy, set
732 return fullpath, foldercounter, filename, year, doy, set
728
733
729 def __setNextFileOffline(self):
734 def __setNextFileOffline(self):
730
735
731 idFile = self.fileIndex
736 idFile = self.fileIndex
732
737
733 while (True):
738 while (True):
734 idFile += 1
739 idFile += 1
735 if not(idFile < len(self.filenameList)):
740 if not(idFile < len(self.filenameList)):
736 self.flagNoMoreFiles = 1
741 self.flagNoMoreFiles = 1
737 # print "[Reading] No more Files"
742 # print "[Reading] No more Files"
738 return 0
743 return 0
739
744
740 filename = self.filenameList[idFile]
745 filename = self.filenameList[idFile]
741
746
742 if not(self.__verifyFile(filename)):
747 if not(self.__verifyFile(filename)):
743 continue
748 continue
744
749
745 fileSize = os.path.getsize(filename)
750 fileSize = os.path.getsize(filename)
746 fp = open(filename,'rb')
751 fp = open(filename,'rb')
747 break
752 break
748
753
749 self.flagIsNewFile = 1
754 self.flagIsNewFile = 1
750 self.fileIndex = idFile
755 self.fileIndex = idFile
751 self.filename = filename
756 self.filename = filename
752 self.fileSize = fileSize
757 self.fileSize = fileSize
753 self.fp = fp
758 self.fp = fp
754
759
755 # print "[Reading] Setting the file: %s"%self.filename
760 # print "[Reading] Setting the file: %s"%self.filename
756
761
757 return 1
762 return 1
758
763
759 def __setNextFileOnline(self):
764 def __setNextFileOnline(self):
760 """
765 """
761 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
766 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
762 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
767 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
763 siguientes.
768 siguientes.
764
769
765 Affected:
770 Affected:
766 self.flagIsNewFile
771 self.flagIsNewFile
767 self.filename
772 self.filename
768 self.fileSize
773 self.fileSize
769 self.fp
774 self.fp
770 self.set
775 self.set
771 self.flagNoMoreFiles
776 self.flagNoMoreFiles
772
777
773 Return:
778 Return:
774 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
779 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
775 1 : si el file fue abierto con exito y esta listo a ser leido
780 1 : si el file fue abierto con exito y esta listo a ser leido
776
781
777 Excepciones:
782 Excepciones:
778 Si un determinado file no puede ser abierto
783 Si un determinado file no puede ser abierto
779 """
784 """
780 nFiles = 0
785 nFiles = 0
781 fileOk_flag = False
786 fileOk_flag = False
782 firstTime_flag = True
787 firstTime_flag = True
783
788
784 self.set += 1
789 self.set += 1
785
790
786 if self.set > 999:
791 if self.set > 999:
787 self.set = 0
792 self.set = 0
788 self.foldercounter += 1
793 self.foldercounter += 1
789
794
790 #busca el 1er file disponible
795 #busca el 1er file disponible
791 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
796 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
792 if fullfilename:
797 if fullfilename:
793 if self.__verifyFile(fullfilename, False):
798 if self.__verifyFile(fullfilename, False):
794 fileOk_flag = True
799 fileOk_flag = True
795
800
796 #si no encuentra un file entonces espera y vuelve a buscar
801 #si no encuentra un file entonces espera y vuelve a buscar
797 if not(fileOk_flag):
802 if not(fileOk_flag):
798 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
803 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
799
804
800 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
805 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
801 tries = self.nTries
806 tries = self.nTries
802 else:
807 else:
803 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
808 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
804
809
805 for nTries in range( tries ):
810 for nTries in range( tries ):
806 if firstTime_flag:
811 if firstTime_flag:
807 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
812 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
808 sleep( self.delay )
813 sleep( self.delay )
809 else:
814 else:
810 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
815 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
811
816
812 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
817 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
813 if fullfilename:
818 if fullfilename:
814 if self.__verifyFile(fullfilename):
819 if self.__verifyFile(fullfilename):
815 fileOk_flag = True
820 fileOk_flag = True
816 break
821 break
817
822
818 if fileOk_flag:
823 if fileOk_flag:
819 break
824 break
820
825
821 firstTime_flag = False
826 firstTime_flag = False
822
827
823 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
828 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
824 self.set += 1
829 self.set += 1
825
830
826 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
831 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
827 self.set = 0
832 self.set = 0
828 self.doy += 1
833 self.doy += 1
829 self.foldercounter = 0
834 self.foldercounter = 0
830
835
831 if fileOk_flag:
836 if fileOk_flag:
832 self.fileSize = os.path.getsize( fullfilename )
837 self.fileSize = os.path.getsize( fullfilename )
833 self.filename = fullfilename
838 self.filename = fullfilename
834 self.flagIsNewFile = 1
839 self.flagIsNewFile = 1
835 if self.fp != None: self.fp.close()
840 if self.fp != None: self.fp.close()
836 self.fp = open(fullfilename, 'rb')
841 self.fp = open(fullfilename, 'rb')
837 self.flagNoMoreFiles = 0
842 self.flagNoMoreFiles = 0
838 # print '[Reading] Setting the file: %s' % fullfilename
843 # print '[Reading] Setting the file: %s' % fullfilename
839 else:
844 else:
840 self.fileSize = 0
845 self.fileSize = 0
841 self.filename = None
846 self.filename = None
842 self.flagIsNewFile = 0
847 self.flagIsNewFile = 0
843 self.fp = None
848 self.fp = None
844 self.flagNoMoreFiles = 1
849 self.flagNoMoreFiles = 1
845 # print '[Reading] No more files to read'
850 # print '[Reading] No more files to read'
846
851
847 return fileOk_flag
852 return fileOk_flag
848
853
849 def setNextFile(self):
854 def setNextFile(self):
850 if self.fp != None:
855 if self.fp != None:
851 self.fp.close()
856 self.fp.close()
852
857
853 if self.online:
858 if self.online:
854 newFile = self.__setNextFileOnline()
859 newFile = self.__setNextFileOnline()
855 else:
860 else:
856 newFile = self.__setNextFileOffline()
861 newFile = self.__setNextFileOffline()
857
862
858 if not(newFile):
863 if not(newFile):
859 print '[Reading] No more files to read'
864 print '[Reading] No more files to read'
860 return 0
865 return 0
861
866
862 print '[Reading] Setting the file: %s' % self.filename
867 print '[Reading] Setting the file: %s' % self.filename
863
868
864 self.__readFirstHeader()
869 self.__readFirstHeader()
865 self.nReadBlocks = 0
870 self.nReadBlocks = 0
866 return 1
871 return 1
867
872
868 def __waitNewBlock(self):
873 def __waitNewBlock(self):
869 """
874 """
870 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
875 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
871
876
872 Si el modo de lectura es OffLine siempre retorn 0
877 Si el modo de lectura es OffLine siempre retorn 0
873 """
878 """
874 if not self.online:
879 if not self.online:
875 return 0
880 return 0
876
881
877 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
882 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
878 return 0
883 return 0
879
884
880 currentPointer = self.fp.tell()
885 currentPointer = self.fp.tell()
881
886
882 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
887 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
883
888
884 for nTries in range( self.nTries ):
889 for nTries in range( self.nTries ):
885
890
886 self.fp.close()
891 self.fp.close()
887 self.fp = open( self.filename, 'rb' )
892 self.fp = open( self.filename, 'rb' )
888 self.fp.seek( currentPointer )
893 self.fp.seek( currentPointer )
889
894
890 self.fileSize = os.path.getsize( self.filename )
895 self.fileSize = os.path.getsize( self.filename )
891 currentSize = self.fileSize - currentPointer
896 currentSize = self.fileSize - currentPointer
892
897
893 if ( currentSize >= neededSize ):
898 if ( currentSize >= neededSize ):
894 self.basicHeaderObj.read(self.fp)
899 self.basicHeaderObj.read(self.fp)
895 return 1
900 return 1
896
901
897 if self.fileSize == self.fileSizeByHeader:
902 if self.fileSize == self.fileSizeByHeader:
898 # self.flagEoF = True
903 # self.flagEoF = True
899 return 0
904 return 0
900
905
901 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
906 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
902 sleep( self.delay )
907 sleep( self.delay )
903
908
904
909
905 return 0
910 return 0
906
911
907 def waitDataBlock(self,pointer_location):
912 def waitDataBlock(self,pointer_location):
908
913
909 currentPointer = pointer_location
914 currentPointer = pointer_location
910
915
911 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
916 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
912
917
913 for nTries in range( self.nTries ):
918 for nTries in range( self.nTries ):
914 self.fp.close()
919 self.fp.close()
915 self.fp = open( self.filename, 'rb' )
920 self.fp = open( self.filename, 'rb' )
916 self.fp.seek( currentPointer )
921 self.fp.seek( currentPointer )
917
922
918 self.fileSize = os.path.getsize( self.filename )
923 self.fileSize = os.path.getsize( self.filename )
919 currentSize = self.fileSize - currentPointer
924 currentSize = self.fileSize - currentPointer
920
925
921 if ( currentSize >= neededSize ):
926 if ( currentSize >= neededSize ):
922 return 1
927 return 1
923
928
924 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
929 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
925 sleep( self.delay )
930 sleep( self.delay )
926
931
927 return 0
932 return 0
928
933
929 def __jumpToLastBlock(self):
934 def __jumpToLastBlock(self):
930
935
931 if not(self.__isFirstTimeOnline):
936 if not(self.__isFirstTimeOnline):
932 return
937 return
933
938
934 csize = self.fileSize - self.fp.tell()
939 csize = self.fileSize - self.fp.tell()
935 blocksize = self.processingHeaderObj.blockSize
940 blocksize = self.processingHeaderObj.blockSize
936
941
937 #salta el primer bloque de datos
942 #salta el primer bloque de datos
938 if csize > self.processingHeaderObj.blockSize:
943 if csize > self.processingHeaderObj.blockSize:
939 self.fp.seek(self.fp.tell() + blocksize)
944 self.fp.seek(self.fp.tell() + blocksize)
940 else:
945 else:
941 return
946 return
942
947
943 csize = self.fileSize - self.fp.tell()
948 csize = self.fileSize - self.fp.tell()
944 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
949 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
945 while True:
950 while True:
946
951
947 if self.fp.tell()<self.fileSize:
952 if self.fp.tell()<self.fileSize:
948 self.fp.seek(self.fp.tell() + neededsize)
953 self.fp.seek(self.fp.tell() + neededsize)
949 else:
954 else:
950 self.fp.seek(self.fp.tell() - neededsize)
955 self.fp.seek(self.fp.tell() - neededsize)
951 break
956 break
952
957
953 # csize = self.fileSize - self.fp.tell()
958 # csize = self.fileSize - self.fp.tell()
954 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
959 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
955 # factor = int(csize/neededsize)
960 # factor = int(csize/neededsize)
956 # if factor > 0:
961 # if factor > 0:
957 # self.fp.seek(self.fp.tell() + factor*neededsize)
962 # self.fp.seek(self.fp.tell() + factor*neededsize)
958
963
959 self.flagIsNewFile = 0
964 self.flagIsNewFile = 0
960 self.__isFirstTimeOnline = 0
965 self.__isFirstTimeOnline = 0
961
966
962 def __setNewBlock(self):
967 def __setNewBlock(self):
963
968
964 if self.fp == None:
969 if self.fp == None:
965 return 0
970 return 0
966
971
967 # if self.online:
972 # if self.online:
968 # self.__jumpToLastBlock()
973 # self.__jumpToLastBlock()
969
974
970 if self.flagIsNewFile:
975 if self.flagIsNewFile:
971 self.lastUTTime = self.basicHeaderObj.utc
976 self.lastUTTime = self.basicHeaderObj.utc
972 return 1
977 return 1
973
978
974 if self.realtime:
979 if self.realtime:
975 self.flagDiscontinuousBlock = 1
980 self.flagDiscontinuousBlock = 1
976 if not(self.setNextFile()):
981 if not(self.setNextFile()):
977 return 0
982 return 0
978 else:
983 else:
979 return 1
984 return 1
980
985
981 currentSize = self.fileSize - self.fp.tell()
986 currentSize = self.fileSize - self.fp.tell()
982 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
987 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
983
988
984 if (currentSize >= neededSize):
989 if (currentSize >= neededSize):
985 self.basicHeaderObj.read(self.fp)
990 self.basicHeaderObj.read(self.fp)
986 self.lastUTTime = self.basicHeaderObj.utc
991 self.lastUTTime = self.basicHeaderObj.utc
987 return 1
992 return 1
988
993
989 if self.__waitNewBlock():
994 if self.__waitNewBlock():
990 self.lastUTTime = self.basicHeaderObj.utc
995 self.lastUTTime = self.basicHeaderObj.utc
991 return 1
996 return 1
992
997
993 if not(self.setNextFile()):
998 if not(self.setNextFile()):
994 return 0
999 return 0
995
1000
996 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1001 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
997 self.lastUTTime = self.basicHeaderObj.utc
1002 self.lastUTTime = self.basicHeaderObj.utc
998
1003
999 self.flagDiscontinuousBlock = 0
1004 self.flagDiscontinuousBlock = 0
1000
1005
1001 if deltaTime > self.maxTimeStep:
1006 if deltaTime > self.maxTimeStep:
1002 self.flagDiscontinuousBlock = 1
1007 self.flagDiscontinuousBlock = 1
1003
1008
1004 return 1
1009 return 1
1005
1010
1006 def readNextBlock(self):
1011 def readNextBlock(self):
1007
1012
1008 #Skip block out of startTime and endTime
1013 #Skip block out of startTime and endTime
1009 while True:
1014 while True:
1010 if not(self.__setNewBlock()):
1015 if not(self.__setNewBlock()):
1011 return 0
1016 return 0
1012
1017
1013 if not(self.readBlock()):
1018 if not(self.readBlock()):
1014 return 0
1019 return 0
1015
1020
1016 self.getBasicHeader()
1021 self.getBasicHeader()
1017
1022
1018 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1023 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1019
1024
1020 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1025 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1021 self.processingHeaderObj.dataBlocksPerFile,
1026 self.processingHeaderObj.dataBlocksPerFile,
1022 self.dataOut.datatime.ctime())
1027 self.dataOut.datatime.ctime())
1023 continue
1028 continue
1024
1029
1025 break
1030 break
1026
1031
1027 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1032 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1028 self.processingHeaderObj.dataBlocksPerFile,
1033 self.processingHeaderObj.dataBlocksPerFile,
1029 self.dataOut.datatime.ctime())
1034 self.dataOut.datatime.ctime())
1030 return 1
1035 return 1
1031
1036
1032 def __readFirstHeader(self):
1037 def __readFirstHeader(self):
1033
1038
1034 self.basicHeaderObj.read(self.fp)
1039 self.basicHeaderObj.read(self.fp)
1035 self.systemHeaderObj.read(self.fp)
1040 self.systemHeaderObj.read(self.fp)
1036 self.radarControllerHeaderObj.read(self.fp)
1041 self.radarControllerHeaderObj.read(self.fp)
1037 self.processingHeaderObj.read(self.fp)
1042 self.processingHeaderObj.read(self.fp)
1038
1043
1039 self.firstHeaderSize = self.basicHeaderObj.size
1044 self.firstHeaderSize = self.basicHeaderObj.size
1040
1045
1041 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1046 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1042 if datatype == 0:
1047 if datatype == 0:
1043 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1048 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1044 elif datatype == 1:
1049 elif datatype == 1:
1045 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1050 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1046 elif datatype == 2:
1051 elif datatype == 2:
1047 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1052 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1048 elif datatype == 3:
1053 elif datatype == 3:
1049 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1054 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1050 elif datatype == 4:
1055 elif datatype == 4:
1051 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1056 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1052 elif datatype == 5:
1057 elif datatype == 5:
1053 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1058 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1054 else:
1059 else:
1055 raise ValueError, 'Data type was not defined'
1060 raise ValueError, 'Data type was not defined'
1056
1061
1057 self.dtype = datatype_str
1062 self.dtype = datatype_str
1058 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1063 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1059 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1064 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1060 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1065 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1061 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1066 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1062 self.getBlockDimension()
1067 self.getBlockDimension()
1063
1068
1064 def __verifyFile(self, filename, msgFlag=True):
1069 def __verifyFile(self, filename, msgFlag=True):
1065
1070
1066 msg = None
1071 msg = None
1067
1072
1068 try:
1073 try:
1069 fp = open(filename, 'rb')
1074 fp = open(filename, 'rb')
1070 except IOError:
1075 except IOError:
1071
1076
1072 if msgFlag:
1077 if msgFlag:
1073 print "[Reading] File %s can't be opened" % (filename)
1078 print "[Reading] File %s can't be opened" % (filename)
1074
1079
1075 return False
1080 return False
1076
1081
1077 currentPosition = fp.tell()
1082 currentPosition = fp.tell()
1078 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1083 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1079
1084
1080 if neededSize == 0:
1085 if neededSize == 0:
1081 basicHeaderObj = BasicHeader(LOCALTIME)
1086 basicHeaderObj = BasicHeader(LOCALTIME)
1082 systemHeaderObj = SystemHeader()
1087 systemHeaderObj = SystemHeader()
1083 radarControllerHeaderObj = RadarControllerHeader()
1088 radarControllerHeaderObj = RadarControllerHeader()
1084 processingHeaderObj = ProcessingHeader()
1089 processingHeaderObj = ProcessingHeader()
1085
1090
1086 if not( basicHeaderObj.read(fp) ):
1091 if not( basicHeaderObj.read(fp) ):
1087 fp.close()
1092 fp.close()
1088 return False
1093 return False
1089
1094
1090 if not( systemHeaderObj.read(fp) ):
1095 if not( systemHeaderObj.read(fp) ):
1091 fp.close()
1096 fp.close()
1092 return False
1097 return False
1093
1098
1094 if not( radarControllerHeaderObj.read(fp) ):
1099 if not( radarControllerHeaderObj.read(fp) ):
1095 fp.close()
1100 fp.close()
1096 return False
1101 return False
1097
1102
1098 if not( processingHeaderObj.read(fp) ):
1103 if not( processingHeaderObj.read(fp) ):
1099 fp.close()
1104 fp.close()
1100 return False
1105 return False
1101
1106
1102 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1107 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1103 else:
1108 else:
1104 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1109 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1105
1110
1106 fp.close()
1111 fp.close()
1107
1112
1108 fileSize = os.path.getsize(filename)
1113 fileSize = os.path.getsize(filename)
1109 currentSize = fileSize - currentPosition
1114 currentSize = fileSize - currentPosition
1110
1115
1111 if currentSize < neededSize:
1116 if currentSize < neededSize:
1112 if msgFlag and (msg != None):
1117 if msgFlag and (msg != None):
1113 print msg
1118 print msg
1114 return False
1119 return False
1115
1120
1116 return True
1121 return True
1117
1122
1118 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1123 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1119
1124
1120 path_empty = True
1125 path_empty = True
1121
1126
1122 dateList = []
1127 dateList = []
1123 pathList = []
1128 pathList = []
1124
1129
1125 multi_path = path.split(',')
1130 multi_path = path.split(',')
1126
1131
1127 if not walk:
1132 if not walk:
1128
1133
1129 for single_path in multi_path:
1134 for single_path in multi_path:
1130
1135
1131 if not os.path.isdir(single_path):
1136 if not os.path.isdir(single_path):
1132 continue
1137 continue
1133
1138
1134 fileList = glob.glob1(single_path, "*"+ext)
1139 fileList = glob.glob1(single_path, "*"+ext)
1135
1140
1136 if not fileList:
1141 if not fileList:
1137 continue
1142 continue
1138
1143
1139 path_empty = False
1144 path_empty = False
1140
1145
1141 fileList.sort()
1146 fileList.sort()
1142
1147
1143 for thisFile in fileList:
1148 for thisFile in fileList:
1144
1149
1145 if not os.path.isfile(os.path.join(single_path, thisFile)):
1150 if not os.path.isfile(os.path.join(single_path, thisFile)):
1146 continue
1151 continue
1147
1152
1148 if not isRadarFile(thisFile):
1153 if not isRadarFile(thisFile):
1149 continue
1154 continue
1150
1155
1151 if not isFileInDateRange(thisFile, startDate, endDate):
1156 if not isFileInDateRange(thisFile, startDate, endDate):
1152 continue
1157 continue
1153
1158
1154 thisDate = getDateFromRadarFile(thisFile)
1159 thisDate = getDateFromRadarFile(thisFile)
1155
1160
1156 if thisDate in dateList:
1161 if thisDate in dateList:
1157 continue
1162 continue
1158
1163
1159 dateList.append(thisDate)
1164 dateList.append(thisDate)
1160 pathList.append(single_path)
1165 pathList.append(single_path)
1161
1166
1162 else:
1167 else:
1163 for single_path in multi_path:
1168 for single_path in multi_path:
1164
1169
1165 if not os.path.isdir(single_path):
1170 if not os.path.isdir(single_path):
1166 continue
1171 continue
1167
1172
1168 dirList = []
1173 dirList = []
1169
1174
1170 for thisPath in os.listdir(single_path):
1175 for thisPath in os.listdir(single_path):
1171
1176
1172 if not os.path.isdir(os.path.join(single_path,thisPath)):
1177 if not os.path.isdir(os.path.join(single_path,thisPath)):
1173 continue
1178 continue
1174
1179
1175 if not isRadarFolder(thisPath):
1180 if not isRadarFolder(thisPath):
1176 continue
1181 continue
1177
1182
1178 if not isFolderInDateRange(thisPath, startDate, endDate):
1183 if not isFolderInDateRange(thisPath, startDate, endDate):
1179 continue
1184 continue
1180
1185
1181 dirList.append(thisPath)
1186 dirList.append(thisPath)
1182
1187
1183 if not dirList:
1188 if not dirList:
1184 continue
1189 continue
1185
1190
1186 dirList.sort()
1191 dirList.sort()
1187
1192
1188 for thisDir in dirList:
1193 for thisDir in dirList:
1189
1194
1190 datapath = os.path.join(single_path, thisDir, expLabel)
1195 datapath = os.path.join(single_path, thisDir, expLabel)
1191 fileList = glob.glob1(datapath, "*"+ext)
1196 fileList = glob.glob1(datapath, "*"+ext)
1192
1197
1193 if not fileList:
1198 if not fileList:
1194 continue
1199 continue
1195
1200
1196 path_empty = False
1201 path_empty = False
1197
1202
1198 thisDate = getDateFromRadarFolder(thisDir)
1203 thisDate = getDateFromRadarFolder(thisDir)
1199
1204
1200 pathList.append(datapath)
1205 pathList.append(datapath)
1201 dateList.append(thisDate)
1206 dateList.append(thisDate)
1202
1207
1203 dateList.sort()
1208 dateList.sort()
1204
1209
1205 if walk:
1210 if walk:
1206 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1211 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1207 else:
1212 else:
1208 pattern_path = multi_path[0]
1213 pattern_path = multi_path[0]
1209
1214
1210 if path_empty:
1215 if path_empty:
1211 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1216 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1212 else:
1217 else:
1213 if not dateList:
1218 if not dateList:
1214 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1219 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1215
1220
1216 if include_path:
1221 if include_path:
1217 return dateList, pathList
1222 return dateList, pathList
1218
1223
1219 return dateList
1224 return dateList
1220
1225
1221 def setup(self,
1226 def setup(self,
1222 path=None,
1227 path=None,
1223 startDate=None,
1228 startDate=None,
1224 endDate=None,
1229 endDate=None,
1225 startTime=datetime.time(0,0,0),
1230 startTime=datetime.time(0,0,0),
1226 endTime=datetime.time(23,59,59),
1231 endTime=datetime.time(23,59,59),
1227 set=None,
1232 set=None,
1228 expLabel = "",
1233 expLabel = "",
1229 ext = None,
1234 ext = None,
1230 online = False,
1235 online = False,
1231 delay = 60,
1236 delay = 60,
1232 walk = True,
1237 walk = True,
1233 getblock = False,
1238 getblock = False,
1234 nTxs = 1,
1239 nTxs = 1,
1235 realtime=False):
1240 realtime=False):
1236
1241
1237 if path == None:
1242 if path == None:
1238 raise ValueError, "[Reading] The path is not valid"
1243 raise ValueError, "[Reading] The path is not valid"
1239
1244
1240 if ext == None:
1245 if ext == None:
1241 ext = self.ext
1246 ext = self.ext
1242
1247
1243 if online:
1248 if online:
1244 print "[Reading] Searching files in online mode..."
1249 print "[Reading] Searching files in online mode..."
1245
1250
1246 for nTries in range( self.nTries ):
1251 for nTries in range( self.nTries ):
1247 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1252 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1248
1253
1249 if fullpath:
1254 if fullpath:
1250 break
1255 break
1251
1256
1252 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1257 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1253 sleep( self.delay )
1258 sleep( self.delay )
1254
1259
1255 if not(fullpath):
1260 if not(fullpath):
1256 print "[Reading] There 'isn't any valid file in %s" % path
1261 print "[Reading] There 'isn't any valid file in %s" % path
1257 return
1262 return
1258
1263
1259 self.year = year
1264 self.year = year
1260 self.doy = doy
1265 self.doy = doy
1261 self.set = set - 1
1266 self.set = set - 1
1262 self.path = path
1267 self.path = path
1263 self.foldercounter = foldercounter
1268 self.foldercounter = foldercounter
1264 last_set = None
1269 last_set = None
1265
1270
1266 else:
1271 else:
1267 print "[Reading] Searching files in offline mode ..."
1272 print "[Reading] Searching files in offline mode ..."
1268 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1273 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1269 startTime=startTime, endTime=endTime,
1274 startTime=startTime, endTime=endTime,
1270 set=set, expLabel=expLabel, ext=ext,
1275 set=set, expLabel=expLabel, ext=ext,
1271 walk=walk)
1276 walk=walk)
1272
1277
1273 if not(pathList):
1278 if not(pathList):
1274 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1279 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1275 # datetime.datetime.combine(startDate,startTime).ctime(),
1280 # datetime.datetime.combine(startDate,startTime).ctime(),
1276 # datetime.datetime.combine(endDate,endTime).ctime())
1281 # datetime.datetime.combine(endDate,endTime).ctime())
1277
1282
1278 # sys.exit(-1)
1283 # sys.exit(-1)
1279
1284
1280 self.fileIndex = -1
1285 self.fileIndex = -1
1281 self.pathList = []
1286 self.pathList = []
1282 self.filenameList = []
1287 self.filenameList = []
1283 return
1288 return
1284
1289
1285 self.fileIndex = -1
1290 self.fileIndex = -1
1286 self.pathList = pathList
1291 self.pathList = pathList
1287 self.filenameList = filenameList
1292 self.filenameList = filenameList
1288 file_name = os.path.basename(filenameList[-1])
1293 file_name = os.path.basename(filenameList[-1])
1289 basename, ext = os.path.splitext(file_name)
1294 basename, ext = os.path.splitext(file_name)
1290 last_set = int(basename[-3:])
1295 last_set = int(basename[-3:])
1291
1296
1292 self.online = online
1297 self.online = online
1293 self.realtime = realtime
1298 self.realtime = realtime
1294 self.delay = delay
1299 self.delay = delay
1295 ext = ext.lower()
1300 ext = ext.lower()
1296 self.ext = ext
1301 self.ext = ext
1297 self.getByBlock = getblock
1302 self.getByBlock = getblock
1298 self.nTxs = nTxs
1303 self.nTxs = nTxs
1299 self.startTime = startTime
1304 self.startTime = startTime
1300 self.endTime = endTime
1305 self.endTime = endTime
1301
1306
1302 if not(self.setNextFile()):
1307 if not(self.setNextFile()):
1303 if (startDate!=None) and (endDate!=None):
1308 if (startDate!=None) and (endDate!=None):
1304 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1309 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1305 elif startDate != None:
1310 elif startDate != None:
1306 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1311 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1307 else:
1312 else:
1308 print "[Reading] No files"
1313 print "[Reading] No files"
1309
1314
1310 self.fileIndex = -1
1315 self.fileIndex = -1
1311 self.pathList = []
1316 self.pathList = []
1312 self.filenameList = []
1317 self.filenameList = []
1313 return
1318 return
1314
1319
1315 # self.getBasicHeader()
1320 # self.getBasicHeader()
1316
1321
1317 if last_set != None:
1322 if last_set != None:
1318 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1323 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1319 return
1324 return
1320
1325
1321 def getBasicHeader(self):
1326 def getBasicHeader(self):
1322
1327
1323 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1328 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1324
1329
1325 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1330 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1326
1331
1327 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1332 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1328
1333
1329 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1334 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1330
1335
1331 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1336 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1332
1337
1333 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1338 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1334
1339
1335 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1340 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1336
1341
1337 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1342 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1338
1343
1339
1344
1340 def getFirstHeader(self):
1345 def getFirstHeader(self):
1341
1346
1342 raise NotImplementedError
1347 raise NotImplementedError
1343
1348
1344 def getData(self):
1349 def getData(self):
1345
1350
1346 raise NotImplementedError
1351 raise NotImplementedError
1347
1352
1348 def hasNotDataInBuffer(self):
1353 def hasNotDataInBuffer(self):
1349
1354
1350 raise NotImplementedError
1355 raise NotImplementedError
1351
1356
1352 def readBlock(self):
1357 def readBlock(self):
1353
1358
1354 raise NotImplementedError
1359 raise NotImplementedError
1355
1360
1356 def isEndProcess(self):
1361 def isEndProcess(self):
1357
1362
1358 return self.flagNoMoreFiles
1363 return self.flagNoMoreFiles
1359
1364
1360 def printReadBlocks(self):
1365 def printReadBlocks(self):
1361
1366
1362 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1367 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1363
1368
1364 def printTotalBlocks(self):
1369 def printTotalBlocks(self):
1365
1370
1366 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1371 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1367
1372
1368 def printNumberOfBlock(self):
1373 def printNumberOfBlock(self):
1369
1374
1370 if self.flagIsNewBlock:
1375 if self.flagIsNewBlock:
1371 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1376 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1372 self.processingHeaderObj.dataBlocksPerFile,
1377 self.processingHeaderObj.dataBlocksPerFile,
1373 self.dataOut.datatime.ctime())
1378 self.dataOut.datatime.ctime())
1374
1379
1375 def printInfo(self):
1380 def printInfo(self):
1376
1381
1377 if self.__printInfo == False:
1382 if self.__printInfo == False:
1378 return
1383 return
1379
1384
1380 self.basicHeaderObj.printInfo()
1385 self.basicHeaderObj.printInfo()
1381 self.systemHeaderObj.printInfo()
1386 self.systemHeaderObj.printInfo()
1382 self.radarControllerHeaderObj.printInfo()
1387 self.radarControllerHeaderObj.printInfo()
1383 self.processingHeaderObj.printInfo()
1388 self.processingHeaderObj.printInfo()
1384
1389
1385 self.__printInfo = False
1390 self.__printInfo = False
1386
1391
1387
1392
1388 def run(self, **kwargs):
1393 def run(self, **kwargs):
1389
1394
1390 if not(self.isConfig):
1395 if not(self.isConfig):
1391
1396
1392 # self.dataOut = dataOut
1397 # self.dataOut = dataOut
1393 self.setup(**kwargs)
1398 self.setup(**kwargs)
1394 self.isConfig = True
1399 self.isConfig = True
1395
1400
1396 self.getData()
1401 self.getData()
1397
1402
1398 class JRODataWriter(JRODataIO):
1403 class JRODataWriter(JRODataIO):
1399
1404
1400 """
1405 """
1401 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1406 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1402 de los datos siempre se realiza por bloques.
1407 de los datos siempre se realiza por bloques.
1403 """
1408 """
1404
1409
1405 blockIndex = 0
1410 blockIndex = 0
1406
1411
1407 path = None
1412 path = None
1408
1413
1409 setFile = None
1414 setFile = None
1410
1415
1411 profilesPerBlock = None
1416 profilesPerBlock = None
1412
1417
1413 blocksPerFile = None
1418 blocksPerFile = None
1414
1419
1415 nWriteBlocks = 0
1420 nWriteBlocks = 0
1416
1421
1417 fileDate = None
1422 fileDate = None
1418
1423
1419 def __init__(self, dataOut=None):
1424 def __init__(self, dataOut=None):
1420 raise NotImplementedError
1425 raise NotImplementedError
1421
1426
1422
1427
1423 def hasAllDataInBuffer(self):
1428 def hasAllDataInBuffer(self):
1424 raise NotImplementedError
1429 raise NotImplementedError
1425
1430
1426
1431
1427 def setBlockDimension(self):
1432 def setBlockDimension(self):
1428 raise NotImplementedError
1433 raise NotImplementedError
1429
1434
1430
1435
1431 def writeBlock(self):
1436 def writeBlock(self):
1432 raise NotImplementedError
1437 raise NotImplementedError
1433
1438
1434
1439
1435 def putData(self):
1440 def putData(self):
1436 raise NotImplementedError
1441 raise NotImplementedError
1437
1442
1438
1443
1439 def getProcessFlags(self):
1444 def getProcessFlags(self):
1440
1445
1441 processFlags = 0
1446 processFlags = 0
1442
1447
1443 dtype_index = get_dtype_index(self.dtype)
1448 dtype_index = get_dtype_index(self.dtype)
1444 procflag_dtype = get_procflag_dtype(dtype_index)
1449 procflag_dtype = get_procflag_dtype(dtype_index)
1445
1450
1446 processFlags += procflag_dtype
1451 processFlags += procflag_dtype
1447
1452
1448 if self.dataOut.flagDecodeData:
1453 if self.dataOut.flagDecodeData:
1449 processFlags += PROCFLAG.DECODE_DATA
1454 processFlags += PROCFLAG.DECODE_DATA
1450
1455
1451 if self.dataOut.flagDeflipData:
1456 if self.dataOut.flagDeflipData:
1452 processFlags += PROCFLAG.DEFLIP_DATA
1457 processFlags += PROCFLAG.DEFLIP_DATA
1453
1458
1454 if self.dataOut.code is not None:
1459 if self.dataOut.code is not None:
1455 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1460 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1456
1461
1457 if self.dataOut.nCohInt > 1:
1462 if self.dataOut.nCohInt > 1:
1458 processFlags += PROCFLAG.COHERENT_INTEGRATION
1463 processFlags += PROCFLAG.COHERENT_INTEGRATION
1459
1464
1460 if self.dataOut.type == "Spectra":
1465 if self.dataOut.type == "Spectra":
1461 if self.dataOut.nIncohInt > 1:
1466 if self.dataOut.nIncohInt > 1:
1462 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1467 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1463
1468
1464 if self.dataOut.data_dc is not None:
1469 if self.dataOut.data_dc is not None:
1465 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1470 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1466
1471
1467 if self.dataOut.flagShiftFFT:
1472 if self.dataOut.flagShiftFFT:
1468 processFlags += PROCFLAG.SHIFT_FFT_DATA
1473 processFlags += PROCFLAG.SHIFT_FFT_DATA
1469
1474
1470 return processFlags
1475 return processFlags
1471
1476
1472 def setBasicHeader(self):
1477 def setBasicHeader(self):
1473
1478
1474 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1479 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1475 self.basicHeaderObj.version = self.versionFile
1480 self.basicHeaderObj.version = self.versionFile
1476 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1481 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1477
1482
1478 utc = numpy.floor(self.dataOut.utctime)
1483 utc = numpy.floor(self.dataOut.utctime)
1479 milisecond = (self.dataOut.utctime - utc)* 1000.0
1484 milisecond = (self.dataOut.utctime - utc)* 1000.0
1480
1485
1481 self.basicHeaderObj.utc = utc
1486 self.basicHeaderObj.utc = utc
1482 self.basicHeaderObj.miliSecond = milisecond
1487 self.basicHeaderObj.miliSecond = milisecond
1483 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1488 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1484 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1489 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1485 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1490 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1486
1491
1487 def setFirstHeader(self):
1492 def setFirstHeader(self):
1488 """
1493 """
1489 Obtiene una copia del First Header
1494 Obtiene una copia del First Header
1490
1495
1491 Affected:
1496 Affected:
1492
1497
1493 self.basicHeaderObj
1498 self.basicHeaderObj
1494 self.systemHeaderObj
1499 self.systemHeaderObj
1495 self.radarControllerHeaderObj
1500 self.radarControllerHeaderObj
1496 self.processingHeaderObj self.
1501 self.processingHeaderObj self.
1497
1502
1498 Return:
1503 Return:
1499 None
1504 None
1500 """
1505 """
1501
1506
1502 raise NotImplementedError
1507 raise NotImplementedError
1503
1508
1504 def __writeFirstHeader(self):
1509 def __writeFirstHeader(self):
1505 """
1510 """
1506 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1511 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1507
1512
1508 Affected:
1513 Affected:
1509 __dataType
1514 __dataType
1510
1515
1511 Return:
1516 Return:
1512 None
1517 None
1513 """
1518 """
1514
1519
1515 # CALCULAR PARAMETROS
1520 # CALCULAR PARAMETROS
1516
1521
1517 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1522 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1518 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1523 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1519
1524
1520 self.basicHeaderObj.write(self.fp)
1525 self.basicHeaderObj.write(self.fp)
1521 self.systemHeaderObj.write(self.fp)
1526 self.systemHeaderObj.write(self.fp)
1522 self.radarControllerHeaderObj.write(self.fp)
1527 self.radarControllerHeaderObj.write(self.fp)
1523 self.processingHeaderObj.write(self.fp)
1528 self.processingHeaderObj.write(self.fp)
1524
1529
1525 def __setNewBlock(self):
1530 def __setNewBlock(self):
1526 """
1531 """
1527 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1532 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1528
1533
1529 Return:
1534 Return:
1530 0 : si no pudo escribir nada
1535 0 : si no pudo escribir nada
1531 1 : Si escribio el Basic el First Header
1536 1 : Si escribio el Basic el First Header
1532 """
1537 """
1533 if self.fp == None:
1538 if self.fp == None:
1534 self.setNextFile()
1539 self.setNextFile()
1535
1540
1536 if self.flagIsNewFile:
1541 if self.flagIsNewFile:
1537 return 1
1542 return 1
1538
1543
1539 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1544 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1540 self.basicHeaderObj.write(self.fp)
1545 self.basicHeaderObj.write(self.fp)
1541 return 1
1546 return 1
1542
1547
1543 if not( self.setNextFile() ):
1548 if not( self.setNextFile() ):
1544 return 0
1549 return 0
1545
1550
1546 return 1
1551 return 1
1547
1552
1548
1553
1549 def writeNextBlock(self):
1554 def writeNextBlock(self):
1550 """
1555 """
1551 Selecciona el bloque siguiente de datos y los escribe en un file
1556 Selecciona el bloque siguiente de datos y los escribe en un file
1552
1557
1553 Return:
1558 Return:
1554 0 : Si no hizo pudo escribir el bloque de datos
1559 0 : Si no hizo pudo escribir el bloque de datos
1555 1 : Si no pudo escribir el bloque de datos
1560 1 : Si no pudo escribir el bloque de datos
1556 """
1561 """
1557 if not( self.__setNewBlock() ):
1562 if not( self.__setNewBlock() ):
1558 return 0
1563 return 0
1559
1564
1560 self.writeBlock()
1565 self.writeBlock()
1561
1566
1562 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1567 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1563 self.processingHeaderObj.dataBlocksPerFile)
1568 self.processingHeaderObj.dataBlocksPerFile)
1564
1569
1565 return 1
1570 return 1
1566
1571
1567 def setNextFile(self):
1572 def setNextFile(self):
1568 """
1573 """
1569 Determina el siguiente file que sera escrito
1574 Determina el siguiente file que sera escrito
1570
1575
1571 Affected:
1576 Affected:
1572 self.filename
1577 self.filename
1573 self.subfolder
1578 self.subfolder
1574 self.fp
1579 self.fp
1575 self.setFile
1580 self.setFile
1576 self.flagIsNewFile
1581 self.flagIsNewFile
1577
1582
1578 Return:
1583 Return:
1579 0 : Si el archivo no puede ser escrito
1584 0 : Si el archivo no puede ser escrito
1580 1 : Si el archivo esta listo para ser escrito
1585 1 : Si el archivo esta listo para ser escrito
1581 """
1586 """
1582 ext = self.ext
1587 ext = self.ext
1583 path = self.path
1588 path = self.path
1584
1589
1585 if self.fp != None:
1590 if self.fp != None:
1586 self.fp.close()
1591 self.fp.close()
1587
1592
1588 timeTuple = time.localtime( self.dataOut.utctime)
1593 timeTuple = time.localtime( self.dataOut.utctime)
1589 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1594 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1590
1595
1591 fullpath = os.path.join( path, subfolder )
1596 fullpath = os.path.join( path, subfolder )
1592 setFile = self.setFile
1597 setFile = self.setFile
1593
1598
1594 if not( os.path.exists(fullpath) ):
1599 if not( os.path.exists(fullpath) ):
1595 os.mkdir(fullpath)
1600 os.mkdir(fullpath)
1596 setFile = -1 #inicializo mi contador de seteo
1601 setFile = -1 #inicializo mi contador de seteo
1597 else:
1602 else:
1598 filesList = os.listdir( fullpath )
1603 filesList = os.listdir( fullpath )
1599 if len( filesList ) > 0:
1604 if len( filesList ) > 0:
1600 filesList = sorted( filesList, key=str.lower )
1605 filesList = sorted( filesList, key=str.lower )
1601 filen = filesList[-1]
1606 filen = filesList[-1]
1602 # el filename debera tener el siguiente formato
1607 # el filename debera tener el siguiente formato
1603 # 0 1234 567 89A BCDE (hex)
1608 # 0 1234 567 89A BCDE (hex)
1604 # x YYYY DDD SSS .ext
1609 # x YYYY DDD SSS .ext
1605 if isNumber( filen[8:11] ):
1610 if isNumber( filen[8:11] ):
1606 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1611 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1607 else:
1612 else:
1608 setFile = -1
1613 setFile = -1
1609 else:
1614 else:
1610 setFile = -1 #inicializo mi contador de seteo
1615 setFile = -1 #inicializo mi contador de seteo
1611
1616
1612 setFile += 1
1617 setFile += 1
1613
1618
1614 #If this is a new day it resets some values
1619 #If this is a new day it resets some values
1615 if self.dataOut.datatime.date() > self.fileDate:
1620 if self.dataOut.datatime.date() > self.fileDate:
1616 setFile = 0
1621 setFile = 0
1617 self.nTotalBlocks = 0
1622 self.nTotalBlocks = 0
1618
1623
1619 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1624 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1620
1625
1621 filename = os.path.join( path, subfolder, filen )
1626 filename = os.path.join( path, subfolder, filen )
1622
1627
1623 fp = open( filename,'wb' )
1628 fp = open( filename,'wb' )
1624
1629
1625 self.blockIndex = 0
1630 self.blockIndex = 0
1626
1631
1627 #guardando atributos
1632 #guardando atributos
1628 self.filename = filename
1633 self.filename = filename
1629 self.subfolder = subfolder
1634 self.subfolder = subfolder
1630 self.fp = fp
1635 self.fp = fp
1631 self.setFile = setFile
1636 self.setFile = setFile
1632 self.flagIsNewFile = 1
1637 self.flagIsNewFile = 1
1633 self.fileDate = self.dataOut.datatime.date()
1638 self.fileDate = self.dataOut.datatime.date()
1634
1639
1635 self.setFirstHeader()
1640 self.setFirstHeader()
1636
1641
1637 print '[Writing] Opening file: %s'%self.filename
1642 print '[Writing] Opening file: %s'%self.filename
1638
1643
1639 self.__writeFirstHeader()
1644 self.__writeFirstHeader()
1640
1645
1641 return 1
1646 return 1
1642
1647
1643 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1648 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1644 """
1649 """
1645 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1650 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1646
1651
1647 Inputs:
1652 Inputs:
1648 path : directory where data will be saved
1653 path : directory where data will be saved
1649 profilesPerBlock : number of profiles per block
1654 profilesPerBlock : number of profiles per block
1650 set : initial file set
1655 set : initial file set
1651 datatype : An integer number that defines data type:
1656 datatype : An integer number that defines data type:
1652 0 : int8 (1 byte)
1657 0 : int8 (1 byte)
1653 1 : int16 (2 bytes)
1658 1 : int16 (2 bytes)
1654 2 : int32 (4 bytes)
1659 2 : int32 (4 bytes)
1655 3 : int64 (8 bytes)
1660 3 : int64 (8 bytes)
1656 4 : float32 (4 bytes)
1661 4 : float32 (4 bytes)
1657 5 : double64 (8 bytes)
1662 5 : double64 (8 bytes)
1658
1663
1659 Return:
1664 Return:
1660 0 : Si no realizo un buen seteo
1665 0 : Si no realizo un buen seteo
1661 1 : Si realizo un buen seteo
1666 1 : Si realizo un buen seteo
1662 """
1667 """
1663
1668
1664 if ext == None:
1669 if ext == None:
1665 ext = self.ext
1670 ext = self.ext
1666
1671
1667 self.ext = ext.lower()
1672 self.ext = ext.lower()
1668
1673
1669 self.path = path
1674 self.path = path
1670
1675
1671 if set is None:
1676 if set is None:
1672 self.setFile = -1
1677 self.setFile = -1
1673 else:
1678 else:
1674 self.setFile = set - 1
1679 self.setFile = set - 1
1675
1680
1676 self.blocksPerFile = blocksPerFile
1681 self.blocksPerFile = blocksPerFile
1677
1682
1678 self.profilesPerBlock = profilesPerBlock
1683 self.profilesPerBlock = profilesPerBlock
1679
1684
1680 self.dataOut = dataOut
1685 self.dataOut = dataOut
1681 self.fileDate = self.dataOut.datatime.date()
1686 self.fileDate = self.dataOut.datatime.date()
1682 #By default
1687 #By default
1683 self.dtype = self.dataOut.dtype
1688 self.dtype = self.dataOut.dtype
1684
1689
1685 if datatype is not None:
1690 if datatype is not None:
1686 self.dtype = get_numpy_dtype(datatype)
1691 self.dtype = get_numpy_dtype(datatype)
1687
1692
1688 if not(self.setNextFile()):
1693 if not(self.setNextFile()):
1689 print "[Writing] There isn't a next file"
1694 print "[Writing] There isn't a next file"
1690 return 0
1695 return 0
1691
1696
1692 self.setBlockDimension()
1697 self.setBlockDimension()
1693
1698
1694 return 1
1699 return 1
1695
1700
1696 def run(self, dataOut, **kwargs):
1701 def run(self, dataOut, **kwargs):
1697
1702
1698 if not(self.isConfig):
1703 if not(self.isConfig):
1699
1704
1700 self.setup(dataOut, **kwargs)
1705 self.setup(dataOut, **kwargs)
1701 self.isConfig = True
1706 self.isConfig = True
1702
1707
1703 self.putData()
1708 self.putData()
1704
1709
General Comments 0
You need to be logged in to leave comments. Login now