source: trunk/anuga_core/anuga/lib/maxasc.py @ 9737

Last change on this file since 9737 was 9543, checked in by steve, 10 years ago

Changing back all as I think the error wa due to folders of the same name

File size: 3.0 KB
Line 
1#!/usr/bin/env python
2
3"""
4The MaxAsc() function.
5Takes 1 or more ASC files and generates an output ASC file with
6an element-wise maximum.
7"""
8
9import sys
10import re
11
12# if user does 'from ... import *' only give her/him MaxAsc
13__all__ = ['MaxAsc']
14
15HEADER_SIZE = 6
16
17# pattern string used to split multimax data
18SpacesPatternString = ' +'
19
20# generate 're' pattern for 'any number of spaces'
21SpacesPattern = re.compile(SpacesPatternString)
22
23
24def MaxAsc(out_file, in_files):
25    """
26    MaxAsc('output_filename', ['list', 'of', 'filenames'])
27   
28    The output file is an ASC file with each element being the maximum of
29    the corresponding element in all the input ASC files.  The output file
30    has the same shape as the input file(s).
31    """
32   
33    # get all file data into memory
34    file_data = []
35    for f in in_files:
36        fd = open(f, 'r')
37        data = fd.readlines()
38        file_data.append(data)
39        fd.close()
40
41    # check we have same number of lines in each file
42    num_lines = len(file_data[0])
43    for (i, d) in enumerate(file_data):
44        if len(d) != num_lines:
45            raise RuntimeError, \
46                  ("File %s has the wrong number of lines "
47                   "(%d, expected %d)." % (in_files[i], len(d), num_lines))
48   
49    # open the output file
50    out_fd = open(out_file, 'w')
51
52    # read header lines, check same, write out
53    for i in range(HEADER_SIZE):
54        line = file_data[0][i]
55        for (j, f) in enumerate(file_data):
56            d = f[i]
57            if d != line:
58                out_fd.close()
59                raise RuntimeError, \
60                      "File %s has the wrong header at line %d." % \
61                      (in_files[j], i)
62        out_fd.write(line)
63
64    # read data lines
65    for line_num in range(HEADER_SIZE, num_lines):
66        lines = []
67        col_file = ''
68        columns = None
69        for (i, f) in enumerate(file_data):
70            data = f[line_num]
71            if len(data) > 1:
72                data = data.strip()
73                data = SpacesPattern.split(data)
74                if columns:
75                    if columns != len(data):
76                        out_fd.close()
77                        raise RuntimeError, \
78                              ("File %s doesn't have same number of columns "
79                               "(%d) as file %s (%d), line %d!?" %
80                               (fd.name, len(data), col_file, columns, line_num))
81                else:
82                    col_file = in_files[i]
83                    columns = len(data)
84                fdata = [float(value) for value in data]
85                lines.append(fdata)
86        outline = ''
87        for i in range(columns):
88            maximum = lines[0][i]
89            for d in lines:
90                if maximum < d[i]:
91                    maximum = d[i]
92            outline += ' %10.4e ' % maximum
93        out_fd.write('%s\n' % outline)
94       
95    # close the output file
96    out_fd.close()
Note: See TracBrowser for help on using the repository browser.