[1093] | 1 | """This module contains various auxiliary function used by pyvolution. |
---|
[258] | 2 | |
---|
[1093] | 3 | It is also a clearing house for functions that may later earn a module |
---|
[886] | 4 | of their own. |
---|
[826] | 5 | """ |
---|
[258] | 6 | |
---|
| 7 | def angle(v): |
---|
| 8 | """Compute angle between e1 (the unit vector in the x-direction) |
---|
| 9 | and the specified vector |
---|
| 10 | """ |
---|
| 11 | |
---|
| 12 | from math import acos, pi, sqrt |
---|
| 13 | from Numeric import sum, array |
---|
| 14 | |
---|
| 15 | l = sqrt( sum (array(v)**2)) |
---|
| 16 | v1 = v[0]/l |
---|
| 17 | v2 = v[1]/l |
---|
| 18 | |
---|
[1053] | 19 | try: |
---|
| 20 | theta = acos(v1) |
---|
| 21 | except ValueError, e: |
---|
[1093] | 22 | print 'WARNING (util.py): Angle acos(%s) failed: %s' %(str(v1), e) |
---|
[1053] | 23 | |
---|
[1062] | 24 | #FIXME, hack to avoid acos(1.0) Value error |
---|
| 25 | # why is it happening? |
---|
| 26 | # is it catching something we should avoid? |
---|
| 27 | s = 1e-6 |
---|
| 28 | if (v1+s > 1.0) and (v1-s < 1.0) : |
---|
| 29 | theta = 0.0 |
---|
| 30 | elif (v1+s > -1.0) and (v1-s < -1.0): |
---|
| 31 | theta = 3.1415926535897931 |
---|
[1093] | 32 | print 'WARNING (util.py): angle v1 is %f, setting acos to %f '\ |
---|
| 33 | %(v1, theta) |
---|
| 34 | |
---|
[258] | 35 | if v2 < 0: |
---|
| 36 | #Quadrant 3 or 4 |
---|
| 37 | theta = 2*pi-theta |
---|
| 38 | |
---|
| 39 | return theta |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | def anglediff(v0, v1): |
---|
| 43 | """Compute difference between angle of vector x0, y0 and x1, y1. |
---|
| 44 | This is used for determining the ordering of vertices, |
---|
| 45 | e.g. for checking if they are counter clockwise. |
---|
| 46 | |
---|
| 47 | Always return a positive value |
---|
| 48 | """ |
---|
| 49 | |
---|
| 50 | from math import pi |
---|
| 51 | |
---|
| 52 | a0 = angle(v0) |
---|
| 53 | a1 = angle(v1) |
---|
| 54 | |
---|
| 55 | #Ensure that difference will be positive |
---|
| 56 | if a0 < a1: |
---|
| 57 | a0 += 2*pi |
---|
| 58 | |
---|
| 59 | return a0-a1 |
---|
| 60 | |
---|
| 61 | |
---|
[274] | 62 | def mean(x): |
---|
| 63 | from Numeric import sum |
---|
| 64 | return sum(x)/len(x) |
---|
[258] | 65 | |
---|
[623] | 66 | |
---|
[671] | 67 | def point_on_line(x, y, x0, y0, x1, y1): |
---|
[655] | 68 | """Determine whether a point is on a line segment |
---|
| 69 | |
---|
[671] | 70 | Input: x, y, x0, x0, x1, y1: where |
---|
| 71 | point is given by x, y |
---|
| 72 | line is given by (x0, y0) and (x1, y1) |
---|
[655] | 73 | |
---|
| 74 | """ |
---|
| 75 | |
---|
| 76 | from Numeric import array, dot, allclose |
---|
| 77 | from math import sqrt |
---|
| 78 | |
---|
[659] | 79 | a = array([x - x0, y - y0]) |
---|
| 80 | a_normal = array([a[1], -a[0]]) |
---|
| 81 | |
---|
[655] | 82 | b = array([x1 - x0, y1 - y0]) |
---|
| 83 | |
---|
[659] | 84 | if dot(a_normal, b) == 0: |
---|
| 85 | #Point is somewhere on the infinite extension of the line |
---|
| 86 | |
---|
| 87 | len_a = sqrt(sum(a**2)) |
---|
| 88 | len_b = sqrt(sum(b**2)) |
---|
| 89 | if dot(a, b) >= 0 and len_a <= len_b: |
---|
| 90 | return True |
---|
| 91 | else: |
---|
| 92 | return False |
---|
[655] | 93 | else: |
---|
[659] | 94 | return False |
---|
| 95 | |
---|
[1137] | 96 | def ensure_numeric(A, typecode = None): |
---|
| 97 | """Ensure that sequence is a Numeric array. |
---|
| 98 | Inputs: |
---|
| 99 | A: Sequance. If A is already a Numeric array it will be returned |
---|
| 100 | unaltered |
---|
| 101 | If not, an attempt is made to convert it to a Numeric |
---|
| 102 | array |
---|
| 103 | typecode: Numeric type. If specified, use this in the conversion. |
---|
| 104 | If not, let Numeric decide |
---|
[849] | 105 | |
---|
[1137] | 106 | This function is necessary as array(A) can cause memory overflow. |
---|
| 107 | """ |
---|
| 108 | |
---|
| 109 | from Numeric import ArrayType, array |
---|
| 110 | |
---|
| 111 | if typecode is None: |
---|
| 112 | if type(A) == ArrayType: |
---|
| 113 | return A |
---|
| 114 | else: |
---|
| 115 | return array(A) |
---|
| 116 | else: |
---|
| 117 | if type(A) == ArrayType: |
---|
| 118 | if A.typecode == typecode: |
---|
| 119 | return array(A) |
---|
| 120 | else: |
---|
| 121 | return A.astype(typecode) |
---|
| 122 | else: |
---|
| 123 | return array(A).astype(typecode) |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
[900] | 127 | def file_function(filename, domain=None, quantities = None, interpolation_points = None, verbose = False): |
---|
[858] | 128 | """If domain is specified, don't specify quantites as they are automatically derived |
---|
| 129 | """ |
---|
[957] | 130 | |
---|
| 131 | |
---|
| 132 | #FIXME (OLE): Should check origin of domain against that of file |
---|
[999] | 133 | #In fact, this is where origin should be converted to that of domain |
---|
| 134 | #Also, check that file covers domain fully. |
---|
[957] | 135 | |
---|
[849] | 136 | assert type(filename) == type(''),\ |
---|
| 137 | 'First argument to File_function must be a string' |
---|
| 138 | |
---|
| 139 | try: |
---|
| 140 | fid = open(filename) |
---|
| 141 | except Exception, e: |
---|
| 142 | msg = 'File "%s" could not be opened: Error="%s"'\ |
---|
| 143 | %(filename, e) |
---|
| 144 | raise msg |
---|
| 145 | |
---|
| 146 | line = fid.readline() |
---|
| 147 | fid.close() |
---|
| 148 | |
---|
[850] | 149 | if domain is not None: |
---|
| 150 | quantities = domain.conserved_quantities |
---|
| 151 | else: |
---|
| 152 | quantities = None |
---|
| 153 | |
---|
[849] | 154 | #Choose format |
---|
| 155 | #FIXME: Maybe these can be merged later on |
---|
| 156 | if line[:3] == 'CDF': |
---|
[900] | 157 | return File_function_NetCDF(filename, domain, quantities, interpolation_points, verbose = verbose) |
---|
[849] | 158 | else: |
---|
[850] | 159 | return File_function_ASCII(filename, domain, quantities, interpolation_points) |
---|
[849] | 160 | |
---|
| 161 | |
---|
| 162 | class File_function_NetCDF: |
---|
| 163 | """Read time history of spatial data from NetCDF sww file and |
---|
| 164 | return a callable object f(t,x,y) |
---|
| 165 | which will return interpolated values based on the input file. |
---|
| 166 | |
---|
| 167 | x, y may be either scalars or vectors |
---|
| 168 | |
---|
| 169 | #FIXME: More about format, interpolation and order of quantities |
---|
[850] | 170 | |
---|
| 171 | The quantities returned by the callable objects are specified by |
---|
| 172 | the list quantities which must contain the names of the |
---|
| 173 | quantities to be returned and also reflect the order, e.g. for |
---|
| 174 | the shallow water wave equation, on would have |
---|
| 175 | quantities = ['stage', 'xmomentum', 'ymomentum'] |
---|
| 176 | |
---|
[851] | 177 | interpolation_points decides at which points interpolated |
---|
| 178 | quantities are to be computed whenever object is called. |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
[850] | 182 | If None, return average value |
---|
[849] | 183 | """ |
---|
[850] | 184 | |
---|
[955] | 185 | def __init__(self, filename, domain=None, quantities=None, |
---|
| 186 | interpolation_points=None, verbose = False): |
---|
[849] | 187 | """Initialise callable object from a file. |
---|
| 188 | |
---|
| 189 | If domain is specified, model time (domain.starttime) |
---|
| 190 | will be checked and possibly modified |
---|
[850] | 191 | |
---|
[849] | 192 | All times are assumed to be in UTC |
---|
| 193 | """ |
---|
| 194 | |
---|
[900] | 195 | #FIXME: Check that model origin is the same as file's origin |
---|
| 196 | #(both in UTM coordinates) |
---|
| 197 | #If not - modify those from file to match domain |
---|
| 198 | |
---|
[849] | 199 | import time, calendar |
---|
| 200 | from config import time_format |
---|
| 201 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 202 | |
---|
| 203 | #Open NetCDF file |
---|
[900] | 204 | if verbose: print 'Reading', filename |
---|
[849] | 205 | fid = NetCDFFile(filename, 'r') |
---|
| 206 | |
---|
[850] | 207 | if quantities is None or len(quantities) < 1: |
---|
| 208 | msg = 'ERROR: File must contain at least one independent value' |
---|
| 209 | raise msg |
---|
| 210 | |
---|
| 211 | missing = [] |
---|
| 212 | for quantity in ['x', 'y', 'time'] + quantities: |
---|
| 213 | if not fid.variables.has_key(quantity): |
---|
| 214 | missing.append(quantity) |
---|
| 215 | |
---|
| 216 | if len(missing) > 0: |
---|
| 217 | msg = 'Quantities %s could not be found in file %s'\ |
---|
| 218 | %(str(missing), filename) |
---|
| 219 | raise msg |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | #Get first timestep |
---|
[849] | 223 | try: |
---|
[955] | 224 | self.starttime = fid.starttime[0] |
---|
[849] | 225 | except ValueError: |
---|
[850] | 226 | msg = 'Could not read starttime from file %s' %filename |
---|
[849] | 227 | raise msg |
---|
| 228 | |
---|
[955] | 229 | #Get origin |
---|
| 230 | self.xllcorner = fid.xllcorner[0] |
---|
| 231 | self.yllcorner = fid.yllcorner[0] |
---|
[850] | 232 | |
---|
| 233 | self.number_of_values = len(quantities) |
---|
| 234 | self.fid = fid |
---|
[849] | 235 | self.filename = filename |
---|
[850] | 236 | self.quantities = quantities |
---|
[849] | 237 | self.domain = domain |
---|
[862] | 238 | self.interpolation_points = interpolation_points |
---|
[849] | 239 | |
---|
| 240 | if domain is not None: |
---|
[1102] | 241 | msg = 'WARNING: Start time as specified in domain (%f)'\ |
---|
[850] | 242 | %domain.starttime |
---|
[1102] | 243 | msg += ' is earlier than the starttime of file %s (%f).'\ |
---|
[850] | 244 | %(self.filename, self.starttime) |
---|
[1102] | 245 | msg += ' Modifying domain starttime accordingly.' |
---|
[850] | 246 | if self.starttime > domain.starttime: |
---|
[1102] | 247 | if verbose: print msg |
---|
[850] | 248 | domain.starttime = self.starttime #Modifying model time |
---|
[1102] | 249 | if verbose: print 'Domain starttime is now set to %f' %domain.starttime |
---|
[849] | 250 | |
---|
[850] | 251 | #Read all data in and produce values for desired data points at each timestep |
---|
[900] | 252 | self.spatial_interpolation(interpolation_points, verbose = verbose) |
---|
[858] | 253 | fid.close() |
---|
[850] | 254 | |
---|
[900] | 255 | def spatial_interpolation(self, interpolation_points, verbose = False): |
---|
[850] | 256 | """For each timestep in fid: read surface, interpolate to desired points |
---|
| 257 | and store values for use when object is called. |
---|
| 258 | """ |
---|
| 259 | |
---|
[880] | 260 | from Numeric import array, zeros, Float, alltrue, concatenate, reshape |
---|
[850] | 261 | |
---|
| 262 | from config import time_format |
---|
| 263 | from least_squares import Interpolation |
---|
| 264 | import time, calendar |
---|
| 265 | |
---|
| 266 | fid = self.fid |
---|
| 267 | |
---|
| 268 | #Get variables |
---|
[1102] | 269 | if verbose: print 'Get variables' |
---|
[880] | 270 | x = fid.variables['x'][:] |
---|
| 271 | y = fid.variables['y'][:] |
---|
| 272 | z = fid.variables['elevation'][:] |
---|
| 273 | triangles = fid.variables['volumes'][:] |
---|
| 274 | time = fid.variables['time'][:] |
---|
[850] | 275 | |
---|
| 276 | #Check |
---|
| 277 | msg = 'File %s must list time as a monotonuosly ' %self.filename |
---|
| 278 | msg += 'increasing sequence' |
---|
[851] | 279 | assert alltrue(time[1:] - time[:-1] > 0 ), msg |
---|
[850] | 280 | |
---|
| 281 | |
---|
| 282 | if interpolation_points is not None: |
---|
| 283 | |
---|
| 284 | try: |
---|
[1137] | 285 | P = ensure_numeric(interpolation_points) |
---|
[850] | 286 | except: |
---|
| 287 | msg = 'Interpolation points must be an N x 2 Numeric array or a list of points\n' |
---|
| 288 | msg += 'I got: %s.' %( str(interpolation_points)[:60] + '...') |
---|
| 289 | raise msg |
---|
| 290 | |
---|
| 291 | |
---|
[955] | 292 | self.T = time[:] #Time assumed to be relative to starttime |
---|
| 293 | self.index = 0 #Initial time index |
---|
[851] | 294 | |
---|
| 295 | self.values = {} |
---|
| 296 | for name in self.quantities: |
---|
| 297 | self.values[name] = zeros( (len(self.T), |
---|
| 298 | len(interpolation_points)), |
---|
| 299 | Float) |
---|
[880] | 300 | |
---|
| 301 | #Build interpolator |
---|
[900] | 302 | if verbose: print 'Build interpolation matrix' |
---|
[880] | 303 | x = reshape(x, (len(x),1)) |
---|
| 304 | y = reshape(y, (len(y),1)) |
---|
| 305 | vertex_coordinates = concatenate((x,y), axis=1) #m x 2 array |
---|
[886] | 306 | |
---|
[880] | 307 | interpol = Interpolation(vertex_coordinates, |
---|
| 308 | triangles, |
---|
| 309 | point_coordinates = P, |
---|
| 310 | alpha = 0, |
---|
[900] | 311 | verbose = verbose) |
---|
[880] | 312 | |
---|
[900] | 313 | |
---|
| 314 | if verbose: print 'Interpolate' |
---|
[851] | 315 | for i, t in enumerate(self.T): |
---|
[880] | 316 | #Interpolate quantities at this timestep |
---|
[901] | 317 | #print ' time step %d of %d' %(i, len(self.T)) |
---|
[851] | 318 | for name in self.quantities: |
---|
| 319 | self.values[name][i, :] =\ |
---|
| 320 | interpol.interpolate(fid.variables[name][i,:]) |
---|
[850] | 321 | |
---|
[955] | 322 | #Report |
---|
| 323 | if verbose: |
---|
| 324 | print '------------------------------------------------' |
---|
| 325 | print 'File_function statistics:' |
---|
| 326 | print ' Name: %s' %self.filename |
---|
| 327 | print ' Reference:' |
---|
| 328 | print ' Lower left corner: [%f, %f]'\ |
---|
| 329 | %(self.xllcorner, self.yllcorner) |
---|
[1102] | 330 | print ' Start time (file): %f' %self.starttime |
---|
| 331 | #print ' Start time (domain): %f' %domain.starttime |
---|
[955] | 332 | print ' Extent:' |
---|
| 333 | print ' x in [%f, %f], len(x) == %d'\ |
---|
| 334 | %(min(x.flat), max(x.flat), len(x.flat)) |
---|
| 335 | print ' y in [%f, %f], len(y) == %d'\ |
---|
| 336 | %(min(y.flat), max(y.flat), len(y.flat)) |
---|
| 337 | print ' t in [%f, %f], len(t) == %d'\ |
---|
| 338 | %(min(self.T), max(self.T), len(self.T)) |
---|
| 339 | print ' Quantities:' |
---|
| 340 | for name in self.quantities: |
---|
| 341 | q = fid.variables[name][:].flat |
---|
| 342 | print ' %s in [%f, %f]' %(name, min(q), max(q)) |
---|
| 343 | |
---|
| 344 | |
---|
[994] | 345 | print ' Interpolation points (xi, eta):'\ |
---|
| 346 | 'number of points == %d ' %P.shape[0] |
---|
| 347 | print ' xi in [%f, %f]' %(min(P[:,0]), max(P[:,0])) |
---|
| 348 | print ' eta in [%f, %f]' %(min(P[:,1]), max(P[:,1])) |
---|
[1043] | 349 | print ' Interpolated quantities (over all timesteps):' |
---|
[955] | 350 | for name in self.quantities: |
---|
| 351 | q = self.values[name][:].flat |
---|
| 352 | print ' %s at interpolation points in [%f, %f]'\ |
---|
| 353 | %(name, min(q), max(q)) |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | |
---|
| 357 | |
---|
| 358 | print '------------------------------------------------' |
---|
[849] | 359 | else: |
---|
[955] | 360 | msg = 'File_function_NetCDF must be invoked with ' |
---|
| 361 | msg += 'a list of interpolation points' |
---|
| 362 | raise msg |
---|
[849] | 363 | |
---|
[851] | 364 | def __repr__(self): |
---|
| 365 | return 'File function' |
---|
| 366 | |
---|
| 367 | def __call__(self, t, x=None, y=None, point_id = None): |
---|
| 368 | """Evaluate f(t, point_id) |
---|
[850] | 369 | |
---|
[851] | 370 | Inputs: |
---|
| 371 | t: time - Model time (tau = domain.starttime-self.starttime+t) |
---|
| 372 | must lie within existing timesteps |
---|
| 373 | point_id: index of one of the preprocessed points. |
---|
| 374 | If point_id is None all preprocessed points are computed |
---|
| 375 | |
---|
| 376 | FIXME: point_id could also be a slice |
---|
| 377 | FIXME: One could allow arbitrary x, y coordinates |
---|
[955] | 378 | (requires computation of a new interpolator) |
---|
| 379 | Maybe not,.,. |
---|
[851] | 380 | FIXME: What if x and y are vectors? |
---|
| 381 | """ |
---|
| 382 | |
---|
| 383 | from math import pi, cos, sin, sqrt |
---|
| 384 | from Numeric import zeros, Float |
---|
| 385 | |
---|
[955] | 386 | |
---|
| 387 | if point_id is None: |
---|
| 388 | msg = 'NetCDF File function needs a point_id when invoked' |
---|
| 389 | raise msg |
---|
| 390 | |
---|
| 391 | |
---|
[851] | 392 | #Find time tau such that |
---|
| 393 | # |
---|
| 394 | # domain.starttime + t == self.starttime + tau |
---|
[850] | 395 | |
---|
[851] | 396 | if self.domain is not None: |
---|
| 397 | tau = self.domain.starttime-self.starttime+t |
---|
| 398 | else: |
---|
[1102] | 399 | #print 'DOMAIN IS NONE!!!!!!!!!' |
---|
[851] | 400 | tau = t |
---|
[955] | 401 | |
---|
| 402 | #print 'D start', self.domain.starttime |
---|
| 403 | #print 'S start', self.starttime |
---|
| 404 | #print 't', t |
---|
| 405 | #print 'tau', tau |
---|
[851] | 406 | |
---|
[955] | 407 | msg = 'Time interval derived from file %s [%s:%s]'\ |
---|
| 408 | %(self.filename, self.T[0], self.T[1]) |
---|
[1102] | 409 | msg += ' does not match model time: %s\n' %tau |
---|
| 410 | msg += 'Domain says its starttime == %f' %(self.domain.starttime) |
---|
[955] | 411 | |
---|
[851] | 412 | if tau < self.T[0]: raise msg |
---|
| 413 | if tau > self.T[-1]: raise msg |
---|
[850] | 414 | |
---|
[858] | 415 | |
---|
[851] | 416 | oldindex = self.index #Time index |
---|
[850] | 417 | |
---|
[851] | 418 | #Find time slot |
---|
| 419 | while tau > self.T[self.index]: self.index += 1 |
---|
| 420 | while tau < self.T[self.index]: self.index -= 1 |
---|
[850] | 421 | |
---|
| 422 | |
---|
[858] | 423 | if tau == self.T[self.index]: |
---|
| 424 | #Protect against case where tau == T[-1] (last time) |
---|
| 425 | # - also works in general when tau == T[i] |
---|
| 426 | ratio = 0 |
---|
| 427 | else: |
---|
| 428 | #t is now between index and index+1 |
---|
| 429 | ratio = (tau - self.T[self.index])/\ |
---|
| 430 | (self.T[self.index+1] - self.T[self.index]) |
---|
| 431 | |
---|
[955] | 432 | |
---|
| 433 | |
---|
[851] | 434 | |
---|
| 435 | #Compute interpolated values |
---|
| 436 | q = zeros( len(self.quantities), Float) |
---|
[858] | 437 | |
---|
[851] | 438 | for i, name in enumerate(self.quantities): |
---|
| 439 | Q = self.values[name] |
---|
[850] | 440 | |
---|
[858] | 441 | if ratio > 0: |
---|
| 442 | q[i] = Q[self.index, point_id] +\ |
---|
| 443 | ratio*(Q[self.index+1, point_id] -\ |
---|
| 444 | Q[self.index, point_id]) |
---|
| 445 | else: |
---|
| 446 | q[i] = Q[self.index, point_id] |
---|
[850] | 447 | |
---|
[969] | 448 | |
---|
| 449 | |
---|
[851] | 450 | #Return vector of interpolated values |
---|
| 451 | return q |
---|
| 452 | |
---|
[849] | 453 | |
---|
| 454 | class File_function_ASCII: |
---|
[820] | 455 | """Read time series from file and return a callable object: |
---|
| 456 | f(t,x,y) |
---|
| 457 | which will return interpolated values based on the input file. |
---|
[655] | 458 | |
---|
[820] | 459 | The file format is assumed to be either two fields separated by a comma: |
---|
| 460 | |
---|
| 461 | time [DD/MM/YY hh:mm:ss], value0 value1 value2 ... |
---|
| 462 | |
---|
| 463 | or four comma separated fields |
---|
| 464 | |
---|
| 465 | time [DD/MM/YY hh:mm:ss], x, y, value0 value1 value2 ... |
---|
| 466 | |
---|
| 467 | In either case, the callable object will return a tuple of |
---|
| 468 | interpolated values, one each value stated in the file. |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | E.g |
---|
| 472 | |
---|
| 473 | 31/08/04 00:00:00, 1.328223 0 0 |
---|
| 474 | 31/08/04 00:15:00, 1.292912 0 0 |
---|
| 475 | |
---|
| 476 | will provide a time dependent function f(t,x=None,y=None), |
---|
| 477 | ignoring x and y, which returns three values per call. |
---|
| 478 | |
---|
| 479 | |
---|
| 480 | NOTE: At this stage the function is assumed to depend on |
---|
| 481 | time only, i.e no spatial dependency!!!!! |
---|
[835] | 482 | When that is needed we can use the least_squares interpolation. |
---|
[844] | 483 | |
---|
| 484 | #FIXME: This should work with netcdf (e.g. sww) and thus render the |
---|
| 485 | #spatio-temporal boundary condition in shallow water fully general |
---|
[850] | 486 | |
---|
| 487 | #FIXME: Specified quantites not used here - |
---|
| 488 | #always return whatever is in the file |
---|
[820] | 489 | """ |
---|
| 490 | |
---|
| 491 | |
---|
[850] | 492 | def __init__(self, filename, domain=None, quantities = None, interpolation_points=None): |
---|
[820] | 493 | """Initialise callable object from a file. |
---|
| 494 | See docstring for class File_function |
---|
| 495 | |
---|
| 496 | If domain is specified, model time (domain,starttime) |
---|
| 497 | will be checked and possibly modified |
---|
| 498 | |
---|
[833] | 499 | All times are assumed to be in UTC |
---|
[820] | 500 | """ |
---|
| 501 | |
---|
| 502 | import time, calendar |
---|
| 503 | from Numeric import array |
---|
| 504 | from config import time_format |
---|
| 505 | |
---|
[849] | 506 | fid = open(filename) |
---|
| 507 | line = fid.readline() |
---|
| 508 | fid.close() |
---|
| 509 | |
---|
| 510 | fields = line.split(',') |
---|
| 511 | msg = 'File %s must have the format date, value0 value1 value2 ...' |
---|
| 512 | msg += ' or date, x, y, value0 value1 value2 ...' |
---|
| 513 | mode = len(fields) |
---|
| 514 | assert mode in [2,4], msg |
---|
| 515 | |
---|
| 516 | try: |
---|
| 517 | starttime = calendar.timegm(time.strptime(fields[0], time_format)) |
---|
| 518 | except ValueError: |
---|
| 519 | msg = 'First field in file %s must be' %filename |
---|
| 520 | msg += ' date-time with format %s.\n' %time_format |
---|
| 521 | msg += 'I got %s instead.' %fields[0] |
---|
| 522 | raise msg |
---|
| 523 | |
---|
| 524 | |
---|
| 525 | #Split values |
---|
| 526 | values = [] |
---|
| 527 | for value in fields[mode-1].split(): |
---|
| 528 | values.append(float(value)) |
---|
| 529 | |
---|
[1137] | 530 | q = ensure_numeric(values) |
---|
[849] | 531 | |
---|
| 532 | msg = 'ERROR: File must contain at least one independent value' |
---|
| 533 | assert len(q.shape) == 1, msg |
---|
| 534 | |
---|
| 535 | self.number_of_values = len(q) |
---|
| 536 | |
---|
| 537 | self.filename = filename |
---|
| 538 | self.starttime = starttime |
---|
| 539 | self.domain = domain |
---|
| 540 | |
---|
| 541 | if domain is not None: |
---|
[850] | 542 | msg = 'WARNING: Start time as specified in domain (%s)'\ |
---|
| 543 | %domain.starttime |
---|
| 544 | msg += ' is earlier than the starttime of file %s: %s.'\ |
---|
| 545 | %(self.filename, self.starttime) |
---|
| 546 | msg += 'Modifying starttime accordingly.' |
---|
| 547 | if self.starttime > domain.starttime: |
---|
| 548 | #FIXME: Print depending on some verbosity setting |
---|
[1102] | 549 | ##if verbose: print msg |
---|
[850] | 550 | domain.starttime = self.starttime #Modifying model time |
---|
| 551 | |
---|
| 552 | #if domain.starttime is None: |
---|
| 553 | # domain.starttime = self.starttime |
---|
| 554 | #else: |
---|
| 555 | # msg = 'WARNING: Start time as specified in domain (%s)'\ |
---|
| 556 | # %domain.starttime |
---|
| 557 | # msg += ' is earlier than the starttime of file %s: %s.'\ |
---|
| 558 | # %(self.filename, self.starttime) |
---|
| 559 | # msg += 'Modifying starttime accordingly.' |
---|
| 560 | # if self.starttime > domain.starttime: |
---|
| 561 | # #FIXME: Print depending on some verbosity setting |
---|
| 562 | # #print msg |
---|
| 563 | # domain.starttime = self.starttime #Modifying model time |
---|
[849] | 564 | |
---|
| 565 | if mode == 2: |
---|
| 566 | self.read_times() #Now read all times in. |
---|
| 567 | else: |
---|
| 568 | raise 'x,y dependency not yet implemented' |
---|
| 569 | |
---|
| 570 | |
---|
| 571 | def read_times(self): |
---|
| 572 | """Read time and values |
---|
| 573 | """ |
---|
| 574 | from Numeric import zeros, Float, alltrue |
---|
| 575 | from config import time_format |
---|
| 576 | import time, calendar |
---|
| 577 | |
---|
| 578 | fid = open(self.filename) |
---|
| 579 | lines = fid.readlines() |
---|
| 580 | fid.close() |
---|
| 581 | |
---|
| 582 | N = len(lines) |
---|
| 583 | d = self.number_of_values |
---|
| 584 | |
---|
| 585 | T = zeros(N, Float) #Time |
---|
| 586 | Q = zeros((N, d), Float) #Values |
---|
| 587 | |
---|
| 588 | for i, line in enumerate(lines): |
---|
| 589 | fields = line.split(',') |
---|
| 590 | realtime = calendar.timegm(time.strptime(fields[0], time_format)) |
---|
| 591 | |
---|
| 592 | T[i] = realtime - self.starttime |
---|
| 593 | |
---|
| 594 | for j, value in enumerate(fields[1].split()): |
---|
| 595 | Q[i, j] = float(value) |
---|
| 596 | |
---|
| 597 | msg = 'File %s must list time as a monotonuosly ' %self.filename |
---|
| 598 | msg += 'increasing sequence' |
---|
| 599 | assert alltrue( T[1:] - T[:-1] > 0 ), msg |
---|
| 600 | |
---|
| 601 | self.T = T #Time |
---|
| 602 | self.Q = Q #Values |
---|
| 603 | self.index = 0 #Initial index |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | def __repr__(self): |
---|
| 607 | return 'File function' |
---|
| 608 | |
---|
[851] | 609 | def __call__(self, t, x=None, y=None, point_id=None): |
---|
[849] | 610 | """Evaluate f(t,x,y) |
---|
| 611 | |
---|
| 612 | FIXME: x, y dependency not yet implemented except that |
---|
| 613 | result is a vector of same length as x and y |
---|
| 614 | FIXME: Naaaa |
---|
[851] | 615 | |
---|
| 616 | FIXME: Rethink semantics when x,y are vectors. |
---|
[849] | 617 | """ |
---|
| 618 | |
---|
| 619 | from math import pi, cos, sin, sqrt |
---|
| 620 | |
---|
| 621 | |
---|
| 622 | #Find time tau such that |
---|
| 623 | # |
---|
| 624 | # domain.starttime + t == self.starttime + tau |
---|
| 625 | |
---|
| 626 | if self.domain is not None: |
---|
| 627 | tau = self.domain.starttime-self.starttime+t |
---|
| 628 | else: |
---|
| 629 | tau = t |
---|
| 630 | |
---|
| 631 | |
---|
| 632 | msg = 'Time interval derived from file %s (%s:%s) does not match model time: %s'\ |
---|
| 633 | %(self.filename, self.T[0], self.T[1], tau) |
---|
| 634 | if tau < self.T[0]: raise msg |
---|
| 635 | if tau > self.T[-1]: raise msg |
---|
| 636 | |
---|
| 637 | oldindex = self.index |
---|
| 638 | |
---|
| 639 | #Find slot |
---|
| 640 | while tau > self.T[self.index]: self.index += 1 |
---|
| 641 | while tau < self.T[self.index]: self.index -= 1 |
---|
| 642 | |
---|
| 643 | #t is now between index and index+1 |
---|
| 644 | ratio = (tau - self.T[self.index])/\ |
---|
| 645 | (self.T[self.index+1] - self.T[self.index]) |
---|
| 646 | |
---|
| 647 | #Compute interpolated values |
---|
| 648 | q = self.Q[self.index,:] +\ |
---|
| 649 | ratio*(self.Q[self.index+1,:] - self.Q[self.index,:]) |
---|
| 650 | |
---|
| 651 | #Return vector of interpolated values |
---|
| 652 | if x == None and y == None: |
---|
| 653 | return q |
---|
| 654 | else: |
---|
| 655 | try: |
---|
| 656 | N = len(x) |
---|
| 657 | except: |
---|
| 658 | return q |
---|
| 659 | else: |
---|
| 660 | from Numeric import ones, Float |
---|
| 661 | #x is a vector - Create one constant column for each value |
---|
| 662 | N = len(x) |
---|
| 663 | assert len(y) == N, 'x and y must have same length' |
---|
| 664 | |
---|
| 665 | res = [] |
---|
| 666 | for col in q: |
---|
| 667 | res.append(col*ones(N, Float)) |
---|
| 668 | |
---|
| 669 | return res |
---|
| 670 | |
---|
| 671 | |
---|
| 672 | #FIXME: TEMP |
---|
| 673 | class File_function_copy: |
---|
| 674 | """Read time series from file and return a callable object: |
---|
| 675 | f(t,x,y) |
---|
| 676 | which will return interpolated values based on the input file. |
---|
| 677 | |
---|
| 678 | The file format is assumed to be either two fields separated by a comma: |
---|
| 679 | |
---|
| 680 | time [DD/MM/YY hh:mm:ss], value0 value1 value2 ... |
---|
| 681 | |
---|
| 682 | or four comma separated fields |
---|
| 683 | |
---|
| 684 | time [DD/MM/YY hh:mm:ss], x, y, value0 value1 value2 ... |
---|
| 685 | |
---|
| 686 | In either case, the callable object will return a tuple of |
---|
| 687 | interpolated values, one each value stated in the file. |
---|
| 688 | |
---|
| 689 | |
---|
| 690 | E.g |
---|
| 691 | |
---|
| 692 | 31/08/04 00:00:00, 1.328223 0 0 |
---|
| 693 | 31/08/04 00:15:00, 1.292912 0 0 |
---|
| 694 | |
---|
| 695 | will provide a time dependent function f(t,x=None,y=None), |
---|
| 696 | ignoring x and y, which returns three values per call. |
---|
| 697 | |
---|
| 698 | |
---|
| 699 | NOTE: At this stage the function is assumed to depend on |
---|
| 700 | time only, i.e no spatial dependency!!!!! |
---|
| 701 | When that is needed we can use the least_squares interpolation. |
---|
| 702 | |
---|
| 703 | #FIXME: This should work with netcdf (e.g. sww) and thus render the |
---|
| 704 | #spatio-temporal boundary condition in shallow water fully general |
---|
| 705 | """ |
---|
| 706 | |
---|
| 707 | |
---|
| 708 | def __init__(self, filename, domain=None): |
---|
| 709 | """Initialise callable object from a file. |
---|
| 710 | See docstring for class File_function |
---|
| 711 | |
---|
| 712 | If domain is specified, model time (domain,starttime) |
---|
| 713 | will be checked and possibly modified |
---|
| 714 | |
---|
| 715 | All times are assumed to be in UTC |
---|
| 716 | """ |
---|
| 717 | |
---|
| 718 | import time, calendar |
---|
| 719 | from Numeric import array |
---|
| 720 | from config import time_format |
---|
| 721 | |
---|
[820] | 722 | assert type(filename) == type(''),\ |
---|
| 723 | 'First argument to File_function must be a string' |
---|
| 724 | |
---|
| 725 | |
---|
| 726 | try: |
---|
| 727 | fid = open(filename) |
---|
| 728 | except Exception, e: |
---|
| 729 | msg = 'File "%s" could not be opened: Error="%s"'\ |
---|
| 730 | %(filename, e) |
---|
| 731 | raise msg |
---|
| 732 | |
---|
| 733 | |
---|
| 734 | line = fid.readline() |
---|
| 735 | fid.close() |
---|
| 736 | fields = line.split(',') |
---|
| 737 | msg = 'File %s must have the format date, value0 value1 value2 ...' |
---|
| 738 | msg += ' or date, x, y, value0 value1 value2 ...' |
---|
| 739 | mode = len(fields) |
---|
| 740 | assert mode in [2,4], msg |
---|
| 741 | |
---|
| 742 | try: |
---|
| 743 | starttime = calendar.timegm(time.strptime(fields[0], time_format)) |
---|
| 744 | except ValueError: |
---|
| 745 | msg = 'First field in file %s must be' %filename |
---|
| 746 | msg += ' date-time with format %s.\n' %time_format |
---|
| 747 | msg += 'I got %s instead.' %fields[0] |
---|
| 748 | raise msg |
---|
| 749 | |
---|
| 750 | |
---|
| 751 | #Split values |
---|
| 752 | values = [] |
---|
| 753 | for value in fields[mode-1].split(): |
---|
| 754 | values.append(float(value)) |
---|
| 755 | |
---|
[1137] | 756 | q = ensure_numeric(values) |
---|
[820] | 757 | |
---|
| 758 | msg = 'ERROR: File must contain at least one independent value' |
---|
| 759 | assert len(q.shape) == 1, msg |
---|
| 760 | |
---|
| 761 | self.number_of_values = len(q) |
---|
| 762 | |
---|
| 763 | self.filename = filename |
---|
| 764 | self.starttime = starttime |
---|
| 765 | self.domain = domain |
---|
| 766 | |
---|
| 767 | if domain is not None: |
---|
| 768 | if domain.starttime is None: |
---|
| 769 | domain.starttime = self.starttime |
---|
| 770 | else: |
---|
| 771 | msg = 'WARNING: Start time as specified in domain (%s)'\ |
---|
| 772 | %domain.starttime |
---|
| 773 | msg += ' is earlier than the starttime of file %s: %s.'\ |
---|
| 774 | %(self.filename, self.starttime) |
---|
| 775 | msg += 'Modifying starttime accordingly.' |
---|
| 776 | if self.starttime > domain.starttime: |
---|
| 777 | #FIXME: Print depending on some verbosity setting |
---|
| 778 | #print msg |
---|
| 779 | domain.starttime = self.starttime #Modifying model time |
---|
| 780 | |
---|
| 781 | if mode == 2: |
---|
| 782 | self.read_times() #Now read all times in. |
---|
| 783 | else: |
---|
[835] | 784 | raise 'x,y dependency not yet implemented' |
---|
[820] | 785 | |
---|
| 786 | |
---|
| 787 | def read_times(self): |
---|
| 788 | """Read time and values |
---|
| 789 | """ |
---|
| 790 | from Numeric import zeros, Float, alltrue |
---|
| 791 | from config import time_format |
---|
| 792 | import time, calendar |
---|
| 793 | |
---|
| 794 | fid = open(self.filename) |
---|
| 795 | lines = fid.readlines() |
---|
| 796 | fid.close() |
---|
| 797 | |
---|
| 798 | N = len(lines) |
---|
| 799 | d = self.number_of_values |
---|
| 800 | |
---|
| 801 | T = zeros(N, Float) #Time |
---|
| 802 | Q = zeros((N, d), Float) #Values |
---|
| 803 | |
---|
| 804 | for i, line in enumerate(lines): |
---|
| 805 | fields = line.split(',') |
---|
| 806 | realtime = calendar.timegm(time.strptime(fields[0], time_format)) |
---|
| 807 | |
---|
| 808 | T[i] = realtime - self.starttime |
---|
| 809 | |
---|
| 810 | for j, value in enumerate(fields[1].split()): |
---|
| 811 | Q[i, j] = float(value) |
---|
| 812 | |
---|
| 813 | msg = 'File %s must list time as a monotonuosly ' %self.filename |
---|
| 814 | msg += 'increasing sequence' |
---|
| 815 | assert alltrue( T[1:] - T[:-1] > 0 ), msg |
---|
| 816 | |
---|
| 817 | self.T = T #Time |
---|
| 818 | self.Q = Q #Values |
---|
| 819 | self.index = 0 #Initial index |
---|
| 820 | |
---|
| 821 | |
---|
| 822 | def __repr__(self): |
---|
| 823 | return 'File function' |
---|
| 824 | |
---|
| 825 | |
---|
| 826 | |
---|
| 827 | def __call__(self, t, x=None, y=None): |
---|
| 828 | """Evaluate f(t,x,y) |
---|
| 829 | |
---|
| 830 | FIXME: x, y dependency not yet implemented except that |
---|
| 831 | result is a vector of same length as x and y |
---|
[835] | 832 | FIXME: Naaaa |
---|
[820] | 833 | """ |
---|
| 834 | |
---|
| 835 | from math import pi, cos, sin, sqrt |
---|
| 836 | |
---|
| 837 | |
---|
| 838 | #Find time tau such that |
---|
| 839 | # |
---|
| 840 | # domain.starttime + t == self.starttime + tau |
---|
| 841 | |
---|
| 842 | if self.domain is not None: |
---|
| 843 | tau = self.domain.starttime-self.starttime+t |
---|
| 844 | else: |
---|
| 845 | tau = t |
---|
| 846 | |
---|
| 847 | |
---|
| 848 | msg = 'Time interval derived from file %s (%s:%s) does not match model time: %s'\ |
---|
| 849 | %(self.filename, self.T[0], self.T[1], tau) |
---|
| 850 | if tau < self.T[0]: raise msg |
---|
| 851 | if tau > self.T[-1]: raise msg |
---|
| 852 | |
---|
| 853 | oldindex = self.index |
---|
| 854 | |
---|
| 855 | #Find slot |
---|
| 856 | while tau > self.T[self.index]: self.index += 1 |
---|
| 857 | while tau < self.T[self.index]: self.index -= 1 |
---|
| 858 | |
---|
| 859 | #t is now between index and index+1 |
---|
| 860 | ratio = (tau - self.T[self.index])/\ |
---|
| 861 | (self.T[self.index+1] - self.T[self.index]) |
---|
| 862 | |
---|
| 863 | #Compute interpolated values |
---|
| 864 | q = self.Q[self.index,:] +\ |
---|
| 865 | ratio*(self.Q[self.index+1,:] - self.Q[self.index,:]) |
---|
| 866 | |
---|
| 867 | #Return vector of interpolated values |
---|
| 868 | if x == None and y == None: |
---|
| 869 | return q |
---|
| 870 | else: |
---|
[835] | 871 | try: |
---|
| 872 | N = len(x) |
---|
| 873 | except: |
---|
| 874 | return q |
---|
| 875 | else: |
---|
| 876 | from Numeric import ones, Float |
---|
| 877 | #x is a vector - Create one constant column for each value |
---|
| 878 | N = len(x) |
---|
| 879 | assert len(y) == N, 'x and y must have same length' |
---|
| 880 | |
---|
| 881 | res = [] |
---|
| 882 | for col in q: |
---|
| 883 | res.append(col*ones(N, Float)) |
---|
[820] | 884 | |
---|
[835] | 885 | return res |
---|
| 886 | |
---|
[820] | 887 | |
---|
[834] | 888 | def read_xya(filename, format = 'netcdf'): |
---|
[832] | 889 | """Read simple xya file, possibly with a header in the first line, just |
---|
[820] | 890 | x y [attributes] |
---|
| 891 | separated by whitespace |
---|
[826] | 892 | |
---|
[834] | 893 | Format can be either ASCII or NetCDF |
---|
| 894 | |
---|
[832] | 895 | Return list of points, list of attributes and |
---|
| 896 | attribute names if present otherwise None |
---|
[820] | 897 | """ |
---|
[836] | 898 | #FIXME: Probably obsoleted by similar function in load_ASCII |
---|
[832] | 899 | |
---|
[826] | 900 | from Scientific.IO.NetCDF import NetCDFFile |
---|
[834] | 901 | |
---|
| 902 | if format.lower() == 'netcdf': |
---|
[832] | 903 | #Get NetCDF |
---|
| 904 | |
---|
| 905 | fid = NetCDFFile(filename, 'r') |
---|
[826] | 906 | |
---|
| 907 | # Get the variables |
---|
| 908 | points = fid.variables['points'] |
---|
[840] | 909 | keys = fid.variables.keys() |
---|
| 910 | attributes = {} |
---|
| 911 | for key in keys: |
---|
[842] | 912 | attributes[key] = fid.variables[key] |
---|
[832] | 913 | #Don't close - arrays are needed outside this function, |
---|
| 914 | #alternatively take a copy here |
---|
[834] | 915 | else: |
---|
[832] | 916 | #Read as ASCII file assuming that it is separated by whitespace |
---|
[826] | 917 | fid = open(filename) |
---|
| 918 | lines = fid.readlines() |
---|
[832] | 919 | fid.close() |
---|
| 920 | |
---|
| 921 | #Check if there is a header line |
---|
| 922 | fields = lines[0].strip().split() |
---|
| 923 | try: |
---|
| 924 | float(fields[0]) |
---|
| 925 | except: |
---|
| 926 | #This must be a header line |
---|
| 927 | attribute_names = fields |
---|
| 928 | lines = lines[1:] |
---|
| 929 | else: |
---|
[841] | 930 | attribute_names = ['elevation'] #HACK |
---|
| 931 | |
---|
| 932 | attributes = {} |
---|
| 933 | for key in attribute_names: |
---|
| 934 | attributes[key] = [] |
---|
| 935 | |
---|
| 936 | points = [] |
---|
[826] | 937 | for line in lines: |
---|
| 938 | fields = line.strip().split() |
---|
[832] | 939 | points.append( (float(fields[0]), float(fields[1])) ) |
---|
[841] | 940 | for i, key in enumerate(attribute_names): |
---|
| 941 | attributes[key].append( float(fields[2+i]) ) |
---|
[820] | 942 | |
---|
[840] | 943 | return points, attributes |
---|
[820] | 944 | |
---|
| 945 | |
---|
| 946 | ##################################### |
---|
[826] | 947 | #POLYGON STUFF |
---|
[820] | 948 | # |
---|
[1093] | 949 | #FIXME: All these should be put into new module polygon.py |
---|
[820] | 950 | |
---|
[1093] | 951 | |
---|
| 952 | |
---|
| 953 | #Redefine these to use separate_by_polygon which will put all inside indices |
---|
| 954 | #in the first part of the indices array and outside indices in the last |
---|
| 955 | |
---|
| 956 | def inside_polygon(points, polygon, closed = True, verbose = False): |
---|
| 957 | """See separate_points_by_polygon for documentation |
---|
| 958 | """ |
---|
| 959 | |
---|
| 960 | from Numeric import array, Float, reshape |
---|
| 961 | |
---|
| 962 | if verbose: print 'Checking input to inside_polygon' |
---|
| 963 | try: |
---|
[1137] | 964 | points = ensure_numeric(points, Float) |
---|
[1093] | 965 | except: |
---|
| 966 | msg = 'Points could not be converted to Numeric array' |
---|
| 967 | raise msg |
---|
| 968 | |
---|
| 969 | try: |
---|
[1137] | 970 | polygon = ensure_numeric(polygon, Float) |
---|
[1093] | 971 | except: |
---|
| 972 | msg = 'Polygon could not be converted to Numeric array' |
---|
| 973 | raise msg |
---|
| 974 | |
---|
| 975 | |
---|
| 976 | |
---|
| 977 | if len(points.shape) == 1: |
---|
| 978 | one_point = True |
---|
| 979 | points = reshape(points, (1,2)) |
---|
| 980 | else: |
---|
| 981 | one_point = False |
---|
| 982 | |
---|
| 983 | indices, count = separate_points_by_polygon(points, polygon, |
---|
| 984 | closed, verbose) |
---|
| 985 | |
---|
| 986 | if one_point: |
---|
| 987 | return count == 1 |
---|
| 988 | else: |
---|
| 989 | return indices[:count] |
---|
| 990 | |
---|
| 991 | def outside_polygon(points, polygon, closed = True, verbose = False): |
---|
| 992 | """See separate_points_by_polygon for documentation |
---|
| 993 | """ |
---|
| 994 | |
---|
| 995 | from Numeric import array, Float, reshape |
---|
| 996 | |
---|
| 997 | if verbose: print 'Checking input to outside_polygon' |
---|
| 998 | try: |
---|
[1137] | 999 | points = ensure_numeric(points, Float) |
---|
[1093] | 1000 | except: |
---|
| 1001 | msg = 'Points could not be converted to Numeric array' |
---|
| 1002 | raise msg |
---|
| 1003 | |
---|
| 1004 | try: |
---|
[1137] | 1005 | polygon = ensure_numeric(polygon, Float) |
---|
[1093] | 1006 | except: |
---|
| 1007 | msg = 'Polygon could not be converted to Numeric array' |
---|
| 1008 | raise msg |
---|
| 1009 | |
---|
| 1010 | |
---|
| 1011 | |
---|
| 1012 | if len(points.shape) == 1: |
---|
| 1013 | one_point = True |
---|
| 1014 | points = reshape(points, (1,2)) |
---|
| 1015 | else: |
---|
| 1016 | one_point = False |
---|
| 1017 | |
---|
| 1018 | indices, count = separate_points_by_polygon(points, polygon, |
---|
| 1019 | closed, verbose) |
---|
| 1020 | |
---|
| 1021 | if one_point: |
---|
| 1022 | return count != 1 |
---|
| 1023 | else: |
---|
| 1024 | return indices[count:][::-1] #return reversed |
---|
| 1025 | |
---|
| 1026 | |
---|
| 1027 | def separate_points_by_polygon(points, polygon, |
---|
| 1028 | closed = True, verbose = False): |
---|
[655] | 1029 | """Determine whether points are inside or outside a polygon |
---|
| 1030 | |
---|
| 1031 | Input: |
---|
[1093] | 1032 | points - Tuple of (x, y) coordinates, or list of tuples |
---|
[655] | 1033 | polygon - list of vertices of polygon |
---|
[664] | 1034 | closed - (optional) determine whether points on boundary should be |
---|
| 1035 | regarded as belonging to the polygon (closed = True) |
---|
| 1036 | or not (closed = False) |
---|
[655] | 1037 | |
---|
[1093] | 1038 | Outputs: |
---|
| 1039 | indices: array of same length as points with indices of points falling |
---|
| 1040 | inside the polygon listed from the beginning and indices of points |
---|
| 1041 | falling outside listed from the end. |
---|
[655] | 1042 | |
---|
[1093] | 1043 | count: count of points falling inside the polygon |
---|
| 1044 | |
---|
| 1045 | The indices of points inside are obtained as indices[:count] |
---|
| 1046 | The indices of points outside are obtained as indices[count:] |
---|
| 1047 | |
---|
| 1048 | |
---|
[655] | 1049 | Examples: |
---|
[1024] | 1050 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
[655] | 1051 | |
---|
[1093] | 1052 | separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U) |
---|
| 1053 | will return the indices [0, 2, 1] and count == 2 as only the first |
---|
| 1054 | and the last point are inside the unit square |
---|
[655] | 1055 | |
---|
| 1056 | Remarks: |
---|
[664] | 1057 | The vertices may be listed clockwise or counterclockwise and |
---|
| 1058 | the first point may optionally be repeated. |
---|
| 1059 | Polygons do not need to be convex. |
---|
| 1060 | Polygons can have holes in them and points inside a hole is |
---|
| 1061 | regarded as being outside the polygon. |
---|
[655] | 1062 | |
---|
[664] | 1063 | Algorithm is based on work by Darel Finley, |
---|
| 1064 | http://www.alienryderflex.com/polygon/ |
---|
[655] | 1065 | |
---|
| 1066 | """ |
---|
| 1067 | |
---|
[1093] | 1068 | from Numeric import array, Float, reshape, Int, zeros |
---|
[655] | 1069 | |
---|
| 1070 | |
---|
| 1071 | #Input checks |
---|
| 1072 | try: |
---|
[1137] | 1073 | points = ensure_numeric(points, Float) |
---|
[655] | 1074 | except: |
---|
[1093] | 1075 | msg = 'Points could not be converted to Numeric array' |
---|
[655] | 1076 | raise msg |
---|
| 1077 | |
---|
| 1078 | try: |
---|
[1137] | 1079 | polygon = ensure_numeric(polygon, Float) |
---|
[655] | 1080 | except: |
---|
[1093] | 1081 | msg = 'Polygon could not be converted to Numeric array' |
---|
[655] | 1082 | raise msg |
---|
[1093] | 1083 | |
---|
[655] | 1084 | assert len(polygon.shape) == 2,\ |
---|
[1093] | 1085 | 'Polygon array must be a 2d array of vertices' |
---|
[655] | 1086 | |
---|
| 1087 | assert polygon.shape[1] == 2,\ |
---|
[1093] | 1088 | 'Polygon array must have two columns' |
---|
[655] | 1089 | |
---|
[1093] | 1090 | assert len(points.shape) == 2,\ |
---|
| 1091 | 'Points array must be a 2d array' |
---|
| 1092 | |
---|
| 1093 | assert points.shape[1] == 2,\ |
---|
| 1094 | 'Points array must have two columns' |
---|
| 1095 | |
---|
[655] | 1096 | N = polygon.shape[0] #Number of vertices in polygon |
---|
[997] | 1097 | M = points.shape[0] #Number of points |
---|
| 1098 | |
---|
[655] | 1099 | px = polygon[:,0] |
---|
| 1100 | py = polygon[:,1] |
---|
[656] | 1101 | |
---|
[999] | 1102 | #Used for an optimisation when points are far away from polygon |
---|
[997] | 1103 | minpx = min(px); maxpx = max(px) |
---|
| 1104 | minpy = min(py); maxpy = max(py) |
---|
| 1105 | |
---|
| 1106 | |
---|
[1093] | 1107 | #Begin main loop |
---|
| 1108 | indices = zeros(M, Int) |
---|
| 1109 | |
---|
| 1110 | inside_index = 0 #Keep track of points inside |
---|
| 1111 | outside_index = M-1 #Keep track of points outside (starting from end) |
---|
| 1112 | |
---|
[997] | 1113 | for k in range(M): |
---|
| 1114 | |
---|
| 1115 | if verbose: |
---|
| 1116 | if k %((M+10)/10)==0: print 'Doing %d of %d' %(k, M) |
---|
| 1117 | |
---|
[655] | 1118 | #for each point |
---|
| 1119 | x = points[k, 0] |
---|
| 1120 | y = points[k, 1] |
---|
[659] | 1121 | |
---|
[655] | 1122 | inside = False |
---|
[997] | 1123 | |
---|
[1093] | 1124 | if not x > maxpx or x < minpx or y > maxpy or y < minpy: |
---|
| 1125 | #Check polygon |
---|
| 1126 | for i in range(N): |
---|
| 1127 | j = (i+1)%N |
---|
| 1128 | |
---|
| 1129 | #Check for case where point is contained in line segment |
---|
| 1130 | if point_on_line(x, y, px[i], py[i], px[j], py[j]): |
---|
| 1131 | if closed: |
---|
| 1132 | inside = True |
---|
| 1133 | else: |
---|
| 1134 | inside = False |
---|
| 1135 | break |
---|
| 1136 | else: |
---|
| 1137 | #Check if truly inside polygon |
---|
| 1138 | if py[i] < y and py[j] >= y or\ |
---|
| 1139 | py[j] < y and py[i] >= y: |
---|
| 1140 | if px[i] + (y-py[i])/(py[j]-py[i])*(px[j]-px[i]) < x: |
---|
| 1141 | inside = not inside |
---|
| 1142 | |
---|
| 1143 | if inside: |
---|
| 1144 | indices[inside_index] = k |
---|
| 1145 | inside_index += 1 |
---|
| 1146 | else: |
---|
| 1147 | indices[outside_index] = k |
---|
| 1148 | outside_index -= 1 |
---|
[655] | 1149 | |
---|
[1093] | 1150 | return indices, inside_index |
---|
[656] | 1151 | |
---|
[655] | 1152 | |
---|
[1093] | 1153 | def separate_points_by_polygon_c(points, polygon, |
---|
| 1154 | closed = True, verbose = False): |
---|
[999] | 1155 | """Determine whether points are inside or outside a polygon |
---|
| 1156 | |
---|
| 1157 | C-wrapper |
---|
| 1158 | """ |
---|
| 1159 | |
---|
| 1160 | from Numeric import array, Float, reshape, zeros, Int |
---|
| 1161 | |
---|
[1091] | 1162 | |
---|
[1093] | 1163 | if verbose: print 'Checking input to separate_points_by_polygon' |
---|
[999] | 1164 | #Input checks |
---|
| 1165 | try: |
---|
[1137] | 1166 | points = ensure_numeric(points, Float) |
---|
[999] | 1167 | except: |
---|
[1093] | 1168 | msg = 'Points could not be converted to Numeric array' |
---|
[1104] | 1169 | raise msg |
---|
| 1170 | |
---|
| 1171 | #if verbose: print 'Checking input to separate_points_by_polygon 2' |
---|
[999] | 1172 | try: |
---|
[1137] | 1173 | polygon = ensure_numeric(polygon, Float) |
---|
[999] | 1174 | except: |
---|
[1093] | 1175 | msg = 'Polygon could not be converted to Numeric array' |
---|
[999] | 1176 | raise msg |
---|
[1093] | 1177 | |
---|
[1104] | 1178 | if verbose: print 'check' |
---|
| 1179 | |
---|
[999] | 1180 | assert len(polygon.shape) == 2,\ |
---|
[1093] | 1181 | 'Polygon array must be a 2d array of vertices' |
---|
[999] | 1182 | |
---|
| 1183 | assert polygon.shape[1] == 2,\ |
---|
[1093] | 1184 | 'Polygon array must have two columns' |
---|
[999] | 1185 | |
---|
[1093] | 1186 | assert len(points.shape) == 2,\ |
---|
| 1187 | 'Points array must be a 2d array' |
---|
| 1188 | |
---|
| 1189 | assert points.shape[1] == 2,\ |
---|
| 1190 | 'Points array must have two columns' |
---|
| 1191 | |
---|
| 1192 | N = polygon.shape[0] #Number of vertices in polygon |
---|
| 1193 | M = points.shape[0] #Number of points |
---|
[999] | 1194 | |
---|
[1093] | 1195 | from util_ext import separate_points_by_polygon |
---|
[999] | 1196 | |
---|
[1091] | 1197 | if verbose: print 'Allocating array for indices' |
---|
| 1198 | |
---|
[1093] | 1199 | indices = zeros( M, Int ) |
---|
[999] | 1200 | |
---|
[1091] | 1201 | if verbose: print 'Calling C-version of inside poly' |
---|
[1093] | 1202 | count = separate_points_by_polygon(points, polygon, indices, |
---|
| 1203 | int(closed), int(verbose)) |
---|
[999] | 1204 | |
---|
[1093] | 1205 | return indices, count |
---|
[999] | 1206 | |
---|
[1024] | 1207 | |
---|
| 1208 | |
---|
[656] | 1209 | class Polygon_function: |
---|
| 1210 | """Create callable object f: x,y -> z, where a,y,z are vectors and |
---|
| 1211 | where f will return different values depending on whether x,y belongs |
---|
| 1212 | to specified polygons. |
---|
| 1213 | |
---|
| 1214 | To instantiate: |
---|
| 1215 | |
---|
| 1216 | Polygon_function(polygons) |
---|
| 1217 | |
---|
[989] | 1218 | where polygons is a list of tuples of the form |
---|
[656] | 1219 | |
---|
[989] | 1220 | [ (P0, v0), (P1, v1), ...] |
---|
[656] | 1221 | |
---|
| 1222 | with Pi being lists of vertices defining polygons and vi either |
---|
| 1223 | constants or functions of x,y to be applied to points with the polygon. |
---|
| 1224 | |
---|
| 1225 | The function takes an optional argument, default which is the value |
---|
| 1226 | (or function) to used for points not belonging to any polygon. |
---|
| 1227 | For example: |
---|
| 1228 | |
---|
| 1229 | Polygon_function(polygons, default = 0.03) |
---|
| 1230 | |
---|
| 1231 | If omitted the default value will be 0.0 |
---|
| 1232 | |
---|
| 1233 | Note: If two polygons overlap, the one last in the list takes precedence |
---|
| 1234 | |
---|
| 1235 | """ |
---|
| 1236 | |
---|
| 1237 | def __init__(self, regions, default = 0.0): |
---|
| 1238 | |
---|
| 1239 | try: |
---|
| 1240 | len(regions) |
---|
| 1241 | except: |
---|
| 1242 | msg = 'Polygon_function takes a list of pairs (polygon, value). Got %s' %polygons |
---|
| 1243 | raise msg |
---|
| 1244 | |
---|
| 1245 | |
---|
| 1246 | T = regions[0] |
---|
| 1247 | try: |
---|
| 1248 | a = len(T) |
---|
| 1249 | except: |
---|
| 1250 | msg = 'Polygon_function takes a list of pairs (polygon, value). Got %s' %polygons |
---|
| 1251 | raise msg |
---|
| 1252 | |
---|
| 1253 | assert a == 2, 'Must have two component each: %s' %T |
---|
| 1254 | |
---|
| 1255 | self.regions = regions |
---|
| 1256 | self.default = default |
---|
| 1257 | |
---|
| 1258 | |
---|
| 1259 | def __call__(self, x, y): |
---|
| 1260 | from util import inside_polygon |
---|
| 1261 | from Numeric import ones, Float, concatenate, array, reshape, choose |
---|
| 1262 | |
---|
| 1263 | x = array(x).astype(Float) |
---|
| 1264 | y = array(y).astype(Float) |
---|
| 1265 | |
---|
[664] | 1266 | N = len(x) |
---|
| 1267 | assert len(y) == N |
---|
| 1268 | |
---|
| 1269 | points = concatenate( (reshape(x, (N, 1)), |
---|
| 1270 | reshape(y, (N, 1))), axis=1 ) |
---|
| 1271 | |
---|
| 1272 | if callable(self.default): |
---|
| 1273 | z = self.default(x,y) |
---|
| 1274 | else: |
---|
| 1275 | z = ones(N, Float) * self.default |
---|
| 1276 | |
---|
[656] | 1277 | for polygon, value in self.regions: |
---|
| 1278 | indices = inside_polygon(points, polygon) |
---|
[664] | 1279 | |
---|
| 1280 | #FIXME: This needs to be vectorised |
---|
| 1281 | if callable(value): |
---|
| 1282 | for i in indices: |
---|
| 1283 | xx = array([x[i]]) |
---|
| 1284 | yy = array([y[i]]) |
---|
| 1285 | z[i] = value(xx, yy)[0] |
---|
| 1286 | else: |
---|
| 1287 | for i in indices: |
---|
| 1288 | z[i] = value |
---|
[656] | 1289 | |
---|
| 1290 | return z |
---|
[659] | 1291 | |
---|
| 1292 | def read_polygon(filename): |
---|
| 1293 | """Read points assumed to form a polygon |
---|
| 1294 | There must be exactly two numbers in each line |
---|
| 1295 | """ |
---|
| 1296 | |
---|
| 1297 | #Get polygon |
---|
| 1298 | fid = open(filename) |
---|
| 1299 | lines = fid.readlines() |
---|
| 1300 | fid.close() |
---|
| 1301 | polygon = [] |
---|
| 1302 | for line in lines: |
---|
| 1303 | fields = line.split(',') |
---|
| 1304 | polygon.append( [float(fields[0]), float(fields[1])] ) |
---|
| 1305 | |
---|
| 1306 | return polygon |
---|
[623] | 1307 | |
---|
[799] | 1308 | def populate_polygon(polygon, number_of_points, seed = None): |
---|
| 1309 | """Populate given polygon with uniformly distributed points. |
---|
[623] | 1310 | |
---|
[799] | 1311 | Input: |
---|
| 1312 | polygon - list of vertices of polygon |
---|
| 1313 | number_of_points - (optional) number of points |
---|
| 1314 | seed - seed for random number generator (default=None) |
---|
| 1315 | |
---|
| 1316 | Output: |
---|
| 1317 | points - list of points inside polygon |
---|
| 1318 | |
---|
| 1319 | Examples: |
---|
| 1320 | populate_polygon( [[0,0], [1,0], [1,1], [0,1]], 5 ) |
---|
[999] | 1321 | will return five randomly selected points inside the unit square |
---|
[799] | 1322 | """ |
---|
[623] | 1323 | |
---|
[799] | 1324 | from random import uniform, seed |
---|
| 1325 | |
---|
| 1326 | seed(seed) |
---|
| 1327 | |
---|
| 1328 | points = [] |
---|
| 1329 | |
---|
| 1330 | #Find outer extent of polygon |
---|
| 1331 | max_x = min_x = polygon[0][0] |
---|
| 1332 | max_y = min_y = polygon[0][1] |
---|
| 1333 | for point in polygon[1:]: |
---|
| 1334 | x = point[0] |
---|
| 1335 | if x > max_x: max_x = x |
---|
| 1336 | if x < min_x: min_x = x |
---|
| 1337 | y = point[1] |
---|
| 1338 | if y > max_y: max_y = y |
---|
| 1339 | if y < min_y: min_y = y |
---|
| 1340 | |
---|
| 1341 | |
---|
| 1342 | while len(points) < number_of_points: |
---|
| 1343 | x = uniform(min_x, max_x) |
---|
| 1344 | y = uniform(min_y, max_y) |
---|
| 1345 | |
---|
| 1346 | if inside_polygon( [x,y], polygon ): |
---|
| 1347 | points.append([x,y]) |
---|
| 1348 | |
---|
| 1349 | return points |
---|
| 1350 | |
---|
[258] | 1351 | #################################################################### |
---|
| 1352 | #Python versions of function that are also implemented in util_gateway.c |
---|
| 1353 | # |
---|
| 1354 | |
---|
| 1355 | def gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2): |
---|
| 1356 | """ |
---|
| 1357 | """ |
---|
| 1358 | |
---|
| 1359 | det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0) |
---|
| 1360 | a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0) |
---|
| 1361 | a /= det |
---|
| 1362 | |
---|
| 1363 | b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0) |
---|
| 1364 | b /= det |
---|
| 1365 | |
---|
| 1366 | return a, b |
---|
| 1367 | |
---|
| 1368 | |
---|
| 1369 | |
---|
| 1370 | ############################################## |
---|
| 1371 | #Initialise module |
---|
| 1372 | |
---|
| 1373 | import compile |
---|
| 1374 | if compile.can_use_C_extension('util_ext.c'): |
---|
[999] | 1375 | from util_ext import gradient, point_on_line |
---|
[1093] | 1376 | separate_points_by_polygon = separate_points_by_polygon_c |
---|
[258] | 1377 | else: |
---|
| 1378 | gradient = gradient_python |
---|
| 1379 | |
---|
| 1380 | |
---|
| 1381 | if __name__ == "__main__": |
---|
| 1382 | pass |
---|
| 1383 | |
---|
[1093] | 1384 | |
---|
| 1385 | |
---|
| 1386 | |
---|
| 1387 | |
---|
| 1388 | |
---|
| 1389 | #OBSOLETED STUFF |
---|
| 1390 | def inside_polygon_old(point, polygon, closed = True, verbose = False): |
---|
| 1391 | #FIXME Obsoleted |
---|
| 1392 | """Determine whether points are inside or outside a polygon |
---|
| 1393 | |
---|
| 1394 | Input: |
---|
| 1395 | point - Tuple of (x, y) coordinates, or list of tuples |
---|
| 1396 | polygon - list of vertices of polygon |
---|
| 1397 | closed - (optional) determine whether points on boundary should be |
---|
| 1398 | regarded as belonging to the polygon (closed = True) |
---|
| 1399 | or not (closed = False) |
---|
| 1400 | |
---|
| 1401 | Output: |
---|
| 1402 | If one point is considered, True or False is returned. |
---|
| 1403 | If multiple points are passed in, the function returns indices |
---|
| 1404 | of those points that fall inside the polygon |
---|
| 1405 | |
---|
| 1406 | Examples: |
---|
| 1407 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
| 1408 | inside_polygon( [0.5, 0.5], U) |
---|
| 1409 | will evaluate to True as the point 0.5, 0.5 is inside the unit square |
---|
| 1410 | |
---|
| 1411 | inside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U) |
---|
| 1412 | will return the indices [0, 2] as only the first and the last point |
---|
| 1413 | is inside the unit square |
---|
| 1414 | |
---|
| 1415 | Remarks: |
---|
| 1416 | The vertices may be listed clockwise or counterclockwise and |
---|
| 1417 | the first point may optionally be repeated. |
---|
| 1418 | Polygons do not need to be convex. |
---|
| 1419 | Polygons can have holes in them and points inside a hole is |
---|
| 1420 | regarded as being outside the polygon. |
---|
| 1421 | |
---|
| 1422 | |
---|
| 1423 | Algorithm is based on work by Darel Finley, |
---|
| 1424 | http://www.alienryderflex.com/polygon/ |
---|
| 1425 | |
---|
| 1426 | """ |
---|
| 1427 | |
---|
| 1428 | from Numeric import array, Float, reshape |
---|
| 1429 | |
---|
| 1430 | |
---|
| 1431 | #Input checks |
---|
| 1432 | try: |
---|
| 1433 | point = array(point).astype(Float) |
---|
| 1434 | except: |
---|
| 1435 | msg = 'Point %s could not be converted to Numeric array' %point |
---|
| 1436 | raise msg |
---|
| 1437 | |
---|
| 1438 | try: |
---|
| 1439 | polygon = array(polygon).astype(Float) |
---|
| 1440 | except: |
---|
| 1441 | msg = 'Polygon %s could not be converted to Numeric array' %polygon |
---|
| 1442 | raise msg |
---|
| 1443 | |
---|
| 1444 | assert len(polygon.shape) == 2,\ |
---|
| 1445 | 'Polygon array must be a 2d array of vertices: %s' %polygon |
---|
| 1446 | |
---|
| 1447 | |
---|
| 1448 | assert polygon.shape[1] == 2,\ |
---|
| 1449 | 'Polygon array must have two columns: %s' %polygon |
---|
| 1450 | |
---|
| 1451 | |
---|
| 1452 | if len(point.shape) == 1: |
---|
| 1453 | one_point = True |
---|
| 1454 | points = reshape(point, (1,2)) |
---|
| 1455 | else: |
---|
| 1456 | one_point = False |
---|
| 1457 | points = point |
---|
| 1458 | |
---|
| 1459 | N = polygon.shape[0] #Number of vertices in polygon |
---|
| 1460 | M = points.shape[0] #Number of points |
---|
| 1461 | |
---|
| 1462 | px = polygon[:,0] |
---|
| 1463 | py = polygon[:,1] |
---|
| 1464 | |
---|
| 1465 | #Used for an optimisation when points are far away from polygon |
---|
| 1466 | minpx = min(px); maxpx = max(px) |
---|
| 1467 | minpy = min(py); maxpy = max(py) |
---|
| 1468 | |
---|
| 1469 | |
---|
| 1470 | #Begin main loop (FIXME: It'd be crunchy to have this written in C:-) |
---|
| 1471 | indices = [] |
---|
| 1472 | for k in range(M): |
---|
| 1473 | |
---|
| 1474 | if verbose: |
---|
| 1475 | if k %((M+10)/10)==0: print 'Doing %d of %d' %(k, M) |
---|
| 1476 | |
---|
| 1477 | #for each point |
---|
| 1478 | x = points[k, 0] |
---|
| 1479 | y = points[k, 1] |
---|
| 1480 | |
---|
| 1481 | inside = False |
---|
| 1482 | |
---|
| 1483 | #Optimisation |
---|
| 1484 | if x > maxpx or x < minpx: continue |
---|
| 1485 | if y > maxpy or y < minpy: continue |
---|
| 1486 | |
---|
| 1487 | #Check polygon |
---|
| 1488 | for i in range(N): |
---|
| 1489 | j = (i+1)%N |
---|
| 1490 | |
---|
| 1491 | #Check for case where point is contained in line segment |
---|
| 1492 | if point_on_line(x, y, px[i], py[i], px[j], py[j]): |
---|
| 1493 | if closed: |
---|
| 1494 | inside = True |
---|
| 1495 | else: |
---|
| 1496 | inside = False |
---|
| 1497 | break |
---|
| 1498 | else: |
---|
| 1499 | #Check if truly inside polygon |
---|
| 1500 | if py[i] < y and py[j] >= y or\ |
---|
| 1501 | py[j] < y and py[i] >= y: |
---|
| 1502 | if px[i] + (y-py[i])/(py[j]-py[i])*(px[j]-px[i]) < x: |
---|
| 1503 | inside = not inside |
---|
| 1504 | |
---|
| 1505 | if inside: indices.append(k) |
---|
| 1506 | |
---|
| 1507 | if one_point: |
---|
| 1508 | return inside |
---|
| 1509 | else: |
---|
| 1510 | return indices |
---|
| 1511 | |
---|
| 1512 | |
---|
| 1513 | #def remove_from(A, B): |
---|
| 1514 | # """Assume that A |
---|
| 1515 | # """ |
---|
| 1516 | # from Numeric import search_sorted## |
---|
| 1517 | # |
---|
| 1518 | # ind = search_sorted(A, B) |
---|
| 1519 | |
---|
| 1520 | |
---|
| 1521 | |
---|
| 1522 | def outside_polygon_old(point, polygon, closed = True, verbose = False): |
---|
| 1523 | #OBSOLETED |
---|
| 1524 | """Determine whether points are outside a polygon |
---|
| 1525 | |
---|
| 1526 | Input: |
---|
| 1527 | point - Tuple of (x, y) coordinates, or list of tuples |
---|
| 1528 | polygon - list of vertices of polygon |
---|
| 1529 | closed - (optional) determine whether points on boundary should be |
---|
| 1530 | regarded as belonging to the polygon (closed = True) |
---|
| 1531 | or not (closed = False) |
---|
| 1532 | |
---|
| 1533 | Output: |
---|
| 1534 | If one point is considered, True or False is returned. |
---|
| 1535 | If multiple points are passed in, the function returns indices |
---|
| 1536 | of those points that fall outside the polygon |
---|
| 1537 | |
---|
| 1538 | Examples: |
---|
| 1539 | U = [[0,0], [1,0], [1,1], [0,1]] #Unit square |
---|
| 1540 | outside_polygon( [0.5, 0.5], U ) |
---|
| 1541 | will evaluate to False as the point 0.5, 0.5 is inside the unit square |
---|
| 1542 | |
---|
| 1543 | ouside_polygon( [1.5, 0.5], U ) |
---|
| 1544 | will evaluate to True as the point 1.5, 0.5 is outside the unit square |
---|
| 1545 | |
---|
| 1546 | outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) |
---|
| 1547 | will return the indices [1] as only the first and the last point |
---|
| 1548 | is inside the unit square |
---|
| 1549 | """ |
---|
| 1550 | |
---|
| 1551 | #FIXME: This is too slow |
---|
| 1552 | |
---|
| 1553 | res = inside_polygon(point, polygon, closed, verbose) |
---|
| 1554 | |
---|
| 1555 | if res is True or res is False: |
---|
| 1556 | return not res |
---|
| 1557 | |
---|
| 1558 | #Now invert indices |
---|
| 1559 | from Numeric import arrayrange, compress |
---|
| 1560 | outside_indices = arrayrange(len(point)) |
---|
| 1561 | for i in res: |
---|
| 1562 | outside_indices = compress(outside_indices != i, outside_indices) |
---|
| 1563 | return outside_indices |
---|
| 1564 | |
---|
| 1565 | def inside_polygon_c(point, polygon, closed = True, verbose = False): |
---|
| 1566 | #FIXME: Obsolete |
---|
| 1567 | """Determine whether points are inside or outside a polygon |
---|
| 1568 | |
---|
| 1569 | C-wrapper |
---|
| 1570 | """ |
---|
| 1571 | |
---|
| 1572 | from Numeric import array, Float, reshape, zeros, Int |
---|
| 1573 | |
---|
| 1574 | |
---|
| 1575 | if verbose: print 'Checking input to inside_polygon' |
---|
| 1576 | #Input checks |
---|
| 1577 | try: |
---|
| 1578 | point = array(point).astype(Float) |
---|
| 1579 | except: |
---|
| 1580 | msg = 'Point %s could not be converted to Numeric array' %point |
---|
| 1581 | raise msg |
---|
| 1582 | |
---|
| 1583 | try: |
---|
| 1584 | polygon = array(polygon).astype(Float) |
---|
| 1585 | except: |
---|
| 1586 | msg = 'Polygon %s could not be converted to Numeric array' %polygon |
---|
| 1587 | raise msg |
---|
| 1588 | |
---|
| 1589 | assert len(polygon.shape) == 2,\ |
---|
| 1590 | 'Polygon array must be a 2d array of vertices: %s' %polygon |
---|
| 1591 | |
---|
| 1592 | |
---|
| 1593 | assert polygon.shape[1] == 2,\ |
---|
| 1594 | 'Polygon array must have two columns: %s' %polygon |
---|
| 1595 | |
---|
| 1596 | |
---|
| 1597 | if len(point.shape) == 1: |
---|
| 1598 | one_point = True |
---|
| 1599 | points = reshape(point, (1,2)) |
---|
| 1600 | else: |
---|
| 1601 | one_point = False |
---|
| 1602 | points = point |
---|
| 1603 | |
---|
| 1604 | from util_ext import inside_polygon |
---|
| 1605 | |
---|
| 1606 | if verbose: print 'Allocating array for indices' |
---|
| 1607 | |
---|
| 1608 | indices = zeros( points.shape[0], Int ) |
---|
| 1609 | |
---|
| 1610 | if verbose: print 'Calling C-version of inside poly' |
---|
| 1611 | count = inside_polygon(points, polygon, indices, |
---|
| 1612 | int(closed), int(verbose)) |
---|
| 1613 | |
---|
| 1614 | if one_point: |
---|
| 1615 | return count == 1 #Return True if the point was inside |
---|
| 1616 | else: |
---|
| 1617 | if verbose: print 'Got %d points' %count |
---|
| 1618 | |
---|
| 1619 | return indices[:count] #Return those indices that were inside |
---|
| 1620 | |
---|