def export(output_id, target_dir, check_schema=False): """ Export the given hazard calculation output from the database to the specified directory. :param int output_id: ID of a :class:`openquake.engine.db.models.Output`. :param str target_dir: Directory where output artifacts should be written. :param bool check_schema: If True, checks that the generated file is valid for the NRML schema :returns: List of file names (including the full directory path) containing the exported results. The quantity and type of the result files depends on the type of output, as well as calculation parameters. (See the `output_type` attribute of :class:`openquake.engine.db.models.Output`.) """ output = models.Output.objects.get(id=output_id) export_fn = globals().get( 'export_' + output.output_type, core._export_fn_not_implemented) fnames = export_fn(output, os.path.expanduser(target_dir)) if check_schema: for fname in fnames: nrmllib.assert_valid(fname) return fnames
def check_export(output_id, target): """ Call hazard.export by checking that the exported file is valid according to our XML schema. """ out_file = hazard.export(output_id, target, 'xml') nrmllib.assert_valid(out_file) return out_file
def node_to_nrml(node, output=sys.stdout, nsmap=None): """ Convert a node into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and checked against the NRML schema. :params node: a Node object :params output: a file-like object in write or read-write mode :params nsmap: a dictionary with the XML namespaces (default the NRML ones) """ nsmap = nsmap or nrmllib.SERIALIZE_NS_MAP root = Node('nrml', nodes=[node]) for nsname, nsvalue in nsmap.iteritems(): if nsname is None: root['xmlns'] = nsvalue else: root['xmlns:%s' % nsname] = nsvalue node_to_xml(root, output) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) nrmllib.assert_valid(output)
def node_to_nrml(node, output=sys.stdout, nsmap=None): """ Convert a node into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and checked against the NRML schema. :params node: a Node object :params output: a file-like object in write or read-write mode :params nsmap: a dictionary with the XML namespaces (default the NRML ones) """ assert isinstance(node, Node), node # better safe than sorry nsmap = nsmap or nrmllib.SERIALIZE_NS_MAP root = Node('nrml', nodes=[node]) for nsname, nsvalue in nsmap.iteritems(): if nsname is None: root['xmlns'] = nsvalue else: root['xmlns:%s' % nsname] = nsvalue node_to_xml(root, output) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) nrmllib.assert_valid(output)
def node_from_nrml(xmlfile, nodefactory=Node): """ Convert a NRML file into a Node object. :param xmlfile: a file name or file object open for reading """ root = nrmllib.assert_valid(xmlfile).getroot() node = node_from_elem(root, nodefactory) for nsname, nsvalue in root.nsmap.iteritems(): if nsname is None: node['xmlns'] = nsvalue else: node['xmlns:%s' % nsname] = nsvalue return node