1 package nom.tam.fits.compression.provider.param.base;
2
3 import nom.tam.fits.BinaryTable;
4 import nom.tam.fits.BinaryTableHDU;
5 import nom.tam.fits.FitsException;
6 import nom.tam.fits.Header;
7 import nom.tam.fits.HeaderCard;
8 import nom.tam.fits.HeaderCardException;
9 import nom.tam.fits.compression.provider.param.api.ICompressColumnParameter;
10 import nom.tam.fits.compression.provider.param.api.ICompressHeaderParameter;
11 import nom.tam.fits.compression.provider.param.api.ICompressParameters;
12
13 /*
14 * #%L
15 * nom.tam FITS library
16 * %%
17 * Copyright (C) 1996 - 2024 nom-tam-fits
18 * %%
19 * This is free and unencumbered software released into the public domain.
20 *
21 * Anyone is free to copy, modify, publish, use, compile, sell, or
22 * distribute this software, either in source code form or as a compiled
23 * binary, for any purpose, commercial or non-commercial, and by any
24 * means.
25 *
26 * In jurisdictions that recognize copyright laws, the author or authors
27 * of this software dedicate any and all copyright interest in the
28 * software to the public domain. We make this dedication for the benefit
29 * of the public at large and to the detriment of our heirs and
30 * successors. We intend this dedication to be an overt act of
31 * relinquishment in perpetuity of all present and future rights to this
32 * software under copyright law.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
38 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
39 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
40 * OTHER DEALINGS IN THE SOFTWARE.
41 * #L%
42 */
43
44 import static nom.tam.fits.header.Standard.TTYPEn;
45
46 /**
47 * (<i>for internal use</i>) A set of {@link CompressParameter}s that are bundled together, typically because they are
48 * parameters that all link to the same {@link nom.tam.fits.compression.algorithm.api.ICompressOption}
49 *
50 * @see CompressParameter
51 */
52 public abstract class CompressParameters implements ICompressParameters, Cloneable {
53
54 @Override
55 public void addColumnsToTable(BinaryTableHDU hdu) throws FitsException {
56 for (ICompressColumnParameter parameter : activeColumnParameters()) {
57 Object column = parameter.getColumnData();
58 if (column != null) {
59 hdu.setColumnName(hdu.addColumn(column) - 1, parameter.getName(), null);
60 }
61 }
62 }
63
64 @Override
65 protected CompressParameters clone() {
66 try {
67 return (CompressParameters) super.clone();
68 } catch (CloneNotSupportedException e) {
69 return null;
70 }
71 }
72
73 @Override
74 public void setTileIndex(int index) {
75 }
76
77 @Override
78 public void getValuesFromColumn(int index) {
79 for (ICompressColumnParameter parameter : columnParameters()) {
80 parameter.getValueFromColumn(index);
81 }
82 }
83
84 @Override
85 public void getValuesFromHeader(Header header) throws HeaderCardException {
86 for (ICompressHeaderParameter compressionParameter : headerParameters()) {
87 compressionParameter.getValueFromHeader(header);
88 }
89 }
90
91 @Override
92 public void initializeColumns(Header header, BinaryTable binaryTable, int size)
93 throws HeaderCardException, FitsException {
94 for (ICompressColumnParameter parameter : columnParameters()) {
95 parameter.setColumnData(getNullableColumn(header, binaryTable, parameter.getName()));
96 }
97 }
98
99 @Override
100 public void initializeColumns(int size) {
101 for (ICompressColumnParameter parameter : columnParameters()) {
102 parameter.setColumnSize(size);
103 }
104 }
105
106 @Override
107 public void setValuesInColumn(int index) {
108 for (ICompressColumnParameter parameter : columnParameters()) {
109 parameter.setValueInColumn(index);
110 }
111 }
112
113 @Override
114 public void setValuesInHeader(Header header) throws HeaderCardException {
115 for (ICompressHeaderParameter parameter : activeHeaderParameters()) {
116 parameter.setValueInHeader(header);
117 }
118 }
119
120 private Object getNullableColumn(Header header, BinaryTable binaryTable, String columnName)
121 throws HeaderCardException, FitsException {
122 for (int i = 1; i <= binaryTable.getNCols(); i++) {
123 HeaderCard card = header.getCard(TTYPEn.n(i));
124 if (card != null) {
125 if (card.getValue().trim().equals(columnName)) {
126 return binaryTable.getColumn(i - 1);
127 }
128 }
129 }
130 return null;
131 }
132
133 /**
134 * Returns the complete subset of parameters from within, which are recorded in compressed table columns along with
135 * the compressed data.
136 *
137 * @return the subset of parameters that are recorded in compressed table columns.
138 *
139 * @see #activeColumnParameters()
140 * @see #headerParameters()
141 */
142 protected ICompressColumnParameter[] columnParameters() {
143 return new ICompressColumnParameter[0];
144 }
145
146 /**
147 * Returns the complete subset of parameters from within, which are recorded in the header of the compressed HDU.
148 *
149 * @return the subset of parameters that are recorded in the compressed HDU's header.
150 *
151 * @see #activeHeaderParameters()
152 * @see #columnParameters()
153 */
154 protected abstract ICompressHeaderParameter[] headerParameters();
155
156 /**
157 * Returns the subset of active parameters from within, which should be recorded in compressed table columns along
158 * with the compressed data.
159 *
160 * @return the subset of parameters that are recorded in compressed table columns.
161 *
162 * @see #columnParameters()
163 * @see #activeHeaderParameters()
164 *
165 * @since 1.23
166 */
167 protected ICompressColumnParameter[] activeColumnParameters() {
168 return columnParameters();
169 }
170
171 /**
172 * Returns the subset of active parameters from within, which should be recorded in the header of the compressed
173 * HDU.
174 *
175 * @return the subset of parameters that are recorded in the compressed HDU's header.
176 *
177 * @see #headerParameters()
178 * @see #activeColumnParameters()
179 *
180 * @since 1.23
181 */
182 protected ICompressHeaderParameter[] activeHeaderParameters() {
183 return headerParameters();
184 }
185 }