U
    bb                     @   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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lmZ d dlZ	ded d edd edd ZdZd	Zd
d ZdddZdddZdd ZG dd dZdddZe dkr
eej!dd  dS )    N)sensorcluster	RAMP_LIST)readfile	writefileutils)	STEP_LISTa=  Command line options for steps processing with names are chosen from the following list:

{}
{}
{}

In order to use either --start or --dostep, it is necessary that a
previous run was done using one of the steps options to process at least
through the step immediately preceding the starting step of the current run.
      a=  example:
  smallbaselineApp.py                         #run with default template 'smallbaselineApp.cfg'
  smallbaselineApp.py <custom_template>       #run with default and custom templates
  smallbaselineApp.py -h / --help             #help
  smallbaselineApp.py -H                      #print    default template options
  smallbaselineApp.py -g                      #generate default template if it does not exist
  smallbaselineApp.py -g <custom_template>    #generate/update default template based on custom template
  smallbaselineApp.py --plot                  #plot results without run

  # Run with --start/stop/dostep options
  smallbaselineApp.py GalapagosSenDT128.template --dostep velocity  #run at step 'velocity' only
  smallbaselineApp.py GalapagosSenDT128.template --end load_data    #end after step 'load_data'
zreference:
  Yunjun, Z., H. Fattahi, and F. Amelung (2019), Small baseline InSAR time series analysis: 
  Unwrapping error correction and noise reduction, Computers & Geosciences, 133, 104331,
  doi:10.1016/j.cageo.2019.104331.
c                  C   s   t jdt jtd t d} | jdddd | jdd	d
ddd | jddddd | jddddd | jddddd | jddddd | dt}|jddd td! d"d# |jd$d%d&d td' d(d# |jd)d*d d+d, | S )-Nz;Routine Time Series Analysis for Small Baseline InSAR Stack
)descriptionformatter_classepilogcustomTemplateFile?z[custom template with option settings.
ignored if the default smallbaselineApp.cfg is input.)nargshelp--dirz
--work-dirworkDir./z'work directory, (default: %(default)s).)destdefaultr   -ggenerate_template
store_truez:generate default template (if it does not exist) and exit.)r   actionr   z-Hprint_templatez)print the default template file and exit.-vz	--versionzprint software version and exit)r   r   --plotplotz5plot results [only] without running smallbaselineApp.z#steps processing (start/end/dostep)z--start	startStepSTEPr   z:start processing at the named step (default: %(default)s).)r   metavarr   r   z--endz--stopendStepz7end processing at the named step (default: %(default)s)z--dostepdoStepz%run processing at the named step only)r   r"   r   )	argparseArgumentParserRawTextHelpFormatter	REFERENCEEXAMPLEadd_argumentadd_argument_group	STEP_HELPr   )parserstep r0   M/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/smallbaselineApp.pycreate_parser<   s>    




r2   c              	   C   s  t  }|j| d}| r| ntjdd |_tjtjtj	d}|j
rt|d}| }W 5 Q R X z6ddlm} ddlm} | }|||d	d
d W n tk
r   t| Y nX td |jrttjj td |js6tjtj|s6|js6|  tt d}	|	d7 }	|	d7 }	td|	 |jrtj|jsXt|jtj|j|_tj|jtj|krd|_|jdgkrd}
td nd}
t|t |
d|_!|S )zCommand line parser.)args   Ndefaults/smallbaselineApp.cfgrr   )Console)Syntaxcfgr   )Zbackground_colorz$no template file found! It requires:z&
  1) input a custom template file, ORzN
  2) there is a default template 'smallbaselineApp.cfg' in current directory.zERROR: r   Tz*plot smallbaselineApp results without run.F)	step_list	plot_only)"r2   
parse_argssysargvospathjoindirnamemintpy__file__r   openreadZrich.consoler7   Zrich.syntaxr8   printImportErrorexitversionversion_descriptionr   isfilebasenamer   print_usager*   
SystemExitFileNotFoundErrorabspathread_inps2run_stepsr   runSteps)iargsr.   inpstemplate_fileflinesr7   r8   Zconsolemsgr;   r0   r0   r1   cmd_line_parse[   sR    



rZ   Fc           	      C   sF  dD ]<}t | | }|r||krd|}|d|7 }t|q| jrX| j| _| j| _|| j}|| j}||krd| j| j}t||||d  }| js|rg }t|dkr:t|dkrt	t
jj nt	t
jj t	dtj  t	dt  t	d	tjt| t	d
||d d  t	d |S )z(read/get run_steps from input arguments.)r    r#   r%   zInput step not found: {}z
Available steps: {}z2input start step "{}" is AFTER input end step "{}"r4   r   z--RUN-at-{}--zCurrent directory: {}z+Run routine processing with {} on steps: {}zRemaining steps: {}Nz2--------------------------------------------------)varsformat
ValueErrorr%   r    r#   indexr   lenrG   rC   rJ   rK   logodatetimenowr?   getcwdr@   rM   rD   )	rU   r:   r;   keyvaluerY   idx0idx1	run_stepsr0   r0   r1   rR      s6    


rR   c                    s   t jt jtjd}t j| d}t j|sNtd| t	
||  nNt|}t| t fdd| D rt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.
    r5   zsmallbaselineApp.cfgz/copy default template file {} to work directoryc                    s   g | ]}|   kqS r0   )keys).0rd   cdictr0   r1   
<listcomp>   s     z8get_the_latest_default_template_file.<locals>.<listcomp>zAobsolete default template detected, update to the latest version.)r?   r@   rA   rB   rC   rD   rL   rG   r\   shutilcopy2r   read_templateanyri   utupdate_template_file)work_dirZlfilecfileldictr0   rk   r1   $get_the_latest_default_template_file   s    

rw   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)r   r?   r@   rQ   r   rc   cwd)selfr   r   r0   r0   r1   __init__   s    zTimeSeriesAnalysis.__init__c                 C   st   d| _ | jr4t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 everytime.
        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@   splitextrM   rG   makedirsr   chdirrw   templateFile_read_templater{   r0   r0   r1   rE      s    zTimeSeriesAnalysis.openc              	   C   s  d | _ | jrtd| j t| j}ddddddddd}| D ] \}}|| krB|| ||< qBdD ](}|| krh||  dd||< qhd	| kr|d	 |d
< dD ]}|| kr|	| qt
|| _ td t| j| j | _dD ]}tj| j|}tj|dd | j| jfD ]b}|r tjtj|tj||ddddkr t|| tdtj|tj| q qtd| j t| j| _t| j| _| jd dkrdD ]4}| j| dkrd| jd< td|  qqd S )Nzread custom template file:autoyesno)defr   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}   F)out_filein_filecheck_readable	print_msgrunz&copy {} to {:<8} directory for backup.zread default template file:mintpy.geocode)mintpy.save.hdfEos5mintpy.save.kmzz*Turn ON mintpy.geocode in order to run {}.)customTemplater   rG   r   rp   itemsri   lowerreplacepopdictrr   rs   r   r?   r@   rA   r   r   run_or_skiprM   rn   ro   r\   templatecheck_template_auto_value)r{   rl   ZstandardValuesrd   re   backup_dirname
backup_dirtfiler0   r0   r1   r     sf        



z!TimeSeriesAnalysis._read_templatec                 C   s   |    d| jg}| jr$|| jg7 }| jr8|d| jg7 }tdd| tj| t	
| j tj| jdddd \}}}| jrtd	t	j|t	j|t	j| j t|| j t|| j |s| jd
d | jd
d td|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 Tr   r      z:updating {}, {} metadata based on custom template file: {}F)	print_aux)
normal_endz.step {}: NOT all required dataset found, exit.N)_copy_aux_filer   r   r   rG   rA   rC   	load_datamainr?   r   r   rr   check_loaded_datasetr\   r@   rM   add_attributer   plot_resultcloseRuntimeError)r{   	step_namerT   load_complete
stack_file	geom_filer0   r0   r1   run_load_dataG  s.    


z TimeSeriesAnalysis.run_load_datac                    s   t dr| jrt jt d| j dddg}tj fdd|D dd}|D ]D}tjt j||d	d
dkrPt	
|| j tdt j| qPd S )NZ
SCRATCHDIRzPROCESS/unavco_attributes.txtzPROCESS/bl_list.txtzSLC/summary*slc.jpgc                    s   g | ]}t j |qS r0   )r?   r@   rA   rj   iproj_dirr0   r1   rm     s     z5TimeSeriesAnalysis._copy_aux_file.<locals>.<listcomp>T)rQ   Fr   r   r   r   zcopy {} to work directory)r?   getenvr   r@   rA   rr   get_file_listr   rM   rn   ro   r   rG   r\   )r{   flistfnamer0   r   r1   r   x  s    z!TimeSeriesAnalysis._copy_aux_filec                    s  t j jdddd \}}tj jd} fdddD }zd	d |D d
 }W n   d}Y nX tj jd}dt|krtd	|| t j
||ddkrtj|dd\}}t|}	dD ]8}
|
|	krtd	|
 tj||
dd
 }d
||d
k< qd|d< tj|||d |d jg}tdd| tj| |d jdg}t|}tdd |D r|ddddd g7 }n&td!d |D r|dd"dd#d$g7 }td%d|  jd& rt j
||| jgdd'dkrtj| ntd( dS ))z>Modify network of interferograms before the network inversion.Fr   r4   r   zcoherenceSpatialAvg.txtc                    s   g | ]}t j j|d qS )znetwork.pdf)r?   r@   rA   r   r   r   r0   r1   rm     s     z?TimeSeriesAnalysis.run_network_modification.<locals>.<listcomp>) r   c                 S   s   g | ]}t j|r|qS r0   r?   r@   rL   r   r0   r0   r1   rm     s      r   NwaterMask.h5	waterMaskz#generate {} from {} for conveniencyr   r   r   )datasetName)latitude	longitudez)set pixels with 0 in {} to 0 in waterMask	FILE_TYPE)r   metadata-tz
modify_network.pyr   --nodisplayc                 s   s   | ]}d |  kV  qdS phaseNr   r   r0   r0   r1   	<genexpr>  s     z>TimeSeriesAnalysis.run_network_modification.<locals>.<genexpr>z-d	coherencer   z0.2z1.0c                 s   s   | ]}d |  kV  qdS offsetNr   r   r0   r0   r1   r     s     	offsetSNR020z
plot_network.pymintpy.plotr   z1mintpy.plot is turned OFF, skip plotting network.)rr   r   r   r?   r@   rA   r   get_dataset_listrG   r\   r   rF   r   writer   rC   modify_networkr   rq   r   Zplot_network)r{   r   r   r   Zcoh_txtZnet_figwater_mask_fileZ
water_maskatrZds_name_listds_namedsrT   dsNamesr0   r   r1   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 r|d	d
|dg}t	dd| t
j| tdd |D r|ddd
|dg}n"tdd |D r|ddd
|dg}t	dd| t
j| dS )z.Generate auxiliary files from ifgramStack fileFr   r4   maskConnComp.h5avgSpatialCoh.h5zavgSpatialSNR.h5c                 s   s   | ]}d |  kV  qdS r   r   r   r0   r0   r1   r     s     z>TimeSeriesAnalysis.generate_ifgram_aux_file.<locals>.<genexpr>z	--nonzero-o--update
generate_mask.pyr   c                 s   s   | ]}d |  kV  qdS r   r   r   r0   r0   r1   r     s     	--datasetr   c                 s   s   | ]}d |  kV  qdS r   r   r   r0   r0   r1   r     s     r   
temporal_average.pyN)rr   r   r   r   r   r?   r@   rA   rq   rG   rC   generate_maskr   temporal_average)r{   r   r   	mask_filecoh_fileZsnr_filerT   r0   r0   r1   generate_ifgram_aux_file  s     
z+TimeSeriesAnalysis.generate_ifgram_aux_filec                 C   sz   |    tj| jdddd \}}}tj| jd}|d| jd|g}|dk	rZ|d	|g7 }td
d| t	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
        Fr   r4      r   r   -cNz--lookupz
reference_point.pyr   )r   rr   r   r   r?   r@   rA   r   rG   rC   reference_pointr   )r{   r   r   r   lookup_filer   rT   r0   r0   r1   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
| tj	| tj| jd}|d|dddg}tdd
| t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
        Fr   r4   avgPhaseVelocity.h5r   unwrapPhaser   r   r   r   r   z--water-maskz--actionZ	calculate
unwrap_error_phase_closure.pyN)rr   r   r   r?   r@   rA   rG   rC   r   r   unwrap_error_phase_closure)r{   r   r   Zpha_vel_filerT   r   r0   r0   r1   run_quick_overview  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kr~tdd| t	j
| n|dkrtdd| t	j| nr|dkr|ddddg7 }tdd| t	j
| |ddddg7 }tdd| t	j| ntd|dS )zCorrect phase-unwrapping errorszmintpy.unwrapError.methodz)phase-unwrapping error correction is OFF.NFr   r4   r   r   r   z	--cc-maskZbridgingz
unwrap_error_bridging.pyr   Zphase_closurer   zbridging+phase_closurez-ir   r   unwrapPhase_bridging!unwrapPhase_bridging_phaseClosurezun-recognized method: {})r   rG   rr   r   r   r?   r@   rA   r   rC   Zunwrap_error_bridgingr   r   r]   r\   )r{   r   methodr   r   Ziargs_bridgeZiargs_closurer0   r0   r1   run_unwrap_error_correction   s:    

  z.TimeSeriesAnalysis.run_unwrap_error_correctionc                 C   sJ   t j| jddd }|d| jdg}tdd| t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
        Fr   r4   r   r   z
ifgram_inversion.pyr   N)
rr   r   r   r   rG   rA   rC   Zifgram_inversionr    generate_temporal_coherence_mask)r{   r   r   rT   r0   r0   r1   run_network_inversion&  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
kr|dt|kr||d|ddddg7 }t	dd| dd dD }t	d d}t j
||dddkrd}nVt	d|| t| t fdd|D rd}t	d| nt	d| t	d| |dkrdtj| i  |D ]}j|  |< qBt |  tt|d d k}	t	d!|	 tjd" }
|	|
k rd#t|
}|d$7 }|d%7 }|d&7 }t|d'S )(z4Generate reliable pixel mask from temporal coherenceFr      temporalCoherence.h5maskTempCoh.h5"mintpy.networkInversion.minTempCoh-mr   "mintpy.networkInversion.shadowMaskT
shadowMask--base--base-dataset--base-value1r   r   c                 S   s   g | ]}d  |qS )zmintpy.networkInversion.{})r\   r   r0   r0   r1   rm   J  s     zGTimeSeriesAnalysis.generate_temporal_coherence_mask.<locals>.<listcomp>)Z
minTempCohr  zupdate mode: ONskip)r   r   r   r   z?1) output file: {} already exists and newer than input file: {}c                 3   s(   | ] }t j|  |d kV  qdS )FalseN)strr   getr   r   r{   r0   r1   r   R  s     zFTimeSeriesAnalysis.generate_temporal_coherence_mask.<locals>.<genexpr>z82) NOT all key configuration parameters are the same: {}z42) all key configuration parameters are the same: {}zrun or skip: {}r   g        znumber of reliable pixels: {}z#mintpy.networkInversion.minNumPixelz,Not enough reliable pixels (minimum of {}). 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 subsetsN)rr   r   r   r?   r@   rA   r   r   r   rG   r   r\   read_attributerq   rC   r   r   r   npsumrF   floatintr   )r{   r   	tcoh_filer   tcoh_minrT   Zconfig_keysflagrd   	num_pixelmin_num_pixelrY   r0   r  r1   r  8  sL    



z3TimeSeriesAnalysis.generate_temporal_coherence_maskr   c                    s  t j|}t j|d}t j|d}t|}dddddg}t  |D ]}|}|dkr|d  drd		t j
|d
 }nR|dkr| d }|rd	t j
|d
 }n$|dkrP| d }| d }|r|dkrd	t j
|d
 }nX|dkrd	t j
|d
 }n6|dkr<d	t j
|d
 |}nd	|}	t|	n|dkr| d }|r|tkrd	t j
|d
 }n d	|}	|	d	t7 }	t|	n.|dkr| d }|rd	t j
|d
 }t }
||
d< ||
d< |
 |< qJ fdd|D }| fd d|D 7 }ttt|}t }
||
d< |
 d!< t }
 d! d d" |
d< |
 d#< |
 d$< d%| krt }
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_topographyPLATFORMenvz	{}_LOD.h5r   zmintpy.solidEarthTidesz	{}_SET.h5r   %mintpy.troposphericDelay.weatherModelheight_correlationz{}_tropHgt.h5gacosz{}_GACOS.h5pyapsz{}_{}.h5z0un-recognized tropospheric correction method: {}r   z
{}_ramp.h5z!un-recognized phase ramp type: {}z
available ramp types:
{}zmintpy.topographicResidualz{}_demErr.h5inputoutputc                    s   g | ]} | d  qS )r,  r0   rj   snamestepsr0   r1   rm     s     z>TimeSeriesAnalysis.get_timeseries_filename.<locals>.<listcomp>c                    s   g | ]} | d  qS )r+  r0   r-  r/  r0   r1   rm     s     reference_dater$   velocitygeocodeY_FIRST
geo/geo_{}hdfeos5)r?   r@   rQ   rA   r   r  r   r   
startswithr\   r   r]   r   sortedlistsetri   rB   rM   )r   rt   Zfname0Zfname1r   Zphase_correction_stepsr.  r  modelrY   r/   fnamesfdirfbaser0   r/  r1   get_timeseries_filenameo  s    












z*TimeSeriesAnalysis.get_timeseries_filenamec           	      C   s   t j| jddd }| | j| j| }|d }|d }||kr|||d|g}tdd| t j||d	d
krtj	
| n$t|}|dd}td| dS )zCorrect local oscillator drift (LOD).
        Automatically applied for Envisat data.
        Automatically skipped for all the other data.
        Fr   r  r+  r,  r   z
local_oscilator_drift.pyr   r   r   r%  Nz6No local oscillator drift correction is needed for {}.)rr   r   r   r?  r   rG   rA   r   rC   Zlocal_oscilator_driftr   r   r  r  r\   )	r{   r   r   r<  r   r   rT   r   satr0   r0   r1   %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r|d|d|dg}td	d
| t j||ddkrddlm	} |
| ntd dS )z Correct solid Earth tides (SET).Fr   r  r+  r,  r   r   r   z
solid_earth_tides.pyr   r   r   r   )solid_earth_tidesz No solid Earth tides correction.N)rr   r   r   r?  r   rG   rA   r   rC   rB  r   )r{   r   r   r<  r   r   rT   rB  r0   r0   r1    run_solid_earth_tides_correction  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rtj| q|
dkr^| jd }d|d|d|d|g}t	d t	d d| t j
||ddkrtj| q|
d!krd|d"|d|d#|	g}t	d$ t	d%| d&|}t j
|||gddkrtj|r||||kr||d|d'g}t	d( t	d)| t	d*d| tj| nt|d+krDd,d-lm} t	d.d| || nB|d/krvd,d0lm} t	d1d| || ntd2| d3nt	d4 d5S )6zCorrect tropospheric delays.Fr   r  r  r+  r,  z"mintpy.troposphericDelay.polyOrderr'  z#mintpy.troposphericDelay.weatherDirr   c                 S   s   t | }|d |d fS )NLENGTHWIDTH)r   r  )r   r   r0   r0   r1   get_dataset_size   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.pyr   r   r   r)  z!mintpy.troposphericDelay.gacosDirz-fr   z1tropospheric 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/{}.h5z--force,--------------------------------------------z'Use existed tropospheric delay file: {}z
diff.py)ZERA5r   )tropo_pyaps3z
tropo_pyaps3.py)ZMERRAZNARR)tropo_pyapsz
tropo_pyaps.pyzun-recognized dataset name: .z!No tropospheric delay correction.N)rr   r   r   r?   r@   rA   r?  r   upperrG   r   rC   Ztropo_phase_elevationr   Ztropo_gacosr\   rL   diffrI  rJ  r]   )r{   r   r   r   r<  r   r   Z
poly_ordertropo_modelZweather_dirr  rF  Z
tropo_lookZtropo_min_corrT   Z	GACOS_dir
tropo_filerI  rJ  r0   r0   r1   !run_tropospheric_delay_correction  s|    





      




 

z4TimeSeriesAnalysis.run_tropospheric_delay_correctionc                 C   s   | j d }| j d }| | j | j| }|d }|d }||krtd| |d|d|d|d	g}td
d| tj| ntd dS )z5Estimate and remove phase ramp from each acquisition.zmintpy.deramp.maskFiler   r+  r,  z,Remove for each acquisition a phase ramp: {}z-sr	  r   r   z
remove_ramp.pyr   zNo phase ramp removal.N)	r   r?  r   rG   r\   rA   rC   Zremove_rampr   )r{   r   r   r  r<  r   r   rT   r0   r0   r1   run_phase_deramping?  s    

z&TimeSeriesAnalysis.run_phase_derampingc                 C   s   t j| jddd }| | j| j| }|d }|d }||kr|d| jd|dg}| jd	 rh|d
|g7 }tdd| tj	
| ntd dS )zbstep - correct_topography
        Topographic residual (DEM error) correction (optional).
        Fr   r  r+  r,  r   r   r   z,mintpy.topographicResidual.pixelwiseGeometryr   z
dem_error.pyr   z#No topographic residual correction.N)rr   r   r   r?  r   r   rG   rA   rC   Z	dem_errorr   )r{   r   r   r<  r   r   rT   r0   r0   r1   #run_topographic_residual_correctionQ  s    
z6TimeSeriesAnalysis.run_topographic_residual_correctionc                 C   sF   d}t j|r:|d| jg}tdd| tj| ntd dS )z-Noise evaluation based on the phase residual.ztimeseriesResidual.h5r   z
timeseries_rms.pyr   z9No residual phase file found! Skip residual RMS analysis.N)	r?   r@   rL   r   rG   rA   rC   timeseries_rmsr   )r{   r   Zres_filerT   r0   r0   r1   run_residual_phase_rmsf  s    z)TimeSeriesAnalysis.run_residual_phase_rmsc                 C   sj   | j d r^d| jg}| | j | j| d }|D ]}||g7 }q0tdd| tj| ntd dS )z;Change reference date for all time-series files (optional).zmintpy.reference.dater   r+  z
reference_date.pyr   zNo reference date change.N)	r   r   r?  r   rG   rA   rC   r1  r   )r{   r   rT   in_filesr   r0   r0   r1   run_reference_dater  s    

z%TimeSeriesAnalysis.run_reference_datec           
      C   s"  |  | j| j| d }tj| jd}|d| jd|dg}tdd| tj	
| | jd  }tj| jd	|}tj|rtjtj|d
 }dtj|d
 |}tj| j|}|d| jd|dg}t|}	|d|	d d|	d |	d g7 }tdd| tj	
| dS )z7Estimate average velocity from displacement time-seriesr+  velocity.h5r   r   r   z
timeseries2velocity.pyr   r'  zinputs/{}.h5r   z{}{}.h5z
--ref-dateREF_DATEz--ref-yxREF_YREF_XN)r?  r   r   r?   r@   rA   r   rG   rC   Ztimeseries2velocityr   rL  r\   rL   r   rM   r   r  )
r{   r   ts_filevel_filerT   rN  rO  suffixZtropo_vel_filer   r0   r0   r1   run_timeseries2velocity  s"    
z*TimeSeriesAnalysis.run_timeseries2velocityc                 C   s  | j d rx| | j | j| d }t|}d| krnt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| tj| tj|dtj|}tj|d}	tj|d}
| j d }|	d|d|
g}| j d dkr<dt|kr<|d|ddddg7 }td d| t
j|
|	d!d"krvtj| ntd# ntd$ d%S )&z:geocode data files in radar coordinates into ./geo folder.r   r+  r4  geoTr}   Fr   r  r   r  r   rW  rG  r   z--outdirr   z
geocode.pyr   zgeo_{}geo_temporalCoherence.h5geo_maskTempCoh.h5r  r	  r   r
  r  r  r  r  r  r   r   r   z1dataset is geocoded, skip geocoding and continue.zgeocoding is OFFN)r   r?  r   r   r  ri   r?   r@   rA   r   rr   r   r   rG   rC   r3  r   r\   rM   r   r   r   )r{   r   r[  r   out_dirr   r   rU  rT   r  r   r  r0   r0   r1   run_geocode  s4    


zTimeSeriesAnalysis.run_geocodec                 C   s   | j d dkrtd tj| jd}t|}d| krLtj| jd}d	tj
|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rtj| ntd dS )zESave velocity file in geo coordinates into Google Earth raster image.r   Tz>creating Google Earth KMZ file for geocoded velocity file: ...rW  r4  zgeo/geo_velocity.h5z{}.kmzr   r   z
save_kmz.pyr   c                 S   s   g | ]}t j|r|qS r0   r   r   r0   r0   r1   rm     s    z<TimeSeriesAnalysis.run_save2google_earth.<locals>.<listcomp>z./geo/{}z./pic/{}NFr   r   z,save velocity to Google Earth format is OFF.)r   rG   r?   r@   rA   r   r   r  ri   r\   r   rM   r_   rr   r   rC   Zsave_kmzr   )r{   r   r\  r   Zkmz_filerT   r>  Z	kmz_filesr0   r0   r1   run_save2google_earth  s&    

z(TimeSeriesAnalysis.run_save2google_earthc                 C   s  | j d dkrx| | j | j| d }| jdk	r@t|| j tj| jd}tj| jd}tj| jd}tj	| jdd	d
 }d|krt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rH|
d nd}tj||||||gddkrtj| ntd dS )zUSave displacement time-series and its aux data in geo coordinate into HDF-EOS5 formatr   Tr+  Nr  r   r  Fr   r  r_  zgeo/geo_temporalCoherence.h5zgeo/geo_avgSpatialCoh.h5zgeo/geo_maskTempCoh.h5r5  rH  z--tcz--ascr	  r   r   z
save_hdfeos5.pyr   z{}_*.he5r   r   r   z+save time-series to HDF-EOS5 format is OFF.)r   r?  r   r   rr   r   r?   r@   rA   r   r\   rM   rG   r   r   r  r   get_unavco_mission_namer   r_   r   rC   Zsave_hdfeos5r   )r{   r   r[  r  Z	scoh_filer   r   rT   r   ZSATZhdfeos5_filesZhdfeos5_filer0   r0   r1   run_save2hdfeos5  sF    
     

z#TimeSeriesAnalysis.run_save2hdfeos5c                 C   sz  |D ]n}t d| |dkr,| | q|dkr@| | q|dkrT| | q|dkrh| | 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kr| | q|dkr
| | q|dkr | | q|dkr6| | q|dkrL| | q|dkrb| | q|dkr| | qdS )zrun the chosen steps.z5

******************** step - {} ********************r   r   r   Zquick_overviewcorrect_unwrap_errorZinvert_networkr   r!  r"  r#  r$  Zresidual_RMSr1  r2  r3  Zgoogle_earthr6  N)rG   r\   r   r   r   r   r  r  rA  rC  rP  rQ  rR  rT  rV  r^  rc  rd  rf  )r{   r0  r.  r0   r0   r1   r   
  sJ    





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}
rt	j
n|rt	j
|n|}|rt	j
|n|}|rt	j
|n|}|	rt	j
|	n|	}	dddddddg}dd|d|ddgddddddgd
dddddg|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d'dddddgd(dddddg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
|	d0g| d/| d1dd!gd2dd!gg}g }|D ]~}|d3 |dd	  }}|sXq4d4|krtt|}t|d3kr|D ]}||g|  qn||kr4|| q4d5d6 |D }td7d6 D 7 fd8d6|D }d9t|d:d;d<d=t|gfd>d6|D }t }d}| jd? }|r| jd@ }tj||}tjt|d|dA\}}} dB|  k rdCkrn ndDn
dD|dC  }t|tt|| d}|r|dkrt dE| ||dF fdGdH|D  n|D ]}tj| qt dI tdJ}|D ]}t ||
 qt dK tdL}|tdM7 }|tdN7 }|tt	j
|	dN7 }|D ]$}t!|t	j
|
t	j
"| qzt#t | dO\}}t dP|| dQ}|rt | d	S )Rz1Plot data files and save to figures in pic folderz=
******************** plot & save to pic ********************r'  zmintpy.plot.maxMemoryzmintpy.compute.maxMemoryzmintpy.plot.dpiFr   r4   Nr  r_  r   z--noaxisz-ucmz--wrapz--wrap-rangez-55rW  z--demz--maskr  r   grayr   r   r  zunwrapPhase-z--zero-maskcmyz
coherence-r   zconnectComponent-zunwrapPhase_bridging-zunwrapPhase_phaseClosure-z"unwrapPhase_bridging_phaseClosure-r   r   r   r  ztimeseries_*.h5ra  r`  zgeo_avgSpatialCoh.h5zgeo_velocity.h5r2  zgeo_timeseries*.h5z.h5znumInvIfgram.h5r   *c                 S   s    g | ]}t j|d  r|qS )r   r   rj   rT   r0   r0   r1   rm     s      z2TimeSeriesAnalysis.plot_result.<locals>.<listcomp>c                 S   s   g | ]}| d qS )r   r0   )rj   xr0   r0   r1   rm     s     c                    s4   g | ],}|d  ks,|d  kr|d  kr|qS )r   r4   r0   rm  )stack_dset_listr   r0   r1   rm     s
    z--dpiz--noverboser   r   z--memoryc                    s   g | ]} | qS r0   r0   rm  )
opt_commonr0   r1   rm     s     zmintpy.compute.clusterzmintpy.compute.numWorker)r   maxParallelNumg       @g      @g      ?z&parallel processing using {} cores ...)Zn_jobsc                 3   s   | ]} t jj|V  qd S ry   )rC   viewr   rm  )delayedr0   r1   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.1f} 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 Goodle Earth KMZ file in points for time-series (interactive)
        )$rG   r   rL  absr  r  rr   r   r   r?   r@   rA   relpathr8  globr_   appendr   r   r  timer   ZDaskClusterZformat_num_workercheck_parallelminmaxr\   rC   rr  r   rn   ro   moverM   divmod) r{   r   rN  Zmax_plot_memory
max_memoryZfig_dpir   r   r   Zgeo_dirZpic_dirZopt4tsZiargs_list0Z
iargs_listrT   r   r3   r<  
start_timeZrun_parallelZcluster_typenum_workers	num_coresParallelZplot_memoryZtfilesr   ZpfilesZpfilemsrY   r0   )rs  rp  ro  r   r1   r   D  s    
+




,

"zTimeSeriesAnalysis.plot_resultc                 C   s<   t d| j t| j |r8d}|d7 }|d7 }t | d S )NzGo back to directory:z1
################################################z.
   Normal end of smallbaselineApp processing!)rG   rz   r?   r   )r{   r   rY   r0   r0   r1   r     s    zTimeSeriesAnalysis.close)NN)r   )T)T) __name__
__module____qualname____doc__r|   rE   r   r   r   r   r   r   r   r  r  r  staticmethodr?  rA  rC  rP  rQ  rR  rT  rV  r^  rc  rd  rf  r   r   r   r0   r0   r0   r1   rx      s<   
D16&7[M&+:
 $rx   c                 C   s   t   }t| }t|j|j}|  |j|jd |jsR|j	d rZt
|jdkrZ|  |  tt   | d\}}td|| d S )Nr/  r   r4   rt  z'Time used: {:02.0f} mins {:02.1f} secs
)ry  rZ   rx   r   r   rE   r   rS   r   r   r_   r   r   r~  rG   r\   )rT   r  rU   appr  r  r0   r0   r1   r     s    r   __main__r4   )N)F)N)"r?   r=   rw  ry  ra   rn   r&   numpyr  rC   mintpy.objectsr   r   r   mintpy.utilsr   r   r   rr   Zmintpy.defaults.templater   Zmintpy.workflowr\   r-   r*   r)   r2   rZ   rR   rw   rx   r   r  r>   r0   r0   r0   r1   <module>   sF   	
 
 

A
-        

