Class: MDArray

Inherits:
Object
  • Object
show all
Defined in:
lib/JRubyR/as_mdarray.rb,
lib/JRubyR/index.rb

Overview



Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(type, shape, storage = nil, layout = :row) ⇒ Object


Builds a new MDArray


Parameters:

  • type

    the type of the new mdarray to build, could be boolean, byte, short, int, long, float, double, string, structure

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/JRubyR/index.rb', line 53

def self.build(type, shape, storage = nil, layout = :row)

  if !@LAYOUT.include?(layout)
    raise "Unknown layout #{layout}"
  end

  if (shape.is_a? String)
    # building from csv
    # using shape as filename
    # using storage as flag for headers
    storage = (storage)? storage : false
    parameters = Csv.read_numeric(shape, storage)
    shape=[parameters[0], parameters[1]]
    storage = parameters[2]
  end

  if (storage)
    # nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
    nc_array = make_nc_array(type, shape, storage, layout)
  else
    nc_array = Java::UcarMa2.Array
      .factory(DataType.valueOf(type.upcase), shape.to_java(:int))
  end

  klass = Object.const_get("#{type.capitalize}MDArray")
  return klass.new(type, nc_array)

end

.comp_stride(shape) ⇒ Object


Computes the stride for the given shape and column-major layout




106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/JRubyR/index.rb', line 106

def self.comp_stride(shape)

  stride = Array.new(shape.size)
  stride[-1], stride[-2] = shape[-2], 1
  product = shape[-1] * shape[-2]

  if (shape.size > 2)
    (shape.length - 3).downto(0).each do |i|
      stride[i] = product
      product *= shape[i]
    end
  end

  stride

end

.from_jstorage(type, shape, jstorage, section = false, layout = :row) ⇒ Object





147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/JRubyR/index.rb', line 147

def self.from_jstorage(type, shape, jstorage, section = false, layout = :row)

  if !@LAYOUT.include?(layout)
    raise "Unknown layout #{layout}"
  end

  if (shape.size == 1 && shape[0] == 0)
    return nil
  end

  dtype = DataType.valueOf(type.upcase)
  jshape = shape.to_java :int

  case layout
  when :row
    nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
    klass = Object.const_get("#{type.capitalize}MDArray")
    return klass.new(type, nc_array, section)
  else
    jclass = Java::UcarMa2::Array.java_class
    jstorage = storage.to_java(type)
    nc_array = Java::RbScicom::PrivateCall
      .factoryInvoke(jclass, type.capitalize, index_factory(shape), jstorage)
    MDArray.build_from_nc_array(type, nc_array)
  end

end

.index_factory(shape) ⇒ Object


Creates new index with the given shape and column-major layout




86
87
88
89
90
91
92
93
94
# File 'lib/JRubyR/index.rb', line 86

def self.index_factory(shape)

  stride = comp_stride(shape)
  index = Java::UcarMa2.Index.factory(shape.to_java(:int))
  index.stride = stride.to_java(:int)
  index.precalc
  index

end

.make_nc_array(type, shape, storage, layout) ⇒ Object


Makes a NetCDF Array with the given storage and layout.




127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/JRubyR/index.rb', line 127

def self.make_nc_array(type, shape, storage, layout)

  dtype = DataType.valueOf(type.upcase)
  jshape = shape.to_java :int
  jstorage = storage.to_java type.downcase.to_sym

  if (layout == :row || shape.size == 1)
    nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
  else
    jclass = Java::UcarMa2::Array.java_class
    nc_array = Java::RbScicom::PrivateCall
      .factoryInvoke(jclass, type.capitalize, index_factory(shape), jstorage)
  end

end

Instance Method Details

#ppObject





43
44
45
# File 'lib/JRubyR/as_mdarray.rb', line 43

def pp
  print
end

#zObject





35
36
37
# File 'lib/JRubyR/as_mdarray.rb', line 35

def z
  self[0]
end