-
Notifications
You must be signed in to change notification settings - Fork 13
/
tranform_logs_to_directory.rb
executable file
·55 lines (41 loc) · 1.73 KB
/
tranform_logs_to_directory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env ruby
# This file reads a directory and transforms the files (generated by
# the SensorLogger android app) to Octave-readable files
#
# Roemer Vlasveld ([email protected])
require 'fileutils'
include FileUtils
suffix_to_filename = %w[rotation lin_acceleration gravity gyroscope accelerometer magnetic_field orientation proximity light]
directory = ARGV[0]
exit if directory.nil?
puts "Directory to scan: " + directory.inspect
all_files = Dir.entries(directory).select {|f| f if (f.split('.').last == 'csv') }
puts all_files
# Get first file to determine subdir name (timestamp)
unless all_files.empty?
filename = all_files.first
filename_parts = filename.split('_')
new_directory_name = File.join( directory, filename_parts[0] + '_' + filename_parts[1])
else
# No original csv files, assume the directory with modified files is already created
new_directory_name = File.join( directory, Dir.entries(directory).select { |d| File.directory? File.join(directory,d) and !(d =='.' || d == '..') }.first )
end
puts "New directory name: " + new_directory_name.inspect
mkdir_p( new_directory_name )
all_files.each do |filename|
new_file_name = suffix_to_filename[ filename.split('_').last[0].to_i ] + '.csv'
origin = File.join(directory, filename)
destination = File.join(new_directory_name, new_file_name)
# Replace ";" by ",", remove "," on end of line and escape comments
text = File.read(origin)
text.gsub!(/;/, ',')
text.gsub!(/,\n/, "\n" )
text.gsub!(/(Start|Unixtime)/, '#\1' )
# Write new file
File.open(destination, 'w') { |file| file.puts text }
end
# Call the octave plotter for all the generated files
plot_octave = ARGV[1] || true
if plot_octave == true
`./plot_sensors.m #{new_directory_name}`
end