def test_ValidCellNum(self): """Testing validCellNum""" knownValues = ((-10, False), (0, True), (100, True), (175, True), (176, False), (2000, False)) for cell, valid in knownValues: result = statutils.validCellNum(cell, self.gridLimit, self.gridSpace) self.assertEqual(result, valid)
def extractParameters(self, cellNum): """ Extracts cyclone parameters for a given cell """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): raise ValueError, "Invalid input on cellNum: cell number is out of range" lon = self.lonlat[:, 0] lat = self.lonlat[:, 1] wLon, nLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) eLon = wLon + self.gridSpace["x"] sLat = nLat - self.gridSpace["y"] indij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon)) self.bearing = myutils.removeNum(self.bearing_list[indij]) self.speed = myutils.removeNum(self.speed_list[indij]) self.pressure = myutils.removeNum(self.pressure_list[indij]) self.bearingRate = myutils.removeNum(self.bearingRate_list[indij]) self.speedRate = myutils.removeNum(self.speedRate_list[indij]) self.pressureRate = myutils.removeNum(self.pressureRate_list[indij])
def extractParameters(self, cellNum): """ Extracts cyclone parameters for a given cell """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): raise ValueError, 'Invalid input on cellNum: cell number is out of range' lon = self.lonlat[:,0] lat = self.lonlat[:,1] wLon, nLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) eLon = wLon + self.gridSpace['x'] sLat = nLat - self.gridSpace['y'] indij = np.where(((lat >= sLat) & (lat < nLat)) & \ (lon >= wLon) & (lon < eLon)) self.bearing = myutils.removeNum(self.bearing_list[indij]) self.speed = myutils.removeNum(self.speed_list[indij]) self.pressure = myutils.removeNum(self.pressure_list[indij]) self.bearingRate = myutils.removeNum(self.bearingRate_list[indij]) self.speedRate = myutils.removeNum(self.speedRate_list[indij]) self.pressureRate = myutils.removeNum(self.pressureRate_list[indij])
def extractParameter(self, cellNum): """extractParameter(cellNum): Extracts the cyclone parameter data for the given cell. If the population of a cell is insufficient for generating a PDF, the bounds of the cell are expanded until the population is sufficient. Null/missing values are removed. """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): self.logger.critical("Invalid input on cellNum: cell number %i is out of range"%cellNum) raise InvalidArguments, 'Invalid input on cellNum: cell number is out of range' lon = self.lonLat[:,0] lat = self.lonLat[:,1] cellLon, cellLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) wLon = cellLon eLon = cellLon + self.gridSpace['x'] nLat = cellLat sLat = cellLat - self.gridSpace['y'] indij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon)) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) while np.size(self.parameter) <= self.minSamplesCell: self.logger.debug("Insufficient samples. Increasing the size of the cell") wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) if (wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last): errMsg = "Insufficient grid points in selected domain to " \ + "estimate storm statistics - please select a larger " \ + "domain. Samples = %i / %i" % (np.size(self.parameter), self.minSamplesCell) self.logger.critical(errMsg) raise StopIteration, errMsg indij = np.where(((lat >= sLat) & (lat < nLat)) & ((lon >= wLon) & (lon < eLon))) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) # Check to see if all values in the array are the same. If the # values are the same, bandwidth would be 0, and therefore KDE # cannot proceed while self.parameter.max() == self.parameter.min(): self.logger.debug("Parameter values appear to be the same. Increasing the size of the cell") wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) if (wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last): errMsg = "Insufficient grid points in selected domain to estimate storm statistics - please select a larger domain." self.logger.critical(errMsg) raise StopIteration, errMsg indij = np.where(((lat >= sLat) & (lat < nLat)) & ((lon >= wLon) & (lon < eLon))) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) self.logger.debug("Number of valid observations in cell %s : %s" % (str(cellNum), str(np.size(self.parameter))))
def extractParameter(self, cellNum, onLand): """ Extracts the cyclone parameter data for the given cell. If the population of a cell is insufficient for generating a PDF, the bounds of the cell are expanded until the population is sufficient. Null/missing values are removed. :param int cellNum: The cell number to process. :returns: None. The :attr:`parameter` attribute is updated. :raises InvalidArguments: if the cell number is not valid (i.e. if it is outside the possible range of cell numbers). """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): self.logger.critical("Invalid input on cellNum: cell number %i is out of range"%cellNum) raise InvalidArguments, 'Invalid input on cellNum: cell number %i is out of range'%cellNum cellLon, cellLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) wLon = cellLon eLon = cellLon + self.gridSpace['x'] nLat = cellLat sLat = cellLat - self.gridSpace['y'] lon = self.lonLat[:,0] lat = self.lonLat[:,1] lsflag = self.lonLat[:,2] if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag>0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag==0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) while np.size(p) <= self.minSample: wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) # Check if grid has reached maximum extent if (wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last): if onLand: if not self.domain_warning_raised: self.domain_warning_raised = True self.logger.warning("Insufficient grid points over land in selected domain to estimate storm statistics - reverting to statistics for open ocean.") return self.extractParameter(cellNum, False) else: errMsg = ("Insufficient grid points in selected " "domain to estimate storm statistics - " "please select a larger domain.") self.logger.critical(errMsg) raise StopIteration, errMsg if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag>0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag==0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) # Check to see if all values in the np.array are the same. If the values # are the same, bandwidth would be 0, and therefore KDE cannot be generated while p.max() == p.min(): wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) # Check if grid has reached maximum extent if (wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last): if onLand: if not self.domain_warning_raised: self.domain_warning_raised = True self.logger.warning("Insufficient grid points over land in selected domain to estimate storm statistics - reverting to statistics for open ocean.") return self.extractParameter(cellNum, False) else: errMsg = "Insufficient grid points in selected domain to estimate storm statistics - please select a larger domain." self.logger.critical(errMsg) raise StopIteration, errMsg if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag>0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag==0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) return p
def extractParameter(self, cellNum, onLand): """ Extracts the cyclone parameter data for the given cell. If the population of a cell is insufficient for generating a PDF, the bounds of the cell are expanded until the population is sufficient. Null/missing values are removed. :param int cellNum: The cell number to process. :returns: None. The :attr:`parameter` attribute is updated. :raises IndexError: if the cell number is not valid (i.e. if it is outside the possible range of cell numbers). """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): self.logger.critical( "Invalid input on cellNum: cell number %i is out of range" % cellNum) raise IndexError, 'Invalid input on cellNum: cell number %i is out of range' % cellNum cellLon, cellLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) wLon = cellLon eLon = cellLon + self.gridSpace['x'] nLat = cellLat sLat = cellLat - self.gridSpace['y'] lon = self.lonLat[:, 0] lat = self.lonLat[:, 1] lsflag = self.lonLat[:, 2] if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag > 0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag == 0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) while np.size(p) <= self.minSample: wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) # Check if grid has reached maximum extent if (wLon == wLon_last) & (eLon == eLon_last) & ( nLat == nLat_last) & (sLat == sLat_last): if onLand: if not self.domain_warning_raised: self.domain_warning_raised = True self.logger.warning( "Insufficient grid points over land in selected domain to estimate storm statistics - reverting to statistics for open ocean." ) return self.extractParameter(cellNum, False) else: errMsg = ("Insufficient grid points in selected " "domain to estimate storm statistics - " "please select a larger domain.") self.logger.critical(errMsg) raise StopIteration, errMsg if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag > 0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag == 0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) # Check to see if all values in the np.array are the same. If the values # are the same, bandwidth would be 0, and therefore KDE cannot be generated while p.max() == p.min(): wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) # Check if grid has reached maximum extent if (wLon == wLon_last) & (eLon == eLon_last) & ( nLat == nLat_last) & (sLat == sLat_last): if onLand: if not self.domain_warning_raised: self.domain_warning_raised = True self.logger.warning( "Insufficient grid points over land in selected domain to estimate storm statistics - reverting to statistics for open ocean." ) return self.extractParameter(cellNum, False) else: errMsg = "Insufficient grid points in selected domain to estimate storm statistics - please select a larger domain." self.logger.critical(errMsg) raise StopIteration, errMsg if onLand: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag > 0)) else: ij = np.where(((lat >= sLat) & (lat < nLat)) & (lon >= wLon) & (lon < eLon) & (lsflag == 0)) p_ = self.param[ij] p = stats.statRemoveNum(np.array(p_), self.missingValue) return p
def extractParameter(self, cellNum): """ Extracts the cyclone parameter data for the given cell. If the population of a cell is insufficient for generating a PDF, the bounds of the cell are expanded until the population is sufficient. Null/missing values are removed. :param int cellNum: The cell number to process. :returns: None. The :attr:`parameter` attribute is updated. :raises IndexError: if the cell number is not valid (i.e. if it is outside the possible range of cell numbers). """ if not stats.validCellNum(cellNum, self.gridLimit, self.gridSpace): self.logger.critical(("Invalid input on cellNum: " "cell number %i is out of range")%cellNum) raise IndexError('Invalid input on cellNum: ' 'cell number is out of range') lon = self.lonLat[:, 0] lat = self.lonLat[:, 1] cellLon, cellLat = stats.getCellLonLat(cellNum, self.gridLimit, self.gridSpace) wLon = cellLon eLon = cellLon + self.gridSpace['x'] nLat = cellLat sLat = cellLat - self.gridSpace['y'] indij = np.where(((lat >= sLat) & (lat < nLat)) & ((lon >= wLon) & (lon < eLon))) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) while np.size(self.parameter) <= self.minSamplesCell: self.logger.debug(("Insufficient samples. Increasing the " "size of the cell")) wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) if ((wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last)): errMsg = ("Insufficient grid points in selected domain to " "estimate storm statistics - please select a larger " "domain. Samples = %i / %i")%(np.size(self.parameter), self.minSamplesCell) self.logger.critical(errMsg) raise StopIteration(errMsg) indij = np.where(((lat >= sLat) & (lat < nLat)) & ((lon >= wLon) & (lon < eLon))) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) # Check to see if all values in the array are the same. If the # values are the same, bandwidth would be 0, and therefore KDE # cannot proceed while self.parameter.max() == self.parameter.min(): self.logger.debug(("Parameter values appear to be the same. " "Increasing the size of the cell")) wLon_last = wLon eLon_last = eLon nLat_last = nLat sLat_last = sLat wLon, eLon, nLat, sLat = self._expandCell(lon, lat, wLon, eLon, nLat, sLat) if ((wLon == wLon_last) & (eLon == eLon_last) & (nLat == nLat_last) & (sLat == sLat_last)): errMsg = ("Insufficient grid points in selected domain " "to estimate storm statistics - " "please select a larger domain.") self.logger.critical(errMsg) raise StopIteration(errMsg) indij = np.where(((lat >= sLat) & (lat < nLat)) & ((lon >= wLon) & (lon < eLon))) parameter_ = self.pList[indij] self.parameter = stats.statRemoveNum(np.array(parameter_), self.missingValue) self.logger.debug("Number of valid observations in cell %s : %s", str(cellNum), str(np.size(self.parameter)))