Back to index

salome-kernel  6.5.0
HDFdataset.cc
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
00024 //  File   : HDFdataset.cc
00025 //  Module : SALOME
00026 //
00027 #include "hdfi.h"
00028 #include <string.h>
00029 #include "HDFdataset.hxx"
00030 #include "HDFcontainerObject.hxx"
00031 #include "HDFexception.hxx"
00032 
00033 #include <iostream>
00034 
00035 herr_t dataset_attr(hid_t loc_id, const char *attr_name, void *operator_data)
00036 {
00037   *(char**)operator_data = new char[strlen(attr_name)+1];
00038   strcpy(*(char**)operator_data, attr_name);
00039   return 1;
00040 }
00041 
00042 HDFdataset::HDFdataset(const char *name, HDFcontainerObject *father,hdf_type type, 
00043                      hdf_size dim[], int dimsize, hdf_byte_order order)
00044   : HDFinternalObject(name)
00045 {
00046   int i;
00047 
00048   _father = father;
00049   _fid = _father->GetId();
00050   _father->AddSon(this);
00051   _type = type;
00052   _ndim = dimsize;
00053   _dim = new hdf_size[dimsize];
00054   _byte_order = order;
00055   _size = 1;
00056   _attribute = NULL;
00057   for (i=0;i<dimsize;i++)
00058     {
00059       _dim[i] = dim[i];
00060       _size = _size * _dim[i];
00061     }
00062   _arrayId = -1;
00063 }
00064 
00065 
00066 HDFdataset::HDFdataset(const char *name,HDFcontainerObject *father)
00067   : HDFinternalObject(name)
00068 {
00069   _father = father;
00070   _fid = _father->GetId();
00071   _father->AddSon(this);
00072   _type = HDF_NONE;
00073   _ndim = -1;
00074   _dim = 0;
00075   _byte_order = H5T_ORDER_ERROR;
00076   _size = -1;
00077   _attribute = NULL;
00078   _arrayId = -1;
00079 }
00080 
00081 HDFdataset::~HDFdataset()
00082 {
00083   delete [] _dim;
00084 }
00085 
00086 void HDFdataset::CreateOnDisk()
00087 {
00088   if ((_id = HDFdatasetCreate(_fid,_name,_type,_dim,_ndim,_byte_order,_arrayId)) < 0)
00089     throw HDFexception("Can't create dataset");
00090 }
00091 
00092 void HDFdataset::OpenOnDisk()
00093 {
00094   if ((_id = HDFdatasetOpen(_fid,_name)) < 0)
00095     throw HDFexception("Can't open dataset");
00096 }
00097 
00098 void HDFdataset::CloseOnDisk()
00099 {
00100   hdf_err ret;
00101 
00102   if ((ret = HDFdatasetClose(_id)) < 0)
00103     throw HDFexception("Can't close dataset");
00104   _id = -1;
00105 }
00106 
00107 void HDFdataset::WriteOnDisk(void *values)
00108 {
00109   hdf_err ret;
00110 
00111   if ((ret = HDFdatasetWrite(_id,values)) < 0)
00112     throw HDFexception("Can't write dataset");
00113  
00114 }
00115 
00116 void HDFdataset::ReadFromDisk(void *values)
00117 {
00118   hdf_err ret;
00119 
00120   if ((ret = HDFdatasetRead(_id,values)) < 0)
00121       throw HDFexception("Can't read dataset");
00122 }
00123 
00124 HDFcontainerObject *HDFdataset::GetFather()
00125 {
00126   return _father;
00127 }
00128 
00129 hdf_type HDFdataset::GetType()
00130 {
00131   if (_type == HDF_NONE)
00132     if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
00133       throw HDFexception("Can't determine the type of data in the dataset");
00134   
00135   return _type;
00136 }
00137 
00138 int HDFdataset::nDim()
00139 {
00140   if (_ndim == -1)
00141     if ((_ndim = HDFdatasetGetnDim(_id)) < 0)
00142       throw HDFexception("Can't determine the dataset dimensions number");
00143 
00144   return _ndim;
00145 }
00146 
00147 void HDFdataset::GetDim(hdf_size dim[])
00148 {
00149   int i;
00150   int ndim;
00151   hdf_err ret;
00152 
00153   if (_dim == 0)
00154     {
00155       if (_ndim == -1)
00156        ndim = HDFdatasetGetnDim(_id);
00157       else
00158        ndim = _ndim;
00159       _dim = new hdf_size[ndim];
00160       if ((ret = HDFdatasetGetDim(_id,_dim)) < 0)
00161        throw HDFexception("Can't determine the size dimensions of the dataset ");
00162     }
00163 
00164   for (i=0;i<_ndim;i++)
00165     dim[i] = _dim[i];
00166 }
00167 
00168 hdf_size HDFdataset::GetSize()
00169 {
00170   int size_type;
00171 
00172   if (_size == -1)
00173     {
00174       if ((_size = HDFdatasetGetSize(_id)) < 0)
00175        throw HDFexception("Can't determine the size of the dataset");
00176       
00177       if (_type == HDF_NONE)
00178        if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
00179          throw HDFexception("Can't determine the size of the dataset");
00180       
00181       switch (_type)
00182        {
00183        case HDF_INT32 : 
00184          size_type = 4;
00185          break;
00186          
00187        case HDF_INT64 :
00188        case HDF_FLOAT64 :
00189          size_type = 8;
00190          break;
00191          
00192        default :
00193          size_type = 1;
00194        }
00195       _size = _size / size_type;
00196     }
00197 
00198   return _size;
00199 }
00200 
00201 hdf_byte_order HDFdataset::GetOrder()
00202 {
00203   if (_byte_order < 0 )
00204     if ((_byte_order = HDFdatasetGetOrder( _id )) < 0)
00205       throw HDFexception("Can't determine the byte order of the dataset");
00206   return _byte_order;
00207 }
00208 
00209 hdf_object_type HDFdataset::GetObjectType()
00210 {
00211   return HDF_DATASET;
00212 }
00213 
00214 
00215 int HDFdataset::nAttributes()
00216 {
00217   int nbAttrs = H5Aget_num_attrs(_id);
00218   if(nbAttrs <= 0) nbAttrs = 0;
00219   return nbAttrs; 
00220 }
00221 
00222 
00223 char* HDFdataset::GetAttributeName(unsigned idx)
00224 {
00225   int nbAttrs = nAttributes();
00226   if(nbAttrs == 0) return NULL;
00227   H5Aiterate(_id, &idx, dataset_attr, &_attribute);
00228   return _attribute;
00229 }
00230 
00231 void HDFdataset::SetArrayId(hdf_idt arrayId) {
00232   _arrayId = arrayId;
00233 }