Changeset 1661


Ignore:
Timestamp:
Aug 1, 2005, 12:11:53 PM (19 years ago)
Author:
ole
Message:

Brought xya2pts back

File:
1 edited

Legend:

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

    r1660 r1661  
    5151from Numeric import concatenate
    5252
    53 from coordinate_transforms.geo_reference import Geo_reference, DEFAULT_ZONE
     53from coordinate_transforms.geo_reference import Geo_reference
    5454
    5555def make_filename(s):
     
    921921    return cls(domain, mode)
    922922
     923def xya2pts(basename_in, basename_out=None, verbose=False,
     924            #easting_min=None, easting_max=None,
     925            #northing_min=None, northing_max=None,
     926            attribute_name = 'elevation'):
     927    """Read points data from ascii (.xya)
     928
     929    Example:
     930
     931              x(m)        y(m)        z(m)
     932         0.00000e+00  0.00000e+00  1.3535000e-01
     933         0.00000e+00  1.40000e-02  1.3535000e-01
     934
     935   
     936
     937    Convert to NetCDF pts format which is
     938
     939    points:  (Nx2) Float array
     940    elevation: N Float array
     941
     942    Only lines that contain three numeric values are processed
     943    """
     944
     945    import os
     946    from Scientific.IO.NetCDF import NetCDFFile
     947    from Numeric import Float, arrayrange, concatenate
     948
     949    root, ext = os.path.splitext(basename_in)
     950
     951    if ext == '': ext = '.xya'
     952
     953    #Get NetCDF
     954    infile = open(root + ext, 'r')  #Open existing xya file for read
     955
     956    if verbose: print 'Reading xya points from %s' %(root + ext)
     957
     958    points = []
     959    attribute = []
     960    for i, line in enumerate(infile.readlines()):
     961        fields = line.split()
     962
     963        try:
     964            assert len(fields) == 3
     965        except:   
     966            print 'WARNING: Line %d doesn\'t have 3 elements: %s' %(i, line)
     967
     968        try:
     969            x = float( fields[0] )
     970            y = float( fields[1] )
     971            z = float( fields[2] )           
     972        except:
     973            continue
     974
     975        points.append( [x, y] )
     976        attribute.append(z)
     977
     978
     979    #Get output file
     980    if basename_out == None:
     981        ptsname = root + '.pts'
     982    else:
     983        ptsname = basename_out + '.pts'
     984
     985    if verbose: print 'Store to NetCDF file %s' %ptsname
     986    # NetCDF file definition
     987    outfile = NetCDFFile(ptsname, 'w')
     988
     989    #Create new file
     990    outfile.institution = 'Geoscience Australia'
     991    outfile.description = 'NetCDF pts format for compact and '\
     992                          'portable storage of spatial point data'
     993   
     994
     995    #Georeferencing
     996    from coordinate_transforms.geo_reference import Geo_reference
     997    Geo_reference().write_NetCDF(outfile)
     998   
     999
     1000    outfile.createDimension('number_of_points', len(points))
     1001    outfile.createDimension('number_of_dimensions', 2) #This is 2d data
     1002
     1003    # variable definitions
     1004    outfile.createVariable('points', Float, ('number_of_points',
     1005                                             'number_of_dimensions'))
     1006    outfile.createVariable(attribute_name, Float, ('number_of_points',))
     1007
     1008    # Get handles to the variables
     1009    nc_points = outfile.variables['points']
     1010    nc_attribute = outfile.variables[attribute_name]
     1011
     1012    #Store data
     1013    nc_points[:, :] = points
     1014    nc_attribute[:] = attribute
     1015
     1016    infile.close()
     1017    outfile.close()
     1018   
    9231019def dem2pts(basename_in, basename_out=None, verbose=False,
    9241020            easting_min=None, easting_max=None,
     
    11441240    if origin is None:
    11451241
    1146         # get geo_reference
     1242        #Get geo_reference
    11471243        #sww files don't have to have a geo_ref
    11481244        try:
    11491245            geo_reference = Geo_reference(NetCDFObject=fid)
    11501246        except AttributeError, e:
    1151             geo_reference = Geo_reference(DEFAULT_ZONE,0,0)
     1247            geo_reference = Geo_reference() #Default georef object
    11521248
    11531249        xllcorner = geo_reference.get_xllcorner()
Note: See TracChangeset for help on using the changeset viewer.