def reviewLocalAlpha(self):
        alphas = self.alphas
        betas = self.betas
        if (not self.isBoundaryCell) and (self.totPoly != None):
            dirs = self.directions
            newalphas = np.ones(alphas.shape)
            newbetas = np.ones(betas.shape)
            for cstPoly in self.coastPolygons:
                # breakwater must intersect the cell
                cints = cstPoly.intersection(self.cell)
                if abUtils.greater(cints.area, 0):
                    # the breakwater must cross at least two sides of the cell
                    boundaryIntersect = cints.boundary.intersection(
                        self.cell.boundary)
                    if boundaryIntersect.__class__ is g.MultiLineString:
                        # the number of intersected cells of the neighborhood must be >= 3
                        nints = self._countIntersectNeighbors(cstPoly) + 1
                        plSz = abCellSize.abCellSize(cstPoly)
                        cstPolySizeKm = plSz.computeSizesKm(
                            [plSz.majorAxisDir])[0]
                        if nints >= self.thresholdNInts and cstPolySizeKm > self.minObstSizeKm:
                            cstSection = cstPoly.intersection(self.totPoly)
                            if not cstSection.__class__ is g.Polygon:
                                # skipping strange case
                                return alphas, betas

                            clSz = abCellSize.abCellSize(cstSection)
                            avgCoastDir = clSz.majorAxisDir

                            for idr in range(len(dirs)):
                                dr, alpha, beta = dirs[
                                    idr], alphas[:, idr], betas[:, idr]
                                drDelta = np.abs(
                                    abUtils.angleDiff(avgCoastDir, dr))
                                if drDelta > np.pi / 2.:
                                    drDelta = np.pi - drDelta
                                coeff = 1. - drDelta / (np.pi / 2.)
                                assert 0. <= coeff <= 1.
                                nalpha = alpha * coeff
                                nbeta = beta * coeff
                                newalphas[:, idr] = nalpha
                                newbetas[:, idr] = nbeta
                            # assuming a maximum one breakwater per cell
                            return newalphas, newbetas
        return alphas, betas
 def computeLocalOneCell(self, crd, geoCrd, cell):
   try:
     cellAlphaMtx = self.alphaMtx.getAlphaSubMatrix(cell)
     cellMtxCoversCell = cellAlphaMtx.coversPoly()
     localAlphaBetaExist = not cellAlphaMtx.empty()
     onLand = cellAlphaMtx.onLand()
     if cellMtxCoversCell and localAlphaBetaExist and not onLand:
       alphaEst = aEst.abSingleCellAlphaEstimator(cell, cellAlphaMtx, recalibFactor = self.locRecalibFactor)
       alphaEst.loginfo.debugPlotSave = True
       alphaEst.loginfo.contextStrs = ['local']
       betaEst = bEst.abSingleCellBetaEstimator(cell, cellAlphaMtx, 
                         recalibFactor = self.locRecalibFactor,
                         maxSubSections = self.betaMaxSubSections)
       cellSizeEst = csEst.abCellSize(cell)
       cellSizes = cellSizeEst.computeSizesKm(self.dirs)
       if min(cellSizes) < self.minSizeKm:
         return False, None, None, None, False, None, None, cell
       lalpha = []
       lbeta = []
       totallyBlocked = False
       if self.alphaMtx.hasFreqs:
         raise abUtils.abException('alpha high resolution matrix with frequencies unsupported')
       else:
         fqalpha0 = []
         fqbeta0 = []
         f = self.freqs[0]
         #for d in self.dirs:
         for d in self.computationDirs:
           a = alphaEst.computeAlpha(d, f)
           fqalpha0.append(a)
           b = betaEst.computeBeta(d, f)
           fqbeta0.append(b)
           totallyBlocked = totallyBlocked or abUtils.isClose(a, 0)
         fqalpha = np.interp(self.dirs, self.computationDirs, fqalpha0, period = 2*np.pi)
         fqbeta = np.interp(self.dirs, self.computationDirs, fqbeta0, period = 2*np.pi)
         lalpha = [fqalpha for f in self.freqs]
         lbeta = [fqbeta for f in self.freqs]
       meanAlpha = np.mean(lalpha)
       meanBeta = np.mean(lbeta)
       if (meanAlpha < 1.) and (meanAlpha > self.minAvgAlpha and meanBeta > self.minAvgBeta):
         return True, lalpha, lbeta, cellSizes, totallyBlocked, crd, geoCrd, cell
       else:
         # This cell is on land. The resolved component of the model should take care of this
         return False, None, None, None, False, None, None, cell
     else:
       return False, None, None, None, False, None, None, cell
   except:
     raise abUtils.abException("".join(traceback.format_exception(*sys.exc_info())))
 def __init__(self, cell, neighbors, coastPolygons, directions, alphas,
              betas):
     self.cell = cell
     self.neighbors = [c for c in neighbors if c != cell]
     totPoly = cell
     for cl in self.neighbors:
         totPoly = totPoly.union(cl)
     if totPoly.__class__ != g.Polygon:
         # totPoly = totPoly.convex_hull
         totPoly = None
     self.totPoly = totPoly
     self.isBoundaryCell = totPoly.boundary.intersects(
         cell.boundary) if (totPoly != None) else True
     self.coastPolygons = [g.Polygon(p) for p in coastPolygons]
     self.directions = directions
     self.alphas = np.array(alphas)
     self.betas = np.array(betas)
     self.thresholdNInts = 3
     clSz = abCellSize.abCellSize(cell)
     self.minObstSizeKm = clSz.computeSizesKm([clSz.majorAxisDir])[0] * 2.
Exemplo n.º 4
0
    def test2(self):
        crds = [[0, 0], [1, 0], [1, 2], [0, 2], [0, 0]]
        rotangle = np.pi / 6
        expd0 = 1.
        expdPi_2 = 2.
        expdPi_4 = (1. + 5.**.5) / 2.
        ang2 = rotangle + np.arctan(4.)
        expdang20 = 2.118034
        expdang21 = 1.154508497

        cell = g.Polygon(crds)
        cell = a.rotate(cell, rotangle, use_radians=True)
        cs = abCellSize(cell)

        d0 = cs.computeSize(0. + rotangle)
        d1 = cs.computeSize(np.pi / 2. + rotangle)
        d2 = cs.computeSize(np.pi + rotangle)
        d3 = cs.computeSize(np.pi * 3. / 2. + rotangle)
        d4 = cs.computeSize(2. * np.pi + rotangle)
        self.assertAlmostEqual(d0, expd0)
        self.assertAlmostEqual(d2, expd0)
        self.assertAlmostEqual(d1, expdPi_2)
        self.assertAlmostEqual(d3, expdPi_2)

        dPi_40 = cs.computeSize(np.pi / 4 + rotangle)
        dPi_41 = cs.computeSize(np.pi / 4 + np.pi / 2. + rotangle)
        dPi_42 = cs.computeSize(np.pi / 4 + np.pi + rotangle)
        dPi_43 = cs.computeSize(np.pi / 4 + np.pi * 3. / 2. + rotangle)
        self.assertAlmostEqual(dPi_40, expdPi_4)
        self.assertAlmostEqual(dPi_41, expdPi_4)
        self.assertAlmostEqual(dPi_42, expdPi_4)
        self.assertAlmostEqual(dPi_43, expdPi_4)

        dang20 = cs.computeSize(ang2)
        dang21 = cs.computeSize(ang2 + np.pi / 2.)
        dang22 = cs.computeSize(ang2 + np.pi)
        dang23 = cs.computeSize(ang2 + np.pi * 3. / 2.)
        self.assertAlmostEqual(dang20, expdang20)
        self.assertAlmostEqual(dang21, expdang21)
        self.assertAlmostEqual(dang22, expdang20)
        self.assertAlmostEqual(dang23, expdang21)
  def computeShadowOneCell(self, crd, geoCrd, cell):
    try:
      cellNeighbors = self.grid.getNeighbors(cell)
      if not len(cellNeighbors):
        self._print('cell ' + str(crd) + ' has no neighbors. Skipping')
        return False, None, None, None, None, None
        
      neighTotPoly = cell
      for p in cellNeighbors:
        neighTotPoly = neighTotPoly.union(p)
      if neighTotPoly.__class__ != g.Polygon:
        neighTotPoly = neighTotPoly.convex_hull - cell
      if neighTotPoly.__class__ != g.Polygon:
        self._print('cell ' + str(crd) + ': impossible to estimate neighboring polygon. Skipping')
        return False, None, None, None, None, None
      totPolyAlphaMtx = self.alphaMtx.getAlphaSubMatrix(neighTotPoly)
      shadAlphaBetaExist =  not totPolyAlphaMtx.isNull() and not totPolyAlphaMtx.empty()
      onLand = totPolyAlphaMtx.onLand() if shadAlphaBetaExist else False
      if shadAlphaBetaExist and not onLand:
        lalpha0 = []
        lbeta0 = []
        blockedCellCount = sum([1 for cl in cellNeighbors if tuple(cl.boundary.coords[:]) in self.totallyBlockedCells])
        obstrAlleviationEnabled = blockedCellCount <= self.shadowAlleviationMaxBlockedNeighborCount
        cellSizeEst = csEst.abCellSize(cell)
        cellSizes = cellSizeEst.computeSizesKm(self.dirs)
        if min(cellSizes) < self.minSizeKm:
          return False, None, None, None, None, None
        for dr in self.computationDirs:
          upstreamPoly = upe.getUpstreamPoly(cell, neighTotPoly, dr)
          upstreamPolyIsPolygon = upstreamPoly.__class__ == g.Polygon
          if upstreamPolyIsPolygon and (not abUtils.isClose(upstreamPoly.area, 0)):
            alphaEst = aEst.abSingleCellAlphaEstimator(upstreamPoly, self.alphaMtx, cell,\
                 kshape = self.shadowKShape, obstrAlleviationParam = self.shadowAlphaAlleviationParam, recalibFactor = self.shadRecalibFactor)
            alphaEst.obstrAlleviationEnabled = obstrAlleviationEnabled
            alphaEst.loginfo.debugPlotSave = False
            alphaEst.loginfo.contextStrs = ['shadow']
            betaEst = bEst.abSingleCellBetaEstimator(upstreamPoly, self.alphaMtx, kshape = self.shadowKShape, recalibFactor = self.shadRecalibFactor)
            betaEst.obstrAlleviationEnabled = obstrAlleviationEnabled
            diralpha = []
            lalpha0.append(diralpha)
            dirbeta = []
            lbeta0.append(dirbeta)
            if self.alphaMtx.hasFreqs:
              raise abUtils.abException('alpha high resolution matrix with frequencies unsupported')
             #for fq in self.freqs:
             #  alpha = alphaEst.computeAlpha(dr, fq)
             #  diralpha.append(alpha)
             #  beta = max(betaEst.computeBeta(dr, fq), alpha)
             #  dirbeta.append(beta)
            else:
              fq = self.freqs[0]
              alpha = alphaEst.computeAlpha(dr, fq) if not alphaEst.alphaMtx.nullOrEmpty() else 1
              diralpha.extend([alpha for f in self.freqs])
              beta = max(betaEst.computeBeta(dr, fq), alpha) if not betaEst.alphaMtx.nullOrEmpty() else 1
              dirbeta.extend([beta for f in self.freqs])
          else:
            alpha, beta = 1, 1
            diralpha = [alpha for f in self.freqs]
            dirbeta = [beta for f in self.freqs]
            lalpha0.append(diralpha)
            lbeta0.append(dirbeta)

        lalpmtx0 = np.array(lalpha0).transpose()
        lbetmtx0 = np.array(lbeta0).transpose()

        lalpha, lbeta = [], []
        for alparr, betarr in zip(lalpmtx0, lbetmtx0):
          alp = np.interp(self.dirs, self.computationDirs, alparr, period = 2*np.pi)
          lalpha.append(alp)
          bet = np.interp(self.dirs, self.computationDirs, betarr, period = 2*np.pi)
          lbeta.append(bet)

        lalpmtx = np.array(lalpha)
        lbetmtx = np.array(lbeta)

        meanAlpha = np.mean(lalpmtx)
        if meanAlpha < 1.:
          return True, lalpmtx, lbetmtx, cellSizes, crd, geoCrd
        else:
          return False, None, None, None, None, None
      else:
        return False, None, None, None, None, None
    except:
      raise abUtils.abException("".join(traceback.format_exception(*sys.exc_info())))