Class: Gooby::GoobyCommand

Inherits:
GoobyObject show all
Defined in:
lib/gooby_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GoobyKernel

#character_align, #default_delimiter, #invalid_altitude, #invalid_heartbeat, #invalid_latitude, #invalid_longitude, #invalid_time, #project_author, #project_copyright, #project_date, #project_embedded_comment, #project_license, #project_name, #project_version_number, #project_year, #read_as_ascii_lines, #read_lines, #strip_lines, #tokenize

Constructor Details

#initialize(gooby_yaml_filename = nil) ⇒ GoobyCommand

Returns a new instance of GoobyCommand.



15
16
17
# File 'lib/gooby_command.rb', line 15

def initialize(gooby_yaml_filename=nil)
  @configuration = Gooby::Configuration.init(gooby_yaml_filename)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



13
14
15
# File 'lib/gooby_command.rb', line 13

def configuration
  @configuration
end

#csv_col_namesObject (readonly)

Returns the value of attribute csv_col_names.



13
14
15
# File 'lib/gooby_command.rb', line 13

def csv_col_names
  @csv_col_names
end

#csv_pointsObject (readonly)

Returns the value of attribute csv_points.



13
14
15
# File 'lib/gooby_command.rb', line 13

def csv_points
  @csv_points
end

Instance Method Details

#been_there(course_id, proximity = 0.0070, uom = 'deg') ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gooby_command.rb', line 108

def been_there(course_id, proximity=0.0070, uom='deg')
  unless @csv_points
    puts "You must first invoke method 'read_csv_files' with a list of parsed CSV filenames."
    return
  end
  course = configuration.get_course("#{course_id}")
  unless course
    puts "Unable to find course id '#{course_id}' in the gooby config yaml file."
    return
  end
  puts ''
  # collect the csv_points into run arrays
  @curr_run_id = 'x'      
  @csv_runs = Array.new  
  @csv_points.each { |csv_point|      
    if (csv_point.run_id == @curr_run_id)
      @curr_run.add_point(csv_point)  
    else
      @curr_run_id = csv_point.run_id
      @curr_run = Gooby::CsvRun.new(@curr_run_id)
      @curr_run.add_point(csv_point)
      @csv_runs << @curr_run
    end
  }
  
  # iterate the runs - looking for a match vs the course
  @csv_runs.each { |csv_run|
    puts "Scanning run id '#{csv_run.id}', point count= #{csv_run.points.size}" if false
    run_points = csv_run.points
    course.reset
    course.points.each { |course_point|
      closest_diff  = 999
      closest_point = nil
      run_points.each { |run_point|
        diff  = course_point.degrees_diff(run_point) 
        if ((diff < proximity) && (diff < closest_diff))
            closest_diff  = diff
            closest_point = run_point
            closest_point.degrees_diff = diff
            course.matched(course_point.number, run_point)
        end            
      }
    }
    if course.matched?
      puts "Course '#{course.name}' matched vs run id '#{csv_run.id}'" 
      course.display_matches
    else
      puts "course not matched" if false
    end
  }
end

#display_configurationObject



19
20
21
22
# File 'lib/gooby_command.rb', line 19

def display_configuration
  #puts @configuration.to_s
  @configuration.print_all
end

#display_versionObject



24
25
26
27
# File 'lib/gooby_command.rb', line 24

def display_version
  s = "# #{project_name} #{project_version_number} #{project_date}.  #{project_copyright}."
  puts s
end

#first_trackpoints_as_poi(date_string) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gooby_command.rb', line 160

def first_trackpoints_as_poi(date_string)
  # cat 2007_poi.csv | sort +4n > 2007_poi_sorted.csv
  home_point = Point.new('35.495469007128285', '-80.83213448524475', '752.5755445824', 'Home at South Faulkner Way Stopsign') # TODO
  # poi.123  35.484114   -80.820274    788.7661681152  2007-06-30 10:48:32 1.01214244648463
  @csv_points.each { |csv_point|
  proximity = home_point.proximity(csv_point, 'm')
  if (csv_point.lap_number == 1)
    if date_string
      if csv_point.date == date_string
        puts sprintf("%s %s %s %s %s %s %s", 'poi.   ', 
          character_align('.', csv_point.latitude,  3, 7), 
          character_align('.', csv_point.longitude, 4, 7), 
          character_align('.', csv_point.altitude,  5, 11),
          character_align('.', proximity.to_s,  3, 20),              
          csv_point.date,  
          csv_point.time,
          csv_point.distance)
      end
    else
      puts sprintf("%s %s %s %s %s %s %s", 'poi.   ', 
        character_align('.', csv_point.latitude,  3, 7), 
        character_align('.', csv_point.longitude, 4, 7), 
        character_align('.', csv_point.altitude,  5, 11),
        character_align('.', proximity.to_s,  3, 20),   
        csv_point.date,  
        csv_point.time,
        csv_point.distance)
    end
  end
  }
end

#generate_google_map(argv) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/gooby_command.rb', line 192

def generate_google_map(argv)  
  if (argv == nil)
    puts "ERROR: no ARGV args passed."
  elsif (argv.size < 1)
    puts ""
    puts "Invalid program args; one required - input csv filename"
    puts "  the first arg, csv filename, was produced by the Gooby 'parser.rb' program."
    puts "Please correct the program arguments and try again. \n\n"
  else
    csv_filename  = argv[0]
    configuration = Gooby::Configuration.get_config
    generator = Gooby::GoogleMapGenerator.new(csv_filename)  
    generator.generate_page(configuration)
  end      
end

#parse_garmin_forerunner_logbook_xml(xml_filename, uom) ⇒ Object



85
86
87
88
89
# File 'lib/gooby_command.rb', line 85

def parse_garmin_forerunner_logbook_xml(xml_filename, uom)  
  handler = Gooby::ForerunnerXmlParser.new(uom)    
  Document.parse_stream((File.new xml_filename), handler)
  handler.put_all_run_tkpt_csv()
end

#parse_garmin_training_center_xml(tcx_filename, uom) ⇒ Object



91
92
93
94
95
# File 'lib/gooby_command.rb', line 91

def parse_garmin_training_center_xml(tcx_filename, uom)  
  handler = Gooby::TrainingCenterXmlParser.new(uom)   
  Document.parse_stream((File.new tcx_filename), handler)
  handler.put_all_run_tkpt_csv()
end

#parse_garmin_xml_file(argv) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gooby_command.rb', line 62

def parse_garmin_xml_file(argv)
  if (argv == nil)
    puts "ERROR: no ARGV args passed."
  elsif (argv.size < 2)
    puts ""
    puts "Invalid program args; three args required - format, uom, input (xml) filename"
    puts "  the first arg, format, should be one of: garmin201, garmin205, garmin305, etc."
    puts "  the second arg, unit-of-measure, should be either 'english' or 'metric'." 
    puts "  the third arg, input xml filename, was produced by the Gooby 'splitter.rb' program."
    puts "Please correct the program arguments and try again. \n\n"
  else
    format   = argv[0].downcase
    uom      = argv[1].downcase        
    filename = argv[2]
    puts Trackpoint.csv_header        
    if (format == 'garmin201')
      parse_garmin_forerunner_logbook_xml(filename, uom)
    else
      parse_garmin_training_center_xml(filename, uom) 
    end
  end
end

#read_csv_files(array_of_filenames, record_index = 0) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/gooby_command.rb', line 97

def read_csv_files(array_of_filenames, record_index=0)
  @csv_reader = Gooby::CsvReader.new(array_of_filenames)
  @csv_points = @csv_reader.read
  @csv_col_names = @csv_reader.col_names
  puts @csv_reader.to_s
  if (record_index > 0)
    @csv_reader.display_formatted_record(record_index) 
  end
  @csv_points
end

#split_garmin_export_file(argv) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gooby_command.rb', line 29

def split_garmin_export_file(argv)
  if (argv == nil)
    puts "ERROR: no ARGV args passed."
  elsif (argv.size < 3)
    puts ""
    puts "Invalid program args; three args required - format, input filename, output directory"
    puts "  the first arg, format, should be one of: garmin201, garmin205, garmin305, etc."
    puts "  the second arg is the input filename - which is a garmin export file"
    puts "  the third arg is the output directory where the split files are written to\n\n"
    puts "Please correct the program arguments and try again. \n\n"
  else
    format   = argv[0].downcase
    filename = argv[1]
    out_dir  = argv[2]
    
    if (format == 'garmin201')
      split_garmin_forerunner_logbook_xml(filename, out_dir)
    else
      split_garmin_training_center_xml(filename, out_dir) 
    end
  end
end

#split_garmin_forerunner_logbook_xml(xml_filename, out_dir) ⇒ Object



52
53
54
55
# File 'lib/gooby_command.rb', line 52

def split_garmin_forerunner_logbook_xml(xml_filename, out_dir)  
  splitter = Gooby::ForerunnerXmlSplitter.new(xml_filename, out_dir)  
  splitter.split
end

#split_garmin_training_center_xml(tcx_filename, out_dir) ⇒ Object



57
58
59
60
# File 'lib/gooby_command.rb', line 57

def split_garmin_training_center_xml(tcx_filename, out_dir)  
  splitter = Gooby::TrainingCenterXmlSplitter.new(tcx_filename, out_dir)  
  splitter.split
end