
    Zfi+                         d dl Z d dlZd dlmZ d dlmZ  ed      ZdZdZdZ	ddZ
dd	Zd
 ZddZedk(  r eej                  dd        yy)    N)get_template_content)	arg_utilsvelocityu  references:
  Efron, B., and R. Tibshirani (1986), Bootstrap methods for standard errors, confidence intervals,
    and other measures of statistical accuracy, Statistical Science, 54-75, doi:10.1214/ss/1177013815.
  Fattahi, H., and F. Amelung (2015), InSAR bias and uncertainty due to the systematic and stochastic
    tropospheric delay, J. Geophy. Res. Solid Earth, 120(12), 8758-8773, doi:10.1002/2015JB012419.
  Hetland, E., Musé, P., Simons, M., Lin, Y., Agram, P., & DiCaprio, C. (2012). Multiscale InSAR time
    series (MInTS) analysis of surface deformation. Journal of Geophysical Research: Solid Earth,
    117(B2), doi:10.1029/2011JB008731
a  example:
  timeseries2velocity.py timeseries_ERA5_demErr.h5
  timeseries2velocity.py timeseries_ERA5_demErr_ramp.h5 -t KyushuAlosDT73.txt
  timeseries2velocity.py timeseries.h5  --start-date 20080201  --end-date 20100508
  timeseries2velocity.py timeseries.h5  --ex exclude_date.txt

  # complex time functions
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --poly 3 --period 1 0.5 --step 20170910
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --poly 1 --exp 20170910 90
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --poly 1 --log 20170910 60.4
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --poly 1 --log 20170910 60.4 200 --log 20171026 200.7
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --poly 1 --polyline 20190101 20200501

  # uncertainty quantification of the estimated time functions
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --uq residue
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --uq covariance --ts-cov timeseriesCov.h5
  timeseries2velocity.py timeseries_ERA5_demErr.h5 --uq bootstrap
z-exclude_date.txt:
20040502
20060708
20090103
c           	         d}t         dz   t        z   dz   t        z   }t        j	                  d      d   }t        j                  |||||       }|j                  dd       |j                  d	d
dd       |j                  dddd       |j                  dddd       t        j                  |d      }|j                  d      }|j                  dddd       |j                  ddd d!       |j                  d"d#d$d%g d&t        z   '       |j                  d(d)      }|j                  d*d+d,d-d.h d/d01       |j                  d2d3d4d5       |j                  d6d7d8t        d9d:;       t        j                  |      }|j                  d<d=      }|j                  d>d?d@ddA       |j                  dBdCdDdEdFG       t        j                  |      }|S )HNz4Estimate velocity / time functions from time-series.
.)synopsisdescriptionepilog
subparserstimeseries_filez.Time series file for time function estimation.)helpz
--templatez-ttemplate_fileztemplate file with options)destr   z-oz--outputoutfilezoutput file namez--updateupdate_mode
store_truezEnable update mode, and skip estimation if:
1) output file already exists, readable and newer than input file
2) all configuration parameters are the same.)r   actionr   F)plotzDates of interestz-sz--start-date	startDatez'start date for time function estimationz-ez
--end-dateendDatez%end date for time function estimationz--exz	--ex-dateexcludeDate+zxdate(s) not included in time function estimation, i.e.:
--exclude 20040502 20060708 20090103
--exclude exclude_date.txt
)r   nargsdefaultr   zUncertainty quantification (UQ)z+Estimating the time function parameters STDz--uqz--uncertaintyuncertaintyQuantificationVALresidue>   r   	bootstrap
covariancezUncertainty quantification method (default: %(default)s).
residue    - STD from time series fitting residue (Fattahi & Amelung, 2015)
covariance - STD from time series covariance
bootstrap  - STD from bootstrap resampling (Efron & Tibshirani, 1986))r   metavarr   choicesr   z--ts-covz--ts-cov-filetimeSeriesCovFilezB4D time-series (co)variance file for time function STD calculationz--bcz--bootstrap-countbootstrapCounti  z>number of iterations for bootstrapping (default: %(default)s).)r   typer   r   zResidual filez4Save residual displacement time-series to HDF5 file.z
--save-resz--save_residualsave_resz8Save the residual displacement time-series to HDF5 file.z
--res-filez--residual-fileres_fileztimeseriesResidual.h5zJOutput file name for the residual time-series file (default: %(default)s).)r   r   r   )	REFERENCETEMPLATEEXAMPLE__name__splitr   create_argument_parseradd_argumentadd_reference_argumentadd_argument_groupDROP_DATE_TXTintadd_timefunc_argumentadd_memory_argument)r   r
   r   nameparserdateuqresids           W/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/cli/timeseries2velocity.pycreate_parserr<   6   sE   EH(4/'9F>>#r"D--xXfQ[]F )0`a
dGcd jy?QR

|M  N --f5AF $$%89Dd>D  Fd<iB  Df[}CQS::GH  I 
	"	"#DFs	tBOOFO2MW\%/Ua  b OOJ5H]  _OOF/6FSZ]Y  [ ,,V4F %%o7mnE	|%6ZP\V  X	|%6ZQhh  j **62FM    c                 &   t               }|j                  |       }ddlm}m} |j                  |j                        }|d   }|dvrt        d| d      |j                  rt        |j                  |      }|j                  dk(  rv|j                  d	k  rd
|_
        t        d       t        d       |j                  d	k7  s0|j                  s$|j                  s|j                   s|j"                  rlt%        d      |j                  dk(  rR|j&                  r)t(        j*                  j-                  |j&                        sd
|_
        t        d       t        d       |j.                  r|j1                  |      }|j3                  |j.                  d   |j.                  d	         dd \  }}	|c|	a||	g|_        t        d|j.                  d    d|j.                  d	    d       t        d|j4                  d    d|j4                  d	    d       |j                  dk  rt%        d|j                   d      |j6                  r|j                  dk(  rt%        d      t9        |j                        |_        t9        |j6                        |_        |j:                  sot(        j*                  j=                  t(        j*                  j?                  |j                              d   }
|
dv r|
jA                  d      d   }nd}d| d|_        |S ) zCommand line parser.)argsr   )readfileutils	FILE_TYPE)
timeseriesgiantTimeseriesHDFEOSzinput file is z, NOT timeseries!r       r   z0WARNING: bootstrapCount should be larger than 1!zUChange the uncertainty quantification method from bootstrap to residue, and continue.z:bootstrapping support polynomial with the order of 1 ONLY!r!   z.WARNING: NO time series covariance file found!zVChange the uncertainty quantification method from covariance to residue, and continue.N   z&input reference point in (lat, lon): (z, )z"corresponding   point in (y, x): (z--polynomial (z) can NOT be smaller than zero!z6--polyline is NOT supported when --polynomial is zero!)timeseriesRgtimeseriesAzrC   r	    r   z.h5)!r<   
parse_argsmintpy.utilsr@   rA   read_attributer   	Exceptionr   read_template2inpsr   r%   print
polynomialperiodicstepDateexplog
ValueErrorr$   ospathisfileref_lalo
coordinate	geo2radarref_yxpolylinesortedr   splitextbasenamer-   )iargsr7   inpsr@   utatrftypecoordref_yref_xfbasesuffixs               r;   cmd_line_parserm   u   s    _F%(D 3 
!
!$"6"6
7C E??./@ABB !$"4"4d; %%4!#-6D*DEijOOq DMMT]]dhhRVRZRZYZZ		'	'<	7%%RWW^^D<R<R-S-6D*BCjk }}c"t}}Q'7q9IJ2ANu!2 %.DK:4==;K:LBt}}]^O_N``abc6t{{1~6FbUVHXXYZ[ >$//)::YZ[[}}A-QRR 4==)DM4==)DM <<  !1!1$2F2F!GHK44[[.r2FF!&-Kr=   c                 D   t        dt        j                  j                  |       z          ddlm}m}m} t        |      }|j                  |j                  ddg      }|j                  |      }d}t        |j                               D cg c]  }||z   |j                         v s| }	}|	D ]0  }
|||
z      }|s|
dv r|j                  |      ||
<   (|
d	v r$|j                  |j                  d
            ||
<   P|
dv r<|j!                  dd
      j                  d
      D cg c]  }t#        |       c}||
<   |
dv r%|j!                  dd
      j                  d
      ||
<   |
dv rU|j!                  dd      j!                  dd      }|j                  d      D cg c]  }|j                  d
       c}||
<   |
dv r|||
<   |
dv s#t%        |      ||
<   3 d}
|
|j                         v r||
   rt#        ||
         |_        |S c c}w c c}w c c}w )z.Read input template file into inps.excludeDatez!read options from template file: r   )ptimer@   rA   [])
skip_charszmintpy.timeFunc.)r   r   )r   ,)rS   ;)rT   )rU   rV   /|)r   r$   )rR   r%   zmintpy.compute.maxMemory)rQ   rX   rY   rb   rM   ro   r@   rA   varsread_templater   check_template_auto_valuelistkeysyyyymmddr-   replacefloatr3   	maxMemory)r   rd   ro   r@   re   iDicttemplate
key_prefixikey_listkeyvaluexs                r;   rP   rP      s   	
-bgg.>.>}.M
MN99JE%%d&8&8c3Z%PH++H5H $J

-QaA1PQHQ (C(.."^^E2c
'"^^EKK,<=c
$05c#0F0L0LS0QR1eAhRc
$"]]3s399#>c
&c#.66s3?49KK4DEqaggclEc
JJ"c
88 Zc
/(2 %C
hmmo(3-x}-K= R S Fs   H)H(H#Hc                 j    t        |       }ddlm}m} |j                  r ||      dk(  ry  ||       y )Nr   )run_or_skiprun_timeseries2time_funcskip)rm   mintpy.timeseries2velocityr   r   r   )rc   rd   r   r   s       r;   mainr      s5    % D K-7 T"r=   __main__rF   )N)rX   sysmintpy.defaults.templater   rM   r   r*   r)   r+   r2   r<   rm   rP   r   r,   argv r=   r;   <module>r      sm    
 
 9 "  
+	&<~EP*\#& z!" r=   