Exemple #1
0
def process_frame(fn1, fn2, out_dir):
    
    flat_fn = None
    obj_fn = None
    
    fn1_header = fits.PrimaryHDU.readfrom(fn1, ignore_missing_end=True).header
    fn2_header = fits.PrimaryHDU.readfrom(fn2, ignore_missing_end=True).header

    
    if fn1_header['flat'] == 1 and fn1_header['calmpos'] == 1:
        flat_fn = fn1
        obj_fn = fn2
        obj_header = fn2_header
        flat_header = fn1_header
    if fn2_header['flat'] == 1 and fn2_header['calmpos'] == 1:
        if flat_fn is not None:
            raise DrpException('two flats, no object frame')
        else:
            flat_fn = fn2
            obj_fn = fn1
            obj_header = fn1_header
            flat_header = fn2_header
    if flat_fn is None:
        raise DrpException('no flat')

    if obj_header['ECHLPOS'] > 100:
        print('ERROR: cannot reduce low-resolution image (ECHLPOS > 100')
        exit(1)
        
    if obj_header['NAXIS1'] != constants.N_COLS:
        raise DrpException('NAXIS1 != {}'.format(constants.N_COLS))
    if obj_header['NAXIS2'] != constants.N_ROWS:
        raise DrpException('NAXIS2 != {}'.format(constants.N_COLS))
    if obj_header['FILNAME'].lower().find('nirspec') < 0:
        raise DrpException('unsupported filter: {}'.format(obj_header['FILNAME']))
    
    if create_raw_data_sets.flat_criteria_met(obj_header, flat_header, ignore_dispers=True) is False:
        raise DrpException('flat is not compatible with object frame')
    
    rawDataSet = RawDataSet.RawDataSet(obj_fn, obj_header)
    rawDataSet.flatFileNames.append(flat_fn)
     
    if not os.path.exists(out_dir):
        try: 
            os.mkdir(out_dir)
        except: 
            msg = 'output directory {} does not exist and cannot be created'.format(out_dir)
            raise IOError(msg)
                
    logger = logging.getLogger('main')

    # generate reduced data set by reducing raw data set
    reducedDataSet = reduce_frame.reduce(rawDataSet, out_dir)
    
    # write reduction summary to log file
    write_summary(reducedDataSet)
        
    # produce data products from reduced data set
    if config.params['no_products'] is True:
        logger.info('data product generation inhibited by command line switch')
    else:
        products.gen(reducedDataSet, out_dir)

    # if diagnostics mode is enabled, then produce diagnostic data products
    if config.params['dgn'] is True:
        logger.info('diagnostic mode enabled, generating diagnostic data products')
        dgn.gen(reducedDataSet, out_dir)
        
    return    
Exemple #2
0
def process_dir(in_dir, base_out_dir):
    """
    NSDRP. Assembles raw data sets from FITS files in the input directory,
    then generates reduced data sets from raw data sets.  Level 1 data products
    are generated from the reduced data sets and saved in the output directory.
    """
        
    # use main logger for outer logging, reduce_frame makes the per frame logger
    logger = logging.getLogger('main')
    
    ssFptr = start_summary_ss(in_dir, base_out_dir)
    
    # get list of raw data sets arranged in chronological order
    rawDataSets = create_raw_data_sets.create(in_dir)
    
    if rawDataSets is None:
        return
    
    # keep track of how many data sets have been successfully reduced
    n_reduced = len(rawDataSets)
        
    # instantiate a flat cacher so each flat or set of flats is only reduced once 
    flatCacher = FlatCacher.FlatCacher(logger, base_out_dir + '/flats')
    
    reducedDataSets = []
    nirspecConfig = None
    
    # process each raw data set   
    for rawDataSet in rawDataSets:
        
        # determine the output directory to use for this frame
        out_dir = get_out_dir(rawDataSet.objFileName, base_out_dir);
        
        if nirspecConfig is None:
            nirspecConfig = NirspecConfig.NirspecConfig(rawDataSet.objHeader)
        else:
            if nirspecConfig.isTheSame(rawDataSet.objHeader) == False:
    
                if len(reducedDataSets) > 0:
    
                    if len(reducedDataSets) > 1:
                        mcal(reducedDataSets)
    
                    gen_data_products(reducedDataSets, nirspecConfig, base_out_dir, ssFptr)
    
                    del reducedDataSets[:] 
                    logger.info('starting new multi-frame set for nirpsec config: {}'.format(
                            nirspecConfig.toString()))
                    nirspecConfig = NirspecConfig.NirspecConfig(rawDataSet.objHeader)
            
        try:
            reducedDataSets.append(reduce_frame.reduce(rawDataSet, out_dir, flatCacher))  
        except DrpException as e:
            n_reduced -= 1
            logger.error('failed to reduce {}: {}'.format(
                    rawDataSet.objFileName, e.message))
        except IOError as e:
            logger.critical('DRP failed due to I/O error: {}'.format(str(e)))
            sys.exit(1)
            
    if len(reducedDataSets) > 1:
        logger.info('doing multi-frame calibration on {} frames, nirspec config: {}'.format(
                len(reducedDataSets), nirspecConfig.toString()))
        mcal(reducedDataSets)

    gen_data_products(reducedDataSets, nirspecConfig, base_out_dir, ssFptr)
            
    if len(rawDataSets) > 0:
        logger.info('n object frames reduced = {}/{}'.format(
                n_reduced, len(rawDataSets)))   
        
    ssFptr.close()
        
    logger.info('end nsdrp')
    return