import re


def span_from_usgs_code(code):
    """
    Derive a span from a USGS geologic map unit code

    USGS geologic data has a convention of using the first letter (or two) to
    indiciate the age of the unit, roughly corresponding to the symbols at
    https://ngmdb.usgs.gov/fgdc_gds/geolsymstd/fgdc-geolsym-sec32.pdf
    """
    mapping = {
        "Cz": "cenozoic",
        "Q": "quaternary",
        "T": "tertiary",
        "N": "neogene",
        "Pe": "paleogene",
        "Mz": "mesozoic",
        "K": "cretaceous",
        "J": "jurassic",
        "Tr": "triassic",
        "Pz": "paleozoic",
        "P": "permian",
        "C": "carboniferous",
        # "P": "pennsylvanian", # not sure how to render this symbol
        "M": "mississippian",
        "D": "devonian",
        "S": "silurian",
        "O": "ordovician",
        # "C": "Cambrian"
        "pC": "precambrian",
        # "P": "proterozoic",
        "Z": "late proterozoic",
        "Y": "middle proterozoic",
        "Y3": "late middle proterozoic",
        "Y2": "middle middle proterozoic"
    }
    for prefix, span in mapping.items():
        if re.match(rf"^{prefix}", code):
            return span