Advertisement
Guest User

Interacting between ATpy and TOPCAT using SAMPy

a guest
Sep 10th, 2010
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. # The atexit module is just to try and close off zombie clients and hubs.
  2. # This could be done with a class deconstructor (if that's what they're called in python), but I've been told they're unreliable.
  3.  
  4. import atpy, atexit, numpy, os, sampy
  5.  
  6. class sampCommunications:
  7.  
  8.     def __init__(self):
  9.         # Before we start, let's kill off any zombies
  10.         try: samp.kill()
  11.         except: pass
  12.        
  13.         # sampy seems to fall over sometimes if 'localhost' isn't specified, even though it shouldn't
  14.         self.hub = sampy.SAMPHubServer(addr='localhost')
  15.        
  16.         self.hub.start()
  17.        
  18.         self.metadata = {
  19.                             'samp.name' : 'ATPy module',
  20.                             'samp.icon.url' : 'http://dribbble.com/system/users/2302/avatars/thumb/Panda2.png?1281017958', # Gratuitous cute panda
  21.                             'samp.description.text' : 'ATPy Module',
  22.                             'client.version' : '0.9.4'
  23.                         }
  24.        
  25.         self.client = sampy.SAMPIntegratedClient(metadata=self.metadata, addr='localhost')
  26.         self.client.connect()
  27.        
  28.         # Bind interaction functions - we will register that we want to listen for table.highlight.row (the SAMP highlight protocol), and all the typical SAMP-stuff
  29.         # We could include this to listen for other sorts of subscriptions like pointAt(), etc.
  30.         self.client.bindReceiveNotification('table.highlight.row', self.highlightRow)
  31.         self.client.bindReceiveNotification('samp.app.*', self.receiveNotification)
  32.         self.client.bindReceiveCall('samp.app.*', self.receiveCall)
  33.         self.client.bindReceiveResponse('*', self.receiveResponse)
  34.        
  35.         # Try to avoid zombie hubs and clients
  36.         atexit.register(self.kill)
  37.        
  38.        
  39.     # Try to die quietly
  40.     def kill(self):
  41.         if self.client.isConnected(): self.client.disconnect()
  42.         if self.hub._is_running: self.hub.stop()
  43.        
  44.  
  45.     # Generic response protocols
  46.     def receiveNotification(self, privateKey, senderID, mType, params, extra):
  47.         print '[SAMP] Notification ', privateKey, senderID, mType, params, extra
  48.            
  49.     def receiveResponse(self, privateKey, senderID, msgID, response):
  50.         print '[SAMP] Response ', privateKey, senderID, msgID, response
  51.        
  52.     def receiveCall(self, privateKey, senderID, msgID, mType, params, extra):
  53.         print '[SAMP] Call ', privateKey, senderID, msgID, mType, params, extra
  54.         self.client.ereply(msgID, sampy.SAMP_STATUS_OK, result = {'txt' : 'printed' })
  55.        
  56.     # Let's see if topcat is running - this will return the first version of topcat running, if there are multiple/zombies
  57.     def isTopcatRunning(self):
  58.    
  59.         neighbours = self.client.getRegisteredClients()
  60.        
  61.         for neighbour in neighbours:
  62.             metadata = self.client.getMetadata(neighbour)
  63.            
  64.             try:
  65.                 if (metadata['samp.name'] == 'topcat'):
  66.                     self.topcat = neighbour
  67.                     return True
  68.                
  69.             except KeyError:
  70.                 continue
  71.                    
  72.         self.topcat = None
  73.         return False
  74.        
  75.     # Broadcast a table file to TOPCAT
  76.     def broadcastTable(self, table):
  77.    
  78.         metadata = {
  79.                     'samp.mtype' : 'table.load.votable',
  80.                     'samp.params' : {
  81.                                      'name' : table,
  82.                                      'table-id' : table,
  83.                                      'url' : 'file://' + os.getcwd() + '/' + table
  84.                                     }
  85.                    }
  86.                    
  87.         if self.isTopcatRunning(): return self.client.notify(self.topcat, metadata)
  88.         else: return False
  89.    
  90.     def highlightRow(self, privateKey, senderID, mType, params, extra):
  91.         print '[SAMP] Highlighted row', privateKey, senderID, mType, params, extra
  92.        
  93.         if self.isTopcatRunning() and (senderID == self.topcat):
  94.            
  95.             try:
  96.                 filename, row = [params['url'], int(params['row'])]
  97.             except KeyError:
  98.                 print '[SAMP] Highlighted row was missing vital information'
  99.             else:
  100.                 print 'TOPCAT tells us that row %s of file %s was highlighted!' % (row, filename)
  101.        
  102.         else:
  103.             # Interactions with Aladin?
  104.             pass
  105.        
  106.  
  107. # In the code that I use, my spectra is analysed by a function, and before it starts it initiates a sampCommunications() class
  108. # This gives TOPCAT a second to connect to the hub
  109. samp = sampCommunications()
  110.  
  111. # Some data is generated from my program. Let's use some mock data.
  112. x = numpy.arange(0, 100)
  113. y = x**2
  114.  
  115. # Put this data into a VO table and save it
  116. table = atpy.Table()
  117.  
  118. table.add_column('x', x)
  119. table.add_column('y(x) = x^2', y)
  120.  
  121. table.write('test.xml', verbose=False)
  122.  
  123.  
  124. # Now broadcast the table to TOPCAT
  125. samp.broadcastTable('test.xml')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement