Class: NumRu::GPhys::Grib

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/gphys/grib.rb

Overview

GribUtils

Defined Under Namespace

Classes: GribBDS, GribBMS, GribES, GribGDS, GribIS, GribPDS, GribSegment

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(fname) ⇒ Object



1417
1418
1419
# File 'lib/numru/gphys/grib.rb', line 1417

def create(fname)
  Grib.new(fname,"w")
end

.is_a_Grib?(fname) ⇒ Boolean

Returns:

  • (Boolean)


1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
# File 'lib/numru/gphys/grib.rb', line 1399

def is_a_Grib?(fname)
  file = nil
  begin
    file = File.open(fname,"rb")
    is = (file.read(4) == "GRIB")
  ensure
    file.close if file
  end
  return defined?(is) && is
end

.open(fname) ⇒ Object



1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/numru/gphys/grib.rb', line 1409

def open(fname)
  #warn "This grib library is obsoleted."
  #warn "Use http://ruby.gfd-dennou.org/products/rb-grib/ instead."
  f = Grib.new(fname,"r")
  mbio = NumRu::MultibitIO.new(fname)
  f.parse(mbio)
  return f
end

Instance Method Details

#att(name) ⇒ Object



1471
1472
1473
# File 'lib/numru/gphys/grib.rb', line 1471

def att(name)
  @attr[name]
end

#att_namesObject



1468
1469
1470
# File 'lib/numru/gphys/grib.rb', line 1468

def att_names
  @attr.keys
end

#closeObject



1422
1423
1424
1425
1426
1427
# File 'lib/numru/gphys/grib.rb', line 1422

def close
  @grib_vars = nil
  @vars = nil
  @attr = nil
  @file.close
end

#def_var(name) ⇒ Object



1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
# File 'lib/numru/gphys/grib.rb', line 1456

def def_var(name)
  until @file.stat.writable?
    raise("File #{@file.path} is not writable.")
  end
  if !String===name
    raise "def_var(String)"
  end
  raise "#{name} is already defined" if @vars.has_key?(name)
  var = GribVar.new(@file,name)
  @vars[name] = var
  return var
end

#inspectObject



1552
1553
1554
# File 'lib/numru/gphys/grib.rb', line 1552

def inspect
  "Grib: #{path}"
end

#parse(mbio) ⇒ Object



1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# File 'lib/numru/gphys/grib.rb', line 1428

def parse(mbio)
  @file.rewind
  while true
    @file.read(1)=="\000" && next
    @file.eof? && break
    @file.seek(-1, ::IO::SEEK_CUR)
    sgm = GribSegment.parse(@file, mbio)
    return nil unless sgm
    name = sgm.pds.sname
    if @vars.has_key?(name)
      @vars[name].push(sgm)
    else
      @vars[name] = [sgm]
    end
  end
  return self
end

#pathObject



1445
1446
1447
# File 'lib/numru/gphys/grib.rb', line 1445

def path
  @file.path
end

#var(name) ⇒ Object



1451
1452
1453
1454
1455
# File 'lib/numru/gphys/grib.rb', line 1451

def var(name)
  var = @vars[name]
  return nil if var.nil?
  return GribVar.parse(self,var,name)
end

#var_namesObject



1448
1449
1450
# File 'lib/numru/gphys/grib.rb', line 1448

def var_names
  @vars.keys
end

#writeObject



1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'lib/numru/gphys/grib.rb', line 1474

def write
  iz=0
  it=0
  izmax = 0
  vn = var_names
  @file.rewind
  while (vn.length>0)
    vn.dup.each{|name|
      var = @vars[name]
      sgm = nil
      if var.rank==2
        sgm = GribSegment.create(@file)
        val = [var.get,var.att("long_name"),var.att("units")]
        z = var.att("level")
        t = var.att("time")
        vn.delete(name)
      elsif var.rank==4
        sha = var.shape
        izmax < sha[2] && izmax = sha[2]
        if iz<sha[2]&&it<sha[3]
          sgm = GribSegment.create(@file)
          val = [var[true,true,iz,it],var.att("long_name"),var.att("units")]
          zd = var.dim(2)
          zv = zd.get
          if Array===zv
            zv = zv.collect{|a| a["value"][iz] }
          else
            zv = [zv[iz]]
          end
          z = [zd.att("long_name"), zv]
          t = var.dim(3)[it]
        elsif it==sha[3]
          vn.delete(name)
        end
      elsif !var.dim_names.include?("time")
        sha = var.shape
        izmax < sha[2] && izmax = sha[2]
        if iz<sha[2]
          sgm = GribSegment.create(@file)
          val = [var[true,true,iz],var.att("long_name"),var.att("units")]
          zd = var.dim(2)
          zv = zd.get
          if Array===zv
            zv = zv.collect{|a| a["value"][iz] }
          else
            zv = [zv[iz]]
          end
          z = [zd.att("long_name"), zv]
          t = var.att("time")
        end
        vn.delete(name) if iz==sha[2]-1
      else
        sha = var.shape
        if it<sha[2]
          sgm = GribSegment.create(@file)
          val = [var[true,true,it],var.att("long_name"),var.att("units")]
          z = var.att("level")
          t = var.dim(2)[it]
        else
          vn.delete(name)
        end
      end
      if sgm
        sgm.set_xy(var.dim(0),var.dim(1))
        sgm.set_level(z)
        sgm.set_time(t)
        sgm.set_var(*val)
        sgm.write
      end
      if iz==izmax-1
        iz = 0
        it += 1
      else
        iz += 1
      end
    }
  end
end