def plotPressureMeanDiff(self): """ Plot a map of the difference between observed and synthetic mean pressure values. """ datarange = (-25, 25) figure = ArrayMapFigure() map_kwargs = dict(llcrnrlon=self.lon_range[:-1].min(), llcrnrlat=self.lat_range[:-1].min(), urcrnrlon=self.lon_range[:-1].max(), urcrnrlat=self.lat_range[:-1].max(), projection='merc', resolution='i') cbarlab = "Mean central pressure difference (hPa)" data = self.histMean - self.synMean xgrid, ygrid = np.meshgrid(self.lon_range[:-1], self.lat_range[:-1]) data = self.histMin - self.synMin figure.add(np.transpose(data), xgrid, ygrid, "", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'meanPressureDiff.png') saveFigure(figure, outputFile)
def plotTrackDensityPercentiles(self): """ Plot upper and lower percentiles of track density derived from synthetic event sets """ datarange = (0, self.hist.max()) figure = ArrayMapFigure() map_kwargs = dict(llcrnrlon=self.lon_range[:-1].min(), llcrnrlat=self.lat_range[:-1].min(), urcrnrlon=self.lon_range[:-1].max(), urcrnrlat=self.lat_range[:-1].max(), projection='merc', resolution='i') cbarlab = "TC observations/yr" xgrid, ygrid = np.meshgrid(self.lon_range[:-1], self.lat_range[:-1]) figure.add(self.synHistUpper.T, xgrid, ygrid, "Upper percentile", datarange, cbarlab, map_kwargs) figure.add(self.synHistLower.T, xgrid, ygrid, "Lower percentile", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'track_density_percentiles.png') saveFigure(figure, outputFile)
def plotPressureMin(self): """ Plot a map of observed and synthetic minimum central pressure values. """ datarange = (900, 1000) figure = ArrayMapFigure() map_kwargs = dict(llcrnrlon=self.lon_range[:-1].min(), llcrnrlat=self.lat_range[:-1].min(), urcrnrlon=self.lon_range[:-1].max(), urcrnrlat=self.lat_range[:-1].max(), projection='merc', resolution='i') cbarlab = "Minimum central pressure (hPa)" xgrid, ygrid = np.meshgrid(self.lon_range[:-1], self.lat_range[:-1]) figure.add(np.transpose(self.histMin), xgrid, ygrid, "Historic", datarange, cbarlab, map_kwargs) figure.add(np.transpose(self.synMin), xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'minPressure.png') saveFigure(figure, outputFile)
def plotGenesisDensity(self): """Plot genesis density information""" datarange = (0, self.hist.max()) figure = FilledContourMapFigure() lvls, exponent = levels(self.hist.max()) map_kwargs = dict(llcrnrlon=self.lon_range.min(), llcrnrlat=self.lat_range.min(), urcrnrlon=self.lon_range.max(), urcrnrlat=self.lat_range.max(), projection='merc', resolution='i') cbarlab = "TCs/yr" xgrid, ygrid = np.meshgrid(self.lon_range, self.lat_range) figure.add(self.hist.T * (10.**-exponent), self.X, self.Y, "Historic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure.add(self.synHistMean.T * (10.**-exponent), xgrid, ygrid, "Synthetic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'genesis_density.png') saveFigure(figure, outputFile) figure2 = FilledContourMapFigure() figure2.add(self.hist.T * (10.**-exponent), xgrid, ygrid, "Historic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure2.add(self.synHist[0, :, :].T * (10.**-exponent), xgrid, ygrid, "Synthetic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure2.add(self.synHist[1, :, :].T * (10.**-exponent), xgrid, ygrid, "Synthetic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure2.add(self.synHist[2, :, :].T * (10.**-exponent), xgrid, ygrid, "Synthetic", lvls * (10.**-exponent), cbarlab, map_kwargs) figure2.plot() outputFile = pjoin(self.plotPath, 'genesis_density_samples.png') saveFigure(figure2, outputFile)
def plotGenesisDensityPercentiles(self): """ Plot upper and lower percentiles of genesis density derived from synthetic event sets """ datarange = (0, self.hist.max()) figure = FilledContourMapFigure() lvls, exponent = levels(self.hist.max()) map_kwargs = dict(llcrnrlon=self.lon_range.min(), llcrnrlat=self.lat_range.min(), urcrnrlon=self.lon_range.max(), urcrnrlat=self.lat_range.max(), projection='merc', resolution='i') cbarlab = "TCs/yr" xgrid, ygrid = np.meshgrid(self.lon_range, self.lat_range) figure.add(self.synHistUpper.T * (10.**-exponent), xgrid, ygrid, "Upper percentile", lvls * (10.**-exponent), cbarlab, map_kwargs) figure.add(self.synHistLower.T * (10.**-exponent), xgrid, ygrid, "Lower percentile", lvls * (10.**-exponent), cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'genesis_density_percentiles.png') saveFigure(figure, outputFile)
def plotTrackDensity(self): """Plot track density information""" datarange = (0, self.hist.max()) figure = ArrayMapFigure() cbarlab = "TC observations/yr" figure.add(self.hist.T, self.X, self.Y, "Historic", datarange, cbarlab, self.map_kwargs) figure.add(self.synHistMean.T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'track_density.png') saveFigure(figure, outputFile) figure2 = ArrayMapFigure() figure2.add(self.hist.T, self.X, self.Y, "Historic", datarange, cbarlab, self.map_kwargs) figure2.add(self.synHist[0, :, :].T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure2.add(self.synHist[10, :, :].T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure2.add(self.synHist[20, :, :].T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure2.add(self.synHist[30, :, :].T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure2.add(self.synHist[40, :, :].T, self.X, self.Y, "Synthetic", datarange, cbarlab, self.map_kwargs) figure2.plot() outputFile = pjoin(self.plotPath, 'track_density_samples.png') saveFigure(figure2, outputFile)
def plotGenesisDensity(self): """Plot genesis density information""" datarange = (0, self.hist.max()) figure = ArrayMapFigure() map_kwargs = dict(llcrnrlon=self.lon_range.min(), llcrnrlat=self.lat_range.min(), urcrnrlon=self.lon_range.max(), urcrnrlat=self.lat_range.max(), projection='merc', resolution='i') cbarlab = "TCs/yr" xgrid, ygrid = np.meshgrid(self.lon_range, self.lat_range) figure.add(self.hist.T, xgrid, ygrid, "Historic", datarange, cbarlab, map_kwargs) figure.add(self.synHistMean.T, xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'genesis_density.png') saveFigure(figure, outputFile) figure2 = ArrayMapFigure() figure2.add(self.hist.T, xgrid, ygrid, "Historic", datarange, cbarlab, map_kwargs) figure2.add(self.synHist[0, :, :].T, xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure2.add(self.synHist[1, :, :].T, xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure2.add(self.synHist[2, :, :].T, xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure2.plot() outputFile = pjoin(self.plotPath, 'genesis_density_samples.png') saveFigure(figure2, outputFile)
def plotGenesisDensityPercentiles(self): """ Plot upper and lower percentiles of genesis density derived from synthetic event sets """ datarange = (0, self.hist.max()) figure = ArrayMapFigure() map_kwargs = dict( llcrnrlon=self.lon_range.min(), llcrnrlat=self.lat_range.min(), urcrnrlon=self.lon_range.max(), urcrnrlat=self.lat_range.max(), projection="merc", resolution="i", ) cbarlab = "TCs/yr" xgrid, ygrid = np.meshgrid(self.lon_range, self.lat_range) figure.add(self.synHistUpper.T, xgrid, ygrid, "Upper percentile", datarange, cbarlab, map_kwargs) figure.add(self.synHistLower.T, xgrid, ygrid, "Lower percentile", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, "genesis_density_percentiles.png") saveFigure(figure, outputFile)
def plotMinPressureQuantiles(self): x = self.histMinCP y = self.synMinCP lims = (850, 1000) fig = QuantileFigure() fig.add(x.compress(x>0), y.compress(y>0), lims, "Observed pressure (hPa)", "Simulated pressure (hPa)", "Q-Q plot of minimum central pressure") fig.plot() outputFile = pjoin(self.plotPath, 'minPressureQuantiles.png') saveFigure(fig, outputFile)
def main(configFile): from Utilities.loadData import loadTrackFile from Utilities.config import ConfigParser from os.path import join as pjoin, normpath, dirname baseDir = normpath(pjoin(dirname(__file__), '..')) inputPath = pjoin(baseDir, 'input') config = ConfigParser() config.read(configFile) inputFile = config.get('DataProcess', 'InputFile') source = config.get('DataProcess', 'Source') gridLimit = config.geteval('Region', 'gridLimit') xx = np.arange(gridLimit['xMin'], gridLimit['xMax'] + .1, 0.1) yy = np.arange(gridLimit['yMin'], gridLimit['yMax'] + .1, 0.1) xgrid, ygrid = np.meshgrid(xx, yy) if len(dirname(inputFile)) == 0: inputFile = pjoin(inputPath, inputFile) try: tracks = loadTrackFile(configFile, inputFile, source) except (TypeError, IOError, ValueError): log.critical("Cannot load historical track file: {0}".format(inputFile)) raise title = source outputPath = config.get('Output', 'Path') outputPath = pjoin(outputPath, 'plots', 'stats') outputFile = pjoin(outputPath, 'tctracks.png') map_kwargs = dict(llcrnrlon=xgrid.min(), llcrnrlat=ygrid.min(), urcrnrlon=xgrid.max(), urcrnrlat=ygrid.max(), projection='merc', resolution='i') figure = TrackMapFigure() figure.add(tracks, xgrid, ygrid, title, map_kwargs) figure.plot() saveFigure(figure, outputFile)
def saveTrackMap(tracks, xgrid, ygrid, title, map_kwargs, filename): """ Create a track map and save to file. :param tracks: collection of :class:`Track` objects :param xgrid: :class:`numpy.ndarray` of longitude points defining the domain. :param ygrid: :class:`numpy.ndarray` of latitude points defining the domain. :param str title: Title string for the plot. :param dict map_kwargs: Keyword args that will define the :class:`GeoAxes` instance. :param str filename: Path to the file to save the image. """ fig = SingleTrackMap() fig.plot(tracks, xgrid, ygrid, title, map_kwargs) saveFigure(fig, filename)
def plotTrackDensityPercentiles(self): """ Plot upper and lower percentiles of track density derived from synthetic event sets """ datarange = (0, self.hist.max()) figure = ArrayMapFigure() cbarlab = "TC observations/yr" figure.add(self.synHistUpper.T, self.X, self.Y, "Upper percentile", datarange, cbarlab, self.map_kwargs) figure.add(self.synHistLower.T, self.X, self.Y, "Lower percentile", datarange, cbarlab, self.map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'track_density_percentiles.png') saveFigure(figure, outputFile)
def plotTrackDensity(self): """Plot track density information""" datarange = (0, self.hist.max()) figure = ArrayMapFigure() map_kwargs = dict(llcrnrlon=self.lon_range[:-1].min(), llcrnrlat=self.lat_range[:-1].min(), urcrnrlon=self.lon_range[:-1].max(), urcrnrlat=self.lat_range[:-1].max(), projection='merc', resolution='i') cbarlab = "TC observations/yr" xgrid, ygrid = np.meshgrid(self.lon_range[:-1], self.lat_range[:-1]) figure.add(self.hist.T, xgrid, ygrid, "Historic", datarange, cbarlab, map_kwargs) figure.add(self.synHistMean.T, xgrid, ygrid, "Synthetic", datarange, cbarlab, map_kwargs) figure.plot() outputFile = pjoin(self.plotPath, 'track_density.png') saveFigure(figure, outputFile)
def generateKDE(self, bw=None, save=False, plot=False): """ Generate the PDF for cyclone origins using kernel density estimation technique then save it to a file path provided by user. :param float bw: Optional, bandwidth to use for generating the PDF. If not specified, use the :attr:`bw` attribute. :param boolean save: If ``True``, save the resulting PDF to a netCDF file called 'originPDF.nc'. :param boolean plot: If ``True``, plot the resulting PDF. :returns: ``x`` and ``y`` grid and the PDF values. """ grid2d = KPDF.MPDF2DGrid2Array(self.x, self.y, 1) if bw: self.bw = bw pdf = self._generatePDF(grid2d, self.bw) # Normalise PDF so total probability equals one # Note: Need to investigate why output from KPDF is not correctly normalised pdf = pdf / pdf.sum() pdf.shape = (pdf.shape[0]/self.x.size, self.x.size) self.pdf = pdf.transpose() if save: outputFile = os.path.join(self.processPath, 'originPDF.nc') dimensions = { 0: { 'name': 'lat', 'values': self.y, 'dtype': 'f', 'atts': { 'long_name':' Latitude', 'units': 'degrees_north' } }, 1: { 'name': 'lon', 'values': self.x, 'dtype': 'f', 'atts': { 'long_name': 'Longitude', 'units': 'degrees_east' } } } variables = { 0: { 'name': 'gpdf', 'dims': ('lat', 'lon'), 'values': numpy.array(pdf), 'dtype': 'f', 'atts': { 'long_name': 'TC Genesis probability distribution', 'units': '' } } } ncSaveGrid(outputFile, dimensions, variables) if plot: from Utilities.plotField import plotField from PlotInterface.maps import FilledContourMapFigure, saveFigure # Automatically determine appropriate contour levels min_lvls = 6.0 lvls_options = numpy.array([1.0, 0.5, 0.25, 0.2, 0.1]) pdfMax = pdf.max() exponent = int(numpy.floor(numpy.log10(pdfMax))) significand = pdfMax * 10**-exponent lvl_step = lvls_options[numpy.where((significand/lvls_options) > min_lvls)[0][0]] lvls = numpy.arange(0, pdf.max(), lvl_step*(10.0**exponent)) [gx,gy] = numpy.meshgrid(self.x,self.y) map_kwargs = dict(llcrnrlon=self.x.min(), llcrnrlat=self.y.min(), urcrnrlon=self.x.max(), urcrnrlat=self.y.max(), projection='merc', resolution='i') cbarlabel = r'Genesis probability ($\times 10^{' + str(exponent) + '}$)' title = 'TC Genesis probability' figure = FilledContourMapFigure() figure.add(pdf, gx, gy, title, lvls, cbarlabel, map_kwargs) figure.plot() outputFile = os.path.join(self.outputPath, 'plots', 'stats', 'originPDF_fill.png') saveFigure(figure, outputFile) return self.x, self.y, self.pdf
lat = ncGetDims(ncobj, 'lat') ardata = getData(ncobj, 'alpha', ij) mudata = getData(ncobj, 'mu', ij) mindata = getData(ncobj, 'min', ij) sigdata = getData(ncobj, 'sig', ij) ncobj.close() fig = ArrayMapFigure() fig.add(mudata, xgrid, ygrid, 'Mean pressure ', [950, 1000], 'Pressure (hPa)', map_kwargs) fig.add(mindata, xgrid, ygrid, 'Minimum pressure', [900, 1000], 'Pressure (hPa)', map_kwargs) fig.add(sigdata, xgrid, ygrid, 'Pressure standard deviation', [0, 50], 'Std dev.', map_kwargs) fig.add(ardata, xgrid, ygrid, 'Pressure AR(1)', [-1, 1], 'AR(1)', map_kwargs) fig.plot() saveFigure(fig, pjoin(plotsPath, "pressure_stats.png")) ncobj = ncLoadFile(pjoin(processPath, "pressure_stats.nc")) lon = ncGetDims(ncobj, 'lon') lat = ncGetDims(ncobj, 'lat') ardata = getData(ncobj, 'alpha', ij) mudata = getData(ncobj, 'mu', ij) mindata = getData(ncobj, 'min', ij) sigdata = getData(ncobj, 'sig', ij) ncobj.close() fig = ArrayMapFigure() fig.add(mudata, xgrid, ygrid, 'Mean pressure ', [950, 1000], 'Pressure (hPa)', map_kwargs) fig.plot() fig.axes[0].set_ylim((-35, -5)) saveFigure(fig, pjoin(plotsPath, "pressure_mean.png"))
def generateKDE(self, save=False, plot=False): """ Generate the PDF for cyclone origins using kernel density estimation technique then save it to a file path provided by user. :param float bw: Optional, bandwidth to use for generating the PDF. If not specified, use the :attr:`bw` attribute. :param boolean save: If ``True``, save the resulting PDF to a netCDF file called 'originPDF.nc'. :param boolean plot: If ``True``, plot the resulting PDF. :returns: ``x`` and ``y`` grid and the PDF values. """ self.kde = KDEMultivariate(self.lonLat, bw=self.bw, var_type='cc') xx, yy = np.meshgrid(self.x, self.y) xy = np.vstack([xx.ravel(), yy.ravel()]) pdf = self.kde.pdf(data_predict=xy) pdf = pdf.reshape(xx.shape) self.pdf = pdf.transpose() if save: dimensions = { 0: { 'name': 'lat', 'values': self.y, 'dtype': 'f', 'atts': { 'long_name': ' Latitude', 'units': 'degrees_north' } }, 1: { 'name': 'lon', 'values': self.x, 'dtype': 'f', 'atts': { 'long_name': 'Longitude', 'units': 'degrees_east' } } } variables = { 0: { 'name': 'gpdf', 'dims': ('lat', 'lon'), 'values': np.array(pdf), 'dtype': 'f', 'atts': { 'long_name': 'TC Genesis probability distribution', 'units': '' } } } ncSaveGrid(pjoin(self.processPath, 'originPDF.nc'), dimensions, variables) if plot: from PlotInterface.maps import FilledContourMapFigure, \ saveFigure, levels lvls, exponent = levels(pdf.max()) [gx, gy] = np.meshgrid(self.x, self.y) map_kwargs = dict(llcrnrlon=self.x.min(), llcrnrlat=self.y.min(), urcrnrlon=self.x.max(), urcrnrlat=self.y.max(), projection='merc', resolution='i') cbarlabel = r'Genesis probability ($\times 10^{' + \ str(exponent) + '}$)' figure = FilledContourMapFigure() figure.add(pdf * (10**-exponent), gx, gy, 'TC Genesis probability', lvls * (10**-exponent), cbarlabel, map_kwargs) figure.plot() outputFile = pjoin(self.outputPath, 'plots', 'stats', 'originPDF.png') saveFigure(figure, outputFile) return self.x, self.y, self.pdf