from pybufr_ecmwf.bufr_table import CompositeDescriptor
from pybufr_ecmwf.bufr_table import ExtendedDelayedRepeater as EDR2
from pybufr_ecmwf.bufr_table import BufrTable
from pybufr_ecmwf.bufr_template import BufrTemplate
from pybufr_ecmwf.bufr_interface_ecmwf import BUFRInterfaceECMWF
from pybufr_ecmwf.raw_bufr_file import RawBUFRFile
from pybufr_ecmwf.bufr import BUFRReader

# varname, reference, name, unit, unit_scale, unit_reference, data_width
B_entries = [
    ('dummy1', '063251', 'DUMMY VARIABLE', 'DUMMY UNIT', 0, 0, 25),
    ('extdelrep', '031012', 'DELAYED DESCRIPTOR AND DATA REPETITION FACTOR',
     'NUMERIC', 0, 0, 16),
    ]

bufr_table_set = BufrTable()

# import global namespace __main__ as g
import __main__ as g
for (varname, reference, name, unit, unit_scale,
     unit_reference, data_width) in B_entries:
    reference_nr = int(reference, 10)
    setattr(g, varname, Descriptor(reference_nr, name, unit, unit_scale,
                                   unit_reference, data_width))
    Bdescr = getattr(g, varname)
    bufr_table_set.add_to_B_table(Bdescr)

# compose the D table entry (the actual template to be used)
# EDR2 = ExtendedDelayedRepeater

# reference, descriptor_list, comment
#!/usr/bin/env python

'''a simple example script showing how to retrieve a descriptor code
when you only know (part of) a description name'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

from __future__ import print_function
from pybufr_ecmwf.bufr_table import BufrTable

BTABLE = 'pybufr_ecmwf/ecmwf_bufrtables/B2550000000098006001.TXT'
SEARCH_STRING = 'WMO'

BT = BufrTable()
BT.load(BTABLE)

print('seaching for descriptors that contain substring: ', SEARCH_STRING)

KEYS = BT.table_b.keys()
for k in sorted(KEYS):
    obj = BT.get_descr_object(k)
    if SEARCH_STRING in obj.name:
        # this is not python 2.6 compatible
        #print('descriptor: {:06d} name: {}'.format(k, obj.name))
        # so use this in stead
        print('descriptor: %06d name: %s' % (k, obj.name))
Example #3
0
from pybufr_ecmwf.bufr_table import ExtendedDelayedReplicator as EDR1
from pybufr_ecmwf.bufr_table import BufrTable
from pybufr_ecmwf.bufr_template import BufrTemplate
from pybufr_ecmwf.bufr_interface_ecmwf import BUFRInterfaceECMWF
from pybufr_ecmwf.raw_bufr_file import RawBUFRFile
from pybufr_ecmwf.bufr import BUFRReader

# varname, reference, name, unit, unit_scale, unit_reference, data_width
B_entries = [
    ('dummy1', '063251', 'DUMMY VARIABLE', 'DUMMY UNIT', 0, 0, 25),
    ('dummy2', '063252', 'DUMMY VARIABLE', 'DUMMY UNIT', 0, 0, 25),
    ('extdelrepl', '031002', 'EXTENDED DELAYED DESCRIPTOR REPLICATION FACTOR',
     'NUMERIC', 0, 0, 16),
]

bufr_table_set = BufrTable()

# import global namespace __main__ as g
import __main__ as g
for (varname, reference, name, unit, unit_scale, unit_reference,
     data_width) in B_entries:
    reference_nr = int(reference, 10)
    setattr(
        g, varname,
        Descriptor(reference_nr, name, unit, unit_scale, unit_reference,
                   data_width))
    Bdescr = getattr(g, varname)
    bufr_table_set.add_to_B_table(Bdescr)

# compose the D table entry (the actual template to be used)
# EDR1 = ExtendedDelayedReplicator
Example #4
0
#!/usr/bin/env python
'''a simple example script showing how to retrieve a descriptor code
when you only know (part of) a description name'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

from __future__ import print_function
from pybufr_ecmwf.bufr_table import BufrTable

BTABLE = 'pybufr_ecmwf/ecmwf_bufrtables/B2550000000098006001.TXT'
SEARCH_STRING = 'WMO'

BT = BufrTable()
BT.load(BTABLE)

print('seaching for descriptors that contain substring: ', SEARCH_STRING)

KEYS = BT.table_b.keys()
for k in sorted(KEYS):
    obj = BT.get_descr_object(k)
    if SEARCH_STRING in obj.name:
        # this is not python 2.6 compatible
        #print('descriptor: {:06d} name: {}'.format(k, obj.name))
        # so use this in stead
        print('descriptor: %06d name: %s' % (k, obj.name))
#!/usr/bin/env python

'''a simple example script showing how to retrieve a description name
from a descroptor code.'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

from __future__ import print_function
from pybufr_ecmwf.bufr_table import BufrTable

BT = BufrTable()
BTABLE = 'pybufr_ecmwf/ecmwf_bufrtables/B2550000000098006001.TXT'
BT.load(BTABLE)

OBJ = BT.get_descr_object(int('001001', 10))
print('OBJ: ', OBJ)
print('OBJ.name: ', OBJ.name)
#!/usr/bin/env python
'''a simple example script showing how to retrieve a description name
from a descroptor code.'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

from __future__ import print_function
from pybufr_ecmwf.bufr_table import BufrTable

BT = BufrTable()
BTABLE = 'pybufr_ecmwf/ecmwf_bufrtables/B2550000000098006001.TXT'
BT.load(BTABLE)

OBJ = BT.get_descr_object(int('001001', 10))
print('OBJ: ', OBJ)
print('OBJ.name: ', OBJ.name)
def create_bufr_tables():
    #  #[
    """
    a small function to hold the actual code to create a set of BUFR tables
    """

    # bt1 = BufrTable(autolink_tablesdir = "tmp_BUFR_TABLES")
    bt1 = BufrTable()

    # explanation on the reference number:
    # F=0 indicates this is a B-table entry
    # XX indicates the class or category within the table.
    # Private/local definitions should always be between 48 and 63
    # to avoid clashing with standard WMO descriptors
    # (see WMO_BUFR_Guide_Layer3-English-only section 3.1.2.1)
    # YYY should be in the range 000 to 255, but if a standard WMO class
    # is used it should be between 192 and 255 for local use.

    #            FXXYYY
    reference = '048001'
    name = 'my test variable A' # max. 64 characters
    unit = 'm/s' # SI units are recommended here. max.
    unit_scale = 3 # the power of 10 by which the element has to be
    #              # multiplied prior to encoding, or divided after
    #              # decoding.
    unit_reference = 1250 # the number to be subtracted from the element, after
    #                       scaling (if any) and prior to encoding (or added
    #                       after decoding)
    data_width = 4 # the number of bits used to store the data, which will
    #                always be converted to an integer (using the above
    #                scaling and reference subtraction)

    # NOTE: on linux the ECMWF BUFR library is always compiled using 32-bits
    # integers, so the max. data_width is 32 and the maximum unit_reference
    # is 2147483647 (2^31-1)

    descr_048001 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048001)

    #                 FXXYYY
    reference = '048002'
    name = 'my test variable B' # max. 64 characters
    unit = 'kg/m' # SI units are recommended here. max.
    unit_scale = 12
    unit_reference = 3456
    data_width = 5
    descr_048002 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048002)

    #            FXXYYY
    reference = '048003'
    name = 'a dummy counter' # max. 64 characters
    unit = 'FLAG TABLE 048003'
    unit_scale = 0
    unit_reference = 0
    data_width = 10
    descr_048003 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048003)

    #            FXXYYY
    reference = '048003'
    fldef = FlagDefinition(reference)
    fldef.flag_dict[0] = 'zero'
    fldef.flag_dict[1] = 'one'
    fldef.flag_dict[2] = 'two'
    fldef.flag_dict[3] = 'a very long line to describe the number 3; '*10
    fldef.flag_dict[4] = 'four'
    fldef.flag_dict[999] = 'missing'
    bt1.table_c[int(reference)] = fldef

    reference = '348001'
    descriptor_list = [descr_048001, descr_048002]
    comment = 'a small test D entry' # not written to file
    bufr_table_set = bt1
    descr_348001 = CompositeDescriptor(reference, descriptor_list,
                                       comment, bufr_table_set)

    bt1.add_to_D_table(descr_348001)

    print '='*50
    print "B-table:"
    print '='*50
    bt1.print_B_table()
    print '='*50
    print "C-table:"
    print '='*50
    bt1.print_C_table()
    print '='*50
    print "D-table:"
    print '='*50
    bt1.print_D_table()
    print '='*50

    # define the table name without preceding 'B' or 'D' character
    # (which will be prepended by the below write method)
    table_name = '_my_test_BUFR_table.txt'
    bt1.write_tables(table_name)
def create_bufr_tables(table_name):
    #  #[ create the tables
    '''
    compose the BUFR B and D tables
    '''

    # some dummy initialisations to prevent pylint from complaining
    # about undefined variables
    year, month, day = 0, 0, 0
    product_name, lat, lon = '', 0, 0
    variable1, variable2 = 0, 0

    bufr_table_set = BufrTable()

    # varname, reference, name, unit, unit_scale, unit_reference, data_width
    b_entries = [
        ('year', '004001', 'YEAR', 'YEAR', 0, 0, 12),
        ('month', '004002', 'MONTH', 'MONTH', 0, 0, 4),
        ('day', '004003', 'DAY', 'DAY', 0, 0, 6),
        ('lat', '005001', 'LATITUDE (HIGH ACCURACY)',
         'DEGREE', 5, -9000000, 25),
        ('lon', '006001', 'LONGITUDE (HIGH ACCURACY)',
         'DEGREE', 5, -18000000, 26),
        ('variable1', '048001', 'SOME VARIABLE', 'M', 0, 0, 13),
        ('variable2', '048002', 'SOME OTHER VARIABLE', 'S', 2, 500, 13),
        ('product_name', '048003', 'PRODUCT NAME', 'CCITT IA5', 0, 0, 64*8),
        ('variable3', '048004', 'REPL. VARIABLE', 'KG', 2, -1000, 13),
        ('delrepl', '031001', 'DELAYED DESCRIPTOR REPLICATION FACTOR',
         'NUMERIC', 0, 0, 8),
        ]

    for (varname, reference, name, unit, unit_scale,
         unit_reference, data_width) in b_entries:
        reference_nr = int(reference, 10)
        setattr(g, varname, Descriptor(reference_nr, name, unit, unit_scale,
                                       unit_reference, data_width))
        b_descr = getattr(g, varname)
        bufr_table_set.add_to_B_table(b_descr)

    # reference, descriptor_list, comment
    # note: codes 348001 and above don't work as expected. No idea why.
    d_entries = [
        ('301192', [year, month, day,      # reference date
                    product_name],         # product name
         "header information"),
        ('301193', [lat,        # Latitude
                    lon,        # Longitude
                    variable1,  # first variable
                    Replicator(3, [variable2,]), # second variable, 3x repeated
                   ],
         "measurement information"),
        ]

    for (reference, descriptor_list, comment) in d_entries:
        varname = 'D_'+reference
        setattr(g, varname, CompositeDescriptor(reference, descriptor_list,
                                                comment, bufr_table_set))
        d_descr = getattr(g, varname)
        bufr_table_set.add_to_D_table(d_descr)

    print '='*50
    print "B-table:"
    print '='*50
    bufr_table_set.print_B_table()
    print '='*50
    print "D-table:"
    print '='*50
    bufr_table_set.print_D_table()
    print '='*50

    # define the table name without preceding 'B' or 'D' character
    # (which will be prepended by the below write method)
    bufr_table_set.write_tables(table_name)
Example #9
0
BUFR tables, and write them to the ECMWF specific format.
'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

import os  # , csv
from pybufr_ecmwf.bufr_table import BufrTable

# tested with files from the BUFRCREX_21_0_0.zip package
# downloaded from the WMO website.
# see:
# http://www.wmo.int/pages/prog/www/WMOCodes/WMO306_vI2/LatestVERSION/LatestVERSION.html
# and
# http://www.wmo.int/pages/prog/www/WMOCodes/WMO306_vI2/PrevVERSIONS/PreviousVERSIONS.html

WMO_TABLEDIR = 'BUFRCREX_21_0_0'
WMO_B_FILE = 'BUFRCREX_21_0_0_TableB_en.txt'
WMO_D_FILE = 'BUFR_21_0_0_TableD_en.txt'

SET_OF_BUFR_TABLES = BufrTable()

FILENAME = os.path.join(WMO_TABLEDIR, WMO_B_FILE)
SET_OF_BUFR_TABLES.read_WMO_csv_table_b(FILENAME)

FILENAME = os.path.join(WMO_TABLEDIR, WMO_D_FILE)
SET_OF_BUFR_TABLES.read_WMO_csv_table_d(FILENAME)

SET_OF_BUFR_TABLES.write_tables('_WMO_BUFR_TABLE.txt')
Example #10
0
def create_bufr_tables(table_name):
    #  #[ create the tables
    '''
    compose the BUFR B and D tables
    '''

    # some dummy initialisations to prevent pylint from complaining
    # about undefined variables
    year, month, day = 0, 0, 0
    product_name, lat, lon = '', 0, 0
    variable1, variable2 = 0, 0

    bufr_table_set = BufrTable()

    # varname, reference, name, unit, unit_scale, unit_reference, data_width
    b_entries = [
        ('year', '004001', 'YEAR', 'YEAR', 0, 0, 12),
        ('month', '004002', 'MONTH', 'MONTH', 0, 0, 4),
        ('day', '004003', 'DAY', 'DAY', 0, 0, 6),
        ('lat', '005001', 'LATITUDE (HIGH ACCURACY)',
         'DEGREE', 5, -9000000, 25),
        ('lon', '006001', 'LONGITUDE (HIGH ACCURACY)',
         'DEGREE', 5, -18000000, 26),
        ('variable1', '048001', 'SOME VARIABLE', 'M', 0, 0, 13),
        ('variable2', '048002', 'SOME OTHER VARIABLE', 'S', 2, 500, 13),
        ('product_name', '048003', 'PRODUCT NAME', 'CCITT IA5', 0, 0, 64*8),
        ('variable3', '048004', 'REPL. VARIABLE', 'KG', 2, -1000, 13),
        ('delrepl', '031001', 'DELAYED DESCRIPTOR REPLICATION FACTOR',
         'NUMERIC', 0, 0, 8),
        ]

    for (varname, reference, name, unit, unit_scale,
         unit_reference, data_width) in b_entries:
        reference_nr = int(reference, 10)
        setattr(g, varname, Descriptor(reference_nr, name, unit, unit_scale,
                                       unit_reference, data_width))
        b_descr = getattr(g, varname)
        bufr_table_set.add_to_B_table(b_descr)

    # reference, descriptor_list, comment
    # note: codes 348001 and above don't work as expected. No idea why.
    d_entries = [
        ('301192', [year, month, day,      # reference date
                    product_name],         # product name
         "header information"),
        ('301193', [lat,        # Latitude
                    lon,        # Longitude
                    variable1,  # first variable
                    Replicator(3, [variable2,]), # second variable, 3x repeated
                   ],
         "measurement information"),
        ]

    for (reference, descriptor_list, comment) in d_entries:
        varname = 'D_'+reference
        setattr(g, varname, CompositeDescriptor(reference, descriptor_list,
                                                comment, bufr_table_set))
        d_descr = getattr(g, varname)
        bufr_table_set.add_to_D_table(d_descr)

    print '='*50
    print "B-table:"
    print '='*50
    bufr_table_set.print_B_table()
    print '='*50
    print "D-table:"
    print '='*50
    bufr_table_set.print_D_table()
    print '='*50

    # define the table name without preceding 'B' or 'D' character
    # (which will be prepended by the below write method)
    bufr_table_set.write_tables(table_name)
Example #11
0
BUFR tables, and write them to the ECMWF specific format.
'''

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

import os # , csv
from pybufr_ecmwf.bufr_table import BufrTable

# tested with files from the BUFRCREX_21_0_0.zip package
# downloaded from the WMO website.
# see:
# http://www.wmo.int/pages/prog/www/WMOCodes/WMO306_vI2/LatestVERSION/LatestVERSION.html
# and
# http://www.wmo.int/pages/prog/www/WMOCodes/WMO306_vI2/PrevVERSIONS/PreviousVERSIONS.html

WMO_TABLEDIR = 'BUFRCREX_21_0_0'
WMO_B_FILE = 'BUFRCREX_21_0_0_TableB_en.txt'
WMO_D_FILE = 'BUFR_21_0_0_TableD_en.txt'

SET_OF_BUFR_TABLES = BufrTable()

FILENAME = os.path.join(WMO_TABLEDIR, WMO_B_FILE)
SET_OF_BUFR_TABLES.read_WMO_csv_table_b(FILENAME)

FILENAME = os.path.join(WMO_TABLEDIR, WMO_D_FILE)
SET_OF_BUFR_TABLES.read_WMO_csv_table_d(FILENAME)

SET_OF_BUFR_TABLES.write_tables('_WMO_BUFR_TABLE.txt')
Example #12
0
def create_bufr_tables():
    #  #[
    """
    a small function to hold the actual code to create a set of BUFR tables
    """

    # bt1 = BufrTable(autolink_tablesdir = "tmp_BUFR_TABLES")
    bt1 = BufrTable()

    # explanation on the reference number:
    # F=0 indicates this is a B-table entry
    # XX indicates the class or category within the table.
    # Private/local definitions should always be between 48 and 63
    # to avoid clashing with standard WMO descriptors
    # (see WMO_BUFR_Guide_Layer3-English-only section 3.1.2.1)
    # YYY should be in the range 000 to 255, but if a standard WMO class
    # is used it should be between 192 and 255 for local use.

    #            FXXYYY
    reference = '048001'
    name = 'my test variable A' # max. 64 characters
    unit = 'm/s' # SI units are recommended here. max.
    unit_scale = 3 # the power of 10 by which the element has to be
    #              # multiplied prior to encoding, or divided after
    #              # decoding.
    unit_reference = 1250 # the number to be subtracted from the element, after
    #                       scaling (if any) and prior to encoding (or added
    #                       after decoding)
    data_width = 4 # the number of bits used to store the data, which will
    #                always be converted to an integer (using the above
    #                scaling and reference subtraction)

    # NOTE: on linux the ECMWF BUFR library is always compiled using 32-bits
    # integers, so the max. data_width is 32 and the maximum unit_reference
    # is 2147483647 (2^31-1)

    descr_048001 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048001)

    #                 FXXYYY
    reference = '048002'
    name = 'my test variable B' # max. 64 characters
    unit = 'kg/m' # SI units are recommended here. max.
    unit_scale = 12
    unit_reference = 3456
    data_width = 5
    descr_048002 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048002)

    #            FXXYYY
    reference = '048003'
    name = 'a dummy counter' # max. 64 characters
    unit = 'FLAG TABLE 048003'
    unit_scale = 0
    unit_reference = 0
    data_width = 10
    descr_048003 = Descriptor(reference, name, unit, unit_scale,
                              unit_reference, data_width)
    bt1.add_to_B_table(descr_048003)

    #            FXXYYY
    reference = '048003'
    fldef = FlagDefinition(reference)
    fldef.flag_dict[0] = 'zero'
    fldef.flag_dict[1] = 'one'
    fldef.flag_dict[2] = 'two'
    fldef.flag_dict[3] = 'a very long line to describe the number 3; '*10
    fldef.flag_dict[4] = 'four'
    fldef.flag_dict[999] = 'missing'
    bt1.table_c[int(reference)] = fldef

    reference = '348001'
    descriptor_list = [descr_048001, descr_048002]
    comment = 'a small test D entry' # not written to file
    bufr_table_set = bt1
    descr_348001 = CompositeDescriptor(reference, descriptor_list,
                                       comment, bufr_table_set)

    bt1.add_to_D_table(descr_348001)

    print('='*50)
    print("B-table:")
    print('='*50)
    bt1.print_B_table()
    print('='*50)
    print("C-table:")
    print('='*50)
    bt1.print_C_table()
    print('='*50)
    print("D-table:")
    print('='*50)
    bt1.print_D_table()
    print('='*50)

    # define the table name without preceding 'B' or 'D' character
    # (which will be prepended by the below write method)
    table_name = '_my_test_BUFR_table.txt'
    bt1.write_tables(table_name)
Example #13
0
#!/usr/bin/env python

"""
this small example program loads the BUFR B- and D-tables
and reports any inconsistencies it finds in the table definitions
"""

# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html

import sys
from pybufr_ecmwf.bufr_table import BufrTable

if len(sys.argv) < 3:
    print 'please give 2 BUFR TABLE files as argument'
    sys.exit(1)

BTABLE_FILE = sys.argv[1]
DTABLE_FILE = sys.argv[2]

#BT = bufr.BufrTable(tables_dir="my_BUFR_TABLES")
#BT = bufr.BufrTable(tables_dir="../ecmwf_bufrtables")
BT = BufrTable()
BT.load(BTABLE_FILE)
BT.load(DTABLE_FILE)