def load_fixtures(session):
    # Allows to insert test entries in the earthquake db.
    csv_filename = in_data_dir('query_catalogue.csv')
    with open(csv_filename) as eq_source:
        reader = CsvEqCatalogueReader(eq_source)
        entries = [entry for entry in reader.read(Converter())]

    event_source = 'query_catalogue'

    for entry in entries:
        agency = entry['solutionAgency']

        entry_time = datetime(entry['year'], entry['month'], entry['day'],
                              entry['hour'], entry['minute'],
                              int(entry['second']))
        entry_pos = 'POINT(%f %f)' % (entry['Longitude'], entry['Latitude'])
        origin = dict(
            time=entry_time, position=WKTSpatialElement(entry_pos),
            origin_key=entry['eventKey'], depth=entry['depth'])

        mag_measure = models.MagnitudeMeasure(
            agency=agency,
            scale=entry['mag_type'],
            value=entry['magnitude'], standard_error=0.2,
            event_key=entry['eventKey'],
            event_source=event_source, **origin)

        session.add(mag_measure)
def load_fixtures(session):
    # Allows to insert test entries in the earthquake db.
    csv_filename = in_data_dir('query_catalogue.csv')
    with open(csv_filename) as eq_source:
        reader = CsvEqCatalogueReader(eq_source)
        entries = [entry for entry in reader.read(Converter())]

    event_source = models.EventSource(name='query_catalogue')
    session.add(event_source)
    for entry in entries:
        inserted_agency = session.query(models.Agency).filter(
                models.Agency.source_key == entry['solutionAgency'])
        if not inserted_agency.count():
            agency = models.Agency(source_key=entry['solutionAgency'],
                    eventsource=event_source)
            session.add(agency)
        else:
            agency = inserted_agency.all()[0]

        inserted_event = session.query(
                models.Event).filter_by(
                source_key=entry['eventKey'])
        if not inserted_event.count():
            event = models.Event(source_key=entry['eventKey'],
                eventsource=event_source)
            session.add(event)
        else:
            event = inserted_event.all()[0]

        entry_time = datetime(entry['year'], entry['month'], entry['day'],
                                entry['hour'], entry['minute'],
                                int(entry['second']))
        entry_pos = 'POINT(%f %f)' % (entry['Longitude'], entry['Latitude'])
        origin = models.Origin(
            time=entry_time, position=WKTSpatialElement(entry_pos),
            depth=entry['depth'], eventsource=event_source,
            source_key=entry['eventKey'])

        mag_measure = models.MagnitudeMeasure(agency=agency, event=event,
                origin=origin, scale=entry['mag_type'],
            value=entry['magnitude'], standard_error=0.2)

        measure_meta = models.MeasureMetadata(
                metadata_type='stations', value=entry['stations'],
                magnitudemeasure=mag_measure)

        session.add(origin)
        session.add(mag_measure)
        session.add(measure_meta)