U
     xb                     @   s*   d dl Z d dlZd dlZG dd dZdS )    Nc                   @   s>   e Zd ZdZdddZd	d
 ZdddZdddZdd ZdS )progressBara  Creates a text-based progress bar. Call the object with
    the simple print command to see the progress bar, which looks
    something like this:
    [=======> 22%       ]
    You may specify the progress bar's min and max values on init.

    Code modified from PyAPS release 1.0 (http://earthdef.caltech.edu/projects/pyaps/wiki/Main)
    Code originally from http://code.activestate.com/recipes/168639/

    example:
        from mintpy.objects import timeseries
        from mintpy.utils import ptime
        date_list = timeseries('timeseries.h5').get_date_list()
        num_date = len(date_list)
        prog_bar = ptime.progressBar(maxValue=num_date)
        for i in range(num_date):
            do_work()
            prog_bar.update(i+1, suffix=date_list[i])
        prog_bar.close()
    d    r   F   Tc                 C   s@   d| _ || _|| _|| | _d| _|| _|| _|| _|   d S )Nz[]r   )	prog_barminmaxspansuffixprefix	print_msgwidthreset)selfmaxValuer   ZminValueZ
totalWidthr    r   N/home/exouser/operations/rsmas_insar/sources/MintPy/mintpy/objects/progress.py__init__'   s    
zprogressBar.__init__c                 C   s   t   | _d| _| d d S )Nr   )time
start_timeamountupdate_amountr   r   r   r   r   6   s    
zprogressBar.resetc                 C   s  t || j}t|| j }|| _|r*d| }t| j| j }|t| j d }tt|}| jd d }|d | }tt|}|dkrd	| j
d|d  | _n||krd	| j
d	| | _|  j|7  _nd
	d	|d  d||  | _tt| jd tt| }d	|}d| jd| || j|t| d g| _| j
| j | | _|dkrt | j }	t|	d| d  }
|  jd	t|	t|
7  _dS )z Update the progress bar with the new amount (with min and max
        values set at initialization; if it is over or under, it takes the
        min or max value as a default.  g      Y@      r   z{}[>{}]   z{}[{}]=z[{}>{}]z {}% r   Nz{:5d}s / {:5d}s)r   r   r   npfloatr	   introundr   formatr   r   lenstrjoinr   r   )r   	newAmountr
   ZdiffFromMinZpercentDoneZallFullZ	numHashesZpercentPlaceZpercentStringZelapse_timeZremain_timer   r   r   r   ;   s8    

zprogressBar.update_amountr   c                 C   sJ   || dks|| j krF| j||d | jrFtjd| j  tj  dS )z Updates the amount, and writes to stdout. Prints a
         carriage return first, so it will overwrite the current
          line in stdout.r   )r&   r
   N)r   r   r   sysstdoutwriter   flush)r   valueZeveryr
   r   r   r   updaten   s
    zprogressBar.updatec                 C   s   | j rtd dS )zWPrints a blank space at the end to ensure proper printing
        of future statements.r   N)r   printr   r   r   r   closex   s    zprogressBar.closeN)r   r   r   r   T)r   r   )r   r   )	__name__
__module____qualname____doc__r   r   r   r-   r/   r   r   r   r   r      s   

3

r   )r(   r   numpyr   r   r   r   r   r   <module>   s   