[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problems with Perl/NetCDF



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
==========================================================================