Skip to content

Instantly share code, notes, and snippets.

@ina111
Last active September 8, 2021 07:09
Show Gist options
  • Save ina111/9185516a0bd39b1af0a1988e669bc922 to your computer and use it in GitHub Desktop.
Save ina111/9185516a0bd39b1af0a1988e669bc922 to your computer and use it in GitHub Desktop.
気象庁の高層風のページからデータをスクレイピングして、プロット
# coding: UTF-8
# 気象庁の高層風のページからデータ取得してプロット
# cf. http://www.data.jma.go.jp/obd/stats/etrn/upper/index.php
import os
import urllib2
from BeautifulSoup import BeautifulSoup
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tick
import os
plt.close('all')
plt.ion()
def date2windDateFrame(year,month,day,hour):
dt = datetime.datetime(year,month,day,hour)
url = "http://www.data.jma.go.jp/obd/stats/etrn/upper/view/daily_uwd.php?year=" + str(dt.year) + "&month=" + str(dt.month) + "&day=" + str(dt.day) + "&hour=" + str(dt.hour) + "&point=47418"
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
trs = soup.find('table', {'class' : 'data2_s'})
temp = []
for tr in trs.findAll('tr')[1:]:
tds = tr.findAll('td')
temp.append([u"Kushiro Japan",
str(dt),
float(tds[0].string),
int(tds[1].string),
float(tds[2].string),
int(tds[3].string),
tds[4].string])
col_list = ["position","datetime","pressure","altitude","speed", "direction", "note"]
return pd.DataFrame(temp, columns = col_list)
def windplot(dateframe):
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(12,6))
ax1.plot(dateframe["speed"], dateframe["altitude"])
ax1.set_xlabel(u'wind speed (m/s)')
ax1.set_ylabel(u'altitude (m)')
ax1.set_xlim([0,80])
ax1.set_ylim([0,35000])
ax1.set_title(dateframe["position"][0] + " " + dateframe["datetime"][0])
ax1.grid()
ax2.plot(dateframe["direction"], dateframe["altitude"])
ax2.set_xlabel(u'wind direction (deg)')
ax2.set_xlim([0,360])
plt.gca().xaxis.set_major_locator(tick.MultipleLocator(45))
f.subplots_adjust(hspace=0)
ax2.grid()
if (not os.path.isdir("output")):
os.mkdir("output")
plt.savefig("output/" + dateframe["datetime"][0].rstrip(":00:00"))
if __name__ == "__main__":
df = date2windDateFrame(2016,1,1,9)
windplot(df)
@ina111
Copy link
Author

ina111 commented Apr 9, 2016

目的:北海道十勝の海沿いの高層大気の風の統計データおよび、1日ごとの風を可視化
一番近かったのが釧路のデータだったので釧路をURLで指定
気象庁のデータはURLで日にちと場所の指定が出来、htmlで表示される仕様だったので、htmlからパースしてデータを抽出。後々統計データとするためpandasのデータフレームで出力する関数とした。
データフレームからプロットする関数も作成。outputフォルダにpngで出力

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment