Search in sources :

Example 1 with StringValue

use of fr.jmmc.aspro.model.oi.StringValue in project aspro by JMMC-OpenDev.

the class SearchCalVOTableHandler method processMessage.

/**
 * Process the given votable
 *
 * @param votable votable to process
 * @param searchCalVersion SearchCal GUI version
 * @return true if the votable was processed i.e. contains proper SearchCal data
 *
 * @throws IOException if an I/O exception occured
 * @throws IllegalArgumentException if the file is not an Observation
 */
static boolean processMessage(final String votable, final String searchCalVersion) throws IOException {
    // use an XSLT to transform the SearchCal votable document to an Aspro 2 Observation:
    final long start = System.nanoTime();
    final String document = XslTransform.transform(votable, XSLT_FILE).trim();
    logger.info("VOTable transformation (XSLT) duration = {} ms.", 1e-6d * (System.nanoTime() - start));
    if (document.length() == 0) {
        logger.debug("document is empty (probably not a SearchCal VOTable)");
        return false;
    }
    logger.debug("document:\n{}", document);
    final ObservationManager om = ObservationManager.getInstance();
    try {
        final ObservationSetting searchCalObservation = om.load(new StringReader(document));
        // check GetStar / SearchCal command:
        final String cmdName = searchCalObservation.getName();
        logger.debug("cmdName: {}", cmdName);
        if (!GET_STAR_NAME.equalsIgnoreCase(cmdName)) {
            // SearchCal case:
            final List<Target> calibrators = searchCalObservation.getTargets();
            final Target scienceTarget;
            if (calibrators.isEmpty()) {
                scienceTarget = null;
            } else {
                // first target is the science target (partial information only):
                scienceTarget = calibrators.remove(0);
                // format the target name:
                scienceTarget.updateNameAndIdentifier();
                logger.debug("science target: {}", scienceTarget);
                if (logger.isDebugEnabled()) {
                    logger.debug("calibrators:");
                    for (Target cal : calibrators) {
                        logger.debug(cal.toString());
                    }
                }
                // @note SCIENCE_DISTANCE_CHECK : filter science target if distance is less than science object detection distance preference (1 arcsec):
                for (Iterator<Target> it = calibrators.iterator(); it.hasNext(); ) {
                    final Target cal = it.next();
                    final BaseValue dist = cal.getCalibratorInfos().getField(CalibratorInformations.FIELD_DISTANCE);
                    if (dist != null) {
                        final double rowDistance = dist.getNumber().doubleValue();
                        // If the distance is close enough to be detected as a science object
                        if (rowDistance < SCIENCE_DETECTION_DISTANCE) {
                            if (logger.isInfoEnabled()) {
                                logger.info("calibrator distance is [{}] - skip this calibrator considered as science object: {} - IDS = {}", rowDistance, cal, cal.getIDS());
                            }
                            it.remove();
                            // reuse science target data to update scienceTarget object:
                            Target.merge(scienceTarget, cal);
                        }
                    }
                }
                // Add the SearchCalGuiVersion parameter to calibrators if missing:
                final StringValue paramSearchCalVersion = new StringValue();
                paramSearchCalVersion.setName(CalibratorInformations.PARAMETER_SCL_GUI_VERSION);
                paramSearchCalVersion.setValue(searchCalVersion);
                for (Target cal : calibrators) {
                    if (cal.getCalibratorInfos().getParameter(CalibratorInformations.PARAMETER_SCL_GUI_VERSION) == null) {
                        cal.getCalibratorInfos().getParameters().add(paramSearchCalVersion);
                    }
                }
            }
            // Use invokeLater to avoid concurrency and ensure that
            // data model is modified and fire events using Swing EDT:
            SwingUtils.invokeEDT(new Runnable() {

                @Override
                public void run() {
                    // check the number of calibrators:
                    if (calibrators.isEmpty()) {
                        MessagePane.showErrorMessage("No calibrator found in SearchCal response !");
                        return;
                    }
                    if (!TargetImporter.confirmImport(calibrators.size())) {
                        return;
                    }
                    // find correct diameter among UD_ for the Aspro instrument band ...
                    // or using alternate diameters (in order of priority): UD, LD, UDDK, DIA12
                    om.defineCalibratorDiameter(calibrators);
                    final TargetEditorDialog targetEditor = TargetEditorDialog.getTargetEditor();
                    // Prepare the data model (editable targets and user infos) :
                    final TargetEditContext editTargetCtx = (targetEditor != null) ? targetEditor.getTargetEditCtx() : om.getMainObservation().createTargetEditContext();
                    final List<Target> editTargets = editTargetCtx.getTargets();
                    final TargetUserInformations editTargetUserInfos = editTargetCtx.getTargetUserInfos();
                    if (logger.isDebugEnabled()) {
                        logger.debug("initial targets:");
                        for (Target t : editTargets) {
                            logger.debug(t.toString());
                        }
                    }
                    // Find any target (id + position) within 5 arcsecs:
                    Target currentScienceTarget = Target.matchTarget(scienceTarget, editTargets);
                    if (currentScienceTarget == null) {
                        logger.info("Target '{}' not found in targets; adding it (partial information).", scienceTarget.getName());
                        editTargets.add(scienceTarget);
                        currentScienceTarget = scienceTarget;
                    }
                    final String report = mergeTargets(editTargets, editTargetUserInfos, currentScienceTarget, calibrators);
                    if (logger.isDebugEnabled()) {
                        logger.debug("updated targets:");
                        for (Target t : editTargets) {
                            logger.debug(t.toString());
                        }
                    }
                    if (targetEditor != null) {
                        // refresh target editor:
                        targetEditor.refreshDialog();
                    } else {
                        // update the complete list of targets and force to update references:
                        // needed to replace old target references by the new calibrator targets:
                        om.updateTargets(editTargetCtx);
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info(report);
                    }
                    // display report message:
                    MessagePane.showMessage(report);
                }
            });
        } else {
            // GetStar case:
            final List<Target> targets = searchCalObservation.getTargets();
            if (logger.isDebugEnabled()) {
                logger.debug("targets:");
                for (Target t : targets) {
                    logger.debug(t.toString());
                }
            }
            // Use invokeLater to avoid concurrency and ensure that
            // data model is modified and fire events using Swing EDT:
            SwingUtils.invokeEDT(new Runnable() {

                @Override
                public void run() {
                    // check the number of targets:
                    if (targets.isEmpty()) {
                        MessagePane.showErrorMessage("No target found in GetStar response !");
                        return;
                    }
                    if (!TargetImporter.confirmImport(targets.size())) {
                        return;
                    }
                    // find correct diameter among UD_ for the Aspro instrument band ...
                    // or using alternate diameters (in order of priority): UD, LD, UDDK, DIA12
                    om.defineCalibratorDiameter(targets);
                    final TargetEditorDialog targetEditor = TargetEditorDialog.getTargetEditor();
                    // Prepare the data model (editable targets and user infos) :
                    final TargetEditContext editTargetCtx = (targetEditor != null) ? targetEditor.getTargetEditCtx() : om.getMainObservation().createTargetEditContext();
                    final List<Target> editTargets = editTargetCtx.getTargets();
                    if (logger.isDebugEnabled()) {
                        logger.debug("initial targets:");
                        for (Target t : editTargets) {
                            logger.debug(t.toString());
                        }
                    }
                    final String report = mergeTargets(editTargets, targets);
                    if (logger.isDebugEnabled()) {
                        logger.debug("updated targets:");
                        for (Target t : editTargets) {
                            logger.debug(t.toString());
                        }
                    }
                    if (targetEditor != null) {
                        // refresh target editor:
                        targetEditor.refreshDialog();
                    } else {
                        // update the complete list of targets and force to update references:
                        // needed to replace old target references by the new calibrator targets:
                        om.updateTargets(editTargetCtx);
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info(report);
                    }
                    // display report message:
                    MessagePane.showMessage(report);
                }
            });
        }
    } catch (IllegalArgumentException iae) {
        // Report both Observation and VOTable in a new IllegalArgumentException to get them in the feedback report:
        throw new IllegalArgumentException("Invalid generated Aspro2 Observation:\n\n" + document + "\n\nSAMP VOTable argument:\n\n" + votable, iae);
    }
    // interpreted as SearchCal votable:
    return true;
}
Also used : ObservationSetting(fr.jmmc.aspro.model.oi.ObservationSetting) BaseValue(fr.jmmc.aspro.model.oi.BaseValue) ObservationManager(fr.jmmc.aspro.model.ObservationManager) Target(fr.jmmc.aspro.model.oi.Target) TargetEditContext(fr.jmmc.aspro.model.TargetEditContext) StringReader(java.io.StringReader) List(java.util.List) TargetUserInformations(fr.jmmc.aspro.model.oi.TargetUserInformations) StringValue(fr.jmmc.aspro.model.oi.StringValue) TargetEditorDialog(fr.jmmc.aspro.gui.TargetEditorDialog)

Aggregations

TargetEditorDialog (fr.jmmc.aspro.gui.TargetEditorDialog)1 ObservationManager (fr.jmmc.aspro.model.ObservationManager)1 TargetEditContext (fr.jmmc.aspro.model.TargetEditContext)1 BaseValue (fr.jmmc.aspro.model.oi.BaseValue)1 ObservationSetting (fr.jmmc.aspro.model.oi.ObservationSetting)1 StringValue (fr.jmmc.aspro.model.oi.StringValue)1 Target (fr.jmmc.aspro.model.oi.Target)1 TargetUserInformations (fr.jmmc.aspro.model.oi.TargetUserInformations)1 StringReader (java.io.StringReader)1 List (java.util.List)1