Search in sources :

Example 1 with MLUInt64

use of com.jmatio.types.MLUInt64 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)1 MLCell (com.jmatio.types.MLCell)1 MLChar (com.jmatio.types.MLChar)1 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 MLSparse (com.jmatio.types.MLSparse)1 MLStructure (com.jmatio.types.MLStructure)1 MLUInt64 (com.jmatio.types.MLUInt64)1 MLUInt8 (com.jmatio.types.MLUInt8)1