-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.py
76 lines (75 loc) · 2.58 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy
def gridasciitonumpyarrayfloat(ingridfilefullpath):
i=0
row = 0
headerstr=''
infile=open(ingridfilefullpath, "r")
for line in infile:
if i==0:
ncols=int(line.strip().split()[-1])
headerstr+=line
elif i==1:
nrows=int(line.strip().split()[-1])
headerstr += line
elif i==2:
xllcorner=float(line.strip().split()[-1])
headerstr += line
elif i==3:
yllcorner=float(line.strip().split()[-1])
headerstr += line
elif i==4:
cellsize=float(line.strip().split()[-1])
headerstr += line
elif i==5:
NODATA_value=float(line.strip().split()[-1])
arr=numpy.zeros((nrows, ncols), dtype=float)
arr[:,:]=NODATA_value
headerstr += line.replace("\n","")
elif i>5 and i<nrows:
col=0
while col<ncols:
for item in line.strip().split():
arr[row,col]=float(item)
col+=1
row+=1
i+=1
infile.close()
return arr, ncols, nrows, xllcorner, yllcorner, cellsize, NODATA_value, headerstr
def gridasciitonumpyarrayint(ingridfilefullpath):
i=0
row = 0
headerstr=''
infile=open(ingridfilefullpath, "r")
for line in infile:
if i==0:
ncols=int(line.strip().split()[-1])
headerstr+=line
elif i==1:
nrows=int(line.strip().split()[-1])
headerstr += line
elif i==2:
xllcorner=float(line.strip().split()[-1])
headerstr += line
elif i==3:
yllcorner=float(line.strip().split()[-1])
headerstr += line
elif i==4:
cellsize=float(line.strip().split()[-1])
headerstr += line
elif i==5:
NODATA_value=float(line.strip().split()[-1])
arr=numpy.zeros((nrows, ncols), dtype=int)
arr[:,:]=NODATA_value
headerstr += line.replace("\n","")
elif i>5 and i<nrows:
col=0
while col<ncols:
for item in line.strip().split():
arr[row,col]=float(item)
col+=1
row+=1
i+=1
infile.close()
return arr, ncols, nrows, xllcorner, yllcorner, cellsize, NODATA_value, headerstr
#print "write the output...."
#numpy.savetxt(outdir+"/"+"outarrname"+".asc", outarrbestguess, delimiter=' ',newline='\n', header=headerstr, comments='')