Changeset 832


Ignore:
Timestamp:
Feb 2, 2005, 5:53:35 PM (20 years ago)
Author:
ole
Message:

Work towards conversions for dems

Location:
inundation/ga/storm_surge/pyvolution
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/data_manager.py

    r826 r832  
    793793
    794794def dem2xya(filename, verbose=False):
    795     """Read Digitial Elevation model from the following ASCII format (.asc)
     795    """Read Digitial Elevation model from the following NetCDF format (.asc)
    796796
    797797    Example:
     
    862862    #Store data
    863863    #FIXME: Could be faster using array operations
    864     #FIXME: I think I swapped x and y here -
    865     #       but this function is probably obsolete anyway
     864    #FIXME: Perhaps the y dimension needs to be reversed
     865    #FIXME: x and y definitely needs to be swapped
    866866    for i, line in enumerate(lines[6:]):
    867867        fields = line.split()
     
    881881
    882882                                     
    883 
    884 def dem2netcdf(filename, verbose=False):
     883def asciidem2netcdf(filename, verbose=False):
    885884    """Read Digitial Elevation model from the following ASCII format (.asc)
    886885
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r826 r832  
    161161
    162162    if verbose: print 'Read xya'
    163     points, attributes = util.read_xya(xya_name)
     163    points, attributes, _ = util.read_xya(xya_name)
    164164
    165165    #Reduce number of points a bit
  • inundation/ga/storm_surge/pyvolution/test_data_manager.py

    r826 r832  
    536536
    537537        #Convert to NetCDF xya
     538        asciidem2netcdf(filename)
    538539        dem2xya(filename)
    539540
    540541        #Check contents
    541542        #Get NetCDF
    542         fid = NetCDFFile(root+'.xya.', 'r')
     543        fid = NetCDFFile(root+'.xya', 'r')
    543544     
    544545        # Get the variables
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r820 r832  
    337337       
    338338
    339     def test_xya(self):
     339    def test_xya_ascii(self):
    340340        import time, os
    341341        FN = 'xyatest' + str(time.time()) + '.xya'
     
    346346        fid.close()
    347347       
    348         points, attributes = read_xya(FN)
     348        points, attributes, _ = read_xya(FN)
    349349       
    350350        assert allclose(points, [ [0,1], [1,0], [1,1] ])
     
    354354        os.remove(FN)
    355355       
     356    def test_xya_ascii_w_names(self):
     357        import time, os
     358        FN = 'xyatest' + str(time.time()) + '.xya'
     359        fid = open(FN, 'w')
     360        fid.write('      %s %s %s\n' %('a1', 'a2', 'a3') )       
     361        fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) )
     362        fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) )
     363        fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) )           
     364        fid.close()
     365       
     366        points, attributes, attribute_names = read_xya(FN)
     367
     368        assert attribute_names[0] == 'a1'
     369        assert attribute_names[1] == 'a2'
     370        assert attribute_names[2] == 'a3'       
     371        assert allclose(points, [ [0,1], [1,0], [1,1] ])
     372        assert allclose(attributes, [ [10,20,30], [30,20,10],
     373                [40.2,40.3,40.4] ])
     374       
     375        os.remove(FN)
    356376       
    357377    def test_polygon_function_constants(self):
  • inundation/ga/storm_surge/pyvolution/util.py

    r826 r832  
    5151
    5252
    53 
    54 #def point_on_line(point, line):
    5553def point_on_line(x, y, x0, y0, x1, y1):
    5654    """Determine whether a point is on a line segment
     
    6159       
    6260    """
    63    
    64     #FIXME: Could do with some C-optimisation         
    6561       
    6662    from Numeric import array, dot, allclose
     
    291287
    292288def read_xya(filename):
    293     """Read simple xya file, no header, just
     289    """Read simple xya file, possibly with a header in the first line, just
    294290    x y [attributes]
    295291    separated by whitespace
     
    297293    If xya is a NetCDF file it will be read otherwise attemot to read as ASCII
    298294   
    299     Return list of points and list of attributes
    300     """
    301 
    302 
     295    Return list of points, list of attributes and
     296    attribute names if present otherwise None
     297    """
     298   
    303299    from Scientific.IO.NetCDF import NetCDFFile
    304 
    305 
    306300    try:
    307         #Get NetCDF       
    308         fid = NetCDFFile(filename, 'r')
     301        #Get NetCDF
     302       
     303        #FIXME: How can we suppress error message if this fails?       
     304        fid = NetCDFFile(filename, 'r')
    309305     
    310306        # Get the variables
    311307        points = fid.variables['points']
    312308        attributes = fid.variables['attributes']
    313         ncols = fid.ncols
    314         nrows = fid.nrows
    315         #fid.close()  #Don't close - arrays are needed outside this function
     309        attribute_names = fid.variables['attribute_names']
     310        #Don't close - arrays are needed outside this function,
     311        #alternatively take a copy here
    316312    except:
    317         #Read as ASCII
     313        #Read as ASCII file assuming that it is separated by whitespace
    318314        fid = open(filename)
    319315        lines = fid.readlines()
     316        fid.close()
     317
     318        #Check if there is a header line
     319        fields = lines[0].strip().split()
     320        try:
     321            float(fields[0])
     322        except:
     323            #This must be a header line
     324            attribute_names = fields
     325            lines = lines[1:]
     326        else:
     327            attribute_names = None
    320328   
    321329        points = []     
     
    323331        for line in lines:
    324332            fields = line.strip().split()
    325             points.append( (float(fields[0]),  float(fields[1])) )
     333            points.append( (float(fields[0]), float(fields[1])) )
    326334            attributes.append( [float(z) for z in fields[2:] ] )
    327         nrows = ncols = None #FIXME: HACK   
    328        
    329     return points, attributes #, nrows[0], ncols[0]
     335       
     336    return points, attributes, attribute_names
    330337   
    331338
     
    407414    py = polygon[:,1]   
    408415
    409    
    410416    #Begin algorithm
    411417    indices = []
     
    420426           
    421427            #Check for case where point is contained in line segment   
    422             ##if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]):
    423428            if point_on_line(x, y, px[i], py[i], px[j], py[j]):
    424429                if closed:
Note: See TracChangeset for help on using the changeset viewer.