def spatial_agg(df, deg=2.5): 
    """
    For a given df calculate aggregation (count, mean, median, std, min, max) 
    for data variables (temperature, pressure, humidity, magnetic_tot)
    grouped by latitude and longitude category

    Parameters
    ----------
    df: pandas DataFrame
        Pandas dataframe with data for the desired sampling range (hourly, daily)
    
    deg: int or float, default 2.5
        Spatial degree interval for for latitude and longitude data
    
    Returns
    -------
    data_agg: pandas DataFrame
        DataFrame with aggregated data for every atmospheric variable

    data_count: pandas Series
        Series with count of data points for every location
    """
    # Group data points by lat, lng categories
    df = df.discretize_latlng(deg=deg)

    # create a groupby object grouped by lat, lng categories
    grouped = df.groupby(by=["lat_cat","lng_cat"])

    # custom agg functions to calculate na count and percentage 
    na_pct = lambda df: df.isna().mean()
    na_count = lambda df: df.isna().sum()

    # group by custom functions
    na_pct = grouped.agg([na_pct]).rename({"<lambda>":"na_pct"}, axis=1)
    na_cnt = grouped.agg([na_count]).rename({"<lambda>":"na_count"}, axis=1)

    # group by regular statistics
    agg = grouped.agg(["mean","median","std","min","max","count"])

    # join all groups and reshape dataframe so it has statistics as index not columns
    agg = agg.join([na_cnt, na_pct]).T.unstack().T

    # rename indices and columns for readability
    agg.columns.names = ["atmos"]
    agg.index.names = ["lat", "lng", "stat"]

    return agg