Noise Removal of a Raster Data

import netCDF4
import numpy as np
import os

fname='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=9
indexer = filter_size // 2
new_image = data.copy()
nrow, ncol = data.shape
for i in range(nrow):
  for j in range(ncol):
    for k in range(i-indexer, i+indexer+1):
      for m in range(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)
    if data[i,j] > max_value:
        new_image[i,j] = max_value
    elif data[i,j] < min_value:
        new_image[i,j] = min_value
    temp =[]
nc[v][:,:]=new_image[:,:]
nc.close()