Class: RubyTDMS::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_tdms/segment.rb

Overview

Implements the TDMS segment, including a Segment factory and stream parser. TODO: Refactor the parser out? Too much coupling between Segment and Document!

Constant Summary collapse

FLAG_META_DATA =
1 << 1
FLAG_RAW_DATA =
1 << 3
FLAG_DAQMX_RAW_DATA =
1 << 7
FLAG_INTERLEAVED_DATA =
1 << 5
FLAG_BIG_ENDIAN =
1 << 6
FLAG_NEW_OBJECT_LIST =
1 << 2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Segment

Returns a new instance of Segment.



18
19
20
21
# File 'lib/ruby_tdms/segment.rb', line 18

def initialize(document)
	@document = document
	@objects = []
end

Instance Attribute Details

#chunk_countObject (readonly)

Returns the value of attribute chunk_count.



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

def chunk_count
  @chunk_count
end

#documentObject (readonly)

Returns the value of attribute document.



14
15
16
# File 'lib/ruby_tdms/segment.rb', line 14

def document
  @document
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#meta_data_lengthObject (readonly)

Returns the value of attribute meta_data_length.



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

def 
  @meta_data_length
end

#meta_data_offsetObject (readonly)

Returns the value of attribute meta_data_offset.



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

def 
  @meta_data_offset
end

#objectsObject (readonly)

Returns the value of attribute objects.



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

def objects
  @objects
end

#raw_data_offsetObject (readonly)

Returns the value of attribute raw_data_offset.



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

def raw_data_offset
  @raw_data_offset
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.parse_stream(stream, document) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ruby_tdms/segment.rb', line 25

def parse_stream(stream, document)
	new(document).tap do |new|
		new.parse_lead_in stream
		document.segments << new # TODO: smelly
		new. stream if new.meta_data?
	end
end

Instance Method Details

#big_endian?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ruby_tdms/segment.rb', line 68

def big_endian?
	flag? FLAG_BIG_ENDIAN
end

#daqmx_data?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/ruby_tdms/segment.rb', line 58

def daqmx_data?
	flag? FLAG_DAQMX_RAW_DATA
end

#flag?(flag) ⇒ Boolean

Checks if the segment flags have flag set.

Parameters:

  • flag (Fixnum)

    The flag mask to check, like FLAG_META_DATA or FLAG_RAW_DATA.

Returns:

  • (Boolean)

    Whether the segment has the flag in question set.



43
44
45
# File 'lib/ruby_tdms/segment.rb', line 43

def flag?(flag)
	!!(@flags & flag == flag)
end

#interleaved_data?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ruby_tdms/segment.rb', line 63

def interleaved_data?
	flag? FLAG_INTERLEAVED_DATA
end

#meta_data?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ruby_tdms/segment.rb', line 48

def meta_data?
	flag? FLAG_META_DATA
end

#new_object_list?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruby_tdms/segment.rb', line 73

def new_object_list?
	flag? FLAG_NEW_OBJECT_LIST
end

#parse_lead_in(stream) ⇒ Object

protected



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_tdms/segment.rb', line 80

def parse_lead_in(stream)
	@tag = stream.read 4
	@flags = stream.read_u32
	@version = stream.read_u32
	@length = stream.read_u64 # Overall length of segment minus length of lead-in, aka "Next segment offset" in NI docs.
	@meta_data_length = stream.read_u64 # Overall length of meta information, aka "Raw data offset" in NI docs.

	@meta_data_offset = @meta_data_length > 0 ? stream.pos : nil
	@raw_data_offset = stream.pos + @meta_data_length # Stream offset at which raw data for this segment begins.
	@raw_data_length = @length == -1 ? stream.length - @raw_data_offset : @length - @meta_data_length # Number of bytes raw data occupies. NI docs say @length == -1 means the entire file, after lead-in and header, is raw data.
end

#parse_meta_data(stream) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_tdms/segment.rb', line 93

def (stream)
	@number_of_objects = stream.read_u32

	@number_of_objects.times do
		object = ObjectParser.parse_stream stream, document, self
		@objects << object
	end


	@chunk_length = raw_channels.map(&:chunk_length).reduce(:+) # Length of an individual data chunk (summation of each object's raw data length)
	@chunk_count = @raw_data_length / @chunk_length
	raw_channels.each { |object| object.calculate_offsets }
end

#raw_channelsObject



35
36
37
# File 'lib/ruby_tdms/segment.rb', line 35

def raw_channels
	objects.select { |object| object.is_a? Objects::Channel }
end

#raw_data?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ruby_tdms/segment.rb', line 53

def raw_data?
	flag? FLAG_RAW_DATA
end