Good afternoon,

I´m doing a number of calculations on a timeseries of data and want to create a 2d netcdf file with the results of those calculations. But when saving the calculated values in the new array I get the following message: IndexError: too many indices for array.

I am using python 3.6 with the NetCDF4 and numpy libraries.

#create netcdf
out_file=netCDF4.Dataset("test.nc","w", format="NETCDF4")
tempgrp.createDimension('lon', 575)
tempgrp.createDimension('lat', 541)
tempgrp.createDimension('time', None)

longitude = tempgrp.createVariable('Longitude', 'f4', 'lon')
latitude = tempgrp.createVariable('Latitude', 'f4', 'lat')  
temp = tempgrp.createVariable(var, 'f4', ('time', 'lon', 'lat'))
time = tempgrp.createVariable('Time', 'i4', 'time')

longitude[:] = 575
latitude[:] = 541

for i in range(540):
    for j in range(574):
                del val_list
                for t in range(30):
                    #create list of i,j coordinate
                temp[0,j,i] = av #this is where the error occurs

This is the output of print(temp):

float32 Chlx(time, lon, lat) path = /Chlx unlimited dimensions: time current shape = (0, 575, 541) filling on, default _FillValue of 9.969209968386869e+36 used

The idea is to in the end create a 3d array (that is why the time dimension is there). Can anyone give me a hint to why I get this index error?

  • Without knowing the values in the undeclared variables (len_time, len_lat, len_lon, data) it is hard to say why you are getting this error. Also, it seems as though you could do away with the loop and get the desired value using numpy mathematical functions on the array as a whole and then filling your 2D array all at once. – Jacob F Aug 24 '17 at 15:28
  • I edited the question to include the missing values. Do I understand that it is possible to use numpy calculations to work directly with all values that are located at a specific location? I have been looking into scipy´s amin function [link] (docs.scipy.org/doc/numpy/reference/generated/…) but could not figure out how to identify the time axis at a specific location. – fjboogert Aug 24 '17 at 16:04
  • Your original question was about filling a NetCDF and I mentioned the numpy functions because your example was calculating a mean which would not require the loops in your code. If you are trying to do some other calculation with your array then you probably need to start a new question, and it's not really a GIS question. The output you are getting from print(temp) is expected. If you want to see the actual values you would use temp[:]. The index error you are receiving is because you are passing too many indices to an array, probably data. What is the output from data.shape? – Jacob F Aug 24 '17 at 17:09
  • Thanks for the tips @JacobF. The output of data.shape is (31, 1, 541, 575) which tells me that data contains (time, z, lat, lon). Which is as expected. What I do not get however is that av, which is one single value, can not be stored at a location of my choosing. In this case temp[0,i,j].I assume the first value to be stored would be in location 0,0,0. – fjboogert Aug 24 '17 at 22:39
  • Not sure what's causing your error. I added the lines: data = numpy.random.rand(31,1,541,575) var = 'test' at the top of your code above and it ran with no errors. – Jacob F Aug 25 '17 at 14:08

In case someone has the same issue.

It turns out the netCDF4 library gets an index error if you try to insert a NaN value. I have some areas in the map that are on land and thus have no value. I avoided the issue by adding the following check:

if av == int:
 temp[0,j,i] = av
 temp[0,j,i] = 9.969209968386869e+36

Now when av is a non value it is replaced by the fill value of the netCDF before iserting it. There might be a more ellegant solution but this seems to work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.