B
    Qa              4   @   s  d dl Z 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
 ejejejejejejejejejejejejejejejejejejejejejdZdddgZdddd	d
dgZddddddddddddgZddddddddd d!d"d#d$d%d&gZd'd(d(d'd'd(d)d)d)d)d(d*d+d+d(d(d+d+d*d(d(d(d*d*d*d*d*d*d*d(d,d-d(d.d.d.d.d.d.d.d(d(d'd'd'd(d*d*d*d(d(d/3ZG d0d dZG d1d2 d2ZG d3d4 d4ZG d5d6 d6ZG d7d dZdS )8    N)ptime	time_func)boolbyteflagintint16shortint32int64longfloatfloat32float_float64complex	complex64Zcpx_float32cfloatZcfloat32
complex128complex_Zcpx_float64
timeseriesHDFEOSgiantTimeseriesrawtroposphericDelaytopographicResidualrampdisplacementheightlatitude	longitude
rangeCoordazimuthCoordincidenceAngleazimuthAngleslantRangeDistance
shadowMask	waterMask
commonMaskbperpunwrapPhase!unwrapPhase_bridging_phaseClosureunwrapPhase_bridgingZunwrapPhase_phaseClosure	coherenceconnectComponent	wrapPhase	ionoPhase	magnitudeazimuthOffsetazimuthOffsetStdrangeOffsetrangeOffsetStd	offsetSNRZrefPhaseZradian1Zpixelmdegreezm/yearzm/year^2mm)3r*   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r   r   r   r   r   r   temporalCoherencevelocityZaccelerationmaskr   reconsrawtssar_aps	igram_apsfigramigramcmaskifgcntZunwr   flatZcorZdemZhgtZhgt_simr1   Z	intensityc               @   s   e Zd ZdZejZd ddZd!ddZd"dd	Zd
d Z	dd Z
dd Zd#ddZd$ddZd%ddZd&ddZd'ddZdd Zd(ddZdS ))r   a  
    Time-series object for displacement of a set of SAR images from the same platform and track.
    It contains three datasets in root level: date, bperp and timeseries.

    File structure: https://mintpy.readthedocs.io/en/latest/api/data_structure/#timeseries
    Nc             C   s   || _ d| _d S )Nr   )filename)selfrG    rJ   J/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/objects/stack.py__init__   s    ztimeseries.__init__Tc             C   s<   y*| j   |r(tdtj| j W n   Y nX d S )Nzclose timeseries file: {})fcloseprintformatospathbasenamerG   )rI   	print_msgrJ   rJ   rK   rN      s    
ztimeseries.closec          	      sn  |r t d jtj j        	   j
 j  _t jdF}y,|d d d   _  j j j 8  _W n   d  _Y nX W d Q R X t jd  _t fdd jD  _d jkstdd	  jD rt jd
 tfdd jD  _tjdd  j j j  D tjd _dd  jD  _ fdd jD  _d S )Nzopen {} file: {}rr)   r   c                s   g | ]}t j| jqS rJ   )dtdatetimestrptime
dateFormat).0i)rI   rJ   rK   
<listcomp>   s    z#timeseries.open.<locals>.<listcomp>Tc             s   s"   | ]}|j d ko|jd kV  qdS )r   N)hourminute)rZ   r[   rJ   rJ   rK   	<genexpr>   s    z"timeseries.open.<locals>.<genexpr>ZCENTER_LINE_UTCc                s   g | ]}|t j d  qS ))seconds)rV   	timedelta)rZ   r[   )utc_secrJ   rK   r\      s    c             S   s   g | ]}|j |jd   qS )iQ )daysra   )rZ   r[   rJ   rJ   rK   r\      s   )dtypec             S   s$   g | ]}|j | jd  d  qS )   g     v@)year	timetupletm_yday)rZ   r[   rJ   rJ   rK   r\      s    c                s   g | ]}d   j|qS )z{}-{})rP   rH   )rZ   r[   )rI   rJ   rK   r\      s    )rO   rP   rH   rQ   rR   rS   rG   get_metadataget_sizeget_date_listlengthwidthnumPixelh5pyFilepbaserefIndexr   get_date_str_formatdateListrY   nparraytimesallr   metadatar   tbaseyearList	sliceList)rI   rT   rM   rJ   )rI   rc   rK   open   s.     
ztimeseries.openc          	   C   s   t | jd"}t|j| _|d d d  }W d Q R X xB| j D ]4\}}y|d| j|< W qB   || j|< Y qBX qBW dd |D }d| j kr|d | jd< |	| jd | _
|d | jd< |d	 | jd
< | jS )NrU   dateutf8c             S   s   g | ]}| d qS )r   )decode)rZ   r[   rJ   rJ   rK   r\      s    z+timeseries.get_metadata.<locals>.<listcomp>REF_DATEr   
START_DATEEND_DATE)rp   rq   rG   dictattrsrz   itemsr   keysindexrs   )rI   rM   dateskeyvalueru   rJ   rJ   rK   rj      s    ztimeseries.get_metadatac          	   C   sB   t | jd}|| j j\| _| _| _W d Q R X | j| j| jfS )NrU   )rp   rq   rG   rH   shapenumDaterm   rn   )rI   rM   rJ   rJ   rK   rk      s    "ztimeseries.get_sizec          	   C   s<   t | jd"}dd |d d d  D | _W d Q R X | jS )NrU   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\      s    z,timeseries.get_date_list.<locals>.<listcomp>r   )rp   rq   rG   ru   )rI   rM   rJ   rJ   rK   rl      s    &ztimeseries.get_date_listc       
   	   C   s8  |rt d| j| j | jdd |r0|dkr6g }nt|trF|g}dd |D }t| jd}|| j }t|tj	r|| j }t
j| jt
jd}|sd	|d
d
< nx|D ]}d	|| j|< qW |d
krdd| j| jg}|||d |d |d |d f }	|r*tdd |	jD r*t
|	}	W d
Q R X |	S )a  Read dataset from timeseries file
        Parameters: self : timeseries object
                    datasetName : (list of) string in YYYYMMDD format
                    box : tuple of 4 int, indicating x0,y0,x1,y1 of range
        Returns:    data : 2D or 3D dataset
        Examples:   from mintpy.objects import timeseries
                    tsobj = timeseries('timeseries_ERA5_demErr.h5')
                    data = tsobj.read(datasetName='20161020')
                    data = tsobj.read(datasetName='20161020', box=(100,300,500,800))
                    data = tsobj.read(datasetName=['20161020','20161026','20161101'])
                    data = tsobj.read(box=(100,300,500,800))
        z!reading {} data from file: {} ...F)rT   r   c             S   s    g | ]}| d d ddqS )r    -)replace)rZ   r[   rJ   rJ   rK   r\      s    z#timeseries.read.<locals>.<listcomp>rU   )re   TNr   rf         c             s   s   | ]}|d kV  qdS )rf   NrJ   )rZ   r[   rJ   rJ   rK   r`     s    z"timeseries.read.<locals>.<genexpr>)rO   rP   rH   rG   r~   
isinstancestrrp   rq   Grouprv   zerosr   bool_ru   r   rn   rm   anyr   squeeze)
rI   datasetNameboxr   rT   rM   dsdateFlagedatarJ   rJ   rK   read   s2    



ztimeseries.readc          	   C   s  |s
| j }|rt|}|jdd |dkr0|j}|dkr>|j}|dkrL|j}|dkrzt|d}	|	td  j	}W dQ R X |j
dd tj|tjd}tj|tjd}tj|tjd}t|}| j|d< tjtj|}
tj|
st|
 td|
 td	| t|d
}tdjt|j|j|d |jd|d|d tdt|j|j |jd|d |jdkrtdt|j|j |jd|d x$| D ]\}}t||j|< qW W dQ R X td| |S )a7  
        Parameters: data  : 3D array of float32
                    dates : 1D array/list of string in YYYYMMDD format
                    bperp : 1D array/list of float32 (optional)
                    metadata : dict
                    outFile : string
                    refFile : string
                    compression : string or None
        Returns: outFile : string
        Examples:
            from mintpy.objects import timeseries

            ##Generate a new timeseries file
            tsobj = timeseries('timeseries.h5')
            timeseries.write(data, dates=dateList, bperp=bperp, metadata=atr)

            ##Generate a timeseries with same attributes and same date/bperp info
            tsobj = timeseries('timeseries_demErr.h5')
            timeseries.write(data, refFile='timeseries.h5')
        F)rT   NrU   r   )re   	FILE_TYPEzcreate directory: {}z+create timeseries HDF5 file: {} with w modewzIcreate dataset /timeseries of {t:<10} in size of {s} with compression={c})tscr   T)r   chunkscompressionz2create dataset /dates      of {:<10} in size of {}r   )r   rJ   z2create dataset /bperp      of {:<10} in size of {}r)   zfinished writing to {})rG   r   r~   rz   ru   rr   rp   rq   timeseriesDatasetNamesr   rN   rv   rw   r   string_r   rH   rQ   rR   dirnameabspathisdirmakedirsrO   rP   r   re   r   create_datasetr   r   )rI   r   outFiler   r)   rz   ZrefFiler   ZrefobjZrfZoutDirrM   r   r   rJ   rJ   rK   
write2hdf5  sV    


ztimeseries.write2hdf5c          
   C   s   |   }|r:t|  }td|  tj|dd|dkf< tj|dd| _d}|d| j7 }|d|7 }|d	7 }|st	j
t	j
t	j
| jd
t	j
t	j
| jd }tj|tt| jdd| jddfdd|d td| |S )zxCalculate the standard deviation (STD) for acquisition of time-series,
           output result to a text file.
        zread mask from file: Nr   )rf   r   )axisz@Standard Deviation in space for each acquisition of time-series
zTimeseries file: {}
zMask file: {}
zDate		STD (m)z
std_{}.txtr   rf   z%s	)fmt	delimiterheaderz$save timeseries STD to text file: {})r   singleDatasetrO   rv   nannanstdstdrP   rG   rQ   rR   joinr   r   splitextrS   savetxthstackrw   ru   reshape)rI   maskFiler   r   r=   r   rJ   rJ   rK   timeseries_stdc  s"    ",ztimeseries.timeseries_stdc       
   
   C   s  |   }t|}|r8tj|r8td|  t| }t	|tj
 | _td| j| j tj|d}xt|D ]x}| jd|| dd}|rtj|rtj
||dk< ttjt|dd	| j|< |j|d
 d|d
 |d qtW |  d}	|	d| j7 }	|	d|7 }	|	d7 }	|sdtjtjtj| jdtjtj| jd }tj|tt| j dd
| j dd
fdd|	d td| |S )zyCalculate the Root Mean Square for each acquisition of time-series
            and output result to a text file.
        zread mask from file: z!reading {} data from file: {} ...)maxValuez{}F)r   rT   r   )r   rf   )r   rf   z{}/{})suffixz>Root Mean Square in space for each acquisition of time-series
zTimeseries file: {}
zMask file: {}
zDate		RMS (m)z
rms_{}.txtr   z%sr   )r   r   r   z$save timeseries RMS to text file: {})!rl   lenrQ   rR   isfilerO   r   r   rv   r   r   ZrmsrP   rH   rG   r   progressBarrangesqrtnanmeansquareupdaterN   r   r   r   r   rS   r   r   rw   ru   r   )
rI   r   r   	date_listnum_dater=   prog_barr[   r   r   rJ   rJ   rK   timeseries_rms|  s4     "",ztimeseries.timeseries_rmsFc             C   s   | j dd | j|d}|rVtj|rVtd|  t|j|d}tj||t	|k< |d k	rvd|||k< d|||k< tj
|dd}|| jfS )	NF)rT   )r   zread mask from file: rf   r   )rf   r   )r   )r~   r   rQ   rR   r   rO   r   rv   r   r   r   ru   )rI   r   r   reverseMask	thresholdr   r=   dmeanrJ   rJ   rK   spatial_average  s    ztimeseries.spatial_averagec             C   s:   t d| j | jdd | jdd}tj|dd}|S )Nz7calculating the temporal average of timeseries file: {}F)rT   )r   r   )r   )rO   rP   rG   r~   r   rv   r   )rI   r   r   rJ   rJ   rK   temporal_average  s
    ztimeseries.temporal_averagebl_list.txtc          	   C   sx   | j dd dd | jD }| j }td| t |d0}x(t||D ]\}}|d|| qLW W dQ R X |S )	z2Generate bl_list.txt file from timeseries h5 file.F)rT   c             S   s   g | ]}|d d qS )r      rJ   )rZ   r[   rJ   rJ   rK   r\     s    z0timeseries.save2bl_list_file.<locals>.<listcomp>z$write baseline list info to file: {}r   z{}	{}
N)r~   ru   rr   tolistrO   rP   zipwrite)rI   out_fileZ
date6_listZ
pbase_listrM   drr   rJ   rJ   rK   save2bl_list_file  s    
 ztimeseries.save2bl_list_file)N)T)T)NNTT)NNNNNN)NN)NN)NNFN)r   )__name__
__module____qualname____doc__r   Zget_design_matrix4time_funcrL   rN   r~   rj   rk   rl   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   r      s   

	

2
K

&
c               @   sR   e Zd ZdZdddZdddZddd	Zd
d Zdd Ze	d ddfddZ
dS )geometryzp Geometry object.

    File structure: https://mintpy.readthedocs.io/en/latest/api/data_structure/#geometry
    Nc             C   s   || _ d| _d S )Nr   )rG   rH   )rI   rG   rJ   rJ   rK   rL     s    zgeometry.__init__Tc             C   s<   y*| j   |r(tdtj| j W n   Y nX d S )Nzclose geometry file: {})rM   rN   rO   rP   rQ   rR   rS   rG   )rI   rT   rJ   rJ   rK   rN     s    
zgeometry.closec          	      s  |r t d| jtj| j |   |   | j	| j
 | _d| _d| j krXd| _t| jd  fdd  D | _t| j| _d  krd	d  d
 d d  D | _t| j| _y| jd W n   Y nX |  jdd | jD 7  _nd | _W d Q R X d S )Nzopen {} file: {}FY_FIRSTTrU   c                s    g | ]}t  | tjr|qS rJ   )r   rp   Dataset)rZ   r[   )rM   rJ   rK   r\     s    z!geometry.open.<locals>.<listcomp>r)   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    r   c             S   s   g | ]}d | qS )zbperp-rJ   )rZ   r   rJ   rJ   rK   r\     s    )rO   rP   rH   rQ   rR   rS   rG   rj   rk   rm   rn   ro   Zgeocodedrz   r   rp   rq   datasetNameslistr}   ru   r   r   remove)rI   rT   rJ   )rM   rK   r~     s(    zgeometry.openc          	   C   st   t | jdT}dd | D d }|| j}t|dkrR|dd \| _| _n|\| _| _W d Q R X | j| jfS )NrU   c             S   s   g | ]}|t kr|qS rJ   )geometryDatasetNames)rZ   r[   rJ   rJ   rK   r\     s    z%geometry.get_size.<locals>.<listcomp>r   r   rf   )rp   rq   rG   r   r   r   rm   rn   )rI   rM   dsNameZdsShaperJ   rJ   rK   rk     s    
zgeometry.get_sizec          	   C   sp   t | jd}t|j| _W d Q R X xB| j D ]4\}}y|d| j|< W q2   || j|< Y q2X q2W | jS )NrU   r   )rp   rq   rG   r   r   rz   r   r   )rI   rM   r   r   rJ   rJ   rK   rj      s    zgeometry.get_metadatar   c       	   	      s  | j dd |dkr$dd| j| jf}|dkr6td }nt|trF|g}t| jd6}|d 	dd  |  }|rt
d | j t|jdkr|dd }nt|jd	kr||d |d
 |d |d	 f }ntj|jd tjd} fdd|D }tdd |D r"d|dd< n x|D ]}d|| j|< q(W |||d |d
 |d |d	 f }tdd |jD rt|}W dQ R X |S )ax  Read 2D / 3D dataset with bounding box in space
        Parameters: datasetName : (list of) string, to point to specific 2D dataset, e.g.:
                        height
                        incidenceAngle
                        bperp
                        ...
                        bperp-20161020
                        bperp-20161026
                        bperp-...
                    box : tuple of 4 int, for (x0,y0,x1,y1)
                    print_msg : bool
        Returns: data : 2D or 3D array
        Example:
            obj = geometry('./inputs/geometryRadar.h5')
            obj.read(datasetName='height')
            obj.read(datasetName='incidenceAngle')
            obj.read(datasetName='bperp')
            obj.read(datasetName='bperp-20161020')
            obj.read(datasetName=['bperp-20161020',
                                  'bperp-20161026'])
        F)rT   Nr   rU   r   z%reading {:<15} data from file: {} ...rf   r   r   )re   c                s    g | ]}|  d  dd qS )r   r   )r   )rZ   r[   )
familyNamerJ   rK   r\   6  s    z!geometry.read.<locals>.<listcomp>c             s   s   | ]}| V  qd S )NrJ   )rZ   r[   rJ   rJ   rK   r`   7  s    z geometry.read.<locals>.<genexpr>Tc             s   s   | ]}|d kV  qdS )rf   NrJ   )rZ   r[   rJ   rJ   rK   r`   B  s    )r~   rn   rm   r   r   r   rp   rq   rG   splitrO   rP   r   r   rv   r   r   r   ru   r   r   )	rI   r   r   rT   rM   r   r   r   r   rJ   )r   rK   r   
  s8    

&
zgeometry.read)N)T)T)r   r   r   r   rL   rN   r~   rk   rj   r   r   rJ   rJ   rJ   rK   r     s   




r   c               @   s   e Zd ZdZd.ddZd/ddZd0dd	Zd
d Zd1ddZdd Z	d2ddZ
d3ddZd4ddZdd Zd5ddZd6ddZd7dd Zd8d"d#Zd$d% Zed&d' Zed9d(d)Zd:d*d+Zd,d- ZdS );ifgramStackz Interferograms Stack object.

    File structure: https://mintpy.readthedocs.io/en/latest/api/data_structure/#ifgramstack
    Nc             C   s   || _ d| _d S )Nr   )rG   rH   )rI   rG   rJ   rJ   rK   rL   P  s    zifgramStack.__init__Tc             C   s@   y.| j   |r,td| jtj| j W n   Y nX d S )Nzclose {} file: {})	rM   rN   rO   rP   rH   rQ   rR   rS   rG   )rI   rT   rJ   rJ   rK   rN   T  s    
 zifgramStack.closec          	      s  |r t djtjj     	  j
j _dd tjjD _tjdd jj D tjd_tjdnd dd _d	 dd _fd
d D fddtD _ jdd D 7  _W dQ R X g _x.jD ]$  j fddjD 7  _qW jdd_ t!j _"y$t#j$d _%t#j$d _&W n   d_%d_&Y nX y$t'j$d _(t'j$d _)W n   d_(d_)Y nX dS )z
        Time format/rules:
            All datetime.datetime objects named with time
            All string in YYYYMMDD        named with date (following roipac)
        zopen {} file: {}c             S   s   g | ]\}}d  ||qS )z{}_{})rP   )rZ   r[   jrJ   rJ   rK   r\   j  s    z$ifgramStack.open.<locals>.<listcomp>c             S   s   g | ]}|j |jd   qS )iQ )rd   ra   )rZ   r[   rJ   rJ   rK   r\   k  s   )re   rU   
dropIfgramNr)   c                s>   g | ]6}t  | tjr | jd d jjfkr|qS )N)r   rp   r   r   rm   rn   )rZ   r[   )rM   rI   rJ   rK   r\   t  s    c                s   g | ]}| kr|qS rJ   rJ   )rZ   r[   )dsNamesrJ   rK   r\   w  s    c             S   s   g | ]}|t kr|qS rJ   )ifgramDatasetNames)rZ   r[   rJ   rJ   rK   r\   x  s    c                s   g | ]}d   |qS )z{}-{})rP   )rZ   r[   )r   rJ   rK   r\   }  s    F)r   REF_YREF_XZREF_LATZREF_LON)*rO   rP   rH   rQ   rR   rS   rG   rj   rk   read_datetimesrm   rn   ro   r   mDatessDates
date12Listrv   rw   sTimesmTimesr   tbaseIfgramrp   rq   r   pbaseIfgramr   r   r   r}   rl   ru   r   r   r   rz   refYrefXr   ZrefLatZrefLon)rI   rT   rJ   )r   r   rM   rI   rK   r~   \  sB    
"$zifgramStack.openc          	   C   s   t | jd&}t|j| _|d d d   }W d Q R X xB| j D ]4\}}y|d| j|< W qF   || j|< Y qFX qFW t	dd |D }|d | jd< |d | jd	< | jS )
NrU   r   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    z,ifgramStack.get_metadata.<locals>.<listcomp>r   r   r   r   )
rp   rq   rG   r   r   rz   flattenr   r   sorted)rI   rM   r   r   r   ru   rJ   rJ   rK   rj     s    zifgramStack.get_metadataFc          	      sz   t | jdV |d kr. fdddD d } | j\| _| _| _|r`t d d d  | _W d Q R X | j| j| jfS )NrU   c                s   g | ]}|   kr|qS rJ   )r   )rZ   r[   )rM   rJ   rK   r\     s    z(ifgramStack.get_size.<locals>.<listcomp>)r*   r4   r2   r   r   )	rp   rq   rG   r   	numIfgramrm   rn   rv   sum)rI   r   r   rJ   )rM   rK   rk     s    "zifgramStack.get_sizec          	      s   t  jd}|d dd }W dQ R X t|d  _tdd |dddf D  _tdd |ddd	f D  _	t fd
d jD  _
t fdd j	D  _dS )z4Read date1/2 into array of datetime.datetime objectsrU   r   N)r   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    z.ifgramStack.read_datetimes.<locals>.<listcomp>r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    rf   c                s   g | ]}t j| jqS rJ   )rV   rW   rX   rY   )rZ   r[   )rI   rJ   rK   r\     s    c                s   g | ]}t j| jqS rJ   )rV   rW   rX   rY   )rZ   r[   )rI   rJ   rK   r\     s    )rp   rq   rG   r   rt   rY   rv   rw   r   r   r   r   )rI   rM   r   rJ   )rI   rK   r     s    ""zifgramStack.read_datetimesr*   c          	      s  | j dd | jdd}|dkr(dg}nt|tr8|g}t| jdL}|d dd  |  }|rztd	 | j t
j| jt
jd	} fd
d|D }tdd |D r|r|d dd }qd|dd< nx|D ]}	d|||	< qW |dkr
dd| j| jf}t
|dk rB|||d |d |d |d f }
n.|dd|d |d |d |d f | }
tdd |
jD rt
|
}
W dQ R X |
S )aH  Read 3D dataset with bounding box in space
        Parameters: datasetName : string, to point to specific 2D dataset, e.g.:
                        unwrapPhase
                        coherence
                        connectComponent
                        ...
                        unwrapPhase-20161020_20161026
                        unwrapPhase-...
                        coherence-20161020_20161026
                        ...
                        ['unwrapPhase-20161020_20161026',
                         'unwrapPhase-20161020_20161101',
                         ...]
                    box : tuple of 4 int, for (x0,y0,x1,y1)
                    print_msg : bool
        Returns: data : 2D or 3D array
        Example:
            obj = ifgramStack('./inputs/ifgramStack.h5')
            obj.read(datasetName='unwrapPhase')
            obj.read(datasetName='coherence')
            obj.read(datasetName='unwrapPhase-20161020_20161026')
            obj.read(datasetName=['unwrapPhase-20161020_20161026',
                                  'unwrapPhase-20161020_20161101'])
        F)r   Nr*   rU   r   r   z!reading {} data from file: {} ...)re   c                s    g | ]}|  d  dd qS )r   r   )r   )rZ   r[   )r   rJ   rK   r\     s    z$ifgramStack.read.<locals>.<listcomp>c             s   s   | ]}| V  qd S )NrJ   )rZ   r[   rJ   rJ   rK   r`     s    z#ifgramStack.read.<locals>.<genexpr>r   T2   rf   r   r   c             s   s   | ]}|d kV  qdS )rf   NrJ   )rZ   r[   rJ   rJ   rK   r`     s    )rk   get_date12_listr   r   rp   rq   rG   r   rO   rP   rv   r   r   r   r   r   rn   rm   r  r   r   )rI   r   r   rT   r   r   rM   r   r   r   r   rJ   )r   rK   r     s>    


zifgramStack.readr-   c          	   C   s  |dkrd}|r$t d|| j nt d|| j |rdtj|rdt d|  t|j|d}nd}t	| jd}|| }	|	j
d }
tj|
tjd	}tj|
d
}xt|
D ]}|j|d d|d |
d |	||d |d |d |d f }|rtj||t|k< |dkr*tj||dk< |dk	rLd|||k< d|||k< |rbt|||< qt|||< qW |  W dQ R X || jfS )z Calculate the spatial average.Nr-   z/calculating spatial median of {} in file {} ...z-calculating spatial mean of {} in file {} ...zread mask from file: )r   rU   r   )re   )r   rf   z{}/{})r   r   r   )rO   rP   rG   rQ   rR   r   r   r   rp   rq   r   rv   r   r   r   r   r   r   r   r   	nanmedianr   rN   r   )rI   r   r   r   Z	useMedianr   r   r=   rM   dsetr   r   r   r[   r   rJ   rJ   rK   r     s:    
&

zifgramStack.spatial_averagec          	   C   s   t | jd6}|d d d  }|r@||d d d  d d f }W d Q R X tdd |d d df D }tdd |d d df D }d	d t||D }|S )
NrU   r   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   >  s    z/ifgramStack.get_date12_list.<locals>.<listcomp>r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   ?  s    rf   c             S   s   g | ]\}}d  ||qS )z{}_{})rP   )rZ   r[   r   rJ   rJ   rK   r\   @  s    )rp   rq   rG   rv   rw   r   )rI   r   rM   r   r   r   r   rJ   rJ   rK   r  9  s    &  zifgramStack.get_date12_listc          	   C   s   t | jd4}|d d d  }||d d d   d d f }W d Q R X tdd |d d df D }tdd |d d df D }d	d t||D }|S )
NrU   r   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   G  s    z4ifgramStack.get_drop_date12_list.<locals>.<listcomp>r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   H  s    rf   c             S   s   g | ]\}}d  ||qS )z{}_{})rP   )rZ   r[   r   rJ   rJ   rK   r\   I  s    )rp   rq   rG   rv   rw   r   )rI   rM   r   r   r   r   rJ   rJ   rK   get_drop_date12_listC  s    (  z ifgramStack.get_drop_date12_listc          	   C   s   t | jd6}|d d d  }|r@||d d d  d d f }W d Q R X dd |d d df D }dd |d d df D }ttt|| }|S )	NrU   r   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   Q  s    z-ifgramStack.get_date_list.<locals>.<listcomp>r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\   R  s    rf   )rp   rq   rG   r   r   set)rI   r   rM   r   r   r   ru   rJ   rJ   rK   rl   L  s    &zifgramStack.get_date_listc             C   s   | j dd |r4t| j|dd tj}td nXd| j krLtdn@td	| j
| j| | j|| j| j
| jd	 | j
d	 f|dd
}|S )a_  Get reference value
        Parameters: unwDatasetName : string, unwrapPhase, or unwrapPhase_unwCor
                    skip_reference : bool, skip reference value (for simulation only)
                    dropIfgram : bool, skip ifgrams marked as dropped or not
        Returns:    ref_phase : 1D np.array in size of (num_ifgram,) in float32
        F)rT   )r   r   zIskip checking reference pixel info - This is for offset and testing ONLY.r   zCNo REF_X/Y found!
run reference_point.py to select reference pixel.z1reference pixel in y/x: ({}, {}) from dataset: {}rf   )r   r   r   rT   )r~   rv   r   rk   r   rO   rz   r   
ValueErrorrP   r   r   r   )rI   ZunwDatasetNameZskip_referencer   Z	ref_phaserJ   rJ   rK   get_reference_phaseV  s    

zifgramStack.get_reference_phasec          	      s*  | j dd t| jd |dkr< fdddD d }td	|  | }tj|jd
d tj	d}tj|jd tj	d}|r| j
}t|}t|d }tj|d}	xbt|D ]V}
|	j|
d
 d|
d
 |d |||
 ddddf }d||dk< d|t|< qW |	  W dQ R X |S )z|Return the common mask of pixels with non-zero value in dataset of all ifgrams.
           Ignoring dropped ifgrams
        F)rT   rU   Nc                s   g | ]}|   kr|qS rJ   )r   )rZ   r[   )rM   rJ   rK   r\   r  s    z,ifgramStack.nonzero_mask.<locals>.<listcomp>)r.   r*   r   z:calculate the common mask of pixels with non-zero {} valuerf   r   )re   )r   z{}/{})r   g        )r~   rp   rq   rG   rO   rP   rv   onesr   r   r   r  wherer   r   r   r   isnanrN   )rI   r   rT   r   r  r=   ZdropIfgramFlagZnum2readZidx2readr   r[   r   rJ   )rM   rK   nonzero_maskk  s*    
zifgramStack.nonzero_mask   c          
   C   s  | j dd |d krd}td|| j tj| jtjd}|r\| j}t	|dkr\t
dd|krd	t| jd
  dtj  }tj| jtjdd }|| }t| jd"}|| }d }	d|kr.| jd k	r.d| j  kr| jkr.n n>| jd k	r.d| j  kr| jkr.n n||| j| jf }	tj|tjd| j | j d }
tt|
d |d  }tt| j| d d }tt| j| }tj|jdd tjd}tj |d}xt!|D ]}|| }t"|| | j}|j#|d d|| jd |d d ||d d f | }d|kr|	d k	r`|t$|	%d	ddd|jd |jd f8 }x>t!|jd D ],}||d d d d f  |||  9  < qpW tj&|dd|||d d f< qW |'  W d Q R X |S )NF)rT   r-   z3calculate the temporal average of {} in file {} ...)re   g        zMALL interferograms are marked as dropped, can not calculate temporal average.r*   r   Z
WAVELENGTHg      @g     v@rU   r   r  r   i   @
   rf   )r   zlines {}/{})r   r   )r   )(r~   rO   rP   rG   rv   r
  r   r   r   ry   	Exceptionr   rz   pirw   r   r   rp   rq   r   rn   r   rm   r  r   r   ceilrintr   r   r   r   r   r   minr   tiler   r   rN   )rI   r   r   
max_memoryZifgram_flagZphase2ranger{   rM   r  Zref_valZds_sizeZnum_stepZrow_stepr   r   r[   Zr0Zr1r   r   rJ   rJ   rK   r     sN    
*, 

,,$zifgramStack.temporal_averagec             C   s   |   }| j|ddd }tj|jd tjd}xNt|jd D ]<}||d d f }t|dkd t|dkd  ||< q@W t|S )Nr   )refDate)re   rf   r   )	r  get_design_matrix4timeseriesrv   r   r   r   r   r  max)rI   date12_listAZnum_connr[   ZAirJ   rJ   rK   get_max_connection_number  s    ,z%ifgramStack.get_max_connection_numberc          	   C   s  t | } dd | D }dd t|D }tttj|}t|d}t|}g }x|D ]\}}}	||f}
||	f}||	f}y||
 }|| }|| }W n t	k
r   wZY nX t
j|t
jd}d||< d||< d||< || qZW t|d	krtd
|  dS t
|t
jS )a  Generate the design matrix of ifgram triangle for unwrap error correction using phase closure
        Parameters: date12_list : list of string in YYYYMMDD_YYYYMMDD format
        Returns:    C : 2D np.array in size of (num_tri, num_ifgram) consisting 0, 1, -1
                        for 3 SAR acquisition in t1, t2 and t3 in time order,
                        ifg1 for (t1, t2) with 1
                        ifg2 for (t1, t3) with -1
                        ifg3 for (t2, t3) with 1
        Examples:   obj = ifgramStack('./inputs/ifgramStack.h5')
                    date12_list = obj.get_date12_list(dropIfgram=True)
                    C = ifgramStack.get_design_matrix4triplet(date12_list)
        c             S   s   g | ]}t |d qS )_)tupler   )rZ   r   rJ   rJ   rK   r\     s    z9ifgramStack.get_design_matrix4triplet.<locals>.<listcomp>c             S   s   i | ]\}}||qS rJ   rJ   )rZ   idxZifgrJ   rJ   rK   
<dictcomp>  s    z9ifgramStack.get_design_matrix4triplet.<locals>.<dictcomp>r   )re   rf   r   r   z9
WARNING: No triangles found from input date12_list:
{}!
N)r   	enumerater   r  	itertoolschainfrom_iterablecombinationsr   KeyErrorrv   r   int8appendrO   rP   stackastyper   )r  Zdate12_tuplesZ
ifg_to_idxr   Zclosure_listMZC_listZdate1Zdate2Zdate3Zifg12Zifg23Zifg13Zidx12Zidx23Zidx13rowrJ   rJ   rK   get_design_matrix4triplet  s4    z%ifgramStack.get_design_matrix4tripletc                s  t | } dd | D }dd | D }tt t|| t| }t}td  t fddD }dd ||d  D }tj|tjdd }t	||ftj}t	||ftj}	xtt
|D ]h}
fd	d| |
 d
D \}}d||
|f< d||
|f< ||d |d  |||  |	|
||f< qW |dkr|dkrNd }|r|}t|ddd|f |dd|d df f}|	ddddf }	||	fS )a  Return design matrix of the input ifgramStack for timeseries estimation
        Parameters: date12_list - list of string in YYYYMMDD_YYYYMMDD format
                    refDate     - str, date in YYYYMMDD format
                                  set to None for the 1st date
                                  set to 'no' to disable reference date
        Returns:    A - 2D array of float32 in size of (num_ifgram, num_date-1)
                    B - 2D array of float32 in size of (num_ifgram, num_date-1)
        Examples:   obj = ifgramStack('./inputs/ifgramStack.h5')
                    A, B = obj.get_design_matrix4timeseries(obj.get_date12_list(dropIfgram=True))
                    A = ifgramStack.get_design_matrix4timeseries(date12_list, refDate='20101022')[0]
                    A = ifgramStack.get_design_matrix4timeseries(date12_list, refDate=0)[0] #do not omit the 1st column
        c             S   s   g | ]}| d d qS )r  r   )r   )rZ   r[   rJ   rJ   rK   r\     s    z<ifgramStack.get_design_matrix4timeseries.<locals>.<listcomp>c             S   s   g | ]}| d d qS )r  rf   )r   )rZ   r[   rJ   rJ   rK   r\     s    r   c                s   g | ]}t j| qS rJ   )rV   rW   rX   )rZ   r[   )date_formatrJ   rK   r\     s    c             S   s   g | ]}|j |jd   qS )iQ )rd   ra   )rZ   r[   rJ   rJ   rK   r\      s    )re   g     v@c                s   g | ]}  |qS rJ   )r   )rZ   r   )r   rJ   rK   r\   '  s    r  r   rf   noN)r   r   r  r   r   rt   rv   rw   r   r   r   r   r   r   )r  r  Zdate1sZdate2sZ
num_ifgramr   r   r{   r  Br[   ind1ind2Zind_rrJ   )r.  r   rK   r    s2     0


2z(ifgramStack.get_design_matrix4timeseriesc          	   C   s   t | jd.}|d dd }|r8||d dd  }W dQ R X | j|d}| |d }tj|jd d tjd}tj	j
||dd	d |dd< |S )
z[Get spatial perpendicular baseline in timeseries from ifgramStack, ignoring dropped ifgramsrU   r)   Nr   )r   r   rf   )re   )rcond)rp   rq   rG   r  r  rv   r   r   r   linalglstsq)rI   r   rM   r   r   r  ZpbaseTimeseriesrJ   rJ   rK   get_perp_baseline_timeseries;  s    z(ifgramStack.get_perp_baseline_timeseriesc          	      s    dkrdS | j dd}| j dd}ttt|t| }| krPtd dS t| jd}td| j td t	j
 fd	d
|D t	jd|d dd< xFtD ]>}|| krtd| tt || jd< td qW W dQ R X dS )z8Update dropIfgram dataset based on input date12List2DropNF)r   Tz^The same date12List2Drop / dropIfgram is already marked in the file, skip updating dropIfgram.zr+zopen file {} with r+ modez"update HDF5 dataset "/dropIfgram".c                s   g | ]}| kqS rJ   rJ   )rZ   r[   )date12List2DroprJ   rK   r\   Z  s    z2ifgramStack.update_drop_ifgram.<locals>.<listcomp>)re   r   z.update MODIFICATION_TIME in HDF5 dataset "/{}"ZMODIFICATION_TIMErf   )r  r   r   r  rO   rp   rq   rG   rP   rv   rw   r   r   r   r   timer   sleep)rI   r7  Zdate12ListAllZdate12ListKeptOldZdate12List2DropOldrM   r   rJ   )r7  rK   update_drop_ifgramJ  s"    *
zifgramStack.update_drop_ifgram)N)T)T)FN)r*   NTF)r-   NNFFN)T)F)r*   FF)NTT)r-   Tr  )N)T)r   r   r   r   rL   rN   r~   rj   rk   r   r   r   r  r  rl   r	  r  r   r  staticmethodr-  r  r6  r:  rJ   rJ   rJ   rK   r   J  s,   


5

F 
2

	




>
73
r   c               @   s    e Zd ZdddZdddZdS )r   Nc             C   s
   || _ d S )N)rG   )rI   rG   rJ   rJ   rK   rL   i  s    zsingleDataset.__init__c          	   C   sj   t | jd&}t| d }|| d d  }W d Q R X |d k	rf||d |d |d |d f }|S )NrU   r   rf   r   r   )rp   rq   rG   r   r   )rI   r   rM   r   r   rJ   rJ   rK   r   l  s    zsingleDataset.read)N)N)r   r   r   rL   r   rJ   rJ   rJ   rK   r   h  s   
r   c               @   sH   e Zd ZdZdddZdddZddd	Zd
d Zdd ZdddZ	dS )r   a3  
    Time-series object in HDF-EOS5 format for Univ of Miami's InSAR Time-series Web Viewer
        Link: http://insarmaps.miami.edu
    It contains a "timeseries" group and three datasets: date, bperp and timeseries.

    File structure: https://mintpy.readthedocs.io/en/latest/hdfeos5/#file_structure
    Nc             C   s<   || _ d| _ddddddddddddddddddd| _d S )Nr   ZobservationZqualityr   )r   r   r   r   r   r   r;   r=   r-   ZvarianceZuncertaintyr   r#   r%   r$   r&   r'   r)   )rG   rH   datasetGroupNameDict)rI   rG   rJ   rJ   rK   rL     s(    zHDFEOS.__init__Tc             C   s<   y*| j   |r(tdtj| j W n   Y nX d S )Nzclose timeseries file: {})rM   rN   rO   rP   rQ   rR   rS   rG   )rI   rT   rJ   rJ   rK   rN     s    
zHDFEOS.closec          	      s.  |r t d| jtj| j |   t| j	d | _
t| j	d | _g | _t| jd}d |  }dd |d d d  D | _|d	 d d  | _t| j| _|  j fd
d| jD 7  _x\dD ]T |  }xF| D ]:}t|| tjrt|| jdkr| jd | qW qW W d Q R X d S )Nzopen {} file: {}LENGTHWIDTHrU   z#HDFEOS/GRIDS/timeseries/observationc             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    zHDFEOS.open.<locals>.<listcomp>r   r)   c                s   g | ]}d   |qS )z{}/displacement-{})rP   )rZ   r[   )gnamerJ   rK   r\     s    )zHDFEOS/GRIDS/timeseries/qualityz HDFEOS/GRIDS/timeseries/geometryr   z{}/{})rO   rP   rH   rQ   rR   rS   rG   rj   r   rz   rm   rn   r}   rp   rq   ru   rr   r   r   r   r   r   r   r(  )rI   rT   rM   gr   rJ   )r?  rK   r~     s$    
"zHDFEOS.openc          	   C   s   t | jd"}t|j| _|d d d  }W d Q R X xB| j D ]4\}}y|d| j|< W qB   || j|< Y qBX qBW | j| jd< dd |D }d| j	 kr|d | jd< |
| jd | _| jS )	NrU   z(HDFEOS/GRIDS/timeseries/observation/dater   r   c             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    z'HDFEOS.get_metadata.<locals>.<listcomp>r   r   )rp   rq   rG   r   r   rz   r   r   rH   r   r   rs   )rI   rM   r   r   r   ru   rJ   rJ   rK   rj     s    zHDFEOS.get_metadatac          	   C   sD   t | jd*}|d }dd |d d d  D | _W d Q R X | jS )NrU   z#HDFEOS/GRIDS/timeseries/observationc             S   s   g | ]}| d qS )r   )r   )rZ   r[   rJ   rJ   rK   r\     s    z(HDFEOS.get_date_list.<locals>.<listcomp>r   )rp   rq   rG   ru   )rI   rM   r@  rJ   rJ   rK   rl     s    &zHDFEOS.get_date_listc       
   	      s  | j dd |dkr$dd| j| jg}|dkr8td g}nt|trH|g}t| jdH}|d 	dd  | j
  }|d|  }|rtd	 | j t|jd
kr|dd }nt|jdkr||d
 |d |d |d f }ntj|jd tjd} fdd|D }tdd |D r6d|dd< n x|D ]}	d|| j|	< q<W |||d
 |d |d |d f }tdd |jD rt|}W dQ R X |S )a  Read dataset from HDF-EOS5 file
        Parameters: self : HDFEOS object
                    datasetName : (list of) str
                    box : tuple of 4 int, for (x0, y0, x1, y1)
                    print_msg : bool
        Returns:    data: 2D or 3D array
        Example:    obj = HDFEOS('S1_IW1_128_0593_0597_20141213_20171221.he5')
                    obj.read(datasetName='displacement')
                    obj.read(datasetName='displacement-20150915')
                    obj.read(datasetName=['displacement-20150915',
                                          'displacement-20150921'])
                    obj.read(datasetName='incidenceAngle')
        F)rT   Nr   r   rU   r   zHDFEOS/GRIDS/timeseries/{}/{}z!reading {} data from file: {} ...rf   r   r   )re   c                s    g | ]}|  d  dd qS )r   r   )r   )rZ   r[   )r   rJ   rK   r\     s    zHDFEOS.read.<locals>.<listcomp>c             s   s   | ]}| V  qd S )NrJ   )rZ   r[   rJ   rJ   rK   r`     s    zHDFEOS.read.<locals>.<genexpr>Tc             s   s   | ]}|d kV  qdS )rf   NrJ   )rZ   r[   rJ   rJ   rK   r`     s    )r~   rn   rm   r   r   r   rp   rq   rG   r   r<  rP   rO   r   r   rv   r   r   r   ru   r   r   )
rI   r   r   rT   rM   Z	groupNamer   r   r   r   rJ   )r   rK   r     s:    

&
zHDFEOS.read)N)T)T)NNT)
r   r   r   r   rL   rN   r~   rj   rl   r   rJ   rJ   rJ   rK   r   x  s   


)rQ   r8  r"  rW   rV   rp   numpyrv   mintpy.utilsr   r   r   r   r
   r   r   r   r   r   ZdataTypeDictZtimeseriesKeyNamesr   r   r   ZdatasetUnitDictr   r   r   r   r   rJ   rJ   rJ   rK   <module>   s   
  :    "