| Class | NumRu::HE5PtField |
| In: |
lib/hdfeos5.rb
|
| Parent: | Object |
HE5PtField クラスに関して
| MissValAtts | = | ["MissingValue","_FillValue"] |
# File lib/hdfeos5.rb, line 1552
1552: def create(file, pname,fldname)
1553: if(file.is_a?(String))
1554: ptid = HE5Pt.create(file, pname)
1555: elsif(!file.is_a?(HE5))
1556: raise TypeError,
1557: "1st arg must be a HDF-EOS5 (file object) or a String (path)"
1558: end
1559: file.var(fldname)
1560: end
# File lib/hdfeos5.rb, line 1562
1562: def open(file, pname, fldname)
1563: if(file.is_a?(String))
1564: ptid = HE5Pt.open(file, pname)
1565: elsif(!file.is_a?(HE5))
1566: raise TypeError,
1567: "1st arg must be a HDF-EOS5 (file object) or a String (path)"
1568: end
1569: ptid.var(fldname)
1570: end
# File lib/hdfeos5.rb, line 1751
1751: def [](*a)
1752: if a.length == 0
1753: return self.get
1754: end
1755: a = __rubber_expansion(a)
1756: count = Array.new
1757: set_count = false
1758: a.each{|i|
1759: if(i.is_a?(Fixnum))
1760: count.push(i)
1761: elsif(i.is_a?(Range))
1762: count.push(i.first)
1763: elsif(i.is_a?(Hash))
1764: r = (i.to_a[0])[0]
1765: s = (i.to_a[0])[1]
1766: if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
1767: raise TypeError, "Hash argument must be {a_Range, step}"
1768: end
1769: count.push(r.first)
1770: set_count = true
1771: elsif(i.is_a?(TrueClass))
1772: count.push(0)
1773: elsif( i.is_a?(Array) || i.is_a?(NArray))
1774: a_new = a.dup
1775: at = a.index(i)
1776: i = NArray.to_na(i) if i.is_a?(Array)
1777: for n in 0..i.length-1
1778: a_new[at] = i[n]..i[n]
1779: na_tmp = self[*a_new]
1780: if n==0 then
1781: k = at
1782: if at > 0
1783: a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end}
1784: end
1785: shape_tmp = na_tmp.shape
1786: shape_tmp[k] = i.length
1787: na = na_tmp.class.new(na_tmp.typecode,*shape_tmp)
1788: index_tmp = Array.new(shape_tmp.length,true)
1789: end
1790: index_tmp[k] = n..n
1791: na[*index_tmp] = na_tmp
1792: end
1793: return na
1794: else
1795: raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray"
1796: end
1797: }
1798:
1799: if(set_count)
1800: na = self.get({"count"=>count})
1801: else
1802: na = self.get()
1803: end
1804: shape = na.shape
1805: (a.length-1).downto(0){ |i|
1806: shape.delete_at(i) if a[i].is_a?(Fixnum)
1807: }
1808: na.reshape!( *shape )
1809: na
1810: end
# File lib/hdfeos5.rb, line 1812
1812: def []=(*a)
1813: val = a.pop
1814: a = __rubber_expansion(a)
1815: count = Array.new
1816: set_count = false
1817: a.each{|i|
1818: if(i.is_a?(Fixnum))
1819: count.push(i)
1820: elsif(i.is_a?(Range))
1821: count.push(i.first)
1822: elsif(i.is_a?(Hash))
1823: r = (i.to_a[0])[0]
1824: s = (i.to_a[0])[1]
1825: if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
1826: raise ArgumentError, "Hash argument must be {first..last, step}"
1827: end
1828: count.push(r.first)
1829: set_stride = true
1830: elsif(i.is_a?(TrueClass))
1831: count.push(0)
1832: elsif(i.is_a?(Array) || i.is_a?(NArray))
1833: a_new = a.dup
1834: at = a.index(i)
1835: i = NArray.to_na(i) if i.is_a?(Array)
1836: val = NArray.to_na(val) if val.is_a?(Array)
1837: rank_of_subset = a.dup.delete_if{|v| v.is_a?(Fixnum)}.length
1838: if val.rank != rank_of_subset
1839: raise "rank of the rhs (#{val.rank}) is not equal to the rank "+
1840: "of the subset specified by #{a.inspect} (#{rank_of_subset})"
1841: end
1842: k = at
1843: a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end}
1844: if i.length != val.shape[k]
1845: raise "length of the #{k+1}-th dim of rhs is incorrect "+
1846: "(#{i.length} for #{val.shape[k]})"
1847: end
1848: index_tmp = Array.new(val.rank,true) if !val.is_a?(Numeric) #==>Array-like
1849: for n in 0..i.length-1
1850: a_new[at] = i[n]..i[n]
1851: if !val.is_a?(Numeric) then
1852: index_tmp[k] = n..n
1853: self[*a_new] = val[*index_tmp]
1854: else
1855: self[*a_new] = val
1856: end
1857: end
1858: return self
1859: else
1860: raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray"
1861: end
1862: }
1863:
1864: if(set_count)
1865: self.put(val, {"count"=>count})
1866: else
1867: self.put(val)
1868: end
1869: end
# File lib/hdfeos5.rb, line 1634
1634: def att_names
1635: nattrs, attrnames, strbufsize = inqlocattrs()
1636: return attrnames.split(/,/)
1637: end
# File lib/hdfeos5.rb, line 1602
1602: def dim(dimid)
1603: rank, dims, ntype, dimlist = fieldinfo()
1604: return dimlist.split(",")[dimid]
1605: end
# File lib/hdfeos5.rb, line 1612
1612: def dim_names
1613: rank, dims, ntype, dimlist = fieldinfo()
1614: return dimlist.split(",").reverse
1615: end
# File lib/hdfeos5.rb, line 1607
1607: def dim_val(dimid)
1608: rank, dims, ntype, dimlist = fieldinfo()
1609: return dimlist.split(",")[dimid]
1610: end
# File lib/hdfeos5.rb, line 1639
1639: def each_att
1640: attlist=Array.new
1641: attnames=att_names()
1642: attnum = natts()
1643: attnames.each{|attname|
1644: list=Array.new
1645: attrval=att(attname)
1646: list.push(attname, attrval)
1647: attlist.push(list)
1648: }
1649: attlist
1650: end
# File lib/hdfeos5.rb, line 1626
1626: def get_att(attname)
1627: if att_names.include?(attname)
1628: get_att_(attname)
1629: else
1630: nil
1631: end
1632: end
# File lib/hdfeos5.rb, line 1718
1718: def get_with_miss(*args)
1719: na = simple_get(*args)
1720: mv = nil
1721: MissValAtts.each do |nm|
1722: mv = get_att(nm)
1723: break if !mv.nil?
1724: end
1725: if mv.nil?
1726: na
1727: else
1728: NArrayMiss.to_nam_no_dup( na, (na.ne(mv[0])) )
1729: end
1730: end
# File lib/hdfeos5.rb, line 1871
1871: def inspect
1872: 'HE5PtField:'+point.file.path+'?var='+name
1873: end
# File lib/hdfeos5.rb, line 1621
1621: def natts
1622: nattrs, attrname, strbufsize = inqlocattrs()
1623: return nattrs
1624: end
# File lib/hdfeos5.rb, line 1596
1596: def ndims
1597: rank, dims, ntype, dimlist = fieldinfo()
1598: return rank
1599: end
# File lib/hdfeos5.rb, line 1573
1573: def ntype
1574: rank, dims, ntype, dimlist = fieldinfo()
1575: return ntype
1576: end
# File lib/hdfeos5.rb, line 1652
1652: def put_att(name,value,atttype=nil)
1653: count = Array.new
1654: count[0] = value.size
1655: writelocattr(name,atttype,count,value)
1656: end
# File lib/hdfeos5.rb, line 1579
1579: def shape
1580: rank, dims, ntype, dimlist = fieldinfo()
1581: return dims[-1..0].to_a # NArray --> reverced --> Array
1582: end
# File lib/hdfeos5.rb, line 1586
1586: def shape_ul0
1587: sh = shape_current
1588: dim_names.each_with_index do |dnm,i|
1589: if dnm == "Unlim"
1590: sh[i] = 0
1591: end
1592: end
1593: sh
1594: end
# File lib/hdfeos5.rb, line 1698
1698: def simple_get(hash=nil)
1699: if hash == nil
1700: if self.ntype == "char" || self.ntype=="byte"
1701: get_vars_char()
1702: elsif self.ntype=="sint"
1703: get_vars_short()
1704: elsif self.ntype=="int"
1705: get_vars_int()
1706: elsif self.ntype=="sfloat"
1707: get_vars_float()
1708: elsif self.ntype=="float"
1709: get_vars_double()
1710: else
1711: raise ArgumentError, "variable type isn't supported in HDF-EOS5"
1712: end
1713: else
1714: raise ArgumentError,"hash isn't needed"
1715: end
1716: end
# File lib/hdfeos5.rb, line 1662
1662: def simple_put(var,hash=nil)
1663: if hash == nil
1664: if self.ntype == "char" || self.ntype=="byte"
1665: put_vars_char([0,0,0,0,0,0,0,0], var)
1666: elsif self.ntype=="sint"
1667: put_vars_short([0,0,0,0,0,0,0,0], var)
1668: elsif self.ntype=="int"
1669: put_vars_int([0,0,0,0,0,0,0,0], var)
1670: elsif self.ntype=="sfloat"
1671: put_vars_float([0,0,0,0,0,0,0,0], var)
1672: elsif self.ntype=="float"
1673: put_vars_double([0,0,0,0,0,0,0,0], var)
1674: else
1675: raise ArgumentError, "variable type isn't supported in HDF-EOS5"
1676: end
1677: elsif hash == "count"
1678: count = hash["count"]
1679: if self.ntype == "char" || self.ntype=="byte"
1680: put_vars_char(count, var)
1681: elsif self.ntype=="sint"
1682: put_vars_short(count, var)
1683: elsif self.ntype=="int"
1684: put_vars_int(count, var)
1685: elsif self.ntype=="sfloat"
1686: put_vars_float(count, var)
1687: elsif self.ntype=="float"
1688: put_vars_double(count, var)
1689: else
1690: raise ArgumentError, "variable type isn't supported in HDF-EOS5"
1691: end
1692: else
1693: raise ArgumentError,"{'count'}=>{ARRAY} is needed"
1694: end
1695: end
# File lib/hdfeos5.rb, line 1733
1733: def __rubber_expansion( args )
1734: if (id = args.index(false)) # substitution into id
1735: # false is incuded
1736: alen = args.length
1737: if args.rindex(false) != id
1738: raise ArguemntError,"only one rubber dimension is permitted"
1739: elsif alen > rank+1
1740: raise ArgumentError, "too many args"
1741: end
1742: ar = ( id!=0 ? args[0..id-1] : [] )
1743: args = ar + [true]*(rank-alen+1) + args[id+1..-1]
1744: elsif args.length == 0 # to support empty [], []=
1745: args = [true]*rank
1746: end
1747: args
1748: end