Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / HDFPersist / test4.cxx
1 using namespace std;
2 #include <iostream.h>
3 #include "HDFOI.hxx"
4 #include <stdlib.h>
5
6
7 int main()
8 {
9   HDFfile *hdf_file;
10   HDFgroup *hdf_group;
11   HDFdataset *hdf_dataset1, *hdf_dataset2;
12   int n,i;
13   char name[HDF_NAME_MAX_LEN+1];
14   hdf_object_type type;
15   hdf_type data_type;
16   int ndim, size;
17   hdf_size *dim;
18   hdf_float64 *val1;
19   hdf_int32 *val2;
20
21   try  
22     {
23       // a new HDFfile object
24       hdf_file = new HDFfile("file_test3.hdf");
25       MESSAGE( ">> A HDFfile object is created" );
26       
27       hdf_file->OpenOnDisk(HDF_RDONLY);
28       MESSAGE(">> The HDF file is opened on Disk with HDF_RDONLY access mode") 
29       // It is possible to read the name of all (objects) sons in the file
30       n = hdf_file->nInternalObjects(); 
31       for (i=0;i<n;i++) 
32         {
33           hdf_file->InternalObjectIndentify(i,name);
34           MESSAGE( "--> First Level Internal Object Name : " << name );
35         }   
36
37       // Or it is possible to ask if an object is in the file
38       if (hdf_file->ExistInternalObject("MESH"))
39           MESSAGE( "--> The object naming 'MESH' is in the file " );
40
41       // it is possible to determine the type of an object
42       type = hdf_file->InternalObjectType("MESH");
43       switch (type)
44         {
45         case HDF_GROUP :
46           MESSAGE( "--> Its type is HDF_GROUP" );
47           break;
48           
49         case HDF_DATASET :
50           MESSAGE( "--> Its type is HDF_DATASET" );
51           break;
52           
53         default :
54           MESSAGE( "--> !!!! PANIC !!!" );
55         }
56
57       hdf_group = new HDFgroup(name,hdf_file); 
58       MESSAGE( ">> A new HDF group object " << name << " is created in memory")
59
60       hdf_group->OpenOnDisk();
61       MESSAGE( ">> The group " << name << " is opened on disk" );
62  
63       // object inside the group ?
64       n = hdf_group->nInternalObjects();  
65       MESSAGE( ">> There are " << n << " objects in this group " );
66       for (i=0;i<n;i++)     
67         {
68           hdf_group->InternalObjectIndentify(i,name);
69           type = hdf_group->InternalObjectType(name);
70           MESSAGE( "--> First Level Internal Object Name : " << name );
71           switch (type)
72             {
73             case HDF_GROUP :
74               MESSAGE( "--> Its type is HDF_GROUP" );
75               break;
76               
77             case HDF_DATASET :
78               MESSAGE( "--> Its type is HDF_DATASET" );
79               break;
80               
81             default :
82               MESSAGE( "--> !!!! PANIC !!!" );
83             }
84         } 
85
86       // The first dataset object 'COORDINATES'
87       hdf_dataset1 = new HDFdataset("COORDINATES",hdf_group);
88       MESSAGE( ">> The dataset object 'COORDINATES' is created in memory " );
89
90       hdf_dataset1->OpenOnDisk();
91       MESSAGE( ">> The dataset object 'COORDINATES' is opened on disk " );
92
93       data_type = hdf_dataset1->GetType();
94       switch(data_type)
95         {
96         case HDF_INT32 :
97           MESSAGE( "--> Type of data : HDF_INT32 " );
98           break; 
99         case HDF_INT64 :
100           MESSAGE( "--> Type of data : HDF_INT64 " );
101           break; 
102         case HDF_FLOAT64 :
103           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
104           break;
105         default :
106           MESSAGE( "--> Type of data : HDF_STRING " );
107         }
108  
109       ndim = hdf_dataset1->nDim();
110       MESSAGE( "--> Number of dimensions : " << ndim );
111
112       dim = new hdf_size[ndim];
113       hdf_dataset1->GetDim(dim);
114       for (i=0;i<ndim;i++)
115         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
116       delete dim;
117
118       size = hdf_dataset1->GetSize();
119       val1 = new hdf_float64[size];
120       hdf_dataset1->ReadFromDisk(val1);
121       MESSAGE( "--> The values are : ");
122       for (i=0;i<size;i++)
123         MESSAGE( " " << val1[i]);
124       MESSAGE();
125       delete val1;
126
127       hdf_dataset1->CloseOnDisk();
128       MESSAGE( ">> The dataset object 'COORDINATES' is closed on disk " );
129
130       // The second dataset 'UNIT'
131       hdf_dataset2 = new HDFdataset("UNIT",hdf_group);
132       MESSAGE( ">> The dataset object 'UNIT' is created in memory " );
133
134       hdf_dataset2->OpenOnDisk();
135       MESSAGE( ">> The dataset object 'UNIT' is opened on disk " );
136
137       data_type = hdf_dataset2->GetType();
138       switch(data_type)
139         {
140         case HDF_INT32 :
141           MESSAGE( "--> Type of data : HDF_INT32 " );
142           break;
143         case HDF_INT64 :
144           MESSAGE( "--> Type of data : HDF_INT64 " );
145           break;
146         case HDF_FLOAT64 :
147           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
148           break;
149         default :
150           MESSAGE( "--> Type of data : HDF_STRING " );
151         }
152
153       ndim = hdf_dataset2->nDim();
154       MESSAGE( "--> Number of dimensions : " << ndim );
155
156       dim = new hdf_size[ndim];
157       hdf_dataset2->GetDim(dim);
158       for (i=0;i<ndim;i++)
159         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
160       delete dim;
161
162       size = hdf_dataset2->GetSize();
163       val2 = new hdf_int32[size];
164       hdf_dataset2->ReadFromDisk(val2);
165       MESSAGE( "--> The values are : ");
166       for (i=0;i<size;i++)
167         MESSAGE( " " << val2[i]);
168       MESSAGE();
169       delete val2;
170
171       hdf_dataset2->CloseOnDisk();
172       MESSAGE( ">> The dataset object 'UNIT' is closed on disk " );
173       
174       hdf_group->CloseOnDisk();
175       MESSAGE( ">> The group is closed on file" );
176       
177       hdf_file->CloseOnDisk();
178       MESSAGE(">> The HDF file is closed on Disk" )      
179       
180       // Memory clean
181       delete hdf_dataset1;
182       delete hdf_dataset2;
183       delete hdf_group;
184       delete hdf_file;
185       MESSAGE( ">> MEMORY CLEAN : all HDF objects have been deleted" );
186     }
187   catch (HDFexception)
188     {
189       MESSAGE( "!!! HDFexception !!! " )
190     }  
191   
192   return 0;
193 }