Changeset 8591


Ignore:
Timestamp:
Oct 23, 2012, 11:57:16 AM (11 years ago)
Author:
davies
Message:

Updating plot_utils

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/utilities/plot_utils.py

    r8389 r8591  
    1313                       transect (e.g. a channel cross-section) -- see example below
    1414
    15 
    16 
     15    util.sort_sww_filenames -- match sww filenames by a wildcard, and order
     16                               them according to their 'time'. This means that
     17                               they can be stuck together using 'combine_outputs' correctly
     18
     19    util.triangle_areas -- compute the areas of every triangle in a get_outputs object [ must be vertex-based]
     20
     21    util.water_volume -- compute the water volume at every time step in an sww file (needs both vertex and centroid value input).
     22 
    1723    Here is an example ipython session which uses some of these functions:
    1824
     
    2430    > pyplot.ion() # Interactive plotting
    2531    > pyplot.scatter(xxx[1],p.vel[140,xxx[0]],color='red') # Plot along the transect
     32
     33    FIXME: TODO -- Convert to a single function 'get_output', which can either take a
     34          single filename, a list of filenames, or a wildcard defining a number of
     35          filenames, and ensure that in each case, the output will be as desired.
    2636
    2737"""
     
    8090                p1.yvel, p1.vel, p1.minimum_allowed_height
    8191
    82 
     92####################
     93
     94def sort_sww_filenames(sww_wildcard):
     95    # Function to take a 'wildcard' sww filename,
     96    # and return a list of all filenames of this type,
     97    # sorted by their time.
     98    # This can then be used efficiently in 'combine_outputs'
     99    # if you have many filenames starting with the same pattern
     100    import glob
     101    filenames=glob.glob(sww_wildcard)
     102   
     103    # Extract time from filenames
     104    file_time=range(len(filenames)) # Predefine
     105     
     106    for i,filename in enumerate(filenames):
     107        filesplit=filename.rsplit('_time_')
     108        if(len(filesplit)>1):
     109            file_time[i]=int(filesplit[1].split('_0.sww')[0])
     110        else:
     111            file_time[i]=0         
     112   
     113    name_and_time=zip(file_time,filenames)
     114    name_and_time.sort() # Sort by file_time
     115   
     116    output_times, output_names = zip(*name_and_time)
     117   
     118    return list(output_names)
     119
     120##############
    83121
    84122class get_output:
     
    337375
    338376    return near_points, local_coord
     377
     378########################
     379# TRIANGLE AREAS, WATER VOLUME
     380def triangle_areas(p, subset='null'):
     381    # Compute areas of triangles in p -- assumes p contains vertex information
     382    # subset = vector of centroid indices to include in the computation.
     383
     384    if(subset=='null'):
     385        subset=range(len(p.vols[:,0]))
     386   
     387    x0=p.x[p.vols[subset,0]]
     388    x1=p.x[p.vols[subset,1]]
     389    x2=p.x[p.vols[subset,2]]
     390   
     391    y0=p.y[p.vols[subset,0]]
     392    y1=p.y[p.vols[subset,1]]
     393    y2=p.y[p.vols[subset,2]]
     394   
     395    # Vectors for cross-product
     396    v1_x=x0-x1
     397    v1_y=y0-y1
     398    #
     399    v2_x=x2-x1
     400    v2_y=y2-y1
     401    # Area
     402    area=(v1_x*v2_y-v1_y*v2_x)*0.5
     403    area=abs(area)
     404    return area
     405
     406###
     407
     408def water_volume(p,p2, per_unit_area=False, subset='null'):
     409    # Compute the water volume from p(vertex values) and p2(centroid values)
     410
     411    if(subset=='null'):
     412        subset=range(len(p2.x))
     413
     414    l=len(p2.time)
     415    area=triangle_areas(p, subset=subset)
     416   
     417    total_area=area.sum()
     418    volume=p2.time*0.
     419   
     420    # This accounts for how volume is measured in ANUGA
     421    # Compute in 2 steps to reduce precision error (important sometimes)
     422    for i in range(l):
     423        #volume[i]=((p2.stage[i,subset]-p2.elev[subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     424        volume[i]=((p2.stage[i,subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     425        volume[i]=volume[i]+((-p2.elev[subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     426   
     427    if(per_unit_area):
     428        volume=volume/total_area
     429   
     430    return volume
     431
     432
     433
Note: See TracChangeset for help on using the changeset viewer.