Example #1
0
def image_ts(input_file, output_file, psf, model, scales, downsample, residual,
             morphology, width, overwrite, threshold):
    """
    Compute source model residual images.

    The input ``data`` FITS file must contain the following HDU extensions:

    * 'counts' -- Counts image
    * 'background' -- Background image
    * 'exposure' -- Exposure image
    """
    # Execute script
    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale

    # Read data
    log.info('Reading {}'.format(input_file))
    maps = fits.open(input_file)
    log.info('Reading {}'.format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        log.info('Reading {}'.format(model))
        data = fits.getdata(model)
        header = fits.getheader(model)
        maps.append(fits.ImageHDU(data, header, 'OnModel'))

    results = compute_ts_map_multiscale(maps, psf_parameters, scales,
                                        downsample, residual, morphology,
                                        width, threshold)

    filename = Path(output_file).name
    folder = Path(output_file).parent

    Path(folder).mkdir(exist_ok=True)

    # Write results to file
    header = maps[0].header
    if len(results) > 1:
        for scale, result in zip(scales, results):
            # TODO: this is unnecessarily complex
            # Simplify, e.g. by letting the user specify a `base_dir`.
            filename_ = filename.replace('.fits',
                                         '_{0:.3f}.fits'.format(scale))
            fn = Path(folder) / filename_

            log.info('Writing {}'.format(fn))
            result.write(str(fn), header, clobber=overwrite)
    elif results:
        log.info('Writing {}'.format(output_file))
        results[0].write(output_file, header, clobber=overwrite)
    else:
        log.info("No results to write to file: all scales have failed")
Example #2
0
def image_ts(input_file, output_file, psf, model, scales, downsample, residual,
             morphology, width, overwrite, threshold):
    """
    Compute source model residual images.

    The input ``data`` FITS file must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    * 'Diffuse' -- Diffuse model image
    * 'ExpGammaMap' -- Exposure image
    """
    # Execute script
    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale

    # Read data
    log.info('Reading {}'.format(input_file))
    maps = fits.open(input_file)
    log.info('Reading {}'.format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        log.info('Reading {}'.format(model))
        data = fits.getdata(model)
        header = fits.getheader(model)
        maps.append(fits.ImageHDU(data, header, 'OnModel'))

    results = compute_ts_map_multiscale(maps, psf_parameters, scales, downsample,
                                        residual, morphology, width, threshold)

    filename = Path(output_file).name
    folder = Path(output_file).parent
    
    Path(folder).mkdir(exist_ok=True) 
    
    # Write results to file
    header = maps[0].header
    if len(results) > 1:
        for scale, result in zip(scales, results):
            # TODO: this is unnecessarily complex
            # Simplify, e.g. by letting the user specify a `base_dir`.
            filename_ = filename.replace('.fits', '_{0:.3f}.fits'.format(scale))          
            fn = Path(folder) / filename_
            
            log.info('Writing {}'.format(fn))
            result.write(str(fn), header, overwrite=overwrite)
    elif results:
        log.info('Writing {}'.format(output_file))
        results[0].write(output_file, header, overwrite=overwrite)
    else:
        log.info("No results to write to file: all scales have failed")
Example #3
0
def image_ts(
    input_file, output_file, psf, model, scales, downsample, residual, morphology, width, overwrite, threshold
):
    """
    Compute source model residual images.

    The input ``data`` FITS file must contain the following HDU extensions:

    * 'counts' -- Counts image
    * 'background' -- Background image
    * 'exposure' -- Exposure image
    """
    # Execute script
    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale
    from gammapy.image import SkyImageCollection

    # Read data
    log.info("Reading {}".format(input_file))
    skyimages = SkyImageCollection.read(input_file)
    log.info("Reading {}".format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        log.info("Reading {}".format(model))
        skyimages["model"] = SkyImage.read(model)

    results = compute_ts_map_multiscale(
        skyimages, psf_parameters, scales, downsample, residual, morphology, width, threshold
    )

    filename = Path(output_file).name
    folder = Path(output_file).parent

    Path(folder).mkdir(exist_ok=True)

    # Write results to file
    if len(results) > 1:
        for scale, result in zip(scales, results):
            # TODO: this is unnecessarily complex
            # Simplify, e.g. by letting the user specify a `base_dir`.
            filename_ = filename.replace(".fits", "_{0:.3f}.fits".format(scale))
            fn = Path(folder) / filename_

            log.info("Writing {}".format(fn))
            result.write(str(fn), clobber=overwrite)
    elif results:
        log.info("Writing {}".format(output_file))
        results[0].write(output_file, clobber=overwrite)
    else:
        log.info("No results to write to file: all scales have failed")
Example #4
0
def ts_image(input_file, output_file, psf, model, scales, downsample, residual,
             morphology, width, overwrite, threshold):
    """
    Compute source model residual images.

    The input `data` fits file must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    * 'Diffuse' -- Diffuse model image
    * 'ExpGammaMap' -- Exposure image
    """
    # Execute script
    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale

    # Read data
    log.info('Reading {}'.format(input_file))
    maps = fits.open(input_file)
    log.info('Reading {}'.format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        log.info('Reading {}'.format(model))
        data = fits.getdata(model)
        header = fits.getheader(model)
        maps.append(fits.ImageHDU(data, header, 'OnModel'))
    results = compute_ts_map_multiscale(maps, psf_parameters, scales,
                                        downsample, residual, morphology,
                                        width)

    folder, filename = os.path.split(output_file)
    if not os.path.exists(folder) and folder != '':
        os.mkdir(folder)

    # Write results to file
    header = maps[0].header
    if len(results) > 1:
        for scale, result in zip(scales, results):
            filename_ = filename.replace('.fits',
                                         '_{0:.3f}.fits'.format(scale))
            log.info('Writing {}'.format(os.path.join(folder, filename_)))
            result.write(os.path.join(folder, filename_),
                         header,
                         overwrite=overwrite)
    else:
        log.info('Writing {}'.format(output_file))
        results[0].write(output_file, header, overwrite=overwrite)
Example #5
0
def ts_image(input_file, output_file, psf, model, scales, downsample, residual,
             morphology, width, overwrite, threshold):
    """
    Compute source model residual images.

    The input `data` fits file must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    * 'Diffuse' -- Diffuse model image
    * 'ExpGammaMap' -- Exposure image
    """
    # Execute script
    import json
    import logging
    logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')

    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale

    # Read data
    logging.info('Reading {}'.format(input_file))
    maps = fits.open(input_file)
    logging.info('Reading {}'.format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        logging.info('Reading {}'.format(model))
        data = fits.getdata(model)
        header = fits.getheader(model)
        maps.append(fits.ImageHDU(data, header, 'OnModel'))
    results = compute_ts_map_multiscale(maps, psf_parameters, scales, downsample,
                                        residual, morphology, width)

    folder, filename = os.path.split(output_file)
    if not os.path.exists(folder) and folder != '':
        os.mkdir(folder)

    # Write results to file
    header = maps[0].header
    if len(results) > 1:
        for scale, result in zip(scales, results):
            filename_ = filename.replace('.fits', '_{0:.3f}.fits'.format(scale))
            logging.info('Writing {}'.format(os.path.join(folder, filename_)))
            result.write(os.path.join(folder, filename_), header, overwrite=overwrite)
    else:
        logging.info('Writing {}'.format(output_file))
        results[0].write(output_file, header, overwrite=overwrite)
Example #6
0
def ts_image(input_file, psf, model, scales, downsample, residual, morphology,
             width, overwrite, folder, threshold):
    """
    Compute source model residual images.

    The input `data` fits file must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    * 'Diffuse' -- Diffuse model image
    * 'ExpGammaMap' -- Exposure image
    """
    # Execute script
    import json
    import logging
    logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')

    from astropy.io import fits
    from gammapy.detect import compute_ts_map_multiscale

    # Read data
    logging.info('Reading {0}'.format(input_file))
    maps = fits.open(input_file)
    logging.info('Reading {0}'.format(psf))
    psf_parameters = json.load(open(psf))

    if residual:
        logging.info('Reading {0}'.format(model))
        data = fits.getdata(model)
        header = fits.getheader(model)
        maps.append(fits.ImageHDU(data, header, 'OnModel'))
    results = compute_ts_map_multiscale(maps, psf_parameters, scales, downsample,
                                        residual, morphology, width)
    folder = morphology.lower()
    if not os.path.exists(folder):
        os.mkdir(folder)

    # Write results to file
    header = maps[0].header
    for scale, result in zip(scales, results):
        filename = os.path.join(folder, 'ts_{0:.3f}.fits'.format(scale))
        logging.info('Writing {0}'.format(filename))
        result.write(filename, header, overwrite=overwrite)