Changeset 1416


Ignore:
Timestamp:
May 18, 2005, 9:55:42 AM (20 years ago)
Author:
duncan
Message:

working on throwing errors

Location:
inundation/ga/storm_surge/pmesh
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py

    r1401 r1416  
    1313Use the same delimiter for the attribute names and the data
    1414
     15An xya file can optionally end with
     16#geo reference
     1756
     18466600.0
     198644444.0
     20
     21When the 1st # is the zone,
     222nd # the xllcorner and
     233rd # the yllcorner
    1524The format for a Points dictionary is:
    1625
     
    6978from os.path import splitext
    7079
    71 from coordinate_transforms.geo_reference import Geo_reference
     80from coordinate_transforms.geo_reference import Geo_reference,TITLE
    7281
    7382import exceptions
     
    8089# (DSG-DSG) put an exception handler in import_triangulation/import_mesh_file
    8190# Think about exceptions before doing this!
    82 def mesh_file_to_mesh_dictionary(fileName):
     91def mesh_file_to_mesh_dictionary(file_name):
    8392    """Load a pmesh file.  Returning the mesh dictionary.
    8493    """
    8594    try:
    86         meshdic = import_mesh_file(fileName)
     95        meshdic = import_mesh_file(file_name)
    8796    except IOError, e:       
    88         msg = 'Could not open file %s ' %fileName
     97        msg = 'Could not open file %s ' %file_name
    8998        raise IOError, msg
    9099    return meshdic
     
    95104    read a mesh file, either .tsh or .msh
    96105   
    97     Note: This might throw a can't load file error
     106    Note: will throw an IOError if it can't load the file.
     107    Catch these!
    98108    """
    99109    try:
     
    126136    Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
    127137    """
    128     #FIXME DSG-DSG: automate
     138    #FIXME DSG-anyone: automate
    129139    if not mesh_dict.has_key('points'):
    130140        mesh_dict['points'] = []
     
    164174    #print "DSG************"
    165175   
    166     try:
    167         if (ofile[-4:] == ".tsh"):
    168             _write_tsh_file(ofile,mesh_dict)
    169         elif (ofile[-4:] == ".msh"):
    170             _write_msh_file(ofile, mesh_dict)
    171     except IOError, e:       
    172         msg = 'Could not write file %s ' %fileName
     176    if (ofile[-4:] == ".tsh"):
     177        _write_tsh_file(ofile,mesh_dict)
     178    elif (ofile[-4:] == ".msh"):
     179        _write_msh_file(ofile, mesh_dict)
     180    else:
     181        msg = 'Unknown file type %s ' %ofile
    173182        raise IOError, msg
    174 
    175183
    176184def _read_tsh_file(ofile):
     
    554562
    555563
    556 def export_mesh_file(ofile,mesh_dict):
    557     """
    558     write a file, ofile, with the format
    559    
    560     First line:  <# of vertices> <# of attributes>
    561     Following lines:  <vertex #> <x> <y> [attributes]
    562     One line:  <# of triangles>
    563     Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
    564     One line:  <# of segments>
    565     Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
    566     """
    567     if not mesh_dict.has_key('points'):
    568         mesh_dict['points'] = []
    569     if not mesh_dict.has_key('point_attributes'):
    570         mesh_dict['point_attributes'] = []
    571     if not mesh_dict.has_key('outline_segments'):
    572         mesh_dict['outline_segments'] = []
    573     if not mesh_dict.has_key('outline_segment_tags'):
    574         mesh_dict['outline_segment_tags'] = []
    575     if not mesh_dict.has_key('holes'):
    576         mesh_dict['holes'] = []
    577     if not mesh_dict.has_key('regions'):
    578         mesh_dict['regions'] = []
    579        
    580     if not mesh_dict.has_key('region_tags'):
    581         mesh_dict['region_tags'] = []
    582     if not mesh_dict.has_key('region_max_areas'):
    583         mesh_dict['region_max_areas'] = []
    584     if not mesh_dict.has_key('vertices'):
    585         mesh_dict['vertices'] = []
    586     if not mesh_dict.has_key('vertex_attributes'):
    587         mesh_dict['vertex_attributes'] = []
    588     if not mesh_dict.has_key('vertex_attribute_titles'):
    589         mesh_dict['vertex_attribute_titles'] = []
    590     if not mesh_dict.has_key('segments'):
    591         mesh_dict['segments'] = []
    592     if not mesh_dict.has_key('segment_tags'):
    593         mesh_dict['segment_tags'] = []
    594     if not mesh_dict.has_key('triangles'):
    595         mesh_dict['triangles'] = []
    596     if not mesh_dict.has_key('triangle_tags'):
    597         mesh_dict['triangle_tags'] = []
    598     if not mesh_dict.has_key('triangle_neighbors'):
    599         mesh_dict['triangle_neighbors'] = []
    600     #print "DSG************"
    601     #print "mesh_dict",mesh_dict
    602     #print "DSG************"
    603    
    604     try:
    605         if (ofile[-4:] == ".tsh"):
    606             _write_tsh_file(ofile,mesh_dict)
    607         elif (ofile[-4:] == ".msh"):
    608             _write_msh_file(ofile, mesh_dict)
    609     except IOError, e:       
    610         msg = 'Could not write file %s ' %fileName
    611         raise IOError, msg
    612 
    613564def _write_tsh_file(ofile,mesh_dict):
    614565            fd = open(ofile,'w')
     
    684635             " # <# of regions>, next lines <Region #> [Max Area] ...Mesh Regions..." + "\n")
    685636    for i,r in enumerate(regions):
    686         if region_max_areas[i] <= 0 :
    687             area = ""
    688         else:
    689             area = str(region_max_areas[i])
     637        area = str(region_max_areas[i])
    690638           
    691639        fd.write(str(i) + " " + area + "\n")
     
    695643        dict['geo_reference'].write_ASCII(fd)
    696644
    697 def _write_msh_file(filename, mesh):
     645def _write_msh_file(file_name, mesh):
    698646    """Write .msh NetCDF file   
    699647
     
    731679   
    732680    # NetCDF file definition
    733     outfile = NetCDFFile(filename, 'w')
     681    outfile = NetCDFFile(file_name, 'w')
    734682   
    735683    #Create new file
     
    886834    #Check contents
    887835    #Get NetCDF
     836   
     837    # see if the file is there.  Throw a QUIET IO error if it isn't
     838    fd = open(file_name,'r')
     839    fd.close()
     840
     841    #throws prints to screen if file not present
    888842    fid = NetCDFFile(file_name, 'r')
    889843
     
    1027981    #if not mesh_dict.has_key('points'):
    1028982    #    mesh_dict['points'] = []
    1029     try:
    1030         if (ofile[-4:] == ".xya"):
    1031             _write_xya_file(ofile, point_dict)
    1032         elif (ofile[-4:] == ".pts"):
    1033             _write_pts_file(ofile, point_dict)
    1034     except IOError, e:       
    1035         msg = 'Could not write file %s ' %fileName
    1036         raise IOError, msg
     983    if (ofile[-4:] == ".xya"):
     984        _write_xya_file(ofile, point_dict)
     985    elif (ofile[-4:] == ".pts"):
     986        _write_pts_file(ofile, point_dict)
     987    else:
     988        msg = 'Unknown file type %s ' %ofile
     989        raise IOError, msg
    1037990               
    1038991def import_points_file(ofile, delimiter = None, verbose = False):
    1039992    """
    1040     load an .xya or .pts file   
     993    load an .xya or .pts file
     994
     995    Note: will throw an IOError if it can't load the file.
     996    Catch these!
    1041997    """
    1042998   
     
    10551011                points_dict = _read_xya_file(fd, delimiter)
    10561012            fd.close()
    1057             points_dict['geo_reference'] = None
    1058         except IOError, e:       
    1059             msg = 'Could not open file %s ' %ofile
    1060             raise IOError, msg
    10611013        except SyntaxError:
     1014            fd.close()   
    10621015            msg = 'File could not be opened'
    10631016            raise IOError, msg
    10641017        except IndexError:
     1018            fd.close()   
     1019            msg = 'File could not be opened'
     1020            raise IOError, msg
     1021        except ValueError: # thrown by geo_ref, read ASCII
     1022            fd.close()   
    10651023            msg = 'File could not be opened'
    10661024            raise IOError, msg
     
    10691027        try:
    10701028            points_dict = _read_pts_file(ofile, verbose)       
    1071         except IOError, e:       
     1029        except IOError, e:   
    10721030            msg = 'Could not open file %s ' %ofile
    10731031            raise IOError, msg
     
    11521110    return dic.keys(), point_attributes
    11531111
    1154 def _read_pts_file(filename, verbose = False):
     1112def _read_pts_file(file_name, verbose = False):
    11551113    """Read .pts NetCDF file
    11561114   
     
    11671125    from Scientific.IO.NetCDF import NetCDFFile
    11681126
    1169     if verbose: print 'Reading ', filename
    1170     fid = NetCDFFile(filename, 'r')
     1127    if verbose: print 'Reading ', file_name
     1128
     1129   
     1130    # see if the file is there.  Throw a QUIET IO error if it isn't
     1131    fd = open(file_name,'r')
     1132    fd.close()
     1133
     1134    #throws prints to screen if file not present
     1135    fid = NetCDFFile(file_name, 'r')
    11711136
    11721137    point_atts = {} 
     
    12031168    return point_atts
    12041169
    1205 def _write_pts_file(filename, point_atts):
     1170def _write_pts_file(file_name, point_atts):
    12061171    """Write .pts NetCDF file   
    12071172
     
    12151180    point_atts2array(point_atts)
    12161181    # NetCDF file definition
    1217     outfile = NetCDFFile(filename, 'w')
     1182    outfile = NetCDFFile(file_name, 'w')
    12181183   
    12191184    #Create new file
     
    12431208   
    12441209def _read_xya_file(fd, delimiter):
    1245     lines = fd.readlines()
     1210    #lines = fd.readlines()
    12461211    points = []
    12471212    pointattributes = []
    1248     if len(lines) <= 1:
    1249         raise SyntaxError
    1250     title = lines.pop(0) # the first (title) line
     1213    #if len(lines) <= 1:
     1214    #    raise SyntaxError
     1215    title = fd.readline()
     1216    #title = lines.pop(0) # the first (title) line
    12511217    att_names = clean_line(title,delimiter)
    12521218
    12531219    att_dict = {}
    1254     for line in lines:
     1220    line = fd.readline()
     1221    numbers = clean_line(line,delimiter)
     1222    #for line in lines:
     1223    while len(numbers) > 1:
    12551224        #print "line >%s" %line
    1256         numbers = clean_line(line,delimiter)
     1225        #numbers = clean_line(line,delimiter)
    12571226        #print "numbers >%s<" %numbers
    1258         if len(numbers) < 2 and numbers != []:
     1227        #if len(numbers) < 2 and numbers != []:
     1228           
    12591229            # A line without two numbers
    12601230            # or a bad delimiter
    12611231            #FIXME dsg-dsg change error that is raised.
    1262             raise SyntaxError
     1232            #raise SyntaxError
    12631233        if numbers != []:
    12641234            try:
     
    12811251                        #attributes.append(float(num))
    12821252                        att_dict.setdefault(att_names[i],[]).append(float(num))
    1283                        
     1253                   
    12841254            except ValueError:
    12851255                raise SyntaxError
    1286             #pointattributes.append(attributes)
     1256        line = fd.readline()
     1257        numbers = clean_line(line,delimiter)
     1258       
     1259    if line == '':
     1260        # end of file
     1261        geo_reference = None
     1262    else:
     1263        geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
     1264   
    12871265    xya_dict = {}
    12881266    xya_dict['pointlist'] = array(points).astype(Float)
     
    12911269        att_dict[key] = array(att_dict[key]).astype(Float)
    12921270    xya_dict['attributelist'] = att_dict
    1293 
    1294    
    1295     ##print "xya_dict",xya_dict
     1271    xya_dict['geo_reference'] = geo_reference
     1272    #print "xya_dict",xya_dict
    12961273    return xya_dict
    12971274
     
    13231300    combined['geo_reference'] = dict1['geo_reference']
    13241301    return combined
    1325    
    1326 #FIXME (DSG) need an export_points_file method..               
     1302                 
    13271303def _write_xya_file( file_name, xya_dict, delimiter = ','):
    13281304    """
    1329     export a file, ofile, with the format
    1330    
    1331     First line: Title variable
    1332     Following lines:  <vertex #> <x> <y> [attributes]
    1333 
    1334     file_name - the name of the new file
    1335     xya_dict - point and point attribute info in a dictionary
    1336     title - info to write in the first line
    1337     """
    1338     #FIXME, move the test for this from meshharness to loadasciiharness
     1305    export a file, ofile, with the xya format
     1306   
     1307    """
    13391308    points = xya_dict['pointlist']
    13401309    pointattributes = xya_dict['attributelist']
     
    13581327                  + str(vert[1])
    13591328                  + attlist + "\n")
     1329   
     1330    # geo_reference info
     1331    if xya_dict.has_key('geo_reference') and \
     1332           not xya_dict['geo_reference'] is None:
     1333        xya_dict['geo_reference'].write_ASCII(fd)
    13601334    fd.close()
    13611335     
  • inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py

    r1394 r1416  
    163163        pass
    164164
    165        
     165  ############### .TSH ##########     
    166166    def test_export_mesh_file(self):
    167167        import os
     
    210210        os.remove(fileName)
    211211 
     212    def test_read_write_tsh_file(self):
     213        dict = self.dict.copy()
     214        fileName = tempfile.mktemp(".tsh")
     215        export_mesh_file(fileName,dict)
     216        loaded_dict = import_mesh_file(fileName)
     217        os.remove(fileName)
     218        dict = self.dict
     219        #print "*********************"
     220        #print dict
     221        #print "**loaded_dict*******************"
     222        #print loaded_dict
     223        #print "*********************"       
     224        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file')
     225       
     226    def test_read_write_tsh_fileII(self):
     227        dict = self.sparse_dict.copy()
     228        fileName = tempfile.mktemp(".tsh")
     229        export_mesh_file(fileName,dict)
     230        loaded_dict = import_mesh_file(fileName)
     231        dict = self.sparse_dict   
     232        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file')
     233        os.remove(fileName)
     234       
     235    def test_read_write_tsh_fileIII(self):
     236        dict = self.blank_dict.copy()
     237        fileName = tempfile.mktemp(".tsh")
     238        export_mesh_file(fileName,dict)
     239        loaded_dict = import_mesh_file(fileName)
     240        os.remove(fileName)
     241        dict = self.blank_dict
     242        #print "*********************"
     243        #print dict
     244        #print "**loaded_dict*******************"
     245        #print loaded_dict
     246        #print "*********************"       
     247        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII')
     248
     249    def test_read_write_tsh_file4(self):
     250        dict = self.seg_dict.copy()
     251        fileName = tempfile.mktemp(".tsh")
     252        export_mesh_file(fileName,dict)
     253        loaded_dict = import_mesh_file(fileName)
     254        os.remove(fileName)
     255        dict = self.seg_dict   
     256        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file4')
     257
     258    def test_read_write_tsh_file5(self):
     259        dict = self.triangle_tags_dict.copy()
     260        fileName = tempfile.mktemp(".tsh")
     261        export_mesh_file(fileName,dict)
     262        loaded_dict = import_mesh_file(fileName)
     263        dict = self.triangle_tags_dict
     264        #print "*********************"
     265        #print dict
     266        #print "**loaded_dict*******************"
     267        #print loaded_dict
     268        #print "*********************"       
     269        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file5') 
     270        os.remove(fileName)
     271
     272    def test_read_write_tsh_file6(self):
     273        dict = self.tri_dict.copy()
     274        fileName = tempfile.mktemp(".tsh")
     275        export_mesh_file(fileName,dict)
     276        loaded_dict = import_mesh_file(fileName)
     277        dict = self.tri_dict   
     278        self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file6') 
     279        os.remove(fileName)
     280       
     281########################## BAD .TSH ##########################
     282
     283    def test_load_bad_no_file_tsh(self):
     284        import os
     285        import tempfile
     286       
     287        fileName = tempfile.mktemp(".tsh")
     288        #print fileName
     289        try:
     290            dict = import_mesh_file(fileName)
     291        except IOError:
     292            pass
     293        else:
     294            self.failUnless(0 ==1,
     295                        'imaginary file did not raise error!')
     296         
     297    def test_read_write_tsh_file_bad(self):
     298        dict = self.tri_dict.copy()
     299        fileName = tempfile.mktemp(".xxx")
     300        try:
     301            export_mesh_file(fileName,dict)
     302        except IOError:
     303            pass
     304        else:
     305            self.failUnless(0 ==1,
     306                        'bad tsh file did not raise error!')       
     307       
     308    def test_import_tsh_bad(self):
     309        import os
     310        import tempfile
     311       
     312        fileName = tempfile.mktemp(".tsh")
     313        file = open(fileName,"w")
     314        #   this is  a bad tsh file
     315        file.write("elevn\n\
     3161.0 what \n\
     3170.0 the \n\
     3181.0 !!! \n")
     319        file.close()
     320        #print fileName
     321        try:
     322            dict = import_points_file(fileName,delimiter = ' ')
     323        except IOError:
     324            pass
     325        else:
     326            self.failUnless(0 ==1,
     327                        'bad tsh file did not raise error!')       
     328        os.remove(fileName)
     329
     330    def test_import_tsh3(self):
     331        import os
     332        import tempfile
     333       
     334        fileName = tempfile.mktemp(".tsh")
     335        file = open(fileName,"w")
     336        file.write("1.0 \n\
     337showme1.0 0.0 10.0 \n\
     3380.0 1.0\n\
     33913.0 \n")
     340        file.close()
     341        #print fileName
     342        try:
     343            dict = import_points_file(fileName,delimiter = ' ')
     344        except IOError:
     345            pass
     346        else:
     347            self.failUnless(0 ==1,
     348                        'bad tsh file did not raise error!')
     349       
     350        os.remove(fileName)         
     351
     352           
     353  ############### .MSH ##########
     354       
    212355    def test_read_write_msh_file(self):
    213356        dict = self.dict.copy()
     
    272415        loaded_dict = loadASCII._read_msh_file(fileName)
    273416        os.remove(fileName)
    274         dict = self.seg_dict
    275         #print "*********************"
     417        dict = self.triangle_tags_dict
     418        #print "msh_file5*********************"
    276419        #print dict
    277420        #print "**loaded_dict*******************"
     
    281424                                       
    282425       
    283     def test_read_write_msh_file5(self):
     426    def test_read_write_msh_file6(self):
    284427        dict = self.tri_dict.copy()
    285428        fileName = tempfile.mktemp(".msh")
     
    359502                         loaded_dict['geo_reference'] == None)   ,
    360503                        fail_string + ' failed!! Test geo_reference')
     504 
     505########################## BAD .MSH ##########################         
     506
     507    def test_load_bad_no_file_msh(self):
     508        import os
     509        import tempfile
     510       
     511        fileName = tempfile.mktemp(".msh")
     512        #print fileName
     513        try:
     514            dict = import_mesh_file(fileName)
     515        except IOError:
     516            pass
     517        else:
     518            self.failUnless(0 ==1,
     519                        'imaginary file did not raise error!')
     520           
     521    def test_import_msh_bad(self):
     522        import os
     523        import tempfile
     524       
     525        fileName = tempfile.mktemp(".msh")
     526        file = open(fileName,"w")
     527        #   this is  a bad tsh file
     528        file.write("elevn\n\
     5291.0 what \n\
     5300.0 the \n\
     5311.0 !!! \n")
     532        file.close()
     533        #print fileName
     534        try:
     535            dict = import_points_file(fileName,delimiter = ' ')
     536        except IOError:
     537            pass
     538        else:
     539            self.failUnless(0 ==1,
     540                        'bad msh file did not raise error!')       
     541        os.remove(fileName)         
     542   
     543  ###################### .XYA ##############################
     544       
     545    def test_export_xya_file(self):
     546        dict = {}
     547        att_dict = {}
     548        dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     549        att_dict['elevation'] = array([10.0, 0.0, 10.4])
     550        att_dict['brightness'] = array([10.0, 0.0, 10.4])
     551        dict['attributelist'] = att_dict
     552        dict['geo_reference'] = Geo_reference(56,1.9,1.9)
     553       
     554       
     555        fileName = tempfile.mktemp(".xya")
     556        export_points_file(fileName, dict)
     557        dict2 = import_points_file(fileName)
     558        #print "fileName",fileName
     559        os.remove(fileName)
     560        #print "dict2",dict2
     561       
     562        assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     563        assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
     564        answer = [10.0, 0.0, 10.4]
     565        assert allclose(dict2['attributelist']['brightness'], answer)
     566        #print "dict2['geo_reference']",dict2['geo_reference']
     567        self.failUnless(dict['geo_reference'] == dict2['geo_reference'],
     568                         'test_writepts failed. Test geo_reference')
     569
     570    def test_export_xya_file2(self):
     571        dict = {}
     572        att_dict = {}
     573        dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     574        att_dict['elevation'] = array([10.0, 0.0, 10.4])
     575        att_dict['brightness'] = array([10.0, 0.0, 10.4])
     576        dict['attributelist'] = att_dict
     577       
     578       
     579        fileName = tempfile.mktemp(".xya")
     580        export_points_file(fileName, dict)
     581        dict2 = import_points_file(fileName)
     582        #print "fileName",fileName
     583        os.remove(fileName)
     584        #print "dict2",dict2
     585       
     586        assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     587        assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
     588        answer = [10.0, 0.0, 10.4]
     589        assert allclose(dict2['attributelist']['brightness'], answer)
     590
     591       
     592    def test_loadxya(self):
     593        """
     594        comma delimited
     595        """
     596       
     597        fileName = tempfile.mktemp(".xya")
     598        file = open(fileName,"w")
     599        file.write("elevation  , speed \n\
     6001.0, 0.0, 10.0, 0.0\n\
     6010.0, 1.0, 0.0, 10.0\n\
     6021.0, 0.0, 10.4, 40.0\n")
     603        file.close()
     604        #print fileName
     605        dict = import_points_file(fileName,delimiter = ',')
     606        os.remove(fileName)
     607        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     608        assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
     609        assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
     610
     611    #FIXME - redundant test?
     612    def test_loadxy(self):
     613        """
     614        To test the mesh side of loading xya files.
     615        Not the loading of xya files
     616        """
     617        import os
     618        import tempfile
     619       
     620        fileName = tempfile.mktemp(".xya")
     621        file = open(fileName,"w")
     622        file.write("elevation speed \n\
     6231.0 0.0 10.0 0.0\n\
     6240.0 1.0 0.0 10.0\n\
     6251.0 0.0 10.4 40.0\n")
     626        file.close()
     627        #print fileName
     628        dict = import_points_file(fileName)
     629        os.remove(fileName)
     630        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     631        assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
     632        assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
     633
     634               
     635    def test_loadxya2(self):
     636        """
     637        space delimited
     638        """
     639        import os
     640        import tempfile
     641       
     642        fileName = tempfile.mktemp(".xya")
     643        file = open(fileName,"w")
     644        file.write("  elevation   speed \n\
     6451.0 0.0 10.0 0.0\n\
     6460.0 1.0 0.0 10.0\n\
     6471.0 0.0 10.4 40.0\n")
     648        file.close()
     649        #print fileName
     650        dict = import_points_file(fileName,delimiter = ' ')
     651        os.remove(fileName)
     652        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     653        assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
     654        assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
     655     
     656    def test_loadxya3(self):
     657        """
     658        space delimited
     659        """
     660        import os
     661        import tempfile
     662       
     663        fileName = tempfile.mktemp(".xya")
     664        file = open(fileName,"w")
     665        file.write("  elevation   speed \n\
     6661.0 0.0 10.0 0.0\n\
     6670.0 1.0 0.0 10.0\n\
     6681.0 0.0 10.4 40.0\n\
     669#geocrap\n\
     67056\n\
     67156.6\n\
     6723\n")
     673        file.close()
     674        #print fileName
     675        dict = import_points_file(fileName,delimiter = ' ')
     676        os.remove(fileName)
     677        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     678        assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
     679        assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
     680     
     681        geo_reference = Geo_reference(56, 56.6, 3.0)
     682       
     683        self.failUnless(geo_reference == dict['geo_reference'],
     684                         'test_writepts failed. Test geo_reference')
     685       
     686########################## BAD .XYA ##########################
     687 
     688    def test_loadxy_bad_no_file_xya(self):
     689        import os
     690        import tempfile
     691       
     692        fileName = tempfile.mktemp(".xya")
     693        #print fileName
     694        try:
     695            dict = import_points_file(fileName,delimiter = ' ')
     696        except IOError:
     697            pass
     698        else:
     699            self.failUnless(0 ==1,
     700                        'imaginary file did not raise error!')
     701 
     702    def test_read_write_points_file_bad(self):
     703        dict = self.tri_dict.copy()
     704        fileName = tempfile.mktemp(".xxx")
     705        try:
     706            export_points_file(fileName,dict)
     707        except IOError:
     708            pass
     709        else:
     710            self.failUnless(0 ==1,
     711                        'bad points file extension did not raise error!')
     712     
     713    def test_read_write_points_file_bad2(self):
     714        dict = {}
     715        att_dict = {}
     716        dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     717        att_dict['elevation'] = array([10.0, 0.0, 10.4])
     718        att_dict['brightness'] = array([10.0, 0.0, 10.4])
     719        dict['attributelist'] = att_dict
     720        dict['geo_reference'] = Geo_reference(56,1.9,1.9)
     721        try:
     722            export_points_file("e:yeah.xya",dict)
     723        except IOError:
     724            pass
     725        else:
     726            self.failUnless(0 ==1,
     727                        'bad points file extension did not raise error!')
     728                   
     729    def test_loadxy_bad(self):
     730        import os
     731        import tempfile
     732       
     733        fileName = tempfile.mktemp(".xya")
     734        file = open(fileName,"w")
     735        file.write("  elevation   \n\
     7361.0 0.0 10.0 0.0\n\
     7370.0 1.0 0.0 10.0\n\
     7381.0 0.0 10.4 40.0\n")
     739        file.close()
     740        #print fileName
     741        try:
     742            dict = import_points_file(fileName,delimiter = ' ')
     743        except IOError:
     744            pass
     745        else:
     746            self.failUnless(0 ==1,
     747                        'bad xya file did not raise error!')
     748        os.remove(fileName)
     749       
     750    def test_loadxy_bad2(self):
     751        import os
     752        import tempfile
     753       
     754        fileName = tempfile.mktemp(".xya")
     755        file = open(fileName,"w")
     756        file.write("elevation\n\
     7571.0 0.0 10.0 \n\
     7580.0 1.0\n\
     7591.0 \n")
     760        file.close()
     761        #print fileName
     762        try:
     763            dict = import_points_file(fileName,delimiter = ' ')
     764        except IOError:
     765            pass
     766        else:
     767            self.failUnless(0 ==1,
     768                        'bad xya file did not raise error!')   
     769        os.remove(fileName)
     770   
     771    def test_loadxy_bad3(self):
     772        """
     773        specifying wrong delimiter
     774        """
     775        import os
     776        import tempfile
     777       
     778        fileName = tempfile.mktemp(".xya")
     779        file = open(fileName,"w")
     780        file.write("  elevation  , speed \n\
     7811.0, 0.0, 10.0, 0.0\n\
     7820.0, 1.0, 0.0, 10.0\n\
     7831.0, 0.0, 10.4, 40.0\n")
     784        file.close()
     785        try:
     786            dict = import_points_file(fileName,delimiter = ' ')
     787        except IOError:
     788            pass
     789        else:
     790            self.failUnless(0 ==1,
     791                        'bad xya file did not raise error!')   
     792        os.remove(fileName)
     793     
     794    def test_loadxy_bad4(self):
     795        """
     796        specifying wrong delimiter
     797        """
     798        import os
     799        import tempfile
     800       
     801        fileName = tempfile.mktemp(".xya")
     802        file = open(fileName,"w")
     803        file.write("  elevation   speed \n\
     8041.0 0.0 10.0 0.0\n\
     8050.0 1.0 0.0 10.0\n\
     8061.0 0.0 10.4 40.0\n\
     807yeah")
     808        file.close()
     809        try:
     810            dict = import_points_file(fileName,delimiter = ' ')
     811        except IOError:
     812            pass
     813        else:
     814            self.failUnless(0 ==1,
     815                        'bad xya file did not raise error!')   
     816        os.remove(fileName)
     817 
     818    def test_loadxy_bad4(self):
     819        """
     820        specifying wrong delimiter
     821        """
     822        import os
     823        import tempfile
     824       
     825        fileName = tempfile.mktemp(".xya")
     826        file = open(fileName,"w")
     827        file.write("  elevation   speed \n\
     8281.0 0.0 10.0 0.0\n\
     8290.0 1.0 0.0 10.0\n\
     8301.0 0.0 10.4 40.0\n\
     831#geocrap")
     832        file.close()
     833        try:
     834            dict = import_points_file(fileName,delimiter = ' ')
     835        except IOError:
     836            pass
     837        else:
     838            self.failUnless(0 ==1,
     839                        'bad xya file did not raise error!')   
     840        os.remove(fileName)
     841
     842    def test_loadxy_bad5(self):
     843        """
     844        specifying wrong delimiter
     845        """
     846        import os
     847        import tempfile
     848       
     849        fileName = tempfile.mktemp(".xya")
     850        file = open(fileName,"w")
     851        file.write("  elevation   speed \n\
     8521.0 0.0 10.0 0.0\n\
     8530.0 1.0 0.0 10.0\n\
     8541.0 0.0 10.4 40.0\n\
     855#geocrap\n\
     856crap")
     857        file.close()
     858        try:
     859            dict = import_points_file(fileName,delimiter = ' ')
     860        except IOError:
     861            pass
     862        else:
     863            self.failUnless(0 ==1,
     864                        'bad xya file did not raise error!')   
     865        os.remove(fileName)       
     866  ############### .PTS ##########
    361867           
    362868    def test_loadpts(self):
     
    397903        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    398904        assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    399 
    400905 
     906    def test_writepts(self):
     907        dict = {}
     908        att_dict = {}
     909        dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     910        att_dict['elevation'] = array([10.0, 0.0, 10.4])
     911        att_dict['brightness'] = array([10.0, 0.0, 10.4])
     912        dict['attributelist'] = att_dict
     913        dict['geo_reference'] = Geo_reference(56,1.9,1.9)
     914       
     915       
     916        fileName = tempfile.mktemp(".pts")
     917        export_points_file(fileName, dict)
     918        dict2 = import_points_file(fileName)
     919        #print "fileName",fileName
     920        os.remove(fileName)
     921        #print "dict2",dict2
     922       
     923        assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     924        assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
     925        answer = [10.0, 0.0, 10.4]
     926        assert allclose(dict2['attributelist']['brightness'], answer)
     927
     928        #print "dict['geo_reference'] ",dict['geo_reference']
     929        #print "dict2['geo_reference']",dict2['geo_reference']
     930       
     931        self.failUnless(dict['geo_reference'] == dict2['geo_reference'],
     932                         'test_writepts failed. Test geo_reference')
     933       
     934 ########################## BAD .PTS ##########################         
     935
     936    def test_load_bad_no_file_pts(self):
     937        import os
     938        import tempfile
     939       
     940        fileName = tempfile.mktemp(".pts")
     941        #print fileName
     942        try:
     943            dict = import_points_file(fileName)
     944        except IOError:
     945            pass
     946        else:
     947            self.failUnless(0 ==1,
     948                        'imaginary file did not raise error!')
     949           
     950  ############### .PTS OTHER ##########
     951
    401952    def test_concatinate_attributelist(self):
    402953        dic = {}
     
    412963                         'test_concatinate_attributelist failed.')
    413964        assert allclose(block, [[4,2,5,3,1],[47,7,17,79,2]])
    414        
    415  
    416     def test_writepts(self):
    417         dict = {}
    418         att_dict = {}
    419         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    420         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    421         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    422         dict['attributelist'] = att_dict
    423         dict['geo_reference'] = Geo_reference(56,1.9,1.9)
    424        
    425        
    426         fileName = tempfile.mktemp(".pts")
    427         export_points_file(fileName, dict)
    428         dict2 = import_points_file(fileName)
    429         #print "fileName",fileName
    430         os.remove(fileName)
    431         #print "dict2",dict2
    432        
    433         assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    434         assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
    435         answer = [10.0, 0.0, 10.4]
    436         assert allclose(dict2['attributelist']['brightness'], answer)
    437 
    438         #print "dict['geo_reference'] ",dict['geo_reference']
    439         #print "dict2['geo_reference']",dict2['geo_reference']
    440        
    441         self.failUnless(dict['geo_reference'] == dict2['geo_reference'],
    442                          'test_writepts failed. Test geo_reference')
    443 
    444     def test_write_xya(self):
    445         dict = {}
    446         att_dict = {}
    447         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    448         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    449         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    450         dict['attributelist'] = att_dict
    451         dict['geo_reference'] = Geo_reference(56,1.9,1.9)
    452        
    453        
    454         fileName = tempfile.mktemp(".xya")
    455         export_points_file(fileName, dict)
    456         dict2 = import_points_file(fileName)
    457         #print "fileName",fileName
    458         os.remove(fileName)
    459         #print "dict2",dict2
    460        
    461         assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    462         assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
    463         answer = [10.0, 0.0, 10.4]
    464         assert allclose(dict2['attributelist']['brightness'], answer)
    465 
    466        
     965
    467966    def test_half_pts(self):
    468967        dict = {}
     
    488987       
    489988        #print "out_dict['pointlist']",out_dict #['pointlist']
    490         assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0],[10.0,10.0],[0.0, 10.0]])
     989        assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0],
     990                                               [10.0,10.0],[0.0, 10.0]])
    491991
    492992        self.failUnless(dict['attributelist']  == {},
     
    5441044        assert allclose(dict2['attributelist']['elevation'], [10.0])
    5451045        assert allclose(dict2['attributelist']['brightness'], [10.0])
    546        
    547     def test_loadxy3(self):
    548        
    549         fileName = tempfile.mktemp(".xya")
    550         file = open(fileName,"w")
    551         file.write("  elevation  , speed \n\
    552 1.0, 0.0, 10.0, 0.0\n\
    553 0.0, 1.0, 0.0, 10.0\n\
    554 1.0, 0.0, 10.4, 40.0\n")
    555         file.close()
    556         #print fileName
    557         dict = import_points_file(fileName,delimiter = ',')
    558         os.remove(fileName)
    559         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    560         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    561         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    562        
    563     def test_loadxy4(self):
    564         import os
    565         import tempfile
    566        
    567         fileName = tempfile.mktemp(".xya")
    568         file = open(fileName,"w")
    569         file.write("  elevation   speed \n\
    570 1.0 0.0 10.0 0.0\n\
    571 0.0 1.0 0.0 10.0\n\
    572 1.0 0.0 10.4 40.0\n")
    573         file.close()
    574         #print fileName
    575         dict = import_points_file(fileName,delimiter = ' ')
    576         os.remove(fileName)
    577         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    578         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    579         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    580      
    581     def test_loadxy5(self):
    582         import os
    583         import tempfile
    584        
    585         fileName = tempfile.mktemp(".xya")
    586         file = open(fileName,"w")
    587         file.write("  elevation   \n\
    588 1.0 0.0 10.0 0.0\n\
    589 0.0 1.0 0.0 10.0\n\
    590 1.0 0.0 10.4 40.0\n")
    591         file.close()
    592         #print fileName
    593         try:
    594             dict = import_points_file(fileName,delimiter = ' ')
    595         except IOError:
    596             pass
    597         else:
    598             self.failUnless(0 ==1,
    599                         'bad xya file did not raise error!')
    600        
    601         os.remove(fileName)
    602        
    603     def test_loadxy6(self):
    604         import os
    605         import tempfile
    606        
    607         fileName = tempfile.mktemp(".xya")
    608         file = open(fileName,"w")
    609         file.write("elevation\n\
    610 1.0 0.0 10.0 \n\
    611 0.0 1.0\n\
    612 1.0 \n")
    613         file.close()
    614         #print fileName
    615         try:
    616             dict = import_points_file(fileName,delimiter = ' ')
    617         except IOError:
    618             pass
    619         else:
    620             self.failUnless(0 ==1,
    621                         'bad xya file did not raise error!')
    622        
    623         os.remove(fileName)
    624          
    625  
    626     def test_import_tsh(self):
    627         import os
    628         import tempfile
    629        
    630         fileName = tempfile.mktemp(".tsh")
    631         file = open(fileName,"w")
    632         file.write("elevation\n\
    633 1.0 0.0 10.0 \n\
    634 0.0 1.0\n\
    635 1.0 \n")
    636         file.close()
    637         #print fileName
    638         try:
    639             dict = import_points_file(fileName,delimiter = ' ')
    640         except IOError:
    641             pass
    642         else:
    643             self.failUnless(0 ==1,
    644                         'bad tsh file did not raise error!')
    645        
    646         os.remove(fileName)
    647  
    648     def test_import_tsh2(self):
    649         import os
    650         import tempfile
    651        
    652         fileName = tempfile.mktemp(".msh")
    653         file = open(fileName,"w")
    654         file.write("1.0 \n\
    655 1.0 0.0 10.0 \n\
    656 0.0 1.0\n\
    657 13.0 \n")
    658         file.close()
    659         #print fileName
    660         try:
    661             dict = import_points_file(fileName,delimiter = ' ')
    662         except IOError:
    663             pass
    664         else:
    665             self.failUnless(0 ==1,
    666                         'bad tsh file did not raise error!')
    667        
    668         os.remove(fileName)         
    669  
    670     def test_import_tsh3(self):
    671         import os
    672         import tempfile
    673        
    674         fileName = tempfile.mktemp(".tsh")
    675         file = open(fileName,"w")
    676         file.write("1.0 \n\
    677 showme1.0 0.0 10.0 \n\
    678 0.0 1.0\n\
    679 13.0 \n")
    680         file.close()
    681         #print fileName
    682         try:
    683             dict = import_points_file(fileName,delimiter = ' ')
    684         except IOError:
    685             pass
    686         else:
    687             self.failUnless(0 ==1,
    688                         'bad tsh file did not raise error!')
    689        
    690         os.remove(fileName)         
    691  
     1046                     
    6921047#-------------------------------------------------------------
    6931048if __name__ == "__main__":
  • inundation/ga/storm_surge/pmesh/test_mesh.py

    r1401 r1416  
    863863                        'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed')
    864864
    865     def test_loadxy2(self):
    866         """
    867         To test the mesh side of loading xya files.
    868         Not the loading of xya files
    869         """
    870         import os
    871         import tempfile
    872        
    873         fileName = tempfile.mktemp(".xya")
    874         file = open(fileName,"w")
    875         file.write("elevation, speed \n\
    876 1.0, 0.0, 10.0, 0.0\n\
    877 0.0, 1.0, 0.0, 10.0\n\
    878 1.0, 0.0, 10.4, 40.0\n")
    879         file.close()
    880         #print fileName
    881         m = importMeshFromFile(fileName)
    882         os.remove(fileName)
    883        
    884865    def test_loadxy(self):
    885866        """
Note: See TracChangeset for help on using the changeset viewer.