Salome HOME
First stable version after merging with V3_2_2
[modules/kernel.git] / src / HDFPersist / test4.cxx
1 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : test4.cxx
25 //  Module : SALOME
26
27 #include <iostream.h>
28 #include "HDFOI.hxx"
29 #include <stdlib.h>
30 using namespace std;
31
32
33 int main()
34 {
35   HDFfile *hdf_file;
36   HDFgroup *hdf_group;
37   HDFdataset *hdf_dataset1, *hdf_dataset2;
38   int n,i;
39   char name[HDF_NAME_MAX_LEN+1];
40   hdf_object_type type;
41   hdf_type data_type;
42   int ndim, size;
43   hdf_size *dim;
44   hdf_float64 *val1;
45   hdf_int32 *val2;
46
47   try  
48     {
49       // a new HDFfile object
50       hdf_file = new HDFfile("file_test3.hdf");
51       MESSAGE( ">> A HDFfile object is created" );
52       
53       hdf_file->OpenOnDisk(HDF_RDONLY);
54       MESSAGE(">> The HDF file is opened on Disk with HDF_RDONLY access mode") 
55       // It is possible to read the name of all (objects) sons in the file
56       n = hdf_file->nInternalObjects(); 
57       for (i=0;i<n;i++) 
58         {
59           hdf_file->InternalObjectIndentify(i,name);
60           MESSAGE( "--> First Level Internal Object Name : " << name );
61         }   
62
63       // Or it is possible to ask if an object is in the file
64       if (hdf_file->ExistInternalObject("MESH"))
65           MESSAGE( "--> The object naming 'MESH' is in the file " );
66
67       // it is possible to determine the type of an object
68       type = hdf_file->InternalObjectType("MESH");
69       switch (type)
70         {
71         case HDF_GROUP :
72           MESSAGE( "--> Its type is HDF_GROUP" );
73           break;
74           
75         case HDF_DATASET :
76           MESSAGE( "--> Its type is HDF_DATASET" );
77           break;
78           
79         default :
80           MESSAGE( "--> !!!! PANIC !!!" );
81         }
82
83       hdf_group = new HDFgroup(name,hdf_file); 
84       MESSAGE( ">> A new HDF group object " << name << " is created in memory")
85
86       hdf_group->OpenOnDisk();
87       MESSAGE( ">> The group " << name << " is opened on disk" );
88  
89       // object inside the group ?
90       n = hdf_group->nInternalObjects();  
91       MESSAGE( ">> There are " << n << " objects in this group " );
92       for (i=0;i<n;i++)     
93         {
94           hdf_group->InternalObjectIndentify(i,name);
95           type = hdf_group->InternalObjectType(name);
96           MESSAGE( "--> First Level Internal Object Name : " << name );
97           switch (type)
98             {
99             case HDF_GROUP :
100               MESSAGE( "--> Its type is HDF_GROUP" );
101               break;
102               
103             case HDF_DATASET :
104               MESSAGE( "--> Its type is HDF_DATASET" );
105               break;
106               
107             default :
108               MESSAGE( "--> !!!! PANIC !!!" );
109             }
110         } 
111
112       // The first dataset object 'COORDINATES'
113       hdf_dataset1 = new HDFdataset("COORDINATES",hdf_group);
114       MESSAGE( ">> The dataset object 'COORDINATES' is created in memory " );
115
116       hdf_dataset1->OpenOnDisk();
117       MESSAGE( ">> The dataset object 'COORDINATES' is opened on disk " );
118
119       data_type = hdf_dataset1->GetType();
120       switch(data_type)
121         {
122         case HDF_INT32 :
123           MESSAGE( "--> Type of data : HDF_INT32 " );
124           break; 
125         case HDF_INT64 :
126           MESSAGE( "--> Type of data : HDF_INT64 " );
127           break; 
128         case HDF_FLOAT64 :
129           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
130           break;
131         default :
132           MESSAGE( "--> Type of data : HDF_STRING " );
133         }
134  
135       ndim = hdf_dataset1->nDim();
136       MESSAGE( "--> Number of dimensions : " << ndim );
137
138       dim = new hdf_size[ndim];
139       hdf_dataset1->GetDim(dim);
140       for (i=0;i<ndim;i++)
141         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
142       delete dim;
143
144       size = hdf_dataset1->GetSize();
145       val1 = new hdf_float64[size];
146       hdf_dataset1->ReadFromDisk(val1);
147       MESSAGE( "--> The values are : ");
148       for (i=0;i<size;i++)
149         MESSAGE( " " << val1[i]);
150       MESSAGE();
151       delete val1;
152
153       hdf_dataset1->CloseOnDisk();
154       MESSAGE( ">> The dataset object 'COORDINATES' is closed on disk " );
155
156       // The second dataset 'UNIT'
157       hdf_dataset2 = new HDFdataset("UNIT",hdf_group);
158       MESSAGE( ">> The dataset object 'UNIT' is created in memory " );
159
160       hdf_dataset2->OpenOnDisk();
161       MESSAGE( ">> The dataset object 'UNIT' is opened on disk " );
162
163       data_type = hdf_dataset2->GetType();
164       switch(data_type)
165         {
166         case HDF_INT32 :
167           MESSAGE( "--> Type of data : HDF_INT32 " );
168           break;
169         case HDF_INT64 :
170           MESSAGE( "--> Type of data : HDF_INT64 " );
171           break;
172         case HDF_FLOAT64 :
173           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
174           break;
175         default :
176           MESSAGE( "--> Type of data : HDF_STRING " );
177         }
178
179       ndim = hdf_dataset2->nDim();
180       MESSAGE( "--> Number of dimensions : " << ndim );
181
182       dim = new hdf_size[ndim];
183       hdf_dataset2->GetDim(dim);
184       for (i=0;i<ndim;i++)
185         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
186       delete dim;
187
188       size = hdf_dataset2->GetSize();
189       val2 = new hdf_int32[size];
190       hdf_dataset2->ReadFromDisk(val2);
191       MESSAGE( "--> The values are : ");
192       for (i=0;i<size;i++)
193         MESSAGE( " " << val2[i]);
194       MESSAGE();
195       delete val2;
196
197       hdf_dataset2->CloseOnDisk();
198       MESSAGE( ">> The dataset object 'UNIT' is closed on disk " );
199       
200       hdf_group->CloseOnDisk();
201       MESSAGE( ">> The group is closed on file" );
202       
203       hdf_file->CloseOnDisk();
204       MESSAGE(">> The HDF file is closed on Disk" )      
205       
206       // Memory clean
207       delete hdf_dataset1;
208       delete hdf_dataset2;
209       delete hdf_group;
210       delete hdf_file;
211       MESSAGE( ">> MEMORY CLEAN : all HDF objects have been deleted" );
212     }
213   catch (HDFexception)
214     {
215       MESSAGE( "!!! HDFexception !!! " )
216     }  
217   
218   return 0;
219 }