Class: EagleTree::Log::File

Inherits:
Object
  • Object
show all
Defined in:
lib/eagletree/log/file.rb

Overview

Represents a single Eagle Tree data logger file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ File

Returns a new instance of File.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/eagletree/log/file.rb', line 32

def initialize uri
  @sessions = []

  open(uri, 'rb') do |file|
    @name = file.gets.strip
    @meta = file.gets.strip.split
    session_count = @meta[27].to_i

    if 'All Sessions' != file.gets.strip
      raise RuntimeError, "No 'All Sessions' marker found"
    end

    # empty files are still valid, just have no sessions
    if session_count.zero?
      break
    end

    # read off the full file range
    all_sessions_range = file.gets.strip.split.map(&:to_i)

    session_count.times do |expected|
      num = /Session (?<num>\d)/.match(file.gets)[:num].to_i
      if (expected + 1) != num
        raise RuntimeError, "Unexpected session marker encountered"
      end

      range = file.gets.strip.split.map(&:to_i).map { |v| v * @meta[22].to_i }
      @sessions << Session.new(num, range)
    end

    session_index = 0
    session = @sessions[session_index]
    session_rows = []
    CSV.new(file, { :col_sep => ' ', :headers => true }).each do |csv|
      if !((session.range[0]..(session.range[1]-1)).include? csv[0].to_i)
        session.rows = session_rows
        session_index += 1
        session = @sessions[session_index]
        session_rows = []
      end

      session_rows << csv
    end
    session.rows = session_rows unless session.nil?

  end

  @hardware = @meta[23] # TODO interpret correctly
  @version = @meta[25].to_f

rescue
  raise ArgumentError, 'File does not appear to be an Eagle Tree log'
end

Instance Attribute Details

#hardwareString (readonly)

Returns Hardware that recorded this file.

Returns:

  • (String)

    Hardware that recorded this file



11
12
13
# File 'lib/eagletree/log/file.rb', line 11

def hardware
  @hardware
end

#nameString (readonly)

Returns Name given to the log file.

Returns:

  • (String)

    Name given to the log file



14
15
16
# File 'lib/eagletree/log/file.rb', line 14

def name
  @name
end

#sessionsArray<Session> (readonly)

Returns Sessions contained in this file.

Returns:

  • (Array<Session>)

    Sessions contained in this file



17
18
19
# File 'lib/eagletree/log/file.rb', line 17

def sessions
  @sessions
end

#versionFixNum (readonly)

Returns Version of the data recorder software.

Returns:

  • (FixNum)

    Version of the data recorder software



20
21
22
# File 'lib/eagletree/log/file.rb', line 20

def version
  @version
end

Class Method Details

.eagle_tree?(uri) ⇒ Boolean

Determines if the file at the given URI is a EagleTree log file. If one intends to read the file, simply using #new should be preferred in favor of this, catching any errors that may be raised.

Parameters:

  • uri

    URI to file to read

Returns:

  • (Boolean)

    true if the file is a EagleTree log file, false otherwise



28
29
30
# File 'lib/eagletree/log/file.rb', line 28

def self.eagle_tree? uri
  !!File.new(uri) rescue false
end

Instance Method Details

#durationFloat

Gets the total duration of all sessions contained within.

Returns:

  • (Float)

    total duration of all sessions, in seconds



89
90
91
# File 'lib/eagletree/log/file.rb', line 89

def duration
  @sessions.map(&:duration).reduce(&:+)
end

#to_kml(options = {}) ⇒ String

Converts the file into a KML document containing placemarks for each session containing GPS data.

Parameters:

  • options (Hash) (defaults to: {})

    hash containing options for file

Returns:

  • (String)

    KML document for all applicable sessions in the file

Raises:

  • (RuntimeError)

See Also:



106
107
108
109
# File 'lib/eagletree/log/file.rb', line 106

def to_kml(options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  to_kml_file(options).render
end

#to_kml?Boolean

Determines if KML methods can be called for this file.

Returns:

  • (Boolean)

    true if KML can be generated for this file, false otherwise



96
97
98
# File 'lib/eagletree/log/file.rb', line 96

def to_kml?
  @sessions.any?(&:to_kml?)
end

#to_kml_file(options = {}) ⇒ KMLFile

Converts the file into a KMLFile containing placemarks for each session containing GPS data.

Parameters:

  • options (Hash) (defaults to: {})

    hash containing options for file

Options Hash (options):

  • :name (String)

    name option of KML::Document

  • :description (String)

    name option of KML::Document

Returns:

  • (KMLFile)

    file for the session

Raises:

  • (RuntimeError)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/eagletree/log/file.rb', line 118

def to_kml_file(options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  options = apply_default_file_options(options)

  style = 'kmlfile-style-id'
  kml_sessions = @sessions.select(&:to_kml?)
  marks = kml_sessions.each_with_object({ :style_url => "##{style}" }).map(&:to_kml_placemark)

  kml = KMLFile.new
  kml.objects << KML::Document.new(
    :name => options[:name],
    :description => options[:description],
    :styles => [
      KML::Style.new(
        :id => style,
        :line_style => KML::LineStyle.new(:color => '7F00FFFF', :width => 4),
        :poly_style => KML::PolyStyle.new(:color => '7F00FF00')
      )
    ],
    :features => marks
  )
  kml
end