Reader.java
37.4 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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
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<Element> imagesElementList = fovElement.getChildren("image");
Iterator<Element> 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<Element> masksElementList = imageElement.getChildren("mask");
Iterator<Element> 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<Element> slitsList = maskElement.getChild("slitsList").getChildren("slit");
Iterator<Element> 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 (vslit<mnslit)
{
mnslit=vslit;
}
if (vslit>mxslit)
{
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<Element> objectsLists = imageElement.getChildren("objectsList");
Iterator<Element> iterator = objectsLists.iterator();
while (iterator.hasNext()) {
Element currentObjectsList = iterator.next();
List<Element> skyObjectsList = currentObjectsList.getChildren("skyObject");
Iterator<Element> 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<Element> listSlits = mask.getChild("slitsList").getChildren("slit");
Iterator<Element> 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<Element> refObjectsList = mask.getChildren("refObject");
Iterator<Element> 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;
}
}
}
}