4

I have a large netcdf file which is three dimensional. I want to replace for the variable LU_INDEX in the netcdf file all the values 10 with 2.

I wrote this python script to do so but it does not seem to work.

filelocation = 'D:/dataset.nc'

ncdataset = nc.Dataset(filelocation,'r')
lat           = ncdataset.variables['XLAT_M'][0,:,:]
lon           = ncdataset.variables['XLONG_M'][0,:,:]
lu_index     = ncdataset.variables['LU_INDEX'][0,:,:]
lu_index_new = lu_index
ncdataset.close()

nlat,nlon=lat.shape

for ilat in range(nlat):
    for ilon in range(lon):
        if lu_index == 10:
          lu_index_new[ilat,ilon] = 2

newfilename = 'D:/dataset.new.nc'
copyfile(ncdataset,newfilename)


newfile     = nc.Dataset(newfilename,'r+')
newfile.variables['LU_INDEX'][0,:,:]   = lu_index_new
newfile.close()

I get the error:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I am not a very experienced with python, so if there is a more easier way to do this you are very welcome to comment.

3 Answers 3

10

You might try NCO

ncap2 -s 'where(LU_INDEX == 10) LU_INDEX=2' in.nc out.nc
Sign up to request clarification or add additional context in comments.

Comments

2

I worked it out as follow:

import netCDF4 as nc
import numpy as np

pathname = 'D:'
filename = '%s/dataset.nc'%pathname
ncfile = nc.Dataset(filename,'r+')
lu_index = ncfile.variables['LU_INDEX'][:]
I = np.where(lu_index == 10)
lu_index[I] = 2
ncfile.variables['LU_INDEX'][:] = lu_index
filename.close()

print 'conversion complete'

3 Comments

The concision difference between the NCO and Python solutions is striking.
NCO is probably 1000x faster too. Pro tip: always leverage NCO where possible.
not just faster but also memory efficient. It's an awesome set of programs! Thanks Charlie
2

using np.array might not work for a very large dataset with the following error

"ValueError: array is too big; arr.size * arr.dtype.itemsize is larger than the maximum possible size."

CDO can be a nice tool other than NCO and for me, it is much faster.

cdo setvals,10,2 in.nc out.nc

It is particularly fast when you have to replace values in more than one variables in the same nc file (e.g. replace missing values representation). One can use setrtoc in place of setval for specifying a range.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.