Changeset 4306


Ignore:
Timestamp:
Mar 21, 2007, 1:58:06 PM (17 years ago)
Author:
nick
Message:

Added get_data_from_file to util.py and add some tests

Location:
anuga_core/source/anuga/abstract_2d_finite_volumes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_util.py

    r4278 r4306  
    12441244
    12451245        check_list(['stage','xmomentum'])
     1246   
     1247    def test_get_data_from_file(self):
     1248#    from anuga.abstract_2d_finite_volumes.util import get_data_from_file
     1249       
     1250        import os
     1251       
     1252        fileName = tempfile.mktemp(".txt")
     1253#        print"filename",fileName
     1254        file = open(fileName,"w")
     1255        file.write("elevation, stage\n\
     12561.0, 3  \n\
     12570.0, 4 \n\
     12584.0, 3 \n\
     12591.0, 6 \n")
     1260        file.close()
     1261       
     1262        header,x = get_data_from_file(fileName)
     1263#        print 'x',x
     1264        os.remove(fileName)
     1265       
     1266        assert allclose(x[:,0], [1.0, 0.0,4.0, 1.0])
     1267       
     1268    def test_get_data_from_file1(self):
     1269#    from anuga.abstract_2d_finite_volumes.util import get_data_from_file
     1270       
     1271        import os
     1272       
     1273        fileName = tempfile.mktemp(".txt")
     1274#        print"filename",fileName
     1275        file = open(fileName,"w")
     1276        file.write("elevation stage\n\
     12771.3 3  \n\
     12780.0 4 \n\
     12794.5 3.5 \n\
     12801.0 6 \n")
     1281        file.close()
     1282       
     1283        header, x = get_data_from_file(fileName,separator_value=' ')
     1284        os.remove(fileName)
     1285#        x = get_data_from_file(fileName)
     1286#        print '1x',x[:,0]
     1287       
     1288        assert allclose(x[:,0], [1.3, 0.0,4.5, 1.0])
     1289
     1290     
    12461291
    12471292       
    12481293#-------------------------------------------------------------
    12491294if __name__ == "__main__":
    1250     suite = unittest.makeSuite(Test_Util,'test')
    1251     #suite = unittest.makeSuite(Test_Util,'test_add_directories')
     1295    #suite = unittest.makeSuite(Test_Util,'test')
     1296    suite = unittest.makeSuite(Test_Util,'test_get_data_from_file')
    12521297    runner = unittest.TextTestRunner()
    12531298    runner.run(suite)
  • anuga_core/source/anuga/abstract_2d_finite_volumes/util.py

    r4304 r4306  
    13851385    return dir
    13861386
    1387 def get_data_from_file(filename):
    1388     """ Read in data information from file
    1389     WARNING THERE IS NO UNIT TEST FOR THIS!
    1390     or the get_gauges_from_file()
     1387def get_data_from_file(filename,separator_value = ','):
     1388    """
     1389    Read in data information from file
     1390    NOTE: wont deal with columns with different lenghts and there must be
     1391    no blank lines at the end.
    13911392    """
    13921393    from os import sep, getcwd, access, F_OK, mkdir
     1394    from Numeric import array, resize,shape,Float
     1395    import string
    13931396    fid = open(filename)
    13941397    lines = fid.readlines()
     1398   
    13951399    fid.close()
    13961400   
    1397 #    seperated_value = ','
    1398    
    1399     time = []
    1400     speed = []
    1401     stage = []
    1402     momentum = []
    1403     elevation = []
    1404     line1 = lines[0]
    1405     line11 = line1.split(',')
    1406 #    east_index = len(line11)+1
    1407 #    north_index = len(line11)+1
    1408 #    name_index = len(line11)+1
    1409 #    elev_index = len(line11)+1
    1410 #    Time     Stage     Momentum     Speed     Elevation
    1411    
    1412     #read header to find the position (index) of each column of data
    1413     for i in range(len(line11)):
    1414         if line11[i].strip('\n').strip(' ').lower() == 'time':
    1415             time_index = i
    1416             print'time index', time_index
    1417         if line11[i].strip('\n').strip(' ').lower() == 'stage': stage_index = i
    1418         if line11[i].strip('\n').strip(' ').lower() == 'momentum': momentum_index = i
    1419         if line11[i].strip('\n').strip(' ').lower() == 'speed': speed_index = i
    1420         if line11[i].strip('\n').strip(' ').lower() == 'elevation': elevation_index = i
    1421         print'i',i
    1422     print'time',time_index,'stage',stage_index,'elevation',elevation_index
    1423    
    1424 
    1425     for line in lines[1:]:
    1426         fields = line.split(',')
    1427         if elevation_index < len(line11): elevation.append(float(fields[elevation_index]))
    1428         if time_index < len(line11): time.append(float(fields[time_index]))
    1429         if momentum_index < len(line11): momentum.append(float(fields[momentum_index]))
    1430         if speed_index < len(line11): speed.append(float(fields[speed_index]))
    1431         if stage_index < len(line11): stage.append(float(fields[stage_index]))
    1432        
    1433        
    1434     '''       
    1435         if east_index < len(line11) and north_index < len(line11):
    1436             gauges.append([float(fields[east_index]), float(fields[north_index])])
    1437         else:
    1438             msg = 'WARNING: %s does not contain location information' %(filename)
    1439             raise Exception, msg
    1440         if elev_index < len(line11): elev.append(float(fields[elev_index]))
    1441         if name_index < len(line11):
    1442             loc = fields[name_index]
    1443             gaugelocation.append(loc.strip('\n'))
    1444     '''
    1445     return time, stage, momentum, speed, elevation
    1446 
    1447 
    1448 
     1401    header_line = lines[0]
     1402    header_fields = header_line.split(separator_value)
     1403
     1404    #array to store data, number in there is to allow float...
     1405    #i'm sure there is a better way!
     1406    data=array([],typecode=Float)
     1407    data=resize(data,((len(lines)-1),len(header_fields)))
     1408#    print 'number of fields',range(len(header_fields))
     1409#    print 'number of lines',len(lines), shape(data)
     1410#    print'data',data[1,1],header_line
     1411
     1412    array_number = 0
     1413    line_number = 1
     1414    while line_number < (len(lines)):
     1415        for i in range(len(header_fields)):
     1416            #this get line below the header, explaining the +1
     1417            #and also the line_number can be used as the array index
     1418            fields = lines[line_number].split(separator_value)
     1419            #assign to array
     1420            data[array_number,i] = float(fields[i])
     1421           
     1422        line_number = line_number +1
     1423        array_number = array_number +1
     1424       
     1425    return header_fields, data
     1426
     1427
     1428
Note: See TracChangeset for help on using the changeset viewer.