forked from BayAreaMetro/travel-model-one
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wsp-sag/bart-feat-omx-read
Feature - Add OMX Read
- Loading branch information
Showing
29 changed files
with
302 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
core/cmf/common-base/src/java/com/pb/common/matrix/OMXMatrixReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
core/cmf/common-base/src/java/com/pb/common/matrix/OMXMatrixWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
core/cmf/common-base/src/java/com/pb/common/matrix/OMXTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.