#!/usr/bin/ruby require 'gdbm' def xyz(lat, lon) phi = Math::PI * lat / 180.0 lam = Math::PI * lon / 180.0 z = Math::sin(phi) x = Math::cos(phi) * Math::cos(lam) y = Math::cos(phi) * Math::sin(lam) [x, y, z] end dbfile = ARGV.shift raise "usage: #$0 dbfile [input ...]" unless dbfile GDBM.open(dbfile) {|db| stations = db.keys for cccc in stations r = Hash[*db[cccc].to_s.split(/\t/).map{|s| s.split(/:/, 2)}.flatten] la1, lo1, la2, lo2 = r.values_at('la1', 'lo1', 'la2', 'lo2') next unless la1 and lo1 and la2 and lo2 la1, lo1, la2, lo2 = [la1, lo1, la2, lo2].map{|s| s.to_f} x1 = xyz(la1, lo1) x2 = xyz(la2, lo2) d12 = Math::hypot(Math::hypot(x2[0] - x1[0], x2[1] - x1[1]), x2[2] - x1[2]) d12 = format('%6.2f', Math::asin(d12 * 0.5) / Math::PI * 360.0) r['d12'] = d12 STDOUT.write "[#{cccc} #{d12}]" db[cccc] = r.to_a.sort.map{|kv| kv.join(':')}.join("\t") end puts '.' }