Пример #1
0
def create_iteration_xml_string(iteration_name, solver_name, events,
                                min_period, max_period):
    """
    Creates a new iteration string.

    Returns a string containing the XML representation.

    :param iteration_name: The iteration name. Should start with a number.
    :param solver_name: The name of the solver to be used for this iteration.
    :param events: A dictionary. The key is the event name, and the value a
        list of station ids for this event.
    :param min_period: The minimum period for the iteration.
    :param max_period: The maximum period for the iteration.
    """
    solver_doc = _get_default_solver_settings(solver_name, min_period,
                                              max_period)
    if solver_name.lower() == "ses3d_4_1":
        solver_name = "SES3D 4.1"
    elif solver_name.lower() == "ses3d_2_0":
        solver_name = "SES3D 2.0"
    elif solver_name.lower() == "specfem3d_cartesian":
        solver_name = "SPECFEM3D CARTESIAN"
    else:
        raise NotImplementedError

    if min_period >= max_period:
        msg = "min_period needs to be smaller than max_period."
        raise ValueError(msg)

    # Loop over all events.
    events_doc = []
    # Also over all stations.
    for event_name, stations in events.iteritems():
        stations_doc = [E.station(
            E.station_id(station),
            E.station_weight("1.0"),
            E.time_correction_in_s("0.0")) for station in stations]
        events_doc.append(E.event(
            E.event_name(event_name),
            E.event_weight("1.0"),
            E.time_correction_in_s("0.0"),
            *stations_doc))

    doc = E.iteration(
        E.iteration_name(iteration_name),
        E.iteration_description(""),
        E.comment(""),
        E.data_preprocessing(
            E.highpass_period(str(max_period)),
            E.lowpass_period(str(min_period))),
        E.rejection_criteria(
            E.minimum_trace_length_in_s("500.0"),
            E.signal_to_noise(
                E.test_interval_from_origin_in_s("100.0"),
                E.max_amplitude_ratio("100.0"))),
        E.source_time_function("Filtered Heaviside"),
        E.solver_parameters(
            E.solver(solver_name),
            solver_doc),
        *events_doc)

    string_doc = etree.tostring(doc, pretty_print=True,
                                xml_declaration=True, encoding="UTF-8")
    return string_doc
Пример #2
0
    def write(self, filename):
        """
        Serialized the Iteration structure once again.

        :param filename: The path that will be written to.
        """
        solver_settings = _recursive_etree(
            self.solver_settings["solver_settings"])

        contents = []
        contents.extend([
            E.iteration_name(self.iteration_name),
            E.iteration_description(self.description),
        ])
        contents.extend([E.comment(_i) for _i in self.comments])
        contents.extend([
            E.data_preprocessing(
                E.highpass_period(
                    str(self.data_preprocessing["highpass_period"])),
                E.lowpass_period(
                    str(self.data_preprocessing["lowpass_period"]))
            ),
            E.rejection_criteria(
                E.minimum_trace_length_in_s(str(
                    self.rejection_criteria["minimum_trace_length_in_s"]
                )),
                E.signal_to_noise(
                    E.test_interval_from_origin_in_s(str(
                        self.rejection_criteria["signal_to_noise"][
                            "test_interval_from_origin_in_s"]
                    )),
                    E.max_amplitude_ratio(str(
                        self.rejection_criteria["signal_to_noise"][
                            "max_amplitude_ratio"]
                    ))
                )
            ),
            E.source_time_function(self.source_time_function),
            E.solver_parameters(
                E.solver(self.solver_settings["solver"]),
                E.solver_settings(*solver_settings)
            ),
        ])

        # Add all events.
        for key, value in self.events.iteritems():
            event = E.event(
                E.event_name(key),
                E.event_weight(str(value["event_weight"])),
                E.time_correction_in_s(str(value["time_correction_in_s"])),
                *[E.comment(_i) for _i in value["comments"] if _i]
            )
            for station_id, station_value in value["stations"].iteritems():
                event.append(E.station(
                    E.station_id(station_id),
                    E.station_weight(str(station_value["station_weight"])),
                    E.time_correction_in_s(str(
                        station_value["time_correction_in_s"])),
                    *[E.comment(_i)
                      for _i in station_value["comments"] if _i]
                ))
            contents.append(event)

        doc = E.iteration(*contents)
        doc.getroottree().write(filename, xml_declaration=True,
                                encoding="UTF-8")