def _load_votable(self, params): if 'url' in params.field_results: buff = CStringIO(curlrequest.urlopen( params.field_results['url']).read()) return parse(buff, filename=params.filename) else: return parse(params.filename)
def table(self): if self.__file is None: self.__file = tempfile.NamedTemporaryFile() self.__file.write(self.data.encode('utf-8')) self.__file.flush() array = votable.parse( self.__file, pedantic=self.__pedantic).get_first_table().array self.__table = array # .to_table() return self.__table
def table(self): if self.__file is None: self.__file = tempfile.NamedTemporaryFile() self.__file.write(self.data.encode('utf-8')) self.__file.flush() array = parse(self.__file, pedantic=self.__pedantic).get_first_table().array self.__table = Table(array) return self.__table
def _query_gator(options, debug=False): # Construct query URL url = GATOR_URL + "?" + \ string.join(["%s=%s" % (x, urllib.quote_plus(str(options[x]))) for x in options], "&") if debug: print(url) # Request page req = urllib2.Request(url) response = urllib2.urlopen(req) with aud.get_readable_fileobj(response, cache=True) as f: result = f.read() # Check if results were returned if 'The catalog is not on the list' in result: raise Exception("Catalog not found") # Check that object name was not malformed if 'Either wrong or missing coordinate/object name' in result: raise Exception("Malformed coordinate/object name") # Check that the results are not of length zero if len(result) == 0: raise Exception("The IRSA server sent back an empty reply") # Write table to temporary file output = tempfile.NamedTemporaryFile() output.write(result) output.flush() # Read it in using the astropy VO table reader try: firsttable = votable.parse(output.name, pedantic=False).get_first_table() array = firsttable.array except Exception as ex: print("Failed to parse votable! Returning output file instead.") print(ex) return open(output.name,'r') # Convert to astropy.table.Table instance table = array.to_table() # Check if table is empty if len(table) == 0: warnings.warn("Query returned no results, so the table will be empty") # Remove temporary file output.close() return table
def _query_gator(options): # Construct query URL url = GATOR_URL + "?" + \ string.join(["%s=%s" % (x, urllib.quote_plus(str(options[x]))) for x in options], "&") # Request page req = urllib2.Request(url) response = urllib2.urlopen(req) result = response.read() # Check if results were returned if 'The catalog is not on the list' in result: raise Exception("Catalog not found") # Check that object name was not malformed if 'Either wrong or missing coordinate/object name' in result: raise Exception("Malformed coordinate/object name") # Check that the results are not of length zero if len(result) == 0: raise Exception("The IRSA server sent back an empty reply") # Write table to temporary file output = tempfile.NamedTemporaryFile() output.write(result) output.flush() # Read it in using the astropy VO table reader array = parse(output.name, pedantic=False).get_first_table().array # Convert to astropy.table.Table instance table = Table(array) # Check if table is empty if len(table) == 0: warnings.warn("Query returned no results, so the table will be empty") # Remove temporary file output.close() return table
def vizquery(query, server=None): """ VizieR search query. This function can be used to search all the catalogs available through the VizieR service. Parameters ---------- query: dict A dictionary specifying the query. For acceptable keys, refer to the links given in the references section. The dictionary values can be any of the following types: * string * list of string * astropy.table.Table (containing columns "_RAJ2000" and "_DEJ2000" in degrees) server: str, optional The VizieR server to use. (See VizieR mirrors at http://vizier.u-strasbg.fr) If not specified, `server` is set by the `VIZIER_SERVER` configuration item. Returns ------- table : `~astropy.table.Table` A table containing the results of the query References ---------- * http://vizier.u-strasbg.fr/doc/asu-summary.htx * http://vizier.u-strasbg.fr/vizier/vizHelp/menu.htx """ #Check VizieR server server = (VIZIER_SERVER() if server is None else server) # Always add calculated _RAJ2000 & _DEJ2000 to the query. # This is used for cross correlations between queries if '-out.add' in query: query["-out.add"] += ['_RAJ2000', '_DEJ2000'] else: query["-out.add"] = ['_RAJ2000', '_DEJ2000'] # Assemble the actual query body = [] for (key, value) in query.items(): if type(value) is str: body += ["%s=%s" % (key, value)] elif type( value ) is Table: # Value is a table, convert it to a string, list of positions pos = [] for elem in np.array(value, copy=False): pos += ["%.8f%+.8f" % (elem['_RAJ2000'], elem['_DEJ2000']) ] # Position with the format: _RAJ2000+_DEJ2000 body += [ "-out.add=_q" ] # This calculated index is a reference to the input table body += ["%s=%s" % (key, "<<;" + ";".join(pos)) ] # The proper convention: <<;pos1;pos2;pos3 elif type(value) is list: # Value is a list, join it with commas body += ["%s=%s" % (key, ",".join(value))] else: raise Exception("Don't know how to handle %s" % repr(value)) body = "\r\n".join(body) # Fetch the VOTABLE corresponding to the query r = requests.post("http://" + server + "/viz-bin/votable", data=body) s = io.BytesIO(r.content) voTable = votable.parse(s, pedantic=False) # Convert VOTABLE into a list of astropy Table. tableList = [] for voTreeTable in voTable.iter_tables(): if len(voTreeTable.array) > 0: # Table names come from the VOTABLE fields names = [] for field in voTreeTable.fields: names += [field.name.encode('ascii')] # Table data come from the VOTABLE record array tableList += [voTreeTable.to_table()] # Merge the Table list table = tableList[0] if len(tableList) > 1: for t in tableList[1:]: if len(t) > 0: for row in t: table.add_row(row) return table
def _load_votable(self, params): if params.field_results.has_key('url'): buff = StringIO(urlopen(params.field_results['url']).read()) return parse(buff, filename=params.filename) else: return parse(params.filename)
def vizquery(query, server="vizier.u-strasbg.fr"): """ VizieR search query. This function can be used to search all the catalogs available through the VizieR service. Parameters ---------- query: dict A dictionary specifying the query. For acceptable keys, refer to the links given in the references section. The dictionary values can be any of the following types: * string * list of string * astropy.table.Table (containing columns "_RAJ2000" and "_DEJ2000" in degrees) server: str, optional The VizieR server to use. (See VizieR mirrors at http://vizier.u-strasbg.fr) Defaults to "vizier.u-strasbg.fr". Returns ------- table : `~astropy.table.Table` A table containing the results of the query References ---------- * http://vizier.u-strasbg.fr/doc/asu-summary.htx * http://vizier.u-strasbg.fr/vizier/vizHelp/menu.htx """ # Always add calculated _RAJ2000 & _DEJ2000 to the query. # This is used for cross correlations between queries if '-out.add' in query: query["-out.add"] += ['_RAJ2000', '_DEJ2000'] else: query["-out.add"] = ['_RAJ2000', '_DEJ2000'] # Assemble the actual query body = [] for (key,value) in query.items(): if type(value) is str: body += ["%s=%s"%(key, value)] elif type(value) is Table: # Value is a table, convert it to a string, list of positions pos = [] for elem in np.array(value, copy=False): pos += ["%.8f%+.8f"%(elem['_RAJ2000'],elem['_DEJ2000'])] # Position with the format: _RAJ2000+_DEJ2000 body += ["-out.add=_q"] # This calculated index is a reference to the input table body += ["%s=%s"%(key, "<<;"+";".join(pos))] # The proper convention: <<;pos1;pos2;pos3 elif type(value) is list: # Value is a list, join it with commas body += ["%s=%s"%(key, ",".join(value))] else: raise Exception("Don't know how to handle %s"%repr(value)) body = "\r\n".join(body) # Fetch the VOTABLE corresponding to the query r = requests.post("http://"+server+"/viz-bin/votable", data=body) s = io.BytesIO(r.content) voTable = votable.parse(s, pedantic=False) # Convert VOTABLE into a list of astropy Table. tableList = [] for voTreeTable in voTable.iter_tables(): if len(voTreeTable.array)>0: # Table names come from the VOTABLE fields names = [] for field in voTreeTable.fields: names += [field.name.encode('ascii')] # Table data come from the VOTABLE record array tableList += [voTreeTable.to_table()] # Merge the Table list table = tableList[0] if len(tableList)>1: for t in tableList[1:]: if len(t)>0: for row in t: table.add_row(row) return table