Gijs Koetsenruijter
Gijs Koetsenruijter

Reputation: 61

Changing value of a netcdf file for a variable

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.

Upvotes: 4

Views: 12377

Answers (3)

Vinod Kumar
Vinod Kumar

Reputation: 1622

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.

Upvotes: 2

Charlie Zender
Charlie Zender

Reputation: 6312

You might try NCO

ncap2 -s 'where(LU_INDEX == 10) LU_INDEX=2' in.nc out.nc

Upvotes: 10

Gijs Koetsenruijter
Gijs Koetsenruijter

Reputation: 61

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'

Upvotes: 2

Related Questions