Class: NumRu::GribMessage
- Inherits:
-
Object
- Object
- NumRu::GribMessage
- Defined in:
- lib/numru/grib/grib.rb,
ext/grib.c
Overview
class Grib
Constant Summary collapse
- MEMBER_NAME =
{1 => "", 2 => "n", 3 => "p"}
Instance Method Summary collapse
- #date ⇒ Object
- #date_raw ⇒ Object
- #ensemble_member ⇒ Object
-
#get_data ⇒ Object
NumRu::GribMessage#get_data() -> [lon, lat, value].
-
#get_keys(*args) ⇒ Object
NumRu::GribMessage#get_keys() -> Array.
-
#get_value(*args) ⇒ Object
NumRu::GribMessage#get_value(name [,type]) -> String.
- #get_xy ⇒ Object
- #gtype ⇒ Object
-
#initialize(file) ⇒ Object
constructor
NumRu::GribMessage#initialize().
- #missing_value ⇒ Object
- #name ⇒ Object
- #sname ⇒ Object
- #step ⇒ Object
- #step_units ⇒ Object
- #time_interval ⇒ Object
- #units ⇒ Object (also: #unit)
- #z_sname ⇒ Object
- #z_type ⇒ Object
- #z_units ⇒ Object
- #z_value ⇒ Object
Constructor Details
#initialize(file) ⇒ Object
NumRu::GribMessage#initialize()
584 585 586 587 588 589 590 591 |
# File 'ext/grib.c', line 584
static VALUE
rg_message_initialize(VALUE self, VALUE file)
{
rg_message *message;
Data_Get_Struct(self, rg_message, message);
message->file = file;
return self;
}
|
Instance Method Details
#date ⇒ Object
77 78 79 |
# File 'lib/numru/grib/grib.rb', line 77 def date return DateTime.parse(date_raw) end |
#date_raw ⇒ Object
80 81 82 83 84 |
# File 'lib/numru/grib/grib.rb', line 80 def date_raw d = get_value("dataDate").to_s.rjust(8,"0") h = get_value("dataTime").to_s.rjust(4,"0") d << h end |
#ensemble_member ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/numru/grib/grib.rb', line 95 def ensemble_member pn = get_value("perturbationNumber") te = get_value("typeOfEnsembleForecast") name = "" name << pn.to_s if pn name << (MEMBER_NAME[te] || te.to_s) if te name end |
#get_data ⇒ Object
NumRu::GribMessage#get_data() -> [lon, lat, value]
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'ext/grib.c', line 720
static VALUE
rg_message_get_data(VALUE self)
{
rg_message *message;
Data_Get_Struct(self, rg_message, message);
int error;
grib_iterator *iter = grib_iterator_new(message->handle, 0, &error);
check_error(error);
long np;
check_error(grib_get_long(message->handle, "numberOfPoints", &np));
double *lon, *lat, *value;
VALUE na_lon, na_lat, na_value;
struct NARRAY *nary;
int shape[1];
shape[0] = np;
na_lon = na_make_object(NA_DFLOAT, 1, shape, cNArray);
GetNArray(na_lon, nary);
lon = (double*) nary->ptr;
na_lat = na_make_object(NA_DFLOAT, 1, shape, cNArray);
GetNArray(na_lat, nary);
lat = (double*) nary->ptr;
na_value = na_make_object(NA_DFLOAT, 1, shape, cNArray);
GetNArray(na_value, nary);
value = (double*) nary->ptr;
int n = 0;
double lo, la, val;
while( grib_iterator_next(iter, &la, &lo, &val) ) {
lat[n] = la;
lon[n] = lo;
value[n] = val;
n++;
}
grib_iterator_delete(iter);
return rb_ary_new3(3, na_lon, na_lat, na_value);
}
|
#get_keys(*args) ⇒ Object
NumRu::GribMessage#get_keys() -> Array
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'ext/grib.c', line 596
static VALUE
rg_message_get_keys(int argc, VALUE *argv, VALUE self)
{
VALUE rflag, rns;
unsigned long flag = GRIB_KEYS_ITERATOR_SKIP_READ_ONLY || GRIB_KEYS_ITERATOR_SKIP_COMPUTED;
// unsigned long flag = GRIB_KEYS_ITERATOR_ALL_KEYS;
char *name_space = NULL;
rb_scan_args(argc, argv, "02", &rflag, &rns);
if (rflag != Qnil) flag = NUM2ULONG(rflag);
if (rns != Qnil) name_space = StringValueCStr(rns);
rg_message *message;
Data_Get_Struct(self, rg_message, message);
grib_keys_iterator *ki = NULL;
ki = grib_keys_iterator_new(message->handle, flag, name_space);
if (!ki)
rb_raise(rb_eRuntimeError, "unable to create key iterator");
VALUE keys = rb_ary_new();
while (grib_keys_iterator_next(ki)) {
const char *name = grib_keys_iterator_get_name(ki);
rb_ary_push(keys, rb_str_new2(name));
}
grib_keys_iterator_delete(ki);
return keys;
}
|
#get_value(*args) ⇒ Object
NumRu::GribMessage#get_value(name [,type]) -> String
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'ext/grib.c', line 625
static VALUE
rg_message_get_value(int argc, VALUE *argv, VALUE self)
{
VALUE rname, rtype;
rb_scan_args(argc, argv, "11", &rname, &rtype);
char *name = StringValueCStr(rname);
int type;
rg_message *message;
Data_Get_Struct(self, rg_message, message);
if (rtype == Qnil)
grib_get_native_type(message->handle, name, &type);
else
type = NUM2INT(rtype);
size_t len;
VALUE ret = Qnil;
grib_get_size(message->handle, name, &len);
switch (type) {
case GRIB_TYPE_UNDEFINED:
case GRIB_TYPE_STRING:
case GRIB_TYPE_LABEL:
{
char value[MAX_VALUE_LENGTH];
len = MAX_VALUE_LENGTH;
bzero(value, len);
if (grib_get_string(message->handle, name, value, &len) == GRIB_SUCCESS) {
if (value[len-1] == '\0') len--;
ret = rb_str_new(value, len);
}
}
break;
case GRIB_TYPE_BYTES:
{
unsigned char value[MAX_VALUE_LENGTH];
len = MAX_VALUE_LENGTH;
bzero(value, len);
if (grib_get_bytes(message->handle, name, value, &len) == GRIB_SUCCESS)
ret = rb_str_new((char*)value, len);
}
break;
case GRIB_TYPE_LONG:
{
if (len == 1) {
long l;
if (rtype == Qnil) {
char value[MAX_VALUE_LENGTH];
len = MAX_VALUE_LENGTH;
bzero(value, len);
if (grib_get_string(message->handle, name, value, &len) == GRIB_SUCCESS) {
check_error(grib_get_long(message->handle, name, &l));
if (atol(value) == l)
ret = LONG2NUM(l);
else
ret = rb_str_new2(value);
}
} else {
check_error(grib_get_long(message->handle, name, &l));
ret = LONG2NUM(l);
}
} else {
int shape[1];
struct NARRAY *nary;
shape[0] = len;
VALUE rnary = na_make_object(NA_LINT, 1, shape, cNArray);
GetNArray(rnary, nary);
if (grib_get_long_array(message->handle, name, (long*)nary->ptr, &len) == GRIB_SUCCESS)
ret = rnary;
}
}
break;
case GRIB_TYPE_DOUBLE:
{
if (len == 1) {
double value;
if (grib_get_double(message->handle, name, &value) == GRIB_SUCCESS)
ret = rb_float_new(value);
} else {
int shape[1];
struct NARRAY *nary;
shape[0] = len;
VALUE rnary = na_make_object(NA_DFLOAT, 1, shape, cNArray);
GetNArray(rnary, nary);
if (grib_get_double_array(message->handle, name, (double*)nary->ptr, &len) == GRIB_SUCCESS)
ret = rnary;
}
}
break;
default:
rb_raise(rb_eArgError, "type is invalid: %d", type);
}
return ret;
}
|
#get_xy ⇒ Object
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 |
# File 'lib/numru/grib/grib.rb', line 109 def get_xy lo, la, = get_data case gtype when "regular_ll", "regular_gg" lon = Hash.new lat = Hash.new if get_value("jPointsAreConsecutive") == 1 lon["ij"] = 1 lat["ij"] = 0 shape = [get_value("Nj"), get_value("Ni")] else lon["ij"] = 0 lat["ij"] = 1 shape = [get_value("Ni"), get_value("Nj")] end lo.reshape!(*shape) la.reshape!(*shape) lon["short_name"] = "lon" lon["long_name"] = "longitude" lon["units"] = "degrees_east" idx = [0,0]; idx[lon["ij"]] = true lon["value"] = lo[*idx] lat["short_name"] = "lat" lat["long_name"] = "latitude" lat["units"] = "degrees_north" idx = [0,0]; idx[lat["ij"]] = true lat["value"] = la[*idx] return [lon,lat] else raise "not defined yet: #{gtype}" end end |
#gtype ⇒ Object
103 104 105 |
# File 'lib/numru/grib/grib.rb', line 103 def gtype get_value("typeOfGrid") end |
#missing_value ⇒ Object
106 107 108 |
# File 'lib/numru/grib/grib.rb', line 106 def missing_value get_value("missingValue") end |
#name ⇒ Object
49 50 51 52 53 |
# File 'lib/numru/grib/grib.rb', line 49 def name n = get_value("name") return n if (n && n != "unknown") get_value("parameterName") || get_value("indicatorOfParameter") end |
#sname ⇒ Object
54 55 56 57 58 |
# File 'lib/numru/grib/grib.rb', line 54 def sname sn = get_value("shortName") return sn if (sn && sn != "unknown") "id" + (get_value("indicatorOfParameter", ::NumRu::Grib::TYPE_LONG) || get_value("parameterNumber", ::NumRu::Grib::TYPE_LONG)).to_s end |
#step ⇒ Object
85 86 87 |
# File 'lib/numru/grib/grib.rb', line 85 def step get_value("step") end |
#step_units ⇒ Object
91 92 93 |
# File 'lib/numru/grib/grib.rb', line 91 def step_units get_value("stepUnits") end |
#time_interval ⇒ Object
88 89 90 |
# File 'lib/numru/grib/grib.rb', line 88 def time_interval get_value("lengthOfTimeRange") end |
#units ⇒ Object Also known as: unit
59 60 61 |
# File 'lib/numru/grib/grib.rb', line 59 def units get_value("parameterUnits") || get_value("units") end |
#z_sname ⇒ Object
68 69 70 |
# File 'lib/numru/grib/grib.rb', line 68 def z_sname get_value("indicatorOfTypeOfLevel") || get_value("typeOfLevel") || "level" end |
#z_type ⇒ Object
63 64 65 66 67 |
# File 'lib/numru/grib/grib.rb', line 63 def z_type zt = get_value("typeOfLevel") return zt if zt && zt != "unknown" z_sname end |
#z_units ⇒ Object
74 75 76 |
# File 'lib/numru/grib/grib.rb', line 74 def z_units get_value("pressureUnits") end |
#z_value ⇒ Object
71 72 73 |
# File 'lib/numru/grib/grib.rb', line 71 def z_value get_value("levels") || get_value("level") end |