On 29 March 2011 11:29, Norman Vine <[email protected]> wrote:
> following untested but should work
> assumes a whitespace field separator
> [code]
> readfile='bethlehem.xyz'
>
> file = open(readfile)
> def inside(x,y):
> if x >= -66483.300 and x <= -33474.900:
> if y >= -3155672.310 and y <= -3122229.700:
> return True
> return False
> while 1:
> # read a chunck of the file
> lines = file.readlines(100000)
> if not lines:
> break
> for line in lines:
> # extract x, y and z
> fields = line.split() # or X,Y,Z = line.split()
> if inside(float(fields[0]),float(fields[1])): # or
> inside(float(X),float(Y))
> print line
> [/code]
Alternatively, writing the result into a CSV file:
[code]
import numpy as np
readfile='bethlehem.xyz'
writefile='bethlehem-extract.xyz'
fp_in = open(readfile)
fp_out = open(writefile, 'w')
for line in fp_in:
# extract x, y and z
x, y, z = np.fromstring(line, sep=' ')
if x >= -66483.300 and x <= -33474.900 and \
y >= -3155672.310 and y <= -3122229.700:
# write x, y, and z to a csv file
fp_out.write('%f, %f, %f\n' % (x, y, z))
fp_in.close()
fp_out.close()
[/code]
You can get rid of the Numpy dependency by using x, y, z =
line.split() and casting to float (x = float(x) etc..) as Norman
suggests.
Cheers,
Scott