import math


def met_scatter_lims(met_data, param, met_param, xlims, ylims, serials,
                     avg_df):
    """Set axes limits for plots generated by ``normalized_met_scatter()``.

    Args:
        met_data (pandas DataFrame):
            Reference dataset containing meteorological data for either
            temperature or relative humidity, logged at 1-hour averages.
        param (str):
            The name of the SDFS parameter for which normalized sensor-reference
            concentration pairs will be displayed along the y-axis.
        met_param (str):
            The name of the meteorological parameter displayed along the x-axis.
            The name is the SDFS parameter name assocaited with the
            meteorological parameter; for temperature, pass ``'Temp'``, for
            relative humidity, pass ``'RH'``.
        xlims (Two-element tuple):
            The x-limits for the normalized meteorological scatterplot. Data
            along the x-axis are meteorological measurements (either temperature
            or relative humidity).
        ylims (Two-element tuple):
            The y-limits for the normalized meteorological scatterplot. Data
            along the y-axis are normalized sensor/reference concentration
            pairs.
        serials (dict):
            A dictionary of serial identifiers unique to each sensor in the
            deployment testing group.
        avg_df (pandas DataFrame):
            A dataset containing the intersensor average for concurrently
            recorded sensor measurements for each parameter measured by the
            air sensor. The 1st and 99th percentile of the normalized
            (sensor/reference) intersensor average values are used to set
            the y-limits for plots.

    Returns:
        (tuple): Four-element tuple containing:
            - **xmin** (*float*)
            - **xmax** (*float*)
            - **ymin** (*float*)
            - **ymax** (*float*)

    """
    # Automatically generate x-axis limits if none specified
    if xlims is None:
        xmax = met_data[met_param + '_Value'].max()
        xmin = met_data[met_param + '_Value'].min()

        xmax = round(xmax, -1)
        xmin = math.floor(xmin)
    else:
        xmin, xmax = xlims[0], xlims[1]

    # Automatically generate y-axis limits if none specified
    if ylims is None:
        ymin = avg_df['mean_Normalized_' + param + '_Value'].quantile(0.01)
        ymax = avg_df['mean_Normalized_' + param + '_Value'].quantile(0.99)

        if ymax < 5.0:
            rounding_place = 1  # round to nearest tenths place
        else:
            rounding_place = -1  # round to nearest tens place

        ymin = math.floor(ymin) - 0.1*ymin
        ymax = round(ymax, rounding_place) + 0.1*ymax

        if ymin == ymax == 0:
            ymin, ymax = -0.1, 1.1
        if ymin < 0.1 and ymin > -.1:
            ymin = -0.5
        if ymax < 1.0:
            ymin = -1*ymax
            ymax = 1.1
    else:
        ymin, ymax = ylims[0], ylims[1]

    return (xmin, xmax), (ymin, ymax)