-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTML31Create.java
More file actions
1357 lines (1292 loc) · 50.9 KB
/
RTML31Create.java
File metadata and controls
1357 lines (1292 loc) · 50.9 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
/*
Copyright 2006, Astrophysics Research Institute, Liverpool John Moores University.
This file is part of org.estar.rtml.
org.estar.rtml is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
org.estar.rtml is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with org.estar.rtml; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// RTML31Create.java
// $Header: /space/home/eng/cjm/cvs/org_estar_rtml/RTML31Create.java,v 1.12 2014-05-08 09:19:47 cjm Exp $
package org.estar.rtml;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMException;
import org.estar.astrometry.*;
/**
* This class provides the capability of creating an RTML 3.1a document from Java data,
* from an instance of RTMLDocument into a DOM tree, using JAXP.
* The resultant DOM tree is traversed,and created into a valid XML document to send to the server.
* @author Chris Mottram
* @version $Revision$
*/
public class RTML31Create
{
/**
* Revision control system version id.
*/
public final static String RCSID = "$Id$";
/**
* Default Schema location (URL).
*/
//public final static String DEFAULT_SCHEMA_URL_STRING = "http://monet.uni-sw.gwdg.de/XMLSchema/RTML/schemas/RTML-nightly.xsd";
public final static String DEFAULT_SCHEMA_URL_STRING = "http://telescope.livjm.ac.uk/rtml/RTML-nightly.xsd";
/**
* RTML Schema location. This is the URL of the RTML Schema.
* Defaults to DEFAULT_SCHEMA_URL_STRING. It is a class-wide (not per instance) variable.
* @see #DEFAULT_SCHEMA_URL_STRING
*/
private static String schemaURLString = new String(DEFAULT_SCHEMA_URL_STRING);
/**
* Private reference to org.w3c.dom.Document, the head of the DOM tree.
*/
private Document document = null;
/**
* Method to set the Schema URL String. This is the URL of the RTML Schema. e.g.:
* http://monet.uni-sw.gwdg.de/XMLSchema/RTML/schemas/RTML-nightly.xsd
* @param s The string to use. This is the URL of the RTML Schema.
* @see #DEFAULT_SCHEMA_URL_STRING
* @see #schemaURLString
*/
public static void setSchemaURLString(String s)
{
schemaURLString = s;
}
/**
* Default constructor.
* @exception Exception Thrown if init fails.
*/
public RTML31Create()
{
super();
}
/**
* Create an XML representation (DOM tree) from the RTMLDocument (Java object tree).
* @param rtmlDocument The Java representation of an RTML document.
* @param doc The XML DOM document being created from the Java representation.
* @see #document
* @see #createRTML
*/
public void create(RTMLDocument rtmlDocument,Document doc) throws RTMLException
{
document = doc;
createRTML(rtmlDocument);
}
// private methods
/**
* Create RTML element, and add to document.
* @param d The Java representation of an RTML document.
* @see #document
* @see #schemaURLString
* @see #createProject
* @see #createTelescope
* @see #createIntelligentAgent
* @see #createDevice
* @see #createTarget
* @see #createObservation
* @see #createScoring
*/
private void createRTML(RTMLDocument d)
{
RTMLObservation obs = null;
RTMLIntelligentAgent ia = null;
Element rtmlElement = null;
Element respondToElement = null;
rtmlElement = (Element)document.createElement("RTML");
document.appendChild(rtmlElement);
rtmlElement.setAttribute("version",d.getVersion());
if(d.getMode() != null)
rtmlElement.setAttribute("mode",d.getMode());
// add various scheme namespaces
rtmlElement.setAttribute("xmlns","http://www.rtml.org/v3.1a");
rtmlElement.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
rtmlElement.setAttribute("xsi:schemaLocation", schemaURLString);
// This is the mapping from the namespace to it's schema
// The www.rtml.org domain is now owned by someone else, but here it functions
// only as a namespace identifier and it not resolved/contacted during document parsing
// so we think it's still OK to use it here
rtmlElement.setAttribute("xsi:schemaLocation","http://www.rtml.org/v3.1a "+schemaURLString);
// uid - must exist in RTML
if(d.getUId() != null)
rtmlElement.setAttribute("uid",d.getUId());
else
rtmlElement.setAttribute("uid","");
createHistory(rtmlElement,d.getHistory());
if(d.getDevice() != null)
createDevice(rtmlElement,d.getDevice());
if(d.getTarget() != null)
createTarget(rtmlElement,d.getTarget());
if(d.getTelescope() != null)
createTelescope(rtmlElement,d.getTelescope());
// Project (which contains Contact in RTML 3.1a
if(d.getProject() != null)
createProject(rtmlElement,d);
// RespondTo (from Intelligent Agent URI)
if(d.getIntelligentAgent() != null)
{
ia = d.getIntelligentAgent();
if(ia.getUri() != null)
{
respondToElement = (Element)document.createElement("RespondTo");
respondToElement.appendChild(document.createTextNode(ia.getUri()));
rtmlElement.appendChild(respondToElement);
}
else if(ia.getHostname() != null)
{
respondToElement = (Element)document.createElement("RespondTo");
respondToElement.appendChild(document.createTextNode("http://"+ia.getHostname()+":"+
ia.getPort()+"/"));
rtmlElement.appendChild(respondToElement);
}
}
for(int i = 0; i < d.getObservationListCount(); i++)
{
obs = d.getObservation(i);
createObservation(rtmlElement,obs);
}
// Scoring
if(d.getScoresListCount() > 0)
createScoring(rtmlElement,d);
}
/**
* Create a history node and associated sub-elements.
* @param rtmlElement The RTML DOM element to add the History tag to.
* @param history The Java object containing the history data to add.
* @see org.estar.rtml.RTMLHistory
*/
private void createHistory(Element rtmlElement,RTMLHistory history)
{
Element historyElement = null;
Element entryElement = null;
Element subElement = null;
Element subSubElement = null;
RTMLHistoryEntry entry = null;
RTMLDateFormat dateFormat = null;
dateFormat = new RTMLDateFormat();
// create History
historyElement = (Element)document.createElement("History");
if(history != null)
{
for(int i = 0; i< history.getEntryListCount(); i++)
{
entry = history.getEntry(i);
// entry element
entryElement = (Element)document.createElement("Entry");
// time stamp
if(entry.getTimeStamp() != null)
{
entryElement.setAttribute("timeStamp",
dateFormat.formatWithColonTimezone(entry.getTimeStamp()));
}
// agent
if(entry.getAgent() != null)
{
subElement = (Element)document.createElement("Agent");
if(entry.getAgent().getId() != null)
subElement.setAttribute("name",entry.getAgent().getId());
if(entry.getAgent().getUri() != null)
subElement.setAttribute("uri",entry.getAgent().getUri());
entryElement.appendChild(subElement);
}
// descrption
if(entry.getDescription() != null)
{
subElement = (Element)document.createElement("Description");
subElement.appendChild(document.createTextNode(entry.getDescription()));
entryElement.appendChild(subElement);
}
// error
if(entry.getError() != null)
{
subElement = (Element)document.createElement("Error");
subElement.appendChild(document.createTextNode(entry.getError()));
entryElement.appendChild(subElement);
}
// rejection
if(entry.getRejectionReason() != null)
{
subElement = (Element)document.createElement("Rejection");
subElement.setAttribute("reason",entry.getRejectionReason());
if(entry.getRejectionDescription() != null)
{
subSubElement = (Element)document.createElement("Description");
subSubElement.appendChild(document.createTextNode(
entry.getRejectionDescription()));
subElement.appendChild(subSubElement);
}
entryElement.appendChild(subElement);
}
// version
if(entry.getVersion() != 0)
{
subElement = (Element)document.createElement("Version");
subElement.appendChild(document.createTextNode(""+entry.getVersion()));
entryElement.appendChild(subElement);
}
// Add Entry to History
historyElement.appendChild(entryElement);
}
}
// add history to RTML
rtmlElement.appendChild(historyElement);
}
/**
* Create RTML Project node.
* @param rtmlElement The RTML DOM element to add the Project tag to.
* @param rtmlDocument The Java document object containing the project data to add.
* @see org.estar.rtml.RTMLProject
* @see #createContact
*/
private void createProject(Element rtmlElement,RTMLDocument rtmlDocument)
{
RTMLContact contact = null;
RTMLProject project = null;
Element projectElement = null;
// get project from document
project = rtmlDocument.getProject();
// Project
projectElement = (Element)document.createElement("Project");
if((project != null) && (project.getProject() != null))
projectElement.setAttribute("ProjectID",project.getProject());
// if contact exists, add to project
if(rtmlDocument.getContact() != null)
createContact(projectElement,rtmlDocument.getContact());
// add project to document
rtmlElement.appendChild(projectElement);
}
/**
* Create a contact node and associated sub-elements.
* @param rtmlElement The RTML Project DOM element to add the Contact tag to.
* @param contact The Java object containing the contact data to add.
* @see org.estar.rtml.RTMLContact
*/
private void createContact(Element rtmlElement, RTMLContact contact)
{
Element contactElement = null;
Element communicationElement = null;
Element userNameElement = null;
Element subElement = null;
int index;
contactElement = (Element)document.createElement("Contact");
// We can't use contact.getName() here, as RTML3.1a requires a first and last name
// Contact->Username must be reserved for the Contact User.
// Contact User
if(contact.getUser() != null)
{
userNameElement = (Element)document.createElement("Username");
userNameElement.appendChild(document.createTextNode(contact.getUser()));
contactElement.appendChild(userNameElement);
}
// Contact Name
if(contact.getName() != null)
{
// Name
subElement = (Element)document.createElement("Name");
subElement.appendChild(document.createTextNode(contact.getName()));
// add Name to Contact
contactElement.appendChild(subElement);
}
// Contact Institution
if(contact.getInstitution() != null)
{
subElement = (Element)document.createElement("Institution");
subElement.setAttribute("name",contact.getInstitution());
contactElement.appendChild(subElement);
}
// communication
communicationElement = (Element)document.createElement("Communication");
if(contact.getAddress() != null)
{
subElement = (Element)document.createElement("AddressLine");
subElement.appendChild(document.createTextNode(contact.getAddress()));
communicationElement.appendChild(subElement);
}
if(contact.getTelephone() != null)
{
subElement = (Element)document.createElement("Telephone");
subElement.appendChild(document.createTextNode(contact.getTelephone()));
communicationElement.appendChild(subElement);
}
if(contact.getFax() != null)
{
subElement = (Element)document.createElement("Fax");
subElement.appendChild(document.createTextNode(contact.getFax()));
communicationElement.appendChild(subElement);
}
if(contact.getEmail() != null)
{
subElement = (Element)document.createElement("Email");
subElement.appendChild(document.createTextNode(contact.getEmail()));
communicationElement.appendChild(subElement);
}
if(contact.getUrl() != null)
{
subElement = (Element)document.createElement("Uri");
subElement.appendChild(document.createTextNode(contact.getUrl().toString()));
communicationElement.appendChild(subElement);
}
// Add communication to contact
contactElement.appendChild(communicationElement);
// Add contact to project (rtmlElement)
rtmlElement.appendChild(contactElement);
}
/**
* Create RTML Telescope node.
* @param rtmlElement The RTML DOM element to add the Telescope tag to.
* @param telescope The Java object containing the telescope data to add.
* @see #createTelescopeLocation
* @see org.estar.rtml.RTMLTelescope
* @see org.estar.rtml.RTMLTelescope#getName
* @see org.estar.rtml.RTMLTelescope#getAperture
* @see org.estar.rtml.RTMLTelescope#getApertureMeters
* @see org.estar.rtml.RTMLTelescope#getApertureUnits
* @see org.estar.rtml.RTMLTelescope#getApertureType
* @see org.estar.rtml.RTMLTelescope#getFocalRatio
* @see org.estar.rtml.RTMLTelescope#getFocalLength
* @see org.estar.rtml.RTMLTelescope#getFocalLengthMeters
* @see org.estar.rtml.RTMLTelescope#getFocalLengthUnits
*/
private void createTelescope(Element rtmlElement,RTMLTelescope telescope)
{
Element telescopeElement = null;
Element subElement = null;
DecimalFormat nf = null;
nf = new DecimalFormat("#####0.0#");
telescopeElement = (Element)document.createElement("Telescope");
if(telescope.getName() != null)
{
telescopeElement.setAttribute("name",telescope.getName());
}
if((telescope.getAperture() != 0.0)&&(telescope.getApertureUnits() != null))
{
// NB Aperture units are fixed to "meters" in RTML 3.1a.
subElement = (Element)document.createElement("Aperture");
subElement.appendChild(document.createTextNode(nf.format(telescope.getApertureMeters())));
subElement.setAttribute("units","meters");
if(telescope.getApertureType() != null)
subElement.setAttribute("type",telescope.getApertureType());
telescopeElement.appendChild(subElement);
}
if(telescope.getFocalRatio() != null)
{
subElement = (Element)document.createElement("FocalRatio");
subElement.appendChild(document.createTextNode(telescope.getFocalRatio()));
telescopeElement.appendChild(subElement);
}
if((telescope.getFocalLength() != 0.0)&&(telescope.getFocalLengthUnits() != null))
{
// NB FocalLength units are fixed to "meters" in RTML 3.1a.
subElement = (Element)document.createElement("FocalLength");
subElement.appendChild(document.createTextNode(nf.format(telescope.getFocalLengthMeters())));
subElement.setAttribute("units","meters");
telescopeElement.appendChild(subElement);
}
if(telescope.getLocation() != null)
createTelescopeLocation(telescopeElement,telescope.getLocation());
rtmlElement.appendChild(telescopeElement);
}
/**
* Create RTML Telescope Location node.
* @param rtmlElement The RTML DOM element to add the Telescope Location tag to.
* @param telescopeLocation The Java object containing the telescope location data to add.
* @see org.estar.rtml.RTMLTelescopeLocation
* @see org.estar.rtml.RTMLTelescopeLocation#getName
* @see org.estar.rtml.RTMLTelescopeLocation#getLongitude
* @see org.estar.rtml.RTMLTelescopeLocation#getLatitude
* @see org.estar.rtml.RTMLTelescopeLocation#getAltitude
*/
private void createTelescopeLocation(Element rtmlElement,RTMLTelescopeLocation telescopeLocation)
{
Element telescopeLocationElement = null;
Element subElement = null;
DecimalFormat nf = null;
nf = new DecimalFormat("#####0.0#");
telescopeLocationElement = (Element)document.createElement("Location");
if(telescopeLocation.getName() != null)
{
telescopeLocationElement.setAttribute("name",telescopeLocation.getName());
}
if(telescopeLocation.getLongitude() != 0.0)
{
subElement = (Element)document.createElement("EastLongitude");
subElement.setAttribute("units","degrees");
subElement.appendChild(document.createTextNode(nf.format(telescopeLocation.getLongitude())));
telescopeLocationElement.appendChild(subElement);
}
if(telescopeLocation.getLatitude() != 0.0)
{
subElement = (Element)document.createElement("Latitude");
subElement.setAttribute("units","degrees");
subElement.appendChild(document.createTextNode(nf.format(telescopeLocation.getLatitude())));
telescopeLocationElement.appendChild(subElement);
}
if(telescopeLocation.getAltitude() != 0.0)
{
subElement = (Element)document.createElement("Height");
subElement.setAttribute("units","meters");
subElement.appendChild(document.createTextNode(nf.format(telescopeLocation.getAltitude())));
telescopeLocationElement.appendChild(subElement);
}
rtmlElement.appendChild(telescopeLocationElement);
}
/**
* Create the Intelligent Agent node.
*/
private void createIntelligentAgent(Element rtmlElement,RTMLIntelligentAgent rtmlIA)
{
Element iaElement = null;
iaElement = (Element)document.createElement("IntelligentAgent");
if(rtmlIA.getHostname() != null)
iaElement.setAttribute("host",rtmlIA.getHostname());
if(rtmlIA.getPort() != 0)
iaElement.setAttribute("port",""+rtmlIA.getPort());
if(rtmlIA.getId() != null)
iaElement.appendChild(document.createTextNode(rtmlIA.getId()));
rtmlElement.appendChild(iaElement);
}
/**
* Method to create the Device tags.
* Create a Device node and associated sub-elements.
* @param rtmlElement The RTML DOM element to add the Contact tag to.
* @param device The Java object containing the device (instrument) data to add.
* @see org.estar.rtml.RTMLDevice
* @see #createDetector
* @see #createGrating
* @see #createHalfWavePlateSubDevice
*/
private void createDevice(Element rtmlElement,RTMLDevice device)
{
Element deviceElement = null;
Element spectralRegionElement = null;
Element setupElement = null;
Element filterElement = null;
Element filterTypeElement = null;
Element centerElement = null;
deviceElement = (Element)document.createElement("Device");
// RTML 3.1a name and type (DeviceTypes)
if(device.getType() != null)
deviceElement.setAttribute("type",device.getType());
if(device.getName() != null)
deviceElement.setAttribute("name",device.getName());
// spectral region
if(device.getSpectralRegion() != null)
{
spectralRegionElement = (Element)document.createElement("SpectralRegion");
spectralRegionElement.appendChild(document.createTextNode(device.getSpectralRegion()));
deviceElement.appendChild(spectralRegionElement);
}
// create setup containing filter/detector/grating etc
setupElement = (Element)document.createElement("Setup");
// filter
if(device.getFilterType() != null)
{
// filter node/tag
filterElement = (Element)document.createElement("Filter");
filterElement.setAttribute("type",device.getFilterType());
setupElement.appendChild(filterElement);
}
// detector
if(device.getDetector() != null)
createDetector(setupElement,device.getDetector());
// grating
if(device.getGrating() != null)
{
// For FrodoSpec we can use the Grating name.
if(device.getGrating().getName() != null)
{
createGrating(setupElement,device.getGrating());
}
// for Meaburn we want to encapsulate the central wavelength data here
// RTML 3.1a Filter has a Central wavelength mode.
if(device.getGrating().getWavelength() != 0.0f)
{
// filter node/tag
filterElement = (Element)document.createElement("Filter");
centerElement = (Element)document.createElement("Center");
centerElement.setAttribute("units",device.getGrating().getWavelengthUnits());
centerElement.appendChild(document.createTextNode(""+
device.getGrating().getWavelengthString()));
filterElement.appendChild(centerElement);
setupElement.appendChild(filterElement);
}
}
// half-wave plate (Moptop rotor speed)
if(device.getHalfWavePlate() != null)
{
createHalfWavePlateSubDevice(setupElement,device.getHalfWavePlate());
}
// add Setup to Device
deviceElement.appendChild(setupElement);
// Add Device to RTML/Schedule
rtmlElement.appendChild(deviceElement);
}
/**
* Method to create the Detector tags.
* Create a detector node and associated sub-elements.
* @param rtmlElement The RTML DOM element (Setup) to add the Detector tag to.
* @param detector The Java object containing the detector (instrument) data to add.
* @see org.estar.rtml.RTMLDetector
* @see #createDevice
*/
private void createDetector(Element rtmlElement,RTMLDetector detector)
{
Element detectorElement = null;
Element binningElement = null;
Element xyElement = null;
Element gainElement = null;
Element descriptionElement = null;
DecimalFormat df = null;
df = new DecimalFormat("###0.0#");
detectorElement = (Element)document.createElement("Detector");
// binning node/tag
binningElement = (Element)document.createElement("Binning");
detectorElement.appendChild(binningElement);
// X
xyElement = (Element)document.createElement("X");
xyElement.appendChild(document.createTextNode(""+detector.getColumnBinning()));
xyElement.setAttribute("units","pixels");
binningElement.appendChild(xyElement);
// Y
xyElement = (Element)document.createElement("Y");
xyElement.appendChild(document.createTextNode(""+detector.getRowBinning()));
xyElement.setAttribute("units","pixels");
binningElement.appendChild(xyElement);
// gain
if(detector.getUseGain())
{
gainElement = (Element)document.createElement("Gain");
// We should really put the gain in the 'Value' element.
// But the 'Value' element has a fixed 'units' attribute of 'electrons_per_adu'
// We want to specify the EMCCD Gain, which as far as I know, does not use these units.
// Therefore stuff the value into the 'Description' element instead.
// This is reflected in the corresponding parser code which expects the value to be hidden here
// (RTML31Parser.java:parseGainNode)
descriptionElement = (Element)document.createElement("Description");
descriptionElement.appendChild(document.createTextNode(""+df.format(detector.getGain())));
gainElement.appendChild(descriptionElement);
detectorElement.appendChild(gainElement);
}
// append Detector to parent Device element
rtmlElement.appendChild(detectorElement);
}
/**
* Method to create the Grating tags.
* Create a grating node and associated attributes.
* @param rtmlElement The RTML DOM element or Setup to add the Grating tag to.
* @param grating The Java object containing the grating (instrument) data to add.
* @see #createDevice
* @see org.estar.rtml.RTMLGrating
* @see org.estar.rtml.RTMLGrating#getName
* @see org.estar.rtml.RTMLGrating#getWavelengthString
* @see org.estar.rtml.RTMLGrating#getWavelengthUnits
* @see org.estar.rtml.RTMLGrating#getResolutionString
* @see org.estar.rtml.RTMLGrating#getAngleString
*/
private void createGrating(Element rtmlElement,RTMLGrating grating)
{
Element gratingElement = null;
gratingElement = (Element)document.createElement("Grating");
if(grating.getName() != null)
gratingElement.setAttribute("name",grating.getName());
if(grating.getWavelength() != 0.0)
gratingElement.setAttribute("wavelength",grating.getWavelengthString());
if(grating.getWavelengthUnits() != null)
gratingElement.setAttribute("units",grating.getWavelengthUnits());
if(grating.getResolution() != 0.0)
gratingElement.setAttribute("resolution",grating.getResolutionString());
if(grating.getAngle() != 0.0)
gratingElement.setAttribute("angle",grating.getAngleString());
rtmlElement.appendChild(gratingElement);
}
/**
* Method to create a half-wave plate Device (as a sub-device of the Device tag)
* and set a rotorSpeed attribute to encode the Moptop rotator speed.
* @param rtmlElement The RTML DOM Device element to add the half-wave plate sub-Device to.
* @param halfWavePlate The Java object containing the half-wave plate data to add.
* @see #createDevice
* @see org.estar.rtml.RTMLHalfWavePlate
* @see org.estar.rtml.RTMLHalfWavePlate#rotorSpeedToString
*/
private void createHalfWavePlateSubDevice(Element rtmlElement,RTMLHalfWavePlate halfWavePlate)
{
Element subDeviceElement = null;
subDeviceElement = (Element)document.createElement("Device");
subDeviceElement.setAttribute("type","half-wave_plate");
subDeviceElement.setAttribute("rotorSpeed",halfWavePlate.rotorSpeedToString());
rtmlElement.appendChild(subDeviceElement);
}
/**
* Method to create XML in the Observation tag.
* Our document model has Observation's, each Containing one Schedule.
* However, RTML3.1a has the main RTML element containing Schedule elements, each containing one or
* more Observation elements.
* In our document mode, each Observation can have multiple ImageData's for each FITS image
* generated by one observation (which may have SeriesConstraint data).
* However, in RTML3.1a the data in ImageData has to be represented in a ImageData tag <b>and</b>
* a SourceCatalogue tag.
* Therefore, this method creates a Schedule element. It puts an Observation element in for each Image data.
* @param rtmlElement The RTML element to add the Observation tag to.
* @param observation The Java object containing the observation data to add.
* @see #document
* @see #createTarget
* @see #createDevice
* @see #createSchedule
* @see #createImageData
* @see #createSourceCatalogue
*/
private void createObservation(Element rtmlElement,RTMLObservation observation)
{
Element scheduleElement = null;
Element observationElement = null;
RTMLImageData imageData = null;
// RTML 3.1a requires Observation to be inside Schedule
scheduleElement = (Element)document.createElement("Schedule");
if(observation.getTarget() != null)
createTarget(scheduleElement,observation.getTarget());
if(observation.getDevice() != null)
createDevice(scheduleElement,observation.getDevice());
if(observation.getSchedule() != null)
createSchedule(scheduleElement,observation.getSchedule());
// for each ImageData
for(int i = 0; i < observation.getImageDataCount(); i++)
{
imageData = observation.getImageData(i);
// Create an Observation
observationElement = (Element)document.createElement("Observation");
// Create ImageData within Observation
createImageData(observationElement,imageData);
// Create SourceCatalogue within Observation
createSourceCatalogue(observationElement,imageData);
// Add Observation to Schedule
scheduleElement.appendChild(observationElement);
}
// Add Schedule to parent
rtmlElement.appendChild(scheduleElement);
}
/**
* Create a Target element and append it to the specified Observation Schedule/RTML element.
* @param parentElement The parent element to append the new Target element to, probably a Schedule
* or could be an RTML element.
* @param target The RTMLTarget object containing target information.
* @see RTMLTarget
*/
private void createTarget(Element parentElement,RTMLTarget target)
{
Element targetElement = null;
Element coordElement = null;
Element raElement = null;
Element decElement = null;
Element equinoxElement = null;
Element brightnessElement = null;
Element subElement = null;
String s = null;
DecimalFormat df = null;
df = new DecimalFormat("##0.0#");
// target element
targetElement = (Element)document.createElement("Target");
// type attribute does not exist in RTML3.1a - perhaps try Priority sub-element?
//targetElement.setAttribute("type",target.getType());
// ident attribute - map to RTML3.1a RTMLAttribute 'id'
if(target.getIdent() != null)
targetElement.setAttribute("id",target.getIdent());
// target name attribute
if(target.getName() != null)
targetElement.setAttribute("name",target.getName());
// coordinate sub-element
coordElement = (Element)document.createElement("Coordinates");
// add coordinate sub-element to target element
targetElement.appendChild(coordElement);
// ra sub-sub element
if(target.getRA() != null)
{
raElement = (Element)document.createElement("RightAscension");
// hours
subElement = (Element)document.createElement("Hours");
subElement.appendChild(document.createTextNode(""+target.getRA().getHours()));
raElement.appendChild(subElement);
// minutes
subElement = (Element)document.createElement("Minutes");
subElement.appendChild(document.createTextNode(""+target.getRA().getMinutes()));
raElement.appendChild(subElement);
// seconds
subElement = (Element)document.createElement("Seconds");
subElement.appendChild(document.createTextNode(df.format(target.getRA().getSeconds())));
raElement.appendChild(subElement);
// offset
subElement = (Element)document.createElement("Offset");
subElement.setAttribute("units","arcseconds");
subElement.appendChild(document.createTextNode(df.format(target.getRAOffset())));
raElement.appendChild(subElement);
// add ra element to coordElement.
coordElement.appendChild(raElement);
}
// dec sub-sub element
if(target.getDec() != null)
{
decElement = (Element)document.createElement("Declination");
// degrees
subElement = (Element)document.createElement("Degrees");
if(target.getDec().getNegative())
s = new String("-");
else
s = new String("+");
// getDegrees always returns +'ve, we use getNegative to determine the sign
s = new String(s+target.getDec().getDegrees());
subElement.appendChild(document.createTextNode(s));
decElement.appendChild(subElement);
// minutes
subElement = (Element)document.createElement("Arcminutes");
subElement.appendChild(document.createTextNode(""+target.getDec().getMinutes()));
decElement.appendChild(subElement);
// seconds
subElement = (Element)document.createElement("Arcseconds");
subElement.appendChild(document.createTextNode(df.format(target.getDec().getSeconds())));
decElement.appendChild(subElement);
// offset
subElement = (Element)document.createElement("Offset");
subElement.setAttribute("units","arcseconds");
subElement.appendChild(document.createTextNode(df.format(target.getDecOffset())));
decElement.appendChild(subElement);
// add dec element to coordElement.
coordElement.appendChild(decElement);
}
if(target.getEquinox() != null)
{
equinoxElement = (Element)document.createElement("Equinox");
equinoxElement.appendChild(document.createTextNode(target.getEquinox()));
// add equinox element to coordElement.
coordElement.appendChild(equinoxElement);
}
// System = FK5 or ICRS
subElement = (Element)document.createElement("System");
subElement.appendChild(document.createTextNode("FK5"));
// diddly subElement never added here!
// target magnitude
if(target.getMagnitudeFilterType() != null)
{
// TargetBrightness
brightnessElement = (Element)document.createElement("TargetBrightness");
// Magnitude
subElement = (Element)document.createElement("Magnitude");
subElement.appendChild(document.createTextNode(df.format(target.getMagnitude())));
brightnessElement.appendChild(subElement);
// Type (filter)
subElement = (Element)document.createElement("Type");
subElement.appendChild(document.createTextNode(target.getMagnitudeFilterType()));
brightnessElement.appendChild(subElement);
// Error
if(target.getMagnitudeError() != 0.0)
{
subElement = (Element)document.createElement("Error");
subElement.appendChild(document.createTextNode(df.format(target.getMagnitudeError())));
brightnessElement.appendChild(subElement);
}
// append TargetBrightness to Target
targetElement.appendChild(brightnessElement);
}
// acquisition
if(target.getAcquisition() != null)
{
// Acquisition
subElement = (Element)document.createElement("Acquisition");
subElement.appendChild(document.createTextNode(target.getAcquisition().getAcquisitionMode()));
targetElement.appendChild(subElement);
}
// add target to the parent
parentElement.appendChild(targetElement);
}
/**
* Create a Schedule tag details. The Schedule element itself was created in createObservation.
* This is because the document object model has Observation containing Schedule, whilst in RTML 3.1a
* Schedule contains Observation.
* @param scheduleElement The schedule XML node to add the schedule details to.
* @param schedule The RTML schedule data.
* @see #createTimeConstraint
* @see #createSeriesConstraint
* @see #createSeeingConstraint
* @see #createMoonConstraint
* @see #createSkyConstraint
* @see #createExtinctionConstraint
* @see #createAirmassConstraint
* @see RTMLSchedule
*/
private void createSchedule(Element scheduleElement,RTMLSchedule schedule)
{
Element priorityElement = null;
Element exposureElement = null;
Element exposureConstraintElement = null;
Element valueElement = null;
Element exposureCountElement = null;
Element snrElement = null;
DecimalFormat df = null;
df = new DecimalFormat("#####0.0##");
// schedule priority element
priorityElement = (Element)document.createElement("Priority");
priorityElement.appendChild(document.createTextNode(""+schedule.getPriority()));
scheduleElement.appendChild(priorityElement);
// if type of exposure
if(schedule.isExposureTypeSNR())
{
// exposure constraint (type == snr)
exposureConstraintElement = (Element)document.createElement("ExposureConstraint");
// count is a sub-element in constraint!
exposureCountElement = (Element)document.createElement("Count");
exposureCountElement.appendChild(document.createTextNode(""+schedule.getExposureCount()));
exposureConstraintElement.appendChild(exposureCountElement);
// snr - minimum
snrElement = (Element)document.createElement("MinimumSignalToNoise");
exposureConstraintElement.appendChild(document.createTextNode(
df.format(schedule.getExposureLength())));
exposureConstraintElement.appendChild(snrElement);
// snr - maximum
snrElement = (Element)document.createElement("MaximumSignalToNoise");
exposureConstraintElement.appendChild(document.createTextNode(
df.format(schedule.getExposureLength())));
exposureConstraintElement.appendChild(snrElement);
// add exposure to a schedule
scheduleElement.appendChild(exposureConstraintElement);
}
else // getExposureType == "time"
{
// exposure element
exposureElement = (Element)document.createElement("Exposure");
exposureElement.setAttribute("count",""+schedule.getExposureCount());
// exposure value sub-element
valueElement = (Element)document.createElement("Value");
// exposure units are fixed as "seconds" in RTML 3.1
valueElement.setAttribute("units","seconds");
valueElement.appendChild(document.createTextNode(df.format(
schedule.getExposureLengthSeconds())));
exposureElement.appendChild(valueElement);
// add exposure to a schedule
scheduleElement.appendChild(exposureElement);
}
// TimeConstraint
if((schedule.getStartDate() != null) || (schedule.getEndDate() != null))
createTimeConstraint(scheduleElement,schedule);
// SeriesConstraint
if(schedule.getSeriesConstraint() != null)
createSeriesConstraint(scheduleElement,schedule.getSeriesConstraint());
// SeeingConstraint
if(schedule.getSeeingConstraint() != null)
createSeeingConstraint(scheduleElement,schedule.getSeeingConstraint());
// MoonConstraint
if(schedule.getMoonConstraint() != null)
createMoonConstraint(scheduleElement,schedule.getMoonConstraint());
// SkyConstraint
if(schedule.getSkyConstraint() != null)
createSkyConstraint(scheduleElement,schedule.getSkyConstraint());
// ExtinctionConstraint
if(schedule.getExtinctionConstraint() != null)
createExtinctionConstraint(scheduleElement,schedule.getExtinctionConstraint());
// AirmassConstraint
if(schedule.getAirmassConstraint() != null)
createAirmassConstraint(scheduleElement,schedule.getAirmassConstraint());
}
/**
* Create a TimeConstraint tag.
* @param scheduleElement The schedule XML node to add the schedule to.
* @param schedule The RTML schedule data (which contains time constrint data).
* @see RTMLSchedule
* @see RTMLDateFormat
*/
private void createTimeConstraint(Element scheduleElement,RTMLSchedule schedule)
{
Element timeConstraintElement = null;
Element dateTimeElement = null;
RTMLDateFormat dateFormat = null;
dateFormat = new RTMLDateFormat();
// schedule element
timeConstraintElement = (Element)document.createElement("DateTimeConstraint");
timeConstraintElement.setAttribute("type","include");
// start date time element
if(schedule.getStartDate() != null)
{
dateTimeElement = (Element)document.createElement("DateTimeStart");
dateTimeElement.setAttribute("value",
dateFormat.formatWithColonTimezone(schedule.getStartDate()));
dateTimeElement.setAttribute("system","UT");
// add dateTimeElement to a timeConstraintElement
timeConstraintElement.appendChild(dateTimeElement);
}
// end date time element
if(schedule.getEndDate() != null)
{
dateTimeElement = (Element)document.createElement("DateTimeEnd");
dateTimeElement.setAttribute("value",
dateFormat.formatWithColonTimezone(schedule.getEndDate()));
dateTimeElement.setAttribute("system","UT");
// add dateTimeElement to a timeConstraintElement
timeConstraintElement.appendChild(dateTimeElement);
}
// add timeConstraintElement to a scheduleElement
scheduleElement.appendChild(timeConstraintElement);
}