Changeset 146
- Timestamp:
- 09/02/2009 06:20:53 PM (13 years ago)
- Location:
- SHX/trunk/src/SeismicHandler
- Files:
-
- 37 added
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/src/SeismicHandler/core/commands/run.py
r139 r146 34 34 graphical output""" 35 35 36 cmdstr = ""37 38 36 def __init__(self, *args, **kwargs): 37 self.cmdstr = "" 38 39 39 try: 40 40 self.cmdstr = args[0] -
SHX/trunk/src/SeismicHandler/core/error.py
r144 r146 63 63 return """Command "%s" was removed!""" % self.cmd 64 64 65 class CommandNotSuccessfulError(Error): 66 def __init__(self, cmd, errorcode): 67 Error.__init__(self) 68 self.cmd = cmd 69 self.code = errorcode 70 71 def __str__(self): 72 return """Command "%s" failed with error code %u!""" % (self.cmd, self.code) 73 65 74 class InfoEntryNotFoundError(Error): 66 75 def __init__(self, name): -
SHX/trunk/src/SeismicHandler/core/log.py
r139 r146 24 24 import logging 25 25 import os 26 import atexit 26 27 27 28 class Logging(logging.Logger): … … 38 39 def __init__(self): 39 40 """configure logging once""" 40 41 41 42 logging.Logger.__init__(self, "shx") 42 43 … … 49 50 # assume linux system 50 51 dir = "/tmp/" 52 53 # recent logfile depends on pid - concurrently running processes get 54 # a new logfile each 55 logfile = 'sh-%u.log' % os.getpid() 56 self.logdest = os.path.join(dir, logfile) 57 58 # backup location depends on user id (just to avoid permission issues) 59 backupfile = 'shbckp-%s.log' % os.getuid() 60 self.backupdest = os.path.join(dir, backupfile) 51 61 52 62 # add more levels … … 64 74 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 65 75 datefmt='%Y-%m-%d %H:%M:%S', 66 filename= os.path.join(dir, 'sh.log'),76 filename=self.logdest, 67 77 filemode='w') 68 78 except IOError: … … 90 100 self.logging = logging 91 101 102 atexit.register(self.shutdown) 103 92 104 def debug2(self, args, kwargs): 93 105 if self.manager.disable >= logging.DEBUG+1: 94 return 95 106 return 107 108 def shutdown(self): 109 """Move our logfile to backup destination for later checking. 110 111 Please note that the backup will be overwritten by each shutdown. So 112 it may not be consistent!""" 113 114 # This method is called later by atexit, so we need to reimport the os 115 # module. 116 import os 117 118 try: 119 os.rename(self.logdest, self.backupdest) 120 except Exception, e: 121 print e 122 96 123 Logging() 97 124 -
SHX/trunk/src/SeismicHandler/core/modules/Trace.py
r144 r146 60 60 61 61 def __len__(self): 62 """return length of trace in milliseconds"""63 64 return self. delta * self.length * 100062 """return number of samples""" 63 64 return self.length 65 65 66 66 def __getattr__(self, name): … … 82 82 83 83 def __setattr__(self, name, value): 84 idxmap = shlib().idx 85 84 86 try: 85 if not name.upper() in self.__dict__["_info"]: 87 # if not found in info entry list set it as normal class attribute 88 if not name.upper() in idxmap: 86 89 self.__dict__[name] = value 87 90 else: 88 91 # check for readonly attributes 89 idxmap = shlib().idx90 91 92 if idxmap[name.upper()][1] & RDONLY: 92 93 raise InfoEntryReadOnlyError(name.upper()) … … 125 126 def update(self, **kwargs): 126 127 if not kwargs.has_key("memblc"): 127 r eturn False128 raise NotImplementedError("Only updating from memory block supported.") 128 129 129 130 self.logger.info("updating from memory block, trace id %s, address %u" % (self.id, self.address)) … … 187 188 self.sh = shlib().sh 188 189 EventManager.subscribe(Event(EventManager.REDRAW), self.update) 190 EventManager.subscribe(Event(EventManager.TRACEUPDATE), self.update) 189 191 self.logger = logging.getLogger("modules.traces") 190 192 self.update() … … 199 201 if self.progress: 200 202 self.logger.debug("update already running, skip!") 203 return 201 204 else: 202 205 self.progress = 1 … … 217 220 self.tracedata[t].delete() 218 221 222 self.progress = 0 219 223 return 220 224 … … 264 268 """return trace by index using natural count (start from one) 265 269 """ 266 return self.trace s[number-1]270 return self.tracedata[self.traces[number-1]] 267 271 268 272 if __name__ == "__main__": … … 285 289 pylab.subplots_adjust(hspace=0.00001,wspace=20) 286 290 287 for i, trc in enumerate(t.traces): 288 x = t[i] 289 290 x.station = "TST%u" % i 291 x.zoom = 5.5 292 x.comment = "Tell me what you think of me #%u" % i 293 x.chan1 = "B" 294 x.chan2 = "H" 295 x.comp = "N" 291 for i, trc in enumerate(t): 292 trc.station = "TST%u" % i 293 trc.zoom = 5.5 294 trc.comment = "Tell me what you think of me #%u" % i 295 trc.chan1 = "B" 296 trc.chan2 = "H" 297 trc.comp = "N" 296 298 297 299 EventManager.trigger(Event(EventManager.REDRAW)) 298 300 299 # x.listInfo()301 # trc.listInfo() 300 302 301 303 ax = pylab.subplot(len(t),1,i+1) … … 305 307 ax.xaxis.set_major_locator(pylab.NullLocator()) 306 308 307 pylab.plot(np.arange(0, x.delta * x.length, x.delta), x.fetchData(), "k")308 # x.plot()309 pylab.plot(np.arange(0, trc.delta * trc.length, trc.delta), trc.fetchData(), "k") 310 # trc.plot() 309 311 310 312 pylab.show() -
SHX/trunk/src/SeismicHandler/core/shlib.py
r143 r146 34 34 35 35 from SeismicHandler.core.log import logging 36 from SeismicHandler.core.error import MultipleLibError, CommandStreamError, GotoTargetNotFoundError 36 from SeismicHandler.core.error import MultipleLibError, CommandStreamError, GotoTargetNotFoundError, CommandNotSuccessfulError 37 37 from SeismicHandler.config.options import environment 38 38 from SeismicHandler.core.shheaders import LINELENGTH, NOERROR, PARAM, CAPCNV, EMPTYVALUES … … 224 224 except StopIteration: 225 225 break 226 227 # make changeable string (strings in python may no be altered!) 228 cmdstr = C.create_string_buffer(cmdstr) 226 229 227 230 # upper case convert? … … 237 240 if status.value == NOERROR: 238 241 slib.tr_partrans(C.byref(cmd), C.byref(status)) 242 else: 243 raise CommandNotSuccessfulError(cmdstr.value, status.value) 239 244 240 245 logger.debug("command elements: %s", [y.value for y in cmd.p[:cmd.pno+1]]) … … 299 304 logger.info("execution not successful, error code %u" % status.value) 300 305 slib.se_dsplymsg(cc, status) 306 raise CommandNotSuccessfulError(cmdstr.value, status.value) 301 307 else: 302 308 pass -
SHX/trunk/src/SeismicHandler/startup.py
r144 r146 37 37 from SeismicHandler.core.commands import * 38 38 from SeismicHandler.core.modules.Variables import System, Symbol 39 from SeismicHandler.core.modules.Trace import Traces as xTraces 40 41 # access to traces 42 Traces = xTraces() 43 del xTraces 39 44 40 45 # just init adopted cmd line interface -
SHX/trunk/src/SeismicHandler/tests/test_commands.py
r144 r146 6 6 from SeismicHandler.core.commands import Run 7 7 8 import pickle 9 import os 10 8 11 import unittest 9 12 … … 13 16 14 17 def tearDown(self): 18 # Following every test all traces are deleted. 15 19 Run("del all") 16 20 21 def loadCompare(self, name): 22 return pickle.load(open(os.path.join("data", os.path.extsep.join((name, "pkl"))))) 23 24 def checkTraceData(self, trc, cmp=None, cmpindex=None, digits=7): 25 """Compare trc data either to cmp data or data from cmpindex 26 27 For accuracy adjustment use the digits parameter.""" 28 29 if cmp == None: 30 check = self.loadCompare(cmpindex) 31 else: 32 check = cmp 33 34 # Check for identical length. 35 self.assertEqual(len(trc), len(check)) 36 37 # Compare data points itself. 38 for i, dat in enumerate(check): 39 self.assertAlmostEqual(dat, trc.fetchData()[i], digits) 40 41 def testAl(self): 42 # XXX Commands for visualisation are not tested yet. 43 pass 44 17 45 def testAm(self): 18 46 # Create synthetic trace of 100 seconds length. 19 # Maximum amplitude 113 at position 15. 47 # Maximum amplitude 113 at position 15. 20 48 Run("create sharp 0.05 100 113. 15. .1 .5") 21 22 # create symbols (we want to set the result type from python 49 50 # create symbols (we want to set the result type from python) 23 51 Symbol.min = 0. 24 52 Symbol.minpos = 0. … … 26 54 Symbol.maxpos = 0. 27 55 Run("am 1 0. 100. &min &max &minpos &maxpos") 28 56 29 57 # test values taken from SH command line version 30 58 self.assertAlmostEqual(Symbol.min, -60.2811) 31 59 self.assertAlmostEqual(Symbol.minpos, 24.05) 32 60 self.assertAlmostEqual(Symbol.max, 113.00) 33 # please note that the maximum position differs from input (15)34 61 self.assertAlmostEqual(Symbol.maxpos, 17.75) 35 62 36 63 def testAppend(self): 37 pass 38 64 # Create two synthetic traces. 65 EventManager.block(Event(EventManager.REDRAW)) 66 Run("cresharp") 67 Run("cresharp") 68 EventManager.unblock(Event(EventManager.REDRAW)) 69 70 self.assertEqual(self.traces[0].length, 1000) 71 self.assertEqual(self.traces[1].length, 1000) 72 73 # Append trace 1 to trace 2 74 Run("append 1 2") 75 76 self.assertEqual(self.traces[0].length, 1000) 77 self.assertEqual(self.traces[1].length, 2000) 78 79 # Data for comparison saved from SH command line 80 # and converted to pickle. 81 check = self.loadCompare("cresharp") 82 83 # Check trace 1. 84 self.checkTraceData(self.traces[0], check) 85 86 # Check trace 2, simply append comparison data again. 87 self.checkTraceData(self.traces[1], check*2) 88 89 def testArp(self): 90 # Create synthetic trace and run auto correlation three times. 91 Run("cresharp") 92 Run("arp 1 2 1.8 -0.9") 93 Run("arp 1 2 1.8 -0.9") 94 Run("arp 1 2 1.8 -0.9") 95 96 # Unfortunately exporting small floats to ascii format has a very 97 # poor accuracy, so we just use 3 decimal places. 98 self.checkTraceData(self.traces[0], cmpindex="cresharp-arp", digits=3) 99 100 def testBeam(self): 101 # XXX Commands for visualisation are not tested yet. 102 pass 103 104 def testCalc(self): 105 # Calculation operations can be done in python very easily and more 106 # flexible. Except for time data type everything is just built-in. 107 pass 108 109 def testCall(self): 110 # The call command just fills symbol variables. Access to them are 111 # tested in test_variables case. 112 pass 113 114 def testCmd(self): 115 # This is more or less replaced by the history function in SHX. 116 pass 117 118 def testConnect(self): 119 Run("cresharp") 120 Run("cresharp") 121 Run("connect mul 1 2") 122 # This should more or less restore the original trace. 123 Run("connect div 3 1") 124 125 self.checkTraceData(self.traces[2], cmpindex="cresharp-connectmul") 126 self.checkTraceData(self.traces[3], cmpindex="cresharp", digits=5) 127 128 def testCopy(self): 129 Run("cresharp") 130 Run("copy 1 20 40") 131 132 # Compare data from 20 to 40 seconds (and add extra sample, the "seismosample") 133 check = self.loadCompare("cresharp")[20*20:40*20+1] 134 135 self.checkTraceData(self.traces[1], check) 136 137 def testCorr(self): 138 # This test also fails. The resulting trace is somehow scaled down to 139 # a maximum amplitude if 1. Also the trace seems to be distorted... 140 Run("cresharp") 141 Run("cresharp") 142 143 # I set the correlation length to be save, that's the default. 144 Run("corrl -30.0 100.0") 145 Run("corr 1 15. 25. 2") 146 147 # self.traces[2].plot() 148 self.checkTraceData(self.traces[2], cmpindex="cresharp-corr") 149 150 def testCorrl(self): 151 # There's not method to check the values of correlation length. 152 pass 153 154 def testCreate(self): 155 Run("create spike 0.05 250 20 40") 156 Run("create sharp 0.05 250 20 20 .03 .5") 157 Run("create gauss 0.05 250 20 125 5") 158 Run("create exp 0.05 250 20 40 30") 159 # Obviously we cannot test the random trace creation. :) 160 161 self.checkTraceData(self.traces[0], cmpindex="create-spike") 162 self.checkTraceData(self.traces[1], cmpindex="create-sharp", digits=5) 163 self.checkTraceData(self.traces[2], cmpindex="create-gauss", digits=5) 164 self.checkTraceData(self.traces[3], cmpindex="create-exp", digits=5) 165 166 def testCurve(self): 167 # XXX Commands for visualisation are not tested yet. 168 pass 169 170 def testCut(self): 171 Run("cresharp") 172 173 # Just to show that there is a natural counting access to traces. 174 # This is identical to "trc = self.traces[0]" 175 trc = self.traces.get(1) 176 177 # By definition all traces without time information start on 178 # 01-Jul-1970 12:00:00.000 which results in 15681600 seconds unix epoch. 179 start = trc.start.hi 180 self.assertAlmostEqual(start, 15681600) 181 self.assertAlmostEqual(trc.start.lo, 0) 182 183 Run("cut 1 20 40") 184 185 self.checkTraceData(trc, cmpindex="cut") 186 187 # Since the first 20 seconds were cut, the trace start is shifted 188 self.assertEquals(trc.start.hi, start + 20) 189 self.assertAlmostEqual(trc.start.lo, 0) 190 191 def testDecimate(self): 192 Run("cresharp") 193 self.assertAlmostEqual(self.traces[0].delta, 0.05) 194 195 Run("decimate 1 4") 196 self.assertAlmostEqual(self.traces[0].delta, 0.20) 197 198 self.checkTraceData(self.traces[0], cmpindex="decimate") 199 200 def testDefault(self): 201 # Interactive prompting is not tested yet. 202 pass 203 204 def testDel(self): 205 Run("cresharp") 206 self.assertEqual(len(self.traces), 1) 207 Run("del 1") 208 self.assertEqual(len(self.traces), 0) 209 210 def testDelay_sum(self): 211 # XXX Commands for visualisation are not tested yet. 212 pass 213 214 def testDemean(self): 215 Run("cresharp") 216 Run("demean 1") 217 218 self.checkTraceData(self.traces[0], cmpindex="demean") 219 220 def testDerive(self): 221 Run("cresharp") 222 Run("derive 1 3") 223 224 self.checkTraceData(self.traces[0], cmpindex="derive") 225 226 def testDespike(self): 227 Run("create spike 0.05 50 20 20") 228 Run("despike 1 100.") 229 230 # Now we should have only zeros in trace data: 231 trc = self.traces.get(1) 232 self.checkTraceData(trc, [0.,]*trc.length) 233 234 def testDisplay(self): 235 # XXX Commands for visualisation are not tested yet. 236 pass 237 238 def testDtw(self): 239 # XXX Commands for visualisation are not tested yet. 240 pass 241 242 def testEcho(self): 243 # This function is not useful in python context. 244 pass 245 246 def testEchoCh(self): 247 # This function is not useful in python context. 248 pass 249 250 def testEnter(self): 251 # This function is not useful in python context. 252 pass 253 254 def testEntry(self): 255 # Note: Since info entries cannot be unset, this will affect every 256 # test case following this one. 257 Run("entry define foo s 3 3") 258 259 # Reload info entries. 260 EventManager.trigger(Event(EventManager.INFOIDXUPDATE)) 261 262 # Create test trace and assign value to created info entry. 263 Run("cresharp") 264 self.traces.get(1).foo = "lala" 265 266 # Trace data has been altered. 267 EventManager.trigger(Event(EventManager.TRACEUPDATE)) 268 269 self.assertEqual(self.traces.get(1).foo, "lala") 270 271 def testExec(self): 272 # This function is not useful in python context. 273 pass 274 275 def testExtract(self): 276 # XXX q-file related function are not tested yet. 277 pass 278 279 def testFct(self): 280 # XXX Some subfunctions are display related, some not. 281 pass 282 283 def testFft(self): 284 # XXX This test fails. 285 286 Run("cresharp") 287 # Increase amplitude 288 Run("trcfct 1 mul 100") 289 Run("fft 1 5 30 1") 290 291 # self.traces[1].plot() 292 self.checkTraceData(self.traces[1], cmpindex="fft") 293 294 def testFilter(self): 295 Run("create spike 0.05 50 20 20") 296 Run("fili f BP_2S_10HZ_4") 297 Run("filter f 1") 298 299 self.checkTraceData(self.traces[1], cmpindex="filter", digits=5) 300 301 testFili = testFilter 302 303 def testFold(self): 304 Run("cresharp") 305 Run("cresharp") 306 Run("fold 1 10 20 2") 307 308 self.checkTraceData(self.traces[2], cmpindex="fold", digits=5) 309 310 def testGoto(self): 311 # This command is already replaced by python 312 pass 313 314 def testHc(self): 315 # This function is not useful in python context. 316 pass 317 318 def testHelp(self): 319 # This function is not useful in python context. 320 pass 321 322 def testHide(self): 323 # XXX Commands for visualisation are not tested yet. 324 pass 325 326 def testIf(self): 327 # This command is already replaced by python 328 pass 329 330 def testInt(self): 331 Run("cresharp") 332 Run("int 1") 333 334 self.checkTraceData(self.traces[1], cmpindex="int", digits=5) 335 39 336 def testSum(self): 40 337 # this test fails for some reason 41 338 # the summed trace has *never* a length greater 100 seconds 42 339 43 340 EventManager.block(Event(EventManager.REDRAW)) 44 341 45 342 # if you set seconds to 100 or lower, everything works fine 46 343 # this is independent from sampling rate(!) 47 seconds, delta = 1 10., 0.0148 49 # create 2 identical synthetics with "seconds" length, 1/"delta" Hz 344 seconds, delta = 120., 0.01 345 346 # create 2 identical synthetics with "seconds" length, 1/"delta" Hz 50 347 for i in xrange(2): 51 348 Run("CREATE SHARP %.2f %u 1. 15. .1 .5" % (delta, seconds)) 52 349 53 350 Run("sum all") 54 55 # Run("fili f BP_2S_10HZ_4") 56 # Run("filter f 1") 57 351 58 352 EventManager.unblock(Event(EventManager.REDRAW)) 59 60 for i in range(len(self.traces)): 61 trc = self.traces[i+1] 353 354 for trc in self.traces: 62 355 self.assertAlmostEqual(trc.delta, delta, 6) 356 63 357 self.assertEqual(trc.length, seconds/delta) 64 self.assertAlmostEqual(len(trc) /1000., seconds, 1)358 self.assertAlmostEqual(len(trc), seconds/delta, 1) 65 359 66 360 def suite(): 67 361 return unittest.makeSuite(commandsTestCase, 'test') 362 # return unittest.makeSuite(commandsTestCase, 'test2') 68 363 69 364 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.