Changeset 851 for inundation/ga/storm_surge/pyvolution/util.py
- Timestamp:
- Feb 8, 2005, 7:07:56 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/util.py
r850 r851 123 123 quantities = ['stage', 'xmomentum', 'ymomentum'] 124 124 125 interpolation_points decides at which points interpolated quantities are to be computed 125 interpolation_points decides at which points interpolated 126 quantities are to be computed whenever object is called. 127 128 129 126 130 If None, return average value 127 131 """ … … 133 137 will be checked and possibly modified 134 138 135 136 137 139 All times are assumed to be in UTC 138 140 """ … … 213 215 msg = 'File %s must list time as a monotonuosly ' %self.filename 214 216 msg += 'increasing sequence' 215 assert alltrue( 217 assert alltrue(time[1:] - time[:-1] > 0 ), msg 216 218 217 219 … … 227 229 228 230 ####self.Q = zeros( (len(time), 229 230 for i, t in enumerate(time[:]): 231 self.T = time[:] 232 self.index = 0 #Initial index 233 234 self.values = {} 235 for name in self.quantities: 236 self.values[name] = zeros( (len(self.T), 237 len(interpolation_points)), 238 Float) 239 240 for i, t in enumerate(self.T): 231 241 232 242 #Interpolate quantities at this timestep … … 238 248 verbose = False) 239 249 240 for name in self.quantities: 241 print t, name, interpol.interpolate(fid.variables[name][i,:]) 250 for name in self.quantities: 251 self.values[name][i, :] =\ 252 interpol.interpolate(fid.variables[name][i,:]) 242 253 243 254 else: 244 255 pass 245 256 246 self.T = time[:] 247 248 return 249 250 fid = open(self.filename) 251 lines = fid.readlines() 252 fid.close() 253 254 N = len(lines) 255 d = self.number_of_values 256 257 T = zeros(N, Float) #Time 258 Q = zeros((N, d), Float) #Values 259 260 for i, line in enumerate(lines): 261 fields = line.split(',') 262 realtime = calendar.timegm(time.strptime(fields[0], time_format)) 263 264 T[i] = realtime - self.starttime 265 266 for j, value in enumerate(fields[1].split()): 267 Q[i, j] = float(value) 268 269 270 271 self.T = T #Time 272 self.Q = Q #Values 273 self.index = 0 #Initial index 274 275 257 def __repr__(self): 258 return 'File function' 259 260 def __call__(self, t, x=None, y=None, point_id = None): 261 """Evaluate f(t, point_id) 262 263 Inputs: 264 t: time - Model time (tau = domain.starttime-self.starttime+t) 265 must lie within existing timesteps 266 point_id: index of one of the preprocessed points. 267 If point_id is None all preprocessed points are computed 268 269 FIXME: point_id could also be a slice 270 FIXME: One could allow arbitrary x, y coordinates 271 (requires computation of a new interpolator) 272 FIXME: What if x and y are vectors? 273 """ 274 275 from math import pi, cos, sin, sqrt 276 from Numeric import zeros, Float 277 278 #Find time tau such that 279 # 280 # domain.starttime + t == self.starttime + tau 281 282 if self.domain is not None: 283 tau = self.domain.starttime-self.starttime+t 284 else: 285 tau = t 286 287 msg = 'Time interval derived from file %s (%s:%s) does not match model time: %s'\ 288 %(self.filename, self.T[0], self.T[1], tau) 289 if tau < self.T[0]: raise msg 290 if tau > self.T[-1]: raise msg 291 292 oldindex = self.index #Time index 293 294 #Find time slot 295 while tau > self.T[self.index]: self.index += 1 296 while tau < self.T[self.index]: self.index -= 1 297 298 #t is now between index and index+1 299 ratio = (tau - self.T[self.index])/\ 300 (self.T[self.index+1] - self.T[self.index]) 301 302 303 #Compute interpolated values 304 q = zeros( len(self.quantities), Float) 305 306 for i, name in enumerate(self.quantities): 307 Q = self.values[name] 308 309 q[i] = Q[self.index, point_id] +\ 310 ratio*(Q[self.index+1, point_id] - Q[self.index, point_id]) 311 312 313 #Return vector of interpolated values 314 return q 315 276 316 277 317 class File_function_ASCII: … … 430 470 return 'File function' 431 471 432 433 434 def __call__(self, t, x=None, y=None): 472 def __call__(self, t, x=None, y=None, point_id=None): 435 473 """Evaluate f(t,x,y) 436 474 … … 438 476 result is a vector of same length as x and y 439 477 FIXME: Naaaa 478 479 FIXME: Rethink semantics when x,y are vectors. 440 480 """ 441 481
Note: See TracChangeset
for help on using the changeset viewer.