Esempio n. 1
0
def create_gmf_data_records(hazard_job, coordinates=None):
    """
    Returns the created records.

    :param hazard_joint: a :class:`openquake.engine.db.models.OqJob` instance
    :param coordinates: a list of (lon, lat) pairs

    If the coordinates are not set, a list of 5 predefined locations is used
    """
    output = models.Output.objects.create_output(
        hazard_job, "Test SES Collection", "ses")
    gmf, ses_coll = create_gmf_sescoll(output)
    ruptures = create_ses_ruptures(hazard_job, ses_coll, 3)
    records = []
    if coordinates is None:
        coordinates = [(15.310, 38.225), (15.71, 37.225),
                       (15.48, 38.091), (15.565, 38.17),
                       (15.481, 38.25)]
    site_ids = models.save_sites(hazard_job, coordinates)[1]
    for site_id in site_ids:
        records.append(models.GmfData.objects.create(
            gmf=gmf,
            task_no=0,
            imt="PGA",
            gmvs=[0.1, 0.2, 0.3],
            rupture_ids=[r.id for r in ruptures],
            site_id=site_id))

    return records
Esempio n. 2
0
def create_gmf_data_records(hazard_job, coordinates=None):
    """
    Returns the created records.

    :param hazard_joint: a :class:`openquake.engine.db.models.OqJob` instance
    :param coordinates: a list of (lon, lat) pairs

    If the coordinates are not set, a list of 5 predefined locations is used
    """
    output = models.Output.objects.create_output(
        hazard_job, "Test SES Collection", "ses")
    gmf, ses_coll = create_gmf_sescoll(output)
    ruptures = create_ses_ruptures(hazard_job, ses_coll, 3)
    records = []
    if coordinates is None:
        coordinates = [(15.310, 38.225), (15.71, 37.225),
                       (15.48, 38.091), (15.565, 38.17),
                       (15.481, 38.25)]
    for site_id in models.save_sites(hazard_job, coordinates):
        records.append(models.GmfData.objects.create(
            gmf=gmf,
            task_no=0,
            imt="PGA",
            gmvs=[0.1, 0.2, 0.3],
            rupture_ids=[r.id for r in ruptures],
            site_id=site_id))

    return records
Esempio n. 3
0
def create_gmf_data_records(hazard_job, rlz=None, ses_coll=None, points=None):
    """
    Returns the created records.
    """
    gmf = create_gmf(hazard_job, rlz)
    ses_coll = ses_coll or models.SESCollection.objects.create(
        output=models.Output.objects.create_output(
            hazard_job, "Test SES Collection", "ses"),
        lt_model=gmf.lt_realization.lt_model,
        ordinal=0)
    ruptures = create_ses_ruptures(hazard_job, ses_coll, 3)
    records = []
    if points is None:
        points = [(15.310, 38.225), (15.71, 37.225),
                  (15.48, 38.091), (15.565, 38.17),
                  (15.481, 38.25)]
    for site_id in models.save_sites(hazard_job, points):
        records.append(models.GmfData.objects.create(
            gmf=gmf,
            task_no=0,
            imt="PGA",
            gmvs=[0.1, 0.2, 0.3],
            rupture_ids=[r.id for r in ruptures],
            site_id=site_id))

    return records
Esempio n. 4
0
def create_gmf_from_csv(job, fname, output_type='gmf'):
    """
    Populate the gmf_data table for an event_based (default)
    or scenario calculation (output_type="gmf_scenario").

    :param job: an :class:`openquake.engine.db.models.OqJob` instance
    :param output_type: a string with the output type
    """
    hc = job.get_oqparam()
    if output_type == "gmf":  # event based
        hc.investigation_time = 50
        hc.ses_per_logic_tree_path = 1

    # tricks to fool the oqtask decorator
    job.is_running = True
    job.status = 'post_processing'
    job.save()

    output = models.Output.objects.create_output(
        job, "Test SES Collection", "ses")
    gmf, ses_coll = create_gmf_sescoll(output, output_type=output_type)

    with open(fname, 'rb') as csvfile:
        gmfreader = csv.reader(csvfile, delimiter=',')
        locations = gmfreader.next()

        gmv_matrix = numpy.array(
            [map(float, row) for row in gmfreader]).transpose()

        ruptures = create_ses_ruptures(job, ses_coll, len(gmv_matrix[0]))

        for i, gmvs in enumerate(gmv_matrix):
            point = tuple(map(float, locations[i].split()))
            [site_id] = models.save_sites(job, [point])[1]
            models.GmfData.objects.create(
                gmf=gmf,
                task_no=0,
                imt="PGA", gmvs=gmvs,
                rupture_ids=[r.id for r in ruptures],
                site_id=site_id)

    return gmf
Esempio n. 5
0
def create_gmf_from_csv(job, fname, output_type='gmf'):
    """
    Populate the gmf_data table for an event_based (default)
    or scenario calculation (output_type="gmf_scenario").

    :param job: an :class:`openquake.engine.db.models.OqJob` instance
    :param output_type: a string with the output type
    """
    hc = job.get_oqparam()
    if output_type == "gmf":  # event based
        hc.investigation_time = 50
        hc.ses_per_logic_tree_path = 1

    # tricks to fool the oqtask decorator
    job.is_running = True
    job.status = 'post_processing'
    job.save()

    output = models.Output.objects.create_output(
        job, "Test SES Collection", "ses")
    gmf, ses_coll = create_gmf_sescoll(output, output_type=output_type)

    with open(fname, 'rb') as csvfile:
        gmfreader = csv.reader(csvfile, delimiter=',')
        locations = gmfreader.next()

        gmv_matrix = numpy.array(
            [map(float, row) for row in gmfreader]).transpose()

        ruptures = create_ses_ruptures(job, ses_coll, len(gmv_matrix[0]))

        for i, gmvs in enumerate(gmv_matrix):
            point = tuple(map(float, locations[i].split()))
            [site_id] = models.save_sites(job, [point])
            models.GmfData.objects.create(
                gmf=gmf,
                task_no=0,
                imt="PGA", gmvs=gmvs,
                rupture_ids=[r.id for r in ruptures],
                site_id=site_id)

    return gmf
Esempio n. 6
0
def get_fake_risk_job(risk_cfg, hazard_cfg, output_type="curve",
                      username="******"):
    """
    Takes in input the paths to a risk job config file and a hazard job config
    file.

    Creates fake hazard outputs suitable to be used by a risk
    calculation and then creates a :class:`openquake.engine.db.models.OqJob`
    object for a risk calculation. It also returns the input files
    referenced by the risk config file.

    :param output_type: gmf, gmf_scenario, or curve
    """

    hazard_job = get_job(hazard_cfg, username)
    hc = hazard_job.get_oqparam()

    lt_model = models.LtSourceModel.objects.create(
        hazard_calculation=hazard_job,
        ordinal=1, sm_lt_path="test_sm")

    rlz = models.LtRealization.objects.create(
        lt_model=lt_model, ordinal=1, weight=1,
        gsim_lt_path="test_gsim")

    if output_type == "curve":
        models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve_multi"),
            investigation_time=hc.investigation_time)

        hazard_output = models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve"),
            investigation_time=hc.investigation_time,
            imt="PGA", imls=[0.1, 0.2, 0.3])

        for point in [(-1.01, 1.01), (0.9, 1.01), (0.01, 0.01), (0.9, 0.9)]:
            models.HazardSite.objects.create(
                hazard_calculation=hazard_job, lon=point[0], lat=point[1])
            models.HazardCurveData.objects.create(
                hazard_curve=hazard_output,
                poes=[0.1, 0.2, 0.3],
                location="POINT(%s %s)" % point)

    elif output_type == "gmf_scenario":
        hazard_output = models.Gmf.objects.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test gmf scenario output", "gmf_scenario"))

        models.SESCollection.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test SES Collection", "ses"))
        site_ids = models.save_sites(
            hazard_job,
            [(15.48, 38.0900001), (15.565, 38.17), (15.481, 38.25)])[1]
        for site_id in site_ids:
            models.GmfData.objects.create(
                gmf=hazard_output,
                task_no=0,
                imt="PGA",
                site_id=site_id,
                gmvs=[0.1, 0.2, 0.3],
                rupture_ids=[0, 1, 2])

    elif output_type in ("ses", "gmf"):
        hazard_output = create_gmf_data_records(hazard_job)[0].gmf

    else:
        raise RuntimeError('Unexpected output_type: %s' % output_type)

    hazard_job.status = "complete"
    hazard_job.save()
    risk_job = get_job(risk_cfg, username,
                       hazard_output_id=hazard_output.output.id)
    return risk_job, set(risk_job.get_param('inputs'))
Esempio n. 7
0
def get_fake_risk_job(risk_cfg, hazard_cfg, output_type="curve",
                      username="******"):
    """
    Takes in input the paths to a risk job config file and a hazard job config
    file.

    Creates fake hazard outputs suitable to be used by a risk
    calculation and then creates a :class:`openquake.engine.db.models.OqJob`
    object for a risk calculation. It also returns the input files
    referenced by the risk config file.

    :param output_type: gmf, gmf_scenario, or curve
    """

    hazard_job = get_job(hazard_cfg, username)
    hc = hazard_job.get_oqparam()

    lt_model = models.LtSourceModel.objects.create(
        hazard_calculation=hazard_job,
        ordinal=1, sm_lt_path="test_sm")

    rlz = models.LtRealization.objects.create(
        lt_model=lt_model, ordinal=1, weight=1,
        gsim_lt_path="test_gsim")

    if output_type == "curve":
        models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve_multi"),
            investigation_time=hc.investigation_time)

        hazard_output = models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve"),
            investigation_time=hc.investigation_time,
            imt="PGA", imls=[0.1, 0.2, 0.3])

        for point in ["POINT(-1.01 1.01)", "POINT(0.9 1.01)",
                      "POINT(0.01 0.01)", "POINT(0.9 0.9)"]:
            models.HazardSite.objects.create(
                hazard_calculation=hazard_job, location=point)
            models.HazardCurveData.objects.create(
                hazard_curve=hazard_output,
                poes=[0.1, 0.2, 0.3],
                location="%s" % point)

    elif output_type == "gmf_scenario":
        hazard_output = models.Gmf.objects.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test gmf scenario output", "gmf_scenario"))

        models.SESCollection.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test SES Collection", "ses"))
        site_ids = models.save_sites(
            hazard_job,
            [(15.48, 38.0900001), (15.565, 38.17), (15.481, 38.25)])
        for site_id in site_ids:
            models.GmfData.objects.create(
                gmf=hazard_output,
                task_no=0,
                imt="PGA",
                site_id=site_id,
                gmvs=[0.1, 0.2, 0.3],
                rupture_ids=[0, 1, 2])

    elif output_type in ("ses", "gmf"):
        hazard_output = create_gmf_data_records(hazard_job)[0].gmf

    else:
        raise RuntimeError('Unexpected output_type: %s' % output_type)

    hazard_job.status = "complete"
    hazard_job.save()
    risk_job = get_job(risk_cfg, username,
                       hazard_output_id=hazard_output.output.id)
    return risk_job, set(risk_job.get_param('inputs'))
Esempio n. 8
0
def get_fake_risk_job(risk_cfg, hazard_cfg, output_type="curve",
                      username="******"):
    """
    Takes in input the paths to a risk job config file and a hazard job config
    file.

    Creates fake hazard outputs suitable to be used by a risk
    calculation and then creates a :class:`openquake.engine.db.models.OqJob`
    object for a risk calculation. It also returns the input files
    referenced by the risk config file.

    :param output_type: gmf, gmf_scenario, or curve
    """

    hazard_job = get_job(hazard_cfg, username)
    hc = hazard_job.get_oqparam()

    lt_model = models.LtSourceModel.objects.create(
        hazard_calculation=hazard_job,
        ordinal=1, sm_lt_path="test_sm")

    rlz = models.LtRealization.objects.create(
        lt_model=lt_model, ordinal=1, weight=1,
        gsim_lt_path="test_gsim")

    if output_type == "curve":
        models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve_multi"),
            investigation_time=hc.investigation_time)

        hazard_output = models.HazardCurve.objects.create(
            lt_realization=rlz,
            output=models.Output.objects.create_output(
                hazard_job, "Test Hazard output", "hazard_curve"),
            investigation_time=hc.investigation_time,
            imt="PGA", imls=[0.1, 0.2, 0.3])

        for point in ["POINT(-1.01 1.01)", "POINT(0.9 1.01)",
                      "POINT(0.01 0.01)", "POINT(0.9 0.9)"]:
            models.HazardSite.objects.create(
                hazard_calculation=hazard_job, location=point)
            models.HazardCurveData.objects.create(
                hazard_curve=hazard_output,
                poes=[0.1, 0.2, 0.3],
                location="%s" % point)

    elif output_type == "gmf_scenario":
        hazard_output = models.Gmf.objects.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test gmf scenario output", "gmf_scenario"))

        models.SESCollection.objects.create(
            output=models.Output.objects.create_output(
                hazard_job, "Test SES Collection", "ses"),
            lt_model=None, ordinal=0)
        site_ids = models.save_sites(
            hazard_job,
            [(15.48, 38.0900001), (15.565, 38.17), (15.481, 38.25)])
        for site_id in site_ids:
            models.GmfData.objects.create(
                gmf=hazard_output,
                task_no=0,
                imt="PGA",
                site_id=site_id,
                gmvs=[0.1, 0.2, 0.3],
                rupture_ids=[0, 1, 2])

    elif output_type in ("ses", "gmf"):
        hazard_output = create_gmf_data_records(hazard_job, rlz)[0].gmf

    else:
        raise RuntimeError('Unexpected output_type: %s' % output_type)

    hazard_job.status = "complete"
    hazard_job.save()
    job = engine.prepare_job(username)
    params = vars(
        readini.parse_config(
            open(risk_cfg), hazard_output_id=hazard_output.output.id))
    params['hazard_calculation_id'] = hazard_job.id

    # we are removing intensity_measure_types_and_levels because it is not
    # a field of RiskCalculation; this ugliness will disappear when
    # RiskCalculation will be removed
    del params['intensity_measure_types_and_levels']
    job.save_params(params)
    risk_calc = engine.create_calculation(models.RiskCalculation, params)
    job.risk_calculation = risk_calc
    job.save()

    # reload risk calculation to have all the types converted properly
    job.risk_calculation = models.RiskCalculation.objects.get(id=risk_calc.id)

    return job, set(params['inputs'])