[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Example of reading string from NetCDF with netcdf-perl
- Subject: Example of reading string from NetCDF with netcdf-perl
- Date: Tue, 4 Mar 1997 10:36:17 -0700 (MST)
#!/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, 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 < $recnum; $i++ ) {
for( $j = 0; $j < $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
==========================================================================