HARPPI: A HumAn Readable Parameter-file ParsIng library

Since becoming a post-doc, most of my days are spent writing code. I do the majority of my programming in C++, because as a compiled language, the compiler is able to make many optimizations to ensure that even my poorly written scientist code is reasonably fast. As is often the case, there are times I would like to change some values then re-run some code, and it is nice to not have to recompile each time you want to make a small change to some number, or change an input or output file name. Because of this, you want to have your code use parameter files which are read and parsed every time you start the code.

Now, I have been writing code to use parameter files for quite sometime using custom functions that I would write for each piece of code. The functions where different for each piece of code because the required parameters for the code where different. After a while, writing very similar functions over and over again became tiresome, so I started to look for a way that I could have a more flexible parameter file parser.

True, there are libraries out there that do this already, but the vast majority are not easy to learn, and the parameter files are typically less than human readable (i.e. if you are not intimately familiar with the format it would take a substantial amount of time to figure out how to create/read the files). So, my goal was to create a library that I could easily link and use in any piece of code that I am writing, that doesn’t have to have anything done specifically for the program I am writing. Out of this, the HumAn Readable Parameter-file ParsIng library (HARPPI) was created.

The code is available in my github repository and is available under the GPLv3. There is a simple make file that should be called with

$ make localinstall

on linux systems (the only official support at this point). See the harppi.pdf file for complete installation instructions.

Once installed and placed in a directory that is specified in your path as per the installation instructions, you can create a plain text file, call it MyParameters.txt with the format of

double Param1 = 2.339   # Some comment about the parameter
string File = Input.dat  # Some input file
bool deleteFile = false  # Maybe this tells you code to delete or keep the input file
int fileNum = 2

# Below are some lists of values. There must be a comma with
# no spaces between the values.
vector<double> aList = 1.5461,2.3554,1.0253
vector<int> numGals = 500000,250000
vector<string> fileBase = LRGcatalog-,ELGCatalog-

In this way, you can specify all the parameters you code needs. Then in you C++ code you simply need to do the following

#include <iostream>
#include <fstream>
#include <harppi.h>

int main(int argc, char *argv[]) {
    std::ifstream fin;
    parameters p; // Declare parameters object
    p.readParams(argv[1]); // Read the parameter file passed at runtime

    // Some code here

    int totalGals = p.geti("numGals",0) + p.geti("numGals",1);
    double multiply = p.getd("Param1")*totalGals;
    std::cout << "Number of files: " << p.geti("fileNum") << std::endl;
    fin.open(p.gets("File").c_str(), std::ios::in);

// Do some stuff with the data in the file


    // Some more code

    return 0;

As you can see, the data can be easily accessed in the code one the parameter file is loaded (again, the included documentation has much more detail). If you try to access some parameter that wasn’t in your file, the library will throw and error. There is also a way to tell you code what parameters are needed and have the library check that they are there immediately after the parameter file is loaded.

I wanted to share this in case anyone else may find this library useful. If you have questions about the library, let me know in the comments below.



1 thought on “HARPPI: A HumAn Readable Parameter-file ParsIng library”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s