Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Last active November 28, 2021 05:54
Show Gist options
  • Save jj-github-jj/b0ab49745c16eba683c9421fcf0008ff to your computer and use it in GitHub Desktop.
Save jj-github-jj/b0ab49745c16eba683c9421fcf0008ff to your computer and use it in GitHub Desktop.
tdms utilities
tdms_file=TdmsFile.read(fpath)
for name, value in tdms_file.properties.items():
print("{0}: {1}".format(name, value))
print ("------ Groups --------")
all_groups = tdms_file.groups()
for group in all_groups:
print ("Group Name:",group.name)
print ("----------Group Details---------")
maxchannels=20
for group in all_groups:
print ("Group Name:",group.name)
print ("\t","Group Properties:","\t",group.properties)
for ch in group.channels()[0:maxchannels]:
print ("\t","Channel Name:",ch.name,":Channel Properties:", ch.properties)
if (len(group.channels()) > maxchannels):
print ("\t ....")
for ch in group.channels()[-1*maxchannels:]:
print ("\t","Channel Name:",ch.name,":Channel Properties:", ch.properties)
def tdms_print_contents (tdms_file):
for group in tdms_file.groups() :
print ("Group:" ,group.name)
for channel in group.channels():
print ("\t","Group:",group.name, ":","Channel:",channel.name)
def size_lv_flatten_to_string (data,dimension=0):
"""
returns size of array strored by lv in preprend of flatten to string from array - single dim =[0..3],
2d array has two of these and so on. Single dimesnion is 0 not 1
labview prepends with array size in tdms files by default
"""
len1=channel.data[dimension*4:dimension*4+4] # 0 is 0:4, 4:8
len1 = len1[::-1] #reverse
len_bytes = np.array (len1, dtype=np.uint8) #convert to numpy array
data_as_i32 = len_bytes.view(dtype=np.int32)
return (data_as_i32[0])
size_lv_flatten_to_string (channel.data,dimension=0)
# --- doubles stored as bytes using flatten to string then stirng to byte in tdms
d2d = np.reshape(data, (x_array_size, y_array_size*8))
print ("Data 2d shape",d2d.shape)
d2d_as_dfloat = d2d.view(dtype='>f8') #use >f8 or <f8 to interpret the bytes as float64 with correct byte order
d2d_as_dfloat
print ("Data 2 d float shape",d2d_as_dfloat.shape)
y=d2d_as_dfloat[0]
y.shape
with TdmsFile.open("path_to_file.tdms"):
channel = tdms_file[group_name][channel_name]
channel_data = channel[:]
single_data = tdms_file['data']['xmult'][0] # group then channel
# explore tdms file groups channels
for name, value in tdms_file.properties.items():
print("{0}: {1}".format(name, value))
all_groups = tdms_file.groups()
print ("------ Groups --------")
for group in all_groups:
print ("Group Name:",group.name)
print ("----------Group Details---------")
maxchannels=20
for group in all_groups:
print ("Group Name:",group.name)
print ("\t","Group Properties:","\t",group.properties)
for ch in group.channels()[0:maxchannels]:
print ("\t","Channel Name:",ch.name,":Channel Properties:", ch.properties)
if (len(group.channels()) > maxchannels):
print ("\t ....")
for ch in group.channels()[-1*maxchannels:]:
print ("\t","Channel Name:",ch.name,":Channel Properties:", ch.properties)
# tdms_file[group_name][channel_name]
tdms_file['group name'].channels()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment