Changeset 9320


Ignore:
Timestamp:
Sep 3, 2014, 3:47:05 PM (11 years ago)
Author:
davies
Message:

Adding a template tsunami script + associated source adjustments

Location:
trunk
Files:
29 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/shallow_water/swDE1_domain_ext.c

    r9318 r9320  
    20922092   
    20932093  double timestep;
    2094   int max_flux_update_frequency;
    20952094 
    20962095  if (!PyArg_ParseTuple(args, "Od", &domain, &timestep)) {
  • trunk/anuga_core/source/anuga/utilities/quantity_setting_functions.py

    r9299 r9320  
    55"""
    66import copy
     7import os
     8import anuga.utilities.spatialInputUtil as su
    79
    810def make_nearestNeighbour_quantity_function(quantity_xyValueIn, domain, threshold_distance = 9.0e+100, background_value = 9.0e+100):
     
    7981             
    8082        poly_fun_pairs = [ [p0, f0], [p1, f1], ...]
    81                     where fi is a function, or a constant, or the name of a gdal-compatible rasterFile;
    82                     and pi is a polygon, or None, or 'All' (or it can be 'Extent' in the case that fi is a rasterFile name)
     83
     84                    Where:
     85
     86                      fi is a function, or a constant, or the name of a
     87                        gdal-compatible rasterFile, or a numpy array with 3 columns;
     88
     89                      pi is a polygon, or a polygon filename (shapefile or a csv format that anuga.read_polygon will read),
     90                        or None, or 'All' (or it can be 'Extent' in the case that fi is a
     91                        rasterFile name)
    8392             
    8493        IMPORTANT: When polygons overlap, the first elements of the list are given priority.
     
    101110                  pi are polygons where we want to use fi inside
    102111                  SPECIAL pi CASES:
     112                  If pi is a filename ending in .shp or a csv format that anuga.read_polygon can read, we assume it contains a polygon we have to read
    103113                  If any pi = 'All', then we assume that ALL unset points are set
    104114                     using the function. This CAN ONLY happen in the last [fi,pi] pair where pi is
     
    160170                    # Here fi MUST be a gdal-compatible raster
    161171                    # Then we get the extent from the raster itself
    162                     import anuga.utilities.spatialInputUtil as su
    163172                    pi_path=su.getRasterExtent(fi,asPolygon=True)
     173                elif( type(pi)==str and os.path.isfile(pi) ):
     174                    # pi is a file
     175                    pi_path=su.read_polygon(pi)
    164176                else:
     177                    # pi is the actual polygon data
    165178                    pi_path=pi
     179
    166180                notSet=(isSet==0.).nonzero()[0]
    167181                fInds = inside_polygon(xy_array_trans[notSet,:], pi_path)
  • trunk/anuga_core/source/anuga/utilities/spatialInputUtil.py

    r9299 r9320  
    66    readShp_1PolyGeo, readShp_1LineGeo -- read SIMPLE shapefile geometries into ANUGA as a list of points.
    77                                          The supported geometries are quite restricted, see help
     8    read_polygon -- Reads either a polygon or line shapefile or a csv as a polygon
    89    readShpPointsAndAttributes -- read a multi-point shapefile with its attributes into ANUGA
    910
     
    4546import sys
    4647import os
     48import os.path
    4749import copy
    4850import struct
    4951import numpy
    5052#from matplotlib import path
     53import anuga
    5154from anuga.geometry.polygon import inside_polygon
    5255
     
    8285        #dataSrc=ogr.Open(shapefile)
    8386        layer=dataSrc.GetLayer()
    84        
     87
     88        # Check it is a polygon
     89        layerType=ogr.GeometryTypeToName(layer.GetGeomType())
     90        if not layerType=='Polygon':
     91            msg= shapefile +' is not a polygon shapefile'
     92            raise Exception, msg
     93
    8594        # Need a single polygon
    8695        try:
     
    118127        #dataSrc=ogr.Open(shapefile)
    119128        layer=dataSrc.GetLayer()
    120        
     129     
     130        # Check it is a line
     131        layerType=ogr.GeometryTypeToName(layer.GetGeomType())
     132        if not layerType=='Line String':
     133            msg= shapefile +' is not a line shapefile'
     134            raise Exception, msg
     135
    121136        # Need a single line
    122137        try:
    123             assert(len(layer)==1)
     138            assert len(layer)==1
    124139        except:
    125140            print shapefile
     
    134149           
    135150    ####################
    136    
     151    def read_polygon(filename):
     152        """
     153
     154        Read a shapefile (polygon or line) or an anuga_polygon csv file as an anuga polygon
     155
     156        Try to automatically do the correct thing based on the filename
     157
     158        """
     159        # Check the file exists
     160        msg= 'Could not read '+ filename
     161        assert os.path.isfile(filename), msg
     162
     163        # Get the file extension type
     164        fileNameNoExtension , fileExtension = os.path.splitext(filename)
     165
     166        if fileExtension == '.shp':
     167            # Read as either a polygon or line shapefile
     168            try:
     169                outPol=readShp_1PolyGeo(filename)
     170                assert len(outPol)>1
     171            except:
     172                try:
     173                    outPol= readShp_1LineGeo(filename)
     174                    assert len(outPol)>1
     175                except:
     176                    msg= 'Could not read '+ filename +' as either polygon or line shapefile'
     177        else:
     178            try:
     179                # Read as an anuga polygon file
     180                outPol=anuga.read_polygon(filename)
     181            except:
     182                msg = 'Failed reading polygon '+ filename + ' with anuga.utilities.spatialInputUtils.read_polygon'
     183                raise Exception, msg
     184
     185        return outPol
     186
     187    ####################
     188
    137189    def readShpPtsAndAttributes(shapefile):
    138190        """
     
    161213   
    162214        return [pts, attribute, attributeNames]
     215
     216    ########################################
     217    def readShpPts(shapefile):
     218        """
     219            Wrapper around readShpPtsAndAttributes
     220            Only get the points
     221        """
     222
     223        out=readShpPtsAndAttributes(shapefile)[0]
     224        return out
     225
     226    ########################################
     227    def read_points(filename):
     228        """
     229            Reads x,y geometries from a point shapefile or csv file (anuga polygon type format),
     230            and returns as a list of lists
     231        """
     232        # Check the file exists
     233        msg= 'Could not read '+ filename
     234        assert os.path.isfile(filename), msg
     235
     236        # Get the file extension type
     237        fileNameNoExtension , fileExtension = os.path.splitext(filename)
     238
     239        if fileExtension=='.shp':
     240            try:
     241                points = readShpPts(filename)
     242            except:
     243                msg = 'Could not read points from ' + filename
     244                raise Exception, msg
     245        else:
     246            # Assume txt format
     247            try:
     248                #points=numpy.genfromtxt(filename,delimiter=',',skip_header=1)
     249                #points=points[:,0:2].tolist()
     250                points=anuga.read_polygon(filename)
     251            except:
     252                msg = 'Could not read points from ' + filename +\
     253                      '. Make sure it has a single header row, with comma separator, and the first 2 columns are x,y'
     254                raise Exception, msg
     255        return points
    163256   
    164257    ########################################
     
    729822        breakLines=copy.copy(breakLinesIn)
    730823        riverWalls=copy.copy(riverWallsIn)
    731    
     824 
     825        # Quick exit
     826        if (breakLines == {}) and (riverWalls == {}):
     827            return [bounding_polygon, breakLines, riverWalls]
     828 
    732829        # Clean intersections of breakLines with itself
    733830        if(verbose):
     
    9051002   
    9061003    #########################################
    907     def readListOfBreakLines(shapefileList):
    908         """
    909             Take a list with the names of shapefiles
     1004    def readListOfBreakLines(fileList):
     1005        """
     1006            Take a list with the names of shapefiles or anuga_polygon csv files
    9101007       
    9111008            They are assumed to be '2D breaklines', so we just read their
     
    9141011            Read them in
    9151012           
    916             INPUT: shapefileList -- a list of shapefile names [e.g. from glob.glob('GIS/Breaklines/*.shp')]
     1013            INPUT: fileList -- a list of shapefile and/or anuga_polygon csv filenames [e.g. from glob.glob('GIS/Breaklines/*.shp')]
    9171014   
    9181015            OUTPUT: dictionary with breaklines [filenames are keys]
     
    9201017   
    9211018        allBreakLines={}
    922         for shapefile in shapefileList:
    923             allBreakLines[shapefile]=readShp_1LineGeo(shapefile)
     1019        for shapefile in fileList:
     1020            allBreakLines[shapefile]=read_polygon(shapefile) #readShp_1LineGeo(shapefile)
    9241021       
    9251022        return allBreakLines
Note: See TracChangeset for help on using the changeset viewer.