B
    Qa"                 @   s   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 dZ	dd Z
dddZejfdd	ZdddZdddZdddZdddZedkreejdd  dS )    N)readfile	writefileao  example:
  mask.py  velocity.h5     -m Mask.h5
  mask.py  timeseries.h5   -m temporalCoherence.h5  -t 0.7
  mask.py  ifgramStack.h5  -m 100102_101120.cor     -t 0.9  -y  200 300  -x 300 400

  mask.py  filt_20060924_20090214.int -m waterMask.h5 -o filt_20060924_20090214_msk.int
  mask.py  filt_20060924_20090214.cor -m waterMask.h5 -o filt_20060924_20090214_msk.cor
c              C   s   t jdt jtd} | jddd | jdddd	d
d | jdddd | jddtdd | jddttjdd | jddtddd | jddtddd | S )Nz	Mask file)descriptionformatter_classepilogfilezFile to be masked)helpz-mz--mask	mask_fileTz'mask for pixels used in ramp estimation)destrequiredr   z-oz	--outfilezOutput file name.z-t	thresholdzlthreshold value used for masking.
if not specified, only pixels with mask value equal to zero is masked out.)r
   typer   z--fill
fill_valuezfill masked out area with input value. i.e. 
np.nan (default), 0, 1000, ... 
If np.nan and input data matrix is not float/complex, convert matrix data type to np.float32.)r
   r   defaultr   z-xsubset_x   z.subset range in x/cross-track/column direction)r
   r   nargsr   z-ysubset_yz+subset range in y/along-track/row direction)	argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentfloatnpnanint)parser r   A/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/mask.pycreate_parser   s     
r    c             C   s   t  }|j| d}|S )N)args)r    
parse_args)iargsr   inpsr   r   r   cmd_line_parse5   s    r%   c             C   s   t |rB| jjdkrBd}|d| j7 }t| t j| t jd} t| j	dkr^|| |dk< n"t| j	dkr|| dd|dkf< | S )	zmask a 2D matrxi data with mask
    Parameters: data : 2D / 3D np.array
                mask : 2D np.array of bool
                fill_value : number
    Returns:    data : same shape of array as input
    )fdcz/in order to fill the invalid pixels with np.nanz,
	convert input matrix from {} to np.float32)dtyper   r      N)
r   isnanr)   kindformatprintarrayfloat32lenshape)datamaskr   msgr   r   r   mask_matrix<   s    r6   Tc             C   s   |s
t  }|jrXd| ddd|jd f< d| dd|jd df< |rXtd|j |jrd| d|jd ddf< d| |jd dddf< |rtd|j |jrd| | |jk < |rtd|j | S )z?Update mask matrix from input options: subset_x/y and thresholdr   N   zmask out area not in x: {}zmask out area not in y: {}z,mask out pixels with value < {} in mask file)r%   r   r.   r-   r   r   )r4   r$   	print_msgr   r   r   update_mask_with_inpsQ   s"    r9   c             C   s   |s
t  }t|d }t||}t| }tdd |D }i }xL|D ]D}tdj||| d tj| |ddd }	t|	||j	d}	|	||< qHW |st
j| \}
}d	|
|}tj||| d
 |S )a+   Mask input fname with mask_file
    Parameters: fname     - str, file to be masked
                mask_file - str, mask file
                out_file  - str, output file name
                inps      - namespace object, from cmd_line_parse()
    Returns:    out_file  - str, output file name
    r   c             S   s   g | ]}t |qS r   )r1   ).0ir   r   r   
<listcomp>z   s    zmask_file.<locals>.<listcomp>zmasking {d:<{w}} from {f} ...)r'   wr&   F)datasetNamer8   )r   z{}_msk{})out_fileref_file)r%   r   readr9   get_dataset_listmaxr.   r-   r6   r   ospathsplitextr   write)fnamer	   r?   r$   r4   dsNamesmaxDigitdsDictdsNamer3   fbasefextr   r   r   r	   i   s"    


r	   c          
   C   s  | sd S t d| t|d }t| }t|d t|d  }}|d  }t|d }ddd	d
d}	|d  }
|
|	 kr|	|
 }
t d|  t d t	j
| \}}|dkrtj| ||f|
||dd}tj| ||f|
||dd}d||dk< t||f }nh|dkrFtj| |
|| dd|}d||dk< n4|dkrlt| d }d||dk< ntd| |s|dkrd||}n0| drd| dd }ntd| || t d| xdD ]}d| |}d||}t|| t d || t d! d"j||d#}t|d$}| }W d Q R X |t	j
| t	j
|}t|d%}|| W d Q R X qW |S )&Nzread mask from {}r   LENGTHWIDTH
INTERLEAVEBANDSbool_r0   float64	complex64)byter   doublecfloat	DATA_TYPEzread {}z:setting the (phase) value on the masked out pixels to zeroz.unwr7   )	data_typenum_band
interleavebandr   z.int)r)   count)z.corz	.conncompzunsupported ISCE file: {})z.intz.corz.unwz{}_msk{}z.unw.conncompz{}_msk.unw.conncompz unrecognized input file type: {}zfinished writing to file {})xmlvrtz{}.{}zcopy {} to {}z(   and update the corresponding filenamez{o}.{e})oerr=   )r.   r-   r   rA   read_attributer   upperlowerkeysrD   rE   rF   read_binaryr   hstackflattenfromfilereshape
ValueErrorendswithsplittofileshutilcopy2openreplacebasenamerG   )in_filer	   r?   r4   atrlengthwidthr\   r[   dataTypeDictrZ   rM   extampphar3   Zin_meta_fileZout_meta_file	meta_filer&   sr   r   r   mask_isce_file   sl    






r   c             C   sh   t | }tj|jd }tj|jd rH|dkrHt|j|j|j nt|j|j|j| t	d d S )Nr7   z.xml)z.unwz.intz.corz	.conncompzDone.)
r%   rD   rE   rF   r   isfiler   r	   outfiler.   )r#   r$   r|   r   r   r   main   s    r   __main__r7   )N)NT)N)N)N)rD   sysrr   r   numpyr   mintpy.utilsr   r   r   r    r%   r   r6   r9   r	   r   r   __name__argvr   r   r   r   <module>	   s   


"
P
