B
    Qa6*                 @   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m	Z	m
Z
mZ d dlmZmZ dZdd ZdddZd	d
 ZdddZdddZedkreejdd  dS )    N)cluster
timeseriesgiantTimeseriesifgramStackifgramDatasetNames)readfile	writefilea  example:
  diff.py  velocity.h5    velocity_demErr.h5
  diff.py  timeseries.h5  inputs/ERA5.h5  -o timeseries_ERA5.h5
  diff.py  timeseries.h5  inputs/ERA5.h5  -o timeseries_ERA5.h5  --force
  diff.py  timeseries_ERA5_ramp_demErr.h5  ../GIANT/Stack/LS-PARAMS.h5 -o mintpy_giant.h5
  diff.py  reconUnwrapIfgram.h5  ./inputs/ifgramStack.h5  -o diffUnwrapIfgram.h5

  # multiple files
  diff.py  waterMask.h5  maskSantiago.h5  maskFernandina.h5  -o maskIsabela.h5
c              C   sV   t jdt jtd} | jddd | jdddd	 | jd
dddd | jdddd | S )Nz,Generates the difference of two input files.)descriptionformatter_classepilogfile1zfile to be substracted.)helpfile2+zfile used to substract)nargsr   z-oz--outputoutfilez0output file name, default is file1_diff_file2.h5)destr   z--force
store_truezHEnforce the differencing for the shared dates only for time-series files)actionr   )argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argument)parser r   A/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/diff.pycreate_parser&   s    
r   c             C   sN   t  }|j| d}t|j}|d dkrJt|jdkrJtd|d |S )N)args	FILE_TYPE)r   r      z,ERROR: only one file2 is inputed for {} type)	r   
parse_argsr   read_attributer   lenr   
SystemExitformat)iargsr   inpsatrr   r   r   cmd_line_parse4   s    r)   c             C   s   | d | ddkrd}n| d }|  dd}|  dd}|| ddksZ|| ddkrdd}d}n|}|}|dk	r|t|}|dk	rt|}|||fS )zCheck reference date and point
    Parameters: atr1/2   - dict, metadata of file1/2
    Returns:    ref_date - str, None for re-referencing in time  is NOT needed
                ref_y/x  - int, None for re-referencing in space is NOT needed
    REF_DATENREF_YREF_X)getint)atr1atr2ref_dateref_yref_xr   r   r   check_referenceA   s    	 r4   F    קAc       *   	      s  t   }|sVtj| \}}t|dkr0tdd|tjtj|d d |}td| || t	
| }|d }	t	
|d }
|
d }td|	| |	dkr$|d	krtd
t|dkrtdt|d t	
| }t	
|d }
t|   |dkr t|d  d}n|dkr>t|d  d}t||
\}}}fdd D }tjt tjd}| krtd||  |rtt t| }td td| d|t fdd|D < ntdtj|| d t|d t|d  }}ttt | | | }tjdd||f|ddd}|r|r|||d |d f}t	j|d ||dd | }xt|D ]\}}|dkrtd|d | td| td |d  t	j|d ||dd | }|rF|rFtd!tj|d || |t|d"ddd|j d |j d# f8 }|rtd$tj|d | |!|}|t||d%d%d%d%f |j d ddf8 }td |  t	j| |d&d }|d'k}||  |8  < d'||< ~d|j d |d |d( |d |d# g}tj"|||	|d) qW nlt#d*d+ |	|gD rt$| } | %  t$|d }!|!%  tt| j&t|!j&@ tdkrtd,fd-dt'D d }"td.|"|  t	j| |"d/d }#td.|"|d  t	j|d |"d/d }d0|"( krtd1| j)| j* |#d%d%| j)| j*f }$|d%d%|!j)|!j*f }%xxt+|#j d D ]f}|#|d%d%f |#|d%d%f d'k  |$| 8  < ||d%d%f ||d%d%f d'k  |%| 8  < qNW tj,|#|#dk< tj,||dk< |#| }~#~d'|t-|< i }&||&|"< tj.|&|| d2 nt	| d }#t|#|#j/}xJ|D ]B}'t	|'d }tj|tj0dtj|tj0d }t||#j/}q.W td3|  tj.|||d4 t1t   | d5\}(})td6|(|) |S )7aJ  calculate/write file1 - file2

    Parameters: file1   - str, path of file1
                file2   - list of str, path of file2(s)
                out_file - str, path of output file
                force   - bool, overwrite existing output file
                max_num_pixel - float, maximum number of pixels for each block
    r    z7Output file name is needed for more than 2 files input.z{}_diff_{}{}r   z{} - {} --> {}r   zinput files are: {} and {}r   )r   r   z8Input multiple dataset files are not the same file type!zFOnly 2 files substraction is supported for time-series file, {} input.g      ?r   gMbP?c                s   g | ]}| kr|qS r   r   ).0i)	dateList2r   r   
<listcomp>   s    zdiff_file.<locals>.<listcomp>)dtypez,WARNING: {} does not contain all dates in {}zBContinue and enforce the differencing for their shared dates only.z6	with following dates are ignored for differencing:
{}c                s   g | ]}  |qS r   )index)r6   r7   )	dateList1r   r   r9      s    z7To enforce the differencing anyway, use --force option.)ref_fileLENGTHWIDTHyT)box	num_split	dimension	print_msg)datasetNamerA   z5
------- processing patch {} out of {} --------------zbox: {}zread from file: {}z(* referencing data from {} to y/x: {}/{}   z&* referencing data from {} to date: {}N)rA   g           )datarE   blockc             s   s   | ]}|d kV  qdS )r   Nr   )r6   r7   r   r   r   	<genexpr>   s    zdiff_file.<locals>.<genexpr>z$no common dataset between two files!c                s   g | ]}| kr|qS r   r   )r6   r7   )dsNamesr   r   r9      s    zreading {} from file {} ...)rE   Zunwrapphasez referencing to pixel ({},{}) ...)out_filer=   zwriting >>> )rM   metadata<   z&time used: {:02.0f} mins {:02.1f} secs)2timeospathsplitextr#   
ValueErrorr%   basenameprintr   r"   	Exceptionr   get_date_listr   r4   nponesbool_listsetarrayr   layout_hdf5r.   ceilr   split_box2sub_boxesread	enumeratetilereshapeshaper;   write_hdf5_blockallr   opendatasetNamesr   lowerrefYrefXrangenanisnanwriter:   float32divmod)*r   r   rM   forceZmax_num_pixel
start_timefbasefextr/   k1r0   k2Zunit_facr1   r2   r3   ZdateListSharedZ
dateSharedZ
dateListExlengthwidthnum_boxbox_listref_boxref_valr7   rA   data2Zref_indrI   maskrJ   Zobj1obj2dsNameZdata1Zref1Zref2ZdsDictfnamemsr   )r<   r8   rL   r   	diff_fileb   s    	&








,
.&06
 r   c             C   s(   t | }t|j|j|j|jd|_|jS )N)rt   )r)   r   r   r   r   rt   )r&   r'   r   r   r   main  s    r   __main__r    )N)NFr5   )N)rQ   sysrP   r   numpyrY   mintpy.objectsr   r   r   r   r   mintpy.utilsr   r   r   r   r)   r4   r   r   __name__argvr   r   r   r   <module>	   s   
!
 '
	