Class: NetCDF::Variable

Inherits:
CDMNode show all
Defined in:
lib/netcdf/variable.rb

Overview

A Variable is a logical container for data. It has a dataType, a set of Dimensions that define its array shape, and optionally a set of Attributes. The data is a multidimensional array of primitive types, Strings, or Structures. Data access is done through the read() methods, which return a memory resident Array. Immutable if setImmutable() was called.

Direct Known Subclasses

TimeVariable, VariableWriter

Instance Attribute Summary collapse

Attributes inherited from CDMNode

#netcdf_elmt

Instance Method Summary collapse

Methods inherited from CDMNode

#get_full_name, #get_short_name, #initialize

Constructor Details

This class inherits a constructor from NetCDF::CDMNode

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


34
35
36
# File 'lib/netcdf/variable.rb', line 34

def attributes
  @attributes
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions


35
36
37
# File 'lib/netcdf/variable.rb', line 35

def dimensions
  @dimensions
end

#index_iteratorObject (readonly)

Returns the value of attribute index_iterator


36
37
38
# File 'lib/netcdf/variable.rb', line 36

def index_iterator
  @index_iterator
end

Instance Method Details

#cached_data?Boolean


Has data been read and cached.


Returns:

  • (Boolean)

200
201
202
# File 'lib/netcdf/variable.rb', line 200

def cached_data?
  @netcdf_elmt.hasCachedData()
end

#caching=(boolean) ⇒ Object




346
347
348
# File 'lib/netcdf/variable.rb', line 346

def caching=(boolean)
  @netcdf_elmt.setCaching(boolean)
end

#caching?Boolean


Will this Variable be cached when read.


Returns:

  • (Boolean)

216
217
218
# File 'lib/netcdf/variable.rb', line 216

def caching?
  @netcdf_elmt.isCaching()
end

#coordinate_variable?Boolean


Calculate if this is a classic coordinate variable: has same name as its first dimension.


Returns:

  • (Boolean)

225
226
227
# File 'lib/netcdf/variable.rb', line 225

def coordinate_variable?
  @netcdf_elmt.isCoordinateVariable()
end

#create_new_cacheObject


Create a new data cache, use this when you dont want to share the cache.



50
51
52
# File 'lib/netcdf/variable.rb', line 50

def create_new_cache
  @netcdf_elmt.createNewCache()
end

#extra_infoObject




42
43
44
# File 'lib/netcdf/variable.rb', line 42

def extra_info
  @netcdf_elmt.extraInfo()
end

#find_attribute(name, ignore_case = false) ⇒ Object


Finds an attribute by name



58
59
60
61
62
63
64
65
66
# File 'lib/netcdf/variable.rb', line 58

def find_attribute(name, ignore_case = false)

  if (ignore_case)
    @netcdf_elmt.findAttributeIgnoreCase(name)
  else
    @netcdf_elmt.findAttribute(name)
  end

end

#find_attributesObject


Returns the set of attributes for this variable.



80
81
82
83
84
85
86
87
88
89
# File 'lib/netcdf/variable.rb', line 80

def find_attributes

  attributes = @netcdf_elmt.getAttributes()
  atts = Array.new
  attributes.each do |att|
    atts << NetCDF::Attribute.new(att)
  end
  atts

end

#find_dimension_index(name) ⇒ Object


Find the index of the named Dimension in this Variable.



72
73
74
# File 'lib/netcdf/variable.rb', line 72

def find_dimension_index(name)
  @netcdf_elmt.findDimensionIndex(name)
end

#find_dimensionsObject


Get the list of dimensions used by this variable.



119
120
121
122
123
124
125
126
127
128
# File 'lib/netcdf/variable.rb', line 119

def find_dimensions

  dimensions = @netcdf_elmt.getDimensions()
  dims = Array.new
  dimensions.each do |dim|
    dims << NetCDF::Dimension.new(dim)
  end
  dims

end

#get_data_typeObject


Gets the variable data_type



95
96
97
# File 'lib/netcdf/variable.rb', line 95

def get_data_type
  @netcdf_elmt.getDataType().toString()
end

#get_descriptionObject


Get the description of the Variable.



103
104
105
# File 'lib/netcdf/variable.rb', line 103

def get_description
  @netcdf_elmt.getDescription()
end

#get_dimension(index) ⇒ Object


Get the ith dimension.



111
112
113
# File 'lib/netcdf/variable.rb', line 111

def get_dimension(index)
  @netcdf_elmt.getDimension(index)
end

#get_dimensions_stringObject


Get the list of Dimension names, space delineated.



134
135
136
# File 'lib/netcdf/variable.rb', line 134

def get_dimensions_string
  @netcdf_elmt.getDimensionsString()
end

#get_element_sizeObject


Get the number of bytes for one element of this Variable.



142
143
144
# File 'lib/netcdf/variable.rb', line 142

def get_element_size
  @netcdf_elmt.getElementSize()
end

#get_name_and_dimensionsObject


Get the display name plus the dimensions, eg 'float name(dim1, dim2)'



150
151
152
# File 'lib/netcdf/variable.rb', line 150

def get_name_and_dimensions
  @netcdf_elmt.getNameAndDimensions()
end

#get_rankObject Also known as: rank


Get the number of dimensions of the Variable.



158
159
160
# File 'lib/netcdf/variable.rb', line 158

def get_rank
  @netcdf_elmt.getRank()
end

#get_shape(index = nil) ⇒ Object Also known as: shape


Get the shape: length of Variable in each dimension.



168
169
170
171
172
173
174
# File 'lib/netcdf/variable.rb', line 168

def get_shape(index = nil)
  if (index)
    @netcdf_elmt.getShape(index)
  else
    @netcdf_elmt.getShape().to_a
  end
end

#get_sizeObject Also known as: size


Get the total number of elements in the Variable.



182
183
184
# File 'lib/netcdf/variable.rb', line 182

def get_size
  @netcdf_elmt.getSize()
end

#get_units_stringObject


Get the Unit String for the Variable.



192
193
194
# File 'lib/netcdf/variable.rb', line 192

def get_units_string
  @netcdf_elmt.getUnitsString()
end

#immutable?Boolean


Is this Variable immutable


Returns:

  • (Boolean)

233
234
235
# File 'lib/netcdf/variable.rb', line 233

def immutable?
  @netcdf_elmt.isImmutable()
end

#invalidate_cacheObject


Invalidate the data cache



208
209
210
# File 'lib/netcdf/variable.rb', line 208

def invalidate_cache
  @netcdf_elmt.invalidateCache()
end

#metadata?Boolean


Is this variable metadata?.


Returns:

  • (Boolean)

241
242
243
# File 'lib/netcdf/variable.rb', line 241

def metadata?
  @netcdf_elmt.()
end

Prints the content of the current data slice



399
400
401
# File 'lib/netcdf/variable.rb', line 399

def print
  p to_string
end

#read(*args) ⇒ Object


Reads data for this Variable and sets the variable @data to the memory resident array. :origin int array with the origin of data to be read :shape int array with the shape of the data to be read. If origin or



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/netcdf/variable.rb', line 285

def read(*args)

  opts = Map.options(args)
  spec = opts.getopt(:spec)
  origin = opts.getopt(:origin)
  shape = opts.getopt(:shape)

  if (origin || shape)
    MDArray.build_from_nc_array(nil, @netcdf_elmt.read(origin.to_java(:int), 
                                                       shape.to_java(:int)))
  elsif (spec)
    MDArray.build_from_nc_array(nil, @netcdf_elmt.read(spec))
  else
    MDArray.build_from_nc_array(nil, @netcdf_elmt.read())
  end
  
end

#read_scalar(type = nil) ⇒ Object




307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/netcdf/variable.rb', line 307

def read_scalar(type = nil)

  if (!type)
    type = get_data_type
  end

  case type
  when "double"
    @netcdf_elmt.readScalarDouble()
  when "float"
    @netcdf_elmt.readScalarFloat()
  when "long"
    @netcdf_elmt.readScalarLong()
  when "int"
    @netcdf_elmt.readScalarInt()
  when "short"
    @netcdf_elmt.readScalarShort()
  when "byte"
    @netcdf_elmt.readScalarByte()
  when "string"
    @netcdf_elmt.readScalarString()
  else
    raise "Unknown type: #{type}"
  end

end

#scalar?Boolean


Whether this is a scalar Variable (rank == 0).


Returns:

  • (Boolean)

249
250
251
# File 'lib/netcdf/variable.rb', line 249

def scalar?
  @netcdf_elmt.isScalar()
end

#section(*args) ⇒ Object


Create a new Variable that is a logical subsection of this Variable. The subsection can be specified passing the following arguments: shape origin size stride range section spec



362
363
364
365
366
367
# File 'lib/netcdf/variable.rb', line 362

def section(*args)

  sec = MDArray::Section.build(*args)
  NetCDF::Variable.new(@netcdf_elmt.section(sec.netcdf_elmt))
  
end

#set_cached_data(array, metadata) ⇒ Object




338
339
340
# File 'lib/netcdf/variable.rb', line 338

def set_cached_data(array,  = nil)
  @netcdf_elmt.setCachedData(array.nc_array, )
end

#to_stringObject Also known as: to_s


Prints the content of the current data slice



381
382
383
# File 'lib/netcdf/variable.rb', line 381

def to_string
  @netcdf_elmt.toString()
end

#to_string_debugObject




391
392
393
# File 'lib/netcdf/variable.rb', line 391

def to_string_debug
  @netcdf_elmt.toSringDebug()
end

#unlimited?Boolean


Can this variable's size grow?.


Returns:

  • (Boolean)

257
258
259
# File 'lib/netcdf/variable.rb', line 257

def unlimited?
  @netcdf_elmt.isUnlimited()
end

#unsigned?Boolean


Is this Variable unsigned?.


Returns:

  • (Boolean)

265
266
267
# File 'lib/netcdf/variable.rb', line 265

def unsigned?
  @netcdf_elmt.isUnsigned()
end

#variable_length?Boolean


Does this variable have a variable length dimension? If so, it has as one of its mensions Dimension.VLEN.


Returns:

  • (Boolean)

274
275
276
# File 'lib/netcdf/variable.rb', line 274

def variable_length?
  @netcdf_elmt.isVariableLength()
end