This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Mahesh, > To: address@hidden > cc: address@hidden > From: address@hidden (Krishnan Mahesh) > Subject: Basic NetCDFperl question > Organization: . > Keywords: 199911182048.NAA16825 In the above message, you wrote: > I am a new user of netCDF perl, and have a potentially silly > question. Your question isn't silly at all. > I have an array of characters, e.g. > @names = ("this", "is", "a", "test") > > that i am trying to write to a NetCDF file. > I have tried a variety of things, none of which worked. The only > things that i did manage to get working is to have a seprate id for > each element of the array, break up each element into an array of > characters, and then varput the array. Basically, the netCDF libraray treats strings as an array of characters while Perl treats strings as scalars. This causes problems when reading and writing string variables. I found the enclosed email in the netCDF-perl archive. I hope it helps. > Krishnan Mahesh > Stanford University -------- Steve Emmerson <http://www.unidata.ucar.edu> From address@hidden Fri Nov 12 08:57:18 1999 by unidata.ucar.edu (8.8.8/8.8.8) with SMTP id IAA23133; Fri, 12 Nov 1999 08:57:17 -0700 (MST) Message-Id: <address@hidden> To: Steve Diggs <address@hidden> cc: address@hidden Subject: Re: problems with Perl/NetCDF In-reply-to: Your message of "Thu, 11 Nov 1999 16:28:20 PST." Date: Fri, 12 Nov 1999 08:57:20 -0700 From: Steve Emmerson <address@hidden> Steve, >Date: Thu, 11 Nov 1999 16:28:20 -0800 (PST) >From: Steve Diggs <address@hidden> >Organization: Scripps Institution of Oceanography >To: address@hidden >Subject: problems with Perl/NetCDF >Keywords: 199911120028.RAA27258 In the above message, you wrote: > This may be a boneheaded question,but isthere something special that I need > to do in the Perl/NetCDF interface to store and retrieve CHAR-type > variables. Basically, the Perl script has to convert the individual integral values into characters and concatenate them together. I know, it's a pain. > I'm getting the decimal values back when I retreive the variables stored as > NetCDF::CHAR. > > Any pointers to *extensive* and simplistic documentation for the > NetCDF-Perl interface would be much appreciated as would any analysis of > swhat I'm doing wrong. I've included codelets of the writing and reading > routines. BTW, ncdump correctly dumps the variables, but I can't get the > Perl script to return the ASCII version of the stored variables. > > -Steve Diggs > -- > -------------------------------------------------------------------- > Steve Diggs Voice: (619)534-1108 > Scripps Institution of Oceanography FAX : (619)534-7383 > WOCE Hydrographic Program Office/STS EMAIL: address@hidden > 9500 Gilman Drive WWW : whpo.ucsd.edu > La Jolla, CA 92093-0214 > -------------------------------------------------------------------- I'm not a Perl expert -- but one of my colleagues is. He wrote the enclosed example, which I found in the netcdf-perl support-mail archive by searching for the string "string". I believe you'll find what you're looking for near the end of the example. Please let me know if this helps. -------- Steve Emmerson <http://www.unidata.ucar.edu> --------Example: Date: Tue, 4 Mar 1997 12:42:23 -0700 (MST) From: Robb Kambic <address@hidden> To: Mitch Baltuch <address@hidden> cc: address@hidden, address@hidden Subject: again, bad for loop last time #!/usr/local/bin/perl # # Example on reading strings from a NetCDF file using netcdf-perl interface # Using a metar NetCDF file # # use NetCDF ; # process input parameters if( $#ARGV == 0 ) { $ncfile = $ARGV[ 0 ] ; } else { die "Wrong number of parameters " ; } # set error handling to verbose only $result = NetCDF::opts( VERBOSE ) ; # open or create ncfiles if( -e $ncfile ) { $ncid = NetCDF::open( "$ncfile", RDWR ) ; # get value of dimension recNum in variable $recnum $recNum_id = NetCDF::dimid( $ncid, "recNum" ) ; $name_id = "xxxxxxxx" ; $recnum = -1 ; NetCDF::diminq( $ncid, $recNum_id, $name_id, $recnum ) ; if( $recnum > 0 ) { # where to start getting info @start = ( 0 ) ; # get value of dimension stn_name_len in $stn_name_len $stn_name_len_id = NetCDF::dimid( $ncid, "stn_name_len" ) ; NetCDF::diminq( $ncid, $stn_name_len_id, $name_id, $stn_name_len ) ; # How much to read @count = ( $recnum, $stn_name_len ) ; # Initialize array to read into $StrLen = "\0" x $stn_name_len ; @STNS = ( $StrLen ) x ( $recnum ) ; # get id of variable stn_name in stn_name_id $stn_name_id = NetCDF::varid( $ncid, "stn_name" ) ; # read stations into @STNS array NetCDF::varget( $ncid, $stn_name_id, \@start, \@count, \@STNS ) ; # NetCDF treats strings as arrays, perl treats string as # scalars, so convert arrays to scalars for perl for( $i = 0; $i <= $#STNS; $i += 4 ) { for( $j = $i; $j < ( $i + $stn_name_len ); $j++ ) { $station .= chr( $STNS[ $j ] ) ; } print "Station = $station\n" ; undef( $station ) ; } } $result = NetCDF::close( $ncid ) ; } else { print "Can't find input file $ncfile\n" ; exit 0 ; } exit 0 ; _______________________________________________________________________________ Robb Kambic Unidata Program Center Software Engineer Univ. Corp for Atmospheric Research address@hidden WWW: http://www.unidata.ucar.edu/ ========================================================================== "I've seen things you people wouldn't believe... "Attack ships on fire off the shoulder of Orion. "I watched C-beams glitter in the dark near the Tannhauser gate... "All those moments will be lost in time, like tears in rain. "Time to die." movie BladeRunner ==========================================================================