Ignore:
Timestamp:
Mar 16, 2005, 1:01:22 PM (20 years ago)
Author:
prow
Message:

added sww2domain. pretty sure it works ok.

File:
1 edited

Legend:

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

    r1080 r1083  
    15331533
    15341534
     1535def sww2domain(filename,t=None):
     1536    """Read sww Net CDF file containing Shallow Water Wave simulation
     1537
     1538    Quantities stage, elevation, xmomentum and ymomentum.
     1539
     1540    The momentum is not always stored.
     1541
     1542    """
     1543    from Scientific.IO.NetCDF import NetCDFFile
     1544    from domain import Domain
     1545    from Numeric import asarray, transpose
     1546    print 'Reading from ', filename
     1547    fid = NetCDFFile(filename, 'r')    #Open existing file for read
     1548    time = fid.variables['time']       #Timesteps
     1549    if t is None:
     1550        t = time[-1]
     1551    time_interp = get_time_interp(time,t)
     1552################################
     1553########################################   
     1554    # Get the variables as Numeric arrays
     1555    x = fid.variables['x'][:]             #x-coordinates of vertices
     1556    y = fid.variables['y'][:]             #y-coordinates of vertices
     1557    elevation = fid.variables['elevation']     #Elevation
     1558    stage = fid.variables['stage']     #Water level
     1559    xmomentum = fid.variables['xmomentum']   #Momentum in the x-direction
     1560    ymomentum = fid.variables['ymomentum']   #Momentum in the y-direction   
     1561#################################
     1562    xllcorner = fid.xllcorner[0]
     1563    yllcorner = fid.yllcorner[0]
     1564    starttime = fid.starttime[0]
     1565    zone = fid.zone
     1566    volumes = fid.variables['volumes'][:] #Connectivity
     1567    coordinates=transpose(asarray([x.tolist(),y.tolist()]))
     1568#
     1569    conserved_quantities = []
     1570    interpolated_quantities = {}
     1571    other_quantities = []
     1572#
     1573    print '    interpolating quantities'
     1574    for quantity in fid.variables.keys():
     1575        dimensions = fid.variables[quantity].dimensions
     1576        if 'number_of_timesteps' in dimensions:
     1577            conserved_quantities.append(quantity)
     1578            interpolated_quantities[quantity]=\
     1579                  interpolated_quantity(fid.variables[quantity][:],time_interp)
     1580        else: other_quantities.append(quantity)
     1581#
     1582    other_quantities.remove('x')
     1583    other_quantities.remove('y')
     1584    other_quantities.remove('z')
     1585    other_quantities.remove('volumes')
     1586#
     1587    conserved_quantities.remove('time')
     1588#
     1589    print other_quantities
     1590    print conserved_quantities
     1591    print '    building domain'
     1592    domain = Domain(coordinates, volumes,\
     1593                    conserved_quantities = conserved_quantities,\
     1594                    other_quantities = other_quantities,zone=zone,\
     1595                    xllcorner=xllcorner, yllcorner=yllcorner)
     1596    domain.starttime=starttime
     1597    domain.time=t
     1598    return domain
     1599
     1600def interpolated_quantity(saved_quantity,time_interp):
     1601       
     1602    #given an index and ratio, interpolate quantity with respect to time.
     1603    index,ratio = time_interp
     1604    Q = saved_quantity 
     1605    if ratio > 0:
     1606        q = (1-ratio)*Q[index]+ ratio*Q[index+1]
     1607    else:
     1608         q = Q[index]
     1609    #Return vector of interpolated values
     1610    return q
     1611
     1612def get_time_interp(time,t=None):
     1613    #Finds the ratio and index for time interpolation.
     1614    #It is borrowed from previous pyvolution code.
     1615    if t is None:
     1616        t=time[-1]
     1617        index = -1
     1618        ratio = 0.
     1619    else:
     1620        T = time
     1621        tau = t
     1622        index=0
     1623        msg = 'Time interval derived from file %s [%s:%s]'\
     1624            %('FIXMEfilename', T[0], T[-1])
     1625        msg += ' does not match model time: %s' %tau
     1626        if tau < time[0]: raise msg
     1627        if tau > time[-1]: raise msg
     1628        while tau > time[index]: index += 1
     1629        while tau < time[index]: index -= 1
     1630        if tau == time[index]:
     1631            #Protect against case where tau == time[-1] (last time)
     1632            # - also works in general when tau == time[i]
     1633            ratio = 0
     1634        else:
     1635            #t is now between index and index+1           
     1636            ratio = (tau - time[index])/(time[index+1] - time[index])
     1637    return (index,ratio)
     1638
     1639
     1640
    15351641#OBSOLETE STUFF
    15361642#Native checkpoint format.
Note: See TracChangeset for help on using the changeset viewer.