Example #1
0
def test_grib_nearest_multiple():
    gid = eccodes.codes_new_from_samples("reduced_gg_ml_grib2",
                                         eccodes.CODES_PRODUCT_GRIB)
    inlats = (30, 13)
    inlons = (-20, 234)
    is_lsm = False
    nearest = eccodes.codes_grib_find_nearest_multiple(gid, is_lsm, inlats,
                                                       inlons)
    eccodes.codes_release(gid)
    assert nearest[0].index == 1770
    assert nearest[1].index == 2500
Example #2
0
def test_bufr_read_write(tmpdir):
    bid = eccodes.codes_new_from_samples("BUFR4", eccodes.CODES_PRODUCT_BUFR)
    eccodes.codes_set(bid, "unpack", 1)
    assert eccodes.codes_get(bid, "typicalYear") == 2012
    assert eccodes.codes_get(bid, "centre", str) == "ecmf"
    eccodes.codes_set(bid, "totalSunshine", 13)
    eccodes.codes_set(bid, "pack", 1)
    output = tmpdir.join("test_bufr_write.bufr")
    with open(str(output), "wb") as fout:
        eccodes.codes_write(bid, fout)
    assert eccodes.codes_get(bid, "totalSunshine") == 13
    eccodes.codes_release(bid)
Example #3
0
    def __init__(
        self,
        codes_file=None,
        clone=None,
        sample=None,
        headers_only=False,
        other_args_found=False,
    ):
        """
        Open a message and inform the host file that it's been incremented.

        If ``codes_file`` is not supplied, the message is cloned from
        ``CodesMessage`` ``clone``. If neither is supplied,
        the ``CodesMessage`` is cloned from ``sample``.

        :param codes_file: A file readable for ecCodes
        :param clone: A valid ``CodesMessage``
        :param sample: A valid sample path to create ``CodesMessage`` from
        """
        if (not other_args_found and codes_file is None and clone is None
                and sample is None):
            raise RuntimeError("CodesMessage initialization parameters not "
                               "present.")
        #: Unique ID, for ecCodes interface
        self.codes_id = None
        #: File containing message
        self.codes_file = None
        if codes_file is not None:
            self.codes_id = eccodes.codes_new_from_file(
                codes_file.file_handle, self.product_kind, headers_only)
            if self.codes_id is None:
                raise IOError("CodesFile %s is exhausted" % codes_file.name)
            self.codes_file = codes_file
            self.codes_file.message += 1
            self.codes_file.open_messages.append(self)
        elif clone is not None:
            self.codes_id = eccodes.codes_clone(clone.codes_id)
        elif sample is not None:
            self.codes_id = eccodes.codes_new_from_samples(
                sample, self.product_kind)
Example #4
0
 def from_sample_name(cls, sample_name, **kwargs):
     # type: (str, T.Any) -> Message
     codes_id = eccodes.codes_new_from_samples(sample_name, eccodes.CODES_PRODUCT_GRIB)
     return cls(codes_id=codes_id, **kwargs)
Example #5
0
 def from_sample_name(cls,
                      sample_name,
                      product_kind=eccodes.CODES_PRODUCT_GRIB,
                      **kwargs):
     codes_id = eccodes.codes_new_from_samples(sample_name, product_kind)
     return cls(codes_id=codes_id, **kwargs)
Example #6
0
def test_bufr_new_from_samples_error():
    with pytest.raises(eccodes.FileNotFoundError):
        eccodes.codes_new_from_samples("nonExistentSample",
                                       eccodes.CODES_PRODUCT_BUFR)
    with pytest.raises(ValueError):
        eccodes.codes_new_from_samples("BUFR3_local", 1024)
Example #7
0
def test_grib_new_from_samples_error():
    with pytest.raises(eccodes.FileNotFoundError):
        eccodes.codes_new_from_samples("poopoo", eccodes.CODES_PRODUCT_GRIB)