On Wed, 28 Aug 2024 08:41:28 +0100, Daniel wrote:

> That is so cool. I've had the same idea to use the API with AWS for my
> bbs. I also want to do the same thing for other government sites like
> ecfr for pulling aviation regulations.
> 
> Is your code somewhere I can look at it?

The NOAA?  I didn't get dncy so I'm calling get_station_info with a 
hardcoded latitude and longitude.  The commented out print(json.dumps()) 
pretty print the JSON so you can decide what to extract. I'm not 100% sure 
about tacking K onto the grid ID. The famous 'works for me' A URL like

https://forecast.weather.gov/MapClick.php?lat=40.8551337&lon=-114.0140115

will show the same info, in this case for Wendover Airport (KENV).

https://www.weather.gov/documentation/services-web-api

is the documentation for the API. 

import json
import requests

def get_station_info(latitude, longitude):
    url = f"https://api.weather.gov/points/{latitude},{longitude}";
    response = requests.get(url).json()
#    print(json.dumps(response, indent=4, sort_keys=True))
    property = response['properties']
    grid_id = property["gridId"]
    grid_x = property["gridX"]
    grid_y = property["gridY"]
    forecast_url = property["forecast"]
    observation_url = f"https://api.weather.gov/stations/K{grid_id}/
observations/latest"
    response = requests.get(observation_url).json()
#   print(json.dumps(response, indent=4, sort_keys=True))
    properties = response['properties']
    pressure = properties['barometricPressure']
    temperature = properties['temperature']
    humidity = properties['relativeHumidity']
    dewpoint = properties['dewpoint']

    temp = 9 * temperature['value'] / 5 + 32
    dew = 9 * dewpoint['value'] / 5 + 32
    hum = humidity['value']
    bp = pressure['value'] * 0.000295
    print(f"temperature {temp} dewpoint {dew:.2f} humidity  {hum:.2f}  
pressure {bp:.2f}\n")

    response = requests.get(forecast_url).json()
    properties = response['properties']
    periods = properties['periods']
#        print(json.dumps(period, indent=4, sort_keys=True))
    for i in range(0, len(periods)):
        period = periods[i]
        print(period['name'])
        print(period['detailedForecast'])
        print(f"temperature: {period['temperature']}")
        print(f"wind {period['windSpeed']}  {period['windDirection']}")
        print()

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to