This is a quick demo on how to create ASCII grid files of a variety of forms using Python's random module.
'''
write_ascii.py
A quick demo on creating an ascii grid which can be used for testing
purposes. Everything is hard coded now, but it could easily be
converted to a tool for use in programs. You can create random
integer, uniform etc grids using the python random options.
Specify the filename, classes etc below
can also use:
randint( a, b) random integer between a and b
uniform( a, b) random float between a and b
betavariate( alpha, beta)
gammavariate( alpha, beta)
gauss( mu, sigma)
lognormvariate( mu, sigma)
amongst others, check the Python random module documentation
'''
import random
output_file = "c:/temp/Ascii_nodata.asc" #alter to suit
classes = 4
ncols = 10
nrows = 10
xllcorner = 0
yllcorner = 0
cellsize = 1
NODATA_value = -9999
header = ["ncols " + str(ncols) + "\n",
"nrows " + str(nrows) + "\n",
"xllcorner " + str(xllcorner) + "\n",
"yllcorner " + str(yllcorner) + "\n",
"cellsize " + str(cellsize) + "\n",
"NODATA_value " + str(NODATA_value)]
out_file = open(output_file,'w')
for i in header:
out_file.write(i)
for i in range(nrows):
a_line = "\n"
for j in range(ncols):
a_val = int(random.random() * classes) #integer value
#a_val = random.randint(0, 4) #random integer in range 0-4
a_line += str(a_val) + " "
out_file.write(a_line)
out_file.close()
No comments:
Post a Comment