U
     xbv!                     @   s   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d d dgdde ddfdd	Zdddd
dddgfddZddddgdddfddZdd ZdS )    N)pyplotcrs)ticker      Tc	                 C   s   t |||d\}	}
}}|r.td| d|  | jdd|dddd|d |d |d	 |d
 d |r| jdd|d d | jdd|d d tdd| }tjd| dd}tjd| dd}| j| | j	| | j
|
|d | j|	|d | S )aE  Auto draw lat/lon label/tick based on coverage from geo_box
    Parameters: ax           : cartopy axes.
                geo_box      : 4-tuple of float, (W, N, E, S) in degree
                lalo_step    : float, major tick interval for X/Y axes
                lalo_loc     : list of 4 bool, positions where the labels are drawn as in [left, right, top, bottom]
                               default: [1,0,0,1]
                lalo_max_num : int, maximum number of major ticks for X/Y axes
                lalo_offset  : float, distance in points between tick and label.
                               Set to negative value to move the ticklabel inside the plot.
                projection   : cartopy.crs object
                ...
    Example:    geo_box = (128.0, 37.0, 138.0, 30.0)
                m.draw_lalo_label(geo_box)
    )	lalo_steplalo_max_numzplot lat/lon label in step of z and location of bothinTr   r         )which	direction	labelsizeleftrighttopbottom	labelleft
labelrightlabeltoplabelbottomxmajor)axisr   pady.f)number_formatr   )auto_lalo_sequenceprinttick_paramsmaxctickerZLongitudeFormatterZLatitudeFormatterxaxisset_major_formatteryaxis
set_xticks
set_yticks)axgeo_boxr   lalo_locr	   lalo_offset
projection	font_size	print_msglatslonsdigitZ	dec_digitZlon_formatterZlat_formatter r5   G/home/exouser/operations/rsmas_insar/sources/MintPy/mintpy/utils/map.pydraw_lalo_label   s6    
     r7   r   r      c           
   	      s  t | d | d  | d | d  g|st }ttt| t|d dkrl d7  t| } fdd|D }fd	d|D }||t	| }ttt| t
| d d
 d   d
 d   }t
| d d
 d   d
 d   }tt||d  | t||d  |f}tt||d  | t||d  |f}	tt|}tt|	}	|tt|| d k|| d k }|	tt|	| d k|	| d k }	||	| fS )a  Auto calculate lat/lon label sequence based on input geo_box
    Parameters: geo_box        : 4-tuple of float, defining UL_lon, UL_lat, LR_lon, LR_lat coordinate
                lalo_step      : float
                lalo_max_num   : int, rough major tick number along the longer axis
                step_candidate : list of int, candidate list for the significant number of step
    Returns:    lats/lons : np.array of float, sequence of lat/lon auto calculated from input geo_box
                lalo_step : float, lat/lon label step
    Example:    geo_box = (128.0, 37.0, 138.0, 30.0)
                lats, lons, step = m.auto_lalo_sequence(geo_box)
    r   r   r   r   )89c                    s   g | ]}|d    qS )
   r5   .0i)r4   r5   r6   
<listcomp>_   s     z&auto_lalo_sequence.<locals>.<listcomp>c                    s   g | ]}|   d  qS )r   r5   r=   )r	   max_lalo_distr5   r6   r@   `   s     r<   g      $@)r$   
round_to_1npintfloorlog10strroundindexminceilhstackarangesortuniquewherelogical_and)
r,   r   r	   Zstep_candidateZlalo_step_candidatedistanceZ	lat_majorZ	lon_majorr2   r3   r5   )r4   r	   rA   r6   r!   F   s0    $
&&&&r!   degreesg?皙?g?   kc              	   C   s  t jdd}| r| nt } |d |d |d |d    }|d |d |d |d    }	|drx|d |d  }
nF|dr||d |d |d |d d }
|d |d  d	kr| S t|
|d  }|d
krt|d
 d
 }|dr|	||	d|d }t
|| }n|dr(|}||d  }||d  }| j||g|	|	g|d | j||g|	|	d|  g|d | j||g|	|	d|  g|d d}|d
krd}|d9 }|d |d  | }| j||d  |	| |dd| dd||d | S )a8  draw a simple map scale from x1,y to x2,y in map projection coordinates, label it with actual distance
    ref_link: http://matplotlib.1069221.n5.nabble.com/basemap-scalebar-td14133.html
    Parameters: ax       : matplotlib.pyplot.axes object
                geo_box  : tuple of 4 float in (x0, y0, x1, y1) for (W, N, E, S) in degrees / meters
                unit     : str, coordinate unit - degrees or meters
                loc      : list of 3 float in (length, y, x) of scale bar location:
                           length = ratio of the total width
                           y / x = axes fraction of the scale bar center
                labelpad : float
    Returns:    ax       : matplotlib.pyplot.axes object
    Example:    from mintpy.utils import plot as pp
                pp.draw_scale_bar(ax, geo_box)
    WGS84)ellpsr   r   r   r   meterdeg   g     @@Z   g       @)colorrT   mkmgMbP?z.0f center)r   r   svahafontsizer]   )pyprojGeodpltgca
startswithinvrB   rC   rintfwdabsplottext)r+   r,   unitloclabelpadr0   r]   geodlon_clat_cZscene_widthZlength_meterZlon_c2Zlength_displon0lon1Z
txt_offsetr5   r5   r6   draw_scalebarv   sT      

   
ry   c                 C   s$   t ttt| }t| | S )z1Return the most significant digit of input number)rD   rC   rE   rF   rn   rH   )r   r4   r5   r5   r6   rB      s    rB   )rf   numpyrC   
matplotlibr   rh   cartopyr   ccrsZcartopy.mplr   r%   PlateCarreer7   r!   ry   rB   r5   r5   r5   r6   <module>   s     
10L