def _trim_grid(ingrid): outgrid = Grid2D.copyFromGrid(ingrid) while np.isnan(outgrid._data).any(): nrows, ncols = outgrid._data.shape top = outgrid._data[0, :] bottom = outgrid._data[-1, :] left = outgrid._data[:, 0] right = outgrid._data[:, -1] ftop = np.isnan(top).sum() / ncols fbottom = np.isnan(bottom).sum() / ncols fleft = np.isnan(left).sum() / nrows fright = np.isnan(right).sum() / nrows side = np.argmax([ftop, fbottom, fleft, fright]) gdict = outgrid.getGeoDict().asDict() if side == 0: # removing top row outgrid._data = outgrid._data[1:, :] gdict['ymax'] -= gdict['dy'] gdict['ny'] -= 1 elif side == 1: # removing bottom row outgrid._data = outgrid._data[0:-1, :] gdict['ymin'] += gdict['dy'] gdict['ny'] -= 1 elif side == 2: # removing left column outgrid._data = outgrid._data[:, 1:] gdict['xmin'] += gdict['dx'] gdict['nx'] -= 1 elif side == 3: # removing right column outgrid._data = outgrid._data[:, 0:-1] gdict['xmax'] -= gdict['dx'] gdict['nx'] -= 1 geodict = GeoDict(gdict) outgrid = Grid2D(data=outgrid._data, geodict=geodict) return outgrid
def calcExposure(self, shakefile): """Calculate population exposure to shaking. Calculate population exposure to shaking, per country, multiplied by event-year per-capita GDP and alpha correction factor. Also multiply the internal population grid by GDP and alpha. :param shakefile: Path to ShakeMap grid.xml file. :returns: Dictionary containing country code (ISO2) keys, and values of 10 element arrays representing population exposure to MMI 1-10. Dictionary will contain an additional key 'Total', with value of exposure across all countries. """ #create a copy of population grid to hold population * gdp * alpha expdict = super(EconExposure, self).calcExposure(shakefile) self._econpopgrid = Grid2D.copyFromGrid(self._popgrid) econdict = {} isodata = self._isogrid.getData() eventyear = self.getShakeGrid().getEventDict()['event_timestamp'].year total = np.zeros((10, )) for ccode, exparray in expdict.items(): if ccode.find('Total') > -1: continue if ccode == 'UK': #unknown continue lossmodel = self._emploss.getModel(ccode) gdp, outccode = self._gdp.getGDP(ccode, eventyear) isocode = self._country.getCountry(ccode)['ISON'] alpha = lossmodel.alpha econarray = exparray * gdp * alpha cidx = (isodata == isocode) #multiply the population grid by GDP and alpha, so that when the loss model #queries the grid later, those calculations don't have to be re-done. self._econpopgrid._data[ cidx] = self._econpopgrid._data[cidx] * gdp * alpha econdict[ccode] = econarray total += econarray econdict['TotalEconomicExposure'] = total return econdict