a2ff3320b9fca3539306532874a9ae79d1cbafe0
[scilab.git] / scilab / modules / hdf5 / src / java / org / scilab / modules / hdf5 / write / H5Write.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4  * 
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
10  *
11  */
12 package org.scilab.modules.hdf5.write;
13
14 import ncsa.hdf.hdf5lib.H5;
15 import ncsa.hdf.hdf5lib.HDF5Constants;
16 import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
17 import ncsa.hdf.hdf5lib.exceptions.HDF5LibraryException;
18
19 import org.scilab.modules.types.ScilabBoolean;
20 import org.scilab.modules.types.ScilabDouble;
21 import org.scilab.modules.types.ScilabInteger;
22 import org.scilab.modules.types.ScilabList;
23 import org.scilab.modules.types.ScilabMList;
24 import org.scilab.modules.types.ScilabString;
25 import org.scilab.modules.types.ScilabTList;
26 import org.scilab.modules.types.ScilabType;
27
28
29 public class H5Write {  
30
31         public static int createFile(String fileName) {
32                 try {
33                         return H5.H5Fcreate(fileName,  HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
34                 } catch (HDF5LibraryException e) {
35                         // TODO Auto-generated catch block
36                         e.printStackTrace();
37                         return 0;
38                 } catch (NullPointerException e) {
39                         // TODO Auto-generated catch block
40                         e.printStackTrace();
41                         return 0;
42                 }
43         }
44
45         public static void closeFile(int fileId) {
46                 try {
47                         H5.H5Fclose(fileId);
48                 } catch (HDF5LibraryException e) {
49                         // TODO Auto-generated catch block
50                         e.printStackTrace();
51                 }
52         }
53
54         public static void createAttribute(int datasetId, String attributeName, String attributeValue) throws NullPointerException, HDF5Exception {
55                 int tid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
56                 H5.H5Tset_size(tid, attributeValue.length());
57                 long[] attributeDims = {1};
58                 int attributespaceId = H5.H5Screate_simple(1, attributeDims, null);
59                 int attributeId = H5.H5Acreate(datasetId, attributeName, tid, attributespaceId, HDF5Constants.H5P_DEFAULT);
60                 H5.H5Awrite(attributeId, tid, attributeValue.getBytes());
61                 H5.H5Aclose(attributeId);
62                 H5.H5Sclose(attributespaceId);
63                 H5.H5Tclose(tid);
64         }
65
66         public static void createIntAttribute(int datasetId, String attributeName, int attributeValue) throws NullPointerException, HDF5Exception {
67                 long[] attributeDims = {1};
68                 int attributespaceId = H5.H5Screate_simple(1, attributeDims, null);
69                 int attributeId = H5.H5Acreate(datasetId, attributeName, HDF5Constants.H5T_NATIVE_INT, 
70                         attributespaceId, HDF5Constants.H5P_DEFAULT);
71                 H5.H5Awrite(attributeId, HDF5Constants.H5T_NATIVE_INT, new Integer[]{attributeValue});
72                 H5.H5Aclose(attributeId);
73                 H5.H5Sclose(attributespaceId);
74         }
75
76         //    public static void writeInDataSet(int fileId, String dataSetName, int[] data) throws NullPointerException, HDF5Exception {
77         //      int size = data.length;
78         //
79         //      long[] dims = {size};
80         //      int dataspaceId = H5.H5Screate_simple(1, dims, null);
81         //      int datasetId = H5.H5Dcreate(fileId, "/" + dataSetName,
82         //              HDF5Constants.H5T_NATIVE_INT, dataspaceId,
83         //              HDF5Constants.H5P_DEFAULT);
84         //      H5.H5Dwrite(datasetId, HDF5Constants.H5T_NATIVE_INT,
85         //              HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
86         //              HDF5Constants.H5P_DEFAULT, data);
87         //      createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS, H5ScilabConstant.SCILAB_CLASS_INT32);
88         //      H5.H5Dclose(datasetId);
89         //      H5.H5Sclose(dataspaceId);
90         //    }
91         //
92         //    public static void writeInDataSet(int fileId, String dataSetName, int[][] data) throws NullPointerException, HDF5Exception {
93         //      int rows = data[0].length;
94         //      int cols = data.length;
95         //
96         //      long[] dims = {rows, cols};
97         //      int dataspaceId = H5.H5Screate_simple(2, dims, null);
98         //      int datasetId = H5.H5Dcreate(fileId, "/" + dataSetName,
99         //              HDF5Constants.H5T_NATIVE_INT, dataspaceId,
100         //              HDF5Constants.H5P_DEFAULT);
101         //      H5.H5Dwrite(datasetId, HDF5Constants.H5T_NATIVE_INT,
102         //              HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
103         //              HDF5Constants.H5P_DEFAULT, data);
104         //      createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS, H5ScilabConstant.SCILAB_CLASS_INT32);
105         //      H5.H5Dclose(datasetId);
106         //      H5.H5Sclose(dataspaceId);
107         //    }
108
109         public static void writeInDataSet(int file_id, String dataSetName, ScilabType data) throws HDF5Exception {
110                 if(data instanceof ScilabString) {
111                         H5WriteScilabString.writeInDataSet(file_id, dataSetName, (ScilabString) data); 
112                 }else if(data instanceof ScilabList) {
113                         H5WriteScilabList.writeInDataSet(file_id, dataSetName, (ScilabList) data); 
114                 }else if(data instanceof ScilabTList) {
115                         H5WriteScilabTList.writeInDataSet(file_id, dataSetName, (ScilabTList) data); 
116                 }else if(data instanceof ScilabMList) { 
117                         H5WriteScilabMList.writeInDataSet(file_id, dataSetName, (ScilabMList) data); 
118                 }else if(data instanceof ScilabDouble) {
119                         H5WriteScilabDouble.writeInDataSet(file_id, dataSetName, (ScilabDouble) data); 
120                 }else if(data instanceof ScilabBoolean) {
121                         H5WriteScilabBoolean.writeInDataSet(file_id, dataSetName, (ScilabBoolean) data); 
122                 }else if(data instanceof ScilabInteger) { 
123                         H5WriteScilabInteger.writeInDataSet(file_id, dataSetName, (ScilabInteger) data); 
124                 }
125         }
126
127
128 }