2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
20 #include "localization.h"
22 #include "api_scilab.h"
23 #include "../../../call_scilab/includes/call_scilab.h"
24 #include "h5_fileManagement.h"
25 #include "h5_readDataFromFile.h"
26 #include "intmacr2tree.h"
27 #include "expandPathVariable.h"
29 #include "forceJHDF5load.hxx"
35 static int iCloseList = 0;
37 void print_tree(char *_pstMsg);
39 static bool import_variable(int _iFile, char* _pstVarName);
40 static bool import_data(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
41 static bool import_double(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
42 static bool import_string(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
43 static bool import_boolean(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
44 static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
45 static bool import_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
46 static bool import_boolean_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
47 static bool import_poly(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
48 static bool import_list(int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname);
49 static bool import_void(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
50 static bool import_undefined(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
52 int sci_import_from_hdf5(char *fname, unsigned long fname_len)
61 char* pstFilename = NULL;
62 char* pstExpandedFilename = NULL;
65 int iSelectedVar = Rhs - 1;
66 bool bSelectedVar = false;
68 checkInputArgumentAtLeast(pvApiCtx, 1);
77 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
80 printError(&sciErr, 0);
84 if(getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename))
86 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
91 pstExpandedFilename = expandPathVariable(pstFilename);
92 int iFile = openHDF5File(pstExpandedFilename);
95 FREE(pstExpandedFilename);
97 Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFilename);
101 FREE(pstExpandedFilename);
107 char* pstVarName = NULL;
108 for(int i = 0 ; i < iSelectedVar ; i++)
110 sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr);
113 printError(&sciErr, 0);
117 if(getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName))
119 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, i + 1);
123 if(import_variable(iFile, pstVarName) == false)
135 iNbItem = getVariableNames(iFile, NULL);
138 char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem);
140 iNbItem = getVariableNames(iFile, pstVarNameList);
143 for (int i = 0; i < iNbItem; i++)
145 if(import_variable(iFile, pstVarNameList[i]) == false)
154 closeHDF5File(iFile);
156 int *piReturn = NULL;
158 sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, &piReturn);
161 printError(&sciErr, 0);
177 // printf("End gateway !!!\n");
181 static bool import_variable(int _iFile, char* _pstVarName)
183 int iDataSetId = getDataSetIdFromName(_iFile, _pstVarName);
189 return import_data(iDataSetId, 0, NULL, _pstVarName);
192 static bool import_data(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
197 int iVarType = getScilabTypeFromDataSet(_iDatasetId);
203 bRet = import_double(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
208 bRet = import_string(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
215 bRet = import_list(_iDatasetId, iVarType, _iItemPos, _piAddress, _pstVarname);
220 bRet = import_boolean(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
225 bRet = import_poly(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
230 bRet = import_integer(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
235 bRet = import_sparse(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
238 case sci_boolean_sparse:
240 bRet = import_boolean_sparse(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
243 case sci_void: //void item only on list variable
245 bRet = import_void(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
248 case sci_undefined: //undefined item only on list variable
250 bRet = import_undefined(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
255 Scierror(999, _("%s: Invalid HDF5 Scilab format.\n"), "import_from_hdf5");
260 sprintf(pstMsg, "Unknown type : %d", iVarType);
270 static bool import_void(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
276 sciErr = createVoidInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos);
285 printError(&sciErr, 0);
291 static bool import_undefined(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
297 sciErr = createUndefinedInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos);
306 printError(&sciErr, 0);
312 static bool import_double(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
315 double *pdblReal = NULL;
316 double *pdblImg = NULL;
323 LARGE_INTEGER iStart, iEnd, iFreq;
325 QueryPerformanceFrequency(&iFreq);
326 QueryPerformanceCounter(&iStart);
329 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
330 iComplex = isComplexData(_iDatasetId);
336 if (iRows * iCols != 0)
340 pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
341 pdblImg = (double *)MALLOC(iRows * iCols * sizeof(double));
342 iRet = readDoubleComplexMatrix(_iDatasetId, iRows, iCols, pdblReal, pdblImg);
346 pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
347 iRet = readDoubleMatrix(_iDatasetId, iRows, iCols, pdblReal);
357 /*bug 7224 : to close dataset */
358 iRet = readEmptyMatrix(_iDatasetId);
365 if (_piAddress == NULL)
369 sciErr = createNamedComplexMatrixOfDouble(pvApiCtx, _pstVarname, iRows, iCols, pdblReal, pdblImg);
373 sciErr = createNamedMatrixOfDouble(pvApiCtx, _pstVarname, iRows, iCols, pdblReal);
376 else //if not null this variable is in a list
380 sciErr = createComplexMatrixOfDoubleInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal, pdblImg);
384 sciErr = createMatrixOfDoubleInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal);
390 printError(&sciErr, 0);
397 sprintf(pstMsg, "double_%d (%d x %d)", _iItemPos, iRows, iCols);
417 QueryPerformanceCounter(&iEnd);
418 double dblTime = ((iEnd.QuadPart - iStart.QuadPart) * 1000.0) / iFreq.QuadPart;
420 printf("Total Double : %0.3f ms\n\n", dblTime);
426 static bool import_string(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
432 char **pstData = NULL;
436 LARGE_INTEGER iStart1, iEnd1, iStart2, iEnd2, iStart3, iEnd3, iFreq;
438 QueryPerformanceFrequency(&iFreq);
439 QueryPerformanceCounter(&iStart1);
441 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
447 pstData = (char **)MALLOC(iRows * iCols * sizeof(char *));
450 QueryPerformanceCounter(&iStart1);
453 iRet = readStringMatrix(_iDatasetId, iRows, iCols, pstData);
460 QueryPerformanceCounter(&iEnd1);
463 QueryPerformanceCounter(&iStart2);
466 if (_piAddress == NULL)
468 sciErr = createNamedMatrixOfString(pvApiCtx, _pstVarname, iRows, iCols, pstData);
470 else //if not null this variable is in a list
472 sciErr = createMatrixOfStringInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pstData);
477 printError(&sciErr, 0);
482 QueryPerformanceCounter(&iEnd2);
483 QueryPerformanceCounter(&iStart3);
488 sprintf(pstMsg, "string_%d (%d x %d)", _iItemPos, iRows, iCols);
491 for (i = 0; i < iRows * iCols; i++)
503 QueryPerformanceCounter(&iEnd3);
505 //double dblTime =((iEnd1.QuadPart - iStart1.QuadPart) * 1000.0) / iFreq.QuadPart;
506 //printf("HDF5 : %0.3f ms\n", dblTime);
507 //dblTime =((iEnd2.QuadPart - iStart2.QuadPart) * 1000.0) / iFreq.QuadPart;
508 //printf("Stack : %0.3f ms\n", dblTime);
509 //dblTime =((iEnd3.QuadPart - iStart3.QuadPart) * 1000.0) / iFreq.QuadPart;
510 //printf("Clear : %0.3f ms\n", dblTime);
511 double dblTime = ((iEnd3.QuadPart - iStart1.QuadPart) * 1000.0) / iFreq.QuadPart;
513 printf("Total String: %0.3f ms\n\n", dblTime);
518 static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
526 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
532 iRet = getDatasetPrecision(_iDatasetId, &iPrec);
544 pcData = (char *)MALLOC(sizeof(char) * iRows * iCols);
545 iRet = readInterger8Matrix(_iDatasetId, iRows, iCols, pcData);
551 if (_piAddress == NULL)
553 sciErr = createNamedMatrixOfInteger8(pvApiCtx, _pstVarname, iRows, iCols, pcData);
557 sciErr = createMatrixOfInteger8InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pcData);
563 unsigned char *pucData = NULL;
565 pucData = (unsigned char *)MALLOC(sizeof(unsigned char) * iRows * iCols);
566 iRet = readUnsignedInterger8Matrix(_iDatasetId, iRows, iCols, pucData);
572 if (_piAddress == NULL)
574 sciErr = createNamedMatrixOfUnsignedInteger8(pvApiCtx, _pstVarname, iRows, iCols, pucData);
578 sciErr = createMatrixOfUnsignedInteger8InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pucData);
584 short *psData = NULL;
586 psData = (short *)MALLOC(sizeof(short) * iRows * iCols);
587 iRet = readInterger16Matrix(_iDatasetId, iRows, iCols, psData);
593 if (_piAddress == NULL)
595 sciErr = createNamedMatrixOfInteger16(pvApiCtx, _pstVarname, iRows, iCols, psData);
599 sciErr = createMatrixOfInteger16InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, psData);
605 unsigned short *pusData = NULL;
607 pusData = (unsigned short *)MALLOC(sizeof(unsigned short) * iRows * iCols);
608 iRet = readUnsignedInterger16Matrix(_iDatasetId, iRows, iCols, pusData);
614 if (_piAddress == NULL)
616 sciErr = createNamedMatrixOfUnsignedInteger16(pvApiCtx, _pstVarname, iRows, iCols, pusData);
620 sciErr = createMatrixOfUnsignedInteger16InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pusData);
628 piData = (int *)MALLOC(sizeof(int) * iRows * iCols);
629 iRet = readInterger32Matrix(_iDatasetId, iRows, iCols, piData);
635 if (_piAddress == NULL)
637 sciErr = createNamedMatrixOfInteger32(pvApiCtx, _pstVarname, iRows, iCols, piData);
641 sciErr = createMatrixOfInteger32InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, piData);
647 unsigned int *puiData = NULL;
649 puiData = (unsigned int *)MALLOC(sizeof(unsigned int) * iRows * iCols);
650 iRet = readUnsignedInterger32Matrix(_iDatasetId, iRows, iCols, puiData);
656 if (_piAddress == NULL)
658 sciErr = createNamedMatrixOfUnsignedInteger32(pvApiCtx, _pstVarname, iRows, iCols, puiData);
662 sciErr = createMatrixOfUnsignedInteger32InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, puiData);
668 #ifdef __SCILAB_INT64__
669 long long *pllData = NULL;
671 pllData = (long long *)MALLOC(sizeof(long long) * iRows * iCols);
672 iRet = readInterger64Matrix(_iDatasetId, iRows, iCols, pllData);
678 if (_piAddress == NULL)
680 sciErr = createNamedMatrixOfInteger64(_pstVarname, iRows, iCols, pllData);
684 sciErr = createMatrixOfInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, iRows, iCols, pllData);
693 #ifdef __SCILAB_INT64__
694 unsigned long long *pullData = NULL;
696 pullData = (unsigned long long *)MALLOC(sizeof(unsigned long long) * iRows * iCols);
697 iRet = readUnsignedInterger64Matrix(_iDatasetId, iRows, iCols, pullData);
703 if (_piAddress == NULL)
705 sciErr = createNamedMatrixOfUnsignedInteger64(_pstVarname, iRows, iCols, pullData);
709 sciErr = createMatrixOfUnsignedInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, iRows, iCols, pullData);
723 sprintf(pstMsg, "integer_%d (%d x %d)", _iItemPos, iRows, iCols);
729 printError(&sciErr, 0);
736 static bool import_boolean(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
744 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
750 if (iRows * iCols != 0)
752 piData = (int *)MALLOC(iRows * iCols * sizeof(int));
753 iRet = readBooleanMatrix(_iDatasetId, iRows, iCols, piData);
760 if (_piAddress == NULL)
762 sciErr = createNamedMatrixOfBoolean(pvApiCtx, _pstVarname, iRows, iCols, piData);
764 else //if not null this variable is in a list
766 sciErr = createMatrixOfBooleanInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, piData);
771 printError(&sciErr, 0);
778 sprintf(pstMsg, "boolean_%d (%d x %d)", _iItemPos, iRows, iCols);
795 static bool import_poly(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
802 char pstVarName[64] = { 0 };
803 double **pdblReal = NULL;
804 double **pdblImg = NULL;
805 int *piNbCoef = NULL;
808 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
814 iComplex = isComplexData(_iDatasetId);
818 piNbCoef = (int *)MALLOC(iRows * iCols * sizeof(int));
819 pdblReal = (double **)MALLOC(iRows * iCols * sizeof(double *));
820 pdblImg = (double **)MALLOC(iRows * iCols * sizeof(double *));
821 iRet = readPolyComplexMatrix(_iDatasetId, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
825 piNbCoef = (int *)MALLOC(iRows * iCols * sizeof(int));
826 pdblReal = (double **)MALLOC(iRows * iCols * sizeof(double *));
827 iRet = readPolyMatrix(_iDatasetId, pstVarName, iRows, iCols, piNbCoef, pdblReal);
835 if (_piAddress == NULL)
839 sciErr = createNamedComplexMatrixOfPoly(pvApiCtx, _pstVarname, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
843 sciErr = createNamedMatrixOfPoly(pvApiCtx, _pstVarname, pstVarName, iRows, iCols, piNbCoef, pdblReal);
846 else //if not null this variable is in a list
851 createComplexMatrixOfPolyInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, pstVarName, iRows, iCols, piNbCoef, pdblReal,
856 sciErr = createMatrixOfPolyInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, pstVarName, iRows, iCols, piNbCoef, pdblReal);
862 printError(&sciErr, 0);
869 sprintf(pstMsg, "poly_%d (%d x %d)", _iItemPos, iRows, iCols);
873 for (i = 0; i < iRows * iCols; i++)
888 static bool import_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
894 double *pdblReal = NULL;
895 double *pdblImg = NULL;
897 int *piNbItemRow = NULL;
898 int *piColPos = NULL;
901 iRet = getSparseDimension(_iDatasetId, &iRows, &iCols, &iNbItem);
907 iComplex = isComplexData(_iDatasetId);
911 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
912 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
913 pdblReal = (double *)MALLOC(iNbItem * sizeof(double));
914 pdblImg = (double *)MALLOC(iNbItem * sizeof(double));
915 iRet = readSparseComplexMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg);
919 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
920 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
921 pdblReal = (double *)MALLOC(iNbItem * sizeof(double));
922 iRet = readSparseMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
930 if (_piAddress == NULL)
934 sciErr = createNamedComplexSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg);
938 sciErr = createNamedSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
941 else //if not null this variable is in a list
946 createComplexSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos,
952 createSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
958 printError(&sciErr, 0);
965 sprintf(pstMsg, "sparse_%d (%d x %d)", _iItemPos, iRows, iCols);
985 static bool import_boolean_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
991 int *piNbItemRow = NULL;
992 int *piColPos = NULL;
995 iRet = getSparseDimension(_iDatasetId, &iRows, &iCols, &iNbItem);
1001 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
1002 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
1003 iRet = readBooleanSparseMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1009 if (_piAddress == NULL)
1011 sciErr = createNamedBooleanSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1013 else //if not null this variable is in a list
1015 sciErr = createBooleanSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1020 printError(&sciErr, 0);
1027 sprintf(pstMsg, "boolean sparse_%d (%d x %d)", _iItemPos, iRows, iCols);
1042 static bool import_list(int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname)
1047 int *piListAddr = NULL;
1048 hobj_ref_t *piItemRef = NULL;
1051 iRet = getListDims(_iDatasetId, &iItems);
1058 { //special case for empty list
1062 iRet = getListItemReferences(_iDatasetId, &piItemRef);
1072 sprintf(pstMsg, "list_%d (%d)", _iItemPos, iItems);
1076 if (_piAddress == 0)
1081 sciErr = createNamedList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1084 sciErr = createNamedTList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1087 sciErr = createNamedMList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1093 else //if not null this variable is in a list
1098 sciErr = createListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1101 sciErr = createTListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1104 sciErr = createMListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1113 printError(&sciErr, 0);
1118 for (i = 0; i < iItems; i++)
1120 int iItemDataset = 0;
1122 iRet = getListItemDataset(_iDatasetId, piItemRef, i, &iItemDataset);
1123 if (iRet || iItemDataset == 0)
1128 bool bRet = import_data(iItemDataset, i + 1, piListAddr, _pstVarname);
1137 iRet = deleteListItemReferences(_iDatasetId, piItemRef);
1144 printf("Close List %d\n\n", iCloseList++);
1150 sprintf(pstMsg1, "ListEnd_%d", _iItemPos);
1151 print_tree(pstMsg1);
1156 void print_tree(char *_pstMsg)
1159 for (int i = 0; i < iTab; i++)
1163 printf("%s\n", _pstMsg);
1167 /*--------------------------------------------------------------------------*/