
    Ug~                       d dl mZ d dlZd dlZd dlm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mZ 	 d dlmZ 	 d dlZ	 d dlmZ 	 d dlmZ  G d d	e      Z  ejB                  d
d
      Z" ejB                  d
d
d
      Z# e$ ejJ                  dejL                        jO                         D  cg c]
  }  e(|        c}       Z) G d d      Z* G d d      Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2dFdZ3dGdZ4dHdZ5d Z6dIdZ7d Z8d Z9d  Z: ed! "      d#        Z;d$ Z<d% Z=d& Z>d' Z?d( Z@dJd)ZAe"d*        ZBe"d+        ZCe#d,        ZDe#d-        ZEe#d.        ZFe#d/        ZGe"d0        ZHe"d1        ZIe#d2        ZJe#d3        ZKe"d4        ZLe#d5        ZMe#d6        ZNe"d7        ZOe#d8        ZPe#d9        ZQe"d:        ZRe#d;        ZSe#d<        ZTe#d=        ZUe"d>        ZVe"d?        ZWe"d@        ZXe"dA        ZYe"dB        ZZe"dC        Z[e"dD        Z\e"dE        Z]y# e$ r dZY w xY w# e$ r  ed      ZY w xY w# e$ r dZY w xY w# e$ r  ed      ZY 	w xY w# e$ r  ed      ZY w xY wc c} w )K    )annotationsN)getmro)memoize)	DataArray)RaggedDtype)GeometryDtypec                      e Zd ZdZy)VisibleDeprecationWarningzVisible deprecation warning.

    By default, python will not show deprecation warnings, so this class
    can be used when a very visible warning is helpful, for example because
    the usage is most likely a user bug.
    N)__name__
__module____qualname____doc__     0lib/python3.12/site-packages/datashader/utils.pyr
   r
   +   s    r   r
   T)nopythonnogil)r   r   parallelz([0-9]+)\.([0-9]+)\.([0-9]+)c                  (    e Zd ZdZd Zd Zd Zd Zy)ExprzBase class for expression-like objects.

    Implements hashing and equality checks. Subclasses should implement an
    ``inputs`` attribute/property, containing a tuple of everything that fully
    defines that expression.
    c                J    t        t        |       | j                         f      S N)hashtype_hashable_inputsselfs    r   __hash__zExpr.__hash__D   s    T$Z!6!6!89::r   c                t    t        |       t        |      u xr! | j                         |j                         k(  S r   )r   r   r   others     r   __eq__zExpr.__eq__G   s8    T
d5k) D%%'5+A+A+CC	Er   c                    | |k(   S r   r   r    s     r   __ne__zExpr.__ne__K   s    5=  r   c                6   g }| j                   D ]~  }t        |t        t        f      r|j	                  t        |             4t        |t        j                        r |j	                  |j                                n|j	                  |        t        |      S )zt
        Return a version of the inputs tuple that is suitable for hashing and
        equality comparisons
        )	inputs
isinstancelistsetappendtuplenpndarraytobytes)r   resultips      r   r   zExpr._hashable_inputsN   sm    
 ++B"tSk*eBi(B

+bjjl+b!  V}r   N)r   r   r   r   r   r"   r$   r   r   r   r   r   r   =   s    ;E!r   r   c                  $    e Zd ZdZd ZddZd Zy)
DispatcherzSimple single dispatch.c                    i | _         y r   )_lookupr   s    r   __init__zDispatcher.__init__a   s	    r   Nc                     | fdS t        t              rD ]  } j                  ||        |S | j                  <   |S )z6Register dispatch of `func` on arguments of type `typ`c                (    j                  |       S r   )register)fr   typs    r   <lambda>z%Dispatcher.register.<locals>.<lambda>g   s    T]]32r   )r'   r+   r8   r4   )r   r:   functs   ``  r   r8   zDispatcher.registerd   sL    <22c5!a&   !%DLLr   c                    | j                   }t        |      }||v r ||   |g|i |S t        |      dd  D ]  }||v s ||   |g|i |c S  t        dj	                  |            )N   zNo dispatch for {0} type)r4   r   r   	TypeErrorformat)r   headrestkwargslkr:   clss          r   __call__zDispatcher.__call__o   s     \\4j"92c741$1&11#;qr?Cbyr#wt5d5f55 # 299#>??r   r   )r   r   r   r   r5   r8   rG   r   r   r   r2   r2   _   s    !	@r   r2   c                    t         j                  j                  |       } t        | t         j                        xr | t         j
                  j                  v S )zCheck if a datashape is numeric and real.

    Example
    -------
    >>> isrealfloat('int32')
    False
    >>> isrealfloat('float64')
    True
    >>> isrealfloat('string')
    False
    >>> isrealfloat('complex64')
    False
    )	datashape
predicateslaunderr'   Unittypesetsfloatingdts    r   isrealfloatrQ   }   sA     
			%	%b	)Bb)..)ObI4F4F4O4O.OOr   c                    t         j                  j                  |       } t        | t         j                        xr | t         j
                  j                  v S )zCheck if a datashape is numeric and real.

    Example
    -------
    >>> isreal('int32')
    True
    >>> isreal('float64')
    True
    >>> isreal('string')
    False
    >>> isreal('complex64')
    False
    )rI   rJ   rK   r'   rL   rM   realrO   s    r   isrealrT      sA     
			%	%b	)Bb)..)KbI4F4F4K4K.KKr   c                X   t        t        | j                              }|j                  |       |j	                  d|       | j                  |      } t        j                  |       }t        j                  |d      }|| z  }t        j                  |d|       j                  d      S )aW  nansum where all-NaN values remain NaNs.

    Note: In NumPy <=1.9 NaN is returned for slices that are
    all NaN, while later versions return 0. This function emulates
    the older behavior, which allows using NaN as a missing value
    indicator.

    Parameters
    ----------
    array: Array to sum over
    axis:  Axis to sum over
    r   axis)r(   rangendimremoveinsert	transposer,   isnanallwheresum)arrayrW   Tmissing_vals	all_emptyset_to_zeros         r   nansum_missingrf      s     	U5::AHHTNHHQOOAE88E?L|!,I)+K88KE*..A.66r   c                    | j                   dd \  }}| j                  dd \  }}| |   j                  }| |   j                  }|d   |d   z
  |dz
  z  }|d   |d   z
  |dz
  z  }||fS )zCalculate the resolution of xarray.DataArray raster and return it as the
    two-tuple (xres, yres). yres is positive if it is decreasing.
    Nr   r?   )shapedimsvalues)	rasterhwydimxdimxcoordsycoordsxresyress	            r   calc_resrv      s     <<DAqRS!JD$Tl!!GTl!!GBK'!*$Q/DAJ$Q/D:r   c           	        |d   dk  r| j                         n| j                         }|d   dk  r|j                         n|j                         }t        j                  x}}t        j                   x}}t        j                  |d   d|gd|d    |gg dg      }	ddt        |      ft        |       dft        |       t        |      ffD ]R  \  }
}t        j                  |	t        j                  |
|dg            \  }}}||k  r|}||kD  r|}||k  r|}||kD  sQ|}T |d   dz  |d   dz  }}||z
  ||z   ||z
  ||z   fS )a  Calculate the bounding box of a raster, and return it in a four-element
    tuple: (xmin, ymin, xmax, ymax). This calculation assumes the raster is
    uniformly sampled (equivalent to a flat-earth assumption, for geographic
    data) so that an affine transform (using the "Augmented Matrix" approach)
    suffices:
    https://en.wikipedia.org/wiki/Affine_transformation#Augmented_matrix

    Parameters
    ----------
    xs : numpy.array
        1D NumPy array of floats representing the x-values of a raster. This
        likely originated from an xarray.DataArray or xarray.Dataset object
        (xr.open_rasterio).
    ys : numpy.array
        1D NumPy array of floats representing the y-values of a raster. This
        likely originated from an xarray.DataArray or xarray.Dataset object
        (xr.open_rasterio).
    res : tuple
        Two-tuple (int, int) which includes x and y resolutions (aka "grid/cell
        sizes"), respectively.
    r   r?           )rx   rx         ?)r   r   ry          @)maxminr,   infra   lendot)xsysresxboundyboundxminyminxmaxymaxAbx_y_xy_xpadypads                    r   	calc_bboxr      sU   , Q!RVVXFQ!RVVXF&&D466'D4	CFRf-c!fWf-)+ 
,B As2w<#b'1BR7IJB&&RXXr2rl341at8Dt8Dt8Dt8D K QCF2I$D9d4idDI55r   c                    t        |      }t        |      dz  }|j                         |j                         }}||z
  }| |z   |z
  ||z
  |z
  }} t	        | |z  |z        t	        ||z  |z        }
}	|
|	k  r|	|	fS |	|
fS )ai  
    Transform continuous start and end coordinates into array indices.

    Parameters
    ----------
    start : float
        coordinate of the lower bound.
    end : float
        coordinate of the upper bound.
    coords : numpy.ndarray
        coordinate values along the axis.
    res : tuple
        Resolution along an axis (aka "grid/cell sizes")
    rz   )r~   absr|   r{   int)startendcoordsr   sizehalfvminvmaxspansidxeidxs              r   get_indicesr      s     v;Ds8B;Dvzz|$D9DtD#d(4-3EeDj$&'c$h_)=$Dd{Tz:r   c                B    |r| dd d dd d f   } |r| dd d d d df   } | S )N.ri   r   )ra   xflipyflips      r   _flip_arrayr     s5    c4R4l#c1ddl#Lr   c                Z   |t        |       }| j                  }|||dz
     }t        |d   t        j                        rt        j                  dd      nd}t        |d   t        j                        rt        j                  dd      nd}|d   |k  }|d   |kD  }t        |||      }|S )a  
    Reorients the array to a canonical orientation depending on
    whether the x and y-resolution values are positive or negative.

    Parameters
    ----------
    raster : DataArray
        xarray DataArray to be reoriented
    res : tuple
        Two-tuple (int, int) which includes x and y resolutions (aka "grid/cell
        sizes"), respectively.
    layer : int
        Index of the raster layer to be reoriented (optional)

    Returns
    -------
    array : numpy.ndarray
        Reoriented 2d NumPy ndarray
    r?   r   ns)rv   datar'   r,   timedelta64r   )rm   r   layerra   r0zeror1zeror   r   s           r   orient_arrayr     s    ( {vKKEeAg(23q62>>(JR^^At$PQF(23q62>>(JR^^At$PQFFVOEFVOEue,ELr   c                   | j                   dd \  }}| d||t        |      z  z
  d||t        |      z  z
  f   }t        j                  t	        |      D cg c]&  }t	        |      D cg c]  }||d||d|f    c}( c}}      }|dk(  rt        j
                  |d      S |dk(  rt        j                  |d      S |dk(  rt        j                  |d      S |dk(  rt        j                  |d      S |d	k(  rt        j                  |d      S |d
k(  rt        j                  |d      S |dk(  rt        j                  |d      S t        d      c c}w c c}}w )z3Create downsampled aggregate factor in pixels unitsN   meanr   rV   r`   r{   r|   medianstdvarzNInvalid 'how' downsample method. Options mean, sum, max, min, median, std, var)rj   r   r,   concatenaterX   nanmeannansumnanmaxnanmin	nanmediannanstdnanvar
ValueError)		aggregatefactorhowr   r   crarrjiconcats	            r   downsample_aggregater   5  sr   __Ra FB,r2F+,,.Dr2F3C/D.DDEE^^%*6]4%2 &+6]4%2 $AIvIqy&y$89%24%24 5F f}zz&q))	yya((	yya((	yya((	||F++	yya((	yya(( $ % 	%'4 4s   E 
+E?E 
E 
c                   t        j                  | j                        }t        j                  | j                        }|dk(  r*| j                  | j                  dkD     j                         }|dk(  rt        j                  |||      dddf   }nRt        j                  dt        j                  ||z
        t         j                  ||j                        |z   dddf   }t        |      ||fS )zHelper function similar to np.linspace which return values from aggregate min value to
    aggregate max value in either linear or log space.
    r   linearN)basenumdtype)r,   r   rl   r   r   r|   linspacelogspacelog1per   r   )r   r   r   max_valmin_valvalss         r   summarize_aggregate_valuesr   P  s    
 ii	(()Gii	(()G!|..!!3488:
h{{7GS1$':AHHWw%67!#3")--1 4;; =A!GE
 T?GW,,r   c                     g  fd}|S )z&
    simple arg caching decorator
    c                 6    rd   | k7  r
|  |  fd d  d   S )Nr   r?   r   )argsr9   lasts    r   r   zhold.<locals>._l  s*    tAw$AtHnDGAwr   r   )r9   r   r   s   ` @r   holdr   f  s     D Hr   c                   ddl m} t        j                  j	                  |      st        j
                  |       |r	 || |      } | j                         j                  t        j                  j                  |||z                |r| S dS )zPGiven a datashader Image object, saves it to a disk file in the requested formatr   )set_backgroundN)	datashader.transfer_functionsr   ospathexistsmkdirto_pilsavejoin)imgfilenamefmt_returnexport_path
backgroundr   s          r   export_imager   s  sf     =77>>+&
S*-JJLbggll;3?@3#t#r   c                   t        | t        t        f      rt        j                  |       } t        |t        t        f      rt        j                  |      }t        j
                  dz  }| |z  dz  }t        j                  t        j                  d|z   t        j
                  z  dz              |z  t        j
                  z  }||fS )a  
    Projects the given (longitude, latitude) values into Web Mercator
    coordinates (meters East of Greenwich and meters North of the Equator).

    Longitude and latitude can be provided as scalars, Pandas columns,
    or Numpy arrays, and will be returned in the same form.  Lists
    or tuples will be converted to Numpy arrays.

    Examples:
       easting, northing = lnglat_to_meters(-74,40.71)

       easting, northing = lnglat_to_meters(np.array([-74]),np.array([40.71]))

       df=pandas.DataFrame(dict(longitude=np.array([-74]),latitude=np.array([40.71])))
       df.loc[:, 'longitude'], df.loc[:, 'latitude'] = lnglat_to_meters(df.longitude,df.latitude)
    iRa g     f@Z   g     v@)r'   r(   r+   r,   ra   pilogtan)	longitudelatitudeorigin_shifteastingnorthings        r   lnglat_to_metersr     s    " )dE]+HHY'	(T5M*88H%557?L,&.Gvvbffb8mruu4u<=>MPRPUPUUHXr   c                D   t        | j                  t        t        j                  j                              sxt        | j                  t        j
                  j                  j                        s@t        rft        | j                  t        j                  j                  j                        r-| j                  j                  }t        r*t        |t        j                        r|j                         }t        r*t        |t        j                        r|j!                         }t#        j$                  |      }|j                  j&                  dk(  r|j)                  d      }t+        j,                  dj/                  t1        | j                  j                        |j                              }t+        j                  ||| j                  j2                        S | j                  j&                  dk(  rMt5        | j                  dd      }|t7        |      }t+        j8                  t+        j:                  |            S t        | j                  t<        t>        f      r| j                  S t@        r&t        | j                  t@              r| j                  S t*        jB                  jE                  | j                        }|t*        jF                  k(  rt*        jH                  n|}|t*        jH                  t*        jJ                  fv rt+        j8                  |      S |S )	zeReturn an object from datashader.datashape.coretypes given a column from a pandas
    dataframe.
    Uobjectz{} * {})r   orderedMtzN)r   )&r'   r   r   pdCategoricalapitypesCategoricalDtypecudfcoredtypescat
categoriesddIndexcompute	to_pandasr,   ra   kindastyperI   dshaperA   r~   r   getattrstrOptionDateTimer   r   gpd_GeometryDtypeCTypefrom_numpy_dtypeobject_string	datetime_)colpd_categoriesr   
cat_dshaper   r  s         r   dshape_from_pandas_helperr    s    	399d2>>#7#789syy"&&,,"?"?@Z		499+;+;+L+LM***]BHH5)113MJ}djj9)335MXXm,
  C'#**84J%%i&6&6""#'
 
 $$Z*4-0WW__> 	> 
3	SYYd+>RB	 2 2b 9::	CII];	<yy	z#))5FGyy__--cii8F!'9+<+<!<Y&F)""I$7$788''Mr   c                    t        |       t        j                  | j                  D cg c]  }|t	        | |         f c}      z  S c c}w )z=Return a datashape.DataShape object given a pandas dataframe.)r~   rI   Recordcolumnsr  )dfks     r   dshape_from_pandasr    sS    r7Y%%02

'<0:1 )*+DRU+K'L0:'< = = = '<s   A
c                :    t        | d   j                               S )Nr   )r+   __dask_keys__)r   rD   s     r   r;   r;     s    %Q(=(=(?"@r   )keyc                F   | j                   D cg c]  }t        | |   j                  t        t        j
                  j                              s;t        | |   j                  t        j                  j                  j                        rt        | |   j                  dd      s| }}| j                  |d      } t        j                  t        j                  | j                   D cg c]!  }|t        | |   j!                  d            f# c}      z  | fS c c}w c c}w )z;Return a datashape.DataShape object given a dask dataframe.knownTF)indexr   )r  r'   r   r   r   r   r   r   r   r  r   
categorizerI   r   r  r  get_partition)r  r  cat_columnsr  s       r   dshape_from_daskr%    s     zz8!r#w}}d2>>+?+?&@Ar#w}}bffll&C&CD2c7;;6 	z  8
 
{%	0B ==9++HJ

-HR1%be&9&9!&<=>
-  
 8-s   BD(&D
c                    t         j                  t        j                  t        | j                        t        | j
                        z   D cg c]  }|t        | |         f c}      z  S c c}w )z;Return a datashape.DataShape object given a xarray Dataset.)rI   r   r  r(   	data_varsr   r  )xr_dsr  s     r   dshape_from_xarray_datasetr)    sf    ==9++eoo&ell);;-;A 
%eAh/0;-    -s   A,
c                   t        j                  | j                  d   dz         }t         j                  |d<   | |dd t        j                  ||j                  d         }t        j                  |j                  d   |j                  d   dz   f      }t         j                  |dddf<   ||ddddf<   t        j                  ||j                         d      S )aU  
   Converts a set of multiple sequences (eg: time series), stored as a 2 dimensional
   numpy array into a pandas dataframe that can be plotted by datashader.
   The pandas dataframe eventually contains two columns ('x' and 'y') with the data.
   Each time series is separated by a row of NaNs.
   Discussion at: https://github.com/bokeh/datashader/issues/286#issuecomment-334619499

   x_values: 1D numpy array with the values to be plotted on the x axis (eg: time)
   y_values: 2D numpy array with the sequences to be plotted of shape (num sequences X length of
             each sequence)

   r   r?   ri   N)r   r   )r,   zerosrj   nantiler   	DataFrameflatten)x_valuesy_valuesr   r   s       r   !dataframe_from_multiple_sequencesr2    s     
xxq!A%&1661R51Sb6 
wwq(..#$1 
xx"HNN1$5$9:;1ff1QU81QV9 ,,QQYY[1
22r   c                   g d}| j                   |j                   d|f   j                  t        j                        ddf   }|\  }}}||z
  ||z
  }}|d   |d   z  |d   |d   z  z
  }	|	dk\  rg d}|j                   dd|f   }
|
j                  dk(  s|
j                  t        j                        }
t        j
                  | j                   |
d      }|j                  t        j                  |j                  dd       |j                  d         }t        j                  || j                  	      }t        | j                        dkD  }|s4|j                  d
   }|j                   ddd
f   j                  d
      ||<   |S )zkHelper for ``datashader.utils.mesh()``. Both arguments are assumed to be
    Pandas DataFrame objects.
    )r   r?   r   r   Nr   r?   )r   r   r?   int64rV   )r     )rl   r  r,   r4  r   takereshapeprodrj   r   r.  r  r~   repeat)vertices	simpliceswinding	first_triabcp1p2cross_productvertex_idxsr   r   verts_have_weights
weight_cols                  r   _pd_meshrG    sl   
 G	 0 0G < C CBHH MrPQr QRIGAq!UAEBqEBqEMBqEBqEM1M ""1g:.K'!((2778??Ka8D<<

2A/A?D
,,tX%5%5
6C X--.2&&q)
#**1a4077:JJr   c                $   t        | j                         |j                               }t        | j                  |j                        }t	        t        j                  t        |      d|z  z        dz        }t        j                  ||      }|S )ziHelper for ``datashader.utils.mesh()``. Both arguments are assumed to be
    Dask DataFrame objects.
    r5  )	chunksize)
rG  r  r{   npartitionsr   r,   ceilr~   r  from_pandas)r:  r;  r   approx_npartitionsrI  s        r   _dd_meshrN  %  s{     8##%y'8'8':
;C X1193H3HIBGGCH*<(<=>BCI ..	
2CJr   c                   |j                   j                  d   dk\  sJ d       |j                  j                  dd j	                  d       j                         }|sJ d       t        | j                        dkD  s#|j                   j                  d   dkD  sJ d       t        r@t        | t        j                        r&t        |t        j                        rt        | |      S t        | |      S )	zMerge vertices and simplices into a triangular mesh, suitable to be
    passed into the ``Canvas.trimesh()`` method via the ``mesh``
    keyword-argument. Both arguments are assumed to be Dask DataFrame
    objects.
    r?   r5  zFAt least three vertex columns are required for the triangle definitionNc                J    t        j                  | t         j                        S r   )r,   
issubdtypeintegerrO   s    r   r;   zmesh.<locals>.<lambda>B  s    2==RZZ0r   z^Simplices must be integral. You may consider casting simplices to integers with ".astype(int)"r   zMIf no vertex weight column is provided, a triangle weight column is required.)rl   rj   r   ilocmapr^   r~   r  r  r'   r.  rN  rG  )r:  r;  simplices_all_intss      r   meshrV  7  s     !!!$) : -9 :) #))..r2660	ce   7 !6 7 x 1$	(8(8(>(>q(AA(E XWXE 
j2<<0Z	2<<5X),,Hi((r   c                     |r | |i |S  | | S r   r   )r<   r   rD   s      r   applyrX  R  s    T$V$$T{r   c                    | dk  xs | dkD   S )zQ
    Equivalent to isnan for floats, but also numba compatible with integers
    r   r   vals    r   isnullr\  Y  s    
 q#C!G$$r   c                    | dk(  S )zO
    Check for -1 which is equivalent to NaN for some integer aggregations
    ri   r   rZ  s    r   isminus1r^  a  s    
 "9r   c                    | j                         } |j                         }t        j                  t        |             D ](  }t	        | |         st	        ||         r!||   | |<   * y)zPFirst of 2 arrays but taking nans into account.
    Return the first array.
    Nravelnbpranger~   r\  retr!   r   s      r   nanfirst_in_placerf  i  sT    
 ))+CKKMEYYs3x #a&>&q"21XCF !r   c                    | j                         } |j                         }t        j                  t        |             D ]  }t	        ||         r||   | |<    y)zOLast of 2 arrays but taking nans into account.
    Return the first array.
    Nr`  rd  s      r   nanlast_in_placerh  u  sJ    
 ))+CKKMEYYs3x eAh1XCF !r   c                   | j                         } |j                         }t        j                  t        |             D ]K  }t	        | |         rt	        ||         r ||   | |<   )t	        ||         r8||   | |   kD  sD||   | |<   M y)zMax of 2 arrays but taking nans into account.  Could use np.nanmax but
    would need to replace zeros with nans where both arrays are nans.
    Return the first array.
    Nr`  rd  s      r   nanmax_in_placerj    s}     ))+CKKMEYYs3x #a&>%(#qAa!eAhQ&71XCF !r   c                   | j                         } |j                         }t        j                  t        |             D ]K  }t	        | |         rt	        ||         r ||   | |<   )t	        ||         r8||   | |   k  sD||   | |<   M y)zMin of 2 arrays but taking nans into account.  Could use np.nanmin but
    would need to replace zeros with nans where both arrays are nans.
    Accepts 3D (ny, nx, ncat) and 2D (ny, nx) arrays.
    Return the first array.
    Nr`  rd  s      r   nanmin_in_placerl    s}     ))+CKKMEYYs3x #a&>%(#qAa!eAhQ&71XCF !r   c                l    t        |       }t        |dz
  |d      D ]  }| |dz
     | |<    || |<   |dz   S )a(  Insert a value into a 1D array at a particular index, but before doing
    that shift the previous values along one to make room. For use in
    ``FloatingNReduction`` classes such as ``max_n`` and ``first_n`` which
    store ``n`` values per pixel.

    Parameters
    ----------
    target : 1d numpy array
        Target pixel array.

    value : float
        Value to insert into target pixel array.

    index : int
        Index to insert at.

    Returns
    -------
    Index beyond insertion, i.e. where the first shifted value now sits.
    r?   ri   )r~   rX   )targetvaluer!  nr   s        r   shift_and_insertrq    sH    , 	FA1Q3r"1Q3Kq	 #F5M19r   c                    t        |       }d}|D ]:  }t        |      r yt        ||      D ]  }t        | |         s|| |<   |dz   } : < y)af  Single pixel implementation of nanfirst_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   r?   N)r~   r\  rX   	ret_pixelother_pixelrp  istartother_valuer   s         r   _nanfirst_n_implrx    sY     	IAF"+61%)A,'#.IaLqSF &	 #r   c           
         | j                   \  }}}}t        j                  |      D ]:  }t        |      D ]*  }t        |      D ]  }t	        | |||f   ||||f           , < yzN3d version of nanfirst_n_in_place_4d, taking arrays of shape (ny, nx, n).
    Nrj   rb  rc  rX   rx  	re  r!   nynxncat_nr   r   r   s	            r   nanfirst_n_in_place_4dr    sc     yyBD"YYr]rAT{ Q3Yq!Sy1AB #  r   c                    | j                   \  }}}t        j                  |      D ](  }t        |      D ]  }t	        | ||f   |||f           * yrz  r{  re  r!   r}  r~  r  r   r   s          r   nanfirst_n_in_place_3dr    sM     JBBYYr]rASAYad4  r   c                    t        |       }d}|D ].  }t        |      r yt        ||      D ]  }t        | ||      } . 0 y)ae  Single pixel implementation of nanlast_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   Nr~   r\  rX   rq  rs  s         r   _nanlast_n_implr    sI     	IAF"+61%))[&I &	 #r   c           
         | j                   \  }}}}t        j                  |      D ]:  }t        |      D ]*  }t        |      D ]  }t	        | |||f   ||||f           , < yrz  rj   rb  rc  rX   r  r|  s	            r   nanlast_n_in_place_4dr    sc     yyBD"YYr]rAT{Aq#IaCi0@A #  r   c                    | j                   \  }}}t        j                  |      D ](  }t        |      D ]  }t	        | ||f   |||f           * y)zM3d version of nanlast_n_in_place_4d, taking arrays of shape (ny, nx, n).
    Nr  r  s          r   nanlast_n_in_place_3dr    sM     JBBYYr]rAC1IuQT{3  r   c                    t        |       }d}|D ]E  }t        |      r yt        ||      D ]'  }t        | |         s	|| |   kD  st        | ||      } E G y)ad  Single pixel implementation of nanmax_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   Nr  rs  s         r   _nanmax_n_implr    `     	IAF"+61%)A,';1+E-iaHF &	 #r   c           
         | j                   \  }}}}t        j                  |      D ]:  }t        |      D ]*  }t        |      D ]  }t	        | |||f   ||||f           , < y)a  Combine two max-n arrays, taking nans into account. Max-n arrays are 4D
    with shape (ny, nx, ncat, n) where ny and nx are the number of pixels,
    ncat the number of categories (will be 1 if not using a categorical
    reduction) and the last axis containing n values in descending order.
    If there are fewer than n values it is padded with nans.
    Return the first array.
    Nrj   rb  rc  rX   r  r|  s	            r   nanmax_n_in_place_4dr  -  c     yyBD"YYr]rAT{s1a9~uQ3Y/?@ #  r   c                    | j                   \  }}}t        j                  |      D ](  }t        |      D ]  }t	        | ||f   |||f           * y)zL3d version of nanmax_n_in_place_4d, taking arrays of shape (ny, nx, n).
    Nr  r  s          r   nanmax_n_in_place_3dr  =  M     JBBYYr]rA3q!t9eAqDk2  r   c                    t        |       }d}|D ]E  }t        |      r yt        ||      D ]'  }t        | |         s	|| |   k  st        | ||      } E G y)ad  Single pixel implementation of nanmin_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   Nr  rs  s         r   _nanmin_n_implr  G  r  r   c           
         | j                   \  }}}}t        j                  |      D ]:  }t        |      D ]*  }t        |      D ]  }t	        | |||f   ||||f           , < y)a  Combine two min-n arrays, taking nans into account. Min-n arrays are 4D
    with shape (ny, nx, ncat, n) where ny and nx are the number of pixels,
    ncat the number of categories (will be 1 if not using a categorical
    reduction) and the last axis containing n values in ascending order.
    If there are fewer than n values it is padded with nans.
    Return the first array.
    Nrj   rb  rc  rX   r  r|  s	            r   nanmin_n_in_place_4dr  ]  r  r   c                    | j                   \  }}}t        j                  |      D ](  }t        |      D ]  }t	        | ||f   |||f           * y)zL3d version of nanmin_n_in_place_4d, taking arrays of shape (ny, nx, n).
    Nr  r  s          r   nanmin_n_in_place_3dr  m  r  r   c                   | j                         } |j                         }t        j                  t        |             D ]G  }t	        | |         rt	        ||         r ||   | |<   )t	        ||         r8| |xx   ||   z  cc<   I y)zSum of 2 arrays but taking nans into account.  Could use np.nansum but
    would need to replace zeros with nans where both arrays are nans.
    Return the first array.
    Nr`  rd  s      r   nansum_in_placer  w  ss     ))+CKKMEYYs3x #a&>%(#qAa!FeAhF !r   c                    | j                         } |j                         }t        t        |             D ]'  }||   dkD  s| |   dk(  s||   | |   kD  s ||   | |<   ) y)zMaximum of 2 arrays of row indexes.
    Row indexes are integers from 0 upwards, missing data is -1.
    Return the first array.
    ri   Nra  rX   r~   rd  s      r   row_max_in_placer    ]     ))+CKKME3s8_8b=c!fleAhQ.?1XCF r   c                    | j                         } |j                         }t        t        |             D ]'  }||   dkD  s| |   dk(  s||   | |   k  s ||   | |<   ) y)zMinimum of 2 arrays of row indexes.
    Row indexes are integers from 0 upwards, missing data is -1.
    Return the first array.
    ri   Nr  rd  s      r   row_min_in_placer    r  r   c                    t        |       }d}|D ]9  }|dk(  r yt        ||      D ]!  }| |   dk(  s	|| |   kD  st        | ||      } 9 ; y)ae  Single pixel implementation of row_max_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   ri   Nr~   rX   rq  rs  s         r   _row_max_n_implr    `     	IAF""61%Q<2%y|)C-iaHF &	 #r   c           
         | j                   \  }}}}t        |      D ]:  }t        |      D ]*  }t        |      D ]  }t        | |||f   ||||f           , < y)zCombine two row_max_n signed integer arrays.
    Equivalent to nanmax_n_in_place with -1 replacing NaN for missing data.
    Return the first array.
    Nrj   rX   r  r|  s	            r   row_max_n_in_place_4dr    _     yyBD"2YrAT{Aq#IaCi0@A #  r   c                    | j                   \  }}}t        |      D ](  }t        |      D ]  }t        | ||f   |||f           * y r   r  r  s          r   row_max_n_in_place_3dr    G    JBB2YrAC1IuQT{3  r   c                    t        |       }d}|D ]9  }|dk(  r yt        ||      D ]!  }| |   dk(  s	|| |   k  st        | ||      } 9 ; y)ae  Single pixel implementation of row_min_n_in_place.
    ret_pixel and other_pixel are both 1D arrays of the same length.

    Walk along other_pixel a value at a time, find insertion index in
    ret_pixel and shift values along to insert.  Next other_pixel value is
    inserted at a higher index, so this walks the two pixel arrays just once
    each.
    r   ri   Nr  rs  s         r   _row_min_n_implr    r  r   c           
         | j                   \  }}}}t        |      D ]:  }t        |      D ]*  }t        |      D ]  }t        | |||f   ||||f           , < y)zCombine two row_min_n signed integer arrays.
    Equivalent to nanmin_n_in_place with -1 replacing NaN for missing data.
    Return the first array.
    Nrj   rX   r  r|  s	            r   row_min_n_in_place_4dr    r  r   c                    | j                   \  }}}t        |      D ](  }t        |      D ]  }t        | ||f   |||f           * y r   r  r  s          r   row_min_n_in_place_3dr    r  r   )NN)r   )r      )z.pngT. r   )^
__future__r   r   reinspectr   numbarb  numpyr,   pandasr   toolzr   xarrayr   datashader.datashaperI   dask.dataframe	dataframer  ImportErrordatashader.datatypesr   r   r   	Exceptiongeopandas.arrayr   r  spatialpandas.geometryUserWarningr
   jitngjitngjit_parallelr+   match__version__groupsr   numba_versionr   r2   rQ   rT   rf   rv   r   r   r   r   r   r   r   r   r   r  r  r%  r)  r2  rG  rN  rV  rX  r\  r^  rf  rh  rj  rl  rq  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r   s   0r   <module>r     s#   " 	 	       (0#B4
  	D)TDA xrxx;NN(,,2FH(5 6 (5!s1v (5 6 7
 D@ @<P$L$7.
)6X4B%6-,
$:(V= 	@A B3><$)6 % %             8  . C C 5 5  * B B 4 4  * A A 3 3  * A A 3 3   	 	 	 	  * 	B 	B 4 4  * 	B 	B 4 4{  	B
  t*K
  D
  #T
#
  JM"6s^   H H$ H8 I I 7I.H! H!$H54H58IIIII+*I+