o
    ld                     @   sz   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	m
Z
 d dlmZmZmZ dd ZG dd dZdd	 ZdS )
    N)	RAMP_LISTclustersensor)readfileutils	writefilec                    s   t jt jtjd}t j| d}t j|s)td| d t	||  |S t
|}t
| t fdd| D rPtd t	||  t|  |S )zGet the latest version of default template file.
    If an obsolete file exists in the working directory, the existing option values are kept.
    zdefaults/smallbaselineApp.cfgzsmallbaselineApp.cfgzcopy default template file  to work directoryc                    s   g | ]}|   vqS  )keys).0keycdictr	   P/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/smallbaselineApp.py
<listcomp>'       z8get_the_latest_default_template_file.<locals>.<listcomp>zAobsolete default template detected, update to the latest version.)ospathjoindirnamemintpy__file__isfileprintshutilcopy2r   read_templateanyr
   utupdate_template_file)work_dirZlfilecfileldictr	   r   r   $get_the_latest_default_template_file   s   

r#   c                   @   s   e Zd ZdZd;ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zed<ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd=d7d8Zd=d9d:ZdS )>TimeSeriesAnalysiszY Routine processing workflow for time series analysis of small baseline InSAR stacks
    Nc                 C   s*   || _ tj|| _tjt | _d S N)customTemplateFiler   r   abspathworkDirgetcwdcwd)selfr&   r(   r	   r	   r   __init__6   s   zTimeSeriesAnalysis.__init__c                 C   st   d| _ | jrtjtj| jd | _ td| j  tj| jdd t	| j td| j t
| j| _|   dS )zThe starting point of the workflow. It runs every time.
        It 1) grab project name if given
           2) go to work directory
           3) get and read template(s) options
        Nr   zProject name:Texist_okzGo to work directory:)projectNamer&   r   r   splitextbasenamer   makedirsr(   chdirr#   templateFile_read_templater+   r	   r	   r   open;   s   zTimeSeriesAnalysis.openc              	   C   s<  d | _ | jrwtd| j t| j}ddddddddd}| D ]\}}|| v r1|| ||< q!dD ]}|| v rH||  dd||< q4d	| v rU|d	 |d
< dD ]}|| v rd|	| qWt
|| _ td t| j| j | _ttj| j}| jrt|ttj| j}dD ]O}tj| j|}tj|dd dd | j| jfD }|D ]0}	tj|tj|	}
tj|
|	ddddkrt|	| tdjtj|	|tj|d qqtd| j t| j| _t| j| _| jd du rdD ]}| j| du rd| jd< td| d  d S qd S d S )Nzread custom template file:autoyesno)defdefaultyontruenofffalse)mintpy.derampmintpy.troposphericDelay.method-_	processorzmintpy.load.processor)SUBSET_XMINSUBSET_YMINz6update default template based on input custom template)inputspicTr-   c                 S   s   g | ]}|r|qS r	   r	   r   xr	   r	   r   r          z5TimeSeriesAnalysis._read_template.<locals>.<listcomp>F)in_filereadable	print_msgrunz-copy {f:<{l}} to {d:<8} directory for backup.)fldzread default template file:mintpy.geocode)mintpy.save.hdfEos5mintpy.save.kmzz'Turn ON mintpy.geocode in order to run .)customTemplater&   r   r   r   itemsr
   lowerreplacepopdictr   r   r4   lenr   r   r1   maxr   r(   r2   run_or_skipr   r   formattemplatecheck_template_auto_value)r+   r   ZstandardValuesr   valueflenbackup_dirname
backup_dirtfilestfileout_filer	   r	   r   r5   R   sj   


z!TimeSeriesAnalysis._read_templatec           
      C   s   |    d| jg}| jr|| jg7 }| jr|d| jg7 }tdd| ddl}|jj	| t
| j tj| jdddd	 \}}}}| jrod
t
j| j }||fD ]}	|	rnt| dt
j|	  t|	| j qVdS dS )aH  Load InSAR stacks into HDF5 files in ./inputs folder.
        It 1) copy auxiliary files into work directory (for Unvi of Miami only)
           2) load all interferograms stack files into mintpy/inputs directory.
           3) check loading result
           4) add custom metadata (optional, for HDF-EOS5 format only)
        
--templatez	--projectz
load_data.py r   NTrQ      z0updating metadata based on custom template file z for file: )_copy_aux_filer4   r&   r/   r   r   Zmintpy.cli.load_datacli	load_datamainr   r3   r(   r   check_loaded_datasetr   r1   add_attributerZ   )
r+   	step_nameiargsr   
stack_file	geom_filerF   ion_filemsgfnamer	   r	   r   run_load_data   s(   
 z TimeSeriesAnalysis.run_load_datac                    s   t drK| jrMt jt d| j g d}tj fdd|D dd}|D ]'}tjt j||ddd	krJt	
|| j td
t j| d q'd S d S d S )NZ
SCRATCHDIR)zPROCESS/unavco_attributes.txtzPROCESS/bl_list.txtzSLC/summary*slc.jpgc                    s   g | ]	}t j |qS r	   )r   r   r   r   iproj_dirr	   r   r      s    z5TimeSeriesAnalysis._copy_aux_file.<locals>.<listcomp>T)r'   F)rO   rP   rR   zcopy r   )r   getenvr/   r   r   r   get_file_listrb   r1   r   r   r(   r   )r+   flistr}   r	   r   r   rq      s   z!TimeSeriesAnalysis._copy_aux_filec                    s  t j jdddd \}}tj jd} fdddD }d	d |D }tj jd
}dt|v rt j||ddkrt	d| d| d tj
|dd\}}t|}	dD ]}
|
|	v rvt	d|
 d tj
||
dd }d||dk< qYd|d< tj|||d |d jg}t	dd| ddl}|jj| |d jdg}t|}tdd |D r|g d7 }ntdd |D r|g d 7 }t	d!d|  jd" rt j||| jgdd#dkrddl}|jj| dS dS t	d$ dS )%z>Modify network of interferograms before the network inversion.Fro   N   zcoherenceSpatialAvg.txtc                    s   g | ]}t j j|d qS )znetwork.pdf)r   r   r   r(   r   r6   r	   r   r      s    z?TimeSeriesAnalysis.run_network_modification.<locals>.<listcomp>) rK   c                 S      g | ]
}t j|r|qS r	   r   r   r   rL   r	   r	   r   r      s    waterMask.h5	waterMaskrl   rO   rR   z	generate z from z for conveniency)datasetName)latitude	longitudezset pixels with 0 in z to 0 in waterMaskr   	FILE_TYPE)rl   metadata-tz
modify_network.pyrn   --nodisplayc                 s       | ]	}d |  v V  qdS phaseNr\   r   r	   r	   r   	<genexpr>       z>TimeSeriesAnalysis.run_network_modification.<locals>.<genexpr>)-d	coherence-vz0.2z1.0c                 s   r   offsetNr   r   r	   r	   r   r      r   )r   	offsetSNRr   020z
plot_network.pymintpy.plotrl   rO   rP   z1mintpy.plot is turned OFF, skip plotting network.)r   ru   r(   r   r   r   r   get_dataset_listrb   r   readr   writer4   Zmintpy.cli.modify_networkrr   modify_networkrt   r   rd   Zmintpy.cli.plot_networkZplot_network)r+   rw   ry   rz   Zcoh_txtZnet_figwater_mask_fileZ
water_maskatrZds_name_listds_namedsrx   r   dsNamesr	   r6   r   run_network_modification   sP   



z+TimeSeriesAnalysis.run_network_modificationc                 C   s  t j| jddd }t|}tj| jd}tj| jd}tj| jd}tdd |D rJ|d	d
|dg}t	dd| ddl
}|jj| tdd |D r\|ddd
|dg}ntdd |D rm|ddd
|dg}t	dd| ddl}|jj| dS )z.Generate auxiliary files from ifgramStack fileFro   r   maskConnComp.h5avgSpatialCoh.h5zavgSpatialSNR.h5c                 s   r   r   r   r   r	   r	   r   r     r   z>TimeSeriesAnalysis.generate_ifgram_aux_file.<locals>.<genexpr>z	--nonzero-o--update
generate_mask.pyrn   Nc                 s   r   r   r   r   r	   r	   r   r     r   	--datasetr   c                 s   r   r   r   r   r	   r	   r   r     r   r   
temporal_average.py)r   ru   r(   r   r   r   r   r   r   r   mintpy.cli.generate_maskrr   generate_maskrt   mintpy.cli.temporal_averagetemporal_average)r+   ry   r   	mask_filecoh_fileZsnr_filerx   r   r	   r	   r   generate_ifgram_aux_file  s"   
z+TimeSeriesAnalysis.generate_ifgram_aux_filec                 C   s   |    tj| jdddd \}}}tj| jd}|d| jd|g}|dur-|d|g7 }td	d
| ddl	}|j
j| dS )zSelect reference point.
        It 1) generate mask file from common conn comp
           2) generate average spatial coherence and its mask
           3) add REF_X/Y and/or REF_LAT/LON attribute to stack file
        Fro   N   r   r   -cz--lookupz
reference_point.pyrn   r   )r   r   ru   r(   r   r   r   r4   r   Zmintpy.cli.reference_pointrr   reference_pointrt   )r+   rw   ry   rF   lookup_filer   rx   r   r	   r	   r   run_reference_point   s   z&TimeSeriesAnalysis.run_reference_pointc                 C   s   t j| jddd }tj| jd}|ddd|dg}td	d
| ddl}|jj	
| tj| jd}|d|dddg}tdd
| ddl}|jj
| dS )a  A quick overview on the interferogram stack for:
            1) avgPhaseVelocity.h5: possible ground deformation through interferogram stacking
            2) numTriNonzeroIntAmbiguity.h5: phase unwrapping errors
                through the integer ambiguity of phase closure
        Fro   r   avgPhaseVelocity.h5r   unwrapPhaser   r   r   rn   Nr   z--water-maskz--action	calculate
unwrap_error_phase_closure.py)r   ru   r(   r   r   r   r   r   rr   r   rt   %mintpy.cli.unwrap_error_phase_closureunwrap_error_phase_closure)r+   rw   ry   Zpha_vel_filerx   r   r   r	   r	   r   run_quick_overview5  s   z%TimeSeriesAnalysis.run_quick_overviewc                 C   s*  | j d }|std dS tj| jddd }tj| jd}|d| jd	g}|d
|g }ddl	}ddl
}|dkrItdd| |jj| dS |dkr^tdd| |jj| dS |dkr|g d7 }tdd| |jj| |g d7 }tdd| |jj| dS td| )zCorrect phase-unwrapping errorszmintpy.unwrapError.methodz)phase-unwrapping error correction is OFF.NFro   r   r   rm   r   z	--cc-maskZbridgingz
unwrap_error_bridging.pyrn   Zphase_closurer   zbridging+phase_closure)-ir   r   unwrapPhase_bridging)r   r   r   !unwrapPhase_bridging_phaseClosurezun-recognized method: )rd   r   r   ru   r(   r   r   r   r4   Z mintpy.cli.unwrap_error_bridgingr   rr   Zunwrap_error_bridgingrt   r   
ValueError)r+   rw   methodry   r   Ziargs_bridgeZiargs_closurer   r	   r	   r   run_unwrap_error_correctionM  s0   
z.TimeSeriesAnalysis.run_unwrap_error_correctionc                 C   sT   t j| jddd }|d| jdg}tdd| ddl}|jj	| | 
  dS )	zInvert network of interferograms for raw phase time-series.
        1) network inversion --> timeseries.h5, temporalCoherence.h5, numInvIfgram.h5
        2) temporalCoherence.h5 --> maskTempCoh.h5
        Fro   r   r   r   z
ifgram_inversion.pyrn   N)r   ru   r(   r4   r   r   Zmintpy.cli.ifgram_inversionrr   Zifgram_inversionrt    generate_temporal_coherence_mask)r+   rw   ry   rx   r   r	   r	   r   run_network_inversiont  s   z(TimeSeriesAnalysis.run_network_inversionc                    s  t jjddd }tjjd}tjjd}jd }|d|d|g}jd	 d
u r>dt|v r>|d|ddddg7 }t	dd| dd dD }t	d d}t j
||dddkr`d}n,t	d| d|  t| t fdd|D rd}t	d|  nt	d|  t	d|  |dkrd d!l}|jj| i  |D ]	}	j|	  |	< qt |  tt|d  d"k}
t	d#|
  tjd$ }|
|k rd%t| d&}|d'7 }|d(7 }|d)7 }t	d*|  jdd+ t|d!S ),z4Generate reliable pixel mask from temporal coherenceFro      temporalCoherence.h5maskTempCoh.h5"mintpy.networkInversion.minTempCoh-mr   "mintpy.networkInversion.shadowMaskT
shadowMask--base--base-dataset--base-value1r   rn   c                 S   s   g | ]}d | qS )zmintpy.networkInversion.r	   r   r	   r	   r   r         zGTimeSeriesAnalysis.generate_temporal_coherence_mask.<locals>.<listcomp>)Z
minTempCohr   zupdate mode: ONskip)rl   rO   rQ   rR   z1) output file: z+ already exists and newer than input file: c                 3   s*    | ]}t j|  |d kV  qdS )FalseN)strrd   getr   r   r+   r	   r   r     s   ( zFTimeSeriesAnalysis.generate_temporal_coherence_mask.<locals>.<genexpr>z62) NOT all key configuration parameters are the same: z22) all key configuration parameters are the same: zrun or skip: r   Ng        znumber of reliable pixels: z#mintpy.networkInversion.minNumPixelz'Not enough reliable pixels (minimum of z). zTry the following:
zT1) Check the reference pixel and make sure it's not in areas with unwrapping errors
zG2) Check the network and make sure it's fully connected without subsetszERROR: )	print_aux)r   ru   r(   r   r   r   rd   r   r   r   rb   read_attributer   r   rr   r   rt   rv   npsumr   floatintplot_resultRuntimeError)r+   rz   	tcoh_filer   tcoh_minrx   Zconfig_keysflagr   r   	num_pixelmin_num_pixelr|   r	   r   r   r     sP   

z3TimeSeriesAnalysis.generate_temporal_coherence_mask./c                    s  t j|}t j|d}t j|d}t|}g d}t  |D ]}|}|dkr?|d  dr>t j	|d  d}n|dkrU| d	 }|rTt j	|d  d
}n|dkr| d }| d }|r|dkrst j	|d  d}nm|dkrt j	|d  d}n]|dkrt j	|d  d| d}nJd| }	t
|	n@|dkr| d }|r|tv rt j	|d  d}n&d| }	|	dt 7 }	t
|	n|dkr| d }|rt j	|d  d}t }
||
d< ||
d< |
 |< q" fd d!|D }| fd"d!|D 7 }ttt|}t }
||
d< |
 d#< t }
 d# d d$ |
d< |
 d%< |
 d&< d'| vrZt }
t j d# d d$ }t j d# d d$ }t j|d(| |
d< |
 d)<  S )*zGet input/output time-series filename for each step
        Parameters: template : dict, content of smallbaselineApp.cfg
        Returns:    steps    : dict of dicts, input/output filenames for each step
        timeseries.h5)correct_LODcorrect_SETcorrect_tropospherederampcorrect_topographyr   PLATFORMenvr   z_LOD.h5r   zmintpy.solidEarthTidesz_SET.h5r   rD   %mintpy.troposphericDelay.weatherModelheight_correlationz_tropHgt.h5gacosz	_GACOS.h5pyapsrF   .h5z.un-recognized tropospheric correction method: r   rC   z_ramp.h5zun-recognized phase ramp type: z
available ramp types:
r   zmintpy.topographicResidualz
_demErr.h5inputoutputc                       g | ]} | d  qS )r   r	   r   snamestepsr	   r   r   	  r   z>TimeSeriesAnalysis.get_timeseries_filename.<locals>.<listcomp>c                    r   )r   r	   r   r  r	   r   r   
  r   reference_datevelocitygeocodeY_FIRSTgeo/geo_hdfeos5)r   r   r'   r   r   r   r_   r\   
startswithr0   r   r   sortedlistsetr
   r   r1   )rd   r    Zfname0Zfname1r   Zphase_correction_stepsr   r   modelr|   stepfnamesfdirfbaser	   r  r   get_timeseries_filename  s   



z*TimeSeriesAnalysis.get_timeseries_filenamec           
      C   s   t j| jddd }| | j| j| }|d }|d }||krF||d|g}tdd| t j||d	d
krDddl}|j	j
| dS dS t|}|dd}	td|	 d dS )zCorrect local oscillator drift (LOD).
        Automatically applied for Envisat data.
        Automatically skipped for all the other data.
        Fro   r   r   r   r   z
local_oscilator_drift.pyrn   r   rR   r   Nr   z3No local oscillator drift correction is needed for rY   )r   ru   r(   r  rd   r   r   rb   Z mintpy.cli.local_oscilator_driftrr   Zlocal_oscilator_driftrt   r   r   r   )
r+   rw   rz   r  rO   rl   rx   r   r   satr	   r	   r   %run_local_oscillator_drift_correction   s   
z8TimeSeriesAnalysis.run_local_oscillator_drift_correctionc                 C   s   t j| jddd }| | j| j| }|d }|d }||krH|d|d|dg}td	d
| t j||ddkrFddl}|j	j
| dS dS td dS )z Correct solid Earth tides (SET).Fro   r   r   r   -gr   r   z
solid_earth_tides.pyrn   r   rR   r   Nz No solid Earth tides correction.)r   ru   r(   r  rd   r   r   rb   Zmintpy.cli.solid_earth_tidesrr   Zsolid_earth_tidesrt   r+   rw   rz   r  rO   rl   rx   r   r	   r	   r    run_solid_earth_tides_correction5  s   z3TimeSeriesAnalysis.run_solid_earth_tides_correctionc                 C   s  t j| jddd }tj| jd}| | j| j| }|d }|d }||kr\| jd }| jd  }| jd	 }	| jd
 }
dd }|
dkr| jd }| jd }|d|d|d|d|d|d|g}t	d t	dd| t j
||ddkrddl}|jj| dS dS |
dkr| jd }d|d|d|d |g}t	d! t	d"d| t j
||ddkrddl}|jj| dS dS |
d#krXd|d$|d|d%|	g}t	d& t	d'| d(| d)}t j
|||gddkrZtj|r||||kr||d|d*g}t	d+ t	d,|  t	d-d| ddl}|jj| dS |d.v r6t	d/d| ddl}|jj| dS |d0v rPt	d1d| ddl}|jj| dS td2| d3dS dS t	d4 dS )5zCorrect tropospheric delays.Fro   r   r   r   r   z"mintpy.troposphericDelay.polyOrderr   z#mintpy.troposphericDelay.weatherDirrD   c                 S   s   t | }|d |d fS )NLENGTHWIDTH)r   r   )r}   r   r	   r	   r   get_dataset_sizeT  s   
zNTimeSeriesAnalysis.run_tropospheric_delay_correction.<locals>.get_dataset_sizer   zmintpy.troposphericDelay.looksz'mintpy.troposphericDelay.minCorrelationr  z-pr   r   -lr   z>tropospheric delay correction with height-correlation approachz
tropo_phase_elevation.pyrn   r   rR   r   Nr   z!mintpy.troposphericDelay.gacosDirz-fz--dirz1tropospheric delay correction with gacos approachz
tropo_gacos.pyr   z--modelz-wzVAtmospheric correction using Weather Re-analysis dataset (PyAPS, Jolivet et al., 2011)zWeather Re-analysis dataset:z	./inputs/r   z--force,--------------------------------------------z%Use existed tropospheric delay file: z
diff.py)ZERA5z
tropo_pyaps3.py)ZMERRAZNARRz
tropo_pyaps.pyzun-recognized dataset name: rY   z!No tropospheric delay correction.)r   ru   r(   r   r   r   r  rd   upperr   rb   Z mintpy.cli.tropo_phase_elevationrr   Ztropo_phase_elevationrt   Zmintpy.cli.tropo_gacosZtropo_gacosr   Zmintpy.cli.diffdiffZmintpy.cli.tropo_pyaps3Ztropo_pyaps3Zmintpy.legacy.tropo_pyapslegacyZtropo_pyapsr   )r+   rw   rz   r   r  rO   rl   Z
poly_ordertropo_modelZweather_dirr   r  Z
tropo_lookZtropo_min_corrx   r   Z	GACOS_dir
tropo_filer	   r	   r   !run_tropospheric_delay_correctionF  s|   








 

z4TimeSeriesAnalysis.run_tropospheric_delay_correctionc           	      C   s   | j d }| j d }| | j | j| }|d }|d }||krFtd|  |d|d|d|d	g}td
d| ddl}|jj| dS td dS )z5Estimate and remove phase ramp from each acquisition.zmintpy.deramp.maskFilerC   r   r   z*Remove for each acquisition a phase ramp: z-sr   r   r   z
remove_ramp.pyrn   r   NzNo phase ramp removal.)	rd   r  r(   r   r   Zmintpy.cli.remove_ramprr   Zremove_ramprt   )	r+   rw   r   r   r  rO   rl   rx   r   r	   r	   r   run_phase_deramping  s   

z&TimeSeriesAnalysis.run_phase_derampingc                 C   s   t j| jddd }| | j| j| }|d }|d }||krI|d| jd|dg}| jd	 r4|d
|g7 }tdd| ddl}|j	j
| dS td dS )zbstep - correct_topography
        Topographic residual (DEM error) correction (optional).
        Fro   r   r   r   r   r   r   z,mintpy.topographicResidual.pixelwiseGeometryr  z
dem_error.pyrn   r   Nz#No topographic residual correction.)r   ru   r(   r  rd   r4   r   r   Zmintpy.cli.dem_errorrr   Z	dem_errorrt   r  r	   r	   r   #run_topographic_residual_correction  s   
z6TimeSeriesAnalysis.run_topographic_residual_correctionc                 C   sR   d}t j|r#|d| jg}tdd| ddl}|jj	| dS td dS )z-Noise evaluation based on the phase residual.ztimeseriesResidual.h5r   z
timeseries_rms.pyrn   r   Nz9No residual phase file found! Skip residual RMS analysis.)
r   r   r   r4   r   r   Zmintpy.cli.timeseries_rmsrr   timeseries_rmsrt   )r+   rw   Zres_filerx   r   r	   r	   r   run_residual_phase_rms  s   z)TimeSeriesAnalysis.run_residual_phase_rmsc                 C   sv   | j d r5d| jg}| | j | j| d }|D ]}||g7 }qtdd| ddl}|jj	| dS td dS )	z;Change reference date for all time-series files (optional).zmintpy.reference.dater   r   z
reference_date.pyrn   r   NzNo reference date change.)
rd   r4   r  r(   r   r   Zmintpy.cli.reference_daterr   r  rt   )r+   rw   rx   in_filesrO   r   r	   r	   r   run_reference_date  s   

z%TimeSeriesAnalysis.run_reference_datec                 C   s4  |  | j| j| d }tj| jd}|d| jd|dg}tdd| dd	l}|j	j
| | jd
  }tj| jd| d}tj|rtjtj|d }tj|d  | d}	tj| j|	}	|d| jd|	dg}t|}
|d|
d d|
d |
d g7 }tdd| |j	j
| d	S d	S )z7Estimate average velocity from displacement time-seriesr   velocity.h5r   r   r   z
timeseries2velocity.pyrn   r   Nr   zinputs/r   z
--ref-dateREF_DATEz--ref-yxREF_YREF_X)r  rd   r(   r   r   r   r4   r   Zmintpy.cli.timeseries2velocityrr   Ztimeseries2velocityrt   r  r   r0   r1   r   r   )r+   rw   ts_filevel_filerx   r   r!  r"  suffixZtropo_vel_filer   r	   r	   r   run_timeseries2velocity  s$   
z*TimeSeriesAnalysis.run_timeseries2velocityc                 C   s  | j d r| | j | j| d }t|}d| vrtj| jd}tj	|dd t
j| jddd	d
 \}}|dd|dg}|d|d| jd|dg }tdd| ddl}	|	jj| tj|dtj| }tj|d}
tj|d}| j d }|
d|d|g}| j d du rdt|v r|d|ddd d!g7 }td"d| t
j||
d#d$krddl}	|	jj| dS dS td% dS td& dS )'z:geocode data files in radar coordinates into ./geo folder.rV   r   r  geoTr-   Fro   r   r   r   r   r*  r  r   z--outdirr   z
geocode.pyrn   r   NZgeo_geo_temporalCoherence.h5geo_maskTempCoh.h5r   r   r   r   r   r   r   r   r   r   r   rR   z1dataset is geocoded, skip geocoding and continue.zgeocoding is OFF)rd   r  r(   r   r   r
   r   r   r   r2   r   ru   r4   r   Zmintpy.cli.geocoderr   r  rt   r1   r   rb   r   r   )r+   rw   r.  r   out_dirrz   r   r(  rx   r   r   r   r   r	   r	   r   run_geocode  s6   


zTimeSeriesAnalysis.run_geocodec           	      C   s   | j d du rytd tj| jd}t|}d| vr&tj| jd}tj	|d  d}|d	|g}td
d| tj
|}dd |d| d| fD }t|dkr^|d nd}tj||dddkrwddl}|jj| dS dS td dS )zESave velocity file in geo coordinates into Google Earth raster image.rX   Tz>creating Google Earth KMZ file for geocoded velocity file: ...r*  r  zgeo/geo_velocity.h5r   z.kmzr   z
save_kmz.pyrn   c                 S   r   r	   r   r   r	   r	   r   r   *  s    

z<TimeSeriesAnalysis.run_save2google_earth.<locals>.<listcomp>z./geo/z./pic/NFr   rR   z,save velocity to Google Earth format is OFF.)rd   r   r   r   r   r(   r   r   r
   r0   r1   r`   r   rb   Zmintpy.cli.save_kmzrr   Zsave_kmzrt   )	r+   rw   r/  r   Zkmz_filerx   r  Z	kmz_filesr   r	   r	   r   run_save2google_earth  s"   

 z(TimeSeriesAnalysis.run_save2google_earthc                 C   s  | j d du r| | j | j| d }| jdurt|| j tj| jd}tj| jd}tj| jd}tj	| jdd	d
 }d|v rltj| jd}tj| jd}tj| jd}tj| jdtj
| }td |d|d|d|d|d| jg}tdd| t|}t|}	t|	 d}
t|
dkr|
d nd}tj||||||gddkrddl}|jj| dS dS td dS )zUSave displacement time-series and its aux data in geo coordinate into HDF-EOS5 formatrW   Tr   Nr   r   r   Fro   r   r2  zgeo/geo_temporalCoherence.h5zgeo/geo_avgSpatialCoh.h5zgeo/geo_maskTempCoh.h5r  r  z--tcz--ascr   r  r   z
save_hdfeos5.pyrn   z_*.he5r   r   rR   z+save time-series to HDF-EOS5 format is OFF.)rd   r  r(   rZ   r   rv   r   r   r   ru   r1   r   r4   r   r   r   get_unavco_mission_namer   r`   rb   Zmintpy.cli.save_hdfeos5rr   Zsave_hdfeos5rt   )r+   rw   r.  r   Z	scoh_filer   rz   rx   r   ZSATZhdfeos5_filesZhdfeos5_filer   r	   r	   r   run_save2hdfeos56  sL   


z#TimeSeriesAnalysis.run_save2hdfeos5c                 C   sp  |D ]}t d| d |dkr| | q|dkr | | q|dkr*| | q|dkr4| | q|dkr>| | q|dkrH| | q|d	krR| | q|d
kr\| | q|dkrf| 	| q|dkrp| 
| q|dkrz| | q|dkr| | q|dkr| | q|dkr| | q|dkr| | q|dkr| | q|dkr| | qdS )zrun the chosen steps.z

******************** step - z ********************rs   r   r   Zquick_overviewZcorrect_unwrap_errorinvert_networkr   r   r   r   r   Zresidual_RMSr  r  r  Zgoogle_earthr	  N)r   r~   r   r   r   r   r   r  r  r#  r$  r%  r'  r)  r1  r6  r7  r9  )r+   r  r   r	   r	   r   rR   d  sL   
zTimeSeriesAnalysis.runTc           !         s  t d | jd  }tt| jd }tt| jd }t| jd }tj| jdddd	 \}}}t	j
| jd
}	t	j
| jd}
t	j
| jd}rTt	j
n|r^t	j
|n|}|rht	j
|n|}|rrt	j
|n|}|	r|t	j
|	n|	}	|
rt	j
|
n|
}
g d}dd|d|	gg dg d|g|gdddddgddgddddddgdddgddgddgd dgd!gg d"g d#d$g| d%g| t	j
|
d&dd'gt	j
|
d(dd'gt	j
|
d)dd'gt	j
|
d*d+gt	j
|
d,g| d+| d-ddgg d.g}|r||ddg|ddddddgg7 }g }|D ]@}|d/ |d0d }}|s2q d1|v rUtt|}t|d/krS|D ]}||g|  qGq ||vr_|| q d2d3 |D }td4d3 D 7 fd5d3|D }d6t|d7d8d9d:t|gfd;d3|D }t }d}| jd< }|r| jd= }tj||}tjt|d|d>\}}} d?|  k rd@krn ndAndA|d@  }t|tt|| d0}d/dl|r|d0krt dB| dC ||dD fdEdF|D  n|D ]
}jj| qt dG tdH}|D ]	}t || qt dI tdJ}|tdK7 }|tdL7 }|tt	j
|
dL7 }|D ]}t!|t	j
|t	j
"| qKt#t | dM\}}t dN|dOdP|dQdR dS} |rt |  dS dS )Tz1Plot data files and save to figures in pic folderz=
******************** plot & save to pic ********************r   zmintpy.plot.maxMemoryzmintpy.compute.maxMemoryzmintpy.plot.dpiFro   Nrp   r   r2  rK   )z--noaxisz-ucm--wrapz--wrap-rangez-55r*  z--dem--mask)r   r   grayr   r   r   )r   r   r?  r   r   r   zunwrapPhase-z--zero-maskr<  r   Zcmyz
coherence-r:   r   r   r   zconnectComponent-zunwrapPhase_bridging-zunwrapPhase_phaseClosure-z"unwrapPhase_bridging_phaseClosure-r   )r   r   r?  r   r   r   )r   r   r?  r   r   r   r   ztimeseries_*.h5r4  r?  r3  zgeo_avgSpatialCoh.h5zgeo_velocity.h5r  zgeo_timeseries*.h5r   )znumInvIfgram.h5r>  r:   r   r   *c                 S   s    g | ]}t j|d  r|qS )r   r   r   rx   r	   r	   r   r     s     z2TimeSeriesAnalysis.plot_result.<locals>.<listcomp>c                 S   s   g | ]}| d qS )rE   r	   rL   r	   r	   r   r      r   c                    s4   g | ]}|d  ks|d  kr|d  v r|qS )r   r   r	   rA  )stack_dset_listry   r	   r   r     s    z--dpiz--noverboser   r   z--memoryc                    s   g | ]} | qS r	   r	   rA  )
opt_commonr	   r   r   	  rN   zmintpy.compute.clusterzmintpy.compute.numWorker)rQ   maxParallelNumg       @g      @g      ?zparallel processing using z
 cores ...)Zn_jobsc                 3   s"    | ]} j jj|V  qd S r%   )rr   viewrt   rA  )delayedr   r	   r   r   "  s     z1TimeSeriesAnalysis.plot_result.<locals>.<genexpr>z&copy *.txt files into ./pic directory.z*.txtz,move *.png/pdf/kmz files to ./pic directory.z*.pngz*.pdfz*.kmz<   ztime used: 02.0f mins 02.1fz secs.a  Explore more info & visualization options with the following scripts:
        info.py                    # check HDF5 file structure and metadata
        view.py                    # 2D map view
        tsview.py                  # 1D point time-series (interactive)
        transect.py                # 1D profile (interactive)
        plot_coherence_matrix.py   # plot coherence matrix for one pixel (interactive)
        plot_network.py            # plot network configuration of the dataset
        plot_transection.py        # plot 1D profile along a line of a 2D matrix (interactive)
        save_kmz.py                # generate Google Earth KMZ file in raster image
        save_kmz_timeseries.py     # generate Google Earth KMZ file in points for time-series (interactive)
        )$r   rd   r  absr   r   r   ru   r(   r   r   r   relpathr  globr`   appendr   r   r   timer   DaskClusterformat_num_workercheck_parallelminra   Zmintpy.cli.viewrr   rE  rt   r   r   mover1   divmod)!r+   r   r!  Zmax_plot_memory
max_memoryZfig_dpirz   r   r{   r   Zgeo_dirZpic_dirZopt4tsZiargs_list0Z
iargs_listrx   r}   argsr  
start_timeZrun_parallelcluster_typenum_workers	num_coresParallelZplot_memoryrj   rk   ZpfilesZpfilemsr|   r	   )rF  r   rC  rB  ry   r   r     s   
*






, 

"zTimeSeriesAnalysis.plot_resultc                 C   s@   t d| j t| j |rd}|d7 }|d7 }t | d S d S )NzGo back to directory:z1
################################################z.
   Normal end of smallbaselineApp processing!)r   r*   r   r3   )r+   
normal_endr|   r	   r	   r   closeJ  s   zTimeSeriesAnalysis.close)NN)r   )T) __name__
__module____qualname____doc__r,   r7   r5   r~   rq   r   r   r   r   r   r   r   staticmethodr  r  r  r#  r$  r%  r'  r)  r1  r6  r7  r9  rR   r   r`  r	   r	   r	   r   r$   2   s>    
G*6'>[P(.
9 .r$   c                 C   s   t   }t| j| j}|  |j| jd | js%|jd r)t	| jdkr)|
  |  tt   | d\}}td|dd|dd	 d
S )z6Run the small baseline time series analsysis workflow.r  r   r   rG  zTime used: rH  rI  rJ  z secs
N)rO  r$   r&   r(   r7   rR   ZrunStepsplotrd   r`   r   r`  rU  r   )inpsrX  appr]  r^  r	   r	   r   run_smallbaselineAppV  s   ri  )rM  r   r   rO  numpyr   r   mintpy.objectsr   r   r   mintpy.utilsr   r   r   r   r#   r$   ri  r	   r	   r	   r   <module>   s&           ,