Пример #1
0
def read_bufr_desc(args):
    """Read BUFR(s), decode meta-data and descriptor list, write to file-handle.
    """
    try:
        fh_out = open(args.out_file, "w")
    except:
        fh_out = sys.stdout
    for fn_in in args.in_file:
        print("FILE\t%s" % os.path.basename(fn_in), file=fh_out)
        i = 0
        for blob, size, header in load_file.next_bufr(fn_in):
            if args.bulletin is not None and i != args.bulletin:
                i += 1
                continue
            print("BUFR\t#%d (%d B)" % (i, size), file=fh_out)
            i += 1
            print("HEADER\t%s" % header, file=fh_out)
            try:
                bufr = Bufr(args.tables_type, args.tables_path)
                bufr.decode_meta(blob, load_tables=(not args.sparse))
                print("META\n%s" % bufr.get_meta_str(), file=fh_out)
                if args.sparse:
                    d = bufr.get_descr_short()
                else:
                    d = bufr.get_descr_full()
                print("DESC :\n%s" % "\n".join(d), file=fh_out)
            except Exception as e:
                print("ERROR\t%s" % e, file=fh_out)
                if logger.isEnabledFor(logging.DEBUG):
                    logger.exception(e)
    if fh_out is not sys.stdout:
        fh_out.close()
Пример #2
0
import logging
handler = logging.StreamHandler()
handler.setFormatter(
    logging.Formatter("[%(levelname)s: %(module)s] %(message)s"))
handler.setLevel(logging.WARNING)
logging.getLogger('').setLevel(logging.WARNING)
logging.getLogger('').addHandler(handler)

from trollbufr.bufr import Bufr
from trollbufr import load_file
import sys

if len(sys.argv) != 2:
    print "SYNTAX:", sys.argv[0], "<bufr>"
    sys.exit(1)
testfile = sys.argv[1]

bfr = Bufr("eccodes", "tables")
for blob, size, header in load_file.next_bufr(testfile):
    bfr.decode(blob)
    print "\n", testfile, header, "\n", bfr.get_meta_str()
    for subset in bfr.next_subset():
        for k, m, (v, q) in subset.next_data():
            print k, m, v
        break

    with open(header.replace(" ", "_"), "w") as fh:
        fh.write(blob[0:])
Пример #3
0
def read_bufr_data(args):
    """Read BUFR(s), decode data section and write to file-handle.

    Depending on command argument "--array", either process the subsets in
    sequence, which is ideal for un-compressed BUFR, or process each descriptor
    per all subsets at once, which improves performance for compressed BUFR.
    """
    try:
        fh_out = open(args.out_file, "w")
    except:
        fh_out = sys.stdout
    bufr = Bufr(args.tables_type, args.tables_path)
    for fn_in in args.in_file:
        print("FILE\t%s" % os.path.basename(fn_in), file=fh_out)
        i = 0
        for blob, size, header in load_file.next_bufr(fn_in):
            if args.bulletin is not None and i != args.bulletin:
                i += 1
                continue
            print("BUFR\t#%d (%d B)" % (i, size), file=fh_out)
            i += 1
            print("HEADER\t%s" % header, file=fh_out)
            try:
                bufr.decode_meta(blob, load_tables=False)
                tabl = bufr.load_tables()
                print("META:\n%s" % bufr.get_meta_str(), file=fh_out)
                for report in bufr.next_subset(args.array
                                               and bufr.is_compressed):
                    print("SUBSET\t#%d/%d" % report.subs_num, file=fh_out)
                    if args.sparse or (args.array and bufr.is_compressed):
                        for descr_entry in report.next_data():
                            if descr_entry.mark is not None:
                                if isinstance(descr_entry.value, (list)):
                                    descr_value = "".join(
                                        [str(x) for x in descr_entry.value])
                                else:
                                    descr_value = descr_entry.value
                                print("  ",
                                      descr_entry.mark,
                                      descr_value,
                                      end="",
                                      file=fh_out)
                                print(file=fh_out)
                                continue
                            if descr_entry.value is None:
                                print("%06d: ///" % (descr_entry.descr),
                                      file=fh_out)
                            elif descr_entry.quality is not None:
                                print(
                                    "%06d: %s (%s)" %
                                    (descr_entry.descr, str(descr_entry.value),
                                     descr_entry.quality),
                                    file=fh_out)
                            else:
                                print("%06d: %s" % (descr_entry.descr,
                                                    str(descr_entry.value)),
                                      file=fh_out)
                    else:
                        for descr_entry in report.next_data():
                            if descr_entry.mark is not None:
                                if isinstance(descr_entry.value, (list)):
                                    descr_value = "".join(
                                        [str(x) for x in descr_entry.value])
                                else:
                                    descr_value = descr_entry.value
                                print("  ",
                                      descr_entry.mark,
                                      descr_value,
                                      end="",
                                      file=fh_out)
                                print(file=fh_out)
                                continue
                            descr_info = tabl.lookup_elem(descr_entry.descr)
                            if descr_info.type in (TabBType.CODE,
                                                   TabBType.FLAG):
                                if descr_entry.value is None:
                                    print("%06d %-40s = Missing value" %
                                          (descr_entry.descr, descr_info.name),
                                          file=fh_out)
                                else:
                                    v = tabl.lookup_codeflag(
                                        descr_entry.descr, descr_entry.value)
                                    print("%06d %-40s = %s" %
                                          (descr_entry.descr, descr_info.name,
                                           str(v)),
                                          file=fh_out)
                            else:
                                if descr_info.unit in ("CCITT IA5", "Numeric"):
                                    dinf_unit = ""
                                else:
                                    dinf_unit = descr_info.unit
                                if descr_entry.value is None:
                                    print("%06d %-40s = /// %s" %
                                          (descr_entry.descr, descr_info.name,
                                           dinf_unit),
                                          file=fh_out)
                                elif descr_entry.quality is not None:
                                    print("%06d %-40s = %s %s (%s)" %
                                          (descr_entry.descr, descr_info.name,
                                           str(descr_entry.value), dinf_unit,
                                           descr_entry.quality),
                                          file=fh_out)
                                else:
                                    print("%06d %-40s = %s %s" %
                                          (descr_entry.descr, descr_info.name,
                                           str(descr_entry.value), dinf_unit),
                                          file=fh_out)
            except Exception as e:
                print("ERROR\t%s" % e, file=fh_out)
                if logger.isEnabledFor(logging.DEBUG):
                    logger.exception(e)
                else:
                    logger.warning(e)
    if fh_out is not sys.stdout:
        fh_out.close()