<jupyter_start><jupyter_text>Dark Sky API: Current Weather This script gets and prints current weather data from [Dark Sky API](https://darksky.net/dev) in the CLI. An API key is required.  Shebang/Interpreter Directive<jupyter_code>#!/usr/bin/env python3<jupyter_output><empty_output><jupyter_text>Import [urllib3](https://pypi.python.org/pypi/urllib3), [simplejson](https://pypi.python.org/pypi/simplejson/) and [datetime](https://docs.python.org/3/library/datetime.html) If `import json` does not work, then try `simplejson`. My Jupyter Notebook needed to import `json`.<jupyter_code>import urllib3 as ul3
import json
import datetime<jupyter_output><empty_output><jupyter_text>Disable Warnings SSL certification verification returns `InsecureRequestWarning`, so `urllib3.disable_warnings()` can be used to suppress the warning.<jupyter_code>ul3.disable_warnings(ul3.exceptions.InsecureRequestWarning)<jupyter_output><empty_output><jupyter_text>API Key and Geographic Coordinates The following strings fill out the Dark Sky API _key_, followed by geographic _coordinates_ of the weather location.<jupyter_code>k = '<KEY>'  # example
c = '40.7306, -73.9352'<jupyter_output><empty_output><jupyter_text>urllib3.PoolManager, request() `urllib3.PoolManager()` class is used for making the server request.<jupyter_code>h = ul3.PoolManager()
r = h.request('GET', 'https://api.darksky.net/forecast/' + k + '/' + c)<jupyter_output><empty_output><jupyter_text>Convert Angle to Cardinal Direction `windBearing`'s value is an integer which indicates angle of wind direction with true north at 0°. So the following function converts that angle to a cardinal direction letter(s).<jupyter_code>def degrees_to_cardinal(d):
    dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
            "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]
    ix = int((d + 11.25)/22.5 - 0.02)
    return dirs[ix % 16]<jupyter_output><empty_output><jupyter_text>Format Weather Data The following print calls format the weather data for the CLI. Unfortunately, there are two `KeyError`'s which indicate that data was unavailable at the present time. Which is the purpose for the two try/except clauses below. Based on that, additional try/except clauses can be added, if other `KeyError` exceptions are experienced.<jupyter_code>print("\n\033[1;34mCurrent Weather\033[0;0m (data provided thru Dark Sky API)\n")
print("Date/Time:", datetime.datetime.fromtimestamp(json.loads(r.data.decode('utf-8'))['currently']['time']))
print("Summary  :", json.loads(r.data.decode('utf-8'))['currently']['summary'], '\n')
print("Distance to nearest storm:", json.loads(r.data.decode('utf-8'))['currently']['nearestStormDistance'], "miles")
print("\033[1;34mPrecipitation:\033[0;0m")
print("* intensity             :", json.loads(r.data.decode('utf-8'))['currently']['precipIntensity'], "mm/h")
try:
    print("* intensity error margin:", json.loads(r.data.decode('utf-8'))['currently']['precipIntensityError'], "mm/h")
except KeyError:
    print("KeyError: omitted data")
print("* probability           :", json.loads(r.data.decode('utf-8'))['currently']['precipProbability'] * 100, "percent")
try:
    print("* type                  :", json.loads(r.data.decode('utf-8'))['currently']['precipType'], '\n')
except KeyError:
    print("KeyError: omitted data")
print("\033[1;34mTemperature-related conditions:\033[0;0m")
print("Temperature   :", json.loads(r.data.decode('utf-8'))['currently']['temperature'], "deg F")
print("Apparent temp.:", json.loads(r.data.decode('utf-8'))['currently']['apparentTemperature'], "deg F")
print("Dew point     :", json.loads(r.data.decode('utf-8'))['currently']['dewPoint'], "deg F")
print("Humidity      :", json.loads(r.data.decode('utf-8'))['currently']['humidity'] * 100, "percent\n")
print("\033[1;34mAir-related conditions:\033[0;0m")
print("Air pressure:", json.loads(r.data.decode('utf-8'))['currently']['pressure'], "mb")
print("\033[1;34mWind:\033[0;0m")
print("* speed  :", json.loads(r.data.decode('utf-8'))['currently']['windSpeed'], "mph")
print("* gust   :", json.loads(r.data.decode('utf-8'))['currently']['windGust'], "mph")
print("* bearing:", degrees_to_cardinal(json.loads(r.data.decode('utf-8'))['currently']['windBearing']), "degrees\n")
print("\033[1;34mCeiling-related conditions:\033[0;0m")
print("Cloud cover  :", json.loads(r.data.decode('utf-8'))['currently']['cloudCover'] * 100, "percent")
print("UV index     :", json.loads(r.data.decode('utf-8'))['currently']['uvIndex'])
print("Visibility   :", json.loads(r.data.decode('utf-8'))['currently']['visibility'], "miles")
print("Ozone density:", json.loads(r.data.decode('utf-8'))['currently']['ozone'], "DU")<jupyter_output>[1;34mCurrent Weather[0;0m (data provided thru Dark Sky API)

Date/Time: 2018-01-15 17:19:15
Summary  : Breezy and Foggy 

Distance to nearest storm: 4 miles
[1;34mPrecipitation:[0;0m
* intensity             : 0 mm/h
KeyError: omitted data
* probability           : 0 percent
KeyError: omitted data
[1;34mTemperature-related conditions:[0;0m
Temperature   : 12.64 deg F
Apparent temp.: -5.23 deg F
Dew point     : 6.76 deg F
Humidity      : 77.0 percent

[1;34mAir-related conditions:[0;0m
Air pressure: 1027.44 mb
[1;34mWind:[0;0m
* speed  : 19.52 mph
* gust   : 22.53 mph
* bearing: SW degrees

[1;34mCeiling-related conditions:[0;0m
Cloud cover  : 73.0 percent
UV index     : 0
Visibility   : 1.87 miles
Ozone density: 402.91 DU
{'fraction_non_alphanumeric': 0.1798970966556413, 'fraction_numerical': 0.03987504593899302, 'mean_word_length': 5.812265331664581, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, '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}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '565781', 'license': 'MIT', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1815, 'n_words': 413, 'path': 'python-note/python_notebooks-master/modules/weather_current.ipynb', 'repo_name': 'ii6uu99/ipynb', 'size': '5469'}