def ocop2df(filepath, ): file = TF(filepath) #find the group name that the normalized data is in normdata_groupname = None normdata_regex = "(.+_Norm)" for group in file.groups(): m = re.search(normdata_regex, group) if m != None: normdata_groupname = m.groups()[0] break if (normdata_groupname == None): print('could not find Norm group in ' + filepath) return pd.DataFrame() df = file.object(normdata_groupname).as_dataframe() df.index = file.object('Global', "Wavelength").data indexarr = list( zip(*[ file.object('Global', 'MP Pos').data, file.object('Global', 'Time').data ])) df.columns = pd.MultiIndex.from_tuples(indexarr, names=['MP', 'Wavelength']) return df # filepath = "C:\\Labview Test Data\\2018-11-20\\UnspecifiedProj\\Run3\\Log_NIRQuest512_0_Case5_seed_0.tdms" # # df = ocop2df(filepath) # file = TF(filepath)
def cut_log_spectra(fileinpaths, times, fileoutpaths_list, **kwargs): for i, fileinpath in enumerate(fileinpaths): fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j, t in enumerate(times): fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={}) try: with TdmsWriter(fileoutpath, mode='w') as tdmswriter: for group in tdmsfile.groups().remove('Global'): idx1, idx2 = _get_indextime(timedate, t[0], t[1]) for channel in file.group_channels(group)[idx1, idx2]: tdms_writer.write_segment([root_object, channel]) for channel in tdmsfile.group_channels('Global'): if channel.channel == 'Wavelength': channel_object = channel else: channel_object = _cut_channel(channel, time[0], time[1], timedata=None) tdms_writer.write_segment( [root_object, channel_object]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def import_data(self, string): #Will import all data in the defined file path with substring input pathnames = self.pathnames imports = self.imports for pathname in pathnames: if string in pathname: imports[pathname] = TF(pathname) self.imports = imports return
def ocop2df(filepath, ): file = TF(filepath) df = file.object(file.groups()[2]).as_dataframe() df.index = file.object('Global', "Wavelength").data indexarr = list( zip(*[ file.object('Global', 'MP Pos').data, file.object('Global', 'Time').data ])) df.columns = pd.MultiIndex.from_tuples(indexarr, names=['MP', 'Wavelength']) return df
def cut_log_spectra(fileinpaths, times, fileoutpaths_list, **kwargs): for i, fileinpath in enumerate(fileinpaths): fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j, t in enumerate(times): fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={}) try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: timedata = [ dt64(y) for y in tdmsfile.channel_data('Global', 'Time') ] idx1, idx2 = _get_indextime(timedata, t[0], t[1]) if idx1 == idx2: pass else: for group in tdmsfile.groups(): group_object = GroupObject(group, properties={}) if group == "Global": for channel in tdmsfile.group_channels(group): if channel.channel == 'Wavelength': channel_object = ChannelObject( channel.group, channel.channel, channel.data) else: channel_object = ChannelObject( channel.group, channel.channel, channel.data[idx1:idx2]) tdms_writer.write_segment([ root_object, group_object, channel_object ]) else: for channel_object in tdmsfile.group_channels( group)[idx1:idx2]: tdms_writer.write_segment([ root_object, group_object, channel_object ]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def getlaserdata(): #pull the laser profile from a specific tdms file. The format of this tdms file is likely only used once pathnames_laser = _get_pathnames( "C:\\Users\\aspit\\OneDrive\\Data\\LaserProfile") file_laser = TF(pathnames_laser['Test1_20Hz.tdms']) laser_common = file_laser.object('Raw').as_dataframe() laser_data = file_laser.object('Average').as_dataframe() laser_time = laser_common['Time1'] laser_data = laser_data['Mean'] laser_data_norm = laser_data / laser_data.max() #offset_time = 870 offset_time = 35 laser_time_off = laser_time - offset_time laserseries = pd.Series(laser_data_norm.values, index=laser_time_off) return laserseries
def cut_powermeter(fileinpaths, times, fileoutpaths_list, **kwargs): """Cut up a power meter tdms file based on input times.""" localtz = tzlocal.get_localzone() for i in range(len(fileinpaths)): fileinpath = fileinpaths[i] fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j in range(len(times)): time1 = times[j][0].astype('O') time1 = time1.replace(tzinfo=pytz.utc) #convert to datetime time2 = times[j][1].astype('O') time2 = time2.replace(tzinfo=pytz.utc) fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={ #TODO root properties }) try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: for group in tdmsfile.groups(): timedata = tdmsfile.channel_data(group, 'Time_LV') for channel in tdmsfile.group_channels(group): if type(channel.data_type.size) == type(None): break #skips over non numeric channels channel_object = _cut_channel(channel, time1, time2, timedata=timedata) tdms_writer.write_segment( [root_object, channel_object]) timechannel = tdmsfile.object(group, 'Time_LV') timechannel_cut = _cut_datetime_channel( timechannel, time1, time2) tdms_writer.write_segment( [root_object, timechannel_cut]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def open_tdmsfile(self, filepath=0): """ Loads in a tdms file to be displayed for parsing. This function also searches for the eventlog in higher folders. """ if (filepath == 0): paths = QtWidgets.QFileDialog.getOpenFileName( MainWindow, 'Open File', self.ppsettings['defaultpath']) filepath = paths[0] if (filepath == ''): pass else: self.Logfiletdms = TF(filepath) self.logfilepath = filepath folder = os.path.split(filepath)[0] with open(self.settingspath, 'w') as writefile: self.ppsettings['defaultpath'] = folder json.dump(self.ppsettings, writefile) #search upward in file directory for eventlog.json, then set that as the date folder while (True): filelist = os.listdir(folder) if 'Eventlog.json' in filelist: self.datefolder = folder eventlogpath = os.path.join(self.datefolder, 'Eventlog.json') with open(eventlogpath) as fp: self.jsonfile = json.load(fp) break folder = os.path.split(folder)[0] #pull out groups and populate the group display self.groups = self.Logfiletdms.groups() self.selectGroup.clear() self.selectGroup.insertItems(0, self.groups) self.selectGroup.setCurrentRow(0) self.update_channel_display() self.newfile_update()
def cut_log_file(fileinpaths, times, fileoutpaths_list, **kwargs): """ Cuts up a log file based on the supplied times. This function assumes that the channels are waveforms. """ for i, fileinpath in enumerate(fileinpaths): fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j in range(len(times)): time1 = times[j][0] time2 = times[j][1] fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={ #TODO root properties }) try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: for group in tdmsfile.groups(): for channel in tdmsfile.group_channels(group): channel_object = _cut_channel(channel, time1, time2, timedata=None) tdms_writer.write_segment( [root_object, channel_object]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def cut_log_file(fileinpaths, times, fileoutpaths_list, **kwargs): """ Cuts up a log file based on the supplied times. This function assumes that the channels are waveforms. """ for i, fileinpath in enumerate(fileinpaths): fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j in range(len(times)): time1 = times[j][0] time2 = times[j][1] fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={ #TODO root properties }) timegroupwritten = False try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: for group in tdmsfile.groups(): if 'TimeChannelName' in kwargs: if 'TimeGroupName' in kwargs: timegroup = kwargs['TimeGroupName'] else: timegroup = group timegroupwritten = False timechannel = tdmsfile.object( timegroup, kwargs['TimeChannelName']) timedata = timechannel.data timedata = np.array( list(map(lambda x: np.datetime64(x), timedata))) timedata = timedata.astype('M8[us]') if timegroupwritten == False: timechannel_cut = _cut_datetime_channel( timechannel, time1, time2) tdms_writer.write_segment( [root_object, timechannel_cut]) timegroupwritten = True waveform = False else: waveform = True for channel in tdmsfile.group_channels(group): # if type(channel.data_type.size) == type(None): break #skips over non numeric channels if channel.data_type == nptdms.types.DoubleFloat: if waveform: timedata = channel.time_track( absolute_time=True) idx1, idx2 = _get_indextime( timedata, time1, time2) channel_object = _cut_channel( channel, idx1, idx2, waveform) tdms_writer.write_segment( [root_object, channel_object]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def import_data(pathnames, filename): #Will import all data in the defined file path with substring input return TF(pathnames[filename])