Commit 66ee8623 authored by Wei Shoulin's avatar Wei Shoulin
Browse files

ephem

parent 36ba9fbc
...@@ -9,6 +9,9 @@ This package provides APIs to access csst's files and databases in your localize ...@@ -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: This library can be installed with the following command:
```bash ```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 python setup.py install
``` ```
......
from .catalog import CatalogApi
\ No newline at end of file
from urllib import request import urllib
import json import json
from csst_dfs_commons.models import Result
class CatalogApi(object): class CatalogApi(object):
def __init__(self): def __init__(self):
self.data = [] 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): 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") data = { "ra":ra, "dec":dec, "radius":radius, "catalog_name": "gaia3", "min_mag": min_mag, "max_mag": max_mag, "obstime": obstime, "limit": limit}
req.add_header('Content-Type', 'application/json') return self.rest_request(data)
data = { "ra":ra, "dec":dec, "radius":radius, "min_mag": min_mag, "max_mag": max_mag, "obstime": obstime, "limit": limit}
data = json.dumps(data) def rest_request(self, data):
data = data.encode() try:
res = request.urlopen(req, data=data) Headers = {"Content-type": "application/x-www-form-urlencoded"}
content = res.read()
print(content) data = urllib.parse.urlencode(data).encode("utf-8")
return content req = urllib.request.Request(self.url,data,Headers)
\ No newline at end of file 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
...@@ -12,6 +12,7 @@ from astropy.io import fits ...@@ -12,6 +12,7 @@ from astropy.io import fits
from ..common.db import DBClient from ..common.db import DBClient
from ..common.utils import * from ..common.utils import *
from csst_dfs_commons.models import Result
log = logging.getLogger('csst') log = logging.getLogger('csst')
class FitsApi(object): class FitsApi(object):
...@@ -36,9 +37,10 @@ class FitsApi(object): ...@@ -36,9 +37,10 @@ class FitsApi(object):
exp_time = (start, end), exp_time = (start, end),
ccd_num = [int], ccd_num = [int],
qc0_status = [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 = [] paths = []
...@@ -48,28 +50,40 @@ class FitsApi(object): ...@@ -48,28 +50,40 @@ class FitsApi(object):
ccd_num = get_parameter(kwargs, "ccd_num") ccd_num = get_parameter(kwargs, "ccd_num")
qc0_status = get_parameter(kwargs, "qc0_status") qc0_status = get_parameter(kwargs, "qc0_status")
prc_status = get_parameter(kwargs, "prc_status") prc_status = get_parameter(kwargs, "prc_status")
limit = get_parameter(kwargs, "limit", 0)
sql = [] limit = to_int(limit, 0)
sql.append("select * from ifs_rawfits where exp_time<='" + exp_time[1]+"'") try:
if exp_time[0] is not None: sql = []
sql.append(" and exp_time>='" + exp_time[0] + "'")
if obs_time is not None: sql.append("select * from ifs_rawfits where exp_time<='" + exp_time[1]+"'")
sql.append(" and obs_time=" + obs_time)
if ccd_num is not None: if exp_time[0] is not None:
sql.append(" and ccd_num=" + ccd_num) sql.append(" and exp_time>='" + exp_time[0] + "'")
if qc0_status is not None: if obs_time is not None:
sql.append(" and qc0_status=" + qc0_status) sql.append(" and obs_time=" + repr(obs_time))
if prc_status is not None: if ccd_num is not None:
sql.append(" and prc_status=" + prc_status) sql.append(" and ccd_num=" + repr(ccd_num))
if qc0_status is not None:
if file_name: sql.append(" and qc0_status=" + repr(qc0_status))
sql = ["select * from ifs_rawfits where filename='" + file_name + "'"] if prc_status is not None:
_, recs = self.db.select_many("".join(sql)) sql.append(" and prc_status=" + repr(prc_status))
for r in recs: if limit > 0:
r['file_path'] = os.path.join(self.root_dir, r['file_path']) sql.append(f" limit {limit}")
return recs
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): def get(self, **kwargs):
''' '''
...@@ -78,12 +92,15 @@ class FitsApi(object): ...@@ -78,12 +92,15 @@ class FitsApi(object):
return dict or None return dict or None
''' '''
fits_id = get_parameter(kwargs, "fits_id", -1) try:
r = self.db.select_one( fits_id = get_parameter(kwargs, "fits_id", -1)
"select * from ifs_rawfits where id=?", (fits_id,)) r = self.db.select_one(
if r: "select * from ifs_rawfits where id=?", (fits_id,))
r['file_path'] = os.path.join(self.root_dir, r['file_path']) if r:
return 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): def read(self, **kwargs):
''' '''
......
...@@ -56,13 +56,13 @@ class RefFitsApi(object): ...@@ -56,13 +56,13 @@ class RefFitsApi(object):
if exp_time[0] is not None: if exp_time[0] is not None:
sql.append(" and exp_time>='" + exp_time[0] + "'") sql.append(" and exp_time>='" + exp_time[0] + "'")
if obs_time is not None: 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: 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: if ref_type is not None:
sql.append(" and ref_type='" + ref_type + "'") sql.append(" and ref_type='" + ref_type + "'")
if status is not None: if status is not None:
sql.append(" and status=" + status) sql.append(" and status=" + repr(status))
if file_name: if file_name:
sql = ["select * from ifs_ref_fits where filename='" + file_name + "'"] sql = ["select * from ifs_ref_fits where filename='" + file_name + "'"]
......
DBUtils==1.3 DBUtils==1.3
\ No newline at end of file astropy>=4.0
git+https://github.com/astronomical-data-processing/csst-dfs-commons.git
\ No newline at end of file
...@@ -20,7 +20,7 @@ packages = find: ...@@ -20,7 +20,7 @@ packages = find:
python_requires = >=3.7 python_requires = >=3.7
zip_safe = False zip_safe = False
setup_requires = setuptools_scm setup_requires = setuptools_scm
install_requires = install_requires =
astropy>=4.0 astropy>=4.0
DBUtils==1.3 DBUtils==1.3
[options.package_data] [options.package_data]
......
...@@ -10,5 +10,5 @@ class CommonEphemTestCase(unittest.TestCase): ...@@ -10,5 +10,5 @@ class CommonEphemTestCase(unittest.TestCase):
self.api = CatalogApi() self.api = CatalogApi()
def test_gaia3_query(self): 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) result = self.api.gaia3_query(ra=56.234, dec=14.4665, radius=1, min_mag=-1, max_mag=-1, obstime=-1, limit=2)
print('find:', recs) print('return:', result)
\ No newline at end of file \ No newline at end of file
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