def get_passed_amedas_data(node_obj, date, term): """ dateを起点として、term日分の観測データをダウンロードして返す ただし、dateを起点とした過去のデータを返す。 過去の観測データ用です。 """ lines = [] # 過去データを入手 for i in range(1, term): print("--wait some seconds--") time.sleep(wait_seconds) # たまにプロキシ?ファイヤウォール?が通信をカットするのでその対策 _date = date - td(days=i) now = dt.now() if _date.year == now.year and _date.month == now.month and _date.day == now.day: # 現時点の観測データは対応していない continue print(_date) html_txt = node_obj.get_data(_type="hourly", date=_date) if html_txt is None: print("--can't download--") continue html_lines = html_txt.split("\n") data = amp.get_data(html_lines, _date) data = [replace(x) for x in data] #print(data) lines += [",".join(x) for x in data] return lines
def get_amedas_data_typeB(node_obj, date): # 過去のデータを取得 lines = get_passed_amedas_data(node_obj, date, days) # 最新のデータを入手 _date = dt.now() + td(days=1) if date.year == _date.year and date.month == _date.month and date.day == _date.day: html_txt = node_obj.get_data(_type="real-time") html_lines = html_txt.split("\n") data = amp.get_data(html_lines, dt.now()) data = [replace(x) for x in data] print(data) # 最新の観測データは過去の観測データとフォーマットが異なるので、整形する # 降水量と気温を入れ替える dummy = [] for mem in data: x = mem.pop(3) mem.insert(2, x) dummy.append(mem) data = dummy """ for i in range(len(data)): x = data[i][1] y = data[i][2] data[i][1] = y data[i][2] = x """ # 風速と風向を入れ替える dummy = [] for mem in data: x = mem.pop(5) mem.insert(4, x) dummy.append(mem) data = dummy # 無い観測データ項目を追加 data = [y + ["", ""] for y in data] #print(data) # join時にエラーが出ないように全てを文字列化 dummy = [] for x in data: x = [str(y) for y in x] dummy.append(x) data = dummy #print(data) lines += [",".join(x) for x in data] #print(lines) return lines
def get_amedas_data_typeA(node_obj, date): # 過去のデータを取得 lines = get_passed_amedas_data(node_obj, date, days) # 最新のデータを入手 _date = dt.now() + td(days=1) if date.year == _date.year and date.month == _date.month and date.day == _date.day: html_bytes = node_obj.get_data(_type="real-time") html_lines = resolve(html_bytes) data = amp.get_data(html_lines, dt.now()) print(data) # 最新の観測データは過去の観測データとフォーマットが異なるので、整形する # 気圧を入れ替える dummy = [] for mem in data: x = mem.pop(8) mem.insert(2, x) mem.insert(3, "") dummy.append(mem) data = dummy # 降水量と気温を入れ替える dummy = [] for mem in data: x = mem.pop(5) mem.insert(4, x) dummy.append(mem) data = dummy # 露点温度、蒸気圧の欄を作る dummy = [] for mem in data: mem.insert(6, "") mem.insert(6, "") dummy.append(mem) data = dummy # 湿度の位置を変える dummy = [] for mem in data: x = mem.pop(11) mem.insert(8, x) dummy.append(mem) data = dummy """ for i in range(len(data)): x = data[i][1] y = data[i][2] data[i][1] = y data[i][2] = x """ # 風速と風向を入れ替える dummy = [] for mem in data: x = mem.pop(10) mem.insert(9, x) dummy.append(mem) data = dummy # 無い観測データ項目を追加 data = [y + ["", "", "", "", "", ""] for y in data] # 水蒸気圧を計算する dummy = [] for mem in data: P_hPa = "" try: Tb = float(mem[5]) #print("hoge") #print(Tb) P_torr = 10 ** (8.07131 - 1730.63 / (233.426 + Tb)) # https://ja.wikipedia.org/wiki/%E8%92%B8%E6%B0%97%E5%9C%A7 P_hPa = P_torr * 133.322368 / 100.0 # https://ja.wikipedia.org/wiki/%E3%83%88%E3%83%AB #print(P_hPa) except: pass mem[7] = P_hPa dummy.append(mem) data = dummy # 露点温度を計算する def GofGra(t): """ 気温から飽和水蒸気量を求める 氷点下では別の近似式を使った方が良いらしい。 http://d.hatena.ne.jp/Rion778/20121126/1353861179 tの単位:℃ """ water_vapor_at_saturation = 10 ** \ (10.79574 * (1 - 273.16/(t + 273.15)) - \ 5.02800 * math.log10((t + 273.15)/273.16) + \ 1.50475 * 10**(-4) * (1-10**(-8.2969 * ((t + 273.15)/273.16 - 1))) + \ 0.42873 * 10**(-3) * (10**(4.76955*(1 - 273.16/(t + 273.15))) - 1) + \ 0.78614) return water_vapor_at_saturation dummy = [] for mem in data: # http://d.hatena.ne.jp/Rion778/20121208/1354975888 dew_point_temperature = "" try: t = float(mem[5]) # 気温[deg] U = float(mem[8]) # 相対湿度[%] #print("@@@") #print(t, U) dew_point_temperature = -(math.log(GofGra(t)*U/100/6.1078) * 237.3) / \ (math.log(GofGra(t)*U/100/6.1078) - 17.2693882) #print("fuga") #print(U) #print(dew_point_temperature) except: pass mem[6] = dew_point_temperature dummy.append(mem) data = dummy #print(data) # join時にエラーが出ないように全てを文字列化 dummy = [] for x in data: x = [str(y) for y in x] dummy.append(x) data = dummy #print(data) lines += [",".join(x) for x in data] return lines
def get_amedas_data_type(node_obj): html_txt = node_obj.get_data(_type="real-time") html_lines = html_txt.split("\n") data = amp.get_data(html_lines, dt.now()) #print(data) return data
def get_amedas_data_type(node_obj): html_txt = node_obj.get_data(_type="real-time") html_lines = html_txt.split("\n") data = amp.get_data(html_lines, dt.now()) #print(data) return data
import amedas.download as amd import amedas.html_parser as amp import timeKM def replace(data_list): """ 文字列の要素からなるリストを走査して、都合の悪い文字を削除して返す """ new_data = [] for mem in data_list: if "×" == mem: new_data.append("nan") elif "]" in mem: new_data.append(mem.replace("]", "")) else: new_data.append(mem) return new_data with open("hogehoge2.txt", "r", encoding="utf-8-sig") as fr: html_txt = fr.read() html_lines = html_txt.split("\n") data = amp.get_data(html_lines, dt.now()) print(data) for mem in data: print(mem) data = [replace(x) for x in data] for mem in data: print(mem)
def get_amedas_data_typeA(node_obj, date): # 過去のデータを取得 lines = get_passed_amedas_data(node_obj, date, days) # 最新のデータを入手 _date = dt.now() + td(days=1) if date.year == _date.year and date.month == _date.month and date.day == _date.day: html_txt = node_obj.get_data(_type="real-time") #with open("hogehoge.txt", "w", encoding="utf-8-sig") as fw: # debug # fw.write(html_txt) html_lines = html_txt.split("\n") data = amp.get_data(html_lines, dt.now()) data = [replace(x) for x in data] print(data) # 最新の観測データは過去の観測データとフォーマットが異なるので、整形する # 気圧を入れ替える dummy = [] for mem in data: #print(mem) x = mem.pop(8) mem.insert(2, x) mem.insert(3, "") dummy.append(mem) data = dummy # 降水量と気温を入れ替える dummy = [] for mem in data: x = mem.pop(5) mem.insert(4, x) dummy.append(mem) data = dummy # 露点温度、蒸気圧の欄を作る dummy = [] for mem in data: mem.insert(6, "") mem.insert(6, "") dummy.append(mem) data = dummy # 湿度の位置を変える dummy = [] for mem in data: x = mem.pop(11) mem.insert(8, x) dummy.append(mem) data = dummy """ for i in range(len(data)): x = data[i][1] y = data[i][2] data[i][1] = y data[i][2] = x """ # 風速と風向を入れ替える dummy = [] for mem in data: x = mem.pop(10) mem.insert(9, x) dummy.append(mem) data = dummy # 無い観測データ項目を追加 data = [y + ["", "", "", "", "", ""] for y in data] # 水蒸気圧を計算する dummy = [] for mem in data: P_hPa = "" try: t = float(mem[5]) # 気温[deg] U = float(mem[8]) # 相対湿度[%] P_hPa = feature.get_vapor_pressure(U, t) except: pass mem[7] = P_hPa dummy.append(mem) data = dummy # 露点温度 dummy = [] for mem in data: # http://d.hatena.ne.jp/Rion778/20121208/1354975888 dew_point_temperature = "" try: t = float(mem[5]) # 気温[deg] U = float(mem[8]) # 相対湿度[%] dew_point_temperature = feature.get_dew_point(U, t) except: pass mem[6] = dew_point_temperature dummy.append(mem) data = dummy #print(data) # join時にエラーが出ないように全てを文字列化 dummy = [] for x in data: x = [str(y) for y in x] dummy.append(x) data = dummy #print(data) lines += [",".join(x) for x in data] return lines