Beispiel #1
0
types::Function::ReturnValue sci_hdf5_listvar_v3(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int rhs = static_cast<int>(in.size());
    if (rhs != 1)
    {
        Scierror(999, _("%s: Wrong number of input argument(s): %d expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    if (_iRetCount < 1 && _iRetCount > 4)
    {
        Scierror(999, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), fname.data(), 1, 4);
        return types::Function::Error;
    }

    //get filename
    if (in[0]->getId() != types::InternalType::IdScalarString)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    //open hdf5 file
    wchar_t* wfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]);
    char* cfilename = wide_string_to_UTF8(wfilename);
    std::string filename = cfilename;
    FREE(wfilename);
    FREE(cfilename);

    int iFile = openHDF5File(filename.data(), 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data());
        return types::Function::Error;
    }

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        //can't read file with version newer that me !
        closeHDF5File(iFile);
        Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, iVersion);
        return types::Function::Error;
    }

    int items = getVariableNames6(iFile, nullptr);
    std::vector<VarInfo6> info(items);
    if (items != 0)
    {
        std::vector<char*> vars(items);
        items = getVariableNames6(iFile, vars.data());

        if (_iRetCount == 1)
        {
            sciprint("Name                     Type           Size            Bytes\n");
            sciprint("-------------------------------------------------------------\n");
        }

        for (int i = 0; i < items; i++)
        {
            info[i].name = vars[i];
            FREE(vars[i]);
            info[i].size = 0;

            int dset = getDataSetIdFromName(iFile, info[i].name.data());
            if (dset == 0)
            {
                break;
            }


            if (_iRetCount != 2)
            {
                if (read_data(dset, info[i]) == false)
                {
                    break;
                }

                if (_iRetCount == 1)
                {
                    sciprint("%s\n", info[i].info);
                }
            }
            else // == 2
            {
                if (read_short_data(dset, info[i]) == false)
                {
                    break;
                }
            }
        }
    }
    else
    {
        //no variable returms [] for each Lhs
        for (int i = 0; i < _iRetCount; i++)
        {
            out.push_back(types::Double::Empty());
        }

        return types::Function::OK;
    }

    closeHDF5File(iFile);

    //1st Lhs
    types::String* out1 = new types::String(items, 1);
    for (int i = 0; i < items; i++)
    {
        out1->set(i, info[i].name.data());
    }

    out.push_back(out1);

    //2nd
    if (_iRetCount > 1)
    {
        types::Double* out2 = new types::Double(items, 1);
        double* pout2 = out2->get();
        for (int i = 0; i < items; i++)
        {
            pout2[i] = info[i].type;
        }

        out.push_back(out2);
    }

    if (_iRetCount > 2)
    {
        //3rd Lhs
        types::List* out3 = new types::List();
        for (int i = 0; i < items; i++)
        {
            int dims = info[i].dims;
            types::Double* item = new types::Double(1, dims);
            double* pitem = item->get();
            for (int j = 0; j < dims; j++)
            {
                pitem[j] = static_cast<double>(info[i].pdims[j]);
            }

            out3->append(item);
        }

        out.push_back(out3);
    }

    if (_iRetCount > 3)
    {
        //4th Lhs
        types::Double* out4 = new types::Double(items, 1);
        double* pout4 = out4->get();
        for (int i = 0; i < items; i++)
        {
            pout4[i] = info[i].size;
        }

        out.push_back(out4);
    }

    return types::Function::OK;
}
Beispiel #2
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_hdf5_load_v3(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    std::string filename;
    int rhs = static_cast<int>(in.size());

    if (rhs < 1)
    {
        Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    if (in[0]->getId() != types::InternalType::IdScalarString)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    wchar_t* wfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]);
    char* cfilename = wide_string_to_UTF8(wfilename);
    filename = cfilename;
    FREE(wfilename);
    FREE(cfilename);

    int iFile = openHDF5File(filename.data(), 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data());
        return types::Function::Error;
    }

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        //can't read file with version newer that me !
        Scierror(999, _("%s: Wrong SOD file format version. Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, iVersion);
        return types::Function::Error;
    }

    if (rhs > 1)
    {
        for (int i = 1; i < rhs; ++i)
        {
            std::string var;
            char* cvar = wide_string_to_UTF8(in[i]->getAs<types::String>()->get()[0]);
            var = cvar;
            FREE(cvar);

            if (import_variable(iFile, var) == false)
            {
                Scierror(999, _("%s: Unable to load \'%s\'.\n"), fname.data(), var.data());
                return types::Function::Error;
            }
        }
    }
    else
    {
        //restore all variables
        int iNbItem = 0;
        iNbItem = getVariableNames6(iFile, NULL);
        if (iNbItem != 0)
        {
            std::vector<char*> vars(iNbItem);
            iNbItem = getVariableNames6(iFile, vars.data());
            for (auto & var : vars)
            {
                std::string s(var);
                FREE(var);
                if (import_variable(iFile, s) == false)
                {
                    Scierror(999, _("%s: Unable to load \'%s\'.\n"), fname.data(), s.data());
                    return types::Function::Error;
                }
            }
        }
    }

    //close the file
    closeHDF5File(iFile);

    out.push_back(new types::Bool(1));
    return types::Function::OK;
}