[2386] | 1 | """ |
---|
| 2 | Created: 11/01/2006 |
---|
| 3 | Author: John Jakeman |
---|
| 4 | |
---|
| 5 | Program Visualises .sww files |
---|
| 6 | .sww: Netcdf format for storing model output f(t,x,y) |
---|
| 7 | """ |
---|
| 8 | import time as systime |
---|
| 9 | def update_viewer(time_interp, yeildstep, t): |
---|
| 10 | stage = interpolated_quantity(fid.variables['stage'][:],time_interp) |
---|
| 11 | domain.set_quantity('stage',stage) |
---|
| 12 | |
---|
| 13 | ratio = time_interp[1] |
---|
| 14 | domain.set_time(t+ratio*yieldstep) |
---|
| 15 | |
---|
| 16 | #domain.visualiser.update_quantity('stage') |
---|
| 17 | #domain.visualiser.update_timer() |
---|
| 18 | if domain.visualise is True: |
---|
| 19 | domain.visualiser.redraw_ready.set() |
---|
| 20 | domain.visualiser.idle.wait() |
---|
| 21 | domain.visualiser.idle.clear() |
---|
| 22 | domain.visualiser.unpaused.wait() |
---|
| 23 | |
---|
| 24 | print 'time = ',domain.write_time() |
---|
| 25 | #systime.sleep(0.5) |
---|
| 26 | |
---|
| 27 | import project |
---|
| 28 | from pyvolution.data_manager import get_time_interp, interpolated_quantity |
---|
| 29 | from caching import cache |
---|
| 30 | filename = project.filename[:-3] + '.sww' #filename is of form filename.py |
---|
| 31 | #filename = 'run_new_meribula.sww' |
---|
| 32 | #filename = 'results29_01_2006.sww' |
---|
| 33 | filename = 'cairns250m.sww' |
---|
| 34 | verbose = True |
---|
| 35 | very_verbose = False |
---|
| 36 | boundary = None |
---|
| 37 | t = 0.0 |
---|
| 38 | fail_if_NaN=False |
---|
| 39 | NaN_filler=0 |
---|
| 40 | |
---|
| 41 | """ |
---|
| 42 | The following is borrowed from data_manager.sww2domain |
---|
| 43 | """ |
---|
| 44 | NaN=9.969209968386869e+036 |
---|
| 45 | #initialise NaN. |
---|
| 46 | |
---|
| 47 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 48 | from pyvolution.shallow_water_vtk import Domain |
---|
| 49 | from Numeric import asarray, transpose, resize |
---|
| 50 | |
---|
| 51 | if verbose: print 'Reading from ', filename |
---|
| 52 | fid = NetCDFFile(filename, 'r') #Open existing file for read |
---|
| 53 | time = fid.variables['time'] #Timesteps |
---|
| 54 | |
---|
| 55 | #if t is None: |
---|
| 56 | # t = time[-1] |
---|
| 57 | time_interp = 0, 0.0 |
---|
| 58 | #time_interp = get_time_interp(time,t) |
---|
| 59 | |
---|
| 60 | # Get the variables as Numeric arrays |
---|
| 61 | x = fid.variables['x'][:] #x-coordinates of vertices |
---|
| 62 | y = fid.variables['y'][:] #y-coordinates of vertices |
---|
| 63 | elevation = fid.variables['elevation'] #Elevation |
---|
| 64 | stage = fid.variables['stage'] #Water level |
---|
| 65 | xmomentum = fid.variables['xmomentum'] #Momentum in the x-direction |
---|
| 66 | ymomentum = fid.variables['ymomentum'] #Momentum in the y-direction |
---|
| 67 | |
---|
| 68 | starttime = fid.starttime[0] |
---|
| 69 | volumes = fid.variables['volumes'][:] #Connectivity |
---|
| 70 | coordinates=transpose(asarray([x.tolist(),y.tolist()])) |
---|
| 71 | |
---|
| 72 | conserved_quantities = [] |
---|
| 73 | interpolated_quantities = {} |
---|
| 74 | other_quantities = [] |
---|
| 75 | |
---|
| 76 | # get geo_reference |
---|
| 77 | #sww files don't have to have a geo_ref |
---|
| 78 | try: |
---|
| 79 | geo_reference = Geo_reference(NetCDFObject=fid) |
---|
| 80 | except: #AttributeError, e: |
---|
| 81 | geo_reference = None |
---|
| 82 | |
---|
| 83 | if verbose: print ' getting quantities' |
---|
| 84 | for quantity in fid.variables.keys(): |
---|
| 85 | dimensions = fid.variables[quantity].dimensions |
---|
| 86 | if 'number_of_timesteps' in dimensions: |
---|
| 87 | conserved_quantities.append(quantity) |
---|
| 88 | interpolated_quantities[quantity]=\ |
---|
| 89 | interpolated_quantity(fid.variables[quantity][:],time_interp) |
---|
| 90 | else: |
---|
| 91 | other_quantities.append(quantity) |
---|
| 92 | |
---|
| 93 | other_quantities.remove('x') |
---|
| 94 | other_quantities.remove('y') |
---|
| 95 | other_quantities.remove('z') |
---|
| 96 | other_quantities.remove('volumes') |
---|
| 97 | |
---|
| 98 | conserved_quantities.remove('time') |
---|
| 99 | |
---|
| 100 | if verbose: print ' building domain' |
---|
| 101 | # From domain.Domain: |
---|
| 102 | # domain = Domain(coordinates, volumes,\ |
---|
| 103 | # conserved_quantities = conserved_quantities,\ |
---|
| 104 | # other_quantities = other_quantities,zone=zone,\ |
---|
| 105 | # xllcorner=xllcorner, yllcorner=yllcorner) |
---|
| 106 | |
---|
| 107 | # From shallow_water.Domain: |
---|
| 108 | coordinates=coordinates.tolist() |
---|
| 109 | volumes=volumes.tolist() |
---|
| 110 | #FIXME:should this be in mesh?(peter row) |
---|
| 111 | if fid.smoothing == 'Yes': unique = False |
---|
| 112 | else: unique = True |
---|
| 113 | if unique: |
---|
| 114 | coordinates,volumes,boundary=weed(coordinates,volumes,boundary) |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | domain = cache(Domain, (coordinates, volumes, boundary)) |
---|
| 118 | |
---|
| 119 | if not boundary is None: |
---|
| 120 | domain.boundary = boundary |
---|
| 121 | |
---|
| 122 | domain.geo_reference = geo_reference |
---|
| 123 | |
---|
| 124 | domain.starttime=float(starttime)+float(t) |
---|
| 125 | domain.time=0.0 |
---|
| 126 | |
---|
| 127 | for quantity in other_quantities: |
---|
| 128 | try: |
---|
| 129 | NaN = fid.variables[quantity].missing_value |
---|
| 130 | except: |
---|
| 131 | pass #quantity has no missing_value number |
---|
| 132 | X = fid.variables[quantity][:] |
---|
| 133 | if very_verbose: |
---|
| 134 | print ' ',quantity |
---|
| 135 | print ' NaN =',NaN |
---|
| 136 | print ' max(X)' |
---|
| 137 | print ' ',max(X) |
---|
| 138 | print ' max(X)==NaN' |
---|
| 139 | print ' ',max(X)==NaN |
---|
| 140 | print '' |
---|
| 141 | if (max(X)==NaN) or (min(X)==NaN): |
---|
| 142 | if fail_if_NaN: |
---|
| 143 | msg = 'quantity "%s" contains no_data entry'%quantity |
---|
| 144 | raise msg |
---|
| 145 | else: |
---|
| 146 | data = (X<>NaN) |
---|
| 147 | X = (X*data)+(data==0)*NaN_filler |
---|
| 148 | if unique: |
---|
| 149 | X = resize(X,(len(X)/3,3)) |
---|
| 150 | domain.set_quantity(quantity,X) |
---|
| 151 | # |
---|
| 152 | for quantity in conserved_quantities: |
---|
| 153 | try: |
---|
| 154 | NaN = fid.variables[quantity].missing_value |
---|
| 155 | except: |
---|
| 156 | pass #quantity has no missing_value number |
---|
| 157 | X = interpolated_quantities[quantity] |
---|
| 158 | if very_verbose: |
---|
| 159 | print ' ',quantity |
---|
| 160 | print ' NaN =',NaN |
---|
| 161 | print ' max(X)' |
---|
| 162 | print ' ',max(X) |
---|
| 163 | print ' max(X)==NaN' |
---|
| 164 | print ' ',max(X)==NaN |
---|
| 165 | print '' |
---|
| 166 | if (max(X)==NaN) or (min(X)==NaN): |
---|
| 167 | if fail_if_NaN: |
---|
| 168 | msg = 'quantity "%s" contains no_data entry'%quantity |
---|
| 169 | raise msg |
---|
| 170 | else: |
---|
| 171 | data = (X<>NaN) |
---|
| 172 | X = (X*data)+(data==0)*NaN_filler |
---|
| 173 | if unique: |
---|
| 174 | X = resize(X,(X.shape[0]/3,3)) |
---|
| 175 | domain.set_quantity(quantity,X) |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | """ |
---|
| 179 | The following initialises visualiser and uses .sww file to update stage |
---|
| 180 | at each timestep |
---|
| 181 | """ |
---|
| 182 | |
---|
| 183 | domain.initialise_visualiser() |
---|
| 184 | #domain.visualiser.setup_all() |
---|
| 185 | domain.visualiser.scale_z['stage'] = 20.0 |
---|
| 186 | domain.visualiser.coloring['stage'] = True |
---|
| 187 | domain.visualiser.scale_z['elevation'] = 20.0 |
---|
| 188 | domain.visualiser.coloring['elevation'] = False |
---|
| 189 | domain.visualiser.updating['elevation'] = True |
---|
| 190 | #domain.visualiser.setup['stage'] = False |
---|
| 191 | #domain.visualiser.updating['stage'] = False |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | domain.visualiser.start() |
---|
| 195 | # Things go haywire if we start evolving before the vis is ready |
---|
| 196 | domain.visualiser.idle.wait() |
---|
| 197 | domain.visualiser.idle.clear() |
---|
| 198 | |
---|
| 199 | index = 0 |
---|
| 200 | ratio = 0.0 |
---|
| 201 | num_subt = 1 # use 1 if only want to display at yieldstep timeinterval |
---|
| 202 | |
---|
| 203 | if len(time) > 1: |
---|
| 204 | yieldstep = time[1]-time[0] |
---|
| 205 | else: |
---|
| 206 | yieldstep = 0.0 |
---|
| 207 | |
---|
| 208 | for t in time[:]: |
---|
| 209 | if t < time[-1]: |
---|
| 210 | if index%10 == 0: |
---|
| 211 | for subt in range(0,num_subt): |
---|
| 212 | ratio = 1-float(num_subt-subt)/float(num_subt) |
---|
| 213 | #print ratio |
---|
| 214 | time_interp = index, ratio |
---|
| 215 | update_viewer(time_interp, yieldstep, t) |
---|
| 216 | else: |
---|
| 217 | time_interp = index, 0.0 |
---|
| 218 | update_viewer(time_interp, yieldstep, t) |
---|
| 219 | |
---|
| 220 | index = index + 1 |
---|
| 221 | |
---|
| 222 | print 'Finished' |
---|
| 223 | fid.close() |
---|
| 224 | |
---|