def upgrade_bufr_file(input_bufr_file, output_bufr_file):
    #  #[
    """
    an example routine to demonstrate how to read a BUFR message,
    upgrade it's edition number, and write it to an output BUFR file
    """
    # get 2 instances of the RawBUFRFile class
    rbf_in = RawBUFRFile()
    rbf_out = RawBUFRFile()

    # open the file for reading, count nr of BUFR messages in it
    # and store its content in memory, together with
    # an array of pointers to the start and end of each BUFR message
    rbf_in.open(input_bufr_file, 'rb')

    # open the file for writing
    rbf_out.open(output_bufr_file, 'wb')

    # extract the number of BUFR messages from the file
    num_msgs = rbf_in.get_num_bufr_msgs()

    for msg_nr in range(1, num_msgs+1):
        raw_msg, section_sizes, section_start_locations = \
                 rbf_in.get_raw_bufr_msg(msg_nr)
        bufr_obj = BUFRInterfaceECMWF(raw_msg, section_sizes,
                                      section_start_locations,
                                      verbose=True)

        bufr_obj.decode_sections_012()
        bufr_obj.setup_tables()
        bufr_obj.decode_data()

        nsub = bufr_obj.get_num_subsets()
        n_exp_descr = len(bufr_obj.values)/nsub
        bufr_obj.fill_descriptor_list(nr_of_expanded_descriptors=n_exp_descr)

        bufr_obj.ksec0[3-1] = 4 # set bufr edition to 4
        bufr_obj.ktdlst = bufr_obj.get_descriptor_list()

        # extract delayed replication factors
        delayed_repl_data = bufr_obj.derive_delayed_repl_factors()

        # fill the list of replication factors
        bufr_obj.fill_delayed_repl_data(delayed_repl_data)

        # activate this one if the encoding crashes without clear cause:
        # bufr_obj.estimated_num_bytes_for_encoding = 25000

        # encode the data
        bufr_obj.encode_data(bufr_obj.values, bufr_obj.cvals)
        print 'Encode BUFR msg %i' % msg_nr

        rbf_out.write_raw_bufr_msg(bufr_obj.encoded_message)

    # close the file
    rbf_in.close()
    rbf_out.close()
def decoding_example(input_bufr_file, custom_bufr_tables=None):
    #  #[
    """
    wrap the example in a function to circumvent the pylint
    convention of requiring capitals for constants in the global
    scope (since most of these variables are not constants at all))
    """

    # read the binary data using the BUFRFile class
    print('loading testfile: ', input_bufr_file)
    rbf = RawBUFRFile(verbose=False)
    rbf.open(input_bufr_file, 'rb')
    (words, section_sizes, section_start_locations) = \
            rbf.get_next_raw_bufr_msg()
    rbf.close()

    if words is None:
        print('No valid BUFR messages found')
        sys.exit(0)

    print('------------------------------')
    bufr = BUFRInterfaceECMWF(encoded_message=words,
                              section_sizes=section_sizes,
                              section_start_locations=section_start_locations,
                              verbose=True)

    print("calling: decode_sections_012():")
    bufr.decode_sections_012()

    print("Metadata for decoded BUFR message:")
    bufr.print_sections_012_metadata()

    print("calling: setup_tables()")
    if custom_bufr_tables:
        bufr.setup_tables(table_b_to_use=custom_bufr_tables[0],
                          table_c_to_use=custom_bufr_tables[1],
                          table_d_to_use=custom_bufr_tables[2])
    else:
        bufr.setup_tables()

    print("calling: print_sections_012():")
    bufr.print_sections_012()

    # seems not to work correctly now ...
    #bufr.fill_descriptor_list()
    #bufr.print_descriptors()

    print('------------------------------')
    print("calling: bufr.decode_data():")
    bufr.decode_data()

    bufr.decode_sections_0123()
    bufr.fill_descriptor_list_subset(subset=1)

    return bufr
def decoding_example(input_bufr_file, custom_bufr_tables=None):
    #  #[
    """
    wrap the example in a function to circumvent the pylint
    convention of requiring capitals for constants in the global
    scope (since most of these variables are not constants at all))
    """

    # read the binary data using the BUFRFile class
    print('loading testfile: ', input_bufr_file)
    rbf = RawBUFRFile(verbose=False)
    rbf.open(input_bufr_file, 'rb')
    (words, section_sizes, section_start_locations) = \
            rbf.get_next_raw_bufr_msg()
    rbf.close()

    if words is None:
        print('No valid BUFR messages found')
        sys.exit(0)

    print('------------------------------')
    bufr = BUFRInterfaceECMWF(encoded_message=words,
                              section_sizes=section_sizes,
                              section_start_locations=section_start_locations,
                              verbose=True)

    print("calling: decode_sections_012():")
    bufr.decode_sections_012()

    print("Metadata for decoded BUFR message:")
    bufr.print_sections_012_metadata()

    print("calling: setup_tables()")
    if custom_bufr_tables:
        bufr.setup_tables(table_b_to_use=custom_bufr_tables[0],
                          table_c_to_use=custom_bufr_tables[1],
                          table_d_to_use=custom_bufr_tables[2])
    else:
        bufr.setup_tables()

    print("calling: print_sections_012():")
    bufr.print_sections_012()

    # seems not to work correctly now ...
    #bufr.fill_descriptor_list()
    #bufr.print_descriptors()

    print('------------------------------')
    print("calling: bufr.decode_data():")
    bufr.decode_data()

    bufr.decode_sections_0123()
    bufr.fill_descriptor_list_subset(subset=1)

    return bufr
def print_bufr_content3(input_bufr_file, output_fd, separator, max_msg_nr,
                        expand_flags):
    #  #[ implementation 3
    """
    example implementation using the BUFRInterfaceECMWF class
    """
    if expand_flags:
        print('Sorry, expand_flags is not yet implemented ' +
              'for example implementation 3')

    # get an instance of the RawBUFRFile class
    rbf = RawBUFRFile()

    # open the file for reading, count nr of BUFR messages in it
    # and store its content in memory, together with
    # an array of pointers to the start and end of each BUFR message
    rbf.open(input_bufr_file, 'rb')

    # extract the number of BUFR messages from the file
    num_msgs = rbf.get_num_bufr_msgs()

    # print('num_msgs = ',num_msgs)

    for msg_nr in range(1, num_msgs + 1):
        encoded_message, section_sizes, section_start_locations = \
                         rbf.get_raw_bufr_msg(msg_nr)
        bufr_obj = BUFRInterfaceECMWF(encoded_message, section_sizes,
                                      section_start_locations)
        #                              verbose=True)
        bufr_obj.decode_sections_012()
        bufr_obj.setup_tables()
        # print('num_subsets: ', bufr_obj.get_num_subsets())
        # print('num_elements: ',bufr_obj.get_num_elements())
        # bufr_obj.decode_sections_0123()
        # bufr_obj.print_sections_0123_metadata()

        # d = '/home/jos/werk/pybufr_ecmwf_interface/'+\
        #     'BUFR_test_files/radar/bufrtables/'
        # bufr_obj.setup_tables(table_b_to_use = d+'B0000000000085011012.TXT',
        #                      table_d_to_use = d+'D0000000000085011012.TXT')
        # bufr_obj.print_sections_012()
        # bufr_obj.fill_descriptor_list()

        # do the actual decoding
        bufr_obj.decode_data()

        # needed to have the units ready, so autoget_cval will work
        bufr_obj.decode_sections_0123()

        # Create header lines from variable names and units
        if msg_nr == 1:
            list_of_names = []
            list_of_units = []
            for (cname, cunit) in zip(bufr_obj.cnames, bufr_obj.cunits):
                # glue the ndarray of characters together to form strings
                if python3:
                    cname_str = ''.join(c.decode() for c in cname).strip()
                    cunit_str = ''.join(c.decode() for c in cunit).strip()
                else:
                    cname_str = ''.join(cname).strip()
                    cunit_str = ''.join(cunit).strip()

                # cnames is a bit over dimensioned, so check for empty values
                if cname_str.strip() == '':
                    break

                # append the strings to the head list and quote them
                list_of_names.append('"' + cname_str + '"')
                list_of_units.append('"' + cunit_str + '"')

            output_fd.write('"subset nr"' + separator)
            output_fd.write(separator.join(list_of_names) + '\n')

            output_fd.write('""' + separator)
            output_fd.write(separator.join(list_of_units) + '\n')

        nsubsets = bufr_obj.get_num_subsets()
        for subs in range(1, nsubsets + 1):

            # needed to have the units ready, so autoget_cval will work
            bufr_obj.fill_descriptor_list_subset(subs)

            nelements = bufr_obj.get_num_elements()
            data_list = []
            for descr_nr in range(nelements):
                data = bufr_obj.get_value(descr_nr, subs, autoget_cval=True)
                data_list.append(data)
            output_fd.write(
                str(subs) + separator +
                separator.join(str(val) for val in data_list) + "\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 BUFR file
    rbf.close()
    if num_msgs == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')
def print_bufr_content3(input_bufr_file, output_fd, separator,
                        max_msg_nr, expand_flags):
    #  #[ implementation 3
    """
    example implementation using the BUFRInterfaceECMWF class
    """
    if expand_flags:
        print('Sorry, expand_flags is not yet implemented '+
              'for example implementation 3')

    # get an instance of the RawBUFRFile class
    rbf = RawBUFRFile()

    # open the file for reading, count nr of BUFR messages in it
    # and store its content in memory, together with
    # an array of pointers to the start and end of each BUFR message
    rbf.open(input_bufr_file, 'rb')

    # extract the number of BUFR messages from the file
    num_msgs = rbf.get_num_bufr_msgs()

    # print('num_msgs = ',num_msgs)

    for msg_nr in range(1, num_msgs+1):
        encoded_message, section_sizes, section_start_locations = \
                         rbf.get_raw_bufr_msg(msg_nr)
        bufr_obj = BUFRInterfaceECMWF(encoded_message, section_sizes,
                                      section_start_locations)
        #                              verbose=True)
        bufr_obj.decode_sections_012()
        bufr_obj.setup_tables()
        # print('num_subsets: ', bufr_obj.get_num_subsets())
        # print('num_elements: ',bufr_obj.get_num_elements())
        # bufr_obj.decode_sections_0123()
        # bufr_obj.print_sections_0123_metadata()

        # d = '/home/jos/werk/pybufr_ecmwf_interface/'+\
        #     'BUFR_test_files/radar/bufrtables/'
        # bufr_obj.setup_tables(table_b_to_use = d+'B0000000000085011012.TXT',
         #                      table_d_to_use = d+'D0000000000085011012.TXT')
        # bufr_obj.print_sections_012()
        # bufr_obj.fill_descriptor_list()

        # do the actual decoding
        bufr_obj.decode_data()

        # needed to have the units ready, so autoget_cval will work
        bufr_obj.decode_sections_0123()

        # Create header lines from variable names and units
        if msg_nr == 1:
            list_of_names = []
            list_of_units = []
            for (cname, cunit) in zip(bufr_obj.cnames, bufr_obj.cunits):
                # glue the ndarray of characters together to form strings
                if python3:
                    cname_str = ''.join(c.decode() for c in cname).strip()
                    cunit_str = ''.join(c.decode() for c in cunit).strip()
                else:
                    cname_str = ''.join(cname).strip()
                    cunit_str = ''.join(cunit).strip()

                # cnames is a bit over dimensioned, so check for empty values
                if cname_str.strip() == '':
                    break

                # append the strings to the head list and quote them
                list_of_names.append('"'+cname_str+'"')
                list_of_units.append('"'+cunit_str+'"')

            output_fd.write('"subset nr"'+separator)
            output_fd.write(separator.join(list_of_names) + '\n')

            output_fd.write('""'+separator)
            output_fd.write(separator.join(list_of_units) + '\n')

        nsubsets = bufr_obj.get_num_subsets()
        for subs in range(1, nsubsets+1):

            # needed to have the units ready, so autoget_cval will work
            bufr_obj.fill_descriptor_list_subset(subs)

            nelements = bufr_obj.get_num_elements()
            data_list = []
            for descr_nr in range(nelements):
                data = bufr_obj.get_value(descr_nr, subs, autoget_cval=True)
                data_list.append(data)
            output_fd.write(str(subs)+separator+
                            separator.join(str(val) for val in data_list)+
                            "\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 BUFR file
    rbf.close()
    if num_msgs == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')