def command_compile(ns): """ Command to compile the given descriptors. """ from pybufrkit.templatecompiler import TemplateCompiler template_compiler = TemplateCompiler() if os.path.exists(ns.input): decoder = Decoder(definitions_dir=ns.definitions_directory, tables_root_dir=ns.tables_root_directory) with open(ns.input, 'rb') as ins: bufr_message = decoder.process(ins.read(), file_path=ns.input, info_only=True) template, table_group = bufr_message.build_template( ns.tables_root_directory, normalize=1) else: table_group = get_table_group(ns.tables_root_directory, ns.master_table_number, ns.originating_centre, ns.originating_subcentre, ns.master_table_version, ns.local_table_version) descriptor_ids = [x.strip() for x in ns.input.split(',')] template = table_group.template_from_ids(*descriptor_ids) compiled_template = template_compiler.process(template, table_group) print(json.dumps(compiled_template.to_dict()))
def test_compile_serialize_and_deserialize(self): table_group = get_table_group() descriptor_ids = [ x.strip() for x in '311001, 222000, 101018, 31031, 1031, 1032, 101018, 33007' .split(',') ] template = table_group.template_from_ids(*descriptor_ids) compiled_template = self.template_compiler.process( template, table_group) reconstructed_compiled_template = loads_compiled_template( json.dumps(compiled_template.to_dict())) assert reconstructed_compiled_template.to_dict( ) == compiled_template.to_dict()
def build_template(self, tables_root_dir, normalize=1): """ Build the BufrTemplate object using the list of unexpanded descriptors and corresponding table group. :param tables_root_dir: The root directory to find BUFR tables :param normalize: Whether to use some default table group if the specific one is not available. :return: A tuple of BufrTemplate and the associated TableGroup """ table_group = get_table_group( tables_root_dir=tables_root_dir, master_table_number=self.master_table_number.value, originating_centre=self.originating_centre.value, originating_subcentre=self.originating_subcentre.value, master_table_version=self.master_table_version.value, local_table_version=self.local_table_version.value, normalize=normalize) self.table_group_key = table_group.key return table_group.template_from_ids( *self.unexpanded_descriptors.value), table_group
def command_lookup(ns): """ Command to lookup the given descriptors from command line """ table_group = get_table_group(ns.tables_root_directory, ns.master_table_number, ns.originating_centre, ns.originating_subcentre, ns.master_table_version, ns.local_table_version) flat_text_render = FlatTextRenderer() table_group.B.load_code_and_flag( ) # load the code and flag tables for additional details descriptors = table_group.descriptors_from_ids( *[d.strip() for d in ns.descriptors.split(',')]) for descriptor in descriptors: if isinstance(descriptor, ElementDescriptor): print('{}, {}, {}, {}, {}'.format( flat_text_render.render(descriptor), descriptor.unit, descriptor.scale, descriptor.refval, descriptor.nbits)) if ns.code_and_flag and descriptor.unit in ( UNITS_FLAG_TABLE, UNITS_CODE_TABLE, UNITS_COMMON_CODE_TABLE_C1): code_and_flag = table_group.B.code_and_flag_for_descriptor( descriptor) if code_and_flag: for v, description in code_and_flag: output = u'{:8d} {}'.format(v, description) # With Python 2, some terminal utilities, e.g. more, redirect to file, # cause errors when unicode string is printed. The fix is to encode # them before print. if six.PY2: output = output.encode('utf-8', 'ignore') print(output) else: print(flat_text_render.render(descriptor))
def setUp(self): self.table_group = get_table_group()
def setUp(self): self.table_group = get_table_group() self.flat_text_renderer = FlatTextRenderer()
def setUp(self): self.table_group = get_table_group(master_table_version=29) self.flat_text_renderer = FlatTextRenderer()