U
     c7;                     @   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mZm	Z	m
Z d dlmZ dZd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utils)create_argument_parsera[  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

  # exclude 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

  # exclude 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  d}t }tdd }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 |d'd(}|jd)d*d+td,d-d. |jd/d0d+td1d2d. |jd3dd4d |jd5dd6d |jd7d8td+d9d: |d;d<}|jd=d>d?td+d,d@dA |jdBdCdDtd+d1dEdA |jdFdGdHtdIdJd. |jdKdLdHtdIdMd. |jdNdOtdPd |jdQdRdStdTd |jdUdVtdWdXd: |jdYddZd |jd[d\td]d |jd^d_dd`d |jdadbddcd |S )dNz"Generate mask file from input file.)synopsisdescriptionepilog
subparsers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AOI for thresholdz+Define the AOI for thresholding operations.z--vx
v_subset_x   )XMINXMAXzEAOI range in X for threshold operation (and keep the rest untouched.))r   r   r   metavarr   z--vy
v_subset_y)YMINYMAXzEAOI range in Y for threshold operation (and keep the rest untouched.)z
--vroipolyzAAOI via interactive polygonal region of interest (ROI) selection.z--vstdz;mask according to the formula: |velocity| > a * velocityStdz
--vstd-numvstd_numz-multiple of velocityStd (a) to use for cutoff)r   r   defaultr   ZAOIz!define secondary area of interestz-xz--sub-xsubset_xz0selection range in x/cross-track/range direction)r   r   r   r   r   z-yz--sub-ysubset_yz2selection 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 numberz--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.)	EXAMPLE__name__splitr   add_argumentfloatintadd_argument_groupstr)r   r   r
   nameparservmaskaoi r=   K/home/exouser/operations/rsmas_insar/sources/MintPy/mintpy/generate_mask.pycreate_parser8   s        





r?   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)
r?   
parse_argsr+   r,   r   get_dataset_listformatwarningswarnprint)iargsr:   inpsZbase_dataset_listmsgr=   r=   r>   cmd_line_parse   s    

rJ   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 5 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: {}.)rF   ospathisfiler   rC   h5pyFiler   r5   r   attrsgetgetmtime)rH   flagftitor=   r=   r>   run_or_skip   s     ,r[   c                 C   s  | 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	j||ft	jd}| jrt| j\}}d	|d d d |f< d	|d d |d f< | jr
t| j\}	}
d	|d |	d d f< d	||
d d d f< | jrJd	d
lm} |j| j| j | jd}|d k	rJd	||d	k< td t	j||ft	jd}| js~||9 }td | jrd	||dk< td | jd k	r||  || | jk  9  < tdt| j  | jd k	r||  || | jk 9  < tdt| j  | jd k	r\d	dlm} t	|}||| jdd}td| j|t	| f  | jr|d dkrztdtj| jddd	 }||  t	 || | j!||  k9  < td"| j! | j#d k	rt| j#\}}d	|d	|d d f< d	|||d d f< td||f  | j$d k	rjt| j$\}}d	|d d d	|f< d	|d d ||f< td||f  | j%r| j%\}}}t&'|||||f}d	||dk< td"||| | j(r| j(\}}}t&'|||||f}d	||d	k< td"||| | j)r2d	d
lm} |j| j| j | jd}|d k	r2||9 }| j*rtj| j*| j+dd	 }t|jdkrnt	j|d	d }d	||t,| j-k< d!"t.j/0| j*}| j+r|d""| j+7 }|d#"| j-7 }t| | j1rt	j2|t	jd}d$||d%k< d%||d$k< ~d&|d< t3j4|| j5|d' | j5S )(Nz
read %s %szread %s)datasetNamer   z>Only 2D dataset is supported for threshold method, input is 3DLENGTHWIDTH)dtyper   )plot_ext)r\   r.   zDcreate initial mask with the same size as the input file and all = 1zall pixels with nan value = 0g        zexclude pixels with zero valuezexclude pixels with value < %szexclude pixels with value > %s)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={})r'   )axiszexclude pixels in base file {} zdataset {} zwith value == {}FTmask)out_filemetadata)6r   rF   r   r   readlenshape	Exceptionr6   npisnanonesbool_r   sortedr   Zvroipolymintpy.utilsr`   Zget_poly_maskr.   r   r/   r   r8   r   r   Zskimage.morphologyra   sumZvstd
ValueErrorabsr"   rC   r%   r$   r&   utget_circular_maskr*   roipolyr+   r,   r5   r-   rO   rP   basenamerevertarrayr   writer   )rH   dataatrlengthwidthnanmaskr;   Zvx0Zvx1Zvy0Zvy1r`   Z	poly_maskrg   ra   	num_pixelZdata_stdy0y1x0x1xyrM   cmaskZ	base_datarI   tempr=   r=   r>   create_threshold_mask   s    

(
r   c              	      sh  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 5 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|| d S )Nrd   zinput {} file: {}zmaskPoly.h5temporalCoherencerb   ZmaskTempCohzmask.h5Zgeo_ztemporalCoherence.h5gffffff?ifgramStackrM   c                    s   g | ]}|   kr|qS r=   )keys).0irX   r=   r>   
<listcomp>N  s      zmain.<locals>.<listcomp>)connectComponentunwrapPhaser   rK   )rh   r\   <   z'time used: {:02.0f} mins {:02.1f} secs.)timerJ   r   read_attributer   rF   rC   r   ry   r3   
startswithr   endswithr/   r   rR   rS   r0   r[   rw   nonzero_maskr   divmod)rG   
start_timerH   r   ksuffixmsr=   r   r>   main2  s8    
"r   __main__rb   )N)N)N)rO   sysr   rD   rR   numpyrn   rs   r   r   r   rw   mintpy.utils.arg_utilsr   r1   r?   rJ   r[   r   r   r2   argvr=   r=   r=   r>   <module>	   s    $
L
 
0