473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Python trig precision problem

Hello all, having a small problem with a trig routine using python math
module. This code is designed to convert geodetic coordinates to lambert
conformal conic coordinates. It implements the formulas found at
http://mathworld.wolfram.com/Lambert...rojection.html . The
problem is that, at least on my machine, the precision is off to the tune of
around 1 km, which is unacceptable. (using the calculator found at
http://www.deh.gov.au/erin/tools/geo2lam-gda.html ) Anyone have any ideas?
here is the offending code.

from math import *

def lambert(lat,lon,ref_lat,ref_lon,st_parallel_1,st_p arallel_2):

earth_radius= 6371.9986
lon*=pi/180.0
lat *=pi/180.0

ref_lon*=pi/180.0
ref_lat *=pi/180.0

st_parallel_1 *=pi/180.0
st_parallel_2 *=pi/180.0

def sec(theta):
return 1.0/cos(theta)

def cot(theta):
return 1.0/tan(theta)

n = log( cos( st_parallel_1 ) * sec( st_parallel_2 ) ) / ( log( tan ( pi
/ 4.0 + st_parallel_2 / 2.0 ) * cot( pi / 4.0 + st_parallel_1 / 2.0 ) ) )

F = ( cos( st_parallel_1 ) * tan ( pi / 4.0 + st_parallel_1 / 2.0 ) **
n ) / n

rho = F * ( cot ( pi / 4.0 + lat / 2.0 ) ** n )

ref_rho = F * ( cot ( pi / 4.0 + ref_lat / 2.0 ) ** n )

x = rho * sin( n * ( lon-ref_lon ) )
y = ref_rho - ( rho * cos( n * ( lon-ref_lon ) ) )

return earth_radius*x,earth_radius*y
#######################################

lat,lon=35,-90

print lambert(lat,lon,40,-97,33,45)


May 18 '06 #1
3 1670

Cary West wrote:
Hello all, having a small problem with a trig routine using python math
module. This code is designed to convert geodetic coordinates to lambert
conformal conic coordinates. It implements the formulas found at
http://mathworld.wolfram.com/Lambert...rojection.html . The
problem is that, at least on my machine, the precision is off to the tune of
around 1 km, which is unacceptable. (using the calculator found at
http://www.deh.gov.au/erin/tools/geo2lam-gda.html ) Anyone have any ideas?
here is the offending code.


I'd assume you are exceeding the precision available with standard
64-bit floating point. Here is a library for extended precision
floating point, including trig functions.

http://calcrpnpy.sourceforge.net/clnumManual.html

casevh

May 18 '06 #2
If I were you I would see if I could get the Perl script referred to on
the ERIN web page. You might find that the discrepancy is something as
simple as a slightly different value for the Earth's radius. And by the
way, math.radians() might be a bit clearer than the pi/180 business.

May 18 '06 #3
Cary West wrote:
Hello all, having a small problem with a trig routine using python math
module. This code is designed to convert geodetic coordinates to lambert
conformal conic coordinates. It implements the formulas found at
http://mathworld.wolfram.com/Lambert...rojection.html . The
problem is that, at least on my machine, the precision is off to the tune of
around 1 km, which is unacceptable. (using the calculator found at
http://www.deh.gov.au/erin/tools/geo2lam-gda.html ) Anyone have any ideas?
here is the offending code.


If you want to do map projections, I highly recommend using PyProj instead of
implementing the calculations yourself. Geodesy is nontrivial, and MathWorld
doesn't tell you half the details you need to take care of.

http://www.cdc.noaa.gov/people/jeffr...on/pyproj.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
7
by: byte biscuit | last post by:
Hi there everybody! The problem is the following: we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on another DLL. H2O.dll contains only one (1) function, with a known...
89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
12
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last...
2
by: Jon Rea | last post by:
Im looking to do some heavy geomentry based number crunching involving a lot of sin and cos calculations. Performing these operations takes up a lot of computer time, we have calculated ~100...
14
by: chun ping wang | last post by:
Hey i have a stupid question. How do i get python to print the result in only three decimal place... Example>>> round (2.995423333545555, 3) 2.9950000000000001 but i want to get rid of all...
1
by: Charles Vejnar | last post by:
Hi, I have a C library using "long double" numbers. I would like to be able to keep this precision in Python (even if it's not portable) : for the moment I have to cast the "long double" numbers...
4
by: vedrandekovic | last post by:
Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.