コード例 #1
0
def randomise_grib(filename):
    """Randomise the data in the given GRIB file."""
    in_file = open(filename, "rb")
    out_file = open("out.grib", "wb")

    while True:
        grib_message = gribapi.grib_new_from_file(in_file)
        if not grib_message:
            break

        values = gribapi.grib_get_double_array(grib_message, "values")
        values = numpy.random.rand(len(values))
        gribapi.grib_set_double_array(grib_message, "values", values)
        gribapi.grib_write(grib_message, out_file)

    os.remove(filename)
    os.rename("out.grib", filename)
コード例 #2
0
def write_lsm(gribfield_mod, input_path_oifs, output_path_oifs, exp_name_oifs,
              grid_name_oce, num_fields, gid):
    '''
    This function copies the input gribfile to the output folder and modifies
    it by writing the whole gribfield_mod, including the altered land sea mask
    and soil type fields into the new file
    '''

    input_file_oifs = input_path_oifs + 'ICMGG' + exp_name_oifs + 'INIT'
    output_file_oifs = output_path_oifs + 'ICMGG' + exp_name_oifs + 'INIT_' + grid_name_oce
    copy2(input_file_oifs, output_file_oifs)

    with open(output_file_oifs, 'r+') as f:
        for i in range(num_fields):
            gribapi.grib_set_values(gid[i], gribfield_mod[i])
            gribapi.grib_write(gid[i], f)
            gribapi.grib_release(gid[i])
コード例 #3
0
def randomise_grib(filename):
	"""Randomise the data in the given GRIB file."""
	in_file = open(filename, "rb")
	out_file = open("out.grib", "wb")
	
	while True:
		grib_message = gribapi.grib_new_from_file(in_file)
		if not grib_message:
			break
			
		values = gribapi.grib_get_double_array(grib_message, "values")
		values = numpy.random.rand(len(values))
		gribapi.grib_set_double_array(grib_message, "values", values)
		gribapi.grib_write(grib_message, out_file)
		
	os.remove(filename)
	os.rename("out.grib", filename)
コード例 #4
0
    def test_warn_unknown_pdts(self):
        # Test loading of an unrecognised GRIB Product Definition Template.

        # Get a temporary file by name (deleted afterward by context).
        with self.temp_filename() as temp_gribfile_path:
            # Write a test grib message to the temporary file.
            with open(temp_gribfile_path, 'wb') as temp_gribfile:
                grib_message = gribapi.grib_new_from_samples('GRIB2')
                # Set the PDT to something unexpected.
                gribapi.grib_set_long(grib_message,
                                      'productDefinitionTemplateNumber', 5)
                gribapi.grib_write(grib_message, temp_gribfile)

            # Load the message from the file as a cube.
            cube_generator = iris_grib.load_cubes(temp_gribfile_path)
            with self.assertRaises(iris.exceptions.TranslationError) as te:
                cube = next(cube_generator)
            self.assertEqual(
                'Product definition template [5]'
                ' is not supported', str(te.exception))
コード例 #5
0
    def test_warn_unknown_pdts(self):
        # Test loading of an unrecognised GRIB Product Definition Template.

        # Get a temporary file by name (deleted afterward by context).
        with self.temp_filename() as temp_gribfile_path:
            # Write a test grib message to the temporary file.
            with open(temp_gribfile_path, 'wb') as temp_gribfile:
                grib_message = gribapi.grib_new_from_samples('GRIB2')
                # Set the PDT to something unexpected.
                gribapi.grib_set_long(
                    grib_message, 'productDefinitionTemplateNumber', 5)
                gribapi.grib_write(grib_message, temp_gribfile)

            # Load the message from the file as a cube.
            cube_generator = iris.fileformats.grib.load_cubes(
                temp_gribfile_path)
            with self.assertRaises(iris.exceptions.TranslationError) as te:
                cube = next(cube_generator)
                self.assertEqual('Product definition template [5]'
                                 ' is not supported', str(te.exception))
コード例 #6
0
    def test_warn_unknown_pdts(self):
        # Test loading of an unrecognised GRIB Product Definition Template.

        # Get a temporary file by name (deleted afterward by context).
        with self.temp_filename() as temp_gribfile_path:
            # Write a test grib message to the temporary file.
            with open(temp_gribfile_path, 'wb') as temp_gribfile:
                grib_message = gribapi.grib_new_from_samples('GRIB2')
                # Set the PDT to something unexpected.
                gribapi.grib_set_long(grib_message,
                                      'productDefinitionTemplateNumber', 5)
                gribapi.grib_write(grib_message, temp_gribfile)

            # Load the message from the file as a cube.
            cube_generator = iris.fileformats.grib.load_cubes(
                temp_gribfile_path)
            cube = next(cube_generator)

            # Check the cube has an extra "warning" attribute.
            self.assertEqual(
                cube.attributes['GRIB_LOAD_WARNING'],
                'unsupported GRIB2 ProductDefinitionTemplate: #4.5')
コード例 #7
0
ファイル: __init__.py プロジェクト: m1dr/iris-grib
def save_messages(messages, target, append=False):
    """
    Save messages to a GRIB2 file.
    The messages will be released as part of the save.

    Args:

    * messages:
        An iterable of grib_api message IDs.
    * target:
        A filename or open file handle.

    Kwargs:

    * append:
        Whether to start a new file afresh or add the cube(s) to the end of
        the file. Only applicable when target is a filename, not a file
        handle. Default is False.

    """
    # grib file (this bit is common to the pp and grib savers...)
    if isinstance(target, str):
        grib_file = open(target, "ab" if append else "wb")
    elif hasattr(target, "write"):
        if hasattr(target, "mode") and "b" not in target.mode:
            raise ValueError("Target not binary")
        grib_file = target
    else:
        raise ValueError("Can only save grib to filename or writable")

    try:
        for message in messages:
            gribapi.grib_write(message, grib_file)
            gribapi.grib_release(message)
    finally:
        # (this bit is common to the pp and grib savers...)
        if isinstance(target, str):
            grib_file.close()
コード例 #8
0
ファイル: __init__.py プロジェクト: cpelley/iris
def save_messages(messages, target, append=False):
    """
    Save messages to a GRIB2 file.
    The messages will be released as part of the save.

    Args:

    * messages:
        An iterable of grib_api message IDs.
    * target:
        A filename or open file handle.

    Kwargs:

    * append:
        Whether to start a new file afresh or add the cube(s) to the end of
        the file. Only applicable when target is a filename, not a file
        handle. Default is False.

    """
    # grib file (this bit is common to the pp and grib savers...)
    if isinstance(target, six.string_types):
        grib_file = open(target, "ab" if append else "wb")
    elif hasattr(target, "write"):
        if hasattr(target, "mode") and "b" not in target.mode:
            raise ValueError("Target not binary")
        grib_file = target
    else:
        raise ValueError("Can only save grib to filename or writable")

    try:
        for message in messages:
            gribapi.grib_write(message, grib_file)
            gribapi.grib_release(message)
    finally:
        # (this bit is common to the pp and grib savers...)
        if isinstance(target, six.string_types):
            grib_file.close()
コード例 #9
0
ファイル: test_grib_load.py プロジェクト: benbovy/iris
    def test_warn_unknown_pdts(self):
        # Test loading of an unrecognised GRIB Product Definition Template.

        # Get a temporary file by name (deleted afterward by context).
        with self.temp_filename() as temp_gribfile_path:
            # Write a test grib message to the temporary file.
            with open(temp_gribfile_path, 'wb') as temp_gribfile:
                grib_message = gribapi.grib_new_from_samples('GRIB2')
                # Set the PDT to something unexpected.
                gribapi.grib_set_long(
                    grib_message, 'productDefinitionTemplateNumber', 5)
                gribapi.grib_write(grib_message, temp_gribfile)

            # Load the message from the file as a cube.
            cube_generator = iris.fileformats.grib.load_cubes(
                temp_gribfile_path)
            cube = cube_generator.next()

            # Check the cube has an extra "warning" attribute.
            self.assertEqual(
                cube.attributes['GRIB_LOAD_WARNING'],
                'unsupported GRIB2 ProductDefinitionTemplate: #4.5'
            )
コード例 #10
0
ファイル: grib_file.py プロジェクト: EC-Earth/ece2cmor3
 def write(self, file_object_):
     gribapi.grib_write(self.record, file_object_)
コード例 #11
0
ファイル: gribmessage.py プロジェクト: nourou6/PythonicGRIB
 def write(self, outfile=None):
     """Write message to file."""
     if not outfile:
         # This is a hack because the API does not accept inheritance
         outfile = self.grib_file.file_handle
     gribapi.grib_write(self.gid, outfile)
コード例 #12
0
ファイル: griblib.py プロジェクト: akleeman/slocum
def save(source, target, append=False, sample_file=_sample_file):
    """
    Takes a dataset (source) and writes its contents
    as grib 1 to file-like target.  Grib 1 is used (instead
    of grib 2) because some older forecast visualization
    software can't read grib 2.

    This is a heavily modified but none-the-less derivative of
    the grib saving functions from the iris package.

    Parameters
    ----------
    source : Dataset
        A netcdf-like file holding the dataset we want to write
        as grib.  This must contain time, longitude and latitude
        coordinates in order to infer the grib grid and time params
    target : string path or file-like
        Where the contents should be written.  If target is a string
        the file is created or appended to.
    append : boolean
        When creating a new file from string you can optionally
        append to the file.
    """
    if not _has_gribapi:
        raise ImportError("gripapi is required to write grib files.")

    if isinstance(target, basestring):
        grib_file = open(target, "ab" if append else "wb")
    elif hasattr(target, "write"):
        if hasattr(target, "mode") and "b" not in target.mode:
            raise ValueError("Target not binary")
        grib_file = target
    else:
        raise ValueError("Can only save grib to filename or writable")
    if not 'latitude' in source.variables or not 'longitude' in source.variables:
        raise ValueError("Did not find either latitude or longitude.")
    if source['latitude'].ndim != 1 or source['longitude'].ndim != 1:
        raise ValueError("Latitude and Longitude should be regular.")
    if not 'time' in source.variables:
        raise ValueError("Expected time coordinate")
    # sort the lats and lons
    source = source.indexed(latitude=np.argsort(source['latitude'].values))
    lons = source['longitude'].values
    if np.any(np.abs(np.diff(lons)) > 180.):
        # the latitudes must cross the dateline since we only allow 180
        # degree wide bounding boxes, and there is more than a 180 degree
        # difference between longitudes.  Instead we try converting to
        # 0 to 360 degree longitudes before sorting.
        lons = np.mod(lons, 360)
        if np.any(np.abs(np.diff(lons)) > 180.):
            # TODO: I'm sure theres a way to deal with arbitrary longitude
            # specifications for global data ... but its not a high priority
            # so that will wait for later.
            raise ValueError("Longitudes span more than 180 degrees and the dateline?")
    source['longitude'].values[:] = lons
    source = source.indexed(longitude=np.argsort(lons))
    # iterate over variables, unless they are considered
    # auxiliary variables (ie, variables used by slocum
    # but not in grib files).
    auxilary_variables = ['wind_speed', 'wind_from_direction']
    for single_var in (v for k, v in source.noncoordinates.iteritems()
                       if not k in auxilary_variables):
        # then iterate over time slices
        iter_time = (single_var.indexed(**{'time': [i]})
                     for i in range(single_var.coordinates['time'].size))
        for obj in iter_time:
            # Save this slice to the grib file
            gribapi.grib_gribex_mode_off()
            if sample_file is not None and os.path.exists(sample_file):
                with open(sample_file, 'r') as f:
                    grib_message = gribapi.grib_new_from_file(f)
                logger.info("Created grib message from file %s" % sample_file)
            else:
                logger.info("Creating grib message from gribapi sample: GRIB1")
                grib_message = gribapi.grib_new_from_samples("GRIB1")
            set_time(obj, grib_message)
            set_product(obj, grib_message)
            set_grid(obj, grib_message)
            set_data(obj, grib_message)
            gribapi.grib_write(grib_message, grib_file)
            gribapi.grib_release(grib_message)
    # if target was a string then we have to close the file we
    # created, otherwise leave that up to the user.
    if isinstance(target, basestring):
        grib_file.close()
コード例 #13
0
ファイル: gribmessage.py プロジェクト: erget/PythonicGRIB
 def write(self, outfile=None):
     """Write message to file."""
     if not outfile:
         # This is a hack because the API does not accept inheritance
         outfile = self.grib_file.file_handle
     gribapi.grib_write(self.gid, outfile)
コード例 #14
0
def write_record(msgid, files):
    gribapi.grib_write(msgid, files[0])