Nandan Joshi
Nandan Joshi
2 min read

Categories

Tags

The coordinates of any objects in astronomy is given in right ascension and declination, sort of latitude and longitude for the sky. For example, for Altair, one of the brightest star in the sky, it has RA of [19 50 46.99] and DEC of [+08 52 05.95]. Only declination needs plus or minus sign, like latitude on Earth’s surface. So I save these information in IDL in variable such as, RA1, RA2, RA3, DEC1, DEC2, DEC3. Of course, I save last part of RA and DEC in floating number, whereas other variable are of integer. When I want to print it with following command:

IDL > print, RA1, RA2, RA3, DEC1, DEC2, DEC3, format='(I3,I3,F6.2,4x,I3,I3,F6.2)'

I get the output:

    19 50 46.99      8 52  5.95

which is perfectly fine for quick look-up. As you see, the format defines how the output should look like. It prints 3 digit integer, aligned to right. And if the integer is of less than 3 digits, then non-existing integer part is replaced by backspace. I used this fact to make some space between two entries, like DEC1 and DEC2. Without this hassle for backspace, the output would have looked like:

    195046.99     8525.95

Quite bad, right? So having prepending backspace is good thing. But for printing purpose, in publication or in thesis, it is better if they were shown like this:

    19 50 46.99    +08 52 05.95

So I need 0 rather than backspace before 8 and 5.95. And I want the plus sign for DEC1 part, too. So let’s look at how to add 0s first!

For integer formatting it is easy. I replace I3 with I03. Quite simple, well, the output will be like this:

    019050 46.99    008052  5.95

Quite bad. But there’s a solution. You can replace I3 by I3.2, and voila, you have quasi-required output:

    19 50 46.99     08 52  5.95

t says that out of three required space for integer, only two should be occupied, and if there is only one digit integer, then second space prepending to it shall be replaced by 0. Unfortunately, the same trick doesn’t work for floating number, where decimal number in format are used to define decimal places. If I replace F6.2 by F06.2, I get the following output:

    19 50 46.99     08 52005.95

I didn’t find any easy solution to that, nonetheless, there’s a workaround. I add 1x before defining format for floating number and replace I6.2 by I05.2 to include zero in the output. It looks like now:

    19 50 46.99     08 52 05.95

Almost perfect! We are just missing the plus (or minus) sign. It can be done by adding + to format like I+3.2. Beware, + sign in format doesn’t mean only for plus sign, but also minus. Instead of +, if you give sign, then it has different meaning. Because, as default, all outputs are right aligned, the in format allows it to align to left. It is not meant for minus sign in the output.

So finally the following IDL code gives the required output:

IDL> print, RA1, RA2, RA3, DEC1, DEC2, DEC3, format='(I3.2,I3,F6.2,4x,I+3.2,I3,1x,F05.2)'
    19 50 46.99    +08 52 05.95

I guess it was way to long explanation for simple problem. But I was interested in explaining how I went about it. I hope you enjoyed it. You can find more and detailed information on format codes here: http://star.pst.qub.ac.uk/idl/Format_Codes.html