-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbl_preproc.py
147 lines (119 loc) · 5.4 KB
/
bl_preproc.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
##~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~##
## ##
## This file forms part of the BayesLands surface processes modelling companion. ##
## ##
## For full license and copyright information, please refer to the LICENSE.md file ##
## located at the project root, or contact the authors. ##
## ##
##~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~##
#Main Contributer: Danial Azam Email: [email protected]
"""
This script is intended to provide functionality of cropping, rescaling or editing the
input topographies to be used in the model.
"""
import time
import itertools
import numpy as np
import cmocean as cmo
import matplotlib.pyplot as plt
# import pybadlands_companion.resizeInput as resize
from pylab import rcParams
from pyBadlands.model import Model as badlandsModel
from scipy.spatial import cKDTree
from pyBadlands.model import Model as badlandsModel
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.mplot3d import Axes3D
def convertInitialTXT_CSV(directory,fname, res_fact, reduce_factor):
arr = np.loadtxt(fname)
with open('%s/data/convertedInitial_low.csv' %(directory), 'a') as the_file:
for i in xrange(0, arr.shape[0]-2, reduce_factor):
for j in xrange(0, arr.shape[1]-2, reduce_factor):
x_c = i*res_fact
y_c = j*res_fact
line = str(float(y_c)) + ' ' + str(float(x_c))+ ' ' + str(float("{0:.2f}".format(arr[i,j]))) + '\n'
the_file.write(line)
#np.savetxt('data/convert_res_crater.csv', , fmt='%.5f')
def cropTopoCSV(directory,fname, x, y, size, res_fact, max_coord= None):
x = x/res_fact
y = y/res_fact
arr = np.loadtxt(fname)
split_num = max_coord/res_fact
split_arr = np.vsplit(arr,split_num+1)
newarr = []
y_ind = np.arange(y, y+size+1)
print y_ind
print len(split_arr)
for i in y_ind:
p = split_arr[i]
print 'p ', p
newarr.append(p[x:size+1])
X = list(itertools.chain.from_iterable(newarr))
Z = np.vstack(X)
print Z
np.savetxt('%s/data/res_crater.csv' (directory), Z, fmt='%.5f')
def reduceAmplitude(directory,fname, res_fact, amp_percentage):
arr = np.loadtxt(fname)
# new_arr = np.zeros((arr.shape[0],arr.shape[1]))
with open('%s/data/res_crater_reduced_ampl.csv' %(directory), 'a') as the_file:
for i in range(arr.shape[0]-2):
for j in range(arr.shape[1]-2):
x_c = i*res_fact
y_c = j*res_fact
line = str(float(y_c)) + ' ' + str(float(x_c))+ ' ' + str(float("{0:.3f}".format(arr[i,j]*amp_percentage))) + '\n'
the_file.write(line)
def upScale(directory,fname, res_fact):
# Load python class and set required resolution
newRes = resize.resizeInput(requestedSpacing = res_fact)
# Regrid DEM file
newRes.regridDEM(inDEM=fname,outDEM='newnodes.csv')
# Regrid Rain file
# newRes.regridRain(inRain='data/rain.csv',outRain='newrain.csv')
# Regrid Tectonic files (Vertical only file)
# newRes.regridTecto(inTec='data/disp.csv', outTec='newdisp.csv')
# Regrid Tectonic files (3D displacement file)
# newRes.regridDisp(inDisp='data/disp.csv', outDisp='newdisp.csv')
def main():
functionality = input("Would you like to: \n 1) Resuce Amplitude of Initial/Final topo\n 2) Convert topo TXT to CSV w/ reduction\n 3) Crop Topo CSV file\n 4) Upscale Topo")
choice = input("Please choose a Badlands example to apply it to:\n 1) crater_fast\n 2) crater\n 3) etopo_fast\n 4) etopo\n 5) delta\n")
directory = ""
if choice == 1:
directory = 'Examples/crater_fast_3030'
elif choice ==2:
directory = 'Examples/crater'
elif choice ==3:
directory = 'Examples/etopo_fast'
elif choice ==4:
directory = 'Examples/etopo'
# elif choice ==5:
# directory = 'Examples/mountain'
if functionality == 1:
tstart = time.clock()
amp_percentage = input("What percentage would you like to decrease the amplitude by?")
res_fact = input("Resolution Factor")
topo = input("Would you like to apply it to the \n 1) Final topo \n 2) Initial topo")
if topo == 1:
reduceAmplitude(directory,'%s/data/initial_elev.txt'%(directory), res_fact=res_fact, amp_percentage= amp_percentage)
else:
reduceAmplitude(directory,'%s/data/final_elev.txt'%(directory), res_fact=res_fact, amp_percentage= amp_percentage)
print 'Task completed in (s):',time.clock()-tstart
elif functionality == 2:
tstart = time.clock()
reduce_factor = input("What should the reduction factor be?")
res_fact = input("Resolution Factor")
convertInitialTXT_CSV(directory,'%s/data/initial_elev.txt' %(directory), res_fact = res_fact, reduce_factor = reduce_factor)
print 'Task completed in (s):',time.clock()-tstart
elif functionality == 3:
tstart = time.clock()
x_coord = input("X coord")
y_coord = input("Y coord ")
grid_size = input("Size of Grid")
res_fact = input("Resolution Factor")
max_size = 2400
cropTopoCSV(directory,'%s/data/res_crater.csv'%(directory), x_coord, y_coord, grid_size, 10, max_size)
print 'Task completed in (s):',time.clock()-tstart
# elif functionality == 4:
# tstart = time.clock()
# res_fact = input("Resolution Factor")
# upScale('%s/data/res_crater.csv'%(directory), res_fact)
# print 'Task completed in (s):',time.clock()-tstart
if __name__ == "__main__": main()