def __query_object(self, coordinate, radius=None, width=None, height=None, async_job=False, verbose=False): """Launches a job TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, required if no 'width' nor 'height' are provided radius (deg) width : astropy.units, required if no 'radius' is provided box width height : astropy.units, required if no 'radius' is provided box height async_job : bool, optional, default 'False' executes the query (job) in asynchronous/synchronous mode (default synchronous) verbose : bool, optional, default 'False' flag to display information about the process Returns ------- The job results (astropy.table). """ coord = self.__getCoordInput(coordinate, "coordinate") job = None if radius is not None: job = self.__cone_search(coord, radius, async_job=async_job, verbose=verbose) else: raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees widthQuantity = self.__getQuantityInput(width, "width") heightQuantity = self.__getQuantityInput(height, "height") widthDeg = widthQuantity.to(units.deg) heightDeg = heightQuantity.to(units.deg) query = "SELECT DISTANCE(POINT('ICRS',"+str(MAIN_TABLE_RA)+","\ + str(MAIN_TABLE_DEC)+"), \ POINT('ICRS'," +str(ra)+","+str(dec)+")) AS dist, * \ FROM " +str(MAIN_TABLE)+" WHERE CONTAINS(\ POINT('ICRS'," +str(MAIN_TABLE_RA)+","\ + str(MAIN_TABLE_DEC)+"),\ BOX('ICRS'," +str(ra)+","+str(dec)+", "+str(widthDeg.value)+", "\ + str(heightDeg.value)+"))=1 \ ORDER BY dist ASC" if async_job: job = self.__taphandler.launch_job_async(query, verbose=verbose) else: job = self.__taphandler.launch_job(query, verbose=verbose) return job.get_results()
def build(self): """parse things and do de query""" if self.columns != "*": columns = ",".join(map(str, self.columns)) else: columns = "*" if self.radius is not None and self.coords is not None: raHours, dec = coord_to_radec(self.coords) ra = raHours * 15.0 query = """ SELECT {row_limit} {columns}, DISTANCE( POINT('ICRS', {ra_column}, {dec_column}), POINT('ICRS', {ra}, {dec}) ) AS dist FROM {table_name} WHERE 1 = CONTAINS( POINT('ICRS', {ra_column}, {dec_column}), CIRCLE('ICRS', {ra}, {dec}, {radius})) """.format( **{ "ra_column": self.ra_name, "row_limit": "TOP {0}".format(self.row_limit) if self.row_limit > 0 else "", "dec_column": self.dec_name, "columns": columns, "ra": ra, "dec": dec, "radius": self.radius, "table_name": self.table, }) if self.column_filters: query_filters = "".join([ """AND {column} {condition} """.format(**{ "column": column, "condition": condition }) for column, condition in self.column_filters.items() ]) query += query_filters query += """ORDER BY dist ASC """ return query
def __cone_search(self, coordinate, radius, async_job=False, background=False, output_file=None, output_format="votable", verbose=False, dump_to_file=False): """Cone search sorted by distance TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, mandatory radius async_job : bool, optional, default 'False' executes the job in asynchronous/synchronous mode (default synchronous) background : bool, optional, default 'False' when the job is executed in asynchronous mode, this flag specifies whether the execution will wait until results are available output_file : str, optional, default None file name where the results are saved if dumpToFile is True. If this parameter is not provided, the jobid is used instead output_format : str, optional, default 'votable' results format verbose : bool, optional, default 'False' flag to display information about the process dump_to_file : bool, optional, default 'False' if True, the results are saved in a file instead of using memory Returns ------- A Job object """ coord = self.__getCoordInput(coordinate, "coordinate") raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees if radius is not None: radiusQuantity = self.__getQuantityInput(radius, "radius") radiusDeg = commons.radius_to_unit(radiusQuantity, unit='deg') query = "SELECT DISTANCE(POINT('ICRS',"+str(self.MAIN_GAIA_TABLE_RA)+","\ + str(self.MAIN_GAIA_TABLE_DEC)+"), \ POINT('ICRS',"+str(ra)+","+str(dec)+")) AS dist, * \ FROM "+str(self.MAIN_GAIA_TABLE)+" WHERE CONTAINS(\ POINT('ICRS',"+str(self.MAIN_GAIA_TABLE_RA)+","+str(self.MAIN_GAIA_TABLE_DEC)+"),\ CIRCLE('ICRS',"+str(ra)+","+str(dec)+", "+str(radiusDeg)+"))=1 \ ORDER BY dist ASC" if async_job: return self.__gaiatap.launch_job_async(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file, background=background) else: return self.__gaiatap.launch_job(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file)
def __cone_search(self, coordinate, radius, async_job=False, background=False, output_file=None, output_format="votable", verbose=False, dump_to_file=False): """Cone search sorted by distance TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, mandatory radius async_job : bool, optional, default 'False' executes the job in asynchronous/synchronous mode (default synchronous) background : bool, optional, default 'False' when the job is executed in asynchronous mode, this flag specifies whether the execution will wait until results are available output_file : str, optional, default None file name where the results are saved if dumpToFile is True. If this parameter is not provided, the jobid is used instead output_format : str, optional, default 'votable' results format verbose : bool, optional, default 'False' flag to display information about the process dump_to_file : bool, optional, default 'False' if True, the results are saved in a file instead of using memory Returns ------- A Job object """ coord = self.__getCoordInput(coordinate, "coordinate") raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees if radius is not None: radiusQuantity = self.__getQuantityInput(radius, "radius") radiusDeg = commons.radius_to_unit(radiusQuantity, unit='deg') query = "SELECT DISTANCE(POINT('ICRS',"+str(MAIN_GAIA_TABLE_RA)+","\ + str(MAIN_GAIA_TABLE_DEC)+"), \ POINT('ICRS',"+str(ra)+","+str(dec)+")) AS dist, * \ FROM "+str(MAIN_GAIA_TABLE)+" WHERE CONTAINS(\ POINT('ICRS',"+str(MAIN_GAIA_TABLE_RA)+","+str(MAIN_GAIA_TABLE_DEC)+"),\ CIRCLE('ICRS',"+str(ra)+","+str(dec)+", "+str(radiusDeg)+"))=1 \ ORDER BY dist ASC" if async_job: return self.__gaiatap.launch_job_async(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file, background=background) else: return self.__gaiatap.launch_job(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file)
def cone_search(self, coordinates, radius=0.0, filename=None, output_format='votable', cache=True): """ """ coord = self._getCoordInput(coordinates, "coordinate") radiusInGrades = float(radius / 60) # Converts to degrees raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees payload = { "RESOURCE_CLASS": "OBSERVATION", "ADQLQUERY": "SELECT DISTINCT OBSERVATION,OBSERVATION.TYPE," "TARGET.MOVING_TARGET" ",TARGET.TARGET_NAME,TARGET.TARGET_DESCRIPTION,PROPOSAL." "PROPOSAL_ID,PROPOSAL.PI_" "NAME,PROPOSAL.PROPOSAL_TITLE,INSTRUMENT.INSTRUMENT_NAME," "PLANE.METADATA_PROVENANCE" ",PLANE.DATA_PRODUCT_TYPE,PLANE.SOFTWARE_VERSION,POSITION" ".RA,POSITION.DEC,POSITION." "GAL_LAT,POSITION.GAL_LON,POSITION.ECL_LAT,POSITION.ECL_LON" ",POSITION.FOV_SIZE,ENERGY." "WAVE_CENTRAL,ENERGY.WAVE_BANDWIDTH,ENERGY.WAVE_MAX,ENERGY" ".WAVE_MIN,ENERGY.FILTER FROM" " FIELD_NOT_USED WHERE OBSERVATION.COLLECTION='HST' AND " "PLANE.MAIN_SCIENCE_PLANE=" "'true' AND (OBSERVATION.TYPE='HST Composite' OR " "OBSERVATION.TYPE='HST Singleton')" " AND INTERSECTS(CIRCLE('ICRS', {0}, {1}, {2}" "),POSITION)=1 AND PLANE.MAIN_SCIENCE_PLANE='true' " "ORDER BY PROPOSAL.PROPOSAL_ID " "DESC".format(str(ra), str(dec), str(radiusInGrades)), "RETURN_TYPE": str(output_format) } response = self._request('GET', self.metadata_url, params=payload, cache=cache, timeout=self.TIMEOUT) if filename is None: filename = "cone." + str(output_format) if response is None: table = None else: fileobj = BytesIO(response.content) table = Table.read(fileobj, format=output_format) # TODO: add "correct units" material here return table
def __query_object(self, coordinate, radius=None, width=None, height=None, async_job=False, verbose=False): """Launches a job TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, required if no 'width' nor 'height' are provided radius (deg) width : astropy.units, required if no 'radius' is provided box width height : astropy.units, required if no 'radius' is provided box height async_job : bool, optional, default 'False' executes the query (job) in asynchronous/synchronous mode (default synchronous) verbose : bool, optional, default 'False' flag to display information about the process Returns ------- The job results (astropy.table). """ coord = self.__getCoordInput(coordinate, "coordinate") job = None if radius is not None: job = self.__cone_search(coord, radius, async_job=async_job, verbose=verbose) else: raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees widthQuantity = self.__getQuantityInput(width, "width") heightQuantity = self.__getQuantityInput(height, "height") widthDeg = widthQuantity.to(units.deg) heightDeg = heightQuantity.to(units.deg) query = "SELECT DISTANCE(POINT('ICRS',"+str(self.MAIN_GAIA_TABLE_RA)+","\ + str(self.MAIN_GAIA_TABLE_DEC)+"), \ POINT('ICRS',"+str(ra)+","+str(dec)+")) AS dist, * \ FROM "+str(self.MAIN_GAIA_TABLE)+" WHERE CONTAINS(\ POINT('ICRS',"+str(self.MAIN_GAIA_TABLE_RA)+","\ + str(self.MAIN_GAIA_TABLE_DEC)+"),\ BOX('ICRS',"+str(ra)+","+str(dec)+", "+str(widthDeg.value)+", "\ + str(heightDeg.value)+"))=1 \ ORDER BY dist ASC" if async_job: job = self.__gaiatap.launch_job_async(query, verbose=verbose) else: job = self.__gaiatap.launch_job(query, verbose=verbose) return job.get_results()
def build(self): """Build and perform query. Returns ------- query : string with built query. """ if self.columns != "*": columns = ",".join(map(str, self.columns)) else: columns = "*" if self.radius is not None and self.coords is not None: raHours, dec = coord_to_radec(self.coords) ra = raHours * 15.0 row_limit = f"TOP {self.row_limit}" if self.row_limit > 0 else "" query = self.QUERY_TEMPLATE.format( row_limit=row_limit, columns=columns, ra_column=self.ra_name, dec_column=self.dec_name, ra=ra, dec=dec, table_name=self.table, radius=self.radius, ) if self.column_filters: query_filters = "".join([ """AND {column} {condition} """.format(column=column, condition=condition) for column, condition in self.column_filters.items() ]) query += query_filters query += """ORDER BY dist ASC""" return query
def __cone_search(self, coordinate, radius, table_name=MAIN_GAIA_TABLE, ra_column_name=MAIN_GAIA_TABLE_RA, dec_column_name=MAIN_GAIA_TABLE_DEC, async_job=False, background=False, output_file=None, output_format="votable", verbose=False, dump_to_file=False, columns=[]): """Cone search sorted by distance TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, mandatory radius table_name : str, optional, default main gaia table table name doing the cone search against ra_column_name : str, optional, default ra column in main gaia table ra column doing the cone search against dec_column_name : str, optional, default dec column in main gaia table dec column doing the cone search against async_job : bool, optional, default 'False' executes the job in asynchronous/synchronous mode (default synchronous) background : bool, optional, default 'False' when the job is executed in asynchronous mode, this flag specifies whether the execution will wait until results are available output_file : str, optional, default None file name where the results are saved if dumpToFile is True. If this parameter is not provided, the jobid is used instead output_format : str, optional, default 'votable' results format verbose : bool, optional, default 'False' flag to display information about the process dump_to_file : bool, optional, default 'False' if True, the results are saved in a file instead of using memory columns: list, optional, default [] if empty, all columns will be selected Returns ------- A Job object """ coord = self.__getCoordInput(coordinate, "coordinate") raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees if radius is not None: radiusQuantity = self.__getQuantityInput(radius, "radius") radiusDeg = commons.radius_to_unit(radiusQuantity, unit='deg') if columns: columns = ','.join(map(str, columns)) else: columns = "*" query = """ SELECT {row_limit} {columns}, DISTANCE( POINT('ICRS', {ra_column}, {dec_column}), POINT('ICRS', {ra}, {dec}) ) AS dist FROM {table_name} WHERE 1 = CONTAINS( POINT('ICRS', {ra_column}, {dec_column}), CIRCLE('ICRS', {ra}, {dec}, {radius}) ) ORDER BY dist ASC """.format( **{ 'ra_column': ra_column_name, 'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "", 'dec_column': dec_column_name, 'columns': columns, 'ra': ra, 'dec': dec, 'radius': radiusDeg, 'table_name': table_name }) if async_job: return self.launch_job_async(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file, background=background) else: return self.launch_job(query=query, output_file=output_file, output_format=output_format, verbose=verbose, dump_to_file=dump_to_file)
def __query_object(self, coordinate, radius=None, width=None, height=None, async_job=False, verbose=False, columns=[]): """Launches a job TAP & TAP+ Parameters ---------- coordinate : astropy.coordinate, mandatory coordinates center point radius : astropy.units, required if no 'width' nor 'height' are provided radius (deg) width : astropy.units, required if no 'radius' is provided box width height : astropy.units, required if no 'radius' is provided box height async_job : bool, optional, default 'False' executes the query (job) in asynchronous/synchronous mode (default synchronous) verbose : bool, optional, default 'False' flag to display information about the process columns: list, optional, default [] if empty, all columns will be selected Returns ------- The job results (astropy.table). """ coord = self.__getCoordInput(coordinate, "coordinate") job = None if radius is not None: job = self.__cone_search(coord, radius, async_job=async_job, verbose=verbose) else: raHours, dec = commons.coord_to_radec(coord) ra = raHours * 15.0 # Converts to degrees widthQuantity = self.__getQuantityInput(width, "width") heightQuantity = self.__getQuantityInput(height, "height") widthDeg = widthQuantity.to(units.deg) heightDeg = heightQuantity.to(units.deg) if columns: columns = ','.join(map(str, columns)) else: columns = "*" query = """ SELECT {row_limit} DISTANCE( POINT('ICRS', {ra_column}, {dec_column}), POINT('ICRS', {ra}, {dec}) ) as dist, {columns} FROM {table_name} WHERE 1 = CONTAINS( POINT('ICRS', {ra_column}, {dec_column}), BOX( 'ICRS', {ra}, {dec}, {width}, {height} ) ) ORDER BY dist ASC """.format( **{ 'row_limit': "TOP {0}".format(self.ROW_LIMIT ) if self.ROW_LIMIT > 0 else "", 'ra_column': self.MAIN_GAIA_TABLE_RA, 'dec_column': self.MAIN_GAIA_TABLE_DEC, 'columns': columns, 'table_name': self.MAIN_GAIA_TABLE, 'ra': ra, 'dec': dec, 'width': widthDeg.value, 'height': heightDeg.value, }) if async_job: job = self.launch_job_async(query, verbose=verbose) else: job = self.launch_job(query, verbose=verbose) return job.get_results()
def query_region_async(self, coordinates, radius=1 * u.arcmin, equinox='J2000.0', get_query_payload=False): """ Serves the same purpose as `~NedClass.query_region` but returns the raw HTTP response rather than the `astropy.table.Table` object. Parameters ---------- coordinates : str or `astropy.coordinates` object The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as strings as specified in the `astropy.coordinates` module. radius : str or `~astropy.units.Quantity` object, optional The string must be parsable by `astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. Defaults to 1 arcmin. equinox : str, optional The equinox may be either J2000.0 or B1950.0. Defaults to J2000.0 get_query_payload : bool, optional if set to `True` then returns the dictionary sent as the HTTP request. Defaults to `False`. Returns ------- response : `requests.Response` The HTTP response returned from the service """ request_payload = self._request_payload_init() self._set_input_options(request_payload) self._set_output_options(request_payload) # if its a name then query near name if not commons._is_coordinate(coordinates): request_payload['objname'] = coordinates request_payload['search_type'] = 'Near Name Search' request_payload['radius'] = coord.Angle(radius).arcmin else: try: c = commons.parse_coordinates(coordinates) if c.frame.name == 'galactic': request_payload['in_csys'] = 'Galactic' request_payload['lon'] = c.l.degree request_payload['lat'] = c.b.degree # for any other, convert to ICRS and send else: request_payload['in_csys'] = 'Equatorial' ra, dec = commons.coord_to_radec(c) request_payload['lon'] = ra request_payload['lat'] = dec request_payload['search_type'] = 'Near Position Search' request_payload['in_equinox'] = equinox request_payload['radius'] = coord.Angle(radius).arcmin except (u.UnitsError, TypeError): raise TypeError("Coordinates not specified correctly") if get_query_payload: return request_payload response = self._request("GET", url=Ned.OBJ_SEARCH_URL, params=request_payload, timeout=Ned.TIMEOUT) return response