Exemplo n.º 1
0
    def generateCdf(self, save=False):
        """
        Generate the CDFs corresponding to PDFs of cyclone origins,
        then save it on a file path provided by user

        :param boolean save: If ``True``, save the CDF to a netcdf file
                             called 'originCDF.nc'. If ``False``, return
                             the CDF. 
    
        """
        self.cz = stats.cdf2d(self.x, self.y, self.pdf)
        if save:
            self.logger.debug("Saving origin CDF to file")
            grdSave(self.processPath+'originCDF.txt', self.cz, self.x,
                    self.y, self.kdeStep)

        if save:
            outputFile = os.path.join(self.processPath, 'originCDF.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': 'gcdf',
                    'dims': ('lat','lon'),
                    'values': numpy.array(self.cz),
                    'dtype': 'f',
                    'atts': {
                        'long_name': ('TC Genesis cumulative '
                                        'distribution'),
                        'units': ''
                        }
                    }
                }

            ncSaveGrid(outputFile, dimensions, variables)
        else:
            return self.cz
Exemplo n.º 2
0
    def generateCdf(self, save=False):
        """
        Generate the CDFs corresponding to PDFs of cyclone origins,
        then save it on a file path provided by user

        :param boolean save: If ``True``, save the CDF to a netcdf file
                             called 'originCDF.nc'. If ``False``, return
                             the CDF.

        """
        xx, yy = np.meshgrid(self.x, self.y)
        xy = np.vstack([xx.ravel(), yy.ravel()])
        self.cz = self.kde.cdf(data_predict=xy)

        if save:
            outputFile = pjoin(self.processPath, 'originCDF.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': 'gcdf',
                    'dims': ('lat', 'lon'),
                    'values': np.array(self.cz),
                    'dtype': 'f',
                    'atts': {
                        'long_name': ('TC Genesis cumulative '
                                      'distribution'),
                        'units': ''
                    }
                }
            }

            ncSaveGrid(outputFile, dimensions, variables)
        else:
            return self.cz
Exemplo n.º 3
0
 def test_ncSaveGridOpenFile(self):
     """Test ncSaveGrid returns netCDF4.Dataset if keepfileopen=True"""
     ncobj = nctools.ncSaveGrid(self.ncfile, 
                                self.dimensions,
                                self.variables,
                                keepfileopen=True)
     
     self.assertEqual(type(ncobj), Dataset)
     ncobj.close()
Exemplo n.º 4
0
    def test_ncSaveGridOpenFile(self):
        """Test ncSaveGrid returns netCDF4.Dataset if keepfileopen=True"""
        ncobj = nctools.ncSaveGrid(self.ncfile,
                                   self.dimensions,
                                   self.variables,
                                   keepfileopen=True)

        self.assertEqual(type(ncobj), Dataset)
        ncobj.close()
Exemplo n.º 5
0
    def save(self):
        dataFile = pjoin(self.dataPath, 'genesis_density.nc')

        # Simple sanity check (should also include the synthetic data):
        if not hasattr(self, 'hist'):
            log.critical("No historical data available!")
            log.critical(("Check that data has been processed "
                          "before trying to save data"))
            return

        log.info('Saving genesis density data to {0}'.format(dataFile))
        dimensions = {
            0: {
                'name': 'lat',
                'values': self.lat_range,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.lon_range,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Define variables:
        variables = {
            0: {
                'name': 'hist_density',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.hist),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical genesis density',
                    'units': 'TCs per 1-degree grid per year'
                }
            },
            1: {
                'name': 'syn_density',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Genesis density - synthetic events',
                    'units': 'TCs per 1-degree grid per year'
                }
            },
            2: {
                'name': 'syn_density_upper',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistUpper),
                'dtype': 'f',
                'atts': {
                    'long_name': ('Genesis density - upper percentile '
                                  '- synthetic events'),
                    'units':
                    'TCs per 1-degree grid per year',
                    'percentile':
                    '95'
                }
            },
            3: {
                'name': 'syn_density_lower',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistLower),
                'dtype': 'f',
                'atts': {
                    'long_name': ('Genesis density - lower percentile '
                                  '- synthetic events'),
                    'units':
                    'TCs per 1-degree grid per year',
                    'percentile':
                    '5'
                }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables)
Exemplo n.º 6
0
    def save(self):
        """
        Save gridded pressure distributions to file. Data are saved
        to a netCDF file named ``pressureDistribution.nc`` for later analysis.

        """
        dataFile = pjoin(self.dataPath, 'pressureDistribution.nc')

        # Simple sanity check (should also include the synthetic data):
        if not hasattr(self, 'histMin'):
            log.critical("No historical data available!")
            log.critical(("Check that data has been processed "
                          "before trying to save data"))
            return

        log.info('Saving pressure distribution data to {0}'.format(dataFile))
        dimensions = {
            0: {
                'name': 'lat',
                'values': self.lat_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.lon_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Define variables:
        variables = {
            0: {
                'name': 'hist_mean',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.histMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical mean central pressure',
                    'units': 'hPa'
                }
            },
            1: {
                'name': 'syn_mean',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic mean central pressure',
                    'units': 'hPa'
                }
            },
            2: {
                'name': 'hist_min',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.histMin),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical minimum central pressure',
                    'units': 'hPa'
                }
            },
            3: {
                'name': 'syn_min',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synMin),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic minimum central pressure',
                    'units': 'hPa'
                }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables)
        histCPFile = pjoin(self.dataPath, 'histCP.csv')
        synCPFile = pjoin(self.dataPath, 'synCP.csv')
        np.savetxt(histCPFile, self.histMinCP)
        np.savetxt(synCPFile, self.synMinCP)
Exemplo n.º 7
0
    def save(self):
        """Save data to file for archival and/or further processing"""

        dataFile = pjoin(self.dataPath, "lonCrossings.nc")
        log.debug("Saving longitude crossing data to %s" % dataFile)

        dimensions = {
            0: {
                "name": "lat",
                "values": self.gateLats[:-1],
                "dtype": "f",
                "atts": {"long_name": "Latitude", "units": "degrees_north", "axis": "Y"},
            },
            1: {
                "name": "lon",
                "values": self.gateLons,
                "dtype": "f",
                "atts": {"long_name": "Longitude", "units": "degrees_east", "axis": "X"},
            },
        }

        variables = {
            0: {
                "name": "hist",
                "dims": ("lat", "lon"),
                "values": self.lonCrossingHist,
                "dtype": "f",
                "atts": {"long_name": "Historical longitudinal crossing rate", "units": "number of crossings per year"},
            },
            1: {
                "name": "hist_ew",
                "dims": ("lat", "lon"),
                "values": self.lonCrossingEWHist,
                "dtype": "f",
                "atts": {
                    "long_name": ("Historical longitudinal crossing rate " "- east-west crossings"),
                    "units": "number of crossings per year",
                },
            },
            2: {
                "name": "hist_we",
                "dims": ("lat", "lon"),
                "values": self.lonCrossingWEHist,
                "dtype": "f",
                "atts": {
                    "long_name": ("Historical longitudinal crossing rate " "- west-east crossings"),
                    "units": "number of crossings per year",
                },
            },
            3: {
                "name": "syn_mean",
                "dims": ("lat", "lon"),
                "values": self.synCrossMean,
                "dtype": "f",
                "atts": {
                    "long_name": "Mean synthetic longitudinal crossing rate",
                    "units": "number of crossings per year",
                },
            },
            4: {
                "name": "syn_mean_ew",
                "dims": ("lat", "lon"),
                "values": self.synCrossEW,
                "dtype": "f",
                "atts": {
                    "long_name": ("Mean synthetic longitudinal crossing rate " "- east-west crossings"),
                    "units": "number of crossings per year",
                },
            },
            5: {
                "name": "syn_mean_we",
                "dims": ("lat", "lon"),
                "values": self.synCrossWE,
                "dtype": "f",
                "atts": {
                    "long_name": ("Mean synthetic longitudinal crossing rate " "- west-east crossings"),
                    "units": "number of crossings per year",
                },
            },
            6: {
                "name": "syn_upper",
                "dims": ("lat", "lon"),
                "values": self.synCrossUpper,
                "dtype": "f",
                "atts": {
                    "long_name": ("Upper percentile synthetic longitudinal ", "crossing rate"),
                    "units": "number of crossings per year",
                    "percentile": 90,
                },
            },
            7: {
                "name": "syn_upper_ew",
                "dims": ("lat", "lon"),
                "values": self.synCrossEWUpper,
                "dtype": "f",
                "atts": {
                    "long_name": ("Upper percentile synthetic longitudinal " "crossing rate - east-west crossings"),
                    "units": "number of crossings per year",
                    "percentile": 90,
                },
            },
            8: {
                "name": "syn_upper_we",
                "dims": ("lat", "lon"),
                "values": self.synCrossWEUpper,
                "dtype": "f",
                "atts": {
                    "long_name": ("Upper percentile synthetic longitudinal " "crossing rate - west-east crossings"),
                    "units": "number of crossings per year",
                    "percentile": 90,
                },
            },
            9: {
                "name": "syn_lower",
                "dims": ("lat", "lon"),
                "values": self.synCrossLower,
                "dtype": "f",
                "atts": {
                    "long_name": ("Lower percentile synthetic longitudinal " "crossing rate"),
                    "units": "number of crossings per year",
                    "percentile": 5,
                },
            },
            10: {
                "name": "syn_lower_ew",
                "dims": ("lat", "lon"),
                "values": self.synCrossEWLower,
                "dtype": "f",
                "atts": {
                    "long_name": ("Lower percentile synthetic longitudinal " "crossing rate - east-west crossings"),
                    "units": "number of crossings per year",
                    "percentile": 5,
                },
            },
            11: {
                "name": "syn_lower_we",
                "dims": ("lat", "lon"),
                "values": self.synCrossWELower,
                "dtype": "f",
                "atts": {
                    "long_name": ("Lower percentile synthetic longitudinal " "crossing rate - west-east crossings"),
                    "units": "number of crossings per year",
                    "percentile": 5,
                },
            },
        }

        ncSaveGrid(dataFile, dimensions, variables, gatts=self.gatts)

        return
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
    def save(self):
        dataFile = pjoin(self.dataPath, 'density.nc')

        # Simple sanity check (should also include the synthetic data):
        if not hasattr(self, 'hist'):
            log.critical("No historical data available!")
            log.critical("Check that data has been processed before trying to save data")
            return

        log.info('Saving track density data to {0}'.format(dataFile))
        dimensions = {
            0: {
                'name': 'lat',
                'values': self.lat_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.lon_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units':'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Define variables:
        variables = {
            0: {
                'name': 'hist_density',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.hist),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical track density',
                    'units':'observations per 1-degree grid per year'
                }
            },
            1: {
                'name': 'syn_density',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Track density - synthetic events',
                    'units':'observations per 1-degree grid per year'
                }
            },
            2: {
                'name': 'syn_density_upper',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistUpper),
                'dtype': 'f',
                'atts': {
                    'long_name': ('Track density - upper percentile '
                                  '- synthetic events'),
                    'units':' observations per 1-degree grid per year',
                    'percentile': '95'
                }
            },
            3: {
                'name': 'syn_density_lower',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synHistLower),
                'dtype': 'f',
                'atts': {
                    'long_name': ('Track density - lower percentile '
                                  '- synthetic events'),
                    'units': 'observations per 1-degree grid per year',
                    'percentile': '5'
                }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables)
Exemplo n.º 10
0
    def saveHazard(self):
        """
        Save hazard data to a netCDF file.

        """

        log.info("Saving hazard data file")
        # FIXME: need to ensure CF-1.6 and OGC compliance in output files.
        lon, lat = self.tilegrid.getDomainExtent()

        dimensions = {
            0: {
                'name': 'years',
                'values': self.years,
                'dtype': 'f',
                'atts': {
                    'long_name' : 'Return period',
                    'units' : 'years'
                }
            },
            1: {
                'name': 'lat',
                'values': lat,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            2: {
                'name': 'lon',
                'values': lon,
                'dtype': 'd',
                'atts': {
                    'long_name': 'Longitude',
                    'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Create variables:
        variables = {
            0: {
                'name': 'loc',
                'dims': ('lat', 'lon'),
                'values': self.loc,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Location parameter for GEV distribution',
                    'units': 'm/s',
                    'actual_range': (np.min(self.loc), np.max(self.loc)),
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            1: {
                'name': 'scale',
                'dims': ('lat', 'lon'),
                'values': self.scale,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Scale parameter for GEV distribution',
                    'units': '',
                    'grid_mapping': 'crs'
                }
            },
            2: {
                'name': 'shp',
                'dims': ('lat', 'lon'),
                'values': self.shp,
                'dtype': 'f',
                'least_significant_digit': 5,
                'atts': {
                    'long_name': 'Shape parameter for GEV distribution',
                    'units': '',
                    'grid_mapping': 'crs'
                }
            },
            3: {
                'name': 'wspd',
                'dims': ('years', 'lat', 'lon'),
                'values': self.Rp,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Return period wind speed',
                    'units': 'm/s',
                    'actual_range': (np.min(self.Rp), np.max(self.Rp)),
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            4: {
                'name': 'wspdupper',
                'dims': ('years', 'lat', 'lon'),
                'values': self.RPupper,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Upper percentile return period wind speed',
                    'units': 'm/s',
                    'percentile': 95,
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            5: {
                'name': 'wspdlower',
                'dims': ('years', 'lat', 'lon'),
                'values': self.RPlower,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Lower percentile return period wind speed',
                    'units': 'm/s',
                    'percentile': 5,
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            6: {
                'name': 'crs',
                'dims': (),
                'values': None,
                'dtype': 'i',
                'atts': {
                    'grid_mapping_name': 'latitude_longitude',
                    'semi_major_axis': 6378137.0,
                    'inverse_flattening': 298.257222101,
                    'longitude_of_prime_meridian': 0.0
                }
            }
        }

        # Create output file for return-period gust wind speeds and
        # GEV parameters
        nctools.ncSaveGrid(pjoin(self.outputPath, 'hazard.nc'),
                           dimensions, variables,
                           nodata=self.nodata,
                           datatitle='TCRM hazard simulation',
                           gatts=self.global_atts, writedata=True,
                           keepfileopen=False)
Exemplo n.º 11
0
    def saveHazard(self):
        """
        Save hazard data to a netCDF file.

        """

        log.info("Saving hazard data file")
        # FIXME: need to ensure CF-1.6 and OGC compliance in output files.
        lon, lat = self.tilegrid.getDomainExtent()

        dimensions = {
            0: {
                'name': 'years',
                'values': self.years,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Return period',
                    'units': 'years'
                }
            },
            1: {
                'name': 'lat',
                'values': lat,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            2: {
                'name': 'lon',
                'values': lon,
                'dtype': 'd',
                'atts': {
                    'long_name': 'Longitude',
                    'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Create variables:
        variables = {
            0: {
                'name': 'loc',
                'dims': ('lat', 'lon'),
                'values': self.loc,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Location parameter for GEV distribution',
                    'units': 'm/s',
                    'actual_range': (np.min(self.loc), np.max(self.loc)),
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            1: {
                'name': 'scale',
                'dims': ('lat', 'lon'),
                'values': self.scale,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Scale parameter for GEV distribution',
                    'units': '',
                    'grid_mapping': 'crs'
                }
            },
            2: {
                'name': 'shp',
                'dims': ('lat', 'lon'),
                'values': self.shp,
                'dtype': 'f',
                'least_significant_digit': 5,
                'atts': {
                    'long_name': 'Shape parameter for GEV distribution',
                    'units': '',
                    'grid_mapping': 'crs'
                }
            },
            3: {
                'name': 'wspd',
                'dims': ('years', 'lat', 'lon'),
                'values': self.Rp,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Return period wind speed',
                    'units': 'm/s',
                    'actual_range': (np.min(self.Rp), np.max(self.Rp)),
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            4: {
                'name': 'wspdupper',
                'dims': ('years', 'lat', 'lon'),
                'values': self.RPupper,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Upper percentile return period wind speed',
                    'units': 'm/s',
                    'percentile': 95,
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            5: {
                'name': 'wspdlower',
                'dims': ('years', 'lat', 'lon'),
                'values': self.RPlower,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Lower percentile return period wind speed',
                    'units': 'm/s',
                    'percentile': 5,
                    'valid_range': (0.0, 200.),
                    'grid_mapping': 'crs'
                }
            },
            6: {
                'name': 'crs',
                'dims': (),
                'values': None,
                'dtype': 'i',
                'atts': {
                    'grid_mapping_name': 'latitude_longitude',
                    'semi_major_axis': 6378137.0,
                    'inverse_flattening': 298.257222101,
                    'longitude_of_prime_meridian': 0.0
                }
            }
        }

        # Create output file for return-period gust wind speeds and
        # GEV parameters
        nctools.ncSaveGrid(pjoin(self.outputPath, 'hazard.nc'),
                           dimensions,
                           variables,
                           nodata=self.nodata,
                           datatitle='TCRM hazard simulation',
                           gatts=self.global_atts,
                           writedata=True,
                           keepfileopen=False)
Exemplo n.º 12
0
    def _saveGustToFile(self, trackfile, result, filename):
        """
        Save gusts to a file.
        """
        lat, lon, speed, Vx, Vy, P = result

        trackfileDate = flModDate(trackfile)

        gatts = {
            'title': 'TCRM hazard simulation - synthetic event wind field',
            'tcrm_version': flProgramVersion(),
            'python_version': sys.version,
            'track_file': trackfile,
            'track_file_date': trackfileDate,
            'radial_profile': self.profileType,
            'boundary_layer': self.windFieldType,
            'beta': self.beta}

        # Add configuration settings to global attributes:
        for section in self.config.sections():
            for option in self.config.options(section):
                key = "{0}_{1}".format(section, option)
                value = self.config.get(section, option)
                gatts[key] = value

        dimensions = {
            0: {
                'name': 'lat',
                'values': lat,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': lon,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        variables = {
            0: {
                'name': 'vmax',
                'dims': ('lat', 'lon'),
                'values': speed,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Maximum 3-second gust wind speed',
                    'standard_name': 'wind_speed_of_gust',
                    'units': 'm/s',
                    'actual_range': (np.min(speed), np.max(speed)),
                    'valid_range': (0.0, 200.),
                    'cell_methods': ('time: maximum '
                                     'time: maximum (interval: 3 seconds)'),
                    'grid_mapping': 'crs'
                }
            },
            1: {
                'name': 'ua',
                'dims': ('lat', 'lon'),
                'values': Vx,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Eastward component of maximum wind speed',
                    'standard_name': 'eastward_wind',
                    'units': 'm/s',
                    'actual_range': (np.min(Vx), np.max(Vx)),
                    'valid_range': (-200., 200.),
                    'grid_mapping': 'crs'
                }
            },
            2: {
                'name': 'va',
                'dims': ('lat', 'lon'),
                'values': Vy,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Northward component of maximim wind speed',
                    'standard_name': 'northward_wind',
                    'units': 'm/s',
                    'actual_range': (np.min(Vy), np.max(Vy)),
                    'valid_range': (-200., 200.),
                    'grid_mapping': 'crs'
                }
            },
            3: {
                'name': 'slp',
                'dims': ('lat', 'lon'),
                'values': P,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Minimum air pressure at sea level',
                    'standard_name': 'air_pressure_at_sea_level',
                    'units': 'Pa',
                    'actual_range': (np.min(P), np.max(P)),
                    'valid_range': (70000., 115000.),
                    'cell_methods': 'time: minimum',
                    'grid_mapping': 'crs'
                }
            },
            4: {
                'name': 'crs',
                'dims': (),
                'values': None,
                'dtype': 'i',
                'atts': {
                    'grid_mapping_name': 'latitude_longitude',
                    'semi_major_axis': 6378137.0,
                    'inverse_flattening': 298.257222101,
                    'longitude_of_prime_meridian': 0.0
                }
            }
        }

        nctools.ncSaveGrid(filename, dimensions, variables, gatts=gatts)
Exemplo n.º 13
0
    def dumpExtremesFromTrackfile(self, trackfile, dumpfile, callback=None):
        """
        Helper method to calculate the wind extremes from a `trackfile` and
        save them to a file called `dumpfile`.


        :type  trackfile: str
        :param trackfile: the file name of the trackfile.

        :type  dumpfile: str
        :param dumpfile: the file name where to save the wind extremes.

        :type  callback: function
        :param callback: optional function to be called at each timestep to
                         extract point values for specified locations.
        """
        result = self.calculateExtremesFromTrackfile(trackfile, callback)

        gust, bearing, Vx, Vy, P, lon, lat = result

        dimensions = {
            0: {
                'name': 'lat',
                'values': lat,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'

                }
            },
            1: {
                'name': 'lon',
                'values': lon,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        variables = {
            0: {
                'name': 'vmax',
                'dims': ('lat', 'lon'),
                'values': gust,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Maximum 3-second gust wind speed',
                    'units': 'm/s',
                    'actual_range':(np.min(gust), np.max(gust)),
                    'grid_mapping': 'crs'
                }
            },
            1: {
                'name': 'ua',
                'dims': ('lat', 'lon'),
                'values': Vx,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Maximum eastward wind',
                    'units': 'm/s',
                    'actual_range':(np.min(Vx), np.max(Vx)),
                    'grid_mapping': 'crs'
                }
            },
            2: {
                'name': 'va',
                'dims': ('lat', 'lon'),
                'values': Vy,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Maximum northward wind',
                    'units': 'm/s',
                    'actual_range':(np.min(Vy), np.max(Vy)),
                    'grid_mapping': 'crs'
                }
            },
            3: {
                'name': 'slp',
                'dims': ('lat', 'lon'),
                'values': P,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Minimum air pressure at sea level',
                    'units': 'Pa',
                    'actual_range':(np.min(P), np.max(P)),
                    'grid_mapping': 'crs'
                }
            },
            4: {
                'name': 'crs',
                'dims': (),
                'values': None,
                'dtype': 'i',
                'atts': {
                    'grid_mapping_name': 'latitude_longitude',
                    'semi_major_axis': 6378137.0,
                    'inverse_flattening': 298.257222101,
                    'longitude_of_prime_meridian': 0.0
                }
            }
        }

        nctools.ncSaveGrid(dumpfile, dimensions, variables)
Exemplo n.º 14
0
    def save(self):
        dataFile = pjoin(self.dataPath, "genesis_density.nc")

        # Simple sanity check (should also include the synthetic data):
        if not hasattr(self, "hist"):
            log.critical("No historical data available!")
            log.critical("Check that data has been processed before trying to save data")
            return

        log.info("Saving genesis density data to {0}".format(dataFile))
        dimensions = {
            0: {
                "name": "lat",
                "values": self.lat_range,
                "dtype": "f",
                "atts": {"long_name": "Latitude", "units": "degrees_north", "axis": "Y"},
            },
            1: {
                "name": "lon",
                "values": self.lon_range,
                "dtype": "f",
                "atts": {"long_name": "Longitude", "units": "degrees_east", "axis": "X"},
            },
        }

        # Define variables:
        variables = {
            0: {
                "name": "hist_density",
                "dims": ("lat", "lon"),
                "values": np.transpose(self.hist),
                "dtype": "f",
                "atts": {"long_name": "Historical genesis density", "units": "TCs per 1-degree grid per year"},
            },
            1: {
                "name": "syn_density",
                "dims": ("lat", "lon"),
                "values": np.transpose(self.synHistMean),
                "dtype": "f",
                "atts": {"long_name": "Genesis density - synthetic events", "units": "TCs per 1-degree grid per year"},
            },
            2: {
                "name": "syn_density_upper",
                "dims": ("lat", "lon"),
                "values": np.transpose(self.synHistUpper),
                "dtype": "f",
                "atts": {
                    "long_name": ("Genesis density - upper percentile " "- synthetic events"),
                    "units": "TCs per 1-degree grid per year",
                    "percentile": "95",
                },
            },
            3: {
                "name": "syn_density_lower",
                "dims": ("lat", "lon"),
                "values": np.transpose(self.synHistLower),
                "dtype": "f",
                "atts": {
                    "long_name": ("Genesis density - lower percentile " "- synthetic events"),
                    "units": "TCs per 1-degree grid per year",
                    "percentile": "5",
                },
            },
        }

        ncSaveGrid(dataFile, dimensions, variables)
Exemplo n.º 15
0
    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
Exemplo n.º 16
0
 def test_ncSaveGridNullValue(self):
     """Test ncSaveGrid can save a variable with no values"""
     nctools.ncSaveGrid(self.ncfile,
                        self.dimensions,
                        self.nullvalue_var)
Exemplo n.º 17
0
    def save(self):
        """Save data to file for archival and/or further processing"""

        dataFile = pjoin(self.dataPath, 'lonCrossings.nc')
        LOG.debug("Saving longitude crossing data to %s" % dataFile)

        dimensions = {
            0: {
                'name': 'lat',
                'values': self.gateLats[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.gateLons,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        variables = {
            0: {
                'name': 'hist',
                'dims': ('lat', 'lon'),
                'values': self.lonCrossingHist,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical longitudinal crossing rate',
                    'units': 'number of crossings per year'
                }
            },
            1: {
                'name': 'hist_ew',
                'dims': ('lat', 'lon'),
                'values': self.lonCrossingEWHist,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Historical longitudinal crossing rate '
                                  '- east-west crossings'),
                    'units':
                    'number of crossings per year'
                }
            },
            2: {
                'name': 'hist_we',
                'dims': ('lat', 'lon'),
                'values': self.lonCrossingWEHist,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Historical longitudinal crossing rate '
                                  '- west-east crossings'),
                    'units':
                    'number of crossings per year'
                }
            },
            3: {
                'name': 'syn_mean',
                'dims': ('lat', 'lon'),
                'values': self.synCrossMean,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic longitudinal crossing rate',
                    'units': 'number of crossings per year'
                }
            },
            4: {
                'name': 'syn_mean_ew',
                'dims': ('lat', 'lon'),
                'values': self.synCrossEW,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Mean synthetic longitudinal crossing rate '
                                  '- east-west crossings'),
                    'units':
                    'number of crossings per year'
                }
            },
            5: {
                'name': 'syn_mean_we',
                'dims': ('lat', 'lon'),
                'values': self.synCrossWE,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Mean synthetic longitudinal crossing rate '
                                  '- west-east crossings'),
                    'units':
                    'number of crossings per year'
                }
            },
            6: {
                'name': 'syn_upper',
                'dims': ('lat', 'lon'),
                'values': self.synCrossUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal ',
                                  'crossing rate'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    90
                }
            },
            7: {
                'name': 'syn_upper_ew',
                'dims': ('lat', 'lon'),
                'values': self.synCrossEWUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal '
                                  'crossing rate - east-west crossings'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    90
                }
            },
            8: {
                'name': 'syn_upper_we',
                'dims': ('lat', 'lon'),
                'values': self.synCrossWEUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal '
                                  'crossing rate - west-east crossings'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    90
                }
            },
            9: {
                'name': 'syn_lower',
                'dims': ('lat', 'lon'),
                'values': self.synCrossLower,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Lower percentile synthetic longitudinal '
                                  'crossing rate'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    5
                }
            },
            10: {
                'name': 'syn_lower_ew',
                'dims': ('lat', 'lon'),
                'values': self.synCrossEWLower,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Lower percentile synthetic longitudinal '
                                  'crossing rate - east-west crossings'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    5
                }
            },
            11: {
                'name': 'syn_lower_we',
                'dims': ('lat', 'lon'),
                'values': self.synCrossWELower,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Lower percentile synthetic longitudinal '
                                  'crossing rate - west-east crossings'),
                    'units':
                    'number of crossings per year',
                    'percentile':
                    5
                }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables, gatts=self.gatts)

        return
Exemplo n.º 18
0
    def save(self, filename, description=''):
        """
        Save parameters to a netcdf file for later access.

        :param str filename: Path to the netcdf file to be created.
        :param str description: Name of the parameter.

        """

        description = ' ' + description.strip()
        self.logger.debug('Saving' + description +
                          ' statistics to %s' % filename)

        lon = np.arange(self.gridLimit['xMin'], self.gridLimit['xMax'],
                        self.gridSpace['x'])
        lat = np.arange(self.gridLimit['yMax'], self.gridLimit['yMin'],
                        -1 * self.gridSpace['y'])

        nx = len(lon)
        ny = len(lat)

        dimensions = {
            0: {
                'name': 'lat',
                'values': lat,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north'
                }
            },
            1: {
                'name': 'lon',
                'values': lon,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units': 'degrees_east'
                }
            }
        }

        variables = {
            0: {
                'name': 'mu',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.mu.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean' + description,
                    'units': 'm/s'
                }
            },
            1: {
                'name': 'alpha',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.alpha.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Lag-1 autocorrelation of' + description,
                    'units': ''
                }
            },
            2: {
                'name': 'sig',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.sig.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Standard deviation' + description,
                    'units': 'm/s'
                }
            },
            3: {
                'name': 'min',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.min.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Minimum' + description,
                    'units': 'm/s'
                }
            },
            4: {
                'name': 'lmu',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.lmu.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean' + description + ' (over land)',
                    'units': 'm/s'
                }
            },
            5: {
                'name': 'lalpha',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.lalpha.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name':
                    'Lag-1 autocorrelation of' + description + ' (over land)',
                    'units': ''
                }
            },
            6: {
                'name': 'lsig',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.lsig.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name':
                    'Standard deviation of' + description + ' (over land)',
                    'units': 'm/s'
                }
            },
            7: {
                'name': 'lmin',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.lmin.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Minimum' + description + ' (over land)',
                    'units': 'm/s'
                }
            },
            8: {
                'name': 'cell',
                'dims': ('lat', 'lon'),
                'values': np.arange(self.maxCell + 1).reshape((ny, nx)),
                'dtype': 'i',
                'atts': {
                    'long_name': 'Cell',
                    'units': ''
                }
            },
            9: {
                'name': 'phi',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.phi.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'phi',
                    'units': ''
                }
            },
            10: {
                'name': 'lphi',
                'dims': ('lat', 'lon'),
                'values': self.coeffs.lphi.reshape((ny, nx)),
                'dtype': 'f',
                'atts': {
                    'long_name': 'land phi',
                    'units': ''
                }
            }
        }

        import Utilities.nctools as nctools

        nctools.ncSaveGrid(filename,
                           dimensions,
                           variables,
                           nodata=self.missingValue,
                           datatitle=None,
                           writedata=True,
                           keepfileopen=False)
Exemplo n.º 19
0
    def save(self):
        dataFile = pjoin(self.dataPath, 'pressureDistribution.nc')

        # Simple sanity check (should also include the synthetic data):
        if not hasattr(self, 'histMin'):
            log.critical("No historical data available!")
            log.critical("Check that data has been processed before trying to save data")
            return

        log.info('Saving pressure distribution data to {0}'.format(dataFile))
        dimensions = {
            0: {
                'name': 'lat',
                'values': self.lat_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.lon_range[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name': 'Longitude',
                    'units':'degrees_east',
                    'axis': 'X'
                }
            }
        }

        # Define variables:
        variables = {
            0: {
                'name': 'hist_mean',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.histMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical mean central pressure',
                    'units':'hPa'
                }
            },
            1: {
                'name': 'syn_mean',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synMean),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic mean central pressure',
                    'units':'hPa'
                }
            },
            2: {
                'name': 'hist_min',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.histMin),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Historical minimum central pressure',
                    'units':'hPa'
                }
            },
            3: {
                'name': 'syn_min',
                'dims': ('lat', 'lon'),
                'values': np.transpose(self.synMin),
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic minimum central pressure',
                    'units': 'hPa'
                }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables)
        histCPFile = pjoin(self.dataPath, 'histCP.csv')
        synCPFile = pjoin(self.dataPath, 'synCP.csv')
        np.savetxt(histCPFile, self.histMinCP)
        np.savetxt(synCPFile, self.synMinCP)
Exemplo n.º 20
0
    def save(self, filename, description=''):
        """
        Save parameters to a netcdf file for later access.

        :param str filename: Path to the netcdf file to be created.
        :param str description: Name of the parameter.

        """
        
        description = ' ' + description.strip()
        self.logger.debug('Saving' + description + ' statistics to %s' % filename)

        lon = np.arange(self.gridLimit['xMin'],self.gridLimit['xMax'],self.gridSpace['x'])
        lat = np.arange(self.gridLimit['yMax'],self.gridLimit['yMin'],-1*self.gridSpace['y'])

        nx = len(lon)
        ny = len(lat)

        dimensions = {0:{'name':'lat','values':lat,'dtype':'f',
                         'atts':{'long_name':'Latitude','units':'degrees_north'} },
                      1:{'name':'lon','values':lon,'dtype':'f',
                         'atts':{'long_name':'Longitude','units':'degrees_east'} } }

        variables = {0:{'name':'mu','dims':('lat','lon'),
                        'values':self.coeffs.mu.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Mean' + description,
                                'units':'m/s'} },
                     1:{'name':'alpha','dims':('lat','lon'),
                        'values':self.coeffs.alpha.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Lag-1 autocorrelation of' + description,
                                'units':''} },
                     2:{'name':'sig','dims':('lat','lon'),
                        'values':self.coeffs.sig.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Standard deviation' + description,
                                'units':'m/s'} },
                     3:{'name':'min','dims':('lat','lon'),
                        'values':self.coeffs.min.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Minimum' + description,
                                'units':'m/s'} },
                     4:{'name':'lmu','dims':('lat','lon'),
                        'values':self.coeffs.lmu.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Mean' + description +' (over land)',
                                'units':'m/s'} },
                     5:{'name':'lalpha','dims':('lat','lon'),
                        'values':self.coeffs.lalpha.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Lag-1 autocorrelation of' + description + ' (over land)',
                                'units':''} },
                     6:{'name':'lsig','dims':('lat','lon'),
                        'values':self.coeffs.lsig.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Standard deviation of' + description + ' (over land)',
                                'units':'m/s'} },
                     7:{'name':'lmin','dims':('lat','lon'),
                        'values':self.coeffs.lmin.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'Minimum' + description + ' (over land)',
                                'units':'m/s'} },
                     8:{'name':'cell', 'dims':('lat','lon'),
                        'values':np.arange(self.maxCell+1).reshape((ny,nx)),
                        'dtype':'i',
                        'atts':{'long_name':'Cell', 'units':''} },
                     9:{'name':'phi', 'dims':('lat','lon'),
                        'values':self.coeffs.phi.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'phi', 'units':''} },
                    10:{'name':'lphi', 'dims':('lat','lon'),
                        'values':self.coeffs.lphi.reshape((ny,nx)),
                        'dtype':'f',
                        'atts':{'long_name':'land phi', 'units':''} }
                     }

        import Utilities.nctools as nctools

        nctools.ncSaveGrid(filename, dimensions, variables,
                           nodata=self.missingValue,
                           datatitle=None, writedata=True, 
                           keepfileopen=False)
Exemplo n.º 21
0
 def test_ncSaveGridNullValue(self):
     """Test ncSaveGrid can save a variable with no values"""
     nctools.ncSaveGrid(self.ncfile, self.dimensions, self.nullvalue_var)
Exemplo n.º 22
0
    def saveGustToFile(self, trackfile, result, filename):
        """
        Save gusts to a file.
        """
        lat, lon, speed, Vx, Vy, P = result

        trackfileDate = flModDate(trackfile)

        gatts = {
            'title': 'TCRM hazard simulation - synthetic event wind field',
            'tcrm_version': flProgramVersion(),
            'python_version': sys.version,
            'track_file': trackfile,
            'track_file_date': trackfileDate,
            'radial_profile': self.profileType,
            'boundary_layer': self.windFieldType,
            'beta': self.beta
        }

        # Add configuration settings to global attributes:
        for section in self.config.sections():
            for option in self.config.options(section):
                key = "{0}_{1}".format(section, option)
                value = self.config.get(section, option)
                gatts[key] = value

        dimensions = {
            0: {
                'name': 'lat',
                'values': lat,
                'dtype': 'float64',
                'atts': {
                    'long_name': 'Latitude',
                    'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': lon,
                'dtype': 'float64',
                'atts': {
                    'long_name': 'Longitude',
                    'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'
                }
            }
        }

        variables = {
            0: {
                'name': 'vmax',
                'dims': ('lat', 'lon'),
                'values': speed,
                'dtype': 'float32',
                'atts': {
                    'long_name':
                    'Maximum 0.2-second gust wind speed',
                    'standard_name':
                    'wind_speed_of_gust',
                    'units':
                    'm/s',
                    'actual_range': (np.min(speed), np.max(speed)),
                    'valid_range': (0.0, 200.),
                    'cell_methods': ('time: maximum ',
                                     'time: maximum (interval: 0.2 seconds)'),
                    'grid_mapping':
                    'crs'
                }
            },
            1: {
                'name': 'ua',
                'dims': ('lat', 'lon'),
                'values': Vx,
                'dtype': 'float32',
                'atts': {
                    'long_name': 'Eastward component of maximum wind speed',
                    'standard_name': 'eastward_wind',
                    'units': 'm/s',
                    'actual_range': (np.min(Vx), np.max(Vx)),
                    'valid_range': (-200., 200.),
                    'grid_mapping': 'crs'
                }
            },
            2: {
                'name': 'va',
                'dims': ('lat', 'lon'),
                'values': Vy,
                'dtype': 'float32',
                'atts': {
                    'long_name': 'Northward component of maximim wind speed',
                    'standard_name': 'northward_wind',
                    'units': 'm/s',
                    'actual_range': (np.min(Vy), np.max(Vy)),
                    'valid_range': (-200., 200.),
                    'grid_mapping': 'crs'
                }
            },
            3: {
                'name': 'slp',
                'dims': ('lat', 'lon'),
                'values': P,
                'dtype': 'float32',
                'atts': {
                    'long_name': 'Minimum air pressure at sea level',
                    'standard_name': 'air_pressure_at_sea_level',
                    'units': 'Pa',
                    'actual_range': (np.min(P), np.max(P)),
                    'valid_range': (70000., 115000.),
                    'cell_methods': 'time: minimum',
                    'grid_mapping': 'crs'
                }
            },
            4: {
                'name': 'crs',
                'dims': (),
                'values': None,
                'dtype': 'i',
                'atts': {
                    'grid_mapping_name': 'latitude_longitude',
                    'semi_major_axis': 6378137.0,
                    'inverse_flattening': 298.257222101,
                    'longitude_of_prime_meridian': 0.0
                }
            }
        }

        nctools.ncSaveGrid(filename, dimensions, variables, gatts=gatts)
Exemplo n.º 23
0
    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.
        """
        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

            [gx,gy] = numpy.meshgrid(self.x,self.y)

            # 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))

            plotField(gx, gy, (10.0**-exponent)*pdf, res='l',levels=(10.0**-exponent)*lvls, cmap='jet', smoothing=False,
              title=None, xlab='Lonigtude', ylab='Latitude', clab=r'Genesis probability ($\times 10^{' + str(exponent) + '}$)',
              maskland=True,outputFile=os.path.join(self.outputPath, 'plots', 'stats', 'originPDF_contour.png'),
              fill=False)
            plotField(gx, gy, (10.0**-exponent)*pdf, res='l',levels=(10.0**-exponent)*lvls, cmap='jet', smoothing=False,
              title=None, xlab='Lonigtude', ylab='Latitude', clab=r'Genesis probability ($\times 10^{' + str(exponent) + '}$)',
              maskland=False,outputFile=os.path.join(self.outputPath, 'plots', 'stats', 'originPDF_fill.png'),
              fill=True)
            self.logger.debug("Saving origin PDF to file")
            #grdSave(os.path.join(self.processPath, 'originPDF.txt'),
            #        pdf, self.x, self.y, self.kdeStep)
        
        return self.x, self.y, self.pdf
Exemplo n.º 24
0
    def save(self):
        """Save data to file for archival and/or further processing"""

        dataFile = pjoin(self.dataPath, 'lonCrossings.nc')
        log.debug("Saving longitude crossing data to %s" % dataFile)

        dimensions = {
            0: {
                'name': 'lat',
                'values': self.gateLats[:-1],
                'dtype': 'f',
                'atts': {
                    'long_name':'Latitude',
                    'units':'degrees_north',
                    'axis': 'Y'
                }
            },
            1: {
                'name': 'lon',
                'values': self.gateLons,
                'dtype': 'f',
                'atts': {
                    'long_name':'Longitude',
                    'units':'degrees_east',
                    'axis': 'X'
                }
            }
        }

        variables = {
            0: {
                'name': 'hist',
                'dims': ('lat', 'lon'),
                'values' :self.lonCrossingHist,
                'dtype': 'f',
                'atts': {
                    'long_name':'Historical longitudinal crossing rate',
                    'units':'number of crossings per year'
                }
            },
            1: {
                'name': 'hist_ew',
                'dims': ('lat', 'lon'),
                'values': self.lonCrossingEWHist,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Historical longitudinal crossing rate '
                                  '- east-west crossings'),
                    'units': 'number of crossings per year'
                }
            },
            2: {
                'name': 'hist_we',
                'dims': ('lat', 'lon'),
                'values': self.lonCrossingWEHist,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Historical longitudinal crossing rate '
                                  '- west-east crossings'),
                    'units': 'number of crossings per year'
                }
            },
            3: {
                'name': 'syn_mean',
                'dims':('lat', 'lon'),
                'values': self.synCrossMean,
                'dtype': 'f',
                'atts': {
                    'long_name': 'Mean synthetic longitudinal crossing rate',
                    'units': 'number of crossings per year'
                }
            },
            4: {
                'name': 'syn_mean_ew',
                'dims': ('lat', 'lon'),
                'values': self.synCrossEW,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Mean synthetic longitudinal crossing rate '
                                  '- east-west crossings'),
                    'units':'number of crossings per year'
                }
            },
            5: {
                'name': 'syn_mean_we',
                'dims': ('lat', 'lon'),
                'values': self.synCrossWE,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Mean synthetic longitudinal crossing rate '
                                  '- west-east crossings'),
                    'units': 'number of crossings per year'
                }
            },
            6: {
                'name': 'syn_upper',
                'dims': ('lat', 'lon'),
                'values': self.synCrossUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal ',
                                  'crossing rate' ),
                    'units': 'number of crossings per year',
                    'percentile': 90
                }
            },
            7: {
                'name': 'syn_upper_ew',
                'dims': ('lat', 'lon'),
                'values': self.synCrossEWUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal '
                                  'crossing rate - east-west crossings'),
                    'units': 'number of crossings per year',
                    'percentile': 90
                }
            },
            8: {
                'name': 'syn_upper_we',
                'dims': ('lat', 'lon'),
                'values': self.synCrossWEUpper,
                'dtype': 'f',
                'atts': {
                    'long_name': ('Upper percentile synthetic longitudinal '
                                  'crossing rate - west-east crossings'),
                    'units': 'number of crossings per year',
                    'percentile': 90
                }
            },
            9: {
                'name': 'syn_lower',
                'dims': ('lat', 'lon'),
                'values': self.synCrossLower,
                'dtype': 'f',
                'atts': {
                    'long_name':('Lower percentile synthetic longitudinal '
                                 'crossing rate'),
                    'units':'number of crossings per year',
                    'percentile': 5
                }
            },
            10: {
                 'name': 'syn_lower_ew',
                 'dims': ('lat', 'lon'),
                 'values': self.synCrossEWLower,
                 'dtype': 'f',
                 'atts': {
                    'long_name':('Lower percentile synthetic longitudinal '
                                  'crossing rate - east-west crossings'),
                    'units':'number of crossings per year',
                    'percentile': 5
                }
            },
            11: {
                 'name': 'syn_lower_we',
                 'dims': ('lat', 'lon'),
                 'values': self.synCrossWELower,
                 'dtype': 'f',
                 'atts': {
                    'long_name': ('Lower percentile synthetic longitudinal '
                                   'crossing rate - west-east crossings'),
                    'units': 'number of crossings per year',
                    'percentile': 5
                 }
            }
        }

        ncSaveGrid(dataFile, dimensions, variables, gatts=self.gatts)

        return