o
    NrfL                     @   sL   d dl mZ d dlZd dlZdd ZdddZdddZdd	 Z	d
d Z
dS )    Nc                 C   sd   | d }d|j v r|j d }nd}d|j v r|j d }n|j d }|||j d |j d |j d d	S )
zt
    Get information about the tileset.

    :param hdf_file: A file handle for an HDF5 file (h5py.File('...'))
    metazmin-posr   zmax-posz
max-lengthz	max-widthmax-zoom	tile-size)max_posmin_pos	max_widthmax_zoom	tile_size)attrs)hdf_filedr   r    r   Z/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/hdf_tiles.pyget_tileset_info   s   


r   c                 C   s`   |dk rt d|du rt| }||k r.|| d }|| | |dk r(|d }n|}||k s|S )a  Bisect_left with with an additional comparator.

    Based on the bisect_left function from the python bisect module.

    Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.

    Args:
        a (array): The array to bisect
        x (object): The object to find the insertion point of
        lo (int): The starting index of items to search in a
        hi (int): The end index of items to search in a
        comparator (function(a,b)): A way to compare objects
    r   lo must be non-negativeN      
ValueErrorlenaxlohi
comparatormidr   r   r   bisect_left!   s   
r   c                 C   s`   |dk rt d|du rt| }||k r.|| d }||| | dk r&|}n|d }||k s|S )a   Bisect_right with with an additional comparator.

    Based on the bisect_right function from the python bisect module.

    Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.


    Args:
        a (array): The array to bisect
        x (object): The object to find the insertion point of
        lo (int): The starting index of items to search in a
        hi (int): The end index of items to search in a
        comparator (function(a,b)): A way to compare objects
    r   r   Nr   r   r   r   r   r   r   bisect_rightD   s   r   c                 C   s   |d| krt d g S |dk rt d g S | d }t|jd }t|jd }| t| }|d||   }|| }|| }	dd	 }
d
d }t||g|
d}t||	g|
d}||| S )ak  
    Get a discrete set of data from an hdf_tile file.

    Args:
        hdf_file (h5py.File): File handle for the file containing the information
        z (int): The zoom level of this tile
        x (int): The x position of this tile

    Returns:
        A 2D array of entries at that position. It is assumed that names of the
        columns are known.
    r   OUT OF RIGHT RANGEr   OUT OF LEFT RANGEr   r   r   c                 S      t | d t |d  S )Nr   intr   br   r   r   comparator_start      z+get_discrete_data.<locals>.comparator_startc                 S   r!   )Nr   r"   r$   r   r   r   comparator_end   r'   z)get_discrete_data.<locals>.comparator_end)r   )printr#   r
   strr   r   )r   zr   r   r	   r   fZ
tile_widthZ
tile_startZtile_endr&   r(   Ztile_data_startZtile_data_endr   r   r   get_discrete_datag   s$   r-   c                 C   s  |d| krt d g S |dk rt d g S | d }t|jd }t|jd }t|jd }|d|  }d	|jv rBt|jd	 }n|}|| }	|t|	|  }
|	|
 }d| }|| }t|d|  | }t|| }t|d|
  }	 | d
tt|
  }||krt|| }|tj	 t
|t|}n0||k r||k r||| dd }tj	||d |< t
|t|}nt
||| t|}	 d}dtt|
 | v r
| dtt|
  }t
||| t|}d||  }tt|}|| || }|| }|S |S )z
    Return a tile from an hdf_file.

    :param hdf_file: A file handle for an HDF5 file (h5py.File('...'))
    :param z: The zoom level
    :param x: The x position of the tile
    r   r   r   r    r   r   z	zoom-stepr   zmax-positionZvalues_Nr   Znan_values_)r)   r#   r
   mathfloorr*   npZzerosfillnanctZ	aggregater   )r   r+   r   r   r	   Z	zoom_stepr   r   Zmax_positionZrzZnext_stored_zoomZzoom_offsetZ
num_to_aggZtotal_in_length	start_posend_posr,   r   Z	ret_arrayZf_nanZ	nan_arrayZnum_aggregatedZnum_vals_arrayZnum_summed_arrayZaverages_arrayr   r   r   get_data   sZ   

	
r6   )r   NN)Zclodius.arrayarrayr3   r.   numpyr0   r   r   r   r-   r6   r   r   r   r   <module>   s    

##3