Exemple #1
0
    def testLocal(self):
        abSingleCellAlphaEstimator.defaultKShape = 1
        abSingleCellBetaEstimator.defaultKShape = 1
        abClEstMod.defaultShadowAlphaAlleviationParam = 1

        rectGridBld = grdBld(minX=1,
                             minY=10,
                             dx=1,
                             dy=1,
                             nx=3,
                             ny=2,
                             minXYIsCentroid=False)
        hiresxs = np.arange(0, 4, .1)
        hiresys = np.arange(9, 13, .1)
        alphas = np.ones([len(hiresys), len(hiresxs)])
        xobstr = np.array([25])
        yobstr = np.arange(0, len(hiresys), 2)
        xyobstr = np.meshgrid(xobstr, yobstr)
        alphas[xyobstr[1], xyobstr[0]] = 0
        hrmtx = hiResAMtx(hiresxs, hiresys, alphas)
        coastalCellDetector = self.getMockHiResAlphaMtxAndCstCellDet()
        grd = rectGridBld.buildGrid(hrmtx, coastalCellDetector)

        dirs = [0., np.pi / 2., np.pi, 3. * np.pi / 2.]
        freqs = [.1, .2, .3]
        cellEst = abCellsEstimator(grd, hrmtx, dirs, freqs, None)
        cellEstP = abCellsEstimatorParallel(cellEst, 4, None)

        loccrd, locgeocrd, localpha, locbeta, sizes = cellEstP.computeLocalAlphaBeta(
        )
        self.assertEqual(2, len(loccrd))
        self.assertTrue((np.array([1, 0]) == loccrd[0]).all())
        self.assertTrue((np.array([1, 1]) == loccrd[1]).all())
        self.assertEqual(2, len(locgeocrd))
        self.assertEqual([(2.5, 10.5), (2.5, 11.5)], locgeocrd)

        self.assertEqual(2, len(localpha))
        a0 = localpha[0]
        a00 = a0[0]
        a01 = a0[-1]
        self.assertTrue((a00 == a01).all())
        self.assertTrue(np.allclose(a00, np.array([.5, .9, .5, .9])))
        self.assertTrue(np.allclose(localpha[0], localpha[1]))

        self.assertEqual(2, len(locbeta))
        b0 = locbeta[0]
        b00 = b0[0]
        b01 = b0[-1]
        self.assertTrue((b00 == b01).all())
        self.assertTrue(
            np.allclose(b00, np.array([.75, .9, .7, .91]), rtol=.05))
        self.assertTrue(np.allclose(locbeta[0], locbeta[1]))
  def testShadow(self):
    abSingleCellAlphaEstimator.defaultKShape = 1
    abSingleCellBetaEstimator.defaultKShape = 1
    abClEstMod.defaultShadowAlphaAlleviationParam = 1

    rectGridBld = grdBld(minX = 1, minY = 10, dx = 1, dy = 1, nx = 3, ny = 2, minXYIsCentroid = False)
    hiResAlphaMtx = self.getMockHiResAlphaMtxAndCstCellDet()
    coastalCellDetector = self.getMockHiResAlphaMtxAndCstCellDet()
    grd = rectGridBld.buildGrid(hiResAlphaMtx, coastalCellDetector)
    hiresxs = np.arange(0, 4, .1)
    hiresys = np.arange(9, 13, .1)
    alphas = np.ones([len(hiresys), len(hiresxs)])
    xobstr = np.array([25])
    yobstr = np.arange(0, len(hiresys), 2)
    xyobstr = np.meshgrid(xobstr, yobstr)
    alphas[xyobstr[1], xyobstr[0]] = 0
    hrmtx = hiResAMtx(hiresxs, hiresys, alphas)
  
    dirs = [0., np.pi/2., np.pi, 3.*np.pi/2.]
    freqs = [.1, .2, .3]
    cellEst = abCellsEstimator(grd, hrmtx, dirs, freqs, abOptions(shadRecalibFactor=1))
    
    cellEst.computeLocalAlphaBeta()
    shdcrd, shdgeocrd, shdalpha, shdbeta, sizes = cellEst.computeShadowAlphaBeta()
    self.assertEqual(6, len(shdcrd))
    self.assertTrue((np.array([0, 0]) == shdcrd[0]).all())
    self.assertTrue((np.array([0, 1]) == shdcrd[1]).all())
    self.assertTrue((np.array([1, 0]) == shdcrd[2]).all())
    self.assertTrue((np.array([1, 1]) == shdcrd[3]).all())
    self.assertTrue((np.array([2, 0]) == shdcrd[4]).all())
    self.assertTrue((np.array([2, 1]) == shdcrd[5]).all())
    self.assertEqual(6, len(shdgeocrd))
    self.assertEqual([(1.5, 10.5), (1.5, 11.5), (2.5, 10.5), (2.5, 11.5), (3.5, 10.5), (3.5, 11.5)], shdgeocrd)
    
    expshdalpha = np.array(\
    [np.array([[ 1. ,  1. ,  0.5,  1. ],\
               [ 1. ,  1. ,  0.5,  1. ],\
               [ 1. ,  1. ,  0.5,  1. ]]),\
     np.array([[ 1. ,  1. ,  0.5,  1. ],\
               [ 1. ,  1. ,  0.5,  1. ],\
               [ 1. ,  1. ,  0.5,  1. ]]),\
     np.array([[ 1. ,  1. ,  1. ,  0.9],\
               [ 1. ,  1. ,  1. ,  0.9],\
               [ 1. ,  1. ,  1. ,  0.9]]),\
     np.array([[ 1. ,  0.9,  1. ,  1. ],\
               [ 1. ,  0.9,  1. ,  1. ],\
               [ 1. ,  0.9,  1. ,  1. ]]),\
     np.array([[ 0.5,  1. ,  1. ,  1. ],\
               [ 0.5,  1. ,  1. ,  1. ],\
               [ 0.5,  1. ,  1. ,  1. ]]),\
     np.array([[ 0.5,  1. ,  1. ,  1. ],\
               [ 0.5,  1. ,  1. ,  1. ],\
               [ 0.5,  1. ,  1. ,  1. ]])])
    self.assertTrue(np.allclose(expshdalpha, np.array(shdalpha)))

    expshdbeta = np.array(\
    [np.array([[ 1. ,  1. ,  0.7,  1. ],
               [ 1. ,  1. ,  0.7,  1. ],\
               [ 1. ,  1. ,  0.7,  1. ]]),\
     np.array([[ 1. ,  1. ,  0.7,  1. ],\
               [ 1. ,  1. ,  0.7,  1. ],\
               [ 1. ,  1. ,  0.7,  1. ]]),\
     np.array([[ 1. ,  1. ,  1. ,  0.91],\
               [ 1. ,  1. ,  1. ,  0.91],\
               [ 1. ,  1. ,  1. ,  0.91]]),\
     np.array([[ 1. ,  0.9,  1. ,  1. ],
               [ 1. ,  0.9,  1. ,  1. ],\
               [ 1. ,  0.9,  1. ,  1. ]]),\
     np.array([[ 0.75,  1.  ,  1.  ,  1.  ],\
               [ 0.75,  1.  ,  1.  ,  1.  ],\
               [ 0.75,  1.  ,  1.  ,  1.  ]]),\
     np.array([[ 0.75,  1.  ,  1.  ,  1.  ],
               [ 0.75,  1.  ,  1.  ,  1.  ],\
               [ 0.75,  1.  ,  1.  ,  1.  ]])])
    self.assertTrue(np.allclose(expshdbeta, np.array(shdbeta), rtol = .05))
Exemple #3
0
def _abEstimateAndSave(dirs,
                       freqs,
                       gridName,
                       grid,
                       highResolutionBathyMatrix,
                       outputDirectory,
                       nParWorker,
                       abOptions=None):
    """
  _abEstimateAndSave:
  computes all that is needed by alphaBetaLab, given:
  - a spectral grid (dirs, freqs)
  - a spatial mesh (gridName, grid, that is an abGrid object containing a list of polygons)
  - a high resolution matrix of alpha, got from a high resolution bathymetry
  - outputDirectory, directory to save the output files.
  """

    saveLocalOnly = getOption(abOptions, 'saveLocalOnly', False)
    savePropSchemeFile = getOption(abOptions, 'savePropSchemeFile', False)

    parallel = nParWorker > 1
    advObstrLocFileName = 'obstructions_local.' + gridName + '.in'
    advObstrShdFileName = 'obstructions_shadow.' + gridName + '.in'

    advObstrLocFilePath = os.path.join(outputDirectory, advObstrLocFileName)
    advObstrShdFilePath = os.path.join(outputDirectory, advObstrShdFileName)

    t = time.time()

    try:
        cellEst = abCellsEstimator.abCellsEstimator(grid,
                                                    highResolutionBathyMatrix,
                                                    dirs, freqs, abOptions)
        if parallel:
            cellEstP = abCellsEstimatorParallel.abCellsEstimatorParallel(
                cellEst, nParWorker, abOptions)
            cellEst.grid.buildNeighCache()
            locParams = cellEstP.computeLocalAlphaBeta()
            if not saveLocalOnly:
                shdParams = cellEstP.computeShadowAlphaBeta()
            else:
                shdParams = None, None, None, None, None
        else:
            locParams = cellEst.computeLocalAlphaBeta()
            if not saveLocalOnly:
                shdParams = cellEst.computeShadowAlphaBeta()
            else:
                shdParams = None, None, None, None, None
        locCoords, locAlphas = locParams[0], locParams[2]

        try:
            os.makedirs(outputDirectory)
        except:
            pass

        wwiiiObstrFileSaver = abWwiiiObstrFileSaver.abWwiiiObstrFileSaver(
            *(locParams + shdParams))
        if saveLocalOnly:
            wwiiiObstrFileSaver.saveLocFile(advObstrLocFilePath)
        else:
            wwiiiObstrFileSaver.saveFiles(advObstrLocFilePath,
                                          advObstrShdFilePath)

        if savePropSchemeFile:
            stdObstrFileName = gridName + '.obstr_lev1'
            stdObstrFilePath = os.path.join(propSchemeObstrDestDir,
                                            stdObstrFileName)
            propSchObstrFileSaver = abWwiiiPropSchObstrFileSaver.\
                     abWwiiiPropSchObstrFileSaver(grid, dirs, locCoords, locAlphas)
            propSchObstrFileSaver.saveFile(stdObstrFilePath)
    finally:
        timeElapsed = time.time() - t
        print('Complete. Time elapsed in seconds: ' + str(timeElapsed))
        print
        printOpts()