sascha@3412: package de.intevation.flys.artifacts.states.fixation;
sascha@3412:
raimund@3610: import java.text.DateFormat;
raimund@3610: import java.util.Date;
raimund@3610: import java.util.List;
raimund@3610:
raimund@3610: import org.apache.log4j.Logger;
raimund@3610:
sascha@3412: import de.intevation.artifactdatabase.state.Facet;
ingo@3785: import de.intevation.artifactdatabase.state.FacetActivity;
ingo@3785: import de.intevation.artifacts.Artifact;
sascha@3412: import de.intevation.artifacts.CallContext;
sascha@3412: import de.intevation.flys.artifacts.FLYSArtifact;
sascha@3412: import de.intevation.flys.artifacts.access.FixAnalysisAccess;
sascha@3412: import de.intevation.flys.artifacts.model.CalculationResult;
sascha@3412: import de.intevation.flys.artifacts.model.DataFacet;
sascha@3412: import de.intevation.flys.artifacts.model.DateRange;
sascha@3412: import de.intevation.flys.artifacts.model.FacetTypes;
sascha@3412: import de.intevation.flys.artifacts.model.ReportFacet;
raimund@3610: import de.intevation.flys.artifacts.model.fixings.FixAnalysisCalculation;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixAnalysisEventsFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixAnalysisPeriodsFacet;
raimund@3610: import de.intevation.flys.artifacts.model.fixings.FixAnalysisResult;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixAvSectorFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixDerivateFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixDeviationFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixLongitudinalAnalysisFacet;
raimund@3610: import de.intevation.flys.artifacts.model.fixings.FixLongitudinalAvSectorFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixLongitudinalDeviationFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixLongitudinalReferenceFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixOutlierFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixReferenceEventsFacet;
sascha@3412: import de.intevation.flys.artifacts.model.fixings.FixWQCurveFacet;
sascha@3412: import de.intevation.flys.artifacts.resources.Resources;
sascha@3412: import de.intevation.flys.artifacts.states.DefaultState;
sascha@3412: import de.intevation.flys.utils.IdGenerator;
sascha@3412:
sascha@3412: /**
sascha@3412: * @author Raimund Renkert
sascha@3412: */
sascha@3412: public class FixAnalysisCompute
sascha@3412: extends DefaultState
sascha@3412: implements FacetTypes
sascha@3412: {
sascha@3412: /** The log used in this class. */
sascha@3412: private static Logger log = Logger.getLogger(FixAnalysisCompute.class);
sascha@3412:
christian@3907: private static final String I18N_REFERENCEPERIOD_SHORT = "fix.reference.period.event.short";
sascha@3412:
sascha@3412: private static final String I18N_ANALYSISPERIODS = "fix.analysis.periods";
sascha@3412:
sascha@3412: private static final String I18N_DERIVATIVE = "fix.derivative";
sascha@3412:
sascha@3412: private static final String I18N_OUTLIER = "fix.outlier";
sascha@3412:
raimund@3610: private static final String I18N_ANALYSIS = "fix.analysis.short";
sascha@3412:
sascha@3412: private static final String I18N_DEVIATION = "fix.deviation";
sascha@3412:
sascha@3412: private static final String I18N_REFERENCEDEVIATION = "fix.reference.deviation";
sascha@3412:
christian@3907: private static final String I18N_REFERENCEPERIOD = "state.fix.analysis.referenceperiod";
christian@3907:
sascha@3412: public static final String [] SECTOR_LABELS = {
sascha@3412: "[0 - (MNQ+MQ)/2)",
sascha@3412: "[(MNQ+MQ)/2 - (MQ+MHQ)/2)",
sascha@3412: "[(MQ+MHQ)/2 - HQ5)",
sascha@3412: "[HQ5 - \u221e)"
sascha@3412: };
sascha@3412:
felix@4166: // TODO Why does this happen here? In other cases its implemented in the
felix@4166: // respective artifact, not State.
ingo@3785: static {
ingo@3785: // Active/deactivate facets.
ingo@3785: FacetActivity.Registry.getInstance().register(
ingo@3785: "fixanalysis",
ingo@3785: new FacetActivity() {
ingo@3785: @Override
ingo@3785: public Boolean isInitialActive(
ingo@3785: Artifact artifact,
ingo@3785: Facet facet,
ingo@3785: String output
ingo@3785: ) {
felix@4432: log.debug("Registry:is active? " + output + "/" + facet.getName());
felix@4166: if (output.contains(FacetTypes.ChartType.FLSC.toString())) {
felix@4166: // Longitudinal section chart
christian@3913: String name = facet.getName();
ingo@3785:
christian@3913: if (name.contains(FacetTypes.FIX_ANALYSIS_EVENTS_DWT)
christian@3913: || name.contains(FacetTypes.FIX_ANALYSIS_EVENTS_LS)
christian@3913: || name.contains(FacetTypes.FIX_ANALYSIS_EVENTS_WQ)
christian@3913: || name.contains(FacetTypes.FIX_REFERENCE_EVENTS_DWT)
christian@3913: || name.contains(FacetTypes.FIX_REFERENCE_EVENTS_LS)
christian@3913: || name.contains(FacetTypes.FIX_REFERENCE_EVENTS_WQ)
felix@4432: // Wrong palace! Go to AnnotationArtifact.
felix@4432: || name.contains(FacetTypes.LONGITUDINAL_ANNOTATION)
christian@3913: ) {
christian@3913: return Boolean.FALSE;
christian@3913: }
ingo@3785: }
felix@4432: else if (output.contains(FacetTypes.ChartType.FDWC.toString())
felix@4166: && facet.getName().contains(FacetTypes.FIX_SECTOR_AVERAGE_DWT)) {
felix@4166: return Boolean.FALSE;
felix@4166: }
felix@4432: else if (output.contains(FacetTypes.ChartType.LS.toString())
felix@4432: && facet.getName().contains(FacetTypes.LONGITUDINAL_ANNOTATION)) {
felix@4432:
felix@4432: return Boolean.FALSE;
felix@4432: }
ingo@3785:
ingo@3785: return Boolean.TRUE;
ingo@3785: }
ingo@3785: });
ingo@3785: }
ingo@3785:
sascha@3412: /**
sascha@3412: * The default constructor that initializes an empty State object.
sascha@3412: */
sascha@3412: public FixAnalysisCompute() {
sascha@3412: }
sascha@3412:
sascha@3412: @Override
sascha@3412: public Object computeAdvance(
sascha@3412: FLYSArtifact artifact,
sascha@3412: String hash,
sascha@3412: CallContext context,
sascha@3412: List facets,
sascha@3412: Object old
sascha@3412: ) {
sascha@3412: log.debug("FixAnalysisCompute.computeAdvance");
sascha@3412:
sascha@3412: CalculationResult res;
sascha@3412:
sascha@3412: FixAnalysisAccess access =
sascha@3412: new FixAnalysisAccess(artifact);
sascha@3412:
sascha@3412: if (old instanceof CalculationResult) {
sascha@3412: res = (CalculationResult)old;
sascha@3412: }
sascha@3412: else {
sascha@3412: FixAnalysisCalculation calc = new FixAnalysisCalculation(access);
sascha@3412: res = calc.calculate();
sascha@3412: }
sascha@3412:
sascha@3412: if (facets == null) {
sascha@3412: return res;
sascha@3412: }
sascha@3412:
sascha@3412: if (res.getReport().hasProblems()) {
sascha@3412: facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
sascha@3412: }
sascha@3412:
sascha@3415: FixAnalysisResult fr = (FixAnalysisResult)res.getData();
sascha@3412: if (fr == null) {
sascha@3412: return res;
sascha@3412: }
sascha@3412:
sascha@3412: facets.add(
sascha@3412: new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
sascha@3412: facets.add(
sascha@3412: new DataFacet(
sascha@3412: FIX_PARAMETERS, "parameters", ComputeType.ADVANCE, hash, id));
raimund@3599: facets.add(
raimund@3599: new DataFacet(AT, "AT data", ComputeType.ADVANCE, hash, id));
sascha@3412:
sascha@3412: int maxId = -100;
sascha@3412:
sascha@3412: int sectorMask = fr.getUsedSectorsInAnalysisPeriods();
sascha@3412:
sascha@3412: int qsS = access.getQSectorStart();
sascha@3412: int qsE = access.getQSectorEnd();
sascha@3412:
sascha@3412: // TODO: i18n
sascha@3412: DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
sascha@3412:
sascha@3412: DateRange [] periods = access.getAnalysisPeriods();
sascha@3412:
sascha@3412: for (int i = 0; i < periods.length; i++) {
sascha@3412: DateRange period = periods[i];
sascha@3412: String startDate = df.format(period.getFrom());
sascha@3412: String endDate = df.format(period.getTo());
sascha@3412:
sascha@3412: for (int j = qsS; j <= qsE; j++) {
sascha@3412:
sascha@3412: // Only emit facets for sectors that really have data.
sascha@3412: if ((sectorMask & (1 << j)) == 0) {
sascha@3412: continue;
sascha@3412: }
sascha@3412:
sascha@3412: String sector = SECTOR_LABELS[j];
sascha@3412: String description = sector + ": " +
sascha@3412: startDate + " - " +
sascha@3412: endDate;
sascha@3412:
sascha@3412: int sectorNdx = j - qsS;
sascha@3412: int facetNdx = i << 2;
sascha@3412: facetNdx = facetNdx | j;
sascha@3412:
sascha@3412: if (facetNdx > maxId) {
sascha@3412: maxId = facetNdx;
sascha@3412: }
sascha@3412:
sascha@3412: facets.add(
sascha@3412: new FixAvSectorFacet(
sascha@3412: facetNdx,
sascha@3412: FIX_SECTOR_AVERAGE_DWT + "_" + sectorNdx,
sascha@3412: description));
sascha@3412: facets.add(
raimund@3610: new FixLongitudinalAvSectorFacet(
sascha@3412: facetNdx,
sascha@3412: FIX_SECTOR_AVERAGE_LS + "_" + sectorNdx,
sascha@3412: description));
sascha@3412: // TODO: i18n
sascha@3412: String dev = "Abweichung: " + description;
sascha@3412: facets.add(
raimund@3610: new FixLongitudinalAvSectorFacet(
sascha@3412: facetNdx,
sascha@3412: FIX_SECTOR_AVERAGE_LS_DEVIATION + "_" + sectorNdx,
sascha@3412: dev));
sascha@3412: facets.add(
sascha@3412: new FixAvSectorFacet(
sascha@3412: facetNdx,
sascha@3412: FIX_SECTOR_AVERAGE_WQ + "_" + sectorNdx,
sascha@3412: description));
sascha@3412:
sascha@3412: }
sascha@3412:
sascha@3412: String eventDesc =
sascha@3412: Resources.getMsg(context.getMeta(),
sascha@3412: I18N_ANALYSIS,
raimund@3610: I18N_ANALYSIS);
raimund@3610:
raimund@3610: int k = 0;
raimund@3610: for (Date d: fr.getAnalysisEventsDates(i)) {
raimund@3610: int anaNdx = i << 8;
raimund@3610: anaNdx = anaNdx | k;
raimund@3610: facets.add(new FixAnalysisEventsFacet(anaNdx,
raimund@3610: FIX_ANALYSIS_EVENTS_DWT,
raimund@3610: eventDesc + (i+1) + " - " + df.format(d)));
raimund@3610: facets.add(new FixLongitudinalAnalysisFacet(anaNdx,
raimund@3610: FIX_ANALYSIS_EVENTS_LS,
raimund@3610: eventDesc + (i+1) + " - " + df.format(d)));
raimund@3610: facets.add(new FixAnalysisEventsFacet(anaNdx,
raimund@3610: FIX_ANALYSIS_EVENTS_WQ,
raimund@3610: eventDesc + (i+1) +" - " + df.format(d)));
raimund@3610: k++;
raimund@3610: }
sascha@3412: }
sascha@3412:
sascha@3412: IdGenerator idg = new IdGenerator(maxId + 1);
sascha@3412:
sascha@3412: String i18n_ref = Resources.getMsg(context.getMeta(),
christian@3907: I18N_REFERENCEPERIOD_SHORT,
christian@3907: I18N_REFERENCEPERIOD_SHORT);
sascha@3412: String i18n_dev = Resources.getMsg(context.getMeta(),
sascha@3412: I18N_REFERENCEDEVIATION,
sascha@3412: I18N_REFERENCEDEVIATION);
sascha@3412:
raimund@3610: int i = 0;
sascha@3730: for (Date d: fr.getReferenceEventsDates()) {
raimund@3610: int refNdx = idg.next() << 8;
raimund@3610: refNdx |= i;
raimund@3610: facets.add(new FixReferenceEventsFacet(refNdx,
raimund@3610: FIX_REFERENCE_EVENTS_DWT,
raimund@3610: i18n_ref + " - " + df.format(d)));
raimund@3610: refNdx = idg.next() << 8;
raimund@3610: refNdx = refNdx | i;
raimund@3610: facets.add(new FixLongitudinalReferenceFacet(refNdx,
raimund@3610: FIX_REFERENCE_EVENTS_LS,
raimund@3610: i18n_ref + " - " + df.format(d)));
raimund@3610: refNdx = idg.next() << 8;
raimund@3610: refNdx |= i;
raimund@3610: facets.add(new FixReferenceEventsFacet(refNdx,
raimund@3610: FIX_REFERENCE_EVENTS_WQ,
raimund@3610: i18n_ref + " - " + df.format(d)));
raimund@3610: i++;
raimund@3610: }
sascha@3412:
sascha@3412:
sascha@3412: facets.add(new FixLongitudinalDeviationFacet(idg.next(),
sascha@3412: FIX_DEVIATION_LS,
sascha@3412: i18n_dev));
sascha@3412:
sascha@3412: String i18n_ana = Resources.getMsg(context.getMeta(),
sascha@3412: I18N_ANALYSISPERIODS,
sascha@3412: I18N_ANALYSISPERIODS);
sascha@3412: facets.add(new FixAnalysisPeriodsFacet(idg.next(),
sascha@3412: FIX_ANALYSIS_PERIODS_DWT,
sascha@3412: i18n_ana));
sascha@3412: facets.add(new FixAnalysisPeriodsFacet(idg.next(),
sascha@3412: FIX_ANALYSIS_PERIODS_LS,
sascha@3412: i18n_ana));
sascha@3412: facets.add(new FixAnalysisPeriodsFacet(idg.next(),
sascha@3412: FIX_ANALYSIS_PERIODS_WQ,
sascha@3412: i18n_ana));
sascha@3412:
christian@3907: String i18n_refp = Resources.getMsg(context.getMeta(),
christian@3907: I18N_REFERENCEPERIOD,
christian@3907: I18N_REFERENCEPERIOD);
christian@3907: facets.add(new DataFacet(idg.next(),
christian@3907: FIX_REFERENCE_PERIOD_DWT,
christian@3907: i18n_refp,
christian@3907: ComputeType.ADVANCE, null, null));
sascha@3412:
sascha@3412: facets.add(new FixWQCurveFacet(idg.next(), "W/Q"));
sascha@3412:
sascha@3412: Boolean preprocessing = access.getPreprocessing();
sascha@3412:
sascha@3412: if (preprocessing != null && preprocessing) {
sascha@3412: facets.add(new FixOutlierFacet(
sascha@3412: idg.next(),
sascha@3412: FIX_OUTLIER,
sascha@3412: Resources.getMsg(
sascha@3412: context.getMeta(), I18N_OUTLIER, I18N_OUTLIER)));
sascha@3412: }
sascha@3412:
sascha@3412: facets.add(new FixDerivateFacet(
sascha@3412: idg.next(),
christian@3917: FIX_DERIVATE_CURVE,
sascha@3412: Resources.getMsg(
sascha@3412: context.getMeta(),
sascha@3412: I18N_DERIVATIVE,
sascha@3412: I18N_DERIVATIVE)));
sascha@3412:
sascha@3412: facets.add(new FixDeviationFacet(
sascha@3412: idg.next(),
sascha@3412: FIX_DEVIATION_DWT,
sascha@3412: Resources.getMsg(context.getMeta(),
sascha@3412: I18N_DEVIATION,
sascha@3412: I18N_DEVIATION)));
sascha@3412: return res;
sascha@3412: }
sascha@3412: }
sascha@3412: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :