User Tools

Site Tools


ch10summary.py

This is an old revision of the document!


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 08:37:08 2018
 
@author: BBaggerman
"""
 
import sys
import os
 
import Py106
import Py106.MsgDecodeTMATS
import Py106.Time
 
# -----------------------------------------------------------------------------
 
def RecorderVer2String(RecVersion):
    if   (RecVersion == 0):
        return "106-05 or earlier"
    elif (RecVersion == 7):
        return "106-07"
    elif (RecVersion == 8):
        return "106-09"
    elif (RecVersion == 9):
        return "106-11"
    elif (RecVersion == 10):
        return "106-13"
    elif (RecVersion == 11):
        return "106-15"
    elif (RecVersion == 12):
        return "106-17"
    elif (RecVersion == 13):
        return "106-19"
    else:
        return "Unknown"
 
 
# -----------------------------------------------------------------------------
 
# Find the R index and R\DSI index for a given Channel ID number
def Find_R_DSI_Num(ChID):
    sNumIndexes = DecodeTmats.find("R-1\\N")
    if sNumIndexes == "":
        return None
 
    ReturnVal = None    
    for Index in range(1,int(sNumIndexes)+1):
        sTMATS = "R-1\\TK1-{0}".format(Index)
        sChID = DecodeTmats.find(sTMATS)
        if int(sChID) == ChID:
            ReturnVal = (1, Index)
 
    return ReturnVal
 
 
# -----------------------------------------------------------------------------
 
def Make_Ch10_Summary(Ch10Filename, OutputHandle):
 
    print ("", file=OutputHandle)
    print ("-----------------------------------", file=OutputHandle)
    print ("IRIG 106 Ch 10/11 Data File Summary", file=OutputHandle)
    print ("-----------------------------------", file=OutputHandle)
 
    # Get some info about the file
    # ----------------------------
 
    FilePath,FileName = os.path.split(Ch10Filename)
    FileSize = os.path.getsize(Ch10Filename)
 
    # Read the file contents
    # ----------------------
 
    # Initialize counts variables
    Counts = {}
 
    RetStatus = PktIO.open(Ch10Filename, Py106.Packet.FileMode.READ)
    if RetStatus != Py106.Status.OK :
        print ("Error opening data file '{0}'".format(Ch10Filename), file=sys.stderr)
        return
 
    TimeUtils.SyncTime(False, 0)
 
    # Using Python iteration
    for PktHdr in PktIO.packet_headers():
        # Check for TMATS
        if PktHdr.DataType == Py106.Packet.DataType.TMATS:
            PktIO.read_data()
            status = DecodeTmats.decode_tmats()
 
        if (PktHdr.ChID, PktHdr.DataType) in Counts:
            Counts[(PktHdr.ChID, PktHdr.DataType)] += 1
        else:
            Counts[(PktHdr.ChID, PktHdr.DataType)]  = 1
 
    # Gather some info
    # ----------------
 
    IrigVersion      = DecodeTmats.TmatsInfo.Ch10Version
    TmatsVersion     = DecodeTmats.find("G\\106")
    RecorderManu     = DecodeTmats.find("R-1\\RI1")
    RecorderModel    = DecodeTmats.find("R-1\\RI2")
    RecDateTime      = DecodeTmats.find("R-1\\RI4")
    RecorderFirmware = DecodeTmats.find("R-1\\RI10")
 
    if (DecodeTmats.find("R-1\\IDX\\E") == "T") : IndexEnabled = "Enabled"
    else                                        : IndexEnabled = "Disabled"
 
    if (DecodeTmats.find("R-1\\EV\\E") == "T")  : EventsEnabled = "Enabled"
    else                                        : EventsEnabled = "Disabled"
 
    # Get the data start and stop time
    PktIO.first()
    PktIO.read_next_header()
    PktIO.read_next_header()
    StartTime = TimeUtils.Rel2IrigTime(PktIO.Header.RefTime)
 
    PktIO.last()
    PktIO.read_next_header()
    while PktIO.Header.DataType == Py106.Packet.DataType.RECORDING_INDEX:
        PktIO.read_prev_header()
    StopTime = TimeUtils.Rel2IrigTime(PktIO.Header.RefTime)
 
    # Print out the results
    # ---------------------
 
    print("File Name                : {0}".format(FileName), file=OutputHandle)
    print("File Size                : {0:#0,} bytes".format(FileSize), file=OutputHandle)
    print("Recorder Manufacturer    : {0}".format(RecorderManu), file=OutputHandle)
    print("Recorder Model           : {0}".format(RecorderModel), file=OutputHandle)
    print("Recorder Firmware        : {0}".format(RecorderFirmware), file=OutputHandle)
    print("IRIG 106 Version (Data)  : {0}".format(RecorderVer2String(IrigVersion)), file=OutputHandle)
    print("IRIG 106 Version (TMATS) : 106-{0}".format(TmatsVersion), file=OutputHandle)
    print("Indexing                 : {0}".format(IndexEnabled), file=OutputHandle)
    print("Events                   : {0}".format(EventsEnabled), file=OutputHandle)
    print("Recording Date / Time    : {0}".format(RecDateTime), file=OutputHandle)
    print("Data Start Time          : {0}".format(StartTime), file=OutputHandle)
    print("Data Stop Time           : {0}".format(StopTime), file=OutputHandle)
 
    print("Data Types               : Channel  Data Type              (Type Num)  Packet Count", file=OutputHandle)
    print("                           -------  ---------------------- ----------  ------------", file=OutputHandle)
    for (ChID, DataTypeNum) in sorted(Counts.keys()):
        print ("                            {0:>5}   {1:<24} (0x{2:02x})    {3:>10}  ".format(ChID, Py106.Packet.DataType.TypeName(DataTypeNum), DataTypeNum, Counts[(ChID, DataTypeNum)]), end='', file=OutputHandle)
        # Print some more info for some selected channels
        if (DataTypeNum == Py106.Packet.DataType.PCM_FMT_0) or \
           (DataTypeNum == Py106.Packet.DataType.PCM_FMT_1) :
            (R_Index, R_DSI_Index) = Find_R_DSI_Num(ChID)
            R_Search = "R-{0}\\PDP-{1}".format(R_Index, R_DSI_Index);
            Tmats_PDP = DecodeTmats.find(R_Search)
            if   Tmats_PDP == "UN" : PCM_Mode = "Unpacked"
            elif Tmats_PDP == "TM" : PCM_Mode = "Throughput"
            elif Tmats_PDP == "PFS": PCM_Mode = "Packed with Frame Sync"
            else                   : PCM_Mode = "Unknown PCM Mode"
            print (" {0}".format(PCM_Mode), file=OutputHandle)
        elif (DataTypeNum == Py106.Packet.DataType.ANALOG) :
            R_Indexes = Find_R_DSI_Num(ChID)
            if R_Indexes != None:
                (R_Index, R_DSI_Index) = R_Indexes
                R_Search = "R-{0}\\ADP-{1}".format(R_Index, R_DSI_Index);
                Tmats_ADP = DecodeTmats.find(R_Search)
                if   Tmats_ADP == "YES" : Analog_Mode = "Packed"
                elif Tmats_ADP == "NO"  : Analog_Mode = "Unpacked"
                else                    : Analog_Mode = ""
                print (" {0}".format(Analog_Mode), file=OutputHandle)
            else:
                print("", file=OutputHandle)
        else:
            print("", file=OutputHandle)
 
    # Free up the previously malloc'ed TMATS memory and close the data file
    DecodeTmats.free_tmatsinfo()
    PktIO.close()
 
# =============================================================================
 
PktIO       = Py106.Packet.IO()
DecodeTmats = Py106.MsgDecodeTMATS.DecodeTMATS(PktIO)
TimeUtils   = Py106.Time.Time(PktIO)
 
path_root = "J:/Bob/irig106/Data/Web/"
for dir_path, dir_names, file_names in os.walk(path_root):
    for filename in file_names:
        if filename.endswith(".ch10") or filename.endswith(".c10"):
            filename_ch10 = os.path.join(dir_path, filename)
            (filename_base, filename_ext) = os.path.splitext(filename_ch10)
            filename_summary = filename_base + "_summary.txt"
            output_handle = open(filename_summary, "w")
 
            print(filename_ch10)
            Make_Ch10_Summary(filename_ch10, output_handle)
            output_handle.close
ch10summary.py.1542994390.txt.gz · Last modified: 2018/11/23 11:33 by bob