Convert decimal to sexigesimal in awk

Yesterday I had the need to convert a decimal number to a sexigesimal one – I know there are lots of astronomical tools out there todo this but I felt then need to do this in awk… so here it is:

awk ‘{if ($2>=0) {h=($1/15); h2=h-(h%1); m=(h-h2)*60; m2=m-(m%1); s=(((h-h2)*60)- m2)*60; d=($2-$2%1); am=($2-d)*60; am2=am-(am%1); as=((($2-d)*60 -am)*60); print h2, m2, s,d, am2, as} else{h=($1/15); h2=h-(h%1); m=(h-h2)*60; m2=m-(m%1); s=(((h-h2)*60)- m2)*60; d=($2-$2%1); am=(d-$2)*60; am2=am-(am%1); as=((am – (d-$2)*60)*60); print h2, m2, s,d, am2, as} }’ sourcelist.dat

were sourcelist.dat is a space separated file with ra and dec in decimal.
This is a typical thing that you might want to do in astronomy, though you tend to want to take the position in HMS and make a decimal number, don’t ask why I wanted to go the other way.

Advertisements

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

%d bloggers like this: