Class: NumRu::Grib
- Inherits:
-
Object
- Object
- NumRu::Grib
- Defined in:
- lib/numru/grib/grib.rb,
lib/numru/grib/version.rb,
ext/grib.c
Constant Summary collapse
- VERSION =
"0.2.2"
- TYPE_UNDEFINED =
INT2NUM(GRIB_TYPE_UNDEFINED)
- TYPE_LONG =
INT2NUM(GRIB_TYPE_LONG)
- TYPE_DOUBLE =
INT2NUM(GRIB_TYPE_DOUBLE)
- TYPE_STRING =
INT2NUM(GRIB_TYPE_STRING)
- TYPE_BYTES =
INT2NUM(GRIB_TYPE_BYTES)
- TYPE_SECTION =
INT2NUM(GRIB_TYPE_SECTION)
- TYPE_LABEL =
INT2NUM(GRIB_TYPE_LABEL)
- TYPE_MISSING =
INT2NUM(GRIB_TYPE_MISSING)
- NEAREST_SAME_GRID =
ULONG2NUM(GRIB_NEAREST_SAME_GRID)
- NEAREST_SAME_DATA =
ULONG2NUM(GRIB_NEAREST_SAME_DATA)
- NEAREST_SAME_POINT =
ULONG2NUM(GRIB_NEAREST_SAME_POINT)
- KEYS_ITERATOR_ALL_KEYS =
ULONG2NUM(GRIB_KEYS_ITERATOR_ALL_KEYS)
- KEYS_ITERATOR_SKIP_READ_ONLY =
ULONG2NUM(GRIB_KEYS_ITERATOR_SKIP_READ_ONLY)
- @@gribs =
cache due to momery leak in the original grib_api library
Hash.new
Class Method Summary collapse
- ._new ⇒ Object
- .is_a_Grib?(fname) ⇒ Boolean
-
.multi=(flag) ⇒ Object
NumRu::Grib.multi=(flag) -> True/False.
- .new(fname, mode = "r") ⇒ Object (also: open)
Instance Method Summary collapse
-
#_close ⇒ Object
class << self.
-
#close ⇒ Object
NumRu::Grib#close() -> nil.
-
#initialize(*args) ⇒ Object
constructor
NumRu::Grib#initialize(filename, [, mode]).
- #inspect ⇒ Object
-
#path ⇒ Object
NumRu::Grib#path() -> String.
-
#var(rvname) ⇒ Object
NumRu::Grib#var(String) -> NumRu::GribVar.
-
#var_names ⇒ Object
NumRu::Grib#var_names() -> Array.
Constructor Details
#initialize(*args) ⇒ Object
NumRu::Grib#initialize(filename, [, mode])
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'ext/grib.c', line 379
static VALUE
rg_file_initialize(int argc, VALUE *argv, VALUE self)
{
char *fname, *mode;
VALUE rfname, rmode;
rb_scan_args(argc, argv, "11", &rfname, &rmode);
fname = StringValueCStr(rfname);
if (rmode == Qnil)
mode = "r";
else
mode = StringValueCStr(rmode);
FILE *file = fopen(fname, mode);
if (!file)
rb_raise(rb_eRuntimeError, "unable to open file %s", fname);
rg_file *gfile;
Data_Get_Struct(self, rg_file, gfile);
gfile->file = file;
gfile->fname = ALLOC_N(char, strlen(fname)+1);
strcpy(gfile->fname, fname);
int error;
grib_handle *handle;
var_list *var = NULL;
while ((handle = grib_handle_new_from_file(0, file, &error))) {
check_error(error);
if (handle == NULL)
rb_raise(rb_eRuntimeError, "unable to create handle in %s", fname);
push_msg_var(&var, handle, self);
}
gfile->var = var;
var_list *var2;
const char *vname;
char buf[MAX_VALUE_LENGTH];
int first_ltype, first_gtype;
int i;
while (var) {
vname = var->vname;
var2 = var->next;
i = 0;
first_ltype = 1;
first_gtype = 1;
while (var2) {
if (strcmp(var2->vname, vname) == 0) {
if (var->ltype_id != var2->ltype_id) {
if (first_ltype) {
if (strcmp(var->ltype,"isobaricInhPa") != 0 && strcmp(var->ltype,"pl") !=0) {
if (strcmp(var->ltype, "unknown")==0)
sprintf(buf, "%s_level%ld", var->vname, var->ltype_id);
else
sprintf(buf, "%s_%s", var->vname, var->ltype);
strcpy(var->vname, buf);
}
first_ltype = 0;
}
if (strcmp(var2->ltype,"isobaricInhPa") != 0 && strcmp(var2->ltype,"pl") != 0) {
if (strcmp(var2->ltype, "unknown")==0)
sprintf(buf, "%s_level%ld", var2->vname, var2->ltype_id);
else
sprintf(buf, "%s_%s", var2->vname, var2->ltype);
strcpy(var2->vname, buf);
}
} else if (strcmp(var->gtype, var2->gtype) != 0) {
if (first_gtype) {
sprintf(buf, "%s_%s", var->vname, var->gtype);
strcpy(var->vname, buf);
first_gtype = 0;
}
sprintf(buf, "%s_%s", var2->vname, var2->gtype);
strcpy(var2->vname, buf);
} else {
if (i==0) {
sprintf(buf, "%s_%d", var->vname, i);
strcpy(var->vname, buf);
i += 1;
}
sprintf(buf, "%s_%d", var2->vname, i);
strcpy(var2->vname, buf);
i += 1;
}
}
var2 = var2->next;
}
var = var->next;
}
return self;
}
|
Class Method Details
._new ⇒ Object
13 |
# File 'lib/numru/grib/grib.rb', line 13 alias :_new :new |
.is_a_Grib?(fname) ⇒ Boolean
6 7 8 9 10 11 12 |
# File 'lib/numru/grib/grib.rb', line 6 def is_a_Grib?(fname) is = false File.open(fname) do |file| is = (file.read(4) == "GRIB") end return is end |
.multi=(flag) ⇒ Object
NumRu::Grib.multi=(flag) -> True/False
364 365 366 367 368 369 370 371 372 373 374 |
# File 'ext/grib.c', line 364
static VALUE
rg_multi(VALUE self, VALUE flag)
{
if (flag == Qtrue)
grib_multi_support_on(0);
else if (flag == Qfalse)
grib_multi_support_off(0);
else
rb_raise(rb_eArgError, "flag must be true or false");
return flag;
}
|
.new(fname, mode = "r") ⇒ Object Also known as: open
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/numru/grib/grib.rb', line 14 def new(fname, mode="r") key = [fname,mode] if (obj = @@gribs[key]) obj[1] += 1 return obj[0] else grib = _new(fname, mode) @@gribs[key] = [grib, 1] return grib end end |
Instance Method Details
#_close ⇒ Object
class << self
28 |
# File 'lib/numru/grib/grib.rb', line 28 alias :_close :close |
#close ⇒ Object
NumRu::Grib#close() -> nil
471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'ext/grib.c', line 471 def close @@gribs.each do |k, val| if self == val[0] val[1] -= 1 if val[1] < 1 _close @@gribs.delete(k) return true end end end return false end |
#inspect ⇒ Object
43 44 45 |
# File 'lib/numru/grib/grib.rb', line 43 def inspect "Grib: #{path}" end |
#path ⇒ Object
NumRu::Grib#path() -> String
483 484 485 486 487 488 489 |
# File 'ext/grib.c', line 483
static VALUE
rg_file_path(VALUE self)
{
rg_file *gfile;
Data_Get_Struct(self, rg_file, gfile);
return rb_str_new2(gfile->fname);
}
|
#var(rvname) ⇒ Object
NumRu::Grib#var(String) -> NumRu::GribVar
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'ext/grib.c', line 512
static VALUE
rg_file_var(VALUE self, VALUE rvname)
{
char *vname = StringValueCStr(rvname);
rg_file *gfile;
Data_Get_Struct(self, rg_file, gfile);
var_list *var_list = gfile->var;
while (var_list) {
if (strcmp(var_list->vname, vname) == 0) {
VALUE rvar = var_alloc(cVar);
rg_var *var;
Data_Get_Struct(rvar, rg_var, var);
var->file = self;
var->var_list = var_list;
rb_funcall(rvar, id_init, 0);
return rvar;
}
var_list = var_list->next;
}
rb_raise(rb_eArgError, "cannot find variable: %s", vname);
}
|
#var_names ⇒ Object
NumRu::Grib#var_names() -> Array
494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'ext/grib.c', line 494
static VALUE
rg_file_var_names(VALUE self)
{
rg_file *gfile;
Data_Get_Struct(self, rg_file, gfile);
VALUE ary = rb_ary_new();
var_list *var = gfile->var;
while (var) {
rb_ary_push(ary, rb_str_new2(var->vname));
var = var->next;
}
return ary;
}
|