Changeset 8821


Ignore:
Timestamp:
Apr 9, 2013, 4:17:11 PM (12 years ago)
Author:
steve
Message:

Speed up of dem2pts via conversion to numpy

Location:
trunk/anuga_core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/demos/cairns/project.py

    r8763 r8821  
    5757
    5858# bigger base_scale == less triangles
    59 #base_scale = 100000 # 162170 triangles
    60 base_scale = 400000 # 42093
     59base_scale = 100000 # 162170 triangles
     60#base_scale = 400000 # 42093
    6161default_res = 100 * base_scale   # Background resolution
    6262islands_res = base_scale
  • trunk/anuga_core/demos/cairns/run_parallel_cairns.py

    r8728 r8821  
    156156if project.scenario == 'slide':
    157157    # Initial run without any event
    158     for t in domain.evolve(yieldstep=10, finaltime=60):
    159         print domain.timestepping_statistics()
    160         #print domain.boundary_statistics(tags='ocean_east')
     158    for t in domain.evolve(yieldstep=10, finaltime=60):
     159        if myid == 0:
     160            print domain.timestepping_statistics()
     161            #print domain.boundary_statistics(tags='ocean_east')
    161162       
    162163    # Add slide to water surface
     
    167168    for t in domain.evolve(yieldstep=10, finaltime=5000,
    168169                           skip_initial_step=True):
    169         print domain.timestepping_statistics()
    170         #print domain.boundary_statistics(tags='ocean_east')
     170        if myid == 0:
     171            print domain.timestepping_statistics()
     172            #print domain.boundary_statistics(tags='ocean_east')
    171173
    172174if project.scenario == 'fixed_wave':
    173175    # Save every two mins leading up to wave approaching land
    174     for t in domain.evolve(yieldstep=2*60, finaltime=5000):
    175         print domain.timestepping_statistics()
    176         #print domain.boundary_statistics(tags='ocean_east')
     176    for t in domain.evolve(yieldstep=2*60, finaltime=5000):
     177        if myid == 0:
     178            print domain.timestepping_statistics()
     179            #print domain.boundary_statistics(tags='ocean_east')
    177180
    178181    # Save every 30 secs as wave starts inundating ashore
    179182    for t in domain.evolve(yieldstep=60*0.5, finaltime=10000,
    180183                           skip_initial_step=True):
    181         print domain.timestepping_statistics()
    182         #print domain.boundary_statistics(tags='ocean_east')
    183            
    184 print 'That took %.2f seconds' %(time.time()-t0)
     184        if myid == 0:
     185            print domain.timestepping_statistics()
     186            #print domain.boundary_statistics(tags='ocean_east')
     187
     188
     189domain.sww_merge()
     190
     191if myid == 0:
     192    print 'That took %.2f seconds' %(time.time()-t0)
     193
     194   
     195
     196finalize()
  • trunk/anuga_core/source/anuga/file_conversion/dem2pts.py

    r8782 r8821  
    156156    totalnopoints = nrows*ncols
    157157
    158     # Calculating number of NODATA_values for each row in clipped region
    159     # FIXME: use array operations to do faster
    160     nn = 0
    161     k = 0
    162     i1_0 = 0
    163     j1_0 = 0
    164     thisj = 0
    165     thisi = 0
    166     for i in range(nrows):
    167         y = (nrows-i-1)*cellsize + yllcorner
    168         for j in range(ncols):
    169             x = j*cellsize + xllcorner
    170             if easting_min <= x <= easting_max \
    171                and northing_min <= y <= northing_max:
    172                 thisj = j
    173                 thisi = i
    174                 if dem_elevation_r[i,j] == NODATA_value:
    175                     nn += 1
    176 
    177                 if k == 0:
    178                     i1_0 = i
    179                     j1_0 = j
    180 
    181                 k += 1
    182 
    183     index1 = j1_0
    184     index2 = thisj
    185 
    186     # Dimension definitions
    187     nrows_in_bounding_box = int(round((northing_max-northing_min)/cellsize))
    188     ncols_in_bounding_box = int(round((easting_max-easting_min)/cellsize))
    189 
    190     clippednopoints = (thisi+1-i1_0)*(thisj+1-j1_0)
    191     nopoints = clippednopoints-nn
    192 
    193     clipped_dem_elev = dem_elevation_r[i1_0:thisi+1,j1_0:thisj+1]
     158
     159
     160
     161#    #=======================================================================
     162#    # Calculating number of NODATA_values for each row in clipped region
     163#    # FIXME: use array operations to do faster
     164#    nn = 0
     165#    k = 0
     166#    i1_0 = 0
     167#    j1_0 = 0
     168#    thisj = 0
     169#    thisi = 0
     170#    for i in range(nrows):
     171#        y = (nrows-i-1)*cellsize + yllcorner
     172#        for j in range(ncols):
     173#            x = j*cellsize + xllcorner
     174#            if easting_min <= x <= easting_max \
     175#               and northing_min <= y <= northing_max:
     176#                thisj = j
     177#                thisi = i
     178#                if dem_elevation_r[i,j] == NODATA_value:
     179#                    nn += 1
     180#
     181#                if k == 0:
     182#                    i1_0 = i
     183#                    j1_0 = j
     184#
     185#                k += 1
     186#
     187#    index1 = j1_0
     188#    index2 = thisj
     189#
     190#    # Dimension definitions
     191#    nrows_in_bounding_box = int(round((northing_max-northing_min)/cellsize))
     192#    ncols_in_bounding_box = int(round((easting_max-easting_min)/cellsize))
     193#
     194#    clippednopoints = (thisi+1-i1_0)*(thisj+1-j1_0)
     195#    nopoints = clippednopoints-nn
     196#
     197#    clipped_dem_elev = dem_elevation_r[i1_0:thisi+1,j1_0:thisj+1]
     198#
     199#    if verbose:
     200#        log.critical('There are %d values in the elevation' % totalnopoints)
     201#        log.critical('There are %d values in the clipped elevation'
     202#                     % clippednopoints)
     203#        log.critical('There are %d NODATA_values in the clipped elevation' % nn)
     204#
     205#    outfile.createDimension('number_of_points', nopoints)
     206#    outfile.createDimension('number_of_dimensions', 2) #This is 2d data
     207#
     208#    # Variable definitions
     209#    outfile.createVariable('points', netcdf_float, ('number_of_points',
     210#                                                    'number_of_dimensions'))
     211#    outfile.createVariable('elevation', netcdf_float, ('number_of_points',))
     212#
     213#    # Get handles to the variables
     214#    points = outfile.variables['points']
     215#    elevation = outfile.variables['elevation']
     216#
     217#    # Number of points
     218#    N = points.shape[0]
     219#
     220#    lenv = index2-index1+1
     221#
     222#    # Store data
     223#    global_index = 0
     224#    # for i in range(nrows):
     225#    for i in range(i1_0, thisi+1, 1):
     226#        if verbose and i % ((nrows+10)/10) == 0:
     227#            log.critical('Processing row %d of %d' % (i, nrows))
     228#
     229#        lower_index = global_index
     230#
     231#        v = dem_elevation_r[i,index1:index2+1]
     232#        no_NODATA = num.sum(v == NODATA_value)
     233#        if no_NODATA > 0:
     234#            newcols = lenv - no_NODATA  # ncols_in_bounding_box - no_NODATA
     235#        else:
     236#            newcols = lenv              # ncols_in_bounding_box
     237#
     238#        telev = num.zeros(newcols, num.float)
     239#        tpoints = num.zeros((newcols, 2), num.float)
     240#
     241#        local_index = 0
     242#
     243#        y = (nrows-i-1)*cellsize + yllcorner
     244#        #for j in range(ncols):
     245#        for j in range(j1_0,index2+1,1):
     246#            x = j*cellsize + xllcorner
     247#            if easting_min <= x <= easting_max \
     248#               and northing_min <= y <= northing_max \
     249#               and dem_elevation_r[i,j] != NODATA_value:
     250#
     251#                #print [x-easting_min, y-northing_min]
     252#                #print x , y
     253#                #print easting_min, northing_min
     254#                #print xllcorner, yllcorner
     255#                #print cellsize
     256#
     257#                tpoints[local_index, :] = [x-easting_min, y-northing_min]
     258#                telev[local_index] = dem_elevation_r[i, j]
     259#                global_index += 1
     260#                local_index += 1
     261#
     262#        upper_index = global_index
     263#
     264#        if upper_index == lower_index + newcols:
     265#
     266#            # Seems to be an error with the windows version of
     267#            # Netcdf. The following gave errors
     268#
     269#            try:
     270#                points[lower_index:upper_index, :] = tpoints
     271#                elevation[lower_index:upper_index] = telev
     272#            except:
     273#                # so used the following if an error occurs
     274#                for index in range(newcols):
     275#                    points[index+lower_index, :] = tpoints[index,:]
     276#                    elevation[index+lower_index] = telev[index]
     277#
     278#    assert global_index == nopoints, 'index not equal to number of points'
     279
     280
     281    #========================================
     282    # Do the preceeding with numpy
     283    #========================================
     284
     285    start = (nrows-1)*cellsize+yllcorner
     286    stop = yllcorner-cellsize
     287    step = -cellsize
     288    y = num.arange(start, stop,step)
     289
     290    start = xllcorner
     291    stop = (ncols)*cellsize + xllcorner
     292    step = cellsize
     293    x = num.arange(start, stop,step)
     294
     295    #print nrows,ncols
     296
     297    xx,yy = num.meshgrid(x,y)
     298
     299    #print xx
     300    #print yy
     301
     302    xx = xx.flatten()
     303    yy = yy.flatten()
     304
     305    flag = num.logical_and(num.logical_and((xx <= easting_max),(xx >= easting_min)),
     306                           num.logical_and((yy <= northing_max),(yy >= northing_min)))
     307
     308
     309    #print flag
     310
     311    #print xx
     312    #print yy
     313    #print easting_min, easting_max, northing_min, northing_max
     314
     315    dem = dem_elevation[:].flatten()
     316
     317
     318    id = num.where(flag)[0]
     319
     320    xx = xx[id]
     321    yy = yy[id]
     322    dem = dem[id]
     323
     324    clippednopoints = len(dem)
     325    #print clippedpoints
     326   
     327    #print xx
     328    #print yy
     329    #print dem
     330
     331    data_flag = dem != NODATA_value
     332
     333    data_id = num.where(data_flag)
     334
     335    xx = xx[data_id]
     336    yy = yy[data_id]
     337    dem = dem[data_id]
     338
     339    nn = clippednopoints - len(dem)
     340
     341    nopoints = len(dem)
     342
    194343
    195344    if verbose:
     
    211360    elevation = outfile.variables['elevation']
    212361
    213     # Number of points
    214     N = points.shape[0]
    215 
    216     lenv = index2-index1+1
    217 
    218     # Store data
    219     global_index = 0
    220     # for i in range(nrows):
    221     for i in range(i1_0, thisi+1, 1):
    222         if verbose and i % ((nrows+10)/10) == 0:
    223             log.critical('Processing row %d of %d' % (i, nrows))
    224 
    225         lower_index = global_index
    226 
    227         v = dem_elevation_r[i,index1:index2+1]
    228         no_NODATA = num.sum(v == NODATA_value)
    229         if no_NODATA > 0:
    230             newcols = lenv - no_NODATA  # ncols_in_bounding_box - no_NODATA
    231         else:
    232             newcols = lenv              # ncols_in_bounding_box
    233 
    234         telev = num.zeros(newcols, num.float)
    235         tpoints = num.zeros((newcols, 2), num.float)
    236 
    237         local_index = 0
    238 
    239         y = (nrows-i-1)*cellsize + yllcorner
    240         #for j in range(ncols):
    241         for j in range(j1_0,index2+1,1):
    242             x = j*cellsize + xllcorner
    243             if easting_min <= x <= easting_max \
    244                and northing_min <= y <= northing_max \
    245                and dem_elevation_r[i,j] != NODATA_value:
    246 
    247                 #print [x-easting_min, y-northing_min]
    248                 #print x , y
    249                 #print easting_min, northing_min
    250                 #print xllcorner, yllcorner
    251                 #print cellsize
    252                
    253                 tpoints[local_index, :] = [x-easting_min, y-northing_min]
    254                 telev[local_index] = dem_elevation_r[i, j]
    255                 global_index += 1
    256                 local_index += 1
    257 
    258         upper_index = global_index
    259 
    260         if upper_index == lower_index + newcols:
    261 
    262             # Seems to be an error with the windows version of
    263             # Netcdf. The following gave errors
    264 
    265             try:
    266                 points[lower_index:upper_index, :] = tpoints
    267                 elevation[lower_index:upper_index] = telev
    268             except:
    269                 # so used the following if an error occurs
    270                 for index in range(newcols):
    271                     points[index+lower_index, :] = tpoints[index,:]
    272                     elevation[index+lower_index] = telev[index]
    273 
    274     assert global_index == nopoints, 'index not equal to number of points'
     362    points[:,0] = xx - easting_min
     363    points[:,1] = yy - northing_min
     364    elevation[:] = dem
     365
    275366
    276367    infile.close()
  • trunk/anuga_core/source/anuga/file_conversion/test_dem2pts.py

    r8780 r8821  
    208208        # Get the variables
    209209        #print fid.variables.keys()
    210         points = fid.variables['points']
    211         elevation = fid.variables['elevation']
     210        points = fid.variables['points'][:]
     211        elevation = fid.variables['elevation'][:]
    212212
    213213        #Check values
     
    246246
    247247
     248
    248249        assert num.allclose(points, new_ref_points)
    249250        assert num.allclose(elevation, ref_elevation)
Note: See TracChangeset for help on using the changeset viewer.