def read_bufr_file(input_bufr_file):
    #  #[ read a bufr file
    """
    read the file using the BUFRReader class and get the data
    with the get_values_as_2d_array method
    """

    # get an instance of the BUFR class
    # which automatically opens the file for reading and decodes it
    bob = BUFRReader(input_bufr_file, warn_about_bufr_size=False)

    msg_nr = 0
    while True:
        try:
            bob.get_next_msg()
            msg_nr += 1
        except EOFError:
            break

        nsubsets = bob.get_num_subsets()
        data1 = bob.get_subset_values(1)

        if numpy.shape(data1)[0] == 0:
            print('NO DATA FOUND! this seems an empty BUFR message !')
            continue

        print('loaded BUFR msg nr. ', msg_nr, 'shape = ', numpy.shape(data1))
        print('data1[:2] = ', data1[:2])

        if nsubsets > 1:
            data2 = bob.get_subset_values(2)

            if numpy.shape(data2)[0] == 0:
                print('NO DATA FOUND! this seems an empty BUFR message !')
                continue

            print('data2[:2] = ', data2[:2])

    # close the file
    bob.close()
    if msg_nr == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')
def reopen_bufr_file(input_bufr_file):
    #  #[ open bufr file
    '''
    open a bufr file for reading and print its content
    '''
    print '*'*50
    from pybufr_ecmwf.bufr import BUFRReader

    bob = BUFRReader(input_bufr_file, warn_about_bufr_size=False)
    bob.setup_tables(table_b_to_use='B'+TABLE_NAME,
                     table_d_to_use='D'+TABLE_NAME)

    bob.get_next_msg()
    print 'num_subsets:  ', bob.get_num_subsets()

    if USE_DELAYED_REPLICATION:
        data1 = bob.get_subset_values(0)
        print 'data1 = ', data1
        data2 = bob.get_subset_values(1)
        print 'data2 = ', data2
    else:
        print 'num_elements: ', bob.get_num_elements()
        print bob.get_names()
        print bob.get_units()
        data = bob.get_values_as_2d_array()
        print data.shape
        print data
        print 'bob.bufr_obj.values = '
        print bob.bufr_obj.values

    textdata = bob.get_value(3, 0)
    print 'textdata(3,0)', textdata
    textdata = bob.get_value(3, 0, get_cval=True)
    print 'textdata(3,0)', textdata
    textdata = bob.get_values(3, get_cval=True)
    print 'textdata(3,:)', textdata

    bob.close()
예제 #3
0
def reopen_bufr_file(input_bufr_file):
    #  #[ open bufr file
    '''
    open a bufr file for reading and print its content
    '''
    print '*'*50
    from pybufr_ecmwf.bufr import BUFRReader

    bob = BUFRReader(input_bufr_file, warn_about_bufr_size=False)
    bob.setup_tables(table_b_to_use='B'+TABLE_NAME,
                     table_d_to_use='D'+TABLE_NAME)

    bob.get_next_msg()
    print 'num_subsets:  ', bob.get_num_subsets()

    if USE_DELAYED_REPLICATION:
        data1 = bob.get_subset_values(0)
        print 'data1 = ', data1
        data2 = bob.get_subset_values(1)
        print 'data2 = ', data2
    else:
        print 'num_elements: ', bob.get_num_elements()
        print bob.get_names()
        print bob.get_units()
        data = bob.get_values_as_2d_array()
        print data.shape
        print data
        print 'bob.bufr_obj.values = '
        print bob.bufr_obj.values

    textdata = bob.get_value(3, 0)
    print 'textdata(3,0)', textdata
    textdata = bob.get_value(3, 0, get_cval=True)
    print 'textdata(3,0)', textdata
    textdata = bob.get_values(3, get_cval=True)
    print 'textdata(3,:)', textdata

    bob.close()
예제 #4
0
def print_bufr_content5(input_bufr_file, output_fd, separator, max_msg_nr,
                        expand_flags):
    #  #[ implementation 5
    """
    example implementation using the BUFRReader class
    to decode a bufr file using delayed replication.
    Since these files may have different descriptor lists
    for each subset, a different call pattern is needed.
    """

    # testcases:
    # ./example_programs/bufr_to_ascii.py -5 -c -o tmp.csv \
    #     -i ./pybufr_ecmwf/ecmwf_bufr_lib/bufrdc_000403/data/syno_1.bufr
    #
    # ./example_programs/bufr_to_ascii.py -5 -c -o tmp.csv \
    #     -i ../BUFR_test_files/synop_knmi_via_ko_janssen/MSSAEOL_00002950.b
    #

    names_to_be_selected = ['temperature', 'wind']
    names_to_be_excluded = ['minimum', 'maximum']

    write_names_and_units_just_once = True

    # get an instance of the BUFR class
    # which automatically opens the file for reading and decodes it
    bob = BUFRReader(input_bufr_file,
                     warn_about_bufr_size=False,
                     verbose=False,
                     expand_flags=expand_flags)

    msg_nr = 0
    not_yet_printed = True
    while True:
        try:
            bob.get_next_msg()
            msg_nr += 1
        except EOFError:
            break

        # since this example assumes a bufr file using delayed replication
        # always request and add the header for each subset
        nsubsets = bob.get_num_subsets()
        for subs in range(1, nsubsets + 1):

            print('==> subset ', subs)

            # add header strings
            (list_of_names, list_of_units) = bob.get_names_and_units(subs)
            data = bob.get_subset_values(subs)  #,autoget_cval=True)

            selected_names = []
            selected_units = []
            selected_values = []
            for i, name in enumerate(list_of_names):
                selected = False
                for name in names_to_be_selected:
                    if name in name.lower():
                        selected = True
                for name in names_to_be_excluded:
                    if name in name.lower():
                        selected = False

                if selected:
                    # print(' '*10,name,'=',data[i],list_of_units[i])
                    selected_names.append(list_of_names[i])
                    selected_units.append(list_of_units[i])
                    selected_values.append(data[i])

            if len(selected_values) == 0:
                print('NO DATA SELECTED for BUFR message %d and subset %d!' %
                      (msg_nr, subs))
                continue

            if write_names_and_units_just_once and not_yet_printed:
                output_fd.write('"subset nr"' + separator +
                                separator.join(selected_names) + "\n")
                output_fd.write('""' + separator +
                                separator.join(selected_units) + "\n")
                not_yet_printed = False

            output_fd.write(
                str(subs) + separator +
                separator.join(str(val) for val in selected_values) + "\n")

        print('=' * 25)
        print('converted BUFR msg nr. ', msg_nr)
        print('=' * 25)
        if (max_msg_nr > 0) and (msg_nr >= max_msg_nr):
            print('skipping remainder of this BUFR file')
            break

    # close the file
    bob.close()
    if msg_nr == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')
예제 #5
0
def print_bufr_content5(input_bufr_file, output_fd, separator,
                        max_msg_nr, expand_flags):
    #  #[ implementation 5
    """
    example implementation using the BUFRReader class
    to decode a bufr file using delayed replication.
    Since these files may have different descriptor lists
    for each subset, a different call pattern is needed.
    """

    # testcases:
    # ./example_programs/bufr_to_ascii.py -5 -c -o tmp.csv \
    #     -i ./pybufr_ecmwf/ecmwf_bufr_lib/bufrdc_000403/data/syno_1.bufr
    #
    # ./example_programs/bufr_to_ascii.py -5 -c -o tmp.csv \
    #     -i ../BUFR_test_files/synop_knmi_via_ko_janssen/MSSAEOL_00002950.b
    #

    names_to_be_selected = ['temperature', 'wind']
    names_to_be_excluded = ['minimum', 'maximum']

    write_names_and_units_just_once = True

    # get an instance of the BUFR class
    # which automatically opens the file for reading and decodes it
    bob = BUFRReader(input_bufr_file, warn_about_bufr_size=False,
                     verbose=False, expand_flags=expand_flags)

    msg_nr = 0
    not_yet_printed = True
    while True:
        try:
            bob.get_next_msg()
            msg_nr += 1
        except EOFError:
            break

        # since this example assumes a bufr file using delayed replication
        # always request and add the header for each subset
        nsubsets = bob.get_num_subsets()
        for subs in range(1, nsubsets+1):

            print('==> subset ', subs)

            # add header strings
            (list_of_names, list_of_units) = bob.get_names_and_units(subs)
            data = bob.get_subset_values(subs) #,autoget_cval=True)

            selected_names = []
            selected_units = []
            selected_values = []
            for i, name in enumerate(list_of_names):
                selected = False
                for name in names_to_be_selected:
                    if name in name.lower():
                        selected = True
                for name in names_to_be_excluded:
                    if name in name.lower():
                        selected = False

                if selected:
                    # print(' '*10,name,'=',data[i],list_of_units[i])
                    selected_names.append(list_of_names[i])
                    selected_units.append(list_of_units[i])
                    selected_values.append(data[i])

            if len(selected_values) == 0:
                print('NO DATA SELECTED for BUFR message %d and subset %d!' %
                      (msg_nr, subs))
                continue

            if write_names_and_units_just_once and not_yet_printed:
                output_fd.write('"subset nr"'+separator+
                                separator.join(selected_names) + "\n")
                output_fd.write('""'+separator+
                                separator.join(selected_units) + "\n")
                not_yet_printed = False

            output_fd.write(str(subs)+separator+
                            separator.join(str(val) for val in selected_values)+
                            "\n")

        print('='*25)
        print('converted BUFR msg nr. ', msg_nr)
        print('='*25)
        if (max_msg_nr > 0) and (msg_nr >= max_msg_nr):
            print('skipping remainder of this BUFR file')
            break

    # close the file
    bob.close()
    if msg_nr == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')
예제 #6
0
def print_bufr_content4(input_bufr_file, output_fd, separator,
                        max_msg_nr, expand_flags):
    #  #[ implementation 4
    """
    example implementation using the BUFRReader class
    to decode a bufr file using delayed replication.
    Since these files may have different descriptor lists
    for each subset, a different call pattern is needed.
    """

    # get an instance of the BUFR class
    # which automatically opens the file for reading and decodes it
    bob = BUFRReader(input_bufr_file, warn_about_bufr_size=False,
#                     verbose=True, expand_flags=expand_flags)
                     verbose=False, expand_flags=expand_flags)

    msg_nr = 0
    while True:
        try:
            bob.get_next_msg()
            msg_nr += 1
        except EOFError:
            break

        # since this example assumes a bufr file using delayed replication
        # always request and add the header for each subset
        nsubsets = bob.get_num_subsets()
        for subs in range(1, nsubsets+1):

            # add header strings
            (list_of_names, list_of_units) = bob.get_names_and_units(subs)

            # currently not used
            # list_of_unexp_descr = bob.bufr_obj.py_unexp_descr_list

            data = bob.get_subset_values(subs, autoget_cval=True)

            # print('len(list_of_names) = ', len(list_of_names))
            # print('len(list_of_units) = ', len(list_of_units))
            # print('len(data) = ', len(data))

            if numpy.shape(data)[0] == 0:
                print('NO DATA FOUND! this seems an empty BUFR message !')
                continue

            output_fd.write('"subset nr"'+separator+
                            separator.join(list_of_names) + "\n")
            output_fd.write('""'+separator+
                            separator.join(list_of_units) + "\n")
            output_fd.write(str(subs)+separator+
                            separator.join(str(val) for val in data[:])+
                            "\n")

        print('converted BUFR msg nr. ', msg_nr)
        if (max_msg_nr > 0) and (msg_nr >= max_msg_nr):
            print('skipping remainder of this BUFR file')
            break

    # close the file
    bob.close()
    if msg_nr == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')