[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 | |
---|
[1814] | 29 | def interpolate_sww2xya(sww_file, quantity_name,point_file_in, |
---|
| 30 | point_file_out, display_errors = True): |
---|
| 31 | """ |
---|
| 32 | This function catches exceptions. |
---|
| 33 | """ |
---|
| 34 | try: |
---|
| 35 | interp = Interpolate_sww(sww_file, quantity_name) |
---|
| 36 | interp.interpolate_xya(point_file_in) |
---|
| 37 | interp.write_depth_xya(point_file_out) |
---|
| 38 | except IOError,e: #need to convert key error to ioerror |
---|
| 39 | if display_errors: |
---|
| 40 | print "Could not load bad file. ", e |
---|
| 41 | import sys; sys.exit() |
---|
| 42 | |
---|
| 43 | # FIXME (DSG-DSG): how are bad quantities caught? |
---|
| 44 | #try: |
---|
| 45 | # interp = Interpolate_sww(sww_file, quantity_name) |
---|
| 46 | #except KeyError: |
---|
| 47 | # print "Error: Unknown quantity" |
---|
| 48 | # sys.exit(1) |
---|
| 49 | |
---|
| 50 | interp.interpolate_xya(point_file_in) |
---|
| 51 | interp.write_depth_xya(point_file_out) |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
[852] | 55 | class Interpolate_sww: |
---|
[1137] | 56 | def __init__(self, file_name, quantity_name): |
---|
[852] | 57 | |
---|
| 58 | #It's bad to have the quantity_name passed in here. |
---|
| 59 | # But it works with how the program is currently used. |
---|
| 60 | # Refactor when necessary. - DSG |
---|
| 61 | |
---|
| 62 | x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name) |
---|
| 63 | vertex_coordinates = transpose([x,y]) |
---|
| 64 | |
---|
| 65 | if False: |
---|
| 66 | print "****************************" |
---|
| 67 | print "x " |
---|
| 68 | print x |
---|
| 69 | print "****************************" |
---|
| 70 | print "Y " |
---|
| 71 | print y |
---|
| 72 | print "****************************" |
---|
| 73 | print "V " |
---|
| 74 | print volumes |
---|
| 75 | print "****************************" |
---|
| 76 | print "Time " |
---|
| 77 | print time |
---|
| 78 | print "****************************" |
---|
| 79 | print "quantity " |
---|
| 80 | print quantity |
---|
| 81 | print "****************************" |
---|
| 82 | print "vertex_coordinates" |
---|
| 83 | print vertex_coordinates |
---|
| 84 | print "****************************" |
---|
[902] | 85 | |
---|
[852] | 86 | self.interp = Interpolation(vertex_coordinates, volumes, alpha=0) |
---|
| 87 | self.time = time |
---|
| 88 | |
---|
| 89 | self.quantity_name = quantity_name |
---|
| 90 | self.quantity = quantity |
---|
| 91 | |
---|
[1137] | 92 | def interpolate_xya(self, file_name): |
---|
[852] | 93 | """ |
---|
| 94 | Given a point file, interpolate the height w.r.t. time at the points |
---|
| 95 | specified in the point file. |
---|
| 96 | |
---|
| 97 | Input; |
---|
| 98 | file_name - the xya file |
---|
| 99 | """ |
---|
[1396] | 100 | |
---|
| 101 | from load_mesh.loadASCII import import_points_file |
---|
| 102 | |
---|
| 103 | point_dict = import_points_file(file_name) |
---|
| 104 | self.point_coordinates = point_dict['pointlist'] |
---|
[852] | 105 | self.interp.build_interpolation_matrix_A(self.point_coordinates) |
---|
[1396] | 106 | self.interpolated_quantity_raw = self.interp.interpolate(transpose(self.quantity)) |
---|
| 107 | self.interpolated_quantity = {} |
---|
| 108 | for i,time_slice in enumerate(self.time): |
---|
| 109 | self.interpolated_quantity[str(time_slice)] = self.interpolated_quantity_raw[:,i] |
---|
[852] | 110 | |
---|
[1396] | 111 | |
---|
[1632] | 112 | def read_sww(self, file_name, quantity_name): |
---|
[852] | 113 | """ |
---|
| 114 | Read in an sww file. |
---|
| 115 | |
---|
| 116 | Input; |
---|
| 117 | file_name - the sww file |
---|
| 118 | |
---|
| 119 | Output; |
---|
| 120 | x - Vector of x values |
---|
| 121 | y - Vector of y values |
---|
| 122 | z - Vector of bed elevation |
---|
| 123 | volumes - Array. Each row has 3 values, representing |
---|
| 124 | the vertices that define the volume |
---|
| 125 | time - Vector of the times where there is stage information |
---|
| 126 | stage - array with respect to time and vertices (x,y) |
---|
| 127 | """ |
---|
| 128 | |
---|
| 129 | #FIXME Have this reader as part of data_manager? |
---|
| 130 | |
---|
[1814] | 131 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 132 | import tempfile |
---|
| 133 | import sys |
---|
| 134 | import os |
---|
[852] | 135 | |
---|
| 136 | #Check contents |
---|
| 137 | #Get NetCDF |
---|
[1814] | 138 | |
---|
| 139 | # see if the file is there. Throw a QUIET IO error if it isn't |
---|
| 140 | fd = open(file_name,'r') |
---|
| 141 | fd.close() |
---|
| 142 | |
---|
| 143 | #throws prints to screen if file not present |
---|
| 144 | |
---|
| 145 | junk = tempfile.mktemp(".txt") |
---|
| 146 | fd = open(junk,'w') |
---|
| 147 | stdout = sys.stdout |
---|
| 148 | sys.stdout = fd |
---|
[852] | 149 | fid = NetCDFFile(file_name, 'r') |
---|
[1814] | 150 | sys.stdout = stdout |
---|
| 151 | fd.close() |
---|
| 152 | #clean up |
---|
| 153 | os.remove(junk) |
---|
[852] | 154 | |
---|
| 155 | # Get the variables |
---|
| 156 | x = fid.variables['x'][:] |
---|
| 157 | y = fid.variables['y'][:] |
---|
| 158 | volumes = fid.variables['volumes'][:] |
---|
| 159 | time = fid.variables['time'][:] |
---|
| 160 | try: |
---|
[863] | 161 | if quantity_name == 'depth': |
---|
[852] | 162 | z = fid.variables['elevation'][:] |
---|
| 163 | stage = fid.variables['stage'][:,:] |
---|
[902] | 164 | quantity = stage - z # 2D, using broadcasting |
---|
| 165 | #print quantity |
---|
[852] | 166 | else: |
---|
| 167 | quantity = fid.variables[quantity_name][:,:] # 2D |
---|
| 168 | except (KeyError, IndexError),e: |
---|
| 169 | fid.close() |
---|
| 170 | raise KeyError |
---|
| 171 | |
---|
| 172 | fid.close() |
---|
| 173 | return x, y, volumes, time, quantity |
---|
| 174 | |
---|
| 175 | def write_depth_xya(self,point_file, delimiter = ','): |
---|
| 176 | """ |
---|
| 177 | pre condition: |
---|
| 178 | point attributes have been determined |
---|
| 179 | (interpolate_xya has been called) |
---|
| 180 | The time list is defined |
---|
| 181 | """ |
---|
[1632] | 182 | from load_mesh.loadASCII import export_points_file |
---|
[852] | 183 | |
---|
| 184 | xya_dict = {} |
---|
| 185 | xya_dict['pointlist'] = self.point_coordinates |
---|
[1396] | 186 | xya_dict['attributelist'] = self.interpolated_quantity |
---|
| 187 | export_points_file(point_file, xya_dict) |
---|
[852] | 188 | |
---|
[1632] | 189 | |
---|
[852] | 190 | #------------------------------------------------------------- |
---|
| 191 | |
---|
| 192 | |
---|
[2432] | 193 | if __name__ == "__main__": |
---|
| 194 | """ |
---|
| 195 | Load in an sww file and an xya file and return an xya file |
---|
| 196 | """ |
---|
| 197 | import os, sys |
---|
| 198 | usage = "usage: %s pyvolution_results.sww points.xya depth.xya [depth|stage|(other quantities)]" % os.path.basename(sys.argv[0]) |
---|
| 199 | if len(sys.argv) < 4: |
---|
| 200 | print usage |
---|
| 201 | else: |
---|
| 202 | sww_file = sys.argv[1] |
---|
| 203 | point_file_in = sys.argv[2] |
---|
| 204 | point_file_out = sys.argv[3] |
---|
| 205 | if len(sys.argv) == 5: |
---|
| 206 | quantity_name = sys.argv[4] |
---|
| 207 | else: |
---|
| 208 | quantity_name = DEFAULT_QUANTITY |
---|
| 209 | #print "quantity",quantity |
---|
| 210 | interpolate_sww2xya(sww_file, quantity_name,point_file_in, |
---|
| 211 | point_file_out) |
---|
[852] | 212 | |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | |
---|