# Path: asammdf/types.py



from collections.abc import Iterator
from functools import lru_cache
from io import StringIO
from pathlib import Path
from random import randint
from struct import Struct
from tempfile import TemporaryDirectory
from typing import Any, Dict, overload, Tuple
from typing_extensions import Literal, TypedDict
    from cchardet import detect
        from chardet import detect
from canmatrix.canmatrix import CanMatrix
from numpy import arange, bool_, dtype, interp, where
from numpy.typing import NDArray
from pandas import Series
from . import v2_v3_constants as v3c
from . import v4_constants as v4c
from ..types import (
    ChannelType,
    DataGroupType,
    MDF_v2_v3_v4,
    RasterType,
    ReadableBufferType,
    StrPathType,
)
import logging
import re
import string
import subprocess
import sys
import xml.etree.ElementTree as ET
import canmatrix.formats
import numpy as np

def downcast(array: NDArray[Any]) -> NDArray[Any]:
    kind = array.dtype.kind
    if kind == "f":
        array = array.astype(np.float32)
    elif kind in "ui":
        min_ = array.min()
        max_ = array.max()
        if min_ >= 0:
            if max_ < 255:
                array = array.astype(np.uint8)
            elif max_ < 65535:
                array = array.astype(np.uint16)
            elif max_ < 4294967295:
                array = array.astype(np.uint32)
            else:
                array = array.astype(np.uint64)
        else:
            if min_ > np.iinfo(np.int8).min and max_ < np.iinfo(np.int8).max:
                array = array.astype(np.int8)
            elif min_ > np.iinfo(np.int16).min and max_ < np.iinfo(np.int16).max:
                array = array.astype(np.int16)
            elif min_ > np.iinfo(np.int32).min and max_ < np.iinfo(np.int32).max:
                array = array.astype(np.int32)
            elif min_ > np.iinfo(np.int64).min and max_ < np.iinfo(np.int64).max:
                array = array.astype(np.int64)

    return array


def master_using_raster(

# Path: asammdf/types.py



# Path: asammdf/blocks/utils.py
from collections.abc import Iterator
from functools import lru_cache
from io import StringIO
from pathlib import Path
from random import randint
from struct import Struct
from tempfile import TemporaryDirectory
from typing import Any, Dict, overload, Tuple
from typing_extensions import Literal, TypedDict
    from cchardet import detect
        from chardet import detect
from canmatrix.canmatrix import CanMatrix
from numpy import arange, bool_, dtype, interp, where
from numpy.typing import NDArray
from pandas import Series
from . import v2_v3_constants as v3c
from . import v4_constants as v4c
from ..types import (
    ChannelType,
    DataGroupType,
    MDF_v2_v3_v4,
    RasterType,
    ReadableBufferType,
    StrPathType,
)
import logging
import re
import string
import subprocess
import sys
import xml.etree.ElementTree as ET
import canmatrix.formats
import numpy as np

def downcast(array: NDArray[Any]) -> NDArray[Any]:
    kind = array.dtype.kind
    if kind == "f":
        array = array.astype(np.float32)
    elif kind in "ui":
        min_ = array.min()
        max_ = array.max()
        if min_ >= 0:
            if max_ < 255:
                array = array.astype(np.uint8)
            elif max_ < 65535:
                array = array.astype(np.uint16)
            elif max_ < 4294967295:
                array = array.astype(np.uint32)
            else:
                array = array.astype(np.uint64)
        else:
            if min_ > np.iinfo(np.int8).min and max_ < np.iinfo(np.int8).max:
                array = array.astype(np.int8)
            elif min_ > np.iinfo(np.int16).min and max_ < np.iinfo(np.int16).max:
                array = array.astype(np.int16)
            elif min_ > np.iinfo(np.int32).min and max_ < np.iinfo(np.int32).max:
                array = array.astype(np.int32)
            elif min_ > np.iinfo(np.int64).min and max_ < np.iinfo(np.int64).max:
                array = array.astype(np.int64)

    return array


def master_using_raster(

    mdf: MDF_v2_v3_v4, raster: RasterType, endpoint: bool = False
