def test_serialize(self): """Message can be serialized to file.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) with open(TEST_OUTPUT, "w") as test: msg.write(test) os.unlink(TEST_OUTPUT)
def test_metadata(self): """Metadata is read correctly from GribMessage.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) message_length = 245 if DWD else 243 self.assertEqual(len(msg), message_length) self.assertEqual(msg.size(), 160219) self.assertEqual(len(msg.keys()), 245)
def test_iteration_works(self): """The GribFile allows proper iteration over all messages.""" step_ranges = [] with GribFile(TESTGRIB) as grib: for i in range(len(grib)): msg = GribMessage(grib) step_ranges.append(msg["stepRange"]) self.assertSequenceEqual(step_ranges, ["0", "6", "12", "18", "24"])
def test_missing_message_behavior(self): """Missing messages are detected properly.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) self.assertTrue(msg.missing("scaleFactorOfSecondFixedSurface")) msg["scaleFactorOfSecondFixedSurface"] = 5 msg.set_missing("scaleFactorOfSecondFixedSurface") with self.assertRaises(KeyError): msg["scaleFactorOfSecondFixedSurface"]
def test_memory_management(self): """Messages in GribFile can be opened and closed properly.""" with GribFile(TESTGRIB) as grib: self.assertEqual(len(grib), 5) for i in range(len(grib)): msg = GribMessage(grib) short_name = "P" if DWD else "msl" self.assertEqual(msg["shortName"], short_name) self.assertEqual(len(grib.open_messages), 5) self.assertEqual(len(grib.open_messages), 0)
def test_clone(self): """Messages can be used to produce clone Messages.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) msg2 = GribMessage(clone=msg) self.assertSequenceEqual(msg.keys(), msg2.keys())
def test_value_setting(self): """Keys can be set properly.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) msg["scaleFactorOfSecondFixedSurface"] = 5 msg["values"] = [1, 2, 3]
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Nov 28 17:04:57 2016 @author: wolfensb """ from pyth_grib import GribFile, GribIndex, GribMessage from pyth_grib.gribmessage import IndexNotSelectedError filename = './lfsf00132500' with GribFile(filename) as grib: for i in range(len(grib)): msg = GribMessage(grib) print(msg["shortName"])