
    Zf4                         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 j                  d      Zej                  e j                         ddZ	 	 ddZd	dZy)
    N)	attributereadfile	writefilenumpyc                    g d}||vrd| d}|d| z  }t        |      t        |      }t        |      }||z  dk(  r| S t        j                  | j                  t
              }t        |      dk(  rt        j                  |||fz        j                  t              ||fz  }| d|d	   d|d   f   }|d
v r|j                  |d	   |z  ||d   |z  |f      }	t        j                         5  t        j                  dt               |dk(  rt        j                  |	d      }
n|dk(  rt        j                  |	d      }
ddd       nk|dk(  re|t        |dz        d|t        |dz        d|f   }
n>t        |      dk(  rt        j                  |d||fz        j                  t              d||fz  }| d|d	   d|d   d|d   f   }|d
v r|j                  |d	   |d   |z  ||d   |z  |f      }	t        j                         5  t        j                  dt               |dk(  rt        j                  |	d      }
n|dk(  rt        j                  |	d      }
ddd       nI|dk(  rD|ddt        |dz        d|t        |dz        d|f   }
nt        d| dt        |       d      t        j                  
| j                         }
|
S # 1 sw Y   ,xY w# 1 sw Y   8xY w)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
    )meanmediannearestzun-supported multilook method: z. zAvailable methods:    )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methodmethod_listmsgr   	new_shape	crop_datatempout_datas              I/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/multilook.pymultilook_datar/      sF    0K[ /xr:$[M22o JEJEu}HHTZZu-E
5zQHHUeU^34;;C@E5>Q	)A,156	'' $$ile&;U&/le&;U&D ED ((* ?%%hHV#!zz$V<Hx'!||Dv>H? ?& y  U1W!4u!4!$U1W!4u!4"5 6H 
UqHHUa%667>>sCq%QVFWW	)A,&)A,&)A,' (	 ''$$il&/le&;U&/le&;U&D ED
 ((* ?%%hHV#!zz$V<Hx'!||Dv>H? ? y  !$U1W!4u!4!$U1W!4u!4"5 6H
 8uSZLPQRSS xx

3HOo? ?J? ?s   2AKAK+K(+K4c
           
      B   t        |      }t        |      }| j                  d      rt        j                  |       }
nt        j                  |       }
t        |
d         t        |
d         }}t        d|         t        d|        t        d| d|        |ra||d   z   }||d	   z   }|d
|z  z
  d
|d   z  z
  |d   z
  |z  d	z   }|d
|z  z
  d
|d	   z  z
  |d	   z
  |z  d	z   }|||z  z   }|||z  z   }||||f}n|	rt        j                  |	      }t        |d         t        |d         }}|||z  z
  d
z  }|||z  z
  d
z  }|||z  z   }|||z  z   }||||f}d|cxk  r|cxk  r|k  rn nd|cxk  r|cxk  r|k  sn d| d| d| d}t        |      dd||f}| j                  d      rt        | ||||      }|S |st        j                         t        j                  j                  t        j                  j                  |             k(  r0t        j                  j                  |       \  }}| d| d| d| }nt        j                  j                  |       }t        j                  j                  |      d	   }t        j                   |
|||      }
|dv rt#        j$                  ||
|        t        j&                  |       }t)        d |D              }t+               }|D ]  }t        dj-                  ||t        j                  j                  |                    |dv rt/        j0                  |d      5 }t3        j4                  ||   j6                        dz  }ddd       t        t3        j8                  dz  |dz  z              }t        t3        j:                  ||z  dz        dz        }t)        |d      }n|d   |d	   z
  }t        t3        j8                  |d   |d	   z
  ||z  z              }t=        |      D ]_  } |d	   ||z  | z  z   }!|d	   ||z  | d	z   z  z   }"t?        |"|d         }"|d   |!|d
   |"f}#t        |d   |d   z
  |z        t        |!|d	   z
  |z        t        |d
   |d   z
  |z        t        |"|d	   z
  |z        f}$t        d|$        t+        ||#d !      }%|d"k(  rt        j@                  | f||d#|%d   }&n(t        j@                  | fi |%d   }&tC        |&|||$      }&|&jD                  dk(  r"d|&j6                  d   |$d	   |$d   |$d   |$d
   g}'n|$d	   |$d   |$d   |$d
   g}'|dv rt#        jF                  ||&||'d %       [|&||<   b  tI        |jK                               d
k(  rht        j                  j                  |       d	   dvrD|
jM                  d&d'      jO                         d'k7  r!t        d(|
d&           t        d)       d'|
d&<   |dvrtI        |jK                               |
d*<   t#        jP                  |||
| +       t        j                  jS                  | d,z         s"t        j                  jS                  | d-z         rt#        jT                  |
|       |S # 1 sw Y   'xY w).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_alks_rlks)r5   )z.h5z.he5)metadataref_filec              3   2   K   | ]  }t        |        y w)N)r   ).0is     r.   	<genexpr>z!multilook_file.<locals>.<genexpr>   s     +a3q6+s   z"multilooking {d:<{w}} from {f} ...)dwfrr   Ni   @
   r   zbox: F)datasetNamer5   	print_msgr
   )xstepystep)r'   )r$   rE   blockrF   
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)r6   r:   r;   .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_memory
search_win	xcorr_winmarginoff_fileatrlengthwidthx0y0out_widout_lenx1y1r5   atr_offr)   fbasefextdsNamesmaxDigitdsDictdsNamerB   ds_sizenum_steprow_stepr>   r0r1box_ibox_okwargsr$   rI   s(                                           r.   multilook_filer   w   s    JEJE v$$V,%%f-H&CL(9EF	x
()	vh
'(	0s5'
BC jm#jm# 1V8#a
1o5	!DNQRRAfH$qA61E%ORSS'E/!'E/!2r2	 ))(3ww/0#gh6G2Hgo%!+w&1,'E/!'E/!2r2B$$u$A,Bb,BF,B%cU*OPUwV_`f_ggijCS/! !UF# v 3Q 99;"''//"''//&*ABB''**62KE4quUG4v>Ggg&&v.G77G$Q'D 
)
)#ue
ECgfE ''/G+7++HVF 6&299BGG$4$4V$< : > 	? ?" 7C( 7A''!F)//2Q672777Q;*w2F#GHIH2776H#4r#9:R?@H8R(H 1vAHrwwAQHu4DEFGx $	&AQ(U*Q..BQ(U*a!e44BRQB VRQ,E#a&3q6/U23S"s1v+9N5O#a&3q6/U23S"s1v+9N5OQEE%/" f%5IF"}}VP5PPQRS}}V6v6q9%dE5H yyA~DJJqMq58q58- q58q58- &**7047=165:	< "&vI$	&%6&r 	FKKMa  (+>Ae,224=KCP\L]K^_`YZ!L?"6;;=)G3P 77>>&-(BGGNN6*;L,M$$S'2NA7 7s   	&ZZ	c           
      L   t        d       ddlm} |s@| j                  d      r| dd }n)| j                  d      r	| dd d	z   }nt	        d
|  d      t        d|         t        d|        |j                  | |j                        }|sdd|j                  |j                  f}|d   |d   z
  }|d   |d   z
  }t        ||z        }	t        ||z        }
|d   }|d   }||	|z  z   }||
|z  z   }d|	 d|
 d}|d| d| d| d| d	z  }|j                  ||||j                         |j                  ||j                        }|j                  |dz   ||j                  d             t        j                  j                  |dz         sddlm}  ||dz          |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 (r4   zinput : zoutput: r   r   r   z-of ENVI -a_nodata 0 -outsize  z	 -srcwin )optionscallbackr1   VRT)r`   )r   rM   )gdal2isce_xml)rQ   osgeor   rN   r   OpenGA_ReadOnlyRasterXSizeRasterYSizer   	TranslateTermProgress_nocbTranslateOptionsrS   rU   ro   isce.applications.gdal2isce_xmlr   )in_filer%   r&   r5   r6   r   dsin_widin_lenr}   r~   src_x0src_y0src_x1src_y1options_strdsor   s                     r.   rR   rR     s    

56 O,s|Hj)s|f,H<WIRHII	HWI
	HXJ
 	7D,,	-B!R^^R^^4Vc!f_FVc!f_F&5.!G&5.!GVFVFgo%Fgo%F 37)1WIQGKYvhaxq&CCKNN8Rt?U?UNV ))Hd..
/CNN8F?C1F1Fe1F1TNU 77>>(6/*Ahvo&O    )r   r   r   )Nr   r   NNr   N)NN)loggingrS   r   ra   r   r   mintpy.utilsr   rZ   r   r   	getLogger	np_loggersetLevelWARNINGr/   r   rR    r   r.   <module>r      s_     	    ? ? Gg&	 	  7?? #]@ RSGK[|4r   