# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2016-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.importosimportnumpyfromopenquake.baselibimporthdf5,node,performancefromopenquake.hazardlibimportnrml
[docs]defconvert_xml_hdf5(input_file,output_file):withhdf5.File(output_file,'w')asout:inp=nrml.read(input_file)ifinp['xmlns'].endswith('nrml/0.4'):# old versiond=os.path.dirname(input_file)or'.'raiseValueError('Please upgrade with `oq upgrade_nrml %s`'%d)elifinp['xmlns'].endswith('nrml/0.5'):# current versionsm=inp.sourceModelelse:# not a NRMLraiseValueError('Unknown NRML:'%inp['xmlns'])out.save(node.node_to_dict(sm))returnoutput_file
[docs]defmain(input):""" Convert .xml and .npz files to .hdf5 files. """withperformance.Monitor('to_hdf5')asmon:forinput_fileininput:ifinput_file.endswith('.npz'):output=convert_npz_hdf5(input_file,input_file[:-3]+'hdf5')elifinput_file.endswith('.xml'):# for source model filesoutput=convert_xml_hdf5(input_file,input_file[:-3]+'hdf5')else:continueprint('Generated %s'%output)print(mon)
main.input=dict(help='.npz file to convert',nargs='+')