View Javadoc
1   package nom.tam.fits.compression.provider.param.base;
2   
3   import nom.tam.fits.Header;
4   import nom.tam.fits.HeaderCard;
5   import nom.tam.fits.HeaderCardException;
6   import nom.tam.fits.compression.provider.param.api.ICompressHeaderParameter;
7   import nom.tam.fits.compression.provider.param.api.IHeaderAccess;
8   
9   /*
10   * #%L
11   * nom.tam FITS library
12   * %%
13   * Copyright (C) 1996 - 2024 nom-tam-fits
14   * %%
15   * This is free and unencumbered software released into the public domain.
16   *
17   * Anyone is free to copy, modify, publish, use, compile, sell, or
18   * distribute this software, either in source code form or as a compiled
19   * binary, for any purpose, commercial or non-commercial, and by any
20   * means.
21   *
22   * In jurisdictions that recognize copyright laws, the author or authors
23   * of this software dedicate any and all copyright interest in the
24   * software to the public domain. We make this dedication for the benefit
25   * of the public at large and to the detriment of our heirs and
26   * successors. We intend this dedication to be an overt act of
27   * relinquishment in perpetuity of all present and future rights to this
28   * software under copyright law.
29   *
30   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36   * OTHER DEALINGS IN THE SOFTWARE.
37   * #L%
38   */
39  
40  import static nom.tam.fits.header.Compression.ZNAMEn;
41  import static nom.tam.fits.header.Compression.ZVALn;
42  
43  /**
44   * (<i>for internal use</i>) Visibility may be reduced to protected.
45   * 
46   * @param <OPTION> The generic type of the compression option for which this parameter is used.
47   */
48  @SuppressWarnings("javadoc")
49  public abstract class CompressHeaderParameter<OPTION> extends CompressParameter<OPTION>
50          implements ICompressHeaderParameter {
51  
52      /**
53       * Instantiates a new compression parameter associated to a header value.
54       * 
55       * @param name   the FITS parameter name, that is the column name which stores the values
56       * @param option the compression option that uses the parameter value. It should not be <code>null</code>.
57       */
58      protected CompressHeaderParameter(String name, OPTION option) {
59          super(name, option);
60      }
61  
62      /**
63       * @deprecated Use {@link #findZVal(Header)} instead.
64       */
65      @Deprecated
66      public HeaderCard findZVal(IHeaderAccess header) {
67          return findZVal(header.getHeader());
68      }
69  
70      /**
71       * @deprecated Use {@link #nextFreeZVal(Header)} instead.
72       */
73      @Deprecated
74      public int nextFreeZVal(IHeaderAccess header) {
75          return nextFreeZVal(header.getHeader());
76      }
77  
78      /**
79       * Finds the ZVAL header value corresponding to this compression parameter
80       * 
81       * @param  header              The compressed HDU header
82       * 
83       * @return                     the header card containing the ZVAL for this compression parameter
84       * 
85       * @throws HeaderCardException if there was an issue accessing the header
86       */
87      public HeaderCard findZVal(Header header) throws HeaderCardException {
88          int nval = 1;
89          HeaderCard card = header.getCard(ZNAMEn.n(nval));
90          while (card != null) {
91              if (card.getValue().equals(getName())) {
92                  return header.getCard(ZVALn.n(nval));
93              }
94              card = header.getCard(ZNAMEn.n(++nval));
95          }
96          return null;
97      }
98  
99      /**
100      * <p>
101      * Finds the next available (or previously used) the ZNAME / ZVAL index in the header that we can use to store this
102      * parameter.
103      * </p>
104      * <p>
105      * Unfortunately, the way it was implemented, using this repeatedly on the same header and compression parameter
106      * keeps adding new entries, rather than updating the existing one. As of 1.19, the behavior is changed to update
107      * existing values -- resulting in a more predictable behavior.
108      * </p>
109      * 
110      * @param  header              The compressed HDU header
111      * 
112      * @return                     the ZNAME / ZVAL index we might use to store a new parameter
113      * 
114      * @throws HeaderCardException if there was an issue accessing the header
115      */
116     public int nextFreeZVal(Header header) throws HeaderCardException {
117         for (int n = 1;; n++) {
118             HeaderCard card = header.getCard(ZNAMEn.n(n));
119             if (card == null) {
120                 return n;
121             }
122             if (getName().equals(card.getValue())) {
123                 return n;
124             }
125         }
126     }
127 }