2 /* Copyright: Thomas McGlynn 1998.
3 * This code may be used for any purpose, non-commercial
4 * or commercial so long as this copyright notice is retained
5 * in the source code or included in or referred to in any
7 * Many thanks to David Glowacki (U. Wisconsin) for substantial
8 * improvements, enhancements and bug fixes.
11 import nom.tam.util.*;
12 import java.io.IOException;
13 import java.io.EOFException;
15 /** This class instantiates FITS Random Groups data.
16 * Random groups are instantiated as a two-dimensional
17 * array of objects. The first dimension of the array
18 * is the number of groups. The second dimension is 2.
19 * The first object in every row is a one dimensional
20 * parameter array. The second element is the n-dimensional
23 public class RandomGroupsData extends Data {
25 private Object[][] dataArray;
27 /** Create the equivalent of a null data element.
29 public RandomGroupsData() {
30 dataArray = new Object[0][];
33 /** Create a RandomGroupsData object using the specified object to
34 * initialize the data array.
35 * @param x The initial data array. This should a two-d
36 * array of objects as described above.
38 public RandomGroupsData(Object[][] x) {
42 /** Get the size of the actual data element. */
43 protected long getTrueSize() {
45 if (dataArray != null && dataArray.length > 0) {
46 return (ArrayFuncs.computeLSize(dataArray[0][0]) +
47 ArrayFuncs.computeLSize(dataArray[0][1])) * dataArray.length;
53 /** Read the RandomGroupsData */
54 public void read(ArrayDataInput str) throws FitsException {
59 str.readLArray(dataArray);
60 } catch (IOException e) {
61 throw new FitsException("IO error reading Random Groups data "+e);
63 int pad = FitsUtil.padding(getTrueSize());
66 } catch (EOFException e) {
67 throw new PaddingException("EOF reading padding after random groups", this);
68 } catch (IOException e) {
69 throw new FitsException("IO error reading padding after random groups");
73 /** Write the RandomGroupsData */
74 public void write(ArrayDataOutput str) throws FitsException {
76 str.writeArray(dataArray);
77 FitsUtil.pad(str, getTrueSize());
78 } catch (IOException e) {
79 throw new FitsException("IO error writing random groups data "+e);
83 protected void fillHeader(Header h) throws FitsException {
85 if (dataArray.length <= 0 || dataArray[0].length != 2) {
86 throw new FitsException("Data not conformable to Random Groups");
89 int gcount = dataArray.length;
90 Object paraSamp = dataArray[0][0];
91 Object dataSamp = dataArray[0][1];
93 Class pbase = nom.tam.util.ArrayFuncs.getBaseClass(paraSamp);
94 Class dbase = nom.tam.util.ArrayFuncs.getBaseClass(dataSamp);
97 throw new FitsException("Data and parameters do not agree in type for random group");
100 int[] pdims = nom.tam.util.ArrayFuncs.getDimensions(paraSamp);
101 int[] ddims = nom.tam.util.ArrayFuncs.getDimensions(dataSamp);
103 if (pdims.length != 1) {
104 throw new FitsException("Parameters are not 1 d array for random groups");
107 // Got the information we need to build the header.
110 if (dbase == byte.class) {
112 } else if (dbase == short.class) {
114 } else if (dbase == int.class) {
116 } else if (dbase == long.class) { // Non-standard
118 } else if (dbase == float.class) {
120 } else if (dbase == double.class) {
123 throw new FitsException("Data type:"+dbase+" not supported for random groups");
127 h.setNaxes(ddims.length+1);
128 h.addValue("NAXIS1", 0, "ntf::randomgroupsdata:naxis1:1");
129 for (int i=2; i<=ddims.length+1; i += 1) {
130 h.addValue("NAXIS"+i, ddims[i-2], "ntf::randomgroupsdata:naxisN:1");
133 h.addValue("GROUPS", true, "ntf::randomgroupsdata:groups:1");
134 h.addValue("GCOUNT", dataArray.length, "ntf::randomgroupsdata:gcount:1");
135 h.addValue("PCOUNT", pdims[0], "ntf::randomgroupsdata:pcount:1");
138 public Object getData() {