Class: NumRu::GPhys::GribVar
- Inherits:
-
Object
- Object
- NumRu::GPhys::GribVar
- Extended by:
- GribUtils
- Includes:
- GribUtils
- Defined in:
- lib/numru/gphys/grib.rb
Overview
NumRu::GPhys::GribVar
Class Methods
—always_time_seq=(t_or_f) If true is given, a GribVar will always have a time dimension, even when the time is unique (i.e., length of the time coordinate is 1).
—new( file, name, obj, dims )
Methods
—file —name —rank —total —set_var —set_miss —dim_names —dim( index ) —ndims —def_dim(name,index) —put_att( key, value ) —set_att( key, value ) —att( key ) —att_names —shape —typecode —get( indics ) —[]( indics ) —inspect
Constant Summary collapse
- @@always_time_seq =
false
Class Method Summary collapse
Instance Method Summary collapse
- #att(key) ⇒ Object
- #att_names ⇒ Object
- #def_dim(name, index) ⇒ Object
- #dim(index) ⇒ Object
- #dim_names ⇒ Object
- #file ⇒ Object
- #get(*indices) ⇒ Object (also: #[], #val)
-
#initialize(file, name) ⇒ GribVar
constructor
end definition of class methods GribVar.
- #inspect ⇒ Object
- #name ⇒ Object
- #ndims ⇒ Object
- #put(val) ⇒ Object
- #put_att(key, val) ⇒ Object (also: #set_att)
- #rank ⇒ Object
- #set_miss(miss) ⇒ Object
- #set_sgms(sgms) ⇒ Object
- #shape ⇒ Object
- #total ⇒ Object
- #typecode ⇒ Object
Constructor Details
#initialize(file, name) ⇒ GribVar
end definition of class methods GribVar
1867 1868 1869 1870 1871 1872 1873 |
# File 'lib/numru/gphys/grib.rb', line 1867 def initialize(file,name) @file = file @name = name @attr = Hash.new @miss = false @dims = Array.new end |
Class Method Details
.always_time_seq ⇒ Object
1698 1699 1700 |
# File 'lib/numru/gphys/grib.rb', line 1698 def always_time_seq @@always_time_seq end |
.always_time_seq=(t_or_f) ⇒ Object
1695 1696 1697 |
# File 'lib/numru/gphys/grib.rb', line 1695 def always_time_seq=(t_or_f) @@always_time_seq = t_or_f end |
.parse(file, sgms, name) ⇒ Object
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 |
# File 'lib/numru/gphys/grib.rb', line 1747 def parse(file,sgms,name) miss = false sgms.each{|sgm| pds = sgm.pds if !vars_same?(sgms[0],sgm) raise "coordinate is not same" end miss = miss | pds.bms? } pds = sgms[0].pds gds = sgms[0].gds va = GribVar.new(file,name) va.set_sgms(sgms) va.put_att("long_name",pds.name) std_name = pds.standard_name va.put_att("standard_name",std_name) if std_name va.put_att("units",pds.unit) if pds.unit va.set_miss(miss) vdim = Array.new x,y = gds.grid x_sname = x["short_name"] d = va.def_dim(x_sname,x["ij"]) d.put(x["value"]) x.each{|k,v| if k!="value"&&k!="ij"&&k!="length" d.put_att(k,v) end } y_sname = y["short_name"] d = va.def_dim(y_sname,y["ij"]) d.put(y["value"]) y.each{|k,v| if k!="value"&&k!="ij"&&k!="length" d.put_att(k,v) end } if sgms.length>=1 nz = pds.z_value.length if nz!=0 z = Array.new(nz).collect{Array.new} n=nil m=nil sgms.each{|sgm| zv = sgm.pds.z_value nz.times{|m| if zv[m] z[m].push( zv[m]["value"] ) else z[m].push(m) end } } nz.times{|m| z[m].uniq! z[m].length==z[0].length || raise("length is not same") } if z[0].length>1 d = va.def_dim(pds.z_sname,2) d.put_att("long_name",pds.z_type) if pds.z_type=="Hybrid level" list_pv = gds.list_pv zval = pds.z_value[0] tmp = Array.new z[0].each{|n| tmp += [n-1,n] } tmp.uniq! tmp.sort! ap = list_pv[0...list_pv.length/2] b = list_pv[list_pv.length/2..-1] z[0] = {"value"=>NArray.to_na(z[0]),"name"=>zval["name"]} z[1] = {"value"=>ap[tmp], "name"=>"ap_half_lev", "long_name"=>"hybrid ap at half levels", "units"=>"Pa"} z[2] = {"value"=>b[tmp], "name"=>"b_half_lev", "long_name"=>"hybrid b at half levels", "units"=>"1"} elsif nz==1 zval = pds.z_value[0] z = NArray.to_na(z[0]) d.put_att("units",zval["units"]) d.put_att("value_type",zval["name"]) else nz.times{|mm| m = nz-m-1 zval = pds.z_value[m] z[m+1] = {"value"=>NArray.to_na(z[m]),"name"=>zval["name"],"units"=>zval["units"]} } z[0] = {"value"=>NArray.sint(z[0].length).indgen,"name"=>"level number"} end d.put(z) else va.put_att("level", pds.z_type) pds.z_value.each{|zz| va.put_att(zz["name"].gsub(/\s/,"_"), "#{zz["value"]} #{zz["units"]}") } end else va.put_att("level", pds.z_type) end len = sgms.length time = Array.new(len) len.times{|i| time[i] = get_time(sgms[i].pds.date) } # sgms.each{|sgm| # time.push( get_time(sgm.pds.date) ) # } time.uniq! if @@always_time_seq or time.length>1 time = NArray.to_na(time) d = va.def_dim("time",-1) d.put(time) d.put_att("long_name","time") d.put_att("units","hours since 1900-01-01 00:00:0.0") else date = pds.date va.put_att("time","#{date[0]} #{"%02d"%date[1][0]}:#{"%02d"%date[1][1]}:0.0") end end return va end |
Instance Method Details
#att(key) ⇒ Object
1920 1921 1922 |
# File 'lib/numru/gphys/grib.rb', line 1920 def att(key) @attr[key] end |
#att_names ⇒ Object
1923 1924 1925 |
# File 'lib/numru/gphys/grib.rb', line 1923 def att_names @attr.keys end |
#def_dim(name, index) ⇒ Object
1907 1908 1909 1910 1911 1912 1913 1914 1915 |
# File 'lib/numru/gphys/grib.rb', line 1907 def def_dim(name,index) d = GribDim.new(self,name) if index==-1 @dims.push(d) else @dims[index] = d end return d end |
#dim(index) ⇒ Object
1899 1900 1901 1902 1903 |
# File 'lib/numru/gphys/grib.rb', line 1899 def dim(index) index = dim_names.index(index) if String===index return nil if index.nil? @dims[index] end |
#dim_names ⇒ Object
1896 1897 1898 |
# File 'lib/numru/gphys/grib.rb', line 1896 def dim_names @dims.collect{|d| d.name } end |
#file ⇒ Object
1874 1875 1876 |
# File 'lib/numru/gphys/grib.rb', line 1874 def file @file end |
#get(*indices) ⇒ Object Also known as: [], val
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 |
# File 'lib/numru/gphys/grib.rb', line 1938 def get(*indices) if @obj.nil? return nil elsif NArray===@obj||NArrayMiss===@obj return @obj[*indices] else sha = shape ndims = sha.length after_slice = false dimdel = nil if indices.length!=0 after_slicer = Array.new(2,true) imap = (3..ndims).collect{Hash.new} # index map org-->current for z,t if indices[0] == false indices[0] = [true]*(ndims-indices.length+1) elsif indices[-1] == false indices[-1] = [true]*(ndims-indices.length+1) elsif indices.include?(false) raise "invalid indices" elsif ndims!=indices.length raise "invalid indices" end dimdel = [] ndims.times{|n| ind = indices[n] dlen = sha[n] case ind when true if n<=1 indices[n] = 0..dlen-1 else (0..dlen-1).each{|i| imap[n-2][i] = i} end next when Fixnum sha[n] = 1 imap[n-2][ind] = 0 dimdel.push(n) next when Range f = ind.first f += dlen if f<0 e = ind.end e += dlen if e<0 e -= 1 if ind.exclude_end? sha[n] = e-f+1 if n<=1 indices[n] = f..e else (f..e).each{|i| imap[n-2][i] = i-f} end next when Hash ran, step = ind.to_a[0] f = ran.first f += dlen if f<0 e = ran.end e += dlen if e<0 e -= 1 if ran.exclude_end? sha[n] = (e-f)/step + 1 if n<=1 indices[n] = f..e after_slice = true after_slicer[n] = (0...sha[n]).collect{|i| i*step} else ia = NArray.int(sha[n]).indgen! * step + f indices[n] = NArray.to_na(ia) j=0 (f..e).step(step){|i| imap[n-2][i] = j; j+=1} if n>=2 end next when Array, NArray ind = ind.collect{|i| raise("Array indices must be integers") unless Integer===i i>=0 ? i : i+dlen } sha[n] = ind.length if n<=1 f = ind.min e = ind.max indices[n] = f..e after_slice = true after_slicer[n] = ind.collect{|i| i-f} else (0...ind.length).each{|i| imap[n-2][ind[i]] = i } end else raise "invalid indices" end } if rank>2 subsetzt = true else subsetzt = false end else subsetzt = false end value = @miss ? NArrayMiss.sfloat(*sha) : NArray.sfloat(*sha) if rank==2 vz = nil vt = nil index = [] elsif rank==4 vz = dim(2).val vt = dim(3).val index = Array.new(2) elsif !dim_names.include?("time") vz = dim(2).val vt = nil index = Array.new(1) else vt = dim(2).val vz = nil index = Array.new(1) end @obj.each{|sgm| pds = sgm.pds if vz index[0] = vz.eq(pds.z_value[0]["value"]).where[0] end if vt index[-1] = vt.eq(get_time(pds.date)).where[0] end if subsetzt to_read = true index.each_with_index{|i,d| to_read &&= imap[d][i]} next unless to_read end bds = sgm.bds if indices.length==0 || (indices[0]==true&&indices[1]==true) val = bds.value(shape[0..1]) else val = bds.value(shape[0..1],indices[0..1]) end d = pds.dfact val = val*10.0**(-d) val = val[*after_slicer] if after_slice if !subsetzt idx = index else idx = [] index.each_with_index{|i,d| idx.push(imap[d][i])} end value[true,true,*idx] = val } dimdel.reverse_each{|d| sha.delete_at(d)} if dimdel value.reshape!(*sha) return value end end |
#inspect ⇒ Object
2094 2095 2096 |
# File 'lib/numru/gphys/grib.rb', line 2094 def inspect "GribVar: #{@file.path}?var=#{@name}" end |
#name ⇒ Object
1877 1878 1879 |
# File 'lib/numru/gphys/grib.rb', line 1877 def name @name end |
#ndims ⇒ Object
1904 1905 1906 |
# File 'lib/numru/gphys/grib.rb', line 1904 def ndims @dims.length end |
#put(val) ⇒ Object
2091 2092 2093 |
# File 'lib/numru/gphys/grib.rb', line 2091 def put(val) @obj = val end |
#put_att(key, val) ⇒ Object Also known as: set_att
1916 1917 1918 |
# File 'lib/numru/gphys/grib.rb', line 1916 def put_att(key,val) @attr[key]=val end |
#rank ⇒ Object
1886 1887 1888 |
# File 'lib/numru/gphys/grib.rb', line 1886 def rank dim_names.length end |
#set_miss(miss) ⇒ Object
1883 1884 1885 |
# File 'lib/numru/gphys/grib.rb', line 1883 def set_miss(miss) @miss = miss end |
#set_sgms(sgms) ⇒ Object
1880 1881 1882 |
# File 'lib/numru/gphys/grib.rb', line 1880 def set_sgms(sgms) @obj = sgms end |
#shape ⇒ Object
1926 1927 1928 |
# File 'lib/numru/gphys/grib.rb', line 1926 def shape @dims.collect{|d| d.length } end |
#total ⇒ Object
1889 1890 1891 1892 1893 1894 1895 |
# File 'lib/numru/gphys/grib.rb', line 1889 def total total = 1 ndims.times{|i| total = total*dim(i).length } total end |
#typecode ⇒ Object
1929 1930 1931 1932 1933 1934 1935 1936 1937 |
# File 'lib/numru/gphys/grib.rb', line 1929 def typecode if @obj.nil? nil elsif Array===@obj NArray::SFLOAT else @obj.typecode end end |