Class: NumRu::GPhys::Grib::GribSegment

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bdsObject

Returns the value of attribute bds.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def bds
  @bds
end

#bmsObject

Returns the value of attribute bms.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def bms
  @bms
end

#esObject

Returns the value of attribute es.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def es
  @es
end

#fileObject

Returns the value of attribute file.



249
250
251
# File 'lib/numru/gphys/grib.rb', line 249

def file
  @file
end

#gdsObject

Returns the value of attribute gds.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def gds
  @gds
end

#isObject

Returns the value of attribute is.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def is
  @is
end

#mbioObject

Returns the value of attribute mbio.



249
250
251
# File 'lib/numru/gphys/grib.rb', line 249

def mbio
  @mbio
end

#pdsObject

Returns the value of attribute pds.



250
251
252
# File 'lib/numru/gphys/grib.rb', line 250

def pds
  @pds
end

#posObject

Returns the value of attribute pos.



249
250
251
# File 'lib/numru/gphys/grib.rb', line 249

def pos
  @pos
end

Class Method Details

.create(file) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/numru/gphys/grib.rb', line 192

def create(file)
  obj = GribSegment.new
  obj.file = file
  obj.is = GribIS.new(obj)
  obj.pds = GribPDS.new(obj)
  obj.gds = GribGDS.new(obj)
  obj.bms = GribBMS.new(obj)
  obj.bds = GribBDS.new(obj)
  obj.es = GribES.new(obj)
  obj.is.update_total_length
  return obj
end

.parse(file, mbio) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/numru/gphys/grib.rb', line 204

def parse(file, mbio)
  obj = GribSegment.new
  obj.file = file
  obj.mbio = mbio
  obj.pos = file.pos

  is = GribIS.new(obj,ss = file.read(8))
  unless is.version == 1
    warn "This Grib version (#{is.version}) has not supported yet"
    return nil
  end
  unless is.grib?
    raise "This file is not Grib file (INITIAL SECTION)"
  end
  obj.is = is

  pds = GribPDS.new(obj,file.read(file.read(3).to_uint3-3))
  obj.pds = pds

  str = pds.gds? ? file.read(file.read(3).to_uint3-3) : nil
  gds = GribGDS.new(obj,str)
  obj.gds = gds

  str = pds.bms? ? file.read(file.read(3).to_uint3-3) : nil
  bms = GribBMS.new(obj,str)
  obj.bms = bms

  len = file.read(3).to_uint3
  bds = GribBDS.new(obj,file.pos,len)
  obj.bds = bds
  file.seek(len-3,::IO::SEEK_CUR)

  es = GribES.new(obj,file.read(4))
  unless es.grib?
    raise "This file is not Grib file (END SECTION)"
  end
  obj.es = es

  if is.total_length!=is.length+pds.length+gds.length+bms.length+bds.length+es.length
    raise "total length is not equal to the sum of each section\nfile may be broken"
  end

  return obj
end

Instance Method Details

#set_level(z) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/numru/gphys/grib.rb', line 276

def set_level(z)
  if Array===z
    id = Z_TYPES.invert[Z_TYPES.values.assoc(z[0])]
    id = 100 if id.nil?
    pds.set_z_id(id)
    pds.set_z_value(z[1])
  else
    id = Z_TYPES.invert[Z_TYPES.values.assoc(z)]
    pds.set_z_id(id)
  end
end

#set_time(t) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/numru/gphys/grib.rb', line 287

def set_time(t)
  if String===t
    ye,mo,da,ho,mi, = Time.parse(t).to_a.reverse![4..-2]
  else
    if t.nil?
      ye = 0
      mo = 1
      da = 1
      ho = 0
      mi = 0
    else
      d = D19000101 + t/24
      ye = d.year
      mo = d.month
      da = d.day
      ho = t%24
      mi = 0
    end
  end
  pds.set_initial(ye,mo,da,ho,mi)
end

#set_var(var, name, units) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/numru/gphys/grib.rb', line 308

def set_var(var,name,units)
  is.set_version(1)
  pds.set_version(2)
  pds.set_center_id(0)
  pds.set_pid(0)
  id = PARAMS_2.invert[NAMES_UNITS.invert[NAMES_UNITS.values.assoc(name)]]
  id = 000 if id.nil?
  pds.set_name_id(id)
  pds.set_time_unit_id(0)
  pds.set_p1(0)
  pds.set_p2(0)
  pds.set_time_range_id(1)
  pds.set_ave(0)
  pds.set_miss(0)
  pds.set_sub_center_id(0)
  pds.set_dfact(0)
  if NArrayMiss===var
    mask = var.get_mask!
    if mask.count_false>0
      pds.set_bms(true)
      bms.set_map_number(0)
      bms.set_map(mask)
    end
  end
  bds.set_grid(true)
  bds.set_simple(true)
  if units && pds.unit
    f,o = Units[units].factor_and_offset(Units[pds.unit])
    var = var*f+o
  end
  if var.typecode==NArray::SINT || var.typecode==NArray::INT
    bds.set_ingeger(true)
  else
    bds.set_float(true)
  end
  bds.set_value(var)
end

#set_xy(x, y) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/numru/gphys/grib.rb', line 251

def set_xy(x,y)
  pds.set_gds(true)
  if x.att("long_name").downcase=="longitude" && y.att("long_name").downcase=="latitude"
    xv = x.val
    yv = y.val
    rel = false
    if (xv[1]-xv[0])==(xv[-1]-xv[-2]) && (yv[1]-yv[0])==(yv[-1]-yv[-2])
      id = 0
    else
      raise "not defined yet"
    end
  elsif y.att("long_name").downcase=="longitude" && x.att("long_name").downcase=="latitude"
    xv = y.val
    yv = x.val
    rel = true
    if (xv[1]-xv[0])==(xv[-1]-xv[-2]) && (yv[1]-yv[0])==(yv[-1]-yv[-2])
      id = 0
    else
      raise "not defined yet"
    end
  else
    raise "not defined yet"
  end
  gds.set_grid(id,xv,yv,rel)
end

#writeObject



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/numru/gphys/grib.rb', line 345

def write
  tlen = is.update_total_length
  len = 0
  len += @file.write(is.get)
  len += @file.write(pds.get)
  len += @file.write(gds.get)
  len += @file.write(bms.get)
  len += @file.write(bds.get)
  len += @file.write(es.get)
  tlen==len || raise("length is not correct")
end