Package glue :: Module cis
[hide private]
[frames] | no frames]

Source Code for Module glue.cis

  1  # Copyright (C) 2013 Duncan Macleod 
  2  # 
  3  # This program is free software; you can redistribute it and/or modify it 
  4  # under the terms of the GNU General Public License as published by the 
  5  # Free Software Foundation; either version 3 of the License, or (at your 
  6  # option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, but 
  9  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 11  # Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License along 
 14  # with this program; if not, write to the Free Software Foundation, Inc., 
 15  # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
 16   
 17  """Interface to the LIGO Channel Information System 
 18   
 19  Queries can be made using any part of a channel name, returning a list 
 20  of entries (key,value dicts) of found channels:: 
 21   
 22  >>> from glue import cis 
 23  >>> out = cis.query('PSL-ISS_PSB_OUT_DQ') 
 24  >>> print([channel['name'] for channel in out]) 
 25  [u'H1:PSL-ISS_PDB_OUT_DQ', u'H2:PSL-ISS_PDB_OUT_DQ', u'L1:PSL-ISS_PDB_OUT_DQ'] 
 26   
 27  (example accessed August 26 2013). 
 28  """ 
 29   
 30  import json 
 31  import numpy 
 32  from six.moves import urllib 
 33   
 34  from glue.auth.saml import HTTPNegotiateAuthHandler 
 35   
 36  from . import (git_version, auth) 
 37   
 38  __author__ = "Duncan Macleod <duncan.macleod@ligo.org>" 
 39  __version__ = git_version.id 
 40  __date__ = git_version.date 
 41   
 42  CIS_API_URL = 'https://cis.ligo.org/api/channel' 
 43  CIS_DATA_TYPE = {4: numpy.float32} 
 44   
 45   
46 -def query(name, debug=False):
47 """Query the Channel Information System for details on the given 48 channel name 49 50 Example:: 51 52 >>> from glue import cis 53 >>> out = cis.query('PSL-ISS_PSB_OUT_DQ') 54 >>> print([channel['name'] for channel in out]) 55 [u'H1:PSL-ISS_PDB_OUT_DQ', u'H2:PSL-ISS_PDB_OUT_DQ', 56 u'L1:PSL-ISS_PDB_OUT_DQ'] 57 58 Parameters 59 ---------- 60 name : str 61 Name of the channel of interest, or part of channel to query 62 63 Returns 64 ------- 65 list 66 list of (key, value) dicts recovered from the CIS 67 68 Raises 69 ------ 70 ValueError 71 if no channels are found matching the given name 72 """ 73 url = '%s/?q=%s' % (CIS_API_URL, name) 74 more = True 75 out = [] 76 while more: 77 reply = _get(url, debug=debug) 78 if reply['count'] == 0: 79 raise ValueError("No channels found with name '%s'" % name) 80 try: 81 out.extend(reply[u'results']) 82 except KeyError: 83 pass 84 more = 'next' in reply and reply['next'] is not None 85 if more: 86 url = reply['next'] 87 else: 88 break 89 out.sort(key=lambda c: c['name']) 90 return out
91 92
93 -def _get(url, debug=False):
94 """Perform a GET query against the CIS 95 """ 96 try: 97 response = auth.request_ligodotorg(url, debug=debug) 98 except urllib.error.HTTPError: 99 raise ValueError("Channel named '%s' not found in Channel " 100 "Information System. Please double check the " 101 "name and try again." % url.strip('=')[-1]) 102 return json.loads(response.read())
103