o
    <c                     @   sN   d dl Z d dlZd dlmZ d dlmZ d dlmZm	Z	 dd Z
d
dd	ZdS )    N)check_reference)
timeseries)readfile	writefilec                 C   s\   t j| t jd} t j|t jd}| | }|t |  |t | < | t | |t |< |S )zSum of 2 input matrixdtype)nparrayfloat32isnan)Zdata1data2data r   C/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/add.py
add_matrix   s   r   Fc              	      s  t | }td| d|   tj| d d }|sBtj| d d }td|D ]}|dtjtj| | d  7 }q)||7 }dd | D }td|  |d d	krq| d | d }}t|}	t|}
t	|
  t	|
 fd
d D }tjt  tjd}| krtd| d|  |rtt t| }td td|  d|t fdd|D < ntdt|	|
\}}}td|  tj||dd }|r|rtdtj| d| d|  |||d |d f}tj|||dd }|t|dddd|jd |jd f8 }|rGtdtj| d|  ||}|t||ddddf |jd ddf8 }td|  t|d }|dk}||  |7  < d||< ~tj|||	|d |S dd | D }ttjtt| }tdd  |D r|d }td!| t |dk rtd"|  i }|D ]E}td#| d$ tj| d |d\}}t| dd D ]"\}}t ||d  dkrdn|}tj||dd }t||}q|||< qtd%| d   tj|||| d d |S )&aL  Generate sum of all input files
    Parameters: fnames   - list of str, path/name of input files to be added
                out_file - str, optional, path/name of output file
    Returns:    out_file - str, path/name of output file
    Example:    'mask_all.h5' = add_file(['mask_1.h5','mask_2.h5','mask_3.h5'], 'mask_all.h5')
    zinput files to be added: (z)
r      _plus_c                 S   s   g | ]	}t |d  qS )	FILE_TYPE)r   read_attribute.0xr   r   r   
<listcomp>/   s    zadd_file.<locals>.<listcomp>zinput file types: r   c                    s   g | ]}| v r|qS r   r   r   i)	dateList2r   r   r   9   s    r   z	WARNING: z does not contain all dates in zBContinue and enforce the differencing for their shared dates only.z4	with following dates are ignored for differencing:
c                    s   g | ]}  |qS r   )indexr   )	dateList1r   r   r   A       z7To enforce the differencing anyway, use --force option.zread from file: )datasetNamez* referencing data from z	 to y/x: /)r   box   z
 to date: Ng        )out_filemetadataZref_filec                 S   s   g | ]}t |qS r   )r   get_dataset_listr   r   r   r   r   f   r   c                 s   s    | ]	}t |d kV  qdS )r   N)lenr   r   r   r   	<genexpr>i   s    zadd_file.<locals>.<genexpr>z&List of common datasets across files: z&No common datasets found among files:
zadding z ...z use metadata from the 1st file: ) r'   printospathsplitextrangebasenamer   r   r   get_date_listr   onesbool_listsetr	   	Exceptionr   readtilereshapeshaper   r   writeintersectionmapall
ValueError	enumerater   )fnamesr$   forcenum_fileextr   ZftypesZfile1Zfile2Zatr1Zatr2ZdateListSharedZ
dateSharedZ
dateListExref_dateref_yref_xr   Zref_boxref_valZref_indr   maskZds_names_listds_namesZdsDictds_nameatrfnameZds_name2readr   )r   r   r   add_file   s   &

",
. 
rL   )NF)r*   numpyr   Zmintpy.diffr   mintpy.objectsr   mintpy.utilsr   r   r   rL   r   r   r   r   <module>   s   
