require 'detac' require 'rubygems' require 'sqlite3' # decodes SYNOP in ARGF files, then lists stations in synop-*.tsv format # using parameters in SQLite3 database "wmo9.sqlite" class ListStn def initialize argv db = argv.shift raise "specify station db" unless File.readable?(db) @db = SQLite3::Database.new(db) end def close @db.close end def resolve stnid a = nil sql = <<-ENDSQL SELECT lat, lon, hp, stnname, cntname FROM vola WHERE idxnum = ? ORDER BY idxsub ASC LIMIT 1 ENDSQL @db.execute(sql, stnid) {|row| a = row } a end def run argv for file in argv DeTac.new(file).decode {|mimj, dcd| stnid = dcd['@ID'] next unless stnid stnid = stnid.to_i next if dcd['Lo.4'] or dcd['Lo.6'] lat, lon, hp, stnname, cntname = resolve(stnid) next unless cntname name = [stnname, cntname].join(', ').gsub(/\t/, ' ') state = [] state.push 'P' if dcd['P.4'] state.push 'w' if dcd['ww'] or dcd['iw'] == 2 or dcd['iw'] == 5 state.push 'N' if dcd['N'] state.push hp.to_i.to_s state = state.join(',') printf("%05u\t%s\t%+05.1f%+06.1f/\t%s\t%s\n", stnid, state, lat.to_f, lon.to_f, name, dcd['AHL']) } end ensure close end end ListStn.new(ARGV).run(ARGV)