#!/usr/bin/ruby # processes output of prep-liststn.rb DISTANCE = 4 $count = 2 if /^N=(\d+)$/ === ARGV.last then $count = $1.to_i ARGV.pop end flat = Float(ARGV.shift) flon = Float(ARGV.shift) db = {} for line in ARGF sid, attrs, pos, name, ahl = line.chomp.split(/\t/) unless /^([-+]\d\d\.\d*)([-+]\d\d\d\.\d*)\// === pos STDERR.puts "#{pos}: bad position format" next end lat, lon = $1.to_f, $2.to_f distance = Math.hypot(lat - flat, lon - flon) distance += 1 unless /P/ === attrs distance += 1 unless /N/ === attrs next unless distance < DISTANCE attrs = attrs.split(/,/) ahl = [ahl.sub(/([A-Z]{4}[0-9]{2} [A-Z]{4}).*/, "\\1")] if db.include? sid attrs = (attrs + db[sid][0]).uniq ahl = (ahl + db[sid][4]).uniq end db[sid] = [attrs, distance, pos, name, ahl] end stns = db.keys.sort{|a, b| db[a][1] <=> db[b][1]} $count.times do |i| sid = stns[i] next unless sid attrs, distance, pos, name, ahl = db[sid] attrs = attrs.sort.reverse attrs.unshift(':') unless i.zero? puts [sid, attrs.join(','), pos, name, ahl.join(',')].join("\t") end