Commit 75c17930 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

add CsstMsFile

parent 20053b69
...@@ -149,6 +149,7 @@ class CsstMsDataManager: ...@@ -149,6 +149,7 @@ class CsstMsDataManager:
>>> # define an L1 file (non-detector-specified) >>> # define an L1 file (non-detector-specified)
>>> dm.l1_file("flipped_image.fits") >>> dm.l1_file("flipped_image.fits")
""" """
# TODO: update docstring # TODO: update docstring
def __init__(self, def __init__(self,
ver_sim: str = "C6.2", ver_sim: str = "C6.2",
...@@ -773,19 +774,19 @@ class CsstMsDataManager: ...@@ -773,19 +774,19 @@ class CsstMsDataManager:
ra=pointing_ra, ra=pointing_ra,
dec=pointing_dec, dec=pointing_dec,
columns=( columns=(
'ref_epoch', 'ref_epoch',
'ra', 'ra',
'ra_error', 'ra_error',
'dec', 'dec',
'dec_error', 'dec_error',
'parallax', 'parallax',
'parallax_error', 'parallax_error',
'pmra', 'pmra',
'pmra_error', 'pmra_error',
'pmdec', 'pmdec',
'pmdec_error', 'pmdec_error',
'phot_g_mean_mag', 'phot_g_mean_mag',
'source_id' 'source_id'
), ),
radius=2, radius=2,
min_mag=0, min_mag=0,
...@@ -1004,3 +1005,80 @@ class CsstMsDataManager: ...@@ -1004,3 +1005,80 @@ class CsstMsDataManager:
# temporarily compatible with old interface # temporarily compatible with old interface
CsstMbiDataManager = CsstMsDataManager CsstMbiDataManager = CsstMsDataManager
class CsstMsFile(dict):
def __init__(self, filepath, ver_sim="C6.2", dir_l0=".", dir_l1=".", dfs_root=None, **kwargs):
super(CsstMsFile, self).__init__()
self.ver_sim = ver_sim
self.dir_l0 = dir_l0
self.dir_l1 = dir_l1
self.dfs_root = dfs_root
self.filename = os.path.basename(filepath)
self.dir_l0 = os.path.dirname(filepath)
# parse filename
pattern = re.compile(
r"(?P<telescope>[A-Z]+)_"
r"(?P<instrument>[A-Z]+)_"
r"(?P<project>[A-Z]+)_"
r"(?P<obs_type>[A-Z]+)_"
r"(?P<exp_start>[0-9]{14})_"
r"(?P<exp_stop>[0-9]{14})_"
r"(?P<obs_id>[0-9]{11})_"
r"(?P<detector>[0-9]{2})_"
r"L(?P<level>[0-9]{1})_"
r"V(?P<version>[0-9]{2})"
r".(?P<ext>[a-z]{4})"
)
mo = re.fullmatch(pattern, self.filename)
assert mo is not None
mogd = mo.groupdict()
# mogd.pop("detector")
for k, v in mogd.items():
self.__setattr__(k, v)
for k, v in kwargs.items():
self.__setattr__(k, v)
@property
def dir_l0(self):
if self.dfs_root is not None:
return os.path.join(self.dfs_root, self._dir_l0)
else:
return self._dir_l0
@dir_l0.setter
def dir_l0(self, dir_l0):
self._dir_l0 = dir_l0
def fpo(self, post=".fits"):
if post.startswith("."):
fn = f"{self.telescope}_{self.instrument}_{self.project}_{self.obs_type}_{self.exp_start}_{self.exp_stop}_" \
f"{self.obs_id}_{self.detector}_L1_V{self.version}{post}"
else:
fn = f"{self.telescope}_{self.instrument}_{self.project}_{self.obs_type}_{self.exp_start}_{self.exp_stop}_" \
f"{self.obs_id}_{self.detector}_L1_V{self.version}_{post}"
return os.path.join(self.dir_l1, fn)
def fpi(self):
return os.path.join(self.dir_l0, self.filename)
@staticmethod
def from_l1id(id=17796, ver_sim="C6.2", dir_l0=".", dir_l1=".", dfs_root=None):
L1DataApi = Level1DataApi()
rec = L1DataApi.get(id=id)
try:
assert rec["code"] == 0
except AssertionError as ae:
print(rec)
raise ae
header = rec["data"].header
return CsstMsFile(
filepath=rec["data"].file_path, ver_sim=ver_sim, dir_l0=dir_l0, dir_l1=dir_l1, dfs_root=dfs_root, **header)
# file = CsstMsFile(
# "L1/MSC/SCI/62173/10160000108/CSST_MSC_MS_SCI_20290206174352_20290206174622_10160000108_21_L1_V01.fits")
#
# file = CsstMsFile.from_l1id(id=17796, dfs_root="/share/dfs")
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