문제

I have opened a netCDF file that has two variables. Each variable has 4 dimensions: time, height, latitude, and longitude.

I am using a script that is looping over every latitude(i) & longitude step(j) and delivers the data into a float format and are written into my output file, although I am getting this error:

utemp = float(uwind[i][i][j])  # data 
TypeError: only length-1 arrays can be converted to Python scalars

Here I give you the coding I am using.

Read the length:

tstep = range(len(time))
lonstep = range(len(lon))
latstep = range(len(lat))

Set variables:

time = ncfile.variables['time']
lat = ncfile.variables['lat']
lon = ncfile.variables['lon'] 
uwind = ncfile.variables['10u']  

Write into the file:

for t in tstep:
  for i in latstep:      
    for j in lonstep:      
      utemp = float(uwind[t][i][j])  
      windfile.write('%.4f '%utemp)  

Can anybody help me convert the variable into the right format, without getting the error?

This is the change I made and it seems to write into the file just before it stops:

    for t in tstep:     

   windfile.write(header+'\n')    
   for hts in htstep:
      for i in latstep:     
         for j in lonstep:     
            utemp = float(uwind[hts][t][i][j])  # data 
            windfile.write('%.4f '%utemp)  
         windfile.write('\n')
   for hts in htstep:
      for i in latstep:
         for j in lonstep:
            vtemp = float(vwind[hts][t][i][j])
            windfile.write('%.4f '%vtemp)
         windfile.write('\n')
windfile.close() 

From that I get

Traceback (most recent call last):
  File "CFSR2SWAN.py", line 52, in <module>
    utemp = float(uwind[hts][t][i][j])  # data 
IndexError: index out of bounds

I would be grateful if someone could have a look

thank you

도움이 되었습니까?

해결책

You say that uwind is defined at each lat, lon, height, and time, but you are only giving three indices when you call uwind[i][i][j]. So the output will be a 1-d array. Giving a 4th index, e.g. uwind[i,i,j,j] should resolve the error.

However, conceptually I'm not sure why you're using the latstep index i for both of the first two dimensions. If you're just trying to write the array to an external file, consider the pickle (or cPickle) built-in library. (Looping over each gridpoint and time is extremely inefficient.)

Also, I'm assuming you're using the netCDF4 module, in which case there is no need for the float() conversion -- the values will be floating point numbers already (which you can test with, e.g. type(uwind[0,0,0,0]).

다른 팁

Just a sum of the comments and the solution found, Spencer Hill (in the comments) help me guide to the solution.

The problem was initially that not all of the dimensions were assigned to the temp value, each netCDF file has usually 3 or more dimensions (apart from the dataset included), in this case the 3 additional dimensions where time (t), height (ht) , latitude (lat), longitude (lon).

To see the data that are included in the netCDF one can either use >ncdump -h fname or ncdsip in Matlab, after some searching i found out that even though one can see the variables python's build in netCDF modules open them in a different logic.

So after one has registered the data included in the file and identified them do in python

>>>shape(uwind)

and you will get something like this

(8737, 1, 5, 9)

While if you use ncdump or ncdisp the presentation of the variables dimension may differ.

so initially i had this line of code, due the fact that i was using ncdump to "open" the content of the file in Linux/UNIX

utemp = float(uwind[hts][t][i][j])

that produce the error mention.

When In python i tried to >shape(varname) i rewrote the temp as

 vtemp = float(vwind[t][hts][i][j])

And then the code was written correctly

I hope its gonna be helpful

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top