Skip to content

Instantly share code, notes, and snippets.

@etes
Last active January 3, 2016 05:39
Show Gist options
  • Save etes/8417023 to your computer and use it in GitHub Desktop.
Save etes/8417023 to your computer and use it in GitHub Desktop.
rows, cols = 10, 10
iceraster = (10*numpy.random.random((rows, cols))).astype(int)*5
iceraster = numpy.where( (iceraster >= 20), 20, iceraster)
outarray = numpy.zeros((rows, cols), numpy.float)
NumberOfDays = 3
#Obsolete pixel-by-pixel version, takes very long
for (i,j), value in numpy.ndenumerate(outarray):
if iceraster[i,j] == 0:
outarray[i,j] = outarray[i,j] + ( 0.0 / NumberOfDays)
elif iceraster[i,j] == 5:
outarray[i,j] = outarray[i,j] + ( 5.0 / NumberOfDays)
elif iceraster[i,j] == 10:
outarray[i,j] = outarray[i,j] + ( 10.0 / NumberOfDays)
elif iceraster[i,j] == 15:
outarray[i,j] = outarray[i,j] + ( 15.0 / NumberOfDays)
elif iceraster[i,j] == 20:
outarray[i,j] = outarray[i,j] + ( 20.0 / NumberOfDays)
outarray2= outarray
outarray = numpy.zeros((rows, cols), numpy.float)
#Array calculation with numpy -- much faster
outarray = numpy.where( (iceraster == 0), (outarray + ( 0.0 / NumberOfDays)) , outarray)
outarray = numpy.where( (iceraster == 5), (outarray + ( 5.0 / NumberOfDays)) , outarray)
outarray = numpy.where( (iceraster == 10), (outarray + ( 10.0 / NumberOfDays)) , outarray)
outarray = numpy.where( (iceraster == 15), (outarray + ( 15.0 / NumberOfDays)) , outarray)
outarray = numpy.where( (iceraster == 20), (outarray + ( 20.0 / NumberOfDays)) , outarray)
outarray.sum() == outarray2.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment