Created
January 27, 2014 11:33
-
-
Save zeptometer/8647027 to your computer and use it in GitHub Desktop.
局地気象作成補助スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| require 'httpclient' | |
| require 'nokogiri' | |
| require 'date' | |
| # 2005/12/16-18 | |
| # 2002/12/14~15 | |
| # 2002/12/15~16 | |
| # | |
| class DataOfMonth | |
| attr_accessor :data | |
| def initialize(year, month) | |
| uri = 'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php' | |
| query = {'prec_no' => '48', 'block_no' => '0414', 'year' => year.to_s, 'month' => month.to_s} | |
| clnt = HTTPClient.new | |
| htxt = clnt.get_content(uri, query) | |
| @year = year | |
| @month = month | |
| @data = Nokogiri::HTML(htxt).css(".data2_s") | |
| end | |
| def dataOfDay(day) | |
| dd = @data.children[4+day] | |
| { | |
| month: @month, | |
| day: day, | |
| fall: dd.children[1].children[0].text.to_f, | |
| tmp_avg: dd.children[4].children[0].text.to_f, | |
| tmp_max: dd.children[5].children[0].text.to_f, | |
| tmp_min: dd.children[6].children[0].text.to_f, | |
| wind_max: dd.children[8].children[0].text.to_f, | |
| wind_dir: dd.children[9].children[0].text, | |
| time_day: dd.children[13].children[0].text.to_f, | |
| } | |
| end | |
| end | |
| def getWeatherTable(from, to) | |
| prev_month = nil | |
| dom = nil | |
| list = [] | |
| from.upto(to) do |date| | |
| if(prev_month != date.month) | |
| dom = DataOfMonth.new(date.year, date.month) | |
| end | |
| list.push(dom.dataOfDay(date.day)) | |
| end | |
| list | |
| end | |
| def writeTexTable(table, os) | |
| os.puts '' | |
| os.puts '\\begin{center}' | |
| os.puts ' \\begin{tabular}{r|l|lll|ll|ll} \\toprule' | |
| os.puts ' 日 & 降水量 & 平均 & 最高 & 最低 & 風速 & 風向 & 日照 \\\\ \\midrule' | |
| table.each do |row| | |
| os.printf(" %2d/%2d & %3.1f & %3.1f & %3.1f & %3.1f & %3.1f & %s & %3.1f \\\\\n", row[:month], row[:day], row[:fall], row[:tmp_avg], row[:tmp_max],row[:tmp_min],row[:wind_max],row[:wind_dir],row[:time_day]) | |
| end | |
| p os.puts ' \\bottomrule' | |
| os.puts ' \\end{tabular}' | |
| os.puts '\\end{center}' | |
| end | |
| $path = "/home/zeptometer/programs/TWV/tmp/" | |
| def dlpdf(y, m) | |
| filename = sprintf("%4d%02d.pdf",y,m) | |
| if (!File.exist?($path+filename)) | |
| `cd #{$path}; curl -O http://www.data.jma.go.jp/fcd/yoho/data/hibiten/#{y}/#{filename}` | |
| end | |
| end | |
| def makethumb(year,month,day) | |
| mx = 155 | |
| my = 245 | |
| w = (2360-mx)/3 | |
| h = (3250-my)/3 | |
| dlpdf(year,month); | |
| page = x = y = 0 | |
| if day<9 | |
| page = 1 | |
| x = day%3 | |
| y = day/3 | |
| else | |
| page = (day-9)/9+2 | |
| x = day%3 | |
| y = day%9/3 | |
| end | |
| inputfile = sprintf("%4d%02d.pdf",year,month) | |
| middlefile = sprintf("%4d%02d_%d",year,month,page) | |
| outputfile = sprintf("%4d%02d%02d",year,month,day) | |
| if (!File.exist?($path+middlefile+'png')) | |
| `pdftk #$path#{inputfile} cat #{page} output #$path#{middlefile}.pdf` | |
| `gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -sOutputFile=#$path#{middlefile}.png -r300 #$path#{middlefile}.pdf` | |
| puts "convert #$path#{middlefile}.png -crop #{w}x#{h}+#{mx+x*w}+#{my+y*h} #$path#{outputfile}.png" | |
| `convert #$path#{middlefile}.png -crop #{w}x#{h}+#{mx+x*w}+#{my+y*h} #$path/../img/#{outputfile}.png` | |
| `extractbb #$path/../img/#{outputfile}.png` | |
| end | |
| end | |
| def addtable(fy,fm,fd,ty,tm,td,file) | |
| f = open(file, "a") | |
| from = Date.new(fy,fm,fd) | |
| to = Date.new(ty,tm,td) | |
| f.puts "" | |
| f.puts "\\subsubsection*{気象データ(原村)}" | |
| writeTexTable(getWeatherTable(from, to), f) | |
| f.puts "" | |
| f.puts "\\subsubsection*{天気図}" | |
| f.puts "\\begin{center}" | |
| from.upto(to) do |day| | |
| makethumb(day.year, day.month, day.day) | |
| imgname = sprintf("%4d%02d%02d.png",day.year,day.month,day.day) | |
| f.puts "\\includegraphics[width=4cm]{img/#{imgname}}" | |
| end | |
| f.puts "\\end{center}" | |
| f.puts "" | |
| f.puts "\\subsubsection*{山行の記録}" | |
| (from+1).upto(to) do |day| | |
| f.puts "" | |
| f.puts "#{day.month}/#{day.day} " | |
| f.puts "\\begin{quotation}" | |
| f.puts "" | |
| f.puts "\\end{quotation}" | |
| end | |
| f.puts "" | |
| f.puts "\\subsubsection*{考察}" | |
| f.close() | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment