From 66ee8623c196fa3bd006b435dba1b6c22042c0a0 Mon Sep 17 00:00:00 2001 From: shoulinwei Date: Thu, 15 Apr 2021 10:37:25 +0800 Subject: [PATCH] ephem --- README.md | 3 ++ csst_dfs_api_local/common/__init__.py | 1 + csst_dfs_api_local/common/catalog.py | 35 ++++++++---- csst_dfs_api_local/ifs/fits.py | 77 ++++++++++++++++----------- csst_dfs_api_local/ifs/reffits.py | 6 +-- requirements.txt | 4 +- setup.cfg | 2 +- tests/test_common_catalog.py | 4 +- 8 files changed, 84 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 56c4a2b..73640e5 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 e69de29..a45b30a 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 da37b36..e017364 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 838c8d7..4e4514e 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 2fe7585..5979cfe 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 9f02d2e..ac7c290 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 473ec09..7e465db 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 ca8e642..9cc4ad8 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 -- GitLab