Exemple #1
0
def test_grib_set_error():
    gid = eccodes.codes_grib_new_from_samples("regular_ll_sfc_grib1")
    with pytest.raises(TypeError):
        eccodes.codes_set_long(gid, "centre", "kwbc")
    with pytest.raises(TypeError):
        eccodes.codes_set_double(gid, "centre", "kwbc")
    with pytest.raises(eccodes.CodesInternalError):
        eccodes.codes_set(gid, "centre", [])
Exemple #2
0
 def __setitem__(self, k: str, v: Union[int, float, str]):
     try:
         if isinstance(v, int):
             eccodes.codes_set_long(self.gid, k, v)
         elif isinstance(v, float):
             eccodes.codes_set_double(self.gid, k, v)
         elif isinstance(v, str):
             eccodes.codes_set_string(self.gid, k, v)
         else:
             raise RuntimeError(
                 f"Cannot set {k}={v!r} (of type {type(v).__name__} in GRIB file"
             )
     except eccodes.KeyValueNotFoundError as e:
         raise RuntimeError(
             f"Cannot set {k}={v!r} (of type {type(v).__name__} in GRIB file: {e}"
         )
Exemple #3
0
def interpolate_grid(
    message,
    latitude,
    longitude,
    scheme: str = "linear",
    engine: str = "scipy",
    **kwargs: Dict,
):
    """

    Parameters
    ----------
    message
    latitude
    longitude
        interpolate method.
    scheme
    engine
        interpolate engine, `scipy` or `xarray`
    **kwargs

    Returns
    -------

    """
    target_grid = xr.DataArray(coords=[("latitude",
                                        latitude), ("longitude", longitude)])

    field = create_data_array_from_message(message)

    target_field = interpolate_grid_field(data=field,
                                          target=target_grid,
                                          scheme=scheme,
                                          engine=engine,
                                          **kwargs)

    eccodes.codes_set_double(message, 'longitudeOfFirstGridPointInDegrees',
                             target_field.longitude.values[0])
    eccodes.codes_set_double(message, 'longitudeOfLastGridPointInDegrees',
                             target_field.longitude.values[-1])
    # eccodes.codes_set_double(message, 'iDirectionIncrementInDegrees', 0.28125)
    eccodes.codes_set_long(message, 'Ni', len(target_field.longitude.values))

    eccodes.codes_set_double(message, 'latitudeOfFirstGridPointInDegrees',
                             target_field.latitude.values[0])
    eccodes.codes_set_double(message, 'latitudeOfLastGridPointInDegrees',
                             target_field.latitude.values[-1])
    # eccodes.codes_set_double(message, 'jDirectionIncrementInDegrees', 0.28125)
    eccodes.codes_set_long(message, 'Nj', len(target_field.latitude.values))

    eccodes.codes_set_double_array(message, "values",
                                   target_field.values.flatten())

    del field
    del target_field

    return message
Exemple #4
0
def extract_region(
    message,
    start_longitude: Union[float, int],
    end_longitude: Union[float, int],
    start_latitude: Union[float, int],
    end_latitude: Union[float, int],
):
    """
    extract region from gridded data array.

    Parameters
    ----------
    message
    start_longitude
    end_longitude
    start_latitude
    end_latitude

    Returns
    -------
    """
    field = create_data_array_from_message(message)
    target_field = extract_region_field(field,
                                        start_longitude=start_longitude,
                                        end_longitude=end_longitude,
                                        start_latitude=start_latitude,
                                        end_latitude=end_latitude)

    eccodes.codes_set_double(message, 'longitudeOfFirstGridPointInDegrees',
                             target_field.longitude.values[0])
    eccodes.codes_set_double(message, 'longitudeOfLastGridPointInDegrees',
                             target_field.longitude.values[-1])
    # eccodes.codes_set_double(message, 'iDirectionIncrementInDegrees', self.grid.lon_step)
    eccodes.codes_set_long(message, 'Ni', len(target_field.longitude.values))

    eccodes.codes_set_double(message, 'latitudeOfFirstGridPointInDegrees',
                             target_field.latitude.values[0])
    eccodes.codes_set_double(message, 'latitudeOfLastGridPointInDegrees',
                             target_field.latitude.values[-1])
    # eccodes.codes_set_double(message, 'jDirectionIncrementInDegrees', self.grid.lat_step)
    eccodes.codes_set_long(message, 'Nj', len(target_field.latitude.values))

    eccodes.codes_set_double_array(message, "values",
                                   target_field.values.flatten())

    del field
    del target_field

    return message
Exemple #5
0
 def set_double(self, key, value):
     eccodes.codes_set_double(self.handle, key, value)