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

ephem

parent 7c92aad2
......@@ -9,13 +9,15 @@ This package provides APIs to access csst's files and databases.
This library can be installed with the following command:
```bash
git clone https://github.com/astronomical-data-processing/csst-dfs-api.git
cd csst-dfs-api
pip install -r requirements.txt
python setup.py install
```
## Configuration
enviroment variables
- CSST_DFS_API_MODE = local or cluster # default: local
- CSST_LOCAL_FILE_ROOT = [a local file directory] # required if DFS_API_MODE = local, default: /opt/temp/csst
- CSST_DFS_CONFIG_SERVER = [config server's address] # required if DFS_API_MODE = cluster,
- CSST_DFS_API_MODE = local or cluster # default: local
- CSST_LOCAL_FILE_ROOT = [a local file directory] # required if DFS_API_MODE = local, default: /opt/temp/csst
- CSST_DFS_GATEWAY = [gateway server's address] # required if DFS_API_MODE = cluster,
from .common.logging import setup_logging
from csst_dfs_commons.logging import setup_logging
setup_logging()
\ No newline at end of file
from .delegate import Delegate
from csst_dfs_commons.models import Result
class CatalogApi(object):
def __init__(self):
self.module = Delegate().load(sub_module = "common")
......@@ -15,16 +16,16 @@ class CatalogApi(object):
max_mag: maximal magnitude
obstime: seconds
limit: limits returns the number of records
return: a dict as {success: true, totalCount: 100, records:[.....]}
return: csst_dfs_common.models.Result
'''
if catalog_name == "gaia3":
return self.gaia3_query(ra, dec, radius, min_mag, max_mag, obstime, limit)
else:
raise Exception("%s catalog search not yet implemented" %(catalog_name, ))
return Result.error(message="%s catalog search not yet implemented" %(catalog_name, ))
def gaia3_query(self, ra: float, dec: float, radius: float, min_mag: float, max_mag: float, obstime: int, limit: int):
''' retrieval GAIA DR 3
''' retrieval GAIA EDR 3
args:
ra: in deg
dec: in deg
......@@ -33,7 +34,9 @@ class CatalogApi(object):
max_mag: maximal magnitude
obstime: seconds
limit: limits returns the number of records
return: a dict as {success: true, totalCount: 100, records:[.....]}
return: csst_dfs_common.models.Result
'''
return self.stub.gaia3_query(ra, dec, radius, min_mag, max_mag, obstime, limit)
try:
return self.stub.gaia3_query(ra, dec, radius, min_mag, max_mag, obstime, limit)
except Exception as e:
return Result.error(message=repr(e))
......@@ -2,7 +2,8 @@ import os
import importlib
import logging
from .errors import *
from csst_dfs_commons.models.errors import *
from .constants import *
log = logging.getLogger('csst')
......@@ -26,13 +27,19 @@ class Delegate(object):
raise CSSTFatalException("please install csst_dfs_api_local firstly.")
if self.mode == MODE_CLUSTER:
self.config_server = os.getenv("CSST_DFS_CONFIG_SERVER")
if not self.config_server:
raise CSSTGenericException("enviroment variable CSST_DFS_CONFIG_SERVER is not set")
self.gateway = os.getenv("CSST_DFS_GATEWAY")
if not self.gateway:
raise CSSTGenericException("enviroment variable CSST_DFS_GATEWAY is not set")
try:
from csst_dfs_api_cluster._version import version as cluster_version
except ImportError:
raise CSSTFatalException("please install csst_dfs_api_cluster firstly.")
if not os.getenv("CSST_DFS_APP_ID"):
raise CSSTGenericException("enviroment variable CSST_DFS_APP_ID is not set")
if not os.getenv("CSST_DFS_APP_TOKEN"):
raise CSSTGenericException("enviroment variable CSST_DFS_APP_TOKEN is not set")
def load(self, sub_module):
return importlib.import_module(f"{API_MODULE_PREFIX}{self.mode}.{sub_module}")
class CSSTGenericException(Exception):
def __init__(self, *args: object) -> None:
super(CSSTGenericException, self).__init__(*args)
class CSSTFatalException(Exception):
def __init__(self, *args: object) -> None:
super(CSSTFatalException, self).__init__(*args)
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020 Shoulin Wei
#
# This file is part of CSST.
# coding: utf-8
import logging
import logging.handlers
def setup_logging():
""" Setup logging configuration """
# Console formatter, mention name
cfmt = logging.Formatter(('%(name)s - %(levelname)s - %(message)s'))
# File formatter, mention time
ffmt = logging.Formatter(('%(asctime)s - %(levelname)s - %(message)s'))
# Console handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(cfmt)
# File handler
fh = logging.handlers.RotatingFileHandler('csst_dfs.log',
maxBytes=10*1024*1024, backupCount=10)
fh.setLevel(logging.INFO)
fh.setFormatter(ffmt)
# Create the logger,
# adding the console and file handler
csst_logger = logging.getLogger('csst')
csst_logger.handlers = []
csst_logger.setLevel(logging.DEBUG)
csst_logger.addHandler(ch)
csst_logger.addHandler(fh)
# Set up the concurrent.futures logger
cf_logger = logging.getLogger('concurrent.futures')
cf_logger.setLevel(logging.DEBUG)
cf_logger.addHandler(ch)
cf_logger.addHandler(fh)
return csst_logger
def setup_test_logging():
# Console formatter, mention name
cfmt = logging.Formatter(('%(name)s - %(levelname)s - %(message)s'))
# File formatter, mention time
ffmt = logging.Formatter(('%(asctime)s - %(levelname)s - %(message)s'))
# Only warnings and more serious stuff on the console
ch = logging.StreamHandler()
ch.setLevel(logging.WARN)
ch.setFormatter(cfmt)
# Outputs DEBUG level logging to file
fh = logging.FileHandler('csst-test.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(ffmt)
# Set up the montblanc logger
csst_logger = logging.getLogger('csst')
csst_logger.handlers = []
csst_logger.setLevel(logging.DEBUG)
csst_logger.addHandler(ch)
csst_logger.addHandler(fh)
# Set up the concurrent.futures logger
cf_logger = logging.getLogger('concurrent.futures')
cf_logger.setLevel(logging.DEBUG)
cf_logger.addHandler(ch)
cf_logger.addHandler(fh)
return csst_logger
\ No newline at end of file
......@@ -9,7 +9,6 @@ class FitsApi(object):
self.sub_system = sub_system
self.module = Delegate().load(sub_module = "ifs")
self.stub = getattr(self.module, "FitsApi")()
self.file_prefix = self.stub.root_dir
def find(self, **kwargs):
'''
......@@ -19,9 +18,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
'''
return self.stub.find(**kwargs)
......
......@@ -4,7 +4,7 @@ import pandas as pd
import logging
from astropy.io import fits
from csst_dfs_api.common.logging import setup_logging
from csst_dfs_commons.logging import setup_logging
from csst_dfs_api.ifs import FitsApi, RefFitsApi, Result0Api, Result1Api
setup_logging()
......@@ -22,7 +22,10 @@ class RSS(object):
try:
self.raw = self.fitsApi.find(file_name=file_name)
self.raw = self.raw[0] if self.raw else None
if self.raw.success():
self.raw = self.raw.data()[0] if len(self.raw.data())>0 else None
if self.raw is None:
log.error('raw %s not found' %(file_name,))
else:
......
astropy
\ 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
......@@ -10,5 +10,5 @@ class CommonCatalogTestCase(unittest.TestCase):
self.api = CatalogApi()
def test_gaia3_query(self):
recs = self.api.gaia3_query(ra=56.234039029108935, dec=14.466534827703473, radius=4, min_mag=-1, max_mag=-1, obstime = -1, limit = 2)
print('find:', recs)
result = self.api.gaia3_query(ra=160, dec=-17, radius=0.2, min_mag=-1, max_mag=-1, obstime = -1, limit = 2)
print('return:', result)
......@@ -10,47 +10,47 @@ class IFSFitsTestCase(unittest.TestCase):
self.api = FitsApi()
def test_find(self):
recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
print('find:', recs)
assert len(recs) == 1
# recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
# print('find:', recs)
# assert len(recs) == 1
recs = self.api.find()
recs = self.api.find(limit=1)
print('find:', recs)
assert len(recs) > 1
# assert len(recs) > 1
def test_read(self):
recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
print("The full path: ", os.path.join(self.api.file_prefix, recs[0]['file_path']))
# def test_read(self):
# recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
# print("The full path: ", os.path.join(self.api.file_prefix, recs[0]['file_path']))
file_segments = self.api.read(file_path=recs[0]['file_path'])
file_bytes = b''.join(file_segments)
hdul = fits.HDUList.fromstring(file_bytes)
print(hdul.info())
hdr = hdul[0].header
print(repr(hdr))
# file_segments = self.api.read(file_path=recs[0]['file_path'])
# file_bytes = b''.join(file_segments)
# hdul = fits.HDUList.fromstring(file_bytes)
# print(hdul.info())
# hdr = hdul[0].header
# print(repr(hdr))
def test_update_proc_status(self):
recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
# def test_update_proc_status(self):
# recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
self.api.update_proc_status(fits_id=recs[0]['id'],status=1)
# self.api.update_proc_status(fits_id=recs[0]['id'],status=1)
rec = self.api.get(fits_id=recs[0]['id'])
assert rec['prc_status'] == 1
# rec = self.api.get(fits_id=recs[0]['id'])
# assert rec['prc_status'] == 1
def test_update_qc0_status(self):
recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
# def test_update_qc0_status(self):
# recs = self.api.find(file_name='CCD1_ObsTime_300_ObsNum_7.fits')
self.api.update_qc0_status(fits_id=recs[0]['id'],status=1)
# self.api.update_qc0_status(fits_id=recs[0]['id'],status=1)
rec = self.api.get(fits_id=recs[0]['id'])
assert rec['qc0_status'] == 1
# rec = self.api.get(fits_id=recs[0]['id'])
# assert rec['qc0_status'] == 1
def test_write(self):
recs = self.api.write(file_path='/opt/temp/csst_ifs/CCD2_ObsTime_1200_ObsNum_40.fits')
# def test_write(self):
# recs = self.api.write(file_path='/opt/temp/csst_ifs/CCD2_ObsTime_1200_ObsNum_40.fits')
recs = self.api.find(file_name='CCD2_ObsTime_1200_ObsNum_40.fits')
# recs = self.api.find(file_name='CCD2_ObsTime_1200_ObsNum_40.fits')
rec = self.api.get(fits_id=recs[0]['id'])
# rec = self.api.get(fits_id=recs[0]['id'])
print(rec)
# print(rec)
\ 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