示例#1
0
def write_message(topic, payload, outfile):
    try:
        m = parse_message(topic, payload)
        if m is None:
            return
        msg = dballe.Message("generic")
        if m["ident"] is not None:
            msg.set_named("ident", dballe.var("B01011", m["ident"]))
        msg.set_named("longitude", dballe.var("B06001", m["lon"]))
        msg.set_named("latitude", dballe.var("B05001", m["lat"]))
        msg.set_named("rep_memo", dballe.var("B01194", m["rep_memo"]))
        msg.set_named("year", dballe.var("B04001", m["datetime"].year))
        msg.set_named("month", dballe.var("B04002", m["datetime"].month))
        msg.set_named("day", dballe.var("B04003", m["datetime"].day))
        msg.set_named("hour", dballe.var("B04004", m["datetime"].hour))
        msg.set_named("minute", dballe.var("B04005", m["datetime"].minute))
        msg.set_named("second", dballe.var("B04006", m["datetime"].second))

        var = dballe.var(m["var"], m["value"])

        for b, v in m["attributes"].items():
            var.seta(dballe.var(b, v))

        msg.set(m["level"], m["trange"], var)

        exporter = dballe.Exporter(encoding="BUFR")
        outfile.write(exporter.to_binary(msg))
        outfile.flush()
    except Exception:
        import traceback
        traceback.print_exc()
示例#2
0
 def test_empty(self):
     msg = dballe.Message("synop")
     self.assertEqual(str(msg), "Message")
     self.assertEqual(repr(msg), "dballe.Message object")
     self.assertEqual(msg.type, "synop")
     self.assertIsNone(msg.datetime)
     self.assertIsNone(msg.coords)
     self.assertIsNone(msg.ident)
     self.assertEqual(msg.report, "synop")
示例#3
0
def do_qc(input_file, output_file, preserve):
    importer = dballe.Importer("BUFR")
    exporter = dballe.Exporter("BUFR")

    with importer.from_file(input_file) as fp:
        for msgs in fp:
            for msg in msgs:
                count_vars = 0
                new_msg = dballe.Message("generic")

                new_msg.set_named("year", msg.datetime.year)
                new_msg.set_named("month", msg.datetime.month)
                new_msg.set_named("day", msg.datetime.day)
                new_msg.set_named("hour", msg.datetime.hour)
                new_msg.set_named("minute", msg.datetime.minute)
                new_msg.set_named("second", msg.datetime.second)
                new_msg.set_named("rep_memo", msg.report)
                new_msg.set_named("longitude", int(msg.coords[0] * 10 ** 5))
                new_msg.set_named("latitude", int(msg.coords[1] * 10 ** 5))
                if msg.ident:
                    new_msg.set_named("ident", msg.ident)

                for data in msg.query_data({"query": "attrs"}):
                    variable = data["variable"]
                    attrs = variable.get_attrs()
                    is_ok = pass_qc(attrs)
                    v = dballe.var(
                        data["variable"].code, data["variable"].get()
                    )

                    if not is_ok:
                        if preserve:
                            v.seta(dballe.var("B33007", 0))
                        else:
                            continue

                    new_msg.set(data["level"], data["trange"], v)
                    count_vars += 1

                for data in msg.query_station_data({"query": "attrs"}):
                    variable = data["variable"]
                    attrs = variable.get_attrs()
                    v = dballe.var(
                        data["variable"].code, data["variable"].get()
                    )
                    for a in attrs:
                        v.seta(a)

                    new_msg.set(dballe.Level(), dballe.Trange(), v)

                if count_vars > 0:
                    output_file.write(exporter.to_binary(new_msg))
示例#4
0
    def test_iterate(self):
        """
        Try iterating the message with cursors
        """
        msg = dballe.Message("synop")
        msg.set_named("year", dballe.var("B04001", 2009))
        msg.set_named("month", dballe.var("B04002", 2))
        msg.set_named("day", dballe.var("B04003", 24))
        msg.set_named("hour", dballe.var("B04004", 11))
        msg.set_named("minute", dballe.var("B04005", 31))
        msg.set_named("latitude", dballe.var("B05001", 48.90500))
        msg.set_named("longitude", dballe.var("B06001", 10.63667))
        msg.set(None, None, dballe.var("B01019", "Test"))
        lv = dballe.Level(1, 0, None, None)
        tr = dballe.Trange(254, 0, 0)
        msg.set(lv, tr, dballe.var("B11001", 33))
        msg.set(lv, tr, dballe.var("B12101", 240.0))

        count = 0
        for cur in msg.query_stations():
            self.assertEqual(cur["lat"], Decimal('48.90500'))
            self.assertEqual(cur["lon"], Decimal('10.63667'))
            count += 1
        self.assertEqual(count, 1)

        count = 0
        expected = {
            "B04001": 2009,
            "B04002": 2,
            "B04003": 24,
            "B04004": 11,
            "B04005": 31,
            "B05001": 48.90500,
            "B06001": 10.63667,
            "B01019": "Test",
        }
        for cur in msg.query_station_data():
            val = expected.pop(cur["var"])
            self.assertEqual(cur["variable"].enq(), val)
            count += 1
        self.assertEqual(expected, {})
        self.assertEqual(count, 8)

        res = []
        for cur in msg.query_data():
            res.append((cur["var"], cur[cur["var"]].enq()))
        self.assertEqual(res, [("B11001", 33), ("B12101", 240.0)])
示例#5
0
def on_message(client, userdata, message):
    try:
        m = parse_message(message.topic, message.payload.decode("utf-8"))
        if m is None:
            return
        msg = dballe.Message("generic")
        if m["ident"] is not None:
            msg.set_named("ident", dballe.var("B01011", m["ident"]))

        msg.set_named("longitude", dballe.var("B06001", m["lon"]))
        msg.set_named("latitude", dballe.var("B05001", m["lat"]))
        msg.set_named("rep_memo", dballe.var("B01194", m["rep_memo"]))

        if all([
                m["level"] != (None, None, None, None),
                m["trange"] != (None, None, None),
                userdata["overwrite_date"],
        ]):
            m["datetime"] = datetime.utcnow()

        if m["datetime"] is not None:
            msg.set_named("year", dballe.var("B04001", m["datetime"].year))
            msg.set_named("month", dballe.var("B04002", m["datetime"].month))
            msg.set_named("day", dballe.var("B04003", m["datetime"].day))
            msg.set_named("hour", dballe.var("B04004", m["datetime"].hour))
            msg.set_named("minute", dballe.var("B04005", m["datetime"].minute))
            msg.set_named("second", dballe.var("B04006", m["datetime"].second))

        var = dballe.var(m["var"], m["value"])

        for b, v in m["attributes"].items():
            var.seta(dballe.var(b, v))

        msg.set(m["level"], m["trange"], var)

        exporter = dballe.Exporter(encoding="BUFR")
        userdata["outfile"].write(exporter.to_binary(msg))
        userdata["outfile"].flush()
    except Exception:
        import traceback
        traceback.print_exc()
示例#6
0
    def test_set_named(self):
        msg = dballe.Message("synop")

        msg.set_named("year", dballe.var("B04001", 2009))
        var = msg.get_named("year")
        self.assertEqual(var.code, "B04001")
        self.assertEqual(var.enqi(), 2009)

        msg.set_named("year", 2009)
        var = msg.get_named("year")
        self.assertEqual(var.code, "B04001")
        self.assertEqual(var.enqi(), 2009)

        msg.set_named("year", 2009.0)
        var = msg.get_named("year")
        self.assertEqual(var.code, "B04001")
        self.assertEqual(var.enqi(), 2009)

        msg.set_named("year", "2009")
        var = msg.get_named("year")
        self.assertEqual(var.code, "B04001")
        self.assertEqual(var.enqi(), 2009)
示例#7
0
    def make_gts_acars_uk1_message(self):
        msg = dballe.Message("amdar")
        msg.set_named("year", dballe.var("B04001", 2009))
        msg.set_named("month", dballe.var("B04002", 2))
        msg.set_named("day", dballe.var("B04003", 24))
        msg.set_named("hour", dballe.var("B04004", 11))
        msg.set_named("minute", dballe.var("B04005", 31))
        msg.set_named("ident", dballe.var("B01011", "EU3375"))
        msg.set_named("latitude", dballe.var("B05001", 48.90500))
        msg.set_named("longitude", dballe.var("B06001", 10.63667))

        lv = dballe.Level(102, 6260000, None, None)
        tr = dballe.Trange(254, 0, 0)
        msg.set(lv, tr, dballe.var("B01006", "LH968"))
        msg.set(lv, tr, dballe.var("B02061", 0))
        msg.set(lv, tr, dballe.var("B02062", 3))
        msg.set(lv, tr, dballe.var("B02064", 0))
        msg.set(lv, tr, dballe.var("B07030", 6260.0))
        msg.set(lv, tr, dballe.var("B08004", 3))
        msg.set(lv, tr, dballe.var("B11001", 33))
        msg.set(lv, tr, dballe.var("B11002", 33.4))
        msg.set(lv, tr, dballe.var("B12101", 240.0))
        msg.set(lv, tr, dballe.var("B13002", 0.0))
        return msg