<commit_before># coding=utf-8
from datetime import timedelta, datetime
import json
from string import capwords
from pytz import utc
import time
from urllib2 import urlopen

from flaskext.babel import lazy_gettext as _
from molly.apps.common.attribution import Attribution

class Provider(object):

    def __init__(self, config):
        self._config = config

    attribution = Attribution(
        licence_name='Open Government Licence',
        licence_url='http://www.nationalarchives.gov.uk/doc/open-government-licence/',
        attribution_text='Contains public sector information provided by the Met Office'
    )

    def latest_observations(self):
        response = json.load(
            urlopen('http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all' +
                '/json/{location_id}?res=hourly&key={api_key}'.format(**self._config))
        )

        source_observation = response['SiteRep']['DV']['Location']['Period'][-1]['Rep'][-1]
        minutes_since_midnight = timedelta(minutes=int(source_observation['$']))
        obs_time = datetime(
            *time.strptime(response['SiteRep']['DV']['Location']['Period'][-1]['value'], "%Y-%m-%dZ")[:6],
            tzinfo=utc
        )
        obs_time += minutes_since_midnight

        return {
            'type': self.WEATHER_TYPES.get(source_observation['W']),
            'temperature': u'{} °C'.format(source_observation['T']),
            'wind_speed': '{} mph'.format(source_observation['S']),
            'gust_speed': '{} mph'.format(source_observation['G']) if 'G' in source_observation else 'N/A',
            'wind_direction': source_observation['D'],
            'pressure': '{} mb'.format(source_observation['P']),
            'obs_location': capwords(response['SiteRep']['DV']['Location']['name']),
            'obs_time': obs_time.isoformat()
        }

    WEATHER_TYPES = {
        'NA': _('Not available'),
        '0': _('Clear night'),
        '1': _('Sunny day'),
        '2': _('Partly cloudy'),
        '3': _('Partly cloudy'),
        '4': _('Not used'),
        '5': _('Mist'),
        '6': _('Fog'),
        '7': _('Cloudy'),
        '8': _('Overcast'),
        '9': _('Light rain shower'),
        '10': _('Light rain shower'),
        '11': _('Drizzle'),
        '12': _('Light rain'),
        '13': _('Heavy rain shower'),
        '14': _('Heavy rain shower'),
        '15': _('Heavy rain'),
        '16': _('Sleet shower'),
        '17': _('Sleet shower'),
        '18': _('Sleet'),
        '19': _('Hail shower'),
        '20': _('Hail shower'),
        '21': _('Hail'),
        '22': _('Light snow shower'),
        '23': _('Light snow shower'),
        '24': _('Light snow'),
        '25': _('Heavy snow shower'),
        '26': _('Heavy snow shower'),
        '27': _('Heavy snow'),
        '28': _('Thunder shower'),
        '29': _('Thunder shower'),
        '30': _('Thunder')
    }<commit_msg>Add high level weather type IDs to observation<commit_after># coding=utf-8
from datetime import timedelta, datetime
import json
from string import capwords
from pytz import utc
import time
from urllib2 import urlopen

from flaskext.babel import lazy_gettext as _
from molly.apps.common.attribution import Attribution

class Provider(object):

    def __init__(self, config):
        self._config = config

    attribution = Attribution(
        licence_name='Open Government Licence',
        licence_url='http://www.nationalarchives.gov.uk/doc/open-government-licence/',
        attribution_text='Contains public sector information provided by the Met Office'
    )

    def latest_observations(self):
        response = json.load(
            urlopen('http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all' +
                '/json/{location_id}?res=hourly&key={api_key}'.format(**self._config))
        )

        source_observation = response['SiteRep']['DV']['Location']['Period'][-1]['Rep'][-1]
        minutes_since_midnight = timedelta(minutes=int(source_observation['$']))
        obs_time = datetime(
            *time.strptime(response['SiteRep']['DV']['Location']['Period'][-1]['value'], "%Y-%m-%dZ")[:6],
            tzinfo=utc
        )
        obs_time += minutes_since_midnight

        weather_type, weather_type_id = self.WEATHER_TYPES.get(source_observation['W'])

        return {
            'type': weather_type,
            'type_id': weather_type_id,
            'temperature': u'{} °C'.format(source_observation['T']),
            'wind_speed': '{} mph'.format(source_observation['S']),
            'gust_speed': '{} mph'.format(source_observation['G']) if 'G' in source_observation else 'N/A',
            'wind_direction': source_observation['D'],
            'pressure': '{} mb'.format(source_observation['P']),
            'obs_location': capwords(response['SiteRep']['DV']['Location']['name']),
            'obs_time': obs_time.isoformat()
        }

    WEATHER_TYPES = {
        'NA': (_('Not available'), ''),
        '0': (_('Clear night'), 'clear_night'),
        '1': (_('Sunny day'), 'sun'),
        '2': (_('Partly cloudy'), 'cloud'),
        '3': (_('Partly cloudy'), 'cloud'),
        '5': (_('Mist'), 'fog'),
        '6': (_('Fog'), 'fog'),
        '7': (_('Cloudy'), 'cloud'),
        '8': (_('Overcast'), 'cloud'),
        '9': (_('Light rain shower'), 'rain'),
        '10': (_('Light rain shower'), 'rain'),
        '11': (_('Drizzle'), 'rain'),
        '12': (_('Light rain'), 'rain'),
        '13': (_('Heavy rain shower'), 'rain'),
        '14': (_('Heavy rain shower'), 'rain'),
        '15': (_('Heavy rain'), 'rain'),
        '16': (_('Sleet shower'), 'rain'),
        '17': (_('Sleet shower'), 'rain'),
        '18': (_('Sleet'), 'rain'),
        '19': (_('Hail shower'), 'rain'),
        '20': (_('Hail shower'), 'rain'),
        '21': (_('Hail'), 'rain'),
        '22': (_('Light snow shower'), 'snow'),
        '23': (_('Light snow shower'), 'snow'),
        '24': (_('Light snow'), 'snow'),
        '25': (_('Heavy snow shower'), 'snow'),
        '26': (_('Heavy snow shower'), 'snow'),
        '27': (_('Heavy snow'), 'snow'),
        '28': (_('Thunder shower'), 'thunder'),
        '29': (_('Thunder shower'), 'thunder'),
        '30': (_('Thunder'), 'thunder')
    }
{'fraction_non_alphanumeric': 0.1985576923076923, 'fraction_numerical': 0.01842948717948718, 'mean_word_length': 2.7893139040680026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3421093', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1949, 'n_words': 423}