Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.stsci.roman.datamodels.exception.RomanDatamodelsException;
import edu.stsci.roman.datamodels.model.MosaicModel;
import edu.stsci.roman.datamodels.model.RomanModel;
import org.apache.commons.lang3.function.TriFunction;
import org.asdfformat.asdf.Asdf;
import org.asdfformat.asdf.AsdfFile;
import org.asdfformat.asdf.node.AsdfNode;
Expand All @@ -11,13 +12,12 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
* Static methods for reading Roman ASDF files.
*/
public class RomanDatamodels {
private static final Map<String, BiFunction<AsdfFile, AsdfNode, RomanModel<?>>> MODELS = new HashMap<>();
private static final Map<String, TriFunction<AsdfFile, AsdfNode, Boolean, RomanModel<?>>> MODELS = new HashMap<>();
static {
MODELS.put(MosaicModel.TAG_PREFIX, MosaicModel::new);
}
Expand All @@ -35,25 +35,36 @@ private RomanDatamodels() {
public static RomanModel<?> open(final Path path) throws IOException {
final AsdfFile asdfFile = Asdf.open(path);

if (!asdfFile.getTree().containsKey("roman")) {
try {
return open(asdfFile, true);
} catch (final Exception e) {
asdfFile.close();
throw e;
}
}

/**
* Wrap a Roman ASDF file in a datamodel class.
* @param asdfFile open ASDF file
* @return datamodel instance (type appropriate to the file)
* @throws RomanDatamodelsException when this library cannot handle the file content
*/
public static RomanModel<?> open(final AsdfFile asdfFile) {
return open(asdfFile, false);
}

private static RomanModel<?> open(final AsdfFile asdfFile, final boolean manageAsdfFile) {
if (!asdfFile.getTree().containsKey("roman")) {
throw new RomanDatamodelsException("ASDF file does not appear to be a Roman file");
}

final AsdfNode romanNode = asdfFile.getTree().get("roman");
for (final Map.Entry<String, BiFunction<AsdfFile, AsdfNode, RomanModel<?>>> entry : MODELS.entrySet()) {
for (final Map.Entry<String, TriFunction<AsdfFile, AsdfNode, Boolean, RomanModel<?>>> entry : MODELS.entrySet()) {
if (romanNode.getTag().startsWith(entry.getKey())) {
try {
return entry.getValue().apply(asdfFile, romanNode);
} catch (final Exception e) {
asdfFile.close();
throw e;
}
return entry.getValue().apply(asdfFile, romanNode, manageAsdfFile);
}
}

asdfFile.close();

throw new RomanDatamodelsException(String.format(
"Roman tag '%s' not yet supported by this library",
romanNode.getTag()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class MosaicModel implements RomanImageModel<MosaicModel.Meta> {

private final AsdfFile asdfFile;
private final AsdfNode node;
private final boolean manageAsdfFile;

/**
* Metadata.
Expand All @@ -39,10 +40,12 @@ public DoubleNdArray getData() {

@Override
public void close() {
try {
asdfFile.close();
} catch (final IOException e) {
throw new RuntimeException(e);
if (manageAsdfFile) {
try {
asdfFile.close();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}

Expand Down
Loading