def zip_source_model(ssmLT, archive_zip='', log=logging.info): """ Zip the source model files starting from the smmLT.xml file """ basedir = os.path.dirname(ssmLT) if os.path.basename(ssmLT) != 'ssmLT.xml': orig = ssmLT ssmLT = os.path.join(basedir, 'ssmLT.xml') with open(ssmLT, 'wb') as f: f.write(open(orig, 'rb').read()) archive_zip = archive_zip or os.path.join(basedir, 'ssmLT.zip') if os.path.exists(archive_zip): sys.exit('%s exists already' % archive_zip) smlt = logictree.SourceModelLogicTree(ssmLT) files = list(smlt.hdf5_files) + smlt.info.smpaths oq = mock.Mock(inputs={'source_model_logic_tree': ssmLT}, random_seed=42, number_of_logic_tree_samples=0, sampling_method='early_weights') checksum = readinput.get_checksum32(oq) checkfile = os.path.join(os.path.dirname(ssmLT), 'CHECKSUM.txt') with open(checkfile, 'w') as f: f.write(str(checksum)) files.extend([os.path.abspath(ssmLT), os.path.abspath(checkfile)]) general.zipfiles(files, archive_zip, log=log, cleanup=True) return archive_zip
def get_source_model_lt(oqparam): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs['source_model_logic_tree'] return logictree.SourceModelLogicTree( fname, validate=False, seed=oqparam.random_seed, num_samples=oqparam.number_of_logic_tree_samples)
def get_input_files(oqparam, hazard=False): """ :param oqparam: an OqParam instance :param hazard: if True, consider only the hazard files :returns: input path names in a specific order """ fnames = set() # files entering in the checksum for key in oqparam.inputs: fname = oqparam.inputs[key] if hazard and key not in ('source_model_logic_tree', 'gsim_logic_tree', 'source'): continue # collect .hdf5 tables for the GSIMs, if any elif key == 'gsim_logic_tree': gsim_lt = get_gsim_lt(oqparam) for gsims in gsim_lt.values.values(): for gsim in gsims: for k, v in gsim.kwargs.items(): if k.endswith(('_file', '_table')): fnames.add(v) fnames.add(fname) elif key == 'source_model': # UCERF f = oqparam.inputs['source_model'] fnames.add(f) fname = nrml.read(f).sourceModel.UCERFSource['filename'] fnames.add(os.path.join(os.path.dirname(f), fname)) elif key == 'exposure': # fname is a list for exp in asset.Exposure.read_headers(fname): fnames.update(exp.datafiles) fnames.update(fname) elif isinstance(fname, dict): fnames.update(fname.values()) elif isinstance(fname, list): for f in fname: if f == oqparam.input_dir: raise InvalidFile('%s there is an empty path in %s' % (oqparam.inputs['job_ini'], key)) fnames.update(fname) elif key == 'source_model_logic_tree': args = (fname, oqparam.random_seed, oqparam.number_of_logic_tree_samples, oqparam.sampling_method) try: smlt = smlt_cache[args] except KeyError: smlt = smlt_cache[args] = logictree.SourceModelLogicTree(*args) fnames.update(smlt.hdf5_files) fnames.update(smlt.info.smpaths) fnames.add(fname) else: fnames.add(fname) return sorted(fnames)
def get_source_model_lt(oqparam): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs['source_model_logic_tree'] # NB: converting the random_seed into an integer is needed on Windows return logictree.SourceModelLogicTree( fname, validate=False, seed=int(oqparam.random_seed), num_samples=oqparam.number_of_logic_tree_samples)
def get_source_model_lt(oqparam): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs['source_model_logic_tree'] content = codecs.open(fname, encoding='utf8').read().encode('utf8') return logictree.SourceModelLogicTree( content, oqparam.base_path, fname, validate=False, seed=oqparam.random_seed, num_samples=oqparam.number_of_logic_tree_samples)
def get_source_model_lt(oqparam, branchID=None): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs['source_model_logic_tree'] args = (fname, oqparam.random_seed, oqparam.number_of_logic_tree_samples, oqparam.sampling_method, False, branchID) smlt = logictree.SourceModelLogicTree(*args) if oqparam.discard_trts: trts = set(trt.strip() for trt in oqparam.discard_trts.split(',')) # smlt.tectonic_region_types comes from applyToTectonicRegionType smlt.tectonic_region_types = smlt.tectonic_region_types - trts if oqparam.is_ucerf(): smlt.tectonic_region_types = {'Active Shallow Crust'} return smlt
def get_source_model_lt(oqparam, validate=True): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs.get('source_model_logic_tree') if fname: # NB: converting the random_seed into an integer is needed on Windows smlt = logictree.SourceModelLogicTree( fname, validate, seed=int(oqparam.random_seed), num_samples=oqparam.number_of_logic_tree_samples) else: smlt = logictree.FakeSmlt(oqparam.inputs['source_model'], int(oqparam.random_seed), oqparam.number_of_logic_tree_samples) return smlt
def get_source_model_lt(oqparam): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance """ fname = oqparam.inputs['source_model_logic_tree'] # NB: converting the random_seed into an integer is needed on Windows smlt = logictree.SourceModelLogicTree( fname, seed=int(oqparam.random_seed), num_samples=oqparam.number_of_logic_tree_samples) if oqparam.discard_trts: trts = set(trt.strip() for trt in oqparam.discard_trts.split(',')) # smlt.tectonic_region_types comes from applyToTectonicRegionType smlt.tectonic_region_types = smlt.tectonic_region_types - trts if 'ucerf' in oqparam.calculation_mode: smlt.tectonic_region_types = {'Active Shallow Crust'} return smlt