[852] | 1 | """ Interpolation of a sww file. |
---|
| 2 | Used to interpolate height information from sww files. |
---|
| 3 | |
---|
| 4 | When using as a stand alone application, |
---|
| 5 | Input; |
---|
| 6 | - The sww file with stage and bed elevation information |
---|
| 7 | - An xya file specifying the points (x,y) where the height values |
---|
| 8 | (w.r.t. time) are needed. |
---|
| 9 | |
---|
| 10 | Ouput; |
---|
| 11 | An xya file with x,y and height values w.r.t. time. |
---|
| 12 | |
---|
| 13 | The first row of the output xya file has the time value for each height |
---|
| 14 | Each following row has the format x,y,[height,] |
---|
| 15 | |
---|
| 16 | NOTE: stage = bed elevation + height |
---|
| 17 | |
---|
| 18 | Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou |
---|
| 19 | Geoscience Australia, 2004. |
---|
| 20 | """ |
---|
| 21 | |
---|
[1219] | 22 | ##FIXME (DSG-DSG) no sww file? give a better error message. |
---|
| 23 | |
---|
[852] | 24 | from Numeric import transpose |
---|
| 25 | from least_squares import Interpolation |
---|
| 26 | |
---|
[902] | 27 | DEFAULT_QUANTITY = "depth" |
---|
[852] | 28 | |
---|
| 29 | class Interpolate_sww: |
---|
[1137] | 30 | def __init__(self, file_name, quantity_name): |
---|
[852] | 31 | |
---|
| 32 | #It's bad to have the quantity_name passed in here. |
---|
| 33 | # But it works with how the program is currently used. |
---|
| 34 | # Refactor when necessary. - DSG |
---|
| 35 | |
---|
| 36 | x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name) |
---|
| 37 | vertex_coordinates = transpose([x,y]) |
---|
| 38 | |
---|
| 39 | if False: |
---|
| 40 | print "****************************" |
---|
| 41 | print "x " |
---|
| 42 | print x |
---|
| 43 | print "****************************" |
---|
| 44 | print "Y " |
---|
| 45 | print y |
---|
| 46 | print "****************************" |
---|
| 47 | print "V " |
---|
| 48 | print volumes |
---|
| 49 | print "****************************" |
---|
| 50 | print "Time " |
---|
| 51 | print time |
---|
| 52 | print "****************************" |
---|
| 53 | print "quantity " |
---|
| 54 | print quantity |
---|
| 55 | print "****************************" |
---|
| 56 | print "vertex_coordinates" |
---|
| 57 | print vertex_coordinates |
---|
| 58 | print "****************************" |
---|
[902] | 59 | |
---|
[852] | 60 | self.interp = Interpolation(vertex_coordinates, volumes, alpha=0) |
---|
| 61 | self.time = time |
---|
| 62 | |
---|
| 63 | self.quantity_name = quantity_name |
---|
| 64 | self.quantity = quantity |
---|
| 65 | |
---|
[1137] | 66 | def interpolate_xya(self, file_name): |
---|
[852] | 67 | """ |
---|
| 68 | Given a point file, interpolate the height w.r.t. time at the points |
---|
| 69 | specified in the point file. |
---|
| 70 | |
---|
| 71 | Input; |
---|
| 72 | file_name - the xya file |
---|
| 73 | """ |
---|
[1396] | 74 | |
---|
| 75 | from load_mesh.loadASCII import import_points_file |
---|
| 76 | |
---|
| 77 | point_dict = import_points_file(file_name) |
---|
| 78 | self.point_coordinates = point_dict['pointlist'] |
---|
[852] | 79 | self.interp.build_interpolation_matrix_A(self.point_coordinates) |
---|
[1396] | 80 | self.interpolated_quantity_raw = self.interp.interpolate(transpose(self.quantity)) |
---|
| 81 | self.interpolated_quantity = {} |
---|
| 82 | for i,time_slice in enumerate(self.time): |
---|
| 83 | self.interpolated_quantity[str(time_slice)] = self.interpolated_quantity_raw[:,i] |
---|
[852] | 84 | |
---|
[1396] | 85 | |
---|
[1632] | 86 | def read_sww(self, file_name, quantity_name): |
---|
[852] | 87 | """ |
---|
| 88 | Read in an sww file. |
---|
| 89 | |
---|
| 90 | Input; |
---|
| 91 | file_name - the sww file |
---|
| 92 | |
---|
| 93 | Output; |
---|
| 94 | x - Vector of x values |
---|
| 95 | y - Vector of y values |
---|
| 96 | z - Vector of bed elevation |
---|
| 97 | volumes - Array. Each row has 3 values, representing |
---|
| 98 | the vertices that define the volume |
---|
| 99 | time - Vector of the times where there is stage information |
---|
| 100 | stage - array with respect to time and vertices (x,y) |
---|
| 101 | """ |
---|
| 102 | |
---|
| 103 | #FIXME Have this reader as part of data_manager? |
---|
| 104 | |
---|
| 105 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 106 | |
---|
| 107 | #Check contents |
---|
| 108 | #Get NetCDF |
---|
| 109 | fid = NetCDFFile(file_name, 'r') |
---|
| 110 | |
---|
| 111 | # Get the variables |
---|
| 112 | x = fid.variables['x'][:] |
---|
| 113 | y = fid.variables['y'][:] |
---|
| 114 | volumes = fid.variables['volumes'][:] |
---|
| 115 | time = fid.variables['time'][:] |
---|
| 116 | try: |
---|
[863] | 117 | if quantity_name == 'depth': |
---|
[852] | 118 | z = fid.variables['elevation'][:] |
---|
| 119 | stage = fid.variables['stage'][:,:] |
---|
[902] | 120 | quantity = stage - z # 2D, using broadcasting |
---|
| 121 | #print quantity |
---|
[852] | 122 | else: |
---|
| 123 | quantity = fid.variables[quantity_name][:,:] # 2D |
---|
| 124 | except (KeyError, IndexError),e: |
---|
| 125 | fid.close() |
---|
| 126 | raise KeyError |
---|
| 127 | |
---|
| 128 | fid.close() |
---|
| 129 | return x, y, volumes, time, quantity |
---|
| 130 | |
---|
| 131 | def write_depth_xya(self,point_file, delimiter = ','): |
---|
| 132 | """ |
---|
| 133 | pre condition: |
---|
| 134 | point attributes have been determined |
---|
| 135 | (interpolate_xya has been called) |
---|
| 136 | The time list is defined |
---|
| 137 | """ |
---|
[1632] | 138 | from load_mesh.loadASCII import export_points_file |
---|
[852] | 139 | |
---|
| 140 | xya_dict = {} |
---|
| 141 | xya_dict['pointlist'] = self.point_coordinates |
---|
[1396] | 142 | xya_dict['attributelist'] = self.interpolated_quantity |
---|
| 143 | export_points_file(point_file, xya_dict) |
---|
[852] | 144 | |
---|
[1632] | 145 | |
---|
[852] | 146 | #------------------------------------------------------------- |
---|
| 147 | if __name__ == "__main__": |
---|
| 148 | """ |
---|
| 149 | Load in an sww file and an xya file and return an xya file |
---|
| 150 | """ |
---|
| 151 | import os, sys |
---|
[863] | 152 | usage = "usage: %s pyvolution_results.sww points.xya depth.xya [depth|stage|(other quantities)]" % os.path.basename(sys.argv[0]) |
---|
[852] | 153 | if len(sys.argv) < 4: |
---|
| 154 | print usage |
---|
| 155 | else: |
---|
| 156 | sww_file = sys.argv[1] |
---|
| 157 | point_file_in = sys.argv[2] |
---|
| 158 | point_file_out = sys.argv[3] |
---|
| 159 | if len(sys.argv) == 5: |
---|
| 160 | quantity_name = sys.argv[4] |
---|
| 161 | else: |
---|
| 162 | quantity_name = DEFAULT_QUANTITY |
---|
| 163 | #print "quantity",quantity |
---|
| 164 | try: |
---|
| 165 | interp = Interpolate_sww(sww_file, quantity_name) |
---|
| 166 | except KeyError: |
---|
| 167 | print "Error: Unknown quantity" |
---|
| 168 | sys.exit(1) |
---|
| 169 | |
---|
| 170 | interp.interpolate_xya(point_file_in) |
---|
| 171 | interp.write_depth_xya(point_file_out) |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|