def test_encode_decode(): ens = EnsembleData() ens.EnsembleNumber = 2668 ens.NumBins = 37 ens.NumBeams = 4 ens.DesiredPingCount = 45 ens.ActualPingCount = 46 ens.SerialNumber = "01H00000000000000000000000999999" ens.SysFirmwareMajor = 2 ens.SysFirmwareMinor = 11 ens.SysFirmwareRevision = 5 ens.SysFirmwareSubsystemCode = "A" ens.SubsystemConfig = 3 ens.Status = 9 ens.Year = 2019 ens.Month = 3 ens.Day = 9 ens.Hour = 12 ens.Minute = 23 ens.Second = 24 ens.HSec = 33 bin_Data = ens.encode() # Encode ens1 = EnsembleData() ens1.decode(bytearray(bin_Data)) # Decode assert ens.EnsembleNumber == ens1.EnsembleNumber assert ens.NumBins == ens1.NumBins assert ens.NumBeams == ens1.NumBeams assert ens.DesiredPingCount == ens1.DesiredPingCount assert ens.ActualPingCount == ens1.ActualPingCount assert ens.SerialNumber == ens1.SerialNumber assert ens.SysFirmwareMajor == ens1.SysFirmwareMajor assert ens.SysFirmwareMinor == ens1.SysFirmwareMinor assert ens.SysFirmwareRevision == ens1.SysFirmwareRevision assert ens.SysFirmwareSubsystemCode == ens1.SysFirmwareSubsystemCode assert ens.SubsystemConfig == ens1.SubsystemConfig assert ens.Status == ens1.Status assert ens.Year == ens1.Year assert ens.Month == ens1.Month assert ens.Day == ens1.Day assert ens.Hour == ens1.Hour assert ens.Minute == ens1.Minute assert ens.Second == ens1.Second assert ens.HSec == ens1.HSec
def decode_data_sets(ens): """ Decode the datasets in the ensemble. Use verify_ens_data if you are using this as a static method to verify the data is correct. :param ens: Ensemble data. Decode the dataset. :return: Return the decoded ensemble. """ #print(ens) packetPointer = Ensemble().HeaderSize type = 0 numElements = 0 elementMultiplier = 0 imag = 0 nameLen = 0 name = "" dataSetSize = 0 ens_len = len(ens) # Create the ensemble ensemble = Ensemble() # Add the raw data to the ensemble #ensemble.AddRawData(ens) try: # Decode the ensemble datasets for x in range(Ensemble().MaxNumDataSets): # Check if we are at the end of the payload if packetPointer >= ens_len - Ensemble.ChecksumSize - Ensemble.HeaderSize: break try: # Get the dataset info ds_type = Ensemble.GetInt32(packetPointer + (Ensemble.BytesInInt32 * 0), Ensemble().BytesInInt32, ens) num_elements = Ensemble.GetInt32(packetPointer + (Ensemble.BytesInInt32 * 1), Ensemble().BytesInInt32, ens) element_multiplier = Ensemble.GetInt32(packetPointer + (Ensemble.BytesInInt32 * 2), Ensemble().BytesInInt32, ens) image = Ensemble.GetInt32(packetPointer + (Ensemble.BytesInInt32 * 3), Ensemble().BytesInInt32, ens) name_len = Ensemble.GetInt32(packetPointer + (Ensemble.BytesInInt32 * 4), Ensemble().BytesInInt32, ens) name = str(ens[packetPointer+(Ensemble.BytesInInt32 * 5):packetPointer+(Ensemble.BytesInInt32 * 5)+8], 'UTF-8') except Exception as e: logging.warning("Bad Ensemble header" + str(e)) break # Calculate the dataset size data_set_size = Ensemble.GetDataSetSize(ds_type, name_len, num_elements, element_multiplier) # Beam Velocity if "E000001" in name: logging.debug(name) bv = BeamVelocity(num_elements, element_multiplier) bv.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddBeamVelocity(bv) # Instrument Velocity if "E000002" in name: logging.debug(name) iv = InstrumentVelocity(num_elements, element_multiplier) iv.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddInstrumentVelocity(iv) # Earth Velocity if "E000003" in name: logging.debug(name) ev = EarthVelocity(num_elements, element_multiplier) ev.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddEarthVelocity(ev) # Amplitude if "E000004" in name: logging.debug(name) amp = Amplitude(num_elements, element_multiplier) amp.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddAmplitude(amp) # Correlation if "E000005" in name: logging.debug(name) corr = Correlation(num_elements, element_multiplier) corr.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddCorrelation(corr) # Good Beam if "E000006" in name: logging.debug(name) gb = GoodBeam(num_elements, element_multiplier) gb.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddGoodBeam(gb) # Good Earth if "E000007" in name: logging.debug(name) ge = GoodEarth(num_elements, element_multiplier) ge.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddGoodEarth(ge) # Ensemble Data if "E000008" in name: logging.debug(name) ed = EnsembleData(num_elements, element_multiplier) ed.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddEnsembleData(ed) # Ancillary Data if "E000009" in name: logging.debug(name) ad = AncillaryData(num_elements, element_multiplier) ad.decode(ens[packetPointer:packetPointer+data_set_size]) ensemble.AddAncillaryData(ad) # Bottom Track if "E000010" in name: logging.debug(name) bt = BottomTrack(num_elements, element_multiplier) bt.decode(ens[packetPointer:packetPointer + data_set_size]) ensemble.AddBottomTrack(bt) # NMEA data if "E000011" in name: logging.debug(name) nd = NmeaData(num_elements, element_multiplier) nd.decode(ens[packetPointer:packetPointer + data_set_size]) ensemble.AddNmeaData(nd) # System Setup if "E000014" in name: logging.debug(name) ss = SystemSetup(num_elements, element_multiplier) ss.decode(ens[packetPointer:packetPointer + data_set_size]) ensemble.AddSystemSetup(ss) # Range Tracking if "E000015" in name: logging.debug(name) rt = RangeTracking(num_elements, element_multiplier) rt.decode(ens[packetPointer:packetPointer + data_set_size]) ensemble.AddRangeTracking(rt) # Move to the next dataset packetPointer += data_set_size except Exception as e: logging.warning("Error decoding the ensemble. " + str(e)) return None return ensemble