Begin versioning.
[fits.git] / src / nom / tam / fits / FitsFactory.java
1 package nom.tam.fits;
2
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.
12  *
13  */
14 public class FitsFactory {
15
16     private static boolean useAsciiTables = true;
17     private static boolean useHierarch = false;
18     private static boolean checkAsciiStrings = false;
19
20     /** Indicate whether ASCII tables should be used
21      *  where feasible.
22      */
23     public static void setUseAsciiTables(boolean flag) {
24         useAsciiTables = flag;
25     }
26
27     /** Get the current status of ASCII table writing */
28     static boolean getUseAsciiTables() {
29         return useAsciiTables;
30     }
31
32     /** Enable/Disable hierarchical keyword processing. */
33     public static void setUseHierarch(boolean flag) {
34         useHierarch = flag;
35     }
36
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.
42      */
43     public static void setCheckAsciiStrings(boolean flag) {
44         checkAsciiStrings = flag;
45     }
46
47     /** Get the current status for string checking. */
48     static boolean getCheckAsciiStrings() {
49         return checkAsciiStrings;
50     }
51
52     /** Are we processing HIERARCH style keywords */
53     public static boolean getUseHierarch() {
54         return useHierarch;
55     }
56
57     /** Given a Header return an appropriate datum.
58      */
59     public static Data dataFactory(Header hdr) throws FitsException {
60
61         if (ImageHDU.isHeader(hdr)) {
62             Data d = ImageHDU.manufactureData(hdr);
63             hdr.afterExtend();  // Fix for positioning error noted by V. Forchi
64             return d;
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);
73         } else {
74             throw new FitsException("Unrecognizable header in dataFactory");
75         }
76
77     }
78
79     /** Given an object, create the appropriate
80      *  FITS header to describe it.
81      *  @param  o       The object to be described.
82      */
83     public static BasicHDU HDUFactory(Object o) throws FitsException {
84         Data d;
85         Header h;
86
87         if (o instanceof Header) {
88             h = (Header) o;
89             d = dataFactory(h);
90
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);
106         } else {
107             throw new FitsException("Invalid data presented to HDUFactory");
108         }
109
110         return HDUFactory(h, d);
111
112     }
113
114     /** Given Header and data objects return
115      *  the appropriate type of HDU.
116      */
117     public static BasicHDU HDUFactory(Header hdr, Data d) throws
118             FitsException {
119
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);
130         }
131
132         return null;
133     }
134 }
135
136