Wednesday, March 21, 2012

Programming Basics for Iolite

We're getting a lot of users that want to customise the way Iolite processes their data, which is really exciting as it's exactly why we built Iolite in the way that we did. But we realise that there may be many more that just find getting into the programming code a little too intimidating, or that have started to take a look but would like a few more tips.

So the aim of this post is to give a really basic introduction to how to read and understand the programming code in Iolite – it's aimed at someone with no experience with programming, so hopefully it will give confidence to those people wanting to start looking at the code behind a DRS.

The first step is to find the programming code for a DRS. To do this, go to the Iolite dropdown menu and select "Edit the active data reduction scheme" (note that you need to have a DRS selected prior to doing this). If you'd like to make changes to a DRS we'd recommend making your own DRS so that you can keep the original – the easiest way to do this is by activating a DRS that you'd like to use as a template, then selecting "Create a new data reduction scheme" from the Iolite dropdown.

Once you've selected "Edit the active data reduction scheme" a window should pop up containing the programming code. The first thing you'll notice is that it's quite colourful - this is some helpful colour coding to make it easier to identify different types of things in the text.





Red text contains comments, which don't actually do anything except explain what the code is doing. You'll notice that all comments are preceded by "//", this is IgorPro's way of identifying comments in the text, so if you put "//" anywhere on a line then everything to the right of it will be ignored by IgorPro. This is of course really useful for anyone wanting to explain what lines or sections of code do. Obviously good commenting is therefore fundamental to making code that someone else can easily read and understand.

Blue text can be thought of as defining concrete things, like functions, strings, variables and waves.

Green text indicates something that will be printed, either in the history window, a popup window or on a graph. Green text is always bounded by quotation marks.

Black text is normal text, so if you write a simple equation or refer to things (e.g., a variable) that already exist then these will be black.

There are some other colours (e.g. the purple at the top of the window), but the main colours are those listed above

Another visual aid is indentation, which is a way of denoting increasingly local code. So a first level of indentation may be for all text within a function, then a further level within that may be for a loop that runs within the function, and maybe even a 3rd level of indentation (for example if there's an "if" statement within the loop).

Igor Pro has fantastic help for programming, and instead of attempting to describe things like loops and "if" statements I'd recommend using the built-in help. To do this is really simple, just right click on anything you'd like to know more about and select "Help for …..". This right-click menu also has some other great features, like the ability to insert a template for a function, which makes it much easier to use the correct syntax.

Having said that, here's a very quick summary of the main objects you'll want to use:

String = stores text
Variable = stores a number
Wave = stores a list of numbers (or text). Waves can be thought of as a single column in a table, or a line graph. Things can get pretty complicated once waves are involved, but we've done our best to keep things simple in the DRS code, and in most cases you can operate with a wave in a very similar fashion to a variable.

It's also important to think about the order that things are laid out. When Igor Pro executes the code it runs down the code one line at a time, so things have to be laid out vertically in the order that you want them to happen (although this simple flow may look more complicated once things like loops are involved). For a given line equations are read using normal mathematical hierarchy, regardless of the order things are laid out in, so brackets will be multiplication before addition, etc.

Some programming languages are case sensitive, so a variable called "Bob" would be different to another called "bob", but Igor Pro isn't like that. You can vary between upper and lower case in the code and it generally won't make any difference. Similarly, the code isn't sensitive to spaces or blank lines, so both "X = 2 + 5" and "X= 2+5" will give the same answer (an exception to both of these things is green text - it's sensitive to both capitals and spaces).

Finally, it's handy to know that things need to be defined prior to using them. For example, if you want to store a number you can do this in a "variable", but you'll need to let Igor Pro know that you'd like to make a variable and what name it should have.
 
In a similar way, instead of creating something, you could just make a connection to something that already exists. For example, if you want to perform calculations on a wave that already exists you would need to give it a label in the function, in combination with telling Igor Pro where it's located (there's an Iolite function called "IoliteDFPath" for making the location part easy).


So, hopefully the above basic bits and pieces will help a few more people venture into the DRS code, even if it's just to have a look around. And of course there is a huge amount of detail not covered here, so if you have any questions, whether very basic or more advanced, feel free to ask us on the forum and we'll do our best to help.

Happy programming!