def export_gmf_scenario(output, target_dir): """ Export the GMFs specified by ``output`` to the ``target_dir``. :param output: :class:`openquake.engine.db.models.Output` with an `output_type` of `gmf_scenario`. :param str target_dir: Destination directory location for exported files. :returns: A list of exported file names (including the absolute path to each file). """ haz_calc = output.oq_job.hazard_calculation calc_dir = 'calc_%s' % haz_calc.id path = os.path.abspath(os.path.join(target_dir, calc_dir, 'gmf', 'gmf.xml')) core.makedirs(os.path.dirname(path)) gmfs = models.get_gmfs_scenario(output) writer = writers.ScenarioGMFXMLWriter(path) writer.serialize(gmfs) return [path]
def _get_result_export_dest(calc_id, target, result, file_ext='xml'): """ Get the full absolute path (including file name) for a given ``result``. As a side effect, intermediate directories are created such that the file can be created and written to immediately. :param int calc_id: ID of the associated :class:`openquake.engine.db.models.OqJob`. :param target: Destination directory location for exported files OR a file-like object. If file-like, we just simply return it. :param result: :mod:`openquake.engine.db.models` result object with a foreign key reference to :class:`~openquake.engine.db.models.Output`. :param file_ext: Desired file extension for the output file. Defaults to 'xml'. :returns: Full path (including filename) to the destination export file. If the ``target`` is a file-like, we don't do anything special and simply return it. """ if not isinstance(target, (basestring, buffer)): # It's not a file path. In this case, we expect a file-like object. # Just return it. return target output = result.output output_type = output.output_type samples = output.oq_job.get_param('number_of_logic_tree_samples', 0) # Create the names for each subdirectory calc_dir = 'calc_%s' % calc_id type_dir = output_type imt_dir = '' # if blank, we don't have an IMT dir if output_type in ('hazard_curve', 'hazard_map', 'disagg_matrix'): imt_dir = result.imt if result.imt == 'SA': imt_dir = 'SA-%s' % result.sa_period # construct the directory which will contain the result XML file: directory = os.path.join(target, calc_dir, type_dir, imt_dir) core.makedirs(directory) if output_type in ('hazard_curve', 'hazard_curve_multi', 'hazard_map', 'uh_spectra'): # include the poe in hazard map and uhs file names if output_type in ('hazard_map', 'uh_spectra'): output_type = '%s-poe_%s' % (output_type, result.poe) if result.statistics is not None: # we could have stats if result.statistics == 'quantile': # quantile filename = '%s-%s.%s' % (output_type, 'quantile_%s' % result.quantile, file_ext) else: # mean filename = '%s-%s.%s' % (output_type, result.statistics, file_ext) else: # otherwise, we need to include logic tree branch info ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal, file_ext ) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.%s' % ( output_type, sm_ltp, gsim_ltp, file_ext ) elif output_type == 'gmf': # only logic trees, no stats ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal, file_ext ) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.%s' % ( output_type, sm_ltp, gsim_ltp, file_ext ) elif output_type == 'ses': sm_ltp = core.LT_PATH_JOIN_TOKEN.join(result.sm_lt_path) filename = '%s-%s-smltp_%s.%s' % ( output_type, result.ordinal, sm_ltp, file_ext ) elif output_type == 'disagg_matrix': # only logic trees, no stats out = '%s(%s)' % (output_type, result.poe) location = 'lon_%s-lat_%s' % (result.location.x, result.location.y) ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( out, location, sm_ltp, gsim_ltp, ltr.ordinal, file_ext ) else: # End Branch Enumeration filename = '%s-%s-smltp_%s-gsimltp_%s.%s' % ( out, location, sm_ltp, gsim_ltp, file_ext ) else: filename = '%s.%s' % (output_type, file_ext) return os.path.abspath(os.path.join(directory, filename))
def _get_result_export_dest(calc_id, target, result, file_ext='xml'): """ Get the full absolute path (including file name) for a given ``result``. As a side effect, intermediate directories are created such that the file can be created and written to immediately. :param int calc_id: ID of the associated :class:`openquake.engine.db.models.OqJob`. :param target: Destination directory location for exported files OR a file-like object. If file-like, we just simply return it. :param result: :mod:`openquake.engine.db.models` result object with a foreign key reference to :class:`~openquake.engine.db.models.Output`. :param file_ext: Desired file extension for the output file. Defaults to 'xml'. :returns: Full path (including filename) to the destination export file. If the ``target`` is a file-like, we don't do anything special and simply return it. """ if not isinstance(target, (basestring, buffer)): # It's not a file path. In this case, we expect a file-like object. # Just return it. return target output = result.output output_type = output.output_type samples = output.oq_job.get_param('number_of_logic_tree_samples', 0) # Create the names for each subdirectory calc_dir = 'calc_%s' % calc_id type_dir = output_type imt_dir = '' # if blank, we don't have an IMT dir if output_type in ('hazard_curve', 'hazard_map', 'disagg_matrix'): imt_dir = result.imt if result.imt == 'SA': imt_dir = 'SA-%s' % result.sa_period # construct the directory which will contain the result XML file: directory = os.path.join(target, calc_dir, type_dir, imt_dir) core.makedirs(directory) if output_type in ('hazard_curve', 'hazard_curve_multi', 'hazard_map', 'uh_spectra'): # include the poe in hazard map and uhs file names if output_type in ('hazard_map', 'uh_spectra'): output_type = '%s-poe_%s' % (output_type, result.poe) if result.statistics is not None: # we could have stats if result.statistics == 'quantile': # quantile filename = '%s-%s.%s' % (output_type, 'quantile_%s' % result.quantile, file_ext) else: # mean filename = '%s-%s.%s' % (output_type, result.statistics, file_ext) else: # otherwise, we need to include logic tree branch info ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal, file_ext) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.%s' % (output_type, sm_ltp, gsim_ltp, file_ext) elif output_type == 'gmf': # only logic trees, no stats ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal, file_ext) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.%s' % (output_type, sm_ltp, gsim_ltp, file_ext) elif output_type == 'ses': sm_ltp = core.LT_PATH_JOIN_TOKEN.join(result.sm_lt_path) filename = '%s-%s-smltp_%s.%s' % (output_type, result.ordinal, sm_ltp, file_ext) elif output_type == 'disagg_matrix': # only logic trees, no stats out = '%s(%s)' % (output_type, result.poe) location = 'lon_%s-lat_%s' % (result.location.x, result.location.y) ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if samples: # Monte-Carlo logic tree sampling filename = '%s-%s-smltp_%s-gsimltp_%s-ltr_%s.%s' % ( out, location, sm_ltp, gsim_ltp, ltr.ordinal, file_ext) else: # End Branch Enumeration filename = '%s-%s-smltp_%s-gsimltp_%s.%s' % (out, location, sm_ltp, gsim_ltp, file_ext) else: filename = '%s.%s' % (output_type, file_ext) return os.path.abspath(os.path.join(directory, filename))
def _get_result_export_path(calc_id, target_dir, result): """ Get the full absolute path (including file name) for a given ``result``. As a side effect, intermediate directories are created such that the file can be created and written to immediately. :param int calc_id: ID of the associated :class:`openquake.engine.db.models.HazardCalculation`. :param str target_dir: Destination directory location for exported files. :param result: :mod:`openquake.engine.db.models` result object with a foreign key reference to :class:`~openquake.engine.db.models.Output`. :returns: Full path (including filename) to the destination export file. """ output = result.output output_type = output.output_type # Create the names for each subdirectory calc_dir = 'calc_%s' % calc_id type_dir = output_type if output_type == 'complete_lt_gmf': type_dir = 'gmf' elif output_type == 'complete_lt_ses': type_dir = 'ses' imt_dir = '' # if blank, we don't have an IMT dir if output_type in ('hazard_curve', 'hazard_map', 'disagg_matrix'): imt_dir = result.imt if result.imt == 'SA': imt_dir = 'SA-%s' % result.sa_period # construct the directory which will contain the result XML file: directory = os.path.join(target_dir, calc_dir, type_dir, imt_dir) core.makedirs(directory) if output_type in ('hazard_curve', 'hazard_map', 'uh_spectra'): # include the poe in hazard map and uhs file names if output_type in ('hazard_map', 'uh_spectra'): output_type = '%s-poe_%s' % (output_type, result.poe) if result.statistics is not None: # we could have stats if result.statistics == 'quantile': # quantile filename = '%s-%s.xml' % (output_type, 'quantile_%s' % result.quantile) else: # mean filename = '%s-%s.xml' % (output_type, result.statistics) else: # otherwise, we need to include logic tree branch info ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if ltr.weight is None: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.xml' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal ) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.xml' % ( output_type, sm_ltp, gsim_ltp ) elif output_type in ('gmf', 'ses'): # only logic trees, no stats ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if ltr.weight is None: # Monte-Carlo logic tree sampling filename = '%s-smltp_%s-gsimltp_%s-ltr_%s.xml' % ( output_type, sm_ltp, gsim_ltp, ltr.ordinal ) else: # End Branch Enumeration filename = '%s-smltp_%s-gsimltp_%s.xml' % ( output_type, sm_ltp, gsim_ltp ) elif output_type == 'disagg_matrix': # only logic trees, no stats location = 'lon_%s-lat_%s' % (result.location.x, result.location.y) ltr = result.lt_realization sm_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.sm_lt_path) gsim_ltp = core.LT_PATH_JOIN_TOKEN.join(ltr.gsim_lt_path) if ltr.weight is None: # Monte-Carlo logic tree sampling filename = '%s-%s-smltp_%s-gsimltp_%s-ltr_%s.xml' % ( output_type, location, sm_ltp, gsim_ltp, ltr.ordinal ) else: # End Branch Enumeration filename = '%s-%s-smltp_%s-gsimltp_%s.xml' % ( output_type, location, sm_ltp, gsim_ltp ) else: filename = '%s.xml' % output_type return os.path.abspath(os.path.join(directory, filename))