Changeset 1865


Ignore:
Timestamp:
Oct 4, 2005, 8:39:27 PM (19 years ago)
Author:
ole
Message:

Wrapped sww2asc and sww2ers into one new function: sww2dem
Finished unittest for ers format

Location:
inundation/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/data_manager.py

    r1861 r1865  
    12551255    false_easting = 500000
    12561256    false_northing = 10000000
    1257     NODATA_value = -9999
    12581257
    12591258    if quantity is None:
     
    13241323    #Now q has dimension: number_of_points
    13251324
    1326 
     1325    #Create grid and update xll/yll corner and x,y
     1326    if verbose: print 'Creating grid'
     1327    ncols = int((xmax-xmin)/cellsize)+1
     1328    nrows = int((ymax-ymin)/cellsize)+1
     1329
     1330    newxllcorner = xmin+xllcorner
     1331    newyllcorner = ymin+yllcorner
     1332
     1333    x = x+xllcorner-newxllcorner
     1334    y = y+yllcorner-newyllcorner
     1335
     1336    vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
     1337    assert len(vertex_points.shape) == 2
     1338
     1339
     1340    from Numeric import zeros, Float
     1341    grid_points = zeros ( (ncols*nrows, 2), Float )
     1342
     1343
     1344    for i in xrange(nrows):
     1345        yg = i*cellsize
     1346        for j in xrange(ncols):
     1347            xg = j*cellsize
     1348            k = i*ncols + j
     1349
     1350            grid_points[k,0] = xg
     1351            grid_points[k,1] = yg
     1352
     1353    #Interpolate
     1354    from least_squares import Interpolation
     1355    from util import inside_polygon
     1356
     1357    #FIXME: This should be done with precrop = True, otherwise it'll
     1358    #take forever. With expand_search  set to False, some grid points might
     1359    #miss out....
     1360
     1361    interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,
     1362                           precrop = False, expand_search = False,
     1363                           verbose = verbose)
     1364
     1365    #Interpolate using quantity values
     1366    if verbose: print 'Interpolating'
     1367    grid_values = interp.interpolate(q).flat
     1368
     1369    #Write
    13271370    #Write prj file
    13281371    if verbose: print 'Writing %s' %prjfile
     
    13391382    prjid.close()
    13401383
    1341     #Create grid and update xll/yll corner and x,y
    1342     if verbose: print 'Creating grid'
    1343     ncols = int((xmax-xmin)/cellsize)+1
    1344     nrows = int((ymax-ymin)/cellsize)+1
    1345 
    1346     newxllcorner = xmin+xllcorner
    1347     newyllcorner = ymin+yllcorner
    1348 
    1349     x = x+xllcorner-newxllcorner
    1350     y = y+yllcorner-newyllcorner
    1351 
    1352     vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
    1353     assert len(vertex_points.shape) == 2
    1354 
    1355 
    1356     from Numeric import zeros, Float
    1357     grid_points = zeros ( (ncols*nrows, 2), Float )
    1358 
    1359 
    1360     for i in xrange(nrows):
    1361         yg = i*cellsize
    1362         for j in xrange(ncols):
    1363             xg = j*cellsize
    1364             k = i*ncols + j
    1365 
    1366             #print k, xg, yg
    1367             grid_points[k,0] = xg
    1368             grid_points[k,1] = yg
    1369 
    1370 
    1371     #Interpolate
    1372 
    1373     from least_squares import Interpolation
    1374     from util import inside_polygon
    1375 
    1376     #FIXME: This should be done with precrop = True, otherwise it'll
    1377     #take forever. With expand_search  set to False, some grid points might
    1378     #miss out....
    1379 
    1380     interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,
    1381                            precrop = False, expand_search = False,
    1382                            verbose = verbose)
    1383 
    1384     #Interpolate using quantity values
    1385     if verbose: print 'Interpolating'
    1386     grid_values = interp.interpolate(q).flat
    1387 
    1388     #Write
     1384
     1385   
    13891386    if verbose: print 'Writing %s' %ascfile
     1387    NODATA_value = -9999
     1388 
    13901389    ascid = open(ascfile, 'w')
    13911390
     
    14191418    ascid.close()
    14201419    fid.close()
     1420
     1421def sww2ers(basename_in, basename_out = None,
     1422            quantity = None,
     1423            timestep = None,
     1424            reduction = None,
     1425            cellsize = 10,
     1426            verbose = False,
     1427            origin = None,
     1428            datum = 'WGS84'):
     1429   
     1430    """Read SWW file and convert to Digitial Elevation model format (.asc)
     1431
     1432    Example:
     1433
     1434    ncols         3121
     1435    nrows         1800
     1436    xllcorner     722000
     1437    yllcorner     5893000
     1438    cellsize      25
     1439    NODATA_value  -9999
     1440    138.3698 137.4194 136.5062 135.5558 ..........
     1441
     1442    Also write accompanying file with same basename_in but extension .prj
     1443    used to fix the UTM zone, datum, false northings and eastings.
     1444
     1445    The prj format is assumed to be as
     1446
     1447    Projection    UTM
     1448    Zone          56
     1449    Datum         WGS84
     1450    Zunits        NO
     1451    Units         METERS
     1452    Spheroid      WGS84
     1453    Xshift        0.0000000000
     1454    Yshift        10000000.0000000000
     1455    Parameters
     1456
     1457
     1458    if quantity is given, out values from quantity otherwise default to
     1459    elevation
     1460
     1461    if timestep (an index) is given, output quantity at that timestep
     1462
     1463    if reduction is given use that to reduce quantity over all timesteps.
     1464
     1465    """
     1466    from Numeric import array, Float, concatenate, NewAxis, zeros, reshape, sometrue
     1467    import ermapper_grids
     1468
     1469    header = {}
     1470    false_easting = 500000
     1471    false_northing = 10000000
     1472    NODATA_value = 0
     1473
     1474    if quantity is None:
     1475        quantity = 'elevation'
     1476
     1477    if reduction is None:
     1478        reduction = max
     1479
     1480    if basename_out is None:
     1481        basename_out = basename_in + '_%s' %quantity
     1482 
     1483    swwfile = basename_in + '.sww'
     1484    # Note the use of a .ers extension is optional (write_ermapper_grid will
     1485    # deal with either option
     1486    ersfile = basename_out
     1487##    prjfile = basename_out + '.prj'
     1488
     1489
     1490    if verbose: print 'Reading from %s' %swwfile
     1491    #Read sww file
     1492    from Scientific.IO.NetCDF import NetCDFFile
     1493    fid = NetCDFFile(swwfile)
     1494
     1495    #Get extent and reference
     1496    x = fid.variables['x'][:]
     1497    y = fid.variables['y'][:]
     1498    volumes = fid.variables['volumes'][:]
     1499
     1500    ymin = min(y); ymax = max(y)
     1501    xmin = min(x); xmax = max(x)
     1502
     1503    number_of_timesteps = fid.dimensions['number_of_timesteps']
     1504    number_of_points = fid.dimensions['number_of_points']
     1505    if origin is None:
     1506
     1507        #Get geo_reference
     1508        #sww files don't have to have a geo_ref
     1509        try:
     1510            geo_reference = Geo_reference(NetCDFObject=fid)
     1511        except AttributeError, e:
     1512            geo_reference = Geo_reference() #Default georef object
     1513
     1514        xllcorner = geo_reference.get_xllcorner()
     1515        yllcorner = geo_reference.get_yllcorner()
     1516        zone = geo_reference.get_zone()
     1517    else:
     1518        zone = origin[0]
     1519        xllcorner = origin[1]
     1520        yllcorner = origin[2]
     1521
     1522
     1523    #Get quantity and reduce if applicable
     1524    if verbose: print 'Reading quantity %s' %quantity
     1525
     1526    if quantity.lower() == 'depth':
     1527        q = fid.variables['stage'][:] - fid.variables['elevation'][:]
     1528    else:
     1529        q = fid.variables[quantity][:]
     1530
     1531
     1532    if len(q.shape) == 2:
     1533        if verbose: print 'Reducing quantity %s' %quantity
     1534        q_reduced = zeros( number_of_points, Float )
     1535
     1536        for k in range(number_of_points):
     1537            q_reduced[k] = reduction( q[:,k] )
     1538
     1539        q = q_reduced
     1540
     1541    #Now q has dimension: number_of_points
     1542
     1543    #Create grid and update xll/yll corner and x,y
     1544    if verbose: print 'Creating grid'
     1545    ncols = int((xmax-xmin)/cellsize)+1
     1546    nrows = int((ymax-ymin)/cellsize)+1
     1547
     1548    newxllcorner = xmin+xllcorner
     1549    newyllcorner = ymin+yllcorner
     1550
     1551    x = x+xllcorner-newxllcorner
     1552    y = y+yllcorner-newyllcorner
     1553
     1554    vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
     1555    assert len(vertex_points.shape) == 2
     1556
     1557
     1558    from Numeric import zeros, Float
     1559    grid_points = zeros ( (ncols*nrows, 2), Float )
     1560
     1561
     1562    for i in xrange(nrows):
     1563        yg = (nrows-i)*cellsize # this will flip the order of the y values
     1564        for j in xrange(ncols):
     1565            xg = j*cellsize
     1566            k = i*ncols + j
     1567
     1568            grid_points[k,0] = xg
     1569            grid_points[k,1] = yg
     1570
     1571    #Interpolate
     1572    from least_squares import Interpolation
     1573    from util import inside_polygon
     1574
     1575    #FIXME: This should be done with precrop = True (?), otherwise it'll
     1576    #take forever. With expand_search  set to False, some grid points might
     1577    #miss out....
     1578
     1579    interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,
     1580                           precrop = False, expand_search = False,
     1581                           verbose = verbose)
     1582
     1583    #Interpolate using quantity values
     1584    if verbose: print 'Interpolating'
     1585    grid_values = interp.interpolate(q).flat
     1586    grid_values = reshape(grid_values,(nrows, ncols))
     1587
     1588
     1589    # setup header information
     1590    header['datum'] = '"' + datum + '"'
     1591    # FIXME The use of hardwired UTM and zone number needs to be made optional
     1592    # FIXME Also need an automatic test for coordinate type (i.e. EN or LL)
     1593    header['projection'] = '"UTM-' + str(zone) + '"'
     1594    header['coordinatetype'] = 'EN'
     1595    if header['coordinatetype'] == 'LL':
     1596        header['longitude'] = str(newxllcorner)
     1597        header['latitude'] = str(newyllcorner)
     1598    elif header['coordinatetype'] == 'EN':
     1599        header['eastings'] = str(newxllcorner)
     1600        header['northings'] = str(newyllcorner)
     1601    header['nullcellvalue'] = str(NODATA_value)
     1602    header['xdimension'] = str(cellsize)
     1603    header['ydimension'] = str(cellsize)
     1604    header['value'] = '"' + quantity + '"'
     1605
     1606
     1607    #Write
     1608    if verbose: print 'Writing %s' %ersfile
     1609    ermapper_grids.write_ermapper_grid(ersfile,grid_values, header)
     1610
     1611    fid.close()   
     1612
     1613##    ascid = open(ascfile, 'w')
     1614##
     1615##    ascid.write('ncols         %d\n' %ncols)
     1616##    ascid.write('nrows         %d\n' %nrows)
     1617##    ascid.write('xllcorner     %d\n' %newxllcorner)
     1618##    ascid.write('yllcorner     %d\n' %newyllcorner)
     1619##    ascid.write('cellsize      %f\n' %cellsize)
     1620##    ascid.write('NODATA_value  %d\n' %NODATA_value)
     1621##
     1622##
     1623##    #Get bounding polygon from mesh
     1624##    P = interp.mesh.get_boundary_polygon()
     1625##    inside_indices = inside_polygon(grid_points, P)
     1626##
     1627##    for i in range(nrows):
     1628##        if verbose and i%((nrows+10)/10)==0:
     1629##            print 'Doing row %d of %d' %(i, nrows)
     1630##
     1631##        for j in range(ncols):
     1632##            index = (nrows-i-1)*ncols+j
     1633##
     1634##            if sometrue(inside_indices == index):
     1635##                ascid.write('%f ' %grid_values[index])
     1636##            else:
     1637##                ascid.write('%d ' %NODATA_value)
     1638##
     1639##        ascid.write('\n')
     1640##
     1641##    #Close
     1642##    ascid.close()
     1643##    fid.close()
     1644
     1645
     1646
     1647def sww2dem(basename_in, basename_out = None,
     1648            quantity = None,
     1649            timestep = None,
     1650            reduction = None,
     1651            cellsize = 10,
     1652            verbose = False,
     1653            origin = None,
     1654            datum = 'WGS84',
     1655            format = 'ers'):
     1656   
     1657    """Read SWW file and convert to Digitial Elevation model format (.asc or .ers)
     1658
     1659    Example (ASC):
     1660
     1661    ncols         3121
     1662    nrows         1800
     1663    xllcorner     722000
     1664    yllcorner     5893000
     1665    cellsize      25
     1666    NODATA_value  -9999
     1667    138.3698 137.4194 136.5062 135.5558 ..........
     1668
     1669    Also write accompanying file with same basename_in but extension .prj
     1670    used to fix the UTM zone, datum, false northings and eastings.
     1671
     1672    The prj format is assumed to be as
     1673
     1674    Projection    UTM
     1675    Zone          56
     1676    Datum         WGS84
     1677    Zunits        NO
     1678    Units         METERS
     1679    Spheroid      WGS84
     1680    Xshift        0.0000000000
     1681    Yshift        10000000.0000000000
     1682    Parameters
     1683
     1684
     1685    if quantity is given, out values from quantity otherwise default to
     1686    elevation
     1687
     1688    if timestep (an index) is given, output quantity at that timestep
     1689
     1690    if reduction is given use that to reduce quantity over all timesteps.
     1691
     1692    datum
     1693   
     1694    format can be either 'asc' or 'ers'
     1695    """
     1696
     1697    from Numeric import array, Float, concatenate, NewAxis, zeros, reshape, sometrue
     1698
     1699    msg = 'Format must be either asc or ers'
     1700    assert format.lower() in ['asc', 'ers'], msg
     1701   
     1702    false_easting = 500000
     1703    false_northing = 10000000
     1704
     1705    if quantity is None:
     1706        quantity = 'elevation'
     1707
     1708    if reduction is None:
     1709        reduction = max
     1710
     1711    if basename_out is None:
     1712        basename_out = basename_in + '_%s' %quantity
     1713 
     1714    swwfile = basename_in + '.sww'
     1715    demfile = basename_out + '.' + format
     1716    # Note the use of a .ers extension is optional (write_ermapper_grid will
     1717    # deal with either option
     1718    # ersfile = basename_out
     1719
     1720    if verbose: print 'Reading from %s' %swwfile
     1721    #Read sww file
     1722    from Scientific.IO.NetCDF import NetCDFFile
     1723    fid = NetCDFFile(swwfile)
     1724
     1725    #Get extent and reference
     1726    x = fid.variables['x'][:]
     1727    y = fid.variables['y'][:]
     1728    volumes = fid.variables['volumes'][:]
     1729
     1730    ymin = min(y); ymax = max(y)
     1731    xmin = min(x); xmax = max(x)
     1732
     1733    number_of_timesteps = fid.dimensions['number_of_timesteps']
     1734    number_of_points = fid.dimensions['number_of_points']
     1735    if origin is None:
     1736
     1737        #Get geo_reference
     1738        #sww files don't have to have a geo_ref
     1739        try:
     1740            geo_reference = Geo_reference(NetCDFObject=fid)
     1741        except AttributeError, e:
     1742            geo_reference = Geo_reference() #Default georef object
     1743
     1744        xllcorner = geo_reference.get_xllcorner()
     1745        yllcorner = geo_reference.get_yllcorner()
     1746        zone = geo_reference.get_zone()
     1747    else:
     1748        zone = origin[0]
     1749        xllcorner = origin[1]
     1750        yllcorner = origin[2]
     1751
     1752
     1753    #Get quantity and reduce if applicable
     1754    if verbose: print 'Reading quantity %s' %quantity
     1755
     1756    if quantity.lower() == 'depth':
     1757        q = fid.variables['stage'][:] - fid.variables['elevation'][:]
     1758    else:
     1759        q = fid.variables[quantity][:]
     1760
     1761
     1762    if len(q.shape) == 2:
     1763        if verbose: print 'Reducing quantity %s' %quantity
     1764        q_reduced = zeros( number_of_points, Float )
     1765
     1766        for k in range(number_of_points):
     1767            q_reduced[k] = reduction( q[:,k] )
     1768
     1769        q = q_reduced
     1770
     1771    #Now q has dimension: number_of_points
     1772
     1773    #Create grid and update xll/yll corner and x,y
     1774    if verbose: print 'Creating grid'
     1775    ncols = int((xmax-xmin)/cellsize)+1
     1776    nrows = int((ymax-ymin)/cellsize)+1
     1777
     1778    newxllcorner = xmin+xllcorner
     1779    newyllcorner = ymin+yllcorner
     1780
     1781    x = x+xllcorner-newxllcorner
     1782    y = y+yllcorner-newyllcorner
     1783
     1784    vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
     1785    assert len(vertex_points.shape) == 2
     1786
     1787
     1788    from Numeric import zeros, Float
     1789    grid_points = zeros ( (ncols*nrows, 2), Float )
     1790
     1791
     1792    for i in xrange(nrows):
     1793        if format.lower() == 'asc':
     1794            yg = i*cellsize
     1795        else:   
     1796            #this will flip the order of the y values for ers
     1797            yg = (nrows-i)*cellsize
     1798           
     1799        for j in xrange(ncols):
     1800            xg = j*cellsize
     1801            k = i*ncols + j
     1802
     1803            grid_points[k,0] = xg
     1804            grid_points[k,1] = yg
     1805
     1806    #Interpolate
     1807    from least_squares import Interpolation
     1808    from util import inside_polygon
     1809
     1810    #FIXME: This should be done with precrop = True (?), otherwise it'll
     1811    #take forever. With expand_search  set to False, some grid points might
     1812    #miss out....
     1813
     1814    interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,
     1815                           precrop = False, expand_search = False,
     1816                           verbose = verbose)
     1817
     1818    #Interpolate using quantity values
     1819    if verbose: print 'Interpolating'
     1820    grid_values = interp.interpolate(q).flat
     1821
     1822    if format.lower() == 'ers':
     1823        # setup ERS header information
     1824        grid_values = reshape(grid_values,(nrows, ncols))   
     1825        NODATA_value = 0
     1826        header = {}
     1827        header['datum'] = '"' + datum + '"'
     1828        # FIXME The use of hardwired UTM and zone number needs to be made optional
     1829        # FIXME Also need an automatic test for coordinate type (i.e. EN or LL)
     1830        header['projection'] = '"UTM-' + str(zone) + '"'
     1831        header['coordinatetype'] = 'EN'
     1832        if header['coordinatetype'] == 'LL':
     1833            header['longitude'] = str(newxllcorner)
     1834            header['latitude'] = str(newyllcorner)
     1835        elif header['coordinatetype'] == 'EN':
     1836            header['eastings'] = str(newxllcorner)
     1837            header['northings'] = str(newyllcorner)
     1838        header['nullcellvalue'] = str(NODATA_value)
     1839        header['xdimension'] = str(cellsize)
     1840        header['ydimension'] = str(cellsize)
     1841        header['value'] = '"' + quantity + '"'
     1842
     1843
     1844        #Write
     1845        if verbose: print 'Writing %s' %ersfile
     1846        import ermapper_grids
     1847        ermapper_grids.write_ermapper_grid(demfile, grid_values, header)
     1848
     1849        fid.close()   
     1850    else:
     1851        #Write to Ascii format
     1852       
     1853        #Write prj file
     1854        prjfile = basename_out + '.prj'
     1855       
     1856        if verbose: print 'Writing %s' %prjfile
     1857        prjid = open(prjfile, 'w')
     1858        prjid.write('Projection    %s\n' %'UTM')
     1859        prjid.write('Zone          %d\n' %zone)
     1860        prjid.write('Datum         %s\n' %datum)
     1861        prjid.write('Zunits        NO\n')
     1862        prjid.write('Units         METERS\n')
     1863        prjid.write('Spheroid      %s\n' %datum)
     1864        prjid.write('Xshift        %d\n' %false_easting)
     1865        prjid.write('Yshift        %d\n' %false_northing)
     1866        prjid.write('Parameters\n')
     1867        prjid.close()
     1868
     1869
     1870   
     1871        if verbose: print 'Writing %s' %ascfile
     1872        NODATA_value = -9999
     1873 
     1874        ascid = open(demfile, 'w')
     1875
     1876        ascid.write('ncols         %d\n' %ncols)
     1877        ascid.write('nrows         %d\n' %nrows)
     1878        ascid.write('xllcorner     %d\n' %newxllcorner)
     1879        ascid.write('yllcorner     %d\n' %newyllcorner)
     1880        ascid.write('cellsize      %f\n' %cellsize)
     1881        ascid.write('NODATA_value  %d\n' %NODATA_value)
     1882
     1883
     1884        #Get bounding polygon from mesh
     1885        P = interp.mesh.get_boundary_polygon()
     1886        inside_indices = inside_polygon(grid_points, P)
     1887
     1888        for i in range(nrows):
     1889            if verbose and i%((nrows+10)/10)==0:
     1890                print 'Doing row %d of %d' %(i, nrows)
     1891
     1892            for j in range(ncols):
     1893                index = (nrows-i-1)*ncols+j
     1894
     1895                if sometrue(inside_indices == index):
     1896                    ascid.write('%f ' %grid_values[index])
     1897                else:
     1898                    ascid.write('%d ' %NODATA_value)
     1899
     1900            ascid.write('\n')
     1901
     1902        #Close
     1903        ascid.close()
     1904        fid.close()
    14211905
    14221906
     
    27153199    outfile.close()
    27163200
    2717 def sww2ers(basename_in, basename_out = None,
    2718             quantity = None,
    2719             timestep = None,
    2720             reduction = None,
    2721             cellsize = 10,
    2722             verbose = False,
    2723             origin = None,
    2724             datum = 'WGS84'):
    2725    
    2726     """Read SWW file and convert to Digitial Elevation model format (.asc)
    2727 
    2728     Example:
    2729 
    2730     ncols         3121
    2731     nrows         1800
    2732     xllcorner     722000
    2733     yllcorner     5893000
    2734     cellsize      25
    2735     NODATA_value  -9999
    2736     138.3698 137.4194 136.5062 135.5558 ..........
    2737 
    2738     Also write accompanying file with same basename_in but extension .prj
    2739     used to fix the UTM zone, datum, false northings and eastings.
    2740 
    2741     The prj format is assumed to be as
    2742 
    2743     Projection    UTM
    2744     Zone          56
    2745     Datum         WGS84
    2746     Zunits        NO
    2747     Units         METERS
    2748     Spheroid      WGS84
    2749     Xshift        0.0000000000
    2750     Yshift        10000000.0000000000
    2751     Parameters
    2752 
    2753 
    2754     if quantity is given, out values from quantity otherwise default to
    2755     elevation
    2756 
    2757     if timestep (an index) is given, output quantity at that timestep
    2758 
    2759     if reduction is given use that to reduce quantity over all timesteps.
    2760 
    2761     """
    2762     from Numeric import array, Float, concatenate, NewAxis, zeros, reshape, sometrue
    2763     import ermapper_grids
    2764 
    2765     header = {}
    2766     false_easting = 500000
    2767     false_northing = 10000000
    2768     NODATA_value = 0
    2769 
    2770     if quantity is None:
    2771         quantity = 'elevation'
    2772 
    2773     if reduction is None:
    2774         reduction = max
    2775 
    2776     if basename_out is None:
    2777         basename_out = basename_in + '_%s' %quantity
    2778  
    2779     swwfile = basename_in + '.sww'
    2780     # Note the use of a .ers extension is optional (write_ermapper_grid will
    2781     # deal with either option
    2782     ersfile = basename_out
    2783 ##    prjfile = basename_out + '.prj'
    2784 
    2785 
    2786     if verbose: print 'Reading from %s' %swwfile
    2787     #Read sww file
    2788     from Scientific.IO.NetCDF import NetCDFFile
    2789     fid = NetCDFFile(swwfile)
    2790 
    2791     #Get extent and reference
    2792     x = fid.variables['x'][:]
    2793     y = fid.variables['y'][:]
    2794     volumes = fid.variables['volumes'][:]
    2795 
    2796     ymin = min(y); ymax = max(y)
    2797     xmin = min(x); xmax = max(x)
    2798 
    2799     number_of_timesteps = fid.dimensions['number_of_timesteps']
    2800     number_of_points = fid.dimensions['number_of_points']
    2801     if origin is None:
    2802 
    2803         #Get geo_reference
    2804         #sww files don't have to have a geo_ref
    2805         try:
    2806             geo_reference = Geo_reference(NetCDFObject=fid)
    2807         except AttributeError, e:
    2808             geo_reference = Geo_reference() #Default georef object
    2809 
    2810         xllcorner = geo_reference.get_xllcorner()
    2811         yllcorner = geo_reference.get_yllcorner()
    2812         zone = geo_reference.get_zone()
    2813     else:
    2814         zone = origin[0]
    2815         xllcorner = origin[1]
    2816         yllcorner = origin[2]
    2817 
    2818 
    2819     #Get quantity and reduce if applicable
    2820     if verbose: print 'Reading quantity %s' %quantity
    2821 
    2822     if quantity.lower() == 'depth':
    2823         q = fid.variables['stage'][:] - fid.variables['elevation'][:]
    2824     else:
    2825         q = fid.variables[quantity][:]
    2826 
    2827 
    2828     if len(q.shape) == 2:
    2829         if verbose: print 'Reducing quantity %s' %quantity
    2830         q_reduced = zeros( number_of_points, Float )
    2831 
    2832         for k in range(number_of_points):
    2833             q_reduced[k] = reduction( q[:,k] )
    2834 
    2835         q = q_reduced
    2836 
    2837     #Now q has dimension: number_of_points
    2838 
    2839     #Create grid and update xll/yll corner and x,y
    2840     if verbose: print 'Creating grid'
    2841     ncols = int((xmax-xmin)/cellsize)+1
    2842     nrows = int((ymax-ymin)/cellsize)+1
    2843 
    2844     newxllcorner = xmin+xllcorner
    2845     newyllcorner = ymin+yllcorner
    2846 
    2847     x = x+xllcorner-newxllcorner
    2848     y = y+yllcorner-newyllcorner
    2849 
    2850     vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
    2851     assert len(vertex_points.shape) == 2
    2852 
    2853 
    2854     from Numeric import zeros, Float
    2855     grid_points = zeros ( (ncols*nrows, 2), Float )
    2856 
    2857 
    2858     for i in xrange(nrows):
    2859 ##        yg = i*cellsize
    2860         yg = (nrows-i)*cellsize # this will flip the order of the y values
    2861         for j in xrange(ncols):
    2862             xg = j*cellsize
    2863             k = i*ncols + j
    2864 
    2865 ##            print k, xg, yg
    2866             grid_points[k,0] = xg
    2867             grid_points[k,1] = yg
    2868 
    2869     #Interpolate
    2870     from least_squares import Interpolation
    2871     from util import inside_polygon
    2872 
    2873     #FIXME: This should be done with precrop = True (?), otherwise it'll
    2874     #take forever. With expand_search  set to False, some grid points might
    2875     #miss out....
    2876 
    2877     interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,
    2878                            precrop = False, expand_search = False,
    2879                            verbose = verbose)
    2880 
    2881     #Interpolate using quantity values
    2882     if verbose: print 'Interpolating'
    2883     grid_values = interp.interpolate(q).flat
    2884     grid_values = reshape(grid_values,(nrows, ncols))
    2885 
    2886 
    2887     # setup header information
    2888     header['datum'] = '"' + datum + '"'
    2889     # FIXME The use of hardwired UTM and zone number needs to be made optional
    2890     # FIXME Also need an automatic test for coordinate type (i.e. EN or LL)
    2891     header['projection'] = '"UTM-' + str(zone) + '"'
    2892     header['coordinatetype'] = 'EN'
    2893     if header['coordinatetype'] == 'LL':
    2894         header['longitude'] = str(newxllcorner)
    2895         header['latitude'] = str(newyllcorner)
    2896     elif header['coordinatetype'] == 'EN':
    2897         header['eastings'] = str(newxllcorner)
    2898         header['northings'] = str(newyllcorner)
    2899     header['nullcellvalue'] = str(NODATA_value)
    2900     header['xdimension'] = str(cellsize)
    2901     header['ydimension'] = str(cellsize)
    2902     header['value'] = '"' + quantity + '"'
    2903 
    2904 
    2905     #Write
    2906     if verbose: print 'Writing %s' %ersfile
    2907     ermapper_grids.write_ermapper_grid(ersfile,grid_values, header)
    2908 
    2909     fid.close()   
    2910 
    2911 ##    ascid = open(ascfile, 'w')
    2912 ##
    2913 ##    ascid.write('ncols         %d\n' %ncols)
    2914 ##    ascid.write('nrows         %d\n' %nrows)
    2915 ##    ascid.write('xllcorner     %d\n' %newxllcorner)
    2916 ##    ascid.write('yllcorner     %d\n' %newyllcorner)
    2917 ##    ascid.write('cellsize      %f\n' %cellsize)
    2918 ##    ascid.write('NODATA_value  %d\n' %NODATA_value)
    2919 ##
    2920 ##
    2921 ##    #Get bounding polygon from mesh
    2922 ##    P = interp.mesh.get_boundary_polygon()
    2923 ##    inside_indices = inside_polygon(grid_points, P)
    2924 ##
    2925 ##    for i in range(nrows):
    2926 ##        if verbose and i%((nrows+10)/10)==0:
    2927 ##            print 'Doing row %d of %d' %(i, nrows)
    2928 ##
    2929 ##        for j in range(ncols):
    2930 ##            index = (nrows-i-1)*ncols+j
    2931 ##
    2932 ##            if sometrue(inside_indices == index):
    2933 ##                ascid.write('%f ' %grid_values[index])
    2934 ##            else:
    2935 ##                ascid.write('%d ' %NODATA_value)
    2936 ##
    2937 ##        ascid.write('\n')
    2938 ##
    2939 ##    #Close
    2940 ##    ascid.close()
    2941 ##    fid.close()
  • inundation/pyvolution/test_data_manager.py

    r1864 r1865  
    740740
    741741
    742     def test_sww2asc_elevation(self):
     742    def test_sww2dem_asc_elevation(self):
    743743        """Test that sww information can be converted correctly to asc/prj
    744744        format readable by e.g. ArcView
     
    785785
    786786        #Export to ascii/prj files
    787         sww2asc(self.domain.filename,
     787        sww2dem(self.domain.filename,
    788788                quantity = 'elevation',
    789789                cellsize = cellsize,
    790                 verbose = False)
    791 
    792 
     790                verbose = False,
     791                format = 'asc')
     792               
    793793        #Check prj (meta data)
    794794        prjid = open(prjfile)
     
    877877
    878878
    879     def test_sww2asc_stage_reduction(self):
     879    def test_sww2dem_asc_stage_reduction(self):
    880880        """Test that sww information can be converted correctly to asc/prj
    881881        format readable by e.g. ArcView
     
    925925
    926926        #Export to ascii/prj files
    927         sww2asc(self.domain.filename,
     927        sww2dem(self.domain.filename,
    928928                quantity = 'stage',
    929929                cellsize = cellsize,
    930                 reduction = min)
     930                reduction = min,
     931                format = 'asc')
    931932
    932933
     
    986987
    987988
    988     def test_sww2asc_missing_points(self):
     989    def test_sww2dem_asc_missing_points(self):
    989990        """Test that sww information can be converted correctly to asc/prj
    990991        format readable by e.g. ArcView
     
    10731074
    10741075        #Export to ascii/prj files
    1075         sww2asc(domain.filename,
     1076        sww2dem(domain.filename,
    10761077                quantity = 'elevation',
    10771078                cellsize = cellsize,
    1078                 verbose = False)
     1079                verbose = False,
     1080                format = 'asc')
    10791081
    10801082
     
    11281130        os.remove(ascfile)
    11291131        os.remove(swwfile)
     1132
     1133    def test_sww2ers_simple(self):
     1134        """Test that sww information can be converted correctly to asc/prj
     1135        format readable by e.g. ArcView
     1136        """
     1137
     1138        import time, os
     1139        from Numeric import array, zeros, allclose, Float, concatenate
     1140        from Scientific.IO.NetCDF import NetCDFFile
     1141
     1142        #Setup
     1143        self.domain.filename = 'datatest'
     1144
     1145        headerfile = self.domain.filename + '.ers'
     1146        swwfile = self.domain.filename + '.sww'
     1147
     1148        self.domain.set_datadir('.')
     1149        self.domain.format = 'sww'
     1150        self.domain.smooth = True
     1151        self.domain.set_quantity('elevation', lambda x,y: -x-y)
     1152
     1153        self.domain.geo_reference = Geo_reference(56,308500,6189000)
     1154
     1155        sww = get_dataobject(self.domain)
     1156        sww.store_connectivity()
     1157        sww.store_timestep('stage')
     1158
     1159        self.domain.evolve_to_end(finaltime = 0.01)
     1160        sww.store_timestep('stage')
     1161
     1162        cellsize = 0.25
     1163        #Check contents
     1164        #Get NetCDF
     1165
     1166        fid = NetCDFFile(sww.filename, 'r')
     1167
     1168        # Get the variables
     1169        x = fid.variables['x'][:]
     1170        y = fid.variables['y'][:]
     1171        z = fid.variables['elevation'][:]
     1172        time = fid.variables['time'][:]
     1173        stage = fid.variables['stage'][:]
     1174
     1175
     1176        #Export to ers files
     1177        #sww2ers(self.domain.filename,
     1178        #        quantity = 'elevation',
     1179        #        cellsize = cellsize,
     1180        #        verbose = False)
     1181               
     1182        sww2dem(self.domain.filename,
     1183                quantity = 'elevation',
     1184                cellsize = cellsize,
     1185                verbose = False,
     1186                format = 'ers')
     1187
     1188        #Check header data
     1189        from ermapper_grids import read_ermapper_header, read_ermapper_data
     1190       
     1191        header = read_ermapper_header(self.domain.filename + '_elevation.ers')
     1192        #print header
     1193        assert header['projection'].lower() == '"utm-56"'
     1194        assert header['datum'].lower() == '"wgs84"'
     1195        assert header['units'].lower() == '"meters"'   
     1196        assert header['value'].lower() == '"elevation"'         
     1197        assert header['xdimension'] == '0.25'
     1198        assert header['ydimension'] == '0.25'   
     1199        assert float(header['eastings']) == 308500.0   #xllcorner
     1200        assert float(header['northings']) == 6189000.0 #yllcorner       
     1201        assert int(header['nroflines']) == 5
     1202        assert int(header['nrofcellsperline']) == 5     
     1203        assert int(header['nullcellvalue']) == 0   #?           
     1204        #FIXME - there is more in the header                   
     1205
     1206               
     1207        #Check grid data               
     1208        grid = read_ermapper_data(self.domain.filename + '_elevation')
     1209       
     1210        #FIXME (Ole): Why is this the desired reference grid for -x-y?
     1211        ref_grid = [0,      0,     0,     0,     0,
     1212                    -1,    -1.25, -1.5,  -1.75, -2.0,
     1213                    -0.75, -1.0,  -1.25, -1.5,  -1.75,             
     1214                    -0.5,  -0.75, -1.0,  -1.25, -1.5,
     1215                    -0.25, -0.5,  -0.75, -1.0,  -1.25]             
     1216                                         
     1217                                         
     1218        assert allclose(grid, ref_grid)
     1219
     1220        fid.close()
     1221       
     1222        #Cleanup
     1223        #FIXME the file clean-up doesn't work (eg Permission Denied Error)
     1224        #Done (Ole) - it was because sww2ers didn't close it's sww file
     1225        os.remove(sww.filename)
     1226
     1227
     1228    def xxxtestz_sww2ers_real(self):
     1229        """Test that sww information can be converted correctly to asc/prj
     1230        format readable by e.g. ArcView
     1231        """
     1232
     1233        import time, os
     1234        from Numeric import array, zeros, allclose, Float, concatenate
     1235        from Scientific.IO.NetCDF import NetCDFFile
     1236
     1237
     1238
     1239
     1240        #Export to ascii/prj files
     1241        sww2ers('karratha_100m.sww',
     1242                quantity = 'depth',
     1243                cellsize = 5,
     1244                verbose = False)
    11301245
    11311246
     
    20522167        os.remove(root + '_100.dem')
    20532168
    2054     def test_sww2ers_simple(self):
    2055         """Test that sww information can be converted correctly to asc/prj
    2056         format readable by e.g. ArcView
    2057         """
    2058 
    2059         import time, os
    2060         from Numeric import array, zeros, allclose, Float, concatenate
    2061         from Scientific.IO.NetCDF import NetCDFFile
    2062 
    2063         #Setup
    2064         self.domain.filename = 'datatest'
    2065 
    2066         headerfile = self.domain.filename + '.ers'
    2067         swwfile = self.domain.filename + '.sww'
    2068 
    2069         self.domain.set_datadir('.')
    2070         self.domain.format = 'sww'
    2071         self.domain.smooth = True
    2072         self.domain.set_quantity('elevation', lambda x,y: -x-y)
    2073 
    2074         self.domain.geo_reference = Geo_reference(56,308500,6189000)
    2075 
    2076         sww = get_dataobject(self.domain)
    2077         sww.store_connectivity()
    2078         sww.store_timestep('stage')
    2079 
    2080         self.domain.evolve_to_end(finaltime = 0.01)
    2081         sww.store_timestep('stage')
    2082 
    2083         cellsize = 0.25
    2084         #Check contents
    2085         #Get NetCDF
    2086 
    2087         fid = NetCDFFile(sww.filename, 'r')
    2088 
    2089         # Get the variables
    2090         x = fid.variables['x'][:]
    2091         y = fid.variables['y'][:]
    2092         z = fid.variables['elevation'][:]
    2093         time = fid.variables['time'][:]
    2094         stage = fid.variables['stage'][:]
    2095 
    2096 
    2097         #Export to ascii/prj files
    2098         sww2ers(self.domain.filename,
    2099                 quantity = 'elevation',
    2100                 cellsize = cellsize,
    2101                 verbose = False)
    2102 
    2103 #FIXME (Ole) Why aren't we testing anything
    2104 
    2105 ##        #Check prj (meta data)
    2106 ##        prjid = open(prjfile)
    2107 ##        lines = prjid.readlines()
    2108 ##        prjid.close()
    2109 ##
    2110 ##        L = lines[0].strip().split()
    2111 ##        assert L[0].strip().lower() == 'projection'
    2112 ##        assert L[1].strip().lower() == 'utm'
    2113 ##
    2114 ##        L = lines[1].strip().split()
    2115 ##        assert L[0].strip().lower() == 'zone'
    2116 ##        assert L[1].strip().lower() == '56'
    2117 ##
    2118 ##        L = lines[2].strip().split()
    2119 ##        assert L[0].strip().lower() == 'datum'
    2120 ##        assert L[1].strip().lower() == 'wgs84'
    2121 ##
    2122 ##        L = lines[3].strip().split()
    2123 ##        assert L[0].strip().lower() == 'zunits'
    2124 ##        assert L[1].strip().lower() == 'no'
    2125 ##
    2126 ##        L = lines[4].strip().split()
    2127 ##        assert L[0].strip().lower() == 'units'
    2128 ##        assert L[1].strip().lower() == 'meters'
    2129 ##
    2130 ##        L = lines[5].strip().split()
    2131 ##        assert L[0].strip().lower() == 'spheroid'
    2132 ##        assert L[1].strip().lower() == 'wgs84'
    2133 ##
    2134 ##        L = lines[6].strip().split()
    2135 ##        assert L[0].strip().lower() == 'xshift'
    2136 ##        assert L[1].strip().lower() == '500000'
    2137 ##
    2138 ##        L = lines[7].strip().split()
    2139 ##        assert L[0].strip().lower() == 'yshift'
    2140 ##        assert L[1].strip().lower() == '10000000'
    2141 ##
    2142 ##        L = lines[8].strip().split()
    2143 ##        assert L[0].strip().lower() == 'parameters'
    2144 ##
    2145 ##
    2146 ##        #Check asc file
    2147 ##        ascid = open(ascfile)
    2148 ##        lines = ascid.readlines()
    2149 ##        ascid.close()
    2150 ##
    2151 ##        L = lines[0].strip().split()
    2152 ##        assert L[0].strip().lower() == 'ncols'
    2153 ##        assert L[1].strip().lower() == '5'
    2154 ##
    2155 ##        L = lines[1].strip().split()
    2156 ##        assert L[0].strip().lower() == 'nrows'
    2157 ##        assert L[1].strip().lower() == '5'
    2158 ##
    2159 ##        L = lines[2].strip().split()
    2160 ##        assert L[0].strip().lower() == 'xllcorner'
    2161 ##        assert allclose(float(L[1].strip().lower()), 308500)
    2162 ##
    2163 ##        L = lines[3].strip().split()
    2164 ##        assert L[0].strip().lower() == 'yllcorner'
    2165 ##        assert allclose(float(L[1].strip().lower()), 6189000)
    2166 ##
    2167 ##        L = lines[4].strip().split()
    2168 ##        assert L[0].strip().lower() == 'cellsize'
    2169 ##        assert allclose(float(L[1].strip().lower()), cellsize)
    2170 ##
    2171 ##        L = lines[5].strip().split()
    2172 ##        assert L[0].strip() == 'NODATA_value'
    2173 ##        assert L[1].strip().lower() == '-9999'
    2174 ##
    2175 ##        #Check grid values
    2176 ##        for j in range(5):
    2177 ##            L = lines[6+j].strip().split()
    2178 ##            y = (4-j) * cellsize
    2179 ##            for i in range(5):
    2180 ##                assert allclose(float(L[i]), -i*cellsize - y)
    2181 ##
    2182 
    2183         fid.close()
    2184        
    2185         #Cleanup
    2186         #FIXME the file clean-up doesn't work (eg Permission Denied Error)
    2187         #Done (Ole) - it was because sww2ers didn't close it's sww file
    2188         os.remove(sww.filename)
    2189 
    2190 
    2191     def xxxtestz_sww2ers_real(self):
    2192         """Test that sww information can be converted correctly to asc/prj
    2193         format readable by e.g. ArcView
    2194         """
    2195 
    2196         import time, os
    2197         from Numeric import array, zeros, allclose, Float, concatenate
    2198         from Scientific.IO.NetCDF import NetCDFFile
    2199 
    2200 
    2201 
    2202 
    2203         #Export to ascii/prj files
    2204         sww2ers('karratha_100m.sww',
    2205                 quantity = 'depth',
    2206                 cellsize = 5,
    2207                 verbose = False)
    2208 
    22092169
    22102170
Note: See TracChangeset for help on using the changeset viewer.