def _ensure_forecast_measurement_compatibility(forecast, measurement):
    """Checks that a forecast is compatible with the measurement (observation
    or aggregate) it is compared to.

    Criteria:
    * matching variable
    * interval length of measurement is less than or equal to that of the
      forecast.
    * observations are made at the same site as the forecast
    * the forecast is made for the aggregate.

    Parameters
    ----------
    forecast: dict
    measurement: dict

    Returns
    -------
    dict
        Dictionary mapping field names to error messages. Dict will be empty
        if no issues are found.
    """
    errors = {}
    if forecast['variable'] != measurement['variable']:
        errors['variable'] = 'Must match forecast variable.'
    if forecast['interval_length'] < measurement['interval_length']:
        errors['interval_length'] = ('Must be less than or equal to '
                                     'forecast interval_length.')
    if 'site_id' in measurement:
        if forecast['site_id'] != measurement['site_id']:
            errors['site_id'] = 'Must match forecast site_id.'
    else:
        if forecast['aggregate_id'] != measurement['aggregate_id']:
            errors['aggregate_id'] = 'Must match forecast aggregate_id.'
    return errors