Search in sources :

Example 1 with MLSparse

use of com.jmatio.types.MLSparse in project org.csstudio.display.builder by kasemir.

the class MatFileIncrementalWriter method writeMatrix.

/**
 * Writes MATRIX into <code>OutputStream</code>.
 *
 * @param os - <code>OutputStream</code>
 * @param array - a <code>MLArray</code>
 * @throws IOException
 */
private void writeMatrix(DataOutputStream output, MLArray array) throws IOException {
    OSArrayTag tag;
    ByteArrayOutputStream buffer;
    DataOutputStream bufferDOS;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    // flags
    writeFlags(dos, array);
    // dimensions
    writeDimensions(dos, array);
    // array name
    writeName(dos, array);
    switch(array.getType()) {
        case MLArray.mxCHAR_CLASS:
            // write char data
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            Character[] ac = ((MLChar) array).exportChar();
            for (int i = 0; i < ac.length; i++) {
                bufferDOS.writeByte((byte) ac[i].charValue());
            }
            tag = new OSArrayTag(MatDataTypes.miUTF8, buffer.toByteArray());
            tag.writeTo(dos);
            break;
        case MLArray.mxDOUBLE_CLASS:
            tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxUINT8_CLASS:
            tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxINT8_CLASS:
            tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxINT64_CLASS:
            tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxUINT64_CLASS:
            tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxSTRUCT_CLASS:
            // field name length
            int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
            dos.writeInt(itag);
            dos.writeInt(((MLStructure) array).getMaxFieldLenth());
            // get field names
            tag = new OSArrayTag(MatDataTypes.miINT8, ((MLStructure) array).getKeySetToByteArray());
            tag.writeTo(dos);
            for (MLArray a : ((MLStructure) array).getAllFields()) {
                writeMatrix(dos, a);
            }
            break;
        case MLArray.mxCELL_CLASS:
            for (MLArray a : ((MLCell) array).cells()) {
                writeMatrix(dos, a);
            }
            break;
        case MLArray.mxSPARSE_CLASS:
            int[] ai;
            // write ir
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            ai = ((MLSparse) array).getIR();
            for (int i : ai) {
                bufferDOS.writeInt(i);
            }
            tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
            tag.writeTo(dos);
            // write jc
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            ai = ((MLSparse) array).getJC();
            for (int i : ai) {
                bufferDOS.writeInt(i);
            }
            tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
            tag.writeTo(dos);
            // write real
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            Double[] ad = ((MLSparse) array).exportReal();
            for (int i = 0; i < ad.length; i++) {
                bufferDOS.writeDouble(ad[i].doubleValue());
            }
            tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                buffer = new ByteArrayOutputStream();
                bufferDOS = new DataOutputStream(buffer);
                ad = ((MLSparse) array).exportImaginary();
                for (int i = 0; i < ad.length; i++) {
                    bufferDOS.writeDouble(ad[i].doubleValue());
                }
                tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
                tag.writeTo(dos);
            }
            break;
        default:
            throw new MatlabIOException("Cannot write matrix of type: " + MLArray.typeToString(array.getType()));
    }
    // write matrix
    // matrix tag
    output.writeInt(MatDataTypes.miMATRIX);
    // size of matrix
    output.writeInt(baos.size());
    // matrix data
    output.write(baos.toByteArray());
}
Also used : MLChar(com.jmatio.types.MLChar) MLArray(com.jmatio.types.MLArray) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MLSparse(com.jmatio.types.MLSparse) MLStructure(com.jmatio.types.MLStructure) MLCell(com.jmatio.types.MLCell) MLNumericArray(com.jmatio.types.MLNumericArray)

Example 2 with MLSparse

use of com.jmatio.types.MLSparse in project org.csstudio.display.builder by kasemir.

the class MatFileWriter method writeMatrix.

/**
 * Writes MATRIX into <code>OutputStream</code>.
 *
 * @param os - <code>OutputStream</code>
 * @param array - a <code>MLArray</code>
 * @throws IOException
 */
private void writeMatrix(DataOutputStream output, MLArray array) throws IOException {
    OSArrayTag tag;
    ByteArrayOutputStream buffer;
    DataOutputStream bufferDOS;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    // flags
    writeFlags(dos, array);
    // dimensions
    writeDimensions(dos, array);
    // array name
    writeName(dos, array);
    switch(array.getType()) {
        case MLArray.mxCHAR_CLASS:
            // write char data
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            Character[] ac = ((MLChar) array).exportChar();
            for (int i = 0; i < ac.length; i++) {
                bufferDOS.writeByte((byte) ac[i].charValue());
            }
            tag = new OSArrayTag(MatDataTypes.miUTF8, buffer.toByteArray());
            tag.writeTo(dos);
            break;
        case MLArray.mxDOUBLE_CLASS:
            tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxSINGLE_CLASS:
            tag = new OSArrayTag(MatDataTypes.miSINGLE, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miSINGLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxUINT8_CLASS:
            tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxINT8_CLASS:
            tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxINT64_CLASS:
            tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxUINT64_CLASS:
            tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
                tag.writeTo(dos);
            }
            break;
        case MLArray.mxSTRUCT_CLASS:
            // field name length
            int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
            dos.writeInt(itag);
            dos.writeInt(((MLStructure) array).getMaxFieldLenth());
            // get field names
            tag = new OSArrayTag(MatDataTypes.miINT8, ((MLStructure) array).getKeySetToByteArray());
            tag.writeTo(dos);
            for (MLArray a : ((MLStructure) array).getAllFields()) {
                writeMatrix(dos, a);
            }
            break;
        case MLArray.mxCELL_CLASS:
            for (MLArray a : ((MLCell) array).cells()) {
                writeMatrix(dos, a);
            }
            break;
        case MLArray.mxSPARSE_CLASS:
            int[] ai;
            // write ir
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            ai = ((MLSparse) array).getIR();
            for (int i : ai) {
                bufferDOS.writeInt(i);
            }
            tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
            tag.writeTo(dos);
            // write jc
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            ai = ((MLSparse) array).getJC();
            for (int i : ai) {
                bufferDOS.writeInt(i);
            }
            tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
            tag.writeTo(dos);
            // write real
            buffer = new ByteArrayOutputStream();
            bufferDOS = new DataOutputStream(buffer);
            Double[] ad = ((MLSparse) array).exportReal();
            for (int i = 0; i < ad.length; i++) {
                bufferDOS.writeDouble(ad[i].doubleValue());
            }
            tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
            tag.writeTo(dos);
            // write real imaginary
            if (array.isComplex()) {
                buffer = new ByteArrayOutputStream();
                bufferDOS = new DataOutputStream(buffer);
                ad = ((MLSparse) array).exportImaginary();
                for (int i = 0; i < ad.length; i++) {
                    bufferDOS.writeDouble(ad[i].doubleValue());
                }
                tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
                tag.writeTo(dos);
            }
            break;
        default:
            throw new MatlabIOException("Cannot write matrix of type: " + MLArray.typeToString(array.getType()));
    }
    // write matrix
    // matrix tag
    output.writeInt(MatDataTypes.miMATRIX);
    // size of matrix
    output.writeInt(baos.size());
    // matrix data
    output.write(baos.toByteArray());
}
Also used : MLChar(com.jmatio.types.MLChar) MLArray(com.jmatio.types.MLArray) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MLSparse(com.jmatio.types.MLSparse) MLStructure(com.jmatio.types.MLStructure) MLCell(com.jmatio.types.MLCell) MLNumericArray(com.jmatio.types.MLNumericArray)

Example 3 with MLSparse

use of com.jmatio.types.MLSparse in project org.csstudio.display.builder by kasemir.

the class MatFileReader method readMatrix.

/**
 * Reads miMATRIX from from input stream.
 *
 * If reading was not finished (which is normal for filtered results)
 * returns <code>null</code>.
 *
 * Modifies <code>buf</code> position to the position when reading
 * finished.
 *
 * Uses recursive processing for some ML**** data types.
 *
 * @param buf -
 *            input byte buffer
 * @param isRoot -
 *            when <code>true</code> informs that if this is a top level
 *            matrix
 * @return - <code>MLArray</code> or <code>null</code> if matrix does
 *         not match <code>filter</code>
 * @throws IOException when error occurs while reading the buffer.
 */
private MLArray readMatrix(ByteBuffer buf, boolean isRoot) throws IOException {
    // result
    MLArray mlArray;
    ISMatTag tag;
    // read flags
    int[] flags = readFlags(buf);
    int attributes = (flags.length != 0) ? flags[0] : 0;
    int nzmax = (flags.length != 0) ? flags[1] : 0;
    int type = attributes & 0xff;
    // read Array dimension
    int[] dims = readDimension(buf);
    // read array Name
    String name = readName(buf);
    // if this array is filtered out return immediately
    if (isRoot && !filter.matches(name)) {
        return null;
    }
    // read data >> consider changing it to stategy pattern
    switch(type) {
        case MLArray.mxSTRUCT_CLASS:
            MLStructure struct = new MLStructure(name, dims, type, attributes);
            // field name length - this subelement always uses the compressed data element format
            tag = new ISMatTag(buf);
            // maximum field length
            int maxlen = buf.getInt();
            // ////  read fields data as Int8
            tag = new ISMatTag(buf);
            // calculate number of fields
            int numOfFields = tag.size / maxlen;
            // padding after field names
            int padding = (tag.size % 8) != 0 ? 8 - (tag.size % 8) : 0;
            String[] fieldNames = new String[numOfFields];
            for (int i = 0; i < numOfFields; i++) {
                byte[] names = new byte[maxlen];
                buf.get(names);
                fieldNames[i] = zeroEndByteArrayToString(names);
            }
            buf.position(buf.position() + padding);
            // read fields
            for (int index = 0; index < struct.getM() * struct.getN(); index++) {
                for (int i = 0; i < numOfFields; i++) {
                    // read matrix recursively
                    tag = new ISMatTag(buf);
                    if (tag.size > 0) {
                        MLArray fieldValue = readMatrix(buf, false);
                        struct.setField(fieldNames[i], fieldValue, index);
                    } else {
                        struct.setField(fieldNames[i], new MLEmptyArray(), index);
                    }
                }
            }
            mlArray = struct;
            break;
        case MLArray.mxCELL_CLASS:
            MLCell cell = new MLCell(name, dims, type, attributes);
            for (int i = 0; i < cell.getM() * cell.getN(); i++) {
                tag = new ISMatTag(buf);
                if (tag.size > 0) {
                    // read matrix recursively
                    MLArray cellmatrix = readMatrix(buf, false);
                    cell.set(cellmatrix, i);
                } else {
                    cell.set(new MLEmptyArray(), i);
                }
            }
            mlArray = cell;
            break;
        case MLArray.mxDOUBLE_CLASS:
            mlArray = new MLDouble(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxSINGLE_CLASS:
            mlArray = new MLSingle(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxUINT8_CLASS:
            mlArray = new MLUInt8(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxINT8_CLASS:
            mlArray = new MLInt8(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxINT64_CLASS:
            mlArray = new MLInt64(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxUINT64_CLASS:
            mlArray = new MLUInt64(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getRealByteBuffer(), (MLNumericArray<?>) mlArray);
            // read complex
            if (mlArray.isComplex()) {
                tag = new ISMatTag(buf);
                tag.readToByteBuffer(((MLNumericArray<?>) mlArray).getImaginaryByteBuffer(), (MLNumericArray<?>) mlArray);
            }
            break;
        case MLArray.mxCHAR_CLASS:
            MLChar mlchar = new MLChar(name, dims, type, attributes);
            // read real
            tag = new ISMatTag(buf);
            char[] ac = tag.readToCharArray();
            for (int i = 0; i < ac.length; i++) {
                mlchar.setChar(ac[i], i);
            }
            mlArray = mlchar;
            break;
        case MLArray.mxSPARSE_CLASS:
            MLSparse sparse = new MLSparse(name, dims, attributes, nzmax);
            // read ir (row indices)
            tag = new ISMatTag(buf);
            int[] ir = tag.readToIntArray();
            // read jc (column count)
            tag = new ISMatTag(buf);
            int[] jc = tag.readToIntArray();
            // read pr (real part)
            tag = new ISMatTag(buf);
            double[] ad1 = tag.readToDoubleArray();
            int count = 0;
            for (int column = 0; column < sparse.getN(); column++) {
                while (count < jc[column + 1]) {
                    sparse.setReal(ad1[count], ir[count], column);
                    count++;
                }
            }
            // read pi (imaginary part)
            if (sparse.isComplex()) {
                tag = new ISMatTag(buf);
                double[] ad2 = tag.readToDoubleArray();
                count = 0;
                for (int column = 0; column < sparse.getN(); column++) {
                    while (count < jc[column + 1]) {
                        sparse.setImaginary(ad2[count], ir[count], column);
                        count++;
                    }
                }
            }
            mlArray = sparse;
            break;
        // break;
        default:
            throw new MatlabIOException("Incorrect matlab array class: " + MLArray.typeToString(type));
    }
    return mlArray;
}
Also used : MLDouble(com.jmatio.types.MLDouble) MLChar(com.jmatio.types.MLChar) MLArray(com.jmatio.types.MLArray) MLInt64(com.jmatio.types.MLInt64) MLUInt64(com.jmatio.types.MLUInt64) MLSparse(com.jmatio.types.MLSparse) MLStructure(com.jmatio.types.MLStructure) MLCell(com.jmatio.types.MLCell) MLSingle(com.jmatio.types.MLSingle) MLEmptyArray(com.jmatio.types.MLEmptyArray) MLInt8(com.jmatio.types.MLInt8) MLUInt8(com.jmatio.types.MLUInt8)

Aggregations

MLArray (com.jmatio.types.MLArray)3 MLCell (com.jmatio.types.MLCell)3 MLChar (com.jmatio.types.MLChar)3 MLSparse (com.jmatio.types.MLSparse)3 MLStructure (com.jmatio.types.MLStructure)3 MLNumericArray (com.jmatio.types.MLNumericArray)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 MLDouble (com.jmatio.types.MLDouble)1 MLEmptyArray (com.jmatio.types.MLEmptyArray)1 MLInt64 (com.jmatio.types.MLInt64)1 MLInt8 (com.jmatio.types.MLInt8)1 MLSingle (com.jmatio.types.MLSingle)1 MLUInt64 (com.jmatio.types.MLUInt64)1 MLUInt8 (com.jmatio.types.MLUInt8)1