python - ArcPy: Initialize zero valued grid Raster and iteratively increment -
i'm jumping right arcpy
out friend needs automate arcgis work. problem @ hand can reduced following pseudocode:
accumulationraster = somezeroedraster each raster pair, r, s, pertaining rain , snow: accumulationraster += con(s == 0, con(r >= 1, 1, 0), 0)
in other words, iterate through data of rain , snow of each day. perform logical operation on each cell pair, corresponding cell in accumulationraster
gets incremented if no snow (s == 0
) , rain (r >= 1)
.
having no luck far various attempts, thought i'd ask here, problem should have easy solution. full working code missing gaps pointed out below. of trivial file path set up, , above pseudocode should suffice:
the missing pieces numbered 1
, 2
, , 3
.
how initializase 0 raster w/proper dimensions.
how make copy of 0 raster. if
1
simple, instead.increment accumulation raster.
import os import arcpy arcpy import env arcpy.sa import * in_workspace = "c:/data/projectfolder" os.chdir(in_workspace) arcpy.env.workspace = in_workspace maaneder = ["01_januar", "02_februar", "03_mars", "04_april", "05_mai", "06_juni", "07_juli", "08_august", "09_september", "10_oktober", "11_november", "12_desember"] ################ 1 ################### baseaccum = #1: ?? (zero raster w/correct type & dimensions) ###################################### outputpath = "raindaysingrowthseason/" year in range(1997,2015): month in range(1, 13): ################ 2 ################### monthaccum = #2: ?? (copy of basesum) ###################################### day in range(1,32): # e.g 1997/09_september/ fpath = str(year) + "/" + maaneder[month-1] + "/" # e.g. 1997_09_08.asc filesuffix = str(year) + "_" + str(month).zfill(2) + "_" + str(day).zfill(2) + ".asc" # e.g. snoedybde/1997/09_september/sd_1997_09_08.asc snoefile = "snoedybde/" + fpath + "sd_" + filesuffix # e.g. rrl/1997/09_september/rrl_1997_09_08.asc rrlfile = "rrl/" + fpath + "rrl_" + filesuffix hassnoe = os.path.isfile(snoefile) hasrrl = os.path.isfile(rrlfile) if (not hassnoe or not hasrrl): continue snoeraster = raster(snoefile) rrlraster = raster(rrlfile) ############## 3 ##################### monthaccum = #3: ?? # a'la: # += con(snoeraster == 0, con(rrlraster >= 1, 1, 0), 0) ###################################### # e.g. raindaysingrowthseason/1997 outputdir = outputpath + str(year) if (not os.path.isdir(outputdir)): os.mkdir(outputdir) monthaccum.save(outputdir + "/r_" + str(year) + "_" + str(month).zfill(2))
you can use arcpy.numpyarraytoraster make 0 base raster.
if wanted raster 50 rows , 50 columns 1 meter resolution. can this.
import numpy myarr = numpy.zeros(50, 50) myzeroraster = arcpy.numpyarraytoraster(myarr, arcpy.point(355355, 3199277), 1, 1)
you can convert raster numpy array , accumulations using 1 numpy array another.
or if have spatial analyst, use tool create constant raster create 0 based raster , use math -> plus tool add rasters together
Comments
Post a Comment