Class: Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/mdarray/csv.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.epochObject (readonly)

Returns the value of attribute epoch.



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

def epoch
  @epoch
end

Instance Attribute Details

#col_sepObject (readonly)

Returns the value of attribute col_sep.



6
7
8
# File 'lib/mdarray/csv.rb', line 6

def col_sep
  @col_sep
end

#quote_charObject (readonly)

Returns the value of attribute quote_char.



8
9
10
# File 'lib/mdarray/csv.rb', line 8

def quote_char
  @quote_char
end

#row_sepObject (readonly)

Returns the value of attribute row_sep.



7
8
9
# File 'lib/mdarray/csv.rb', line 7

def row_sep
  @row_sep
end

Class Method Details

.read_numeric(filename, headers = false) ⇒ Object





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
56
57
58
59
# File 'lib/mdarray/csv.rb', line 20

def self.read_numeric(filename, headers = false)

  buffer = Array.new
  lines = 0
  columns = nil

  CSV.foreach(filename,  
              {return_headers: false, 
                # headers: true,
                converters: [:numeric, :date]} ) do |row|

    if (headers)
      headers = false
      next
    end

    columns ||= row.size
    lines += 1

    row.each do |data|

      if (row.size != columns)
        raise "Data does not have the same number of columns for all lines"
      end

      # if it is a Date, then convert it to seconds since epoch
      if (data.is_a? Date)
        buffer << data.to_time.to_i
      end

      if (data.is_a? Numeric)
        buffer << data
      end
    end

  end

  [lines, columns, buffer]

end