U
     cJ"                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlm	Z	 dZ
d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	writefile)create_argument_parserao  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   d}t }tdd }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.)synopsisdescriptionepilog
subparsers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)	EXAMPLE__name__splitr   add_argumentfloatnpnanint)r
   r   r	   nameparser r"   B/home/exouser/operations/rsmas_insar/sources/MintPy/mintpy/mask.pycreate_parser   s8        
r$   c                 C   s   t  }|j| d}|S )N)args)r$   
parse_args)iargsr!   inpsr"   r"   r#   cmd_line_parse7   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    r:   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   r2   r1   r   r   )r8   r(   	print_msgr"   r"   r#   update_mask_with_inpsS   s"    r=   c                 C   s   |s
t  }t|d }t||}t| }tdd |D }i }|D ]D}tdj||| d tj| |ddd }	t|	||j	d}	|	||< qF|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"   )r5   ).0ir"   r"   r#   
<listcomp>|   s     zmask_file.<locals>.<listcomp>zmasking {d:<{w}} from {f} ...)r+   wr*   F)datasetNamer<   )r   {}_msk{})out_fileZref_file)r)   r   readr=   Zget_dataset_listmaxr2   r1   r:   r   ospathsplitextr   write)fnamer   rD   r(   r8   dsNamesZmaxDigitZdsDictdsNamer7   fbaseZfextr"   r"   r#   r   k   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| dD ]}d| |}d||}t|| t d || t d! d"j||d#}t|d$}| }W 5 Q R X |t	j
| t	j
|}t|d%}|| W 5 Q R X q|S )&Nzread mask from {}r   LENGTHWIDTHZ
INTERLEAVEZBANDSbool_r4   float64	complex64)byter   doublecfloatZ	DATA_TYPEzread {}z:setting the (phase) value on the masked out pixels to zero.unwr;   )	data_typenum_band
interleaveZbandr   .int)r-   countr   ).corz	.conncompzunsupported ISCE file: {})r[   r]   rW   rC   z.unw.conncompz{}_msk.unw.conncompz unrecognized input file type: {}zfinished writing to file {})xmlZvrtz{}.{}zcopy {} to {}z(   and update the corresponding filenamez{o}.{e})oerrA   )r2   r1   r   rE   Zread_attributer   upperlowerkeysrG   rH   rI   read_binaryr   hstackflattenfromfilereshape
ValueErrorendswithr   tofileshutilcopy2openreplacebasenamerJ   )in_filer   rD   r8   ZatrlengthwidthrZ   rY   dataTypeDictrX   rN   extZampZphar7   Zin_meta_fileZout_meta_fileZ	meta_filer*   sr"   r"   r#   mask_isce_file   s|    

    




rx   c                 C   s:   t | }tj|jd }t|j|j|j| td d S )Nr;   zDone.)r)   rG   rH   rI   r   r   Zoutfiler2   )r'   r(   rv   r"   r"   r#   main   s
    ry   __main__r;   )N)N)NT)N)N)N)rG   sysrm   numpyr   mintpy.utilsr   r   Zmintpy.utils.arg_utilsr   r   r$   r)   r   r:   r=   r   rx   ry   r   argvr"   r"   r"   r#   <module>	   s   




"
P
