Class: NumRu::NetCDFVar
- Inherits:
-
Object
- Object
- NumRu::NetCDFVar
- Defined in:
- lib/netcdf.rb,
lib/netcdf_miss.rb
Constant Summary collapse
- @@unpack_type =
nil
Class Method Summary collapse
- .new(file, varname, mode = "r", share = false) ⇒ Object (also: open)
- .unpack_type ⇒ Object
- .unpack_type=(na_type) ⇒ Object
Instance Method Summary collapse
- #[](*a) ⇒ Object
- #[]=(*a) ⇒ Object
- #att_names ⇒ Object
- #dim_names ⇒ Object
- #each_att ⇒ Object
- #get_with_miss(*args) ⇒ Object
- #get_with_miss_and_scaling(*args) ⇒ Object
- #inspect ⇒ Object
-
#pack(na) ⇒ Object
The put and get methods in the NetCDFVar class.
- #put_att(attname, val, atttype = nil) ⇒ Object
- #put_with_miss(data, *args) ⇒ Object
- #put_with_miss_and_scaling(data, *args) ⇒ Object
- #scaled_get(hash = nil) ⇒ Object
- #scaled_put(var, hash = nil) ⇒ Object
- #shape_current ⇒ Object
- #shape_ul0 ⇒ Object
- #simple_get(hash = nil) ⇒ Object (also: #get)
- #simple_put(var, hash = nil) ⇒ Object (also: #put)
- #unpack(na) ⇒ Object
Class Method Details
.new(file, varname, mode = "r", share = false) ⇒ Object Also known as: open
268 269 270 271 272 273 274 275 |
# File 'lib/netcdf.rb', line 268 def new(file,varname,mode="r",share=false) if(file.is_a?(String)) file = NetCDF.open(file,mode,share) elsif(!file.is_a?(NetCDF)) raise TypeError, "1st arg must be a NetCDF (file object) or a String (path)" end file.var(varname) end |
.unpack_type ⇒ Object
374 375 376 |
# File 'lib/netcdf.rb', line 374 def unpack_type @@unpack_type end |
.unpack_type=(na_type) ⇒ Object
377 378 379 380 381 382 383 384 |
# File 'lib/netcdf.rb', line 377 def unpack_type=(na_type) if [NArray::BYTE, NArray::SINT, NArray::INT, NArray::SFLOAT, NArray::FLOAT, nil].include?(na_type) @@unpack_type = na_type else raise ArgumentError, "Arg must be one of NArray::BYTE, NArray::SINT, NArray::INT, NArray::SFLOAT, NArray::FLOAT" end end |
Instance Method Details
#[](*a) ⇒ Object
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/netcdf.rb', line 633 def [](*a) if a.length == 0 return self.get end a = __rubber_expansion(a) first = Array.new last = Array.new stride = Array.new set_stride = false a.each{|i| if(i.is_a?(Fixnum)) first.push(i) last.push(i) stride.push(1) elsif(i.is_a?(Range)) first.push(i.first) last.push(i.exclude_end? ? i.last-1 : i.last) stride.push(1) elsif(i.is_a?(Hash)) r = (i.to_a[0])[0] s = (i.to_a[0])[1] if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) ) raise TypeError, "Hash argument must be {a_Range, step}" end first.push(r.first) last.push(r.exclude_end? ? r.last-1 : r.last) stride.push(s) set_stride = true elsif(i.is_a?(TrueClass)) first.push(0) last.push(-1) stride.push(1) elsif( i.is_a?(Array) || i.is_a?(NArray)) a_new = a.dup at = a.index(i) i = NArray.to_na(i) if i.is_a?(Array) for n in 0..i.length-1 a_new[at] = i[n]..i[n] na_tmp = self[*a_new] if n==0 then k = at if at > 0 a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end} end shape_tmp = na_tmp.shape shape_tmp[k] = i.length na = na_tmp.class.new(na_tmp.typecode,*shape_tmp) index_tmp = Array.new(shape_tmp.length,true) end index_tmp[k] = n..n na[*index_tmp] = na_tmp end return na else raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray" end } if(set_stride) na = self.get({"start"=>first, "end"=>last, "stride"=>stride}) else na = self.get({"start"=>first, "end"=>last}) end shape = na.shape (a.length-1).downto(0){ |i| shape.delete_at(i) if a[i].is_a?(Fixnum) } na.reshape!( *shape ) na end |
#[]=(*a) ⇒ Object
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
# File 'lib/netcdf.rb', line 704 def []=(*a) val = a.pop a = __rubber_expansion(a) first = Array.new last = Array.new stride = Array.new set_stride = false a.each{|i| if(i.is_a?(Fixnum)) first.push(i) last.push(i) stride.push(1) elsif(i.is_a?(Range)) first.push(i.first) last.push(i.exclude_end? ? i.last-1 : i.last) stride.push(1) elsif(i.is_a?(Hash)) r = (i.to_a[0])[0] s = (i.to_a[0])[1] if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) ) raise ArgumentError, "Hash argument must be {first..last, step}" end first.push(r.first) last.push(r.exclude_end? ? r.last-1 : r.last) stride.push(s) set_stride = true elsif(i.is_a?(TrueClass)) first.push(0) last.push(-1) stride.push(1) elsif(i.is_a?(Array) || i.is_a?(NArray)) a_new = a.dup at = a.index(i) i = NArray.to_na(i) if i.is_a?(Array) val = NArray.to_na(val) if val.is_a?(Array) rank_of_subset = a.dup.delete_if{|v| v.is_a?(Fixnum)}.length if val.rank != rank_of_subset raise "rank of the rhs (#{val.rank}) is not equal to the rank "+ "of the subset specified by #{a.inspect} (#{rank_of_subset})" end k = at a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end} if i.length != val.shape[k] raise "length of the #{k+1}-th dim of rhs is incorrect "+ "(#{i.length} for #{val.shape[k]})" end index_tmp = Array.new(val.rank,true) if !val.is_a?(Numeric) #==>Array-like for n in 0..i.length-1 a_new[at] = i[n]..i[n] if !val.is_a?(Numeric) then index_tmp[k] = n..n self[*a_new] = val[*index_tmp] else self[*a_new] = val end end return self else raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray" end } if(set_stride) self.put(val, {"start"=>first, "end"=>last, "stride"=>stride}) else self.put(val, {"start"=>first, "end"=>last}) end end |
#att_names ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/netcdf.rb', line 296 def att_names num_att=natts() names=[] for attnum in 0..num_att-1 obj_Att=id2att(attnum) names=names+[obj_Att.name] end return names end |
#dim_names ⇒ Object
290 291 292 293 294 |
# File 'lib/netcdf.rb', line 290 def dim_names ary = Array.new() dims.each{|dim| ary.push(dim.name)} ary end |
#each_att ⇒ Object
282 283 284 285 286 287 288 |
# File 'lib/netcdf.rb', line 282 def each_att num_att=natts() for attnum in 0..num_att-1 obj_Att=id2att(attnum) yield(obj_Att) end end |
#get_with_miss(*args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/netcdf_miss.rb', line 8 def get_with_miss(*args) __interpret_missing_params if !defined?(@missval) data = simple_get(*args) if @vmin || @vmax if @vmin mask = (data >= @vmin) mask = mask.and(data <= @vmax) if @vmax else mask = (data <= @vmax) end data = NArrayMiss.to_nam(data, mask) elsif @missval # only missing_value is present. mask = (data.ne(@missval)) data = NArrayMiss.to_nam(data, mask) end data end |
#get_with_miss_and_scaling(*args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/netcdf_miss.rb', line 26 def get_with_miss_and_scaling(*args) __interpret_missing_params if !defined?(@missval) data = simple_get(*args) if @vmin || @vmax if @vmin mask = (data >= @vmin) mask = mask.and(data <= @vmax) if @vmax else mask = (data <= @vmax) end data = NArrayMiss.to_nam(data, mask) elsif @missval # only missing_value is present. mask = (data.ne(@missval)) data = NArrayMiss.to_nam(data, mask) end data = unpack( data ) data end |
#inspect ⇒ Object
773 774 775 |
# File 'lib/netcdf.rb', line 773 def inspect 'NetCDFVar:'+file.path+'?var='+name end |
#pack(na) ⇒ Object
The put and get methods in the NetCDFVar class
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/netcdf.rb', line 332 def pack(na) sf = att('scale_factor') ao = att('add_offset') if ( sf == nil && ao == nil ) then na else na = NArray.to_na(na) if na.is_a?(Array) if sf csf = sf.get raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String) raise NetcdfError, "scale_factor is not unique" if csf.length != 1 raise NetcdfError, "zero scale_factor" if csf[0] == 0 else csf = nil end if ao cao = ao.get raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String) raise NetcdfError, "add_offset is not unique" if cao.length != 1 else cao = nil end if csf and cao packed = (na - cao) / csf elsif csf packed = na / csf elsif cao packed = na - cao end if self.typecode <= NArray::LINT packed = packed.round end packed end end |
#put_att(attname, val, atttype = nil) ⇒ Object
306 307 308 |
# File 'lib/netcdf.rb', line 306 def put_att(attname,val,atttype=nil) put_attraw(attname,val,atttype) end |
#put_with_miss(data, *args) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/netcdf_miss.rb', line 45 def put_with_miss(data, *args) if data.is_a?( NArrayMiss ) __interpret_missing_params if !defined?(@missval) if @missval simple_put(data.to_na(@missval), *args) else simple_put(data.to_na, *args) end else simple_put(data, *args) end end |
#put_with_miss_and_scaling(data, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/netcdf_miss.rb', line 58 def put_with_miss_and_scaling(data, *args) if data.is_a?( NArrayMiss ) __interpret_missing_params if !defined?(@missval) if @missval data = pack( data ) data = data.to_na(@missval) else data = pack( data ) data = data.to_na end simple_put(data, *args) else scaled_put(data, *args) end end |
#scaled_get(hash = nil) ⇒ Object
421 422 423 |
# File 'lib/netcdf.rb', line 421 def scaled_get(hash=nil) unpack( simple_get(hash) ) end |
#scaled_put(var, hash = nil) ⇒ Object
368 369 370 |
# File 'lib/netcdf.rb', line 368 def scaled_put(var,hash=nil) simple_put( pack(var), hash) end |
#shape_current ⇒ Object
322 323 324 325 326 327 328 |
# File 'lib/netcdf.rb', line 322 def shape_current sh = [] dims.each{|d| sh.push(d.length) } sh end |
#shape_ul0 ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/netcdf.rb', line 310 def shape_ul0 sh = [] dims.each{|d| if d.unlimited? then sh.push(0) else sh.push(d.length) end } sh end |
#simple_get(hash = nil) ⇒ Object Also known as: get
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/netcdf.rb', line 531 def simple_get(hash=nil) t_var = self.vartype if hash == nil if t_var == "char" get_var_char elsif t_var == "byte" get_var_byte elsif t_var == "int16" get_var_sint elsif t_var == "int32" get_var_int elsif t_var == "float32" get_var_sfloat elsif t_var == "float64" get_var_float else raise NetcdfError, "variable type #{t_var} isn't supported in netCDF" end elsif hash.key?("index")==true ind = hash["index"] if t_var == "char" get_var1_char(ind) elsif t_var == "byte" get_var1_byte(ind) elsif t_var == "int16" get_var1_sint(ind) elsif t_var == "int32" get_var1_int(ind) elsif t_var == "float32" get_var1_sfloat(ind) elsif t_var == "float64" get_var1_float(ind) else raise NetcdfError,"variable type #{t_var} isn't supported in netCDF" end elsif hash.key?("start")==true h_sta = hash["start"] h_end = hash["end"] # can be nill h_str = hash["stride"] # can be nill if false && NetCDF.nc4? && h_str && ((xstr=h_str[-1]) != 1) # Tentative treatment for the very slow netcdf-4 reading with step. # Reading with step is generally slow with NetCDF 4, but it is # particularly so for the last (fastest) dimension. # Ref: http://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2013/msg00311.html h_str[-1] = 1 nc4remedy = true else nc4remedy = false end if t_var == "char" v = get_vars_char(h_sta,h_end,h_str) elsif t_var == "byte" v = get_vars_byte(h_sta,h_end,h_str) elsif t_var == "int16" v = get_vars_sint(h_sta,h_end,h_str) elsif t_var == "int32" v = get_vars_int(h_sta,h_end,h_str) elsif t_var == "float32" v = get_vars_sfloat(h_sta,h_end,h_str) elsif t_var == "float64" v = get_vars_float(h_sta,h_end,h_str) else raise NetcdfError, "variable type #{t_var} isn't supported in netCDF" end if nc4remedy idx = [] (0...v.shape[-1]).step(xstr){|k| idx.push(k)} newshape = v.shape newshape[-1] = idx.size newv = NMatrix.new(newshape, dtype: v.dtype) idx.each_with_index do |iold, inew| newv[*[:*]*(v.dim-1), inew] = v[*[:*]*(v.dim-1), iold] # equiv to [false,iold] for narray end v = newv #v = v.slice(idx,*[:*]*(v.dim-1)) # equiv to [idx, false] for narray end v else raise ArgumentError,"{'start'}=>{ARRAY} or {'index'}=>{ARRAY} is needed" end end |
#simple_put(var, hash = nil) ⇒ Object Also known as: put
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/netcdf.rb', line 425 def simple_put(var,hash=nil) if hash==nil if self.vartype == "char" put_var_char(var) elsif self.vartype == "byte" put_var_byte(var) elsif self.vartype == "int16" put_var_sint(var) elsif self.vartype == "int32" put_var_int(var) elsif self.vartype == "float32" put_var_sfloat(var) elsif self.vartype == "float64" put_var_float(var) else raise NetcdfError,"variable type isn't supported in netCDF" end elsif hash.key?("index")==true if self.vartype == "char" put_var1_char(var,hash["index"]) elsif self.vartype=="byte" put_var1_byte(var,hash["index"]) elsif self.vartype=="int16" put_var1_sint(var,hash["index"]) elsif self.vartype == "int32" put_var1_int(var,hash["index"]) elsif self.vartype == "float32" put_var1_sfloat(var,hash["index"]) elsif self.vartype == "float64" put_var1_float(var,hash["index"]) else raise NetcdfError,"variable type isn't supported in netCDF" end elsif hash.key?("start")==true if hash.key?("end")==false && hash.key?("stride")==false if self.vartype == "char" put_vars_char(var,hash["start"],nil,nil) elsif self.vartype=="byte" put_vars_byte(var,hash["start"],nil,nil) elsif self.vartype=="int16" put_vars_sint(var,hash["start"],nil,nil) elsif self.vartype=="int32" put_vars_int(var,hash["start"],nil,nil) elsif self.vartype=="float32" put_vars_sfloat(var,hash["start"],nil,nil) elsif self.vartype=="float64" put_vars_float(var,hash["start"],nil,nil) else raise NetcdfError, "variable type isn't supported in netCDF" end elsif hash.key?("end")==true && hash.key?("stride") == false if self.vartype == "char" put_vars_char(var,hash["start"],hash["end"],nil) elsif self.vartype=="byte" put_vars_byte(var,hash["start"],hash["end"],nil) elsif self.vartype=="int16" put_vars_sint(var,hash["start"],hash["end"],nil) elsif self.vartype=="int32" put_vars_int(var,hash["start"],hash["end"],nil) elsif self.vartype == "float32" put_vars_sfloat(var,hash["start"],hash["end"],nil) elsif self.vartype =="float64" put_vars_float(var,hash["start"],hash["end"],nil) else raise NetcdfError, "variable type isn't supported in netCDF" end elsif hash.key?("end")==false && hash.key?("stride")==true if self.vartype == "char" put_vars_char(var,hash["start"],nil,hash["stride"]) elsif self.vartype=="byte" put_vars_byte(var,hash["start"],nil,hash["stride"]) elsif self.vartype=="int16" put_vars_sint(var,hash["start"],nil,hash["stride"]) elsif self.vartype=="int32" put_vars_int(var,hash["start"],nil,hash["stride"]) elsif self.vartype=="float32" put_vars_sfloat(var,hash["start"],nil,hash["stride"]) elsif self.vartype=="float64" put_vars_float(var,hash["start"],nil,hash["stride"]) else raise NetcdfError, "variable type isn't supported in netCDF" end else hash.key?("end")==true && hash.key?("stride")==true if self.vartype == "char" put_vars_char(var,hash["start"],hash["end"],hash["stride"]) elsif self.vartype=="byte" put_vars_byte(var,hash["start"],hash["end"],hash["stride"]) elsif self.vartype=="int16" put_vars_sint(var,hash["start"],hash["end"],hash["stride"]) elsif self.vartype=="int32" put_vars_int(var,hash["start"],hash["end"],hash["stride"]) elsif self.vartype=="float32" put_vars_sfloat(var,hash["start"],hash["end"],hash["stride"]) elsif self.vartype=="float64" put_vars_float(var,hash["start"],hash["end"],hash["stride"]) else raise NetcdfError, "variable type isn't supported in netCDF" end end else raise ArgumentError,"{'start'}=>[ARRAY] or {'index'}=>[ARRAY] is needed" end end |
#unpack(na) ⇒ Object
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 |
# File 'lib/netcdf.rb', line 388 def unpack(na) sf = att('scale_factor') ao = att('add_offset') if ( sf == nil && ao == nil ) then na else if sf csf = sf.get raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String) raise NetcdfError, "scale_factor is not unique" if csf.length != 1 raise NetcdfError, "zero scale_factor" if csf[0] == 0 else csf =nil end if ao cao = ao.get raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String) raise NetcdfError, "add_offset is not unique" if cao.length != 1 else cao = nil end if csf and cao una = na * csf + cao # csf & cao are NArray -> coerced to their types elsif csf una = na * csf elsif cao una = na + cao end una = una.to_type(@@unpack_type) if @@unpack_type una end end |