Method: MDArray.build

Defined in:
lib/mdarray/creation.rb

.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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mdarray/creation.rb', line 63

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

  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

  # Java-NetCDF creates an ArrayObject when given type string.  It should create an
  # ArrayString instead.  Some string methods in Java-NetCDF expect an ArrayObject
  # instead of an ArrayString, however, other libraries actually expect an ArrayString,
  # so we know have two type: "string" stores internally the data as an ArrayObject, 
  # "rstring" stores data internally as an ArrayString
  rtype = (type == "rstring")? "string" : type
  dtype = DataType.valueOf(rtype.upcase)

  jshape = shape.to_java :int

  if (storage)
    jstorage = storage.to_java rtype.downcase.to_sym
    if (type == "rstring")
      # circunvent bug in Java-NetCDF.  Type rstring is actually type string but should
      # and should build and ArrayString and not an ObjectString which is currently being
      # build.
      index = Java::UcarMa2.Index.factory(jshape)
      nc_array = Java::UcarMa2.ArrayString.factory(index, jstorage)
    else
      nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
    end
  else
    nc_array = Java::UcarMa2.Array.factory(dtype, jshape)
  end

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

end