source: trunk/anuga_core/source/anuga/file/mux.py @ 8144

Last change on this file since 8144 was 8144, checked in by wilsonr, 14 years ago

Removed '@brief' comments.

File size: 4.1 KB
Line 
1"""
2    Read a mux2 file.
3"""
4
5from anuga.utilities.numerical_tools import ensure_numeric
6import numpy as num     
7       
8################################################################################
9# READ MUX2 FILES line of points
10################################################################################
11
12WAVEHEIGHT_MUX_LABEL = '-z-mux'
13EAST_VELOCITY_LABEL =  '-e-mux'
14NORTH_VELOCITY_LABEL =  '-n-mux'
15
16WAVEHEIGHT_MUX2_LABEL = '-z-mux2'
17EAST_VELOCITY_MUX2_LABEL = '-e-mux2'
18NORTH_VELOCITY_MUX2_LABEL = '-n-mux2'
19
20def read_mux2_py(filenames,
21                 weights=None,
22                 permutation=None,
23                 verbose=False):
24    """Access the mux files specified in the filenames list. Combine the
25       data found therin as a weighted linear sum as specifed by the weights.
26       If permutation is None or empty extract timeseries data for all gauges
27       within the files.
28
29       Input:
30           filenames:   List of filenames specifiying the file containing the
31                        timeseries data (mux2 format) for each source
32           weights:     Weighs associated with each source
33                        (defaults to 1 for each source)
34           permutation: Specifies the gauge numbers that for which data is to be
35                        extracted
36    """
37
38    from urs_ext import read_mux2
39
40    numSrc = len(filenames)
41
42    file_params = -1 * num.ones(3, num.float)                    # [nsta,dt,nt]
43
44    # Convert verbose to int C flag
45    if verbose:
46        verbose = 1
47    else:
48        verbose = 0
49
50    if weights is None:
51        weights = num.ones(numSrc)
52
53    if permutation is None:
54        permutation = ensure_numeric([], num.float)
55
56    # Call underlying C implementation urs2sts_ext.c
57    data = read_mux2(numSrc, filenames, weights, file_params,
58                     permutation, verbose)
59
60    msg = 'File parameter values were not read in correctly from c file'
61    assert len(num.compress(file_params > 0, file_params)) != 0, msg
62
63    msg = 'The number of stations specifed in the c array and in the file ' \
64          'are inconsistent'
65    assert file_params[0] >= len(permutation), msg
66
67    msg = 'The number of stations returned is inconsistent with ' \
68          'the requested number'
69    assert len(permutation) == 0 or len(permutation) == data.shape[0], msg
70
71    nsta = int(file_params[0])
72    msg = 'Must have at least one station'
73    assert nsta > 0, msg
74
75    dt = file_params[1]
76    msg = 'Must have a postive timestep'
77    assert dt > 0, msg
78
79    nt = int(file_params[2])
80    msg = 'Must have at least one gauge value'
81    assert nt > 0, msg
82
83    OFFSET = 5 # Number of site parameters p passed back with data
84               # p = [geolat,geolon,depth,start_tstep,finish_tstep]
85
86    # FIXME (Ole): What is the relationship with params and data.shape ?
87    # It looks as if the following asserts should pass but they don't always
88    #
89    #msg = 'nt = %d, data.shape[1] == %d' %(nt, data.shape[1])
90    #assert nt == data.shape[1] - OFFSET, msg
91    #
92    #msg = 'nsta = %d, data.shape[0] == %d' %(nsta, data.shape[0])
93    #assert nsta == data.shape[0], msg
94
95    # Number of stations in ordering file
96    number_of_selected_stations = data.shape[0]
97
98    # Index where data ends and parameters begin
99    parameters_index = data.shape[1] - OFFSET
100
101    times = dt * num.arange(parameters_index)
102    latitudes = num.zeros(number_of_selected_stations, num.float)
103    longitudes = num.zeros(number_of_selected_stations, num.float)
104    elevation = num.zeros(number_of_selected_stations, num.float)
105    quantity = num.zeros((number_of_selected_stations, parameters_index), \
106                                                    num.float)
107
108    starttime = 1e16
109    for i in range(number_of_selected_stations):
110        quantity[i][:] = data[i][:parameters_index]
111        latitudes[i] = data[i][parameters_index]
112        longitudes[i] = data[i][parameters_index+1]
113        elevation[i] = -data[i][parameters_index+2]
114        first_time_step = data[i][parameters_index+3]
115        starttime = min(dt*first_time_step, starttime)
116
117    return times, latitudes, longitudes, elevation, quantity, starttime
118
119
120
Note: See TracBrowser for help on using the repository browser.