o
    Bd4                     @   sx   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
 e dZee j dddZ		dd	d
ZdddZdS )    N)	attributereadfile	writefilenumpy   meanc                 C   s  g d}||vrd| d}|d| 7 }t |t|}t|}|| dkr)| S tj| jtd}t|dkrt|||f t||f }| d|d	 d|d f }|d
v r|	|d	 | ||d | |f}	t
 & t
jdtd |dkrtj|	dd}
n|dkrtj|	dd}
W d   n1 sw   Y  n|dkr|t|d d|t|d d|f }
nt|dkrNt|d||f td||f }| d|d	 d|d d|d f }|d
v r1|	|d	 |d | ||d | |f}	t
 ( t
jdtd |dkrtj|	dd}
n|dkr tj|	dd}
W d   n	1 s+w   Y  n*|dkrM|ddt|d d|t|d d|f }
nt d| dt| dtj|
| jd}
|
S )a,  Apply multilooking (spatial averaging/resampling) to a multi-dimensional array.

    Link: https://stackoverflow.com/questions/34689519/how-to-coarser-the-2-d-array-data-resolution

    Parameters: data     - 2D / 3D np.array in real or complex
                lks_y    - int, number of multilook in y/azimuth direction
                lks_x    - int, number of multilook in x/range direction
                method   - str, multilook method, mean, median or nearest
    Returns:    out_data - 2D / 3D np.array after multilooking in last two dimension
    )r   mediannearestzun-supported multilook method: z. zAvailable methods: r   )dtype   Nr   )r   r   ignore)categoryr   )r      )axisr   r	   r   )r      zUn-supported data dimension: z --> !)
ValueErrorintnparrayshapefloatlenfloorastypereshapewarningscatch_warningssimplefilterRuntimeWarningnanmean	nanmedianr
   )datalks_ylks_xmethodZmethod_listmsgr   	new_shapeZ	crop_datatempout_data r*   I/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/multilook.pymultilook_data   sx    
$







r,   r   c
           (   	   C   s  t |}t |}| drt| }
nt| }
t |
d t |
d }}td|   td|  td| d|  |r||d  }||d	  }|d
|  d
|d   |d  | d	 }|d
|  d
|d	   |d	  | d	 }|||  }|||  }||||f}nn|	rt|	}t |d t |d }}|||  d
 }|||  d
 }|||  }|||  }||||f}d|  kr|  k r|krn nd|  kr|  k r|ksn d| d| d| d}t|ndd||f}| drt| ||||d}|S |s3t	 tj
tj
| kr-tj
| \}}| d| d| d| }ntj
| }tj
|d	 }tj|
|||d}
|dv rQtj||
| d t| }tdd |D }t }|D ];}tdj||tj
| d |dv rt|d}t|| jd }W d   n	1 sw   Y  t t|d |d  }t t|| d d }t|d}n|d |d	  }t t|d |d	  ||  }t|D ]} |d	 || |   }!|d	 || | d	   }"t|"|d }"|d |!|d
 |"f}#t |d |d  | t |!|d	  | t |d
 |d  | t |"|d	  | f}$td |$  t||#d!d"}%|d#krMtj | f||d$|%d }&ntj | fi |%d }&t!|&|||d%}&|&j"dkrzd|&jd |$d	 |$d |$d |$d
 g}'n|$d	 |$d |$d |$d
 g}'|dv rtj#||&||'d!d& q|&||< qؐqdt$|% d
krtj
| d	 dvr|
&d'd(' d(krtd)|
d'   td* d(|
d'< |dvrt$|% |
d+< tj(|||
| d, tj
)| d- stj
)| d. rt*|
| |S )/a   Multilook input file.

    Parameters: infile     - str, path of input file to be multilooked.
                lks_y      - int, number of looks in y / row direction.
                lks_x      - int, number of looks in x / column direction.
                outfile    - str, path of output file
                max_memory - float, maximum used memory in GB
                search_win - list(int), ampcor (half) search window in (width, length)
                xcorr_win  - list(int), ampcor cross-correlation window in (width, length)
                margin     - int, ampcor margin
                off_file   - str, path to the ampcor dense offsets file
    Returns:    outfile    - str, path of output file
    .vrtLENGTHWIDTHzmultilooking file: zmultilook method: z$number of looks in y / x direction: z / r   r   r   zCalculated bbox (z%) exceeds input file coverage (width=z	, length=)!)boxout_file_Zalks_Zrlks)r1   )z.h5z.he5)metadataref_filec                 s   s    | ]}t |V  qd S )N)r   ).0ir*   r*   r+   	<genexpr>   s    z!multilook_file.<locals>.<genexpr>z"multilooking {d:<{w}} from {f} ...)dwfrr   Ni   @
   r   zbox: F)datasetNamer1   	print_msgr	   )xstepystep)r%   )r"   r>   blockr?   
INTERLEAVEBILz;the input binary file has 2 bands with band interleave as: zIfor the output binary file, change the band interleave to BIL as default.BANDS)r2   r4   r5   .xmlz.aux.xml)+r   endswithr   read_gdal_vrtread_attributeprintr   multilook_gdalosgetcwdpathdirnameabspathsplitextbasenameattrupdate_attribute4multilookr   layout_hdf5get_dataset_listmaxdictformath5pyFiler   prodr   ceilrintrangeminreadr,   ndimwrite_hdf5_blockr   keysgetupperwriteisfilewrite_isce_xml)(infiler#   r$   outfiler%   
max_memoryZ
search_winZ	xcorr_winmarginZoff_fileatrlengthwidthx0y0out_widout_lenx1y1r1   Zatr_offr&   fbasefextdsNamesmaxDigitdsDictdsNamer;   ds_sizenum_steprow_stepr7   r0r1Zbox_iZbox_okwargsr"   rB   r*   r*   r+   multilook_filew   s   

((
B



"((



'
$r   c              
   C   s  t d ddlm} |s.| dr| dd }n| dr&| dd d	 }ntd
|  dt d|   t d|  || |j}|sMdd|j|jf}|d |d  }|d |d  }t	|| }	t	|| }
|d }|d }||	|  }||
|  }d|	 d|
 d}|d| d| d| d| d	7 }|j
||||jd |||j}|j
|d ||jddd tj|d sddlm} ||d  |S )aq  Apply multilooking via gdal_translate.

    Parameters: in_file  - str, path to the input GDAL VRT file
                lks_y/x  - int, number of looks in Y/X direction
                box      - tuple(int), area of interest in x0, y0, x1, y1
                out_file - str, path to the output data file
    Returns:    out_file - str, path to the output data file
    z)apply multilooking via gdal.Translate ...r   )gdalz.rdr.full.vrtNiz.rdr.vrtz.mlizun-recognized ISCE VRT file (r0   zinput : zoutput: r   r   r   z-of ENVI -a_nodata 0 -outsize  z	 -srcwin )optionscallbackr-   ZVRT)rY   )r   rF   )gdal2isce_xml)rJ   osgeor   rG   r   OpenGA_ReadOnlyRasterXSizeRasterYSizer   Z	TranslateZTermProgress_nocbZTranslateOptionsrL   rN   rh   Zisce.applications.gdal2isce_xmlr   )in_filer#   r$   r1   r2   r   dsZin_widZin_lenrs   rt   Zsrc_x0Zsrc_y0Zsrc_x1Zsrc_y1Zoptions_strZdsor   r*   r*   r+   rK     s<   	

"rK   )r   r   r   )Nr   r   NNr   N)NN)loggingrL   r   rZ   r   r   mintpy.utilsr   rS   r   r   	getLoggerZ	np_loggersetLevelWARNINGr,   r   rK   r*   r*   r*   r+   <module>   s   

`
 