Tag: dap

Reading dymolas mat-files with python

INFO: This post is outdated! Please have a look here.

Dymola stores its simulation results in regular mat-files, but the program uses a special variable structure to store the data efficiently. An easy way to access the data is MATLAB, but I was looking for a more pythonic solution.

If you want to access the data from python you can use the function scipy.io.loadmat() from the scipy package. Some conversion is necessary get access to the data as simple time rows. The format of the result file dsres.mat is explained dymola users guide. Short version: the file contains matrices of different shape, every matrix has its own abscissa. The names of all dymola variables are stored in a special matrix. Every dymola variable name points to a column of one of the data matrices. Usually a lot of names point to the same column, possibly using a different sign (+/-).

The following python script can be imported as a module or run as a standalone script.


The first argument is the name of the mat-file. If no second argument is given, it prints  the names of all variables in the file. When you provide a variable name as the second argument, the values are printed with the corresponding time values in a format that can be directly printed with gnuplot. You may need to quote the variable name for the shell in some cases.

$ python DyMat.py dsres.mat
$ python DyMat.py dsres.mat "der(x)" # Time | der(x)
0.000000 0
200.000000 0.0597325
400.000000 0.117873
600.000000 0.172892
800.000000 0.223386
1000.000000 0.268128
$ python DyMat.py dsres.mat "der(x)" > tmp
$ gnuplot
gnuplot> plot "tmp"


The module needs more testing. I played a bit with the conversion to netCDF, XLS and CSV, but it’s not easy to transfer the space-efficient data structure to these formats. The conversion to netCDF with the same structure is trivial, but has no advantage over the original format. If you expand all variables the size of the file will explode. This needs to be checked in detail.

When I find the time I will implement a handler for pydap. A plotting tool based on PyQwt would be nice too.

ncDataReader2 | 2.2.4

A new release 2.2.4 of ncDataReader2 is out. Some bugs were fixed and the API has slightly changed. This version should work both with Dymola and OpenModelica on Linux and Windows. When you have a DAP-enabled netcdf library  it will even fetch data from the web in background. I already did some Dymola simulations with on-demand loading of data from a server, a post about this topic will follow.

Changes include:

  • the meaning of offset and scale factor for a ncDatSet1D was changed
  • chunk loading was implemented
  • Akima interpolation was changed heavily, works much faster now
  • better support for newer netcdf libraries
  • new helper functions for setting attributes
  • changed some char* to const char* to work with OpenModelica
  • a lot of bugs were fixed