importnetCDF4importnumpyasnpimportosfname='v50_SO2_2015_3_TNR_Ship.0.1x0.1.nc'nc=netCDF4.Dataset(fname,'r')v='emi_so2'data=np.array(nc[v][:,:])nc.close()fnameO=fname.replace('.nc','N.nc')os.system('cp '+fname+' '+fnameO)nc=netCDF4.Dataset(fnameO,'r+')# first a conservative filter for grayscale images will be defined.
temp=[]filter_size=9indexer=filter_size//2new_image=data.copy()nrow,ncol=data.shapeforiinrange(nrow):forjinrange(ncol):forkinrange(i-indexer,i+indexer+1):forminrange(j-indexer,j+indexer+1):if(k>-1)and(k<nrow):if(m>-1)and(m<ncol):temp.append(data[k,m])temp.remove(data[i,j])max_value=max(temp)min_value=min(temp)ifdata[i,j]>max_value:new_image[i,j]=max_valueelifdata[i,j]<min_value:new_image[i,j]=min_valuetemp=[]nc[v][:,:]=new_image[:,:]nc.close()