Beispiel #1
0
def main():
    """ Main function for command line usage """
    usage = "usage: %(prog)s [options] "
    description = "Merge a set of Fermi-LAT files."

    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('-o', '--output', default=None, type=str,
                        help='Output file.')
    parser.add_argument('--ccube', default=None, type=str,
                        help='Input counts cube file .')
    parser.add_argument('--bexpcube', default=None, type=str,
                        help='Input binned exposure cube.')
    parser.add_argument('--hpx_order', default=None, type=int,
                        help='Order of output map: default = counts map order')
    parser.add_argument('--clobber', action='store_true',
                        help='Overwrite output file')
 
    args = parser.parse_args()

    ccube = HpxMap.create_from_fits(args.ccube, hdu='SKYMAP')
    bexpcube = HpxMap.create_from_fits(args.bexpcube, hdu='HPXEXPOSURES')
    
    if args.hpx_order:
        hpx_order = args.hpx_order
    else:
        hpx_order = ccube.hpx.order

    out_cube = intensity_cube(ccube, bexpcube, hpx_order)
    out_cube.hpx.write_fits(out_cube.data, args.output, clobber=args.clobber)
Beispiel #2
0
def stack_energy_planes_hpx(filelist, **kwargs):
    """
    """
    from fermipy.skymap import HpxMap
    from fermipy.hpx_utils import HPX
    maplist = [HpxMap.create_from_fits(fname, **kwargs) for fname in filelist]
    energies = np.log10(np.hstack([amap.hpx.evals
                                   for amap in maplist])).squeeze()

    counts = np.hstack([amap.counts.flat for amap in maplist])
    counts = counts.reshape((len(energies), int(len(counts) / len(energies))))

    template_map = maplist[0]
    hpx = HPX.create_from_header(template_map.hpx.make_header(), energies)
    return HpxMap(counts, hpx)
Beispiel #3
0
def stack_energy_planes_hpx(filelist, **kwargs):
    """
    """
    from fermipy.skymap import HpxMap
    from fermipy.hpx_utils import HPX
    maplist = [HpxMap.create_from_fits(fname, **kwargs) for fname in filelist]
    energies = np.log10(
        np.hstack([amap.hpx.evals for amap in maplist])).squeeze()

    counts = np.hstack([amap.counts.flat for amap in maplist])
    counts = counts.reshape((len(energies), int(len(counts) / len(energies))))

    template_map = maplist[0]
    hpx = HPX.create_from_header(template_map.hpx.make_header(), energies)
    return HpxMap(counts, hpx)
Beispiel #4
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        # Read the input maps
        ccube_dirty = HpxMap.create_from_fits(args.ccube_dirty, hdu='SKYMAP')
        bexpcube_dirty = HpxMap.create_from_fits(args.bexpcube_dirty, hdu='HPXEXPOSURES')
        ccube_clean = HpxMap.create_from_fits(args.ccube_clean, hdu='SKYMAP')
        bexpcube_clean = HpxMap.create_from_fits(args.bexpcube_clean, hdu='HPXEXPOSURES')

        # Decide what HEALPix order to work at
        if args.hpx_order:
            hpx_order = args.hpx_order
        else:
            hpx_order = ccube_dirty.hpx.order

        # Cast all the input maps to match ccube_clean
        cube_dict = ResidualCR._match_cubes(ccube_clean, ccube_dirty,
                                            bexpcube_clean, bexpcube_dirty, hpx_order)

        # Intenstiy maps
        intensity_clean = ResidualCR._compute_intensity(cube_dict['ccube_clean'],
                                                        cube_dict['bexpcube_clean'])
        intensity_dirty = ResidualCR._compute_intensity(cube_dict['ccube_dirty'],
                                                        cube_dict['bexpcube_dirty'])
        # Mean & ratio of intensity maps
        intensity_mean = ResidualCR._compute_mean(intensity_dirty,
                                                  intensity_clean)
        intensity_ratio = ResidualCR._compute_ratio(intensity_dirty,
                                                    intensity_clean)
        # Selecting the bright pixels for Aeff correction and to mask when filling output map
        bright_pixel_select = ResidualCR._make_bright_pixel_mask(intensity_mean,
                                                                 args.select_factor)
        bright_pixel_mask = ResidualCR._make_bright_pixel_mask(intensity_mean,
                                                               args.mask_factor)
        # Compute thte Aeff corrections using the brightest pixels
        aeff_corrections = ResidualCR._get_aeff_corrections(intensity_ratio,
                                                            bright_pixel_select)
        # Apply the Aeff corrections and get the intensity residual
        corrected_dirty = ResidualCR._apply_aeff_corrections(intensity_dirty,
                                                             aeff_corrections)
        corrected_ratio = ResidualCR._compute_ratio(corrected_dirty,
                                                    intensity_clean)
        intensity_resid = ResidualCR._compute_diff(corrected_dirty,
                                                   intensity_clean)
        # Replace the masked pixels with the map mean to avoid features associates with sources
        filled_resid = ResidualCR._fill_masked_intensity_resid(intensity_resid,
                                                               bright_pixel_mask)
        # Smooth the map
        smooth_resid = ResidualCR._smooth_hpx_map(filled_resid,
                                                  args.sigma)
        # Convert to a differential map
        out_model = ResidualCR._intergral_to_differential(smooth_resid)

        # Make the ENERGIES HDU
        out_energies = ccube_dirty.hpx.make_energies_hdu()

        # Write the maps
        cubes = dict(SKYMAP=out_model)
        fits_utils.write_maps(None, cubes,
                              args.outfile, energy_hdu=out_energies)

        if args.full_output:
            # Some diagnostics
            check = ResidualCR._differential_to_integral(out_model)
            check_resid = ResidualCR._compute_diff(smooth_resid, check)
            counts_resid =\
                ResidualCR._compute_counts_from_intensity(intensity_resid,
                                                          cube_dict['bexpcube_dirty'])
            pred_counts\
                = ResidualCR._compute_counts_from_model(out_model,
                                                        cube_dict['bexpcube_dirty'])
            pred_resid = ResidualCR._compute_diff(pred_counts, counts_resid)

            out_ebounds = ccube_dirty.hpx.make_energy_bounds_hdu()
            cubes = dict(INTENSITY_CLEAN=intensity_clean,
                         INTENSITY_DIRTY=intensity_dirty,
                         INTENSITY_RATIO=intensity_ratio,
                         CORRECTED_DIRTY=corrected_dirty,
                         CORRECTED_RATIO=corrected_ratio,
                         INTENSITY_RESID=intensity_resid,
                         PIXEL_SELECT=bright_pixel_select,
                         PIXEL_MASK=bright_pixel_mask,
                         FILLED_RESID=filled_resid,
                         SMOOTH_RESID=smooth_resid,
                         CHECK=check,
                         CHECK_RESID=check_resid,
                         COUNTS_RESID=counts_resid,
                         PRED_COUNTS=pred_counts,
                         PRED_RESID=pred_resid)

            fits_utils.write_maps(None, cubes,
                                  args.outfile.replace('.fits', '_full.fits'),
                                  energy_hdu=out_ebounds)