Changeset 834


Ignore:
Timestamp:
Feb 4, 2005, 1:58:09 PM (20 years ago)
Author:
ole
Message:

Fixed up dem conversions

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

Legend:

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

    r832 r834  
    66
    77All data is assumed to reside at vertex locations. 
     8
     9
     10Formats used within GANUGA:
     11
     12.sww: Netcdf format for storing model output
     13.xya: Either ASCII or NetCDF format for storing arbitrary points and associated attributed
     14.dem: NetCDF representation of regular DEM data
     15.tsh: ASCII format for storing meshes and associated boundary and region info
     16FIXME: What else
    817
    918"""
     
    790799    return cls(domain, mode)
    791800
    792 
    793 
    794801def dem2xya(filename, verbose=False):
    795     """Read Digitial Elevation model from the following NetCDF format (.asc)
     802    """Read Digitial Elevation model from the following NetCDF format (.dem)
    796803
    797804    Example:
     
    813820
    814821    root, ext = os.path.splitext(filename)
    815     fid = open(filename)
     822
     823    #Get NetCDF
     824    infile = NetCDFFile(filename, 'r')  #Open existing netcdf file for read
    816825
    817826    if verbose: print 'Reading DEM from %s' %filename
    818     lines = fid.readlines()
    819     fid.close()
    820 
    821     if verbose: print 'Got', len(lines), ' lines'
    822    
    823     ncols = int(lines[0].split()[1].strip())
    824     nrows = int(lines[1].split()[1].strip())
    825     xllcorner = float(lines[2].split()[1].strip())
    826     yllcorner = float(lines[3].split()[1].strip())
    827     cellsize = float(lines[4].split()[1].strip())
    828     NODATA_value = int(lines[5].split()[1].strip())
    829 
    830     assert len(lines) == nrows + 6
    831 
    832     netcdfname = root + '.xya'
    833     if verbose: print 'Store to NetCDF file %s' %netcdfname
     827   
     828    ncols = infile.ncols[0]
     829    nrows = infile.nrows[0]
     830    xllcorner = infile.xllcorner[0]
     831    yllcorner = infile.yllcorner[0]
     832    cellsize = infile.cellsize[0]
     833    NODATA_value = infile.NODATA_value[0]
     834    elevation = infile.variables['elevation']
     835
     836    #Get output file                               
     837    xyaname = root + '.xya'
     838    if verbose: print 'Store to NetCDF file %s' %xyaname
    834839    # NetCDF file definition
    835     fid = NetCDFFile(netcdfname, 'w')
     840    outfile = NetCDFFile(xyaname, 'w')
    836841       
    837842    #Create new file
    838     fid.institution = 'Geoscience Australia'
    839     fid.description = 'NetCDF xya format for compact and portable storage ' +\
     843    outfile.institution = 'Geoscience Australia'
     844    outfile.description = 'NetCDF xya format for compact and portable storage ' +\
    840845                      'of spatial point data'
    841846
    842     fid.ncols = ncols
    843     fid.nrows = nrows   
     847    outfile.ncols = ncols
     848    outfile.nrows = nrows   
    844849
    845850
    846851    # dimension definitions
    847     fid.createDimension('number_of_points', nrows*ncols)   
    848     fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt
    849     fid.createDimension('number_of_dimensions', 2) #This is 2d data
     852    outfile.createDimension('number_of_points', nrows*ncols)   
     853    outfile.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt
     854    outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    850855   
    851856
    852857    # variable definitions
    853     fid.createVariable('points', Float, ('number_of_points',
     858    outfile.createVariable('points', Float, ('number_of_points',
    854859                                         'number_of_dimensions'))
    855     fid.createVariable('attributes', Float, ('number_of_points',
     860    outfile.createVariable('attributes', Float, ('number_of_points',
    856861                                             'number_of_attributes'))
    857862
    858863    # Get handles to the variables
    859     points = fid.variables['points']
    860     attributes = fid.variables['attributes']
     864    points = outfile.variables['points']
     865    attributes = outfile.variables['attributes']
    861866
    862867    #Store data
    863868    #FIXME: Could be faster using array operations
    864     #FIXME: Perhaps the y dimension needs to be reversed
    865     #FIXME: x and y definitely needs to be swapped
    866     for i, line in enumerate(lines[6:]):
    867         fields = line.split()
     869    for i in range(nrows):
    868870        if verbose: print 'Processing row %d of %d' %(i, nrows)
    869871       
    870         x = i*cellsize           
    871         for j, field in enumerate(fields):
     872        y = (nrows-i)*cellsize           
     873        for j in range(ncols):
    872874            index = i*ncols + j
    873875           
    874             y = j*cellsize
     876            x = j*cellsize
    875877            points[index, :] = [x,y]
    876 
    877             z = float(field)                       
    878             attributes[index, 0] = z
    879 
    880     fid.close()
     878            attributes[index, 0] = elevation[i, j]           
     879
     880    infile.close()           
     881    outfile.close()
     882
     883
     884# def dem2xya(filename, verbose=False):
     885#     """Read Digitial Elevation model from the following ASCII format (.asc)
     886
     887#     Example:
     888
     889#     ncols         3121
     890#     nrows         1800
     891#     xllcorner     722000
     892#     yllcorner     5893000
     893#     cellsize      25
     894#     NODATA_value  -9999
     895#     138.3698 137.4194 136.5062 135.5558 ..........
     896
     897#     Convert to NetCDF xyz format (.xya)
     898#     """
     899
     900#     import os
     901#     from Scientific.IO.NetCDF import NetCDFFile
     902#     from Numeric import Float, arrayrange, concatenate   
     903
     904#     root, ext = os.path.splitext(filename)
     905#     fid = open(filename)
     906
     907#     if verbose: print 'Reading DEM from %s' %filename
     908#     lines = fid.readlines()
     909#     fid.close()
     910
     911#     if verbose: print 'Got', len(lines), ' lines'
     912   
     913#     ncols = int(lines[0].split()[1].strip())
     914#     nrows = int(lines[1].split()[1].strip())
     915#     xllcorner = float(lines[2].split()[1].strip())
     916#     yllcorner = float(lines[3].split()[1].strip())
     917#     cellsize = float(lines[4].split()[1].strip())
     918#     NODATA_value = int(lines[5].split()[1].strip())
     919
     920#     assert len(lines) == nrows + 6
     921
     922#     netcdfname = root + '.xya'
     923#     if verbose: print 'Store to NetCDF file %s' %netcdfname
     924#     # NetCDF file definition
     925#     fid = NetCDFFile(netcdfname, 'w')
     926       
     927#     #Create new file
     928#     fid.institution = 'Geoscience Australia'
     929#     fid.description = 'NetCDF xya format for compact and portable storage ' +\
     930#                       'of spatial point data'
     931
     932#     fid.ncols = ncols
     933#     fid.nrows = nrows   
     934
     935
     936#     # dimension definitions
     937#     fid.createDimension('number_of_points', nrows*ncols)   
     938#     fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt
     939#     fid.createDimension('number_of_dimensions', 2) #This is 2d data
     940   
     941
     942#     # variable definitions
     943#     fid.createVariable('points', Float, ('number_of_points',
     944#                                          'number_of_dimensions'))
     945#     fid.createVariable('attributes', Float, ('number_of_points',
     946#                                              'number_of_attributes'))
     947
     948#     # Get handles to the variables
     949#     points = fid.variables['points']
     950#     attributes = fid.variables['attributes']
     951
     952#     #Store data
     953#     #FIXME: Could be faster using array operations
     954#     #FIXME: Perhaps the y dimension needs to be reversed
     955#     #FIXME: x and y definitely needs to be swapped
     956#     for i, line in enumerate(lines[6:]):
     957#         fields = line.split()
     958#         if verbose: print 'Processing row %d of %d' %(i, nrows)
     959       
     960#         x = i*cellsize           
     961#         for j, field in enumerate(fields):
     962#             index = i*ncols + j
     963           
     964#             y = j*cellsize
     965#             points[index, :] = [x,y]
     966
     967#             z = float(field)                       
     968#             attributes[index, 0] = z
     969
     970#     fid.close()
    881971
    882972                                     
    883 def asciidem2netcdf(filename, verbose=False):
     973def convert_dem_from_ascii2netcdf(filename, verbose=False):
    884974    """Read Digitial Elevation model from the following ASCII format (.asc)
    885975
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r832 r834  
    148148   
    149149def xya2rectangular(xya_name, M, N, alpha = DEFAULT_ALPHA,
    150                     verbose = False, reduction = 1):
     150                    verbose = False, reduction = 1, format = 'netcdf'):
    151151    """Fits attributes from xya file to MxN rectangular mesh
    152152   
     
    161161
    162162    if verbose: print 'Read xya'
    163     points, attributes, _ = util.read_xya(xya_name)
     163    points, attributes, _ = util.read_xya(xya_name, format)
    164164
    165165    #Reduce number of points a bit
  • inundation/ga/storm_surge/pyvolution/test_data_manager.py

    r832 r834  
    523523        ref_attributes = []
    524524        for i in range(6):
    525             x = i*25.0           
     525            y = (6-i)*25.0           
    526526            for j in range(5):
    527                 y = j*25.0
     527                x = j*25.0
    528528                z = x+2*y
    529529
     
    536536
    537537        #Convert to NetCDF xya
    538         asciidem2netcdf(filename)
    539         dem2xya(filename)
     538        convert_dem_from_ascii2netcdf(filename)
     539        dem2xya(root + '.dem')
    540540
    541541        #Check contents
  • inundation/ga/storm_surge/pyvolution/test_least_squares.py

    r820 r834  
    499499        fid.close()
    500500       
    501         points, triangles, boundary, attributes = xya2rectangular(FN, 4, 4)
     501        points, triangles, boundary, attributes =\
     502                xya2rectangular(FN, 4, 4, format = 'asc')
    502503       
    503        
    504        
    505 
    506504        z1 = [2, 4]
    507505        z2 = [4, 8]
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r832 r834  
    346346        fid.close()
    347347       
    348         points, attributes, _ = read_xya(FN)
     348        points, attributes, _ = read_xya(FN, format = 'asc')
    349349       
    350350        assert allclose(points, [ [0,1], [1,0], [1,1] ])
     
    364364        fid.close()
    365365       
    366         points, attributes, attribute_names = read_xya(FN)
     366        points, attributes, attribute_names = read_xya(FN, format = 'asc')
    367367
    368368        assert attribute_names[0] == 'a1'
  • inundation/ga/storm_surge/pyvolution/util.py

    r833 r834  
    286286            return res
    287287
    288 def read_xya(filename):
     288def read_xya(filename, format = 'netcdf'):
    289289    """Read simple xya file, possibly with a header in the first line, just
    290290    x y [attributes]
    291291    separated by whitespace
    292292
    293     If xya is a NetCDF file it will be read otherwise attemot to read as ASCII
    294     
     293    Format can be either ASCII or NetCDF
     294   
    295295    Return list of points, list of attributes and
    296296    attribute names if present otherwise None
     
    298298   
    299299    from Scientific.IO.NetCDF import NetCDFFile
    300     try:
     300
     301    if format.lower() == 'netcdf':
    301302        #Get NetCDF
    302303       
    303         #FIXME: How can we suppress error message if this fails?       
    304304        fid = NetCDFFile(filename, 'r')
    305305     
     
    310310        #Don't close - arrays are needed outside this function,
    311311        #alternatively take a copy here
    312     except:
     312    else:   
    313313        #Read as ASCII file assuming that it is separated by whitespace
    314314        fid = open(filename)
Note: See TracChangeset for help on using the changeset viewer.