Changeset 6360
- Timestamp:
- Feb 18, 2009, 2:44:41 PM (16 years ago)
- Location:
- branches/numpy/anuga
- Files:
-
- 24 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy/anuga/coordinate_transforms/geo_reference.py
r6304 r6360 9 9 import types, sys 10 10 from anuga.utilities.numerical_tools import ensure_numeric 11 from anuga.utilities.anuga_exceptions import ANUGAError, TitleError, ParsingError,\12 ShapeError11 from anuga.utilities.anuga_exceptions import ANUGAError, TitleError, \ 12 ParsingError, ShapeError 13 13 from anuga.config import netcdf_float, netcdf_int, netcdf_float32 14 14 … … 17 17 18 18 DEFAULT_ZONE = -1 19 TITLE = '#geo reference' + "\n" # this title is referred to in the test format19 TITLE = '#geo reference' + "\n" # this title is referred to in the test format 20 20 21 21 DEFAULT_PROJECTION = 'UTM' … … 23 23 DEFAULT_UNITS = 'm' 24 24 DEFAULT_FALSE_EASTING = 500000 25 DEFAULT_FALSE_NORTHING = 10000000 #Default for southern hemisphere 26 25 DEFAULT_FALSE_NORTHING = 10000000 # Default for southern hemisphere 26 27 28 ## 29 # @brief A class for ... 27 30 class Geo_reference: 28 31 """ 32 Attributes of the Geo_reference class: 33 .zone The UTM zone (default is -1) 34 .false_easting ?? 35 .false_northing ?? 36 .datum The Datum used (default is wgs84) 37 .projection The projection used (default is 'UTM') 38 .units The units of measure used (default metres) 39 .xllcorner The X coord of origin (default is 0.0 wrt UTM grid) 40 .yllcorner The y coord of origin (default is 0.0 wrt UTM grid) 41 .is_absolute ?? 42 29 43 """ 30 44 45 ## 46 # @brief Instantiate an instance of class Geo_reference. 47 # @param zone The UTM zone. 48 # @param xllcorner X coord of origin of georef. 49 # @param yllcorner Y coord of origin of georef. 50 # @param datum ?? 51 # @param projection The projection used (default UTM). 52 # @param units Units used in measuring distance (default m). 53 # @param false_easting ?? 54 # @param false_northing ?? 55 # @param NetCDFObject NetCDF file *handle* to write to. 56 # @param ASCIIFile ASCII text file *handle* to write to. 57 # @param read_title Title of the georeference text. 31 58 def __init__(self, 32 zone =DEFAULT_ZONE,33 xllcorner =0.0,34 yllcorner =0.0,35 datum =DEFAULT_DATUM,36 projection =DEFAULT_PROJECTION,37 units =DEFAULT_UNITS,38 false_easting =DEFAULT_FALSE_EASTING,39 false_northing = DEFAULT_FALSE_NORTHING,59 zone=DEFAULT_ZONE, 60 xllcorner=0.0, 61 yllcorner=0.0, 62 datum=DEFAULT_DATUM, 63 projection=DEFAULT_PROJECTION, 64 units=DEFAULT_UNITS, 65 false_easting=DEFAULT_FALSE_EASTING, 66 false_northing=DEFAULT_FALSE_NORTHING, 40 67 NetCDFObject=None, 41 68 ASCIIFile=None, … … 43 70 """ 44 71 input: 45 NetCDFObject - a handle to the netCDF file to be written to 72 NetCDFObject - a handle to the netCDF file to be written to 46 73 ASCIIFile - a handle to the text file 47 74 read_title - the title of the georeference text, if it was read in. … … 54 81 must be the default info, since ANUGA assumes it isn't 55 82 changing. 56 """ 83 """ 84 57 85 if zone is None: 58 86 zone = DEFAULT_ZONE 59 87 self.false_easting = false_easting 60 self.false_northing = false_northing 88 self.false_northing = false_northing 61 89 self.datum = datum 62 90 self.projection = projection 63 self.zone = zone 91 self.zone = zone 64 92 self.units = units 65 93 self.xllcorner = xllcorner 66 self.yllcorner = yllcorner 67 #self.is_absolute = num.allclose([self.xllcorner, self.yllcorner], 0) 68 94 self.yllcorner = yllcorner 95 #self.is_absolute = num.allclose([self.xllcorner, self.yllcorner], 0) 96 69 97 if NetCDFObject is not None: 70 98 self.read_NetCDF(NetCDFObject) 71 99 72 100 if ASCIIFile is not None: 73 101 self.read_ASCII(ASCIIFile, read_title=read_title) 74 102 103 # # Might be better to have this method instead of the following 3. 104 # def get_origin(self): 105 # return (self.zone, self.xllcorner, self.yllcorner) 106 107 ## 108 # @brief Get the X coordinate of the origin of this georef. 75 109 def get_xllcorner(self): 76 110 return self.xllcorner 77 111 112 ## 113 # @brief Get the Y coordinate of the origin of this georef. 78 114 def get_yllcorner(self): 79 115 return self.yllcorner 80 116 117 ## 118 # @brief Get the zone of this georef. 81 119 def get_zone(self): 82 120 return self.zone 83 121 122 ## 123 # @brief Write <something> to an open NetCDF file. 124 # @param outfile Handle to open NetCDF file. 84 125 def write_NetCDF(self, outfile): 85 outfile.xllcorner = self.xllcorner 126 outfile.xllcorner = self.xllcorner 86 127 outfile.yllcorner = self.yllcorner 87 128 outfile.zone = self.zone … … 90 131 outfile.false_northing = self.false_northing 91 132 92 outfile.datum = self.datum 133 outfile.datum = self.datum 93 134 outfile.projection = self.projection 94 135 outfile.units = self.units 95 136 137 ## 138 # @brief Read data from an open NetCDF file. 139 # @param infile Handle to open NetCDF file. 96 140 def read_NetCDF(self, infile): 97 141 self.xllcorner = infile.xllcorner[0] 98 self.yllcorner = infile.yllcorner[0] 142 self.yllcorner = infile.yllcorner[0] 99 143 self.zone = infile.zone[0] 100 144 101 102 145 # Fix some assertion failures 103 146 if isinstance(self.zone, num.ndarray) and self.zone.shape == (): 104 147 self.zone = self.zone[0] 105 if isinstance(self.xllcorner, num.ndarray) and self.xllcorner.shape == (): 148 if (isinstance(self.xllcorner, num.ndarray) and 149 self.xllcorner.shape == ()): 106 150 self.xllcorner = self.xllcorner[0] 107 if isinstance(self.yllcorner, num.ndarray) and self.yllcorner.shape == (): 151 if (isinstance(self.yllcorner, num.ndarray) and 152 self.yllcorner.shape == ()): 108 153 self.yllcorner = self.yllcorner[0] 109 154 … … 113 158 self.yllcorner.dtype.kind in num.typecodes['Integer']) 114 159 assert (self.zone.dtype.kind in num.typecodes['Integer']) 115 160 116 161 try: 117 162 self.false_easting = infile.false_easting[0] 118 163 self.false_northing = infile.false_northing[0] 119 120 self.datum = infile.datum 164 165 self.datum = infile.datum 121 166 self.projection = infile.projection 122 167 self.units = infile.units 123 168 except: 124 169 pass 125 if (self.false_easting != DEFAULT_FALSE_EASTING): 126 print "WARNING: False easting of %f specified." %self.false_easting 127 print "Default false easting is %f." %DEFAULT_FALSE_EASTING 170 171 if self.false_easting != DEFAULT_FALSE_EASTING: 172 print "WARNING: False easting of %f specified." % self.false_easting 173 print "Default false easting is %f." % DEFAULT_FALSE_EASTING 128 174 print "ANUGA does not correct for differences in False Eastings." 129 130 if (self.false_northing != DEFAULT_FALSE_NORTHING):131 print "WARNING: False northing of %f specified." \132 %self.false_northing133 print "Default false northing is %f." % DEFAULT_FALSE_NORTHING175 176 if self.false_northing != DEFAULT_FALSE_NORTHING: 177 print ("WARNING: False northing of %f specified." 178 % self.false_northing) 179 print "Default false northing is %f." % DEFAULT_FALSE_NORTHING 134 180 print "ANUGA does not correct for differences in False Northings." 135 136 if (self.datum.upper() != DEFAULT_DATUM.upper()): 137 print "WARNING: Datum of %s specified." \ 138 %self.datum 139 print "Default Datum is %s." %DEFAULT_DATUM 181 182 if self.datum.upper() != DEFAULT_DATUM.upper(): 183 print "WARNING: Datum of %s specified." % self.datum 184 print "Default Datum is %s." % DEFAULT_DATUM 140 185 print "ANUGA does not correct for differences in datums." 141 142 if (self.projection.upper() != DEFAULT_PROJECTION.upper()): 143 print "WARNING: Projection of %s specified." \ 144 %self.projection 145 print "Default Projection is %s." %DEFAULT_PROJECTION 186 187 if self.projection.upper() != DEFAULT_PROJECTION.upper(): 188 print "WARNING: Projection of %s specified." % self.projection 189 print "Default Projection is %s." % DEFAULT_PROJECTION 146 190 print "ANUGA does not correct for differences in Projection." 147 148 if (self.units.upper() != DEFAULT_UNITS.upper()): 149 print "WARNING: Units of %s specified." \ 150 %self.units 151 print "Default units is %s." %DEFAULT_UNITS 191 192 if self.units.upper() != DEFAULT_UNITS.upper(): 193 print "WARNING: Units of %s specified." % self.units 194 print "Default units is %s." % DEFAULT_UNITS 152 195 print "ANUGA does not correct for differences in units." 153 154 ### ASCII files with geo-refs are currently not used 196 197 ################################################################################ 198 # ASCII files with geo-refs are currently not used 199 ################################################################################ 200 201 ## 202 # @brief Write georef data to an open text file. 203 # @param fd Handle to open text file. 155 204 def write_ASCII(self, fd): 156 205 fd.write(TITLE) 157 fd.write(str(self.zone) + "\n") 158 fd.write(str(self.xllcorner) + "\n") 206 fd.write(str(self.zone) + "\n") 207 fd.write(str(self.xllcorner) + "\n") 159 208 fd.write(str(self.yllcorner) + "\n") 160 209 161 def read_ASCII(self, fd,read_title=None): 210 ## 211 # @brief Read georef data from an open text file. 212 # @param fd Handle to open text file. 213 def read_ASCII(self, fd, read_title=None): 162 214 try: 163 215 if read_title == None: 164 read_title = fd.readline() # remove the title line216 read_title = fd.readline() # remove the title line 165 217 if read_title[0:2].upper() != TITLE[0:2].upper(): 166 msg = 'File error. Expecting line: %s. Got this line: %s' \167 %(TITLE, read_title)168 raise TitleError, msg 218 msg = ('File error. Expecting line: %s. Got this line: %s' 219 % (TITLE, read_title)) 220 raise TitleError, msg 169 221 self.zone = int(fd.readline()) 170 222 self.xllcorner = float(fd.readline()) 171 223 self.yllcorner = float(fd.readline()) 172 224 except SyntaxError: 173 msg = 'File error. Got syntax error while parsing geo reference'174 175 225 msg = 'File error. Got syntax error while parsing geo reference' 226 raise ParsingError, msg 227 176 228 # Fix some assertion failures 177 229 if isinstance(self.zone, num.ndarray) and self.zone.shape == (): 178 230 self.zone = self.zone[0] 179 if isinstance(self.xllcorner, num.ndarray) and self.xllcorner.shape == (): 231 if (isinstance(self.xllcorner, num.ndarray) and 232 self.xllcorner.shape == ()): 180 233 self.xllcorner = self.xllcorner[0] 181 if isinstance(self.yllcorner, num.ndarray) and self.yllcorner.shape == (): 234 if (isinstance(self.yllcorner, num.ndarray) and 235 self.yllcorner.shape == ()): 182 236 self.yllcorner = self.yllcorner[0] 183 237 184 # useless asserts - see try/except code above 185 # assert (type(self.xllcorner) == types.FloatType) 186 # assert (type(self.yllcorner) == types.FloatType) 187 # assert (type(self.zone) == types.IntType) 188 189 190 def change_points_geo_ref(self, points, 191 points_geo_ref=None): 238 ################################################################################ 239 240 def change_points_geo_ref(self, points, points_geo_ref=None): 192 241 """ 193 242 Change the geo reference of a list or numeric array of points to … … 201 250 202 251 points = ensure_numeric(points, num.float) 203 252 204 253 if len(points.shape) == 1: 205 # One point has been passed254 # One point has been passed 206 255 msg = 'Single point must have two elements' 207 256 assert len(points) == 2, msg 208 257 points = num.reshape(points, (1,2)) 209 258 210 msg = 'Points array must be two dimensional.\n'211 msg += 'I got %d dimensions' %len(points.shape)259 msg = ('Points array must be two dimensional.\n' 260 'I got %d dimensions' % len(points.shape)) 212 261 assert len(points.shape) == 2, msg 213 262 214 msg = 'Input must be an N x 2 array or list of (x,y) values. ' 215 msg += 'I got an %d x %d array' %points.shape 216 assert points.shape[1] == 2, msg 217 218 219 # FIXME (Ole): Could also check if zone, xllcorner, yllcorner 220 # are identical in the two geo refs. 263 msg = ('Input must be an N x 2 array or list of (x,y) values. ' 264 'I got an %d x %d array' % points.shape) 265 assert points.shape[1] == 2, msg 266 267 # FIXME (Ole): Could also check if zone, xllcorner, yllcorner 268 # are identical in the two geo refs. 221 269 if points_geo_ref is not self: 222 270 # If georeferences are different 223 224 271 if not points_geo_ref is None: 225 272 # Convert points to absolute coordinates 226 points[:,0] += points_geo_ref.xllcorner 227 points[:,1] += points_geo_ref.yllcorner 228 273 points[:,0] += points_geo_ref.xllcorner 274 points[:,1] += points_geo_ref.yllcorner 275 229 276 # Make points relative to primary geo reference 230 points[:,0] -= self.xllcorner 277 points[:,0] -= self.xllcorner 231 278 points[:,1] -= self.yllcorner 232 279 233 280 # if we got a list, return a list 234 281 if is_list: 235 282 points = points.tolist() 236 283 237 284 return points 238 285 239 286 ## 287 # @brief Is this georef absolute? 288 # @return True if ??? 240 289 def is_absolute(self): 241 """Return True if xllcorner==yllcorner==0 indicating that points 242 in question are absolute. 243 """ 244 245 return num.allclose([self.xllcorner, self.yllcorner], 0) 246 247 248 ## 249 # @brief 250 # @param points 251 # @return 252 # @note 290 """Return True if xllcorner==yllcorner==0 291 indicating that points in question are absolute. 292 """ 293 294 return num.allclose([self.xllcorner, self.yllcorner], 0) 295 296 ## 297 # @brief Convert points to absolute points in this georef instance. 298 # @param points 299 # @return 300 # @note 253 301 def get_absolute(self, points): 254 302 """Given a set of points geo referenced to this instance, … … 259 307 # return points 260 308 309 # remember if we got a list 261 310 is_list = False 262 311 if type(points) == types.ListType: … … 266 315 if len(points.shape) == 1: 267 316 #One point has been passed 268 msg = 'Single point must have two elements'269 317 if not len(points) == 2: 270 raise ShapeError, msg 318 msg = 'Single point must have two elements' 319 raise ShapeError, msg 271 320 #points = reshape(points, (1,2)) 272 321 273 msg = 'Input must be an N x 2 array or list of (x,y) values. '274 msg += 'I got an %d x %d array' %points.shape275 322 if not points.shape[1] == 2: 276 raise ShapeError, msg 277 323 msg = ('Input must be an N x 2 array or list of (x,y) values. ' 324 'I got an %d x %d array' % points.shape) 325 raise ShapeError, msg 326 278 327 # Add geo ref to points 279 328 #if not self.is_absolute: 280 329 if not self.is_absolute(): 281 points[:,0] += self.xllcorner 330 points[:,0] += self.xllcorner 282 331 points[:,1] += self.yllcorner 283 332 #self.is_absolute = True 284 333 334 # if we got a list, return a list 285 335 if is_list: 286 336 points = points.tolist() 287 337 288 338 return points 289 339 290 340 ## 341 # @brief Convert points to relative measurement. 342 # @param points Points to convert to relative measurements. 343 # @return A set of points relative to the geo_reference instance. 291 344 def get_relative(self, points): 292 """ 293 Given a set of points in absolute UTM coordinates, 345 """Given a set of points in absolute UTM coordinates, 294 346 make them relative to this geo_reference instance, 295 347 return the points as relative values. … … 305 357 if len(points.shape) == 1: 306 358 #One point has been passed 307 msg = 'Single point must have two elements'308 359 if not len(points) == 2: 309 raise ShapeError, msg 360 msg = 'Single point must have two elements' 361 raise ShapeError, msg 310 362 #points = reshape(points, (1,2)) 311 363 312 313 msg = 'Input must be an N x 2 array or list of (x,y) values. '314 msg += 'I got an %d x %d array' %points.shape315 364 if not points.shape[1] == 2: 316 raise ShapeError, msg 317 318 365 msg = ('Input must be an N x 2 array or list of (x,y) values. ' 366 'I got an %d x %d array' % points.shape) 367 raise ShapeError, msg 368 319 369 # Subtract geo ref from points 320 370 if not self.is_absolute(): 321 points[:,0] -= self.xllcorner 371 points[:,0] -= self.xllcorner 322 372 points[:,1] -= self.yllcorner 323 373 324 374 # if we got a list, return a list 325 375 if is_list: 326 376 points = points.tolist() 327 377 328 378 return points 329 379 330 331 380 ## 381 # @brief ?? 382 # @param other ?? 332 383 def reconcile_zones(self, other): 333 384 if other is None: 334 385 other = Geo_reference() 335 if self.zone == other.zone or\ 336 self.zone == DEFAULT_ZONE and other.zone == DEFAULT_ZONE: 386 if (self.zone == other.zone or 387 self.zone == DEFAULT_ZONE and 388 other.zone == DEFAULT_ZONE): 337 389 pass 338 390 elif self.zone == DEFAULT_ZONE: 339 391 self.zone = other.zone 340 392 elif other.zone == DEFAULT_ZONE: 341 other.zone = self.zone 342 else: 343 msg = 'Geospatial data must be in the same '+\344 'ZONE to allow reconciliation. I got zone %d and %d'\345 %(self.zone, other.zone)393 other.zone = self.zone 394 else: 395 msg = ('Geospatial data must be in the same ' 396 'ZONE to allow reconciliation. I got zone %d and %d' 397 % (self.zone, other.zone)) 346 398 raise ANUGAError, msg 347 399 348 400 #def easting_northing2geo_reffed_point(self, x, y): 349 401 # return [x-self.xllcorner, y - self.xllcorner] … … 352 404 # return [x-self.xllcorner, y - self.xllcorner] 353 405 406 ## 407 # @brief Get origin of this geo_reference. 408 # @return (zone, xllcorner, yllcorner). 354 409 def get_origin(self): 355 410 return (self.zone, self.xllcorner, self.yllcorner) 356 411 412 ## 413 # @brief Get a string representation of this geo_reference instance. 357 414 def __repr__(self): 358 return "(zone=%i easting=%f, northing=%f)" %(self.zone, self.xllcorner, self.yllcorner) 359 360 def __cmp__(self,other): 415 return ('(zone=%i easting=%f, northing=%f)' 416 % (self.zone, self.xllcorner, self.yllcorner)) 417 418 ## 419 # @brief Compare two geo_reference instances. 420 # @param self This geo_reference instance. 421 # @param other Another geo_reference instance to compare against. 422 # @return 0 if instances have the same attributes, else 1. 423 # @note Attributes are: zone, xllcorner, yllcorner. 424 def __cmp__(self, other): 361 425 # FIXME (DSG) add a tolerence 362 426 if other is None: … … 371 435 return cmp 372 436 437 438 ## 439 # @brief Write a geo_reference to a NetCDF file (usually SWW). 440 # @param origin A georef instance or parameters to create a georef instance. 441 # @param outfile Path to file to write. 442 # @return A normalized geo_reference. 373 443 def write_NetCDF_georeference(origin, outfile): 374 """ 375 Write georeferrence info to a netcdf file, usually sww. 376 377 The origin can be a georef instance or parrameters for a geo_ref instance 444 """Write georeference info to a netcdf file, usually sww. 445 446 The origin can be a georef instance or parameters for a geo_ref instance 378 447 379 448 outfile is the name of the file to be written to. 380 449 """ 450 381 451 geo_ref = ensure_geo_reference(origin) 382 452 geo_ref.write_NetCDF(outfile) 383 453 return geo_ref 384 454 455 456 ## 457 # @brief Convert an object to a georeference instance. 458 # @param origin A georef instance or (zone, xllcorner, yllcorner) 459 # @return A georef object, or None if 'origin' was None. 385 460 def ensure_geo_reference(origin): 386 461 """ … … 391 466 effect code logic 392 467 """ 393 if isinstance(origin, Geo_reference): 468 469 if isinstance(origin, Geo_reference): 394 470 geo_ref = origin 395 471 elif origin is None: 396 472 geo_ref = None 397 473 else: 398 geo_ref = apply(Geo_reference, origin) 474 geo_ref = apply(Geo_reference, origin) 475 399 476 return geo_ref 400 477 401 478 402 479 #----------------------------------------------------------------------- 403 480 -
branches/numpy/anuga/coordinate_transforms/test_geo_reference.py
r6304 r6360 326 326 self.failUnless(isinstance(new_points, num.ndarray), 'failed') 327 327 self.failUnless(type(new_points) == type(points), 'failed') 328 msg = ('Second call of .get_absolute() returned %s\nexpected%s'328 msg = ('Second call of .get_absolute() returned\n%s\nexpected\n%s' 329 329 % (str(new_points), str(expected_new_points))) 330 330 self.failUnless(num.alltrue(expected_new_points == new_points), msg) … … 507 507 if __name__ == "__main__": 508 508 509 #suite = unittest.makeSuite(geo_referenceTestCase,'test')510 suite = unittest.makeSuite(geo_referenceTestCase,'test_get_absolute')509 suite = unittest.makeSuite(geo_referenceTestCase,'test') 510 #suite = unittest.makeSuite(geo_referenceTestCase,'test_get_absolute') 511 511 runner = unittest.TextTestRunner() #verbosity=2) 512 512 runner.run(suite) -
branches/numpy/anuga/coordinate_transforms/test_point.py
r2253 r6360 117 117 %(self.RSISE.BearingTo(self.Kobenhavn), B)) 118 118 119 #------------------------------------------------------------- 119 120 120 121 #-------------------------------------------------------------122 121 if __name__ == "__main__": 123 124 122 mysuite = unittest.makeSuite(TestCase,'test') 125 123 runner = unittest.TextTestRunner() 126 124 runner.run(mysuite) 127 125 128 129 130 131 132 133 134 -
branches/numpy/anuga/fit_interpolate/test_all.py
r2766 r6360 76 76 return unittest.TestSuite(map(load, modules)) 77 77 78 ################################################################################ 79 78 80 if __name__ == '__main__': 79 80 from os import sep81 82 #Attempt to compile all extensions83 #execfile('..' + sep + 'utilities' + sep + 'compile.py')84 85 #FIXME: Temporary measure86 #os.chdir('..' + sep + 'utilities')87 #execfile('compile.py')88 #os.chdir('..' + sep + 'pyvolution')89 90 #FIXME: Temporary measure91 #os.chdir('..' + sep + 'triangle')92 #execfile('compile.py')93 #os.chdir('..' + sep + 'pyvolution')94 95 #os.system('python compile.py')96 97 #print regressionTest()98 #unittest.main(defaultTest='regressionTest')99 100 81 suite = regressionTest() 101 82 runner = unittest.TextTestRunner() #(verbosity=2) -
branches/numpy/anuga/fit_interpolate/test_interpolate.py
r6304 r6360 919 919 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 920 920 921 922 921 point_coords_absolute = [[-2.0, 2.0], 923 [-1.0, 1.0],924 [0.0, 2.0],925 [1.0, 1.0],926 [2.0, 1.0],927 [0.0, 0.0],928 [1.0, 0.0],929 [0.0, -1.0],930 [-0.2, -0.5],931 [-0.9, -1.5],932 [0.5, -1.9],933 [3.0, 1.0]]934 935 geo = Geo_reference(57, 100, 500)922 [-1.0, 1.0], 923 [0.0, 2.0], 924 [1.0, 1.0], 925 [2.0, 1.0], 926 [0.0, 0.0], 927 [1.0, 0.0], 928 [0.0, -1.0], 929 [-0.2, -0.5], 930 [-0.9, -1.5], 931 [0.5, -1.9], 932 [3.0, 1.0]] 933 934 geo = Geo_reference(57, 100, 500) 936 935 point_coords = geo.change_points_geo_ref(point_coords_absolute) 937 point_coords = Geospatial_data(point_coords, geo_reference =geo)936 point_coords = Geospatial_data(point_coords, geo_reference=geo) 938 937 939 938 interp = Interpolate(vertices, triangles) 940 f = num.array([linear_function(vertices), 2*linear_function(vertices)])939 f = num.array([linear_function(vertices), 2*linear_function(vertices)]) 941 940 f = num.transpose(f) 942 #print "f",f941 print 'f=\n%s' % str(f) 943 942 z = interp.interpolate_block(f, point_coords) 944 answer = [linear_function(point_coords.get_data_points( \945 absolute = True)),946 2*linear_function(point_coords.get_data_points( \947 absolute = True)) ]943 answer = [linear_function(point_coords.get_data_points(absolute=True)), 944 2*linear_function(point_coords.get_data_points(absolute=True)) 945 ] 946 print 'After creation, answer=\n%s' % str(answer) 948 947 answer = num.transpose(answer) 949 #print "z",z 950 #print "answer",answer 951 assert num.allclose(z, answer) 948 print "z=\n%s" % str(z) 949 print "answer=\n%s" % str(answer) 950 msg = ('Expected z\n%s\nto be close to answer\n%s' 951 % (str(z), str(answer))) 952 assert num.allclose(z, answer), msg 952 953 953 954 z = interp.interpolate(f, point_coords, start_blocking_len = 2) 954 955 955 #print "z",z956 #print "answer",answer956 msg = ('Expected z\n%s\nto be close to answer\n%s' 957 % (str(z), str(answer))) 957 958 assert num.allclose(z, answer) 958 959 … … 1845 1846 #print "answer",answer 1846 1847 assert num.allclose(z, answer) 1847 1848 1849 #------------------------------------------------------------- 1848 1849 ################################################################################ 1850 1850 1851 if __name__ == "__main__": 1851 1852 suite = unittest.makeSuite(Test_Interpolate,'test') 1853 #suite = unittest.makeSuite(Test_Interpolate,'test_interpolate_geo_spatial') 1852 1854 runner = unittest.TextTestRunner() #verbosity=1) 1853 1855 runner.run(suite) 1854 1856 1855 1856 1857 1858 -
branches/numpy/anuga/fit_interpolate/test_search_functions.py
r6304 r6360 207 207 assert k == 1 208 208 209 210 #------------------------------------------------------------- 209 ################################################################################ 210 211 211 if __name__ == "__main__": 212 212 suite = unittest.makeSuite(Test_search_functions,'test') -
branches/numpy/anuga/geospatial_data/geospatial_data.py
r6304 r6360 20 20 TitleError, DEFAULT_ZONE, ensure_geo_reference, write_NetCDF_georeference 21 21 from anuga.coordinate_transforms.redfearn import convert_from_latlon_to_utm 22 from anuga.utilities.system_tools import clean_line 22 23 from anuga.utilities.anuga_exceptions import ANUGAError 23 24 from anuga.config import points_file_block_line_size as MAX_READ_LINES … … 1403 1404 points_dictionary['attributelist'], 1404 1405 geo_reference = geo) 1405 1406 1407 ##1408 # @brief Split a string into 'clean' fields.1409 # @param str The string to process.1410 # @param delimiter The delimiter string to split 'line' with.1411 # @return A list of 'cleaned' field strings.1412 # @note Any fields that were initially zero length will be removed.1413 # @note If a field contains '\n' it isn't zero length.1414 def clean_line(str, delimiter):1415 """Split string on given delimiter, remove whitespace from each field."""1416 1417 return [x.strip() for x in str.split(delimiter) if x != '']1418 1406 1419 1407 -
branches/numpy/anuga/geospatial_data/test_geospatial_data.py
r6304 r6360 1836 1836 1837 1837 ################################################################################ 1838 # Test the clean_line() utility function.1839 ################################################################################1840 1841 # helper routine to test clean_line()1842 def clean_line_test(self, instr, delim, expected):1843 result = clean_line(instr, delim)1844 self.failUnless(result == expected,1845 "clean_line('%s', '%s'), expected %s, got %s"1846 % (str(instr), str(delim), str(expected), str(result)))1847 1848 def test_clean_line_01(self):1849 self.clean_line_test('abc, ,,xyz,123', ',', ['abc', '', 'xyz', '123'])1850 1851 def test_clean_line_02(self):1852 self.clean_line_test(' abc , ,, xyz , 123 ', ',',1853 ['abc', '', 'xyz', '123'])1854 1855 def test_clean_line_03(self):1856 self.clean_line_test('1||||2', '|', ['1', '2'])1857 1858 def test_clean_line_04(self):1859 self.clean_line_test('abc, ,,xyz,123, ', ',',1860 ['abc', '', 'xyz', '123', ''])1861 1862 def test_clean_line_05(self):1863 self.clean_line_test('abc, ,,xyz,123, , ', ',',1864 ['abc', '', 'xyz', '123', '', ''])1865 1866 def test_clean_line_06(self):1867 self.clean_line_test(',,abc, ,,xyz,123, , ', ',',1868 ['abc', '', 'xyz', '123', '', ''])1869 1870 def test_clean_line_07(self):1871 self.clean_line_test('|1||||2', '|', ['1', '2'])1872 1873 def test_clean_line_08(self):1874 self.clean_line_test(' ,a,, , ,b,c , ,, , ', ',',1875 ['', 'a', '', '', 'b', 'c', '', '', ''])1876 1877 def test_clean_line_09(self):1878 self.clean_line_test('a:b:c', ':', ['a', 'b', 'c'])1879 1880 def test_clean_line_10(self):1881 self.clean_line_test('a:b:c:', ':', ['a', 'b', 'c'])1882 1883 # new version of function should leave last field if contains '\n'.1884 # cf. test_clean_line_10() above.1885 def test_clean_line_11(self):1886 self.clean_line_test('a:b:c:\n', ':', ['a', 'b', 'c', ''])1887 1838 1888 1839 if __name__ == "__main__": -
branches/numpy/anuga/load_mesh/loadASCII.py
r6304 r6360 53 53 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 54 54 """ 55 55 56 ##FIXME (DSG-DSG) Is the dict format mentioned above a list of a numeric array? 56 57 # Needs to be defined … … 58 59 59 60 from string import find, rfind 60 import numpy as num61 61 from os.path import splitext 62 import exceptions 62 63 63 64 from anuga.coordinate_transforms.geo_reference import Geo_reference, TITLE, \ 64 65 TitleError 65 66 from Scientific.IO.NetCDF import NetCDFFile67 66 from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a 68 67 from anuga.config import netcdf_float, netcdf_char, netcdf_int 69 68 70 import exceptions 69 from Scientific.IO.NetCDF import NetCDFFile 70 71 import numpy as num 71 72 72 73 … … 87 88 88 89 Note: will throw an IOError if it can't load the file. 89 Catch these!90 90 """ 91 91 … … 149 149 # @brief Read a mesh file into a dictionary. 150 150 # @param ofile Path of the file to read. 151 # @return Datadictionary from the mesh (.tsh) file.151 # @return Points dictionary from the mesh (.tsh) file. 152 152 def _read_tsh_file(ofile): 153 153 """Read the text file format for meshes""" … … 164 164 165 165 ## 166 # @brief ??166 # @brief Read triangulation data from a file, leave file open. 167 167 # @param fd An open descriptor of the file to read. 168 168 # @return A dictionary ... … … 264 264 265 265 ## 266 # @brief 266 # @brief Read a mesh outline file. 267 267 # @param fd An open descriptor of the file to read. 268 # @return A dictionary ...268 # @return A Points dictionary. 269 269 # @note If file has no mesh info, an empty dict will be returned. 270 270 def _read_outline(fd): … … 393 393 394 394 return meshDict 395 396 397 ##398 # @brief Clean up a line with delimited fields.399 # @param line The string to be cleaned.400 # @param delimiter String used a delimiter when splitting 'line'.401 # @return A list of delimited fields with white-space removed from ends.402 # @note Will also remove any field that was originally ''.403 def clean_line(line, delimiter):404 """Clean up a line - return fields with end space removed."""405 406 line = line.strip()407 numbers = line.split(delimiter)408 i = len(numbers) - 1409 while i >= 0:410 if numbers[i] == '':411 numbers.pop(i)412 else:413 numbers[i] = numbers[i].strip()414 415 i += -1416 417 return numbers418 395 419 396 … … 649 626 num.array(mesh['vertex_attribute_titles'], num.character) 650 627 mesh['segments'] = num.array(mesh['segments'], IntType) 628 print ("Before num.array(), mesh['segment_tags']=%s" 629 % str(mesh['segment_tags'])) 651 630 mesh['segment_tags'] = num.array(mesh['segment_tags'], num.character) 631 print ("After num.array(), mesh['segment_tags']=%s" 632 % str(mesh['segment_tags'])) 633 print "mesh['segment_tags'].shape=%s" % str(mesh['segment_tags'].shape) 652 634 mesh['triangles'] = num.array(mesh['triangles'], IntType) 653 635 mesh['triangle_tags'] = num.array(mesh['triangle_tags']) … … 967 949 fd.close() 968 950 969 970 951 ################################################################################ 971 952 # IMPORT/EXPORT POINTS FILES -
branches/numpy/anuga/load_mesh/test_all.py
r2405 r6360 76 76 return unittest.TestSuite(map(load, modules)) 77 77 78 ################################################################################ 79 78 80 if __name__ == '__main__': 79 80 81 from os import sep 81 82 -
branches/numpy/anuga/load_mesh/test_loadASCII.py
r6304 r6360 3 3 import tempfile 4 4 import unittest 5 6 5 import os 7 6 import tempfile 8 9 7 from os.path import splitext 10 8 11 9 import numpy as num 12 10 13 11 from anuga.load_mesh.loadASCII import * 14 12 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 18 16 class loadASCIITestCase(unittest.TestCase): 19 17 def setUp(self): 20 self.dict = {}18 self.dict = {} 21 19 self.dict['outline_segments'] = [(0, 1), (1, 2), (0, 2), (0, 3)] 22 20 self.dict['outline_segment_tags'] = ['50', '40', '30', '20'] 23 21 self.dict['holes'] = [(0.2, 0.6)] 24 self.dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2, 2]]25 self.dict['regions'] = [(0.3, 0.3), (0.3, 0.4)]22 self.dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2, 2]] 23 self.dict['regions'] = [(0.3, 0.3), (0.3, 0.4)] 26 24 self.dict['region_tags'] = ['1.3', 'yeah'] 27 25 self.dict['region_max_areas'] = [36.0, -7.1] 28 26 self.dict['points'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0)] 29 self.dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), 30 (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] 31 self.dict['triangles'] = [(3, 2, 4), (1, 0, 3), 32 (3, 4,1), (2, 3, 0)] 33 self.dict['segments'] = [(0, 1), (1, 4), (2, 0), 34 (0, 3), (4, 2)] 35 self.dict['triangle_tags'] = ['1.3', '1.3', 36 '1.3', '1.3'] 37 self.dict['vertex_attributes'] = [[1.2, 2.], [1.2, 2.], 38 [1.2, 2.], [1.2, 2.], [1.2, 3.]] 27 self.dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), 28 (1.0, 1.0), (2.0, 2.0)] 29 self.dict['triangles'] = [(3, 2, 4), (1, 0, 3), (3, 4,1), (2, 3, 0)] 30 self.dict['segments'] = [(0, 1), (1, 4), (2, 0), (0, 3), (4, 2)] 31 self.dict['triangle_tags'] = ['1.3', '1.3', '1.3', '1.3'] 32 self.dict['vertex_attributes'] = [[1.2, 2.], [1.2, 2.], [1.2, 2.], 33 [1.2, 2.], [1.2, 3.]] 39 34 self.dict['triangle_neighbors'] = [[-1, 2, 3], [3, 2, -1], 40 35 [-1, 1, 0], [1, -1, 0]] … … 42 37 self.dict['vertex_attribute_titles'] = ['bed elevation', 'height'] 43 38 self.dict['geo_reference'] = Geo_reference(56, 1.9, 1.9) 44 45 self.sparse_dict = {}39 40 self.sparse_dict = {} 46 41 self.sparse_dict['outline_segments'] = [] 47 42 self.sparse_dict['outline_segment_tags'] = [] … … 62 57 self.sparse_dict['segment_tags'] = [] 63 58 self.sparse_dict['vertex_attribute_titles'] = [] 64 65 self.blank_dict = {}59 60 self.blank_dict = {} 66 61 self.blank_dict['outline_segments'] = [] 67 62 self.blank_dict['outline_segment_tags'] = [] 68 63 self.blank_dict['holes'] = [] 69 64 self.blank_dict['points'] = [] 70 self.blank_dict['point_attributes'] = [] 65 self.blank_dict['point_attributes'] = [] 71 66 self.blank_dict['regions'] = [] 72 67 self.blank_dict['region_tags'] = [] … … 80 75 self.blank_dict['segment_tags'] = [] 81 76 self.blank_dict['vertex_attribute_titles'] = [] 82 83 self.tri_dict = {}77 78 self.tri_dict = {} 84 79 self.tri_dict['outline_segments'] = [[0, 1]] 85 80 self.tri_dict['outline_segment_tags'] = [''] 86 81 self.tri_dict['holes'] = [] 87 82 self.tri_dict['points'] = [(9, 8), (7, 8)] 88 self.tri_dict['point_attributes'] = [[], []]83 self.tri_dict['point_attributes'] = [[], []] 89 84 self.tri_dict['regions'] = [] 90 85 self.tri_dict['region_tags'] = [] … … 98 93 self.tri_dict['segment_tags'] = [''] 99 94 self.tri_dict['vertex_attribute_titles'] = [] 100 101 self.seg_dict = {}95 96 self.seg_dict = {} 102 97 self.seg_dict['outline_segments'] = [[0, 1]] 103 98 self.seg_dict['outline_segment_tags'] = [''] 104 99 self.seg_dict['holes'] = [] 105 100 self.seg_dict['points'] = [(9, 8), (7, 8)] 106 self.seg_dict['point_attributes'] = [[], []] 101 self.seg_dict['point_attributes'] = [[], []] 107 102 self.seg_dict['regions'] = [(5, 4)] 108 self.seg_dict['region_tags'] = [''] 103 self.seg_dict['region_tags'] = [''] 109 104 self.seg_dict['region_max_areas'] = [-999] 110 105 self.seg_dict['vertices'] = [(9, 8), (7, 8)] … … 116 111 self.seg_dict['segment_tags'] = [''] 117 112 self.seg_dict['vertex_attribute_titles'] = [] 118 119 self.reg_dict = {}113 114 self.reg_dict = {} 120 115 self.reg_dict['outline_segments'] = [[0, 1]] 121 116 self.reg_dict['outline_segment_tags'] = [''] 122 117 self.reg_dict['holes'] = [] 123 118 self.reg_dict['points'] = [(9, 8), (7, 8)] 124 self.reg_dict['point_attributes'] = [[], []] 119 self.reg_dict['point_attributes'] = [[], []] 125 120 self.reg_dict['regions'] = [(5, 4)] 126 121 self.reg_dict['region_tags'] = [''] … … 134 129 self.reg_dict['segment_tags'] = [''] 135 130 self.reg_dict['vertex_attribute_titles'] = [] 136 137 self.triangle_tags_dict = {}131 132 self.triangle_tags_dict = {} 138 133 self.triangle_tags_dict['outline_segments'] = [(0, 1), (1, 2), 139 134 (0, 2), (0, 3)] … … 143 138 self.triangle_tags_dict['point_attributes'] = [[5, 2], [4, 2], 144 139 [3, 2], [2,2]] 145 self.triangle_tags_dict['regions'] = [(0.3, 0.3), (0.3, 0.4)]140 self.triangle_tags_dict['regions'] = [(0.3, 0.3), (0.3, 0.4)] 146 141 self.triangle_tags_dict['region_tags'] = ['1.3', 'yeah'] 147 142 self.triangle_tags_dict['region_max_areas'] = [36.0, -7.1] … … 152 147 (2.0, 2.0)] 153 148 self.triangle_tags_dict['triangles'] = [(3, 2, 4), (1, 0, 3), 154 (3, 4, 1), (2, 3, 0)]149 (3, 4, 1), (2, 3, 0)] 155 150 self.triangle_tags_dict['segments'] = [(0, 1), (1, 4), (2, 0), 156 151 (0, 3), (4, 2)] … … 165 160 'height'] 166 161 self.triangle_tags_dict['geo_reference'] = Geo_reference(56, 1.9, 1.9) 167 162 168 163 def tearDown(self): 169 164 pass 170 165 171 ############### .TSH ########## 166 ############### .TSH ########## 172 167 def test_export_mesh_file(self): 173 import os174 import tempfile175 176 168 meshDict = self.dict 177 169 fileName = tempfile.mktemp('.tsh') 178 170 export_mesh_file(fileName, meshDict) 179 171 loadedDict = import_mesh_file(fileName) 180 172 181 173 self.failUnless(num.alltrue(num.array(meshDict['vertices']) == 182 174 num.array(loadedDict['vertices'])), … … 207 199 num.array(loadedDict['geo_reference'])), 208 200 'test_export_mesh_file failed. Test 9') 209 210 os.remove(fileName) 211 201 202 os.remove(fileName) 203 212 204 def test_read_write_tsh_file(self): 213 205 dict = self.dict.copy() … … 218 210 dict = self.dict 219 211 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file') 220 212 221 213 def test_read_write_tsh_fileII(self): 222 214 dict = self.sparse_dict.copy() … … 224 216 export_mesh_file(fileName, dict) 225 217 loaded_dict = import_mesh_file(fileName) 226 dict = self.sparse_dict 227 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_ msh_file')228 os.remove(fileName) 229 218 dict = self.sparse_dict 219 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_tsh_fileII') 220 os.remove(fileName) 221 230 222 def test_read_write_tsh_fileIII(self): 231 223 dict = self.blank_dict.copy() … … 235 227 os.remove(fileName) 236 228 dict = self.blank_dict 237 #print "*********************"238 #print dict239 #print "**loaded_dict*******************"240 #print loaded_dict241 #print "*********************"242 229 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_fileIII') 243 230 … … 248 235 loaded_dict = import_mesh_file(fileName) 249 236 os.remove(fileName) 250 dict = self.seg_dict 237 dict = self.seg_dict 251 238 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file4') 252 239 … … 257 244 loaded_dict = import_mesh_file(fileName) 258 245 dict = self.triangle_tags_dict 259 #print "*********************" 260 #print dict 261 #print "**loaded_dict*******************" 262 #print loaded_dict 263 #print "*********************" 264 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file5') 246 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file5') 265 247 os.remove(fileName) 266 248 … … 271 253 loaded_dict = import_mesh_file(fileName) 272 254 dict = self.tri_dict 273 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file6') 274 os.remove(fileName) 275 255 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file6') 256 os.remove(fileName) 257 276 258 ########################## BAD .TSH ########################## 277 259 278 260 def test_load_bad_no_file_tsh(self): 279 import os280 import tempfile281 282 261 fileName = tempfile.mktemp('.tsh') 283 262 try: … … 287 266 else: 288 267 self.failUnless(0 == 1, 'imaginary file did not raise error!') 289 268 290 269 def test_read_write_tsh_file_bad(self): 291 270 dict = self.tri_dict.copy() … … 296 275 pass 297 276 else: 298 self.failUnless(0 == 1, 'bad tsh file did not raise error!') 299 277 self.failUnless(0 == 1, 'bad tsh file did not raise error!') 278 300 279 def test_import_tsh_bad(self): 301 import os302 import tempfile303 304 280 fileName = tempfile.mktemp('.tsh') 305 281 file = open(fileName, 'w') … … 310 286 1.0 !!! \n') 311 287 file.close() 312 #print fileName313 288 try: 314 289 dict = import_mesh_file(fileName) … … 320 295 321 296 def test_import_tsh3(self): 322 import os323 import tempfile324 325 297 fileName = tempfile.mktemp('.tsh') 326 298 file = open(fileName, 'w') … … 336 308 else: 337 309 self.failUnless(0 == 1, 'bad tsh file did not raise error!') 338 339 os.remove(fileName) 340 341 310 os.remove(fileName) 311 342 312 ############### .MSH ########## 343 313 344 314 def test_read_write_msh_file(self): 345 315 dict = self.dict.copy() … … 349 319 os.remove(fileName) 350 320 dict = self.dict 351 #print "*********************"352 #print dict353 #print "**loaded_dict*******************"354 #print loaded_dict355 #print "*********************"356 321 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file') 357 322 … … 363 328 os.remove(fileName) 364 329 dict = self.sparse_dict 365 #print "*********************"366 #print dict367 #print "**loaded_dict*******************"368 #print loaded_dict369 #print "*********************"370 330 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_fileII') 371 331 372 332 def test_read_write_msh_fileIII(self): 373 333 dict = self.blank_dict.copy() … … 377 337 os.remove(fileName) 378 338 dict = self.blank_dict 379 #print "*********************"380 #print dict381 #print "**loaded_dict*******************"382 #print loaded_dict383 #print "*********************"384 339 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_fileIII') 385 340 386 341 def test_read_write_msh_file4(self): 387 342 dict = self.seg_dict.copy() … … 391 346 os.remove(fileName) 392 347 dict = self.seg_dict 393 #print "*********************"394 #print dict395 #print "**loaded_dict*******************"396 #print loaded_dict397 #print "*********************"398 348 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file4') 399 349 400 350 def test_read_write_msh_file5(self): 401 351 dict = self.triangle_tags_dict.copy() … … 405 355 os.remove(fileName) 406 356 dict = self.triangle_tags_dict 407 #print "msh_file5*********************"408 #print dict409 #print "**loaded_dict*******************"410 #print loaded_dict411 #print "*********************"412 357 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file5') 413 358 414 359 def test_read_write_msh_file6(self): 415 360 dict = self.tri_dict.copy() … … 419 364 os.remove(fileName) 420 365 dict = self.tri_dict 421 print "*********************"422 print dict423 print "**loaded_dict*******************"424 print loaded_dict425 print "*********************"426 366 self.check_mesh_dicts(loaded_dict, dict, 'test_read_write_msh_file6') 427 367 428 368 def check_mesh_dicts(self, loaded_dict, dict, fail_string): 429 369 assert num.allclose(num.array(loaded_dict['points']), … … 433 373 assert num.allclose(num.array(loaded_dict['outline_segments']), 434 374 num.array(dict['outline_segments'])) 435 375 436 376 self.failUnless(loaded_dict['outline_segment_tags'] == 437 377 dict['outline_segment_tags'], 438 378 fail_string + ' failed!! Test 4') 439 379 440 380 assert num.allclose(num.array(loaded_dict['regions']), 441 381 num.array(dict['regions'])) 442 382 443 383 self.failUnless(loaded_dict['region_tags'] == dict['region_tags'], 444 384 fail_string + ' failed!! Test 5') 445 385 446 386 assert num.allclose(num.array(loaded_dict['region_max_areas']), 447 387 num.array(dict['region_max_areas'])) 448 388 449 389 assert num.allclose(num.array(loaded_dict['holes']), 450 390 num.array(dict['holes'])) 451 391 452 392 assert num.allclose(num.array(dict['vertices']), 453 393 num.array(loaded_dict['vertices'])) 454 394 455 395 assert num.allclose(num.array(dict['triangles']), 456 396 num.array(loaded_dict['triangles'])) 457 397 458 398 assert num.allclose(num.array(dict['segments']), 459 399 num.array(loaded_dict['segments'])) 460 400 461 for ob, ldob in map(None, dict['triangle_tags'],462 401 for ob, ldob in map(None, dict['triangle_tags'], 402 loaded_dict['triangle_tags']): 463 403 self.failUnless(ob == ldob, 464 404 fail_string + ' failed!! Test triangle_tags') … … 470 410 dict['vertex_attributes'] == []), 471 411 fail_string + ' failed!! Test vertex_attributes') 472 412 473 413 assert num.allclose(num.array(dict['triangle_neighbors']), 474 414 num.array(loaded_dict['triangle_neighbors'])) 475 415 476 for seg, ldseg in map(None, dict['segment_tags'],416 for seg, ldseg in map(None, dict['segment_tags'], 477 417 loaded_dict['segment_tags']): 478 418 self.failUnless(seg == ldseg, 479 419 fail_string + ' failed!! Test 8') 480 try: 481 assert num.allclose(num.array(dict['vertex_attribute_titles']), 482 num.array(loaded_dict['vertex_attribute_titles'])) 483 except TypeError: 484 self.failUnless(num.alltrue(num.array(loaded_dict['vertex_attribute_titles']) == 485 num.array(dict['vertex_attribute_titles'])), 420 421 dict_array = num.array(dict['vertex_attribute_titles']) 422 loaded_dict_array = num.array(loaded_dict['vertex_attribute_titles']) 423 try: 424 assert num.allclose(dict_array, loaded_dict_array) 425 #except TypeError: #??# 426 except IndexError: 427 self.failUnless(num.alltrue(loaded_dict_array == dict_array), 486 428 fail_string + ' failed!! Test 8') 487 try: 429 430 try: 488 431 self.failUnless(loaded_dict['geo_reference'] == 489 432 dict['geo_reference'], 490 433 fail_string + ' failed!! Test geo_reference') 491 except KeyError: 492 self.failUnless(not dict.has_key('geo_reference' and 493 loaded_dict['geo_reference'] == None), 434 except KeyError: #??# 2 lines below?? 435 # self.failUnless(not dict.has_key('geo_reference' and 436 # loaded_dict['geo_reference'] == None), 437 # fail_string + ' failed!! Test geo_reference') 438 self.failUnless(not dict.has_key('geo_reference') and 439 loaded_dict['geo_reference'] == None, 494 440 fail_string + ' failed!! Test geo_reference') 495 496 ########################## BAD .MSH ########################## 441 442 ########################## BAD .MSH ########################## 497 443 498 444 def test_load_bad_no_file_msh(self): 499 import os500 import tempfile501 502 445 fileName = tempfile.mktemp('.msh') 503 446 try: … … 507 450 else: 508 451 self.failUnless(0 == 1, 'imaginary file did not raise error!') 509 452 510 453 def throws_error_2_screen_test_import_mesh_bad(self): 511 import os512 import tempfile513 514 454 fileName = tempfile.mktemp('.msh') 515 455 file = open(fileName, 'w') … … 526 466 else: 527 467 self.failUnless(0 == 1, 'bad msh file did not raise error!') 528 os.remove(fileName) 529 530 #------------------------------------------------------------- 468 os.remove(fileName) 469 470 ################################################################################ 471 531 472 if __name__ == '__main__': 532 533 473 suite = unittest.makeSuite(loadASCIITestCase,'test') 474 #suite = unittest.makeSuite(loadASCIITestCase,'test_read_write_msh_file6') 534 475 runner = unittest.TextTestRunner() #verbosity=2) 535 476 runner.run(suite) 536 477 -
branches/numpy/anuga/pmesh/test_all.py
r4663 r6360 76 76 return unittest.TestSuite(map(load, modules)) 77 77 78 ################################################################################ 79 78 80 if __name__ == '__main__': 79 81 # Assume everything is compiled -
branches/numpy/anuga/pmesh/test_mesh.py
r6304 r6360 2311 2311 yes = False 2312 2312 return yes 2313 2314 #------------------------------------------------------------- 2313 2314 ################################################################################ 2315 2315 2316 if __name__ == "__main__": 2316 2317 suite = unittest.makeSuite(meshTestCase,'test') 2318 #suite = unittest.makeSuite(meshTestCase,'test_import_mesh') 2317 2319 runner = unittest.TextTestRunner() #verbosity=2) 2318 2320 runner.run(suite) -
branches/numpy/anuga/pmesh/test_mesh_interface.py
r6304 r6360 70 70 # poly_point values are relative to the mesh geo-ref 71 71 # make them absolute 72 msg = ('Expected point (%s,%s) to be inside polygon %s' 73 % (str(poly_point.x+x), str(poly_point.y+y), 74 str(polygon_absolute))) 72 75 self.failUnless(is_inside_polygon([poly_point.x+x, poly_point.y+y], 73 76 polygon_absolute, closed=False), 74 'FAILED!') 77 msg) 78 # 'FAILED!') 75 79 76 80 # Assuming the order of the region points is known. … … 833 837 raise Exception, msg 834 838 835 836 #------------------------------------------------------------- 839 ################################################################################ 840 837 841 if __name__ == "__main__": 838 842 suite = unittest.makeSuite(TestCase,'test') -
branches/numpy/anuga/utilities/polygon.py
r6304 r6360 196 196 """ 197 197 198 # print 'polygon.py: 198, is_inside_polygon: point=%s' % str(point) 199 # print 'polygon.py: 198, is_inside_polygon: polygon=%s' % str(polygon) 200 198 201 indices = inside_polygon(point, polygon, closed, verbose) 199 202 … … 250 253 verbose=verbose) 251 254 255 # print 'polygon.py: 255, inside_polygon: indices=%s' % str(indices) 256 # print 'polygon.py: 255, inside_polygon: count=%s' % str(count) 252 257 # Return indices of points inside polygon 253 258 return indices[:count] -
branches/numpy/anuga/utilities/system_tools.py
r5921 r6360 246 246 # p1 = read_polygon(path) 247 247 248 249 250 248 249 ## 250 # @brief Split a string into 'clean' fields. 251 # @param str The string to process. 252 # @param delimiter The delimiter string to split 'line' with. 253 # @return A list of 'cleaned' field strings. 254 # @note Any fields that were initially zero length will be removed. 255 # @note If a field contains '\n' it isn't zero length. 256 def clean_line(str, delimiter): 257 """Split string on given delimiter, remove whitespace from each field.""" 258 259 return [x.strip() for x in str.split(delimiter) if x != ''] 260 261 -
branches/numpy/anuga/utilities/test_cg_solve.py
r6304 r6360 204 204 assert num.allclose(x,xe) 205 205 206 #------------------------------------------------------------- 206 ################################################################################ 207 207 208 if __name__ == "__main__": 208 suite = unittest.makeSuite(Test_CG_Solve, 'test')209 suite = unittest.makeSuite(Test_CG_Solve, 'test') 209 210 #runner = unittest.TextTestRunner(verbosity=2) 210 211 runner = unittest.TextTestRunner(verbosity=1) -
branches/numpy/anuga/utilities/test_data_audit.py
r6158 r6360 385 385 os.remove(data_filename2) 386 386 387 388 389 390 391 #------------------------------------------------------------- 387 ################################################################################ 388 392 389 if __name__ == "__main__": 393 390 suite = unittest.makeSuite(Test_data_audit, 'test') … … 395 392 runner.run(suite) 396 393 397 398 399 -
branches/numpy/anuga/utilities/test_numerical_tools.py
r6304 r6360 466 466 # t(num.array('abc', num.float), False) 467 467 468 469 #------------------------------------------------------------- 468 ################################################################################ 469 470 470 if __name__ == "__main__": 471 #suite = unittest.makeSuite(Test_Numerical_Tools,'test')472 suite = unittest.makeSuite(Test_Numerical_Tools,'test_is_')471 suite = unittest.makeSuite(Test_Numerical_Tools,'test') 472 #suite = unittest.makeSuite(Test_Numerical_Tools,'test_is_') 473 473 runner = unittest.TextTestRunner() 474 474 runner.run(suite) -
branches/numpy/anuga/utilities/test_polygon.py
r6304 r6360 1561 1561 point_spatial = Geospatial_data(point, geo_reference=points_geo_ref) 1562 1562 1563 assert is_inside_polygon(point_absolute, polygon_absolute) 1563 msg = ('point_absolute\n%s\nis not inside polygon_absolute\n%s' 1564 % (str(point_absolute), str(polygon_absolute))) 1565 assert is_inside_polygon(point_absolute, polygon_absolute), msg 1566 1567 msg = ('ensure_numeric(point_absolute)\n%s\n' 1568 'is not inside ensure_numeric(polygon_absolute)\n%s' 1569 % (str(ensure_numeric(ensure_numeric(point_absolute))), 1570 str(polygon_absolute))) 1564 1571 assert is_inside_polygon(ensure_numeric(point_absolute), 1565 ensure_numeric(polygon_absolute)) 1566 msg = ('point_absolute %s is not inside poly_spatial %s' 1572 ensure_numeric(polygon_absolute)), msg 1573 1574 msg = ('point_absolute\n%s\nis not inside poly_spatial\n%s' 1567 1575 % (str(point_absolute), str(poly_spatial))) 1568 1576 assert is_inside_polygon(point_absolute, poly_spatial), msg 1569 assert is_inside_polygon(point_spatial, poly_spatial) 1570 assert is_inside_polygon(point_spatial, polygon_absolute) 1571 assert is_inside_polygon(point_absolute, polygon_absolute) 1577 1578 msg = ('point_spatial\n%s\nis not inside poly_spatial\n%s' 1579 % (str(point_spatial), str(poly_spatial))) 1580 assert is_inside_polygon(point_spatial, poly_spatial), msg 1581 1582 msg = ('point_spatial\n%s\nis not inside polygon_absolute\n%s' 1583 % (str(point_spatial), str(polygon_absolute))) 1584 assert is_inside_polygon(point_spatial, polygon_absolute), msg 1585 1586 msg = ('point_absolute\n%s\nis not inside polygon_absolute\n%s' 1587 % (str(point_absolute), str(polygon_absolute))) 1588 assert is_inside_polygon(point_absolute, polygon_absolute), msg 1572 1589 1573 1590 def NOtest_decimate_polygon(self): … … 1686 1703 assert num.allclose(z, z_ref) 1687 1704 1688 1689 # ------------------------------------------------------------- 1705 ################################################################################ 1706 1690 1707 if __name__ == "__main__": 1691 #suite = unittest.makeSuite(Test_Polygon,'test')1692 suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geospatial')1708 suite = unittest.makeSuite(Test_Polygon,'test') 1709 #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geospatial') 1693 1710 runner = unittest.TextTestRunner() 1694 1711 runner.run(suite) -
branches/numpy/anuga/utilities/test_quad.py
r6304 r6360 258 258 # [[ 4., 1.], [ 5., 4.], [ 4., 4.]]]) 259 259 260 261 #------------------------------------------------------------- 260 ################################################################################ 261 262 262 if __name__ == "__main__": 263 264 263 mysuite = unittest.makeSuite(Test_Quad,'test') 265 264 #mysuite = unittest.makeSuite(Test_Quad,'test_interpolate_one_point_many_triangles') -
branches/numpy/anuga/utilities/test_sparse.py
r6304 r6360 199 199 assert num.allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]]) 200 200 201 202 203 #------------------------------------------------------------- 201 ################################################################################ 202 204 203 if __name__ == "__main__": 205 204 suite = unittest.makeSuite(Test_Sparse, 'test') -
branches/numpy/anuga/utilities/test_system_tools.py
r6304 r6360 135 135 assert checksum == ref_crc, msg 136 136 #print checksum 137 138 139 #------------------------------------------------------------- 137 138 ################################################################################ 139 # Test the clean_line() utility function. 140 ################################################################################ 141 142 # helper routine to test clean_line() 143 def clean_line_test(self, instr, delim, expected): 144 result = clean_line(instr, delim) 145 self.failUnless(result == expected, 146 "clean_line('%s', '%s'), expected %s, got %s" 147 % (str(instr), str(delim), str(expected), str(result))) 148 149 def test_clean_line_01(self): 150 self.clean_line_test('abc, ,,xyz,123', ',', ['abc', '', 'xyz', '123']) 151 152 def test_clean_line_02(self): 153 self.clean_line_test(' abc , ,, xyz , 123 ', ',', 154 ['abc', '', 'xyz', '123']) 155 156 def test_clean_line_03(self): 157 self.clean_line_test('1||||2', '|', ['1', '2']) 158 159 def test_clean_line_04(self): 160 self.clean_line_test('abc, ,,xyz,123, ', ',', 161 ['abc', '', 'xyz', '123', '']) 162 163 def test_clean_line_05(self): 164 self.clean_line_test('abc, ,,xyz,123, , ', ',', 165 ['abc', '', 'xyz', '123', '', '']) 166 167 def test_clean_line_06(self): 168 self.clean_line_test(',,abc, ,,xyz,123, , ', ',', 169 ['abc', '', 'xyz', '123', '', '']) 170 171 def test_clean_line_07(self): 172 self.clean_line_test('|1||||2', '|', ['1', '2']) 173 174 def test_clean_line_08(self): 175 self.clean_line_test(' ,a,, , ,b,c , ,, , ', ',', 176 ['', 'a', '', '', 'b', 'c', '', '', '']) 177 178 def test_clean_line_09(self): 179 self.clean_line_test('a:b:c', ':', ['a', 'b', 'c']) 180 181 def test_clean_line_10(self): 182 self.clean_line_test('a:b:c:', ':', ['a', 'b', 'c']) 183 184 # new version of function should leave last field if contains '\n'. 185 # cf. test_clean_line_10() above. 186 def test_clean_line_11(self): 187 self.clean_line_test('a:b:c:\n', ':', ['a', 'b', 'c', '']) 188 189 ################################################################################ 190 140 191 if __name__ == "__main__": 141 192 suite = unittest.makeSuite(Test_system_tools, 'test') … … 143 194 runner.run(suite) 144 195 145 146 147 -
branches/numpy/anuga/utilities/test_xml_tools.py
r6158 r6360 307 307 os.remove(tmp_name) 308 308 309 310 311 #------------------------------------------------------------- 309 ################################################################################ 310 312 311 if __name__ == "__main__": 313 312 suite = unittest.makeSuite(Test_xml_tools, 'test')
Note: See TracChangeset
for help on using the changeset viewer.