◆気象データを外部サイトから取得する
ネットに紹介されていた気象情報取得を自分もやってみた。下記がそのスクリプト
掲載されたそのままだとなーんかうまくいかない…当たり前だ、中身を理解していないから。。。なので、ところどころ修正しながらとりあえずは自分でデータ確認できる状態を目標にした。データ取得さえできればあとはどのように加工するかはまたほかのテクニックの問題だし。
import requests # “noaa_gfs_global_sflux_0.12d”は気象データID url = "http://api.planetos.com/v1/datasets/noaa_gfs_global_sflux_0.12d/point" # request what you want querystring = { "lat":"35.70", "lon":"139.80", "var":"Temperature_surface", "count":"250", # number of data "apikey":"**************************************" # API KEY } """ ---------------------------------------------- apikey: After you register on PlanetOS, you'll get your API key. ---------------------------------------------- @@@@"var":"Temperature_surface",@@@@ Read below https://data.planetos.com/datasets/noaa_gfs_global_sflux_0.12d GFS Global Weather Forecast By NCEP. Near Surface Parameters. VARIABLES(390)... Variable:Temperature_surface Long name:Temperature @ Ground or water surface Unit: K notes; Easy to find out with Unit "K", because "K" means Kelvin.(Temperature) "var" is option. If you do not set "var", the large mount of data would be comming.... So, you may want to set "var":"something sets". --------------------------------------------- """ # get the information as json response = requests.get(url, params = querystring) # Module import json # json ---> textData jsontext = response.text # check data if you see print(jsontext) # textData ---> Dictionary data data = json.loads(jsontext) # check Dictionary if you see #print(data.keys()) #print(data.values()) #Dictionary have 2 keys. #1.keys=stats (values....) #2.keys=entries (values....) ---> use it at this time #============================================================= # Module import pandas as pd from pandas import json_normalize # Pick up the information from Dictionary pickup = data["entries"] # Easy to read outdata = pd.json_normalize(pickup) # Output on display print(outdata) # DataFrame(two dimensionary array) df = pd.DataFrame( outdata ) # Strings ---> Datetime df['axes.time'] = pd.to_datetime(df['axes.time']) # Fahrenheit ---> Celcius i = 0 for temp in df['data.Temperature_surface']: temp = temp - 273.15 df.loc[i, ['data.Temperature_surface']] = temp i = i + 1 # Check about Temperature # Length of Elements print(len(df['data.Temperature_surface'])) # Check about the data (Time, Temperature) n = 0 while n < len(df['data.Temperature_surface']): print(n,df['axes.time'][n],df['data.Temperature_surface'][n]) n += 1 print("-----------") print("End of line") input()
上記概要はこんな感じ
・PlanetOSというサイトへAPI KEYを使用してアクセス
・PlantOSから気象情報をjson形式で取得
・jsonファイルをテキストとして読込み
・テキストファイルを辞書型に読み込む
・辞書内に2つあるキーの「entries」を読み込む
・entries内にあるデータで、axes.timeの表記変更
・entries内にあるデータで、温度をカ氏→セ氏にする
・時間、温度を書き出す
※ところどころにpinrt文を配置しているのはデータ確認のため
◆気象情報を取得する記事は下記に