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