def make_safety_decision(cfg, logger=None):
    '''
    Method makes decision whether conditions are safe or unsafe.
    '''
    if logger:
        logger.info('Making safety decision')
    # If sky-amb > threshold, then cloudy (safe)
    if 'threshold_cloudy' in cfg.keys():
        threshold_cloudy = cfg['threshold_cloudy']
    else:
        if logger:
            logger.warning('Using default cloudy threshold')
        threshold_cloudy = -20
    # If sky-amb > threshold, then very cloudy (unsafe)
    if 'threshold_very_cloudy' in cfg.keys():
        threshold_very_cloudy = cfg['threshold_very_cloudy']
    else:
        if logger:
            logger.warning('Using default very cloudy threshold')
        threshold_very_cloudy = -15
    # If avg_wind > threshold, then windy (safe)
    if 'threshold_windy' in cfg.keys():
        threshold_windy = cfg['threshold_windy']
    else:
        if logger:
            logger.warning('Using default windy threshold')
        threshold_windy = 20
    # If avg_wind > threshold, then very windy (unsafe)
    if 'threshold_very_windy' in cfg.keys():
        threshold_very_windy = cfg['threshold_very_windy']
    else:
        if logger:
            logger.warning('Using default very windy threshold')
        threshold_very_windy = 30
    # If wind > threshold, then gusty (safe)
    if 'threshold_gusty' in cfg.keys():
        threshold_gusty = cfg['threshold_gusty']
    else:
        if logger:
            logger.warning('Using default gusty threshold')
        threshold_gusty = 40
    # If wind > threshold, then very gusty (unsafe)
    if 'threshold_very_gusty' in cfg.keys():
        threshold_very_gusty = cfg['threshold_very_gusty']
    else:
        if logger:
            logger.warning('Using default very gusty threshold')
        threshold_very_gusty = 50
    # If rain frequency < threshold, then unsafe
    if 'threshold_rainy' in cfg.keys():
        threshold_rain = cfg['threshold_rainy']
    else:
        if logger:
            logger.warning('Using default rain threshold')
        threshold_rain = 230

    # Get Last n minutes of data
    if 'safety_delay' in cfg.keys():
        safety_delay = cfg['safety_delay']
    else:
        if logger:
            logger.warning('Using default safety delay')
        safety_delay = 15.
    end = dt.utcnow()
    start = end - tdelta(0, int(safety_delay * 60))
    db = PanMongo()
    entries = [x for x in db.weather.find({'date': {'$gt': start, '$lt': end}})]
    if logger:
        logger.info('  Found {} weather data entries in last {:.0f} minutes'.format(len(entries), safety_delay))

    # Cloudiness
    sky_diff = [x['data']['Sky Temperature (C)'] - x['data']['Ambient Temperature (C)']
                for x in entries
                if 'Ambient Temperature (C)' in x['data'].keys() and 'Sky Temperature (C)' in x['data'].keys()]

    if len(sky_diff) == 0:
        if logger:
            logger.info('  UNSAFE: no sky tempeartures found')
        sky_safe = False
        cloud_condition = 'Unknown'
    else:
        if max(sky_diff) > threshold_very_cloudy:
            if logger:
                logger.info('  UNSAFE:  Very cloudy in last {:.0f} min.'
                            '  Max sky diff {:.1f} C'.format(safety_delay, max(sky_diff)))
            sky_safe = False
        else:
            sky_safe = True

        if sky_diff[-1] > threshold_very_cloudy:
            cloud_condition = 'Very Cloudy'
        elif sky_diff[-1] > threshold_cloudy:
            cloud_condition = 'Cloudy'
        else:
            cloud_condition = 'Clear'
        if logger:
            logger.info('  Cloud Condition: {} (Sky-Amb={:.1f} C)'.format(cloud_condition, sky_diff[-1]))

    # Wind (average and gusts)
    wind_speed = [x['data']['Wind Speed (km/h)']
                  for x in entries
                  if 'Wind Speed (km/h)' in x['data'].keys()]

    if len(wind_speed) == 0:
        if logger:
            logger.info('  UNSAFE: no wind speed readings found')
        wind_safe = False
        gust_safe = False
        wind_condition = 'Unknown'
        gust_condition = 'Unknown'
    else:
        typical_data_interval = (end - min([x['date'] for x in entries])).total_seconds() / len(entries)
        mavg_count = int(np.ceil(120. / typical_data_interval))
        wind_mavg = movingaverage(wind_speed, mavg_count)

        # Windy?
        if max(wind_mavg) > threshold_very_windy:
            if logger:
                logger.info('  UNSAFE:  Very windy in last {:.0f} min.'
                            '  Max wind speed {:.1f} kph'.format(safety_delay, max(wind_mavg)))
            wind_safe = False
        else:
            wind_safe = True
        if wind_mavg[-1] > threshold_very_windy:
            wind_condition = 'Very Windy'
        elif wind_mavg[-1] > threshold_windy:
            wind_condition = 'Windy'
        else:
            wind_condition = 'Calm'
        if logger:
            logger.info('  Wind Condition: {} ({:.1f} km/h)'.format(wind_condition, wind_mavg[-1]))

        # Gusty?
        if max(wind_speed) > threshold_very_gusty:
            if logger:
                logger.info('  UNSAFE:  Very gusty in last {:.0f} min.'
                            '  Max gust speed {:.1f} kph'.format(safety_delay, max(wind_speed)))
            gust_safe = False
        else:
            gust_safe = True
        if wind_speed[-1] > threshold_very_gusty:
            gust_condition = 'Very Gusty'
        elif wind_speed[-1] > threshold_windy:
            gust_condition = 'Gusty'
        else:
            gust_condition = 'Calm'
        if logger:
            logger.info('  Gust Condition: {} ({:.1f} km/h)'.format(gust_condition, wind_speed[-1]))

    # Rain
    rf_value = [x['data']['Rain Frequency']
                for x in entries
                if 'Rain Frequency' in x['data'].keys()]

    if len(rf_value) == 0:
        rain_safe = False
        rain_condition = 'Unknown'
    else:
        if min(rf_value) < threshold_rain:
            if logger:
                logger.info('  UNSAFE:  Rain in last {:.0f} min.'.format(safety_delay))
            rain_safe = False
        else:
            rain_safe = True
        if rf_value[-1] < threshold_rain:
            rain_condition = 'Rain'
        else:
            rain_condition = 'Dry'
        if logger:
            logger.info('  Rain Condition: {}'.format(rain_condition))

    safe = sky_safe & wind_safe & gust_safe & rain_safe
    translator = {True: 'safe', False: 'unsafe'}
    if logger:
        logger.info('Weather is {}'.format(translator[safe]))

    safe_dict = {'Safe': safe,
                 'Sky': cloud_condition,
                 'Wind': wind_condition,
                 'Gust': gust_condition,
                 'Rain': rain_condition}
    return safe_dict
dataset/ETHPy150Open panoptes/POCS/scripts/read_weather.py/make_safety_decision/original