如何使用python中的一些API计算供暖/制冷天数

时间:2019-06-29 14:01:36

标签: python-3.x api web-services weather-api

我正在尝试使用(T base -T a )公式 T base 通常为65F, T a =(high_temp + low_temp)/ 2

(e.x)

high_temp = 96.5F low_temp=65.21F then 
mean=(high_temp + low_temp)/2
result = mean - 65

65是平均室温
如果结果> 65,则冷却度日(cdd),否则加热度日(hdd)

我从两个API获取天气数据

  • 天气位
  • darksky

weatherbit 中提供了cdd和hdd数据,但是在 darksky 中,我们需要使用上述公式(T base -T < sub> a )

我的问题是两个api都显示不同的结果(e.x)
day的darksky json响应

{
"latitude": 47.552758,
"longitude": -122.150589,
"timezone": "America/Los_Angeles",
"daily": {
    "data": [
          {
            "time": 1560927600,
            "summary": "Light rain in the morning and overnight.",
            "icon": "rain",
            "sunriseTime": 1560946325,
            "sunsetTime": 1561003835,
            "moonPhase": 0.59,
            "precipIntensity": 0.0057,
            "precipIntensityMax": 0.0506,
            "precipIntensityMaxTime": 1561010400,
            "precipProbability": 0.62,
            "precipType": "rain",
            "temperatureHigh": 62.44,
            "temperatureHighTime": 1560981600,
            "temperatureLow": 48,
            "temperatureLowTime": 1561028400,
            "apparentTemperatureHigh": 62.44,
            "apparentTemperatureHighTime": 1560981600,
            "apparentTemperatureLow": 46.48,
            "apparentTemperatureLowTime": 1561028400,
            "dewPoint": 46.61,
            "humidity": 0.75,
            "pressure": 1021.81,
            "windSpeed": 5.05,
            "windGust": 8.36,
            "windGustTime": 1560988800,
            "windBearing": 149,
            "cloudCover": 0.95,
            "uvIndex": 4,
            "uvIndexTime": 1560978000,
            "visibility": 4.147,
            "ozone": 380.8,
            "temperatureMin": 49.42,
            "temperatureMinTime": 1561010400,
            "temperatureMax": 62.44,
            "temperatureMaxTime": 1560981600,
            "apparentTemperatureMin": 47.5,
            "apparentTemperatureMinTime": 1561014000,
            "apparentTemperatureMax": 62.44,
            "apparentTemperatureMaxTime": 1560981600
           }
        ]
    },
    "offset": -7
}

python计算

response = result.get("daily").get("data")[0]
low_temp = response.get("temperatureMin")
hi_temp = response.get("temperatureMax")
mean = (hi_temp + low_temp)/2
#65 is normal room temp
print(65-mean)

这里的平均值是6.509999999999998
65-平均值= 58.49
hdd为58.49 ,因此 cdd为0

weatherbit json响应中的相同日期是:

{
 "threshold_units": "F",
 "timezone": "America/Los_Angeles",
 "threshold_value": 65,
 "state_code": "WA",
 "country_code": "US",
 "city_name": "Newcastle",
 "data": [
   {
     "rh": 68,
     "wind_spd": 5.6,
     "timestamp_utc": null,
     "t_ghi": 8568.9,
     "max_wind_spd": 11.4,
     "cdd": 0.4,
     "dewpt": 46.9,
     "snow": 0,
     "hdd": 6.7,
     "timestamp_local": null,
     "precip": 0.154,
     "t_dni": 11290.6,
     "temp_wetbulb": 53.1,
     "t_dhi": 1413.9,
     "date": "2019-06-20",
     "temp": 58.6,
     "sun_hours": 7.6,
     "clouds": 58,
     "wind_dir": 186
   }
 ],
 "end_date": "2019-06-21",
 "station_id": "727934-94248",
 "count": 1,
 "start_date": "2019-06-20",
 "city_id": 5804676
}

此处 hdd为6.7 cdd为0.4

您能解释一下他们如何获得此结果吗?

1 个答案:

答案 0 :(得分:1)

您需要使用小时数据来计算HDD和CDD,然后将它们取平均值以获得每日价值。

此处有更多详细信息:https://www.weatherbit.io/blog/post/heating-and-cooling-degree-days-weather-api-release