示例#1
0
def savedict(filename, results, yaml_kwargs=dict()):
    """ Save a dictionary to a file. Choose file format based
        upon extension to filename. """
    is_results = lambda x: isinstance(x,dict) or isinstance(x,list)

    if isinstance(filename,str) and is_results(results):
        pass
    elif is_results(filename) and isinstance(results, str):
        filename, results = results, filename
    else:
        raise Exception("Unrecoginized types for filename and results")

    filename = expandvars(filename)

    extension = os.path.splitext(filename)[-1]

    if extension == '.yaml':
        open(filename, 'w').write(yaml.dump(tolist(results), **yaml_kwargs))
    elif extension == '.hdf5':
        if not isinstance(results, dict): raise Exception("Can only save dicts to hdf5 format.")
        import h5py
        f=h5py.File(filename,'w')
        for k,v in results.items(): f[k] = v
        f.close()
    elif extension == '.fits':
        if not isinstance(results, dict): raise Exception("Can only save dicts to fits format.")
        rec = fromarrays(results.values(), names=results.keys())
        makefits(rec, filename, clobber=True)
    elif extension == '.xml':
        from pyxml2obj import XMLout
        open(filename, 'w').write(XMLout(results))
    else:
        raise Exception("Unrecognized extension %s" % extension)
示例#2
0
    def write_map(self, name, fun=lambda x:x, title=None, vmax=None, table=None):
        """ DISABLED for now 
        write out image files for a single table
        name : string 
            name of the table, to be loaded by roi_maps.load_tables if table is None 
        fun  : ufunc function
            ufunc to apply to each pixel when generating the image files
        title : string
            title to apply to plots
        table : None, or a HEALpix array
            """
        if table is None:
            table = healpix_map.load_tables(name, outdir=self.outdir)
        nside = int(np.sqrt(len(table)/12))
        assert len(table)==12*nside**2, 'table length, %d, not HEALpix compatible' % len(table)

        filename = '%s_ait.fits' % name
        fitsfile = os.path.join(self.pivot_dir,filename)
        if os.path.exists(fitsfile):
            if self.overwrite:  os.remove(fitsfile)
            else: fitsfile = ''

        outfile=os.path.join(self.pivot_dir,'%s_ait.png'%name)
        if fitsfile != '':
            # first without the usual scaling function for the FITS version
            print 'generating FITS image %s' %fitsfile
            q=display_map.skyplot(table, title,
                ait_kw=dict(fitsfile=fitsfile,pixelsize=0.1))
            del q
        if not os.path.exists(outfile) or self.overwrite:
            print 'generating %s and its thumbnail...' % outfile
            # now with scaling function to generate png
            plt.close(30)
            fig = plt.figure(30, figsize=(16,8))
            q=display_map.skyplot(fun(table), title, axes=fig.gca(),
                ait_kw=dict(fitsfile='', pixelsize=0.1), vmax=vmax)
            plt.savefig(outfile, bbox_inches='tight', pad_inches=0)
            im = PIL.Image.open(outfile)
            im.thumbnail((200,100))
            im.save(outfile.replace('.png', '_thumbnail.png'))
            del q
     
        outfile = os.path.join(self.pivot_dir,'%s_map.fits' % name)
        if os.path.exists(outfile):
            if self.overwrite: os.remove(outfile)
            else: return
        print 'generating %s' % outfile
        dirs = map(Band(nside).dir, xrange(len(table)))
        ra  = np.array(map(lambda s: s.ra(), dirs), np.float32)
        dec = np.array(map(lambda s: s.dec(),dirs), np.float32)
        outrec = np.rec.fromarrays([ra,dec,np.array(table,np.float32)], 
            names = ['ra','dec', 'value'])
        makerec.makefits(outrec, outfile)
示例#3
0
 def write_FITS(self, TSmin=None):
     """ write out the Catalog format FITS version, and the rois """
     catname = os.path.join(self.pivot_dir,self.name+'.fits') 
     if os.path.exists(catname):
         if self.overwrite: os.remove(catname)
         else: 
             return
     cat = makecat.MakeCat(self.sources, TScut=0 if TSmin is None else TSmin)
     cat(catname)
     for rec, rectype in ((self.sources, 'sources'), (self.rois,'rois')):
         outfile = os.path.join(self.pivot_dir,self.name+'_%s.fits'%rectype)
         if os.path.exists(outfile):
             if self.overwrite: os.remove(outfile)
             else:
                 print 'file %s exists: set overwrite to replace it' % catname
                 continue
         makerec.makefits(rec, outfile)
         print 'wrote %s' %outfile
示例#4
0
    
    print '%s - %s/%s' % (results, i, len(all_results))

    r = yaml.load(open(results))

    if r is None: continue


    for i in r:

        e=i['mc']['extension']
        f=i['mc']['flux']['flux']
        index=i['mc']['model']['Index']
        e_ul=i['extension_ul']['extension']

        #if e_ul is None: e_ul = 0
        if e_ul is None: 
            e_ul = np.nan

        ts=max(i['point']['TS'],0)
        ts_ext=max(i['TS_ext'],0)
        type=i['type']

        rec.append(f,index,e,e_ul,ts,ts_ext,type)

rec = rec()

file=join(datadir,'cached.fits')
if exists(file): remove(file)
makefits(rec,file)