o
    BdB                     @   s`   d dl Z d dlZd dlZd dlmZmZ ejfddZdddZ	dejdfdd	Z
dd
dZdS )    N)readfile	writefilec                 C   s   t |r"| jjdvr"d}|d| j d7 }t| t j| t jd} t| jdkr1|| |dk< | S t| jdkrB|| 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 z to np.float32)dtype   r      N)	npisnanr   kindprintZarrayfloat32lenshape)datamask
fill_valuemsg r   D/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/mask.pymask_matrix   s   r   Tc                 C   s   |rt ndd }|jr-d| ddd|jd f< d| dd|jd df< |d|j  |jrRd| d|jd ddf< d| |jd dddf< |d|j  |jrc| |jk} |d|j d	 |jrt| |jk} |d
|j d	 | S )zEUpdate mask matrix from input options: subset_x/y and mask_vmin/vmax.c                  _   s   d S Nr   )argskwargsr   r   r   <lambda>(   s    z'update_mask_with_inps.<locals>.<lambda>r   N   zmask out area not in x: zmask out area not in y: zmask out pixels with value < z in mask filezmask out pixels with value > )r   subset_xsubset_y	mask_vmin	mask_vmax)r   inps	print_msgZvprintr   r   r   update_mask_with_inps&   s    

r#   c                 C   s   t |d }|durt||}t | }tdd |D }i }|D ]!}	tdj|	|| d t j| |	ddd }
t|
||d	}
|
||	< q"|sUtj	
| \}}| d
| }tj||| d |S )ar   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, including:
                            subset_x/y
                            mask_vmin/vmax
    Returns:    out_file  - str, output file name
    r   Nc                 s   s    | ]}t |V  qd S r   )r   ).0ir   r   r   	<genexpr>Q   s    zmask_file.<locals>.<genexpr>zmasking {d:<{w}} from {f} ...)r   wr   F)ZdatasetNamer"   )r   _msk)out_fileZref_file)r   readr#   Zget_dataset_listmaxr   formatr   ospathsplitextr   write)Zfname	mask_filer)   r   r!   r   ZdsNamesZmaxDigitZdsDictZdsNamer   fbaseZfextr   r   r   r1   ?   s    


r1   c              	   C   s  | sd S t d|  t|d }t| }t|d t|d }}|d  }t|d }ddd	d
d}	|d  }
|
|	 v rG|	|
 }
t d|   t d tj	
| \}}|dkrt||f|
||d}tj| fddi|}tj| fddi|}d||dk< t||f }n2|dkrtj| |
|| dd|}d||dk< n|dv rt| d }d||dk< ntd|  |s|dv r| d| }n| drd| dd }ntd|  || t d|  dD ]m}|  d | }| d | }t|| t d!| d"|  t d# | d | }t|}| }W d    n	1 s2w   Y  |tj	| tj	|}t|d$}|| W d    n	1 s[w   Y  q|S )%Nzread mask from r   ZLENGTHZWIDTHZ
INTERLEAVEZBANDSZbool_r   Zfloat64Z	complex64)ZbytefloatZdoubleZcfloatZ	DATA_TYPEzread z:setting the (phase) value on the masked out pixels to zero.unw)r   	data_typenum_band
interleaveZbandr   r   .int)r   count).corz	.conncompzunsupported ISCE file: )r8   r;   r4   r(   z.unw.conncompz{}_msk.unw.conncompzunrecognized input file type: zfinished writing to file )ZxmlZvrt.zcopy z to z(   and update the corresponding filenamer'   )r   r   r*   Zread_attributeintupperlowerkeysr-   r.   r/   dictZread_binaryr
   ZhstackZflattenZfromfileZreshape
ValueErrorendswithr,   splitZtofileshutilcopy2openreplacebasenamer0   )Zin_filer1   r)   r   Zatrlengthwidthr7   r6   Zdata_type_dictr5   r2   extr   ZampZphar   Zin_meta_fileZout_meta_fileZ	meta_filer   sr   r   r   mask_isce_fileb   s~   





rN   )Tr   )r-   rE   Znumpyr
   mintpy.utilsr   r   nanr   r#   r1   rN   r   r   r   r   <module>   s   
#