package nom.tam.fits.header; /*- * #%L * nom.tam.fits * %% * Copyright (C) 1996 - 2024 nom-tam-fits * %% * This is free and unencumbered software released into the public domain. * * Anyone is free to copy, modify, publish, use, compile, sell, or * distribute this software, either in source code form or as a compiled * binary, for any purpose, commercial or non-commercial, and by any * means. * * In jurisdictions that recognize copyright laws, the author or authors * of this software dedicate any and all copyright interest in the * software to the public domain. We make this dedication for the benefit * of the public at large and to the detriment of our heirs and * successors. We intend this dedication to be an overt act of * relinquishment in perpetuity of all present and future rights to this * software under copyright law. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * #L% */ import java.util.AbstractMap; import java.util.ArrayList; import java.util.Map; import java.util.StringTokenizer; import nom.tam.fits.FitsException; import nom.tam.fits.Header; /** *
* A mapping of image coordinate values for a coordinate axis with {@link WCS#CTYPEna} = 'STOKES' (or
* equivalent), specifying polarization (or cross-polarization) data products along the image direction. The FITS
* standard (4.0) defines a mapping of pixel coordinate values along an image imension to Stokes parameters, and this
* enum provides an implementation of that for this library.
*
* An dataset may typically contain 4 or 8 Stokes parameters (or fewer), which depending on the type of measurement can * be (I, Q, U, [V]), or (RR, LL, RL, LR) and/or (XX, YY, XY, YX). As such, the corresponding {@link WCS#CRPIXna} is * typically 0 and {@link WCS#CDELTna} is +/- 1, and depending on the type of measurement {@link WCS#CRVALna} is 1, or * -1, or -5. You can use the {@link Parameters} subclass to help populate or interpret Stokes parameters in headers. *
* * @author Attila Kovacs * * @since 1.20 * * @see WCS * @see #parameters() */ public enum Stokes { /** Stokes I: total (polarized + unpolarized) power */ I(1), /** Stokes Q: linear polarization Q component */ Q(2), /** Stokes U: linear polarization U component */ U(3), /** Stokes V: circularly polarization */ V(4), /** circular cross-polarization between two right-handed wave components */ RR(-1), /** circular cross-polarization between two left-handed wave components */ LL(-2), /** circular cross-polarization between a right-handled (input 1) and a left-handed (input 2) wave component */ RL(-3), /** circular cross-polarization between a left-handled (input 1) and a right-handed (input 2) wave component */ LR(-4), /** linear cross-polarization between two 'horizontal' wave components (in local orientation) */ XX(-5), /** linear cross-polarization between two 'vertical' wave components (in local orientation) */ YY(-6), /** * linear cross-polarization between a 'horizontal' (input 1) and a 'vertical' (input 2) wave component (in local * orientation) */ XY(-7), /** * linear cross-polarization between a 'vertical' (input 1) and a 'horizontal' (input 2) wave component (in local * orientation) */ YX(-8); /** The value to use for CTYPE type keywords to indicate Stokes parameter data */ public static final String CTYPE = "STOKES"; private int index; private static Stokes[] ordered = {YX, XY, YY, XX, LR, RL, LL, RR, null, I, Q, U, V}; private static final int STANDARD_PARAMETER_COUNT = 4; private static final int FULL_PARAMETER_COUNT = 8; Stokes(int value) { this.index = value; } /** * Returns the WCS coordinate value corresponding to this Stokes parameter for an image coordinate with * {@link WCS#CTYPEna} ='STOKES'.
*
* @return the WCS coordinate value corresponding to this Stokes parameter.
*
* @see #forCoordinateValue(int)
* @see WCS#CTYPEna
* @see WCS#CRVALna
*/
public final int getCoordinateValue() {
return index;
}
/**
* Returns the Stokes parameter for the given pixel coordinate value for an image coordinate with
* {@link WCS#CTYPEna} = 'STOKES'.
*
* @param value The image coordinate value
*
* @return The Stokes parameter, which corresponds to that coordinate value.
*
* @throws IndexOutOfBoundsException if the coordinate value is outt of the range of acceptable Stokes coordinate
* values.
*
* @see #getCoordinateValue()
*/
public static Stokes forCoordinateValue(int value) throws IndexOutOfBoundsException {
return ordered[value - YX.getCoordinateValue()];
}
/**
* Helper class for setting or interpreting a set of measured Stokes parameters stored along an array dimension. Two
* instances of Stokes parameters are considered equal if they measure the same polarization terms, in the same
* order.
*
* @author Attila Kovacs
*
* @since 1.20
*/
public static final class Parameters {
private int flags;
private int offset;
private int step;
private int count;
private Parameters(int flags) {
this.flags = flags;
boolean reversed = (flags & REVERSED_ORDER) != 0;
step = reversed ? -1 : 1;
count = STANDARD_PARAMETER_COUNT;
if ((flags & FULL_CROSS_POLARIZATION) == 0) {
offset = reversed ? Stokes.V.getCoordinateValue() : Stokes.I.getCoordinateValue();
} else {
step = -step;
if ((flags & CIRCULAR_CROSS_POLARIZATION) == 0) {
offset = reversed ? Stokes.YX.getCoordinateValue() : Stokes.XX.getCoordinateValue();
} else if ((flags & LINEAR_CROSS_POLARIZATION) == 0) {
offset = reversed ? Stokes.LR.getCoordinateValue() : Stokes.RR.getCoordinateValue();
} else {
offset = reversed ? Stokes.YX.getCoordinateValue() : Stokes.RR.getCoordinateValue();
count = FULL_PARAMETER_COUNT;
}
}
}
private Parameters(int offset, int step, int n) {
this.offset = offset;
this.step = step;
this.count = n;
if (offset < 0) {
int end = offset + (n - 1) * step;
if (Math.min(offset, end) <= XX.index) {
flags |= LINEAR_CROSS_POLARIZATION;
}
if (Math.max(offset, end) > XX.index) {
flags |= CIRCULAR_CROSS_POLARIZATION;
}
step = -step;
}
if (step < 0) {
flags |= REVERSED_ORDER;
}
}
@Override
public int hashCode() {
return flags ^ Integer.hashCode(offset) ^ Integer.hashCode(step);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Parameters)) {
return false;
}
Parameters p = (Parameters) o;
if (p.flags != flags) {
return false;
}
if (p.offset != offset) {
return false;
}
if (p.step != step) {
return false;
}
return true;
}
boolean isReversedOrder() {
return (flags & REVERSED_ORDER) != 0;
}
/**
* Checks if the parameters are for measuring cross-polarization between two inputs.
*
* @return true if it is for cross polarization, otherwise false.
*/
public boolean isCrossPolarization() {
return (flags & FULL_CROSS_POLARIZATION) != 0;
}
/**
* Checks if the parameters include linear polarization terms.
*
* @return true if linear polarization is measured, otherwise false.
*/
public boolean hasLinearPolarization() {
return (flags & FULL_CROSS_POLARIZATION) != CIRCULAR_CROSS_POLARIZATION;
}
/**
* Checks if the parameters include circular polarization term(s).
*
* @return true if cirular cross polarization is measured, otherwise false.
*/
public boolean hasCircularPolarization() {
return (flags & FULL_CROSS_POLARIZATION) != LINEAR_CROSS_POLARIZATION;
}
/**
* Returns the Stokes parameter for a given Java array index for a dimension that corresponds to the Stokes
* parameters described by this instance.
*
* @param idx the zero-based Java array index, typically [0:3] for single-ended
* polarization or circular or linear-only cross-polarization, or else
* [0:7] for full cross-polarization.
*
* @return The specific Stokes parameter corresponding to the specified array index.
*
* @throws IndexOutOfBoundsException if the index is outside of the expected range.
*
* @see #getAvailableParameters()
*
* @since 1.19.1
*
* @author Attila Kovacs
*/
public Stokes getParameter(int idx) throws IndexOutOfBoundsException {
if (idx < 0 || idx >= count) {
throw new IndexOutOfBoundsException();
}
return Stokes.forCoordinateValue(offset + step * idx);
}
/**
* Returns the ordered list of parameters, which can be used to translate array indexes to Stokes values,
* supported by this parameter set.
*
* @return the ordered list of available Stokes parameters in this measurement set.
*
* @see #getParameter(int)
*/
public ArrayListnull if the header does not contain a fully valid description of a Stokes
* coordinate axis.
*
* @throws FitsException if the header does not contain an NAXIS keyword, necessary for translating Java array
* indices to FITS array indices, or if the CRVALn, CRPIXna or CDELTna values for the
* 'STOKES' dimension are inconsistent with a Stokes coordinate definition.
*
* @see #fromTableHeader(Header, int)
* @see Parameters#fillImageHeader(Header, int)
*
* @since 1.20
*
* @author Attila Kovacs
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Map.Entrynull if the header does not contain a fully
* valid description of a Stokes coordinate axis.
*
* @throws IndexOutOfBoundsException if the column index is invalid.
* @throws FitsException if the header does not contain an TDIMn keyword for the column, necessary for
* translating Java array indices to FITS array indices, or if the iCRVLn,
* iCRPXn or iCDLTn values for the 'STOKES' dimension are inconsistent with a
* Stokes coordinate definition.
*
* @see #fromImageHeader(Header)
* @see Parameters#fillTableHeader(Header, int, int)
*
* @since 1.20
*
* @author Attila Kovacs
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Map.Entry