#!/usr/local/bin/ruby
############################################################################
#                             parse_earthquakes.rb                         #
#                          Copyright (C) 2012, Gary Sherman                #
# Prep a text file of earthquake events with fixed length records to be    #
# imported as delimited text. The "|" is used as the delimiter.            #
#                                                                          #
############################################################################

f = File.open("../db_search10423")
# Skip the first two header records
2.times {f.gets}
# print the delimited header row containing the fields we are interested in
print "event_date|event_time|latitude|longitude|depth|magnitude\n"
# process the earthquake records
while not f.eof
  record = f.gets
  # use a fixed length approach to get the fields we want since
  # splitting on white space isn't feasible
  event_date = record[1..10]
  event_time = record[13..22]
  latitude = record[26..32]
  longitude = record[37..44]
  longitude_direction = record[46..46]
  depth = record[50..54]
  magnitude = record[66..69]
  # if the longitude is in the western hemisphere, it must be
  # negative
  if longitude_direction == 'W'
    longitude = -1 * longitude.to_f
end
  # print a delimited record
  STDOUT << event_date << "|" << event_time << "|" \
    << latitude << "|" << longitude << "|" \
    << depth.strip << "|" << magnitude.strip << "|\n"
end
# close the input file
f.close
