def sample(self, lon, lat, spd, uu, vv, prs, gridx, gridy): """ Extract values from 2-dimensional grids at the given lat/lon. :param float lon: Longitude of the point to extract. :param float lat: Latitude of the point to extract. :param spd: :class:`numpy.ndarray` of speed values. :param uu: :class:`numpy.ndarray` of eastward wind speed values. :param vv: :class:`numpy.ndarray` of northward wind speed values. :param prs: :class:`numpy.ndarray` of pressure values. :param gridx: :class:`numpy.ndarray` of grid longitudes. :param gridy: :class:`numpy.ndarray` of grid latitudes. :return: speed, esatward and northward wind components, and pressure values at the given location :rtype: tuple """ xx = find_index(gridx, float(lon)) yy = find_index(gridy, float(lat)) ss = spd[yy, xx] ux = uu[yy, xx] vy = vv[yy, xx] bb = np.mod((180. / np.pi) * np.arctan2(-ux, -vy), 360.) pp = prs[yy, xx] return (ss, ux, vy, bb, pp)
def sample(self, lon, lat, spd, uu, vv, prs, gridx, gridy): """ Extract values from 2-dimensional grids at the given lat/lon. :param float lon: Longitude of the point to extract. :param float lat: Latitude of the point to extract. :param spd: :class:`numpy.ndarray` of speed values. :param uu: :class:`numpy.ndarray` of eastward wind speed values. :param vv: :class:`numpy.ndarray` of northward wind speed values. :param prs: :class:`numpy.ndarray` of pressure values. :param gridx: :class:`numpy.ndarray` of grid longitudes. :param gridy: :class:`numpy.ndarray` of grid latitudes. :return: speed, esatward and northward wind components, and pressure values at the given location :rtype: tuple """ x = find_index(gridx, float(lon)) y = find_index(gridy, float(lat)) s = spd[y, x] u = uu[y, x] v = vv[y, x] b = np.mod((180. / np.pi) * np.arctan2(-u, -v), 360.) p = prs[y, x] return (s, u, v, b, p)
def processEvent(self, filename, locations, eventNum): """ Process an individual event file :param str filename: Name of a file to process. :param list locations: List of locations to sample data for. :param int eventNum: Ordered event number. """ log.debug("Processing {0}".format(pjoin(self.windfieldPath, filename))) pattern = re.compile(r'\d+') sim, num = pattern.findall(filename) eventId = "%03d-%05d" % (int(sim), int(num)) log.debug("Event ID: {0}".format(eventId)) try: ncobj = Dataset(pjoin(self.windfieldPath, filename)) except: log.warn("Cannot open {0}".\ format(pjoin(self.windfieldPath, filename))) # First perform the event update for tblEvents: fname = pjoin(self.windfieldPath, filename) log.debug("Filename: {0}".format(fname)) si = os.stat(fname) dtWindfieldFile = datetime.fromtimestamp(int(si.st_mtime)) trackfile, dtTrackFile, tcrm_version, minslp, maxwind = \ windfieldAttributes(ncobj) eventparams = ("%06d" % eventNum, eventId, os.path.basename(fname), trackfile, float(maxwind), float(minslp), dtTrackFile, dtWindfieldFile, tcrm_version, "", datetime.now()) # Perform update for tblWindSpeed: lon, lat, vmax, ua, va, pmin = self.loadWindfieldFile(ncobj) ncobj.close() wsparams = list() for loc in locations: locId, locName, locLon, locLat = loc i = find_index(lon, locLon) j = find_index(lat, locLat) locVm = vmax[j, i] locUa = ua[j, i] locVa = va[j, i] locPr = pmin[j, i] locParams = (int(locId), eventId, float(locVm), float(locUa), float(locVa), float(locPr), " ", datetime.now()) wsparams.append(locParams) log.debug("Finished extracting data from {0}".format(filename)) return ( eventparams, wsparams, )
def processHazard(self): """ Update _tblHazard_ with the return period wind speed data. """ log.info("Inserting records into tblHazard") locations = self.getLocations() hazardFile = pjoin(self.hazardPath, 'hazard.nc') ncobj = Dataset(hazardFile) try: tcrm_version = getattr(ncobj, 'tcrm_version') except AttributeError: log.info("Missing tcrm_version attribute from {0}".\ format(hazardFile)) tcrm_version = '' si = os.stat(hazardFile) dtHazardFile = datetime.fromtimestamp(int(si.st_mtime)) lon = ncobj.variables['lon'][:] lat = ncobj.variables['lat'][:] years = ncobj.variables['ari'][:] wspd = ncobj.variables['wspd'][:] wspdUpper = ncobj.variables['wspdupper'][:] wspdLower = ncobj.variables['wspdlower'][:] locationParam = ncobj.variables['loc'][:] scaleParam = ncobj.variables['scale'][:] shpParam = ncobj.variables['shp'][:] ncobj.close() params = [] for k, year in enumerate(years): for loc in locations: locId, locName, locLon, locLat = loc log.debug("Extracting data for location: {0}".format(locName)) i = find_index(lon, locLon) j = find_index(lat, locLat) locWspd = wspd[k, j, i] locUpper = wspdUpper[k, j, i] locLower = wspdLower[k, j, i] locLoc = locationParam[j, i] locScale = scaleParam[j, i] locShp = shpParam[j, i] locParams = (locId, int(year), float(locWspd), float(locUpper), float(locLower), float(locLoc), float(locScale), float(locShp), tcrm_version, dtHazardFile, "", datetime.now()) params.append(locParams) try: self.executemany(INSHAZARD, params) except sqlite3.Error as err: log.exception("Cannot insert records into tblHazard: {0}".\ format(err.args[0])) raise else: self.commit()
def plotHazardCurves(self, inputFile, plotPath): """ Plot the hazard values stored in hazardFile, at the stns stored in stnFile. """ log.info(("Plotting return period curves for locations within the " "model domain")) # Open data file try: ncobj = nctools.ncLoadFile(inputFile) lon = nctools.ncGetDims(ncobj, 'lon') lat = nctools.ncGetDims(ncobj, 'lat') years = nctools.ncGetDims(ncobj, 'ari') except (IOError, RuntimeError, KeyError): log.critical("Cannot load input file: %s"%inputFile) raise placeNames, placeID, placeLats, placeLons, locations = self.getLocations() for name, plat, plon, pID in zip(placeNames, placeLats, placeLons, placeID): pID = int(pID) log.debug("Plotting return period curve for %s"%name) i = find_index(lon, plon) j = find_index(lat, plat) xlabel = 'Average recurrence interval (years)' ylabel = 'Wind speed (%s)'%self.plotUnits.label title = "Return period wind speeds at " + name + ", \n(%5.1f,%5.1f)"%(plon, plat) name.replace(' ', '') log.debug("Working on {0}".format(name)) filename = pjoin(plotPath, 'ARI_curve_%s.%s'%(pID, "png")) log.debug("Saving hazard curve for %s to %s"%(name, filename)) wspd = ncobj.variables['wspd'][:, j, i] recs = database.queries.locationRecords(self.db, pID) data = np.zeros(int(self.numsimulations * 365.25)) if len(recs) > 0: data[-len(recs):] = recs['wspd'] allevents = np.sort(data) log.debug("allevents length = {0}".format(len(allevents))) placeWspd = metutils.convert(wspd, 'mps', self.plotUnits.units) if np.all(placeWspd.mask): log.debug("All values for {0} are null".format(name)) continue if self.ciBounds: wspdLower = ncobj.variables['wspdlower'][:, j, i] wspdUpper = ncobj.variables['wspdupper'][:, j, i] placeWspdLower = metutils.convert(wspdLower, 'mps', self.plotUnits.units) placeWspdUpper = metutils.convert(wspdUpper, 'mps', self.plotUnits.units) else: placeWspdUpper = np.zeros(len(placeWspd)) placeWspdLower = np.zeros(len(placeWspd)) saveHazardCurve(years, allevents, placeWspd, placeWspdUpper, placeWspdLower, xlabel, ylabel, title, filename, self.fit) ncobj.close()
def plotHazardCurves(self, inputFile, plotPath): """ Plot the hazard values stored in hazardFile, at the stns stored in stnFile. """ log.info(("Plotting return period curves for locations within the " "model domain")) # Open data file try: ncobj = nctools.ncLoadFile(inputFile) lon = nctools.ncGetDims(ncobj, 'lon') lat = nctools.ncGetDims(ncobj, 'lat') years = nctools.ncGetDims(ncobj, 'years') except (IOError, RuntimeError, KeyError): log.critical("Cannot load input file: %s"%inputFile) raise # Load data wspd = nctools.ncGetData(ncobj, 'wspd') try: wLower = nctools.ncGetData(ncobj, 'wspdlower') wUpper = nctools.ncGetData(ncobj, 'wspdupper') ciBounds = True except KeyError: ciBounds = False ncobj.close() minLon = min(lon) maxLon = max(lon) minLat = min(lat) maxLat = max(lat) # Use the same maximum value for all localities to simplify # intercomparisons: defaultMax = np.ceil(metutils.convert(100.0, 'mps', self.plotUnits.units)/10.0)*10.0 placeNames, parentCountries, placeLats, placeLons = \ self.getLocations(minLon, maxLon, minLat, maxLat) for name, plat, plon, country in zip(placeNames, placeLats, placeLons, parentCountries): log.debug("Plotting return period curve for %s"%name) i = find_index(lon, plon) j = find_index(lat, plat) xlabel = 'Average recurrence interval (years)' ylabel = 'Wind speed (%s)'%self.plotUnits.label title = "Return period wind speeds at " + name + ", " \ + country + "\n(%5.1f,%5.1f)"%(plon, plat) name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') name.replace(' ', '') filename = pjoin(plotPath, 'ARI_curve_%s.%s'%(name,"png")) log.debug("Saving hazard curve for %s to %s"%(name, filename)) placeWspd = metutils.convert(wspd[:, j, i], 'mps', self.plotUnits.units) maxWspd = placeWspd.max() if ciBounds: placeWspdLower = metutils.convert(wLower[:,j,i], 'mps', self.plotUnits.units) placeWspdUpper = metutils.convert(wUpper[:,j,i], 'mps', self.plotUnits.units) saveHazardCurve(years, placeWspd, placeWspdUpper, placeWspdLower, xlabel, ylabel, title, filename)
def test_findindex(self): """Test find_index function""" for pt, idx in zip(self.findpts, self.indices): i = maputils.find_index(self.lon, pt) self.assertEqual(i, idx)
def plotHazardCurves(self, inputFile, plotPath): """ Plot the hazard values stored in hazardFile, at the stns stored in stnFile. """ log.info(("Plotting return period curves for locations within the " "model domain")) # Open data file try: ncobj = nctools.ncLoadFile(inputFile) lon = nctools.ncGetDims(ncobj, 'lon') lat = nctools.ncGetDims(ncobj, 'lat') years = nctools.ncGetDims(ncobj, 'years') except (IOError, RuntimeError, KeyError): log.critical("Cannot load input file: %s"%inputFile) raise # Load data #wspd = nctools.ncGetData(ncobj, 'wspd') #try: # wLower = nctools.ncGetData(ncobj, 'wspdlower') # wUpper = nctools.ncGetData(ncobj, 'wspdupper') ciBounds = True #except KeyError: # ciBounds = False #ncobj.close() minLon = min(lon) maxLon = max(lon) minLat = min(lat) maxLat = max(lat) # Use the same maximum value for all localities to simplify # intercomparisons: #defaultMax = np.ceil(metutils.convert(100.0, 'mps', # self.plotUnits.units)/10.0)*10.0 placeNames, parentCountries, placeLats, placeLons = \ self.getLocations(minLon, maxLon, minLat, maxLat) for name, plat, plon, country in zip(placeNames, placeLats, placeLons, parentCountries): log.debug("Plotting return period curve for %s"%name) i = find_index(lon, plon) j = find_index(lat, plat) xlabel = 'Average recurrence interval (years)' ylabel = 'Wind speed (%s)'%self.plotUnits.label title = "Return period wind speeds at " + name + ", " \ + country + "\n(%5.1f,%5.1f)"%(plon, plat) name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') name.replace(' ', '') filename = pjoin(plotPath, 'ARI_curve_%s.%s'%(name, "png")) log.debug("Saving hazard curve for %s to %s"%(name, filename)) wspd = ncobj.variables['wspd'][:, j, i] placeWspd = metutils.convert(wspd, 'mps', self.plotUnits.units) if np.all(placeWspd.mask): log.debug("All values for {0} are null".format(name)) continue if ciBounds: wspdLower = ncobj.variables['wspdlower'][:, j, i] wspdUpper = ncobj.variables['wspdupper'][:, j, i] placeWspdLower = metutils.convert(wspdLower, 'mps', self.plotUnits.units) placeWspdUpper = metutils.convert(wspdUpper, 'mps', self.plotUnits.units) saveHazardCurve(years, placeWspd, placeWspdUpper, placeWspdLower, xlabel, ylabel, title, filename) ncobj.close()