Skip to content

Commit

Permalink
Use generic unit codes (#56)
Browse files Browse the repository at this point in the history
* use generic unit codes

* add test
  • Loading branch information
MatthewFlamm committed Oct 2, 2021
1 parent f6d706b commit 861ad43
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pynws/simple_nws.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ def m_p_s_to_km_p_hr(m_p_s):


unit_conversion = {
"unit:degC": unchanged,
"unit:degF": f_to_c,
"unit:km_h-1": unchanged,
"unit:m_s-1": m_p_s_to_km_p_hr,
"unit:m": unchanged,
"unit:Pa": unchanged,
"unit:percent": unchanged,
"unit:degree_(angle)": unchanged,
"degC": unchanged,
"degF": f_to_c,
"km_h-1": unchanged,
"m_s-1": m_p_s_to_km_p_hr,
"m": unchanged,
"Pa": unchanged,
"percent": unchanged,
"degree_(angle)": unchanged,
}

WIND = {name: idx * 360 / 16 for idx, name in enumerate(WIND_DIRECTIONS)}
Expand Down Expand Up @@ -80,6 +80,14 @@ def m_p_s_to_km_p_hr(m_p_s):
}


def convert_unit(unit_code, value):
"""Convert value with unit code to preferred unit."""
for unit, converter in unit_conversion.items():
if unit in unit_code:
return converter(value)
raise ValueError(f"unit code: '{unit_code}' not recognized.")


def convert_weather(weather):
"""Convert short code to readable name."""
return [(API_WEATHER_CODE.get(w[0], w[0]), w[1]) for w in weather]
Expand Down Expand Up @@ -258,7 +266,7 @@ def observation(self):
]
obs_item = next(iter([o for o in obs_list if o]), None)
if isinstance(obs_item, tuple):
data[obs] = unit_conversion[obs_item[1]](obs_item[0])
data[obs] = convert_unit(obs_item[1], obs_item[0])
else:
data[obs] = obs_item

Expand Down
168 changes: 168 additions & 0 deletions tests/fixtures/stations_observations_other_unitcode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"@context": [
"https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld",
{
"wx": "https://api.weather.gov/ontology#",
"s": "https://schema.org/",
"geo": "http://www.opengis.net/ont/geosparql#",
"unit": "http://codes.wmo.int/common/unit/",
"@vocab": "https://api.weather.gov/ontology#",
"geometry": {
"@id": "s:GeoCoordinates",
"@type": "geo:wktLiteral"
},
"city": "s:addressLocality",
"state": "s:addressRegion",
"distance": {
"@id": "s:Distance",
"@type": "s:QuantitativeValue"
},
"bearing": {
"@type": "s:QuantitativeValue"
},
"value": {
"@id": "s:value"
},
"unitCode": {
"@id": "s:unitCode",
"@type": "@id"
},
"forecastOffice": {
"@type": "@id"
},
"forecastGridData": {
"@type": "@id"
},
"publicZone": {
"@type": "@id"
},
"county": {
"@type": "@id"
}
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/stations/KFLL/observations/2019-06-27T10:53:00+00:00",
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-80.150000000000006,
26.07
]
},
"properties": {
"@id": "https://api.weather.gov/stations/KFLL/observations/2019-06-27T10:53:00+00:00",
"@type": "wx:ObservationStation",
"elevation": {
"value": 10,
"unitCode": "wmoUnit:m"
},
"station": "https://api.weather.gov/stations/KFLL",
"timestamp": "2019-06-27T10:53:00+00:00",
"rawMessage": "KFLL 271053Z 35005KT 10SM FEW025 FEW250 26/23 A3005 RMK AO2 SLP176 T02560228",
"textDescription": "Mostly Clear",
"icon": "https://api.weather.gov/icons/land/day/few?size=medium",
"presentWeather": [],
"temperature": {
"value": 10,
"unitCode": "wmoUnit:degC",
"qualityControl": "qc:V"
},
"dewpoint": {
"value": 10,
"unitCode": "wmoUnit:degC",
"qualityControl": "qc:V"
},
"windDirection": {
"value": 10,
"unitCode": "wmoUnit:degree_(angle)",
"qualityControl": "qc:V"
},
"windSpeed": {
"value": 10,
"unitCode": "wmoUnit:m_s-1",
"qualityControl": "qc:V"
},
"windGust": {
"value": 10,
"unitCode": "wmoUnit:m_s-1",
"qualityControl": "qc:Z"
},
"barometricPressure": {
"value": 100000,
"unitCode": "wmoUnit:Pa",
"qualityControl": "qc:V"
},
"seaLevelPressure": {
"value": 100000,
"unitCode": "wmoUnit:Pa",
"qualityControl": "qc:V"
},
"visibility": {
"value": 10000,
"unitCode": "wmoUnit:m",
"qualityControl": "qc:C"
},
"maxTemperatureLast24Hours": {
"value": null,
"unitCode": "wmoUnit:degC",
"qualityControl": null
},
"minTemperatureLast24Hours": {
"value": null,
"unitCode": "wmoUnit:degC",
"qualityControl": null
},
"precipitationLastHour": {
"value": null,
"unitCode": "wmoUnit:m",
"qualityControl": "qc:Z"
},
"precipitationLast3Hours": {
"value": null,
"unitCode": "wmoUnit:m",
"qualityControl": "qc:Z"
},
"precipitationLast6Hours": {
"value": null,
"unitCode": "wmoUnit:m",
"qualityControl": "qc:Z"
},
"relativeHumidity": {
"value": 10,
"unitCode": "wmoUnit:percent",
"qualityControl": "qc:C"
},
"windChill": {
"value": null,
"unitCode": "wmoUnit:degC",
"qualityControl": "qc:V"
},
"heatIndex": {
"value": 10,
"unitCode": "wmoUnit:degC",
"qualityControl": "qc:V"
},
"cloudLayers": [
{
"base": {
"value": 760,
"unitCode": "wmoUnit:m"
},
"amount": "FEW"
},
{
"base": {
"value": 7620,
"unitCode": "wmoUnit:m"
},
"amount": "FEW"
}
]
}
}
]
}
1 change: 1 addition & 0 deletions tests/test_simple_nws.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async def test_nws_set_station_none(aiohttp_client, loop, mock_urls):
"stations_observations.json",
"stations_observations_multiple.json",
"stations_observations_strings.json",
"stations_observations_other_unitcode.json",
],
)
async def test_nws_observation(aiohttp_client, loop, mock_urls, observation_json):
Expand Down

0 comments on commit 861ad43

Please sign in to comment.