def query(profile, ra, dec, radius=1.0, gmax=23.5): """Return data queried from datalab Parameters ---------- profile : Profile for data lab query [str] ra : Right Ascension [deg] dec : Declination [deg] radius : radius around (ra, dec) [deg] Returns ------- data : numpy recarray of data """ qc.set_profile(profile) sql = f''' SELECT ra, dec, gmag, gmag-{R_g}*ebv AS gmag_dered, -- dereddend magnitude gerr, rmag-{R_r}*ebv AS rmag_dered, -- dereddend magnitude rmag, rerr, ebv FROM delvemc_y2t2.object WHERE q3c_radial_query(ra,dec,{ra},{dec},{radius}) AND chi < 3 -- for star-galaxy separation AND prob > 0.8 -- for star-galaxy separation AND abs(sharp) < 1 -- for star-galaxy separation AND gmag < 90 -- for quality AND rmag < 90 -- for quality AND gmag < {gmax} -- for quality ''' data = qc.query(sql=sql,fmt='structarray',timeout=300) return(data)
def get_catalog(self, query=None, query_fields=None, print_query=False, timeout=120): """ Get catalog sources around the given coordinates within self.radius. Args: query (str, optional): SQL query to generate the catalog query_fields (list, optional): Over-ride list of items to query print_query (bool): Print the SQL query generated Returns: astropy.table.Table: Catalog of sources obtained from the SQL query. """ qc.set_profile(self.qc_profile) # Generate the query if query is None: self._gen_cat_query(query_fields) query = self.query if print_query: print(query) # Do it while silencing print statements result = qc.query(self.token, sql=query, timeout=timeout) self.catalog = convert(result, outfmt="table") self.catalog.meta['radius'] = self.radius self.catalog.meta['survey'] = self.survey # Validate self.validate_catalog() # Return return self.catalog.copy()
def getAvaliableDataset(self): # set default profile qc.set_profile('default') # these schemas are not astronomical datasets _remove = set(['ivao', 'ivao_smash', 'tap_schema', 'schema']) # get all schemas from DB _schemas = set(qc.query(self.__TOKEN_SESSION, sql='SELECT schema FROM tbl_stat').split()) # remove non-astro schemas _alldatasets = sorted(list(_schemas - _remove)) print("Datasets available in Data Lab (with current profile):\n", _alldatasets)
def getDesCatalog(self): # The pre-release DES DR1 profile try: qc.set_profile('des-proto') except Exception as e: print(e) try: schema=qc.schema('des_dr1', format='json', profile='des-proto') if type(schema) is not json: tmp=schema.decode('utf8').replace("'", '"') data = json.loads(tmp) s = json.dumps(data, indent=4, sort_keys=True) print(s) print(schema) except Exception as e: print(e)
def run(self): token = getUserToken(self) # Get the query string to be used. This can either be supplied # directly or by specifying a filename. sql = None adql = None res = None if self.adql.value is None or self.adql.value == '': if self.sql.value is None or self.sql.value == '': print("Error: At least one of 'adql' or 'sql' is required.") sys.exit(-1) elif os.path.exists(self.sql.value): with open(self.sql.value, "r", 0) as fd: sql = fd.read(os.path.getsize(self.sql.value) + 1) fd.close() else: sql = self.sql.value elif os.path.exists(self.adql.value): with open(self.adql.value, "r", 0) as fd: adql = fd.read(os.path.getsize(self.adql.value) + 1) fd.close() else: adql = self.adql.value # Execute the query. if self.profile.value != "default": if self.profile.value != "" and self.profile.value is not None: queryClient.set_profile(profile=self.profile.value) try: res = queryClient.query(token, adql=adql, sql=sql, fmt=self.fmt.value, out=self.out.value, async=self. async .value) if self. async .value: print(res) # Return the JobID elif self.out.value == '' or self.out.value is None: print(res) # Return the results
def get_catalog(self, query=None, query_fields=None, print_query=False, timeout=120): """ Get catalog sources around the given coordinates within self.radius. Args: query (str, optional): SQL query to generate the catalog query_fields (list, optional): Over-ride list of items to query print_query (bool): Print the SQL query generated Returns: astropy.table.Table: Catalog of sources obtained from the SQL query. """ qc.set_profile(self.qc_profile) # Generate the query if query is None: self._gen_cat_query(query_fields) query = self.query if print_query: print(query) # Do it while silencing print statements result = qc.query(self.token, sql=query, timeout=timeout) sys.stdout = open(os.devnull, "w") temp = convert(result) sys.stdout = sys.__stdout__ self.catalog = Table.from_pandas(temp) # TODO:: Dig into why the heck it doesn't want to natively # output to a table when it was clearly intended to with 'outfmt=table' # Finish self.catalog.meta['radius'] = self.radius self.catalog.meta['survey'] = self.survey # Validate self.validate_catalog() # Return return self.catalog.copy()