Skip to content

Commit

Permalink
Update tests for WCS output of CD and PC matrixes
Browse files Browse the repository at this point in the history
  • Loading branch information
at88mph committed Aug 13, 2024
1 parent 84ea56b commit db6759f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 125 deletions.
2 changes: 1 addition & 1 deletion cadc-data-ops-fits/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {

sourceCompatibility = 11
group = 'org.opencadc'
version = '0.4.0'
version = '0.4.1'

description = 'OpenCADC FITS cutout library'
def git_url = 'https://github.com/opencadc/dal'
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
import java.util.List;

import nom.tam.fits.Header;
import nom.tam.fits.HeaderCard;
import nom.tam.fits.HeaderCardException;
import nom.tam.fits.header.Standard;
import org.apache.log4j.Logger;
import org.opencadc.soda.PixelRange;
import org.opencadc.soda.server.Cutout;
Expand Down Expand Up @@ -138,6 +140,55 @@ public static PixelRange[] getBounds(final Header header, final Cutout cutout)
return allPixelRanges.toArray(new PixelRange[0]);
}

/**
* Post cutout routine to adjust necessary Header Card values, such as CRPIXn, to match the dimensions of the resulting cutout.
* @param header The Header to adjust.
* @param dimensionLength The length of the dimensions to iterate.
* @param corners The corners (starting co-ordinates) of the resulting image.
* @param steps The striding value to skip while reading.
*/
static void adjustHeaders(final Header header, final int dimensionLength, final int[] corners, final int[] steps) {
// CRPIX values are not set automatically. Adjust them here, if present.
for (int i = 0; i < dimensionLength; i++) {
// Need to run backwards (reverse order) to match the dimensions.
final double nextValue = corners[corners.length - i - 1];
final int stepValue = steps[corners.length - i - 1];

final HeaderCard crPixCard = header.findCard(Standard.CRPIXn.n(i + 1));
if (crPixCard != null) {
final double crPixValue = (Double.parseDouble(crPixCard.getValue()) - nextValue) / stepValue;
if (stepValue > 1) {
final double newValue = crPixValue + (1.0 - (1.0 / stepValue));
crPixCard.setValue(newValue);
LOGGER.debug("Adjusted " + crPixCard.getKey() + " to " + newValue);
} else {
crPixCard.setValue(crPixValue);
LOGGER.debug("Set " + crPixCard.getKey() + " to " + crPixValue);
}
}

// Handle PC values. These typically override CD values, but as this is operating on Archive data, we'll simply
// modify the values as-is, meaning the CD values will be left even if a PC matrix is included.
//
// TODO: Does CDELTn need to come into play somewhere?
// jenkinsd 2024.08.13
//
for (int j = 0; j < dimensionLength; j++) {
final HeaderCard pcMatrixCard = header.findCard(String.format("PC%d_%d", i, j));
if (pcMatrixCard != null) {
final double pcMatrixValue = Double.parseDouble(pcMatrixCard.getValue());
pcMatrixCard.setValue(pcMatrixValue * (double) stepValue);
}

final HeaderCard cdMatrixCard = header.findCard(String.format("CD%d_%d", i, j));
if (cdMatrixCard != null) {
final double cdMatrixValue = Double.parseDouble(cdMatrixCard.getValue());
cdMatrixCard.setValue(cdMatrixValue * (double) stepValue);
}
}
}
}

static PixelRange[] getSpatialBounds(final Header header, final Shape shape)
throws HeaderCardException, NoSuchKeywordException {
final long[] bounds;
Expand Down
15 changes: 4 additions & 11 deletions cadc-data-ops-fits/src/test/java/org/opencadc/fits/FitsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,11 @@ public static void assertFitsEqual(final Fits expected, final Fits result) throw
final BasicHDU<?> nextResultHDU = resultHDUList[expectedIndex];

LOGGER.debug("On Extension at index " + expectedIndex);
FitsTest.assertHDUEqual(nextExpectedHDU, nextResultHDU);
FitsTest.assertHeadersEqual(nextExpectedHDU.getHeader(), nextResultHDU.getHeader());
}
}

public static void assertHDUEqual(final BasicHDU<?> expectedHDU, final BasicHDU<?> resultHDU) throws Exception {
final Header expectedHeader = expectedHDU.getHeader();
final Header resultHeader = resultHDU.getHeader();

FitsTest.assertHeadersEqual(expectedHeader, resultHeader);
}

public static void assertHeadersEqual(final Header expectedHeader, final Header resultHeader) throws Exception {
public static void assertHeadersEqual(final Header expectedHeader, final Header resultHeader) {
Arrays.stream(HEADER_CARD_KEYS_TO_CHECK).forEach(headerCardKey -> {
final HeaderCard expectedCard = expectedHeader.findCard(headerCardKey);
final HeaderCard resultCard = resultHeader.findCard(headerCardKey);
Expand All @@ -135,11 +128,11 @@ public static void assertHeadersEqual(final Header expectedHeader, final Header
if (valueType == Float.class) {
Assert.assertEquals("Header " + headerCardKey.key() + " has the wrong value.",
Float.parseFloat(expectedCard.getValue()),
Float.parseFloat(resultCard.getValue()), 0.0F);
Float.parseFloat(resultCard.getValue()), 1.0e-5F);
} else if (valueType == Double.class) {
Assert.assertEquals("Header " + headerCardKey.key() + " has the wrong value.",
Double.parseDouble(expectedCard.getValue()),
Double.parseDouble(resultCard.getValue()), 0.0D);
Double.parseDouble(resultCard.getValue()), 1.0e-5D);
} else {
Assert.assertEquals("Header " + headerCardKey.key() + " has the wrong value.",
expectedCard.getValue(), resultCard.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,6 @@ public class NDimensionalSlicerTest {
Log4jInit.setLevel("org.opencadc.fits", Level.DEBUG);
}

@Test
public void testIncorrectWCS() throws Exception {
ExtensionSliceFormat fmt = new ExtensionSliceFormat();
List<ExtensionSlice> slices = new ArrayList<>();
slices.add(fmt.parse("[1][*:20,*:20]"));
final Cutout cutout = new Cutout();
cutout.pixelCutouts = slices;

final NDimensionalSlicer slicer = new NDimensionalSlicer();
final File file = FileUtil.getFileFromResource("2490246p.fits.fz", NDimensionalSlicerTest.class);

final String configuredTestWriteDir = System.getenv("TEST_WRITE_DIR");
final String testWriteDir = configuredTestWriteDir == null ? "/tmp" : configuredTestWriteDir;
final File expectedFile = FileUtil.getFileFromResource("2490246p-cutout.fits",
NDimensionalSlicerTest.class);
final Path outputPath = Files.createTempFile(new File(testWriteDir).toPath(), "2490246p-cutout", ".fits");
LOGGER.debug("Writing out to " + outputPath);

try (final OutputStream outputStream = Files.newOutputStream(outputPath.toFile().toPath())) {
slicer.slice(file, cutout, outputStream);
}

final Fits expectedFits = new Fits(expectedFile);
final Fits resultFits = new Fits(outputPath.toFile());

FitsTest.assertFitsEqual(expectedFits, resultFits);
// Files.deleteIfExists(outputPath);
}

@Test
public void testMEFFileSlice() throws Exception {
ExtensionSliceFormat fmt = new ExtensionSliceFormat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,9 @@ public void testMultipleWCS() throws Exception {
}
LOGGER.debug("WCSCutoutUtilTest.testMultipleWCS OK: " + (System.currentTimeMillis() - startMillis) + " ms");
}

@Test
public void testLinearResolutionAdjustment() throws Exception {

}
}

Large diffs are not rendered by default.

0 comments on commit db6759f

Please sign in to comment.