Example #1
0
def import_rows(job, ses_coll, gmf_coll, sorted_tags, rows):
    """
    Import a list of records into the gmf_data and hazard_site tables.

    :param job:
        :class:`openquake.engine.db.models.OqJob` instance
    :param gmf_coll:
        :class:`openquake.engine.db.models.Gmf` instance
    :param rows:
        a list of records (imt_type, sa_period, sa_damping, gmvs, wkt)
    """
    gmfs = []  # list of GmfData instance
    site_id = {}  # dictionary wkt -> site id
    rupture = fake_rupture()
    prob_rup_id, ses_rup_ids, seeds = create_db_ruptures(
        rupture, ses_coll, sorted_tags, seed=42)
    tag2id = dict(zip(sorted_tags, ses_rup_ids))

    for imt_str, tag, data in rows:
        imt = from_string(imt_str)
        rup_id = tag2id[tag]
        for wkt, gmv in data:
            if wkt not in site_id:  # create a new site
                site_id[wkt] = models.HazardSite.objects.create(
                    hazard_calculation=job, location=wkt).id
            gmfs.append(
                models.GmfData(
                    imt=imt[0], sa_period=imt[1], sa_damping=imt[2],
                    gmvs=[gmv], rupture_ids=[rup_id],
                    site_id=site_id[wkt], gmf=gmf_coll, task_no=0))
    writer.CacheInserter.saveall(gmfs)
Example #2
0
def save_gmf(gmfcoll_id, gmf_dict, sites):
    """
    Helper method to save computed GMF data to the database.

    :param int gmfcoll_id:
        the id of a :class:`openquake.engine.db.models.Gmf` record
    :param dict gmf_dict:
        The GMF results during the calculation
    :param sites:
        An :class:`openquake.engine.models.SiteCollection`
        object
    """
    inserter = writer.CacheInserter(models.GmfData, 100)
    # NB: GmfData may contain large arrays and the cache may become large

    for imt, gmfs_ in gmf_dict.iteritems():
        # ``gmfs`` comes in as a numpy.matrix
        # we want it is an array; it handles subscripting
        # in the way that we want
        gmfarray = numpy.array(gmfs_)

        sa_period = None
        sa_damping = None
        if isinstance(imt, openquake.hazardlib.imt.SA):
            sa_period = imt.period
            sa_damping = imt.damping
        imt_name = imt.__class__.__name__

        for i, site in enumerate(sites):
            inserter.add(models.GmfData(
                gmf_id=gmfcoll_id,
                ses_id=None,
                imt=imt_name,
                sa_period=sa_period,
                sa_damping=sa_damping,
                site_id=site.id,
                rupture_ids=None,
                gmvs=gmfarray[i].tolist()))

    inserter.flush()
Example #3
0
 def post_execute(self, result=None):
     """
     Saving the GMFs in the database
     """
     gmf_id = self.gmf.id
     inserter = writer.CacheInserter(models.GmfData, max_cache_size=1000)
     gsim = self.oqparam.gsim
     for imt in self.imts:
         gmfs = numpy.array([
             self.acc[tag][gsim][str(imt)] for tag in self.tags
         ]).transpose()
         for site_id, gmvs in zip(self.sites.sids, gmfs):
             inserter.add(
                 models.GmfData(gmf_id=gmf_id,
                                task_no=0,
                                imt=imt[0],
                                sa_period=imt[1],
                                sa_damping=imt[2],
                                site_id=site_id,
                                rupture_ids=self.rupids,
                                gmvs=list(gmvs)))
     inserter.flush()
Example #4
0
def import_rows(hc, gmf_coll, rows):
    """
    Import a list of records into the gmf_data and hazard_site tables.

    :param hc: :class:`openquake.engine.db.models.HazardCalculation` instance
    :param gmf_coll: :class:`openquake.engine.db.models.Gmf` instance
    :param rows: a list of records (imt_type, sa_period, sa_damping, gmvs, wkt)
    """
    gmfs = []
    site_id = {}  # dictionary wkt -> site id
    for imt_type, sa_period, sa_damping, gmvs, wkt in rows:
        if wkt not in site_id:  # create a new site
            site_id[wkt] = models.HazardSite.objects.create(
                hazard_calculation=hc, location=wkt).id
        gmfs.append(
            models.GmfData(imt=imt_type,
                           sa_period=sa_period,
                           sa_damping=sa_damping,
                           gmvs=gmvs,
                           site_id=site_id[wkt],
                           gmf=gmf_coll))
    del site_id
    writer.CacheInserter.saveall(gmfs)
Example #5
0
def _save_gmfs(ses, imt, gmvs_per_site, ruptures_per_site, sites):
    """
    Helper method to save computed GMF data to the database.
    :param ses:
        A :class:`openquake.engine.db.models.SES` instance
    :param imt:
        An intensity measure type instance
    :param gmf_per_site:
        The GMFs per rupture
    :param rupture_per_site:
        The associated rupture ids
    :param sites:
        An :class:`openquake.hazardlib.site.SiteCollection` object,
        representing the sites of interest for a calculation.
    """
    gmf_coll = models.Gmf.objects.get(
        lt_realization=ses.ses_collection.lt_realization)

    sa_period = None
    sa_damping = None
    if isinstance(imt, openquake.hazardlib.imt.SA):
        sa_period = imt.period
        sa_damping = imt.damping
    imt_name = imt.__class__.__name__

    for site_id in gmvs_per_site:
        inserter.add(models.GmfData(
            gmf=gmf_coll,
            ses_id=ses.id,
            imt=imt_name,
            sa_period=sa_period,
            sa_damping=sa_damping,
            site_id=site_id,
            gmvs=gmvs_per_site[site_id],
            rupture_ids=ruptures_per_site[site_id]))
    inserter.flush()