# Path: lmatools/stream/subset.py
# def coroutine(func):
#     def start(*args,**kwargs):
#         cr = func(*args,**kwargs)
#         next(cr)
#         return cr
#     return start
# 
# Path: lmatools/density_tools.py
# def unique_vectors(*args, **kwargs):
#     """ Given D, N-element arrays of vector components return the
#         unique vectors as a (D, N_reduced) array. If return_indices_only=True
#         is true (the default) then return the N_reduced indices of the original
#         arrays corresponding to the unique vectors.
#         
#         args: x0_i, x1_i, x2_i, ...
#             where each x0, x1, etc. are discretized (bin index) values of point
#             locations.
#         """
#     try:
#         return_indices_only=kwargs['return_indices_only']
#     except KeyError:
#         return_indices_only=True
#         
#     vector_len = len(args)
#     itemsize = max((x_i.itemsize for x_i in args))
#     inttype = 'i{0:d}'.format(itemsize)
#     
#     vec_cast = tuple((x_i.astype(inttype) for x_i in args))
#         
#     # Because we do casting below, ascontiguousarray is important
#     locs = np.ascontiguousarray(np.vstack(vec_cast).T)
# 
# 	# Find unique rows (unique grid cells occupied per flash) by casting to a set
# 	# of strings comprised of the bytes that make up the rows. Strings are not serialized, just ints cast to byte strings.
# 	# Based on the example at
# 	# http://www.mail-archive.com/numpy-discussion@scipy.org/msg04176.html
#     # which doesn't quite get it right: it returns unique elements.
#     vectorbytes = 'S{0:d}'.format(itemsize*vector_len)
#     unq, unq_index = np.unique(locs.view(vectorbytes), return_index=True)
# 
#     if return_indices_only==True:
#         return unq_index
#     else:
#         # this is unnecessary if we only need the row indices
#         unq_restored = unq.view(inttype).reshape(unq.shape[0],vector_len)
#         return unq_restored, unq_index


import glob
import gc
import numpy as np
import logging
from lmatools.stream.subset import coroutine
from lmatools.density_tools import unique_vectors
    """ Adds projected coordinates to the flash and events stream"""
    while True:
        events, flashes = (yield)
        if use_flashes==True:
            points = flashes
        else:
            points = events
        if transform:
            x,y,z = mapProj.fromECEF(*geoProj.toECEF(
                        points[x_coord], points[y_coord], points[z_coord]))
        else:
            x,y,z = points[x_coord], points[y_coord], points[z_coord]
        target.send((events, flashes, np.atleast_1d(x),
                     np.atleast_1d(y), np.atleast_1d(z)))
        del events, flashes, x,y,z, points

@coroutine
def footprint_mean(flash_id_key='flash_id', area_key='area'):
    """ Takes x, y, z flash locations and gets 
        Extent density unique pixels, average all flashes 
    """
    while True:
        events, flash, x,y,z = (yield)
        # print 'Doing extent density',
        x_i = np.floor( (x-x0)/dx ).astype('int32')
        y_i = np.floor( (y-y0)/dy ).astype('int32')

        if len(x_i) > 0:
            footprints = dict(list(zip(flash[flash_id_key], flash[area_key])))
            # print 'with points numbering', len(x_i)

# Path: lmatools/stream/subset.py
# def coroutine(func):
#     def start(*args,**kwargs):
#         cr = func(*args,**kwargs)
#         next(cr)
#         return cr
#     return start
# 
# Path: lmatools/density_tools.py
# def unique_vectors(*args, **kwargs):
#     """ Given D, N-element arrays of vector components return the
#         unique vectors as a (D, N_reduced) array. If return_indices_only=True
#         is true (the default) then return the N_reduced indices of the original
#         arrays corresponding to the unique vectors.
#         
#         args: x0_i, x1_i, x2_i, ...
#             where each x0, x1, etc. are discretized (bin index) values of point
#             locations.
#         """
#     try:
#         return_indices_only=kwargs['return_indices_only']
#     except KeyError:
#         return_indices_only=True
#         
#     vector_len = len(args)
#     itemsize = max((x_i.itemsize for x_i in args))
#     inttype = 'i{0:d}'.format(itemsize)
#     
#     vec_cast = tuple((x_i.astype(inttype) for x_i in args))
#         
#     # Because we do casting below, ascontiguousarray is important
#     locs = np.ascontiguousarray(np.vstack(vec_cast).T)
# 
# 	# Find unique rows (unique grid cells occupied per flash) by casting to a set
# 	# of strings comprised of the bytes that make up the rows. Strings are not serialized, just ints cast to byte strings.
# 	# Based on the example at
# 	# http://www.mail-archive.com/numpy-discussion@scipy.org/msg04176.html
#     # which doesn't quite get it right: it returns unique elements.
#     vectorbytes = 'S{0:d}'.format(itemsize*vector_len)
#     unq, unq_index = np.unique(locs.view(vectorbytes), return_index=True)
# 
#     if return_indices_only==True:
#         return unq_index
#     else:
#         # this is unnecessary if we only need the row indices
#         unq_restored = unq.view(inttype).reshape(unq.shape[0],vector_len)
#         return unq_restored, unq_index


# Path: lmatools/grid/density_to_files.py
import glob
import gc
import numpy as np
import logging
from lmatools.stream.subset import coroutine
from lmatools.density_tools import unique_vectors

    """ Adds projected coordinates to the flash and events stream"""
    while True:
        events, flashes = (yield)
        if use_flashes==True:
            points = flashes
        else:
            points = events
        if transform:
            x,y,z = mapProj.fromECEF(*geoProj.toECEF(
                        points[x_coord], points[y_coord], points[z_coord]))
        else:
            x,y,z = points[x_coord], points[y_coord], points[z_coord]
        target.send((events, flashes, np.atleast_1d(x),
                     np.atleast_1d(y), np.atleast_1d(z)))
        del events, flashes, x,y,z, points

@coroutine
def footprint_mean(flash_id_key='flash_id', area_key='area'):
    """ Takes x, y, z flash locations and gets 
        Extent density unique pixels, average all flashes 
    """
    while True:
        events, flash, x,y,z = (yield)
        # print 'Doing extent density',
        x_i = np.floor( (x-x0)/dx ).astype('int32')
        y_i = np.floor( (y-y0)/dy ).astype('int32')

        if len(x_i) > 0:
            footprints = dict(list(zip(flash[flash_id_key], flash[area_key])))
            # print 'with points numbering', len(x_i)

            unq_idx = unique_vectors(x_i, y_i, events['flash_id'])