I have netCDF files with 1500 rows and 2000 columns. Few of them contain inconsistencies in data at different locations. I want to update such inconsistencies with NoData values. While researching I found many answers where one would like to update variable values above/below a certain threshold. For example:
#------ Research-----
dset['var'][:][dset['var'][:] < 0] = -1
#-----------------
Python : Replacing Values in netcdf file using netCDF4
Since, the values of inconsistencies match with the data values, updating inconsistencies based on below / above a certain threshold value is not possible.
My approach 1:
ncfile = r'C:\\abcd\\55618_12.nc'
variableName = 'MAX'
fh = Dataset(ncfile, mode='r+')
for i in range(500,600,1):
for j in range(200,300,1):
fh.variables[variableName][i][j] = -99900.0 # NoData value
#--- or
#fh.variables[variableName][i:j] = -99900.0
fh.close()
Approach 2:
fh = Dataset(ncfile, mode='r')
val = fh.variables[variableName]
for i in range(500,600,1):
for j in range(200,300,1):
val[i][j] = -99900.0
fh = Dataset(ncfile, mode='w') #(ncfile, mode='a')(ncfile, mode='r+')
fh.variables[variableName] = val
fh.close()
Result: The scripts completes processing successfully. However do not update the .nc file.
Friends, your help is highly appreciated.