o
    Bd`l                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZmZmZmZ ejZdZg dZdd Zd	d
 Zdd ZdddZdd ZdS )    N)linalg)HDFEOSclustergiantTimeseries
timeseries)ptimereadfile	time_func	writefilezmintpy.timeFunc.)	startDateendDateexcludeDate
polynomialperiodicstepDateexploguncertaintyQuantificationtimeSeriesCovFilebootstrapCountc                    s   t d d}tjjsd}t dj d n0t dj d tjj}tjj}||kr@d}t dj d n	t d	j d |dkrrtj t	 fd
dt
D rjd}t dt
 d nt dt
 d t d| d |S )Nzupdate mode: ONskiprunz1) output file z NOT found.z already exists.z-2) output file is NOT newer than input file: .z)2) output file is newer than input file: c                 3   s0    | ]}t t|  t| d kV  qdS )NoneN)strvarsget
key_prefix).0keyatrinps S/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/timeseries2velocity.py	<genexpr>>   s   . zrun_or_skip.<locals>.<genexpr>z63) NOT all key configuration parameters are the same: z23) all key configuration parameters are the same: zrun or skip: )printospathisfileoutfilegetmtimetimeseries_filer   read_attributeanyconfig_keys)r"   flagtitor#   r    r$   run_or_skip)   s(   r3   c                    s  t | jd }|dkrt| j}n|dkrt| j}n|dkr&t| j}ntd| |  tj	|j
| j| j| jd tj| jdrt | j\}}tj|dd	d
k}d
||j
|d < t|d
krtdt|   t|j
|  7  ttt   fdd|j
D | _tj fdd|j
D tjd| _td td|j  d|j
  td t!| jt!|j
krtd ntdt!| j d| j  td | S )aC  Read dates used in the estimation and its related info.

    Parameters: inps - Namespace
    Returns:    inps - Namespace, adding the following new fields:
                       date_list - list of str, dates used for estimation
                       dropDate  - 1D np.ndarray in bool in size of all available dates
    	FILE_TYPEr   r   r   z Un-recognized time-series type: )	date_list
start_dateend_dateexclude_dateZtimeseriesRg)      axisr   REF_DATEz"number of empty dates to exclude: c                    s   g | ]}| vr|qS r#   r#   r   iex_date_listr#   r$   
<listcomp>p   s    z"read_date_info.<locals>.<listcomp>c                    s   g | ]}| vqS r#   r#   r>   r@   r#   r$   rB   s   s    dtypez2--------------------------------------------------zdates from input file: 
z.using all dates to calculate the time functionz*dates used to estimate the time function: )"r   r-   r,   r   r   r   
ValueErroropenr   get_exclude_date_listdateListr   r   r   r'   r(   basename
startswithreadnpnansumindexsumr&   arraytolistsortedlistsetr5   bool_dropDatenumDatelen)r"   ftypeZts_objdatar!   r0   r#   r@   r$   read_date_infoJ   sB   	"
r\   c           ;   	   C   s
  t   }t| j}t|d t|d }}t| } t| j}t	| j}|
dd}d| vrJ| jsJ| jd | _td td| jd  d tj| | jd	}t|}	| jd | jd
 }
}t|}d|d< d|d< |
|d< ||d< |
 d| |d< | jr| jd |d< | jd |d< | jr| j|d< tdt  tD ]}tt| | |t| < qt|||fddd  \}}| jdkrtj||fd g|d< d|d< tj| j|||d | jrt|}dD ]}|| v r|| qt| jd }td| |ft	| jtj gtj|||fd gd}tj| j!||d ||	d  d | | d  }| jd!kr@|| j"|	 | | d  7 }tt#|d" | j$d#  }t%j&dd||f|d$d%d&\}}t'|D ]\}}|d |d  }|d" |d  }|| }|dkrtd'|d  d(| d) td*|  td+|  tj(|	|ft)d,}tj(|	|ft)d,}td-| j d. tj*| j|d/d }| jrtd0| j  | j+| j}|t,||d d d d f |j-d ddf8 }| jr7td1| jd  d2| jd  d3 | jd | jd | jd d | jd d f}tj*| j|d/d }|t,|.|j-d ddd|j-d |j-d f8 }|| j/d d d d f .|d
}|d d4krR|d59 }d } | jd6krtd7| j0 d. tj*| j0|d/d } t| j-d kr|| j-d k r| | j/d d d d d d f } | d d | j/d d d d f } | .||d
} n"t| j-d"kr|| j-d k r| | j/d d d d f } | .|d
} td8 tj1|dd9}!t2t3|! |!d:k}"~!t|d |d  t|d |d  }#}$d|$  kr|k rn nd|#  kr|k rn nd|"|#| |$ < |d d |"f }tt4|"}%t5|"d }&td;6|%||%| d<  |%dkr@qatd= | jd!krtd>6| j" tj78 }'tj(| j"|	|%ft)d,}(t9j:| j"d?})t;| j"D ]4}|'j<||d%d@}*|*=  tj>|||* ? ||* |dAd |(|< |)j@|d dB|d  dC| j" dD qn|)A  |(jBdd9.|	d
|d d |"f< |(jCdd9.|	d
|d d |"f< ~(tjD| j|| j|dE}+ntj>|| j||dA\}+|d d |"f< },| jd6krgt| j-d"krd%ndF}-dG}.|.|-rdHndI7 }.t|. tEF|+}/t9j:|%d?})t;|%D ]J}|&| }0|-r+| d d d d |0f n
tG| d d |0f }1tjEH|/|1|/jIg}2tJtG|2|d d |0f< |)j@|d dJ|d  dK|% dLdM q|)A  n4| jdkrtdN tEKtL|+jI|+}3|,.dd
||	  }4tJtLtG|3.d
d|4|d d |"f< |d |d" |d |d g}5t||||"dOd }6| jdkrtj(|t)d,|6d< tJ|,|6d |"< |6M D ]\}7}8tjN| j|8.|||7|5dP q| jr(d||d |d" |d |d g}5tjO||| ftjPtjd,}9|tL|+|d d |"f  |9d d |"f< tjN| j!|9.|||dQ|5dP qatQt   | dR\}}:tdS|dTdU|:dVdW | jS )XNLENGTHWIDTHCENTER_LINE_UTCr   r=   zHWARNING: No REF_DATE found in time-series file or input in command line.z  Set "--ref-date z" and continue.)r5   velocityr4   m/yearUNIT
START_DATEEND_DATE_DATE12REF_Yr9   REF_Xz1add/update the following configuration metadata:
)ds_shaperesiduem)metadatads_name_dictds_unit_dict)r=   S)dater   )rn   rm   r:      	bootstrap   i   @yT)box	num_split	dimension	print_msgz
------- processing patch z out of z --------------zbox width:  zbox length: rC   zreading data from file z ...)rv   zreferecing to date: zreferencing to point (y, x): (z, )mmgMbP?
covariancez0reading time-series covariance matrix from file z3skip pixels with zero/nan value in all acquisitionsr;   g        z2number of pixels to invert: {} out of {} ({:.1f}%)d   z.estimating time functions via linalg.lstsq ...zFestimating time functions STD with bootstrap resampling ({} times) ...)maxValue)sizereplace)modelr5   dis_tssecondsz
iteration z / )suffix)r   ref_dater   Fz0estimating time functions STD from time-serries zcovariance pixel-by-pixel ...zvariance pixel-by-pixel ...   /z pixels)everyr   zCestimating time functions STD from time-series fitting residual ...)mask)r[   datasetNameblockr   <   ztime used: z02.0fz mins z02.1fz secs.)Rtimer   r-   r,   intr\   rY   r5   rM   rQ   r   keysr   r&   r	   
inps2modelZget_num_paramdictref_yxr/   r   r   r   model2hdf5_datasetr   float32r
   layout_hdf5r*   Zsave_respoprD   string_Zres_filer   ceil	maxMemoryr   split_box2sub_boxes	enumeratezeros	DATA_TYPErL   rO   tileshapereshaperW   r   nanmeanmultiplyisnanrP   whereformatrandomdefault_rngr   progressBarrangechoicesortestimate_time_funcrR   updateclosemeanstdget_design_matrix4time_funcr   pinvdiag	multi_dotTsqrtinvdotitemswrite_hdf5_blockfullnandivmod);r"   
start_timer!   lengthwidthnum_datedatesr   r   	num_paramZdate0date1ZatrVr   rn   ro   ZatrRZ
date_digitZ	memoryAllnum_boxbox_listr?   rv   box_widbox_len	num_pixelrl   m_stdts_dataZref_indref_boxref_valZts_covts_stackr   ryrxZnum_pixel2invZidx_pixel2invrngZm_bootprog_barZboot_indGe2Z
covar_flagmsgZGplusidxZts_coviZm_covG_invZm_varr   ds_dictds_namer[   Zts_ressr#   r#   r$   run_timeseries2time_func   st  




"


.", *8


(  "
2(
,	

 (r   c           !         s  | d }t | d }t | d }t | d }	tdd | d  D }
i }i }i }|durV|jd	kr7|jd	 nd	}|d
|}|d
|}|du rRtj|tjd}n|	 }d}t
|d	 D ]_}|dkrid}d}n|d	krrd}d}n|dkr{d}d}n
d| }d| }|dur||| ddf ||< ||| ddf ||d < t|dg||< |||< t|dg||d < |||d < q^||d	 7 }t
|D ]}| d |  ddg} d	krdd |D }n dkrdd |D }n	 fdd|D }|durM||d|  ddf }||d|  d	 ddf }t|d |d  }tj|td}t|| dks<t|| || ||< t|||gD ]	\}}|||< qCt|dg||d < d||d < t|dg||d	 < d||d	 < q|d| 7 }t
|D ]E}d| d | }|dur||| ddf ||< ||| ddf ||d < t|dg||< d||< t|dg||d < d||d < qv||7 }t
|	D ]z}d | d | }|dur#tj|d	ddf tjd}tj|d	ddf d tjd}t
|d	 D ]}|||| ddf 7 }|||| ddf d 7 }q|||< t|||d < t|dg||< d!||< t|dg||d < d!||d < q||	7 }d}| d  D ]S}| d | D ]I}d| d"| d#}|dur}||| ddf ||< ||| ddf ||d < t|dg||< d||< t|dg||d < d||d < |d	7 }qSqK||
7 }d}| d$  D ]S}| d$ | D ]I} d$| d"|  d#}|dur||| ddf ||< ||| ddf ||d < t|dg||< d||< t|dg||d < d||d < |d	7 }qq|||fS )%a#  Prepare the estimated model parameters into a list of dicts for HDF5 dataset writing.
    Parameters: model        - dict,
                m            - 2D np.ndarray in (num_param, num_pixel) where num_pixel = 1 or length * width
                m_std        - 2D np.ndarray in (num_param, num_pixel) where num_pixel = 1 or length * width
                mask         - 1D np.ndarray in (num_pixel), mask of valid pixels
                ds_shape     - tuple of 2 int in (length, width)
    Returns:    ds_dict      - dict, dictionary of dataset values,     input for writefile.write_hdf5_block()
                ds_name_dict - dict, dictionary of dataset initiation, input for writefile.layout_hdf5()
                ds_unit_dict - dict, dictionary of dataset unit,       input for writefile.layout_hdf5()
    Examples:   # read input model parameters into dict
                model = read_inps2model(inps, date_list=inps.date_list)
                # for time series cube
                ds_name_dict, ds_name_dict = model2hdf5_dataset(model, ds_shape=(200,300))[1:]
                ds_dict = model2hdf5_dataset(model, m, m_std, mask=mask)[0]
                # for time series point
                ds_unit_dict = model2hdf5_dataset(model)[2]
                ds_dict = model2hdf5_dataset(model, m, m_std)[0]
    r   r   r   polylinec                 s   s    | ]	\}}t |V  qd S )N)rY   )r   r   valr#   r#   r$   r%     s    z%model2hdf5_dataset.<locals>.<genexpr>r   Nr9   r`   rC   r   	interceptrl   ra   rb   r:   accelerationzm/year^2polyzm/year^Std	AmplitudePhasec                 S      g | ]}d | qS )Zannualr#   r   xr#   r#   r$   rB         z&model2hdf5_dataset.<locals>.<listcomp>g      ?c                 S   r   )Z
semiAnnualr#   r   r#   r#   r$   rB     r   c                    s   g | ]
}d   d| qS )periodYr#   r   r   r#   r$   rB     s    radianzstep{}zvelocityPost{}zm/yrTauDr   )rY   rP   r   ndimr   r   rM   onesrV   flattenr   r   r   r   allarctan2zipr   rQ   r   r   )!r   rl   r   r   rj   rk   Zpoly_degZ
num_periodnum_stepZ	num_plineZnum_expr   rn   ro   r   p0r?   dsNameunitZdsNameSuffixesdsNamesZcoef_cosZcoef_sinZ
period_ampZ
period_phar[   velZvel_varjZ	exp_onsetZexp_tauZ	log_onsetZlog_taur#   r   r$   r     s   




  


r   c                 C   s   dS )a4   New function, just a place holder now, incomplete.

    Prepare the estimated model parameters into a list of dicts for HDF5 dataset writing.
    Parameters: model        - dict,
                m            - 2D np.ndarray in (num_param, num_pixel) where num_pixel = 1 or length * width
                m_std        - 2D np.ndarray in (num_param, num_pixel) where num_pixel = 1 or length * width
                mask         - 1D np.ndarray in (num_pixel), mask of valid pixels
                ds_shape     - tuple of 2 int in (length, width)
    Returns:    ds_dict      - dict, dictionary of dataset values,     input for writefile.write_hdf5_block()
                ds_name_dict - dict, dictionary of dataset initiation, input for writefile.layout_hdf5()
                ds_unit_dict - dict, dictionary of dataset unit,       input for writefile.layout_hdf5()
    Examples:   # read input model parameters into dict
                model = read_inps2model(inps, date_list=inps.date_list)
                # for time series cube
                ds_name_dict, ds_name_dict = model2hdf5_dataset(model, ds_shape=(200,300))[1:]
                ds_dict = model2hdf5_dataset(model, m, m_std, mask=mask)[0]
                # for time series point
                ds_unit_dict = model2hdf5_dataset(model)[2]
                ds_dict = model2hdf5_dataset(model, m, m_std)[0]

    # deformation model info
    poly_deg   = model['polynomial']
    num_period = len(model['periodic'])
    num_step   = len(model['stepDate'])
    num_exp    = sum(len(val) for key, val in model['exp'].items())

    Nr#   )r   rn   ro   r#   r#   r$   hdf5_dataset2model  s   r   )NNNNN)r'   r   numpyrM   scipyr   mintpy.objectsr   r   r   r   mintpy.utilsr   r   r	   r
   r   r   r   r/   r3   r\   r   r   r   r#   r#   r#   r$   <module>   s"   	!8  
B G