3 /** This class contains the code which
4 * associates particular FITS types with header
5 * and data configurations. It comprises
6 * a set of Factory methods which call
7 * appropriate methods in the HDU classes.
8 * If -- God forbid -- a new FITS HDU type were
9 * created, then the XXHDU, XXData classes would
10 * need to be added and this file modified but
11 * no other changes should be needed in the FITS libraries.
14 public class FitsFactory {
16 private static boolean useAsciiTables = true;
17 private static boolean useHierarch = false;
18 private static boolean checkAsciiStrings = false;
20 /** Indicate whether ASCII tables should be used
23 public static void setUseAsciiTables(boolean flag) {
24 useAsciiTables = flag;
27 /** Get the current status of ASCII table writing */
28 static boolean getUseAsciiTables() {
29 return useAsciiTables;
32 /** Enable/Disable hierarchical keyword processing. */
33 public static void setUseHierarch(boolean flag) {
37 /** Enable/Disable checking of strings values used in tables
38 * to ensure that they are within the range specified by the
39 * FITS standard. The standard only allows the values 0x20 - 0x7E
40 * with null bytes allowed in one limited context.
41 * Disabled by default.
43 public static void setCheckAsciiStrings(boolean flag) {
44 checkAsciiStrings = flag;
47 /** Get the current status for string checking. */
48 static boolean getCheckAsciiStrings() {
49 return checkAsciiStrings;
52 /** Are we processing HIERARCH style keywords */
53 public static boolean getUseHierarch() {
57 /** Given a Header return an appropriate datum.
59 public static Data dataFactory(Header hdr) throws FitsException {
61 if (ImageHDU.isHeader(hdr)) {
62 Data d = ImageHDU.manufactureData(hdr);
63 hdr.afterExtend(); // Fix for positioning error noted by V. Forchi
65 } else if (RandomGroupsHDU.isHeader(hdr)) {
66 return RandomGroupsHDU.manufactureData(hdr);
67 } else if (useAsciiTables && AsciiTableHDU.isHeader(hdr)) {
68 return AsciiTableHDU.manufactureData(hdr);
69 } else if (BinaryTableHDU.isHeader(hdr)) {
70 return BinaryTableHDU.manufactureData(hdr);
71 } else if (UndefinedHDU.isHeader(hdr)) {
72 return UndefinedHDU.manufactureData(hdr);
74 throw new FitsException("Unrecognizable header in dataFactory");
79 /** Given an object, create the appropriate
80 * FITS header to describe it.
81 * @param o The object to be described.
83 public static BasicHDU HDUFactory(Object o) throws FitsException {
87 if (o instanceof Header) {
91 } else if (ImageHDU.isData(o)) {
92 d = ImageHDU.encapsulate(o);
93 h = ImageHDU.manufactureHeader(d);
94 } else if (RandomGroupsHDU.isData(o)) {
95 d = RandomGroupsHDU.encapsulate(o);
96 h = RandomGroupsHDU.manufactureHeader(d);
97 } else if (useAsciiTables && AsciiTableHDU.isData(o)) {
98 d = AsciiTableHDU.encapsulate(o);
99 h = AsciiTableHDU.manufactureHeader(d);
100 } else if (BinaryTableHDU.isData(o)) {
101 d = BinaryTableHDU.encapsulate(o);
102 h = BinaryTableHDU.manufactureHeader(d);
103 } else if (UndefinedHDU.isData(o)) {
104 d = UndefinedHDU.encapsulate(o);
105 h = UndefinedHDU.manufactureHeader(d);
107 throw new FitsException("Invalid data presented to HDUFactory");
110 return HDUFactory(h, d);
114 /** Given Header and data objects return
115 * the appropriate type of HDU.
117 public static BasicHDU HDUFactory(Header hdr, Data d) throws
120 if (d instanceof ImageData) {
121 return new ImageHDU(hdr, d);
122 } else if (d instanceof RandomGroupsData) {
123 return new RandomGroupsHDU(hdr, d);
124 } else if (d instanceof AsciiTable) {
125 return new AsciiTableHDU(hdr, d);
126 } else if (d instanceof BinaryTable) {
127 return new BinaryTableHDU(hdr, d);
128 } else if (d instanceof UndefinedData) {
129 return new UndefinedHDU(hdr, d);