Changeset 1394


Ignore:
Timestamp:
May 16, 2005, 11:06:22 AM (20 years ago)
Author:
duncan
Message:

refactoring dealing with point files

Location:
inundation/ga/storm_surge/pmesh/load_mesh
Files:
2 edited

Legend:

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

    r1377 r1394  
    55
    66for example:
    7 elevation
    8 0.6 0.7 4.9
    9 1.9 2.8 5
    10 2.7 2.4 5.2
     7elevation, friction
     80.6, 0.7, 4.9, 0.3
     91.9, 2.8, 5, 0.3
     102.7, 2.4, 5.2, 0.3
    1111
    1212The first two columns are always implicitly assumed to be x, y coordinates.
    13 
     13Use the same delimiter for the attribute names and the data
    1414
    1515The format for a Points dictionary is:
     
    9797    Note: This might throw a can't load file error
    9898    """
    99    
    100     if ofile[-4:]== ".tsh":
    101         dict = _read_tsh_file(ofile)
    102     elif ofile[-4:]== ".msh":
    103         dict = _read_msh_file(ofile)
    104     else:     
    105         msg = 'Extension %s is unknown' %ofile[-4:]
    106         raise IOError, msg       
     99    try:
     100        if ofile[-4:]== ".tsh":
     101            dict = _read_tsh_file(ofile)
     102        elif ofile[-4:]== ".msh":
     103            dict = _read_msh_file(ofile)
     104        else:     
     105            msg = 'Extension %s is unknown' %ofile[-4:]
     106            raise IOError, msg
     107    except SyntaxError:
     108            msg = 'File could not be opened'
     109            raise IOError, msg
     110    except IndexError:
     111            msg = 'File could not be opened'
     112            raise IOError, msg
     113       
    107114    return dict
    108115
     
    10521059            msg = 'Could not open file %s ' %ofile
    10531060            raise IOError, msg
     1061        except SyntaxError:
     1062            msg = 'File could not be opened'
     1063            raise IOError, msg
     1064        except IndexError:
     1065            msg = 'File could not be opened'
     1066            raise IOError, msg
    10541067       
    10551068    elif ofile[-4:]== ".pts":
     
    12601273                if len(att_names) != len(numbers):
    12611274                    fd.close()
    1262                     raise TitleAmountError
    1263                    
     1275                    # It might not be a problem with the title
     1276                    #raise TitleAmountError
     1277                    raise IOError
    12641278                for i,num in enumerate(numbers):
    12651279                    num.strip()
     
    13111325   
    13121326#FIXME (DSG) need an export_points_file method..               
    1313 def export_xya_file( file_name, xya_dict, title, delimiter = ','):
     1327def _write_xya_file( file_name, xya_dict, delimiter = ','):
    13141328    """
    13151329    export a file, ofile, with the format
     
    13241338    #FIXME, move the test for this from meshharness to loadasciiharness
    13251339    points = xya_dict['pointlist']
    1326     pointattributes = xya_dict['pointattributelist']
     1340    pointattributes = xya_dict['attributelist']
    13271341   
    13281342    fd = open(file_name,'w')
    1329    
    1330     fd.write(title+"\n")
     1343 
     1344    titlelist = ""
     1345    for title in pointattributes.keys():
     1346        titlelist = titlelist + title + delimiter
     1347    titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter
     1348    fd.write(titlelist+"\n")
    13311349    #<vertex #> <x> <y> [attributes]
    1332     for vert, vertatts in map(None, points, pointattributes):
     1350    for i,vert in enumerate( points):
     1351       
    13331352        attlist = ""
    1334         for att in vertatts:
    1335             attlist = attlist + str(att)+ delimiter
     1353        for att in pointattributes.keys():
     1354            attlist = attlist + str(pointattributes[att][i])+ delimiter
    13361355        attlist = attlist[0:-len(delimiter)] # remove the last delimiter
    13371356        attlist.strip()
  • inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py

    r1376 r1394  
    163163        pass
    164164
    165    
    166     def FUNCTION_REMOVEDtest_import_mesh(self):
    167        
    168         dict = self.dict
    169         fileName = tempfile.mktemp(".txt")
    170         fd = open(fileName,'w')
    171         loadASCII._write_ASCII_outline(fd,dict)
    172         fd.close()
    173         loaded_dict = import_mesh(fileName) #FIXME function names are wacky
    174         os.remove(fileName)
    175        
    176         #gen_dict =
    177         #print "*********************"
    178         #print dict
    179         #print "**loaded_dict*******************"
    180         #print loaded_dict
    181         #print "*********************"
    182 
    183        
    184         self.failUnless(array(loaded_dict['points'])  ==
    185                         array(dict['points']),
    186                          'test_import_mesh failed. Test 1')
    187         self.failUnless(array(loaded_dict['point_attributes'])  ==
    188                         array(dict['point_attributes']),
    189                          'test_import_mesh failed. Test 2')
    190         self.failUnless(array(loaded_dict['outline_segments'])  ==
    191                         array(dict['outline_segments']),
    192                          'test_import_mesh failed. Test 3')
    193         self.failUnless(array(loaded_dict['outline_segment_tags'])  ==
    194                         array(dict['outline_segment_tags']),
    195                          'test_import_mesh failed. Test 4')
    196         self.failUnless(array(loaded_dict['regions'])  ==
    197                         array(dict['regions']),
    198                          'test_import_mesh failed. Test 5')
    199         self.failUnless(array(loaded_dict['region_tags'])  ==
    200                         array(dict['region_tags']),
    201                          'test_import_mesh failed. Test 5')
    202         self.failUnless(array(loaded_dict['region_max_areas'])  ==
    203                         array(dict['region_max_areas']),
    204                          'test_import_mesh failed. Test 5')
    205 
    206         self.failUnless(array(loaded_dict['holes'])  ==
    207                         array(dict['holes']),
    208                          'test_import_mesh failed. Test 6')
    209165       
    210166    def test_export_mesh_file(self):
     
    486442                         'test_writepts failed. Test geo_reference')
    487443
     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
    488466       
    489467    def test_half_pts(self):
     
    615593        try:
    616594            dict = import_points_file(fileName,delimiter = ' ')
    617         except TitleAmountError:
     595        except IOError:
    618596            pass
    619597        else:
     
    622600       
    623601        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\
     6101.0 0.0 10.0 \n\
     6110.0 1.0\n\
     6121.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)
    624624         
     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\
     6331.0 0.0 10.0 \n\
     6340.0 1.0\n\
     6351.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\
     6551.0 0.0 10.0 \n\
     6560.0 1.0\n\
     65713.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\
     677showme1.0 0.0 10.0 \n\
     6780.0 1.0\n\
     67913.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)         
    625691 
    626692#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.