0

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.

gmas80
  • 1,218
  • 1
  • 14
  • 44

2 Answers2

2

Following approach worked for me:

import netCDF4 as nc
import numpy as np

ncfile   =   r'C:\\abcd\\55618_12.nc'
variableName =  'MAX'

fh = nc.Dataset(ncfile, mode='r')
val = fh.variables[variableName][:]
fh.close()

print type (val)

for i in range(500,600,1):
    for j in range(200,300,1):
        #print i,j
        val[i][j] = -99900.0
        if val[i][j]> -99900.0:
            print val[i][j]


fh = nc.Dataset(ncfile, mode='r+')
fh.variables[variableName][:]= val
fh.close()
  • 1
    You question is a bit unclear. Do you always want to set the range `i=500:600, j=200:300` to your fill value? In that case, simply use `val[500:600,200:300] = -99900.0` (so without the double loop). And if you directly open the file as `r+`, you don't need to open it twice.. – Bart Aug 02 '17 at 09:36
  • Sorry for the delay in response. The range changes for different files. The provided information is helpful. especially r+ mode information. Thanks. – GIS Learner Nov 01 '17 at 06:23
1

Is the data on a lat/lon grid? If so it may be easier to do it from the command line using cdo:

cdo setclonlatbox,FillValue,lon1,lon2,lat1,lat2  infile.nc outfile.nc

Where FillValue is your missing value which seems to be -99900.0 in your case.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86