Exemplo n.º 1
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        limitfile = args.limitfile
        first = args.seed
        last = first + args.nsims
        flist = [limitfile.replace("_SEED.fits", "_%06i.fits" % seed)\
                     for seed in range(first, last)]

        spec_config = load_yaml(args.specconfig)
        specs = spec_config['specs']
        sum_specs = specs.copy()

        outfile = args.outfile
        summaryfile = args.summaryfile

        hdus = sum_specs + ['INDICES']

        out_tables, out_names = vstack_tables(flist, hdus)

        if is_not_null(outfile):
            fits_utils.write_tables_to_fits(outfile,
                                            out_tables,
                                            namelist=out_names)

        if is_not_null(summaryfile):
            summary_tables = []
            for ot in out_tables[0:-1]:
                summary_table = summarize_limits_results(ot)
                summary_tables.append(summary_table)
            summary_tables.append(Table(out_tables[-1][0]))
            fits_utils.write_tables_to_fits(summaryfile,
                                            summary_tables,
                                            namelist=out_names)
Exemplo n.º 2
0
    def write_fits(self, filepath, clobber=False):
        """ Write this object to a FITS file

        Paramters
        ---------

        filepath : str
            Path to output file

        clobber : bool
            Flag to allow overwriting existing files

        """
        tables = [self._e_table]
        tablenames = ["EBOUNDS"]
        ref_cards = [{}]
        for key, val in self._s_tables.items():
            tables.append(val)
            tablenames.append(key)
            ref_cards.append(self._ref_val_dict[key])
        fits_utils.write_tables_to_fits(filepath,
                                        tables,
                                        clobber=clobber,
                                        namelist=tablenames,
                                        cardslist=ref_cards)
Exemplo n.º 3
0
 def write_stacked(basedir, roster_name, stacked_dict, jprior_key, clobber):
     """ Write the stacked DMCastroData object to a FITS file
     """
     outdir = os.path.join(basedir, "stacked")
     try:
         os.makedirs(outdir)
     except OSError:
         pass
     outpath = os.path.join(
         outdir, "results_%s_%s.fits" % (roster_name, jprior_key))
     print("Writing stacked results %s" % outpath)
     channels = stacked_dict.keys()
     t_list = []
     n_list = []
     mass_table = None
     for chan in channels:
         stacked = stacked_dict[chan]
         if mass_table is None:
             mass_table = stacked.build_mass_table()
         t_list.append(stacked.build_scandata_table())
         n_list.append(chan)
     t_list.append(mass_table)
     n_list.append("MASSES")
     fits_utils.write_tables_to_fits(outpath,
                                     t_list,
                                     clobber=clobber,
                                     namelist=n_list)
Exemplo n.º 4
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        channels = [
            'ee', 'mumu', 'tautau', 'bb', 'tt', 'gg', 'ww', 'zz', 'cc', 'uu',
            'dd', 'ss'
        ]
        norm_type = 'eflux'

        spec_table = DMSpecTable.create_from_fits(args.spec)
        profile = load_yaml(args.profile_yaml)

        j_value = profile.get('j_integ')
        j_sigma = profile.get('j_sigma', None)
        if args.jprior is None or args.jprior == 'None' or j_sigma is None or j_sigma == 0.0:
            j_factor = j_value
            j_prior_key = 'none'
        else:
            j_factor = dict(functype=args.jprior,
                            j_value=j_value,
                            mu=j_value,
                            sigma=j_sigma)
            j_prior_key = args.jprior

        sed = CastroData.create_from_sedfile(args.sed_file, norm_type)
        c_list, t_list, n_list = DMCastroConvertor.convert_sed_to_dm(
            spec_table, sed, channels, norm_type, j_factor)

        fits_utils.write_tables_to_fits(args.outfile,
                                        t_list,
                                        clobber=args.clobber,
                                        namelist=n_list)
Exemplo n.º 5
0
 def write_fits(self, filepath, clobber=False):
     """ Write this object to a FITS file
     """
     fits_utils.write_tables_to_fits(filepath,
                                     [self._e_table, self._s_table],
                                     clobber=clobber,
                                     namelist=["EBOUNDS", "SPECDATA"],
                                     cardslist=[{}, self._ref_vals])
Exemplo n.º 6
0
    def write_fits_files(stacked_dict, resultsfile, limitfile, clobber=False):
        """ Write the stacked DMCastroData object and limits a FITS files

        Parameters
        ----------

        stacked_dict : dict
            Dictionary of `DMCastroData` objects, keyed by channel

        resultsfile : str
            Path to the output file to write the `DMCastroData` objects to

        limitfile : str
            Path to write the upper limits to

        clobber : bool
            Overwrite existing files

        """
        channels = list(stacked_dict.keys())
        t_list = []
        n_list = []
        lim_list = []
        lim_table_list = []
        mass_table = None
        alphas = [0.68, 0.95]
        for chan in channels:
            is_decay = chan.find('_decay') >= 0
            stacked = stacked_dict[chan]
            mles = stacked.mles()
            limit_dict = dict(mles=mles)
            for alpha in alphas:
                if is_decay:
                    limits = stacked.getLimits(1. - alpha)
                    limit_dict['ll_%.02f' % alpha] = limits
                else:
                    limits = stacked.getLimits(alpha)
                    limit_dict['ul_%.02f' % alpha] = limits
            tab_limits = stacked.build_limits_table(limit_dict)
            if mass_table is None:
                mass_table = stacked.build_mass_table()
            t_list.append(stacked.build_scandata_table())
            n_list.append(chan)
            lim_list.append(limit_dict)
            lim_table_list.append(tab_limits)

        t_list.append(mass_table)
        lim_table_list.append(mass_table)
        n_list.append("MASSES")
        fits_utils.write_tables_to_fits(resultsfile,
                                        t_list,
                                        clobber=clobber,
                                        namelist=n_list)
        fits_utils.write_tables_to_fits(limitfile,
                                        lim_table_list,
                                        clobber=clobber,
                                        namelist=n_list)
Exemplo n.º 7
0
 def write_table_file(self, table_file=None):
     """Write the table to self._table_file"""
     if self._table is None:
         raise RuntimeError("No table to write")
     if table_file is not None:
         self._table_file = table_file
     if self._table_file is None:
         raise RuntimeError("No output file specified for table")
     write_tables_to_fits(self._table_file, [self._table], clobber=True,
                          namelist=['FILE_ARCHIVE'])
Exemplo n.º 8
0
 def write_table_file(self, table_file=None):
     """Write the table to self._table_file"""
     if self._table is None:
         raise RuntimeError("No table to write")
     if table_file is not None:
         self._table_file = table_file
     if self._table_file is None:
         raise RuntimeError("No output file specified for table")
     write_tables_to_fits(self._table_file, [self._table],
                          clobber=True,
                          namelist=['FILE_ARCHIVE'])
Exemplo n.º 9
0
 def write_table_file(self, job_table_file=None, file_table_file=None):
     """Write the table to self._table_file"""
     if self._table is None:
         raise RuntimeError("No table to write")
     if self._table_ids is None:
         raise RuntimeError("No ID table to write")
     if job_table_file is not None:
         self._table_file = table_file
     if self._table_file is None:
         raise RuntimeError("No output file specified for table")           
     write_tables_to_fits(self._table_file, [self._table, self._table_ids], clobber=True,
                          namelist=['JOB_ARCHIVE', 'FILE_IDS'])
     self._file_archive.write_table_file(file_table_file)
Exemplo n.º 10
0
 def write_table_file(self, job_table_file=None, file_table_file=None):
     """Write the table to self._table_file"""
     if self._table is None:
         raise RuntimeError("No table to write")
     if self._table_ids is None:
         raise RuntimeError("No ID table to write")
     if job_table_file is not None:
         self._table_file = job_table_file
     if self._table_file is None:
         raise RuntimeError("No output file specified for table")
     write_tables_to_fits(self._table_file, [self._table, self._table_ids],
                          clobber=True,
                          namelist=['JOB_ARCHIVE', 'FILE_IDS'])
     self._file_archive.write_table_file(file_table_file)
Exemplo n.º 11
0
    def write_fits(self, filepath, clobber=False):
        """ Write this object to a FITS file

        Paramters
        ---------

        filepath : str
            Path to output file

        clobber : bool
            Flag to allow overwriting existing files

        """
        fits_utils.write_tables_to_fits(filepath, [self._e_table, self._s_table],
                                        clobber=clobber,
                                        namelist=["EBOUNDS", "SPECDATA"],
                                        cardslist=[{}, self._ref_vals])
Exemplo n.º 12
0
    def write_fits_files(stacked_dict, resultsfile, limitfile, clobber=False):
        """ Write the stacked StackCastroData object and limits a FITS files

        Parameters
        ----------

        stacked_dict : dict
            Dictionary of `StackCastroData` objects, keyed by spec

        resultsfile : str
            Path to the output file to write the `StackCastroData` objects to

        limitfile : str
            Path to write the upper limits to

        clobber : bool
            Overwrite existing files

        """
        specs = stacked_dict.keys()
        t_list = []
        n_list = []
        lim_list = []
        lim_table_list = []
        alphas = [0.68, 0.95]
        for spec in specs:
            stacked = stacked_dict[spec]
            mles = stacked.mles()
            limit_dict = dict(mles=mles)
            for alpha in alphas:
                limits = stacked.getLimits(alpha)
                limit_dict['ul_%.02f' % alpha] = limits
            tab_limits = stacked.build_limits_table(limit_dict)
            t_list.append(stacked.build_scandata_table())
            n_list.append(spec)
            lim_list.append(limit_dict)
            lim_table_list.append(tab_limits)

        fits_utils.write_tables_to_fits(resultsfile,
                                        t_list,
                                        clobber=clobber,
                                        namelist=n_list)
        fits_utils.write_tables_to_fits(limitfile,
                                        lim_table_list,
                                        clobber=clobber,
                                        namelist=n_list)
Exemplo n.º 13
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        limitfile = args.limitfile
        first = args.seed
        last = first + args.nsims
        flist = [limitfile.replace("_SEED.fits", "_%06i.fits" % seed)\
                     for seed in range(first, last)]

        spec_config = load_yaml(args.specconfig)
        channels = spec_config['channels']
        sum_chans = CollectLimits.select_channels(channels, limitfile)

        outfile = args.outfile
        summaryfile = args.summaryfile

        hdus = sum_chans + ['MASSES']

        out_tables, out_names = vstack_tables(flist, hdus)

        if is_not_null(outfile):
            fits_utils.write_tables_to_fits(outfile,
                                            out_tables,
                                            namelist=out_names)

        if is_not_null(summaryfile):
            summary_tables = []
            for ot, chan in zip(out_tables[0:-1], sum_chans):
                decay = chan.find('_decay') >= 0
                summary_table = summarize_limits_results(ot, decay)
                summary_tables.append(summary_table)
            summary_tables.append(Table(out_tables[-1][0]))
            fits_utils.write_tables_to_fits(summaryfile,
                                            summary_tables,
                                            namelist=out_names)
Exemplo n.º 14
0
def main():
    """ Hook for command line access """
    # Argument defintion
    usage = "usage: %(prog)s [input]"
    description = "Collect all the new source"

    parser = argparse.ArgumentParser(usage=usage, description=description)
    parser.add_argument('--input',
                        '-i',
                        default='roi_set.yaml',
                        help='ROI set definition file.')
    parser.add_argument('--output',
                        '-o',
                        type=argparse.FileType('w'),
                        help='Output file.')
    parser.add_argument('--clobber',
                        action='store_true',
                        help='Overwrite output file.')
    parser.add_argument('--targets', '-t', type=str, help='Target file.')
    parser.add_argument('--filestr',
                        '-f',
                        default="tscube.fits",
                        help="Name of file within each ROI sub-directory")

    # Argument parsing
    args = parser.parse_args()

    # Read the target file
    targ_type = os.path.splitext(args.targets)[1]
    print targ_type
    if targ_type in ['.fit', '.fits']:
        targets = DMTargetFactory.read_table(args.targets)
        roster = None
    else:
        targets, roster = DMTargetFactory.make_table([args.targets])

    # Get the sky_crds
    sky_crds = DMTargetFactory.get_sky_crds(targets)

    # Make the roi_set object
    roi_set, basedir = dmp_roi.DMROISet.create_from_yaml(args.input)

    # extract the data
    out_table = roi_set.extract_table_data(sky_crds,
                                           args.filestr,
                                           basedir=basedir,
                                           tables=["SCANDATA", "FITDATA"])

    # add_names_column(out_table,targets['name'])
    col_names = [
        'name', 'ra', 'dec', 'distance', 'proftype', 'glat', 'glon', 'j_integ',
        'd_integ'
    ]
    add_columns(out_table, targets, col_names)

    ebounds_table = roi_set.extract_single_table(args.filestr,
                                                 basedir=basedir,
                                                 table="EBOUNDS")

    # Write the output
    fits_utils.write_tables_to_fits(args.output, [out_table, ebounds_table],
                                    clobber=args.clobber,
                                    namelist=["SCANDATA", "EBOUNDS"])
Exemplo n.º 15
0
    def convert_sed(spec_table, channels, sed_file, outfile, limitfile,
                    **kwargs):
        """Convert a single SED to DM space.

        Parameters
        ----------

        spec_table : `DMSpecTable`
            Object with all the DM spectra

        channels : list
            List of the channels to convert

        sed_file : str
            Path to the SED file

        outfile : str
            Path to write the output `DMCastroData` object to

        limitfile : str
            Path to write the output limits to.


        Keyword arguments
        -----------------

        norm_type : str
            Normalization type to use

        j_factor : dict
            Dictionary with information about the J-factor

        d_factor : dict
            Dictionary with information about the J-factor

        clobber : bool
            Flag to overwrite existing files.



        """
        norm_type = kwargs.get('norm_type', 'eflux')
        j_factor = kwargs.get('j_factor', None)
        d_factor = kwargs.get('d_factor', None)
        clobber = kwargs.get('clobber', False)

        use_chans = ConvertCastro.select_channels(channels, sed_file)

        exttype = os.path.splitext(sed_file)[-1]
        if exttype in ['.fits', '.npy']:
            sed = CastroData.create_from_sedfile(sed_file, norm_type)
        elif exttype in ['.yaml']:
            sed = CastroData.create_from_yamlfile(sed_file)
        else:
            raise ValueError("Can not read file type %s for SED" % exttype)

        c_list, t_list, n_list = ConvertCastro.convert_sed_to_dm(
            spec_table,
            sed,
            use_chans,
            norm_type=norm_type,
            j_val=j_factor,
            d_val=d_factor)

        if is_not_null(outfile):
            fits_utils.write_tables_to_fits(outfile,
                                            t_list,
                                            clobber=clobber,
                                            namelist=n_list)

        if is_not_null(limitfile):
            mass_table = t_list[-1]
            _, t_list_lim, n_list_lim = ConvertCastro.extract_dm_limits(
                c_list, use_chans, [0.68, 0.95], mass_table)
            fits_utils.write_tables_to_fits(limitfile,
                                            t_list_lim,
                                            clobber=clobber,
                                            namelist=n_list_lim)
Exemplo n.º 16
0
    def convert_sed(spec_table, specs, sed_file, outfile, limitfile, **kwargs):
        """Convert a single SED to stacking space.

        Parameters
        ----------

        spec_table : `StackSpecTable`
            Object with all the stacking spectra

        specs : list
            List of the specs to convert

        sed_file : str
            Path to the SED file

        outfile : str
            Path to write the output `StackCastroData` object to

        limitfile : str
            Path to write the output limits to.


        Keyword arguments
        -----------------

        norm_type : str
            Normalization type to use

        astro_val : dict
            Dictionary with information about the astrophysical norm

        clobber : bool
            Flag to overwrite existing files.

        """
        norm_type = kwargs.get('norm_type', 'eflux')
        astro_val = kwargs.get('astro_val', None)
        clobber = kwargs.get('clobber', False)

        use_specs = specs

        exttype = os.path.splitext(sed_file)[-1]
        if exttype in ['.fits', '.npy']:
            sed = CastroData.create_from_sedfile(sed_file, norm_type)
        elif exttype in ['.yaml']:
            sed = CastroData.create_from_yamlfile(sed_file)
        else:
            raise ValueError("Can not read file type %s for SED" % exttype)

        c_list, t_list, n_list = ConvertCastro.convert_sed_to_stack_var(
            spec_table, sed, norm_type=norm_type, astro_val=astro_val)

        if is_not_null(outfile):
            fits_utils.write_tables_to_fits(outfile,
                                            t_list,
                                            clobber=clobber,
                                            namelist=n_list)

        if is_not_null(limitfile):
            stacked_lists = ConvertCastro.extract_stack_limits(
                c_list, use_specs, [0.68, 0.95])
            fits_utils.write_tables_to_fits(limitfile,
                                            stacked_lists[1],
                                            clobber=clobber,
                                            namelist=stacked_lists[2])