package osp; import java.util.List; import java.awt.geom.Point2D; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.lang.Math; import jsky.coords.WorldCoords; import jsky.image.fits.codec.FITSImage; import jsky.image.gui.ImageCoordinateConverter; import nom.tam.fits.FitsException; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import osp.ui.OSPE_MainFrame; import osp.utils.CustomJDialog; import osp.utils.Tools; /** * Class used to read an XML file and load a project or a mask. */ public class Reader { // Attributes // ----------- /** Document to read */ private static Document document; /** Field of view root element (root when loading a project) */ private static Element fovElement; /** Mask root element (root when loading a mask) */ private static Element mask; /** Parent Main Frame */ private OSPE_MainFrame pater; /** Field of view */ private FieldOfView fov; private Image image; private int res=-1; private int selectedMask = -1; private String absPath = null; private String loadError = null; private int minJoinedSlit=-1; private int[][] slitJoinArray = new int [55][2]; /** * Constructs a new reader. * * @param p * Main frame of the application. * @param fov * Field of view of the application */ public Reader(OSPE_MainFrame p, FieldOfView fov) { pater = p; this.fov = fov; } /** * Reads a project from a file. Returns 1 if the project has been loaded * correctly, -1 if an error occurred. * * @param fileName * Path of the file containing the project to read. * @return 1 if the project has been loaded correctly, -1 if there has been * an error. */ public int read(String fileName) { SAXBuilder saxB = new SAXBuilder(); try { document = saxB.build(new File(fileName)); } catch (Exception e) { } fovElement = document.getRootElement(); String nameDoc=document.getRootElement().getName(); String f="fieldOfView"; if (f.equals(nameDoc)) { res = loadProject(); return res; } else return res=-1; } /** * Loads the project, iterating through the XML nodes. * * @return 1 if the project has been loaded correctly, -1 if there has been * an error. */ @SuppressWarnings("unchecked") private int loadProject() { int res = -1; // init associated slits array for (int i=0;i<55;i++) { slitJoinArray[i][0]=0; slitJoinArray[i][1]=0; } List imagesElementList = fovElement.getChildren("image"); Iterator iterator = imagesElementList.iterator(); boolean noError = true; while (noError && iterator.hasNext()) { Element currentElementImage = iterator.next(); noError = loadImageMaskAndObject(currentElementImage); } if (noError){ if (fov.getNbrImages() > 0){ int selectedImage = -1; if (fovElement.getAttribute("selectedImage") != null) selectedImage = Integer.parseInt(fovElement.getAttributeValue("selectedImage")); fov.setSelectedImage(selectedImage); pater.updateButtons(); } if (fov.getSelectedImage() >= 0){ pater.getVisuPanel().getNavigatorImageDisplay().setState(2); } else {// display is empty pater.getVisuPanel().getNavigatorImageDisplay().setState(0); } res = 1; } return res; } /** * Load image properties from a project file * @param noError * @param currentImage * @return */ private boolean loadImageMaskAndObject(Element currentImage) { boolean noError = true; noError = loadImageAndProperties(currentImage); if (noError) { noError = loadObjects(currentImage); } if (noError) { noError = loadMasks(currentImage); } if (noError) { image.addObserver(pater.getVisuPanel()); image.addObserver(pater.getSelectPanel()); image.addObserver(pater.getTableurPanel()); if (image.getNbrMasks() > 0) image.setSelectedMask(selectedMask); if (image.getSelectedMask() >= 0) { pater.getVisuPanel().searchButton.setEnabled(true); pater.getVisuPanel().validateButton.setEnabled(true); pater.getVisuPanel().writeButton.setEnabled(true); final Mask currentMask = image.getCurrentMask(); currentMask.addObserver(pater.getOspeControl()); currentMask.addObserver(pater.getVisuPanel()); currentMask.addObserver(pater.getSelectPanel()); currentMask.getCurrentSlit().addObserver(pater.getVisuPanel()); pater.getVisuPanel().slitCombo.setSelectedIndex(currentMask.getSelectedSlit()); pater.getVisuPanel().updateSlides(); } } return noError; } /** * Load mask properties from a project file * @param imageElement : mask from image. * @return boolean noError */ @SuppressWarnings("unchecked") private boolean loadMasks(Element imageElement) { boolean noError = true; // Mask UUID String m_uuid=""; List masksElementList = imageElement.getChildren("mask"); Iterator iterator = masksElementList.iterator(); while (noError && iterator.hasNext()) { Element maskElement = iterator.next(); Point2D.Double maskCenter = new Point2D.Double(); double maskAlpha=0.0, maskDelta=0.0, maskAngle = 0.0; // HMS maskLeftCornerAlpha= new HMS(); // DMS maskLeftCornerDelta= new DMS(); boolean maskValidation = false; int selectedSlit = -1; Point2D.Double mTmpCenter = new Point2D.Double(); Point2D.Double mTmpLeftCorner = new Point2D.Double(); try { // if (maskElement.getChild("maskUUID")!= null) // { // m_uuid=maskElement.getChild("maskUUID").getText();; // } // else m_uuid="N/A"; //System.out.println("Reader loadMask UUID = "+m_uuid); maskCenter.x = Double.parseDouble(maskElement.getChild("center").getChild("x").getValue()); maskCenter.y = Double.parseDouble(maskElement.getChild("center").getChild("y").getValue()); maskAlpha = Double.parseDouble(maskElement.getChild("alpha").getValue()); maskDelta = Double.parseDouble(maskElement.getChild("delta").getValue()); maskAngle = Double.parseDouble(maskElement.getChild("angle").getValue()); mTmpCenter = new Point2D.Double(maskAlpha, maskDelta); if (maskElement.getChild("maskCoordLeftCorner")!= null) { String raLeftC = maskElement.getChild("maskCoordLeftCorner").getChild("ra").getText(); String decLeftC = maskElement.getChild("maskCoordLeftCorner").getChild("dec").getText(); WorldCoords wcM =new WorldCoords(raLeftC,decLeftC); mTmpLeftCorner = new Point2D.Double(wcM.getRaDeg(),wcM.getDecDeg()); // maskLeftCornerAlpha = wcM.getRA(); // maskLeftCornerDelta = wcM.getDec(); } if (maskElement.getAttribute("validated") != null && maskElement.getAttribute("validated").equals("true")) maskValidation = true; if (maskElement.getAttribute("selectedSlit") != null) selectedSlit = Integer.parseInt(maskElement.getAttributeValue("selectedSlit")); } catch (Exception err2) { loadError = "Error loading Mask properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_, pater); noError = false; } if (noError) { noError = addMaskToImage(maskElement, maskCenter, mTmpCenter, mTmpLeftCorner, maskAngle, maskValidation, selectedSlit); Image currentImage = fov.getCurrentImage(); Mask currentMask=currentImage.getCurrentMask(); currentMask.addObserver(pater.getVisuPanel()); currentMask.addObserver(pater.getSelectPanel()); currentMask.addObserver(pater.getOspeControl()); // if (m_uuid.equals("N/A")) currentMask.setM_uuid(); // else currentMask.set_uuid_fromF(m_uuid); } } return noError; } /** * Add mask to the image * @param maskElement : Element figuring mask * @param maskCenter : Mask Center point in canvas coord * @param maskCenterDeg : Mask Center point in degree * @param maskLeftCorner : Mask left bottom corner in WorldCoords. * @param maskAngle : Mask rotation angle * @param maskValidation : yes/no if the mask is validated * @param selectedSlit : Selected slit id. * @return */ @SuppressWarnings("unchecked") protected boolean addMaskToImage(Element maskElement, Point2D.Double maskCenter, Point2D.Double maskCenterDeg, Point2D.Double maskLeftCorner, double maskAngle, boolean maskValidation, int selectedSlit) { boolean noError = true; image.addMask(maskCenter); image.getCurrentMask().setCenterWc(maskCenterDeg.getX(), maskCenterDeg.getY()); image.getCurrentMask().setCenterDeg(maskCenterDeg); image.getCurrentMask().setOmega(maskAngle); image.getCurrentMask().init2Display(); if ((maskLeftCorner.getX()!= 0.0) && (maskLeftCorner.getY() != 0.0)) { //System.out.println("Reader addMaskToImage - left corner ra "+maskLeftCorner.getX()); image.getCurrentMask().setLeftCornerWc(maskLeftCorner.getX(), maskLeftCorner.getY()); // image.getCurrentMask().setAlphaCorner(mLeftCnRa); // image.getCurrentMask().setDeltaCorner(mLeftCnDec); } else { calculateMaskLeftCorner(image.getCurrentMask(), false); } // Load slits List slitsList = maskElement.getChild("slitsList").getChildren("slit"); Iterator k = slitsList.iterator(); while (noError && k.hasNext()) { Element currentSlit = k.next(); noError = loadSlit(currentSlit); } // Associated slits when joined int l=0; int mnslit=100,mxslit=0,vslit=0,rslit=0,refSlit=0; for (l=0;l<55;l++) { if (slitJoinArray[l][0]>0) { vslit=l; rslit=slitJoinArray[l][1]; if (refSlit==0) refSlit=rslit; if (refSlit!=rslit) { associatedSlit(mnslit, mxslit); refSlit=rslit; mnslit=100; mxslit=0; } if (refSlit==rslit) { if (vslitmxslit) { mxslit=vslit; } } } } associatedSlit(mnslit, mxslit); if (noError) { // Set the selected slit image.getCurrentMask().setCurrentSlit(selectedSlit); } return noError; } /** * Load objects from a project file * @param imageElement * @param noError * @return */ @SuppressWarnings("unchecked") private boolean loadObjects(Element imageElement) { boolean noError = true; List objectsLists = imageElement.getChildren("objectsList"); Iterator iterator = objectsLists.iterator(); while (iterator.hasNext()) { Element currentObjectsList = iterator.next(); List skyObjectsList = currentObjectsList.getChildren("skyObject"); Iterator n = skyObjectsList.iterator(); while (noError && n.hasNext()) { Element skyObjectElement = n.next(); // Load object properties int objectId = 0; String idCatObj=""; int numObject =0; int nbCat =0; String objectFrom = "-1"; String objectCatLib = ""; int group; boolean objectIsRef = false; Point2D.Double objectPosition = new Point2D.Double(); Point2D.Double objectNewPosition = null; int objectPriority = 0; WorldCoords wcObject=new WorldCoords(); WorldCoords wcObjectRecalc=new WorldCoords(); boolean recalc=false; String ra="",dec=""; //System.out.println("reader- ob "); String [] tab = new String[] {" "," ", " ", " ", " "}; try { if (skyObjectElement.getAttribute("id") != null) { objectId = Integer.parseInt(skyObjectElement.getAttributeValue("id")); } if (skyObjectElement.getAttribute("group") != null) { objectFrom = skyObjectElement.getAttribute("group").getValue(); } if (skyObjectElement.getChildText("libelleFrom") != null) objectCatLib = skyObjectElement.getChildText("libelleFrom"); if (skyObjectElement.getChild("priority") != null) { String objP = skyObjectElement.getChild("priority").getValue(); objectPriority = Integer.valueOf(objP); } // else // System.out.println("reader- ob prior N"); if (skyObjectElement.getAttributeValue("idCat") != null) { idCatObj = skyObjectElement.getAttributeValue("idCat"); } if (skyObjectElement.getAttributeValue("isRef") != null) objectIsRef = skyObjectElement.getAttributeValue("isRef").equals("true"); objectPosition.x = Double.parseDouble(skyObjectElement .getChild("position").getChild("x").getValue()); objectPosition.y = Double.parseDouble(skyObjectElement .getChild("position").getChild("y").getValue()); if (skyObjectElement.getChild("objCoord") != null) { ra = skyObjectElement.getChild("objCoord").getChildText("ra"); dec = skyObjectElement.getChild("objCoord").getChildText("dec"); wcObject= new WorldCoords(ra, dec); } if (skyObjectElement.getChild("newPosition") != null) { objectNewPosition = new Point2D.Double(); objectNewPosition.x = Double.parseDouble(skyObjectElement .getChild("newPosition").getChild("x").getValue()); objectNewPosition.y = Double.parseDouble(skyObjectElement .getChild("newPosition").getChild("y").getValue()); objectFrom=objectFrom+"(R)"; recalc=true; } if (skyObjectElement.getChild("objNewCoord") != null) { ra = skyObjectElement.getChild("objNewCoord").getChildText("ra"); dec = skyObjectElement.getChild("objNewCoord").getChildText("dec"); wcObjectRecalc= new WorldCoords(ra, dec); recalc=true; } try { nbCat = Integer.valueOf(objectFrom); } catch (Exception e) { nbCat=-1; } numObject=image.getNbrObjects(); // image.addObject(objectId, numObject, objectIsRef, nbCat, objectCatLib, objectPosition, wcObject, objectPriority ); image.addObject(idCatObj, numObject, objectIsRef, nbCat, objectCatLib, objectPosition, wcObject, objectPriority ); if (recalc) { SkyObject lastObject = image.getLastObject(); lastObject.setPosPixRecalc(objectNewPosition); lastObject.setObjWorldRecalc(wcObjectRecalc); } } catch (Exception err4) { loadError = "Error loading Sky objects properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); noError = false; } } } return noError; } /** * @param currentImage * @param noError * @return */ private boolean loadImageAndProperties(Element currentImage) { boolean noError = true; try { absPath = currentImage.getAttributeValue("absPath"); if (!testIfFileExist(absPath)) { String mess = "Error - one of project Image file does not exist !"; new CustomJDialog(mess, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); noError = false; } String imName=null; String imCat=null; if (currentImage.getChild("imageName") != null) imName = currentImage.getChild("imageName").getText(); if (currentImage.getChild("imageCatalog") != null) imCat = currentImage.getChild("imageCatalog").getText(); /** get image position in ..... coord */ Point2D.Double posit = new Point2D.Double(); posit.x = Double.parseDouble(currentImage.getChild("center").getChild("x").getValue()); posit.y = Double.parseDouble(currentImage.getChild("center").getChild("y").getValue()); /** get image center in world coord */ String valRa, valDec; valRa = currentImage.getChild("coordinates").getChild("ra").getText(); valDec = currentImage.getChild("coordinates").getChild("dec").getText(); WorldCoords imCenterWc = new WorldCoords(valRa,valDec); float scale = Float.parseFloat(currentImage.getChild("scale").getValue()); double resol = Double.parseDouble(currentImage.getChild("resol").getValue()); double lowCut = 1.0, highCut = 1.0; int flgColor = 0, flgCutLevels = 0; if (currentImage.getChild("lowCut") != null) { lowCut = Double.parseDouble(currentImage.getChild("lowCut").getValue()); flgCutLevels = 1; } if (currentImage.getChild("highCut") != null){ highCut = Double.parseDouble(currentImage.getChild("highCut").getValue()); } String colorMap = " ", colorIntens = " "; if (currentImage.getChild("colorMap") != null) { colorMap = currentImage.getChild("colorMap").getText(); flgColor = 1; } if (currentImage.getChild("colorIntensity") != null) colorIntens = currentImage.getChild("colorIntensity").getText(); Element colorAlgo = null; int cAlgo = 0; if (currentImage.getChild("colorAlgo") != null) colorAlgo = currentImage.getChild("colorAlgo"); if (colorAlgo != null) cAlgo = Integer.parseInt(currentImage.getChild("colorAlgo").getValue()); if (noError) { // Add the image to the project and set its properties image = loadImage(fov, absPath, posit, imCenterWc, scale, resol, lowCut, highCut, flgColor, flgCutLevels, colorMap, colorIntens, cAlgo, imName, imCat); selectedMask = -1; if (currentImage.getAttribute("selectedMask") != null) selectedMask = Integer.parseInt(currentImage.getAttributeValue("selectedMask")); } } catch (NumberFormatException | NullPointerException err1) { loadError = "Error loading Image properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); noError = false; } catch (FitsException | IOException exception) { loadError = "Error loading Image: " + absPath; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_,pater); noError = false; } return noError; } private boolean testIfFileExist(String pathFile) { boolean noError = true; File testFile=new File(pathFile); if (testFile.exists()) noError=true; else noError=false; return noError; } /** * loadImage : create an image, add it to the image list of the field of view * @param fov : Field of view * @param absPath : path of image * @param posit : image position in world coord * @param scale : image scale * @param resol : image resolution * @param lowCut : if some low cut level value positionned, take this value * @param highCut : if some hight cut level value positionned, take this value * @param flgColor : flag to indicate that there are colors to integrate * @param flgCutLevels : flag to indicate that there are cut levels to integrate * @param colorMap : Value of the color map * @param colorIntens : intensity color value * @param cAlgo : value of the color algorithm * @param imName : name of the image if there is one (some old projects may not have this parameter) * @param imCat : catalog name if there is one. * @return * @throws IOException * @throws FitsException */ public static Image loadImage(FieldOfView fov, String absPath, Point2D.Double posit, WorldCoords imCenterCoords, float scale, double resol, double lowCut, double highCut, int flgColor, int flgCutLevels, String colorMap, String colorIntens, int cAlgo, String imName, String imCat) throws IOException, FitsException { fov.addImage(new FITSImage(absPath), posit, scale, imName, imCat); Image image = fov.getCurrentImage(); image.setAbsPath(absPath); image.setImCenterWc(imCenterCoords.getRA(), imCenterCoords.getDec()); image.setResol(resol); // Display the cuts levels if (flgCutLevels > 0) { image.setLowCut(lowCut); image.setHighCut(highCut); flgCutLevels = 0; } // Read if there are image's colors if (flgColor > 0) { // Set this parameters on image characteristics image.setColorMap(colorMap); image.setColorIntensity(colorIntens); image.setColorAlgorithm(cAlgo); // Re-init flgColor = 0; } return image; } /** * Load slit properties when loading a project * @param slitElement * @param j TODO * @param noError * @return */ private boolean loadSlit(Element slitElement) { boolean noError = true; int id = 0; int affectedObject = -1; double position=0.0, aperture = 0.0; Element e_slitDegCoord= slitElement.getChild("slitDegCoord"); String slitRa; String slitDec; WorldCoords wcSlit=new WorldCoords(0,0); boolean slitExist=false; boolean isJoined=false; int joinedSlitId=-1; try { id = Integer.parseInt(slitElement.getAttributeValue("id")); slitExist=true; if (id>0) id--; position = Double.parseDouble(slitElement.getChild("position").getValue()); aperture = Double.parseDouble(slitElement.getChild("width").getValue()); if (slitElement.getChild("affectedObject")!=null) affectedObject = Integer.parseInt(slitElement.getChild("affectedObject").getValue()); if (e_slitDegCoord != null) { slitRa=e_slitDegCoord.getChild("raDeg").getText(); slitDec=e_slitDegCoord.getChild("decDeg").getText(); wcSlit=new WorldCoords(slitRa,slitDec); } if (slitElement.getChild("isJoined") != null) { String val=slitElement.getChild("isJoined").getValue(); if (val.equals("true")) isJoined=true; } if (slitElement.getChild("joinedRefSlit") != null) { joinedSlitId = Integer.parseInt(slitElement.getChild("joinedRefSlit").getValue()); } final Slit slit = image.getCurrentMask().getSlit(id); slit.setPosition(position); slit.setAperture(aperture); if (slitExist) { if (e_slitDegCoord != null) { Point2D.Double p=new Point2D.Double(wcSlit.getRaDeg(), wcSlit.getDecDeg()); slit.setWcPosCenter(p); slit.fSkyCoord=true; slit.addObserver(pater.getVisuPanel()); } if (affectedObject>0) slit.setAffectedObject(affectedObject); if (isJoined) { slit.setIsJoined(true); slit.setJoinedRefSlit(joinedSlitId-1); slitJoinArray[id][0]=id; int ref= joinedSlitId-1; slitJoinArray[id][1]=ref; } // endif Joined } // End Slit } catch (Exception err3) { loadError = "Error loading Slit properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); noError = false; } return noError; } /** * Reads a mask from a file. Returns 1 if the mask has been loaded * correctly, -1 if an error occurred. * * @param fileName * Path of the file containing the mask to read. * @return 1 if the mask has been loaded correctly, -1 if there has been an * error. */ public int readMask(String fileName) { SAXBuilder sxb = new SAXBuilder(); try { document = sxb.build(new File(fileName)); } catch (Exception e) { } mask = document.getRootElement(); String nameDoc=document.getRootElement().getName(); String mk="mask"; if (mk.equals(nameDoc)) { res = loadMask(); return res; } else return res=-1; } /** * Loads the mask from a mask file * * @return 1 if the mask has been loaded correctly, -1 if there has been an * error. */ @SuppressWarnings("unchecked") private int loadMask() { Image currentImage = fov.getCurrentImage(); // Load mask properties // Point2D.Double maskCenter = new Point2D.Double(); // Point2D.Double maskCenterTmp = new Point2D.Double(); Point2D.Double maskCenterDeg = new Point2D.Double(); Point2D.Double maskCenterConv = new Point2D.Double(); boolean maskValidation = false; int selectedSlit = -1; // Mask UUID String m_uuid; double maskAlpha =0, maskDelta=0, maskAngle=0, maskBaseAngle=0; String raLeftC="",decLeftC=""; // HMS maskLeftCornerAlpha= new HMS(); // DMS maskLeftCornerDelta= new DMS(); WorldCoords wcMkCorner =new WorldCoords(); // Load mask properties try { if (mask.getChild("maskUUID")!= null) { m_uuid=mask.getChild("maskUUID").getText();; } else m_uuid="N/A"; //System.out.println("Reader loadMask UUID = "+m_uuid); maskAlpha = Double.parseDouble(mask.getChild("alpha").getValue()); maskDelta = Double.parseDouble(mask.getChild("delta").getValue()); maskAngle = Double.parseDouble(mask.getChild("angle").getValue()); if (mask.getChild("maskCoordLeftCorner")!= null) { raLeftC = mask.getChild("maskCoordLeftCorner").getChild("ra").getText(); decLeftC = mask.getChild("maskCoordLeftCorner").getChild("dec").getText(); wcMkCorner =new WorldCoords(raLeftC,decLeftC); // maskLeftCornerAlpha = wcMkCorner.getRA(); // maskLeftCornerDelta = wcMkCorner.getDec(); } if (mask.getAttribute("validated") != null) maskValidation = mask.getAttribute("validated").equals("true"); if (mask.getAttribute("selectedSlit") != null) selectedSlit = Integer.parseInt(mask.getAttributeValue("selectedSlit")); } catch (Exception err1) { loadError = "Error loading Mask properties ! "; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); return 0; } // Set the mask center in degrees. maskCenterDeg.setLocation(maskAlpha, maskDelta); // Test if the mask is inside the image. if (!fitsIn(maskCenterDeg, currentImage)) { String mess = "The mask is not included in the image"; new CustomJDialog(mess, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); return -1; } /** Used to convert the center in canvas coord to display*/ maskCenterConv.setLocation(maskCenterDeg); // /** Used to testing */ //maskCenterTmp.setLocation(maskCenterDeg); /** Tool to convert the coordinates */ // ImageCoordinateConverter icc = new ImageCoordinateConverter(pater.getVisuPanel().getNavigatorImageDisplay()); ImageCoordinateConverter icc = pater.getVisuPanel().getIcc(); // icc.worldToImageCoords(maskCenterTmp, false); // System.out.println("453- Reader : LoadMask W2Image x:"+maskCenterConv.x+" "+maskCenterConv.y); // icc.imageToCanvasCoords(maskCenterTmp, false); // System.out.println("455- Reader : LoadMask Im2Can x:"+maskCenterConv.x+" "+maskCenterConv.y); /** Converting center world coord in canvas */ icc.worldToCanvasCoords(maskCenterConv, false); // System.out.println("459- Reader : LoadMask W2Canvas x:" + maskCenterTmp.x + " " // + maskCenterTmp.y); // Add the mask to the current image currentImage.addMask(maskCenterConv); Mask currentMask = currentImage.getCurrentMask(); // if (m_uuid.equals("N/A")) currentMask.setM_uuid(); // else currentMask.set_uuid_fromF(m_uuid); currentMask.setCenterWc(maskAlpha, maskDelta); currentMask.setOmega(maskAngle); currentMask.setCenterDeg(maskCenterDeg); currentMask.init2Display(); double valRa=0; double valDec=0; valRa = wcMkCorner.getRaDeg(); valDec = wcMkCorner.getDecDeg(); /** A VERIFIER **/ // if ((valRa !=0.0) && (valDec!=0.0)) // { // // currentMask.setMkLeftCornerWc(raLeftC, decLeftC); // maskBaseAngle = addGoodBaseOmegaByImage(currentMask); // } // else calculateMaskLeftCorner(currentMask,true); //if (maskLeftCornerAlpha != null) //currentMask.setAlphaCorner(maskLeftCornerAlpha); //if (maskLeftCornerDelta != null) //currentMask.setDeltaCorner(maskLeftCornerDelta); /** A VERIFIER **/ // if (maskBaseAngle > 0.1) // { // System.out.println("Add Angle :" + maskBaseAngle); // currentMask.setOmega((maskAngle + maskBaseAngle) % 360); // } currentMask.validated = maskValidation; if (currentMask.validated) { currentMask.turnEnable =true; currentMask.moveEnable= true; } else { currentMask.turnEnable =false; currentMask.moveEnable= false; } // Load slits List listSlits = mask.getChild("slitsList").getChildren("slit"); Iterator k = listSlits.iterator(); double position=0.0, aperture = 0.0; while (k.hasNext()) { Element currentSlit = k.next(); // Load slit properties int id = 0; try { id = Integer.parseInt(currentSlit.getAttributeValue("id")); if (id>0) id--; position = Double.parseDouble(currentSlit.getChild("position").getValue()); aperture = Double.parseDouble(currentSlit.getChild("width").getValue()); } catch (Exception err2) { loadError = "Error loading Slit properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); return -1; } // Set slit properties currentMask.getSlit(id).setPosition(position); currentMask.getSlit(id).setAperture(aperture); }// End slits iterator currentMask.setCurrentSlit(selectedSlit); // Load reference objects List refObjectsList = mask.getChildren("refObject"); Iterator l = refObjectsList.iterator(); // int idObj=-1; String idObj="-1"; while (l.hasNext()) { Element currentRefObject = l.next(); // Load reference object properties String objectCatLib = ""; if (currentRefObject.getChildText("libelleFrom") != null) objectCatLib = currentRefObject.getChildText("libelleFrom"); else objectCatLib ="load with mask"; Point2D.Double objectPosition = new Point2D.Double(); WorldCoords wcRefObject = new WorldCoords(0,0); Point2D.Double objectNewPosition = null; try { objectPosition.x = Double.parseDouble(currentRefObject.getChild("position") .getChild("x").getValue()); objectPosition.y = Double.parseDouble(currentRefObject.getChild("position") .getChild("y").getValue()); wcRefObject = new WorldCoords(objectPosition.x ,objectPosition.y); icc.worldToImageCoords(objectPosition, false); if (currentRefObject.getChild("newPosition") != null) { objectNewPosition = new Point2D.Double(); objectNewPosition.x = Double.parseDouble(currentRefObject .getChild("newPosition").getChild("x").getValue()); objectNewPosition.y = Double.parseDouble(currentRefObject .getChild("newPosition").getChild("y").getValue()); icc.worldToImageCoords(objectNewPosition, false); } } catch (Exception err3) { loadError = "Error loading Reference Objects properties !"; new CustomJDialog(loadError, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_, pater); return -1; } // Add the object to the image if it doesn't exist, // or set the object as a reference if it does. boolean exists = false; for (int i = 0; i < currentImage.getNbrObjects(); i++) { SkyObject sk = currentImage.getListObjects().get(i); // if (objectPosition.equals(sk.getPosPix()) // || objectPosition.equals(sk.getPosPixRecalc())) { if (objectPosition.equals(sk.getInitPos()) || objectPosition.equals(sk.getInitPos())) { sk.setRef(true); idObj=sk.getTargetId(); exists = true; break; } } if (!exists) { int prior=0; int nbCat=-1; String [] tab = new String[] {" "," "," "," "}; String temp=" "; currentImage.addObject(idObj,true,nbCat, temp, objectPosition, wcRefObject,prior); currentImage.getLastObject().setPosPixRecalc(objectNewPosition); } }// End reference objects iterator currentMask.addObserver(pater.getOspeControl()); currentMask.addObserver(pater.getVisuPanel()); currentMask.addObserver(pater.getSelectPanel()); currentMask.getCurrentSlit().addObserver(pater.getVisuPanel()); pater.getVisuPanel().slitCombo.setSelectedIndex(currentMask.getSelectedSlit()); pater.getVisuPanel().updateSlides(); currentMask.update(); return 1; } /** * Calculate the left corner of the mask in prevision when a rotation is applied * @param currentMask : Current Mask * @param from : true if loading a mask alone, false if loading a project */ private void calculateMaskLeftCorner(Mask currentMask, boolean from) { // set the new mask's Corner left coordinates double width = currentMask.getWidth2Display(); double height = currentMask.getHeight2Display(); double maskDebutX = currentMask.getCenter().getX() - (width / 2.0); double maskDebutY = currentMask.getCenter().getY() - (height / 2.0); Point2D.Double tmpCorner = new Point2D.Double(); tmpCorner.setLocation(maskDebutX, maskDebutY); currentMask.setLeftCornerPix(tmpCorner); if (from) { WorldCoords wcLeftMaskC = pater.getVisuPanel().getWorldCoords(tmpCorner.getX(), tmpCorner.getY()); tmpCorner.setLocation(wcLeftMaskC.getRaDeg(), wcLeftMaskC.getDecDeg()); currentMask.setLeftCornerDeg(tmpCorner); // currentMask.setAlphaCorner(wcLeftMaskC.getRA()); // currentMask.setDeltaCorner(wcLeftMaskC.getDec()); } else { tmpCorner.setLocation(0.0,0.0); currentMask.setLeftCornerDeg(tmpCorner); } } /** * Find the good omega of the mask if the image is not on the same orientation that the original image */ private double addGoodBaseOmegaByImage(Mask currentMask) { // System.out.println("AddGoodBaseOmegaByImage"); int clockwise; double res; double width = currentMask.getWidth2Display(); double height = currentMask.getHeight2Display(); double maskDebutX = currentMask.getCenter().x - (width / 2.0); double maskDebutY = currentMask.getCenter().y - (height / 2.0); res = 0.0; WorldCoords wc = new WorldCoords(mask.getChild("maskCoordLeftCorner").getChild("ra").getValue(), mask.getChild("maskCoordLeftCorner").getChild("dec").getValue()); //System.out.println("Corner RA " + wc.getRA() + " DEC " + wc.getDec()); WorldCoords wc2 = new WorldCoords(); Tools tools = new Tools(); Point2D.Double pCenter = new Point2D.Double(); Point2D.Double pBaseLeftCorner = new Point2D.Double(); Point2D.Double pRealLeftCorner = new Point2D.Double(); wc = new WorldCoords(mask.getChild("maskCoordLeftCorner").getChild("ra").getValue(), mask.getChild("maskCoordLeftCorner").getChild("dec").getValue()); wc2 = pater.getVisuPanel().getWorldCoords(maskDebutX, maskDebutY); //System.out.println("Real Corner RA " + wc2.getRA() + " DEC " + wc2.getDec()); //System.out.println("End AddGoodBaseBaseOmegaByImage"); // return res; pCenter.setLocation(currentMask.getCenterDeg()); pBaseLeftCorner.setLocation(wc.getRaDeg(), wc.getDecDeg()); pRealLeftCorner.setLocation(wc2.getRaDeg(), wc2.getDecDeg()); clockwise = tools.CounterClockWise(pCenter.getX(), pCenter.getY(), pRealLeftCorner.getX(), pRealLeftCorner.getY(), pBaseLeftCorner.getX(), pBaseLeftCorner.getY()); res = tools.AngleOfView(pCenter.getX(), pCenter.getY(), pBaseLeftCorner.getX(), pBaseLeftCorner.getY(), pRealLeftCorner.getX(),pRealLeftCorner.getY()); if (clockwise == 1) return (res); else if (clockwise == -1) return (360 - res); else return (0); } /** * Test if a mask is included inside an image. It tests both if the world * coordinates correspond (mask center in the image) and if it's fitting * inside the bounds of the image. * * @param mask * The mask to test. * @param image * The image to test. * @return true if the mask is inside the image. */ private boolean fitsIn(Point2D.Double maskCenter, Image image) { // Get the bottom-left and bottom right corners coordinates of the image Point2D.Double imBottomLeftCorner = new Point2D.Double(0, 0); ImageCoordinateConverter icc = new ImageCoordinateConverter(pater.getVisuPanel().getNavigatorImageDisplay()); icc.imageToWorldCoords(imBottomLeftCorner, false); Point2D.Double imTopRightCorner = new Point2D.Double(image.getWidth(), image.getHeight()); icc.imageToWorldCoords(imTopRightCorner, false); /** * To test if the mask loaded is in the image, convert the coord to * |coord| and take the min and the max of them. */ double xM = Math.max(Math.abs(imBottomLeftCorner.x), Math.abs(imTopRightCorner.x)); double xm = Math.min(Math.abs(imBottomLeftCorner.x), Math.abs(imTopRightCorner.x)); double yM = Math.max(Math.abs(imBottomLeftCorner.y), Math.abs(imTopRightCorner.y)); double ym = Math.min(Math.abs(imBottomLeftCorner.y), Math.abs(imTopRightCorner.y)); Point2D.Double imbt = new Point2D.Double(xm, ym); Point2D.Double imtp = new Point2D.Double(xM, yM); Point2D.Double mct = new Point2D.Double(Math.abs(maskCenter.x), Math.abs(maskCenter.y)); //System.out.println("Reader:FitsIn im "+imbt.x+","+imbt.y+" "+imtp.x+","+imtp.y); //System.out.println("Reader:FitsIn mk "+mct.x+","+mct.y); if (mct.x <= imbt.x || mct.y <= imbt.y || mct.x >= imtp.x || mct.y >= imtp.y) return false; return true; } public String getMessageError() { // TODO Auto-generated method stub return loadError; } private void associatedSlit(int min, int max) { for (int i = min; i <= max; i++) { for (int j = i + 1; j <= max; j++) { Image currentImage = fov.getCurrentImage(); Mask currentMask=currentImage.getCurrentMask(); currentMask.associatedSlits[i][j] = true; currentMask.associatedSlits[j][i] = true; } } } }