Skip to content

Commit

Permalink
Adds altitude to the flight_path hash and exits out if the file does …
Browse files Browse the repository at this point in the history
…not have an A record
  • Loading branch information
bbonamin committed Sep 24, 2012
1 parent 0de8ca6 commit 5605fd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lib/igc/geolocation.rb
@@ -1,12 +1,16 @@
module IGC
module Geolocation
# Convert geocoordinates from mindec notation of IGC to dec notation
# Public: Converts geocoordinates from mindec notation of IGC to dec notation
#
# @param [String] long The longitude from the igc file
# @param [String] lat The Latitude from the igc file
# @return [Float, Float] Longitude and Latitude in decimal notation
# @example Convert a pair of coordinates
# GeoLocation.to_dec("01343272E", "4722676N") #=>[13.7212,47.37793333333333]
# long: The longitude from the igc file
# lat: The Latitude from the igc file
#
# Example
#
# GeoLocation.to_dec("01343272E", "4722676N")
# #=>[13.7212,47.37793333333333]
#
# Returns Longitude and Latitude in decimal notation
def self.to_dec(long, lat)

long_m = long.match(/^(\d{3})((\d{2})(\d{3}))(E|W)/)
Expand Down
28 changes: 26 additions & 2 deletions lib/igc/reader.rb
@@ -1,8 +1,10 @@
module IGC
class Reader
attr_reader :contents
attr_reader :a_record

REGEX_H_DTE = /^hf(dte)((\d{2})(\d{2})(\d{2}))/i
REGEX_A = /^[a]([a-z\d]{3})([a-z\d]{3})?(.*)$/i
REGEX_H = /^[h][f|o|p]([\w]{3})(.*):(.*)$/i
REGEX_B = /^(B)(\d{2})(\d{2})(\d{2})(\d{7}[NS])(\d{8}[EW])([AV])(\d{5})(\d{5})/

Expand All @@ -13,6 +15,10 @@ def initialize(file_path)
f = File.open(file_path) do |file|
@contents = file.read
end

unless @a_record = @contents.match(REGEX_A)
raise "Invalid file format"
end
end

# Public : Scans the contents to get the date of the IGC file.
Expand All @@ -25,6 +31,10 @@ def date
date = Date.strptime(date_array[1],'%d%m%y')
end

# Public : Returns the A record of the file
def device_id

end
# Public : Scans the contents to get all the IGC header information (Section H)
#
# Returns a hash with all the header keys as indexes and their values.
Expand All @@ -42,10 +52,24 @@ def headers
# Returns a hash with the time as the key and an array with
def flight_path
flight_path = {}
@contents.scan(REGEX_B).each do |b|
b_records = @contents.scan(REGEX_B)

gps_present = b_records.map{|b| b[8].to_i - b[7].to_i}.uniq

# NOTE: The altitude is given by both the pressure sensor and gps,
# we should use the pressure altitude when it's present (b[8]),
# otherwise, the should use the gps alt. For now we'll just
# use the gps altitude.
b_records.each do |b|

# Gets the datetime from the file date + time of record
time = DateTime.new(date.year, date.month, date.day,
b[1].to_i, b[2].to_i, b[3].to_i).to_s
position = IGC::Geolocation.to_dec(b[5], b[4])

# Gets the position at that time by long, lat and alt.
position = IGC::Geolocation.to_dec(b[5], b[4]) + [b[7].to_i]

# Adds the time and position in the hash
flight_path[time] = position
end
flight_path
Expand Down

0 comments on commit 5605fd4

Please sign in to comment.