import glob
import re


def find_lidar_file(image_path, dirname):
    """
    Find the lidar file that matches RGB tile
    """
    #Look for lidar tile
    laz_files = glob.glob(dirname + "*.laz")

    #extract geoindex
    pattern = r"_(\d+_\d+_image.*)"
    match = re.findall(pattern, image_path)

    #replace .tif with .laz pattern
    match = [x.replace("image", "classified_point_cloud") for x in match]
    match = [x.replace(".tif", ".laz") for x in match]
    if len(match) > 0:
        match = match[0]
    else:
        return None

    #Look for index in available laz
    laz_path = None
    for x in laz_files:
        if match in x:
            laz_path = x

    #Raise if no file found
    if not laz_path:
        print("No matching lidar file, check the lidar path: %s" % (dirname))
        FileNotFoundError

    return laz_path