File: grib_read_spec.rb

package info (click to toggle)
ruby-grib 0.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 444 kB
  • sloc: ansic: 708; ruby: 708; makefile: 4
file content (203 lines) | stat: -rw-r--r-- 6,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require File.expand_path(File.join('.','spec_helper'), File.dirname(__FILE__))
include NumRu

dir = File.expand_path(File.join('..','data'), File.dirname(__FILE__))
GribData = {
  File.join(dir, 'regular_gaussian_surface.grib1') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>2.4552479553e2}}
  },
  File.join(dir, 'regular_gaussian_surface.grib2') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>2.4552479553e2}}
  },
  File.join(dir, 'regular_gaussian_model_level.grib1') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>1.9907820129e2}}
  },
  File.join(dir, 'regular_gaussian_model_level.grib2') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>1.9907820129e2}}
  },
  File.join(dir, 'regular_gaussian_pressure_level.grib1') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>2.4746011353e2}}
  },
  File.join(dir, 'regular_gaussian_pressure_level.grib2') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>2.4746011353e2}}
  },
  File.join(dir, 'regular_gaussian_pressure_level_constant.grib1') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>1.0}}
  },
  File.join(dir, 'regular_gaussian_pressure_level_constant.grib2') =>
  {"t"=>{:dims=>["lon","lat"], :shape=>[128,64], :val=>{[0,0]=>1.0}}
  },
  File.join(dir, 'regular_latlon_surface.grib1') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[16,31], :val=>{[0,0]=>279.0}}
  },
  File.join(dir, 'regular_latlon_surface.grib2') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[16,31], :val=>{[0,0]=>279.0}}
  },
  File.join(dir, 'regular_latlon_surface_constant.grib1') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[16,31], :val=>{[0,0]=>1.0}}
  },
  File.join(dir, 'regular_latlon_surface_constant.grib2') =>
  {"2t"=>{:dims=>["lon","lat"], :shape=>[16,31], :val=>{[0,0]=>1.0}}
  },
  File.join(dir, 'tp_ecmwf.grib') =>
  {"tp"=>{:dims=>["lon","lat","step"], :shape=>[16,31,4], :val=>{[0,0,0]=>2.0546913147e-3}}
  }
}
fname_not_exist = 'not_exist'

describe NumRu::Grib do
  describe 'Grib.is_a_Grib?' do
    it 'returns whether the specific file is a GRIB1/2 file' do
      GribData.each do |fname, hash|
        expect(NumRu::Grib.is_a_Grib?(fname)).to eq true
      end
    end
  end

  describe 'Grib.open' do
    before { @files = [] }
    after { @files.each{|file| file.close} }
    it 'returns Grib object' do
      GribData.each do |fname, hash|
        file = NumRu::Grib.open(fname)
        expect(file).to be_instance_of(NumRu::Grib)
        @files.push file
      end
    end
    it 'raise exception when file name which does not exist is given' do
      expect { NumRu::Grib.open(fname_not_exist) }.to raise_error(RuntimeError)
    end
  end

  describe '#close' do
    before do
      @files = []
      GribData.each{|fname,hash| @files.push NumRu::Grib.open(fname) }
    end
    it do
      @files.each do |file|
        expect {file.close}.not_to raise_error
      end
    end
  end

  describe 'instance methods' do
    before do
      @files = {}
      GribData.each{|fname,hash| @files[fname] = NumRu::Grib.open(fname) }
    end
    after do
      @files.each{|fname,file| file.close}
    end
    it '#path returns path' do
      @files.each do |fname,file|
        expect( file.path ).to eq fname
      end
    end
    it '#var_names returns Array of variable names' do
      @files.each do |fname, file|
        expect( file.var_names ).to eq GribData[fname].keys
      end
    end
    it '#var returns GribVar object' do
      @files.each do |fname, file|
        GribData[fname].each do |vname, hash|
          expect( file.var(vname) ).to be_instance_of NumRu::GribVar
        end
      end
    end
  end
end

describe NumRu::GribVar do
  before do
    @files = []
    @vars = Hash.new
    GribData.each do |fname,vars|
      file = NumRu::Grib.open(fname)
      @files.push file
      vars.each{|vname, hash| @vars[file.var(vname)] = hash}
    end
  end
  after { @files.each{|file| file.close}}
  it '#rank returns rank' do
    @vars.each{|var,hash| expect(var.rank).to eq hash[:shape].length}
  end
  it '#total returns total length' do
    @vars.each{|var,hash| expect(var.total).to eq hash[:shape].inject(1,:*)}
  end
  it '#dim_names returns Array of dimension names' do
    @vars.each{|var,hash| expect(var.dim_names).to eq hash[:dims]}
  end
  it '#shape returns Array of shape' do
    @vars.each{|var,hash| expect(var.shape).to eq hash[:shape]}
  end
  it '#dim returns GribDim' do
    @vars.each do |var,hash|
      hash[:dims].each{|dname| expect(var.dim(dname)).to be_instance_of NumRu::GribDim}
    end
  end
  it '#att_names returns Array of attribute names' do
    @vars.each{|var,hash| expect(var.att_names).to be_instance_of Array}
  end
  it '#att returns attribute value' do
    @vars.each do |var,hash|
      var.att_names.each{|aname| expect(var.att(aname)).to_not eq nil }
    end
  end
  it '#typecode returns type code' do
    @vars.each{|var,hash| expect(var.typecode).to be_instance_of Integer}
  end
  it '#missing_value returns missing value' do
    @vars.each{|var,hash| expect(var.missing_value).to be_kind_of Numeric}
  end
  it '#get returns value' do
    @vars.each do |var,hash|
      hash[:val].each do |idx,val|
        v = var.get[*idx]
        expect((v-val).abs/((v+val)/2)).to be < 1e-10
      end
    end
  end
end

describe NumRu::GribDim do
  before do
    @files = []
    @dims = Hash.new
    GribData.each do |fname, vars|
      file = NumRu::Grib.open(fname)
      @files.push file
      vars.each do |vname, hash|
        var = file.var(vname)
        hash[:dims].each_with_index do |dname,i|
          @dims[var.dim(dname)] = hash[:shape][i]
        end
      end
    end
  end
  after do
    @files.each{|file| file.close}
  end
  it '#length returns length' do
    @dims.each{|dim,len| expect(dim.length).to eq len}
  end
  it '#val returns value' do
    @dims.each do |dim,len|
      expect(dim.val).to be_instance_of NArray
      expect(dim.val.length).to eq len
    end
  end
  it '#typecode returns typecode' do
    @dims.each{|dim,len| expect(dim.typecode).to be_instance_of Integer}
  end
  it '#att_names returns Array of attribute names' do
    @dims.each{|dim,len| expect(dim.att_names).to be_instance_of Array}
  end
  it '#att returns attributes value' do
    @dims.each do |dim,len|
      dim.att_names.each{|aname| expect(dim.att(aname)).to_not eq nil }
    end
  end

end