-
Notifications
You must be signed in to change notification settings - Fork 13
/
filter_pts.py
executable file
·75 lines (67 loc) · 2.16 KB
/
filter_pts.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
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# filter_pts.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Oct 29, 2020
#
# Purpose: The script reads in a points file, reads the values, and
# retains only values smaller or larger than the threshold value. I am
# envisioning using this script to filter land elevations from a
# bathymetric data set.
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python filter_pts.py -i in.csv -t 1 -a lt -o out.csv
#
# where:
# -i --> input points file
# -t --> threshold value
# -a --> action, can be either gt (greater than) or lt (less than)
# -o --> output points file
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys # system parameters
import numpy as np # numpy
#
if len(sys.argv) == 9 :
input_file = sys.argv[2]
threshold = float(sys.argv[4])
action = sys.argv[6]
output_file = sys.argv[8]
else:
print('Wrong number of Arguments, stopping now...')
print('Usage:')
print('python filter_pts.py -i in.csv -t 1 -a gt -o out.csv')
sys.exit()
if ( (action != 'lt') and (action != 'gt') ):
print('Action can only be gt or lt. Exiting.')
sys.exit()
# to read the input data using numpy
input_data = np.loadtxt(input_file, delimiter=',',skiprows=0,unpack=True)
fout = open(output_file, 'w')
# poly coords
x = input_data[0,:]
y = input_data[1,:]
z = input_data[2,:]
# number of points
n = len(x)
for i in range(n):
if (action == 'lt'):
if (z[i] < threshold):
fout.write(str(x[i]) + ',' + str(y[i]) + ',' + str(z[i]) + '\n')
elif (action == 'gt'):
if (z[i] > threshold):
fout.write(str(x[i]) + ',' + str(y[i]) + ',' + str(z[i]) + '\n')
else:
print('Wrong action. Try again!')
fout.close()