moshimore Knowledge

アプリ開発・紹介とメモ書き、日々の日記。

Dark Sky APIを使ってみました!

f:id:moshimore:20180710165532j:plain
先日リリースした頭痛予測マップは、天気予報と低気圧の情報を元に頭痛予測の計算をしています。
この天気予報の情報取得には、Dark SkyというところのAPIを利用しました。
その他にもOpenWeatherMapWeatherUnderGroundなどありましたが、Dark Skyに決めた理由は、1時間毎の予報が取得できる点です。
1日に最大1000回もリクエストでき、サーバーの方でキャッシュしてしまうので回数的には問題ありませんでした。
また、1000回を超えても、1リクエストあたり$0.0001だけの課金です。
課金と言っても、利用登録の際にクレジットカードの情報などは特に必要ありませんでした。

PHPでリクエスト

サーバーの方はPHPを使っているので、以下のような感じでDark Sky APIにリクエストします。
JSONで返ってくるのでデコードします。

<?php
    $url = "https://api.darksky.net/forecast/API_KEY/37.8267,-122.4233";
    $contents = file_get_contents($url);
    $decode = json_decode($contents, true);
    print_r($decode);
?>

取得できるJSON

currentlyは現在の天気、minutelyは1分おきの0~60分まで、hourlyは1時間おきの0~48時間まで、dailyは1日おきの0~7日までの天気予報です。

Array
(
    [latitude] => 37.8267
    [longitude] => -122.4233
    [timezone] => America/Los_Angeles
    [currently] => Array
        (
            [time] => 1531198488
            [summary] => Clear
            [icon] => clear-night
            [nearestStormDistance] => 112
            [nearestStormBearing] => 111
            [precipIntensity] => 0
            [precipProbability] => 0
            [temperature] => 68.48
            [apparentTemperature] => 68.48
            [dewPoint] => 47.1
            [humidity] => 0.46
            [pressure] => 1017.09
            [windSpeed] => 5.09
            [windGust] => 8.31
            [windBearing] => 252
            [cloudCover] => 0.08
            [uvIndex] => 0
            [visibility] => 9.53
            [ozone] => 307.59
        )
    [minutely] => Array
        (
            [summary] => Clear for the hour.
            [icon] => clear-night
            [data] => Array
                (
                    [0] => Array
                        (
                            [time] => 1531198440
                            [precipIntensity] => 0
                            [precipProbability] => 0
                        )
          ~省略~
                    [60] => Array
                        (
                            [time] => 1531202040
                            [precipIntensity] => 0
                            [precipProbability] => 0
                        )
                )
        )
    [hourly] => Array
        (
            [summary] => Clear throughout the day.
            [icon] => clear-day
            [data] => Array
                (
                    [0] => Array
                        (
                            [time] => 1531195200
                            [summary] => Clear
                            [icon] => clear-night
                            [precipIntensity] => 0
                            [precipProbability] => 0
                            [temperature] => 70.46
                            [apparentTemperature] => 70.46
                            [dewPoint] => 45.28
                            [humidity] => 0.4
                            [pressure] => 1016.65
                            [windSpeed] => 4.96
                            [windGust] => 8.74
                            [windBearing] => 253
                            [cloudCover] => 0.09
                            [uvIndex] => 0
                            [visibility] => 10
                            [ozone] => 307.74
                        )
          ~省略~
                    [48] => Array
                        (
                            [time] => 1531368000
                            [summary] => Partly Cloudy
                            [icon] => partly-cloudy-night
                            [precipIntensity] => 0
                            [precipProbability] => 0
                            [temperature] => 59.41
                            [apparentTemperature] => 59.41
                            [dewPoint] => 55.31
                            [humidity] => 0.86
                            [pressure] => 1010.89
                            [windSpeed] => 6.17
                            [windGust] => 8.06
                            [windBearing] => 232
                            [cloudCover] => 0.29
                            [uvIndex] => 0
                            [visibility] => 10
                            [ozone] => 295.67
                        )
                )
        )
    [daily] => Array
        (
            [summary] => No precipitation throughout the week, with high temperatures falling to 68°F on Wednesday.
            [icon] => clear-day
            [data] => Array
                (
                    [0] => Array
                        (
                            [time] => 1531119600
                            [summary] => Clear throughout the day.
                            [icon] => clear-day
                            [sunriseTime] => 1531141023
                            [sunsetTime] => 1531193710
                            [moonPhase] => 0.88
                            [precipIntensity] => 0
                            [precipIntensityMax] => 0
                            [precipProbability] => 0
                            [temperatureHigh] => 79.63
                            [temperatureHighTime] => 1531177200
                            [temperatureLow] => 56.78
                            [temperatureLowTime] => 1531227600
                            [apparentTemperatureHigh] => 79.63
                            [apparentTemperatureHighTime] => 1531177200
                            [apparentTemperatureLow] => 56.78
                            [apparentTemperatureLowTime] => 1531227600
                            [dewPoint] => 49.1
                            [humidity] => 0.56
                            [pressure] => 1017.17
                            [windSpeed] => 5.28
                            [windGust] => 13.07
                            [windGustTime] => 1531177200
                            [windBearing] => 243
                            [cloudCover] => 0.05
                            [uvIndex] => 11
                            [uvIndexTime] => 1531166400
                            [visibility] => 10
                            [ozone] => 307.96
                            [temperatureMin] => 56.47
                            [temperatureMinTime] => 1531141200
                            [temperatureMax] => 79.63
                            [temperatureMaxTime] => 1531177200
                            [apparentTemperatureMin] => 56.47
                            [apparentTemperatureMinTime] => 1531141200
                            [apparentTemperatureMax] => 79.63
                            [apparentTemperatureMaxTime] => 1531177200
                        )
          ~省略~
                    [7] => Array
                        (
                            [time] => 1531724400
                            [summary] => Clear throughout the day.
                            [icon] => clear-day
                            [sunriseTime] => 1531746107
                            [sunsetTime] => 1531798324
                            [moonPhase] => 0.14
                            [precipIntensity] => 0
                            [precipIntensityMax] => 0
                            [precipProbability] => 0
                            [temperatureHigh] => 72.07
                            [temperatureHighTime] => 1531778400
                            [temperatureLow] => 56.37
                            [temperatureLowTime] => 1531825200
                            [apparentTemperatureHigh] => 72.07
                            [apparentTemperatureHighTime] => 1531778400
                            [apparentTemperatureLow] => 56.37
                            [apparentTemperatureLowTime] => 1531825200
                            [dewPoint] => 54.89
                            [humidity] => 0.77
                            [pressure] => 1012.85
                            [windSpeed] => 5.1
                            [windGust] => 11.98
                            [windGustTime] => 1531785600
                            [windBearing] => 234
                            [cloudCover] => 0.01
                            [uvIndex] => 11
                            [uvIndexTime] => 1531771200
                            [visibility] => 10
                            [ozone] => 289.86
                            [temperatureMin] => 55.64
                            [temperatureMinTime] => 1531742400
                            [temperatureMax] => 72.07
                            [temperatureMaxTime] => 1531778400
                            [apparentTemperatureMin] => 55.64
                            [apparentTemperatureMinTime] => 1531742400
                            [apparentTemperatureMax] => 72.07
                            [apparentTemperatureMaxTime] => 1531778400
                        )
                )
        )
    [flags] => Array
        (
            [sources] => Array
                (
                    [0] => nearest-precip
                    [1] => nwspa
                    [2] => cmc
                    [3] => gfs
                    [4] => hrrr
                    [5] => icon
                    [6] => isd
                    [7] => madis
                    [8] => nam
                    [9] => sref
                    [10] => darksky
                )
            [nearest-station] => 1.839
            [units] => us
        )
    [offset] => -7
)

各項目の意味

各項目の意味が分かりずらかったので翻訳を通してみました。
nearestStormDistanceやnearestStormBearing、cloudCover、ozoneは、日本ではあまり使わないですね。

currently

項目名 内容
time 時刻
summry 概要
icon アイコン
nerestStormDistnce 近くの嵐までの距離
nerestStormBering 近くの嵐の向き
precipIntensity 降水量
precipProbbility 降水確率
temperture 温度
pprentTemperture 体感温度
dewPoint 露点
humidity 湿度
pressure 圧力
windSpeed 風速
windGust 暴風
windBering 風向き
cloudCover
uvIndex UV指数
visibility 可視性
ozone オゾン

minutely

項目名 内容
time 時刻
precipIntensity 降水量
precipProbbility 降水確率

hourly

項目名 内容
time 時刻
summry 概要
icon アイコン
precipIntensity 降水量
precipProbbility 降水確率
temperture 温度
pprentTemperture 体感温度
dewPoint 露点
humidity 湿度
pressure 圧力
windSpeed 風速
windGust 暴風
windBering 風向き
cloudCover
uvIndex UV指数
visibility 可視性
ozone オゾン

daily

最後の8項目は、よく分かりませんでした。

項目名 内容
time 時刻
summry 概要
icon アイコン
sunriseTime 日の出時間
sunsetTime 日没時間
moonPhse 月相
precipIntensity 降水量
precipIntensityMx 最大降水量
precipProbbility 降水確率
tempertureHigh 最高気温
tempertureHighTime 最高気温時刻
tempertureLow 最低気温
tempertureLowTime 最低気温時刻
pprentTempertureHigh 体感最高気温
pprentTempertureHighTime 体感最高気温時刻
pprentTempertureLow 体感最低気温
pprentTempertureLowTime 体感最低気温時刻
dewPoint 露点
humidity 湿度
pressure 圧力
windSpeed 風速
windGust 暴風
windGustTime 暴風時刻
windBering 風向き
cloudCover
uvIndex UV指数
uvIndexTime UV指数時刻
visibility 可視性
ozone オゾン
tempertureMin 最高気温?
tempertureMinTime 最高気温時刻?
tempertureMx 最低気温?
tempertureMxTime 最低気温時刻?
pprentTempertureMin 体感最高気温?
pprentTempertureMinTime 体感最高気温時刻?
pprentTempertureMx 体感最低気温?
pprentTempertureMxTime 体感最低気温時刻?