-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraster_builder.py
90 lines (73 loc) · 2.62 KB
/
raster_builder.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# script to merge largest cell from a selection of rasters with identical geometry
# Calum Bradbury 27/10/19
import csv
import pandas as pd
import os
import glob
import linecache
import sys
###############################
##CHANGE THESE VARIABLES ONLY##
###############################
#target directory containing the source rasters
#two \ between each file level, two at the end
target = 'C:\\Users\\calum\\Desktop\\Caesar2019\\'
#output file name. Must include extension such as .txt
output = 'output2.txt'
################################
################################
################################
#index raster. Not really neccesary but make handling easier
index_raster = os.path.join(target,"index.txt")
#index iteration controller. Also sets number of lines to skip
x_i = 1
#logic to set up output file as an empty text file
with open(target+output,'w') as writeLine:
print("reset output file")
#getting all source rasters as a list
rasterList = glob.glob(target+'water*.txt')
#function to append output line by line in target directory
def lineWriter(inputText):
#print(inputText)
with open(target+output,'a') as writeLine:
writeLine.write(inputText)
#function to get line of raster at defined index and return as list
def getRasterLine(inputRaster,index):
#does this linecache funtion work properly?
rasterLine = linecache.getline(inputRaster,index)
rasterLine = rasterLine.replace(' \n','')
#set deliminater
rasterLineList = rasterLine.split(" ")
return rasterLineList
#function to read list of lists into pandas dataframe and return list of column maximums
def toDataFrame(list_of_lists):
data_frame = pd.DataFrame(list_of_lists)
max_series = data_frame.max()
max_list = max_series.tolist()
return max_list
#function to build pandas object from specific line in each raster
def temporaryRaster(index):
temporary_list = []
for raster in rasterList:
#appending list from each raster
temporary_list.append(getRasterLine(raster,index))
maximum_list = toDataFrame(temporary_list)
# need some logic here to convert list to string with appropriate deliminater
maximum_string = ''
for item in maximum_list:
maximum_string = maximum_string+str(item)
maximum_string = maximum_string+' '
#print(maximum_string)
maximum_string = maximum_string+'\n'
lineWriter(maximum_string)
#main script
#driven line by line by the index raster
with open(target+'index.txt','r') as indexRaster:
for line in indexRaster:
if x_i < 7:
#copying ASCII information to output file
lineWriter(line)
else:
print("at index numer ",x_i)
temporaryRaster(x_i)
x_i += 1