output_bufr_file = 'dummy_bufr_file.bfr' # open the file for writing bf1.open(output_bufr_file, 'wb') # write the encoded BUFR message bf1.write_raw_bufr_msg(bufr.encoded_message) # close the file bf1.close() ############################## # reopen the BUFR file as test ############################## print('*'*50) input_bufr_file = output_bufr_file bufr = BUFRReader(input_bufr_file, warn_about_bufr_size=False) bufr.setup_tables(table_b_to_use='B'+table_name, table_d_to_use='D'+table_name) for msg in bufr: print('num_subsets = ', msg.get_num_subsets()) for subs, msg_or_subset_data in enumerate(msg): list_of_names = msg_or_subset_data.names list_of_units = msg_or_subset_data.units data = msg_or_subset_data.data print('"subset nr"'+','+','.join(list_of_names)) print('""'+','+','.join(list_of_units)) print(str(subs+1)+','+','.join(str(val) for val in data[:])) bufr.close()
# debug print('values: ', list(msg.values)) msg.write_msg_to_file() bwr.close() ############################## # reopen the BUFR file as test ############################## print('*' * 50) input_bufr_file = output_bufr_file bufr = BUFRReader(input_bufr_file, expand_strings=True, warn_about_bufr_size=False) # just 1 msg in this test file, so no looping needed for msg in bufr: print('num_subsets = ', msg.get_num_subsets()) for subs, msg_or_subset_data in enumerate(msg): list_of_names = msg_or_subset_data.names list_of_units = msg_or_subset_data.units data = msg_or_subset_data.data print('"subset nr"' + ',' + ','.join(list_of_names)) print('""' + ',' + ','.join(list_of_units)) print(str(subs + 1) + ',' + ','.join(str(val) for val in data[:])) bufr.close()
def select_subsets(input_bufr_file, output_bufr_file): # #[ select on subsets """ select data and write out again """ # 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) # open the file for writing rbf_out = RawBUFRFile() rbf_out.open(output_bufr_file, 'wb') msg_nr = 0 while True: try: bob.get_next_msg() msg_nr += 1 except EOFError: break data = bob.get_values_as_2d_array() print('data.shape = ', data.shape) if data.shape[0] * data.shape[1] == 0: print('NO DATA FOUND! this seems an empty BUFR message !') continue # select every other subset new_data = data[::2, :] print('new_data.shape = ', new_data.shape) #bob.bufr_obj nsub = bob.bufr_obj.get_num_subsets() n_exp_descr = len(bob.bufr_obj.values) / nsub bob.bufr_obj.fill_descriptor_list( nr_of_expanded_descriptors=n_exp_descr) bob.bufr_obj.ktdlst = bob.bufr_obj.get_descriptor_list() delayed_repl_data = bob.bufr_obj.derive_delayed_repl_factors() bob.bufr_obj.fill_delayed_repl_data(delayed_repl_data) new_nsub = new_data.shape[0] bob.bufr_obj.nr_subsets = new_nsub btm = BufrTemplate() btm.add_descriptors(*bob.bufr_obj.ktdlst) #[:self.ktdlen]) btm.nr_of_delayed_repl_factors = 1 btm.del_repl_max_nr_of_repeats_list = list(delayed_repl_data) bob.bufr_obj.register_and_expand_descriptors(btm) # activate this one if the encoding crashes without clear cause: # bob.bufr_obj.estimated_num_bytes_for_encoding = 25000 bob.bufr_obj.kdate = new_nsub * list(delayed_repl_data) print('bob.bufr_obj.cvals.shape = ', bob.bufr_obj.cvals.shape) bob.bufr_obj.encode_data(new_data, bob.bufr_obj.cvals[:32, :]) rbf_out.write_raw_bufr_msg(bob.bufr_obj.encoded_message) #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) # close the file bob.close() if msg_nr == 0: print('no BUFR messages found, are you sure this is a BUFR file?') rbf_out.close()
def sort_msgs(input_bufr_file): # #[ """ a little example routine to demonstrate how to extract BUFR messages from a BUFR file, sort them, and write them out again to another file. """ # 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) num_msgs = bob.rbf.get_num_bufr_msgs() progress_step = max(1, int(num_msgs / 20)) files_dict = {} msg_nr = 0 while True: can_be_decoded = False try: bob.get_next_msg() can_be_decoded = True except EOFError: break except KeyError: # allow sorting of BUFR messages that cannot be decoded # because the needed key is not in the available set of # BUFR table files. This tool only uses unexpanded descriptors # so ability to decode should not be required. # A user still may wish to first sort, and then decode # a subset of messages that can be decoded. pass msg_nr += 1 if progress_step * int(msg_nr / progress_step) == msg_nr: print('handling message nr {} out of {}'.format(msg_nr, num_msgs)) list_of_unexp_descr = bob.bufr_obj.py_unexp_descr_list output_filename = construct_unique_filename(list_of_unexp_descr) if files_dict.has_key(output_filename): fdescr = files_dict[output_filename][0] files_dict[output_filename][1] += 1 # increment count else: fdescr = open(output_filename, 'wb') count = 1 files_dict[output_filename] = [ fdescr, count, can_be_decoded, list_of_unexp_descr ] fdescr.write(bob.bufr_obj.encoded_message) generated_files = files_dict.keys() num_that_can_be_decoded = 0 num_that_cannot_be_decoded = 0 for k in files_dict: fdescr, count, can_be_decoded, list_of_unexp_descr = files_dict[k] print('file {} contains {} messages'.format(k[:25], count)) files_dict[k][0].close() if can_be_decoded: num_that_can_be_decoded += 1 else: num_that_cannot_be_decoded += 1 # check to see if local descriptors are present for descr in list_of_unexp_descr: if int(descr[3:]) >= 192: print('==>A local descriptor definition is present: ', descr) print('==>this template cannot be decoded with ' + 'standard WMO BUFR tables.') print('Sorting results:') print('BUFR messages with {} different templates are present in this file'. format(num_that_can_be_decoded + num_that_cannot_be_decoded)) if num_that_cannot_be_decoded > 0: print('decoding is not possible for {} templates.'.format( num_that_cannot_be_decoded)) return generated_files