Skip to content

Instantly share code, notes, and snippets.

@PeterWaIIace
Last active July 6, 2020 00:05
Show Gist options
  • Save PeterWaIIace/6fbc2571ab1df3a3c9d0908ad479bf12 to your computer and use it in GitHub Desktop.
Save PeterWaIIace/6fbc2571ab1df3a3c9d0908ad479bf12 to your computer and use it in GitHub Desktop.
GNURadio meta files load to python - scirpt for loading GNURadio meta files into numpy array. Additionaly you can access small chunks of data.
# gnu radia meta file load
from gnuradio.blocks import parse_file_metadata as pfm
from gnuradio.gr import pmt
import numpy as np
import scipy
# segment, index and length is only required if you want to access specific chunk of data. You will always get info for that chuck
# Setting segment specify which segment you want get, index specify from which index from that segment function should start reading,
# and length specify how long the function should od reading
def open_grmetafile(filename,max_segments,segment=None,index=None,length=None):
v = np.array([])
nbytes=0
strt = 0
infos = []
n=0
with open(filename,"rb") as f:
content=f.read()
print(type(segment))
if None != segment and type(segment)==int:
print("Getting: ",segment)
pmtcontainer=pmt.deserialize_str(content[8000171*segment:8000171*(segment+1)])
info=pfm.parse_header(pmtcontainer)
extra=pfm.parse_extra_dict(pmtcontainer,info)
infos.append(extra)
strt=pmt.to_uint64(extra["strt"])
nbytes=extra["nbytes"]
savetofile = content[strt:strt+nbytes]
with open("tmp","wb+") as f:
f.write(savetofile)
savetofile = None
if None == index and None == length:
index = 0
length = 1000000
dataframe=np.fromfile(open("tmp"), dtype=np.complex64)[index:index+length]
v=np.append(v,dataframe)
dataframe = None
else:
while(True):
print("Turn: ",n,len(content))
pmtcontainer=pmt.deserialize_str(content[:8000171]) # 8000171 was max for me
info=pfm.parse_header(pmtcontainer)
extra=pfm.parse_extra_dict(pmtcontainer,info)
# print(info,extra)
infos.append(extra)
strt=pmt.to_uint64(extra["strt"])
nbytes=extra["nbytes"]
savetofile = content[strt:strt+nbytes]
with open("tmp","wb+") as f:
f.write(savetofile)
savetofile = None
dataframe=np.fromfile(open("tmp"), dtype=np.complex64)
v=np.append(v,dataframe)
dataframe = None
content=content[strt+nbytes:]
if(len(content)<1 or n > max_segments):
print(nbytes,len(content),n,max_segments)
break
n+=1
return(infos,v) # returns info for last block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment