Exemplo n.º 1
0
def _info(name, filtersources, weightsources):
    if name in base.calculators:
        print(textwrap.dedent(base.calculators[name].__doc__.strip()))
    elif name == 'gsims':
        for gs in gsim.get_available_gsims():
            print(gs)
    elif name.endswith('.xml'):
        print(nrml.read(name).to_str())
    elif name.endswith(('.ini', '.zip')):
        oqparam = readinput.get_oqparam(name)
        if 'exposure' in oqparam.inputs:
            expo = readinput.get_exposure(oqparam)
            sitecol, assets_by_site = readinput.get_sitecol_assets(
                oqparam, expo)
        elif filtersources or weightsources:
            sitecol = readinput.get_site_collection(oqparam)
        else:
            sitecol = None
        if 'source_model_logic_tree' in oqparam.inputs:
            print('Reading the source model...')
            if weightsources:
                sp = source.SourceFilterWeighter
            elif filtersources:
                sp = source.SourceFilter
            else:
                sp = source.BaseSourceProcessor  # do nothing
            csm = readinput.get_composite_source_model(oqparam, sitecol, sp)
            assoc = csm.get_rlzs_assoc()
            dstore = datastore.Fake(vars(oqparam),
                                    rlzs_assoc=assoc,
                                    composite_source_model=csm,
                                    sitecol=sitecol)
            _print_info(dstore, filtersources, weightsources)
    else:
        print("No info for '%s'" % name)
Exemplo n.º 2
0
 def setUpClass(cls):
     cls.oqparam = readinput.get_oqparam('job_loss.ini', pkg=case_2)
     cls.oqparam.insured_losses = True
     cls.sitecol, cls.assets_by_site = readinput.get_sitecol_assets(
         cls.oqparam, readinput.get_exposure(cls.oqparam))
     rmdict = riskmodels.get_risk_models(cls.oqparam)
     cls.riskmodel = readinput.get_risk_model(cls.oqparam, rmdict)
Exemplo n.º 3
0
 def pre_execute(self):
     """
     Read the site collection and initialize GmfComputer, tags and seeds
     """
     if 'exposure' in self.oqparam.inputs:
         logging.info('Reading the exposure')
         exposure = readinput.get_exposure(self.oqparam)
         logging.info('Reading the site collection')
         self.sitecol, _assets = readinput.get_sitecol_assets(
             self.oqparam, exposure)
     else:
         self.sitecol = readinput.get_site_collection(self.oqparam)
     self._init_tags()
Exemplo n.º 4
0
    def read_exposure_sitecol(self):
        """
        Read the exposure (if any) and then the site collection, possibly
        extracted from the exposure.
        """
        inputs = self.oqparam.inputs

        if 'gmfs' in inputs and self.oqparam.sites:
            haz_sitecol = self.sitecol = readinput.get_site_collection(
                self.oqparam)
        if 'scenario_' in self.oqparam.calculation_mode:
            self.gmfs = get_gmfs(self)
            haz_sitecol = self.sitecol
        if 'exposure' in inputs:
            logging.info('Reading the exposure')
            with self.monitor('reading exposure', autoflush=True):
                self.exposure = readinput.get_exposure(self.oqparam)
                self.sitecol, self.assets_by_site = (
                    readinput.get_sitecol_assets(self.oqparam, self.exposure))
                self.cost_types = self.exposure.cost_types
                self.taxonomies = numpy.array(
                    sorted(self.exposure.taxonomies), '|S100')
            num_assets = self.count_assets()
            if self.datastore.parent:
                haz_sitecol = self.datastore.parent['sitecol']
            elif 'gmfs' in inputs:
                pass  # haz_sitecol is already defined
            # TODO: think about the case hazard_curves in inputs
            else:
                haz_sitecol = None
            if haz_sitecol is not None and haz_sitecol != self.sitecol:
                with self.monitor('assoc_assets_sites'):
                    self.sitecol, self.assets_by_site = \
                        self.assoc_assets_sites(haz_sitecol.complete)
                ok_assets = self.count_assets()
                num_sites = len(self.sitecol)
                logging.warn('Associated %d assets to %d sites, %d discarded',
                             ok_assets, num_sites, num_assets - ok_assets)
        elif (self.datastore.parent and 'exposure' in
              OqParam.from_(self.datastore.parent.attrs).inputs):
            logging.info('Re-using the already imported exposure')
        else:  # no exposure
            logging.info('Reading the site collection')
            with self.monitor('reading site collection', autoflush=True):
                self.sitecol = readinput.get_site_collection(self.oqparam)

        # save mesh and asset collection
        self.save_mesh()
        if hasattr(self, 'assets_by_site'):
            self.assetcol = riskinput.build_asset_collection(
                self.assets_by_site, self.oqparam.time_event)
Exemplo n.º 5
0
 def read_exposure(self):
     """
     Read the exposure, the riskmodel and update the attributes .exposure,
     .sitecol, .assets_by_site, .taxonomies.
     """
     logging.info('Reading the exposure')
     with self.monitor('reading exposure', autoflush=True):
         self.exposure = readinput.get_exposure(self.oqparam)
         arefs = numpy.array(self.exposure.asset_refs, hdf5.vstr)
         self.datastore['asset_refs'] = arefs
         self.datastore.set_attrs('asset_refs', nbytes=arefs.nbytes)
         self.cost_calculator = readinput.get_cost_calculator(self.oqparam)
         self.sitecol, self.assets_by_site = (
             readinput.get_sitecol_assets(self.oqparam, self.exposure))
Exemplo n.º 6
0
 def read_exposure(self):
     """
     Read the exposure, the riskmodel and update the attributes .exposure,
     .sitecol, .assets_by_site, .cost_types, .taxonomies.
     """
     logging.info('Reading the exposure')
     with self.monitor('reading exposure', autoflush=True):
         self.exposure = readinput.get_exposure(self.oqparam)
         self.sitecol, self.assets_by_site = (readinput.get_sitecol_assets(
             self.oqparam, self.exposure))
         if len(self.exposure.cost_types):
             self.cost_types = self.exposure.cost_types
         self.taxonomies = numpy.array(sorted(self.exposure.taxonomies),
                                       '|S100')
         self.datastore['time_events'] = sorted(self.exposure.time_events)
Exemplo n.º 7
0
 def pre_execute(self):
     """
     Set the attributes .riskmodel, .sitecol, .assets_by_site
     """
     self.riskmodel = readinput.get_risk_model(self.oqparam)
     self.exposure = readinput.get_exposure(self.oqparam)
     logging.info('Read an exposure with %d assets of %d taxonomies',
                  len(self.exposure.assets), len(self.exposure.taxonomies))
     missing = self.exposure.taxonomies - set(
         self.riskmodel.get_taxonomies())
     if missing:
         raise RuntimeError('The exposure contains the taxonomies %s '
                            'which are not in the risk model' % missing)
     self.sitecol, self.assets_by_site = readinput.get_sitecol_assets(
         self.oqparam, self.exposure)
     logging.info('Extracted %d unique sites from the exposure',
                  len(self.sitecol))
Exemplo n.º 8
0
 def read_exposure(self):
     """
     Read the exposure, the riskmodel and update the attributes .exposure,
     .sitecol, .assets_by_site, .cost_types, .taxonomies.
     """
     logging.info("Reading the exposure")
     with self.monitor("reading exposure", autoflush=True):
         self.exposure = readinput.get_exposure(self.oqparam)
         arefs = numpy.array(self.exposure.asset_refs)
         self.datastore["asset_refs"] = arefs
         self.datastore.set_attrs("asset_refs", nbytes=arefs.nbytes)
         all_cost_types = set(self.oqparam.all_cost_types)
         fname = self.oqparam.inputs["exposure"]
         self.cost_calculator = readinput.get_exposure_lazy(fname, all_cost_types)[-1]
         self.sitecol, self.assets_by_site = readinput.get_sitecol_assets(self.oqparam, self.exposure)
         if len(self.exposure.cost_types):
             self.cost_types = self.exposure.cost_types
Exemplo n.º 9
0
    def read_exposure_sitecol(self):
        """
        Read the exposure (if any) and then the site collection, possibly
        extracted from the exposure.
        """
        logging.info('Reading the site collection')
        with self.monitor('reading site collection', autoflush=True):
            haz_sitecol = readinput.get_site_collection(self.oqparam)
        inputs = self.oqparam.inputs
        if 'exposure' in inputs:
            logging.info('Reading the exposure')
            with self.monitor('reading exposure', autoflush=True):
                self.exposure = readinput.get_exposure(self.oqparam)
                self.sitecol, self.assets_by_site = (
                    readinput.get_sitecol_assets(self.oqparam, self.exposure))
                if len(self.exposure.cost_types):
                    self.cost_types = self.exposure.cost_types
                self.taxonomies = numpy.array(
                    sorted(self.exposure.taxonomies), '|S100')
            num_assets = self.count_assets()
            if self.datastore.parent:
                haz_sitecol = self.datastore.parent['sitecol']
            if haz_sitecol is not None and haz_sitecol != self.sitecol:
                with self.monitor('assoc_assets_sites'):
                    self.sitecol, self.assets_by_site = \
                        self.assoc_assets_sites(haz_sitecol.complete)
                ok_assets = self.count_assets()
                num_sites = len(self.sitecol)
                logging.warn('Associated %d assets to %d sites, %d discarded',
                             ok_assets, num_sites, num_assets - ok_assets)
        elif (self.datastore.parent and 'exposure' in
              OqParam.from_(self.datastore.parent.attrs).inputs):
            logging.info('Re-using the already imported exposure')
        else:  # no exposure
            self.sitecol = haz_sitecol

        # save mesh and asset collection
        self.save_mesh()
        if hasattr(self, 'assets_by_site'):
            self.assetcol = riskinput.build_asset_collection(
                self.assets_by_site, self.oqparam.time_event)
            spec = set(self.oqparam.specific_assets)
            unknown = spec - set(self.assetcol['asset_ref'])
            if unknown:
                raise ValueError('The specific asset(s) %s are not in the '
                                 'exposure' % ', '.join(unknown))
Exemplo n.º 10
0
    def read_exposure_sitecol(self):
        """
        Read the exposure (if any) and then the site collection, possibly
        extracted from the exposure.
        """
        if 'exposure' in self.oqparam.inputs:
            logging.info('Reading the exposure')
            with self.monitor('reading exposure', autoflush=True):
                self.exposure = readinput.get_exposure(self.oqparam)
                self.sitecol, self.assets_by_site = (
                    readinput.get_sitecol_assets(self.oqparam, self.exposure))
                self.cost_types = self.exposure.cost_types
                self.taxonomies = numpy.array(
                    sorted(self.exposure.taxonomies), '|S100')
            num_assets = self.count_assets()
            mesh = readinput.get_mesh(self.oqparam)
            if self.datastore.parent:
                parent_mesh = self.datastore.parent['sitemesh'].value
                if mesh is None:
                    mesh = Mesh(parent_mesh['lon'], parent_mesh['lat'])
            if mesh is not None:
                sites = readinput.get_site_collection(self.oqparam, mesh)
                with self.monitor('assoc_assets_sites'):
                    self.sitecol, self.assets_by_site = \
                        self.assoc_assets_sites(sites)
                ok_assets = self.count_assets()
                num_sites = len(self.sitecol)
                logging.warn('Associated %d assets to %d sites, %d discarded',
                             ok_assets, num_sites, num_assets - ok_assets)

            if (self.is_stochastic and self.datastore.parent and
                    self.datastore.parent['sitecol'] != self.sitecol):
                logging.warn(
                    'The hazard sites are different from the risk sites %s!=%s'
                    % (self.datastore.parent['sitecol'], self.sitecol))
        else:  # no exposure
            logging.info('Reading the site collection')
            with self.monitor('reading site collection', autoflush=True):
                self.sitecol = readinput.get_site_collection(self.oqparam)

        # save mesh and asset collection
        self.save_mesh()
        if hasattr(self, 'assets_by_site'):
            self.assetcol = riskinput.build_asset_collection(
                self.assets_by_site, self.oqparam.time_event)
Exemplo n.º 11
0
    def read_exposure_sitecol(self):
        """
        Read the exposure (if any) and then the site collection, possibly
        extracted from the exposure.
        """
        inputs = self.oqparam.inputs
        if 'exposure' in inputs:
            logging.info('Reading the exposure')
            with self.monitor('reading exposure', autoflush=True):
                self.exposure = readinput.get_exposure(self.oqparam)
                self.sitecol, self.assets_by_site = (
                    readinput.get_sitecol_assets(self.oqparam, self.exposure))
                self.cost_types = self.exposure.cost_types
                self.taxonomies = numpy.array(
                    sorted(self.exposure.taxonomies), '|S100')
            num_assets = self.count_assets()
            if self.datastore.parent:
                haz_sitecol = self.datastore.parent['sitecol']
            elif 'gmfs' in inputs:
                haz_sitecol = readinput.get_site_collection(self.oqparam)
            # TODO: think about the case hazard_curves in inputs
            else:
                haz_sitecol = None
            if haz_sitecol is not None and haz_sitecol != self.sitecol:
                with self.monitor('assoc_assets_sites'):
                    self.sitecol, self.assets_by_site = \
                        self.assoc_assets_sites(haz_sitecol.complete)
                ok_assets = self.count_assets()
                num_sites = len(self.sitecol)
                logging.warn('Associated %d assets to %d sites, %d discarded',
                             ok_assets, num_sites, num_assets - ok_assets)
        elif (self.datastore.parent and 'exposure' in
              self.datastore.parent['oqparam'].inputs):
            logging.info('Re-using the already imported exposure')
        else:  # no exposure
            logging.info('Reading the site collection')
            with self.monitor('reading site collection', autoflush=True):
                self.sitecol = readinput.get_site_collection(self.oqparam)

        # save mesh and asset collection
        self.save_mesh()
        if hasattr(self, 'assets_by_site'):
            self.assetcol = riskinput.build_asset_collection(
                self.assets_by_site, self.oqparam.time_event)
Exemplo n.º 12
0
 def read_exposure(self):
     """
     Read the exposure, the riskmodel and update the attributes .exposure,
     .sitecol, .assets_by_site, .cost_types, .taxonomies.
     """
     logging.info('Reading the exposure')
     with self.monitor('reading exposure', autoflush=True):
         self.exposure = readinput.get_exposure(self.oqparam)
         arefs = numpy.array(self.exposure.asset_refs)
         self.datastore['asset_refs'] = arefs
         self.datastore.set_attrs('asset_refs', nbytes=arefs.nbytes)
         all_cost_types = set(self.oqparam.all_cost_types)
         fname = self.oqparam.inputs['exposure']
         self.cost_calculator = readinput.get_exposure_lazy(
             fname, all_cost_types)[-1]
         self.sitecol, self.assets_by_site = (readinput.get_sitecol_assets(
             self.oqparam, self.exposure))
         if len(self.exposure.cost_types):
             self.cost_types = self.exposure.cost_types
Exemplo n.º 13
0
def _info(name, filtersources, weightsources):
    if name in base.calculators:
        print(textwrap.dedent(base.calculators[name].__doc__.strip()))
    elif name == 'gsims':
        for gs in gsim.get_available_gsims():
            print(gs)
    elif name.endswith('.xml'):
        print(nrml.read(name).to_str())
    elif name.endswith(('.ini', '.zip')):
        oqparam = readinput.get_oqparam(name)
        if 'exposure' in oqparam.inputs:
            expo = readinput.get_exposure(oqparam)
            sitecol, assets_by_site = readinput.get_sitecol_assets(
                oqparam, expo)
        elif filtersources or weightsources:
            sitecol, assets_by_site = readinput.get_site_collection(
                oqparam), []
        else:
            sitecol, assets_by_site = None, []
        if 'source_model_logic_tree' in oqparam.inputs:
            print('Reading the source model...')
            if weightsources:
                sp = source.SourceFilterWeighter
            elif filtersources:
                sp = source.SourceFilter
            else:
                sp = source.BaseSourceProcessor  # do nothing
            csm = readinput.get_composite_source_model(oqparam, sitecol, sp)
            assoc = csm.get_rlzs_assoc()
            dstore = datastore.Fake(vars(oqparam),
                                    rlzs_assoc=assoc,
                                    composite_source_model=csm,
                                    sitecol=sitecol)
            _print_info(dstore, filtersources, weightsources)
        if len(assets_by_site):
            assetcol = riskinput.build_asset_collection(assets_by_site)
            dic = groupby(assetcol, operator.attrgetter('taxonomy'))
            for taxo, num in dic.items():
                print('taxonomy #%d, %d assets' % (taxo, num))
            print('total assets = %d' % len(assetcol))
    else:
        print("No info for '%s'" % name)
Exemplo n.º 14
0
 def pre_execute(self):
     """
     Read the site collection and the sources.
     """
     if 'exposure' in self.oqparam.inputs:
         logging.info('Reading the exposure')
         exposure = readinput.get_exposure(self.oqparam)
         self.sitecol, _assets = readinput.get_sitecol_assets(
             self.oqparam, exposure)
     else:
         logging.info('Reading the site collection')
         self.sitecol = readinput.get_site_collection(self.oqparam)
     logging.info('Reading the effective source models')
     source_models = list(
         readinput.get_effective_source_models(self.oqparam, self.sitecol))
     self.all_sources = [src for src_model in source_models
                         for trt_model in src_model.trt_models
                         for src in trt_model]
     self.job_info = readinput.get_job_info(
         self.oqparam, source_models, self.sitecol)
Exemplo n.º 15
0
def _info(name, filtersources, weightsources):
    if name in base.calculators:
        print(textwrap.dedent(base.calculators[name].__doc__.strip()))
    elif name == 'gsims':
        for gs in gsim.get_available_gsims():
            print(gs)
    elif name.endswith('.xml'):
        print(nrml.read(name).to_str())
    elif name.endswith(('.ini', '.zip')):
        oqparam = readinput.get_oqparam(name)
        if 'exposure' in oqparam.inputs:
            expo = readinput.get_exposure(oqparam)
            sitecol, assets_by_site = readinput.get_sitecol_assets(
                oqparam, expo)
        elif filtersources or weightsources:
            sitecol, assets_by_site = readinput.get_site_collection(
                oqparam), []
        else:
            sitecol, assets_by_site = None, []
        if 'source_model_logic_tree' in oqparam.inputs:
            print('Reading the source model...')
            if weightsources:
                sp = source.SourceFilterWeighter
            elif filtersources:
                sp = source.SourceFilter
            else:
                sp = source.BaseSourceProcessor  # do nothing
            csm = readinput.get_composite_source_model(oqparam, sitecol, sp)
            assoc = csm.get_rlzs_assoc()
            _print_info(
                dict(rlzs_assoc=assoc, oqparam=oqparam,
                     composite_source_model=csm, sitecol=sitecol),
                filtersources, weightsources)
        if len(assets_by_site):
            assetcol = riskinput.build_asset_collection(assets_by_site)
            dic = groupby(assetcol, operator.attrgetter('taxonomy'))
            for taxo, num in dic.items():
                print('taxonomy #%d, %d assets' % (taxo, num))
            print('total assets = %d' % len(assetcol))
    else:
        print("No info for '%s'" % name)
Exemplo n.º 16
0
def _info(name, filtersources, weightsources):
    if name in base.calculators:
        print(textwrap.dedent(base.calculators[name].__doc__.strip()))
    elif name == "gsims":
        for gs in gsim.get_available_gsims():
            print(gs)
    elif name.endswith(".xml"):
        print(nrml.read(name).to_str())
    elif name.endswith((".ini", ".zip")):
        oqparam = readinput.get_oqparam(name)
        if "exposure" in oqparam.inputs:
            expo = readinput.get_exposure(oqparam)
            sitecol, assets_by_site = readinput.get_sitecol_assets(oqparam, expo)
        elif filtersources or weightsources:
            sitecol, assets_by_site = readinput.get_site_collection(oqparam), []
        else:
            sitecol, assets_by_site = None, []
        if "source_model_logic_tree" in oqparam.inputs:
            print("Reading the source model...")
            if weightsources:
                sp = source.SourceFilterWeighter
            elif filtersources:
                sp = source.SourceFilter
            else:
                sp = source.BaseSourceProcessor  # do nothing
            csm = readinput.get_composite_source_model(oqparam, sitecol, sp)
            assoc = csm.get_rlzs_assoc()
            dstore = datastore.Fake(vars(oqparam), rlzs_assoc=assoc, composite_source_model=csm, sitecol=sitecol)
            _print_info(dstore, filtersources, weightsources)
        if len(assets_by_site):
            assetcol = riskinput.build_asset_collection(assets_by_site)
            dic = groupby(assetcol, operator.attrgetter("taxonomy"))
            for taxo, num in dic.items():
                print("taxonomy #%d, %d assets" % (taxo, num))
            print("total assets = %d" % len(assetcol))
    else:
        print("No info for '%s'" % name)
Exemplo n.º 17
0
def _info(name, filtersources, weightsources):
    if name in base.calculators:
        print(textwrap.dedent(base.calculators[name].__doc__.strip()))
    elif name == 'gsims':
        for gs in gsim.get_available_gsims():
            print(gs)
    elif name.endswith('.xml'):
        print(nrml.read(name).to_str())
    elif name.endswith(('.ini', '.zip')):
        oqparam = readinput.get_oqparam(name)
        if 'exposure' in oqparam.inputs:
            expo = readinput.get_exposure(oqparam)
            sitecol, assets_by_site = readinput.get_sitecol_assets(
                oqparam, expo)
        elif filtersources or weightsources:
            sitecol, assets_by_site = readinput.get_site_collection(
                oqparam), []
        else:
            sitecol, assets_by_site = None, []
        if 'source_model_logic_tree' in oqparam.inputs:
            print('Reading the source model...')
            if weightsources:
                sp = source.SourceFilterWeighter
            elif filtersources:
                sp = source.SourceFilter
            else:
                sp = source.BaseSourceProcessor  # do nothing
            csm = readinput.get_composite_source_model(oqparam, sitecol, sp)
            assoc = csm.get_rlzs_assoc()
            _print_info(assoc, oqparam, csm, sitecol,
                        filtersources, weightsources)
        if len(assets_by_site):
            print('assets = %d' %
                  sum(len(assets) for assets in assets_by_site))
    else:
        print("No info for '%s'" % name)
Exemplo n.º 18
0
#  GNU 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/>.

"""
This is an example of script which is useful if you want to play with
the RiskInput objects. You can enable the pdb and see what is inside
the objects.
"""
from __future__ import print_function

import sys
from openquake.commonlib import readinput
from openquake.calculators.calc import calc_gmfs

if __name__ == '__main__':
    job_ini = sys.argv[1:]
    o = readinput.get_oqparam(job_ini)
    exposure = readinput.get_exposure(o)
    sitecol, assets_by_site = readinput.get_sitecol_assets(o, exposure)
    risk_model = readinput.get_risk_model(o)
    gmfs_by_imt = calc_gmfs(o, sitecol)

    for imt in gmfs_by_imt:
        ri = risk_model.build_input(imt, gmfs_by_imt[imt], assets_by_site)
        print(ri)
        #for out in risk_model.gen_outputs([ri]):
        #    print out
        # import pdb; pdb.set_trace()
Exemplo n.º 19
0
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU 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/>.
"""
This is an example of script which is useful if you want to play with
the RiskInput objects. You can enable the pdb and see what is inside
the objects.
"""
from __future__ import print_function

import sys
from openquake.commonlib import readinput
from openquake.commonlib.calculators.calc import calc_gmfs

if __name__ == '__main__':
    job_ini = sys.argv[1:]
    o = readinput.get_oqparam(job_ini)
    exposure = readinput.get_exposure(o)
    sitecol, assets_by_site = readinput.get_sitecol_assets(o, exposure)
    risk_model = readinput.get_risk_model(o)
    gmfs_by_imt = calc_gmfs(o, sitecol)

    for imt in gmfs_by_imt:
        ri = risk_model.build_input(imt, gmfs_by_imt[imt], assets_by_site)
        print(ri)
        #for out in risk_model.gen_outputs([ri]):
        #    print out
        # import pdb; pdb.set_trace()
Exemplo n.º 20
0
 def setUpClass(cls):
     cls.oqparam = readinput.get_oqparam(
         'job_haz.ini,job_risk.ini', pkg=case_2)
     cls.sitecol, cls.assets_by_site = readinput.get_sitecol_assets(
         cls.oqparam, readinput.get_exposure(cls.oqparam))
     cls.riskmodel = readinput.get_risk_model(cls.oqparam)