jswhit / pyspharm

Automatically exported from code.google.com/p/pyspharm
Other
33 stars 17 forks source link

TypeError: 'float' object cannot be interpreted as an integer #6

Open winash12 opened 3 years ago

winash12 commented 3 years ago

I have this sample code that I am running using pyspharm and I am getting this error -

` file1 = 'uwnd_200_2020_3_10_00Z.nc' file2 = 'vwnd_200_2020_3_10_00Z.nc'

nc_uwndFile = Dataset(file1,'r')
nc_vwndFile = Dataset(file2,'r')
lats = nc_uwndFile.variables['lat'][:]  # extract/copy the data
lons = nc_uwndFile.variables['lon'][:]

s = Spharmt(len(lons),len(lats), gridtype='regular',
                         rsphere=6.3712e6, legfunc='stored')

u = nc_uwndFile.variables['uwnd'][:]
v = nc_vwndFile.variables['vwnd'][:]
u = u[0,0,:,:]
v = v[0,0,:,:]
print(u.shape,v.shape)

ntruncation = 13
uspectral = s.grdtospec(u,None)
vspectral = s.grdtospec(v,None)
dudx,dudy = s.getgrad(uspectral)
dvdx,dvdy = s.getgrad(vspectral)

` amd I am getting this error -

Where am I going wrong ?

File "testspharm.py", line 29, in main dudx,dudy = s.getgrad(uspectral) File "/usr/local/lib/python3.8/dist-packages/spharm/spharm.py", line 878, in getgrad chispec = numpy.reshape(chispec, ((ntrunc+1)*(ntrunc+2)/2,1)) File "<__array_function__ internals>", line 5, in reshape File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 299, in reshape return _wrapfunc(a, 'reshape', newshape, order=order) File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 67, in _wrapfunc return _wrapit(obj, method, *args, **kwds) File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 44, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds) TypeError: 'float' object cannot be interpreted as an integer

winash12 commented 3 years ago

Any update on this ? I tried changing to SHTns but that has this issue - https://bitbucket.org/nschaeff/shtns/issues/38/valueerror-nlat-must-be-even

DWesl commented 1 month ago

The issue would be with this line here:

numpy.reshape(chispec, ((ntrunc+1)*(ntrunc+2)/2,1))

In python 3, the normal / division returns a float, even when used with two integers. To have an integer result, one must use the floor division operator //. That is, the following should work:

numpy.reshape(chispec, ((ntrunc+1)*(ntrunc+2)//2,1))

That error in particular looks like issues I found when running the example code recently.

winash12 commented 1 month ago

@DWesl Great work !!!

winash12 commented 1 month ago

@DWesl Would you be interested in collaborating with me on a rewrite of this package ?