Changeset 1814


Ignore:
Timestamp:
Sep 9, 2005, 10:07:22 AM (20 years ago)
Author:
duncan
Message:

added more error checking to files loaded

Location:
inundation/pyvolution
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/interpolate_sww.py

    r1632 r1814  
    2626
    2727DEFAULT_QUANTITY = "depth"
     28
     29def  interpolate_sww2xya(sww_file, quantity_name,point_file_in,
     30                         point_file_out, display_errors = True):
     31    """
     32    This function catches exceptions.
     33    """
     34    try:
     35        interp = Interpolate_sww(sww_file, quantity_name)
     36        interp.interpolate_xya(point_file_in)
     37        interp.write_depth_xya(point_file_out)
     38    except IOError,e: #need to convert key error to ioerror
     39        if display_errors:
     40            print "Could not load bad file. ", e
     41        import sys; sys.exit()
     42
     43        # FIXME (DSG-DSG): how are bad quantities caught?
     44        #try:
     45        #    interp = Interpolate_sww(sww_file, quantity_name)
     46        #except KeyError:
     47        #    print "Error: Unknown quantity"
     48        #    sys.exit(1)
     49
     50        interp.interpolate_xya(point_file_in)
     51        interp.write_depth_xya(point_file_out)
     52
     53
    2854
    2955class Interpolate_sww:
     
    103129        #FIXME Have this reader as part of data_manager?
    104130
    105         from Scientific.IO.NetCDF import NetCDFFile             
     131        from Scientific.IO.NetCDF import NetCDFFile     
     132        import tempfile
     133        import sys
     134        import os
    106135           
    107136        #Check contents
    108137        #Get NetCDF
     138   
     139        # see if the file is there.  Throw a QUIET IO error if it isn't
     140        fd = open(file_name,'r')
     141        fd.close()
     142
     143        #throws prints to screen if file not present
     144       
     145        junk = tempfile.mktemp(".txt")
     146        fd = open(junk,'w')
     147        stdout = sys.stdout
     148        sys.stdout = fd
    109149        fid = NetCDFFile(file_name, 'r')
     150        sys.stdout = stdout
     151        fd.close()
     152        #clean up
     153        os.remove(junk)         
    110154     
    111155        # Get the variables
     
    162206            quantity_name = DEFAULT_QUANTITY   
    163207        #print "quantity",quantity
    164         try:
    165             interp = Interpolate_sww(sww_file, quantity_name)
    166         except KeyError:
    167             print "Error: Unknown quantity"
    168             sys.exit(1)
    169 
    170         interp.interpolate_xya(point_file_in)
    171         interp.write_depth_xya(point_file_out)
    172 
    173 
    174 
    175 
    176 
    177 
    178 
    179 
    180 
     208
     209
     210
     211
     212
     213
     214
     215
     216
  • inundation/pyvolution/least_squares.py

    r1787 r1814  
    6060                     data_origin = None,
    6161                     mesh_origin = None,
    62                      precrop = False):
     62                     precrop = False,
     63                     display_errors = True):
    6364    """
    6465    Given a mesh file (tsh) and a point attribute file (xya), fit
     
    8081                 concatinate_attributelist
    8182
    82     mesh_dict = import_mesh_file(mesh_file)
     83   
     84    try:
     85        mesh_dict = import_mesh_file(mesh_file)
     86    except IOError,e:
     87        if display_errors:
     88            print "Could not load bad file. ", e
     89        import sys; sys.exit()
    8390    vertex_coordinates = mesh_dict['vertices']
    8491    triangles = mesh_dict['triangles']
     
    97104    # load in the .pts file
    98105    try:
    99         point_dict = import_points_file(point_file,
    100                                       delimiter = ',',
    101                                       verbose=verbose)
    102     except SyntaxError,e:
    103         point_dict = import_points_file(point_file,
    104                                       delimiter = ' ',
    105                                       verbose=verbose)
     106        point_dict = import_points_file(point_file, verbose=verbose)
     107    except IOError,e:
     108        if display_errors:
     109            print "Could not load bad file. ", e
     110        import sys; sys.exit()
    106111
    107112    point_coordinates = point_dict['pointlist']
     
    149154    #FIXME (Ole): Remember to output mesh_origin as well
    150155    if verbose: print "exporting to file ",mesh_output_file
    151     export_mesh_file(mesh_output_file, mesh_dict)
    152 
     156
     157    try:
     158        export_mesh_file(mesh_output_file, mesh_dict)
     159    except IOError,e:
     160        if display_errors:
     161            print "Could not write file. ", e
     162        import sys; sys.exit()
    153163
    154164def fit_to_mesh(vertex_coordinates,
  • inundation/pyvolution/test_interpolate_sww.py

    r1396 r1814  
    226226        os.remove(point_file)
    227227
     228    def test_Interpolate_sww_errors(self):
     229        import tempfile
     230        import os
     231        try:
     232            interpolate_sww2xya('??ffd??', 'stage','yeah','yeah.x',
     233                                display_errors = False)
     234        except SystemExit:  pass
     235        else:
     236            self.failUnless(0 ==1,  'Bad file did not raise error!')
     237       
     238    def DISABLED_TEST_test_Interpolate_sww_errorsII(self):
     239        """
     240        THIS TEST HAS BEEN DISABLED, SINCE IT PRINTS TO SCREEN,
     241        but is should still work.  test it sometimes!
     242        """
     243        import tempfile
     244        import os
     245        import sys
     246       
     247        sww_file = tempfile.mktemp(".sww")
     248        fd = open(sww_file,'w')
     249        fd.write("unit testing a bad .sww file \n")
     250        fd.close()
     251       
     252        try:
     253            interpolate_sww2xya(sww_file, 'stage','yeah','yeah.x',
     254                                display_errors = False)
     255           
     256        except SystemExit:  pass
     257        else:
     258            self.failUnless(0 ==1,  'Bad file did not raise error!')       
     259        #clean up
     260        os.remove(sww_file)
     261       
     262    def test_Interpolate_sww_errorsIII(self):
     263        import time, os
     264        from Numeric import array, zeros, allclose, Float, concatenate, \
     265             transpose
     266        from Scientific.IO.NetCDF import NetCDFFile
     267        from load_mesh.loadASCII import  import_points_file, \
     268             concatinate_attributelist
     269
     270        self.domain.filename = 'datatest' + str(time.time())
     271        self.domain.format = 'sww'
     272        self.domain.smooth = True
     273        self.domain.reduction = mean
     274
     275        sww = get_dataobject(self.domain)
     276        sww.store_connectivity()
     277        sww.store_timestep('stage')
     278        self.domain.time = 2.
     279        sww.store_timestep('stage')
     280       
     281        try:
     282            interpolate_sww2xya(self.domain.filename,
     283                                'stage','yeah','yeah.x',
     284                                display_errors = False)
     285        except SystemExit:  pass
     286        else:
     287            self.failUnless(0 ==1,  'Bad file did not raise error!')       
     288        #clean up
     289        os.remove(sww.filename)
    228290#-------------------------------------------------------------
    229291if __name__ == "__main__":
    230292    suite = unittest.makeSuite(Test_Interpolate_sww,'test')
    231     runner = unittest.TextTestRunner()
     293    runner = unittest.TextTestRunner(verbosity=1)
    232294    runner.run(suite)
  • inundation/pyvolution/test_least_squares.py

    r1670 r1814  
    14351435        os.remove(point_file)
    14361436
     1437    def test_fit_to_mesh_file_errors(self):
     1438        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
     1439        import tempfile
     1440        import os
     1441
     1442        # create a .tsh file, no user outline
     1443        mesh_dic = {}
     1444        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
     1445        mesh_dic['triangles'] =  [[0, 2, 1]]
     1446        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
     1447        mesh_dic['triangle_tags'] = ['']
     1448        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
     1449        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
     1450        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
     1451        mesh_dic['segment_tags'] = ['external', 'external','external']
     1452        mesh_file = tempfile.mktemp(".tsh")
     1453        export_mesh_file(mesh_file,mesh_dic)
     1454
     1455        # create an .xya file
     1456        point_file = tempfile.mktemp(".xya")
     1457        fd = open(point_file,'w')
     1458        fd.write("elevation stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
     1459        fd.close()
     1460
     1461        mesh_output_file = "new_triangle.tsh"
     1462        try:
     1463            fit_to_mesh_file(mesh_file, point_file,
     1464                             mesh_output_file, display_errors = False)
     1465        except SystemExit:  pass
     1466        else:
     1467            self.failUnless(0 ==1,  'Bad file did not raise error!')       
     1468        #clean up
     1469        os.remove(mesh_file)
     1470        os.remove(point_file)
     1471
     1472    def test_fit_to_mesh_file_errorsII(self):
     1473        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
     1474        import tempfile
     1475        import os
     1476
     1477        # create a .tsh file, no user outline
     1478        mesh_file = tempfile.mktemp(".tsh")
     1479        fd = open(mesh_file,'w')
     1480        fd.write("unit testing a bad .tsh file \n")
     1481        fd.close()
     1482
     1483        # create an .xya file
     1484        point_file = tempfile.mktemp(".xya")
     1485        fd = open(point_file,'w')
     1486        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
     1487        fd.close()
     1488
     1489        mesh_output_file = "new_triangle.tsh"
     1490        try:
     1491            fit_to_mesh_file(mesh_file, point_file,
     1492                             mesh_output_file, display_errors = False)
     1493        except SystemExit:  pass
     1494        else:
     1495            self.failUnless(0 ==1,  'Bad file did not raise error!')       
     1496        #clean up
     1497        os.remove(mesh_file)
     1498        os.remove(point_file)
     1499
     1500    def test_fit_to_mesh_file_errorsIII(self):
     1501        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
     1502        import tempfile
     1503        import os
     1504
     1505        # create a .tsh file, no user outline
     1506        mesh_dic = {}
     1507        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
     1508        mesh_dic['triangles'] =  [[0, 2, 1]]
     1509        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
     1510        mesh_dic['triangle_tags'] = ['']
     1511        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
     1512        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
     1513        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
     1514        mesh_dic['segment_tags'] = ['external', 'external','external']
     1515        mesh_file = tempfile.mktemp(".tsh")
     1516        export_mesh_file(mesh_file,mesh_dic)
     1517
     1518        # create an .xya file
     1519        point_file = tempfile.mktemp(".xya")
     1520        fd = open(point_file,'w')
     1521        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
     1522        fd.close()
     1523
     1524        mesh_output_file = "e:/new_triangle.tsh"
     1525        try:
     1526            fit_to_mesh_file(mesh_file, point_file,
     1527                             mesh_output_file, display_errors = False)
     1528        except SystemExit:  pass
     1529        else:
     1530            self.failUnless(0 ==1,  'Bad file did not raise error!')       
     1531        #clean up
     1532        os.remove(mesh_file)
     1533        os.remove(point_file)
     1534       
    14371535    def test_fit_to_msh_netcdf_fileII(self):
    14381536        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
Note: See TracChangeset for help on using the changeset viewer.