Changeset 1660


Ignore:
Timestamp:
Aug 1, 2005, 11:58:24 AM (19 years ago)
Author:
hitchman
Message:

introduced vector operations

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

Legend:

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

    r1657 r1660  
    1 """datamanager.py - input output for AnuGA
    2 
    3 
    4 This module takes care of reading and writing datafiles such as topograhies,
    5 model output, etc
    6 
    7 
    8 Formats used within AnuGA:
     1"""Functions to store and retrieve data for the Shallow Water Wave equation.
     2There are two kinds of data
     3
     4  1: Constant data: Vertex coordinates and field values. Stored once
     5  2: Variable data: Conserved quantities. Stored once per timestep.
     6
     7All data is assumed to reside at vertex locations.
     8
     9
     10Formats used within ANUGA:
    911
    1012.sww: Netcdf format for storing model output
     
    207209#Class for storing output to e.g. visualisation
    208210class Data_format_sww(Data_format):
    209     """Interface to native NetCDF format (.sww) for storing model output
    210 
    211     There are two kinds of data
    212 
    213     1: Constant data: Vertex coordinates and field values. Stored once
    214     2: Variable data: Conserved quantities. Stored once per timestep.
    215 
    216     All data is assumed to reside at vertex locations.
     211    """Interface to native NetCDF format (.sww)
    217212    """
    218213
     
    253248            if hasattr(domain, 'texture'):                 
    254249                fid.texture = domain.texture
    255             else:
    256                 pass
    257                 #fid.texture = 'None'       
     250            else:                           
     251                fid.texture = 'None'       
    258252
    259253            #Reference point
     
    927921    return cls(domain, mode)
    928922
    929 
    930 def xya2pts(basename_in, basename_out=None, verbose=False,
    931             #easting_min=None, easting_max=None,
    932             #northing_min=None, northing_max=None,
    933             attribute_name = 'elevation'):
    934     """Read points data from ascii (.xya)
    935 
    936     Example:
    937 
    938               x(m)        y(m)        z(m)
    939          0.00000e+00  0.00000e+00  1.3535000e-01
    940          0.00000e+00  1.40000e-02  1.3535000e-01
    941 
    942    
    943 
    944     Convert to NetCDF pts format which is
    945 
    946     points:  (Nx2) Float array
    947     elevation: N Float array
    948 
    949     Only lines that contain three numeric values are processed
    950     """
    951 
    952     import os
    953     from Scientific.IO.NetCDF import NetCDFFile
    954     from Numeric import Float, arrayrange, concatenate
    955 
    956     root, ext = os.path.splitext(basename_in)
    957 
    958     if ext == '': ext = '.xya'
    959 
    960     #Get NetCDF
    961     infile = open(root + ext, 'r')  #Open existing xya file for read
    962 
    963     if verbose: print 'Reading xya points from %s' %(root + ext)
    964 
    965     points = []
    966     attribute = []
    967     for i, line in enumerate(infile.readlines()):
    968         fields = line.split()
    969 
    970         try:
    971             assert len(fields) == 3
    972         except:   
    973             print 'WARNING: Line %d doesn\'t have 3 elements: %s' %(i, line)
    974 
    975         try:
    976             x = float( fields[0] )
    977             y = float( fields[1] )
    978             z = float( fields[2] )           
    979         except:
    980             continue
    981 
    982         points.append( [x, y] )
    983         attribute.append(z)
    984 
    985 
    986     #Get output file
    987     if basename_out == None:
    988         ptsname = root + '.pts'
    989     else:
    990         ptsname = basename_out + '.pts'
    991 
    992     if verbose: print 'Store to NetCDF file %s' %ptsname
    993     # NetCDF file definition
    994     outfile = NetCDFFile(ptsname, 'w')
    995 
    996     #Create new file
    997     outfile.institution = 'Geoscience Australia'
    998     outfile.description = 'NetCDF pts format for compact and '\
    999                           'portable storage of spatial point data'
    1000    
    1001 
    1002     #Georeferencing
    1003     from coordinate_transforms.geo_reference import Geo_reference
    1004     Geo_reference().write_NetCDF(outfile)
    1005    
    1006 
    1007     outfile.createDimension('number_of_points', len(points))
    1008     outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    1009 
    1010     # variable definitions
    1011     outfile.createVariable('points', Float, ('number_of_points',
    1012                                              'number_of_dimensions'))
    1013     outfile.createVariable(attribute_name, Float, ('number_of_points',))
    1014 
    1015     # Get handles to the variables
    1016     nc_points = outfile.variables['points']
    1017     nc_attribute = outfile.variables[attribute_name]
    1018 
    1019     #Store data
    1020     nc_points[:, :] = points
    1021     nc_attribute[:] = attribute
    1022 
    1023     infile.close()
    1024     outfile.close()
    1025 
    1026 
    1027923def dem2pts(basename_in, basename_out=None, verbose=False,
    1028924            easting_min=None, easting_max=None,
     
    1048944    import os
    1049945    from Scientific.IO.NetCDF import NetCDFFile
    1050     from Numeric import Float, arrayrange, concatenate
     946    from Numeric import Float, zeros
    1051947
    1052948    root = basename_in
     
    11171013
    11181014    # dimension definitions
    1119     nrows_in_bounding_box = int(round((easting_max-easting_min)/cellsize))
    1120     ncols_in_bounding_box = int(round((northing_max-northing_min)/cellsize))
     1015    nrows_in_bounding_box = int(round((northing_max-northing_min)/cellsize))
     1016    ncols_in_bounding_box = int(round((easting_max-easting_min)/cellsize))
    11211017    outfile.createDimension('number_of_points', nrows_in_bounding_box*ncols_in_bounding_box)
    11221018    outfile.createDimension('number_of_dimensions', 2) #This is 2d data
     
    11321028
    11331029    #Store data
    1134     #FIXME: Could perhaps be faster using array operations
    1135     index = 0
     1030    #FIXME: Could perhaps be faster using array operations (Fixed 27/7/05)
     1031    global_index = 0
    11361032    for i in range(nrows):
    11371033        if verbose: print 'Processing row %d of %d' %(i, nrows)
     1034        lower_index = global_index
     1035        tpoints = zeros((ncols_in_bounding_box, 2), Float)
     1036        telev =  zeros(ncols_in_bounding_box, Float)
     1037        local_index = 0
    11381038
    11391039        y = (nrows-i)*cellsize + yllcorner
    11401040        for j in range(ncols):
    1141             #index = i*ncols + j
    11421041
    11431042            x = j*cellsize + xllcorner
    11441043            if easting_min <= x <= easting_max and \
    11451044               northing_min <= y <= northing_max:
    1146                 points[index, :] = [x-easting_min,y-northing_min]
    1147                 elevation[index] = dem_elevation[i, j]
    1148                 index += 1
    1149 
    1150     #print easting_min, easting_max
    1151     #print index, nrows_in_bounding_box*ncols_in_bounding_box
    1152     #print nrows, ncols
    1153     assert index == nrows_in_bounding_box*ncols_in_bounding_box, 'index not equal to number of points'
     1045                tpoints[local_index, :] = [x-easting_min,y-northing_min]
     1046                telev[local_index] = dem_elevation[i, j]
     1047                global_index += 1
     1048                local_index += 1
     1049
     1050        upper_index = global_index
     1051
     1052        if upper_index == lower_index + ncols_in_bounding_box:
     1053            points[lower_index:upper_index, :] = tpoints
     1054            elevation[lower_index:upper_index] = telev
     1055
     1056    assert global_index == nrows_in_bounding_box*ncols_in_bounding_box, 'index not equal to number of points'
    11541057
    11551058    infile.close()
     
    23792282
    23802283    volumes = fid.variables['volumes'] #Connectivity
     2284
     2285
  • inundation/ga/storm_surge/pyvolution/test_data_manager.py

    r1577 r1660  
    18191819
    18201820
     1821
    18211822#-------------------------------------------------------------
    18221823if __name__ == "__main__":
    1823     suite = unittest.makeSuite(Test_Data_Manager,'test')
    1824     #suite = unittest.makeSuite(Test_Data_Manager,'test_sww2domain')
     1824    #suite = unittest.makeSuite(Test_Data_Manager,'test')
     1825    #suite = unittest.makeSuite(Test_Data_Manager,'test_dem2pts_bounding_box')
     1826    #suite = unittest.makeSuite(Test_Data_Manager,'test_decimate_dem')
    18251827    runner = unittest.TextTestRunner()
    18261828    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.