o
    <c	K                     @   s\  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 d dlm	Z
 d dlmZ d dlZd dlZd dlmZ d dlmZ d dlm  mZ d dlmZ d dlmZ d dlm  mZ d dlmZ dZd	Zd
Z dZ!ddddddZ"dddZ#dd Z$d.d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)d*Z-d.d+d,Z.e/d-kre.  dS dS )0    N)make_axes_locatable)
timeseries)datetime)GPS)utils)CGPSg      @g      $@g       @a  example:
   
    # only plot faults and reference points
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor b r --fstyle s d --refpoi refpoi_DT.shp --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
    
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor o m --fstyle s s --refpoi refpoi_DT.shp --vlim -0.02 0.02 --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
   
    # plot data using nonlinear colorbar
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor b r --fstyle s d --refpoi refpoi_DT.shp --nonlinear --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
    

    # plot shape file and gps velocity vector field
    # plot single gps site with extracting incidence and heading angle from geometreRadar 
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor o m --fstyle s s --refpoi refpoi_DT.shp --gps_putton --type NGPS --gps_name GV05 --geometry $SCRATCHDIR/BalochistanSenAT115/mintpy/inputs/geometryRadar.h5 --gpsdir /data/lxr/insarlab/GPS/ --scale 1 --scale_key 0.01 --vlim -0.02 0.02 --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
    
    # plot single gps site using typical incidence and heading angle for Seitinel1
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor o m --fstyle s s --refpoi refpoi_DT.shp --gps_putton --type CGPS --gps_name AHAQ --scale 1 --scale_key 0.01 --vlim -0.02 0.02 --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
    
    # plot multiple gps sites with extracting incidence and heading angle from geometreRadar 
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor o m --fstyle s s --refpoi refpoi_DT.shp --gps_putton --type NGPS --search --bbox 36 40 36 39 --geometry $SCRATCHDIR/BalochistanSenAT115/mintpy/inputs/geometryRadar.h5 --gpsdir /data/lxr/insarlab/GPS/ --scale 1 --scale_key 0.01 --vlim -0.02 0.02 --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 
    
    # plot multiple gps sites with extracting incidence and heading angle from geometreRadar 
    plot_geotiff.py geotiff_file --shpdir /data/lxrtest/Balochistan/shp/ --fault multifault.shp nearbyfault.shp --fcolor o m --fstyle s s --refpoi refpoi_DT.shp --gps_putton --type CGPS --search --bbox 30 35 101 106 --scale 1 --scale_key 0.01 --vlim -0.02 0.02 --outfile velocity --outdir /data/lxrtest/BalochistanSenAT/shp/ 

    # plot GPS data alone
    plot_geotiff.py velocity_201504_202010.tiff --gps_putton --type NGPS --search --bbox 37 43 -121 -115 --gpsdir /data/lxr/insarlab/GPS/ --scale 1 --scale_key 0.01 --vlim -0.02 0.02 --outfile velocity3 --outdir ./
blackredyelloworangemagenta)bryomsoliddashed)sdc                  C   s  t jdt jtd} | jddtdd | jdd}|jd	d
dtdd |jdddtdd |jdddtdd |jdddtdd |jdddtdd | jdd}|jddddd  |jd!d"tdd#d$ |jd%ddd&d  |jd'd(td)d*d+d, |jd-d.dtd/d |jd0d1dtd2d |jd3d4dtd5d |jd6d7dtd8d |jd9d:dtd;d | jd<d=d>td?d | jd@d}|jdAdddBd  | jdCdDdtdEd | jdFdGdtdHd | S )INzplot *.tiff)descriptionformatter_classepiloginput_geotiff   zgeotiff file. 
)nargstypehelpz4option for shape files (faults and reference point).)titlez--shpdirshpdirzdirectory of shp files. 
)destr   r   r   z--faultfault+zshp files of fault. 
z--fcolorfcolorzTdefine color for faults:b -> black; r -> red; y -> yellow;o -> orange; m-> magenta 
z--fstylefstylez6define line stype for faults:s -> solid; d -> dashed 
z--refpoirefpoi?zshp file of reference point. 
z*options for plot gps velocity vector fieldz--gps_putton
store_trueFz)whether plot gps velocity vector field. 
)actiondefaultr   z--typeGtypez3CGPS: China GPS database; NGPS: Nevada GPS database)r    r   r   r   z--searchz3whether search gps sites based on the given SNWE. 
z--bboxSNWE   )SNWEzBounding box of area to be geocoded.
Include the uppler left corner of the first pixeland the lower right corner of the last pixel)r    r   r   metavarr   z
--gps_namegps_namezgps site name. 
z
--geometrygeometryzgeometry data. 
z--gpsdirgpsdirz2directory to store Nevada Geodetic Lab gps data. 
z--scalescalezparameter for quiver.scalez--scale_key	scale_keyzparameter for quiverkey.U 
z--vlimvlim   z?max and min value for displacement to display. The unit is m. 
z?options for the colorbar: linear colorbar or nonlinear colorbarz--nonlinearzwhether use nonlinear colorbar.z--outdiroutdirzoutput directory.
z	--outfileoutfilezoutput file name.
)	argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentstradd_argument_groupfloatint)parserZ
shp_optionZ
gps_optionZcolorbar_option rE   H/home/exouser/operations/rsmas_insar/tools/MimtPy/mimtpy/plot_geotiff.pycreate_parser@   s:   rG   c                 C   s   t  }|j| d}|S )N)args)rG   
parse_args)iargsrD   inpsrE   rE   rF   cmd_line_parsez   s   rL   F      ?c                    s\   | \}}|d |g}|s||g}t tt | tt| t|d    fdd|D }|S )z.Get auto figure size based on input data shapeg      ?r   c                    s   g | ]}|   qS rE   rE   .0i	fig_scaleratiorE   rF   
<listcomp>       z$auto_figure_size.<locals>.<listcomp>)minmin_figsize_singlemax_figsize_singlemaxmax_figsize_height)shape	disp_cbarrS   lengthwidthZ
plot_shapefig_sizerE   rQ   rF   auto_figure_size   s   

r`   c                 C   .   t  }t| |D ]\}}t| }|||< q|S )zdefine fault color)dictzipfcolor_table)fcolors
shp_faultsfault_colorsr#   r!   colorrE   rE   rF   r#      
   
r#   c                 C   ra   )zdefind fault linestyle)rb   rc   flinestyle_table)Zfstylesrf   fault_linestylesr$   r!   stylerE   rE   rF   
flinestyle   ri   rm   c                 C   sf   t | d}|  |st|j|j|j}|}n|| |j}t	
|j|j|j||j|jgg}|S )zprocess single gps site)site)r   openutenu2losZvel_eZvel_nZvel_uZread_gps_los_velocityvel_losnparrayrn   site_latsite_lon)gpsnamer3   gps_objrr   los_vel	site_inverE   rE   rF   cgps_process   s   

"r{   c           
      C   s   t | |d}td|  |  |s&t|j|j|j}|j	}t
||}n||\}}|| |j}t
||j}t
||j}t|j|j|j|||gg}	|	S )zprocess ngps sites)rn   data_dirzprocess {} site)r   printformatro   rp   rq   dis_edis_ndis_udatesdis_velocityread_gps_los_displacementget_gps_los_velocityvelocityrs   rt   rn   ru   rv   )
rw   r3   gps_dirrx   dis_losr   ry   Ze_velZn_velrz   rE   rE   rF   ngps_process   s   
r   c                 C   sJ   dd | D }t |dkr t|}ttj||d }|S tj}|S )zcalculate velocityc                 S   s   g | ]}t |d qS )z%Y%m%d)dtstrftimerN   rE   rE   rF   rT      rU   z dis_velocity.<locals>.<listcomp>r8   r   )lenr   Z"get_design_matrix4average_velocityrs   dotlinalgpinvnan)r   dis	date_listAvelrE   rE   rF   r      s   
r   c              
   C   s|   | j d dkrt| j\}}}n| j d dkr!t| j\}}}tttt|gt|gft|gf}|S )z$search gps sites based on given SNWEr   NGPSr   )	r*   gps
search_gpsr+   cgpsrs   	transposevstackrt   )rK   
site_names	site_lats	site_lonssites_baseinforE   rE   rF   search_gpssites   s   6r   c           &      C   sF  t | jd }	 |d}t|}| jdkr#t|}t|}n
| jd }| jd }t	j
j}t|}t	jdd|d\}	}
|
}td | jrct jj|d|tjdddt|t|d|d	d
}nt jj|d||||d	d | jr| jd }| jrt|| j }td |jd|dd | jrt }| jD ]}t|| ||< qt| j|}t| j|}td | jD ]}|| j|| ||| dd q|dur-t|dddf t}t|dddf t}t|dddf t}t|dddf t}| j d }|j!||||d|d}| j"d }dt#| d }|j$|dd|d|dddd	 |j%dddd d d d d! | jr|	&g d"}t	j
j'tjdddt|t|d|d#}|(g  |)t|t| |	j*||d$d%d&}|j+j%dd' |,t-t|d(dd)t|g n&t.|j/d*d+d+d,}t0jj1|d- |d- d.}t0j*j2|||d/}|j+j%dd' d0d1d2d3} t3j45| jd d4 5d5d }!td6|!  |!d7kr|j6d8| d9 n|!d:kr|j6d;| d9 d0d1d<d3}"|7d=|" |8d>|" |9 |:  }#d?d@ |#D  | j;d dA }$| j<d |$ }%|	j=|%dBdCdD dS )Ezread geotiff datar   r   N)figsizezd

***************************************ploting raster data****************************************gMbP?
   )	linthreshlinscalebasevminvmax皙?)axnormcmapalpha)r   r   r   r   r   ze

************************************ploting reference point****************************************r   r   )rh   r   markerzd

***************************************ploting fault**********************************************)rh   r   	linestyle	linewidthr8      r,   )rh   r5   zv:zm/sg
ףp=
?g?r-   r   )XYUanglelabellabelposrh   
labelcolorbothin   T)which	direction	labelsizebottomtopleftright)gq=
ףp?g
ףp=
?g{Gz?r   )r   r   verticalz%.2f)orientationr~   )r   g{Gzg{Gz?r   z3%)padd   )r   r   )r   r   serifnormalg      4@)familyweightsize_zFile type is %sr   zvelocity [cm/year])fontdictdisplacementzdisplacement [cm]g      2@zLongitude [degree]zLatitude [degree]c                 S   s   g | ]}| d qS )r   )set_fontname)rO   r   rE   rE   rF   rT   b  s    z plot_geotiff.<locals>.<listcomp>z.pngi,  tight)dpibbox_inches)>rasterioro   r   readrs   r[   r7   nanminnanmaxpltcmjetr`   subplotsr}   Z	nonlinearplotshowcolors
SymLogNormr   r%   	geopandas	read_filer!   rb   r#   rm   r$   listastyperB   r5   quiverr6   r@   	quiverkeytick_paramsadd_axesScalarMappable	set_arrayset_climcolorbarr   	set_ticksrt   r   append_axesmpl	NormalizeColorbarBaseospathsplit	set_label
set_xlabel
set_ylabelget_xticklabelsget_yticklabelsr:   r9   savefig)&rK   site_infovelrasterZraster_datar[   Z
raster_minZ
raster_maxr   figure_sizefigaxesax1imr   Z
shp_refpoirf   r!   rg   rk   lonlatZx_componentZy_componentZquiver_scaleh1ZU_parametersZu_labelZcax1Zsm1cbarcaxr   font2filetypefont1labelsZfig_nameZ
fig_outputrE   rE   rF   plot_geotiff   s   









 



( 

r  c           
      C   sL  t | }|jr|j}|jrwt|}t|jd dg}t|d d df t	|jd D ]3\}}|j
d dkrC|j}tt|||}n|j
d dkrQtt||}|ddd f ||d d f< q,tj|d d df |jd d|fdd}	n"|j}|j
d dkr|j}t|||}	n|j
d dkrtt||}	t||	 d S t| d S )Nr   r   r   r   r   )axis)rL   Z
gps_puttonr3   searchr   rs   zerosr[   rc   aranger*   r4   r   r@   r{   concatenatereshaper2   r  )
iagrsrK   Zgeodatar   Z	sites_velrw   rP   r4   Zsite_velr   rE   rE   rF   mainj  s,   *.r  __main__)N)FrM   )0r;   r   r   Zrasterio.plot
matplotlibr   mpl_toolkits.axes_grid1r   matplotlib.pyplotpyplotr   matplotlib.colorsr   r   numpyrs   mintpy.objectsr   r   r   mintpy.objects.gpsobjectsr   r   mintpy.utilsr   rp   Zmimtpy.objects.cgpsr   r   rW   rX   rZ   r>   rd   rj   rG   rL   r`   r#   rm   r{   r   r   r   r  r  __name__rE   rE   rE   rF   <module>   sL   

:
		
 

