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
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
```
......
from .catalog import CatalogApi
\ No newline at end of file
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
......@@ -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):
'''
......
......@@ -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 + "'"]
......
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
......@@ -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]
......
......@@ -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
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