source: anuga_work/production/australia_ph2/sydney/maxasc.py @ 6176

Last change on this file since 6176 was 6176, checked in by jgriffin, 15 years ago

Folder created for phase 2 scripts.
Basic scripts started for Sydney.

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