def _collect_map_keys_per_quantile(keys): quantile_values = {} while len(keys) > 0: quantile_key = keys.pop() curr_qv = tokens.quantile_value_from_hazard_map_key(quantile_key) if curr_qv not in quantile_values: quantile_values[curr_qv] = [] quantile_values[curr_qv].append(quantile_key) return quantile_values
def _collect_map_keys_per_quantile(keys): quantile_values = {} while len(keys) > 0: quantile_key = keys.pop() curr_qv = tokens.quantile_value_from_hazard_map_key(quantile_key) if curr_qv not in quantile_values: quantile_values[curr_qv] = [] quantile_values[curr_qv].append(quantile_key) return quantile_values
def write_hazardmap_file(self, map_keys): """Generate a NRML file with a hazard map for a collection of hazard map nodes from KVS, identified through their KVS keys. map_keys is a list of KVS keys of the hazard map nodes to be serialized. The hazard map file can be written (1) for a mean hazard map at a set of sites (2) for a quantile hazard map at a set of sites Mixing of these three cases is not allowed, i.e., all hazard maps from the set of curve_keys have to be either for mean, or quantile. """ poe_list = [float(x) for x in self.params[classical_psha.POES_PARAM_NAME].split()] if len(poe_list) == 0: return None if _is_mean_hazmap_key(map_keys[0]): hm_attrib_update = {'statistics': 'mean'} filename_part = 'mean' map_mode = 'mean' elif _is_quantile_hazmap_key(map_keys[0]): # get quantile value from KVS key quantile_value = tokens.quantile_value_from_hazard_map_key( map_keys[0]) hm_attrib_update = {'statistics': 'quantile', 'quantileValue': quantile_value} filename_part = "quantile-%.2f" % quantile_value map_mode = 'quantile' else: error_msg = "no valid hazard map type found in KVS key" raise RuntimeError(error_msg) files = [] # path to the output directory output_path = os.path.join(self['BASE_PATH'], self['OUTPUT_DIR']) for poe in poe_list: nrml_file = "%s-%s-%s.xml" % ( HAZARD_MAP_FILENAME_PREFIX, str(poe), filename_part) nrml_path = os.path.join(output_path, nrml_file) LOG.debug("Generating NRML hazard map file for PoE %s, mode %s, "\ "%s nodes in hazard map: %s" % ( poe, map_mode, len(map_keys), nrml_file)) xmlwriter = hazard_output.HazardMapXMLWriter(nrml_path) hm_data = [] for hm_key in map_keys: if tokens.poe_value_from_hazard_map_key(hm_key) != poe: continue elif map_mode == 'mean' and not _is_mean_hazmap_key(hm_key): error_msg = "non-mean hazard map key found in mean mode" raise RuntimeError(error_msg) elif map_mode == 'quantile': if not _is_quantile_hazmap_key(hm_key): error_msg = "non-quantile hazard map key found in "\ "quantile mode" raise RuntimeError(error_msg) elif tokens.quantile_value_from_hazard_map_key(hm_key) != \ quantile_value: error_msg = "quantile value must be the same for all "\ "hazard map nodes in an instance file" raise ValueError(error_msg) hm = kvs.get_value_json_decoded(hm_key) site_obj = shapes.Site(float(hm['site_lon']), float(hm['site_lat'])) # use hazard map IML and vs30 values from KVS hm_attrib = {'investigationTimeSpan': self.params['INVESTIGATION_TIME'], 'IMT': self.params['INTENSITY_MEASURE_TYPE'], 'IML': hm['IML'], 'vs30': hm['vs30'], 'poE': poe} hm_attrib.update(hm_attrib_update) hm_data.append((site_obj, hm_attrib)) hm_geotiff_name = '%s-%s-%s.tiff' % ( HAZARD_MAP_FILENAME_PREFIX, str(poe), filename_part) geotiff_path = os.path.join(output_path, hm_geotiff_name) self._write_hazard_map_geotiff(geotiff_path, hm_data) xmlwriter.serialize(hm_data) files.append(nrml_path) files.append(geotiff_path) return files
def write_hazardmap_file(self, map_keys): """Generate a NRML file with a hazard map for a collection of hazard map nodes from KVS, identified through their KVS keys. map_keys is a list of KVS keys of the hazard map nodes to be serialized. The hazard map file can be written (1) for a mean hazard map at a set of sites (2) for a quantile hazard map at a set of sites Mixing of these three cases is not allowed, i.e., all hazard maps from the set of curve_keys have to be either for mean, or quantile. """ poe_list = [ float(x) for x in self.params[classical_psha.POES_PARAM_NAME].split() ] if len(poe_list) == 0: return None if _is_mean_hazmap_key(map_keys[0]): hm_attrib_update = {'statistics': 'mean'} filename_part = 'mean' map_mode = 'mean' elif _is_quantile_hazmap_key(map_keys[0]): # get quantile value from KVS key quantile_value = tokens.quantile_value_from_hazard_map_key( map_keys[0]) hm_attrib_update = { 'statistics': 'quantile', 'quantileValue': quantile_value } filename_part = "quantile-%.2f" % quantile_value map_mode = 'quantile' else: error_msg = "no valid hazard map type found in KVS key" raise RuntimeError(error_msg) files = [] # path to the output directory output_path = os.path.join(self['BASE_PATH'], self['OUTPUT_DIR']) for poe in poe_list: nrml_file = "%s-%s-%s.xml" % (HAZARD_MAP_FILENAME_PREFIX, str(poe), filename_part) nrml_path = os.path.join(output_path, nrml_file) LOG.debug("Generating NRML hazard map file for PoE %s, mode %s, "\ "%s nodes in hazard map: %s" % ( poe, map_mode, len(map_keys), nrml_file)) xmlwriter = hazard_output.HazardMapXMLWriter(nrml_path) hm_data = [] for hm_key in map_keys: if tokens.poe_value_from_hazard_map_key(hm_key) != poe: continue elif map_mode == 'mean' and not _is_mean_hazmap_key(hm_key): error_msg = "non-mean hazard map key found in mean mode" raise RuntimeError(error_msg) elif map_mode == 'quantile': if not _is_quantile_hazmap_key(hm_key): error_msg = "non-quantile hazard map key found in "\ "quantile mode" raise RuntimeError(error_msg) elif tokens.quantile_value_from_hazard_map_key(hm_key) != \ quantile_value: error_msg = "quantile value must be the same for all "\ "hazard map nodes in an instance file" raise ValueError(error_msg) hm = kvs.get_value_json_decoded(hm_key) site_obj = shapes.Site(float(hm['site_lon']), float(hm['site_lat'])) # use hazard map IML and vs30 values from KVS hm_attrib = { 'investigationTimeSpan': self.params['INVESTIGATION_TIME'], 'IMT': self.params['INTENSITY_MEASURE_TYPE'], 'IML': hm['IML'], 'vs30': hm['vs30'], 'poE': poe } hm_attrib.update(hm_attrib_update) hm_data.append((site_obj, hm_attrib)) hm_geotiff_name = '%s-%s-%s.tiff' % (HAZARD_MAP_FILENAME_PREFIX, str(poe), filename_part) geotiff_path = os.path.join(output_path, hm_geotiff_name) self._write_hazard_map_geotiff(geotiff_path, hm_data) xmlwriter.serialize(hm_data) files.append(nrml_path) files.append(geotiff_path) return files