Search in sources :

Example 1 with TargetEditorDialog

use of fr.jmmc.aspro.gui.TargetEditorDialog 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)

Example 2 with TargetEditorDialog

use of fr.jmmc.aspro.gui.TargetEditorDialog in project aspro by JMMC-OpenDev.

the class ImageSampMessageHandler method processMessage.

/**
 * Implements message processing
 *
 * @param senderId public ID of sender client
 * @param message message with MType this handler is subscribed to
 * @throws SampException if any error occured while message processing
 */
@Override
protected void processMessage(final String senderId, final Message message) throws SampException {
    if (logger.isDebugEnabled()) {
        logger.debug("\tReceived '{}' message from '{}' : '{}'.", this.handledMType(), senderId, message);
    }
    // get url of file (locally stored):
    final String imgURL = (String) message.getRequiredParam("url");
    logger.info("processMessage: Image URL = {}", imgURL);
    if (imgURL == null) {
        throw new SampException("Can not get the url of the observation file");
    }
    URI imgURI;
    try {
        imgURI = new URI(imgURL);
    } catch (URISyntaxException use) {
        logger.error("invalid URI", use);
        throw new SampException("Can not read the image : " + imgURL, use);
    }
    // Guess file name:
    String fileName = FileUtils.getName(imgURL);
    if (fileName.isEmpty()) {
        fileName = StringUtils.replaceNonAlphaNumericCharsByUnderscore(imgURI.getPath());
    }
    // Download or get local file:
    File imgFile = null;
    try {
        final String scheme = imgURI.getScheme();
        if (scheme.equalsIgnoreCase("file")) {
            try {
                imgFile = new File(imgURI);
            } catch (IllegalArgumentException iae) {
                logger.debug("Invalid URI: {}", imgURI, iae);
                String path = imgURI.getPath();
                logger.debug("imgURI path: {}", path);
                final int nbSlash = countStartingSlash(path);
                logger.debug("nbSlash: {}", nbSlash);
                // always use '///path'
                // ds9 bug: URI file://localhost//tmp/...
                path = "///" + path.substring(nbSlash);
                logger.debug("fixed path: {}", path);
                // aladin bug: URI has an authority component
                try {
                    imgURI = new URI(scheme, path, null);
                } catch (URISyntaxException use) {
                    logger.error("invalid URI", use);
                    throw new SampException("Can not read the image : " + imgURL, use);
                }
            }
            try {
                imgFile = new File(imgURI);
            } catch (IllegalArgumentException iae) {
                logger.info("Invalid URI: {}", imgURI, iae);
            }
        } else {
            final File file = FileUtils.getTempFile("aspro-" + fileName, ".fits");
            if (Http.download(imgURI, file, false)) {
                imgFile = file;
            }
        }
        if (imgFile == null) {
            throw new SampException("Not supported URI scheme : " + imgURL);
        }
    } catch (IOException ioe) {
        MessagePane.showErrorMessage("Can not read the image :\n\n" + imgURL);
        throw new SampException("Can not read the image : " + imgURL, ioe);
    }
    final String urlFileName = fileName;
    final File localFile = imgFile;
    // 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() {
            logger.info("localFile: {}", localFile);
            // Use main observation to get current target:
            final ObservationSetting observation = ObservationManager.getInstance().getMainObservation();
            // retrieve the selected target from its name:
            final Target target = observation.getTarget(observation.getSelectedTargetName());
            if (target != null) {
                logger.debug("target = {}", target);
                if (MessagePane.showConfirmMessage("Do you want to use the incoming image as an user model for the target '" + target.getName() + "' ?\n\n" + urlFileName)) {
                    // Ask the user to rename the file as the filename is generated (hash) in persistent folder:
                    final File imageFile = FileChooser.showSaveFileChooser("Choose (persistent) destination to write the Fits image file", null, MimeType.FITS_IMAGE, urlFileName);
                    // Cancel
                    if (imageFile == null) {
                        return;
                    }
                    // copy the file at the user location and name:
                    try {
                        FileUtils.copy(localFile, imageFile);
                    } catch (IOException ioe) {
                        MessagePane.showErrorMessage("Can not write the image to : " + imageFile.getAbsolutePath(), ioe);
                        return;
                    }
                    // Open target editor:
                    final TargetEditorDialog targetEditor = TargetEditorDialog.getTargetEditor();
                    if (targetEditor == null) {
                        // TODO: blocking so give the model file too as an optional argument
                        TargetEditorDialog.showEditor(target.getName(), TargetEditorDialog.TAB_MODELS, imageFile);
                    } else {
                        // refresh dialog to use the given model file for the current selected target
                        targetEditor.defineUserModel(imageFile);
                    }
                }
            }
        }
    });
}
Also used : SampException(org.astrogrid.samp.client.SampException) Target(fr.jmmc.aspro.model.oi.Target) ObservationSetting(fr.jmmc.aspro.model.oi.ObservationSetting) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File) TargetEditorDialog(fr.jmmc.aspro.gui.TargetEditorDialog)

Aggregations

TargetEditorDialog (fr.jmmc.aspro.gui.TargetEditorDialog)2 ObservationSetting (fr.jmmc.aspro.model.oi.ObservationSetting)2 Target (fr.jmmc.aspro.model.oi.Target)2 ObservationManager (fr.jmmc.aspro.model.ObservationManager)1 TargetEditContext (fr.jmmc.aspro.model.TargetEditContext)1 BaseValue (fr.jmmc.aspro.model.oi.BaseValue)1 StringValue (fr.jmmc.aspro.model.oi.StringValue)1 TargetUserInformations (fr.jmmc.aspro.model.oi.TargetUserInformations)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 SampException (org.astrogrid.samp.client.SampException)1