Changeset 1903


Ignore:
Timestamp:
Oct 12, 2005, 2:11:25 PM (18 years ago)
Author:
duncan
Message:

removing / gutting duplication of methods reading xya files

Location:
inundation/pyvolution
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/least_squares.py

    r1897 r1903  
    217217
    218218def pts2rectangular(pts_name, M, N, alpha = DEFAULT_ALPHA,
    219                     verbose = False, reduction = 1, format = 'netcdf'):
     219                    verbose = False, reduction = 1):
    220220    """Fits attributes from pts file to MxN rectangular mesh
    221221
     
    229229    """
    230230
    231     import util, mesh_factory
    232 
     231    import  mesh_factory
     232    from load_mesh.loadASCII import import_points_file
     233   
    233234    if verbose: print 'Read pts'
    234     points, attributes = util.read_xya(pts_name, format)
     235    points_dict = import_points_file(pts_name)
     236    #points, attributes = util.read_xya(pts_name)
    235237
    236238    #Reduce number of points a bit
    237     points = points[::reduction]
    238     elevation = attributes['elevation']  #Must be elevation
     239    points = points_dict['pointlist'][::reduction]
     240    elevation = points_dict['attributelist']['elevation']  #Must be elevation
    239241    elevation = elevation[::reduction]
    240242
  • inundation/pyvolution/quantity.py

    r1754 r1903  
    587587
    588588        #Read from (NetCDF) file
    589         import util
    590         points, attributes = util.read_xya(filename)
     589        from load_mesh.loadASCII import import_points_file
     590        points_dict = import_points_file(filename)
     591        points = points_dict['pointlist']
     592        attributes = points_dict['attributelist']
    591593       
    592594        if attribute_name is None:
  • inundation/pyvolution/test_least_squares.py

    r1898 r1903  
    719719
    720720        points, triangles, boundary, attributes =\
    721                 pts2rectangular(FN, 4, 4, format = 'asc')
     721                pts2rectangular(FN, 4, 4)
    722722
    723723
  • inundation/pyvolution/test_util.py

    r1884 r1903  
    10191019
    10201020
    1021     def test_xya_ascii(self):
    1022         import time, os
    1023         FN = 'xyatest' + str(time.time()) + '.xya'
    1024         fid = open(FN, 'w')
    1025         fid.write('      %s %s %s\n' %('a1', 'a2', 'a3') )
    1026         fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) )
    1027         fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) )
    1028         fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) )
    1029         fid.close()
    1030 
    1031         points, attributes = read_xya(FN, format = 'asc')
    1032 
    1033         assert allclose(points, [ [0,1], [1,0], [1,1] ])
    1034         assert allclose(attributes['a1'], [10,30,40.2])
    1035         assert allclose(attributes['a2'], [20,20,40.3])
    1036         assert allclose(attributes['a3'], [30,10,40.4])
    1037 
    1038         os.remove(FN)
    1039 
    1040     def test_xya_ascii_w_names(self):
    1041         import time, os
    1042         FN = 'xyatest' + str(time.time()) + '.xya'
    1043         fid = open(FN, 'w')
    1044         fid.write('      %s %s %s\n' %('a1', 'a2', 'a3') )
    1045         fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) )
    1046         fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) )
    1047         fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) )
    1048         fid.close()
    1049 
    1050         points, attributes = read_xya(FN, format = 'asc')
    1051 
    1052         assert allclose(points, [ [0,1], [1,0], [1,1] ])
    1053 
    1054         assert allclose(attributes['a1'], [10,30,40.2])
    1055         assert allclose(attributes['a2'], [20,20,40.3])
    1056         assert allclose(attributes['a3'], [30,10,40.4])
    1057 
    1058 
    1059         os.remove(FN)
    1060 
    1061 
    1062 
    10631021
    10641022    #Polygon stuff
  • inundation/pyvolution/util.py

    r1884 r1903  
    379379
    380380
    381 def read_xya(filename, format = 'netcdf'):
     381def read_xya(filename):
    382382    """Read simple xya file, possibly with a header in the first line, just
    383383    x y [attributes]
     
    389389    attribute names if present otherwise None
    390390    """
     391    print "read_xya is obsolete.  Use import_points_file from load_mesh.loadASCII"
    391392    #FIXME: Probably obsoleted by similar function in load_ASCII
    392393    #FIXME: Name read_data_points (or see 'load' in pylab)
    393394
    394     from Scientific.IO.NetCDF import NetCDFFile
    395 
    396     if format.lower() == 'netcdf':
    397         #Get NetCDF
    398 
    399         fid = NetCDFFile(filename, 'r')
    400 
    401         # Get the variables
    402         points = fid.variables['points'][:]
    403         keys = fid.variables.keys()
    404         attributes = {}
    405         for key in keys:
    406             if key != 'points':
    407                 attributes[key] = fid.variables[key][:]
    408 
    409         fid.close()       
    410     else:
    411         #Read as ASCII file assuming that it is separated by whitespace
    412         fid = open(filename)
    413         lines = fid.readlines()
    414         fid.close()
    415 
    416         #Check if there is a header line
    417         fields = lines[0].strip().split()
    418         try:
    419             float(fields[0])
    420         except:
    421             #This must be a header line
    422             attribute_names = fields
    423             lines = lines[1:]
    424         else:
    425             attribute_names = ['elevation']  #HACK
    426 
    427         attributes = {}
    428         for key in attribute_names:
    429             attributes[key] = []
    430 
    431         points = []
    432         for line in lines:
    433             fields = line.strip().split()
    434             points.append( (float(fields[0]), float(fields[1])) )
    435             for i, key in enumerate(attribute_names):
    436                 attributes[key].append( float(fields[2+i]) )
    437 
    438     return points, attributes
     395   
     396    from load_mesh.loadASCII import import_points_file
     397
     398    points_dict = import_points_file(filename)
     399    return points_dict['pointlist'], points_dict['attributelist']
    439400
    440401
Note: See TracChangeset for help on using the changeset viewer.