How to create a measurement file for DIgSILENT PowerFactory from a CSV, TXT, or XLSX file?

Many times when you model time-domain simulations in DIgSILENT PowerFactory you want to use time-series data stored in CSV, TXT, or XLSX files. In PowerFactory you can integrate these time series by means of measurement files (*.ElmFile). These measurement files are simple ASCII files with a column for each variable including the time. The time must be in the first column and the header must specify the number of contained variables (excluding time).

In this post, I present an approach to create a PowerFactory measurement file directly from CSV, TXT, or XLSX format with Python. The created measurement file is then saved in TXT format.

call to action download Python code to create PowerFactory measurement file from CSV, TXT, and XLSX

Anatomy of a measurement file

The good news is that the measurement file is just a simple ASCII file containing a column for each variable including the time. In the header, the number of columns (excluding time) must be specified.

According to the technical reference from PowerFactory, the measurement file needs to fulfill the following criteria:

  • Variables are stored in columns
  • The number of variables is defined in the first row (excluding time)
  • The first column is time
  • Columns are separated by blanks, tabs, or commas
  • The number of columns is constant
  • No empty lines
  • The decimal separator is always ‘.’
  • The numbers must be in floating-point or scientific notation
  • No digit grouping symbols in numbers
  • Maximum of 1024 characters per line

Below an example of the first 5 rows of a measurement file (screenshot from Notepad++) highlighting the most important criteria described above.

Example of a measurement file
Example of the first 5 rows of a measurement file

Create measurement file from CSV

Below a screenshot of a CSV file that contains the time-series data. It contains four columns, the first being the time and the other three being variables. The columns are separated by semicolon and comma is used as a decimal separator.

Screenshot of the CSV file
PowerFactory measurement file
Screenshot of the CSV file

Below the code to create a measurement file directly from a CSV file. You only need to consider the following steps:

  • Change the path and file name to your CSV file
  • Define the separator, decimal, and header row for the pandas import
  • Attention: I assume that the leftmost column in the CSV file is the time. If that is not the case, you need to rearrange the columns in the pandas dataframe or directly in the CSV file.
#Create measurement file from CSV

import pandas as pd
import numpy as np
import numpy.matlib
import os

# %% import csv file
path = r'C:\Your Folder'
file = 'CSV_File.csv'

#path to save PowerFactory measfile (default = same as csv file; add path below if you want to save measfile somewhere else)
path_output = path

#combine path and filename
strFile = os.path.join(path,file)

#import csv file
dat_import=pd.read_csv(strFile, sep=';', decimal=',', header=0)

# %% create PowerFactory measfile
#convert pandas to numpy array
datout = np.array(dat_import)

#convert to string
datout = datout.astype(str)

#add number of columns in header -> N-1 columns (time does not count as column)
datout = np.vstack ((np.empty((1,np.size(datout,1)), dtype="str"), datout))
datout[0,0] = str(np.size(datout,1)-1)

#filename of PowerFactory measfile
filename_out= file[0:-4] + '_PF_measfile_from_CSV.txt'

#save PowerFactory measfile as txt
filepath_out = os.path.join(path_output,filename_out)
np.savetxt(filepath_out, datout, fmt="%s")

Create measurement file from TXT

Below a screenshot of a TXT file that contains the time-series data. It contains the same four columns as the CSV file before. The columns are separated by tab and comma is used as a decimal separator. The TXT file is very similar to the CSV file. Usually, these file types just have different separators and decimals.

Screenshot of the TXT file
PowerFactory measurement file
Screenshot of the TXT file

Below the code to create a measurement file directly from a TXT file. You only need to consider the following steps:

  • Change the path and file name to your TXT file
  • Define the separator, decimal, and header row for the pandas import
  • Attention: I assume that the leftmost column in the TXT file is the time. If that is not the case, you need to rearrange the columns in the pandas dataframe or directly in the TXT file.
#Create measurement file from TXT

import pandas as pd
import numpy as np
import numpy.matlib
import os

# %% import text file
path = r'C:\Your Folder'
file = 'TXT_File.txt'

#path to save PowerFactory measfile (default = same as text file; add path below if you want to save measfile somewhere else)
path_output = path

#combine path and filename
strFile = os.path.join(path,file)

#import text file
dat_import=pd.read_csv(strFile, sep='\t', decimal=',', header=0)

# %% create PowerFactory measfile
#convert pandas to numpy array
datout = np.array(dat_import)

#convert to string
datout = datout.astype(str)

#add number of columns in header -> N-1 columns (time does not count as column)
datout = np.vstack ((np.empty((1,np.size(datout,1)), dtype="str"), datout))
datout[0,0] = str(np.size(datout,1)-1)

#filename of PowerFactory measfile
filename_out= file[0:-4] + '_PF_measfile_from_TXT.txt'

#save PowerFactory measfile as txt
filepath_out = os.path.join(path_output,filename_out)
np.savetxt(filepath_out, datout, fmt="%s")

Create measurement file from XLSX

Below a screenshot of an XLSX file that contains the time-series data. It contains the same four columns as the CSV and TXT files before.

Screenshot of the XLSX file
PowerFactory measurement file
Screenshot of the XLSX file

Below the code to create a measurement file directly from an XLSX file (XLS files should also work – I haven’t tested it though). You only need to consider the following steps:

  • Change the path and file name to your XLSX file
  • Define the header row for the pandas import
  • Attention: I assume that the leftmost column in the XLSX file is the time. If that is not the case, you need to rearrange the columns in the pandas dataframe or directly in the XLSX file.
#Create measurement file from XLSX

import pandas as pd
import numpy as np
import numpy.matlib
import os

# %% import XLSX file
path = r'C:\Your Folder'
file = 'XLSX_File.xlsx'

#path to save PowerFactory measfile (default = same as XLSX file; add path below if you want to save measfile somewhere else)
path_output = path

#combine path and filename
strFile = os.path.join(path,file)

#import XLSX file
dat_import=pd.read_excel(strFile, header=0)

# %% create PowerFactory measfile
#convert pandas to numpy array
datout = np.array(dat_import)

#convert to string
datout = datout.astype(str)

#add number of columns in header -> N-1 columns (time does not count as column)
datout = np.vstack ((np.empty((1,np.size(datout,1)), dtype="str"), datout))
datout[0,0] = str(np.size(datout,1)-1)

#filename of PowerFactory measfile
filename_out= file[0:-5] + '_PF_measfile_from_XLSX.txt'

#save PowerFactory measfile as txt
filepath_out = os.path.join(path_output,filename_out)
np.savetxt(filepath_out, datout, fmt="%s")

Resulting measurement file

Below you can find a screenshot of the resulting measurement file. All the above file formats produce the same measurement file. As you can see, the first row specifies the number of variables, which is three in this case because the time does not count as a variable.

Screenshot of the PowerFactory measurement file
Screenshot of the PowerFactory measurement file (in Notepad++)

If you find this article insightful, you might also like the following post: Run DIgSILENT PowerFactory through the Python API – kickstart your PowerFactory automatization

More insights to follow in the next article.

Michael

call to action download Python code to create PowerFactory measurement file from CSV, TXT, and XLSX
Share this post:

Similar Posts

One Comment

Leave a Reply