Salome HOME
Revert "Synchronize adm files"
[modules/kernel.git] / src / HDFPersist / test4.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
24 //  File   : test4.cxx
25 //  Module : SALOME
26 //
27 #include <iostream.h>
28 #include "HDFOI.hxx"
29 #include <stdlib.h>
30
31
32 int main()
33 {
34   HDFfile *hdf_file;
35   HDFgroup *hdf_group;
36   HDFdataset *hdf_dataset1, *hdf_dataset2;
37   int n,i;
38   char name[HDF_NAME_MAX_LEN+1];
39   hdf_object_type type;
40   hdf_type data_type;
41   int ndim, size;
42   hdf_size *dim;
43   hdf_float64 *val1;
44   hdf_int32 *val2;
45
46   try  
47     {
48       // a new HDFfile object
49       hdf_file = new HDFfile("file_test3.hdf");
50       MESSAGE( ">> A HDFfile object is created" );
51       
52       hdf_file->OpenOnDisk(HDF_RDONLY);
53       MESSAGE(">> The HDF file is opened on Disk with HDF_RDONLY access mode") 
54       // It is possible to read the name of all (objects) sons in the file
55       n = hdf_file->nInternalObjects(); 
56       for (i=0;i<n;i++) 
57         {
58           hdf_file->InternalObjectIndentify(i,name);
59           MESSAGE( "--> First Level Internal Object Name : " << name );
60         }   
61
62       // Or it is possible to ask if an object is in the file
63       if (hdf_file->ExistInternalObject("MESH"))
64           MESSAGE( "--> The object naming 'MESH' is in the file " );
65
66       // it is possible to determine the type of an object
67       type = hdf_file->InternalObjectType("MESH");
68       switch (type)
69         {
70         case HDF_GROUP :
71           MESSAGE( "--> Its type is HDF_GROUP" );
72           break;
73           
74         case HDF_DATASET :
75           MESSAGE( "--> Its type is HDF_DATASET" );
76           break;
77           
78         default :
79           MESSAGE( "--> !!!! PANIC !!!" );
80         }
81
82       hdf_group = new HDFgroup(name,hdf_file); 
83       MESSAGE( ">> A new HDF group object " << name << " is created in memory")
84
85       hdf_group->OpenOnDisk();
86       MESSAGE( ">> The group " << name << " is opened on disk" );
87  
88       // object inside the group ?
89       n = hdf_group->nInternalObjects();  
90       MESSAGE( ">> There are " << n << " objects in this group " );
91       for (i=0;i<n;i++)     
92         {
93           hdf_group->InternalObjectIndentify(i,name);
94           type = hdf_group->InternalObjectType(name);
95           MESSAGE( "--> First Level Internal Object Name : " << name );
96           switch (type)
97             {
98             case HDF_GROUP :
99               MESSAGE( "--> Its type is HDF_GROUP" );
100               break;
101               
102             case HDF_DATASET :
103               MESSAGE( "--> Its type is HDF_DATASET" );
104               break;
105               
106             default :
107               MESSAGE( "--> !!!! PANIC !!!" );
108             }
109         } 
110
111       // The first dataset object 'COORDINATES'
112       hdf_dataset1 = new HDFdataset("COORDINATES",hdf_group);
113       MESSAGE( ">> The dataset object 'COORDINATES' is created in memory " );
114
115       hdf_dataset1->OpenOnDisk();
116       MESSAGE( ">> The dataset object 'COORDINATES' is opened on disk " );
117
118       data_type = hdf_dataset1->GetType();
119       switch(data_type)
120         {
121         case HDF_INT32 :
122           MESSAGE( "--> Type of data : HDF_INT32 " );
123           break; 
124         case HDF_INT64 :
125           MESSAGE( "--> Type of data : HDF_INT64 " );
126           break; 
127         case HDF_FLOAT64 :
128           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
129           break;
130         default :
131           MESSAGE( "--> Type of data : HDF_STRING " );
132         }
133  
134       ndim = hdf_dataset1->nDim();
135       MESSAGE( "--> Number of dimensions : " << ndim );
136
137       dim = new hdf_size[ndim];
138       hdf_dataset1->GetDim(dim);
139       for (i=0;i<ndim;i++)
140         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
141       delete dim;
142
143       size = hdf_dataset1->GetSize();
144       val1 = new hdf_float64[size];
145       hdf_dataset1->ReadFromDisk(val1);
146       MESSAGE( "--> The values are : ");
147       for (i=0;i<size;i++)
148         MESSAGE( " " << val1[i]);
149       MESSAGE();
150       delete val1;
151
152       hdf_dataset1->CloseOnDisk();
153       MESSAGE( ">> The dataset object 'COORDINATES' is closed on disk " );
154
155       // The second dataset 'UNIT'
156       hdf_dataset2 = new HDFdataset("UNIT",hdf_group);
157       MESSAGE( ">> The dataset object 'UNIT' is created in memory " );
158
159       hdf_dataset2->OpenOnDisk();
160       MESSAGE( ">> The dataset object 'UNIT' is opened on disk " );
161
162       data_type = hdf_dataset2->GetType();
163       switch(data_type)
164         {
165         case HDF_INT32 :
166           MESSAGE( "--> Type of data : HDF_INT32 " );
167           break;
168         case HDF_INT64 :
169           MESSAGE( "--> Type of data : HDF_INT64 " );
170           break;
171         case HDF_FLOAT64 :
172           MESSAGE( "--> Type of data : HDF_FLOAT64 " );
173           break;
174         default :
175           MESSAGE( "--> Type of data : HDF_STRING " );
176         }
177
178       ndim = hdf_dataset2->nDim();
179       MESSAGE( "--> Number of dimensions : " << ndim );
180
181       dim = new hdf_size[ndim];
182       hdf_dataset2->GetDim(dim);
183       for (i=0;i<ndim;i++)
184         MESSAGE( "--> Dimension " << i+1 << " of size : " << dim[i] );
185       delete dim;
186
187       size = hdf_dataset2->GetSize();
188       val2 = new hdf_int32[size];
189       hdf_dataset2->ReadFromDisk(val2);
190       MESSAGE( "--> The values are : ");
191       for (i=0;i<size;i++)
192         MESSAGE( " " << val2[i]);
193       MESSAGE();
194       delete val2;
195
196       hdf_dataset2->CloseOnDisk();
197       MESSAGE( ">> The dataset object 'UNIT' is closed on disk " );
198       
199       hdf_group->CloseOnDisk();
200       MESSAGE( ">> The group is closed on file" );
201       
202       hdf_file->CloseOnDisk();
203       MESSAGE(">> The HDF file is closed on Disk" )      
204       
205       // Memory clean
206       delete hdf_dataset1;
207       delete hdf_dataset2;
208       delete hdf_group;
209       delete hdf_file;
210       MESSAGE( ">> MEMORY CLEAN : all HDF objects have been deleted" );
211     }
212   catch (HDFexception)
213     {
214       MESSAGE( "!!! HDFexception !!! " )
215     }  
216   
217   return 0;
218 }