<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2010-2014, 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 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/>.

"""
Scenario calculator core functionality
"""
import random
from django.db import transaction
import numpy

from openquake.nrmllib.hazard.parsers import RuptureModelParser

# HAZARDLIB
from openquake.hazardlib.calc import ground_motion_fields
from openquake.hazardlib.imt import from_string
import openquake.hazardlib.gsim

from openquake.engine.calculators.hazard import general as haz_general
from openquake.engine.utils import tasks, general
from openquake.engine.db import models
from openquake.engine.input import source
from openquake.engine import writer
from openquake.engine.performance import EnginePerformanceMonitor

AVAILABLE_GSIMS = openquake.hazardlib.gsim.get_available_gsims()


@tasks.oqtask
def gmfs(job_id, seeds, sites, rupture, gmf_id, task_no):
    """
    A celery task wrapper function around :func:`compute_gmfs`.
    See :func:`compute_gmfs` for parameter definitions.
    """
    for task_seed in seeds:
        numpy.random.seed(task_seed)
        with EnginePerformanceMonitor('computing gmfs', job_id, gmfs):
            # TODO: add filtering on maximum_distance
            gmf_dict = compute_gmfs(job_id, sites, rupture, gmf_id)
        with EnginePerformanceMonitor('saving gmfs', job_id, gmfs):
            save_gmf(gmf_id, gmf_dict, sites, task_no)


def compute_gmfs(job_id, sites, rupture, gmf_id):
    """
    Compute ground motion fields and store them in the db.

    :param job_id:
        ID of the currently running job.
    :param sites:
        The subset of the full SiteCollection scanned by this task
    :param rupture:
        The hazardlib rupture from which we will generate
        ground motion fields.
    :param gmf_id:
        the id of a :class:`openquake.engine.db.models.Gmf` record
    :param realizations:
        Number of realizations to create.
    """
    hc = models.HazardCalculation.objects.get(oqjob=job_id)
    imts = [from_string(x) for x in hc.intensity_measure_types]
    gsim = AVAILABLE_GSIMS[hc.gsim]()  # instantiate the GSIM class
    correlation_model = haz_general.get_correl_model(hc)
    return ground_motion_fields(
        rupture, sites, imts, gsim,
        hc.truncation_level, realizations=1,
        correlation_model=correlation_model)


@transaction.commit_on_success(using='job_init')
def save_gmf(gmf_id, gmf_dict, sites, task_no):
    """
    Helper method to save computed GMF data to the database.

    :param int gmf_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.hazardlib.site.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<commit_msg>Increased the cache in the CacheInserter<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2010-2014, 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 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/>.

"""
Scenario calculator core functionality
"""
import random
from django.db import transaction
import numpy

from openquake.nrmllib.hazard.parsers import RuptureModelParser

# HAZARDLIB
from openquake.hazardlib.calc import ground_motion_fields
from openquake.hazardlib.imt import from_string
import openquake.hazardlib.gsim

from openquake.engine.calculators.hazard import general as haz_general
from openquake.engine.utils import tasks, general
from openquake.engine.db import models
from openquake.engine.input import source
from openquake.engine import writer
from openquake.engine.performance import EnginePerformanceMonitor

AVAILABLE_GSIMS = openquake.hazardlib.gsim.get_available_gsims()


@tasks.oqtask
def gmfs(job_id, seeds, sites, rupture, gmf_id, task_no):
    """
    A celery task wrapper function around :func:`compute_gmfs`.
    See :func:`compute_gmfs` for parameter definitions.
    """
    for task_seed in seeds:
        numpy.random.seed(task_seed)
        with EnginePerformanceMonitor('computing gmfs', job_id, gmfs):
            # TODO: add filtering on maximum_distance
            gmf_dict = compute_gmfs(job_id, sites, rupture, gmf_id)
        with EnginePerformanceMonitor('saving gmfs', job_id, gmfs):
            save_gmf(gmf_id, gmf_dict, sites, task_no)


def compute_gmfs(job_id, sites, rupture, gmf_id):
    """
    Compute ground motion fields and store them in the db.

    :param job_id:
        ID of the currently running job.
    :param sites:
        The subset of the full SiteCollection scanned by this task
    :param rupture:
        The hazardlib rupture from which we will generate
        ground motion fields.
    :param gmf_id:
        the id of a :class:`openquake.engine.db.models.Gmf` record
    :param realizations:
        Number of realizations to create.
    """
    hc = models.HazardCalculation.objects.get(oqjob=job_id)
    imts = [from_string(x) for x in hc.intensity_measure_types]
    gsim = AVAILABLE_GSIMS[hc.gsim]()  # instantiate the GSIM class
    correlation_model = haz_general.get_correl_model(hc)
    return ground_motion_fields(
        rupture, sites, imts, gsim,
        hc.truncation_level, realizations=1,
        correlation_model=correlation_model)


@transaction.commit_on_success(using='job_init')
def save_gmf(gmf_id, gmf_dict, sites, task_no):
    """
    Helper method to save computed GMF data to the database.

    :param int gmf_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.hazardlib.site.SiteCollection` object
    """
    inserter = writer.CacheInserter(models.GmfData, 10000)
    # insert GmfData in blocks of 10,000 sites
    # NB: GmfData contains arrays of lenght 1

    for imt, gmfs_ in gmf_dict.iteritems():
        # ``gmfs`` comes in as a numpy.matrix
        # we want it is an array; it handles subscripting
{'fraction_non_alphanumeric': 0.07583965330444203, 'fraction_numerical': 0.004875406283856988, 'mean_word_length': 3.67701076630779, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '250404', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2217, 'n_words': 752}