[Numpy-discussion] Conversion functions

Chris Barker - NOAA Federal chris.barker@noaa....
Tue Jan 1 21:41:19 CST 2013

On Tue, Jan 1, 2013 at 6:50 AM, Schönberger Johannes
<hannesschoenberger@gmail.com> wrote:

> I recently opened a new pull request which adds the functionality to
> convert between degrees and degrees, minutes and seconds
> (https://github.com/numpy/numpy/pull/2869).
> The discussion is about whether such conversion functionality should
> be integrated into numpy at all or whether this belongs to scipy.

handy functions, yes, but certainly not something to put in numpy --
maybe scipy, not sure the best place. I see someone (chuck? )on github
suggested a "conversion.py" module -- that should be in scipy, not
numpy, but I"m wary -- where would it stop? RAther, perhaps the
quantaties package should be adopted.

But another note: conversion to deg.min.sec with floating point is a
bit less trivial than you'd think, you can end up with results like:

xdegrees, 60 minutes.... if you're not careful -- it looks from a
first glance that the pull request does not address this. Note: I
suppose we could consider it technically OK, but it's certainly not
aesthetically pleasing.

import numpy as np

def deg2dms(x):
    out = [0,0,0]
    out[0] = np.floor(x)
    out[1] = np.floor((x - out[0]) * 60)
    out[2] = ((x - out[0]) * 60 - out[1]) * 60
    return out

print deg2dms(1.0)
print deg2dms(1.1)
print deg2dms(45.05)

In [60]: run deg2dms.py
[1.0, 0.0, 0.0]
[1.0, 6.0, 3.1974423109204508e-13]
[45.0, 2.0, 59.999999999989768]

you'd really want that to be:

 1degree 6 minutes, 0 seconds


 45 degrees 3 minutes, zero seconds

Here's the code I used:

    def ToDegMin(self, DecDegrees, ustring = False):
        Converts from decimal (binary float) degrees to:
          Degrees, Minutes

        If the optional parameter: "ustring" is True,
        a Unicode string is returned

        if signbit(DecDegrees):
            Sign = -1
            DecDegrees = abs(DecDegrees)
            Sign = 1
        Degrees = int(DecDegrees)
        DecMinutes = round((DecDegrees - Degrees + 1e-14) * 60, 10)#
add a tiny bit then round to avoid binary rounding issues
        if ustring:
            if Sign == 1:
                return u"%i\xb0 %.3f'"%(Degrees, DecMinutes)
                return u"-%i\xb0 %.3f'"%(Degrees, DecMinutes)
            return (Sign*float(Degrees), DecMinutes) # float to preserve -0.0

perhaps ugly but it results in pretty output -- someone smart here
could probably offer a cleaner solution.



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the NumPy-Discussion mailing list