Skip to content

Commit

Permalink
Merge pull request #141 from jbfaden/main
Browse files Browse the repository at this point in the history
add support for 4-index arrays
  • Loading branch information
nutjob4life authored Aug 12, 2024
2 parents c94845b + 9dc2a8a commit d5dcd42
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main/java/gov/nasa/pds/label/object/ArrayObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,49 @@ public double getDouble(int i1, int i2, int i3) throws IOException {
return adapter.getDouble(i1, i2, i3);
}

/**
* Gets an element of a 4-D array, as an int.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as an int
* @throws IOException
*/
public int getInt(int i1, int i2, int i3, int i4) throws IOException {
return getInt(new int[] {i1, i2, i3, i4});
}

/**
* Gets an element of a 4-D array, as a long.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as a long
* @throws IOException
*/
public long getLong(int i1, int i2, int i3, int i4) throws IOException {
return getLong(new int[] {i1, i2, i3, i4});
}

/**
* Gets an element of a 4-D array, as a double.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as a double
* @throws IOException
*/
public double getDouble(int i1, int i2, int i3, int i4) throws IOException {
return adapter.getDouble(i1, i2, i3, i4);
}


/**
* Gets an array element, as an int.
*
Expand Down Expand Up @@ -339,6 +382,30 @@ public double[][][] getElements3D() throws IOException {
return values;
}


/**
* Gets the entire 4-D array, as doubles.
*
* @return an array of double with all array elements
* @throws IOException
*/
public double[][][][] getElements4D() throws IOException {
checkDimensions(4);

double[][][][] values = new double[dimensions[0]][dimensions[1]][dimensions[2]][dimensions[3]];
for (int i = 0; i < dimensions[0]; ++i) {
for (int j = 0; j < dimensions[1]; ++j) {
for (int k = 0; k < dimensions[2]; ++k) {
for (int l = 0; l < dimensions[3]; ++l) {
values[i][j][k][l] = getDouble(i, j, k, l);
}
}
}
}

return values;
}

private void checkDimensions(int expected) {
if (expected != dimensions.length) {
throw new IllegalArgumentException(
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/gov/nasa/pds/objectAccess/array/ArrayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,48 @@ public double getDouble(int i1, int i2, int i3) throws IOException {
return getDouble(new int[] {i1, i2, i3});
}

/**
* Gets an element of a 3-D array, as an int.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as an int
* @throws IOException an exception
*/
public int getInt(int i1, int i2, int i3, int i4) throws IOException {
return getInt(new int[] {i1, i2, i3, i4});
}

/**
* Gets an element of a 3-D array, as a long.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as a long
* @throws IOException an exception
*/
public long getLong(int i1, int i2, int i3, int i4) throws IOException {
return getLong(new int[] {i1, i2, i3, i4});
}

/**
* Gets an element of a 3-D array, as a double.
*
* @param i1 the first index
* @param i2 the second index
* @param i3 the third index
* @param i4 the fourth index
* @return the element value, as a double
* @throws IOException an exception
*/
public double getDouble(int i1, int i2, int i3, int i4) throws IOException {
return getDouble(new int[] {i1, i2, i3, i4});
}

/**
* Gets an array element, as an int.
*
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/gov/nasa/pds/label/object/ArrayObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,67 @@ public void test3DDouble()
array.close();
}

@Test
public void test4DDouble()
throws IOException, InstantiationException, IllegalAccessException, URISyntaxException {
int[] expectedDimensions = {2, 3, 4, 5};
File tempFile = createDoubleArray(expectedDimensions);
gov.nasa.arc.pds.xml.generated.File fileObj = createFileObject(tempFile);
Array arrayObj = createArrayObject(Array2D.class, expectedDimensions, "IEEE754MSBDouble");

ArrayObject array = new ArrayObject(tempFile.getParentFile(), fileObj, arrayObj, 0);
array.open();

checkDimensions(array, expectedDimensions);

assertEquals(array.getElementSize(), Double.SIZE / Byte.SIZE);
assertEquals(array.getOffset(), 0);
assertEquals(array.getSize(), tempFile.length());
assertFalse(array.isImage());

// Access the array element by element.
int expected = 1;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 4; ++k) {
for (int l = 0; l < 5; ++l) {
assertEquals(array.getDouble(i, j, k, l), (double) expected);
assertEquals(array.getInt(i, j, k, l), expected);
assertEquals(array.getLong(i, j, k, l), expected);

int[] position = {i, j, k, l};
assertEquals(array.getDouble(position), (double) expected);
assertEquals(array.getInt(position), expected);
assertEquals(array.getLong(position), expected);

++expected;
}
}
}
}

// Get the entire array.
double[][][][] actual = array.getElements4D();
assertEquals(actual.length, 2);

expected = 1;
for (int i = 0; i < 2; ++i) {
assertEquals(actual[i].length, 3);
for (int j = 0; j < 3; ++j) {
assertEquals(actual[i][j].length, 4);
for (int k = 0; k < 4; ++k) {
assertEquals(actual[i][j][k].length, 5 );
for (int l = 0; l < 5; ++l) {
assertEquals(actual[i][j][k][l], (double) expected);
++expected;
}
}
}
}

array.close();
}

@Test(dataProvider = "BadIndicesTests",
expectedExceptions = {ArrayIndexOutOfBoundsException.class, IllegalArgumentException.class})
public void testBadIndices(int[] position)
Expand Down

0 comments on commit d5dcd42

Please sign in to comment.