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 "h5_attributeConstants.h"
27 #include "intmacr2tree.h"
28 #include "expandPathVariable.h"
30 #include "forceJHDF5load.hxx"
36 static int iCloseList = 0;
38 void print_tree(char *_pstMsg);
40 static bool import_variable(int _iFile, char* _pstVarName);
41 static bool import_data(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
42 static bool import_double(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
43 static bool import_string(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
44 static bool import_boolean(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
45 static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
46 static bool import_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
47 static bool import_boolean_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
48 static bool import_poly(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
49 static bool import_list(int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname);
50 static bool import_void(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
51 static bool import_undefined(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname);
53 int sci_import_from_hdf5(char *fname, unsigned long fname_len)
58 char* pstFilename = NULL;
59 char* pstExpandedFilename = NULL;
62 int iSelectedVar = Rhs - 1;
64 checkInputArgumentAtLeast(pvApiCtx, 1);
73 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
76 printError(&sciErr, 0);
80 if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename))
82 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
87 pstExpandedFilename = expandPathVariable(pstFilename);
88 int iFile = openHDF5File(pstExpandedFilename);
91 FREE(pstExpandedFilename);
93 Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFilename);
97 FREE(pstExpandedFilename);
100 //manage version information
101 int iVersion = getSODFormatAttribute(iFile);
102 if (iVersion > SOD_FILE_VERSION)
103 {//can't read file with version newer that me !
104 Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion);
111 char* pstVarName = NULL;
112 for (int i = 0 ; i < iSelectedVar ; i++)
114 sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr);
117 printError(&sciErr, 0);
121 if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName))
123 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, i + 1);
127 if (import_variable(iFile, pstVarName) == false)
140 iNbItem = getVariableNames(iFile, NULL);
143 char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem);
145 iNbItem = getVariableNames(iFile, pstVarNameList);
148 for (int i = 0; i < iNbItem; i++)
150 if (import_variable(iFile, pstVarNameList[i]) == false)
159 closeHDF5File(iFile);
161 int *piReturn = NULL;
163 sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, &piReturn);
166 printError(&sciErr, 0);
182 // printf("End gateway !!!\n");
186 static bool import_variable(int _iFile, char* _pstVarName)
188 int iDataSetId = getDataSetIdFromName(_iFile, _pstVarName);
194 return import_data(iDataSetId, 0, NULL, _pstVarName);
197 static bool import_data(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
202 int iVarType = getScilabTypeFromDataSet(_iDatasetId);
208 bRet = import_double(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
213 bRet = import_string(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
220 bRet = import_list(_iDatasetId, iVarType, _iItemPos, _piAddress, _pstVarname);
225 bRet = import_boolean(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
230 bRet = import_poly(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
235 bRet = import_integer(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
240 bRet = import_sparse(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
243 case sci_boolean_sparse:
245 bRet = import_boolean_sparse(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
248 case sci_void: //void item only on list variable
250 bRet = import_void(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
253 case sci_undefined: //undefined item only on list variable
255 bRet = import_undefined(_iDatasetId, _iItemPos, _piAddress, _pstVarname);
260 Scierror(999, _("%s: Invalid HDF5 Scilab format.\n"), "import_from_hdf5");
265 sprintf(pstMsg, "Unknown type : %d", iVarType);
275 static bool import_void(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
281 sciErr = createVoidInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos);
290 printError(&sciErr, 0);
296 static bool import_undefined(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
302 sciErr = createUndefinedInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos);
311 printError(&sciErr, 0);
317 static bool import_double(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
320 double *pdblReal = NULL;
321 double *pdblImg = NULL;
328 LARGE_INTEGER iStart, iEnd, iFreq;
330 QueryPerformanceFrequency(&iFreq);
331 QueryPerformanceCounter(&iStart);
334 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
335 iComplex = isComplexData(_iDatasetId);
341 if (iRows * iCols != 0)
345 pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
346 pdblImg = (double *)MALLOC(iRows * iCols * sizeof(double));
347 iRet = readDoubleComplexMatrix(_iDatasetId, iRows, iCols, pdblReal, pdblImg);
351 pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
352 iRet = readDoubleMatrix(_iDatasetId, iRows, iCols, pdblReal);
362 /*bug 7224 : to close dataset */
363 iRet = readEmptyMatrix(_iDatasetId);
370 if (_piAddress == NULL)
374 sciErr = createNamedComplexMatrixOfDouble(pvApiCtx, _pstVarname, iRows, iCols, pdblReal, pdblImg);
378 sciErr = createNamedMatrixOfDouble(pvApiCtx, _pstVarname, iRows, iCols, pdblReal);
381 else //if not null this variable is in a list
385 sciErr = createComplexMatrixOfDoubleInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal, pdblImg);
389 sciErr = createMatrixOfDoubleInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal);
395 printError(&sciErr, 0);
402 sprintf(pstMsg, "double_%d (%d x %d)", _iItemPos, iRows, iCols);
422 QueryPerformanceCounter(&iEnd);
423 double dblTime = ((iEnd.QuadPart - iStart.QuadPart) * 1000.0) / iFreq.QuadPart;
425 printf("Total Double : %0.3f ms\n\n", dblTime);
431 static bool import_string(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
437 char **pstData = NULL;
441 LARGE_INTEGER iStart1, iEnd1, iStart2, iEnd2, iStart3, iEnd3, iFreq;
443 QueryPerformanceFrequency(&iFreq);
444 QueryPerformanceCounter(&iStart1);
446 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
452 pstData = (char **)MALLOC(iRows * iCols * sizeof(char *));
455 QueryPerformanceCounter(&iStart1);
458 iRet = readStringMatrix(_iDatasetId, iRows, iCols, pstData);
465 QueryPerformanceCounter(&iEnd1);
468 QueryPerformanceCounter(&iStart2);
471 if (_piAddress == NULL)
473 sciErr = createNamedMatrixOfString(pvApiCtx, _pstVarname, iRows, iCols, pstData);
475 else //if not null this variable is in a list
477 sciErr = createMatrixOfStringInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pstData);
482 printError(&sciErr, 0);
487 QueryPerformanceCounter(&iEnd2);
488 QueryPerformanceCounter(&iStart3);
493 sprintf(pstMsg, "string_%d (%d x %d)", _iItemPos, iRows, iCols);
496 for (i = 0; i < iRows * iCols; i++)
508 QueryPerformanceCounter(&iEnd3);
510 //double dblTime =((iEnd1.QuadPart - iStart1.QuadPart) * 1000.0) / iFreq.QuadPart;
511 //printf("HDF5 : %0.3f ms\n", dblTime);
512 //dblTime =((iEnd2.QuadPart - iStart2.QuadPart) * 1000.0) / iFreq.QuadPart;
513 //printf("Stack : %0.3f ms\n", dblTime);
514 //dblTime =((iEnd3.QuadPart - iStart3.QuadPart) * 1000.0) / iFreq.QuadPart;
515 //printf("Clear : %0.3f ms\n", dblTime);
516 double dblTime = ((iEnd3.QuadPart - iStart1.QuadPart) * 1000.0) / iFreq.QuadPart;
518 printf("Total String: %0.3f ms\n\n", dblTime);
523 static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
531 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
537 iRet = getDatasetPrecision(_iDatasetId, &iPrec);
549 pcData = (char *)MALLOC(sizeof(char) * iRows * iCols);
550 iRet = readInteger8Matrix(_iDatasetId, iRows, iCols, pcData);
556 if (_piAddress == NULL)
558 sciErr = createNamedMatrixOfInteger8(pvApiCtx, _pstVarname, iRows, iCols, pcData);
562 sciErr = createMatrixOfInteger8InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pcData);
568 unsigned char *pucData = NULL;
570 pucData = (unsigned char *)MALLOC(sizeof(unsigned char) * iRows * iCols);
571 iRet = readUnsignedInteger8Matrix(_iDatasetId, iRows, iCols, pucData);
577 if (_piAddress == NULL)
579 sciErr = createNamedMatrixOfUnsignedInteger8(pvApiCtx, _pstVarname, iRows, iCols, pucData);
583 sciErr = createMatrixOfUnsignedInteger8InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pucData);
589 short *psData = NULL;
591 psData = (short *)MALLOC(sizeof(short) * iRows * iCols);
592 iRet = readInteger16Matrix(_iDatasetId, iRows, iCols, psData);
598 if (_piAddress == NULL)
600 sciErr = createNamedMatrixOfInteger16(pvApiCtx, _pstVarname, iRows, iCols, psData);
604 sciErr = createMatrixOfInteger16InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, psData);
610 unsigned short *pusData = NULL;
612 pusData = (unsigned short *)MALLOC(sizeof(unsigned short) * iRows * iCols);
613 iRet = readUnsignedInteger16Matrix(_iDatasetId, iRows, iCols, pusData);
619 if (_piAddress == NULL)
621 sciErr = createNamedMatrixOfUnsignedInteger16(pvApiCtx, _pstVarname, iRows, iCols, pusData);
625 sciErr = createMatrixOfUnsignedInteger16InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pusData);
633 piData = (int *)MALLOC(sizeof(int) * iRows * iCols);
634 iRet = readInteger32Matrix(_iDatasetId, iRows, iCols, piData);
640 if (_piAddress == NULL)
642 sciErr = createNamedMatrixOfInteger32(pvApiCtx, _pstVarname, iRows, iCols, piData);
646 sciErr = createMatrixOfInteger32InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, piData);
652 unsigned int *puiData = NULL;
654 puiData = (unsigned int *)MALLOC(sizeof(unsigned int) * iRows * iCols);
655 iRet = readUnsignedInteger32Matrix(_iDatasetId, iRows, iCols, puiData);
661 if (_piAddress == NULL)
663 sciErr = createNamedMatrixOfUnsignedInteger32(pvApiCtx, _pstVarname, iRows, iCols, puiData);
667 sciErr = createMatrixOfUnsignedInteger32InNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, puiData);
673 #ifdef __SCILAB_INT64__
674 long long *pllData = NULL;
676 pllData = (long long *)MALLOC(sizeof(long long) * iRows * iCols);
677 iRet = readInteger64Matrix(_iDatasetId, iRows, iCols, pllData);
683 if (_piAddress == NULL)
685 sciErr = createNamedMatrixOfInteger64(_pstVarname, iRows, iCols, pllData);
689 sciErr = createMatrixOfInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, iRows, iCols, pllData);
698 #ifdef __SCILAB_INT64__
699 unsigned long long *pullData = NULL;
701 pullData = (unsigned long long *)MALLOC(sizeof(unsigned long long) * iRows * iCols);
702 iRet = readUnsignedInteger64Matrix(_iDatasetId, iRows, iCols, pullData);
708 if (_piAddress == NULL)
710 sciErr = createNamedMatrixOfUnsignedInteger64(_pstVarname, iRows, iCols, pullData);
714 sciErr = createMatrixOfUnsignedInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, iRows, iCols, pullData);
728 sprintf(pstMsg, "integer_%d (%d x %d)", _iItemPos, iRows, iCols);
734 printError(&sciErr, 0);
741 static bool import_boolean(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
749 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
755 if (iRows * iCols != 0)
757 piData = (int *)MALLOC(iRows * iCols * sizeof(int));
758 iRet = readBooleanMatrix(_iDatasetId, iRows, iCols, piData);
765 if (_piAddress == NULL)
767 sciErr = createNamedMatrixOfBoolean(pvApiCtx, _pstVarname, iRows, iCols, piData);
769 else //if not null this variable is in a list
771 sciErr = createMatrixOfBooleanInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, piData);
776 printError(&sciErr, 0);
783 sprintf(pstMsg, "boolean_%d (%d x %d)", _iItemPos, iRows, iCols);
800 static bool import_poly(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
807 char pstVarName[64] = { 0 };
808 double **pdblReal = NULL;
809 double **pdblImg = NULL;
810 int *piNbCoef = NULL;
813 iRet = getDatasetDims(_iDatasetId, &iRows, &iCols);
819 iComplex = isComplexData(_iDatasetId);
823 piNbCoef = (int *)MALLOC(iRows * iCols * sizeof(int));
824 pdblReal = (double **)MALLOC(iRows * iCols * sizeof(double *));
825 pdblImg = (double **)MALLOC(iRows * iCols * sizeof(double *));
826 iRet = readPolyComplexMatrix(_iDatasetId, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
830 piNbCoef = (int *)MALLOC(iRows * iCols * sizeof(int));
831 pdblReal = (double **)MALLOC(iRows * iCols * sizeof(double *));
832 iRet = readPolyMatrix(_iDatasetId, pstVarName, iRows, iCols, piNbCoef, pdblReal);
840 if (_piAddress == NULL)
844 sciErr = createNamedComplexMatrixOfPoly(pvApiCtx, _pstVarname, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg);
848 sciErr = createNamedMatrixOfPoly(pvApiCtx, _pstVarname, pstVarName, iRows, iCols, piNbCoef, pdblReal);
851 else //if not null this variable is in a list
856 createComplexMatrixOfPolyInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, pstVarName, iRows, iCols, piNbCoef, pdblReal,
861 sciErr = createMatrixOfPolyInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, pstVarName, iRows, iCols, piNbCoef, pdblReal);
867 printError(&sciErr, 0);
874 sprintf(pstMsg, "poly_%d (%d x %d)", _iItemPos, iRows, iCols);
878 for (i = 0; i < iRows * iCols; i++)
893 static bool import_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
899 double *pdblReal = NULL;
900 double *pdblImg = NULL;
902 int *piNbItemRow = NULL;
903 int *piColPos = NULL;
906 iRet = getSparseDimension(_iDatasetId, &iRows, &iCols, &iNbItem);
912 iComplex = isComplexData(_iDatasetId);
916 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
917 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
918 pdblReal = (double *)MALLOC(iNbItem * sizeof(double));
919 pdblImg = (double *)MALLOC(iNbItem * sizeof(double));
920 iRet = readSparseComplexMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg);
924 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
925 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
926 pdblReal = (double *)MALLOC(iNbItem * sizeof(double));
927 iRet = readSparseMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
935 if (_piAddress == NULL)
939 sciErr = createNamedComplexSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg);
943 sciErr = createNamedSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
946 else //if not null this variable is in a list
951 createComplexSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos,
957 createSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal);
963 printError(&sciErr, 0);
970 sprintf(pstMsg, "sparse_%d (%d x %d)", _iItemPos, iRows, iCols);
990 static bool import_boolean_sparse(int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
996 int *piNbItemRow = NULL;
997 int *piColPos = NULL;
1000 iRet = getSparseDimension(_iDatasetId, &iRows, &iCols, &iNbItem);
1006 piNbItemRow = (int *)MALLOC(iRows * sizeof(int));
1007 piColPos = (int *)MALLOC(iNbItem * sizeof(int));
1008 iRet = readBooleanSparseMatrix(_iDatasetId, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1014 if (_piAddress == NULL)
1016 sciErr = createNamedBooleanSparseMatrix(pvApiCtx, _pstVarname, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1018 else //if not null this variable is in a list
1020 sciErr = createBooleanSparseMatrixInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, iNbItem, piNbItemRow, piColPos);
1025 printError(&sciErr, 0);
1032 sprintf(pstMsg, "boolean sparse_%d (%d x %d)", _iItemPos, iRows, iCols);
1047 static bool import_list(int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname)
1052 int *piListAddr = NULL;
1053 hobj_ref_t *piItemRef = NULL;
1056 iRet = getListDims(_iDatasetId, &iItems);
1064 //special case for empty list
1068 iRet = getListItemReferences(_iDatasetId, &piItemRef);
1078 sprintf(pstMsg, "list_%d (%d)", _iItemPos, iItems);
1082 if (_piAddress == 0)
1087 sciErr = createNamedList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1090 sciErr = createNamedTList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1093 sciErr = createNamedMList(pvApiCtx, _pstVarname, iItems, &piListAddr);
1099 else //if not null this variable is in a list
1104 sciErr = createListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1107 sciErr = createTListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1110 sciErr = createMListInNamedList(pvApiCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr);
1119 printError(&sciErr, 0);
1124 for (i = 0; i < iItems; i++)
1126 int iItemDataset = 0;
1128 iRet = getListItemDataset(_iDatasetId, piItemRef, i, &iItemDataset);
1129 if (iRet || iItemDataset == 0)
1134 bool bRet = import_data(iItemDataset, i + 1, piListAddr, _pstVarname);
1143 iRet = deleteListItemReferences(_iDatasetId, piItemRef);
1150 printf("Close List %d\n\n", iCloseList++);
1156 sprintf(pstMsg1, "ListEnd_%d", _iItemPos);
1157 print_tree(pstMsg1);
1162 void print_tree(char *_pstMsg)
1165 for (int i = 0; i < iTab; i++)
1169 printf("%s\n", _pstMsg);
1173 /*--------------------------------------------------------------------------*/