トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

PY-AMEDAS

詳細版は

PY-jma_AMEDAS

https://qiita.com/ti-ginkgo/items/881ebf7d8ea0776d83ab のプログラムを地元の三木版に変更

作ってみる

pip3.6 install lxml
pip3.6 install beautifulsoup4
pip3.6 install matplotlib
pip3.6 install pandas
pip3.6 install numpy

プログラム 月毎の表示

#! /usr/bin/env python3
# -*- coding: utf-8
import urllib.request
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import pandas as pd
from pandas import DataFrame
from bs4 import BeautifulSoup

PREC_NO=63
BLOCK_NO=1227

def read_data_daily(day, month, year):
   # murl = "http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no="+str(PREC_NO)+"&block_no="+str(BLOCK_NO)+"&year="+str(year)+"&month="+str(month)+"&day=&view="
   #    """http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year=2016&month=1&day=1&view="""
   # http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_a1.php?prec_no=63&block_no=1227&year=2014&month=12&day=1&view=
   hurl = "http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_a1.php?prec_no="+str(PREC_NO)+"&block_no="+str(BLOCK_NO)+"&year="+str(year)+"&month="+str(month)+"&day="+str(day)+"&view="
   html = urllib.request.urlopen( hurl )
   # print( murl )
   data = BeautifulSoup(html, 'lxml')

   data.prettify()
   title = data.find("h3")
   day = data.find_all(class_="data_0_0")

   ## print ( day )

   y_vals = []

   # 温度を取得 ( 一時間毎の )
   for i, item in enumerate(day):
       # print ( i, item )
       # get 温度 ( 全7カラムの(3-1)番目
       if i % 7 == 2 : 
           # if  "])" in item.string :
           # print ( item.string )
           if  item.string == None :
              continue
           if  ")" in item.string :
              # print ( "innnnnnn (" )
              continue
           if  "///" in item.string :
              continue
           if  "]" in item.string :
              continue
           # print( item.string )
           y_vals.append(float(item.string))

   ave_data = sum(y_vals) / len(y_vals)
   # ave_data = sum(y_vals) 
   return ave_data

def read_data_monthly(month, year):
   ## 東京
   # html = urllib.request.urlopen("http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=44&block_no=47662&year="+str(year)+"&month="+str(month)+"&day=&view=")
   ## 兵庫 三木
   ## html = urllib.request.urlopen("http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year="+str(year)+"&month="+str(month)+"&day=&view=")
   # http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year=2016&month=1&day=1&view="""
   murl = "http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no="+str(PREC_NO)+"&block_no="+str(BLOCK_NO)+"&year="+str(year)+"&month="+str(month)+"&day=&view="
   #    """http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year=2016&month=1&day=1&view="""
   html = urllib.request.urlopen( murl )
   # print( murl )
   data = BeautifulSoup(html, 'lxml')

   data.prettify()
   title = data.find("h3")
   day = data.find_all(class_="data_0_0")

   ## print ( day )

   y_vals = []

   for i, item in enumerate(day):
       # print ( i, item )
       ## if i % 20 == 5:  ## TOKYO=20
       if i % 15 == 3 : 
           # get 平均温度 ( 全15カラムの(4-1)番目
           # if  "])" in item.string :
           # print ( item.string )
           if  item.string == None :
              continue
           if  ")" in item.string :
              # print ( "innnnnnn (" )
              continue
           if  "]" in item.string :
              continue
           # print( item.string )
           y_vals.append(float(item.string))

   ave_data = sum(y_vals) / len(y_vals)
   # ave_data = sum(y_vals) 
   return ave_data

def main00():

   YEARS = 9
   x_ave = np.zeros((12, 31))

   year = 2017
   # for i in range(0, 10):
   # for i in range(0, YEARS):
   # 7月から12月までのデータを表示
   ## for month in range(0,12):
   for month in range(7,12):
       for day in range(0,31):
           print ( year, month+1 ,day+1)
           x_ave[month][day] = (read_data_daily(day +1, month + 1, year))
   
   # index = [ '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']
   # columns = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
   ## index = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
   index = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
   columns = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14', '15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']

   temperature_ave = DataFrame(x_ave, index=index, columns=columns)
   temperature_ave = temperature_ave.round(1)
   print(temperature_ave)

if __name__ == '__main__':
   main00()
   # month = 12
   # year = 2014 
   # # print ( read_data_mothly(month , year ) )
   # day = 2
   # print ( read_data_daily(day, month , year ) )

プログラム 年度毎の表示

#! /usr/bin/env python3
# -*- coding: utf-8

import urllib.request
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import pandas as pd
from pandas import DataFrame
from bs4 import BeautifulSoup

def read_data(month, year):
   ## 東京
   # html = urllib.request.urlopen("http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=44&block_no=47662&year="+str(year)+"&month="+str(month)+"&day=&view=")
   ## 兵庫 三木
   murl = "http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year="+str(year)+"&month="+str(month)+"&day=&view="
   #    """http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=63&block_no=1227&year=2016&month=1&day=1&view="""
   html = urllib.request.urlopen( murl )
   # print( murl )
   data = BeautifulSoup(html, 'lxml')

   data.prettify()
   title = data.find("h3")
   day = data.find_all(class_="data_0_0")
   ## print ( day )

   y_vals = []

   for i, item in enumerate(day):
       # print ( i, item )
       ## if i % 20 == 5:  ## TOKYO=20
       if i % 15 == 3 : 
           # get 平均温度 ( 全15カラムの(4-1)番目
           # if  "])" in item.string :
           # print ( item.string )
           if  item.string == None :
              continue
           if  ")" in item.string :
              # print ( "innnnnnn (" )
              continue
           if  "]" in item.string :
              continue
           # print( item.string )
           y_vals.append(float(item.string))

   ave_data = sum(y_vals) / len(y_vals)
   # ave_data = sum(y_vals) 
   return ave_data

def main00():

   YEARS = 9 # 本当は10年分やりたいけれど 2008年の途中からデータが。。。(笑)
   x_ave = np.zeros((YEARS, 12))

   year = 2009
   # for i in range(0, 10):
   for i in range(0, YEARS):
       # if year + i == 2014:
       #     year = year + 1
       for month in range(0,12):
           print ( year+i, month+1 )
           x_ave[i][month] = (read_data(month + 1, year + i))

   index = [ '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']
   columns = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']

   temperature_ave = DataFrame(x_ave, index=index, columns=columns)
   temperature_ave = temperature_ave.round(1)
   print(temperature_ave)

if __name__ == '__main__':
   main00()
   # month = 2
   # year = 2010 
   # print ( read_data(month , year ) )