Esempio n. 1
0
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
bool AmpTool::run() {
	if ( !_originID.empty() ) {
		OriginPtr org = Origin::Cast(query()->getObject(Origin::TypeInfo(), _originID));
		if ( !org ) {
			cerr << "Origin not found!" << endl;
			return false;
		}

		_fetchMissingAmplitudes = false;
		query()->loadArrivals(org.get());
		process(org.get());
		return true;
	}

	if ( !_strTimeWindowStartTime.empty() || !_strTimeWindowEndTime.empty() ) {
		if ( database() == NULL ) {
			cerr << "No database currently active for time window reprocessing" << endl;
			return false;
		}

		Core::Time startTime, endTime;

		if ( !_strTimeWindowStartTime.empty()
		  && !startTime.fromString(_strTimeWindowStartTime.c_str(), "%F %T") ) {
			cerr << "Invalid start time: " << _strTimeWindowStartTime << endl;
			return false;
		}

		if ( !_strTimeWindowEndTime.empty()
		  && !endTime.fromString(_strTimeWindowEndTime.c_str(), "%F %T") ) {
			cerr << "Invalid end time: " << _strTimeWindowEndTime << endl;
			return false;
		}

		std::string dbQuery;
		dbQuery += "select PPick." + _T("publicID") + ", Pick.* from Pick,PublicObject as PPick,Amplitude "
		           "where Pick._oid=PPick._oid and Amplitude." + _T("pickID") + "=PPick." + _T("publicID");

		if ( startTime.valid() )
			 dbQuery += " and Pick." + _T("time_value") + ">='" + startTime.toString("%F %T") + "'";

		if ( endTime.valid() )
			 dbQuery += " and Pick." + _T("time_value") + "<'" + endTime.toString("%F %T") + "'";

		dbQuery += " group by Amplitude." + _T("pickID");

		if ( !commandline().hasOption("commit") )
			_testMode = true;

		EventParametersPtr ep;
		if ( _testMode )
			ep = new EventParameters;

		typedef list<PickPtr> PickList;
		PickList picks;

		cerr << "Collecting picks ... " << flush;
		DatabaseIterator db_it = query()->getObjectIterator(dbQuery, Pick::TypeInfo());
		ObjectPtr obj;
		while ( obj = db_it.get() ) {
			Pick *pick = static_cast<Pick*>(obj.get());
			try {
				pick->waveformID().networkCode();
				pick->waveformID().stationCode();
				pick->waveformID().locationCode();
				pick->waveformID().channelCode();

				pick->time().value();
			}
			catch ( ... ) {
				continue;
			}

			++db_it;

			picks.push_back(pick);
			if ( ep ) ep->add(pick);
		}

		db_it.close();

		cerr << picks.size() << endl;

		_report << std::endl;
		_report << "Reprocessing report" << std::endl;
		_report << "-------------------" << std::endl;

		_report << " + Picks" << std::endl;

		int errors = 0;
		int ampsRecomputed = 0;
		int messagesSent = 0;

		int idx = 1;
		for ( PickList::iterator it = picks.begin(); it != picks.end(); ++it, ++idx ) {
			PickPtr pick = *it;
			SingleAmplitudeMap dbAmps;

			if ( isExitRequested() ) break;

			// Clear all processors
			_processors.clear();

			// Clear all station time windows
			_stationRequests.clear();

			_report << "   + " << pick->publicID() << std::endl;
			cerr << "[" << idx << "]" << " " << (*it)->publicID() << endl;
			db_it = query()->getAmplitudesForPick((*it)->publicID());
			while ( obj = db_it.get() ) {
				Amplitude *amp = static_cast<Amplitude*>(obj.get());
				cerr << "  [" << setw(10) << left << amp->type() << "]  ";

				AmplitudeProcessorPtr proc = AmplitudeProcessorFactory::Create(amp->type().c_str());
				if ( !proc ) {
					if ( _amplitudeTypes.find(amp->type()) == _amplitudeTypes.end() )
						cerr << "No processor";
					else {
						cerr << "No processor but enabled";
						++errors;
					}
				}
				else {
					cerr << "Fetch data";
					dbAmps[amp->type()] = amp;
					proc->setTrigger(pick->time().value());
					proc->setReferencingPickID(pick->publicID());
					proc->setPublishFunction(boost::bind(&AmpTool::storeLocalAmplitude, this, _1, _2));
					_report << "     + Data" << std::endl;
					addProcessor(proc.get(), pick.get(), None, None);
				}

				cerr << endl;
				++db_it;
			}

			db_it.close();

			cerr << "  --------------------------------" << endl;

			if ( _stationRequests.empty() ) continue;

			for ( RequestMap::iterator it = _stationRequests.begin(); it != _stationRequests.end(); ++it ) {
				StationRequest &req = it->second;
				for ( WaveformIDSet::iterator wit = req.streams.begin(); wit != req.streams.end(); ++wit ) {
					const WaveformStreamID &wsid = *wit;
					recordStream()->addStream(wsid.networkCode(), wsid.stationCode(),
					                          wsid.locationCode(), wsid.channelCode(),
					                          req.timeWindow.startTime(),
					                          req.timeWindow.endTime());
				}

				_report << " + TimeWindow (" << it->first << "): " << req.timeWindow.startTime().toString("%F %T")
				        << ", " << req.timeWindow.endTime().toString("%F %T") << std::endl;
			}

			_reprocessMap.clear();
			readRecords(false);

			list<AmplitudePtr> updates;

			for ( AmplitudeMap::iterator it = dbAmps.begin();
			      it != dbAmps.end(); ++it ) {
				AmplitudePtr oldAmp = it->second;
				AmplitudePtr newAmp = _reprocessMap[oldAmp->type()];

				cerr << "  [" << setw(10) << left << oldAmp->type() << "]  " << oldAmp->amplitude().value() << "  ";
				if ( newAmp ) {
					if ( newAmp->amplitude().value() != oldAmp->amplitude().value() ) {
						*oldAmp = *newAmp;
						if ( ep )
							ep->add(oldAmp.get());
						else
							updates.push_back(oldAmp);
						cerr << "->  " << newAmp->amplitude().value();
					}
					else
						cerr << "  no changes";

					++ampsRecomputed;
				}
				else {
					cerr << "-";
					++errors;
				}
				cerr << endl;
			}

			if ( !updates.empty() ) {
				if ( !_testMode ) {
					NotifierMessagePtr nmsg = new NotifierMessage;
					for ( list<AmplitudePtr>::iterator it = updates.begin();
					      it != updates.end(); ++it ) {
						nmsg->attach(new Notifier("EventParameters", OP_UPDATE, it->get()));
					}

					connection()->send(nmsg.get());
					++messagesSent;

					if ( messagesSent % 100 == 0 )
						sync();
				}
				else {
					cerr << "  --------------------------------" << endl;
					cerr << "  Test mode, nothing sent" << endl;
				}
			}
		}

		if ( ep ) {
			IO::XMLArchive ar;
			ar.create("-");
			ar.setFormattedOutput(true);
			ar << ep;
			ar.close();
		}

		cerr << "----------------------------------" << endl;
		cerr << "Recomputed " << ampsRecomputed << " amplitudes" << endl;
		cerr << "Sent " << messagesSent << " messages" << endl;
		if ( errors )
			cerr << errors << " errors occurred, check the processing log" << endl;

		return true;
	}


	if ( !_epFile.empty() ) {
		_fetchMissingAmplitudes = false;

		// Disable database
		setDatabase(NULL);
		_cache.setDatabaseArchive(NULL);

		IO::XMLArchive ar;
		if ( !ar.open(_epFile.c_str()) ) {
			SEISCOMP_ERROR("Failed to open %s", _epFile.c_str());
			return false;
		}

		ar >> _ep;
		ar.close();

		if ( !_ep ) {
			SEISCOMP_ERROR("No event parameters found in %s", _epFile.c_str());
			return false;
		}

		if ( commandline().hasOption("reprocess") ) {
			for ( size_t i = 0; i < _ep->amplitudeCount(); ++i ) {
				AmplitudePtr amp = _ep->amplitude(i);
				feed(amp.get());
			}
		}

		for ( size_t i = 0; i < _ep->originCount(); ++i ) {
			OriginPtr org = _ep->origin(i);
			SEISCOMP_INFO("Processing origin %s", org->publicID().c_str());
			process(org.get());
		}

		ar.create("-");
		ar.setFormattedOutput(true);
		ar << _ep;
		ar.close();

		_ep = NULL;

		return true;
	}
Esempio n. 2
0
void CalculateAmplitudes::addProcessor(
    const string &type,
    const DataModel::Pick *pick,
    double dist) {

    AmplitudeProcessorPtr proc = NULL;

    int row;

    proc = AmplitudeProcessorFactory::Create(type.c_str());
    if ( !proc ) {
        row = addProcessingRow(waveformIDToStdString(pick->waveformID()), type);
        setError(row, "no processor");
        return;
    }

    proc->setTrigger(pick->time().value());

    Util::KeyValues *keys = NULL;
    std::string stationID = pick->waveformID().networkCode() + "." +
                            pick->waveformID().stationCode();
    ParameterMap::iterator it = _parameters.find(stationID);
    if ( it != _parameters.end() )
        keys = it->second.get();
    else if ( SCApp->configModule() != NULL ) {
        for ( size_t i = 0; i < SCApp->configModule()->configStationCount(); ++i ) {
            ConfigStation *station = SCApp->configModule()->configStation(i);

            if ( station->networkCode() != pick->waveformID().networkCode() ) continue;
            if ( station->stationCode() != pick->waveformID().stationCode() ) continue;

            Setup *setup = findSetup(station, SCApp->name());
            if ( setup ) {
                ParameterSet *ps = ParameterSet::Find(setup->parameterSetID());
                if ( !ps ) continue;

                Util::KeyValuesPtr keys_ = new Util::KeyValues;
                keys_->init(ps);
                _parameters[stationID] = keys_;
                keys = keys_.get();
            }
        }
    }

    proc->setReferencingPickID(pick->publicID());
    proc->setPublishFunction(boost::bind(&CalculateAmplitudes::emitAmplitude, this, _1, _2));

    switch ( proc->usedComponent() ) {
    case AmplitudeProcessor::Vertical:
        addProcessor(proc.get(), pick, WaveformProcessor::VerticalComponent);
        break;
    case AmplitudeProcessor::FirstHorizontal:
        addProcessor(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        break;
    case AmplitudeProcessor::SecondHorizontal:
        addProcessor(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    case AmplitudeProcessor::Horizontal:
        addProcessor(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        addProcessor(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    case AmplitudeProcessor::Any:
        addProcessor(proc.get(), pick, WaveformProcessor::VerticalComponent);
        addProcessor(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        addProcessor(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    default:
        row = addProcessingRow(waveformIDToStdString(pick->waveformID()), type);
        setError(row, "unsupported processor component");
        return;
    }

    // If initialization fails, abort
    if ( !proc->setup(
                Settings(
                    SCApp->configModuleName(),
                    pick->waveformID().networkCode(), pick->waveformID().stationCode(),
                    pick->waveformID().locationCode(), pick->waveformID().channelCode().substr(0,2),
                    &SCCoreApp->configuration(), keys)) ) {
        return;
    }

    // Set depth hint
    try {
        proc->setHint(WaveformProcessor::Depth, _origin->depth());
        if ( proc->isFinished() ) return;
    }
    catch ( ... ) {}

    if ( dist >= 0 )
        proc->setHint(WaveformProcessor::Distance, dist);

    if ( proc->isFinished() ) {
        pair<TableRowMap::iterator, TableRowMap::iterator> itp = _rows.equal_range(proc.get());
        for ( TableRowMap::iterator row_it = itp.first; row_it != itp.second; ++row_it )
            setError(row_it->second, QString("%1 (%2)").arg(proc->status().toString()).arg(proc->statusValue(), 0, 'f', 2));
        return;
    }

    proc->computeTimeWindow();

    switch ( proc->usedComponent() ) {
    case AmplitudeProcessor::Vertical:
        subscribeData(proc.get(), pick, WaveformProcessor::VerticalComponent);
        break;
    case AmplitudeProcessor::FirstHorizontal:
        subscribeData(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        break;
    case AmplitudeProcessor::SecondHorizontal:
        subscribeData(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    case AmplitudeProcessor::Horizontal:
        subscribeData(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        subscribeData(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    case AmplitudeProcessor::Any:
        subscribeData(proc.get(), pick, WaveformProcessor::VerticalComponent);
        subscribeData(proc.get(), pick, WaveformProcessor::FirstHorizontalComponent);
        subscribeData(proc.get(), pick, WaveformProcessor::SecondHorizontalComponent);
        break;
    default:
        return;
    }
}