def compute_uhs(the_job, site): """Given a `JobContext` and a site of interest, compute UHS. The Java `UHSCalculator` is called to do perform the core computation. :param the_job: :class:`openquake.engine.JobContext` instance. :param site: :class:`openquake.shapes.Site` instance. :returns: An `ArrayList` (Java object) of `UHSResult` objects, one per PoE. """ periods = list_to_jdouble_array(the_job["UHS_PERIODS"]) poes = list_to_jdouble_array(the_job["POES"]) imls = get_iml_list(the_job["INTENSITY_MEASURE_LEVELS"], the_job["INTENSITY_MEASURE_TYPE"]) max_distance = the_job["MAXIMUM_DISTANCE"] cache = java.jclass("KVS")(config.get("kvs", "host"), int(config.get("kvs", "port"))) erf = generate_erf(the_job.job_id, cache) gmpe_map = generate_gmpe_map(the_job.job_id, cache) set_gmpe_params(gmpe_map, the_job.params) uhs_calc = java.jclass("UHSCalculator")(periods, poes, imls, erf, gmpe_map, max_distance) uhs_results = uhs_calc.computeUHS( site.latitude, site.longitude, the_job["VS30_TYPE"], the_job["REFERENCE_VS30_VALUE"], the_job["DEPTHTO1PT0KMPERSEC"], the_job["REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM"], ) return uhs_results
def compute_uhs(the_job, site): """Given a `JobContext` and a site of interest, compute UHS. The Java `UHSCalculator` is called to do perform the core computation. :param the_job: :class:`openquake.engine.JobContext` instance. :param site: :class:`openquake.shapes.Site` instance. :returns: An `ArrayList` (Java object) of `UHSResult` objects, one per PoE. """ periods = list_to_jdouble_array(the_job['UHS_PERIODS']) poes = list_to_jdouble_array(the_job['POES']) imls = general.get_iml_list(the_job['INTENSITY_MEASURE_LEVELS'], the_job['INTENSITY_MEASURE_TYPE']) max_distance = the_job['MAXIMUM_DISTANCE'] cache = java.jclass('KVS')( config.get('kvs', 'host'), int(config.get('kvs', 'port'))) erf = general.generate_erf(the_job.job_id, cache) gmpe_map = general.generate_gmpe_map(the_job.job_id, cache) general.set_gmpe_params(gmpe_map, the_job.params) uhs_calc = java.jclass('UHSCalculator')(periods, poes, imls, erf, gmpe_map, max_distance) site_model = general.get_site_model(the_job.oq_job.id) if site_model is not None: sm_data = general.get_closest_site_model_data(site_model, site) vs30_type = sm_data.vs30_type.capitalize() vs30 = sm_data.vs30 z1pt0 = sm_data.z1pt0 z2pt5 = sm_data.z2pt5 else: jp = the_job.oq_job_profile vs30_type = jp.vs30_type.capitalize() vs30 = jp.reference_vs30_value z1pt0 = jp.depth_to_1pt_0km_per_sec z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param uhs_results = _compute_uhs( uhs_calc, site.latitude, site.longitude, vs30_type, vs30, z1pt0, z2pt5 ) return uhs_results
def test_write_uhs_spectrum_data(self): # Test `write_uhs_spectrum_data`. # To start with, we need to write the 'container' records for the UHS # results: write_uh_spectra(self.job_ctxt) uhs_results = [] # The results we want to write to HDF5 uhs_result = java.jvm().JClass('org.gem.calc.UHSResult') # Build up a result list that we can pass to the function under test: for poe, uhs in self.UHS_RESULTS: uhs_results.append(uhs_result(poe, list_to_jdouble_array(uhs))) realization = 0 test_site = Site(0.0, 0.0) # Call the function under test write_uhs_spectrum_data( self.job_ctxt, realization, test_site, uhs_results) uhs_data = UhSpectrumData.objects.filter( uh_spectrum__uh_spectra__output__oq_job=( self.job.id)) self.assertEqual(len(self.UHS_RESULTS), len(uhs_data)) self.assertTrue(all([x.realization == 0 for x in uhs_data])) uhs_results_dict = dict(self.UHS_RESULTS) # keyed by PoE for uhs_datum in uhs_data: self.assertTrue( numpy.allclose(uhs_results_dict[uhs_datum.uh_spectrum.poe], uhs_datum.sa_values)) self.assertEqual(test_site.point.to_wkt(), uhs_datum.location.wkt)
def test_list_to_jdouble_array(self): """Test construction of a Double[] (Java array) from a list of floats. """ test_input = [0.01, 0.02, 0.03, 0.04] jdouble_a = java.list_to_jdouble_array(list(test_input)) # It should be a jpype Double[] type: self.assertEqual('java.lang.Double[]', jdouble_a.__class__.__name__) # Now check that the len and values are correct: self.assertEqual(len(test_input), len(jdouble_a)) self.assertEqual(test_input, [x.doubleValue() for x in jdouble_a])
def compute_uhs(the_job, site): """Given a `CalculationProxy` and a site of interest, compute UHS. The Java `UHSCalculator` is called to do perform the core computation. :param the_job: :class:`openquake.engine.CalculationProxy` instance. :param site: :class:`openquake.shapes.Site` instance. :returns: An `ArrayList` (Java object) of `UHSResult` objects, one per PoE. """ periods = list_to_jdouble_array(the_job['UHS_PERIODS']) poes = list_to_jdouble_array(the_job['POES']) imls = get_iml_list(the_job['INTENSITY_MEASURE_LEVELS'], the_job['INTENSITY_MEASURE_TYPE']) max_distance = the_job['MAXIMUM_DISTANCE'] cache = java.jclass('KVS')( config.get('kvs', 'host'), int(config.get('kvs', 'port'))) erf = generate_erf(the_job.job_id, cache) gmpe_map = generate_gmpe_map(the_job.job_id, cache) set_gmpe_params(gmpe_map, the_job.params) uhs_calc = java.jclass('UHSCalculator')(periods, poes, imls, erf, gmpe_map, max_distance) uhs_results = uhs_calc.computeUHS( site.latitude, site.longitude, the_job['VS30_TYPE'], the_job['REFERENCE_VS30_VALUE'], the_job['DEPTHTO1PT0KMPERSEC'], the_job['REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM']) return uhs_results
def test_write_uhs_results(self): """Given some mocked up UHS calc results, write them to some temporary HDF5 files. We need to verify that the result file paths are correct, as well as the contents. The results should be written to separate directories (depending on the PoE).""" # Both result files should have the same file name, # just a different directory location. expected_file_name = 'sample:1-lon:0.0-lat:0.0.h5' uhs_results = [] # The results we want to write to HDF5 uhs_result = java.jvm().JClass('org.gem.calc.UHSResult') for poe, uhs in self.UHS_RESULTS: uhs_results.append(uhs_result(poe, list_to_jdouble_array(uhs))) result_dir = tempfile.mkdtemp() result_files = write_uhs_results(result_dir, 1, Site(0.0, 0.0), uhs_results) for i, res_file in enumerate(result_files): print res_file self.assertTrue(os.path.exists(res_file)) expected_dir = os.path.join(result_dir, 'poe:%s' % self.UHS_RESULTS[i][0]) actual_dir, actual_file_name = os.path.split(res_file) self.assertEquals(expected_dir, actual_dir) self.assertEquals(expected_file_name, actual_file_name) with h5py.File(res_file, 'r') as h5_file: self.assertTrue(numpy.allclose(self.UHS_RESULTS[i][1], h5_file['uhs'].value)) shutil.rmtree(result_dir)
def get_iml_list(imls, intensity_measure_type): """Build the appropriate Arbitrary Discretized Func from the IMLs, based on the IMT""" return list_to_jdouble_array( [IML_SCALING[intensity_measure_type](x) for x in imls])