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

Home » Public Forums » archive » writing fixed-length string arrays to netcdf-4
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: writing fixed-length string arrays to netcdf-4 [message #86389 is a reply to message #86348] Mon, 04 November 2013 10:21 Go to previous messageGo to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Chris O'Dell writes:

> Nice try but no cigar. I tried that but honestly they were already fixed length strings in IDL anyway (in my case, length=14). NetCDF simply doesn't care. You have to somehow tell it to make the variable in the output netCDF file be an array of fixed-length strings, ideally through the NCDF_VARDEF command. But I don't know how, and there doesn't seem to be any documentation anywhere that explains it.
>
> Like I said, you can define the netCDF variable as a 2D character array (using /CHAR in the call to NCDF_VARDEF), or as an array of variable-length strings (using the /STRING keyword in the call to NCDF_VARDEF).
>
> If you do get something to work, and get HDFView to tell you the data type in the output netCDF file is "String, length = 8" (or whatever your string length is, so long as it is > 1) please post the code to do so!

Well, I don't know. My NCDF_File object seems to do it just fine.

;----------------------------------------------------------- -----------
; Create a ncdf file with variable length and fixed length strings.
fileObj = Obj_New('ncdf_file', 'test.nc', /Clobber, /Create)
fileObj -> WriteDim, 'length', [10], OBJECT=dimObj
v1 = 'cat'
v2 = 'coyote'
v3 = 'elephant'
v4 = String(v1, Format='(A10)')
v5 = String(v2, Format='(A10)')
v6 = String(v3, Format='(A10)')
fileObj -> WriteVarDef, 'v11', 'length', DATATYPE='STRING', OBJECT=v1Obj
fileObj -> WriteVarDef, 'v22', 'length', DATATYPE='STRING', OBJECT=v2Obj
fileObj -> WriteVarDef, 'v33', 'length', DATATYPE='STRING', OBJECT=v3Obj
fileObj -> WriteVarDef, 'v44', 'length', DATATYPE='STRING', OBJECT=v4Obj
fileObj -> WriteVarDef, 'v55', 'length', DATATYPE='STRING', OBJECT=v5Obj
fileObj -> WriteVarDef, 'v66', 'length', DATATYPE='STRING', OBJECT=v6Obj

fileObj -> WriteVarData, v1Obj, v1
fileObj -> WriteVarData, v2Obj, v2
fileObj -> WriteVarData, v3Obj, v3
fileObj -> WriteVarData, v4Obj, v4
fileObj -> WriteVarData, v5Obj, v5
fileObj -> WriteVarData, v6Obj, v6

fileObj -> Sync
Obj_Destroy, fileObj


; Read the data out of the file.
fObj = Obj_New('ncdf_file', 'test.nc')
v_1 = fObj -> GetVarData('v11')
v_4 = fObj -> GetVarData('v44')
Help, v_1, v_4
v_2 = fObj -> GetVarData('v22')
v_5 = fObj -> GetVarData('v55')
Help, v_2, v_5
v_3 = fObj -> GetVarData('v33')
v_6 = fObj -> GetVarData('v66')
Help, v_3, v_6

END
;----------------------------------------------------------- -----------

Here are the results I get when I run the code above:

IDL> .go
V_1 STRING = 'cat'
V_4 STRING = ' cat'
V_2 STRING = 'coyote'
V_5 STRING = ' coyote'
V_3 STRING = 'elephant'
V_6 STRING = ' elephant'

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Difficulty in succesfully creating a runtime app
Next Topic: derivative function in IDL similar as DIFF in matlab

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

Current Time: Wed Oct 08 16:49:19 PDT 2025

Total time taken to generate the page: 0.00460 seconds