diff --git a/README.md b/README.md index 56c4a2b65c036c76af903d160c880850ec800e77..73640e50ba3aac3cdb22efacd86191f326d3d826 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ This package provides APIs to access csst's files and databases in your localize This library can be installed with the following command: ```bash +git clone https://github.com/astronomical-data-processing/csst-dfs-api-local.git +cd csst-dfs-api-local +pip install -r requirements.txt python setup.py install ``` diff --git a/csst_dfs_api_local/common/__init__.py b/csst_dfs_api_local/common/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a45b30a5f732d9ec81756434cced36116d340319 100644 --- a/csst_dfs_api_local/common/__init__.py +++ b/csst_dfs_api_local/common/__init__.py @@ -0,0 +1 @@ +from .catalog import CatalogApi \ No newline at end of file diff --git a/csst_dfs_api_local/common/catalog.py b/csst_dfs_api_local/common/catalog.py index da37b362a8fb4767715b79eb98b61e460d3a16c0..e01736493b5bcfd9ae34f075c9258c4adf00214e 100644 --- a/csst_dfs_api_local/common/catalog.py +++ b/csst_dfs_api_local/common/catalog.py @@ -1,18 +1,31 @@ -from urllib import request +import urllib import json +from csst_dfs_commons.models import Result class CatalogApi(object): def __init__(self): self.data = [] - self.url = 'http://172.31.248.218:30180/search/v2' + self.url = r'http://csst.astrolab.cn:30180/search/v2' def gaia3_query(self, ra: float, dec: float, radius: float, min_mag: float, max_mag: float, obstime: int, limit: int): - req = request.Request(url = self.url, method="post") - req.add_header('Content-Type', 'application/json') - data = { "ra":ra, "dec":dec, "radius":radius, "min_mag": min_mag, "max_mag": max_mag, "obstime": obstime, "limit": limit} - data = json.dumps(data) - data = data.encode() - res = request.urlopen(req, data=data) - content = res.read() - print(content) - return content \ No newline at end of file + data = { "ra":ra, "dec":dec, "radius":radius, "catalog_name": "gaia3", "min_mag": min_mag, "max_mag": max_mag, "obstime": obstime, "limit": limit} + return self.rest_request(data) + + def rest_request(self, data): + try: + Headers = {"Content-type": "application/x-www-form-urlencoded"} + + data = urllib.parse.urlencode(data).encode("utf-8") + req = urllib.request.Request(self.url,data,Headers) + response = urllib.request.urlopen(req, timeout=30) + html = response.read().decode("utf-8") + resp = json.loads(html) + + if resp["code"] == 0: + return Result.ok_data(data=resp["data"]).append("totalCount", resp["object"]["totalCount"]) + else: + return Result.error(message = resp.message) + + except Exception as e: + return Result.error(message=repr(e)) + \ No newline at end of file diff --git a/csst_dfs_api_local/ifs/fits.py b/csst_dfs_api_local/ifs/fits.py index 838c8d7388faaa9fc705a9d8196f0b37232d5f46..4e4514e27880cc94190c6716327e6ce4a04a4845 100644 --- a/csst_dfs_api_local/ifs/fits.py +++ b/csst_dfs_api_local/ifs/fits.py @@ -12,6 +12,7 @@ from astropy.io import fits from ..common.db import DBClient from ..common.utils import * +from csst_dfs_commons.models import Result log = logging.getLogger('csst') class FitsApi(object): @@ -36,9 +37,10 @@ class FitsApi(object): exp_time = (start, end), ccd_num = [int], qc0_status = [int], - prc_status = [int] + prc_status = [int], + limit: limits returns the number of records - return list of raw records + return: csst_dfs_common.models.Result ''' paths = [] @@ -48,28 +50,40 @@ class FitsApi(object): ccd_num = get_parameter(kwargs, "ccd_num") qc0_status = get_parameter(kwargs, "qc0_status") prc_status = get_parameter(kwargs, "prc_status") - - sql = [] - - sql.append("select * from ifs_rawfits where exp_time<='" + exp_time[1]+"'") - - if exp_time[0] is not None: - sql.append(" and exp_time>='" + exp_time[0] + "'") - if obs_time is not None: - sql.append(" and obs_time=" + obs_time) - if ccd_num is not None: - sql.append(" and ccd_num=" + ccd_num) - if qc0_status is not None: - sql.append(" and qc0_status=" + qc0_status) - if prc_status is not None: - sql.append(" and prc_status=" + prc_status) - - if file_name: - sql = ["select * from ifs_rawfits where filename='" + file_name + "'"] - _, recs = self.db.select_many("".join(sql)) - for r in recs: - r['file_path'] = os.path.join(self.root_dir, r['file_path']) - return recs + limit = get_parameter(kwargs, "limit", 0) + limit = to_int(limit, 0) + + try: + + sql = [] + + sql.append("select * from ifs_rawfits where exp_time<='" + exp_time[1]+"'") + + if exp_time[0] is not None: + sql.append(" and exp_time>='" + exp_time[0] + "'") + if obs_time is not None: + sql.append(" and obs_time=" + repr(obs_time)) + if ccd_num is not None: + sql.append(" and ccd_num=" + repr(ccd_num)) + if qc0_status is not None: + sql.append(" and qc0_status=" + repr(qc0_status)) + if prc_status is not None: + sql.append(" and prc_status=" + repr(prc_status)) + if limit > 0: + sql.append(f" limit {limit}") + + if file_name: + sql = ["select * from ifs_rawfits where filename='" + file_name + "'"] + + totalCount = self.db.select_one("".join(sql).replace("select * from","select count(*) as v from")) + + _, recs = self.db.select_many("".join(sql)) + for r in recs: + r['file_path'] = os.path.join(self.root_dir, r['file_path']) + + return Result.ok_data(data=recs).append("totalCount", totalCount['v']) + except Exception as e: + return Result.error(message=e.message) def get(self, **kwargs): ''' @@ -78,12 +92,15 @@ class FitsApi(object): return dict or None ''' - fits_id = get_parameter(kwargs, "fits_id", -1) - r = self.db.select_one( - "select * from ifs_rawfits where id=?", (fits_id,)) - if r: - r['file_path'] = os.path.join(self.root_dir, r['file_path']) - return r + try: + fits_id = get_parameter(kwargs, "fits_id", -1) + r = self.db.select_one( + "select * from ifs_rawfits where id=?", (fits_id,)) + if r: + r['file_path'] = os.path.join(self.root_dir, r['file_path']) + return Result.ok_data(data=r) + except Exception as e: + return Result.error(message=e.message) def read(self, **kwargs): ''' diff --git a/csst_dfs_api_local/ifs/reffits.py b/csst_dfs_api_local/ifs/reffits.py index 2fe7585c1ba7f2907c2ddfbd04c0616454d77ab2..5979cfe3c2e36a9e8a5eb331f4818b31d712223a 100644 --- a/csst_dfs_api_local/ifs/reffits.py +++ b/csst_dfs_api_local/ifs/reffits.py @@ -56,13 +56,13 @@ class RefFitsApi(object): if exp_time[0] is not None: sql.append(" and exp_time>='" + exp_time[0] + "'") if obs_time is not None: - sql.append(" and obs_time=" + obs_time) + sql.append(" and obs_time=" + repr(obs_time)) if ccd_num is not None: - sql.append(" and ccd_num=" + ccd_num) + sql.append(" and ccd_num=" + repr(ccd_num)) if ref_type is not None: sql.append(" and ref_type='" + ref_type + "'") if status is not None: - sql.append(" and status=" + status) + sql.append(" and status=" + repr(status)) if file_name: sql = ["select * from ifs_ref_fits where filename='" + file_name + "'"] diff --git a/requirements.txt b/requirements.txt index 9f02d2e0150e1207e8c15988894301e454558672..ac7c2900d9d64ab5468d10a454ed6e8cb60900b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ -DBUtils==1.3 \ No newline at end of file +DBUtils==1.3 +astropy>=4.0 +git+https://github.com/astronomical-data-processing/csst-dfs-commons.git \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 473ec0932199e23f5d855f9d54c6c027920964a4..7e465db5c398f3968e25f613dd27d8b861523d53 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,7 +20,7 @@ packages = find: python_requires = >=3.7 zip_safe = False setup_requires = setuptools_scm -install_requires = +install_requires = astropy>=4.0 DBUtils==1.3 [options.package_data] diff --git a/tests/test_common_catalog.py b/tests/test_common_catalog.py index ca8e64211fcd7f694922628aacf76857a9307584..9cc4ad8b41399e7fccfef7ae5dbd63dc4b3c7764 100644 --- a/tests/test_common_catalog.py +++ b/tests/test_common_catalog.py @@ -10,5 +10,5 @@ class CommonEphemTestCase(unittest.TestCase): self.api = CatalogApi() def test_gaia3_query(self): - recs = self.api.gaia3_query(ra=260, dec=-27, radius=0.01, min_mag=-1, max_mag=-1, obstime=-1, limit=2) - print('find:', recs) \ No newline at end of file + result = self.api.gaia3_query(ra=56.234, dec=14.4665, radius=1, min_mag=-1, max_mag=-1, obstime=-1, limit=2) + print('return:', result) \ No newline at end of file