Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread Alan Gauld

"John" <[EMAIL PROTECTED]> wrote


> In all it's glory: I'm just a bit embarrassed because I'm sure it's 
> poor
> coding:
>

Thats what we're here for...

The first thing to note is that gridarea is here a function object.
So are you assigning the function  object  to area[x,y].
Is that what you intend?

> def gridarea(H):
> """ returns an array of area corresponding to each nx,ny,nz
> % input
> %   - H  : Header dict object with nx, ny, nz
> % output
> %   - Numpy array area corresponding to nx,ny,nz
> """
> import math
> import numpy as N

It's traditional to put the imports outside the function, especially
if they will always be called.

> pih=math.pi/180.
> r_earth=6.371e6
> cosfunc = lambda y : math.cos(y*pih)*r_earth

You could use the more conventional

def cosfunc(y): return math.cos(y*pih)*r_earth


lambda is fine by me but some folks find them harder to read.


> nz=H['nz']
> nx=H['nx']
> ny=H['ny']
> outlat0=H['outlat0']
> dyout=H['dyout']
> dxout=H['dxout']
> area=N.zeros((nx,ny)) #creates numpy array
>
> for iy in range(ny):
>   ylata=outlat0+(float(iy)+0.5)*dyout
>   ylatp=ylata+0.5*dyout
>   ylatm=ylata-0.5*dyout
>   if (ylatm<0 and ylatp>0): hzone=dyout*r_earth*pih
>   else:
> cosfact=cosfunc(ylata)
> cosfactp=cosfunc(ylatp)
> cosfactm=cosfunc(ylatm)
> if cosfactp < cosfactm: 
> hzone=math.sqrt(r_earth**2-cosfactp**2)-math.sqrt
>(r_earth**2-cosfactm**2)
>   else: hzone=math.sqrt(r_earth**2-cosfactm**2)-math.sqrt
> (r_earth**2-cosfactp**2)
>

More whitespace will help readability and avoid errors! please...

>  gridarea=2.*math.pi*r_earth*hzone*dxout/360.

This is a bit iffy since you are creating a variable gridarea inside
a function gridarea. That effectively removes the possibility of
using recursion, although otherwise I don't *think* it should
cause a problem in itself.

>  for ix in range(nx):
>   print nx, ix, iy
>   area[ix,iy]=gridarea

And here looks like we have the dodgy line because you are
assigning the internal variable not the function after all. So
gridarea is actually the result of that big calculation.

That is a float value and potentially big, I don't know.
Did you try printing the value of gridarea in your debug trace?
But that shouldn't cause the error since you aren't trying
to convert it to an int.

Very odd.

iy is set in the loop up to ny.  But how does it
compare to the original ny parameter you passed to
N.zeros at the top? Is this a numpy index error we
are actually seeing? I don't know how numpy's zeros
function works... I'm clutching at straws here...

> return area #returns numpy array of area

> Here's the traceback:
>
> ...
>
> 360 357 9
> 360 358 9
> 360 359 9
> 360 0 10

So this is where we loop all the way back to
the outer loop and then start the second loop again..
Can you add gridarea and ny to the debug data?

> OverflowError: long int too large to convert to int
> Traceback:
>  File "", line 1, in ?
>  File "c:\07\Programming\Python\mod_fp\getfp.py", line 11, in ?
>H,fail=readheader(pathname,1,0)
>  File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 170, in 
> readheader
>H['area'] = gridarea(H)
>  File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 332, in 
> gridarea
>area[ix,iy]=gridarea

Personally I'd change gridarea() to take the individiual parameters
rather than the dictionary. There is a shortcut way of doing that
using **kwords which means you canb pass the dictionary but
the unpacking will be done for you by Python...

But most of my comments are stylistic, I have no real idea whats going
wrong here. Sorry.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread Kent Johnson
I have no clue; anyone else?

Maybe you should try the numpy list.

Kent

John wrote:
> for the record:
> nx=360
> ny=180
> nz=1
> 
>  
> On 9/13/07, *John* <[EMAIL PROTECTED] > wrote:
> 
> 
> In all it's glory: I'm just a bit embarrassed because I'm sure it's
> poor coding:
>  
>  
> 
> def gridarea(H):
>  """ returns an array of area corresponding to each nx,ny,nz
>  %===
>  %
>  %---
>  % input
>  %   - H  : Header dict object with nx, ny, nz
>  %
>  % output
>  %   - Numpy array area corresponding to nx,ny,nz
>  %---
>  % 
>  %---
>  % last changes: ww, 2007
>  %===
>  """
>  import math
>  import numpy as N
>  
>  pih=math.pi/180.
>  r_earth=6.371e6
>  cosfunc = lambda y : math.cos(y*pih)*r_earth
>  nz=H['nz']
>  nx=H['nx']
>  ny=H['ny']
>  outlat0=H['outlat0']
>  dyout=H['dyout']
>  dxout=H['dxout']
>  area=N.zeros((nx,ny)) #creates numpy array
>  
>  for iy in range(ny):
>   ylata=outlat0+(float(iy)+0.5)*dyout
>   ylatp=ylata+0.5*dyout
>   ylatm=ylata-0.5*dyout
>   if (ylatm<0 and ylatp>0): hzone=dyout*r_earth*pih
>   else:
>cosfact=cosfunc(ylata)
>cosfactp=cosfunc(ylatp)
>cosfactm=cosfunc(ylatm)
>if cosfactp hzone=math.sqrt(r_earth**2-cosfactp**2)-math.sqrt(r_earth**2-cosfactm**2)
>else:
> hzone=math.sqrt(r_earth**2-cosfactm**2)-math.sqrt(r_earth**2-cosfactp**2)
> 
> 
>   gridarea=2.*math.pi*r_earth*hzone*dxout/360.
>   for ix in range(nx):
>print nx, ix, iy
>area[ix,iy]=gridarea 
>  
>  return area #returns numpy array of area
> 
>  
> 
> Here's the traceback:
> 
> ...
> 
> 360 357 9
> 360 358 9
> 360 359 9
> 360 0 10
> OverflowError: long int too large to convert to int
> Traceback:
>   File "", line 1, in ?
>   File "c:\07\Programming\Python\mod_fp\getfp.py", line 11, in ?
> H,fail=readheader(pathname,1,0)
>   File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 170, in
> readheader
> H['area'] = gridarea(H)
>   File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 332, in
> gridarea
> area[ix,iy]=gridarea
> 
> 
> 
> 
> -- 
> Configuration
> ``
> Plone 2.5.3-final,
> CMF-1.6.4,
> Zope (Zope 2.9.7-final, python 2.4.4, linux2),
> Five 1.4.1,
> Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat 
> 4.1.1-51)],
> PIL 1.1.6

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread John
for the record:
nx=360
ny=180
nz=1


On 9/13/07, John <[EMAIL PROTECTED]> wrote:
>
>
> In all it's glory: I'm just a bit embarrassed because I'm sure it's poor
> coding:
>
>
>
> def gridarea(H):
>  """ returns an array of area corresponding to each nx,ny,nz
>  %===
>  %
>  %---
>  % input
>  %   - H  : Header dict object with nx, ny, nz
>  %
>  % output
>  %   - Numpy array area corresponding to nx,ny,nz
>  %---
>  %
>  %---
>  % last changes: ww, 2007
>  %===
>  """
>  import math
>  import numpy as N
>
>  pih=math.pi/180.
>  r_earth=6.371e6
>  cosfunc = lambda y : math.cos(y*pih)*r_earth
>  nz=H['nz']
>  nx=H['nx']
>  ny=H['ny']
>  outlat0=H['outlat0']
>  dyout=H['dyout']
>  dxout=H['dxout']
>  area=N.zeros((nx,ny)) #creates numpy array
>
>  for iy in range(ny):
>   ylata=outlat0+(float(iy)+0.5)*dyout
>   ylatp=ylata+0.5*dyout
>   ylatm=ylata-0.5*dyout
>   if (ylatm<0 and ylatp>0): hzone=dyout*r_earth*pih
>   else:
>cosfact=cosfunc(ylata)
>cosfactp=cosfunc(ylatp)
>cosfactm=cosfunc(ylatm)
>if cosfactp (r_earth**2-cosfactm**2)
>else: 
> hzone=math.sqrt(r_earth**2-cosfactm**2)-math.sqrt(r_earth**2-cosfactp**2)
>
>
>   gridarea=2.*math.pi*r_earth*hzone*dxout/360.
>   for ix in range(nx):
>print nx, ix, iy
>area[ix,iy]=gridarea
>
>  return area #returns numpy array of area
>
>
>
> Here's the traceback:
>
> ...
>
> 360 357 9
> 360 358 9
> 360 359 9
> 360 0 10
> OverflowError: long int too large to convert to int
> Traceback:
>   File "", line 1, in ?
>   File "c:\07\Programming\Python\mod_fp\getfp.py", line 11, in ?
> H,fail=readheader(pathname,1,0)
>   File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 170, in
> readheader
> H['area'] = gridarea(H)
>   File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 332, in gridarea
> area[ix,iy]=gridarea
>



-- 
Configuration
``
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
4.1.1-51)],
PIL 1.1.6
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread John
In all it's glory: I'm just a bit embarrassed because I'm sure it's poor
coding:



def gridarea(H):
 """ returns an array of area corresponding to each nx,ny,nz
 %===
 %
 %---
 % input
 %   - H  : Header dict object with nx, ny, nz
 %
 % output
 %   - Numpy array area corresponding to nx,ny,nz
 %---
 %
 %---
 % last changes: ww, 2007
 %===
 """
 import math
 import numpy as N

 pih=math.pi/180.
 r_earth=6.371e6
 cosfunc = lambda y : math.cos(y*pih)*r_earth
 nz=H['nz']
 nx=H['nx']
 ny=H['ny']
 outlat0=H['outlat0']
 dyout=H['dyout']
 dxout=H['dxout']
 area=N.zeros((nx,ny)) #creates numpy array

 for iy in range(ny):
  ylata=outlat0+(float(iy)+0.5)*dyout
  ylatp=ylata+0.5*dyout
  ylatm=ylata-0.5*dyout
  if (ylatm<0 and ylatp>0): hzone=dyout*r_earth*pih
  else:
   cosfact=cosfunc(ylata)
   cosfactp=cosfunc(ylatp)
   cosfactm=cosfunc(ylatm)
   if cosfactp", line 1, in ?
  File "c:\07\Programming\Python\mod_fp\getfp.py", line 11, in ?
H,fail=readheader(pathname,1,0)
  File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 170, in readheader
H['area'] = gridarea(H)
  File "c:\07\Programming\Python\mod_fp\mod_fp.py", line 332, in gridarea
area[ix,iy]=gridarea
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread Kent Johnson
John wrote:
> The error occurs here:
> area[ix,iy]=gridarea

What is area? Is it a dict or something else? What is gridarea?

Please show more code and the complete traceback.

Kent
>  
> The values of nx, ix, iy leading up to the error are:
> 360 0 0
> 360 1 0
> 360 2 0
> 360 3 0
> 360 4 0
> 360 ... ...
> 360 357 9
> 360 358 9
> 360 359 9
> 360 0 10
> OverflowError: long int too large to convert to int
> 
> I guess then the problem occurs when iy goes from 9 to 10, but why?? It 
> wasn't a problem before and it's not a problem for ix???
>  
> Thanks!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread John
The error occurs here:
area[ix,iy]=gridarea

The values of nx, ix, iy leading up to the error are:
360 0 0
360 1 0
360 2 0
360 3 0
360 4 0
360 ... ...
360 357 9
360 358 9
360 359 9
360 0 10
OverflowError: long int too large to convert to int

I guess then the problem occurs when iy goes from 9 to 10, but why?? It
wasn't a problem before and it's not a problem for ix???

Thanks!


On 9/13/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> John wrote:
> > I've written a program which calculates areas of grid cells distributed
> > over the globe. It works fine with Python 2.5, however, when I run it
> > with 2.4 (the Enthon edition) I get the following error:
> >
> > OverflowError: long int too large to convert to int
> >
> > It occurs on this line:
> >
> >  for ix in range(nx): area[ix,iy]=gridarea
> >
> > I have no idea which int it's referring to? The values of ix/nx will not
> > exceed 360, iy will not exceed 180. Any suggestions on what to change
> > for the backward compatability?
>
> Are you sure nx is not large? The argument to range() must be an int. I
> would split the line in two so you know which part of it is generating
> the error, then insert a print statement to give you some more data.
>
> Kent
>



-- 
Configuration
``
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
4.1.1-51)],
PIL 1.1.6
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.5 to 2.4 problem with Int

2007-09-13 Thread Kent Johnson
John wrote:
> I've written a program which calculates areas of grid cells distributed 
> over the globe. It works fine with Python 2.5, however, when I run it 
> with 2.4 (the Enthon edition) I get the following error:
> 
> OverflowError: long int too large to convert to int
> 
> It occurs on this line:
> 
>  for ix in range(nx): area[ix,iy]=gridarea   
> 
> I have no idea which int it's referring to? The values of ix/nx will not 
> exceed 360, iy will not exceed 180. Any suggestions on what to change 
> for the backward compatability?

Are you sure nx is not large? The argument to range() must be an int. I 
would split the line in two so you know which part of it is generating 
the error, then insert a print statement to give you some more data.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] 2.5 to 2.4 problem with Int

2007-09-12 Thread John
I've written a program which calculates areas of grid cells distributed over
the globe. It works fine with Python 2.5, however, when I run it with
2.4(the Enthon edition) I get the following error:

OverflowError: long int too large to convert to int

It occurs on this line:

 for ix in range(nx): area[ix,iy]=gridarea

I have no idea which int it's referring to? The values of ix/nx will not
exceed 360, iy will not exceed 180. Any suggestions on what to change for
the backward compatability?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor