U
    a	K                     @   sZ  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,d0d)d*Z-d1d+d,Z.e/d-krVe.  dS )2    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   I/home/centos/operations/rsmas_insar/sources/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>   s     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   s.   t  }t| |D ]\}}t| }|||< q|S )zdefine fault color)dictzipfcolor_table)fcolors
shp_faultsfault_colorsr#   r!   colorrE   rE   rF   r#      s
    
r#   c                 C   s.   t  }t| |D ]\}}t| }|||< q|S )zdefind fault linestyle)r`   ra   flinestyle_table)Zfstylesrd   fault_linestylesr$   r!   stylerE   rE   rF   
flinestyle   s
    
rj   c                 C   sf   t | d}|  |s0t|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arrayrk   site_latsite_lon)gpsnamer3   gps_objro   los_vel	site_inverE   rE   rF   cgps_process   s    

"rx   c           
      C   s   t | |d}td|  |  |sLt|j|j|j}|j	}t
||}n||\}}|| |j}t
||j}t
||j}t|j|j|j|||gg}	|	S )zprocess ngps sites)rk   data_dirzprocess {} site)r   printformatrl   rm   rn   dis_edis_ndis_udatesdis_velocityread_gps_los_displacementget_gps_los_velocityvelocityrp   rq   rk   rr   rs   )
rt   r3   gps_dirru   dis_losr   rv   Ze_velZn_velrw   rE   rE   rF   ngps_process   s    
r   c                 C   sH   dd | D }t |dkr>t|}ttj||d }ntj}|S )zcalculate velocityc                 S   s   g | ]}t |d qS )z%Y%m%d)dtstrftimerN   rE   rE   rF   rT      s     z dis_velocity.<locals>.<listcomp>r8   r   )lenr   Z"get_design_matrix4average_velocityrp   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rBt| 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+   cgpsrp   	transposevstackrq   )rK   
site_names	site_lats	site_lonssites_baseinforE   rE   rF   search_gpssites   s    6r   c           &      C   sR  t | jd }|d}t|}| jdkrDt|}t|}n| jd }| jd }t	j
j}t|}t	jdd|d\}	}
|
}td | jrt 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r t|| j }td |jd|dd | jrt }| jD ]}t|| ||< q4t| j|}t| j|}td | jD ]$}|| j|| ||| dd qt|dk	rbt|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*|	&d"d#d$d	g}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 nLt.|j/d,d-d-d.}t0jj1|d/ |d/ d0}t0j*j2|||d1}|j+j%dd) d2d3d4d5} t3j45| jd d6 5d7d }!td8|!  |!d9kr|j6d:| d; n|!d<kr|j6d=| d; d2d3d>d5}"|7d?|" |8d@|" |9 |:  }#dAdB |#D  | j;d dC }$| j<d |$ }%|	j=|%dDdEdF dS )Gzread geotiff datar   r   N)figsizezd

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

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

***************************************ploting fault**********************************************)rf   r   	linestyle	linewidthr8      r,   )rf   r5   zv:zm/sg
ףp=
?g?r-   r   )XYUanglelabellabelposrf   
labelcolorbothin   T)which	direction	labelsizebottomtopleftrightgq=
ףp?g
ףp=
?g{Gz?)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)>rasteriorl   r   readrp   rZ   r7   nanminnanmaxpltcmjetr_   subplotsrz   Z	nonlinearplotshowcolors
SymLogNormr   r%   	geopandas	read_filer!   r`   r#   rj   r$   listastyperB   r5   quiverr6   r@   	quiverkeytick_paramsadd_axesScalarMappable	set_arrayset_climcolorbarr   	set_ticksrq   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_datarZ   Z
raster_minZ
raster_maxr   Zfigure_sizefigaxesax1imr   Z
shp_refpoird   r!   re   rh   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   sP  t | }|jrD|j}|jrt|}t|jd dg}t|d d df t	|jd D ]f\}}|j
d dkr|j}tt|||}n|j
d dkrtt||}|ddd f ||d d f< qZtj|d d df |jd d|fdd}	nH|j}|j
d dkr|j}t|||}	n|j
d dkr8tt||}	t||	 nt| d S )Nr   r   r   r   r   )axis)rL   Z
gps_puttonr3   searchr   rp   zerosrZ   ra   aranger*   r4   r   r@   rx   concatenatereshaper2   r  )
iagrsrK   Zgeodatar   Z	sites_velrt   rP   r4   Zsite_velr   rE   rE   rF   mainj  s,    *.r  __main__)N)FrM   )N)N)0r;   r   r   Zrasterio.plot
matplotlibr   mpl_toolkits.axes_grid1r   matplotlib.pyplotpyplotr   matplotlib.colorsr   r   numpyrp   mintpy.objectsr   r   r   mintpy.objects.gpsobjectsr   r   mintpy.utilsr   rm   Zmimtpy.objects.cgpsr   r   rV   rW   rY   r>   rb   rg   rG   rL   r_   r#   rj   rx   r   r   r   r  r  __name__rE   rE   rE   rF   <module>   sH   
:

		
 

