« A day out in San Juan | Main | Using Awk to add a column to a file »

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.

TrackBack

TrackBack URL for this entry:
http://www.krioma.net/cgi-bin/mt_new/mt-tb.cgi/411

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on December 4, 2009 5:33 PM.

The previous post in this blog was A day out in San Juan.

The next post in this blog is Using Awk to add a column to a file.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.33