Exemplo n.º 1
0
def write(hdu, filename, quiet=False, clobber=True, shortdir=None):
    '''Write a nice fits file.  Filenames with .gz will be compressed using the system
    gzip call.  hdu can be either a column table list or a pyfits.bintable object.
    it notifies the user what was written.
    
    clobber=[True] -- Overwrite files by default
    quiet=[False]  -- Do not announce that we are writing a file.  Why would you not?
    shortdir=['/path/','<Dir>'] -- Shorten the filename that is printed out to the screen
    '''        
    
    # Handle column lists and hdus
    if isinstance(hdu, pyfits.hdu.table.BinTableHDU):
        hdus = hdu
    elif (isinstance(hdu, list) or isinstance(hdu, tuple)) and (isinstance(hdu[0], pyfits.Column)):
        hdus = pyfits.new_table(pyfits.ColDefs(hdu))
    elif isinstance(hdu, Cat):
        hdus = pyfits.new_table(pyfits.ColDefs(hdu.columns))
    else:
        hdus = pyfits.HDUList(hdu)
    
    # Save and compress if needed
    outname = filename.replace('.gz','')
    hdus.writeto(outname, clobber=clobber)
    if '.gz' in filename:
        subprocess.call(['gzip', '-f', outname])
    
    # You should generally tell the user what was saved, but hey you can
    # play with matches if you want.
    if not quiet:
        # print "Saved to: %s"%(filename)
        if shortdir is not None:
            splog('Saved to:', filename.replace(shortdir[0], shortdir[1]))
        else:
            splog('Saved to:', filename)
Exemplo n.º 2
0
 def save(self, clear=True, quiet=False):
     '''Save the current figure as a new page, and ready
     the figure for the next page'''
     self.pdf.savefig()
     if clear:
         pylab.clf()
     if not quiet:
         util.splog('Added Page:', self.filename)
Exemplo n.º 3
0
 def save(self, clear=True, quiet=False):
     '''Save the current figure as a new page, and ready
     the figure for the next page'''
     self.pdf.savefig()
     if clear:
         pylab.clf()
     if not quiet:
         util.splog('Added Page:',self.filename)
Exemplo n.º 4
0
    def wrapper(*args, **kwargs):
        # get a nice list of arguments -- defaults to nothing if none
        name = fcn.func_name
        tmp = ', '.join(map(str, args))
        if len(tmp) > 0: tmp = '[{}]'.format(tmp)
        # Arguments for splog making things look all nice.
        kw = {'color': 'green', 'name': name, 'stack': 2}

        start = util.deltatime()
        util.splog('Starting:', name, tmp, **kw)
        results = fcn(*args, **kwargs)
        util.splog('Finished running:', tmp, util.deltatime(start), **kw)

        return results
Exemplo n.º 5
0
 def wrapper(*args, **kwargs):
     # get a nice list of arguments -- defaults to nothing if none
     name = fcn.func_name
     tmp = ', '.join(map(str, args))
     if len(tmp) > 0: tmp = '[{}]'.format(tmp)
     # Arguments for splog making things look all nice.
     kw = {'color':'green', 'name':name, 'stack':2}
     
     start = util.deltatime()
     util.splog('Starting:', name, tmp, **kw)
     results = fcn(*args, **kwargs)
     util.splog('Finished running:',tmp, util.deltatime(start), **kw)
     
     return results 
Exemplo n.º 6
0
def multi_process(fcn, args, n=None):
    '''Multiprocess a function with an iterable list of arguments (args).
    Set n>= int for the number of processes to use.   This might blow up, use 
    caution and get coffee.
    '''
    util.SPLOG['multi']=True
    
    # Attempt to all of the CPU except for one.  The documentation suggests     
    # that this might not return a value so default to two processes.
    if n is None:
        try:
            n = multiprocessing.cpu_count()-1
        except:
            n = 2
    util.splog('Starting run with %d processes'%(n), color='green')
    
    
    # get the arguments
    tmp = getargs(fcn,args)
    # only spin up enough processes as needed
    if len(tmp) < n:
        n = len(tmp)+1
    
    start = util.deltatime()
    pool = multiprocessing.Pool(n)
    # p = pool.map_async(do_work, [[fcn]+list(x) for x in args])
    
    # print tmp
    results = None
    p = pool.map_async(do_work, tmp)
    try:
        # This is the timeout to wait for the results.  We default to something
        # really really large.
        results = p.get(0xFFFFFFF)
    except KeyboardInterrupt:
        # This captures the error raised by do_work and raises a different error
        # so that the computation finally ends and we do not attempt to run
        # other problems.
        print 'parent received control-c'
        raise RuntimeError
    finally:
        # Shut down the pool of processes if we are done or failed at our task.
        pool.terminate()
        util.SPLOG['multi'] = False
        
    # look how nice this is
    util.splog("Finished!: %s"%(fcn.__name__), util.deltatime(start),color='green')
    return results
Exemplo n.º 7
0
 def run(self):
     print "Starting " + self.name
     
     while not exitFlag:
         queueLock.acquire()
         if not workQueue.empty():
             args = workQueue.get()
             # args = self.queue.get()
             queueLock.release()
             
             start = util.deltatime()
             util.splog("Starting: %s"%(fcn.__name__), 
                             'with', repr(args), color='green')
             self.fcn(*args)
             util.splog("Finished!: %s"%(fcn.__name__), 
                             util.deltatime(start),color='green')
         else:
             queueLock.release()
         time.sleep(0.1)
     print "Exiting " + self.name
Exemplo n.º 8
0
def saveplot(filename, clear=True, ext=None, nice=False, quiet=False):
    '''Save the current figure to a specific filename.  If the
    filename is not an absolute path it uses the $PYSURVEY_FIGURE
    directory'''
    if nice:
        filename = nicefile(filename)

    if ext is None:
        tmp = os.path.splitext(filename)
        if len(tmp[1]) == 0:
            ext = '.png'
        else:
            filename, ext = tmp

    if not os.path.isabs(filename):
        filename = os.path.join(OUTDIR, filename)
    filename += '.' + ext.replace('.', '')

    pylab.savefig(filename, dpi=150)
    if not quiet:
        util.splog('Saved Figure:', filename)
    if clear:
        pylab.clf()
Exemplo n.º 9
0
def saveplot(filename, clear=True, ext=None, nice=False, quiet=False):
    '''Save the current figure to a specific filename.  If the
    filename is not an absolute path it uses the $PYSURVEY_FIGURE
    directory'''
    if nice:
        filename = nicefile(filename)
    
    if ext is None:
        tmp = os.path.splitext(filename)
        if len(tmp[1]) == 0:
            ext = '.png'
        else:
            filename, ext = tmp

    if not os.path.isabs(filename):
        filename = os.path.join(OUTDIR, filename)
    filename += '.'+ext.replace('.','')
    
    
    pylab.savefig(filename, dpi=150)
    if not quiet:
        util.splog('Saved Figure:', filename)
    if clear:
        pylab.clf()
Exemplo n.º 10
0
 def close(self):
     '''Close out the pdf and any additional final items'''
     self.pdf.close()
     util.splog('Finished Figure:', self.filename)
Exemplo n.º 11
0
def _test(a,b=None):
    # time.sleep(1)
    # if a == 5:
    #     raise ValueError
    util.splog('test',a,b)
    time.sleep(10.1)
Exemplo n.º 12
0
 def close(self):
     '''Close out the pdf and any additional final items'''
     self.pdf.close()
     util.splog('Finished Figure:',self.filename)