Exemple #1
0
def command_info(ns):
    """
    Command to show metadata information of given files from command line.
    """
    flat_text_render = FlatTextRenderer()
    decoder = Decoder(definitions_dir=ns.definitions_directory,
                      tables_root_dir=ns.tables_root_directory)

    def show_message_info(m):
        bufr_template, table_group = m.build_template(
            ns.tables_root_directory, normalize=1)

        print(flat_text_render.render(m))
        if ns.template:
            print(flat_text_render.render(bufr_template))

    for filename in ns.filenames:
        with open(filename, 'rb') as ins:
            s = ins.read()

        if ns.multiple_messages:
            for bufr_message in generate_bufr_message(decoder, s,
                                                      file_path=filename, info_only=True):
                show_message_info(bufr_message)

        elif ns.count_only:
            count = 0
            for _ in generate_bufr_message(decoder, s, info_only=True):
                count += 1
            print('{}: {}'.format(filename, count))

        else:
            bufr_message = decoder.process(s, file_path=filename, info_only=True)
            show_message_info(bufr_message)
Exemple #2
0
    def test(self):
        output = []
        with open(os.path.join(DATA_DIR, 'prepbufr.bufr'), 'rb') as ins:
            for bufr_message in generate_bufr_message(self.decoder,
                                                      ins.read()):
                output.append(FlatTextRenderer().render(bufr_message))

        lines = [
            line for line in ('\n'.join(output)).splitlines(True)
            if not line.startswith('TableGroupKey')
            and not line.startswith('stop_signature')
        ]
        assert ''.join(lines).endswith(compare)
Exemple #3
0
def command_decode(ns):
    """
    Command to decode given files from command line.
    """
    decoder = Decoder(
        definitions_dir=ns.definitions_directory,
        tables_root_dir=ns.tables_root_directory,
        compiled_template_cache_max=ns.compiled_template_cache_max)

    def show_message(m):
        if ns.attributed:
            m.wire()
            if ns.json:
                print(
                    json.dumps(NestedJsonRenderer().render(m),
                               **JSON_DUMPS_KWARGS))
            else:
                print(NestedTextRenderer().render(m))
        else:
            if ns.json:
                print(
                    json.dumps(FlatJsonRenderer().render(m),
                               **JSON_DUMPS_KWARGS))
            else:
                print(FlatTextRenderer().render(m))

    for filename in ns.filenames:
        if filename != '-':
            with open(filename, 'rb') as ins:
                s = ins.read()
        else:
            s = sys.stdin.read()

        if ns.multiple_messages:
            for bufr_message in generate_bufr_message(
                    decoder,
                    s,
                    continue_on_error=ns.continue_on_error,
                    file_path=filename,
                    wire_template_data=False,
                    ignore_value_expectation=ns.ignore_value_expectation,
                    filter_expr=ns.filter):
                show_message(bufr_message)
        else:

            bufr_message = decoder.process(
                s,
                file_path=filename,
                wire_template_data=False,
                ignore_value_expectation=ns.ignore_value_expectation)
            show_message(bufr_message)
Exemple #4
0
def command_split(ns):
    """
    Command to split given files from command line into one file per
    BufrMessage.
    """
    decoder = Decoder(definitions_dir=ns.definitions_directory,
                      tables_root_dir=ns.tables_root_directory)

    for filename in ns.filenames:
        with open(filename, 'rb') as ins:
            s = ins.read()

        for idx, bufr_message in enumerate(
                generate_bufr_message(decoder, s, file_path=filename, info_only=True)):
            new_filename = '{}.{}'.format(filename, idx)
            print(new_filename)
            with open(new_filename, 'wb') as outs:
                outs.write(bufr_message.serialized_bytes)

msg = {
    # subset forecast data
    'significance': data_query(bufr, '316082' + '> 008005')}


for index in msg['significance'].subset_indices():
    sig = np.array(msg['significance'].get_values(index), dtype='int')
    print(sig)

#%%
from pybufrkit.decoder import generate_bufr_message
i=1
with open(os.path.join(path_ecmwf,ecmwf_file), 'rb') as ins:
    for bufr in generate_bufr_message(decoder1, ins.read()):
        text_data = FlatTextRenderer().render(bufr)
        f=open('C:/Users/ATeklesadik/OneDrive - Rode Kruis/Documents/documents/Typhoon-Impact-based-forecasting-model/temp/file_%s.txt'%i,'w')
        f.write(text_data)
        f.close()
        i=i+1

        
#%%    
    
# setup parsers and querents
npparser = pybufrkit.dataquery.NodePathParser()
data_query = pybufrkit.dataquery.DataQuerent(npparser).query

meparser = pybufrkit.mdquery.MetadataExprParser()
meta_query = pybufrkit.mdquery.MetadataQuerent(meparser).query
Exemple #6
0
 def test_can_filter_message(self):
     bufr_messages = [m for m in generate_bufr_message(
         self.decoder,
         read_bufr_file('multi_invalid_messages.bufr'),
         filter_expr='${%data_category} == 2')]
     assert len(bufr_messages) == 1, 'expect one message to be filtered'
Exemple #7
0
 def test_can_continue_on_error(self):
     bufr_messages = [m for m in generate_bufr_message(
         self.decoder,
         read_bufr_file('multi_invalid_messages.bufr'),
         continue_on_error=True)]
     assert len(bufr_messages) == 1, 'expect one good message'