Esempio n. 1
0
    def __init__(self, sys_args=None):
        if sys_args:
            args = sys_args
        elif self._confirm_args():
            args = buildParser(__version__).parse_args()
        else:
            args = buildParser(__version__).parse_args([])

        # acquire any new astrodata classes.
        if args.adpkg:
            import_module(args.adpkg)

        self.mode = args.mode
        self.drpkg = args.drpkg
        self.files = args.files
        self.suffix = args.suffix
        self.ucals = normalize_ucals(args.files, args.user_cal)
        self.uparms = set_btypes(args.userparam)
        self._upload = args.upload
        self._output_filenames = None
        self.recipename = args.recipename if args.recipename else '_default'
Esempio n. 2
0
    def _confirm_args(self):
        """
        Confirm that the first executable frame in the call stack is a reduce
        command line. This asserts that a nominal reduce parser, as returned by
        buildParser() function, is an equivalent Namespace object to that
        of an 'args' key in the stack's 'f_locals' namespace. If the Namespace
        objects are not equal, reduce is not calling this class.

        Parameters
        ----------
        <void>

        Returns
        -------
        is_reduce: <bool>,
            Did 'reduce' call this?

        """
        is_reduce = False
        exe_path = sys.argv[0]
        red_namespace = buildParser(__version__).parse_args([])
        if exe_path:
            cstack = inspect.stack()
            for local, value in list(cstack[-1][0].f_locals.items()):
                if local == 'args':
                    try:
                        assert(
                            list(value.__dict__.keys()) ==
                            list(red_namespace.__dict__.keys())
                        )
                        is_reduce = True
                    except AttributeError:
                        pass
                    except AssertionError:
                        log.stdinfo("A non-reduce command line was detected.")
                        pass

        return is_reduce
Esempio n. 3
0
def get_or_create_tmpdir(tmpdir_factory):
    basetmp = tmpdir_factory.getbasetemp()
    # import pdb; pdb.set_trace()

    # This test suite requires a minimum amount of available disk space.
    #  Will raise a RuntimeError if this isn't the case.
    if get_free_space_mb(os.path.join(
            basetmp.dirname, basetmp.basename)) < FULL_REDUCTION_SPACE_REQD:
        raise RuntimeError('You have insufficient free disk space to run '
                           'the full reduction test suite.')

    try:
        os.chdir(os.path.join(basetmp.dirname, basetmp.basename,
                              FULL_REDUCTION_TMPDIR))
        tmpsubdir = py.path.local(os.getcwd())
        print('tmpsubdir is {}'.format(tmpsubdir))
    except OSError:
        tmpsubdir = tmpdir_factory.mktemp(FULL_REDUCTION_TMPDIR,
                                          numbered=False)
        os.chdir(os.path.join(tmpsubdir.dirname, tmpsubdir.basename))

    # Bring the test data in
    rawfilename = '*MEF.fits'
    rawfiles = glob.glob(os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        'testdata',
        rawfilename))
    if len(rawfiles) == 0:
        raise RuntimeError("Test data for 'fullreduction' test suite is "
                           "not present. Please place a directory "
                           "named 'testdata', filled with "
                           "complete simulated data, into "
                           "{}".format(
            os.sep.join(os.path.abspath(__file__).split(os.sep)[:-1]))
        )
    for _ in rawfiles:
        shutil.copy(
            _,
            os.path.join(tmpsubdir.dirname, tmpsubdir.basename))

    # OLD WAY
    # Blank the calibrations manager
    # print('Blanking calibrations manager')
    # subprocess.check_call(['caldb', 'init',  '-v', '-w', ])

    # NEW WAY
    # Set up the calibration system with appropriate arguments
    os.mkdir('dbdir')
    parser = buildParser(__version__)
    local_db_dir = u'{}'.format(os.path.join(tmpsubdir.dirname,
                                             tmpsubdir.basename, 'dbdir/'), )
    args = parser.parse_args(args=[
        '--local_db_dir',
        local_db_dir,
    ])
    args = normalize_args(args)

    # set_calservice(args)
    #
    # # Get a LocalManager and instantiate
    # # import pdb; pdb.set_trace()
    # lm = LocalManager(globalConf['calibs'].database_dir)
    # lm.init_database(wipe=True)

    cs = CalibrationService()
    cs.config(db_dir=local_db_dir)
    cs.init(wipe=True)

    # TODO If necessary, populate calibration system from testdata/calibs
    # Ideally, however, the database would be populated 'as we go' during the
    # tests

    yield tmpsubdir, cs

    # import pdb; pdb.set_trace()

    # Teardown code - clear out the tmpdir, except the log files
    for _ in glob.glob(os.path.join(
            tmpsubdir.dirname, tmpsubdir.basename,
            '*.fits'),
    ):
        os.remove(_)

    # Remove the detritus of the calibrations system
    try:
        shutil.rmtree(os.path.join(
            tmpsubdir.dirname, tmpsubdir.basename,
            'calibrations'))
    except OSError:
        pass
    try:
        shutil.rmtree(os.path.join(
            tmpsubdir.dirname, tmpsubdir.basename,
            'dbdir'))
    except OSError:
        pass
Esempio n. 4
0
        log.error("Caught KeyboardInterrupt (^C) signal")
        estat = signal.SIGINT
    except Exception as err:
        log.error("reduce caught an unhandled exception.")
        log.error("\nReduce instance aborted.")
        estat = signal.SIGABRT

    if estat != 0:
        log.stdinfo("\n\nreduce exit status: %d\n" % estat)
    else:
        pass
    return estat
# --------------------------------------------------------------------------

if __name__ == "__main__":
    parser = buildParser(rs_version)
    args = parser.parse_args()

    # Deal with argparse structures that are different than optparse
    # Normalizing argument types should happen before 'args' is passed to Reduce.
    args = normalize_args(args)
    args.upload = normalize_upload(args.upload)

    if args.displayflags:
        show_parser_options(parser, args)
        for item in ["Input fits file(s):\t%s" % inf for inf in args.files]:
            print(item)
        sys.exit()

    sys.exit(main(args))