bufr_table_set.print_D_table() print('='*50) # define the table name without preceding 'B' or 'D' character # (which will be prepended by the below write method) table_name = '_test_table.txt' bufr_table_set.write_tables(table_name) # now use these definitions to create a BUFR template max_nr_of_repeats = 5 num_subsets = 2 num_repetitions = [3, 5] # actual number to be used template = BufrTemplate(verbose=True) template.add_descriptor(D_363192) # 1 item template.del_repl_max_nr_of_repeats_list = [max_nr_of_repeats,]*num_subsets # and use this BUFR template to create a test BUFR message bufr = BUFRInterfaceECMWF(verbose=True) # fill sections 0, 1, 2 and 3 bufr.fill_sections_0123(bufr_code_centre=0, bufr_obstype=0, bufr_subtype=0, bufr_table_local_version=0, bufr_table_master=0, bufr_table_master_version=0, bufr_code_subcentre=0, num_subsets=num_subsets, bufr_compression_flag=0) # 64=compression/0=no compression
print('='*50) # define the table name without preceding 'B' or 'D' character # (which will be prepended by the below write method) table_name = '_test_table.txt' bufr_table_set.write_tables(table_name) # now use these definitions to create a BUFR template max_nr_of_replications = 3 num_subsets = 3 num_replications1 = [1, 2, 3] num_replications2 = [[1, ], [2, 2, ], [3, 3, 3]] template = BufrTemplate(verbose=True) template.add_descriptor(D_363192) # 1 item template.del_repl_max_nr_of_repeats_list = ([max_nr_of_replications,]* max_nr_of_replications*num_subsets) # and use this BUFR template to create a test BUFR message bufr = BUFRInterfaceECMWF(verbose=True) # fill sections 0, 1, 2 and 3 bufr.fill_sections_0123(bufr_code_centre=0, bufr_obstype=0, bufr_subtype=0, bufr_table_local_version=0, bufr_table_master=0, bufr_table_master_version=0, bufr_code_subcentre=0, num_subsets=num_subsets, bufr_compression_flag=0) # 64=compression/0=no compression
# now use these definitions to create a BUFR template max_nr_of_replications = 3 num_subsets = 3 num_replications1 = [1, 2, 3] num_replications2 = [[ 1, ], [ 2, 2, ], [3, 3, 3]] template = BufrTemplate(verbose=True) template.add_descriptor(D_363192) # 1 item template.del_repl_max_nr_of_repeats_list = ([ max_nr_of_replications, ] * max_nr_of_replications * num_subsets) # and use this BUFR template to create a test BUFR message bufr = BUFRInterfaceECMWF(verbose=True) # fill sections 0, 1, 2 and 3 bufr.fill_sections_0123(bufr_code_centre=0, bufr_obstype=0, bufr_subtype=0, bufr_table_local_version=0, bufr_table_master=0, bufr_table_master_version=0, bufr_code_subcentre=0, num_subsets=num_subsets, bufr_compression_flag=0)
print('=' * 50) # define the table name without preceding 'B' or 'D' character # (which will be prepended by the below write method) table_name = '_test_table.txt' bufr_table_set.write_tables(table_name) # now use these definitions to create a BUFR template max_nr_of_repeats = 5 num_subsets = 2 num_repetitions = [3, 5] # actual number to be used template = BufrTemplate(verbose=True) template.add_descriptor(D_363192) # 1 item template.del_repl_max_nr_of_repeats_list = [ max_nr_of_repeats, ] * num_subsets # and use this BUFR template to create a test BUFR message bufr = BUFRInterfaceECMWF(verbose=True) # fill sections 0, 1, 2 and 3 bufr.fill_sections_0123(bufr_code_centre=0, bufr_obstype=0, bufr_subtype=0, bufr_table_local_version=0, bufr_table_master=0, bufr_table_master_version=0, bufr_code_subcentre=0, num_subsets=num_subsets, bufr_compression_flag=0)
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 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()