Writer.java 28.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
package osp;

import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Iterator;

import jsky.coords.WorldCoords;
import jsky.image.gui.ImageCoordinateConverter;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import osp.ui.OSPE_MainFrame;
import osp.utils.CustomJDialog;

/**
 * Class used to save the properties of a mask or a project in an XML file.
 */
public class Writer {
	// Attributes
	// -----------
	/** The field of view */
	private FieldOfView fov;
	/** Parent: main frame */
	private OSPE_MainFrame pater;

	private ImageCoordinateConverter icc;

	// Constructors
	// -----------

	/**
	 * Constructs a new writer.
	 * 
	 * @param p
	 *            Main frame of the application.
	 * @param fov2 
	 * 			Field of view of the application
	 */
	public Writer(OSPE_MainFrame p, FieldOfView fov) {
		pater = p;
		this.fov = fov;
		icc = new ImageCoordinateConverter(pater.getVisuPanel().getNavigatorImageDisplay());
	}

	// Methods
	// -----------
	/**
	 * Writes all the project components and their properties in an XML file :
	 * images, objects, masks, and slits.
	 * 
	 * @param fileName
	 *            File name of the project.
	 */
	public void write(String fileName) {
		Element fieldOfView = new Element("fieldOfView");
		Document document = new Document(fieldOfView);

		Attribute path = new Attribute("path", fov.getCompletePath());
		fieldOfView.setAttribute(path);

		
		Attribute selectedImage = new Attribute("selectedImage", String.valueOf(fov
				.getSelectedImage()));
		fieldOfView.setAttribute(selectedImage);

		// Images loop
		for (int i = 0; i < fov.getNbrImages(); i++) 
		{
			
			Image im = fov.getImage(i);
			fieldOfView.addContent(writeImageObjectsMasksAndSlits(im));
		}
			

		// Write in the XML file
		save(fileName, document);
	}// End write	
	
	
	/**
	 * Writes XML file on the disk with
	 *  - if it's a project : the list of the images, all the masks descriptions and objects.
	 *  - if it's a mask : save the mask description with the references objects. 
	 *  
	 * @param fileM
	 *            Name of the XML file
	 * @param doc
	 *            The document to write.
	 */
	static void save(String fileM, Document doc) {
		try {
			XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
			// Notice that you can simply create an instance of FileOutputStream
			// with the name of the file for the serialization.
			sortie.output(doc, new FileOutputStream(fileM));
		} catch (java.io.IOException e) {
		}
	}
	
	/**
	 * Get all the image components : objects and masks. 
	 * @param image : current image
	 * @return e_image : image element
	 */
	private Element writeImageObjectsMasksAndSlits(Image image)
	{
		Element e_AllIm=new Element("image");
		writeImageAndProperties(e_AllIm,image);
		
		if (image.getNbrMasks()>0)
		{
		// Masks loop
			for (int j = 0; j < image.getNbrMasks(); j++) 
			{
				Mask mask = image.getMask(j);
				writeMaskAndProperties(e_AllIm, mask);
			}
		}
		
		if (image.getNbrObjects()>0)
		{
			Element e_objectsList = new Element("objectsList");
			e_AllIm.addContent(e_objectsList);

			// Objects loop
			for (Iterator<SkyObject> itr = image.getListObjects().iterator(); itr.hasNext();) 
			{
				SkyObject sk = itr.next();
				writeProjectObjects(e_objectsList, sk);
		
			} 
		}
		return (e_AllIm);
	}
	/**
	 * Get the image properties : center, scale, resolution, color and cut levels if there are.  
	 * @param e_image : image element
	 * @param im : current image
	 */
	private void writeImageAndProperties(Element e_image, Image im)
	{

		//Element e_image = new Element("image");

		Attribute absPath = new Attribute("absPath", im.getAbsPath());
		e_image.setAttribute(absPath);

		Attribute selectedMask = new Attribute("selectedMask", String.valueOf(im
				.getSelectedMask()));
		e_image.setAttribute(selectedMask);

		// Image name and catalog from (if there is)
		String imName=null;
		String imCat=null;
		
		Element e_imName = new Element("imageName");
		Element e_imCatalog = new Element("imageCatalog");
		imName=im.getImName();
		imCat=im.getImageCatalog();
		
		e_imName.setText(imName);
		e_imCatalog.setText(imCat);
		e_image.addContent(e_imName);
		e_image.addContent(e_imCatalog);
		
		/** Image center to reload the project */
		Element e_imCenter = new Element("center");
		Element e_imCenterX = new Element("x");
		e_imCenterX.setText(String.valueOf(im.getCenter().getX()));
		Element e_imCenterY = new Element("y");
		e_imCenterY.setText(String.valueOf(im.getCenter().getY()));
		e_imCenter.addContent(e_imCenterX);
		e_imCenter.addContent(e_imCenterY);
		e_image.addContent(e_imCenter);

		/** Image coordinates in RA-DEC J2000 */
		Element e_imCoord = new Element("coordinates");
		Element e_imCoordX = new Element("ra");
		e_imCoordX.setText(String.valueOf(im.getAlpha()));
		Element e_imCoordY = new Element("dec");
		e_imCoordY.setText(String.valueOf(im.getDelta()));
		e_imCoord.addContent(e_imCoordX);
		e_imCoord.addContent(e_imCoordY);
		e_image.addContent(e_imCoord);
		
		
		// Image scale
		Element e_scale = new Element("scale");
		e_scale.setText(String.valueOf(im.getScale()));
		e_image.addContent(e_scale);

		// Image resolution
		Element e_resol = new Element("resol");
		e_resol.setText(String.valueOf(im.getResol()));
		e_image.addContent(e_resol);
 
		// If there are cuts levels defined:
		if (String.valueOf(im.getLowCut())!= null)
		{
			Element e_lowCut = new Element("lowCut");
			e_lowCut.setText(String.valueOf(im.getLowCut()));
			e_image.addContent(e_lowCut);
		}
		
		if (String.valueOf(im.getHighCut())!= null)
		{
		Element e_highCut = new Element("highCut");
		e_highCut.setText(String.valueOf(im.getHighCut()));
		e_image.addContent(e_highCut);
		}
		
		// Test if the image colors have been modified and saved them
		if (im.colorsChanged)
		{
			String cMap = im.getColorMap();
			String cIntens=im.getColorIntensity();
			int cAlgo=im.getColorAlgorithm();
		
			if (cMap != null)
			{
				Element e_colorMap = new Element("colorMap");
				e_colorMap.setText(cMap);
				e_image.addContent(e_colorMap);
			}
			if (cIntens != null)
			{
				Element e_colorIntens = new Element("colorIntensity");
				e_colorIntens.setText(cIntens);
				e_image.addContent(e_colorIntens);
			}
			
			Element e_colorAlgo = new Element("colorAlgo");
			e_colorAlgo.setText(String.valueOf(cAlgo));
			e_image.addContent(e_colorAlgo);
		} // Fi colorChanged		
		
	}
	/**
	 * Write a mask on disk with all the slits description and the reference objects needed. 
	 * @param m - Mask
	 * @param fileName - mask file name
	 */
	
	public void writeMask(Mask m, String fileName) 
	{
		Document document = new Document(writeMaskAndProperties(m));
		
		// Write to the XML file
		save(fileName, document);
	}
	
	/**
	 * Write the mask and its properties
	 * Called when saving a mask only
	 * @param m : Current Mask
	 * @return
	 */
	private Element writeMaskAndProperties(Mask m)
	{
		boolean onlyMask=true;
		Element e_mask = new Element("mask");
		
		Attribute maskValidation = new Attribute("validated", String.valueOf(m.validated));
		e_mask.setAttribute(maskValidation);

		Attribute selectedSlit = new Attribute("selectedSlit", String.valueOf(m.getSelectedSlit()));
		e_mask.setAttribute(selectedSlit);
				
		
		String mUUID=m.getS_uuid();
		if (mUUID.isEmpty()) m.setM_uuid();
		
		Element e_maskUUID = new Element("maskUUID");
		e_maskUUID.setText(m.getS_uuid());
		e_mask.addContent(e_maskUUID);

			// Mask alpha delta Coord in degree.
		Element e_maskAlpha = new Element("alpha");
			
		e_maskAlpha.setText(String.valueOf(m.getCenterDeg().getX()));
		e_mask.addContent(e_maskAlpha);

		Element e_maskDelta = new Element("delta");
		e_maskDelta.setText(String.valueOf(m.getCenterDeg().getY()));
		e_mask.addContent(e_maskDelta);

		Element e_maskAngle = new Element("angle");
		e_maskAngle.setText(String.valueOf(m.getOmega()));
		e_mask.addContent(e_maskAngle);
		
		/** Mask coordinates RA - DEC  **/
		Element e_maskCoord = new Element("maskCoord");
		Element e_maskCoordX = new Element("ra");
		e_maskCoordX.setText(m.getAlpha().toString());
		Element e_maskCoordY = new Element("dec");
		e_maskCoordY.setText(m.getDelta().toString());
		e_maskCoord.addContent(e_maskCoordX);
		e_maskCoord.addContent(e_maskCoordY);
		e_mask.addContent(e_maskCoord);   
		
			/** Mask Coordinates RA - DEC left up corner **/
		Element e_maskCoord_leftd = new Element("maskCoordLeftCorner");
		Element e_maskCoord_leftdX = new Element("ra");
		Element e_maskCoord_leftdY = new Element("dec");
		
		if ((m.getAlphaCorner().getVal() != 0.0) || (m.getDeltaCorner().getVal() != 0.0))
		{
			e_maskCoord_leftdX.setText(m.getAlphaCorner().toString());
			e_maskCoord_leftdY.setText(m.getDeltaCorner().toString());
		}
		else 
		{
			e_maskCoord_leftdX.setText("0.0");
			e_maskCoord_leftdY.setText("0.0");
		}
			e_maskCoord_leftd.addContent(e_maskCoord_leftdX);
			e_maskCoord_leftd.addContent(e_maskCoord_leftdY);
			e_mask.addContent(e_maskCoord_leftd);
		
		
		
		 //writeRefObjects(m, e_mask);
						
		writeSlits(m, e_mask, onlyMask);
		
		return (e_mask);
		
	}
	/**
	 * Write mask from a project when saving the project.  
	 * @param e_im : parent element (image) 
	 * @param m : Current Mask
	 */
	private void writeMaskAndProperties(Element e_im, Mask m)
	{
		boolean isOnlyAMask=false;
		Element e_mask = new Element("mask");
		e_im.addContent(e_mask);
		
		Attribute maskValidation = new Attribute("validated", String.valueOf(m.validated));
		e_mask.setAttribute(maskValidation);
		
		Attribute selectedSlit = new Attribute("selectedSlit", String.valueOf(m.getSelectedSlit()));
		e_mask.setAttribute(selectedSlit);
		
//		Element e_maskUUID = new Element("maskUUID");
//		e_maskUUID.setText(m.getS_uuid());
//		e_mask.addContent(e_maskUUID);
			
			// Mask properties in the image selected
			/** Mask coordinates RA - DEC  */
			Element e_maskCoord = new Element("maskCoord");
			Element e_maskCoordX = new Element("ra");
			e_maskCoordX.setText(m.getAlpha().toString());
			Element e_maskCoordY = new Element("dec");
			e_maskCoordY.setText(m.getDelta().toString());
			e_maskCoord.addContent(e_maskCoordX);
			e_maskCoord.addContent(e_maskCoordY);
			e_mask.addContent(e_maskCoord);        		
		
			/** Mask Coordinates RA - DEC left up corner **/
			
			if ((m.getAlphaCorner().getVal() != 0.0) || (m.getDeltaCorner().getVal() != 0.0))
			{
				Element e_maskCoord_leftd = new Element("maskCoordLeftCorner");
				Element e_maskCoord_leftdX = new Element("ra");
				Element e_maskCoord_leftdY = new Element("dec");
				e_maskCoord_leftdX.setText(m.getAlphaCorner().toString());
				e_maskCoord_leftdY.setText(m.getDeltaCorner().toString());
				e_maskCoord_leftd.addContent(e_maskCoord_leftdX);
				e_maskCoord_leftd.addContent(e_maskCoord_leftdY);
				e_mask.addContent(e_maskCoord_leftd);
			}
			
			Element e_maskCenter = new Element("center");
			Element e_maskCenterX = new Element("x");
			e_maskCenterX.setText(String.valueOf(m.getCenter().getX()));
			Element e_maskCenterY = new Element("y");
			e_maskCenterY.setText(String.valueOf(m.getCenter().getY()));
			e_maskCenter.addContent(e_maskCenterX);
			e_maskCenter.addContent(e_maskCenterY);
			e_mask.addContent(e_maskCenter);
			Element e_maskAlpha = new Element("alpha");
			
			e_maskAlpha.setText(String.valueOf(m.getAlphaDouble()));
			e_mask.addContent(e_maskAlpha);

			Element e_maskDelta = new Element("delta");
			e_maskDelta.setText(String.valueOf(m.getDeltaDouble()));
			e_mask.addContent(e_maskDelta);

			Element e_maskAngle = new Element("angle");
			e_maskAngle.setText(String.valueOf(m.getOmega()));
			e_mask.addContent(e_maskAngle);	
		
				
		writeSlits(m, e_mask, isOnlyAMask);
		
	}
	
	/**
	 * Write reference objects in a mask file
	 * @param m - mask
	 * @param e_mask - xml element for the mask contain 
	 */
//	private void writeRefObjects(Mask m, Element e_mask)
//	{
//		// Reference objects loop
//				for (Iterator<SkyObject> itr = m.getRefObjectsList().iterator(); itr.hasNext();) 
//				{
//					SkyObject sk = itr.next();
//
//					Element e_refObjects = new Element("refObject");
//					e_mask.addContent(e_refObjects);
//					writeObjPosition(e_refObjects, sk, true);
//					writeObjFrom(sk, e_refObjects);
//					
//				} // End objects loop
//
//	}
	
	/**
	 * Write the slits specifications
	 * @param m : current mask
	 * @param e_mask : mask element
	 * @param onlyMask : flag if it's only a mask or a project part.
	 * If this flag is not true, an element is added if it'a affected and with the object id    
	 */
	
	private void writeSlits(Mask m, Element e_mask, boolean onlyMask)
	{
		Element e_slitsList = new Element("slitsList");
		e_mask.addContent(e_slitsList);

		// Slits loop
		for (int k = 0; k < Mask._NBRSLITS_; k++) {
			Slit curSlit=m.getSlit(k);
			Element e_slit = new Element("slit");
			
			
			/** If aperture and position != 0, then the slit is used **/
			if ((curSlit.getPosition()!= 0.0) || (curSlit.getAperture()!=0.0))
			{
				
				e_slitsList.addContent(e_slit);
				int idSlit=curSlit.getId()+1;
				Attribute id = new Attribute("id", String.valueOf(idSlit));
				e_slit.setAttribute(id);

				Element e_position = new Element("position");
				double slitPosition = curSlit.getPosition();
				e_position.setText(String.valueOf(slitPosition));
				e_slit.addContent(e_position);

				Element e_width = new Element("width");
				e_width.setText(String.valueOf(curSlit.getAperture()));
				e_slit.addContent(e_width);
			
					
				if ((slitPosition != 0.0) && (curSlit.getAperture()>0) && curSlit.fSkyCoord )
				{
					/** slit coordinates RA - DEC in HMS / DMS format   */
					Element e_slitCoord = new Element("slitCoord");
					Element e_slitCoordX = new Element("ra");
					e_slitCoordX.setText(curSlit.getWcPosCenter().getRA().toString());
					Element e_slitCoordY = new Element("dec");
					e_slitCoordY.setText(curSlit.getWcPosCenter().getDec().toString());
					e_slitCoord.addContent(e_slitCoordX);
					e_slitCoord.addContent(e_slitCoordY);
					e_slit.addContent(e_slitCoord);     
					
			
					/** slit coordinates RA - DEC in degree   */
					Element e_slitDegCoord = new Element("slitDegCoord");	
					WorldCoords wc = new WorldCoords(curSlit.getWcPosCenter().getRaDeg(),curSlit.getWcPosCenter().getDecDeg());
					Element e_slitCoordDegX = new Element("raDeg");
					e_slitCoordDegX.setText(String.valueOf(wc.getRaDeg()));
					Element e_slitCoordDegY = new Element("decDeg");
					e_slitCoordDegY.setText(String.valueOf(wc.getDecDeg()));
					e_slitDegCoord.addContent(e_slitCoordDegX);
					e_slitDegCoord.addContent(e_slitCoordDegY);  
					e_slit.addContent(e_slitDegCoord);
					
					boolean isJoined = curSlit.isJoined();
					if (isJoined)
					{
					   Element e_joined = new Element("isJoined");
					   e_joined.setText(String.valueOf(isJoined));
					   e_slit.addContent(e_joined);
					   int joinedRefSlitId =-1;
					   joinedRefSlitId = curSlit.getJoinedRefSlit()+1;
	
					   Element e_joinedSlit = new Element("joinedRefSlit");
					   e_joinedSlit.setText(String.valueOf(joinedRefSlitId));
					   e_slit.addContent(e_joinedSlit);
					  
					}
				}  
			
			
				Element e_affObject = new Element("affectedObject");
				int numObject=0;
				numObject=m.getSlit(k).getAffectedObject();
				//WorldCoords wcObj = new WorldCoords(0,0);
				
				if (numObject>0)
				{
			
					if (!onlyMask)
					{
				
						e_affObject.setText(String.valueOf(numObject));
				
					}
					else 
					{
						SkyObject sk= fov.getCurrentImage().getListObjects().get(numObject);
			
						Element e_objId = new Element("ObjectId");
						e_objId.setText(sk.getTargetId());
						e_affObject.addContent(e_objId);
						
						Element e_objCoord = new Element("objCoord");
						writeObjPosition(e_objCoord, sk, true);
						e_affObject.addContent(e_objCoord);
						
						Element e_objComments = new Element("Comments");
						writeObjFrom(sk, e_objComments);
						e_affObject.addContent(e_objComments);
						
						if (sk.isRef())
						{
							Element e_objIsRef = new Element("IsReference");
							e_objIsRef.setText("true");
							e_affObject.addContent(e_objIsRef);
						}
						
					}
					e_slit.addContent(e_affObject);
				} // Fi numObject>0
			} // Fi Slit positionned
			
		} // End slits loop
					
	}
	
	/**
	 * Write the object position in degree for the reference objects with a mask. 
	 * @param e_obj : object element.
	 * @param sk : skyObject descriptor.
	 *  @param from : 0 - from project - write position in canvas coord
	 *  			1 - from mask -write position in degrees world coord
	 */
	private void writeObjPosition(Element e_obj, SkyObject sk, boolean from)
	{
		Element e_position = new Element("position");

		Point2D.Double position = new Point2D.Double(sk.getPosPix().getX(), sk.getPosPix()
				.getY());
		// if writing a mask, conversion from displayed coord (Canvas) to world position
		if (from)
		   icc.imageToWorldCoords(position, false);

		Element e_positionX = new Element("x");
		e_positionX.setText(String.valueOf(position.x));
		e_position.addContent(e_positionX);

		Element e_positionY = new Element("y");
		e_positionY.setText(String.valueOf(position.y));
		e_position.addContent(e_positionY);

		e_obj.addContent(e_position);
		
		/** Object coordinates RA - DEC  */
		Element e_objCoord = new Element("objCoord");
		Element e_objCoordX = new Element("ra");
		e_objCoordX.setText(sk.getObjWorldCenter().getRA().toString());
		Element e_objCoordY = new Element("dec");
		e_objCoordY.setText((sk.getObjWorldCenter().getDec().toString()));
		e_objCoord.addContent(e_objCoordX);
		e_objCoord.addContent(e_objCoordY);
		e_obj.addContent(e_objCoord);        		

		if (sk.isCenterRecalc()) 
		{
			Element e_newPosition = new Element("newPosition");

			Point2D.Double newPosition = new Point2D.Double(sk.getPosPixRecalc().getX(), sk
					.getPosPixRecalc().getY());
			icc.imageToWorldCoords(newPosition, false);

			Element e_newPositionX = new Element("x");
			e_newPositionX.setText(String.valueOf(newPosition.x));
			e_newPosition.addContent(e_newPositionX);

			Element e_newPositionY = new Element("y");
			e_newPositionY.setText(String.valueOf(newPosition.y));
			e_newPosition.addContent(e_newPositionY);

			e_obj.addContent(e_newPosition);
			
			/** Object coordinates RA - DEC  */
			Element e_objNewCoord = new Element("objNewCoord");
			Element e_objNewCoordX = new Element("ra");
			e_objNewCoordX.setText(sk.getObjWorldRecalc().getRA().toString());
			Element e_objNewCoordY = new Element("dec");
			e_objNewCoordY.setText(sk.getObjWorldRecalc().getDec().toString());
			e_objNewCoord.addContent(e_objNewCoordX);
			e_objNewCoord.addContent(e_objNewCoordY);
			e_obj.addContent(e_objNewCoord);	
			
			
		}
		
		
		
	}
	
	/**
	 * Write the objects of the project
	 * The coordinates are only in the project references. 
	 * @param e_objectsList : objects element
	 * @param sk : SkyObject descriptor
	 */
	private void writeProjectObjects(Element e_objectsList, SkyObject sk )
	{
		Element e_skyObjects = new Element("skyObject");
		e_objectsList.addContent(e_skyObjects);

		/* This id is the object num in the project*/
		Attribute id = new Attribute("id", String.valueOf(sk.getNum()));
		e_skyObjects.setAttribute(id);

		/* This id is the id object when loaded from catalog */
		Attribute idCat = new Attribute("idCat", String.valueOf(sk.getTargetId()));
		e_skyObjects.setAttribute(idCat);
		
		/* This is the catalog group when loaded from catalog */
		Attribute group = new Attribute("group", String.valueOf(sk.getGroup()));
		e_skyObjects.setAttribute(group);
		
		Attribute isRef = new Attribute("isRef", String.valueOf(sk.isRef()));
		e_skyObjects.setAttribute(isRef);

		writeObjFrom(sk, e_skyObjects);

		Element e_priority = new Element("priority");
		e_priority.setText(String.valueOf(sk.getPriority()));
		e_skyObjects.addContent(e_priority);

		writeObjPosition(e_skyObjects, sk, false);
		
	}
	private void writeObjFrom(SkyObject sk, Element e_skyObjects) {
		// TODO Auto-generated method stub
		
//		Element e_from = new Element("from");
//		e_from.setText(String.valueOf(sk.getNbCat()));
//		e_skyObjects.addContent(e_from);
		
		Element e_fromLib = new Element("libelleFrom");
		String libFrom=String.valueOf(sk.getProperty());
		if (libFrom.equals("")) 
			libFrom=" ";
		e_fromLib.setText(libFrom);
		e_skyObjects.addContent(e_fromLib);
		
	}

	/**
	 * Save the image objects list in a file. 
	 * @param im : current image
	 * @param filename : created file
	 * @return
	 */
public boolean writeLocalCatalog(Image im, String filename)
{		
	boolean noError=true;
	
	File ff=null;
	ff=new File(filename);	
	FileWriter ffw=null;

	try
	{
		ffw=new FileWriter(ff);
		if (im.getNbrObjects()>0)
		{
			  SkyObject	sk;
		
			// Objects loop
			for (Iterator<SkyObject> itr = im.getListObjects().iterator(); itr.hasNext();) 
			{
			   	sk = itr.next();
				ffw.write(String.valueOf(sk.getNum()));
				ffw.write("\t");
				ffw.write(sk.getObjWorldCenter().getRA().toString());		
				ffw.write("\t");
				ffw.write(sk.getObjWorldCenter().getDec().toString());
				ffw.write("\t");
				ffw.write(String.valueOf(sk.getPriority()));
				ffw.write("\t");
				ffw.write(String.valueOf(sk.getProperty()));
				ffw.write("\n");
			  
			} 
		}
		
	    ffw.close();	
	} 
    catch (Exception e) 
	{
    	noError = false;
    	String loadError = "Error wrinting local objects catalog !";
		new CustomJDialog(loadError, CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_, pater);
  //  System.out.println("Could not create file ! ");
    }
	finally 
	{
        try 
        {
           if (ffw != null)
              ffw.close();
        } 
        catch (IOException e) 
        {
        	noError = false;
           e.printStackTrace();
        }
	
    }
	return noError;
	
}
	

/**
 * Save the image objects list in a file. 
 * @param im : current image
 * @param filename : created file
 * @return
 */
public boolean writeSlitsLocation( String fileName, String [][] slitsArray, Mask curMask, double IpaOffset)
{		
boolean noError=true;

File ff=null;
fileName=fileName+".csu";
ff=new File(fileName);	
FileWriter ffw=null;
double angle=curMask.getOmega()+ IpaOffset;
WorldCoords wc=new WorldCoords(curMask.getAlpha(),curMask.getDelta()); 
String entete="RA : "+wc.getRA().toString()+" Dec :"+wc.getDec().toString()+" Angle+offset "+String.valueOf(angle);
String uuid=curMask.getS_uuid();
entete = entete+"\n"+"# Mask_ID : "+uuid;

	try
	{
		ffw=new FileWriter(ff);
		ffw.write("# ");
		ffw.write(entete);
		ffw.write("\n# \n");
		ffw.write("# id");
		ffw.write("\n");
		ffw.write("# pos[mm]");
		ffw.write("\n");
		ffw.write("# flag: 0/1/2 - not assig. / assig./ reference ");
		ffw.write("\n");
		ffw.write("# Virt_Px_X of assigned object");
		ffw.write("\n");
		ffw.write("# Virt_Px_Y of assigned object");
		ffw.write("\n");
		ffw.write("# Real_Px_X of assigned object");
		ffw.write("\n");
		ffw.write("# Real_Px_Y of assigned object");
		ffw.write("\n");
		
		int size=slitsArray.length;

	  if (size >0)
	  {
		// slits values loop
		for (int i=0; i < size; i++) 
		{
			boolean rep=false;
			int s=Integer.valueOf(slitsArray[i][0]);

		 String num="";
		 int ref=Integer.valueOf(slitsArray[i][4]);
		 int k=0;
		 k=Integer.valueOf(slitsArray[i][13]);
		 
		 if ((k>0) &&((ref == 2) || (ref==1)))
		 {
            if (k>0)
              	if (s!=k) 
              	{
              		rep=true; // to replicate affected slit informations
              	}
		 }
			// Slit id and detector values 
		if (slitsArray[i][0].length()<2 )
			num="00"+slitsArray[i][0];
		else if (slitsArray[i][0].length()<3 )
			num="0"+slitsArray[i][0];
		ffw.write(num);
		ffw.write(" ");
		ffw.write(slitsArray[i][1]);
		ffw.write(" ");
		ffw.write(slitsArray[i][4]);
		ffw.write(" ");
		if (rep)
		{
// When joined slits, get the reference slit values
			ffw.write(slitsArray[k-1][5]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][6]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][9]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][10]);
		}
		else 
		{
			ffw.write(slitsArray[i][5]);
			ffw.write(" ");
			ffw.write(slitsArray[i][6]);
			ffw.write(" ");
			ffw.write(slitsArray[i][9]);
			ffw.write(" ");
			ffw.write(slitsArray[i][10]);
		}
		ffw.write("\n");
	// Slit Id
		 if (slitsArray[i][2].length()<3 )
			num="0"+slitsArray[i][2];
		 else num=slitsArray[i][2];
		// CSU Value 
		ffw.write(num);
		ffw.write(" ");
		ffw.write(slitsArray[i][3]);
		ffw.write(" ");
		ffw.write(slitsArray[i][4]);
		ffw.write(" ");
		if (rep)
		{
			ffw.write(slitsArray[k-1][7]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][8]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][11]);
			ffw.write(" ");
			ffw.write(slitsArray[k-1][12]);
			rep=false;
		}
		else 
		{
			ffw.write(slitsArray[i][7]);
			ffw.write(" ");
			ffw.write(slitsArray[i][8]);
			ffw.write(" ");
			ffw.write(slitsArray[i][11]);
			ffw.write(" ");
			ffw.write(slitsArray[i][12]);	
		}
		ffw.write("\n");

		}
	  }
		
		ffw.close();	
	} 
	catch (Exception e) 
	{
		noError = false;
		String loadError = "Error writing slits location file !";
		new CustomJDialog(loadError, CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_, pater);
//  System.out.println("Could not create file ! ");
	}
	finally 
	{
		try 
		{
			if (ffw != null)
				ffw.close();
		} 
		catch (IOException e) 
		{
			noError = false;
			e.printStackTrace();
		}

	}
  return noError;

  } // end writeSlitsLocation

/** 
 * Write a text file with mask description.
 * 
 * **/
public boolean writeMaskConfiguration(String fileName, Mask curMask) {

	boolean noError=true;

	File ff=null;
	fileName=fileName+".conf";
	ff=new File(fileName);	
	FileWriter ffw=null;
	double angle=curMask.getOmega();
	String uuid=curMask.getS_uuid();
	String entete = "Mask_ID : "+uuid+"\n";
	WorldCoords wc=new WorldCoords(curMask.getAlpha(),curMask.getDelta()); 
	entete=entete+"# RA "+wc.getRA().toString()+"  Dec "+wc.getDec().toString()+"  Angle "+String.valueOf(angle);
	
		try
		{
			ffw=new FileWriter(ff);
			ffw.write(entete);
			ffw.write("\n# \n");
			
			for (int k = 0; k < Mask._NBRSLITS_; k++) 
			{
				Slit sl=curMask.getSlit(k);
				boolean isJ=false;
				int idObj= -1;
				String infos="";
				idObj=sl.getAffectedObject();
				isJ=sl.isJoined();

				if ((idObj > 0 ) || (isJ))
				{			
					int v=sl.getId()+1;			
					String slitId = Integer.toString(v);
					
					infos=infos+slitId;
					
					if (isJ)
					{
		
						int i=sl.getJoinedRefSlit();
						//i++;
					    if (i!=k+1) 
					    {
					    	//Slit sref=curMask.getSlit(i-1);
					    	Slit sref=curMask.getSlit(i);
					    	int obj=sref.getAffectedObject();		
					    	if (obj>0)
					    	{
					    		SkyObject sk=fov.getCurrentImage().getObject(obj);
					    		infos=infos+" : "+sk.getTargetId()+" : "+sk.getProperty();
					    	}

					    }
					}

					if (idObj > 0 )
					{
						SkyObject sk=fov.getCurrentImage().getObject(idObj);
						infos=infos+" : "+sk.getTargetId()+" : "+sk.getProperty();					
					}
					
					ffw.write(infos);
					ffw.write("\n");
				}		
				
			}
			
			
			ffw.close();	
		} 
		catch (Exception e) 
		{
			noError = false;
			String loadError = "Error writing mask configuration file !";
			new CustomJDialog(loadError, CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_, pater);
	//  System.out.println("Could not create file ! ");
		}
		finally 
		{
			try 
			{
				if (ffw != null)
					ffw.close();
			} 
			catch (IOException e) 
			{
				noError = false;
				e.printStackTrace();
			}

		}
	  return noError;
}

}