o
    Nrfv                     @   sL   d dl Z d dlZd dlm  mZ dddZdddZdd Z	dd	d
ZdS )    Nc           	      C   sp   g }|D ]1}| d}t|dk rtdt|d }t|d }t| |||d}||t|fg7 }q|S )N.   zNot enough tile info present      )splitlen
IndexErrorinttilesreshapectfZformat_dense_tile)	arrayZtile_idsnot_nan_arrayZtile_valuesZtile_idpartszx	ret_array r   _/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/npvector.pytiles_wrapper   s   
r      c           	      C   s   t | j}tt|| td }|dk rdn|}d| | }|| }|r=|d g}|d g}|d |d  | }n	dg}| jd g}t| jdkrStd| j||||||dS )z,
    Get the tileset info for the array
    r   r   r   z$The array shape is not a vector type)	max_widthmin_posmax_posmax_zoombins_per_dimensionZ	tile_size)maxshapemathceillogr   
ValueError)	r   Zboundsr   max_dimr   r   Zscale_upr   r   r   r   r   tileset_info   s(   


r$   c           	      C   sn   | j d }tt|| td }|dk rdn|}d||  | }|| }t| j d || }|||fS )a  
    Return the maximum zoom level and data corresponding to the
    zoom level and position of the array.

    Parameters
    ----------
    array: np.array
        The array containing the raw numpy data
    z: int
        The zoom level
    x: int
        The x position
    r   r   )r   r   r    r!   min)	r   r   r   bin_sizer#   r   Z
tile_widthx_startx_endr   r   r   max_zoom_and_data_bounds>   s   

r)   c                 C   s  t | |||\}}}| || }d||  }	|	t|jd |	  }
|
|jd  }tj|d|ffdtjfd}tj|d|	fdd}|du rRt	| ||  }n||| }tj|d|fdtjfd}tj|d|	fdd}||d  }||jd  }tj|d|fdtjfdS )	a  
    Return tiles at the given positions.

    Parameters
    -----------
    array: np.array
        An nxn array containing values
    z: int
        The zoom level (0 corresponds to most zoomed out)
    x: int
        The x tile position
    not_nan_array: np.array
        An array storing the number of values which are not nan
        in the original array. Can be precalculated for speed.
    bin_size: int
        The number of values per bin
    r   r   Zconstant)Zconstant_valuesr   r   )ZaxisN)
r)   r   r    r   nppadnanZnansumr   isnan)r   r   r   r   r&   r   r'   r(   dataZ
num_to_sumZdivisible_x_widthZdivisible_x_padar   Znot_nan_datanaZ
norm_arrayZx_padr   r   r   r   ^   s"   r   )N)Nr   )
r   numpyr*   Zclodius.tiles.formatr   formatr   r   r$   r)   r   r   r   r   <module>   s    

% 