Thursday, July 17, 2008

Awk sexagesimal to decimal conversion

In VIM I often need to convert columns of RA/Dec from Sexagesimal into Decimal format.

%!awk '{ra = ($2+$3/60+$4/3600)*15; dec = $6+$7/60+$8/3600; print $1,"ra=",ra,", dec= ",$5,dec}'


The far more irritating inverse operation:

%!awk '{h=($2/15); h=h-(h\%1); m=($2-h*15)/15*60; m=m-(m\%1); s=($2-h*15-m*15/60)/15*3600; d=-($3-$3\%1); am=(-$3-d)*60; am=am-(am\%1); as=(-$3-d-am/60)*3600; printf "\%s \%02i:\%02i:\%02.2f
, -\%02i:\%02i:\%02.2f \%s \%s \%s\n" , $1,h,m,s,d,am,as,$4,$5,$6}'

1 comment:

Duck said...

This is several years old, so hopefully you've fixed the error already. But it's also the first google result I saw, so for anyone else's reference: in converting from sexagesimal declination to decimal, you need to be careful of the negative sign. One way to do this is to capture the sign and multiply it by the entire sign-stripped value. This could be accomplished by:

given declination in the form DECd:DECm:DECs,

decimal DEC = (DECd/(abs(DECd))) * (abs(DECd) + DECm/60 + DECs/3600)