B
    Qa/6                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZ dZdd ZdddZdd	 Zd
d ZdddZedkreejdd  dS )    N)readfile	writefileutilsa[  example:
  generate_mask.py  temporalCoherence.h5 -m 0.7 -o maskTempCoh.h5
  generate_mask.py  temporalCoherence.h5 -m 0.7 -o maskTempCoh.h5 --base inputs/geometryRadar.h5 --base-dset shadow --base-value 1
  generate_mask.py  avgSpatialCoh.h5     -m 0.7 --base waterMask.h5 -o maskSpatialCoh.h5

  # exlcude area by min/max value and/or subset in row/col direction
  generate_mask.py  081018_090118.unw -m 3 -M 8 -y 100 700 -x 200 800 -o mask_1.h5

  # exlcude pixel cluster based on minimum number of pixels
  generate_mask.py  maskTempCoh.h5 -p 10 mask_1.h5

  # exclude pixels with large velocity STD: |velocity| > cutoff (2 by default) * velocityStd
  generate_mask.py  velocity.h5 --vstd
  generate_mask.py  velocity.h5 --vstd --vstd-num 3

  # exclude / include an circular area
  generate_mask.py  maskTempCoh.h5 -m 0.5 --ex-circle 230 283 100 -o maskTempCoh_nonDef.h5
  generate_mask.py  maskTempCoh.h5 -m 0.5 --in-circle 230 283 100 -o maskTempCoh_Def.h5
  # maskout an area within a circle AND with height smaller than a threshold
  generate_mask.py  inputs/geometryGeo.h5 height --in-circle 339 370 21 -M 1400 --revert -o maskCrater.h5

  # use an specific dataset from multiple dataset file
  generate_mask.py  geometryRadar.dem height -m 0.5 -o waterMask.h5
  generate_mask.py  ifgramStack.h5 unwrapPhase-20101120_20110220 -m 4

  # common mask file of pixels in all connected components / with non-zero unwrapped phase
  generate_mask.py  ifgramStack.h5  --nonzero  -o maskConnComp.h5  --update

  # interative polygon selection of region of interest
  # useful for custom mask generation in unwrap error correction with bridging
  generate_mask.py  waterMask.h5 -m 0.5 --roipoly
  generate_mask.py  azOff.h5 --roipoly --view-cmd "-v -0.1 0.1"
  generate_mask.py  velocity.h5 --roipoly --view-cmd "--dem ./inputs/geometryGeo.h5 --contour-step 100 --contour-smooth 0.0"
c              C   s  t jdt jtd} | jddd | jdddd	 | jd
dddd | jddddd | jdddd | jdddtdd | jdddtdd | jd d!d"d#td$d | jd%dd&d | jd'd(td)d*d+ | d,d-}|jd.d/td)d0d1d2 |jd3d4td)d5d6d2 |jd7d8d9td:d;d< |jd=d>d9td:d?d< |jd@dAtdBd |jdCdDdEtdFd |jdGdHtdIdJd+ |jdKddLd |jdMdNtdOd | jdPdQddRd | jdSdTddUd | S )VNz"Generate mask file from input file)descriptionformatter_classepilogfilez
input file)helpdset?z?date of timeseries, or date12 of interferograms to be converted)nargsr	   z-oz--outputoutfilezoutput file name.)destr	   z
--keep-nankeep_nan
store_truez$Do not exclude pixels with NaN value)r   actionr	   z--revertz(revert 0 and 1 value of output mask file)r   r	   z-mz--minvminz!minimum value for selected pixels)r   typer	   z-Mz--maxvmaxz!maximum value for selected pixelsz-pz--mpz--minpixels	minpixelsz?minimum cluster size in pixels, to remove small pixel clusters.z--vstdz;mask according to the formula: |velocity| > a * velocityStdz
--vstd-numvstd_num   z-multiple of velocityStd (a) to use for cutoff)r   r   defaultr	   ZAOIz!define secondary area of interestz-xsubset_x)XMINXMAXz0selection range in x/cross-track/range direction)r   r   r   metavarr	   z-ysubset_y)YMINYMAXz2selection range in y/along-track/azimuth directionz--ex-circle	ex_circle   )XYZRADIUSz@exclude area defined by an circle (x, y, radius) in pixel number)r   r   r   r   r	   z--in-circle	in_circlez@include area defined by an circle (x, y, radius) in pixel numberz--base	base_filezEexclude pixels == base_value
output_mask[base_data == base_value] = 0z--base-dsetz--base-datasetbase_datasetzdataset in base_file to be used, for file with multiple datasets.
i.e.: --base inputs/geometryRadar.h5 --base-dset shadow --base-value 1z--base-value
base_valuer   z7value of pixels in base_file to be excluded.
Default: 0z	--roipolyz9Interactive polygonal region of interest (ROI) selection.z
--view-cmdview_cmdzBview.py command to facilitate the AOI selection.E.g. "-v -0.1 0.1"z	--nonzerononzerozDSelect all non-zero pixels.
i.e. maskConnComp.h5 from ifgramStack.h5z--updateupdate_modez,Enable update checking for --nonzero option.)	argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentfloatintadd_argument_groupstr)parserZaoi r5   J/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/generate_mask.pycreate_parser8   sZ    





r7   c             C   st   t  }|j| d}|jrp|jrpt|j}|j|krpd|j|j}|d|7 }t| t	d d |_d |_|S )N)argsz*dataset {} NOT found in input base file {}z
available datasets:
{}z-ignore --base --base-dset option and continue)
r7   
parse_argsr%   r&   r   get_dataset_listformatwarningswarnprint)iargsr4   inpsZbase_dataset_listmsgr5   r5   r6   cmd_line_parsey   s    

rB   c          	   C   s   t d t d d}tj| js8d}t d| j nt d| j t| jd(}t	|| j
 jdtj| j}W d Q R X tj| j}||krd}t d	| j
 nt d
| j
 t d| |S )Nz2--------------------------------------------------zupdate mode: ONskiprunz1) output file {} NOT exist.z!1) output file {} already exists.rMODIFICATION_TIMEz32) output file is NOT newer than input dataset: {}.z/2) output file is newer than input dataset: {}.zrun or skip: {}.)r>   ospathisfiler   r;   h5pyFiler   r0   r
   attrsgetgetmtime)r@   flagftitor5   r5   r6   run_or_skip   s     ,rS   c             C   sR  | j rtd| j| j f  ntd| j  tj| j| j d\}}t|jdkrVtdt|d t|d  }}t	
| }td t	j||ft	jd	}| js||9 }td
 | jr||  || dk 9  < td | jd k	r||  || | jk  9  < tdt| j  | jd k	rJ||  || | jk 9  < tdt| j  | jd k	rddlm} t	|}||| jdd}td| j|t	| f  | jr|d dkrtdtj| jddd }	||  t	|| | j|	|  k9  < td| j | jd k	rTt| j\}
}d|d|
d d f< d|||d d f< td|
|f  | jd k	rt| j\}}d|d d d|f< d|d d ||f< td||f  | jr| j\}}}t |||||f}d||dk< td||| | j!r2| j!\}}}t |||||f}d||dk< td||| | j"rnddl#m$} |j%| j| j | j&d}|d k	rn||9 }| j'r tj| j'| j(dd }t|jdkrt	j|dd }d||t)| j*k< d!t+j,-| j'}| j(r|d"| j(7 }|d#| j*7 }t| | j.r2t	j/|t	jd	}d$||d%k< d%||d$k< ~d&|d< t0j1|| j2|d' | j2S )(Nz
read %s %szread %s)datasetNamer   z>Only 2D dataset is supported for threshold method, input is 3DLENGTHWIDTHzDcreate initial mask with the same size as the input file and all = 1)dtypezall pixels with nan value = 0g        zexclude pixels with zero valuezexclude pixels with value < %szexclude pixels with value > %sr   )remove_small_objects   )connectivityz>exclude pixel clusters with size < %d pixels: remove %d pixels	FILE_TYPEvelocityz@Input file MUST be a velocity file when using the --vstd option!ZvelocityStdzFexclude pixels according to the formula: |velocity| > {} * velocityStdz%exclude pixels with y OUT of [%d, %d]z%exclude pixels with x OUT of [%d, %d]z=exclude pixels inside of circle defined as (x={}, y={}, r={})z>exclude pixels outside of circle defined as (x={}, y={}, r={}))plot_ext)rT   r(   r!   )axiszexclude pixels in base file {} zdataset {} zwith value == {}FTmask)out_filemetadata)3r
   r>   r   r   readlenshape	Exceptionr1   npisnanonesbool_r   r)   r   r3   r   r   Zskimage.morphologyrX   sumZvstd
ValueErrorabsr   r;   r   sortedr   r    utget_circular_maskr$   roipolymintpy.utilsr]   Zget_poly_maskr(   r%   r&   r0   r'   rG   rH   basenameZrevertarrayr   writer   )r@   dataatrlengthwidthnanmaskr_   rX   	num_pixelZdata_stdy0y1x0x1xyrE   cmaskr]   Z	poly_maskZ	base_datarA   tempr5   r5   r6   create_threshold_mask   s    
(
r   c          	      sl  t   }t| }t|j}|d }td||j |js|jrJd|_n,d|jkrp|j	dd }d| |_nd|_|j
drd|j |_|jd kr|jd	rd
|_|jr6|dkr6|jst|jd  fdddD d |_W d Q R X |jr|jrt|dkr|jS tj|j|j|jd|_|jS t||_tt   | d\}}td|| |jS )Nr[   zinput {} file: {}zmaskPoly.h5temporalCoherencerY   ZmaskTempCohzmask.h5Zgeo_ztemporalCoherence.h5gffffff?ifgramStackrE   c                s   g | ]}|   kr|qS r5   )keys).0i)rP   r5   r6   
<listcomp>3  s    zmain.<locals>.<listcomp>)connectComponentunwrapPhaser   rC   )r`   rT   <   z'time used: {:02.0f} mins {:02.1f} secs.)timerB   r   read_attributer   r>   r;   r   rp   split
startswithr   endswithr)   r
   rJ   rK   r*   rS   rn   nonzero_maskr   divmod)r?   
start_timer@   rv   ksuffixmsr5   )rP   r6   main  s8    
"
r   __main__rY   )N)N)rG   sysr   r<   r+   rJ   numpyrf   rq   r   r   r   rn   r.   r7   rB   rS   r   r   __name__argvr5   r5   r5   r6   <module>	   s   %A
r
/