Script - Workflow template

This series of Python scripts provide an example and a template for automating the processing of data in DIAdem.   All of the data will be from the examples included with a DIAdem installation.  


# --------------------------------------------------------------------
#-- Python Script File
#-- Author:   Mechatronic Solutions LLC
#             Mark W Kiehl
#             www.SavvyDiademPySolutions.com
#             www.MechatronicSolutionsLLC.com
#-- License:  http://www.savvydiademsolutions.com/license.php
#-- Comment:  Demo typical DIAdem data processing workflow:
#               - Read raw data files & convert to TDMS
#               - Analyze data files
#               - Create View sheets from the data files
#               - Create Report sheets from the data files
# --------------------------------------------------------------------
from DIAdem import Application as dd

if False:
    import DIAdem_CodeCompletion as dd

# --------------------------------------------------------------------
dd.LogfileDel()

#import os.path
from platform import python_version
from pathlib import Path
import importlib

print('Script "' + dd.CurrentScriptName + '"  Python version ' + python_version())


FilePathLib = Path(dd.CurrentScriptPath + 'Lib_MechatronicSolutionsLLC.PY')
if not FilePathLib.exists():
    raise Exception('File not found ' + str(FilePathLib))

# --------------------------------------------------------------------

def liGetRawDataFilePathsToProcess():
    """ Modify the path and extension search arguments below to search for
        the raw data files of interest in the folder desired.
    """
    oDataPath = Path(os.getenv('userprofile')).joinpath('documents\data_to_import')
    if not oDataPath.exists():
        raise Exception('Folder not found "' + str(FilePathLib) + '"')
    li = []
    for oFilePath in oDataPath.glob('*.tdm*'):
        #print('\t' + str(Path(oFilePath)) + '\t' + str(type(oFilePath.name)))
        li.append(oFilePath)
    return li


def liRawDataSaveToTdms(oFolderTDMS, liRawDataFilePaths):
    """ Imports raw data files specified by liRawDataFilePaths,
        performing any necessary conversion, cleanup, attribution,
        etc. via the function ProcessTdmsFiles() to the files while 
        loaded into DIAdem, and then saves the files to oFolderTDMS. 
        
        Note that although this example reads DIAdem example TDM file (.tdm),
        as specified by liGetRawDataFilePathsToProcess(), the files could be 
        any type, provided a DataPlugin for the data type is installed 
        (see DIAdem .. Settings .. Extensions .. DataPlugins..).
        
    """
    if not oFolderTDMS.exists():
        raise Exception('Folder not found "' + str(oFolderTDMS) + '"')
    li = []
    for oFilePathRaw in liRawDataFilePaths:
        print('\t' + str(oFilePathRaw))
        dd.Data.Root.Clear()
        dd.DataFileLoad(str(oFilePathRaw),"TDM")
        oFilePathTDMS = oFolderTDMS.joinpath(oFilePathRaw.stem + ".tdms")
        dd.DataFileSave(str(oFilePathTDMS), "TDMS")
        li.append(oFilePathTDMS)
    return li

def ProcessTdmsFiles(liFilePathsTDMS):
    """ Processes the data file currently loaded into the
        DIAdem Data Portal (internal data).  Customize this 
        function to do whatever you like.  
    """
    for oFilePathTDMS in liFilePathsTDMS:
        dd.Data.Root.Clear()
        dd.DataFileLoad(str(oFilePathTDMS),"TDMS")
        oGrp = dd.Data.Root.ChannelGroups.Item(1)   #.Item(name or index)
        oChnX = oGrp.Channels.Item('time')  # oChnX = oGrp.Channels.Item(1)
        oChnY = oGrp.Channels.Item('TOELEnew')
        StatsSelection = dd.eStatsMinimum + dd.eStatsMaximum + dd.eStatsArithmeticMean + dd.eStatsSquareMean + dd.eStatsGeometricMean
        dd.ChnStatisticsChannelCalc(oChnY, StatsSelection,  None,  None, False, False, False, "NameName")
        #The statistics max, min, mean, median, .. were added as properties to the channel 'TOELEnew'
        
        """
        #Iterate over the numeric channels in oGrp (Data Portal / internal data).
        for c in range(1, oGrp.Channels.Count+1):
            oChnY = oGrp.Channels.Item(c)
        """
        
        """
        #Iterate over all channel groups and channels in the Data Portal (internal data).
        print('File: "' + dd.Data.Root.Name + '"')
        for oGrp in dd.Data.Root.ChannelGroups:
            print('\t ' + oGrp.name)
            for c in range(1, oGrp.Channels.Count+1):
                print('\t\t' + oGrp.Channels.Item(c).Name)
        """
        dd.DataFileSave(str(oFilePathTDMS), "TDMS")
    dd.Data.Root.Clear() 


def CreateViewSheets(liFilePathsTDMS):
    dd.View.NewLayout()
    

def CreateReportSheets(liFilePathsTDMS):
    dd.Report.NewLayout()



#Get the file/paths to the raw data files..
dd.Data.Root.Clear()
liRawDataFilePaths = liGetRawDataFilePathsToProcess()
if len(liRawDataFilePaths) == 0:
    raise Exception('No raw data files found.  Check ProcessTdmsFiles()')

#Create a temporary folder where the raw files will be saved as TDMS
oFolderTDMS = Path(os.getenv('temp')).joinpath('tdms')
if oFolderTDMS.exists():
    for oFilePath in oFolderTDMS.glob('*.*'):
        oFilePath.unlink()
    oFolderTDMS.rmdir()
oFolderTDMS.mkdir()
liFilePathsTDMS = liRawDataSaveToTdms(oFolderTDMS, liRawDataFilePaths)
print('\n')
for li in liFilePathsTDMS:
    print('\t' + str(li))

#Analyze each of the TDMS file.
ProcessTdmsFiles(liFilePathsTDMS)

#Create View sheets for each TDMS file.
CreateViewSheets(liFilePathsTDMS)

#Create Report sheets for each TDMS file.
CreateReportSheets(liFilePathsTDMS)

Under Development

 


Do you need help customizing DIAdem using Python for your specific test measurement data management needs?   Click on the button below to book a free 1 hour phone / web share consultation.