PyMU package

aggPhasor

client

class Client(theDestIp, theDestPort, proto='TCP', sockType='INET')[source]

Bases: object

Client class that creates a client and provides simple functions for connecting to PMUs or PDCs without needing to directly use Python’s socket library. Supports INET and UNIX sockets

Parameters:
  • theDestIp (str) – IP address to connect to. If using unix socket this is the file name to connect to
  • theDestPort (int) – Port to connect to
  • proto (str) – Protocol to use. Accepts TCP or UDP
  • sockType (str) – Type of socket to create. INET or UNIX
connectToDest()[source]

Connect socket to destination IP:Port. If UNIX socket then use destIP

createSocket()[source]

Create socket based on constructor arguments

readSample(bytesToRead)[source]

Read a sample from the socket

Parameters:bytesToRead (int) – Number of bytes to read from socket
Returns:Byte array of data read from socket
sendData(bytesToSend)[source]

Send bytes to destination

Parameters:bytesToSend (int) – Number of bytes to send
setTimeout(numOfSecs)[source]

Set socket timeout

Parameters:numOfSecs (int) – Time to wait for socket action to complete before throwing timeout exception
stop()[source]

Close the socket connection

pmuCommandFrame

class CommandFrame(commandStr, pmuIdInt, debug=False)[source]

Bases: pymu.pmuFrame.PMUFrame

Class for creating a Command Frame based on C37.118-2005

Parameters:
  • commandStr (str) – Command to send
  • pmuIdInt (int) – Frame ID of PMU
  • debug (bool) – Print debug statements
createCommand()[source]

Create each field based on the command to send and the frame ID

genChk(crcInputData)[source]

Generate CRC-CCITT based on command frame

genCmd()[source]

Generate command as hex str

Returns:Command field bytes as hex str
genFracsec()[source]

Generate fraction of seconds based on current time

Returns:FRACSEC field bytes as hex str
genIdcode()[source]
Returns:ID code field bytes as hex str
genSoc()[source]

Generate second of century based on current time

Returns:SOC field bytes as hex str
genSync()[source]
Returns:Sync field bytes as hex str

pmuConfigFrame

Class for parsing Config Frame 1 or 2

class Anunit(anunitHexStr, debug=False)[source]

Bases: object

Class for conversion factor for analog channels

Parameters:anunitHexStr (str) – Conversion factor for analog channels field in hex str format
parseAnlgMsrmnt()[source]

Parse analog measurement type

parseUserDefinedScale()[source]

Parse user defined scaling

class ConfigFrame(frameInHexStr, debug=False)[source]

Bases: pymu.pmuFrame.PMUFrame

Parses Config Frame (1 or 2)

Parameters:
  • frameInHexStr (str) – Config frame as byte array in hex str format
  • debug (bool) – Print debug statements
finishParsing()[source]

After first 4 bytes are received, the client reads the remaining config frame bytes. This function parses those remaining bytes

parseDATARATE()[source]

Parse data rate at which data will be received

parseNUM_PMU()[source]

Parse number of PMUs sending data

parseStations()[source]

Parse station names for each PMU

parseTIME_BASE()[source]

Parses resolution of FRACSEC

class Digunit(digunitHexStr, debug=False)[source]

Bases: object

Class for mask of digital status words

Parameters:digunitHexStr (str) – Conversion factor for digital status channels field in hex str format
class Phunit(phunitHexStr, debug=False)[source]

Bases: object

Class for conversion factor for phasor channels

Parameters:phunitHexStr (str) – Conversion factor field in hex str format
parseValue()[source]

Parse value of conversion factor

parseVoltOrCurr()[source]

Determine if measurement type is voltage or current

class Station(theStationHex, debug=False)[source]

Bases: object

Class for parsing station information including all PMU information. Fields 8-19

Parameters:
  • theStationHex (str) – Station fields in hex str format
  • debug (bool) – Print debug statements
parseANNMR()[source]

Parses number of analog values field

parseANUNIT()[source]

Parse conversion factor for analog channels

parseCFGCNT()[source]

Parse configuration change count

parseCHNAME()[source]

Parses phasor and channel names field

parseDGNMR()[source]

Parses number of digital values field

parseDIGUNIT()[source]

Parse mask words for digital status words

parseFNOM()[source]

Nominal line frequency code and flags

parseFORMAT()[source]

Parses data format field

parseIDCODE_data()[source]

Parses station ID code field

parsePHNMR()[source]

Parses number of phasors field

parsePHUNIT()[source]

Parse conversion factor for phasor channels

parseSTN()[source]

Parses station name field

updateLength(sizeToAdd)[source]

Updates length of station frames only

Parameters:sizeToAdd (int) – Number of bytes to add to frame length
class TimeBase(timeBaseHexStr, debug=False)[source]

Bases: object

Class for parsing the TIME_BASE word

pmuDataFrame

class DataFrame(frameInHexStr, theConfigFrame, debug=False)[source]

Bases: pymu.pmuFrame.PMUFrame

Class for creating a Data Frame based on C37.118-2011

Parameters:
  • frameInHexStr (str) – Data frame bytes as hex str
  • theConfigFrame (ConfigFrame) – Config frame describing the data frame
  • debug (bool) – Print debug statements
parsePmus()[source]

Parses each PMU present in the data frame

updateSOC()[source]
class PMU(pmuHexStr, theStationFrame, debug=False)[source]

Bases: object

Class for a PMU in a data frame

Parameters:
  • pmuHexStr (str) – Bytes of PMU fields in hex str format
  • theStationFrame (Station) – Station fields from config frame describing PMU data
  • debug (bool) – Print debug statements
parseAnalog()[source]

Parse analog data

parseDfreq()[source]

Parse rate of change of frequency (ROCOF)

parseDigital()[source]

Parse digital data

parseFreq()[source]

Parse frequency

parsePhasors()[source]

Parse phasor estimates from PMU

parseStat()[source]

Parse bit mapped flags field

updateLength(sizeToAdd)[source]

Keeps track of length for PMU frame only

class Phasor(thePhsrValHex, theStationFrame, theName, debug=False)[source]

Bases: object

Class for holding phasor information

Parameters:
  • thePhsrValHex (str) – Phasor values in hex str format
  • theStationFrame (Station) – Station frame which describe data format
  • theName (str) – Name of phasor channel
  • debug (bool) – Print debug statements
parseFmt()[source]

Parse format and type of phasor

parseVal()[source]

Parse phasor value

toPolar(hexVal)[source]

Convert bytes to polar values

toRect(hexVal)[source]

Convert bytes to rectangular values

class Stat(statHexStr, debug=False)[source]

Bases: object

Class for foling bit mapped flags

Parameters:
  • statHexStr (str) – Stat field in hex string format
  • debug (bool) – Print debug statements
parseConfigChange()[source]

Parse config change bit

parseDataError()[source]

Parse data error bits

parseDataModified()[source]

Parse data modified bit

parsePmuSync()[source]

Parse PMU sync bit

parsePmuTrigger()[source]

Parse PMU trigger bit

parseSorting()[source]

Parse data sorting bit

parseTimeQuality()[source]

Parse time quality bits

parseTriggerReason()[source]

Parse trigger reason bits

parseUnlockTime()[source]

Parse unlocked time bits

pmuEnum

File containing all the enums used for PyMU. For detailed explanations please look at C37.118-2005 standard

class AnlgMsrmnt[source]

Bases: enum.Enum

class Command[source]

Bases: enum.Enum

class ConfigChange[source]

Bases: enum.Enum

class DataError[source]

Bases: enum.Enum

class DataModified[source]

Bases: enum.Enum

class FrameType[source]

Bases: enum.Enum

class FundFreq[source]

Bases: enum.Enum

class MeasurementType[source]

Bases: enum.Enum

class NumType[source]

Bases: enum.Enum

class PhsrFmt[source]

Bases: enum.Enum

class PmuSync[source]

Bases: enum.Enum

class Sorting[source]

Bases: enum.Enum

class TimeQuality[source]

Bases: enum.Enum

class Trigger[source]

Bases: enum.Enum

class TriggerReason[source]

Bases: enum.Enum

class Unit[source]

Bases: enum.Enum

class UnlockedTime[source]

Bases: enum.Enum

pmuFrame

class PMUFrame(frameInHexStr, debug=False)[source]

Bases: object

Super class for all C37.118-2005/C37.118-2011 frames

Parameters:
  • frameInHexStr – Bytes from frame (any time) in hex str format
  • debug (bool) – Print debug statements
finishParsing()[source]

When getting the config frame, the size is unknown. After creating a PMUFrame with the first 4 bytes, the remaining frame bytes are read and added to self.frame. Once that is populated the remaining fields can be parsed

parseCHK()[source]

Parse CRC-CCITT word

parseFRACSEC()[source]

Parse fraction of second and time quality word

parseFRAMESIZE()[source]

Parse frame size

parseIDCODE()[source]

Parse data stream ID number

parseSOC()[source]

Parse second-of-century timestamp

parseSYNC()[source]

Parse frame synchronization word

updateLength(sizeToAdd)[source]

Keeps track of index for overall frame

class SOC(socHexStr, debug=False)[source]

Bases: object

Class for second-of-century (SOC) word (32 bit unsigned)

Parameters:
  • socHexStr (str) – Second-of-century byte array in hex str format
  • debug (bool) – Print debug statements
parseSecCount()[source]

Parse SOC into UTC timestamp and pretty formatted timestamp

class SYNC(syncHexStr, debug=False)[source]

Bases: object

Class for describing the frame synchronization word

Parameters:
  • syncHexStr (str) – Sync byte array in hex str format
  • debug (bool) – Print debug statements
parseType()[source]

Parse frame type

parseVers()[source]

Parse frame version

pmuLib

Commonly used functions for parsing PMU Frames

bytesToFloat(b)[source]

Converts byte array to double

Parameters:b (byte-array) – Byte array to convert
Returns:Float value
bytesToHexStr(bytesInput)[source]

Converts byte array to hex str

Parameters:bytesInput (byte-array) – byte array to convert
Returns:Hex string representing bytesInput
doubleToBytes(f)[source]

Converts double to byte array

Parameters:f (double) – Double value to convert
Returns:Byte array representation of double value
doubleToHex(f)[source]

Converts double to hex

Parameters:f (double) – Double value to convert
Returns:Hex representation of double value
doubleToHexStr(f)[source]

Converts double to hex str

Parameters:f (double) – Double value to convert
Returns:Hex string representation of double value
hexToBin(hexStr, numOfBits)[source]

Converts hex string to binary

Parameters:
  • hexStr (str) – Hex value in string format
  • numOfBits (int) – Number of bits to convert
Returns:

bits representing the hex values

intToBytes(i)[source]

Converts unsigned int to byte array

Parameters:i (int) – Integer value to convert
Returns:Byte array representing i
intToHexStr(i)[source]

Converts int to hex

Parameters:i (int) – Integer value to convert
Returns:Hex string representing i

server

class Server(thePort, proto='TCP', printInfo=False)[source]

Bases: object

Server class that creates a server and provides simple functions for incoming connections/data from PMUs or PDCs without needing to directly use Python’s socket library. Supports INET sockets only (will eventually be updated).

Parameters:
  • thePort (int) – Local port to listen on
  • proto (str) – Protocol to use. Accepts TCP or UDP
  • printInfo (bool) – Specifies whether or not to print debug statements
readSample(length)[source]

Will read exactly exactly as many bytes as specified by length and return them as an int

setTimeout(numOfSecs)[source]

Set socket timeout

Parameters:numOfSecs (int) – Time to wait for socket action to complete before throwing timeout exception
startServer(queueLen)[source]

Starts the python server and listens for connections

Parameters:queueLen (int) – Max number of queued connections. Usually defaults to 5
stop()[source]

Closes server connections

waitForConnection()[source]

Will block program execution until a connection is achieved

tools

Tools for common functions relayed to commanding, reading, and parsing PMU data

createAggPhasors(configFrame)[source]

Creates an array of aggregate phasors for data collection

Parameters:configFrame (ConfigFrame) – ConfigFrame containing stations
Returns:List containing all the station AggPhasor objects
getDataSample(rcvr, debug=False)[source]

Get a data sample regardless of TCP or UDP connection

Parameters:
  • rcvr (Client/Server) – Object used for receiving data frames
  • debug (bool) – Print debug statements
Returns:

Data frame in hex string format

getStations(configFrame)[source]

Returns all station names from the config frame

Parameters:configFrame (ConfigFrame) – ConfigFrame containing stations
Returns:List containing all the station names
parseSamples(data, configFrame, pmus)[source]

Takes in an array of dataFrames and inserts the data into an array of aggregate phasors

Parameters:
  • data (List) – List containing all the data samples
  • configFrame (ConfigFrame) – ConfigFrame containing stations
  • pmus (List) – List of phasor values
Returns:

List containing all the phasor values

readConfigFrame2(cli, debug=False)[source]

Retrieve and return config frame 2 from PMU or PDC

Parameters:
  • cli (Client) – Client connection to data source
  • debug (bool) – Print debug statements
Returns:

Populated ConfigFrame

requestConfigFrame2(cli, idcode)[source]

Send command to request config frame 2

Parameters:
  • cli (Client) – Client connection to data source
  • idcode (int) – Frame ID of data source
startDataCapture(idcode, ip, port=4712, tcpUdp='TCP', debug=False)[source]

Connect to data source, request config frame, send data start command

Parameters:
  • idcode (int) – Frame ID of PMU
  • ip (str) – IP address of data source
  • port (int) – Command port on data source
  • tcpUdp (str) – Use TCP or UDP
  • debug (bool) – Print debug statements
Returns:

Populated pymu.pmuConfigFrame.ConfigFrame object

turnDataOff(cli, idcode)[source]

Send command to turn off real-time data

Parameters:
  • cli (Client) – Client being used to connect to data source
  • idcode (int) – Frame ID of data source
turnDataOn(cli, idcode)[source]

Send command to turn on real-time data

Parameters:
  • cli (Client) – Client connection to data source
  • idcode (int) – Frame ID of data source

transferFrame

class PhasorField(phasor, idNum, theUnits)[source]

Bases: object

Class to hold simplified phasor fields

Parameters:
  • phasor (Phasor) – Phasor containing measurements
  • idNum (int) – Frame ID to use
  • theUnits (str) – Volts or Amps
createPhasorFieldFrame()[source]

Create phasor field frame inside full transfer frame

parseOptions()[source]

Parse options and create bytes as hex str

class TransferFrame(inputDataFrame)[source]

Bases: object

Custom class meant to create a message that can be passed to a socket connection. Only contains timestamp, phasor values, and ID for each phasor

Parameters:inputDataFrame (pmuDataFrame) – Populated data frame containing measurement values
createFullFrame()[source]

Put all the pieces to together to create full transfer frame

genCrc()[source]

Generate CRC-CCITT

parseDataSample()[source]

Parse the input data sample

parsePhasors()[source]

Parse the phasors in the data sample to extract measurements