DTA_reader 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/DTA_reader.rb +178 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53b57d9a9ba0322c8606ab3adbd79a1e9d519b8f
4
+ data.tar.gz: 218bc2dcbc3bd24755222b02e974b570da190d5c
5
+ SHA512:
6
+ metadata.gz: cae5ceb541601801a90d8fda0410fbf8cd8f0085db73603a62261f7a752066d38cbf26086b207fedf8fae7fca8d1543ad3847e04d3f5746cee797addcc08843d
7
+ data.tar.gz: 0c5b3c5e1e6955a347c1ad0498c632a2411615c1dbb7ed76ec2739f9331cac857923497c0daf6499bda673008f244764d845f7d1aa11f2c0da82dd993b38d592
data/lib/DTA_reader.rb ADDED
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'colorize'
4
+
5
+ class DTA_ReaderErr < Exception; end
6
+
7
+ class DTA_Reader
8
+
9
+ attr_reader :output_ary, :fileinfo
10
+
11
+ def initialize(filename, out_filename = nil)
12
+ if File.exist?(filename) then
13
+ @input_file = filename.to_s
14
+ @input_ary = Array.new
15
+ @output_ary = Array.new
16
+ @fileinfo = Hash.new
17
+ File.foreach(@input_file) { |line| @input_ary << line.chomp }
18
+ parse
19
+ export(out_filename) if out_filename
20
+ else
21
+ raise DTA_ReaderErr, "File #{filename} do not exists!".red
22
+ end
23
+ end
24
+
25
+ # Export data to TSV file
26
+ def export(filename)
27
+ out = File.open(filename, 'w+')
28
+
29
+ # Write file eader information
30
+ out.puts '# ' + "#{@input_file} :: #{@fileinfo}"
31
+ outstr = ""
32
+ @output_ary.each { |e|
33
+ outstr += "#{e[:name]}_#{e[:unit]}\t"
34
+ }
35
+ out.puts outstr.rstrip
36
+
37
+ (0..@output_ary[0][:values].size - 1).each { |i|
38
+ outstr = ""
39
+ @output_ary.each { |e|
40
+ outstr += "#{'%.8e' % e[:values][i]}\t"
41
+ }
42
+ out.puts outstr.rstrip
43
+ }
44
+
45
+ out.close
46
+ end
47
+
48
+ private
49
+
50
+ # Actual data parsing
51
+ def parse
52
+ sampling
53
+ count
54
+ trigger
55
+ @output_ary << {:id => 0, :name => "time", :unit => "(s)", :values => generate_time, :factor => 1.0, :offset => 0}
56
+
57
+ (1..6).each { |i|
58
+ e = {:id => i, :name => "", :unit => "", :values => Array.new, :factor => 0.0, :offset => channel_offset(i)}
59
+ if e[:offset] > 0 then
60
+ e[:name] = channel_name(i)
61
+ e[:unit] = channel_unit(i)
62
+ e[:factor] = channel_factor(i)
63
+ e[:values] = channel_values(i)
64
+ @output_ary << e
65
+ end
66
+ }
67
+ end
68
+
69
+ # Channel specific methods: num is always input channel number
70
+
71
+ # Returns the channel data starting point
72
+ def channel_offset(num)
73
+ regexp = /^\[CHANNEL\s*#{num}\]/
74
+ @input_ary.each_with_index { |e,i|
75
+ #puts "Found channel #{num}".green
76
+ return i+1 if e =~ regexp
77
+ }
78
+ puts "WARNING: Channel #{num} not found!".yellow
79
+ return -1
80
+ end
81
+
82
+ # Searches for channel name. Raise an error if it cannot find it.
83
+ def channel_name(num)
84
+ regexp = /^CHANNEL#{num}="(.*)"\s*/
85
+ @input_ary.each { |e|
86
+ return $1.to_s.gsub(/\s+/,"_").strip if e =~ regexp
87
+ }
88
+ raise DTA_ReaderErr, "Cannot find channel #{num} name"
89
+ end
90
+
91
+ # Searches for channel measurement unit, set to default (unknown) if it cannot find it
92
+ def channel_unit(num)
93
+ regexp = /^DIM#{num}="(.*)"\s*/
94
+ @input_ary.each { |e|
95
+ return "(" + $1.to_s.gsub(/\s+/,"_").strip + ")" if e =~ regexp
96
+ }
97
+ puts "WARNING: Cannot find channel #{num} name. Assigning a default value"
98
+ return "(unknown)"
99
+ end
100
+
101
+ # Searches for the channel factor. Returns 1.0 if it cannot find it.
102
+ def channel_factor(num)
103
+ regexp = /^FACTOR#{num}=(.*)\s*/
104
+ @input_ary.each { |e|
105
+ return $1.to_f if e =~ regexp
106
+ }
107
+ puts "WARNING: Cannot find channel #{num} factor. Assigning a default value"
108
+ return 1.0
109
+ end
110
+
111
+ # Read actual channel data starting from the trigger point.
112
+ def channel_values(num)
113
+ ret = Array.new
114
+
115
+ count unless @fileinfo[:count]
116
+ trigger unless @fileinfo[:trigger]
117
+
118
+ offset = channel_offset(num)
119
+ factor = channel_factor(num)
120
+ @input_ary[(offset + @fileinfo[:trigger])..(offset + @fileinfo[:count] - 1)].each_with_index { |line,i|
121
+ split = line.split
122
+ raise DTA_ReaderErr, "Error parsing file at line #{i+1}" if split.size != 3
123
+ ret << (split[0].to_f * factor)
124
+ }
125
+ return ret
126
+ end
127
+
128
+ # Global file methods
129
+ # Searches for sampling time
130
+ def sampling
131
+ @input_ary.each_with_index { |line,i|
132
+ if line =~ /^SAMPLING=([0-9]*)\s*/
133
+ @fileinfo[:sampling] = $1.to_i
134
+ at = i+1
135
+ end
136
+ }
137
+ raise DTA_ReaderErr, "Cannot find sampling time!" unless @fileinfo[:sampling]
138
+ raise DTA_ReaderErr, "Error parsing sampling time :: #{@input_file}:#{at} :: SAMPLING=" + $1.inspect if @fileinfo[:count] == 0
139
+ end
140
+
141
+ # Searches for number of elements
142
+ def count
143
+ @input_ary.each_with_index { |line,i|
144
+ if line =~ /^COUNT=([0-9]*)\s*/
145
+ @fileinfo[:count] = $1.to_i
146
+ at = i+1
147
+ end
148
+ }
149
+ raise DTA_ReaderErr, "Cannot define number of elements!" unless @fileinfo[:count]
150
+ raise DTA_ReaderErr, "Error parsing elements count :: #{@input_file}:#{at} :: COUNT=" + $1.inspect if @fileinfo[:count] == 0
151
+ end
152
+
153
+ # Serches for trigger offset
154
+ def trigger
155
+ count unless @fileinfo[:count]
156
+ @input_ary.each_with_index { |line,i|
157
+ if line =~ /^TRIGGER=([0-9]*)\s*/
158
+ @fileinfo[:trigger] = $1.to_i
159
+ at = i
160
+ end
161
+ }
162
+ raise DTA_ReaderErr, "Cannot define trigger offset!" unless @fileinfo[:count]
163
+ end
164
+
165
+ # Return an array of time, starting from the trigger point
166
+ def generate_time
167
+ ret = Array.new
168
+ count unless @fileinfo[:count]
169
+ trigger unless @fileinfo[:trigger]
170
+ sampling unless @fileinfo[:sampling]
171
+
172
+ (0..@fileinfo[:count] - @fileinfo[:trigger] - 1).each {|i|
173
+ ret << (i * @fileinfo[:sampling] * 1e-6)
174
+ }
175
+ return ret
176
+ end
177
+ end
178
+ #EOF
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DTA_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matteo Ragni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Reader for iTNC530 Oscilloscope *.dta files
14
+ email: matteo.ragni.it@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/DTA_reader.rb
20
+ homepage: http://github.com/MatteoRagni/DTA_reader
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Reader for iTNC530 Oscilloscope *.dta files
44
+ test_files: []
45
+ has_rdoc: