Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/opencadc/dal into conesearch
Browse files Browse the repository at this point in the history
  • Loading branch information
at88mph committed Feb 20, 2024
2 parents 40ce671 + b8596ab commit d3ffc75
Show file tree
Hide file tree
Showing 21 changed files with 728 additions and 577 deletions.
16 changes: 8 additions & 8 deletions cadc-data-ops-fits/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ repositories {

sourceCompatibility = 1.8
group = 'org.opencadc'
version = '0.2.9'
version = '0.3.0'

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

dependencies {
compile 'org.opencadc:cadc-dali:[1.2.10,2.0.0)'
compile 'org.opencadc:cadc-util:[1.6,2.0)'
compile 'org.opencadc:cadc-soda-server:[1.2.1,2.0)'
compile 'org.opencadc:cadc-wcs:[2.0,3.0)'
compile 'org.opencadc:jsky:[1.0.0,2.0.0)'
compile 'org.opencadc:nom-tam-fits:[1.16.8,1.17.0)'
implementation 'org.opencadc:cadc-dali:[1.2.10,2.0.0)'
implementation 'org.opencadc:cadc-util:[1.6,2.0)'
implementation 'org.opencadc:cadc-soda-server:[1.2.1,2.0)'
implementation 'org.opencadc:cadc-wcs:[2.1.4,3.0)'
implementation 'org.opencadc:jsky:[1.0.0,2.0.0)'
implementation 'gov.nasa.gsfc.heasarc:nom-tam-fits:1.18.0'

// Use JUnit test framework
testCompile 'junit:junit:[4.13,5.0)'
testImplementation 'junit:junit:[4.13,5.0)'
}

apply from: '../opencadc.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
import nom.tam.fits.Header;
import nom.tam.fits.HeaderCard;
import nom.tam.util.Cursor;
import nom.tam.util.RandomAccessDataObject;
import nom.tam.util.RandomAccessFileIO;
import org.apache.log4j.Logger;
import org.opencadc.fits.slice.NDimensionalSlicer;
import org.opencadc.soda.server.Cutout;
Expand All @@ -96,9 +96,9 @@
public class FitsOperations {
private static final Logger log = Logger.getLogger(FitsOperations.class);

private final RandomAccessDataObject src;
private final RandomAccessFileIO src;

public FitsOperations(RandomAccessDataObject src) {
public FitsOperations(RandomAccessFileIO src) {
this.src = src;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2022. (c) 2022.
* (c) 2023. (c) 2023.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
Expand Down Expand Up @@ -62,122 +62,32 @@
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
*
************************************************************************
*/

package org.opencadc.pkg.server;
package org.opencadc.fits;

import ca.nrc.cadc.net.NetUtil;
import ca.nrc.cadc.util.FileUtil;
import ca.nrc.cadc.util.Log4jInit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.compress.archivers.ArchiveEntry;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;

public class TarWriterTest {
private static final Logger log = Logger.getLogger(TarWriterTest.class);

static {
Log4jInit.setLevel("ca.nrc.cadc.caom2.pkg", Level.INFO);
}

@Test
public void testCreateTar() {
try {

// Create PackageItems for testing
// Files are in test/resources
URL url1 = getClass().getClassLoader().getResource("GovCanada.gif");
log.debug("url1: " + url1.toString());
PackageItem pi1 = new PackageItem(url1, "some/path/GovCanada.gif");

URL url2 = getClass().getClassLoader().getResource("SymbolCanada.gif");
log.debug("url2: " + url2.toString());
PackageItem pi2 = new PackageItem(url2,"another/path/SymbolCanada.gif");
import java.io.RandomAccessFile;

List<PackageItem> packageContents = new ArrayList<PackageItem>();
packageContents.add(pi1);
packageContents.add(pi2);
import nom.tam.util.RandomAccessFileIO;

File tmp = File.createTempFile("tartest", ".tar");
FileOutputStream fos = new FileOutputStream(tmp);
TarWriter fw = new TarWriter(fos);
for (PackageItem pi : packageContents) {
fw.write(pi);
}
fw.close();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
TarWriter bw = new TarWriter(bos);
for (PackageItem pi : packageContents) {
bw.write(pi);
}
bw.close();

byte[] content = bos.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(content);

TarArchiveInputStream tar = new TarArchiveInputStream(in);
Content c1 = getEntry(tar);
Content c2 = getEntry(tar);

ArchiveEntry te = tar.getNextTarEntry();
Assert.assertNull(te);

Assert.assertEquals("name", "some/path/GovCanada.gif", c1.name);
Assert.assertEquals("name", "another/path/SymbolCanada.gif", c2.name);

// Get the files from the local file system and compare
Path url1Path = Paths.get(url1.getPath());
log.debug("url1Path: " + url1Path.toString());
Assert.assertArrayEquals(c1.content, Files.readAllBytes(url1Path));

Path url2Path = Paths.get(url2.getPath());
log.debug("url2Path: " + url2Path.toString());
Assert.assertArrayEquals(c2.content, Files.readAllBytes(url2Path));

} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
Assert.fail("Unexpected exception: " + unexpected);
}
public class RandomAccessStorageObject extends RandomAccessFile implements RandomAccessFileIO {
public RandomAccessStorageObject(File file, String mode) throws FileNotFoundException {
super(file, mode);
}

class Content {
String name;
byte[] content;
@Override
public long position() throws IOException {
return super.getFilePointer();
}

private Content getEntry(TarArchiveInputStream tar) throws IOException {
Content ret = new Content();

TarArchiveEntry entry = tar.getNextTarEntry();
ret.name = entry.getName();

byte[] bytes = new byte[(int)entry.getSize()];
ByteArrayOutputStream out = new ByteArrayOutputStream();

int read = 0;
while ((read = tar.read(bytes)) > 0) {
out.write(bytes, 0, read);
}
ret.content = out.toByteArray();

return ret;
@Override
public void position(long n) throws IOException {
super.seek(n);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.util.Iterator;
import java.util.Map;

import nom.tam.fits.FitsFactory;
import nom.tam.fits.Header;
import nom.tam.fits.HeaderCard;
import nom.tam.fits.HeaderCardException;
Expand Down Expand Up @@ -407,39 +408,54 @@ private Header cloneHeader(final Header source) throws HeaderCardException {
return destination;
}

/**
* Copy a header card with some potential modifications. COMMENT and HISTORY cards are truncated to the
* maximum length.
* @param destination The Header to write to.
* @param headerCardKey The current key.
* @param valueType The class type of the value.
* @param comment The comment value for COMMENT or HISTORY values.
* @param value The string value.
* @throws HeaderCardException
*/
private void cloneHeaderCard(final Header destination, final String headerCardKey, final Class<?> valueType,
final String comment, final String value) throws HeaderCardException {
// Check for blank lines or just plain comments that are not standard FITS comments.
if (!StringUtil.hasText(headerCardKey)) {
destination.addValue(headerCardKey, (String) null, comment);
} else if (Standard.COMMENT.key().equals(headerCardKey)) {
destination.insertComment(comment);
if (StringUtil.hasText(comment) && comment.length() > HeaderCard.MAX_COMMENT_CARD_COMMENT_LENGTH) {
destination.insertComment(comment.substring(0, HeaderCard.MAX_COMMENT_CARD_COMMENT_LENGTH));
} else {
destination.insertComment(comment);
}
} else if (Standard.HISTORY.key().equals(headerCardKey)) {
destination.insertHistory(comment);
if (StringUtil.hasText(comment) && comment.length() > HeaderCard.MAX_COMMENT_CARD_COMMENT_LENGTH) {
destination.insertHistory(comment.substring(0, HeaderCard.MAX_COMMENT_CARD_COMMENT_LENGTH));
} else {
destination.insertHistory(comment);
}
} else if (headerCardKey.startsWith(CADCExt.CDELT.key())) {
// CDELT values cannot be zero.
final double cdeltValue = Double.parseDouble(value);
destination.addValue(headerCardKey, cdeltValue == 0.0D ? 1.0D : cdeltValue,
comment);
destination.addValue(headerCardKey, cdeltValue == 0.0D ? 1.0D : cdeltValue, comment);
} else {
if (valueType == String.class || valueType == null) {
destination.addValue(headerCardKey, value, comment);
} else if (valueType == Boolean.class) {
destination.addValue(headerCardKey, Boolean.parseBoolean(value) || value.equals("T"), comment);
} else if (valueType == Integer.class) {
destination.addValue(headerCardKey, Integer.parseInt(value),
comment);
destination.addValue(headerCardKey, Integer.parseInt(value), comment);
} else if (valueType == BigInteger.class) {
destination.addValue(headerCardKey, new BigInteger(value), comment);
} else if (valueType == Long.class) {
destination.addValue(headerCardKey, Long.parseLong(value),
comment);
destination.addValue(headerCardKey, Long.parseLong(value), comment);
} else if (valueType == Double.class) {
destination.addValue(headerCardKey, Double.parseDouble(value),
comment);
destination.addValue(headerCardKey, Double.parseDouble(value), comment);
} else if (valueType == BigDecimal.class) {
destination.addValue(headerCardKey, new BigDecimal(value),
comment);
destination.addValue(headerCardKey, new BigDecimal(value), comment);
} else if (valueType == Float.class) {
destination.addValue(headerCardKey, Float.parseFloat(value), comment);
}
}
}
Expand Down
Loading

0 comments on commit d3ffc75

Please sign in to comment.