Class: NumRu::Grid
- Inherits:
-
Object
- Object
- NumRu::Grid
- Defined in:
- lib/numru/gphys/grid.rb
Instance Attribute Summary collapse
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
Instance Method Summary collapse
- #[](*slicer) ⇒ Object
- #add_lost_axes(lost) ⇒ Object
- #assoc_coord_gphys(name) ⇒ Object
- #assoc_coords ⇒ Object
-
#assoc_coords=(assoc_coords) ⇒ Object
-
assoc_coords : an AssocCoords.
-
- #assoccoordnames ⇒ Object
-
#axis(dim_or_dimname) ⇒ Object
(also: #get_axis)
def axis(i) @axes end.
- #axnames ⇒ Object
-
#change_axis(dim, axis) ⇒ Object
def exclude(dim_or_dimname) dim = dim_index(dim_or_dimname) axes = @axes.dup axes.delete_at(dim) self.class.new( *axes ) end.
- #coord(i) ⇒ Object
- #coord_dim_indices(coordname) ⇒ Object
- #coordnames ⇒ Object
- #copy ⇒ Object
- #cut(*args) ⇒ Object
- #cut_assoccoord(hasharg) ⇒ Object
- #cut_rank_conserving(*args) ⇒ Object
- #delete_axes(at, deleted_by = nil) ⇒ Object
- #dim_index(dimname) ⇒ Object
- #has_assoccoord?(*arg) ⇒ Boolean
- #has_axis?(name) ⇒ Boolean
- #has_coord?(name) ⇒ Boolean
-
#initialize(*axes) ⇒ Grid
constructor
A new instance of Grid.
-
#insert_axis(dim, axis) ⇒ Object
def insert_axis!(dim_or_dimname, axis) dim = dim_index(dim_or_dimname) if axis == nil # do nothing else @axes = axis # @axes.insert(dim, axis) if ruby 1.7 @rank = @axes.length __check_and_set_axnames end self end.
- #inspect ⇒ Object
- #lost_axes ⇒ Object
- #merge(other) ⇒ Object
-
#set_assoc_coords(assoc_crds) ⇒ Object
-
assoc_crds : an Array of GPhys.
-
- #set_axis(dim_or_dimname, ax) ⇒ Object
- #set_lost_axes(lost) ⇒ Object
- #shape ⇒ Object (also: #shape_current)
- #transpose(*dims) ⇒ Object
Constructor Details
#initialize(*axes) ⇒ Grid
Returns a new instance of Grid.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/numru/gphys/grid.rb', line 225 def initialize( *axes ) @axes = Array.new axes.each{|ag| ### commented out for the duck typing: #if ag.is_a?(Axis) @axes.push(ag) #else # raise ArgumentError, "each argument must be an Axis" #end } @lost_axes = Array.new # Array of String @rank = @axes.length @axnames = Array.new __check_and_set_axnames @assoc_coords = nil end |
Instance Attribute Details
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
280 281 282 |
# File 'lib/numru/gphys/grid.rb', line 280 def rank @rank end |
Instance Method Details
#[](*slicer) ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/numru/gphys/grid.rb', line 470 def [] (*slicer) if slicer.length == 0 # make a clone axes = Array.new (0...rank).each{ |i| axes.push( @axes[i][0..-1] ) } grid = self.class.new( *axes ) else slicer = __rubber_expansion(slicer) if slicer.length != rank raise ArgumentError,"# of the args does not agree with the rank" end axes = Array.new lost = self.lost_axes #Array.new for i in 0...rank ax = @axes[i][slicer[i]] if ax.is_a?(String) lost.push( ax ) else axes.push( ax ) end end grid = self.class.new( *axes ) grid.set_lost_axes( lost ) if lost.length != 0 grid end if @assoc_coords grid.assoc_coords = @assoc_coords[*slicer] end grid end |
#add_lost_axes(lost) ⇒ Object
368 369 370 371 |
# File 'lib/numru/gphys/grid.rb', line 368 def add_lost_axes( lost ) @lost_axes = @lost_axes + lost # Array of String self end |
#assoc_coord_gphys(name) ⇒ Object
338 339 340 |
# File 'lib/numru/gphys/grid.rb', line 338 def assoc_coord_gphys(name) @assoc_coords && @assoc_coords.coord_gphys(name) end |
#assoc_coords ⇒ Object
260 261 262 |
# File 'lib/numru/gphys/grid.rb', line 260 def assoc_coords @assoc_coords end |
#assoc_coords=(assoc_coords) ⇒ Object
-
assoc_coords : an AssocCoords
256 257 258 |
# File 'lib/numru/gphys/grid.rb', line 256 def assoc_coords=(assoc_coords) @assoc_coords = assoc_coords end |
#assoccoordnames ⇒ Object
316 317 318 |
# File 'lib/numru/gphys/grid.rb', line 316 def assoccoordnames @assoc_coords && @assoc_coords.coordnames end |
#axis(dim_or_dimname) ⇒ Object Also known as: get_axis
def axis(i)
@axes[i]
end
324 325 326 |
# File 'lib/numru/gphys/grid.rb', line 324 def axis(dim_or_dimname) ax_dim(dim_or_dimname)[0] end |
#axnames ⇒ Object
282 283 284 |
# File 'lib/numru/gphys/grid.rb', line 282 def axnames @axnames.dup end |
#change_axis(dim, axis) ⇒ Object
def exclude(dim_or_dimname)
dim = dim_index(dim_or_dimname)
axes = @axes.dup
axes.delete_at(dim)
self.class.new( *axes )
end
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/numru/gphys/grid.rb', line 575 def change_axis(dim, axis) axes = @axes.dup lost = @lost_axes.dup if axis.is_a?(Axis) axes[dim] = axis else lost.push(axis) if axis.is_a?(String) axes.delete_at(dim) end grid = self.class.new( *axes ).add_lost_axes( lost ) if @assoc_coords grid.assoc_coords=@assoc_coords.subset_having_axnames(grid.axnames) end grid end |
#coord(i) ⇒ Object
330 331 332 333 334 335 336 |
# File 'lib/numru/gphys/grid.rb', line 330 def coord(i) if @assoc_coords && i.is_a?(String) && @assoc_coords.has_coord?(i) @assoc_coords.coord(i) else axis(i).pos end end |
#coord_dim_indices(coordname) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/numru/gphys/grid.rb', line 346 def coord_dim_indices(coordname) dim = @axnames.index(coordname) if dim [dim] elsif @assoc_coords && @assoc_coords.has_coord?(coordname) axnms = @assoc_coords.coord_gphys(coordname).axnames axnms.collect{|nm| @axnames.index(nm)} else nil end end |
#coordnames ⇒ Object
289 290 291 292 293 |
# File 'lib/numru/gphys/grid.rb', line 289 def coordnames ret = axnames ret.concat(@assoc_coords.coordnames) if @assoc_coords ret end |
#copy ⇒ Object
433 434 435 436 437 438 439 |
# File 'lib/numru/gphys/grid.rb', line 433 def copy # deep clone onto memory out = self.class.new( *@axes.collect{|ax| ax.copy} ) out.set_lost_axes( @lost_axes.dup ) out.assoc_coords = @assoc_coords.copy if @assoc_coords out end |
#cut(*args) ⇒ Object
523 524 525 |
# File 'lib/numru/gphys/grid.rb', line 523 def cut(*args) _cut_(false, *args) end |
#cut_assoccoord(hasharg) ⇒ Object
517 518 519 520 521 |
# File 'lib/numru/gphys/grid.rb', line 517 def cut_assoccoord(hasharg) ac, acsl = @assoc_coords.cut(hasharg) sl = @axnames.collect{|nm| acsl[nm] || true} [ self[*sl], sl ] end |
#cut_rank_conserving(*args) ⇒ Object
526 527 528 |
# File 'lib/numru/gphys/grid.rb', line 526 def cut_rank_conserving(*args) _cut_(true, *args) end |
#delete_axes(at, deleted_by = nil) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/numru/gphys/grid.rb', line 373 def delete_axes( at, deleted_by=nil ) case at when String at = [dim_index(at)] when Numeric at = [at] when Array at = at.collect{|x| case x when String dim_index(x) when Integer x else raise ArgumentError,"'at' must consist of Integer and/or String" end } else raise TypeError, "1st arg not an Array" end at.collect!{|pos| if pos < 0 pos + a.length else pos end } at.sort! newaxes = @axes.dup at.reverse.each{|pos| del = newaxes.delete_at(pos) if !del raise ArgumentError, "dimension #{pos} does not exist in a #{rank}D Grid" end } newgrid = self.class.new( *newaxes ) newgrid.set_lost_axes( @lost_axes.dup ) if @assoc_coords newgrid.assoc_coords=@assoc_coords.subset_having_axnames(newgrid.axnames) end if !deleted_by msg = '(deleted) ' else raise TypeError, "2nd arg not a String" if !deleted_by.is_a?(String) msg = '('+deleted_by+') ' end lost = at.collect{|pos| mn = sprintf("%g", ( UNumeric===(a=@axes[pos].pos.min) ? a.val : a) ) mx = sprintf("%g", ( UNumeric===(a=@axes[pos].pos.max) ? a.val : a) ) msg + "#{@axes[pos].name}:#{mn}..#{mx}" } newgrid.add_lost_axes( lost ) newgrid end |
#dim_index(dimname) ⇒ Object
342 343 344 |
# File 'lib/numru/gphys/grid.rb', line 342 def dim_index(dimname) ax_dim(dimname)[1] end |
#has_assoccoord?(*arg) ⇒ Boolean
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/numru/gphys/grid.rb', line 299 def has_assoccoord?(*arg) if arg.length == 0 !@assoc_coords.nil? # if a grid has assoc coords else name = arg[0] if @assoc_coords @assoc_coords.has_coord?(name) else false end end end |
#has_axis?(name) ⇒ Boolean
295 296 297 |
# File 'lib/numru/gphys/grid.rb', line 295 def has_axis?(name) @axnames.include?(name) end |
#has_coord?(name) ⇒ Boolean
312 313 314 |
# File 'lib/numru/gphys/grid.rb', line 312 def has_coord?(name) has_axis?(name) || has_assoccoord?(name) end |
#insert_axis(dim, axis) ⇒ Object
def insert_axis!(dim_or_dimname, axis)
dim = dim_index(dim_or_dimname)
if axis == nil
# do nothing
else
@axes[dim+1,0] = axis # @axes.insert(dim, axis) if ruby 1.7
@rank = @axes.length
__check_and_set_axnames
end
self
end
615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/numru/gphys/grid.rb', line 615 def insert_axis(dim, axis) axes = @axes.dup ### commented out for the duck typing: #if axis.is_a?(Axis) axes.insert(dim, axis) #else # raise ArgumentError, "2nd arg must be an Axis" #end grid = self.class.new( *axes ) grid.set_lost_axes( lost_axes ) grid.assoc_coords = @assoc_coords.dup if @assoc_coords grid end |
#inspect ⇒ Object
264 265 266 |
# File 'lib/numru/gphys/grid.rb', line 264 def inspect "<#{rank}D grid #{@axes.collect{|ax| ax.inspect}.join("\n\t")}#{@assoc_coords ? "\n"+@assoc_coords.inspect.gsub(/^/,"\t") : ''}>" end |
#lost_axes ⇒ Object
285 286 287 |
# File 'lib/numru/gphys/grid.rb', line 285 def lost_axes @lost_axes.dup end |
#merge(other) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/numru/gphys/grid.rb', line 441 def merge(other) # merge two grids by basically using copies of self's axes but # using the other's if the length in self is 1 and # the length in the other is longer if self.rank != other.rank raise "ranks do not agree (self:#{self.rank} vs other:#{other.rank})" end axes = Array.new for i in 0...self.rank if @axes[i].length == 1 and other.axis(i).length > 1 axes[i] = other.axis(i) else axes[i] = @axes[i] end end out = self.class.new( *axes ) out.set_lost_axes( (@lost_axes.dup + other.lost_axes).uniq ) if (oac = other.assoc_coords) || (sac = self.assoc_coords) sac ? ac=sac.merge(oac) : ac=oac.merge(sac) out.assoc_coords = ac end out end |
#set_assoc_coords(assoc_crds) ⇒ Object
-
assoc_crds : an Array of GPhys
244 245 246 247 248 249 250 251 252 |
# File 'lib/numru/gphys/grid.rb', line 244 def set_assoc_coords(assoc_crds) @assoc_coords = AssocCoords.new(assoc_crds, @axnames) @assoc_coords.axlens.each do |nm,l| if l && l != (l0 = shape[dim_index(nm)]) raise ArgumentError, "Length mismatch in coord #{nm} (#{l0}) and assoc_crds (#{l}). You may need to regrid (use GPhys#regrid method) associated coordinate(s) in advance." end end self end |
#set_axis(dim_or_dimname, ax) ⇒ Object
358 359 360 361 362 |
# File 'lib/numru/gphys/grid.rb', line 358 def set_axis(dim_or_dimname,ax) @axes[ i = dim_index(dim_or_dimname) ] = ax @axnames[ i ] = ax.name self end |
#set_lost_axes(lost) ⇒ Object
364 365 366 367 |
# File 'lib/numru/gphys/grid.rb', line 364 def set_lost_axes( lost ) @lost_axes = lost # Array of String self end |
#shape ⇒ Object Also known as: shape_current
465 466 467 |
# File 'lib/numru/gphys/grid.rb', line 465 def shape @axes.collect{|ax| ax.length} end |
#transpose(*dims) ⇒ Object
629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/numru/gphys/grid.rb', line 629 def transpose( *dims ) if dims.sort != NArray.int(rank).indgen!.to_a raise ArgumentError, "Args must a permutation of 0..rank-1 (eg, if 3D 2,1,0; 1,0,2;etc)" end axes = Array.new for i in 0...rank axes[i] = @axes[dims[i]] end grid = self.class.new(*axes) grid.set_lost_axes( lost_axes ) grid.assoc_coords = @assoc_coords.dup if @assoc_coords grid end |