最新の10分値を取得したい
※間違っていても無保証
※利用方法は気象庁の規約に順ずる。
気象庁のホームページがリニューアルされて、jsonでデータを取得できることが話題になっています(例えば)。
例で紹介されている
引用
https://www.jma.go.jp/bosai/amedas/data/map/{latest_time}.json
のURIの場合、気圧が取得できないのが悩ましいところでした。
少し気象庁のページを見ていると、次のURIでは各アメダス観測点の3時間分の10分観測値が取得できるため、Pythonで取得してみました(追記3/17:よく読むと紹介記事の末尾に既出でした)。
https://www.jma.go.jp/bosai/amedas/data/point/{amedas_number}/{latest_date}_{divisionNumber}.json
ここで、{amedas_number}
は観測点番号、{divisionNumber}
は24時間表記で3時間ごとの数字2桁(00, 03, 06 ...)を表します。
JSONの構造は下記のようになっていて、10分ごとに観測値があります。1時間値しか無い観測項目があるため、0分は項目が多めです。
json構造(概観)
object {18}
20210316060000 {22}
20210316061000 {18}
20210316062000 {18}
20210316063000 {18}
20210316064000 {18}
20210316065000 {18}
20210316070000 {22}
20210316071000 {18}
...
20210316080000 {22}
20210316081000 {18}
...
jsonの構造(折りたたみ表示)
json構造(冒頭)
{
"20210316060000": {
"prefNumber": 44,
"observationNumber": 136,
"temp": [
9,
0
],
"snow1h": [
0,
null
],
"snow6h": [
0,
null
],
"snow12h": [
0,
null
],
"snow24h": [
0,
null
],
"sun10m": [
0,
0
],
"sun1h": [
0,
0
],
"precipitation10m": [
0,
0
],
"precipitation1h": [
0,
0
],
"precipitation3h": [
0,
0
],
"precipitation24h": [
0,
0
],
"windDirection": [
0,
0
],
"wind": [
0.2,
0
],
"maxTempTime": {
"hour": 15,
"minute": 1
},
"maxTemp": [
11.3,
0
],
"minTempTime": {
"hour": 15,
"minute": 51
},
"minTemp": [
7.5,
0
],
"gustTime": {
"hour": 20,
"minute": 7
},
"gustDirection": [
16,
0
],
"gust": [
3.5,
0
]
},
"20210316061000": {
"prefNumber": 44,
"observationNumber": 136,
"temp": [
9,
0
],
"sun10m": [
0,
0
],
"sun1h": [
0,
0
],
"precipitation10m": [
0,
0
],
"precipitation1h": [
0,
0
],
"precipitation3h": [
0,
0
],
"precipitation24h": [
0,
0
],
"windDirection": [
15,
0
],
"wind": [
1.3,
0
],
"maxTempTime": {
"hour": 15,
"minute": 1
},
"maxTemp": [
11.3,
0
],
"minTempTime": {
"hour": 15,
"minute": 51
},
"minTemp": [
7.5,
0
],
"gustTime": {
"hour": 20,
"minute": 7
},
"gustDirection": [
16,
0
],
"gust": [
3.5,
0
]
},
Pythonコード
get_jma.py
import requests
import json
from datetime import datetime, timedelta, timezone, date, time
import math
import re
JST = timezone(timedelta(hours=+9), 'JST')
#epoch = datetime.now(JST) - timedelta(minutes=20) #適当な更新待ちで済ます場合
latest_time = requests.get('https://www.jma.go.jp/bosai/amedas/data/latest_time.txt')
epoch = datetime.strptime(latest_time.text,"%Y-%m-%dT%H:%M:%S+09:00")
tenth_min = str(int((epoch.minute // 10) * 10)).zfill(2)
outTime = epoch.strftime('%Y-%m-%d %H') + ':' + tenth_min + ':00+09:00'
AMD_JSON_DATA_DIVISION = 8
jsonDataTerm = 24 / AMD_JSON_DATA_DIVISION
divisionNumber = int(math.floor(epoch.hour / jsonDataTerm)* jsonDataTerm)
amdNumber = "44136" #Tokyo
fName = epoch.strftime('%Y%m%d') + '_' + str(divisionNumber).zfill(2)+'.json'
url = 'https://www.jma.go.jp/bosai/amedas/data/point/' + amdNumber + '/' + fName
response = requests.get(url)
jsonData = response.json()
t_clock = epoch.strftime('%Y%m%d%H') + tenth_min + '00'
# o_clock = epoch.strftime('%Y%m%d%H')+'0000' #1時間値しか要らない場合
t = jsonData[t_clock]['temp'][0]
h = jsonData[t_clock]['humidity'][0]
p = jsonData[t_clock]['pressure'][0]
outraw = " ".join([str(outTime), str(t), str(h), str(p)])
outprc = re.sub(r'None', "NaN", outraw)
print(outprc)