From afa06548efd5d2e83447b5d85f890c70f0da1f3f Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 22 Dec 2016 14:08:28 +0000 Subject: [PATCH] Added Python function codes_new_from_samples which takes the product kind --- examples/python/bufr_copy_data.py | 2 +- python/eccodes/eccodes.py | 1 + python/gribapi/gribapi.py | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/examples/python/bufr_copy_data.py b/examples/python/bufr_copy_data.py index 6435dea08..1f4a3b612 100644 --- a/examples/python/bufr_copy_data.py +++ b/examples/python/bufr_copy_data.py @@ -20,7 +20,7 @@ from eccodes import * VERBOSE = 1 # verbose error reporting def example(input_filename, output_filename): - ibufr = codes_bufr_new_from_samples('BUFR3') + ibufr = codes_new_from_samples('BUFR3', CODES_PRODUCT_BUFR) f = open(input_filename) ibufrin = codes_bufr_new_from_file(f) ivalues=( diff --git a/python/eccodes/eccodes.py b/python/eccodes/eccodes.py index 758da913c..aa83611d2 100644 --- a/python/eccodes/eccodes.py +++ b/python/eccodes/eccodes.py @@ -48,6 +48,7 @@ from gribapi import grib_set_long as codes_set_long from gribapi import grib_set_double as codes_set_double from gribapi import grib_new_from_samples as codes_grib_new_from_samples from gribapi import codes_bufr_new_from_samples +from gribapi import codes_new_from_samples from gribapi import codes_bufr_copy_data from gribapi import grib_clone as codes_clone from gribapi import grib_set_double_array as codes_set_double_array diff --git a/python/gribapi/gribapi.py b/python/gribapi/gribapi.py index d559587f2..c59104bb2 100644 --- a/python/gribapi/gribapi.py +++ b/python/gribapi/gribapi.py @@ -763,6 +763,29 @@ def grib_set_double(msgid, key, value): GRIB_CHECK(_internal.grib_c_set_double(msgid, key, value)) +@require(samplename=str, product_kind=int) +def codes_new_from_samples(samplename, product_kind): + """ + @brief Create a new valid message from a sample for a given product. + + The available samples are picked up from the directory pointed to + by the environment variable ECCODES_SAMPLES_PATH. + To know where the samples directory is run the codes_info tool.\n + + \b Examples: \ref grib_samples.py "grib_samples.py" + + @param samplename name of the sample to be used + @param product_kind CODES_PRODUCT_GRIB or CODES_PRODUCT_BUFR + @return id of the message loaded in memory + @exception GribInternalError + """ + if product_kind == CODES_PRODUCT_GRIB: + return grib_new_from_samples(samplename) + if product_kind == CODES_PRODUCT_BUFR: + return codes_bufr_new_from_samples(samplename) + raise Exception("Invalid product kind: " + product_kind) + + @require(samplename=str) def grib_new_from_samples(samplename): """ @@ -792,7 +815,7 @@ def codes_bufr_new_from_samples(samplename): by the environment variable ECCODES_SAMPLES_PATH. To know where the samples directory is run the codes_info tool.\n - \b Examples: \ref grib_samples.py "grib_samples.py" + \b Examples: \ref bufr_copy_data.py "bufr_copy_data.py" @param samplename name of the BUFR sample to be used @return id of the message loaded in memory