
    Zf(#                         d 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g ddd ej                         ddfd	Zddg d
fdZdg dddddfdZd Zy)z5Utilities wrapped around cartopy/matplotlib for maps.    Ncrs)ticker)pyplot)   r   r   r      Tc	                 @   t        |||      \  }	}
}}|rt        d| d|        | j                  dd|dddd|d   |d   |d	   |d
          |r.| j                  dd|d          | j                  dd|d          t        dd|z
        }t	        j
                  d| d      }t	        j                  d| d      }| j                  j                  |       | j                  j                  |       | j                  |
|       | j                  |	|       | 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ctickerLongitudeFormatterLatitudeFormatterxaxisset_major_formatteryaxis
set_xticks
set_yticks)axgeo_boxr
   lalo_locr   lalo_offset
projection	font_size	print_msglatslonsdigit	dec_digitlon_formatterlat_formatters                  I/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/utils/map.pydraw_lalo_labelr=      s8   $ $6g@ICO$Q D$	5 .yk9J8*UV NN49Dd4%a[Xa[$QKXa[  B 
CwKNC
CwKNC AqwI..q1=MNM--a	{!<LMMHH  /HH  / MM$JM'MM$JM'I    )r   r   r   r      c           	         | d   | d   k  s| d   | d   k  rt        d|  d      |r|dk  rt        d| d      t        | d   | d   z
  | d   | d   z
  g      }|st        ||z        }t        t	        j
                  t	        j                  |                  }t        |      d	   d
v r|dz  }t        ||      }|D cg c]
  }|d|z  z   }}|D cg c]  }|||z  z
  dz   }}||j                  t        |               }t        t	        j
                  t	        j                  |                  }t	        j                  | d   d|dz   z  z        d|dz   z  z  }	t	        j                  | d   d|dz   z  z        d|dz   z  z  }
t	        j                  t	        j                  |	|	d|z  z
  |       t	        j                  |	|	d|z  z   |      f      }t	        j                  t	        j                  |
|
d|z  z
  |       t	        j                  |
|
d|z  z   |      f      }t	        j                  t	        j                  |            }t	        j                  t	        j                  |            }|t	        j                   t	        j"                  || d   k\  || d   k                 }|t	        j                   t	        j"                  || d   k\  || d   k                 }||||fS c c}w c c}w )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   zInput geo_box z7 has N <= S or E <= W! Check the order of (W, N, E, S).zInput lalo_step (z) must be positive!)89
   g      $@)
ValueErrorr&   
round_to_1intnpfloorlog10strroundindexminceilhstackarangesortuniquewherelogical_and)r0   r
   r   step_candidatemax_lalo_distr8   ilalo_step_candidatedistance	lat_major	lon_majorr6   r7   s                r<   r#   r#   E   s    qzWQZ71:
#:>'2ijkkY!^,YK7JKLLgaj0'!*wqz2IJKM}|;<	 BHHRXXi012y>"*QJEi/I 5CCqqU{CCEXYQ55!;YY's8}(EF	),-.E b57m35U1WEIb57m35U1WEI99bii	9s=7H+H9*Uii	9s=7H+H9UW XD99bii	9s=7H+H9*Uii	9s=7H+H9UW XD77299T?#D77299T?#D
(:DGAJ<NOPQD
(:DGAJ<NOPQDy%''% DYs   K.K3degrees)皙?r^   皙?g?   kr   c           	         t        j                  d      }| r| nt        j                         } |d   |d   |d   |d   z
  z  z   }	|d   |d   |d   |d   z
  z  z   }
|j	                  d      r|d   |d   z
  }nD|j	                  d      r3|j                  |d   |d   |d   |d         d   }|d   |d   z
  d	kD  r| S t        |d   z        }|d
kD  rt        j                  |d
z        d
z  }|j	                  d      r0|j                  |	|
d|      d   }t        j                  |	|z
        }n|j	                  d      r|}|	dz  z
  }|	|dz  z   }t        ||      } | j                  ||g|
|
gfi |  | j                  ||g|
|
d|z  z   gfi |  | j                  ||g|
|
d|z  z   gfi | d}|d
k\  rd}|dz  }|d   |d   z
  |z  }| j                  ||dz  z   |
|z   |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       @)color	linewidthr_   mkmgMbP?z.0f center)r   r   svahafontsizeri   )pyprojGeodpltgca
startswithinvrF   rH   rintfwdabsdictplottext)r/   r0   unitloclabelpadr4   ri   rj   geodlon_clat_cscene_widthlength_meterlon_c2length_displon0lon1kwargs
txt_offsets                      r<   draw_scalebarr   z   sw    ;;W%DswwyB AJQ71:
#:;;EAJQ71:
#:;;E waj71:-		hhwqz71:&qz71:778: AJ#r)I kCF23Lfww|F23F: u%L9!<ffUV^,		!" ;$$D;$$D 3FBGGT4L5%.3F3BGGT4L5%#k/"9:EfEBGGT4L5%#k/"9:EfE Dv!*wqz)X5JGGd;s?"Jc"!D6*   Ir>   c           	          t        t        j                  t        j                  t	        |                         }t        | |       S )z1Return the most significant digit of input number)rG   rH   rI   rJ   r{   rL   )r   r8   s     r<   rF   rF      s1    #a&)*+EUFr>   )__doc__numpyrH   rs   cartopyr   ccrscartopy.mplr   r'   
matplotlibr   ru   PlateCarreer=   r#   r   rF    r>   r<   <module>r      sj    ;    ) $ ,0,UVdh/t//1TT.b +/Q 0(j %.?T]_qH\r>   