[hdf-forum] Checking for existing attribute

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Nava Whiteford

Hi,

  I'm new to HDF and was wondering if someone could tell me
  if I'm doing this right. I'm using the C++ interface and I want
  to add and attribute if it doesn't exist, and increment it if it
  does.

  The code I'm using to do this is as follows:

  int experiment_count = 1;

  // Get the value of Experiment count
  Group m_root_g = m_file.get_handle().openGroup("/");

  try {
    Attribute expcount = m_root_g.openAttribute("ExperimentCount");
    expcount.read(PredType::NATIVE_INT,&experiment_count);
    experiment_count++;

    expcount.write(PredType::NATIVE_INT, &experiment_count);
    expcount.close();

  } catch (AttributeIException E) {

    // Add or increment attribute called "ExperimentCount"
    DataSpace expcount_att_space(H5S_SCALAR);
    Attribute expcount_att = m_root_g.createAttribute(H5std_string("ExperimentCount"),
                                                      PredType::NATIVE_INT,
                                                      expcount_att_space);
    expcount_att.write(PredType::NATIVE_INT, &experiment_count);
    expcount_att.close();
  }

  So, I've tried to open the attribute in a try/catch block, and if the opening fails I catch
  the exception and create a new attribute. I'm using this attribute to count the number of
  experiment groups I'm holding (as I want to name the groups Experiment1 Experiment2 etc.).

  My question is it wrapping the openAttribute in a try/catch block the correct thing to do? Or
  is there a better way. It all works correctly, but I get the following output:

HDF5-DIAG: Error detected in HDF5 (1.8.1) thread 0:
  #000: H5A.c line 539 in H5Aopen(): unable to load attribute info from object header
    major: Attribute
    minor: Unable to initialize object
  #001: H5Oattribute.c line 504 in H5O_attr_open_by_name(): can't locate attribute
    major: Attribute
    minor: Object not found

  This error makes me think that perhaps there a better way to go about this?

  Any advice appreciated.

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Nava Whiteford
That did it thanks!

On Fri, Sep 12, 2008 at 08:11:37AM -0400, Binh-Minh Ribler wrote:

> Hi,
>
> When you want to catch the exception from openAttribute and handle it
> appropriately yourself, then you'll need to turn off the error printing by
> this call Exception::dontPrint().
>
> Hope it helps!
> Binh-Minh
>
> -----Original Message-----
> From: N Whiteford [mailto:new at sgenomics.org]
> Sent: Friday, September 12, 2008 7:05 AM
> To: hdf-forum at hdfgroup.org
> Subject: [hdf-forum] Checking for existing attribute
>
>
> Hi,
>
>   I'm new to HDF and was wondering if someone could tell me
>   if I'm doing this right. I'm using the C++ interface and I want
>   to add and attribute if it doesn't exist, and increment it if it
>   does.
>
>   The code I'm using to do this is as follows:
>
>   int experiment_count = 1;
>
>   // Get the value of Experiment count
>   Group m_root_g = m_file.get_handle().openGroup("/");
>
>   try {
>     Attribute expcount = m_root_g.openAttribute("ExperimentCount");
>     expcount.read(PredType::NATIVE_INT,&experiment_count);
>     experiment_count++;
>
>     expcount.write(PredType::NATIVE_INT, &experiment_count);
>     expcount.close();
>
>   } catch (AttributeIException E) {
>
>     // Add or increment attribute called "ExperimentCount"
>     DataSpace expcount_att_space(H5S_SCALAR);
>     Attribute expcount_att =
> m_root_g.createAttribute(H5std_string("ExperimentCount"),
>                                                       PredType::NATIVE_INT,
>                                                       expcount_att_space);
>     expcount_att.write(PredType::NATIVE_INT, &experiment_count);
>     expcount_att.close();
>   }
>
>   So, I've tried to open the attribute in a try/catch block, and if the
> opening fails I catch
>   the exception and create a new attribute. I'm using this attribute to
> count the number of
>   experiment groups I'm holding (as I want to name the groups Experiment1
> Experiment2 etc.).
>
>   My question is it wrapping the openAttribute in a try/catch block the
> correct thing to do? Or
>   is there a better way. It all works correctly, but I get the following
> output:
>
> HDF5-DIAG: Error detected in HDF5 (1.8.1) thread 0:
>   #000: H5A.c line 539 in H5Aopen(): unable to load attribute info from
> object header
>     major: Attribute
>     minor: Unable to initialize object
>   #001: H5Oattribute.c line 504 in H5O_attr_open_by_name(): can't locate
> attribute
>     major: Attribute
>     minor: Object not found
>
>   This error makes me think that perhaps there a better way to go about
> this?
>
>   Any advice appreciated.
>
> ----------------------------------------------------------------------
> This mailing list is for HDF software users discussion.
> To subscribe to this list, send a message to
> hdf-forum-subscribe at hdfgroup.org.
> To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.
>
>
>
>
> --
> No virus found in this incoming message.
> Checked by AVG.
> Version: 7.5.524 / Virus Database: 270.6.21/1667 - Release Date: 9/11/2008
> 6:55 PM
>

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Quincey Koziol

On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:

> That did it thanks!

        For those using the C API interface, there are two new routines that  
perform this check:  H5Aexists() and H5Aexists_by_name().

        Quincey

> On Fri, Sep 12, 2008 at 08:11:37AM -0400, Binh-Minh Ribler wrote:
>> Hi,
>>
>> When you want to catch the exception from openAttribute and handle it
>> appropriately yourself, then you'll need to turn off the error  
>> printing by
>> this call Exception::dontPrint().
>>
>> Hope it helps!
>> Binh-Minh
>>
>> -----Original Message-----
>> From: N Whiteford [mailto:new at sgenomics.org]
>> Sent: Friday, September 12, 2008 7:05 AM
>> To: hdf-forum at hdfgroup.org
>> Subject: [hdf-forum] Checking for existing attribute
>>
>>
>> Hi,
>>
>>  I'm new to HDF and was wondering if someone could tell me
>>  if I'm doing this right. I'm using the C++ interface and I want
>>  to add and attribute if it doesn't exist, and increment it if it
>>  does.
>>
>>  The code I'm using to do this is as follows:
>>
>>  int experiment_count = 1;
>>
>>  // Get the value of Experiment count
>>  Group m_root_g = m_file.get_handle().openGroup("/");
>>
>>  try {
>>    Attribute expcount = m_root_g.openAttribute("ExperimentCount");
>>    expcount.read(PredType::NATIVE_INT,&experiment_count);
>>    experiment_count++;
>>
>>    expcount.write(PredType::NATIVE_INT, &experiment_count);
>>    expcount.close();
>>
>>  } catch (AttributeIException E) {
>>
>>    // Add or increment attribute called "ExperimentCount"
>>    DataSpace expcount_att_space(H5S_SCALAR);
>>    Attribute expcount_att =
>> m_root_g.createAttribute(H5std_string("ExperimentCount"),
>>                                                      
>> PredType::NATIVE_INT,
>>                                                      
>> expcount_att_space);
>>    expcount_att.write(PredType::NATIVE_INT, &experiment_count);
>>    expcount_att.close();
>>  }
>>
>>  So, I've tried to open the attribute in a try/catch block, and if  
>> the
>> opening fails I catch
>>  the exception and create a new attribute. I'm using this attribute  
>> to
>> count the number of
>>  experiment groups I'm holding (as I want to name the groups  
>> Experiment1
>> Experiment2 etc.).
>>
>>  My question is it wrapping the openAttribute in a try/catch block  
>> the
>> correct thing to do? Or
>>  is there a better way. It all works correctly, but I get the  
>> following
>> output:
>>
>> HDF5-DIAG: Error detected in HDF5 (1.8.1) thread 0:
>>  #000: H5A.c line 539 in H5Aopen(): unable to load attribute info  
>> from
>> object header
>>    major: Attribute
>>    minor: Unable to initialize object
>>  #001: H5Oattribute.c line 504 in H5O_attr_open_by_name(): can't  
>> locate
>> attribute
>>    major: Attribute
>>    minor: Object not found
>>
>>  This error makes me think that perhaps there a better way to go  
>> about
>> this?
>>
>>  Any advice appreciated.
>>
>> ----------------------------------------------------------------------
>> This mailing list is for HDF software users discussion.
>> To subscribe to this list, send a message to
>> hdf-forum-subscribe at hdfgroup.org.
>> To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.
>>
>>
>>
>>
>> --
>> No virus found in this incoming message.
>> Checked by AVG.
>> Version: 7.5.524 / Virus Database: 270.6.21/1667 - Release Date:  
>> 9/11/2008
>> 6:55 PM
>>
>
> ----------------------------------------------------------------------
> This mailing list is for HDF software users discussion.
> To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org
> .
> To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.
>
>


----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Francesc Alted
A Friday 12 September 2008, Quincey Koziol escrigu?:
> On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:
> > That did it thanks!
>
> For those using the C API interface, there are two new routines that
> perform this check:  H5Aexists() and H5Aexists_by_name().

Now that you mention this, in C I normally use the H5E_BEGIN_TRY /
H5E_END_TRY clause for checking for potentially non-existing objects.  
Something like:

  /* Test whether a child exists or not */
  H5E_BEGIN_TRY {
    ret = H5Gget_objinfo(loc_id, name, TRUE, &statbuf);
  } H5E_END_TRY;
  if (ret < 0)
    return -2;

Is that a recommended practice?

Thanks,

--
Francesc Alted
Freelance developer
Tel +34-964-282-249

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Dimitris Servis
Hi Francesc,

H5E_BEGIN_TRY / H5E_END_TRY just turn off/on error printing. I turn off
error printing anyway.

Regards,

-- dimitris

2008/9/12 Francesc Alted <faltet at pytables.com>

> A Friday 12 September 2008, Quincey Koziol escrigu?:
> > On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:
> > > That did it thanks!
> >
> >       For those using the C API interface, there are two new routines
> that
> > perform this check:  H5Aexists() and H5Aexists_by_name().
>
> Now that you mention this, in C I normally use the H5E_BEGIN_TRY /
> H5E_END_TRY clause for checking for potentially non-existing objects.
> Something like:
>
>  /* Test whether a child exists or not */
>  H5E_BEGIN_TRY {
>    ret = H5Gget_objinfo(loc_id, name, TRUE, &statbuf);
>  } H5E_END_TRY;
>  if (ret < 0)
>    return -2;
>
> Is that a recommended practice?
>
> Thanks,
>
> --
> Francesc Alted
> Freelance developer
> Tel +34-964-282-249
>
> ----------------------------------------------------------------------
> This mailing list is for HDF software users discussion.
> To subscribe to this list, send a message to
> hdf-forum-subscribe at hdfgroup.org.
> To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20080912/9828013c/attachment.html>

Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Francesc Alted
A Friday 12 September 2008, escrigu?reu:
> Hi Francesc,
>
> H5E_BEGIN_TRY / H5E_END_TRY just turn off/on error printing. I turn
> off error printing anyway.

Yes, this is what I thought.  But perhaps a direct call to disable
printing would be better, yes.

Thanks!

>
> Regards,
>
> -- dimitris
>
> 2008/9/12 Francesc Alted <faltet at pytables.com>
>
> > A Friday 12 September 2008, Quincey Koziol escrigu?:
> > > On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:
> > > > That did it thanks!
> > >
> > >       For those using the C API interface, there are two new
> > > routines
> >
> > that
> >
> > > perform this check:  H5Aexists() and H5Aexists_by_name().
> >
> > Now that you mention this, in C I normally use the H5E_BEGIN_TRY /
> > H5E_END_TRY clause for checking for potentially non-existing
> > objects. Something like:
> >
> >  /* Test whether a child exists or not */
> >  H5E_BEGIN_TRY {
> >    ret = H5Gget_objinfo(loc_id, name, TRUE, &statbuf);
> >  } H5E_END_TRY;
> >  if (ret < 0)
> >    return -2;
> >
> > Is that a recommended practice?
> >
> > Thanks,
> >
> > --
> > Francesc Alted
> > Freelance developer
> > Tel +34-964-282-249
> >
> > -------------------------------------------------------------------
> >--- This mailing list is for HDF software users discussion.
> > To subscribe to this list, send a message to
> > hdf-forum-subscribe at hdfgroup.org.
> > To unsubscribe, send a message to
> > hdf-forum-unsubscribe at hdfgroup.org.



--
Francesc Alted
Freelance developer
Tel +34-964-282-249

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Quincey Koziol
In reply to this post by Francesc Alted
Hi Francesc,

On Sep 12, 2008, at 11:03 AM, Francesc Alted wrote:

> A Friday 12 September 2008, Quincey Koziol escrigu?:
>> On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:
>>> That did it thanks!
>>
>> For those using the C API interface, there are two new routines that
>> perform this check:  H5Aexists() and H5Aexists_by_name().
>
> Now that you mention this, in C I normally use the H5E_BEGIN_TRY /
> H5E_END_TRY clause for checking for potentially non-existing objects.
> Something like:
>
>  /* Test whether a child exists or not */
>  H5E_BEGIN_TRY {
>    ret = H5Gget_objinfo(loc_id, name, TRUE, &statbuf);
>  } H5E_END_TRY;
>  if (ret < 0)
>    return -2;
>
> Is that a recommended practice?

        That used to be the only way to perform this sort of check, but other  
errors besides not finding the link/attribute can occur and give false  
feedback with this way of checking.  I don't recommend doing this and  
instead suggest using H5Lexists() or H5Aexists/H5Aexists_by_name with  
the 1.8.x releases.

        Quincey

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Francesc Alted
Hi Quincey,

A Monday 15 September 2008, Quincey Koziol escrigu?:

> Hi Francesc,
>
> On Sep 12, 2008, at 11:03 AM, Francesc Alted wrote:
> > A Friday 12 September 2008, Quincey Koziol escrigu?:
> >> On Sep 12, 2008, at 7:18 AM, Nava Whiteford wrote:
> >>> That did it thanks!
> >>
> >> For those using the C API interface, there are two new routines
> >> that perform this check:  H5Aexists() and H5Aexists_by_name().
> >
> > Now that you mention this, in C I normally use the H5E_BEGIN_TRY /
> > H5E_END_TRY clause for checking for potentially non-existing
> > objects. Something like:
> >
> >  /* Test whether a child exists or not */
> >  H5E_BEGIN_TRY {
> >    ret = H5Gget_objinfo(loc_id, name, TRUE, &statbuf);
> >  } H5E_END_TRY;
> >  if (ret < 0)
> >    return -2;
> >
> > Is that a recommended practice?
>
> That used to be the only way to perform this sort of check, but
> other errors besides not finding the link/attribute can occur and
> give false feedback with this way of checking.  I don't recommend
> doing this and instead suggest using H5Lexists() or
> H5Aexists/H5Aexists_by_name with the 1.8.x releases.

I see.  However, I'd like to maintain backwards compatibility with HDF5
1.6.x series for a while, so I'll stick with this for now.  I've taken
the point though, and will use the H5Xexists functions when 1.8.x would
be more spread.

Thanks for the advice,

--
Francesc Alted
Freelance developer
Tel +34-964-282-249

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Nava Whiteford
> > That used to be the only way to perform this sort of check, but
> > other errors besides not finding the link/attribute can occur and
> > give false feedback with this way of checking.  I don't recommend
> > doing this and instead suggest using H5Lexists() or
> > H5Aexists/H5Aexists_by_name with the 1.8.x releases.
>
> I see.  However, I'd like to maintain backwards compatibility with HDF5
> 1.6.x series for a while, so I'll stick with this for now.  I've taken
> the point though, and will use the H5Xexists functions when 1.8.x would
> be more spread.

Are these functions exposed though the C++ interface somewhere?

Nav


----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.




Reply | Threaded
Open this post in threaded view
|

[hdf-forum] Checking for existing attribute

Binh-Minh Ribler

Hello Nava,

>-----Original Message-----
>From: Nava Whiteford [mailto:new at sgenomics.org]
>Sent: Tuesday, September 16, 2008 4:36 AM
>To: hdf-forum at hdfgroup.org
>Subject: Re: [hdf-forum] Checking for existing attribute
>
>> > That used to be the only way to perform this sort of check, but
>> > other errors besides not finding the link/attribute can occur and
>> > give false feedback with this way of checking.  I don't recommend
>> > doing this and instead suggest using H5Lexists() or
>> > H5Aexists/H5Aexists_by_name with the 1.8.x releases.
>>
>> I see.  However, I'd like to maintain backwards compatibility with HDF5
>> 1.6.x series for a while, so I'll stick with this for now.  I've taken
>> the point though, and will use the H5Xexists functions when 1.8.x would
>> be more spread.
>
>Are these functions exposed though the C++ interface somewhere?

Currently, C++ interface doesn't have wrappers for many new 1.8 C functions
including H5Aexists/H5Lexists.  We hope to add new wrappers when funding
becomes available.

>
>Nav
>
>
>----------------------------------------------------------------------
>This mailing list is for HDF software users discussion.
>To subscribe to this list, send a message to hdf-forum-
>subscribe at hdfgroup.org.
>To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.
>
>
>
>
>--
>No virus found in this incoming message.
>Checked by AVG.
>Version: 7.5.524 / Virus Database: 270.6.21/1674 - Release Date: 9/16/2008
>8:15 AM



----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.