comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: HDF Data sets containg strings?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: HDF Data sets containg strings? [message #13302] Mon, 02 November 1998 00:00
Richard Penrose is currently offline  Richard Penrose
Messages: 4
Registered: November 1998
Junior Member
David Fanning wrote:

> Richard Penrose (rpenrose@uk.ibm.com) writes:
>
>> I would very much like to be able to append an array of three character
>> strings to an HDF file using the 'HDF_SD_ADDDATA' command. I have tried
>> to do this without any luck, and can find no documentation to help me
>> with this.
>>
>> In the end I have had to resort to appending a 3*n dimensional array,
>> i.e. 3 characters * however many dimensions are required.
>>
>> Does anyone have a solution to this problem, I would be most grateful!
>
> I'm no HDF expert by any means, but a quick look at the
> HDF chapter in my IDL Programming Techniques book suggests that
> the HDF_SD_ADDDATA command is for adding a "slab" of data.
> I understand "slab" to mean a two or three dimensional
> array which has been assigned an SDS identifier with the
> HDF_SD_Create command. For character data, I guess I would
> use a byte array. Probably something like this:
>
> strArray = StrArr(4)
> strArray[0] = 'ABC'
> strArray[1] = 'DEF'
> strArray[2] = 'GHI'
> strArray[3] = 'JKL'
> hdfFileID = HDF_SD_START('newfile.hdf')
> sdsID = HDF_SD_CREATE(hdfFileID, 'Comment Array', [3,4], /Byte)
> HDF_SD_ADDDATA, sdsID, Byte(strArray)
>
> Another alternative is to add each string to the file as a file
> attribute with the HDF_SD_AttrSet command.
>
> Cheers,
>
> David
> ----------------------------------------------------------
> David Fanning, Ph.D.
> Fanning Software Consulting
> E-Mail: davidf@dfanning.com
> Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
>
> Note: A copy of this article was e-mailed to the original poster.

David

Thanks very much for you're prompt response.

I had already tried the 'byte' method that you suggested, it just creates a 3
* n dimensional dataset with a byte in each element, I actually did this
using '/STRING' instead of '/BYTE' the result is the same but you get
characters instead of byte values in each dataset element.

I hadn't thought of the 'HDF_SD_AttrSet' work around, I think I will either
use this or just accept that I'll get a dataset element for each character.

Thanks again

Richard

P.S. I thought the newsgroup was pretty good, its the first time I've used
it.
Re: HDF Data sets containg strings? [message #13306 is a reply to message #13302] Mon, 02 November 1998 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Martin Schultz (mgs@io.harvard.edu) writes:

> prompts me to adevrtise my str2byte function that may help you getting byte
> arrays with fixed length from strings with variable length. Please find it
> attached below.

I can see some applications for Martin's Str2Byte function,
but I wanted to remind you of something I learned from Ray
Sterner a long time ago: if you have a string array
with strings of variable lengths, you can easily turn that
into a 2D byte array, in which each string is as long as
the longest string in the array by using the BYTE function.

a = StrArr(4)
a[0] = 'hello'
a[1] = 'my name is coyote'
a[2] = 'what is yours?'
a[3] = 'bye'
done = Byte(a)
Help, done
DONE BYTE = Array[17, 4]

Not only that, but they come back out properly!

Print, String(done[*,0])
hello

Print, StrLen(String(done[*,0]))
5

Cheers,

David

----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: HDF Data sets containg strings? [message #13307 is a reply to message #13302] Mon, 02 November 1998 00:00 Go to previous message
Martin Schultz is currently offline  Martin Schultz
Messages: 515
Registered: August 1997
Senior Member
David Fanning wrote:

> Richard Penrose (rpenrose@uk.ibm.com) writes:
>
>> I would very much like to be able to append an array of three character
>> strings to an HDF file using the 'HDF_SD_ADDDATA' command. I have tried
>> to do this without any luck, and can find no documentation to help me
>> with this.
>>
>> In the end I have had to resort to appending a 3*n dimensional array,
>> i.e. 3 characters * however many dimensions are required.
>>
>> Does anyone have a solution to this problem, I would be most grateful!
>
> I'm no HDF expert by any means, [...]

me neither, but what David suggests next


> [...]. For character data, I guess I would
> use a byte array. [...]

prompts me to adevrtise my str2byte function that may help you getting byte
arrays with fixed length from strings with variable length. Please find it
attached below.

Regards,
Martin.

--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Engineering&Applied Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA

phone: (617)-496-8318
fax : (617)-495-4551

e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------



; $Id: str2byte.pro,v 1.1 1998/10/09 19:53:32 mgs Exp $
;----------------------------------------------------------- --
;+
; NAME:
; STR2BYTE (function)
;
; PURPOSE:
; Convert a string into a byte vector of a given length
; for output in binary data files.
;
; CATEGORY:
; Tools
;
; CALLING SEQUENCE:
; bstr = STR2BYTE(string [,length])
;
; INPUTS:
; STRING -> The string to be converted
;
; LENGTH -> Length of the byte vector. Default is to use the
; length of the string. If LENGTH is shorter, the string
; will be truncated, if it is longer, it will be filled
; with blanks (32B).
;
; KEYWORD PARAMETERS:
; none
;
; OUTPUTS:
; A byte vector of the specified length
;
; SUBROUTINES:
;
; REQUIREMENTS:
;
; NOTES:
;
; EXAMPLE:
; ; write a 80 character string into a binary file
; openw,lun,'test.dat',/F77_UNFORMATTED,/get_lun
; writeu,lun,str2byte('Test string',80)
; free_lun,lun
;
; MODIFICATION HISTORY:
; mgs, 24 Aug 1998: VERSION 1.00
;
;-
; Copyright (C) 1998, Martin Schultz, Harvard University
; This software is provided as is without any warranty
; whatsoever. It may be freely used, copied or distributed
; for non-commercial purposes. This copyright notice must be
; kept with any copy of this software. If this software shall
; be used commercially or sold as part of a larger package,
; please contact the author to arrange payment.
; Bugs and comments should be directed to mgs@io.harvard.edu
; with subject "IDL routine str2byte"
;----------------------------------------------------------- --


function str2byte,str,len

if (n_elements(str) eq 0) then return,[0B]

; if len argument is not given, use actual string size
if (n_elements(len) eq 0) then len=strlen(str)
if (len le 0) then return,[0B]

; convert string to byte; cut if too long
bytstr = byte(strmid(str,0,len))

; make result array of desired length
result = bytarr(len)+32B ; byte array, fill with spaces

; copy string into result array
result[0:n_elements(bytstr)-1] = bytstr

return,result

end
  • Attachment: str2byte.pro
    (Size: 2.12KB, Downloaded 85 times)
Re: HDF Data sets containg strings? [message #13317 is a reply to message #13302] Sun, 01 November 1998 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Richard Penrose (rpenrose@uk.ibm.com) writes:

> I would very much like to be able to append an array of three character
> strings to an HDF file using the 'HDF_SD_ADDDATA' command. I have tried
> to do this without any luck, and can find no documentation to help me
> with this.
>
> In the end I have had to resort to appending a 3*n dimensional array,
> i.e. 3 characters * however many dimensions are required.
>
> Does anyone have a solution to this problem, I would be most grateful!

I'm no HDF expert by any means, but a quick look at the
HDF chapter in my IDL Programming Techniques book suggests that
the HDF_SD_ADDDATA command is for adding a "slab" of data.
I understand "slab" to mean a two or three dimensional
array which has been assigned an SDS identifier with the
HDF_SD_Create command. For character data, I guess I would
use a byte array. Probably something like this:

strArray = StrArr(4)
strArray[0] = 'ABC'
strArray[1] = 'DEF'
strArray[2] = 'GHI'
strArray[3] = 'JKL'
hdfFileID = HDF_SD_START('newfile.hdf')
sdsID = HDF_SD_CREATE(hdfFileID, 'Comment Array', [3,4], /Byte)
HDF_SD_ADDDATA, sdsID, Byte(strArray)

Another alternative is to add each string to the file as a file
attribute with the HDF_SD_AttrSet command.

Cheers,

David
----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/

Note: A copy of this article was e-mailed to the original poster.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Differences between ' and ", syntax error (bug?)
Next Topic: Re: Object Surface Shaded by Elevation (LONG)

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 13:21:58 PDT 2025

Total time taken to generate the page: 0.48279 seconds