o
    Nrf                     @   sh   d dl mZ d dlmZ d dlZd dlZd dlZdde	fddZ
dd	 Zd
d ZdddZdddZdS )    )format_dense_tile)tile_boundsN   max_zoomc                 C   s  t | }|d  }|d  }|d  }|d  }|| }|| }	t||	}
t|dA}|jdt|dfdtj	d}|j
ddgdj|d	d	< ||jd
< ||jd< ||jd< ||jd< ||jd< |
|jd< W d	   n1 stw   Y  |j
ddgdS )z
    Convert a csv file containing points to a numpy array
    of [[x,y]] values.

    Parameters:
    -----------
    csv_file: string
        The filename of the data file

    xywvalues   gzip)compressionZdtype)columnsNmin_xmax_xmin_ymax_yr   	max_width)pdZread_csvminmaxh5pyFileZcreate_datasetlennpZfloat32Zreindexr	   attrs)Zcsv_fileZoutput_filer   dfr   r   r   r   widthheightr   Zf_outZdataset r   ^/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/density.pycsv_to_points	   s*   






r    c                 C   s   t | d1}|d j}t|d t|d gt|d t|d gt|d t|d d	d
W  d   S 1 s:w   Y  dS )z&
    Calculate the extent, etc...
    rr	   r   r   r   r   r   r   false)Zmin_posZmax_posr   r   Zmirror_tilesN)r   r   r   floatint)points_fileZf_inr   r   r   r   tileset_info/   s   


$r&   c                 C   st   | | dddf |d k } | | dddf |d k  } | | dddf |d k } | | dddf |d k  } | S )aI  
    Filter points that are within the extent

    Parameters:
    -----------
    data: [[]]
        A 2D numpy array containing x,y values

    extent: [x_start, x_end, y_start, y_end]
        The region we want to return points within

    Returns
    -------
    data: [[]]
        A 2D numpy array containing x,y values
    Nr   r
         r   )dataZextentr   r   r   filter_points?   s
   r*   r'   c                 C   s   g }t | dh}t|d dd tt| |||||}t|D ]E}	t|D ]>}
t|tt| |||	 ||
 }tj|dddf |dddf ddd j}tj	||dk< ||||	 ||
 f|fg7 }q'q!|W  d   S 1 ssw   Y  dS )	z
    Get a 2D histogram of the given region. If the height and
    width are specified, then we need to partition this into
    multiple returned tiles.
    r!   r	   Nr   r'      )Zbinsg        )
r   r   r*   r   r&   ranger   Zhistogram2dTnan)r%   zr   r   r   r   returnsfZ
all_pointsijZfiltered_pointsdtr   r   r   density_tiles[   s.   $r5   c                 C   s   dd t | |||||D S )Nc                 S   s    g | ]\}}|t | fqS r   )r   flatten).0Ztile_posr)   r   r   r   
<listcomp>|   s    ztiles.<locals>.<listcomp>)r5   )r%   r/   r   r   r   r   r   r   r   tiles{   s   r9   )r   )r'   r'   )Zclodius.tiles.formatr   Zclodius.tiles.utilsr   Zpandasr   numpyr   r   r$   r    r&   r*   r5   r9   r   r   r   r   <module>   s    &
 