def test_encode_decode(): num_bins = 30 num_beams = 4 vel = EarthVelocity(num_bins, num_beams) # Populate data val = 1.0 for beam in range(vel.element_multiplier): for bin_num in range(vel.num_elements): vel.Velocities[bin_num][beam] = val val += 1.1 vel.Magnitude, vel.Direction = EarthVelocity.generate_vectors( vel.Velocities) result = vel.encode() vel1 = EarthVelocity(num_bins, num_beams) vel1.decode(bytearray(result)) for beam in range(vel1.element_multiplier): for bin_num in range(vel1.num_elements): assert vel.Velocities[bin_num][beam] == pytest.approx( vel1.Velocities[bin_num][beam], 0.1) assert vel.Magnitude[bin_num] == pytest.approx( vel1.Magnitude[bin_num], 0.1) assert vel.Direction[bin_num] == pytest.approx( vel1.Direction[bin_num], 0.1)
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