[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fixed problem
- Subject: Re: Fixed problem
- Date: Mon, 22 Nov 1999 14:24:26 -0700 (MST)
On Sun, 21 Nov 1999, Steve Diggs wrote:
> Robb:
>
> OK, now I have another question. How the heck do I get the multi-dim CHAR
> variable back out?
>
> I can't find the proper syntax in the Perl interface to open the NetCDF
> file in your example to read in the CHAR data. Could you extend your
> last example to _specifically_ read in the CHAR array you write out? (as in
> close the file then reopen, get info, read CHAR array (all of it at once)
> into a Perl array, print array). I don't think that this would be so
> difficult. A user manual sure would help...
>
> thanks again,
> -sd
Steve,
Here's an example of reading a NetCDF file using varget.
Robb...
#!/opt/bin/perl
use NetCDF ;
$ncfile = "demo.nc" ;
$ncid = NetCDF::create($ncfile, NetCDF::CLOBBER ) ;
$dimid = NetCDF::dimdef($ncid, 'recNum', NetCDF::UNLIMITED);
$stadim = NetCDF::dimdef($ncid,"stations",10) ;
$strlen = 10 ;
$strdim = NetCDF::dimdef($ncid,"strlen", $strlen) ;
$varid = NetCDF::vardef($ncid,"station",NetCDF::CHAR,[$dimid,$strdim]);
NetCDF::endef($ncid) ;
@names = ( "abcdef", "ABCDEF","A", "B", "C", "D", "E", "F", "G", "H" ) ;
for ($i = 0 ; $i < 10 ; $i++ ) {
$names[ $i ] = padstr( $names[ $i ], $strlen ) ;
NetCDF::varput( $ncid, $varid, [$i,0], [1, $strlen], \$names[ $i ] ) ;
#NetCDF::varput( $ncid, $varid, [$i,0], [1,10], "??????????" ) ;
}
NetCDF::close($ncid) ;
# Example to reopen file and read the data
$ncid = NetCDF::open( "$ncfile", WRITE ) ;
$station_id = NetCDF::varid( $ncid, "station" ) ;
print "station_id =$station_id\n\n" ;
for ($i = 0 ; $i < 10 ; $i++ ) {
@aStn = ($strlen x "\0" ) ;
NetCDF::varget( $ncid, $station_id, [$i,0], [1, $strlen], \@aStn ) ;
$newStation = "";
for ($j = 0; $j < $strlen; $j++) {
$stnChr = chr($aStn[$j]);
last if( $stnChr eq "\0" || $stnChr eq "\\" ) ;
$newStation .= $stnChr ;
}
print "Station $i = $newStation\n" ;
}
NetCDF::close($ncid) ;
exit( 0 ) ;
# pad str to correct length
sub padstr
{
( $str, $len ) = @_ ;
my( $size, $i ) ;
$size = length( $str ) ;
for( $i = $size; $i < $len; $i++ ) {
$str .= "\0" ;
#print "$str,\n" ;
}
if( $size > $len ) {
print STDOUT "String length is over $len chars long:\n $str\n" ;
$str = substr( $str, 0, $len ) ;
#exit 0 ;
}
return $str ;
}
__END__
And this is what I get from ncdump:
ncdump demo.nc
netcdf demo {
dimensions:
recNum = UNLIMITED ; // (10 currently)
stations = 10 ;
strlen = 10 ;
variables:
char station(recNum, strlen) ;
data:
station =
"abcdef",
"ABCDEF",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H" ;
}
===============================================================================
Robb Kambic Unidata Program Center
Software Engineer III Univ. Corp for Atmospheric Research
address@hidden WWW: http://www.unidata.ucar.edu/
===============================================================================
>
> >
> > Steve,
> >
> > I was extremely busy yesterday so I couldn't remember the syntax for the
> > multidimensional arrarys, etc. I modified the the example you sent, using
> > the 1st dim as unlimited, thats what my understanding was yesterday.
> > The original example is dependent on the manner that perl stores
> > strings in arrays thats why the padstr function is used so one gets
> > what one expects. Sometime the API don't work as expected. Don't
> > ever think I will not answer a question, sometimes I need time to get to
> > the problem.
> >
> > Robb...
> >
>
===============================================================================
Robb Kambic Unidata Program Center
Software Engineer III Univ. Corp for Atmospheric Research
address@hidden WWW: http://www.unidata.ucar.edu/
===============================================================================
#!/opt/bin/perl
use NetCDF ;
$ncfile = "demo.nc" ;
$ncid = NetCDF::create($ncfile, NetCDF::CLOBBER ) ;
$dimid = NetCDF::dimdef($ncid, 'recNum', NetCDF::UNLIMITED);
$stadim = NetCDF::dimdef($ncid,"stations",10) ;
$strlen = 10 ;
$strdim = NetCDF::dimdef($ncid,"strlen", $strlen) ;
$varid = NetCDF::vardef($ncid,"station",NetCDF::CHAR,[$dimid,$strdim]);
NetCDF::endef($ncid) ;
@names = ( "abcdef", "ABCDEF","A", "B", "C", "D", "E", "F", "G", "H" ) ;
for ($i = 0 ; $i < 10 ; $i++ ) {
$names[ $i ] = padstr( $names[ $i ], $strlen ) ;
NetCDF::varput( $ncid, $varid, [$i,0], [1, $strlen], \$names[ $i ] ) ;
#NetCDF::varput( $ncid, $varid, [$i,0], [1,10], "??????????" ) ;
}
NetCDF::close($ncid) ;
# Example to reopen file and read the data
$ncid = NetCDF::open( "$ncfile", WRITE ) ;
$station_id = NetCDF::varid( $ncid, "station" ) ;
print "station_id =$station_id\n\n" ;
for ($i = 0 ; $i < 10 ; $i++ ) {
@aStn = ($strlen x "\0" ) ;
NetCDF::varget( $ncid, $station_id, [$i,0], [1, $strlen], \@aStn ) ;
$newStation = "";
for ($j = 0; $j < $strlen; $j++) {
$stnChr = chr($aStn[$j]);
last if( $stnChr eq "\0" || $stnChr eq "\\" ) ;
$newStation .= $stnChr ;
}
print "Station $i = $newStation\n" ;
}
NetCDF::close($ncid) ;
exit( 0 ) ;
# pad str to correct length
sub padstr
{
( $str, $len ) = @_ ;
my( $size, $i ) ;
$size = length( $str ) ;
for( $i = $size; $i < $len; $i++ ) {
$str .= "\0" ;
#print "$str,\n" ;
}
if( $size > $len ) {
print STDOUT "String length is over $len chars long:\n $str\n" ;
$str = substr( $str, 0, $len ) ;
#exit 0 ;
}
return $str ;
}
__END__
And this is what I get from ncdump:
ncdump demo.nc
netcdf demo {
dimensions:
recNum = UNLIMITED ; // (10 currently)
stations = 10 ;
strlen = 10 ;
variables:
char station(recNum, strlen) ;
data:
station =
"abcdef",
"ABCDEF",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H" ;
}
===============================================================================
Robb Kambic Unidata Program Center
Software Engineer III Univ. Corp for Atmospheric Research
address@hidden WWW: http://www.unidata.ucar.edu/
===============================================================================