def print_bufr_edition_number(input_bufr_file): # #[ """ an example routine to demonstrate how to retrieve some meta data from the BUFR messages in a BUFR file """ # 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() for msg_nr in range(1, num_msgs+1): raw_msg = rbf.get_raw_bufr_msg(msg_nr)[0] bufr_obj = BUFRInterfaceECMWF(encoded_message=raw_msg) bufr_obj.decode_sections_012() bufr_edition = bufr_obj.ksec0[3-1] print('BUFR msg %i has version %i' % (msg_nr, bufr_edition)) # close the file rbf.close()
def count_msgs(input_bufr_file): # #[ """ a little example routine to demonstrate how to extract the number of messages from a BUFR file """ # get an instance of the RawBUFRFile class rbf = RawBUFRFile() #rbf = RawBUFRFile(verbose=True) # 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('rbf.nr_of_bufr_messages = ',rbf.nr_of_bufr_messages) # close the file rbf.close() # delete the class instance # (just as test, not really needed here since this scope is about # to be deleted anyway) del rbf return num_msgs
def count_msgs(input_bufr_file): # #[ """ a little example routine to demonstrate how to extract the number of messages from a BUFR file """ # get an instance of the RawBUFRFile class rbf = RawBUFRFile() #rbf = RawBUFRFile(verbose=True) # 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 'rbf.nr_of_bufr_messages = ',rbf.nr_of_bufr_messages # close the file rbf.close() # delete the class instance # (just as test, not really needed here since this scope is about # to be deleted anyway) del rbf return num_msgs
def print_bufr_data_category(input_bufr_file): # #[ """ an example routine to demonstrate how to retrieve some meta datafrom the BUFR messages in a BUFR file """ # 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() for msg_nr in range(1, num_msgs + 1): raw_msg = rbf.get_raw_bufr_msg(msg_nr)[0] bufr_obj = BUFRInterfaceECMWF(encoded_message=raw_msg) bufr_obj.decode_sections_012() data_category = bufr_obj.ksec1[11 - 1] print('BUFR msg %i has data category %i' % (msg_nr, data_category)) # close the file rbf.close()
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 raw_file_reading_example(input_bufr_file): # #[ """ example for reading a BUFR message """ # 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') # print the internal data of the class instance rbf.print_properties(prefix="RawBUFRFile (opened for reading)") # print the number of BUFR messages in the file num_msgs = rbf.get_num_bufr_msgs() print "This file contains: ", num_msgs, " BUFR messages." # sequentially read the raw (undecoded) BUFR messages from the # class instance msg1 = rbf.get_next_raw_bufr_msg()[0] # should return proper data try: msg2 = rbf.get_next_raw_bufr_msg()[0] # should return corrupted data except EOFError: msg2 = None try: msg3 = rbf.get_next_raw_bufr_msg()[0] # should return corrupted data except EOFError: msg3 = None print "a warning is expected here:" # msg4 = try: rbf.get_next_raw_bufr_msg()[0] # should raise an EOF error except EOFError: print "Warning: EOF reached !" for i in range(1, num_msgs+1): # read a selected raw BUFR message from the class instance raw_data = rbf.get_raw_bufr_msg(i)[0] print "msg ", i, " got ", len(raw_data), " words" # close the file rbf.close() # delete the class instance del rbf return (msg1, msg2, msg3)
def raw_file_reading_example(input_bufr_file): # #[ """ example for reading a BUFR message """ # 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') # print the internal data of the class instance rbf.print_properties(prefix="RawBUFRFile (opened for reading)") # print the number of BUFR messages in the file num_msgs = rbf.get_num_bufr_msgs() print("This file contains: ", num_msgs, " BUFR messages.") # sequentially read the raw (undecoded) BUFR messages from the # class instance msg1 = rbf.get_next_raw_bufr_msg()[0] # should return proper data try: msg2 = rbf.get_next_raw_bufr_msg()[0] # should return corrupted data except EOFError: msg2 = None try: msg3 = rbf.get_next_raw_bufr_msg()[0] # should return corrupted data except EOFError: msg3 = None print("a warning is expected here:") # msg4 = try: rbf.get_next_raw_bufr_msg()[0] # should raise an EOF error except EOFError: print("Warning: EOF reached !") for i in range(1, num_msgs+1): # read a selected raw BUFR message from the class instance raw_data = rbf.get_raw_bufr_msg(i)[0] print("msg ", i, " got ", len(raw_data), " words") # close the file rbf.close() # delete the class instance del rbf return (msg1, msg2, msg3)
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?')