Class: NumRu::GrADSVar
- Inherits:
-
Object
- Object
- NumRu::GrADSVar
- Defined in:
- lib/numru/gphys/grads_gridded.rb
Instance Method Summary collapse
-
#[](*a) ⇒ Object
copied from NetCDFVar class.
-
#[]=(*a) ⇒ Object
copied from NetCDFVar class.
- #att(name) ⇒ Object
- #att_names ⇒ Object
-
#attr ⇒ Object
obsolete.
-
#dim_names ⇒ Object
def dim(name) def dims(names).
- #each_att ⇒ Object
- #file ⇒ Object
- #get(hash = nil) ⇒ Object (also: #simple_get)
- #get_att(name) ⇒ Object
-
#initialize(file, varname, varonly = false, dimonly = false) ⇒ GrADSVar
constructor
A new instance of GrADSVar.
- #name ⇒ Object
- #name= ⇒ Object
- #natts ⇒ Object
- #put(var, hash = nil) ⇒ Object (also: #simple_put)
- #put_att(name, value) ⇒ Object
- #scaled_get(hash = nil) ⇒ Object
- #scaled_put(var, hash = nil) ⇒ Object
-
#shape_current ⇒ Object
(also: #shape_ul0)
def shape_ul0 sh = [] @dimensions.each{|dim| if d.unlimited? then sh.push(0) else sh.push(dim) end } sh end.
- #typecode ⇒ Object
- #vartype ⇒ Object (also: #ntype)
Constructor Details
#initialize(file, varname, varonly = false, dimonly = false) ⇒ GrADSVar
Returns a new instance of GrADSVar.
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1253 def initialize(file,varname,varonly=false,dimonly=false) @varname = varname if file.is_a?(String) file = GrADS_Gridded.open(file) elsif !file.is_a?(GrADS_Gridded) raise ArgumentError, "1st arg must be a GrADS_Gridded or a file name" end @ctl = file # control file name @attr = NumRu::Attribute.new found = false if !dimonly @dimensions = @ctl.dimensions @rank = @dimensions.length @ctl.variables.each do |var| if @varname == var[:name] @idim = -1 @attr[:long_name] = var[:description] @attr[:nlev] = var[:nlev].to_s @shape = [@dimensions[0][:len],@dimensions[1][:len], @dimensions[2][:len],@dimensions[3][:len]] @shape[2] = var[:nlev].to_i if( var[:nlev] ) @shape[4] = @dimensions[4][:len] if @rank==5 @attr[:missing_value] = NArray.sfloat(1).fill!(@ctl.undef) found = true end end end if !found && !varonly @ctl.dimensions.each_with_index do |dim,i| if @varname == dim[:name] @idim = i @dimensions = [ dim ] @rank = 1 @attr[:name] = dim[:name] @attr[:long_name] = dim[:description] @attr[:units] = dim[:units] if @attr[:long_name] == "time" && @ctl.get_att("365_day_calendar") == true @attr[:calendar] = "365_day" end @shape = [dim[:len]] found = true end end end raise "variable #{@varname} is not found" if !found end |
Instance Method Details
#[](*a) ⇒ Object
copied from NetCDFVar class
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1523 def [](*a) # copied from NetCDFVar class a = __rubber_expansion(a) first = Array.new last = Array.new stride = Array.new set_stride = false a.each{|i| if(i.is_a?(Integer)) 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?(Integer) 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 Integer, 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?(Integer) } na.reshape!( *shape ) na end |
#[]=(*a) ⇒ Object
copied from NetCDFVar class
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1591 def []=(*a) # copied from NetCDFVar class 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?(Integer)) 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?(Integer)}.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?(Integer) 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 Integer, 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(name) ⇒ Object
1359 1360 1361 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1359 def att(name) raise "use get_att instead of att" end |
#att_names ⇒ Object
1371 1372 1373 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1371 def att_names @attr.keys end |
#attr ⇒ Object
obsolete
1383 1384 1385 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1383 def attr # obsolete @attr end |
#dim_names ⇒ Object
def dim(name)
def dims(names)
1314 1315 1316 1317 1318 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1314 def dim_names ary = Array.new() @dimensions.each{|dim| ary.push(dim[:name])} ary end |
#each_att ⇒ Object
1375 1376 1377 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1375 def each_att raise "each_att not supported" end |
#file ⇒ Object
1387 1388 1389 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1387 def file @ctl.ctlfile end |
#get(hash = nil) ⇒ Object Also known as: simple_get
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1426 def get(hash=nil) if @idim != -1 na = @ctl.get_dim(@dimensions[0]) if hash == nil elsif hash.key?("start")==true h_sta = hash["start"] endq = hash.key?("end") strq = hash.key?("stride") if endq == false && strq == false na = na[h_sta[0]..-1] elsif endq == true && strq == false h_end = hash["end"] na = na[h_sta[0]..h_end[0]] elsif endq == false && strq == true h_str = hash["stride"] raise "sorry, stride is not supported yet." else endq == true && strq == true h_end = hash["end"] h_str = hash["stride"] raise "sorry, stride is not supported yet." end end else h_sta = [0, 0, 0, 0, 0] h_end = [-1, -1, -1, -1, -1] if hash == nil # elsif hash.key?("index")==true else if hash.key?("start")==true h_sta = hash["start"] if hash.key?("end")==true h_end = hash["end"] end end if hash.key?("stride")==true raise "sorry, stride is not supported yet." end end idx_x = NArray.int(@shape[0]).indgen! idx_y = NArray.int(@shape[1]).indgen! idx_z = NArray.int(@shape[2]).indgen! idx_t = NArray.int(@shape[3]).indgen! str_x = idx_x[h_sta[0]]; end_x = idx_x[h_end[0]] str_y = idx_y[h_sta[1]]; end_y = idx_y[h_end[1]] str_z = idx_z[h_sta[2]]; end_z = idx_z[h_end[2]] str_t = idx_t[h_sta[3]]; end_t = idx_t[h_end[3]] if @rank == 5 # has ensemble dimension idx_e = NArray.int(@shape[4]).indgen! str_e = idx_e[h_sta[4]]; end_e = idx_e[h_end[4]] na = NArrayMiss.new(vartype,end_x-str_x+1,end_y-str_y+1,end_z-str_z+1,end_t-str_t+1,end_e-str_e+1) for e in str_e..end_e for t in str_t..end_t for z in str_z..end_z na_xy = @ctl.get(@varname,z,t,[str_x,end_x,str_y,end_y],e) mask = ( na_xy.ne @ctl.undef ) na[true,true,z-str_z,t-str_t,e-str_e] = NArrayMiss.to_nam(na_xy,mask) end end end else na = NArrayMiss.new(vartype,end_x-str_x+1,end_y-str_y+1,end_z-str_z+1,end_t-str_t+1) for t in str_t..end_t for z in str_z..end_z na_xy = @ctl.get(@varname,z,t,[str_x,end_x,str_y,end_y]) mask = ( na_xy.ne @ctl.undef ) na[true,true,z-str_z,t-str_t] = NArrayMiss.to_nam(na_xy,mask) end end end end na end |
#get_att(name) ⇒ Object
1363 1364 1365 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1363 def get_att(name) @attr[name] end |
#name ⇒ Object
1320 1321 1322 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1320 def name @varname end |
#name= ⇒ Object
1324 1325 1326 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1324 def name= raise "name= not supported" end |
#natts ⇒ Object
1367 1368 1369 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1367 def natts @attr.length end |
#put(var, hash = nil) ⇒ Object Also known as: simple_put
1422 1423 1424 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1422 def put(var,hash=nil) raise "put not supported" end |
#put_att(name, value) ⇒ Object
1379 1380 1381 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1379 def put_att(name,value) @attr[name] = value end |
#scaled_get(hash = nil) ⇒ Object
1418 1419 1420 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1418 def scaled_get(hash=nil) raise "scaled_get not supported" end |
#scaled_put(var, hash = nil) ⇒ Object
1414 1415 1416 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1414 def scaled_put(var,hash=nil) raise "scaled_put not supported" end |
#shape_current ⇒ Object Also known as: shape_ul0
def shape_ul0
sh = []
@dimensions.each{|dim|
if d.unlimited? then
sh.push(0)
else
sh.push(dim[:len])
end
}
sh
end
1403 1404 1405 1406 1407 1408 1409 1410 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1403 def shape_current # sh = [] # @dimensions.each{|dim| # sh.push(dim[:len]) # } # sh @shape end |
#typecode ⇒ Object
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1346 def typecode if( @idim == -1 ) for i in 0...@ctl.variables.length if @varname == @ctl.variables[i][:name] byte = @ctl.variables[i][:byte] end end else byte = 4 end byte end |
#vartype ⇒ Object Also known as: ntype
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 |
# File 'lib/numru/gphys/grads_gridded.rb', line 1332 def vartype if( @idim == -1 ) for i in 0...@ctl.variables.length if @varname == @ctl.variables[i][:name] type = @ctl.variables[i][:type] end end else type = "float" end type end |