Class: NumRu::GPhys::GribVar

Inherits:
Object
  • Object
show all
Extended by:
GribUtils
Includes:
GribUtils
Defined in:
lib/numru/gphys/grib.rb

Overview

NumRu::GribVar

Class Methods

—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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, name) ⇒ GribVar

end definition of class methods GribVar



1834
1835
1836
1837
1838
1839
1840
# File 'lib/numru/gphys/grib.rb', line 1834

def initialize(file,name)
  @file = file
  @name = name
  @attr = Hash.new
  @miss = false
  @dims = Array.new
end

Class Method Details

.parse(file, sgms, name) ⇒ Object



1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
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
# File 'lib/numru/gphys/grib.rb', line 1714

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 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



1887
1888
1889
# File 'lib/numru/gphys/grib.rb', line 1887

def att(key)
  @attr[key]
end

#att_namesObject



1890
1891
1892
# File 'lib/numru/gphys/grib.rb', line 1890

def att_names
  @attr.keys
end

#def_dim(name, index) ⇒ Object



1874
1875
1876
1877
1878
1879
1880
1881
1882
# File 'lib/numru/gphys/grib.rb', line 1874

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



1866
1867
1868
1869
1870
# File 'lib/numru/gphys/grib.rb', line 1866

def dim(index)
  index = dim_names.index(index) if String===index
  return nil if index.nil?
  @dims[index]
end

#dim_namesObject



1863
1864
1865
# File 'lib/numru/gphys/grib.rb', line 1863

def dim_names
  @dims.collect{|d| d.name }
end

#fileObject



1841
1842
1843
# File 'lib/numru/gphys/grib.rb', line 1841

def file
  @file
end

#get(*indices) ⇒ Object Also known as: [], val



1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
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
# File 'lib/numru/gphys/grib.rb', line 1905

def get(*indices)
  if @obj.nil?
    return nil
  elsif NArray===@obj||NArrayMiss===@obj
    return @obj[*indices]
  else
    sha = shape
    if indices.length!=0
      if indices[0] == false
        indices[0] = [true]*(sha.length-indices.length+1)
      elsif indices[-1] == false
        indices[-1] = [true]*(sha.length-indices.length+1)
      elsif indices.include?(false)
        raise "invalid indices"
      elsif sha.length!=indices.length
        raise "invalid indices"
      end
      sha.length.times{|n|
        ind = indices[n]
        if ind==true
          indices[n] = 0..sha[n]-1
          next
        elsif Fixnum===ind
          sha[n] = 1
          next
        elsif Range===ind
          f = ind.first
          e = ind.end
          e = sha[n]-1 if e==-1
          e -= 1 if ind.exclude_end?
          sha[n] = e-f+1
          indices[n] = f..e
          next
        else
          raise "invalid indices"
        end
      }
      if rank>2
        mask = NArray.byte(*shape[2..-1])
        mask[*indices[2..-1]]= 1
        first = Array.new(indices.length-2)
 first.length.times{|i|
          ind = indices[2+i]
          if Fixnum===ind
     first[i] = ind < 0 ? shape[2+i]+ind : ind
          elsif Range===ind
            first[i] = ind.first
          else
            raise "invalid indices"
          end
        }
      else
        mask = true
      end
    else
      mask = true
    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
      next if (NArray===mask && mask[*index]==0)
      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],sha[0..1])
      end
      d = pds.dfact
      val = val*10.0**(-d)
      if NArray === mask
        index.length==first.length || raise("invalide indices")
        index.length.times{|i|
          index[i] = index[i]-first[i]
        }
      end
      value[true,true,*index] = val
    }
    sha.delete(1)
    value.reshape!(*sha)
    return value
  end
end

#inspectObject



2015
2016
2017
# File 'lib/numru/gphys/grib.rb', line 2015

def inspect
  "GribVar: #{@file.path}?var=#{@name}"
end

#nameObject



1844
1845
1846
# File 'lib/numru/gphys/grib.rb', line 1844

def name
  @name
end

#ndimsObject



1871
1872
1873
# File 'lib/numru/gphys/grib.rb', line 1871

def ndims
  @dims.length
end

#put(val) ⇒ Object



2012
2013
2014
# File 'lib/numru/gphys/grib.rb', line 2012

def put(val)
  @obj = val
end

#put_att(key, val) ⇒ Object Also known as: set_att



1883
1884
1885
# File 'lib/numru/gphys/grib.rb', line 1883

def put_att(key,val)
  @attr[key]=val
end

#rankObject



1853
1854
1855
# File 'lib/numru/gphys/grib.rb', line 1853

def rank
  dim_names.length
end

#set_miss(miss) ⇒ Object



1850
1851
1852
# File 'lib/numru/gphys/grib.rb', line 1850

def set_miss(miss)
  @miss = miss
end

#set_sgms(sgms) ⇒ Object



1847
1848
1849
# File 'lib/numru/gphys/grib.rb', line 1847

def set_sgms(sgms)
  @obj = sgms
end

#shapeObject



1893
1894
1895
# File 'lib/numru/gphys/grib.rb', line 1893

def shape
  @dims.collect{|d| d.length }
end

#totalObject



1856
1857
1858
1859
1860
1861
1862
# File 'lib/numru/gphys/grib.rb', line 1856

def total
  total = 1
  ndims.times{|i|
    total = total*dim(i).length
  }
  total
end

#typecodeObject



1896
1897
1898
1899
1900
1901
1902
1903
1904
# File 'lib/numru/gphys/grib.rb', line 1896

def typecode
  if @obj.nil?
    nil
  elsif Array===@obj
    NArray::SFLOAT
  else
    @obj.typecode
  end
end