Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csst-dfs
csst-dfs-api
Commits
b99d7b0a
Commit
b99d7b0a
authored
Apr 15, 2021
by
Wei Shoulin
Browse files
ephem
parent
7c92aad2
Changes
11
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
b99d7b0a
...
...
@@ -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,
csst_dfs_api/__init__.py
View file @
b99d7b0a
from
.
common.logging
import
setup_logging
from
csst_dfs_
common
s
.logging
import
setup_logging
setup_logging
()
\ No newline at end of file
csst_dfs_api/common/catalog.py
View file @
b99d7b0a
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
:
r
aise
Exception
(
"%s catalog search not yet implemented"
%
(
catalog_name
,
))
r
eturn
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
E
DR 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
))
csst_dfs_api/common/delegate.py
View file @
b99d7b0a
...
...
@@ -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
}
"
)
csst_dfs_api/common/errors.py
deleted
100644 → 0
View file @
7c92aad2
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
csst_dfs_api/common/logging.py
deleted
100644 → 0
View file @
7c92aad2
#!/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
csst_dfs_api/ifs/fits.py
View file @
b99d7b0a
...
...
@@ -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
)
...
...
examples/ifs_rss_pipeline.py
View file @
b99d7b0a
...
...
@@ -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_common
s
.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
:
...
...
requirements.txt
View file @
b99d7b0a
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
tests/test_common_catalog.py
View file @
b99d7b0a
...
...
@@ -10,5 +10,5 @@ class CommonCatalogTestCase(unittest.TestCase):
self
.
api
=
CatalogApi
()
def
test_gaia3_query
(
self
):
re
c
s
=
self
.
api
.
gaia3_query
(
ra
=
56.234039029108935
,
dec
=
14.466534827703473
,
radius
=
4
,
min_mag
=-
1
,
max_mag
=-
1
,
obstime
=
-
1
,
limit
=
2
)
print
(
'
find
:'
,
re
c
s
)
res
ult
=
self
.
api
.
gaia3_query
(
ra
=
160
,
dec
=-
17
,
radius
=
0.2
,
min_mag
=-
1
,
max_mag
=-
1
,
obstime
=
-
1
,
limit
=
2
)
print
(
'
return
:'
,
res
ult
)
tests/test_ifs_fits.py
View file @
b99d7b0a
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment