def serialize_hazard_curve(self, nrml_file, key_template, hc_attrib_update, sites): """ Serialize the hazard curves of a set of sites. Depending on the parameters the serialized curve will be a plain, mean or quantile hazard curve. :param nrml_file: the output filename :type nrml_file: :py:class:`string` :param key_template: a template for constructing the key to get, for each site, its curve from the KVS :type key_template: :py:class:`string` :param hc_attrib_update: a dictionary containing metadata for the set of curves that will be serialized :type hc_attrib_update: :py:class:`dict` :param sites: the sites of which the curve will be serialized :type sites: list of :py:class:`openquake.shapes.Site` """ nrml_path = self.build_nrml_path(nrml_file) curve_writer = hazard_output.create_hazardcurve_writer( self.job_id, self.serialize_results_to, nrml_path) hc_data = [] for site in sites: # Use hazard curve ordinate values (PoE) from KVS and abscissae # from the IML list in config. hc_attrib = { 'investigationTimeSpan': self['INVESTIGATION_TIME'], 'IMLValues': self.imls, 'IMT': self['INTENSITY_MEASURE_TYPE'], 'PoEValues': kvs.get_value_json_decoded(key_template % hash(site))} hc_attrib.update(hc_attrib_update) hc_data.append((site, hc_attrib)) curve_writer.serialize(hc_data) return nrml_path
def serialize_hazard_curve(self, nrml_file, key_template, hc_attrib_update, sites): """ Serialize the hazard curves of a set of sites. Depending on the parameters the serialized curve will be a plain, mean or quantile hazard curve. :param nrml_file: the output filename :type nrml_file: :py:class:`string` :param key_template: a template for constructing the key to get, for each site, its curve from the KVS :type key_template: :py:class:`string` :param hc_attrib_update: a dictionary containing metadata for the set of curves that will be serialized :type hc_attrib_update: :py:class:`dict` :param sites: the sites of which the curve will be serialized :type sites: list of :py:class:`openquake.shapes.Site` """ def pause_generator(value): """ Returns the initial value when called for the first time and the double value upon each subsequent invocation. N.B.: the maximum value returned will never exceed 90 (seconds). """ yield value while True: if value < 45: value *= 2 yield value # XML serialization context xsc = namedtuple("XSC", "blocks, cblock, i_total, i_done, i_next")( stats.pk_get(self.job_ctxt.job_id, "blocks"), stats.pk_get(self.job_ctxt.job_id, "cblock"), len(sites), 0, 0) nrml_path = self.job_ctxt.build_nrml_path(nrml_file) curve_writer = hazard_output.create_hazardcurve_writer( self.job_ctxt.job_id, self.job_ctxt.serialize_results_to, nrml_path) sites = set(sites) accounted_for = set() min_pause = 0.1 pgen = pause_generator(min_pause) pause = pgen.next() while accounted_for != sites: failures = stats.failure_counters(self.job_ctxt.job_id, "h") if failures: raise RuntimeError("hazard failures (%s), aborting" % failures) hc_data = [] # Sleep a little before checking the availability of additional # hazard curve results. time.sleep(pause) results_found = 0 for site in sites: if site in accounted_for: continue value = kvs.get_value_json_decoded(key_template % hash(site)) if value is None: # No value yet, proceed to next site. continue # Use hazard curve ordinate values (PoE) from KVS and abscissae # from the IML list in config. hc_attrib = { 'investigationTimeSpan': self.job_ctxt['INVESTIGATION_TIME'], 'IMLValues': self.job_ctxt.imls, 'IMT': self.job_ctxt['INTENSITY_MEASURE_TYPE'], 'PoEValues': value } hc_attrib.update(hc_attrib_update) hc_data.append((site, hc_attrib)) accounted_for.add(site) results_found += 1 if not results_found: # No results found, increase the sleep pause. pause = pgen.next() else: hazard_output.SerializerContext().update( xsc._replace(i_next=len(hc_data))) curve_writer.serialize(hc_data) xsc = xsc._replace(i_done=xsc.i_done + len(hc_data)) pause *= 0.8 pause = min_pause if pause < min_pause else pause return nrml_path
def serialize_hazard_curve(self, nrml_file, key_template, hc_attrib_update, sites): """ Serialize the hazard curves of a set of sites. Depending on the parameters the serialized curve will be a plain, mean or quantile hazard curve. :param nrml_file: the output filename :type nrml_file: :py:class:`string` :param key_template: a template for constructing the key to get, for each site, its curve from the KVS :type key_template: :py:class:`string` :param hc_attrib_update: a dictionary containing metadata for the set of curves that will be serialized :type hc_attrib_update: :py:class:`dict` :param sites: the sites of which the curve will be serialized :type sites: list of :py:class:`openquake.shapes.Site` """ def pause_generator(value): """ Returns the initial value when called for the first time and the double value upon each subsequent invocation. N.B.: the maximum value returned will never exceed 90 (seconds). """ yield value while True: if value < 45: value *= 2 yield value # XML serialization context xsc = namedtuple("XSC", "blocks, cblock, i_total, i_done, i_next")( stats.pk_get(self.job_ctxt.job_id, "blocks"), stats.pk_get(self.job_ctxt.job_id, "cblock"), len(sites), 0, 0) nrml_path = self.job_ctxt.build_nrml_path(nrml_file) curve_writer = hazard_output.create_hazardcurve_writer( self.job_ctxt.job_id, self.job_ctxt.serialize_results_to, nrml_path) sites = set(sites) accounted_for = set() min_pause = 0.1 pgen = pause_generator(min_pause) pause = pgen.next() while accounted_for != sites: failures = stats.failure_counters(self.job_ctxt.job_id, "h") if failures: raise RuntimeError("hazard failures (%s), aborting" % failures) hc_data = [] # Sleep a little before checking the availability of additional # hazard curve results. time.sleep(pause) results_found = 0 for site in sites: if site in accounted_for: continue value = kvs.get_value_json_decoded(key_template % hash(site)) if value is None: # No value yet, proceed to next site. continue # Use hazard curve ordinate values (PoE) from KVS and abscissae # from the IML list in config. hc_attrib = { 'investigationTimeSpan': self.job_ctxt['INVESTIGATION_TIME'], 'IMLValues': self.job_ctxt.imls, 'IMT': self.job_ctxt['INTENSITY_MEASURE_TYPE'], 'PoEValues': value} hc_attrib.update(hc_attrib_update) hc_data.append((site, hc_attrib)) accounted_for.add(site) results_found += 1 if not results_found: # No results found, increase the sleep pause. pause = pgen.next() else: hazard_output.SerializerContext().update( xsc._replace(i_next=len(hc_data))) curve_writer.serialize(hc_data) xsc = xsc._replace(i_done=xsc.i_done + len(hc_data)) pause *= 0.8 pause = min_pause if pause < min_pause else pause return nrml_path
def serialize_hazard_curve(self, nrml_file, key_template, hc_attrib_update, sites): """ Serialize the hazard curves of a set of sites. Depending on the parameters the serialized curve will be a plain, mean or quantile hazard curve. :param nrml_file: the output filename :type nrml_file: :py:class:`string` :param key_template: a template for constructing the key to get, for each site, its curve from the KVS :type key_template: :py:class:`string` :param hc_attrib_update: a dictionary containing metadata for the set of curves that will be serialized :type hc_attrib_update: :py:class:`dict` :param sites: the sites of which the curve will be serialized :type sites: list of :py:class:`openquake.shapes.Site` """ def duration_generator(value): """ Returns the initial value when called for the first time and the double value upon each subsequent invocation. N.B.: the maximum value returned will never exceed 90 (seconds). """ yield value while True: if value < 45: value *= 2 yield value nrml_path = self.build_nrml_path(nrml_file) curve_writer = hazard_output.create_hazardcurve_writer(self.job_id, self.serialize_results_to, nrml_path) hc_data = [] sites = set(sites) accounted_for = set() dgen = duration_generator(0.1) duration = dgen.next() while accounted_for != sites: # Sleep a little before checking the availability of additional # hazard curve results. time.sleep(duration) results_found = 0 for site in sites: key = key_template % hash(site) value = kvs.get_value_json_decoded(key) if value is None or site in accounted_for: # The curve for this site is not ready yet. Proceed to # the next. continue # Use hazard curve ordinate values (PoE) from KVS and abscissae # from the IML list in config. hc_attrib = { "investigationTimeSpan": self["INVESTIGATION_TIME"], "IMLValues": self.imls, "IMT": self["INTENSITY_MEASURE_TYPE"], "PoEValues": value, } hc_attrib.update(hc_attrib_update) hc_data.append((site, hc_attrib)) accounted_for.add(site) results_found += 1 if not results_found: # No results found, increase the sleep duration. duration = dgen.next() curve_writer.serialize(hc_data) return nrml_path