Dir rename test.
[pyplotsuite.git] / common / arrayfromfile.py
blobc13f616397bd7dc362633d971f012409da4f132f
1 #!/bin/env python
3 # Copyright (C) 2006-2007 Antonio Ingargiola <tritemio@gmail.com>
5 # This file is part of Image Analyzer.
7 # Image Analyzer is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 from PIL import Image
13 from numpy import array, ones, zeros, sqrt, ceil, floor
14 from common.img import image2array
16 def arrayFromTiff(filename):
17 """
18 Returns and array loading the data from the TIFF file 'filename'.
19 """
20 return image2array(Image.open(filename))
22 def arrayfromfile(file, xmin=None, xmax=None, debug=False):
23 """
24 Load (and returns) an array fron an ASCII text file. 'file' is the opened
25 file object.
27 Optional 'xmin' and 'xmax' permit to load only a slice of colums (for
28 example excluding the fistr and the last).
29 """
31 if xmin == None: start = 0
32 else: start = xmin
34 if xmax == None: end = -1
35 else: end = xmax
37 list_of_rows = []
38 i = 1
39 for line in file:
40 if line.strip() == '': continue
41 data = line.split()
42 if end == -1: row_data = [float(s) for s in data[start:]]
43 else: row_data = [float(s) for s in data[start:end+1]]
44 if debug: print i, 'len:', len(row_data)
45 list_of_rows.append(row_data)
46 i += 1
48 m = array(list_of_rows)
49 return m
51 def arrayFromZemax(zemaxfile, xmin=None, xmax=None):
52 """
53 Returns an arry loading the data from a Zemax simulation file. Zemax
54 simulation file are ASCII file with a particular header and syntax. See the
55 code for more information.
56 """
58 def skip_n_lines(n):
59 for i in xrange(n): zemaxfile.next()
61 for line in zemaxfile:
62 if line.startswith('Detector Viewer listing'):
63 #skip_n_lines(19) ## 2003 Version
64 skip_n_lines(23) ## 2005 Version
65 ## TODO: Automatic file version handling
66 zm = arrayfromfile(zemaxfile)[:,1:]
67 elif line.startswith('Illumination Surface Listing'):
68 skip_n_lines(12)
69 zm = arrayfromfile(zemaxfile, xmin=xmin, xmax=xmax, debug=True)
70 else:
71 print 'arrayFromZemax(): Invalid file format.'
72 zm = None
73 return zm