import pickle


def preprocessingPixelHeadwall(x, area=None):
    """Preprocess Headwall pixel with 146 bands.

    Calculate `mean` and `std` with `calcPreprocessingStats()`

    Parameters
    ----------
    x : list or array
        Original input pixel(s) with 146 spectral bands
    area : str or None, optional (default=None)
        Name of measurement area as str. If `None`, all areas are used.

    """
    # get mean and std
    file_path = "data/scaling/"
    if area is None:
        file_path += "area_all"
    else:
        file_path += "area_"+str(area)
    mean = pickle.load(open(file_path+"_mean.p", "rb"))
    std = pickle.load(open(file_path+"_std.p", "rb"))

    # loop over image channels
    for idx, mean_value in enumerate(mean):
        x[..., idx] -= mean_value
        x[..., idx] /= std[idx]
    return x