Пример #1
0
bool
PDBFileObject::GetIntegerArray(const char *name, int **i, int *nvals)
{
    int *dims = 0;
    int nDims = 0;
    int length = 0;
    TypeEnum t = NO_TYPE;

    // Initially set the return value to zero.
    *i = 0;
    *nvals = 0;

    if(AutoOpen())
    {
        // Read the variable.
        void *val = ReadValues(name, &t, &length, &dims, &nDims);

        // Set up the return array.
        if(val)
        {
            free_mem(dims);
            if(t == INTEGER_TYPE || t == INTEGERARRAY_TYPE)
            {
                *i = (int *)val;
                *nvals = length;
            }
            else
                pdb_free_void_mem(val, t);
        }
    }

    return *i != 0;
}
vtkFloatArray *
Streaker::AssembleData(const std::string &var, int *sdims, int slice, int sliceIndex, 
    const PDBFileObjectVector &pdb) const
{
    const char *mName = "Streaker::AssembleData: ";

    // Let's assemble vtkDataArray from the slices over time.
    debug4 << mName << "Creating new double array sized: "
           << (sdims[0] * sdims[1] * sdims[2]) << endl;
    vtkFloatArray *arr = vtkFloatArray::New();
    arr->SetNumberOfTuples(sdims[0] * sdims[1] * sdims[2]);
    arr->SetName(var.c_str());
    float *dest = (float *)arr->GetVoidPointer(0);
    memset(dest, 0, sizeof(float) * sdims[0] * sdims[1] * sdims[2]);
    int ti = 0;
    for(int r = 0; r < pdb.size(); ++r)
    {
        int *dims = 0;
        int nDims = 0;
        int length = 0;
        TypeEnum t = NO_TYPE;
        void *val = pdb[r]->ReadValues(var.c_str(), &t, &length, &dims, &nDims);
        if(val)
        {
            debug4 << mName << "Read " << var.c_str() << " from " << pdb[r]->GetName().c_str() << endl;
            debug4 << "\tdims = {";
            for(int i = 0; i < nDims; ++i)
                debug4 << dims[i] << " ";
            debug4 << "}" << endl;
            if(t == DOUBLEARRAY_TYPE)
            {
                debug4 << "Storing double values" << endl;
                double *src = (double *)val;
                ti = StoreValues<double>(dest, src, ti, sdims, dims, nDims, slice, sliceIndex);
            }
            else if(t == FLOATARRAY_TYPE)
            {
                debug4 << "Storing float values" << endl;
                float *src = (float *)val;
                ti = StoreValues<float>(dest, src, ti, sdims, dims, nDims, slice, sliceIndex);
            }
            else if(t == INTEGERARRAY_TYPE)
            {
                debug4 << "Storing int values" << endl;
                int *src = (int *)val;
                ti = StoreValues<int>(dest, src, ti, sdims, dims, nDims, slice, sliceIndex);
            }
            else
                debug4 << "Unsupported type" << endl;

            pdb_free_void_mem(val, t);
            delete [] dims;
        }

        // Close the file so we don't get too many files open.
        pdb[r]->Close();
    }

    return arr;
}
Пример #3
0
bool
PDBFileObject::GetDoubleArray(const char *name, double **d, int *nvals)
{
    int *dims = 0;
    int nDims = 0;
    int length = 0;
    TypeEnum t = NO_TYPE;

    // Initially set the return value to zero.
    *d = 0;
    *nvals = 0;

    if(AutoOpen())
    {
        // Read the variable.
        void *val = ReadValues(name, &t, &length, &dims, &nDims);

        // Set up the return array.
        if(val)
        {
            free_mem(dims);
            if(t == DOUBLE_TYPE || t == DOUBLEARRAY_TYPE)
            {
                *d = (double *)val;
                *nvals = length;
            }
            else if(t == FLOAT_TYPE || t == FLOATARRAY_TYPE)
            {
                float *fptr = (float *)val;
                double *dstorage = new double[length];
                double *dptr = dstorage;
                for(int i = 0; i < length; ++i)
                    *dptr++ = double(*fptr++);
                pdb_free_void_mem(val, t);

                *d = dstorage;
                *nvals = length;
            }
            else
                pdb_free_void_mem(val, t);
        }
    }

    return *d != 0;
}
Пример #4
0
bool
PDBFileObject::GetString(const char *name, char **str, int *len)
{
    int *dims = 0;
    int nDims = 0;
    int length = 0;
    TypeEnum t = NO_TYPE;

    // Initially set the return value to zero.
    *str = 0;

    if(AutoOpen())
    {
        // Read the variable.
        void *val = ReadValues(name, &t, &length, &dims, &nDims, 1);

        // Set up the return array.
        if(val)
        {
            free_mem(dims);
            if(t == CHAR_TYPE || t == CHARARRAY_TYPE)
            {
                *str = (char *)val;
                // We allocated the array so it can hold the extra
                // character required for a NULL terminator.
                (*str)[length] = '\0';

                if(len != 0)
                    *len = length;
            }
            else
                pdb_free_void_mem(val, t);
        }
    }

    return *str != 0;
}
Пример #5
0
void *
PDBFileObject::ReadValues(const char *name, TypeEnum *t, int *nTotalElements,
    int **dimensions, int *nDims, int extraElements)
{
    void *retval = 0;

    if(AutoOpen())
    {
        //
        // Get information about the variable if it is in the file.
        //
        if(SymbolExists(name, t, nTotalElements, dimensions, nDims))
        {
            int nels = *nTotalElements + extraElements;

            //
            // Allocate memory for the variable.
            //
            switch(*t)
            {
            case CHAR_TYPE:
            case CHARARRAY_TYPE:
                retval = (void *)new char[nels];
                break;
            case SHORT_TYPE:
            case SHORTARRAY_TYPE:
                retval = (void *)new short[nels];
                break;
            case INTEGER_TYPE:
            case INTEGERARRAY_TYPE:
                retval = (void *)new int[nels];
                break;
            case FLOAT_TYPE:
            case FLOATARRAY_TYPE:
                retval = (void *)new float[nels];
                break;
            case DOUBLE_TYPE:
            case DOUBLEARRAY_TYPE:
                retval = (void *)new double[nels];
                break;
            case LONG_TYPE:
            case LONGARRAY_TYPE:
                retval = (void *)new long[nels];
                break;
            default:
                EXCEPTION1(InvalidVariableException, "unsupported type");
            }

            //
            // Try reading the variable from the file.
            //
            if(PD_read(pdb, (char *)name, retval) == FALSE)
            {
                debug4 << "PDBFileObject::ReadValues: PD_read failed for " << name
                       << ". " << PDBLIB_ERRORSTRING << endl;

                pdb_free_void_mem(retval, *t);
                retval = 0;
                *t = NO_TYPE;
                *nTotalElements = 0;
                free_mem(*dimensions);
                *dimensions = 0;
                *nDims = 0;
            }
        }
        else
        {
            debug4 << "PDBFileObject::ReadValues: Variable \"" << name
                   << "\" could not be located. " << PDBLIB_ERRORSTRING << endl;
        }
    }

    return retval;
}