Beginning With Files
CoPilot

Beginning With Files

For this week’s blog post a very unglamorous subject for the scripting beginner: reading and writing basic text files to and from ABAQUS and formatting the data so that appears in tabular form for use elsewhere. Not difficult, but a potential source of headaches for the beginner. There is no difference in the file operations between using Python in the ABAQUS environment and regular Python. Once you have the basics of file I/O then you can start to interrogate your models and your results to extract useful data.


Print

The simplest trick of all is printing output to the message area in a regular format that can simply be copied and pasted into whatever document you are working on that needs it. This example simply shows my lack of curiosity about the Python language, beyond the limits of Python 2.x for ABAQUS. Until recently I assembled lines of text, with tabs and printed whole lines. The answer is a lot easier in Python 3.x, and can be made available in Python 2.7 as shown:

Article content
Printing terminated by a tab rather than a new line is a very quick way to get a data table - simply print to the message window, copy and paste into your report document.

The future import is needed to bring the end keyword into play, unless you are using Python3.x. It must be included at the head of the module. Now it simply prints to the message window, one element at a time, separated by a tab, without a new line until the blank print statement which produces a carriage return by default. The result is a neatly tab-delimited times table in the message window.


Basic Files

Often printing to the window is not enough, a file is needed, either as a record or for the next stage of processing in another application like Excel – though why would you when you can process your data with Python and plotted prettily with Matplotlib??

Python makes handling text files very simple. Using the with...as syntax for opening the file should mean that the file is automatically closed on completion. A common mistake, if you open the file using just file = open(), is to forget the matching file.close(), resulting in all sorts of trouble. The most vanilla file I/O operations are shown below:

Article content
Writing a short piece of text to a fixed file and then reading the data back from the same file.

This is fine as far as it goes, except that it will dump the output file wherever your current working directory is. This may or may not be what you want. I often write plugins that need to read or write to data files that are in the same folder as the plugin. To do this is simple, you have to specify the full path of the file, including the script’s folder. You can do this manually but then you have to have the source code and edit the path. A better way is to interrogate the script to find out where it is. This is done using the inspect module as shown below:

Article content
The same read and write operations but the output file now resides with the script that created it.

Here the inspect function getFrameInfo returns the path of the currently executing script. The os functions split out the folder. The folder path is returned with back slashes, which is not helpful as you can get escape characters by mistake, so these have been replaced with forward slashes, then the default file name is attached to the string. Note that you need to add the extra forward slash before the file name.


File Dialogs

You can obviously simply define a fixed output folder, which may be all you need, the path could be an argument to the script at run time allowing for easy changes. Effective but inelegant. File dialogs give you more power.

This next is a a shameless copy of a worked example from Mark Hammond’s PyWin32 page on github:

GitHub - mhammond/pywin32: Python for Windows (pywin32) Extensions

Look him up, offer thanks, I certainly do. There are lots of other examples on there that you can bend to your will.

The good news is that the PyWin32 library now ships with ABAQUS so you don’t have the hassle of installing it yourself, like the bad old days. It also works with kernel scripts so there is no need to delve into writing GUIs. This example shows the Save dialog, the Open dialog is exactly the same, just change to win32gui.GetOpenFileNameW in line 56.

Article content
Applying a file dialog to the basic example.
Article content
Creating a file dialog that returns a path for the file. Works in kernel scripts. Much gratitude to Mark Hammond.

Your Own File Format

Mostly you will be putting out text. It may need to be formatted text. The simplest is something like a tab or comma delimited text file, as above. You might need to include other data, for example free form text explaining what the results mean.

Depending on what you are writing you may find the write or writelines functions for the file object the easiest. The example that follows uses both, replicating the trick at the top of this article to create a tab delimited table of data of user determined size, preceded by an arbitrary number of header lines.  The columns and rows in the data table are labelled. The whole data table, including the labels, is put inside markers to allow that table to grow and shrink freely and still be easily read by the reading routine.

There are other simple ways to do this but using markers means that the same code can read a file it created later, without having to put a header line count in. The header could be edited and updated elsewhere without affecting the table data extraction. A primitive markup like this also means that more than one table can be stored in a file and easily extracted. The result of this example is shown here:

Article content
The text file created by the sample code.
Article content
The basic example refactored to separate writing the header and data table. Other elements, with their own markers, could be added.
Article content
Writing the header file. Using writelines means that a complete list of strings can be added to a file in one pass. Useful if you are collecting information about your models into a list as you go.
Article content
A small expansion of the original times table data set - column labels are added as a row before the data and each row is started with a row label. The column labels are a list comprehension that is converted to a string for writelines to use.
Article content
Reading the data file. In this case just extracting the data table from the rest of the file. Once the STARTTABLE tag is read every line is converted into a list of integers, except for the first entry, the label. The column labels generate an error.

Text format files will get you a long way, in storing or post-processing your results and also just being able to paste the results into a report. Consider the case that you have a report template that just needs a data table and some pictures pasted into it to complete it. More complicated file formats can be used and I shall cover examples in RTF and HTML in the future.

#python #beginner #ABAQUS

Article content



To view or add a comment, sign in

More articles by Guy Rushton

  • Direct Access To Data In ABAQUS CustomData

    In this article we shall create a custom data class in ABAQUS’ customData space and access the data directly from the…

  • CustomData In Saved Files

    ABAQUS CAE lets you permanently store user data within the model and output files, the trick is how to access it again……

  • Design Museum & Innovation

    A few weeks ago I had an afternoon off in London while working nights and I wandered into the Design Museum. I have…

    5 Comments
  • Filing Your Plugins The Easy Way...

    Arranging and renaming your ABAQUS plugins without editing the code. Accessing your plugins collection is made easy…

  • The end of the Blocky Horror Show...

    The final part on this mini-project – controlling the game. In Part 2 we saw how the GUI called kernel functions…

  • TETRIS in ABAQUS Part 3

    Creating the look of the game: making the block shapes, playing area and visual elements..

  • TETRIS in ABAQUS Part2 – The User Interface

    Creating an interactive dialog box through the medium of classic gaming… The game design requirements were laid out in…

  • Tetris In ABAQUS... or Writer's Blocks

    Recreating a classic game using Python scripting in ABAQUS..

    13 Comments
  • Templates For ABAQUS Scripts

    Another brief blog post this week as I have been busy on both a coding course and a programming exercise, which, when I…

  • Innovation Promotion

    Creativity is corporate survival. Involving, and recognising, people at all levels of the business in problem solving…

Others also viewed

Explore content categories