Simplify init functions of DataStore class
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import subprocess
-from ..utils.scripts import make_path
+from ..utils.scripts import Path, make_path
from ..utils.testing import Checker
from .obs_table import ObservationTable
from .hdu_index_table import HDUIndexTable
@@ -52,37 +52,41 @@ class DataStore(object):
@classmethod
def from_files(cls, base_dir, hdu_table_filename=None, obs_table_filename=None):
- """Construct from HDU and observation index table files."""
+ """Construct from HDU and observation index table files.
+
+ If no filenames are specified, files with default filenames
+ will be looked for in `base_dir`.
+
+ Alternatively, the filenames can be specified explicitly, either
+ relative to `base_dir` (will be checked first) or by giving absolute paths.
+ """
+ base_dir = make_path(base_dir)
+
if hdu_table_filename:
+ if Path.exists(base_dir / hdu_table_filename):
+ hdu_table_filename = base_dir / hdu_table_filename
+ else:
+ hdu_table_filename = base_dir / cls.DEFAULT_HDU_TABLE
+
+ if obs_table_filename:
+ if Path.exists(base_dir / obs_table_filename):
+ obs_table_filename = base_dir / obs_table_filename
+ else:
+ obs_table_filename = base_dir / cls.DEFAULT_OBS_TABLE
+
+ if not Path.exists(hdu_table_filename):
+ raise IOError("File not found: {}".format(hdu_table_filename))
log.debug("Reading {}".format(hdu_table_filename))
hdu_table = HDUIndexTable.read(str(hdu_table_filename), format="fits")
-
hdu_table.meta["BASE_DIR"] = str(base_dir)
- else:
- hdu_table = None
- if obs_table_filename:
+ if not Path.exists(obs_table_filename):
+ raise IOError("File not found: {}".format(obs_table_filename))
log.debug("Reading {}".format(str(obs_table_filename)))
obs_table = ObservationTable.read(str(obs_table_filename), format="fits")
- else:
- obs_table = None
return cls(hdu_table=hdu_table, obs_table=obs_table)
- @classmethod
- def from_dir(cls, base_dir):
- """Create from a directory.
-
- This assumes that the HDU and observations index tables
- have the default filename.
- """
- base_dir = make_path(base_dir)
- return cls.from_files(
- base_dir=base_dir,
- hdu_table_filename=base_dir / cls.DEFAULT_HDU_TABLE,
- obs_table_filename=base_dir / cls.DEFAULT_OBS_TABLE,
- )
-
@classmethod
def from_config(cls, config):
"""Create from a config dict."""
