Skip to content

Commit

Permalink
Merge pull request #91 from NASA-PDS/validate_7
Browse files Browse the repository at this point in the history
Validate 7: add complex types
  • Loading branch information
jordanpadams authored May 26, 2023
2 parents f48047b + 72cd004 commit 0e6cb56
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/main/java/gov/nasa/pds/label/NameNotKnownException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gov.nasa.pds.label;

public class NameNotKnownException extends IllegalArgumentException {
private static final long serialVersionUID = -2657183691342441420L;
public NameNotKnownException() {
super();
}
public NameNotKnownException(String s) {
super(s);
}
public NameNotKnownException(Throwable cause) {
super(cause);
}
public NameNotKnownException(String message, Throwable cause) {
super(message, cause);
}
}
3 changes: 2 additions & 1 deletion src/main/java/gov/nasa/pds/objectAccess/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public enum NumericDataType {
64), SignedMSB2(16, "HALF"), SignedMSB4(32, "FULL"), SignedMSB8(64), UnsignedMSB2(16,
"HALF"), UnsignedMSB4(32, "FULL"), UnsignedMSB8(64), IEEE754LSBSingle(32,
"REAL"), IEEE754LSBDouble(64, "DOUBLE"), IEEE754MSBSingle(32,
"REAL"), IEEE754MSBDouble(64, "DOUBLE"), ASCII_Integer(32), ASCII_Real(64);
"REAL"), IEEE754MSBDouble(64, "DOUBLE"), ASCII_Integer(32), ASCII_Real(64),
ComplexLSB8(8),ComplexMSB8(8),ComplexLSB16(16),ComplexMSB16(16);

private int bits;
private String vicarAlias;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gov.nasa.pds.objectAccess.array;

import java.nio.ByteBuffer;

public interface ComplexDataTypeAdapter extends DataTypeAdapter {
/**
* Gets the imaginary value as an int.
*
* @param buf the buffer from which to get the value
* @return the imaginary value, as an int
*/
int getImagInt(ByteBuffer buf);
/**
* Gets the imaginary value as a long.
*
* @param buf the buffer from which to get the value
* @return the imaginary value, as a long
*/
long getImagLong(ByteBuffer buf);
/**
* Gets the imaginary value as a double.
*
* @param buf the buffer from which to get the value
* @return the imaginary value, as a double
*/
double getImagDouble(ByteBuffer buf);
/**
* Gets the real value as an int.
*
* @param buf the buffer from which to get the value
* @return the real value, as an int
*/
int getRealInt(ByteBuffer buf);
/**
* Gets the real value as a long.
*
* @param buf the buffer from which to get the value
* @return the real value, as a long
*/
long getRealLong(ByteBuffer buf);
/**
* Gets the real value as a double.
*
* @param buf the buffer from which to get the value
* @return the real value, as a double
*/
double getRealDouble(ByteBuffer buf);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package gov.nasa.pds.objectAccess.array;

import java.nio.ByteBuffer;

public class ComplexDoubleAdapter extends DoubleAdapter implements ComplexDataTypeAdapter {
public ComplexDoubleAdapter(boolean isBigEndian) {
super(isBigEndian);
// TODO Auto-generated constructor stub
}
@Override
public int getInt(ByteBuffer buf) {
return (int)this.getDouble(buf);
}
@Override
public long getLong(ByteBuffer buf) {
return (long)this.getDouble(buf);
}
@Override
public double getDouble(ByteBuffer buf) {
double imag=this.getImagDouble(buf), real=this.getRealDouble(buf);
return Math.sqrt(imag*imag + real*real);
}
@Override
public int getImagInt(ByteBuffer buf) {
return super.getInt(buf);
}
@Override
public long getImagLong(ByteBuffer buf) {
return super.getLong(buf);
}
@Override
public double getImagDouble(ByteBuffer buf) {
return super.getDouble(buf);
}
@Override
public int getRealInt(ByteBuffer buf) {
return super.getInt(buf);
}
@Override
public long getRealLong(ByteBuffer buf) {
return super.getLong(buf);
}
@Override
public double getRealDouble(ByteBuffer buf) {
return super.getDouble(buf);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gov.nasa.pds.objectAccess.array;

import java.nio.ByteBuffer;

public class ComplexFloatAdapter extends FloatAdapter implements ComplexDataTypeAdapter {
public ComplexFloatAdapter(boolean isBigEndian) {
super(isBigEndian);
}
@Override
public int getInt(ByteBuffer buf) {
return (int)this.getDouble(buf);
}
@Override
public long getLong(ByteBuffer buf) {
return (long)this.getDouble(buf);
}
@Override
public double getDouble(ByteBuffer buf) {
double imag=this.getImagDouble(buf), real=this.getRealDouble(buf);
return Math.sqrt(imag*imag + real*real);
}
@Override
public int getImagInt(ByteBuffer buf) {
return super.getInt(buf);
}
@Override
public long getImagLong(ByteBuffer buf) {
return super.getLong(buf);
}
@Override
public double getImagDouble(ByteBuffer buf) {
return super.getDouble(buf);
}
@Override
public int getRealInt(ByteBuffer buf) {
return super.getInt(buf);
}
@Override
public long getRealLong(ByteBuffer buf) {
return super.getLong(buf);
}
@Override
public double getRealDouble(ByteBuffer buf) {
return super.getDouble(buf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public double getDouble(ByteBuffer buf) {
return getValue(buf);
}

private double getValue(ByteBuffer buf) {
protected double getValue(ByteBuffer buf) {
long bits = valueAdapter.getLong(buf);
return Double.longBitsToDouble(bits);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/gov/nasa/pds/objectAccess/array/ElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.util.HashMap;
import java.util.Map;
import gov.nasa.pds.label.NameNotKnownException;

/**
* Implements an object that represents the type of an array element.
Expand All @@ -40,6 +41,14 @@ public class ElementType {

private static final Map<String, ElementType> TYPES = new HashMap<>();
static {
TYPES.put("ComplexLSB8",
new ElementType(Float.SIZE / Byte.SIZE * 2, new ComplexFloatAdapter(false)));
TYPES.put("ComplexMSB8",
new ElementType(Float.SIZE / Byte.SIZE * 2, new ComplexFloatAdapter(true)));
TYPES.put("ComplexLSB16",
new ElementType(Double.SIZE / Byte.SIZE * 2, new ComplexDoubleAdapter(false)));
TYPES.put("ComplexMSB16",
new ElementType(Double.SIZE / Byte.SIZE * 2, new ComplexDoubleAdapter(true)));
TYPES.put("IEEE754LSBDouble",
new ElementType(Double.SIZE / Byte.SIZE, new DoubleAdapter(false)));
TYPES.put("IEEE754MSBDouble",
Expand Down Expand Up @@ -91,7 +100,8 @@ public class ElementType {
* @return the element type for that type name
*/
public static ElementType getTypeForName(String typeName) {
return TYPES.get(typeName);
if (TYPES.containsKey (typeName)) return TYPES.get(typeName);
throw new NameNotKnownException("Cannot determine the ElementType from the name: " + typeName);
}

private ElementType(int size, DataTypeAdapter adapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public double getDouble(ByteBuffer buf) {
return getValue(buf);
}

private double getValue(ByteBuffer buf) {
protected double getValue(ByteBuffer buf) {
int bits = valueAdapter.getInt(buf);
return Float.intBitsToFloat(bits);
}
Expand Down

0 comments on commit 0e6cb56

Please sign in to comment.