# Path: lmatools/io/LMA_h5_file.py
# def parse_lma_h5_filename(filename):
#     file_parts = os.path.split(filename)[-1].split('_')
#     date_parts = file_parts[1:3]
#     start = datetime.strptime('_'.join(date_parts), '%y%m%d_%H%M%S')
#     duration = float(file_parts[3].split('.')[0])
#     end = start + timedelta(0, duration)
#     return start, end


import os, glob, itertools
import logging, json, operator
import numpy as np
from datetime import datetime, timedelta
from lmatools.io.LMA_h5_file import parse_lma_h5_filename
    from shapely.geometry import Polygon
    all_verts = np.asarray([p for p in itertools.chain(*polys)])
    sw_corner = all_verts.min(axis=0)
    ne_corner = all_verts.max(axis=0)
    lons = (sw_corner[0], ne_corner[0])
    lats = (sw_corner[1], ne_corner[1])
    return lons, lats
    
    
def h5_files_from_standard_path(path_to_sort_results, date_min, date_max):
    """ Path contains a set of directories with h5 files as follows
        path/h5_files/yyyy/Mon/dd/LYLOUT*.h5
        where Mon is the three-letter month name (e.g., Dec).
        This is the directory structure produced by many of the lmatools examples.
        date_min and date_max are datetime objects given the range of dates to examine.
        Return a list of unique HDF5 filenames (sorted by filename) on that path.
    
        Right now, date_min and date_max can only be used to span a single day 
        boundary.
    """
    globber_start = path_to_sort_results+'/h5_files/{0}/*.h5'.format(date_min.strftime('%Y/%b/%d'))
    globber_end = path_to_sort_results+'/h5_files/{0}/*.h5'.format(date_max.strftime('%Y/%b/%d'))
    # print globber_start
    # print globber_end
    h5_filenames = glob.glob(globber_start)
    h5_filenames += glob.glob(globber_end)
    # Keep only the unique filenames
    h5_filenames = set(h5_filenames)

    reduced_h5 = []
    for h5i in h5_filenames:

# Path: lmatools/io/LMA_h5_file.py
# def parse_lma_h5_filename(filename):
#     file_parts = os.path.split(filename)[-1].split('_')
#     date_parts = file_parts[1:3]
#     start = datetime.strptime('_'.join(date_parts), '%y%m%d_%H%M%S')
#     duration = float(file_parts[3].split('.')[0])
#     end = start + timedelta(0, duration)
#     return start, end


# Path: lmatools/lasso/cell_lasso_util.py
import os, glob, itertools
import logging, json, operator
import numpy as np
from datetime import datetime, timedelta
from lmatools.io.LMA_h5_file import parse_lma_h5_filename
    from shapely.geometry import Polygon

    all_verts = np.asarray([p for p in itertools.chain(*polys)])
    sw_corner = all_verts.min(axis=0)
    ne_corner = all_verts.max(axis=0)
    lons = (sw_corner[0], ne_corner[0])
    lats = (sw_corner[1], ne_corner[1])
    return lons, lats
    
    
def h5_files_from_standard_path(path_to_sort_results, date_min, date_max):
    """ Path contains a set of directories with h5 files as follows
        path/h5_files/yyyy/Mon/dd/LYLOUT*.h5
        where Mon is the three-letter month name (e.g., Dec).
        This is the directory structure produced by many of the lmatools examples.
        date_min and date_max are datetime objects given the range of dates to examine.
        Return a list of unique HDF5 filenames (sorted by filename) on that path.
    
        Right now, date_min and date_max can only be used to span a single day 
        boundary.
    """
    globber_start = path_to_sort_results+'/h5_files/{0}/*.h5'.format(date_min.strftime('%Y/%b/%d'))
    globber_end = path_to_sort_results+'/h5_files/{0}/*.h5'.format(date_max.strftime('%Y/%b/%d'))
    # print globber_start
    # print globber_end
    h5_filenames = glob.glob(globber_start)
    h5_filenames += glob.glob(globber_end)
    # Keep only the unique filenames
    h5_filenames = set(h5_filenames)

    reduced_h5 = []
    for h5i in h5_filenames:

        h5time, h5endtime = parse_lma_h5_filename(h5i)