Commit d13ae039 authored by Wei Shoulin's avatar Wei Shoulin
Browse files

add columns

parent d8de6103
...@@ -9,13 +9,14 @@ class CatalogApi(object): ...@@ -9,13 +9,14 @@ class CatalogApi(object):
self.module = Delegate().load(sub_module = "common") self.module = Delegate().load(sub_module = "common")
self.stub = getattr(self.module, "CatalogApi")() self.stub = getattr(self.module, "CatalogApi")()
def catalog_query(self, ra: float, dec: float, radius: float, catalog_name: str, min_mag: float, max_mag: float, obstime: int, limit: int): def catalog_query(self, ra: float, dec: float, radius: float, catalog_name: str, columns: tuple, min_mag: float, max_mag: float, obstime: int, limit: int):
''' retrieval catalog ''' retrieval catalog
:param ra: in deg :param ra: in deg
:param dec: in deg :param dec: in deg
:param radius: in deg :param radius: in deg
:param catalog_name: one of ['gaia3','',''] :param catalog_name: one of ['gaia3','','']
:param columns: tuple of str, like ('ra','dec','phot_g_mean_mag')
:param min_mag: minimal magnitude :param min_mag: minimal magnitude
:param max_mag: maximal magnitude :param max_mag: maximal magnitude
:param obstime: seconds :param obstime: seconds
...@@ -32,12 +33,13 @@ class CatalogApi(object): ...@@ -32,12 +33,13 @@ class CatalogApi(object):
def to_table(self, query_result): def to_table(self, query_result):
return to_fits_table(query_result) return to_fits_table(query_result)
def gaia3_query(self, ra: float, dec: float, radius: float, min_mag: float, max_mag: float, obstime: int, limit: int): def gaia3_query(self, ra: float, dec: float, radius: float, columns: tuple,min_mag: float, max_mag: float, obstime: int, limit: int):
"""retrieval GAIA EDR 3 """retrieval GAIA EDR 3
:param ra: in deg :param ra: in deg
:param dec: in deg :param dec: in deg
:param radius: in deg :param radius: in deg
:param columns: tuple of str, like ('ra','dec','phot_g_mean_mag')
:param min_mag: minimal magnitude :param min_mag: minimal magnitude
:param max_mag: maximal magnitude :param max_mag: maximal magnitude
:param obstime: seconds :param obstime: seconds
...@@ -46,6 +48,8 @@ class CatalogApi(object): ...@@ -46,6 +48,8 @@ class CatalogApi(object):
:returns: csst_dfs_common.models.Result :returns: csst_dfs_common.models.Result
""" """
try: try:
return self.stub.gaia3_query(ra, dec, radius, min_mag, max_mag, obstime, limit) if not columns:
raise Exception("columns is empty")
return self.stub.gaia3_query(ra, dec, radius, columns, min_mag, max_mag, obstime, limit)
except Exception as e: except Exception as e:
return Result.error(message=repr(e)) return Result.error(message=repr(e))
...@@ -80,7 +80,36 @@ def fields_dtypes(rec): ...@@ -80,7 +80,36 @@ def fields_dtypes(rec):
dtypes = tuple(dtypes) dtypes = tuple(dtypes)
return fields, dtypes return fields, dtypes
def tuple_fields_dtypes(rec: tuple):
dtypes = []
for _, f in rec:
if f.type == int:
dtypes.append('i8')
elif f.type == float:
dtypes.append('f8')
elif f.type == str:
dtypes.append('S2')
elif f.type == list:
dtypes.append('(12,)f8')
else:
dtypes.append('S2')
dtypes = tuple(dtypes)
return dtypes
def to_table(query_result): def to_table(query_result):
if not query_result.success or not query_result.data:
return Table()
fields = query_result['columns']
dtypes = tuple_fields_dtypes(query_result.data[0])
t = Table(names = fields, dtype = dtypes)
t.meta['comments'] = [str(query_result.data[0].__class__)]
t.meta['total'] = query_result['totalCount']
for rec in query_result.data:
t.add_row(rec)
return t
def object_list_to_table(query_result):
if not query_result.success or not query_result.data: if not query_result.success or not query_result.data:
return Table() return Table()
fields, dtypes = fields_dtypes(query_result.data[0]) fields, dtypes = fields_dtypes(query_result.data[0])
......
...@@ -13,18 +13,18 @@ class CommonCatalogTestCase(unittest.TestCase): ...@@ -13,18 +13,18 @@ class CommonCatalogTestCase(unittest.TestCase):
def test_catalog_query(self): def test_catalog_query(self):
t= time.time() t= time.time()
result = self.api.catalog_query( result = self.api.catalog_query(
ra=193.299, ra=90,
dec=19.08851, dec=24.5,
radius=2, radius=1,
catalog_name='gaia3', catalog_name='gaia3',
min_mag=-1, min_mag=-1,
max_mag=-1, max_mag=-1,
obstime = -1, obstime = -1,
limit = 0 limit = 0
) )
print(result) # print(result)
# dt = self.api.to_table(result) # dt = self.api.to_table(result)
# df = dt.to_pandas() # df = dt.to_pandas()
# print(df.head()) # print(df.head())
print('used:', time.time()-t) print('used:', time.time()-t)
# print('return:', result['totalCount'\]) print('return:', len(result.data))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment