NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
NOTE: The decoders
mailing list is no longer active. The list archives are made available for historical reasons.
#!/opt/bin/perl use NetCDF ; $ncid = NetCDF::create("demo.nc", 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::varput( $ncid, $varid, [3,2], [2,7], \@names ) ; NetCDF::close($ncid) ;# 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 rkambic@xxxxxxxxxxxxxxxx WWW: http://www.unidata.ucar.edu/ ==============================================================================
decoders
archives: