Remove autogenerated comments
[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                         e.printStackTrace();
36                         return 0;
37                 } catch (NullPointerException e) {
38                         e.printStackTrace();
39                         return 0;
40                 }
41         }
42
43         public static void closeFile(int fileId) {
44                 try {
45                         H5.H5Fclose(fileId);
46                 } catch (HDF5LibraryException e) {
47                         e.printStackTrace();
48                 }
49         }
50
51         public static void createAttribute(int datasetId, String attributeName, String attributeValue) throws NullPointerException, HDF5Exception {
52                 int tid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
53                 H5.H5Tset_size(tid, attributeValue.length());
54                 long[] attributeDims = {1};
55                 int attributespaceId = H5.H5Screate_simple(1, attributeDims, null);
56                 int attributeId = H5.H5Acreate(datasetId, attributeName, tid, attributespaceId, HDF5Constants.H5P_DEFAULT);
57                 H5.H5Awrite(attributeId, tid, attributeValue.getBytes());
58                 H5.H5Aclose(attributeId);
59                 H5.H5Sclose(attributespaceId);
60                 H5.H5Tclose(tid);
61         }
62
63         public static void createIntAttribute(int datasetId, String attributeName, int attributeValue) throws NullPointerException, HDF5Exception {
64                 long[] attributeDims = {1};
65                 int attributespaceId = H5.H5Screate_simple(1, attributeDims, null);
66                 int attributeId = H5.H5Acreate(datasetId, attributeName, HDF5Constants.H5T_NATIVE_INT, 
67                         attributespaceId, HDF5Constants.H5P_DEFAULT);
68                 H5.H5Awrite(attributeId, HDF5Constants.H5T_NATIVE_INT, new Integer[]{attributeValue});
69                 H5.H5Aclose(attributeId);
70                 H5.H5Sclose(attributespaceId);
71         }
72
73         //    public static void writeInDataSet(int fileId, String dataSetName, int[] data) throws NullPointerException, HDF5Exception {
74         //      int size = data.length;
75         //
76         //      long[] dims = {size};
77         //      int dataspaceId = H5.H5Screate_simple(1, dims, null);
78         //      int datasetId = H5.H5Dcreate(fileId, "/" + dataSetName,
79         //              HDF5Constants.H5T_NATIVE_INT, dataspaceId,
80         //              HDF5Constants.H5P_DEFAULT);
81         //      H5.H5Dwrite(datasetId, HDF5Constants.H5T_NATIVE_INT,
82         //              HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
83         //              HDF5Constants.H5P_DEFAULT, data);
84         //      createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS, H5ScilabConstant.SCILAB_CLASS_INT32);
85         //      H5.H5Dclose(datasetId);
86         //      H5.H5Sclose(dataspaceId);
87         //    }
88         //
89         //    public static void writeInDataSet(int fileId, String dataSetName, int[][] data) throws NullPointerException, HDF5Exception {
90         //      int rows = data[0].length;
91         //      int cols = data.length;
92         //
93         //      long[] dims = {rows, cols};
94         //      int dataspaceId = H5.H5Screate_simple(2, dims, null);
95         //      int datasetId = H5.H5Dcreate(fileId, "/" + dataSetName,
96         //              HDF5Constants.H5T_NATIVE_INT, dataspaceId,
97         //              HDF5Constants.H5P_DEFAULT);
98         //      H5.H5Dwrite(datasetId, HDF5Constants.H5T_NATIVE_INT,
99         //              HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
100         //              HDF5Constants.H5P_DEFAULT, data);
101         //      createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS, H5ScilabConstant.SCILAB_CLASS_INT32);
102         //      H5.H5Dclose(datasetId);
103         //      H5.H5Sclose(dataspaceId);
104         //    }
105
106         public static void writeInDataSet(int file_id, String dataSetName, ScilabType data) throws HDF5Exception {
107                 if(data instanceof ScilabString) {
108                         H5WriteScilabString.writeInDataSet(file_id, dataSetName, (ScilabString) data); 
109                 }else if(data instanceof ScilabList) {
110                         H5WriteScilabList.writeInDataSet(file_id, dataSetName, (ScilabList) data); 
111                 }else if(data instanceof ScilabTList) {
112                         H5WriteScilabTList.writeInDataSet(file_id, dataSetName, (ScilabTList) data); 
113                 }else if(data instanceof ScilabMList) { 
114                         H5WriteScilabMList.writeInDataSet(file_id, dataSetName, (ScilabMList) data); 
115                 }else if(data instanceof ScilabDouble) {
116                         H5WriteScilabDouble.writeInDataSet(file_id, dataSetName, (ScilabDouble) data); 
117                 }else if(data instanceof ScilabBoolean) {
118                         H5WriteScilabBoolean.writeInDataSet(file_id, dataSetName, (ScilabBoolean) data); 
119                 }else if(data instanceof ScilabInteger) { 
120                         H5WriteScilabInteger.writeInDataSet(file_id, dataSetName, (ScilabInteger) data); 
121                 }
122         }
123
124
125 }