Changeset 154


Ignore:
Timestamp:
09/30/2009 05:10:27 PM (14 years ago)
Author:
marcus
Message:
  • new tests
  • fixed spelling
  • fixed access of trace info entries
  • create traces from numpy data (some double free issue remains)
  • minor debug plot adjustments
Location:
SHX/trunk/src/SeismicHandler
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler/core/modules/Trace.py

    r153 r154  
    5959            self.update(memblc=kwargs.get("memblc")) 
    6060 
     61        # create new trace from numpy data 
     62        # additional information required - at least delta 
     63        if kwargs.has_key("data") and kwargs.has_key("delta"): 
     64            self._create(**kwargs) 
     65 
    6166    def __len__(self): 
    6267        """return number of samples""" 
     
    9398                    raise InfoEntryReadOnlyError(name.upper()) 
    9499 
    95                 self.__dict__["_info"][name] = value 
     100                self.__dict__["_info"][name.upper()] = value 
    96101                shlib().setInfoEntry(self.address, name, value) 
    97102 
     
    173178        self._info = info 
    174179#        self.listInfo() 
     180 
     181    def _create(self, **kwargs): 
     182        slib = shlib().sh 
     183        slib.db_newlist() 
     184 
     185        slib.db_create.restype = C.POINTER(MEMBLC) 
     186 
     187        status = C.c_int(0) 
     188        memblc = slib.db_create(C.byref(status)) 
     189 
     190        slib.ml_inittrc.argtypes = [C.POINTER(MEMBLC), C.POINTER(C.c_float), C.c_int, C.c_float] 
     191        slib.ml_inittrc(memblc, kwargs["data"], len(kwargs["data"]), kwargs["delta"]) 
     192 
     193        self.address = C.addressof(memblc.contents) 
     194        self.update(memblc=memblc.contents) 
    175195 
    176196class Traces(object): 
     
    278298        from SeismicHandler.core.modules.Graphics import pylab 
    279299 
    280         fig = pylab.figure() 
     300        fig = pylab.figure(figsize=(12,7)) 
    281301        fig.canvas.set_window_title("SHX - plot of %u traces" % len(self)) 
    282         pylab.subplots_adjust(hspace=0.1, wspace=20, left=0.1, right=0.95, bottom=0.05, top=0.95) 
     302        pylab.subplots_adjust(hspace=0.1, wspace=20, left=0.1, right=0.98, bottom=0.07, top=0.95) 
    283303 
    284304        fig.set_facecolor('w') 
    285305 
     306        maxx = 0 
    286307        for i, trc in enumerate(self): 
    287308            # link x axis if more than one trace 
     
    297318            ax.yaxis.set_major_locator(pylab.NullLocator()) 
    298319 
    299             # build label from station ... 
     320            # build label from number... 
     321            label = "%u: " % (i+1,) 
     322            # ... station ... 
    300323            if trc.station: 
    301                 label = trc.station 
     324                label += trc.station 
     325            else: 
     326                label += "-?-" 
    302327            # ... and component name 
    303328            if trc.comp: 
    304329                label += " " + trc.comp 
     330            else: 
     331                label += " -?-" 
    305332 
    306333            try: 
     
    314341            else: 
    315342                ax.xaxis.tick_bottom() 
    316  
    317             pylab.plot(np.arange(0, trc.delta * trc.length, trc.delta), trc.fetchData(), "k", linewidth=.5) 
     343                ax.set_xlabel("time in seconds") 
     344 
     345            duration = trc.delta * trc.length 
     346            pylab.plot(np.arange(0, duration, trc.delta), trc.fetchData(), "k", linewidth=.5) 
     347 
     348            maxx = duration > maxx and duration or maxx 
    318349 
    319350            # add some extra space around traces 
    320351            limit = abs(max(pylab.ylim(), key=abs)) * 1.1 
    321352            pylab.ylim(-limit, limit) 
     353 
     354        # set identical maximum x dimension for all traces 
     355        for i in fig.axes: 
     356            i.axis(xmax=maxx) 
    322357 
    323358        if save: 
     
    327362 
    328363if __name__ == "__main__": 
    329     t = Traces() 
    330364    from SeismicHandler.core.commands import Run 
    331365 
    332     MessageService.block(Message(MessageService.REDRAW)) 
     366    data = np.random.randn(1500).astype(np.float32) 
     367    x = Trace(data=(C.c_float*len(data)).from_address(data.ctypes.get_data()), delta=.05) 
     368    x.station = "testx".upper() 
     369    x.comp = "N" 
     370    x.chan1 = "B" 
     371    x.chan2 = "H" 
     372 
     373    data2 = np.random.randn(1800).astype(np.float32) 
     374    y = Trace(data=(C.c_float*len(data2)).from_address(data2.ctypes.get_data()), delta=.05) 
     375 
    333376    # create 2 hours of synthetic data (100 Hz) 
    334377    # position set by counter 
    335378    for i in range(5): 
    336         Run("CREATE SHARP 0.05 100 1. %u .1 .5" % (10+i*6)) 
     379        Run("CREATE SHARP 0.05 100 10. %u .1 .5" % (10+i*6)) 
    337380#        Run("create sharp 0.1 7200 1 %u 0.005 .05" % 5**(i+1)) 
    338381 
    339382    Run("sum all") 
    340     MessageService.unblock(Message(MessageService.REDRAW)) 
    341  
    342     t.plot(True) 
     383 
     384    t = Traces() 
     385    t.plot() 
  • SHX/trunk/src/SeismicHandler/core/shlib.py

    r153 r154  
    8181 
    8282        # somewhat needless, since c lib doesn't deal which command line parameters 
    83         status= C.c_int() 
     83        status = C.c_int() 
    8484        CSTR_ARRAY = C.c_char_p * 1 
    8585        argv = CSTR_ARRAY("") 
     
    144144               "char": "db_setc", 
    145145               "real": "db_setr", 
    146                "flag": "db_setf", # XXX  
     146               "flag": "db_setf", # XXX 
    147147               "pointer": "db_setp", # XXX not needed (still unchangeable) 
    148148               "time": "db_sett", 
     
    176176#        self.call(StringIO("SHSTRTUP")) 
    177177#        return 
    178      
     178 
    179179        self.call(StringIO("wdw/dc=s/ic=s/main")) 
    180180        self.call(StringIO("wdw/ic=s")) 
     
    228228            except StopIteration: 
    229229                break 
    230              
    231             # make changeable string (strings in python may no be altered!)  
     230 
     231            # make changeable string (strings in python may no be altered!) 
    232232            cmdstr = C.create_string_buffer(cmdstr) 
    233233 
  • SHX/trunk/src/SeismicHandler/tests/test_traces.py

    r153 r154  
    88import unittest 
    99 
    10 class eventsTestCase(unittest.TestCase): 
     10class tracesTestCase(unittest.TestCase): 
    1111    def setUp(self): 
    1212        Run("cresharp") 
     
    192192 
    193193def suite(): 
    194     return unittest.makeSuite(eventsTestCase, 'test') 
     194    return unittest.makeSuite(tracesTestCase, 'test') 
    195195 
    196196if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.