Skip to content

Commit

Permalink
Merge pull request #1 from wsp-sag/bart-feat-omx-read
Browse files Browse the repository at this point in the history
Feature - Add OMX Read
  • Loading branch information
i-am-sijia authored May 10, 2022
2 parents d9894c6 + 6ff66b2 commit 49e1e2f
Show file tree
Hide file tree
Showing 29 changed files with 302 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<classpathentry kind="lib" path="core/cmf/common-base/release/common-base.jar"/>
<classpathentry kind="lib" path="core/cmf/common-base/release/log4j-1.2.9.jar"/>
<classpathentry kind="lib" path="core/cmf/common-base/release/transcadMatrix.jar"/>
<classpathentry kind="lib" path="core/projects/mtc/release/mtc.jar"/>
<classpathentry kind="lib" path="core/projects/mtc/release/mtc.jar" sourcepath="core"/>
<classpathentry kind="lib" path="core/third-party/crowbar/crowbar.jar"/>
<classpathentry kind="lib" path="core/third-party/jexcelapi/jxl.jar"/>
<classpathentry kind="lib" path="core/third-party/jppf-2.4/jca-client/lib/commons-logging-1.1.jar"/>
Expand Down Expand Up @@ -111,5 +111,6 @@
<classpathentry kind="lib" path="model-files/runtime/mtc.jar"/>
<classpathentry kind="lib" path="model-files/runtime/quickboards.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="core/cmf/common-base/lib/omx.jar"/>
<classpathentry kind="output" path="model-files/runtime/config/jppf-2.4/JPPF-2.4-application-template/classes"/>
</classpath>
Binary file added core/cmf/common-base/lib/omx.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion core/cmf/common-base/module_common-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<pathelement location="${module.common-base.basedir}/lib/junit-4.1.jar"/>
<pathelement location="${module.common-base.basedir}/lib/javadbf-0.4.0.jar"/>
<pathelement location="${module.common-base.basedir}/lib/cajo.jar"/>
<pathelement location="${module.common-base.basedir}/lib/jna-4.0.0.jar"/>
<pathelement location="${module.common-base.basedir}/lib/jna-4.0.0.jar"/>
<pathelement location="${module.common-base.basedir}/lib/omx.jar"/>
<!-- The following are not need to compile common-base and so should be moved to 'third-party' -->
<!--
<pathelement location="${module.common-base.basedir}/lib/or124.jar"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@ private Matrix readMatrix(DataEntry matrixEntry) {
} else if (matrixEntry.format.equalsIgnoreCase("transcad")) {
MatrixReader mr = MatrixReader.createReader(MatrixType.TRANSCAD, new File(fileName));
matrix = mr.readMatrix(matrixEntry.matrixName);
} else if (matrixEntry.format.equalsIgnoreCase("omx")) {
MatrixReader mr = MatrixReader.createReader(MatrixType.OMX, new File(fileName));
matrix = mr.readMatrix(matrixEntry.matrixName);
} else {
throw new RuntimeException("unsupported matrix type: " + matrixEntry.format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class MatrixException extends RuntimeException {
public static final String ERROR_WRITING_FILE= "Error while writing to file";
public static final String MATRIX_DOES_NOT_EXIST = "Matrix does not exist";
public static final String INVALID_FORMAT = "Invalid format";
public static final String INVALID_TABLE_NAME = "Matrix does not exist with name";
public static final String INVALID_TABLE_NAME = "Matrix does not exist with name";
public static final String FILE_CANNOT_BE_OPENED = "Cannot open matrix";

public MatrixException() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ else if (type.equals(MatrixType.D311)) {
else if (type.equals(MatrixType.TPPLUS32)) { // TODO: remove once 32bit reader DLL is no longer supported
reader = new TpplusMatrixReader( file );
}
else if (type.equals(MatrixType.OMX)) {
reader = new OMXMatrixReader( file );
}
else {
throw new MatrixException(MatrixException.INVALID_TYPE+", "+ type);
}
Expand Down Expand Up @@ -131,6 +134,9 @@ public static Matrix readMatrix(File file, String matrixName) {
} else if (type.equals(MatrixType.TRANSCAD)) {
MatrixReader mr = MatrixReader.createReader(MatrixType.TRANSCAD, file);
matrix = mr.readMatrix(matrixName);
} else if (type.equals(MatrixType.OMX)) {
MatrixReader mr = MatrixReader.createReader(MatrixType.OMX, file);
matrix = mr.readMatrix(matrixName);
} else {
throw new RuntimeException("Unsupported matrix type: " + type);
}
Expand Down Expand Up @@ -181,6 +187,10 @@ else if (fileName.indexOf(".mtx") > 0 || fileName.indexOf(".MTX") > 0 ||
{
type = MatrixType.TRANSCAD;
}
else if (fileName.indexOf(".omx") > 0 || fileName.indexOf(".OMX") > 0)
{
type = MatrixType.OMX;
}
else {
throw new RuntimeException("Could not determine type of matrix file, " + file.getAbsolutePath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class MatrixType implements Serializable {
public static final MatrixType TPPLUS = new MatrixType("TPPlus", 6);
public static final MatrixType TRANSCAD = new MatrixType("Transcad", 7);
public static final MatrixType TPPLUS32 = new MatrixType("TPPlus32", 8); // TODO: remove once 32bit reader DLL is no longer supported

public static final MatrixType OMX = new MatrixType("OMX", 9);

private String id;
private int hashValue;
Expand Down Expand Up @@ -91,13 +91,14 @@ public static MatrixType lookUpMatrixType(String matrixTypeName) {
if (TPPLUS.toString().equalsIgnoreCase(matrixTypeName)) return TPPLUS;
if (TRANSCAD.toString().equalsIgnoreCase(matrixTypeName)) return TRANSCAD;
if (TPPLUS32.toString().equalsIgnoreCase(matrixTypeName)) return TPPLUS32; // TODO: remove once 32bit reader DLL is no longer supported

if (OMX.toString().equalsIgnoreCase(matrixTypeName)) return OMX;

logger.error("Matrix type "+matrixTypeName+" is not defined");
return null;
}

public static MatrixType[] values() {
return new MatrixType[]{BINARY,ZIP,CSV,EMME2,D311,TPPLUS,TRANSCAD,TPPLUS32}; // TODO: remove once 32bit reader DLL is no longer supported
return new MatrixType[]{BINARY,ZIP,CSV,EMME2,D311,TPPLUS,TRANSCAD,TPPLUS32,OMX}; // TODO: remove once 32bit reader DLL is no longer supported

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public static MatrixWriter createWriter(MatrixType type, File file)
// writer = new TranscadMatrixWriter( file );
throw new RuntimeException("TRANSCAD not supported");
}
else
if (type.equals(MatrixType.OMX)) {
writer = new OMXMatrixWriter( file );
}
else
if (type.equals(MatrixType.CSV)) {
writer = new CSVMatrixWriter( file );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.pb.common.matrix;

import java.io.File;
import java.util.Set;

import org.apache.log4j.Logger;

import omx.OmxFile;
import omx.OmxMatrix;
import omx.OmxMatrix.OmxDoubleMatrix;
import omx.OmxLookup;

/**
* Implements an OMX MatrixReader
*
* @author Ben Stabler
* @version 1.0, 02/11/15
*/
public class OMXMatrixReader extends MatrixReader {

private OmxFile omxFile = null;
protected static Logger logger = Logger.getLogger("com.pb.common.matrix");

/**
* Prevent outside classes from instantiating the default constructor.
*/
private OMXMatrixReader() {}

/**
* @param file represents the physical matrix file
*/
public OMXMatrixReader(File file) throws MatrixException {
this.file = file;
openOMXFile();
}

public Matrix readMatrix() throws MatrixException {
return readMatrix("");
}

public Matrix readMatrix(String name) throws MatrixException {
if(omxFile.getMatrixNames().contains(name)){
return readData(name);
}
else{
logger.info("No matrix in " + file.getPath() + " with name " + name);
throw new MatrixException(MatrixException.INVALID_TABLE_NAME + " " + name);
}

}

private void openOMXFile() throws MatrixException {
try {
omxFile = new OmxFile(file.getPath());
omxFile.openReadWrite();
}
catch (Exception e) {
throw new MatrixException(e, MatrixException.FILE_CANNOT_BE_OPENED + ", "+ file.getPath());
}
}

/**
* Reads and returns an entire matrix
*
*/
public Matrix[] readMatrices() throws MatrixException {

Matrix[] m = null;
Set<String> matNames = omxFile.getMatrixNames();
m = new Matrix[matNames.size()];
int i = 0;
for(String matName : omxFile.getMatrixNames()){
m[i] = readMatrix(matName);
i++;
}
return m;
};

/**
* Returns a matrix.
*
*/
private Matrix readData(String name) {
OmxMatrix.OmxDoubleMatrix omxMat = null;
try {
omxMat = (OmxDoubleMatrix) omxFile.getMatrix(name);
double[][] values = omxMat.getData();

//convert to Matrix float[][]
float[][] valuesFloat = new float[values.length][values[0].length];
for (int i = 0 ; i < values.length; i++) {
for (int j = 0 ; j < values[0].length; j++) {
valuesFloat[i][j] = (float) values[i][j];
}
}

Matrix m = new Matrix(name, name, valuesFloat);

//set zone numbers if found
//CUBE - 1 to X
//VISUM - 'NO'
//EMME - 'zone number'
//TransCAD - ?
if (omxFile.getLookupNames().contains("NO")) {
OmxLookup.OmxIntLookup omxZoneNums = (OmxLookup.OmxIntLookup)omxFile.getLookup("NO");
m.setExternalNumbersZeroBased(omxZoneNums.getLookup());
}
if (omxFile.getLookupNames().contains("zone number")) {
OmxLookup.OmxIntLookup omxZoneNums = (OmxLookup.OmxIntLookup)omxFile.getLookup("zone number");
m.setExternalNumbersZeroBased(omxZoneNums.getLookup());
}

return m;
}
catch (Exception e) {
throw new MatrixException("Matrix not found: " + name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.pb.common.matrix;

import java.io.File;

import org.apache.log4j.Logger;

import omx.OmxFile;
import omx.OmxLookup;
import omx.OmxMatrix;

/**
* Implements an OMX MatrixWriter
*
* @author Ben Stabler
* @version 1.0, 02/11/15
*/
public class OMXMatrixWriter extends MatrixWriter {

private OmxFile omxFile = null;
protected static Logger logger = Logger.getLogger("com.pb.common.matrix");
private double defaultNA = Double.valueOf(0);

/**
* Prevent outside classes from instantiating the default constructor.
*/
private OMXMatrixWriter() {}

/**
* @param file represents the physical matrix file
*/
public OMXMatrixWriter(File file) throws MatrixException {
this.file = file;
}

public void writeMatrix(Matrix m) throws MatrixException {
writeMatrix(m.name, m);
}

public void writeMatrix(String name, Matrix m) throws MatrixException {
writeData(name, m);
}

/**
* Writes a matrix to the underlying file.
*
*@param name matrix name
*@param m the matrix to write
*
*/
private void writeData(String name, Matrix m) {
omxFile = new OmxFile(file.getPath());

try {
if(file.exists()) {
omxFile.openReadWrite();
} else {
int[] shape = new int[2];
shape[0] = m.getRowCount();
shape[1] = m.getColumnCount();
omxFile.openNew(shape);
}
} catch (Exception e) {
throw new MatrixException(e, MatrixException.FILE_NOT_FOUND + ", "+ file);
}

try {
//convert to double[][]
float[][] values = m.getValues();
double[][] valuesDouble = new double[values.length][values[0].length];
for (int i = 0 ; i < values.length; i++) {
for (int j = 0 ; j < values[0].length; j++) {
valuesDouble[i][j] = (double) values[i][j];
}
}

//create sequential CUBE_MAT_NUMBER attribute for use with Cube
int count = 1;
for (String matName : omxFile.getMatrixNames()) {
count = count + 1;
}

//add matrix to file
OmxMatrix.OmxDoubleMatrix mat = new OmxMatrix.OmxDoubleMatrix(name,valuesDouble,defaultNA);
omxFile.addMatrix(mat);
mat.setAttribute("CUBE_MAT_NUMBER",count);

//add external zone numbers as NO lookup
if (!omxFile.getLookupNames().contains("NO")) {
OmxLookup.OmxIntLookup omxZoneNums = new OmxLookup.OmxIntLookup("NO", m.getExternalColumnNumbersZeroBased(), 0);
omxFile.addLookup(omxZoneNums);
}

//save file
omxFile.save();

} catch (Exception e) {
throw new MatrixException(e, MatrixException.ERROR_WRITING_FILE);
}
}

/** Writes all tables of an entire matrix
*
*/
public void writeMatrices(String[] names, Matrix[] m) throws MatrixException {
for(int i=0; i<names.length; i++) {
writeData(names[i],m[i]);
}
}

}
41 changes: 41 additions & 0 deletions core/cmf/common-base/src/java/com/pb/common/matrix/OMXTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.pb.common.matrix;

import java.io.File;

import omx.OmxLookup;

/**
* Test basic OMX Matrix Reader and writer
* java -classpath "omx.jar;common-base.jar" -Djava.library.path="jhdfdllFolder" com.pb.common.matrix.OMXTest
* requires omx.jar in classpath and jhdf5.dll,jhdf.dll in the java.library.path
* @author Ben Stabler
* @version 1.0, 02/11/15
*/
public class OMXTest {

private OMXTest() {}

public static void main(String[] args) {

int[] zoneNames = {100,101,102,103,104};
File testFile = new File("test.omx");

Matrix mat = new Matrix("test","test",zoneNames.length,zoneNames.length);
mat.fill(2);
mat.setExternalNumbersZeroBased(zoneNames);

OMXMatrixWriter writer = new OMXMatrixWriter(testFile);
writer.writeMatrix(mat);
System.out.println("write test matrix sum:" + Double.valueOf(mat.getSum()));

OMXMatrixReader reader = new OMXMatrixReader(testFile);
Matrix matIn = reader.readMatrix("test");
System.out.println("read test matrix sum:" + Double.valueOf(matIn.getSum()));

for (int j = 0 ; j < matIn.getExternalRowNumbersZeroBased().length; j++) {
System.out.println("zone " + String.valueOf(matIn.getExternalRowNumbersZeroBased()[j]));
}

}

}
Loading

0 comments on commit 49e1e2f

Please sign in to comment.