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()
Exemple #2
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()
Exemple #3
0
def print_bufr_content1(input_bufr_file, output_fd, separator, max_msg_nr):
    #  #[ implementation 1
    """
    example implementation using the BUFRReader class
    combined 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

        # add header strings
        # print 'DEBUG: bob.msg_loaded ',bob.msg_loaded
        if bob.msg_loaded == 1:
            list_of_names = []
            list_of_units = []
            list_of_names.extend(bob.get_names())
            list_of_units.extend(bob.get_units())
            # print 'DEBUG: ',separator.join(list_of_names)
            # print 'DEBUG: ',separator.join(list_of_units)
            output_fd.write(separator.join(list_of_names) + "\n")
            output_fd.write(separator.join(list_of_units) + "\n")

        data = bob.get_values_as_2d_array()
        # print 'DEBUG: data.shape = ', data.shape
        if data.shape[0] * data.shape[1] == 0:
            print 'NO DATA FOUND! this seems an empty BUFR message !'
            continue

        for subs in range(len(data[:, 0])):
            output_fd.write(
                str(subs) + separator +
                separator.join(str(val) for val in data[subs, :]) + "\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?'
def print_bufr_content1(input_bufr_file, output_fd, separator,
                        max_msg_nr, expand_flags):
    #  #[ implementation 1
    """
    example implementation using the BUFRReader class
    combined 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,
                     expand_flags=expand_flags)

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

        # add header strings
        # print('DEBUG: bob.msg_loaded ',bob.msg_loaded)
        list_of_names = []
        list_of_units = []
        list_of_names.extend(bob.get_names())
        list_of_units.extend(bob.get_units())
        list_of_unexp_descr = bob.get_unexp_descr_list()

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

        if bob.msg_loaded == 1:
            output_fd.write('"subset nr"'+separator)
            if list_of_names:
                for name in list_of_names[:-1]:
                    output_fd.write('"'+name+'"'+separator)
                name = list_of_names[-1]
                output_fd.write('"'+name+'"\n')
            else:
                output_fd.write('"[NO DATA]"\n')

            output_fd.write('""'+separator)
            if list_of_units:
                for unit in list_of_units[:-1]:
                    output_fd.write('"'+unit+'"'+separator)
                unit = list_of_units[-1]
                output_fd.write('"'+unit+'"\n')
            else:
                output_fd.write('"[NO DATA]"\n')

            list_of_unexp_descr_first_msg = bob.bufr_obj.py_unexp_descr_list
            #print('list_of_unexp_descr_first_msg = ',
            #      list_of_unexp_descr_first_msg)

        data = bob.get_values_as_2d_array()

        if list_of_unexp_descr != list_of_unexp_descr_first_msg:
            print('\n\n')
            print('WARNING: it seems different types of BUFR messages')
            print('are mixed in this BUFR file, meaning that the list of')
            print('descriptor names and units printed on the first 2 output')
            print('lines will not match with all lines of data.')
            print('To prevent confusion, therefore decoding is halted')
            print('It is recommended to first sort BUFR messages by type')
            print('before converting them to ascii or csv.')
            print('The example script sort_bufr_msgs.py can be used')
            print('to sort a BUFR file.')
            print('\n\n')
            print('Detailed info:')
            print('list_of_unexp_descr != list_of_unexp_descr_first_msg !')
            print('list_of_unexp_descr           = ',
                  list_of_unexp_descr)
            print('list_of_unexp_descr_first_msg = ',
                  list_of_unexp_descr_first_msg)
            sys.exit(0)

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

        for subs in range(len(data[:, 0])):
            output_fd.write(str(subs+1)+separator+
                            separator.join(str(val) for val in data[subs, :])+
                            "\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?')
def print_bufr_content2(input_bufr_file, output_fd, separator,
                        max_msg_nr, expand_flags):
    #  #[ implementation 2
    """
    example implementation using the BUFRReader class
    combined with the get_value method
    """

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

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

        # add header strings
        if bob.msg_loaded == 1:
            list_of_names = bob.get_names()
            list_of_units = bob.get_units()

            output_fd.write('"subset nr"'+separator)
            if list_of_names:
                for name in list_of_names[:-1]:
                    output_fd.write('"'+name+'"'+separator)
                name = list_of_names[-1]
                output_fd.write('"'+name+'"\n')
            else:
                output_fd.write('"[NO DATA]"\n')

            output_fd.write('""'+separator)
            if list_of_units:
                for unit in list_of_units[:-1]:
                    output_fd.write('"'+unit+'"'+separator)
                unit = list_of_units[-1]
                output_fd.write('"'+unit+'"\n')
            else:
                output_fd.write('"[NO DATA]"\n')

        nsubsets = bob.get_num_subsets()
        for subs in range(1, nsubsets+1):
            nelements = bob.get_num_elements()
            data_list = []
            for descr_nr in range(nelements):
                data = bob.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 file
    bob.close()
    if msg_nr == 0:
        print('no BUFR messages found, are you sure this is a BUFR file?')
Exemple #6
0
        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?'
    #  #]


if __name__ == '__main__':

    bufrdir = '/net/bhw379/nobackup/users/plas/temp/BUFRdata/'
    bufrfile = os.path.join(bufrdir, 'BUFR.radarv.bewid')
    os.environ['BUFR_TABLES'] = os.path.join(bufrdir, 'BUFRtables')

    bob = BUFRReader(bufrfile, warn_about_bufr_size=False)
    bob.get_next_msg()
    names = bob.get_names()
    units = bob.get_units()

    print set(names)
    print 20 * '='
    print set(units)
    for n, u in zip(names, units):
        if "dB" in u: print n, u

    outf = open('radarBUFR.dat', 'w')
    print_bufr_content1(bufrfile, outf, ',', 5)